#include <bits/stdc++.h>
using namespace std;
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
vector<ll> primes;
const int NN = 1e6+1;
int prime[2*NN];
void seive(){
ll i;
Fo(i, 1, 2*NN) prime[i] = i;
for(i=2; i < 2*NN; i++){
if (prime[i] == i){
for(ll j = 2*i; j < 2*NN; j += i)
if (prime[j] == j)
prime[j] = i;
primes.pb(i);
}
}
}
typedef ll var;
pair<var, var> extended_gcd(var a, var b){
bool swp = 0;
if (a<b) swap(a, b), swp = 1;
//1 = N*a + M*b
//returns {N, M}
vector<var> K;
var k, r, FF, SS;
while(1){
k = a/b;
r = a - k*b;
if(r == 1){
FF = 1, SS = -k;
reverse(all(K));
for(int k: K){
var tem = FF;
FF = SS;
SS = tem - SS * k;
}
return swp?mp(SS, FF):mp(FF, SS);
}
else{
K.pb(k);
}
a = b;
b = r;
//{1, k}
}
}
var solve(var a, var b, var n){
pair<var, var> ans = extended_gcd(a, n);
var x = ( ans.F * b ) % n;
if ( x < 0)
x += n;
//a*x is b (mod n)
return x;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i,n,k,j;
i = 2;
seive();
ll sum = 0;
ll PO[30];
PO[0] = 1;
Fo(j, 1, 20) PO[j] = 10*PO[j-1];
while(primes[i] < NN){
int p1 = primes[i];
int p2 = primes[i+1];
int r = p2-p1;
int d = 0;
int v = p1;
while(v) v/=10, d++;
ll a = PO[d];
sum += solve(a, p2-p1, p2) * a + p1;
i++;
}
cout<<sum<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSB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