#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

typedef long long ll;

const ll MOD = 1ll << 32;

inline ll add(ll a, ll b) {
    if (a >= MOD)
        a -= MOD;
    if (b >= MOD)
        b -= MOD;
    a += b;
    if (a >= MOD)
        a -= MOD;
    return a;
}

inline ll mul(ll a, ll b) {
    a %= MOD;
    b %= MOD;
    return (a * b) % MOD;
}

ll A, B, C, D;

ll f(ll x) {
    ll foo = x;
    ll delta = add(D, mul(C, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(B, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(A, foo));
    return delta;
}

ll get(ll n, ll p) {
    ll i = p, delta = 0;
    while (i <= n) {
        delta = add(delta, n / i);
        i *= p;
    }
    return delta;
}

ll solve(ll n) {
    ll delta = 0;
    const ll SEGMENT = max(3, (int) sqrt(n) + 1);
    vector<ll> primes;
    vector<bool> is_prime(SEGMENT, 1);
    for (ll i = 2; i * i < SEGMENT; ++i)
        if (is_prime[i])
            for (ll j = i * i; j < SEGMENT; j += i)
                is_prime[j] = 0;
    for (ll i = 2; i <= min(n , SEGMENT - 1); ++i)
        if (is_prime[i]) {
            primes.push_back(i);
            delta = add(delta, mul(f(i), get(n, i)));
        }
    for (ll i = SEGMENT; i <= n; i += SEGMENT) {
        int to = min(n, i + SEGMENT - 1);
        fill(is_prime.begin(), is_prime.end(), 1);
        for (ll p : primes) {
            ll mp = (i / p) * p;
            if (mp < i)
                mp += p;
            for (ll j = mp; j <= to; j += p)
                is_prime[j - i] = 0;
        }
        for (ll j = i; j <= to; ++j)
            if (is_prime[j - i])
                delta = add(delta, mul(f(j), get(n, j)));
    }
    return delta;
}

int main() {
    ll n;
    scanf("%lld %lld %lld %lld %lld", &n, &A, &B, &C, &D);
    printf("%lld\n", solve(n));
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD = 1ll << 32;

inline ll norm(ll a) {
    return a % MOD;
}

inline ll add(ll a, ll b) {
    return norm(norm(a) + norm(b));
}

inline ll mul(ll a, ll b) {
    return norm(norm(a) * norm(b));
}

ll A, B, C, D;

ll f(ll x) {
    ll foo = x;
    ll delta = add(D, mul(C, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(B, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(A, foo));
    return delta;
}

ll get(ll n, ll p) {
    ll i = p, delta = 0;
    while (i <= n) {
        delta = add(delta, n / i);
        i *= p;
    }
    return delta;
}

ll solve(ll n) {
    ll delta = 0;
    const ll SEGMENT = max(3, (int) sqrt(n) + 1);
    vector<ll> primes;
    vector<bool> is_prime(SEGMENT, 1);
    for (ll i = 2; i * i < SEGMENT; ++i)
        if (is_prime[i])
            for (ll j = i * i; j < SEGMENT; j += i)
                is_prime[j] = 0;
    for (ll i = 2; i <= min(n , SEGMENT - 1); ++i)
        if (is_prime[i]) {
            primes.push_back(i);
            delta = add(delta, mul(f(i), get(n, i)));
        }
    for (ll i = SEGMENT; i <= n; i += SEGMENT) {
        int to = min(n, i + SEGMENT - 1);
        fill(is_prime.begin(), is_prime.end(), 1);
        for (ll p : primes) {
            ll mp = (i / p) * p;
            if (mp < i)
                mp += p;
            for (ll j = mp; j <= to; j += p)
                is_prime[j - i] = 0;
        }
        for (ll j = i; j <= to; ++j)
            if (is_prime[j - i])
                delta = add(delta, mul(f(j), get(n, j)));
    }
    return delta;
}

int main() {
    ll n;
    scanf("%lld %lld %lld %lld %lld", &n, &A, &B, &C, &D);
    printf("%lld\n", solve(n));
    return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

typedef long long ll;

const ll MOD = 1ll << 32;

inline ll norm(ll a) {
    return a % MOD;
}

inline ll add(ll a, ll b) {
    return norm(norm(a) + norm(b));
}

inline ll mul(ll a, ll b) {
    return norm(norm(a) * norm(b));
}

ll A, B, C, D;

ll f(ll x) {
    ll foo = x;
    ll delta = add(D, mul(C, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(B, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(A, foo));
    return delta;
}

ll get(ll n, ll p) {
    ll i = p, delta = 0;
    while (i <= n) {
        delta = add(delta, n / i);
        i *= p;
    }
    return delta;
}

ll solve(ll n) {
    ll delta = 0;
    const ll SEGMENT = max(3, (int) sqrt(n) + 1);
    vector<ll> primes;
    vector<bool> is_prime(SEGMENT, 1);
    for (ll i = 2; i * i < SEGMENT; ++i)
        if (is_prime[i])
            for (ll j = i * i; j < SEGMENT; j += i)
                is_prime[j] = 0;
    for (ll i = 2; i <= min(n , SEGMENT - 1); ++i)
        if (is_prime[i]) {
            primes.push_back(i);
            delta = add(delta, mul(f(i), get(n, i)));
        }
    for (ll i = SEGMENT; i <= n; i += SEGMENT) {
        int to = min(n, i + SEGMENT - 1);
        fill(is_prime.begin(), is_prime.end(), 1);
        for (ll p : primes) {
            ll mp = (i / p) * p;
            if (mp < i)
                mp += p;
            for (ll j = mp; j <= to; j += p)
                is_prime[j - i] = 0;
        }
        for (ll j = i; j <= to; ++j)
            if (is_prime[j - i])
                delta = add(delta, mul(f(j), get(n, j)));
    }
    return delta;
}

int main() {
    ll n;
    scanf("%lld %lld %lld %lld %lld", &n, &A, &B, &C, &D);
    printf("%lld\n", solve(n));
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD = 1ll << 32;

inline int norm(int a) {
    return a % MOD;
}

inline int add(int a, int b) {
    return norm(norm(a) + norm(b));
}

inline int mul(int a, int b) {
    return norm((ll) norm(a) * norm(b));
}

int A, B, C, D;

int f(int x) {
    int foo = x;
    int delta = add(D, mul(C, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(B, foo));
    foo = mul(foo, x);
    delta = add(delta, mul(A, foo));
    return delta;
}

int get(int n, int p) {
    int i = p, delta = 0;
    while (i <= n) {
        delta = add(delta, n / i);
        i *= p;
    }
    return delta;
}

int solve(int n) {
    int delta = 0;
    const int SEGMENT = max(3, (int) sqrt(n) + 1);
    vector<int> primes;
    vector<bool> is_prime(SEGMENT, 1);
    for (int i = 2; i * i < SEGMENT; ++i)
        if (is_prime[i])
            for (int j = i * i; j < SEGMENT; j += i)
                is_prime[j] = 0;
    for (int i = 2; i <= min(n , SEGMENT - 1); ++i)
        if (is_prime[i]) {
            primes.push_back(i);
            delta = add(delta, mul(f(i), get(n, i)));
        }
    for (int i = SEGMENT; i <= n; i += SEGMENT) {
        int to = min(n, i + SEGMENT - 1);
        fill(is_prime.begin(), is_prime.end(), 1);
        for (int p : primes) {
            int mp = (i / p) * p;
            if (mp < i)
                mp += p;
            for (int j = mp; j <= to; j += p)
                is_prime[j - i] = 0;
        }
        for (int j = i; j <= to; ++j)
            if (is_prime[j - i])
                delta = add(delta, mul(f(j), get(n, j)));
    }
    return delta;
}

int main() {
    int n;
    scanf("%d %d %d %d %d", &n, &A, &B, &C, &D);
    printf("%d\n", solve(n));
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cassert>
#include <queue>

#define pb push_back
#define mp make_pair

#define f first
#define s second

using namespace std;

const int N = (int) 1e6 + 7;
const int MOD = (int) 1e9 + 9;

const int TILL = 300000000;

//const int
bitset<100000005> bad;

unsigned int n, a, b, c, d;

inline unsigned int calc(const unsigned int& x) {
    unsigned int res = n;
    unsigned int ans = 0;
    while (res) {
        res /= x;
        ans += res;
    }
    ans *= (a*x*x*x+b*x*x+c*x+d);
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> a >> b >> c >> d;

    unsigned int ans = 0;
    for (int i = 5; i <= TILL; i += 6) {
        int up = (i - 5) / 3;
        int down = up + 1;
        if (!bad[up] && i <= 17320) {
            ans += calc(i);
            for (int j = i * i; j <= TILL; j += (i << 1)) {
                int idx;
                if (j % 3 == 2) idx = (j - 5) / 3;
                else if (j % 3 == 1) idx = (j - 7) / 3 + 1;
                else continue;
                bad[idx] = 1;
            }
        }
        if (!bad[down] && i + 2 <= 17320) {
            ans += calc(i + 2);
            for (int j = (i + 2) * (i + 2); j <= TILL; j += ((i + 2) << 1)) {
                int idx;
                if (j % 3 == 2) idx = (j - 5) / 3;
                else if (j % 3 == 1) idx = (j - 7) / 3 + 1;
                else continue;
                bad[idx] = 1;
            }
        }
    }
    ans += calc(2);
    ans += calc(3);
    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cassert>
#include <queue>
#include <bitset>

#define pb push_back
#define mp make_pair

#define f first
#define s second

using namespace std;

const int N = (int) 1e6 + 7;
const int MOD = (int) 1e9 + 9;

const int TILL = 300000000;

//const int
bitset<100000005> bad;

unsigned int n, a, b, c, d;

inline unsigned int calc(const unsigned int& x) {
    unsigned int res = n;
    unsigned int ans = 0;
    while (res) {
        res /= x;
        ans += res;
    }
    ans *= (a*x*x*x+b*x*x+c*x+d);
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> a >> b >> c >> d;

    unsigned int ans = 0;
    for (int i = 5; i <= TILL; i += 6) {
        int up = (i - 5) / 3;
        int down = up + 1;
        if (!bad[up] && i <= 17320) {
            ans += calc(i);
            for (int j = i * i; j <= TILL; j += (i << 1)) {
                int idx;
                if (j % 3 == 2) idx = (j - 5) / 3;
                else if (j % 3 == 1) idx = (j - 7) / 3 + 1;
                else continue;
                bad[idx] = 1;
            }
        }
        if (!bad[down] && i + 2 <= 17320) {
            ans += calc(i + 2);
            for (int j = (i + 2) * (i + 2); j <= TILL; j += ((i + 2) << 1)) {
                int idx;
                if (j % 3 == 2) idx = (j - 5) / 3;
                else if (j % 3 == 1) idx = (j - 7) / 3 + 1;
                else continue;
                bad[idx] = 1;
            }
        }
    }
    ans += calc(2);
    ans += calc(3);
    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cassert>
#include <queue>
#include <bitset>

#define pb push_back
#define mp make_pair

#define f first
#define s second

using namespace std;

const int N = (int) 1e6 + 7;
const int MOD = (int) 1e9 + 9;

const int TILL = 300000000;

//const int
bitset<100000005> bad;

unsigned int n, a, b, c, d;

inline unsigned int calc(const unsigned int& x) {
    if(x>n)return 0;
    unsigned int res = n;
    unsigned int ans = 0;
    while (res) {
        res /= x;
        ans += res;
    }
    ans *= (a*x*x*x+b*x*x+c*x+d);
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> a >> b >> c >> d;

    unsigned int ans = 0;
    for (int i = 5; i <= TILL; i += 6) {
        int up = (i - 5) / 3;
        if (!bad[up]) {
            ans += calc(i);
            if (i <= 17320) {
                for (int j = i * i; j <= TILL; j += (i << 1)) {
                    int idx;
                    if (j % 3 == 2) idx = (j - 5) / 3;
                    else if (j % 3 == 1) idx = (j - 7) / 3 + 1;
                    else continue;
                    bad[idx] = 1;
                }
            }
        }
        if (!bad[up + 1]) {
            ans += calc(i + 2);
            if (i + 2 <= 17320) {
                for (int j = (i + 2) * (i + 2); j <= TILL; j += ((i + 2) << 1)) {
                    int idx;
                    if (j % 3 == 2) idx = (j - 5) / 3;
                    else if (j % 3 == 1) idx = (j - 7) / 3 + 1;
                    else continue;
                    bad[idx] = 1;
                }
            }
        }
    }
    ans += calc(2);
    ans += calc(3);
    cout << ans;

    return 0;
}


#ifdef DEBUG
//#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ());

int rnd(int x) {
  return mrand() % x;
}

const int maxn = (int) 3e8 + 10;
const int len = maxn / 3 + 10;
bitset<len> bs;

void precalc() {
  bs.set();
  bs[0] = false;
  for (int i6 = 0; i6 * 6 < maxn; i6++) {
    for (int it = 0; it < 2; it++) {
      if (!bs[i6 * 2 + it]) {
        continue;
      }
      int x = i6 * 6 + (!it ? 1 : 5);
      if (x >= maxn) {
        break;
      }
      for (int j6 = 0; x * j6 * 6 < maxn; j6++) {
        for (int it1 = 0; it1 < 2; it1++) {
          int y = j6 * 6 + (!it1 ? 1 : 5);
          int z = x * y;
          if (y == 1 || z >= maxn) {
            continue;
          }
          bs[(z / 6) * 2 + ((z % 6) == 5)] = false;
        }
      }
    }
  }
}

int n;
unsigned int a, b, c, d;

int read() {
  if (scanf("%d%u%u%u%u", &n, &a, &b, &c, &d) < 5) {
    return false;
  }
  return true;
}

unsigned int get(unsigned int p) {
  unsigned int val = ((a * p + b) * p + c) * p + d;
  unsigned int x = n;
  unsigned int res = 0;
  while (x) {
    x /= p;
    res += x;
  }
  res *= val;
  return res;
}

void solve() {
  unsigned int res = get(2) + get(3);
  for (int i = 4; i <= n; i++) {
    if (!(i % 2) || !(i % 3) || !bs[(i / 6) * 2 + ((i % 6) == 5)]) {
      continue;
    }
    res += get(i);
  }
  printf("%u\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


#ifdef DEBUG
//#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ());

int rnd(int x) {
  return mrand() % x;
}

const int maxn = (int) 3e8 + 10;
const int len = maxn / 3 + 10;
bitset<len> bs;

void precalc() {
  bs.set();
  bs[0] = false;
  for (int i6 = 1; i6 * 6 < maxn; i6++) {
    for (int it = 0; it < 2; it++) {
      if (!bs[i6 * 2 + it]) {
        continue;
      }
      int x = i6 * 6 + (!it ? 1 : 5);
      if (x >= maxn) {
        break;
      }
      for (int j6 = 0; x * j6 * 6 < maxn; j6++) {
        for (int it1 = 0; it1 < 2; it1++) {
          int y = j6 * 6 + (!it1 ? 1 : 5);
          int z = x * y;
          if (y == 1 || z >= maxn) {
            continue;
          }
          bs[(z / 6) * 2 + ((z % 6) == 5)] = false;
        }
      }
    }
  }
}

int n;
unsigned int a, b, c, d;

int read() {
  if (scanf("%d%u%u%u%u", &n, &a, &b, &c, &d) < 5) {
    return false;
  }
  return true;
}

unsigned int get(int p) {
  unsigned int val = ((a * p + b) * p + c) * p + d;
  int x = n;
  unsigned int res = 0;
  while (x) {
    x /= p;
    res += (unsigned int) x;
  }
  res *= val;
  return res;
}

void solve() {
  unsigned int res = get(2) + get(3);
  for (int i = 4; i <= n; i++) {
    if (!(i % 2) || !(i % 3) || !bs[(i / 6) * 2 + ((i % 6) == 5)]) {
      continue;
    }
    res += get(i);
  }
  printf("%u\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


#ifdef DEBUG
//#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ());

int rnd(int x) {
  return mrand() % x;
}

const int maxn = (int) 3e8 + 10;
const int len = maxn / 3 + 10;
bitset<len> bs;

void precalc() {
  bs.set();
  bs[0] = false;
  for (int i6 = 1; i6 * 6 < maxn; i6++) {
    for (int it = 0; it < 2; it++) {
      if (!bs[i6 * 2 + it]) {
        continue;
      }
      int x = i6 * 6 + (!it ? 1 : 5);
      if (x >= maxn) {
        break;
      }
      for (int j6 = 0; x * j6 * 6 < maxn; j6++) {
        for (int it1 = 0; it1 < 2; it1++) {
          int y = j6 * 6 + (!it1 ? 1 : 5);
          int z = x * y;
          if (y == 1 || z >= maxn) {
            continue;
          }
          bs[(z / 6) * 2 + ((z % 6) == 5)] = false;
        }
      }
    }
  }
}

int n;
unsigned int a, b, c, d;

int read() {
  if (scanf("%d%u%u%u%u", &n, &a, &b, &c, &d) < 5) {
    return false;
  }
  return true;
}

unsigned int get(unsigned int p) {
  unsigned int val = ((a * p + b) * p + c) * p + d;
  unsigned int x = n;
  unsigned int res = 0;
  while (x) {
    x /= p;
    res += x;
  }
  res *= val;
  return res;
}

void solve() {
  unsigned int res = get(2) + get(3);
  for (int i = 4; i <= n; i++) {
    if (!(i % 2) || !(i % 3) || !bs[(i / 6) * 2 + ((i % 6) == 5)]) {
      continue;
    }
    res += get(i);
  }
  printf("%u\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define ll unsigned long long
#define RG register
#define REP(i,a,b) for(RG int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(RG int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
inline int read()
{
	int sum=0,p=1;char ch=getchar();
	while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
	if(ch=='-')p=-1,ch=getchar();
	while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
	return sum*p;
}

#define int unsigned int

const int maxn=2e5+20;
const int inv3=2863311531;

int prime[maxn],tot,mark[maxn];

int n,rk[maxn],A,B,C,D;
int S,id[maxn],ct;

int a[maxn],b[maxn],c[maxn],d[maxn];
int f[maxn];

inline void init()
{
	cin>>n;S=sqrt(n);
	A=read();B=read();C=read();D=read();
	REP(i,2,S)
	{
		if(!mark[i])prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=S;j++)
		{
			int k=i*prime[j];
			mark[k]=1;
			if(!(i%prime[j]))break;
		}
	}
	REP(i,1,S)b[i]=(int)((ll)i*(i+1)/2*(2*i+1)*inv3),c[i]=(int)((ll)i*(i+1)/2),d[i]=i,a[i]=c[i]*c[i],rk[++ct]=i;
	DREP(i,S-(n/S==S),1)rk[++ct]=n/i,id[i]=ct,b[ct]=(ll)rk[ct]*(rk[ct]+1)/2*(2*rk[ct]+1)*inv3,c[ct]=(ll)rk[ct]*(rk[ct]+1)/2,d[ct]=rk[ct],a[ct]=c[ct]*c[ct];
	REP(i,1,ct)a[i]--,b[i]--,c[i]--,d[i]--;

	REP(i,1,tot)
	{
		DREP(j,ct,1)if((ll)prime[i]*prime[i]<=rk[j])
		{
			ll k=rk[j]/prime[i];
			k=k<=S?k:id[n/k];
			//g[j]=(g[j]-(ll)prime[i]*(g[k]-g[prime[i]-1])%mod+mod)%mod;
			//s[j]=(s[j]-(s[k]-s[prime[i]-1]))%mod;if(s[j]<0)s[j]+=mod;
			a[j]-=prime[i]*prime[i]*prime[i]*(a[k]-a[prime[i]-1]);
			b[j]-=prime[i]*prime[i]*(b[k]-b[prime[i]-1]);
			c[j]-=prime[i]*(c[k]-c[prime[i]-1]);
			d[j]-=(d[k]-d[prime[i]-1]);
		}else break;
	}
	REP(i,1,ct)f[i]=a[i]*A+b[i]*B+c[i]*C+d[i]*D;
}

#define pri pair<int,int>
#define mkr(a,b) make_pair(a,b)
#define fi first
#define se second

inline int FF(int x){ return A*x*x*x+B*x*x+C*x+D;}

pri F(int x,int y)
{
	if(x<prime[y] || x<1)return mkr(0,0);
	int a1=f[x]-f[prime[y-1]],a4=d[x]-d[prime[y-1]];
	REP(i,y,tot)if((ll)prime[i]*prime[i]<=rk[x])
	{
		int st=prime[i];int l=1;
		while((ll)st*prime[i]<=rk[x])
		{
			int t=rk[x]/st;
			t=t<=S?t:id[n/t];
			pri x=F(t,i+1);
			a1+=x.se*l*FF(prime[i]);//pls(ans,(ll)F(t,i+1)*(prime[i]^l)%mod);
			a1+=x.fi;
			a4+=x.se;
			a1+=(l+1)*FF(prime[i]);
			a4++;

			st*=prime[i];l++;
		}
		//if(ans>=(ll)mod*1000000)ans-=(ll)mod*1000000;
	}else break;
	return mkr(a1,a4);
}

inline void doing()
{
	cout<<F(ct,1).fi<<endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
#endif
	init();
	doing();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define RG register
#define REP(i,a,b) for(RG int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(RG int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
inline int read()
{
	int sum=0,p=1;char ch=getchar();
	while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
	if(ch=='-')p=-1,ch=getchar();
	while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
	return sum*p;
}

#define int unsigned int

const int maxn=2e5+20;

int prime[maxn],tot,mark[maxn];

int n,rk[maxn],A,B,C,D;
int S,id[maxn],ct;

int a[maxn],b[maxn],c[maxn],d[maxn];
int f[maxn];

inline void init()
{
	cin>>n;S=sqrt(n);
	A=read();B=read();C=read();D=read();
	REP(i,2,S)
	{
		if(!mark[i])prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=S;j++)
		{
			int k=i*prime[j];
			mark[k]=1;
			if(!(i%prime[j]))break;
		}
	}
	REP(i,1,S)b[i]=(int)((ll)i*(i+1)*(2*i+1)/6),c[i]=(int)((ll)i*(i+1)/2),d[i]=i,a[i]=c[i]*c[i],rk[++ct]=i;
	DREP(i,S-(n/S==S),1)rk[++ct]=n/i,id[i]=ct,b[ct]=(ll)rk[ct]*(rk[ct]+1)*(2*rk[ct]+1)/6,c[ct]=(ll)rk[ct]*(rk[ct]+1)/2,d[ct]=rk[ct],a[ct]=c[ct]*c[ct];
	REP(i,1,ct)a[i]--,b[i]--,c[i]--,d[i]--;

	REP(i,1,tot)
	{
		DREP(j,ct,1)if((ll)prime[i]*prime[i]<=rk[j])
		{
			ll k=rk[j]/prime[i];
			k=k<=S?k:id[n/k];
			//g[j]=(g[j]-(ll)prime[i]*(g[k]-g[prime[i]-1])%mod+mod)%mod;
			//s[j]=(s[j]-(s[k]-s[prime[i]-1]))%mod;if(s[j]<0)s[j]+=mod;
			a[j]-=prime[i]*prime[i]*prime[i]*(a[k]-a[prime[i]-1]);
			b[j]-=prime[i]*prime[i]*(b[k]-b[prime[i]-1]);
			c[j]-=prime[i]*(c[k]-c[prime[i]-1]);
			d[j]-=(d[k]-d[prime[i]-1]);
		}else break;
	}
	REP(i,1,ct)f[i]=a[i]*A+b[i]*B+c[i]*C+d[i]*D;
}

#define pri pair<int,int>
#define mkr(a,b) make_pair(a,b)
#define fi first
#define se second

inline int FF(int x){ return A*x*x*x+B*x*x+C*x+D;}

pri F(int x,int y)
{
	if(x<prime[y] || x<1)return mkr(0,0);
	int a1=f[x]-f[prime[y-1]],a4=d[x]-d[prime[y-1]];
	REP(i,y,tot)if((ll)prime[i]*prime[i]<=rk[x])
	{
		int st=prime[i];int l=1;
		while((ll)st*prime[i]<=rk[x])
		{
			int t=rk[x]/st;
			t=t<=S?t:id[n/t];
			pri x=F(t,i+1);
			a1+=x.se*l*FF(prime[i]);//pls(ans,(ll)F(t,i+1)*(prime[i]^l)%mod);
			a1+=x.fi;
			a4+=x.se;
			a1+=(l+1)*FF(prime[i]);
			a4++;

			st*=prime[i];l++;
		}
		//if(ans>=(ll)mod*1000000)ans-=(ll)mod*1000000;
	}else break;
	return mkr(a1,a4);
}

inline void doing()
{
	cout<<F(ct,1).fi<<endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
#endif
	init();
	doing();
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define RG register
#define REP(i,a,b) for(RG int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(RG int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
inline int read()
{
	int sum=0,p=1;char ch=getchar();
	while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
	if(ch=='-')p=-1,ch=getchar();
	while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
	return sum*p;
}

#define int unsigned int

const int maxn=2e5+20;

int prime[maxn],tot,mark[maxn];

int n,rk[maxn],A,B,C,D;
int S,id[maxn],ct;

int a[maxn],b[maxn],c[maxn],d[maxn];
int f[maxn];

inline void init()
{
	cin>>n;S=sqrt(n);
	A=read();B=read();C=read();D=read();
	REP(i,2,S)
	{
		if(!mark[i])prime[++tot]=i;
		for(int j=1;j<=tot && i*prime[j]<=S;j++)
		{
			int k=i*prime[j];
			mark[k]=1;
			if(!(i%prime[j]))break;
		}
	}
	REP(i,1,S)b[i]=(int)((__int128)i*(i+1)*(2*i+1)/6),c[i]=(int)((ll)i*(i+1)/2),d[i]=i,a[i]=c[i]*c[i],rk[++ct]=i;
	DREP(i,S-(n/S==S),1)rk[++ct]=n/i,id[i]=ct,b[ct]=(__int128)rk[ct]*(rk[ct]+1)*(2*rk[ct]+1)/6,c[ct]=(ll)rk[ct]*(rk[ct]+1)/2,d[ct]=rk[ct],a[ct]=c[ct]*c[ct];
	REP(i,1,ct)a[i]--,b[i]--,c[i]--,d[i]--;

	REP(i,1,tot)
	{
		DREP(j,ct,1)if((ll)prime[i]*prime[i]<=rk[j])
		{
			ll k=rk[j]/prime[i];
			k=k<=S?k:id[n/k];
			//g[j]=(g[j]-(ll)prime[i]*(g[k]-g[prime[i]-1])%mod+mod)%mod;
			//s[j]=(s[j]-(s[k]-s[prime[i]-1]))%mod;if(s[j]<0)s[j]+=mod;
			a[j]-=prime[i]*prime[i]*prime[i]*(a[k]-a[prime[i]-1]);
			b[j]-=prime[i]*prime[i]*(b[k]-b[prime[i]-1]);
			c[j]-=prime[i]*(c[k]-c[prime[i]-1]);
			d[j]-=(d[k]-d[prime[i]-1]);
		}else break;
	}
	REP(i,1,ct)f[i]=a[i]*A+b[i]*B+c[i]*C+d[i]*D;
}

#define pri pair<int,int>
#define mkr(a,b) make_pair(a,b)
#define fi first
#define se second

inline int FF(int x){ return A*x*x*x+B*x*x+C*x+D;}

pri F(int x,int y)
{
	if(x<prime[y] || x<1)return mkr(0,0);
	int a1=f[x]-f[prime[y-1]],a4=d[x]-d[prime[y-1]];
	REP(i,y,tot)if((ll)prime[i]*prime[i]<=rk[x])
	{
		int st=prime[i];int l=1;
		while((ll)st*prime[i]<=rk[x])
		{
			int t=rk[x]/st;
			t=t<=S?t:id[n/t];
			pri x=F(t,i+1);
			a1+=x.se*l*FF(prime[i]);//pls(ans,(ll)F(t,i+1)*(prime[i]^l)%mod);
			a1+=x.fi;
			a4+=x.se;
			a1+=(l+1)*FF(prime[i]);
			a4++;

			st*=prime[i];l++;
		}
		//if(ans>=(ll)mod*1000000)ans-=(ll)mod*1000000;
	}else break;
	return mkr(a1,a4);
}

inline void doing()
{
	cout<<F(ct,1).fi<<endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
#endif
	init();
	doing();
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

unsigned int N, A, B, C, D, sum; unsigned char prime[12500009]; bool pp[100009];

unsigned int pol(unsigned int x) {
	return A * x * x * x + B * x * x + C * x + D;
}

int Get(int pos) {
	return ((prime[pos >> 3] & (1 << (pos & 7))) >> (pos & 7));
}

void init() {
	pp[1] = true;
	for (int i = 2; i*i <= 100000; i++) {
		for (int j = i*i; j <= 100000; j += i) pp[j] = true;
	}
}

int main() {
	init();
	cin >> N >> A >> B >> C >> D; int Backet = 100000000;
	for (int i = 1; i <= N; i += Backet) {
		for (int j = 0; j < Backet / 8; j++) prime[j] = 0;

		if (i == 1) prime[0] = 1;
		for (int j = 2; j*j < i + Backet; j++) {
			if (pp[i] == true) continue;
			int G = (i - 1) / j; G++;
			for (int k = max(j * j, G * j) - i; k < Backet; k += j) prime[k >> 3] |= (1 << (k & 7));
		}
		for (int j = i; j < i + Backet; j++) {
			if (Get(j - i) == 1) continue;
			unsigned int r = 0;
			unsigned int cx = N; while (cx >= 1) { cx /= j; r += cx; }
			sum += pol(j) * r;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

unsigned int N, A, B, C, D, sum; unsigned char prime[12500009];

unsigned int pol(unsigned int x) {
	return A * x * x * x + B * x * x + C * x + D;
}

int Get(int pos) {
	return ((prime[pos >> 3] & (1 << (pos & 7))) >> (pos & 7));
}

int main() {
	cin >> N >> A >> B >> C >> D; int Backet = 100000000;
	for (int i = 1; i <= N; i += Backet) {
		for (int j = 0; j < Backet / 8; j++) prime[j] = 0;

		if (i == 1) prime[0] = 1;
		for (int j = 2; j*j < i + Backet; j++) {
			int G = (i - 1) / j; G++;
			for (int k = max(j * j, G * j) - i; k < Backet; k += j) prime[k >> 3] |= (1 << (k & 7));
		}
		for (int j = i; j < i + Backet; j++) {
			if (Get(j - i) == 1) continue;
			unsigned int r = 0;
			unsigned int cx = N; while (cx >= 1) { cx /= j; r += cx; }
			sum += pol(j) * r;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

unsigned int N, A, B, C, D, sum; unsigned char prime[12500009];

unsigned int pol(unsigned int x) {
	return A * x * x * x + B * x * x + C * x + D;
}

void Set(int pos) {
	prime[pos >> 3] |= (1 << (pos & 7));
}
int Get(int pos) {
	return ((prime[pos >> 3] & (1 << (pos & 7))) >> (pos & 7));
}

int main() {
	cin >> N >> A >> B >> C >> D; int Backet = 100000000;
	for (int i = 1; i <= N; i += Backet) {
		for (int j = 0; j < Backet / 8; j++) prime[j] = 0;

		if (i == 1) Set(0);
		for (int j = 2; j*j < i + Backet; j++) {
			int G = (i - 1) / j; G++;
			for (int k = max(j * j, G * j); k < i + Backet; k += j) Set(k - i);
		}
		for (int j = i; j < i + Backet; j++) {
			if (Get(j - i) == 1) continue;
			unsigned int r = 0;
			unsigned int cx = N; while (cx >= 1) { cx /= j; r += cx; }
			sum += pol(j) * r;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

unsigned int N, A, B, C, D, sum; unsigned char prime[12500009];

unsigned int pol(unsigned int x) {
	return A * x * x * x + B * x * x + C * x + D;
}

int Get(int pos) {
	return ((prime[pos >> 3] & (1 << (pos & 7))) >> (pos & 7));
}

int main() {
	cin >> N >> A >> B >> C >> D; int Backet = 100000000;
	for (int i = 1; i <= N; i += Backet) {
		for (int j = 0; j < Backet / 8; j++) prime[j] = 0;

		if (i == 1) prime[0] = 1;
		for (int j = 2; j*j < i + Backet; j++) {
			int G = (i - 1) / j; G++;
			for (int k = max(j * j, G * j); k < i + Backet; k += j) prime[k >> 3] |= (1 << (k & 7));
		}
		for (int j = i; j < i + Backet; j++) {
			if (Get(j - i) == 1) continue;
			unsigned int r = 0;
			unsigned int cx = N; while (cx >= 1) { cx /= j; r += cx; }
			sum += pol(j) * r;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

unsigned int N, A, B, C, D, sum; unsigned char prime[12500009]; bool pp[100009];

unsigned int pol(unsigned int x) {
	return A * x * x * x + B * x * x + C * x + D;
}

int Get(int pos) {
	return ((prime[pos >> 3] & (1 << (pos & 7))) >> (pos & 7));
}

void init() {
	pp[1] = true;
	for (int i = 2; i*i <= 100000; i++) {
		for (int j = i*i; j <= 100000; j += i) pp[j] = true;
	}
}

int main() {
	init();
	cin >> N >> A >> B >> C >> D; int Backet = 100000000;
	for (int i = 1; i <= N; i += Backet) {
		for (int j = 0; j < Backet / 8; j++) prime[j] = 0;

		if (i == 1) prime[0] = 1;
		for (int j = 2; j*j < i + Backet; j++) {
			if (pp[j] == true) continue;
			int G = (i - 1) / j; G++;
			for (int k = max(j * j, G * j) - i; k < Backet; k += j) prime[k >> 3] |= (1 << (k & 7));
		}
		for (int j = i; j < i + Backet; j++) {
			if (Get(j - i) == 1) continue;
			unsigned int r = 0;
			unsigned int cx = N; while (cx >= 1) { cx /= j; r += cx; }
			sum += pol(j) * r;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

unsigned int N, A, B, C, D, sum; unsigned char prime[10000009];

unsigned int pol(unsigned int x) {
	return A * x * x * x + B * x * x + C * x + D;
}

void Set(int pos) {
	prime[pos >> 3] |= (1 << (pos & 7));
}
int Get(int pos) {
	return ((prime[pos >> 3] & (1 << (pos & 7))) >> (pos & 7));
}

int main() {
	cin >> N >> A >> B >> C >> D; int Backet = 100000000;
	for (int i = 1; i <= N; i += Backet) {
		for (int j = 0; j < Backet / 8; j++) prime[j] = 0;

		if (i == 1) Set(0);
		for (int j = 2; j*j < i + Backet; j++) {
			int G = (i - 1) / j; G++;
			for (int k = max(j * j, G * j); k < i + Backet; k += j) Set(k - i);
		}
		for (int j = i; j < i + Backet; j++) {
			if (Get(j - i) == 1) continue;
			unsigned int r = 0;
			unsigned int cx = N; while (cx >= 1) { cx /= j; r += cx; }
			sum += pol(j) * r;
		}
	}
	cout << sum << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ui unsigned int
using namespace std;
bitset<100000002>bt;
ui sm = 0;
ll n,a,b,c,d;
void func(ll x)
{
    ll p = x;
    while(p<=n)
    {
        sm+=(n/p)*(a*x*x*x+b*x*x+c*x+d);
        p*=x;
    }
}
void check(ll k)
{
    if(k<=sqrt(n+1))
    for(ll j=k*k; j<=n; j+=k+k)
    {
        if(j%6==5) bt[(j+1)/3]=1;
        else if(j%6==1) bt[(j+1)/3+1]=1;
    }
}
int main()
{
    ll i,j,k;
    cin >> n >> a >> b >> c >> d;
    func(2);func(3);
    for(i=1; ; i++)
    {
        if(i*6-1>n)break;
        if(!bt[2*i])
        {
            k = i*6-1;func(k);check(k);
        }
        if(i*6+1>n)break;
        if(!bt[2*i+1])
        {
            k = i*6+1;func(k);check(k);
        }
    }
    cout << sm << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ui unsigned int
using namespace std;
bitset<100000002>bt;
ui sm = 0;
ll n,a,b,c,d;
void func(ll x)
{
    ll p = x;
    while(p<=n)
    {
        sm+=(n/p)*(a*x*x*x+b*x*x+c*x+d);
        p*=x;
    }
}
void check(ll k)
{
    if(k<=sqrt(n+1))
    for(ll j=k*k; j<=n; j+=k+k)
    {
        if(j%6==5) bt[(j+1)/3]=1;
        else if(j%6==1) bt[(j+1)/3+1]=1;
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll i,j,k;
    cin >> n >> a >> b >> c >> d;
    func(2);func(3);
    for(i=1; ; i++)
    {
        if(i*6-1>n)break;
        if(!bt[2*i])
        {
            k = i*6-1;func(k);check(k);
        }
        if(i*6+1>n)break;
        if(!bt[2*i+1])
        {
            k = i*6+1;func(k);check(k);
        }
    }
    cout << sm << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ui unsigned int
using namespace std;
bitset<100000002>bt;
ui sm = 0;
ll n,a,b,c,d;
void func(ll x)
{
    ll p = x;
    while(p<=n)
    {
        sm+=(n/p)*(a*x*x*x+b*x*x+c*x+d);
        p*=x;
    }
}
void check(ll k)
{
    for(ll j=k*k; j*j<=n; j+=k+k)
    {
        if(j%6==5) bt[(j+1)/3]=1;
        else if(j%6==1) bt[(j+1)/3+1]=1;
    }
}
int main()
{
    ll i,j,k;
    cin >> n >> a >> b >> c >> d;
    func(2);func(3);
    for(i=1; ; i++)
    {
        if(i*6-1>n)break;
        if(!bt[2*i])
        {
            k = i*6-1;func(k);check(k);
        }
        if(i*6+1>n)break;
        if(!bt[2*i+1])
        {
            k = i*6+1;func(k);check(k);
        }
    }
    cout << sm << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ui unsigned int
using namespace std;
bitset<100000002>bt;
ui sm = 0;
ll n,a,b,c,d;
void func(ll x)
{
    ll p = x;
    while(p<=n)
    {
        sm+=(n/p)*(a*x*x*x+b*x*x+c*x+d);
        p*=x;
    }
}
void check(ll k)
{
    for(ll j=k*k; j<=n; j+=k+k)
    {
        if(j%6==5) bt[(j+1)/3]=1;
        else if(j%6==1) bt[(j+1)/3+1]=1;
    }
}
int main()
{
    ll i,j,k;
    cin >> n >> a >> b >> c >> d;
    func(2);func(3);
    for(i=1; ; i++)
    {
        if(i*6-1>n)break;
        if(!bt[2*i])
        {
            k = i*6-1;func(k);check(k);
        }
        if(i*6+1>n)break;
        if(!bt[2*i+1])
        {
            k = i*6+1;func(k);check(k);
        }
    }
    cout << sm << endl;
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 100010100
#define pp pair<int, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
#define all(s) s.begin(), s.end()
using namespace std;

const long long MOD = 4294967296;
void Add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; }

bitset<N> bs;

long long n, p[4];

long long cnt(int x, long long n) {
    if (n < x) return 0;
    return n / x + cnt(x, n / x);
}

long long f(int x) {
    long long ans = 0;
    FORD(i, 3, 0) {
        long long t = p[i];
        REP(j, i) t = (t * x) % MOD;
        Add(ans, t);
    }
    return ans;
}

int id(int x) {
    if (x % 6 == 1 || x % 6 == 5) {
        int t = (x / 6) * 2;
        if (x % 6 == 5) t++;
        return t;
    }
    return 0;
}

void Eratos() {
    long long ans = f(2) * cnt(2, n) % MOD;
    Add(ans, f(3) * cnt(3, n) % MOD);
    FOR(i, 5, n) if (i % 6 == 1 || i % 6 == 5) {
        if (!bs[id(i)]) {
             Add(ans, f(i) * cnt(i, n) % MOD);
            for (long long j = 1ll * i * i; j <= n; j += i) bs[id(j)] = 1;
        }
    }
    cout << ans;
}

int main() {
    #ifdef NERO
    freopen("test.inp","r",stdin);
    freopen("test.out","w",stdout);
    double stime = clock();
    #else
        //freopen(taskname".inp","r",stdin);
        //freopen(taskname".out","w",stdout);
    #endif //NERO
    IO;
    cin >> n;
    FORD(i, 3, 0) cin >> p[i];
    Eratos();
    #ifdef NERO
    double etime = clock();
    cerr << "Execution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
    #endif // NERO
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 100010100
#define pp pair<int, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
#define all(s) s.begin(), s.end()
using namespace std;

const long long MOD = 4294967296;
void Add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; }

bitset<N> bs;

long long n, p[4];

long long cnt(int x, long long n) {
    if (n < x) return 0;
    return n / x + cnt(x, n / x);
}

long long f(int x) {
    long long ans = 0;
    FORD(i, 3, 0) {
        long long t = p[i];
        REP(j, i) t = (t * x) % MOD;
        Add(ans, t);
    }
    return ans;
}

int id(int x) {
    if (x % 6 == 1 || x % 6 == 5) {
        int t = (x / 6) * 2;
        if (x % 6 == 5) t++;
        return t;
    }
    return 0;
}

void Eratos() {
    long long ans = f(2) * cnt(2, n);
    Add(ans, f(3) * cnt(3, n));
    FOR(i, 5, n) if (i % 6 == 1 || i % 6 == 5) {
        if (!bs[id(i)]) {
             Add(ans, f(i) * cnt(i, n));
            for (long long j = 1ll * i * i; j <= n; j += i) bs[id(j)] = 1;
        }
    }
    cout << ans;
}

int main() {
    #ifdef NERO
    freopen("test.inp","r",stdin);
    freopen("test.out","w",stdout);
    double stime = clock();
    #else
        //freopen(taskname".inp","r",stdin);
        //freopen(taskname".out","w",stdout);
    #endif //NERO
    IO;
    cin >> n;
    FORD(i, 3, 0) cin >> p[i];
    Eratos();
    #ifdef NERO
    double etime = clock();
    cerr << "Execution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
    #endif // NERO
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 100010100
#define pp pair<int, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
#define all(s) s.begin(), s.end()
using namespace std;

const long long MOD = 4294967296;
void Add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; }

bitset<N> bs;

long long n, p[4];

long long cnt(int x, long long n) {
    if (n < x) return 0;
    return n / x + cnt(x, n / x);
}

long long f(int x) {
    long long ans = 0;
    FORD(i, 3, 0) {
        long long t = p[i];
        REP(j, i) t = (t * x) % MOD;
        Add(ans, t);
    }
    return ans;
}

int id(int x) {
    if (x % 6 == 1 || x % 6 == 5) {
        int t = (x / 6) * 2;
        if (x % 6 == 6) t++;
        return t;
    }
    return 0;
}

void Eratos() {
    long long ans = f(2) * cnt(2, n);
    Add(ans, f(3) * cnt(3, n));
    FOR(i, 5, N - 1) if (i % 6 == 1 || i % 6 == 5) {
        if (!bs[id(i)]) {
            Add(ans, f(i) * cnt(i, n));
            for (long long j = 1ll * i * i; j < N; j += i) bs[id(j)] = 1;
        }
    }
    cout << ans;
}

int main() {
    #ifdef NERO
    freopen("test.inp","r",stdin);
    freopen("test.out","w",stdout);
    double stime = clock();
    #else
        //freopen(taskname".inp","r",stdin);
        //freopen(taskname".out","w",stdout);
    #endif //NERO
    IO;
    cin >> n;
    FORD(i, 3, 0) cin >> p[i];
    Eratos();
    #ifdef NERO
    double etime = clock();
    cerr << "Execution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
    #endif // NERO
}


#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

using bset = uint64_t;
using val = uint32_t;

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N, MX = 300000000;
	val coef[4];
	cin >> N >> coef[0] >> coef[1] >> coef[2] >> coef[3];
	vector<bset> is_prime(MX/3/64+1, 0);
	// small primes
	val ans = 0;
	for(int p = 2; p*p <= MX; p++) {
		bool ok = true;
		for(int i = 2; i*i <= p; i++) if(p%i == 0) {
			ok = false;
			break;
		}
		if(!ok) continue;
		if(p <= N) {
			val cnt = 0;
			int n = N;
			while(n > 0) {
				cnt += n/p;
				n /= p;
			}
			val cur = 0;
			for(int i = 0; i < 4; i++) cur = cur * p + coef[i];
			ans += cur * cnt;
		}
		if(p > 3) {
			int pos = p;
			while((pos-p)%6 != 0) pos++;
			for(int prod = pos*p/6; pos <= MX/p; pos += 6, prod += p) is_prime[prod>>5] |= 1ULL<<((prod&31)<<1);
			int neg = p;
			while((neg+p)%6 != 0) neg++;
			for(int prod = neg*p/6; neg <= MX/p; neg += 6, prod += p) is_prime[prod>>5] |= 1ULL<<((prod&31)*2+1);
		}
	}
	for(int k = 0; k <= MX/3; k++) if(((is_prime[k>>6]>>(k&63))&1) == 0) {
		int p = (k/2)*6 + ((k&1) ? 5 : 1);
		if(1LL*p*p <= MX || p > N) continue;
		val cnt = 0;
		int n = N;
		while(n > 0) {
			cnt += n/p;
			n /= p;
		}
		val cur = 0;
		for(int i = 0; i < 4; i++) cur = cur * p + coef[i];
		ans += cur * cnt;
	}
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing


#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

using bset = uint64_t;
using val = uint32_t;

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N, MX = 300000000;
	val coef[4];
	cin >> N >> coef[0] >> coef[1] >> coef[2] >> coef[3];
	vector<bset> is_prime(MX/3/64+1, -1);
	// small primes
	val ans = 0;
	for(int p = 2; p*p <= MX; p++) {
		bool ok = true;
		for(int i = 2; i*i <= p; i++) if(p%i == 0) {
			ok = false;
			break;
		}
		if(!ok) continue;
		if(p <= N) {
			val cnt = 0;
			int n = N;
			while(n > 0) {
				cnt += n/p;
				n /= p;
			}
			val cur = 0;
			for(int i = 0; i < 4; i++) cur = cur * p + coef[i];
			ans += cur * cnt;
		}
		if(p > 3) {
			int pos = p;
			while((pos-p)%6 != 0) pos++;
			for(int prod = pos*p/6; pos <= MX/p; pos += 6, prod += p) is_prime[prod>>5] |= 1ULL<<((prod&31)<<1);
			int neg = p;
			while((neg+p)%6 != 0) neg++;
			for(int prod = neg*p/6; neg <= MX/p; neg += 6, prod += p) is_prime[prod>>5] |= 1ULL<<((prod&31)*2+1);
		}
	}
	for(int k = 0; k <= MX/3; k++) if((is_prime[k>>6]>>(k&63))&1) {
		int p = (k/2)*6 + ((k&1) ? 5 : 1);
		if(1LL*p*p <= MX || p > N) continue;
		val cur = 0;
		for(int i = 0; i < 4; i++) cur = cur * coef[i] + p;
		ans += cur;
	}
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

using bset = uint64_t;
using val = uint32_t;

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N, MX = 300000000;
	val coef[4];
	cin >> N >> coef[0] >> coef[1] >> coef[2] >> coef[3];
	vector<bset> is_prime(MX/3/64+1, 0);
	// small primes
	val ans = 0;
	for(int p = 2; p*p <= MX; p++) {
		bool ok = true;
		for(int i = 2; i*i <= p; i++) if(p%i == 0) {
			ok = false;
			break;
		}
		if(!ok) continue;
		if(p <= N) {
			val cnt = 0;
			int n = N;
			while(n > 0) {
				cnt += n/p;
				n /= p;
			}
			val cur = 0;
			for(int i = 0; i < 4; i++) cur = cur * p + coef[i];
			ans += cur * cnt;
		}
		if(p > 3) {
			int pos = p;
			while((pos-p)%6 != 0) pos++;
			for(int prod = pos*p/6; pos <= MX/p; pos += 6, prod += p) is_prime[prod>>5] |= 1ULL<<((prod&31)<<1);
			int neg = p;
			while((neg+p)%6 != 0) neg++;
			for(int prod = neg*p/6; neg <= MX/p; neg += 6, prod += p) is_prime[prod>>5] |= 1ULL<<((prod&31)*2+1);
		}
	}
	for(int k = 0; k <= MX/3; k++) if(((is_prime[k>>6]>>(k&63))&1) == 0) {
		int p = (k/2)*6 + ((k&1) ? 5 : 1);
		if(1LL*p*p <= MX || p > N) continue;
		val cnt = 0;
		int n = N;
		while(n > 0) {
			cnt += n/p;
			n /= p;
		}
		val cur = 0;
		for(int i = 0; i < 4; i++) cur = cur * p + coef[i];
		ans += cur * cnt;
	}
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing


#include<bits/stdc++.h>
using namespace std;

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define Debug1(a)           cout<<#a<<"="<<(a)<<"\n"
#define Debug2(a,b)         cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define Debug3(a,b,c)       cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define Debug4(a,b,c,d)     cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define Debug5(a,b,c,d,e)   cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define Debug6(a,b,c,d,e,f) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define Debug(...) macro_dispatcher(Debug, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"

#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)
#define all(x) x.begin(), x.end()
#define geti1(X) cin >> X
#define geti2(X,Y) cin >> X >> Y
#define geti3(X,Y,Z) cin >> X >> Y >> Z
#define geti4(X,Y,Z,W) cin >> X >> Y >> Z >> W
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define geti(...) GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1) (__VA_ARGS__)

#define INF 987654321

#define endl '\n'

// calcul a^n%mod
ll power(ll a, ll n, ll mod)
{
    ll power = a;
    ll result = 1;

    while (n)
    {
        if (n & 1)
            result = (result * power) % mod;
        power = (power * power) % mod;
        n >>= 1;
    }
    return result;
}

// n−1 = 2^s * d with d odd by factoring powers of 2 from n−1
bool witness(ll n, ll s, ll d, ll a)
{
    ll x = power(a, d, n);
    ll y;

    while (s) {
        y = (x * x) % n;
        if (y == 1 && x != 1 && x != n-1)
            return false;
        x = y;
        --s;
    }
    if (y != 1)
        return false;
    return true;
}

/*
 * if n < 1,373,653, it is enough to test a = 2 and 3;
 * if n < 9,080,191, it is enough to test a = 31 and 73;
 * if n < 4,759,123,141, it is enough to test a = 2, 7, and 61;
 * if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803;
 * if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11;
 * if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13;
 * if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17.
 */

bool is_prime_mr(ll n)
{
    if (((!(n & 1)) && n != 2 ) || (n < 2) || (n % 3 == 0 && n != 3))
        return false;
    if (n <= 3)
        return true;

    ll d = n / 2;
    ll s = 1;
    while (!(d & 1)) {
        d /= 2;
        ++s;
    }

    if (n < 1373653)
        return witness(n, s, d, 2) && witness(n, s, d, 3);
    if (n < 9080191)
        return witness(n, s, d, 31) && witness(n, s, d, 73);
    if (n < 4759123141)
        return witness(n, s, d, 2) && witness(n, s, d, 7) && witness(n, s, d, 61);
    if (n < 1122004669633)
        return witness(n, s, d, 2) && witness(n, s, d, 13) && witness(n, s, d, 23) && witness(n, s, d, 1662803);
    if (n < 2152302898747)
        return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11);
    if (n < 3474749660383)
        return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13);
    return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13) && witness(n, s, d, 17);
}

bitset<100000010> bset;
unsigned int N,A,B,C,D;
int main(){
	ios::sync_with_stdio(false); cin.tie(NULL);
	geti(N); geti(A,B,C,D);

	vector<unsigned int> pv;
	for(int i=2;i<=20000;i++){
		if( is_prime_mr(i) ) pv.pb(i);
	}

	unsigned int ans = 0;
	for(unsigned int base = 0;base<=200000000;base+=100000000){
		bset.set();
		for(auto p : pv){
			unsigned int t = base/p;
			unsigned int cur = t * p;
			while( cur < base ) cur += p;
			while( cur <= base+100000000 ) {

				if( base == 0 && cur == p ){
					cur += p; continue;
				}
				bset[cur-base] = 0;
				cur += p;
			}
		}

		for(unsigned int i=0;i<=100000000;i++){
			if( !bset[i] ) continue;
			unsigned int p = i+base;
			if( p == 0 || p == 1 ) continue;
			unsigned int x1 = p; unsigned int x2 = (p*p);
			unsigned int x3 = (x2*p);
			unsigned int f = (x3*A) + (x2*B) + (x1*C) + D;
			unsigned Y = N;

			while( Y ){
				ans = ans + ((Y/p)*f);
				Y /= p;
			}
		}
	}

	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define Debug1(a)           cout<<#a<<"="<<(a)<<"\n"
#define Debug2(a,b)         cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define Debug3(a,b,c)       cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define Debug4(a,b,c,d)     cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define Debug5(a,b,c,d,e)   cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define Debug6(a,b,c,d,e,f) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define Debug(...) macro_dispatcher(Debug, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"

#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)
#define all(x) x.begin(), x.end()
#define geti1(X) cin >> X
#define geti2(X,Y) cin >> X >> Y
#define geti3(X,Y,Z) cin >> X >> Y >> Z
#define geti4(X,Y,Z,W) cin >> X >> Y >> Z >> W
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define geti(...) GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1) (__VA_ARGS__)

#define INF 987654321

#define endl '\n'

// calcul a^n%mod
ll power(ll a, ll n, ll mod)
{
    ll power = a;
    ll result = 1;

    while (n)
    {
        if (n & 1)
            result = (result * power) % mod;
        power = (power * power) % mod;
        n >>= 1;
    }
    return result;
}

// n−1 = 2^s * d with d odd by factoring powers of 2 from n−1
bool witness(ll n, ll s, ll d, ll a)
{
    ll x = power(a, d, n);
    ll y;

    while (s) {
        y = (x * x) % n;
        if (y == 1 && x != 1 && x != n-1)
            return false;
        x = y;
        --s;
    }
    if (y != 1)
        return false;
    return true;
}

/*
 * if n < 1,373,653, it is enough to test a = 2 and 3;
 * if n < 9,080,191, it is enough to test a = 31 and 73;
 * if n < 4,759,123,141, it is enough to test a = 2, 7, and 61;
 * if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803;
 * if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11;
 * if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13;
 * if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17.
 */

bool is_prime_mr(ll n)
{
    if (((!(n & 1)) && n != 2 ) || (n < 2) || (n % 3 == 0 && n != 3))
        return false;
    if (n <= 3)
        return true;

    ll d = n / 2;
    ll s = 1;
    while (!(d & 1)) {
        d /= 2;
        ++s;
    }

    if (n < 1373653)
        return witness(n, s, d, 2) && witness(n, s, d, 3);
    if (n < 9080191)
        return witness(n, s, d, 31) && witness(n, s, d, 73);
    if (n < 4759123141)
        return witness(n, s, d, 2) && witness(n, s, d, 7) && witness(n, s, d, 61);
    if (n < 1122004669633)
        return witness(n, s, d, 2) && witness(n, s, d, 13) && witness(n, s, d, 23) && witness(n, s, d, 1662803);
    if (n < 2152302898747)
        return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11);
    if (n < 3474749660383)
        return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13);
    return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13) && witness(n, s, d, 17);
}

ll N,A,B,C,D;
int main(){
	ios::sync_with_stdio(false); cin.tie(NULL);
	geti(N); geti(A,B,C,D);

	ll ans = 0;
	ll mask = (1LL<<32LL)-1LL;
	for(ll i=2;i<=N;i++){
		if( is_prime_mr(i) == false ) continue;
		ll x1 = i; ll x2 = (i*i)&mask;
		ll x3 = (x2*i)&mask;
		ll f = ((x3*A)&mask) + ((x2*B)&mask) + ((x1*C)&mask) + D;
		f &= mask;

		ll Y = N;
		while( Y ){
			ans = (ans + ((Y/i)*f)&mask )&mask;
			Y /= i;
		}
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define Debug1(a)           cout<<#a<<"="<<(a)<<"\n"
#define Debug2(a,b)         cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define Debug3(a,b,c)       cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define Debug4(a,b,c,d)     cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define Debug5(a,b,c,d,e)   cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define Debug6(a,b,c,d,e,f) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define Debug(...) macro_dispatcher(Debug, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"

#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)
#define all(x) x.begin(), x.end()
#define geti1(X) cin >> X
#define geti2(X,Y) cin >> X >> Y
#define geti3(X,Y,Z) cin >> X >> Y >> Z
#define geti4(X,Y,Z,W) cin >> X >> Y >> Z >> W
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define geti(...) GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1) (__VA_ARGS__)

#define INF 987654321

#define endl '\n'

// calcul a^n%mod
ll power(ll a, ll n, ll mod)
{
    ll power = a;
    ll result = 1;

    while (n)
    {
        if (n & 1)
            result = (result * power) % mod;
        power = (power * power) % mod;
        n >>= 1;
    }
    return result;
}

// n−1 = 2^s * d with d odd by factoring powers of 2 from n−1
bool witness(ll n, ll s, ll d, ll a)
{
    ll x = power(a, d, n);
    ll y;

    while (s) {
        y = (x * x) % n;
        if (y == 1 && x != 1 && x != n-1)
            return false;
        x = y;
        --s;
    }
    if (y != 1)
        return false;
    return true;
}

/*
 * if n < 1,373,653, it is enough to test a = 2 and 3;
 * if n < 9,080,191, it is enough to test a = 31 and 73;
 * if n < 4,759,123,141, it is enough to test a = 2, 7, and 61;
 * if n < 1,122,004,669,633, it is enough to test a = 2, 13, 23, and 1662803;
 * if n < 2,152,302,898,747, it is enough to test a = 2, 3, 5, 7, and 11;
 * if n < 3,474,749,660,383, it is enough to test a = 2, 3, 5, 7, 11, and 13;
 * if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17.
 */

bool is_prime_mr(ll n)
{
    if (((!(n & 1)) && n != 2 ) || (n < 2) || (n % 3 == 0 && n != 3))
        return false;
    if (n <= 3)
        return true;

    ll d = n / 2;
    ll s = 1;
    while (!(d & 1)) {
        d /= 2;
        ++s;
    }

    if (n < 1373653)
        return witness(n, s, d, 2) && witness(n, s, d, 3);
    if (n < 9080191)
        return witness(n, s, d, 31) && witness(n, s, d, 73);
    if (n < 4759123141)
        return witness(n, s, d, 2) && witness(n, s, d, 7) && witness(n, s, d, 61);
    if (n < 1122004669633)
        return witness(n, s, d, 2) && witness(n, s, d, 13) && witness(n, s, d, 23) && witness(n, s, d, 1662803);
    if (n < 2152302898747)
        return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11);
    if (n < 3474749660383)
        return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13);
    return witness(n, s, d, 2) && witness(n, s, d, 3) && witness(n, s, d, 5) && witness(n, s, d, 7) && witness(n, s, d, 11) && witness(n, s, d, 13) && witness(n, s, d, 17);
}

bitset<100000010> bset;
unsigned int N,A,B,C,D;
int main(){
	ios::sync_with_stdio(false); cin.tie(NULL);
	geti(N); geti(A,B,C,D);

	vector<unsigned int> pv;
	for(int i=2;i<=20000;i++){
		if( is_prime_mr(i) ) pv.pb(i);
	}

	unsigned int ans = 0;
	for(unsigned int base = 0;base<=200000000;base+=100000000){
		bset.set();
		for(auto p : pv){
			unsigned int t = base/p;
			unsigned int cur = t * p;
			while( cur < base ) cur += p;
			while( cur < base+100000000 ) {

				if( base == 0 && cur == p ){
					cur += p; continue;
				}
				bset[cur-base] = 0;
				cur += p;
			}
		}
		int cnt = 500;
		//freopen("output.txt","w",stdout);
		for(unsigned int i=0;i<=100000000;i++){
			if( !bset[i] ) continue;
			unsigned int p = i+base;
			if( p == 0 || p == 1 ) continue;
			unsigned int x1 = p; unsigned int x2 = (p*p);
			unsigned int x3 = (x2*p);
			unsigned int f = (x3*A) + (x2*B) + (x1*C) + D;
			unsigned Y = N;

			while( Y ){
				ans = ans + ((Y/p)*f);
				Y /= p;
			}
		}
	}

	cout << ans << endl;
}


#include <bits/stdc++.H>
using namespace std;

const int MaxN = 3e8 + 6.66;
const int Max = 2e4 + 6.66;
bool mark[Max];
vector<int> primes;

void prep() {
  for(int i = 2; i < Max; i++) {
    if(mark[i]) continue;
    primes.push_back(i);
    for(int j = i * i; j < Max; j += i)
      mark[j] = true;
  }
  for(int block = 1; block < MaxN / Max; block++) {
    int st = block * Max;
    int en = block + st;
    memset(mark, 0, sizeof mark);
    for(auto x : primes) {
      if(x * x > en) break;
      for(int i = x * ((st + x - 1) / x); i < en; i += x)
        mark[i - st] = true;
    }
    for(int i = st; i < en; i++)
      if(!mark[i - st]) primes.push_back(i);
  }
}

int main() {
  int n; cin >> n;
  unsigned int A, B, C, D; cin >> A >> B >> C >> D;
  prep();
  unsigned int ans = 0;
  for(auto x : primes) {
    unsigned int val = 1u * A * x * x * x + 1u * B * x * x + 1u * C * x + 1u * D;
    val;
    unsigned int cnt = 0;
    long long c = x;
    while(c <= n) {
      cnt += n / c;
      c *= x;
    }
    ans += val * cnt;
  }
  printf("%d\n", ans);
  return 0;
}


using namespace std;
#include<deque>
#include<algorithm>
#include<stdint.h>
#include<set>
#include<stack>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#include<cstdlib>
#include<ctime>
#include<list>
#define sqr(x) (x)*(x)
#define sort stable_sort
#define mp make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mp(mp(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("input.txt");fpo("output.txt");
#define fast ios::sync_with_stdio(false);
void readi(int &x)
{
	int v=0,f=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-1; else v=v*10+c-'0';
	while (isdigit(c=getchar())) v=v*10+c-'0';
	x=v*f;
}
void readc(char &x)
{
	char c;
	while ((c=getchar())==' ');
	x=c;
}
void writes(string s){puts(s.c_str());}
void writeln(){writes("");}
void writei(int x)
{
	if (!x) putchar('0');
	char a[25];
	int top=0;
	while (x)
	{
		a[++top]=(x%10)+'0';
		x/=10;
	}
	while (top)
	{
		putchar(a[top]);
		top--;
	}
}
/*************************************************/
map<int,int> mx,fx;
set<int> ax;
int n,m,i,j,ll[1111111],rr[1111111],cx,cnt[2222222],l,r;
vector<pair<int,int> > ans;
int main()
{
	readi(n);
	readi(m);
	for (i=1;i<=n;i++)
	{
		readi(ll[i]);
		readi(rr[i]);
		ax.in(ll[i]*2);
		ax.in(rr[i]*2+1);
	}
	for (set<int>::iterator it=ax.begin();it!=ax.end();it++)
	{
		mx[*it]=++cx;
		fx[cx]=*it/2;
	}
	for (i=1;i<=n;i++)
	{
		ll[i]=mx[ll[i]*2];
		rr[i]=mx[rr[i]*2+1];
	}
//	for (i=1;i<=n;i++) cout<<ll[i]<<' '<<rr[i]<<endl;
	for (i=1;i<=n;i++)
	{
		cnt[ll[i]]++;cnt[rr[i]]--;
	}
	for (i=1;i<=cx;i++)
	{
		cnt[i]+=cnt[i-1];
		if (cnt[i]>=m)
		{
			if (l==0) l=r=i;
			else if (r==i-1) r++;
		}
		else
		{
			if (l) ans.push_back(mp(fx[l],fx[i]));
			l=r=0;
		}
	}
	/*if (l)
	{
		ans.push_back(mp(fx[l],fx[r]));
	}*/
	sort(all(ans));
	printf("%d\n",ans.size());
	for (i=0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].fi,ans[i].se);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std ;
long long l[1000001] , r[1000001] , a[1000003] , b,  i , s , n , k , t , no[1000002] , z  , la = 1 , z1;
map<long long , long long>us , us1;
vector< pair<long long , long long> >v,x;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    cin>>n>>k;
    for(i = 1 ; i <= 2 * n ; i ++)
    {
        cin>>a[i];
        if(i % 2 == 1)
            v.push_back({a[i] , -1});
            else v.push_back({a[i] , 1});
    }
    sort(v.begin() , v.end());
    for(i = 0 ; i < v.size() ; i ++)
    {
     if(v[i].second == -1)
        s ++ ;
     if(s >= k && z == 0)
     {
         b = v[i].first;
         z = 1 ;
         la = 0 ;
     }
     if(v[i].second == 1)
        s -- ;
     if(s < k && la == 0)
     {
        la = 1 ;
        z = 0 ;
        z1 = v[i].first ;
        x.push_back({b , z1});
     }
    }
    cout<<x.size()<<endl;
    for(i = 0 ; i < x.size() ; i ++)
    {
        cout<<x[i].first<<" "<<x[i].second<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std ;
long long s , i , k , a , x  = 0, la , p , z = 1 , t , n ;
vector<pair<long long , long long> >v , v1;
int main()
{
    cin>>n>>k;
    n = 2 * n ;
    for(i = 1 ; i <= n ; i ++)
    {
        cin>>a;
        if(i % 2 == 1)
        v.push_back({a , -1});
        else v.push_back({a , 1});
    }
    sort(v.begin() , v.end());
    for(i = 0 ; i < v.size() ; i ++)
    {
        if(v[i].second == -1){s++;
        if(s >= k && x == 0)
        {
            p = v[i].first ;
            x = 1 ;
            z = 0 ;
        }
        }
        else{
        s -- ;
        if(s < k && z == 0)
        {
            v1.push_back({p , v[i].first});
            z = 1 ;
            x = 0 ;
            t ++ ;
        }
        }
    }
    cout<<t<<endl;
    for(i = 0 ; i < v1.size() ; i ++)
    {
        cout<<v1[i].first<<" "<<v1[i].second<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std ;
long long s , i , k , a , x  = 0, la , p , z = 1 , t , n ;
vector<pair<long long , long long> >v , v1;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    cin>>n>>k;
    n = 2 * n ;
    for(i = 1 ; i <= n ; i ++)
    {
        cin>>a;
        if(i % 2 == 1)
        v.push_back({a , -1});
        else v.push_back({a , 1});
    }
    sort(v.begin() , v.end());
    for(i = 0 ; i < v.size() ; i ++)
    {
        if(v[i].second == -1){s++;
        if(s >= k && x == 0)
        {
            p = v[i].first ;
            x = 1 ;
            z = 0 ;
        }
        }
        else{
        s -- ;
        if(s < k && z == 0)
        {
            v1.push_back({p , v[i].first});
            z = 1 ;
            x = 0 ;
            t ++ ;
        }
        }
    }
    cout<<t<<endl;
    for(i = 0 ; i < v1.size() ; i ++)
    {
        cout<<v1[i].first<<" "<<v1[i].second<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MN=2e6+200;
int points[MN];
map<int,int> point;
map<int,int> point2;
int main()
{
    ios::sync_with_stdio(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int l,r;
        cin>>l>>r;
        point[l]++;
        point2[r]--;
        points[2*i]=l;
        points[2*i+1]=r;
    }
    sort(points,points+2*n);
    int cur=0;
    bool flag=0;
    int ans=0;
    for(int i=0;i<2*n;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            flag=1;
            ans++;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            flag=0;
        }
    }
    cout<<ans<<endl;
    cur=0;flag=0;
    for(int i=0;i<2*n;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            cout<<points[i]<<" ";
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            cout<<points[i]<<endl;
            flag=0;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MN=2e6+200;
int points[MN];
map<int,int> point;
map<int,int> point2;
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        int l,r;
        scanf("%d%d",*l,&r);
        point[l]++;
        point2[r]--;
        points[2*i]=l;
        points[2*i+1]=r;
    }
    sort(points,points+2*n);
    int sz=unique(points,points+2*n)-points;
    int cur=0,ans=0;
    bool flag=0;
    for(int i=0;i<sz;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            flag=0;
            ans++;
        }
    }
    print("%d\n",ans);
    cur=0;flag=0;
    for(int i=0;i<sz;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            printf("%d ",poinst[i]);
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            printf("%d\n",poinst[i]);
            flag=0;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MN=2e6+200;
int points[MN];
map<int,int> point;
map<int,int> point2;
int main()
{
    ios::sync_with_stdio(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int l,r;
        cin>>l>>r;
        point[l]++;
        point2[r]--;
        points[2*i]=l;
        points[2*i+1]=r;
    }
    points[2*n]=1e9+100;
    sort(points,points+2*n+1);
    int cur=0;
    bool flag=0;
    int ans=0;
    for(int i=0;i<=2*n;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            flag=1;
            ans++;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            flag=0;
        }
    }
    cout<<ans<<endl;
    for(int i=0;i<=2*n;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            cout<<points[i]<<" ";
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            cout<<points[i]<<endl;
            flag=0;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MN=2e6+200;
int points[MN];
map<int,int> point;
map<int,int> point2;
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        int l,r;
        scanf("%d%d",&l,&r);
        point[l]++;
        point2[r]--;
        points[2*i]=l;
        points[2*i+1]=r;
    }
    sort(points,points+2*n);
    int sz=unique(points,points+2*n)-points;
    int cur=0,ans=0;
    bool flag=0;
    for(int i=0;i<sz;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            flag=0;
            ans++;
        }
    }
    printf("%d\n",ans);
    cur=0;flag=0;
    for(int i=0;i<sz;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            printf("%d ",points[i]);
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            printf("%d\n",points[i]);
            flag=0;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MN=2e6+200;
int points[MN];
map<int,int> point;
map<int,int> point2;
int main()
{
    ios::sync_with_stdio(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int l,r;
        cin>>l>>r;
        point[l]++;
        point2[r]--;
        points[2*i]=l;
        points[2*i+1]=r;
    }
    sort(points,points+2*n);
    int sz=unique(points,points+2*n)-points;
    int cur=0,ans=0;
    bool flag=0;
    for(int i=0;i<sz;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            flag=0;
            ans++;
        }
    }
    cout<<ans<<endl;
    cur=0;flag=0;
    for(int i=0;i<sz;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            cout<<points[i]<<" ";
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            cout<<points[i]<<endl;
            flag=0;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MN=2e6+200;
int points[MN];
map<int,int> point;
map<int,int> point2;
int main()
{
    ios::sync_with_stdio(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int l,r;
        cin>>l>>r;
        point[l]++;
        point2[r]--;
        points[2*i]=l;
        points[2*i+1]=r;
    }
    sort(points,points+2*n);
    int cur=0,ans=0;
    bool flag=0;
    for(int i=0;i<2*n;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            flag=0;
            ans++;
        }
    }
    cout<<ans<<endl;
    cur=0;flag=0;
    for(int i=0;i<2*n;i++){
        cur+=point[points[i]];
        if(cur>=k && !flag){
            cout<<points[i]<<" ";
            flag=1;
        }
        cur+=point2[points[i]];
        if(cur<k && flag){
            cout<<points[i]<<endl;
            flag=0;
        }
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 1000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		if (p.x != q.x) return p.x < q.x;
		return p.flag < q.flag;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= i && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		}
		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 2000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		if (p.x != q.x) return p.x < q.x;
		return p.flag < q.flag;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= i && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		}
		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 2000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		if (p.x != q.x) return p.x < q.x;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= i && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		} else if (flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i - 1].x);
		}

		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 2000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		return p.x < q.x;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= i && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		} else if (flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i - 1].x);
		}

		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 2000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		return p.x < q.x;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	a[++n] = point(2000000000, -1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= i && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		}

		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
	    if (flag) {
		    if (!cnt || ans[cnt].second != a[i].x) ans[++cnt] = make_pair(a[i].x, a[i].x);
		}
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 2000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		return p.x < q.x;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	a[++n] = point(2000000000, -1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= i && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		} else if (flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i - 1].x);
		}

		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <bitset>
#include <string>
using namespace std;

const int N = 2000010;

struct point {
	int x, flag;
	friend bool operator < (const point &p, const point &q) {
		return p.x < q.x;
	}
	point() {}
	point (int x, int flag):x(x), flag(flag) {}
}a[N];

pair<int, int> ans[N];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		a[2 * i - 1] = point(x, 1);
		a[2 * i] = point(y, -1);
	}

	n *= 2;
	sort(a + 1, a + n + 1);
	a[++n] = point(2000000000, -1);
	int i = 1;
    int tot = 0, cnt = 0;
	bool flag = false;
	while (i <= n) {
		int j = i;
		while (j <= n && a[j].x == a[i].x) tot += a[j++].flag == 1;
		if (tot  >= m && flag) {
			if (!cnt || a[i - 1].x != ans[cnt].second) ans[++cnt] = make_pair(a[i - 1].x, a[i].x);
			else ans[cnt].second = a[i].x;
		}

	    if (tot >= m) {
		    if (!cnt || ans[cnt].second != a[i].x) ans[++cnt] = make_pair(a[i].x, a[i].x);
		}
		for (int k = i; k < j; k++)
			if (a[k].flag == -1) tot--;
		if (tot >= m) flag = true;
		else flag = false;
		i = j;
	}

	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int maxn =  1e6 + 10;
#define ll long long
#define ios std::ios::sync_with_stdio(false)
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
/*

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod

ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;}
bool CH(ll a,ll n,ll x,ll t)
{ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;}
bool Miller_Rabin(ll n)
{if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL));
ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;}

*/

#define int long long
#define sc(t) scanf("%lld",&t)
#define pr(ans) printf("%lld\n",ans)
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
typedef unsigned long long ULL;
const double pi = 3.1415926535897932384626;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
#define debug(x) cout << #x << " : " << x << '\n';
#define sb cout << "sb\n";
#define fi first
#define se second
#define pb(a) push_back(a)
#define cn cout << '\n'
#define mp(a , b) make_pair(a , b)
const double ep = 1e-6;

vector<int> one , two;
int l[maxn] , r[maxn];
signed main()
{
    ios;///60 10 50
    cin.tie(0);
    int n , k;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i ++){
        cin >> l[i] >> r[i];
    }
    sort(l + 1 , l + 1 + n);
    sort(r + 1 , r + 1 + n);
    int  cnt = 0 , cntl = 1 , cntr = 1;
    for(int i = 1 ; i <= 2 * n ; i ++){
        if(cntl >= n + 1 && cntr >= n + 1)break;
        if(cntl >= n + 1){
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
        else if(cntr >= n + 1){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else if(l[cntl] == r[cntr]){
            cntl ++ , cntr ++;
            i ++;
            continue;
        }
        else if(l[cntl] < r[cntr]){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else {
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
    }
    if(one.size() < two.size()){
        two.pb(r[n]);
    }
    cout << one.size() << '\n';
    for(int i = 0 ; i < one.size() ; i ++){
        cout << one[i] << ' ' <<two[i] << '\n';
    }
	return 0;
}
/*
18
2 3 3
*/


#include<bits/stdc++.h>

using namespace std;
const int maxn =  1e6 + 10;
#define ll long long
#define ios std::ios::sync_with_stdio(false)
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
/*

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod

ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;}
bool CH(ll a,ll n,ll x,ll t)
{ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;}
bool Miller_Rabin(ll n)
{if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL));
ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;}

*/

#define int long long
#define sc(t) scanf("%lld",&t)
#define pr(ans) printf("%lld\n",ans)
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
typedef unsigned long long ULL;
const double pi = 3.1415926535897932384626;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
#define debug(x) cout << #x << " : " << x << '\n';
#define sb cout << "sb\n";
#define fi first
#define se second
#define pb(a) push_back(a)
#define cn cout << '\n'
#define mp(a , b) make_pair(a , b)
const double ep = 1e-6;

vector<int> one , two;
int l[maxn] , r[maxn];
map<int , int >ma;
signed main()
{
    ios;///60 10 50
    cin.tie(0);
    int n , k;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i ++){
        cin >> l[i] >> r[i];
        if(l[i] == r[i]){
            ma[l[i]] ++;
        }
    }
    sort(l + 1 , l + 1 + n);
    sort(r + 1 , r + 1 + n);
    int  cnt = 0 , cntl = 1 , cntr = 1;
    for(int i = 1 ; i <= 2 * n ; i ++){
        if(cntl >= n + 1 && cntr >= n + 1)break;
        if(cntl >= n + 1){
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
        else if(cntr >= n + 1){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else if(l[cntl] == r[cntr]){
            if(cnt + ma[l[cntl]] >= k && cnt < k){
                one.pb(l[cntl]);
                two.pb(r[cntr]);
            }
            cntl += ma[l[cntl]] , cntr += ma[l[cntl]];
            i += 2 * ma[l[cntl]] - 1;
            continue;
        }
        else if(l[cntl] < r[cntr]){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else {
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
    }
    if(one.size() < two.size()){
        two.pb(r[n]);
    }
    cout << one.size() << '\n';
    for(int i = 0 ; i < one.size() ; i ++){
        cout << one[i] << ' ' <<two[i] << '\n';
    }
	return 0;
}
/*
18
2 3 3
*/


#include<bits/stdc++.h>

using namespace std;
const int maxn =  1e6 + 10;
#define ll long long
#define ios std::ios::sync_with_stdio(false)
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
/*

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod

ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;}
bool CH(ll a,ll n,ll x,ll t)
{ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;}
bool Miller_Rabin(ll n)
{if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL));
ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;}

*/

#define int long long
#define sc(t) scanf("%lld",&t)
#define pr(ans) printf("%lld\n",ans)
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
typedef unsigned long long ULL;
const double pi = 3.1415926535897932384626;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
#define debug(x) cout << #x << " : " << x << '\n';
#define sb cout << "sb\n";
#define fi first
#define se second
#define pb(a) push_back(a)
#define cn cout << '\n'
#define mp(a , b) make_pair(a , b)
const double ep = 1e-6;

vector<int> one , two;
int l[maxn] , r[maxn];
signed main()
{
    ios;///60 10 50
    cin.tie(0);
    int n , k;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i ++){
        cin >> l[i] >> r[i];
    }
    sort(l + 1 , l + 1 + n);
    sort(r + 1 , r + 1 + n);
    int  cnt = 0 , cntl = 1 , cntr = 1;
    for(int i = 1 ; i <= 2 * n ; i ++){
        if(cntl >= n + 1 && cntr >= n + 1)break;
        if(cntl >= n + 1){
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
        else if(cntr >= n + 1){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else if(l[cntl] == r[cntr]){
            int zuo = 1 , you = 1;
            while(cntl + 1 <= n  && l[cntl + 1] == l[cntl]){
                cntl ++ ; zuo ++;
            }
            while(cntr + 1 <= n  && r[cntr + 1] == r[cntr]){
                cntr ++ , you ++;
            }
            if(cnt + min(zuo , you) >= k && cnt < k){
                one.pb(l[cntl]);
                two.pb(r[cntr]);
            }
            cntl -= zuo ,cntr -= you;
            cntl += min(zuo ,you) , cntr += min(zuo , you);
            cntl ++ , cntr ++;
            if(min(zuo , you) - 1 > 0) i += 2 * min(zuo , you) - 1;

            continue;
        }
        else if(l[cntl] < r[cntr]){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else {
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
    }
    if(one.size() < two.size()){
        two.pb(r[n]);
    }
    cout << one.size() << '\n';
    for(int i = 0 ; i < one.size() ; i ++){
        cout << one[i] << ' ' <<two[i] << '\n';
    }
	return 0;
}
/*
18
2 3 3
*/