#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1000005;
int n,a[M],b[M],c[M],d[M];
int main(){
    scanf("%d",&n);
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]); c[i]=a[i]-a[i-1];
    }
    for(int i=1; i<=n; i++){
        scanf("%d",&b[i]); d[i]=b[i]-b[i-1];
    }
    sort(c+1,c+1+n);
    sort(d+1,d+1+n);
    int f=1;
    for(int i=1; i<=n; i++) if(c[i]!=d[i]) f=0;
    if(f) printf("Yes\n");
    else printf("No\n");
    return 0;
}


#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
static const int MAXN=100050;
using namespace std;
int n,a[MAXN],b[MAXN],c[MAXN],d[MAXN];
inline int read(){
    int x=0,sign=0;
    char alpha=0;
    while(!isdigit(alpha)) sign|=alpha=='-',alpha=getchar();
    while(isdigit(alpha)) x=(x<<1)+(x<<3)+(alpha^48),alpha=getchar();
    return sign?-x:x;
}
int main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++) b[i]=read();
    if(a[1]!=b[1]||a[n]!=b[n]) return 0&printf("No\n");
    for(int i=2;i<=n;i++){
        c[i]=a[i]-a[i-1];
        d[i]=b[i]-b[i-1];
    }
    sort(c+2,c+n+1);
    sort(d+2,d+n+1);
    for(int i=2;i<=n;i++){
        if(c[i]!=d[i]) return 0&printf("No\n");
    }
    printf("Yes\n");
    return 0;
}


#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
static const int MAXN=100050;
using namespace std;
vector<int> c,d;
int n,a[MAXN],b[MAXN];
inline int read(){
    int x=0,sign=0;
    char alpha=0;
    while(!isdigit(alpha)) sign|=alpha=='-',alpha=getchar();
    while(isdigit(alpha)) x=(x<<1)+(x<<3)+(alpha^48),alpha=getchar();
    return sign?-x:x;
}
int main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++) b[i]=read();
    if(a[1]!=b[1]||a[n]!=b[n]) return 0&printf("No\n");
    for(int i=2;i<=n;i++){
        c.push_back(a[i]-a[i-1]);
        d.push_back(b[i]-b[i-1]);
    }
    sort(c.begin(),c.end());
    sort(d.begin(),d.end());
    for(int i=0;i<n;i++){
        if(c[i]!=d[i]) return 0&printf("No\n");
    }
    printf("Yes\n");
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstring>
#include <vector>
static const int MAXN=100050;
typedef long long ll;
using namespace std;
int n;
ll a[MAXN],b[MAXN];
vector<int> vec;
inline ll read(){
	ll x=0;bool sign=false;
	char alpha=0;
	while(!isdigit(alpha)) sign|=alpha=='-',alpha=getchar();
	while(isdigit(alpha)) x=(x<<1)+(x<<3)+(alpha^48),alpha=getchar();
	return sign?-x:x;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=n;i++) b[i]=read();
	if(a[1]!=b[1]||a[n]!=b[n]) return 0&printf("No\n");
	for(int i=2;i<=n-1;i++){
		if(a[i]!=b[i]){
			if((ll)a[i-1]+a[i+1]==(ll)a[i]+b[i]) a[i]=b[i];
			else vec.push_back(i);
		}
	}
	for(int i=0;i<vec.size();i++){
		if((ll)(a[vec[i]-1]+a[vec[i]+1])==(ll)(a[vec[i]]+b[vec[i]])) continue;
		else return 0&printf("No");
	}
	printf("Yes\n");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    map<int,int> x,y;
    for (int i=1;i<n;i++)
    {
        x[a[i]-a[i-1]]++;
    }
    for (int i=0;i<n-2;i++)
    {
        x[a[i+1]-a[i]]--;
        if (a[i+1]==b[i+1]) {y[a[i]-a[i+1]]++; continue;}
        y[a[i]-a[i+1]]++;
        int need=b[i+1]-a[i];
        if (!x[need])
        {
            int need1=b[i+1]-a[i+2];
            if (!y[need1]) {cout<<"No"; return 0;}
        }
    }
    cout<<"Yes";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    map<ll,int> x,y;
    for (int i=1;i<n;i++)
    {
        x[a[i]-a[i-1]]++;
    }
    for (int i=0;i<n-2;i++)
    {
        if (x[a[i+1]-a[i]]) x[a[i+1]-a[i]]--;
        if (a[i+1]==b[i+1]) {y[a[i]-a[i+1]]++; continue;}
        int need=b[i+1]-a[i];
        y[a[i]-a[i+1]]++;
        if (!x[need])
        {
            int need1=b[i+1]-a[i+2];
            if (!y[need1]) {cout<<"No"; return 0;}
            else y[need1]--;
        }
        else x[need]--;
    }
    cout<<"Yes";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    map<int,int> x,y;
    for (int i=1;i<n;i++)
    {
        x[a[i]-a[i-1]]++;
    }
    for (int i=0;i<n-2;i++)
    {
        if (x[a[i+1]-a[i]]) x[a[i+1]-a[i]]--;
        if (a[i+1]==b[i+1]) {y[a[i]-a[i+1]]++; continue;}
        int need=b[i+1]-a[i];
        y[a[i]-a[i+1]]++;
        if (!x[need])
        {
            int need1=b[i+1]-a[i+2];
            if (!y[need1]) {cout<<"No"; return 0;}
            else y[need1]--;
        }
        else x[need]--;
    }
    cout<<"Yes";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    map<int,int> x,y;
    for (int i=1;i<n;i++)
    {
        x[a[i]-a[i-1]]++;
    }
    for (int i=0;i<n-2;i++)
    {
        x[a[i+1]-a[i]]--;
        if (a[i+1]==b[i+1]) {y[a[i]-a[i+1]]++; continue;}
        int need=b[i+1]-a[i];
        if (!x[need])
        {
            int need1=b[i+1]-a[i+2];
            if (!y[need1]) {cout<<"No"; return 0;}
        }
        y[a[i]-a[i+1]]++;
    }
    cout<<"Yes";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    vector<int> a1,b1;
    for (int i=1;i<n;i++)
        a1.pb(a[i]-a[i-1]);
    for (int i=1;i<n;i++)
        b1.pb(b[i]-b[i-1]);
    sort(a1.begin(),a1.end());
    sort(b1.begin(),b1.end());
    for (int i=0;i<n-1;i++)
        if (a1[i]!=b1[i]) {cout<<"No"; return 0;}
    cout<<"Yes";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    map<int,int> x,y;
    for (int i=1;i<n;i++)
    {
        x[a[i]-a[i-1]]++;
    }
    for (int i=0;i<n-2;i++)
    {
        x[a[i+1]-a[i]]--;
        if (a[i+1]==b[i+1]) {y[a[i]-a[i+1]]++; continue;}
        int need=b[i+1]-a[i];
        if (!x[need])
        {
            int need1=b[i+1]-a[i+2];
            if (!y[need]) {cout<<"No"; return 0;}
        }
        y[a[i]-a[i+1]]++;
    }
    cout<<"Yes";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ll long long
#define ull unsigned long long

ll md=1e9+7;
const int N=200005;
int INF=1e9+7;

bool check(char c)
{
    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return 1;
    else return 0;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n,a[200005],b[200005];
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    if (a[0]!=b[0]||a[n-1]!=b[n-1]) {cout<<"No"; return 0;}
    map<ll,int> x,y;
    for (int i=1;i<n;i++)
    {
        x[a[i]-a[i-1]]++;
    }
    for (int i=0;i<n-2;i++)
    {
        if (x[a[i+1]-a[i]]) x[a[i+1]-a[i]]--;
        if (a[i+1]==b[i+1]) {y[a[i]-a[i+1]]++; continue;}
        ll need=b[i+1]-a[i];
        y[a[i]-a[i+1]]++;
        if (!x[need])
        {
            ll need1=b[i+1]-a[i+2];
            if (!y[need1]) {cout<<"No"; return 0;}
            else y[need1]--;
        }
        else x[need]--;
    }
    cout<<"Yes";
    return 0;
}


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

const int MAXN = 1e5 + 5;
int n, c[MAXN], t[MAXN], d[MAXN], e[MAXN];
bool ans = true;

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &c[i]);
		if(i)
			d[i-1] = c[i-1]-c[i];
	}
	for(int i = 0; i < n; ++i){
		scanf("%d", &t[i]);
		if(i)
			e[i-1] = c[i-1]-c[i];
	}
	ans = (c[0] == t[0] && c[n-1] == t[n-1]);
	priority_queue<int> p, q;
	for(int i = 0; i < n; ++i){
		p.push(d[i]);
		q.push(e[i]);
	}
	while(!p.empty()){
		if(p.top() != q.top())
			ans = false;
		p.pop(); q.pop();
	}
	if(ans)
		printf("Yes\n");
	else
		printf("No\n");
}


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

const int MAXN = 1e5 + 5;
int n, c[MAXN], t[MAXN], d[MAXN], e[MAXN];
bool ans = true;

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &c[i]);
		if(i)
			d[i-1] = c[i-1]-c[i];
	}
	for(int i = 0; i < n; ++i){
		scanf("%d", &t[i]);
		if(i)
			e[i-1] = c[i-1]-c[i];
	}
	ans = (c[0] == t[0] && c[n-1] == t[n-1]);
	priority_queue<int> p, q;
	for(int i = 0; i < n-1; ++i){
		p.push(d[i]);
		q.push(e[i]);
	}
	while(!p.empty()){
		if(p.top() != q.top())
			ans = false;
		p.pop(); q.pop();
	}
	if(ans)
		printf("Yes\n");
	else
		printf("No\n");
}


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

const int MAXN = 1e5 + 5;
int n, c[MAXN], t[MAXN], d[MAXN], e[MAXN];
bool ans = true;

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &c[i]);
		if(i)
			d[i-1] = c[i-1]-c[i];
	}
	for(int i = 0; i < n; ++i){
		scanf("%d", &t[i]);
		if(i)
			e[i-1] = t[i-1]-t[i];
	}
	ans = (c[0] == t[0] && c[n-1] == t[n-1]);
	priority_queue<int> p, q;
	for(int i = 0; i < n-1; ++i){
		p.push(d[i]);
		q.push(e[i]);
	}
	while(!p.empty()){
		if(p.top() != q.top())
			ans = false;
		p.pop(); q.pop();
	}
	if(ans)
		printf("Yes\n");
	else
		printf("No\n");
}


#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll_inf 201920192019233
#define int_inf 2147483647
typedef long long ll;
const int N=100005;
int a[N],b[N],n;
int c[N],d[N];
int main(){
	ios::sync_with_stdio(false);
	//CODEFORCES只能用cin、cout!
	cin >> n;
	for(re int i=1;i<=n;i++) cin >> a[i];
	a[0]=0;
	a[n+1]=0;
	for(re int i=0;i<=n;i++){
		c[i]=a[i+1]-a[i];
	}
	for(re int i=1;i<=n;i++){
		cin >> b[i];
	}
	b[0]=0;
	b[n+1]=0;
	for(re int i=0;i<=n;i++){
		d[i]=b[i+1]-b[i];
	}
	sort(c,c+n+1);
	sort(d,d+n+1);
	for(int i=0;i<=n;i++){
		if(c[i]!=d[i]){
			cout << "No" << endl;
			return 0;
		}
	}
	if(a[1]!=b[1])cout << "No" << endl;
	else cout << "Yes" << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+20;
int n,a[N],t[N],c[N],d[N];
inline void input(void)
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=1;i<=n;i++)
		scanf("%d",&t[i]);
}
inline bool check(void)
{
	for (int i=1;i<=n;i++)
		c[i] = a[i] - a[i-1];
	for (int i=1;i<=n;i++)
		d[i] = t[i] - t[i-1];
	if ( a[1] ^ t[1] ) return false;
	sort( c+1 , c+n+1 );
	sort( d+1 , d+n+1 );
	for (int i=1;i<=n;i++)
		if ( c[i] ^ d[i] )
			return false;
	return true;
}
int main(void)
{
	input();
	puts( check() ? "Yes" : "No" );
	return 0;
}


#include <bits/stdc++.h>
#define N 5000000
#define M 10005
#define lol long long
#define INF 1<<30
using namespace std;
int read()
{
    int res=0,kkk=1;
    char ch=' ';
    while(!isdigit(ch)){ch=getchar();if(ch=='-')kkk=-1;}
    while(isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
    return res*kkk;
}
int n,c[N],t[N],xc[N],xt[N];
int main()
{
	n=read();
	for(register int i=1;i<=n;i++){c[i]=read();xc[i-1]=c[i]-c[i-1];}
	for(register int i=1;i<=n;i++){t[i]=read();xt[i-1]=t[i]-t[i-1];}
	sort(xc+1,xc+n);
	sort(xt+1,xt+n);
	for(register int i=1;i<n;i++)
		if(xc[i]!=xt[i]){printf("No");exit(0);}
	printf("Yes");
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<map>
#define ll long long
#define MAXN 8000010
using namespace std;
ll n,c[MAXN],t[MAXN],d[MAXN],e[MAXN],ans;
map<ll,ll> m1,m2;

int main(){
    cin>>n;
    for(ll i=1;i<=n;i++) scanf("%lld",&c[i]);
    for(ll i=1;i<=n;i++) scanf("%lld",&t[i]);
    for(ll i=1;i<n;i++) d[i]=c[i+1]-c[i];
    for(ll i=1;i<n;i++) e[i]=t[i+1]-t[i];
    if(c[1]!=t[1]){
        printf("No");
        return 0;
    }
    for(ll i=1;i<n;i++){
        m1[d[i]]++; m2[e[i]]++;
    }
    for(ll i=1;i<n;i++){
        if(m1[d[i]]!=m2[d[i]]){
            printf("No");
            return 0;
        }
    }
    for(ll i=1;i<n;i++){
        if(m1[e[i]]!=m2[e[i]]){
            printf("No");
            return 0;
        }
    }
    printf("Yes");
    return 0;
}


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

bool add[100005],f;
vector<int > v;
vector<pair<int,int > > r;

int main()
{
    int n; scanf("%d",&n);
    for(int i = n/2; i >= 2; --i)
    {
        f=0;
        for(int j = 2; j <= i/j && !f; ++j){ f|=(i%j==0); }
        if(f) continue;

        v.clear();
        v.push_back(i);
        int cur = i + i + i;
        while(cur <= n)
        {
            if(!add[cur])
                v.push_back(cur);
            add[cur]=1;
            cur+=i;
        }
        if(v.size() & 1) v.push_back(i+i), add[i+i]=1;
        for(int i = 0; i < (int)v.size(); i += 2) r.push_back({v[i],v[i+1]});

    }
    printf("%d\n",r.size());
    for(auto i : r) printf("%d %d\n",i.first,i.second);
}


#include<bits/stdc++.h>
#define MAXN 100000+10
#define MOD 1000000007

using namespace std;

long long n;
bool use[MAXN],b[MAXN];
int a[MAXN],Now;
int Ans;
int L[MAXN],R[MAXN],CNT=0;

int main()
{
	int Cnt=0;
	scanf("%I64d",&n);
	for(long long i=3;i<=n;i+=2)
	{
		if(b[i]) continue;

		Now=1;
		a[1]=i;

		for(long long j=2*i;j<=n;j+=i)
		{
			b[j]=false;

			if(use[j]) continue;

			a[++Now]=j;
		}

		if(Now==1) continue;

		if(Now%2==0)
		{
			for(int i=1;i+1<=Now;i+=2)
			{
				Ans++;
				L[Ans]=a[i];use[a[i]]=true;
				R[Ans]=a[i+1];use[a[i+1]]=true;
			}
		}
		else
		{
			Ans++;
			L[Ans]=a[1];use[a[1]]=true;
			R[Ans]=a[3];use[a[3]]=true;
			for(int i=4;i+1<=Ans;i+=2)
			{
				Ans++;
				L[Ans]=a[i];use[a[i]]=true;
				R[Ans]=a[i+1];use[a[i+1]]=true;
			}
		}
	}
	Now=0;
	for(long long j=2;j<=n;j+=2)
	{
		b[j]=false;

		if(use[j]) continue;

		a[++Now]=j;
	}

	if(Now%2==0)
	{
		for(int i=1;i+1<=Now;i+=2)
		{
			Ans++;
			L[Ans]=a[i];use[a[i]]=true;
			R[Ans]=a[i+1];use[a[i+1]]=true;
		}
	}
	else
	{
		if(Now!=1)
		{
			Ans++;
			L[Ans]=a[1];use[a[1]]=true;
			R[Ans]=a[3];use[a[3]]=true;
			for(int i=4;i+1<=Now;i+=2)
			{
				Ans++;
				L[Ans]=a[i];use[a[i]]=true;
				R[Ans]=a[i+1];use[a[i+1]]=true;
			}
		}
	}

	printf("%d\n",Ans);
	for(int i=1;i<=Ans;i++)
		printf("%d %d\n",L[i],R[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 1000005
#define INF 100000007
using namespace std;
int n,notprime[N],vis[N],tmp[N],index,ans[N][4];
void check_vis_and_tmp(int i,int num)
{
	cout<<"i="<<i<<endl;
	for(int i=1;i<=n;i++)
	{
		printf("vis[%d]=%d  ",i,vis[i]);
	}
	cout<<endl;
	for(int i=1;i<=num;i++)
	{
		printf("tmp[%d]=%d  ",i,tmp[i]);
	}
	cout<<endl;
}
void check_ans()
{
	cout<<"------vis-----"<<endl;
	for(int i=1;i<=index;i++)
	{
		printf("(%d,%d)",ans[i][1],ans[i][2]);
		if(i%5==0)cout<<endl;
	}
	cout<<endl;
	cout<<"--------------"<<endl;
}
int main()
{
	cin>>n;
	for(int i=2;i<=n;i++)
	{
		if(!notprime[i])
		{
			for(int j=2;j*i<=n;j++)
			{
				notprime[j*i]=1;
			}
		}
	}
	for(int i=3;i<=n;i++)
	{
		if(!notprime[i])
		{
			int num=0;
			for(int j=1;j*i<=n;j++)
			{
				if(!vis[i*j])
				{
					vis[i*j]=1;
					++num;
					tmp[num]=i*j;
				}
			}
			if(num%2==0)
			{
				for(int j=1;j<=num;j+=2)
				{
					++index;
					ans[index][1]=tmp[j];
					ans[index][2]=tmp[j+1];
				}
			}
			else if(num%2==1&&num>1)
			{
				tmp[2]=tmp[num];
				vis[2*i]=0;//ÊÍ·Åvis[2*i]
				num--;
				for(int j=1;j<=num;j+=2)
				{
					++index;
					ans[index][1]=tmp[j];
					ans[index][2]=tmp[j+1];
				}
			}
			else if(num%2==1&&num==1)
			{
				vis[tmp[num]]=0;
			}
//			check_vis_and_tmp(i,num);
//			check_ans(1);
		}
	}

	int i=2,num=0;
	for(int j=1;j*i<=n;j++)
	{
		if(!vis[i*j])
		{
			vis[i*j]=1;
			++num;
			tmp[num]=i*j;
		}
	}
	for(int j=1;j<=num-num%2;j+=2)
	{
		ans[++index][1]=tmp[j];
		ans[index][2]=tmp[j+1];
	}
//	check_vis_and_tmp(i,num);

	cout<<index<<endl;
	for(int i=1;i<=index;i++)
	{
		printf("%d %d\n",ans[i][1],ans[i][2]);
	}
}


#include <stdio.h>
#include <vector>
using namespace std;
struct num
{
    int zhi = 1;
    int is = 0;
    int a;
    int much = 0;
    vector<int> who;
};
int solve[5000][2];
int sn = -1;
num a[100010];
int main()
{
    int n;
    scanf("%d", &n);
    int ans = 0;

    for (int i = 0; i <= n; i++)
    {
        a[i].a = i;
    }
    int pair = -1;
    for (int i = n / 2 + 1; i >= 2; i--)
    {
        int thisans = 0;
        for (int j = 1; i * j <= n ; j++)
        {
            if(a[i * j].is == 0)
            thisans++;
        }
       // printf("%d ta:%d\n",i,thisans);
        int flag = 0;
        for (int j = 1; i * j <= n ; j++)
        {
            if(a[i * j].is == 0)
            {
                if (thisans % 2 == 0 && thisans >= 2)
            {

                a[i * j].is = 1;

                if (flag % 2 == 0)
                    solve[++sn][0] = i * j;
                if (flag % 2 == 1)
                    solve[sn][1] = i * j;
                flag++;
            }
            else if (thisans >= 2)
            {

                if (j != 2)
                {

                    a[i * j].is = 1;

                    if (flag % 2 == 0)
                        solve[++sn][0] = i * j;
                    if (flag % 2 == 1)
                        solve[sn][1] = i * j;
                        flag++;
                }
            }
            }

        }
    }
    printf("%d\n", sn+1);
    for (int i = 0; i <= sn; i++)
    {
        printf("%d %d\n", solve[i][0], solve[i][1]);
    }
}


#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=110000;
int n,p[maxn],tot;bool check[maxn],f[maxn];
int a[maxn],b[maxn],ans;
int e[maxn];
void getprime(int N)
{
	for(int i=2;i<=N;++i)
	{
		if(!check[i])p[++tot]=i;
		for(int j=1;j<=tot;++j)
		{
			if(i*p[j]>N)break;
			check[i*p[j]]=1;
			if(i%p[j]==0)break;
		}
	}
}
int main()
{
	scanf("%d",&n);
	getprime(n/2);
	for(int i=2;i<=tot;++i)
	{
		int l=1,r=2;
		if(n/p[i]%2)r++;
		while(r*p[i]<=n)
		{
			a[++ans]=l*p[i];b[ans]=r*p[i];
			l=r+1;r+=2;
			f[l*p[i]]=1;f[r*p[i]]=1;
		}
		e[++e[0]]=2*p[i];
	}
	int last=0;
	for(int i=2;i<=n;++i)
	{
		if(i&1)continue;
		if(!f[i])
		{
			if(last)
			{
				a[++ans]=last;b[ans]=i;last=0;
			}
			else last=i;
		}
	}
	printf("%d\n",ans);
	for(int i=1;i<=ans;++i)
	{
		printf("%d %d\n",a[i],b[i]);
	}
	return 0;
}


/*codeforces449C
给定整数n(1<=n<=1e5),将1~n中的数两两配对,使得每队的最大公因数不为1(即不互质)
求能生成符合这样条件的数对的对数,并将配对情况输出(任一解)
首先知道2的倍数是最厉害的,任意两个2的倍数都能配对
（误）任一质数的每一个倍数（包括1倍）,都能任取其2个配对,而若其倍数个数为奇数,必然剩下一个,就令其2倍剩下,这样还能和其他偶数配对
以标记数组used[]来记录某个数是否已经取出
先遍历从3到n/2的质数,取其未取过的倍数,先统计倍数个数,若为偶则全部加入答案序列,若为奇则留下2倍数 其他倍数全部加入答案序列
最后取出未取过的2的倍数,全部加入答案序列,若还有余则无法取出,已达到最优解
 */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

const int MAXN = 100010;
int n, k, prime[MAXN], cnt = 0;
bool iscom[MAXN];

void shaker()
{
    memset(iscom, 0, sizeof(iscom));
    iscom[1] = 1;
    for (int i = 2; i <= MAXN; i++)
    {
        if (!iscom[i])
            prime[++cnt] = i;
        for (int j = 1; j <= cnt && i * prime[j] <= MAXN; j++)
        {
            iscom[i * prime[j]] = 1;
            if (i % prime[j] == 0)
                break;
        }
    }
}

bool used[MAXN];
int ans[MAXN], anscnt = 0;
int main()
{
    scanf("%d", &n);
    shaker();
    /*for(int i=1;i<=n;i++)
    {
        if(iscom[i]) printf("%d ",i);
        else printf("[%d] ",i);
    }*/
    for (int i = 2; prime[i] * 2 <= n; i++)
    {
        int cnt = 0;
        for (int j = 1; j * prime[i] <= n; j++)
        {
            if (used[j * prime[i]] == 0)
                cnt++;
        }
        if ((cnt & 1) == 0)
        {
            for (int j = 1; j * prime[i] <= n; j++)
            {
                if (used[j * prime[i]] == 0)
                {
                    ans[++anscnt] = j * prime[i];
                    used[j * prime[i]] = 1;
                }
            }
        }
        else
        {
            ans[++anscnt] = prime[i];
            for (int j = 3; j * prime[i] <= n; j++)
            {
                if (used[j * prime[i]] == 0)
                {
                    ans[++anscnt] = j * prime[i];
                    used[j * prime[i]] = 1;
                }
            }
        }
    }
    for (int j = 1; j * 2 <= n; j++)
    {
        if (used[j * 2] == 0)
        {
            ans[++anscnt] = j * 2;
            used[j * 2] = 1;
        }
    }
    if((anscnt&1)==1)anscnt--;
    printf("%d\n",anscnt/2);
    for(int i=1;i<=anscnt;i+=2)
    {
        printf("%d %d\n",ans[i],ans[i+1]);
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<iostream>
#include<string>
#include<cstring>
#include<ctime>
using namespace std;
int su[100000];
int tot=0;
int b[100000];
int ans[100010];
struct by
{
	int yz,data;
}ss[100010];
by a[100010];
int n;
int cunt=0;
bool cmp(by m,by n)
{
	return m.yz<n.yz;
}
int main()
{
	cin>>n;
	for(int i=2;i<=n;i++)
	if(!b[i])
	{
		su[++tot]=i;
		for(int j=i;j<=n;j+=i) b[j]=1;
	}
	for(int i=2;i<=n;i++)
	{
		ss[i].data=i;
		for(int j=2;j<=sqrt(i);j++)
		if(i%j==0)  ss[i].yz++;
	}
	memset(b,0,sizeof(b));
	for(int i=tot;i>=1;i--)
	{
		int cnt=0;
		for(int j=su[i];j<=n;j+=su[i])
		if(!b[j])
		{
		  a[++cnt].data=j;
		  a[cnt].yz=ss[j].yz;
		  b[j]=1;
	    }
		if(cnt%2==1)
		{b[a[cnt--].data]=0;}
		sort(a+1,a+1+cnt,cmp);
		for(int j=1;j<=cnt;j+=2)
		{
			ans[++cunt]=a[j].data;
			ans[++cunt]=a[j+1].data;
		}
	}

	printf("%d\n",cunt/2);
	for(int i=1;i<=cunt;i+=2)
	  printf("%d %d\n",ans[i],ans[i+1]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ms(a, b) memset(a, b, sizeof(a))
#define MAXN 100005
using namespace std;
bool bo[MAXN];
bool zs[MAXN];
vector<int> ansl;
int main()
{
    ms(bo, 1);
    ms(zs, 1);
    int n;
    cin >> n;
    int ans = 0;
    int a[MAXN / 2][2];

    for (int i = 2; i <= n / 2; i++)
    {
        if (zs[i])
        {
            int k;
            for (int j = 2; (k = i * j) <= n; j++)
                zs[k] = 0;
        }
    }
    for (int i = n / 2; i >= 2; i--)
    {
        if (zs[i])
        {
            bo[i] = 0;
            int num = 1, k = 0;
            for (int j = 3; (k = i * j) <= n; j++)
            {

                if (bo[k])
                {
                    // printf("%d %d\n", i, k);
                    num++;
                    bo[k] = 0;
                    ansl.push_back(k);
                }
            }
            // if (n 1)
            if (num & 1)
            {

                bo[i * 2] = 0;
                // printf("num %d\n", num);
                ansl.push_back(i * 2);
                ansl.push_back(i);
            }
            else
            {
                ansl.push_back(i);
            }
        }
    }
     printf("%d\n", ansl.size()/2);
    for (int i = 0; i < ansl.size(); i += 2)
    {
        printf("%d %d\n", ansl[i], ansl[i + 1]);
    }
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int tot, n, prime[100010], tot2, vis[100010], a[100010], b[100010], cnt1, cnt2, tot3, notuse[100010];
bool use[100010];

struct node
{
    int x, y;
} e[100010];

void init()
{
    for (int i = 2; i <= n; i++)
    {
        if (!vis[i])
            prime[++tot2] = i;
        for (int j = 1; j <= tot2; j++)
        {
            int t = prime[j] * i;
            if (t > n)
                break;
            vis[t] = 1;
            if (i % prime[j] == 0)
                break;
        }
    }
}

int main()
{
    scanf("%d", &n);
    init();
    for (int i = 2; i <= tot2; i++)
    {
        memset(a, 0, sizeof(a));
        cnt1 = 0;
        a[++cnt1] = prime[i];
        for (int j = 3; j * prime[i] <= n; j++)
            if (!use[j * prime[i]])
                a[++cnt1] = j * prime[i];
        if (2 * prime[i] <= n && !use[2 * prime[i]])
        {
            if (cnt1 % 2 == 1)
                a[++cnt1] = 2 * prime[i];
            else
            {
                use[2 * prime[i]] = 1;
                notuse[++tot3] = 2 * prime[i];
            }
        }
        for (int j = 1; j + 1 <= cnt1; j += 2)
        {
            e[++tot].x = a[j];
            e[tot].y = a[j + 1];
            use[a[j]] = use[a[j + 1]] = 1;
        }
    }
    for (int i = 1; i * 2 <= n; i++)
        if (!use[i * 2])
            notuse[++tot3] = i * 2;
    for (int i = 1; i + 1 <= tot3; i += 2)
    {
        e[++tot].x = notuse[i];
        e[tot].y = notuse[i + 1];
    }
    printf("%d\n", tot);
    for (int i = 1; i <= tot; i++)
        printf("%d %d\n", e[i].x, e[i].y);

    return 0;
}


#include<bits/stdc++.h>
#define MAXN 100000+10
#define MOD 1000000007

using namespace std;

long long n;
bool use[MAXN],b[MAXN];
int a[MAXN],Now;
int Ans;
int L[MAXN],R[MAXN],CNT=0;

int main()
{
	int Cnt=0;
	scanf("%I64d",&n);
	for(long long i=3;i<=n;i+=2)
	{
		if(b[i]) continue;

		Now=1;
		a[1]=i;

		for(long long j=2*i;j<=n;j+=i)
		{
			b[j]=false;

			if(use[j]) continue;

			a[++Now]=j;
		}

		if(Now==1) continue;

		if(Now%2==0)
		{
			for(int i=1;i+1<=Now;i+=2)
			{
				Ans++;
				L[Ans]=a[i];use[a[i]]=true;
				R[Ans]=a[i+1];use[a[i+1]]=true;
			}
		}
		else
		{
			Ans++;
			L[Ans]=a[1];use[a[1]]=true;
			R[Ans]=a[3];use[a[3]]=true;
			for(int i=4;i+1<=Now;i+=2)
			{
				Ans++;
				L[Ans]=a[i];use[a[i]]=true;
				R[Ans]=a[i+1];use[a[i+1]]=true;
			}
		}
	}
	Now=0;
	for(long long j=2;j<=n;j+=2)
	{
		b[j]=false;

		if(use[j]) continue;

		a[++Now]=j;
	}

	if(Now%2==0)
	{
		for(int i=1;i+1<=Now;i+=2)
		{
			Ans++;
			L[Ans]=a[i];use[a[i]]=true;
			R[Ans]=a[i+1];use[a[i+1]]=true;
		}
	}
	else
	{
		if(Now!=1)
		{
			Ans++;
			L[Ans]=a[1];use[a[1]]=true;
			R[Ans]=a[3];use[a[3]]=true;
			for(int i=4;i+1<=Now;i+=2)
			{
				Ans++;
				L[Ans]=a[i];use[a[i]]=true;
				R[Ans]=a[i+1];use[a[i+1]]=true;
			}
		}
	}

	printf("%d\n",Ans);
	for(int i=1;i<=Ans;i++)
		printf("%d %d\n",L[i],R[i]);
	return 0;
}


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

const int maxn=1e5+5;
int a[maxn];
vector<int> pv;
vector<int> cc[maxn];
bool isodd[maxn];
bool vis[maxn];

struct Node {
    int x,y;
};
vector<Node> ans;

void prime(int n){
    for(int i=2;i<=n;i++){
        if(!a[i]){
            cc[i].push_back(i);
            for(int j=i*2;j<=n;j+=i){
                if(!a[j]) cc[i].push_back(j);
                a[j]=1;
            }
            if(i*2<=n) pv.push_back(i);
        }
    }
}

int main()
{
    int n;
    cin>>n;
    prime(n);
    for(int i=0;i<pv.size();i++){
        int t=pv[i];
        if(t==2)continue;
        if(cc[t].size()&1){
            vis[t]=1;
            vis[2*t]=1;
            ans.push_back({t,2*t});
            int cnt=1;
            int x,y;
            for(int j=0;j<cc[t].size();j++){
                if(!vis[cc[t][j]]){
                    if(cnt==1){
                        x=cc[t][j];
                        cnt++;
                    }else if(cnt==2){
                        cnt=1;
                        y=cc[t][j];
                        ans.push_back({x,y});
                    }
                    vis[cc[t][j]]=1;
                }
            }
        }else {
            int cnt=1;
            int x,y;
            for(int j=0;j<cc[t].size();j++){
                if(!vis[cc[t][j]]){
                    if(cnt==1){
                        x=cc[t][j];
                        cnt++;
                    }else if(cnt==2){
                        cnt=1;
                        y=cc[t][j];
                        ans.push_back({x,y});
                    }
                    vis[cc[t][j]]=1;
                }
            }
        }
    }
    int cnt=1;
    int x,y;
    for(int j=0;j<cc[2].size();j++){
        if(!vis[cc[2][j]]){
            if(cnt==1){
                x=cc[2][j];
                cnt++;
            }else if(cnt==2){
                cnt=1;
                y=cc[2][j];
                ans.push_back({x,y});
            }
            vis[cc[2][j]]=1;
        }
    }
    /*
    for(int j=0;j<cc[5].size();j++){
        cout<<cc[5][j]<<' '<<vis[cc[5][j]]<<endl;
    }
    */
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++){
        printf("%d %d\n",ans[i].x,ans[i].y);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pb push_back
#define eb emplace_back
#define maxn 100005
#define eps 1e-8
#define pi acos(-1.0)
#define rep(k,i,j) for(int k=i;k<j;k++)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,int>pli;
typedef pair<int,char> pic;
typedef pair<pair<int,string>,pii> ppp;
typedef unsigned long long ull;
const long long mod=1e9+7;
/*#ifndef ONLINE_JUDGE
        freopen("1.txt","r",stdin);
#endif */
vector<int> a[maxn],ans;
bool book[maxn];

int main(){
    #ifndef ONLINE_JUDGE
     //   freopen("1.txt","r",stdin);
    #endif
    cin>>n;
	if(n==1){
		cout<<0;
		return 0;
	}
	for(int i=3;i<=n;i++){
		if((i%2==1)&&book[i]==0 ){
			for(int j=i;j<=n;j+=i){
				if(book[j]==0){
					a[i].push_back(j);
					book[j]=1;
				}

			}
		}
	}
	for(int i=3;i<=(n/2);i++){
		if(a[i].size()>0){
			if(a[i].size()%2==1){
				a[i].erase(a[i].begin()+1);
				book[2*i]=0;
			}
			while(a[i].size()>0){
				ans.push_back(a[i].back());
				a[i].pop_back();
			}
		}
	}
	for(int i=2 ; i<=n ; i+=2){
		if(book[i]==0){
			a[2].push_back(i);
		}
	}
	for(int i=0;i<a[2].size()-1;i+=2){
		ans.push_back(a[2][i]);
		ans.push_back(a[2][i+1]);
	}
	int answer=(ans.size()>>1);
	cout<<answer<<endl;
	for(int i=ans.size()-1;i>=0;i-=2){
		cout<<ans[i]<<" "<<ans[i-1]<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pb push_back
#define eb emplace_back
#define maxn 100005
#define eps 1e-8
#define pi acos(-1.0)
#define rep(k,i,j) for(int k=i;k<j;k++)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,int>pli;
typedef pair<int,char> pic;
typedef pair<pair<int,string>,pii> ppp;
typedef unsigned long long ull;
const long long mod=1e9+7;
/*#ifndef ONLINE_JUDGE
        freopen("1.txt","r",stdin);
#endif */
vector<int> a[maxn],ans;
bool book[maxn];

int main(){
    #ifndef ONLINE_JUDGE
     //   freopen("1.txt","r",stdin);
    #endif
    int n;
    cin>>n;
	if(n==1){
		cout<<0;
		return 0;
	}
	for(int i=3;i<=n;i++){
		if((i%2==1)&&book[i]==0 ){
			for(int j=i;j<=n;j+=i){
				if(book[j]==0){
					a[i].push_back(j);
					book[j]=1;
				}

			}
		}
	}
	for(int i=3;i<=(n/2);i++){
		if(a[i].size()>0){
			if(a[i].size()%2==1){
				a[i].erase(a[i].begin()+1);
				book[2*i]=0;
			}
			while(a[i].size()>0){
				ans.push_back(a[i].back());
				a[i].pop_back();
			}
		}
	}
	for(int i=2 ; i<=n ; i+=2){
		if(book[i]==0){
			a[2].push_back(i);
		}
	}
	for(int i=0;i<a[2].size()-1;i+=2){
		ans.push_back(a[2][i]);
		ans.push_back(a[2][i+1]);
	}
	int answer=(ans.size()>>1);
	cout<<answer<<endl;
	for(int i=ans.size()-1;i>=0;i-=2){
		cout<<ans[i]<<" "<<ans[i-1]<<endl;
	}
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <iomanip>
#include <vector>
#include <set>
#include <stack>
#include <math.h>
#include <queue>
#include <stdlib.h>
#define F first
#define S second
#define mp make_pair
#define ll long long
#define ull unsigned long long
using namespace std;
int n,used[200000];
vector <int> p;
vector < pair <int,int> > ans;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i=2;i<=n/2;i++)
    if (!used[i]){
        vector <int> t;
        t.clear();
        int cnt=0;
        for (int j=i;j<=n;j+=i)
        if (!used[j]) cnt++;
        if (cnt%2==1) {
        used[2*i]=1;
        p.push_back(2*i);
        }
        for (int j=i;j<=n;j+=i)
        if (!used[j]){
            used[j]=1;
            t.push_back(j);
        }
        for (int j=0;j<t.size();j+=2)
        if (j+1<t.size()) ans.push_back(make_pair(t[j],t[j+1]));
    }
    for (int j=0;j<p.size();j+=2)
    if (j+1<p.size()) ans.push_back(make_pair(p[j],p[j+1]));
    cout << ans.size() << endl;
    for (int i=0;i<ans.size();i++)
    cout << ans[i].F << " " << ans[i].S << endl;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <iomanip>
#include <vector>
#include <set>
#include <stack>
#include <math.h>
#include <queue>
#include <stdlib.h>
#define F first
#define S second
#define mp make_pair
#define ll long long
#define ull unsigned long long
using namespace std;
int n,used[200000];
vector <int> p;
vector < pair <int,int> > ans;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i=3;i<=n/2;i++)
    if (!used[i]){
        vector <int> t;
        t.clear();
        int cnt=0;
        for (int j=i;j<=n;j+=i)
        if (!used[j]) cnt++;
        if (cnt%2==1) {
        used[2*i]=1;
        p.push_back(2*i);
        }
        for (int j=i;j<=n;j+=i)
        if (!used[j]){
            used[j]=1;
            t.push_back(j);
        }
        for (int j=0;j<t.size();j+=2)
        if (j+1<t.size()) ans.push_back(make_pair(t[j],t[j+1]));
    }
    for (int i=2;i<=n;i++)
    if (!used[i] && i%2==0) p.push_back(i);
    for (int j=0;j<p.size();j+=2)
    if (j+1<p.size()) ans.push_back(make_pair(p[j],p[j+1]));
    cout << ans.size() << endl;
    for (int i=0;i<ans.size();i++)
    cout << ans[i].F << " " << ans[i].S << endl;
}


/*
By Tianyi Chen. All rights reserved.
Date: 2016-07-18
*/
#include<bits/stdc++.h>
using namespace std;
int n,j;
vector<pair<int,int>>ans;
int boundary;
vector<int>twos,tmp;
bool isprime[100100],used[100100];
int main() {
 memset(isprime,1,sizeof isprime);
 scanf("%d",&n);boundary=n>>1;
 for(int i=3;i<=n;++i) {
  if (i%2&&isprime[i]) {
   tmp.clear();
   for (int j=i;j<=n;j+=i) {
    isprime[j]=0;
    if (!used[j])used[j]=1,tmp.push_back(j);
   }
   while (tmp.size()>3) {
    int a=tmp.back();tmp.pop_back();
    int b=tmp.back();tmp.pop_back();
    ans.emplace_back(a,b);
   }
   if (tmp.size()==2) {
    int a=tmp.back();tmp.pop_back();
    int b=tmp.back();tmp.pop_back();
    ans.emplace_back(a,b);
   }
   if (tmp.size()==3) {
    int a=tmp[2];
    int b=tmp[0];
    ans.emplace_back(a,b);
    twos.push_back(tmp[1]);
   }
  }
 }
 for (int i=2;i<=n;i+=2)if (!used[i])twos.push_back(i);
 while (twos.size()>=2) {
  int a=twos.back();twos.pop_back();
  int b=twos.back();twos.pop_back();
  ans.emplace_back(a,b);
 }
 printf("%d\n",ans.size());
 for (auto&&x:ans)printf("%d %d\n",x.first,x.second);
}


#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int>primes;
bool mark[100000];
bool ar[100000];
void func(int n)
{
for(int i=2;i*i<=n;i++)
if(ar[i])
continue;
else
for(int j=2;i*j<=n;j++)
ar[i*j]=true;
for(int j=3;j<=n/2;j++)
if(!ar[j])
primes.push_back(j);
vector<pair<int,int> >ans;
vector<int>temp;
for(int i=0;i<primes.size();i++)
{
vector<int>v;
for(int j=3*primes[i];j<=n;j+=primes[i])
if(!mark[j])
{
mark[j]=true;
v.push_back(j);
}
if(!mark[primes[i]])
{
mark[primes[i]]=true;
v.push_back(primes[i]);
}
if(v.size()%2==0)
{
if(!mark[2*primes[i]])
{
mark[2*primes[i]]=true;
temp.push_back(2*primes[i]);
}
for(int i=0;i<v.size();i=i+2)
ans.push_back(make_pair(v[i],v[i+1]));
}
else
{
if(!mark[2*primes[i]])
{
mark[2*primes[i]]=true;
v.push_back(2*primes[i]);
}
for(int i=0;i<v.size();i=i+2)
ans.push_back(make_pair(v[i],v[i+1]));
}
}
for(int j=2;j<=n;j=j+2)
if(!mark[j])
temp.push_back(j);
for(int i=0;i<temp.size();i=i+2)
if(i+1<temp.size())
ans.push_back(make_pair(temp[i],temp[i+1]));
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}

int main()
{
ios_base::sync_with_stdio(false);
int n;
cin>>n;
func(n);
return 0;
}


#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int>primes;
bool mark[100002];
bool ar[100002];
void func(int n)
{
for(int i=2;i*i<=n;i++)
if(ar[i])
continue;
else
for(int j=2;i*j<=n;j++)
ar[i*j]=true;
for(int j=3;j<=n/2;j++)
if(!ar[j])
primes.push_back(j);
vector<pair<int,int> >ans;
vector<int>temp;
for(int i=0;i<primes.size();i++)
{
vector<int>v;
for(int j=3*primes[i];j<=n;j+=primes[i])
if(!mark[j])
{
mark[j]=true;
v.push_back(j);
}
if(!mark[primes[i]])
{
mark[primes[i]]=true;
v.push_back(primes[i]);
}
if(v.size()%2==0)
{
if(!mark[2*primes[i]])
{
mark[2*primes[i]]=true;
temp.push_back(2*primes[i]);
}
for(int i=0;i<v.size();i=i+2)
ans.push_back(make_pair(v[i],v[i+1]));
}
else
{
if(!mark[2*primes[i]])
{
mark[2*primes[i]]=true;
v.push_back(2*primes[i]);
}
for(int i=0;i<v.size();i=i+2)
if(i+1<v.size())
ans.push_back(make_pair(v[i],v[i+1]));
}
}
for(int j=2;j<=n;j=j+2)
if(!mark[j])
temp.push_back(j);
for(int i=0;i<temp.size();i=i+2)
if(i+1<temp.size())
ans.push_back(make_pair(temp[i],temp[i+1]));
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}

int main()
{
ios_base::sync_with_stdio(false);
int n;
cin>>n;
func(n);
return 0;
}


#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int>primes;
bool mark[100000];
bool ar[100000];
void func(int n)
{
for(int i=2;i*i<=n;i++)
if(ar[i])
continue;
else
for(int j=2;i*j<=n;j++)
ar[i*j]=true;
for(int j=3;j<=n/2;j++)
if(!ar[j])
primes.push_back(j);
vector<pair<int,int> >ans;
vector<int>temp;
for(int i=0;i<primes.size();i++)
{
vector<int>v;
for(int j=3*primes[i];j<=n;j+=primes[i])
if(!mark[j])
{
mark[j]=true;
v.push_back(j);
}
if(!mark[primes[i]])
{
mark[primes[i]]=true;
v.push_back(primes[i]);
}
if(v.size()%2==0)
{
if(!mark[2*primes[i]])
{
mark[2*primes[i]]=true;
temp.push_back(2*primes[i]);
}
for(int i=0;i<v.size();i=i+2)
ans.push_back(make_pair(v[i],v[i+1]));
}
else
{
if(!mark[2*primes[i]])
{
mark[2*primes[i]]=true;
v.push_back(2*primes[i]);
}
for(int i=0;i<v.size();i=i+2)
ans.push_back(make_pair(v[i],v[i+1]));
}
}
for(int j=2;j<=n;j=j+2)
if(!mark[j])
temp.push_back(j);
for(int i=0;i+1<temp.size();i=i+2)
ans.push_back(make_pair(temp[i],temp[i+1]));
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}

int main()
{
ios_base::sync_with_stdio(false);
int n;
cin>>n;
func(n);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+2;
bool v[N],use[N];
int main(){
	int n,i,j;
	long long T;
	vector<int> p;
	scanf("%d",&n);
	for(i=2;i<=n;++i){
		if(!v[i]){
			p.push_back(i);
			T = i;T*= i;
			while(T<=n)
				v[T]=1,T+=i;
		}
	}
	vector<pair<int,int> >ret;
	vector<int> x;
	for(i=p.size()-1;i>=1;--i){
		j = p[i];x.clear();
		while(j<=n){
			if(!use[j]){
				x.push_back(j);
			}
			j = j + 2*p[i];
		}
		for(j=0;j<x.size();j+=2){
			if(j==x.size()-1)break;
			ret.push_back(make_pair(x[j],x[j+1]));
			use[x[j]]=1,use[x[j+1]]=1;
		}
		if(x.size()&1){
			if(p[i]*2<=n){
				ret.push_back(make_pair(x.back(),p[i]*2));
				use[x.back()]=1,use[p[i]*2]=1;
			}
		}
	}
	x.clear();
	for(i=2;i<=n;i+=2)
		if(!use[i])
			x.push_back(i);
	int sz = x.size()/2;sz*=2;
	for(i=0;i<sz;i+=2)
		ret.push_back(make_pair(x[i],x[i+1]));
	printf("%d\n",(int)ret.size());
	for(i=0;i<ret.size();++i)
		printf("%d %d\n",ret[i].first,ret[i].second);
}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int tot, n, prime[100010], tot2, vis[100010], a[100010], b[100010], cnt1, cnt2, tot3, notuse[100010];
bool use[100010];

struct node
{
    int x, y;
} e[100010];

void init()
{
    for (int i = 2; i <= n; i++)
    {
        if (!vis[i])
            prime[++tot2] = i;
        for (int j = 1; j <= tot2; j++)
        {
            int t = prime[j] * i;
            if (t > n)
                break;
            vis[t] = 1;
            if (i % prime[j] == 0)
                break;
        }
    }
}

int main()
{
    scanf("%d", &n);
    init();
    for (int i = 2; i <= tot2; i++)
    {
        memset(a, 0, sizeof(a));
        cnt1 = 0;
        a[++cnt1] = prime[i];
        for (int j = 3; j * prime[i] <= n; j++)
            if (!use[j * prime[i]])
                a[++cnt1] = j * prime[i];
        if (2 * prime[i] <= n && !use[2 * prime[i]])
        {
            if (cnt1 % 2 == 1)
                a[++cnt1] = 2 * prime[i];
            else
            {
                use[2 * prime[i]] = 1;
                notuse[++tot3] = 2 * prime[i];
            }
        }
        for (int j = 1; j + 1 <= cnt1; j += 2)
        {
            e[++tot].x = a[j];
            e[tot].y = a[j + 1];
            use[a[j]] = use[a[j + 1]] = 1;
        }
    }
    for (int i = 1; i * 2 <= n; i++)
        if (!use[i * 2])
            notuse[++tot3] = i * 2;
    for (int i = 1; i + 1 <= tot3; i += 2)
    {
        e[++tot].x = notuse[i];
        e[tot].y = notuse[i + 1];
    }
    printf("%d\n", tot);
    for (int i = 1; i <= tot; i++)
        printf("%d %d\n", e[i].x, e[i].y);

    return 0;
}
		  			  	 	 	  			   		      	//hellobig_tq


#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const double PI = 2 * asin(1);
const double EPS = 1e-8;
const int INF = 0x3fffffff;
const int MX = 10000 + 10;

int g_n;
vector<int>vi_prime;
vector<bool>vb_prime, vb_num;
map<int, set<int>>misti_ans;

bool input()
{
    cin >> g_n;
    if(cin.eof())
        return false;
    vb_num.clear(), misti_ans.clear();
    vb_num.resize(g_n + 1);
    return true;
}
int get_prime(int n)
{
    int prime_num = 0;
    vi_prime.clear();
    vb_prime.clear();
    vb_prime.resize(n + 1);
    fill(vb_prime.begin(), vb_prime.end(), true);
    vb_prime[0] = false, vb_prime[1] = false;
    for(int i = 2; i <= n; i++)
    {
        if(vb_prime[i])
        {
            ++prime_num;
            vi_prime.push_back(i);
        }
        for(int j = 1; ((j <= prime_num) && (i * vi_prime[j - 1] <= n)); j++)
        {
            vb_prime[i * vi_prime[j - 1]] = false;
            if(i % vi_prime[j - 1] == 0)
                break;
        }
    }
    return prime_num;
}

void solve()
{
    if(g_n <= 3)
    {
        puts("0");
        return;
    }
    int prime_num = get_prime(g_n);
    int pi;
    for(pi = prime_num - 1; pi >= 0; --pi)
    {
        if(g_n / vi_prime[pi] >= 2)
            break;
    }
    //cout << i << "->" << vi_prime[i] << endl << endl;
    for(int i = pi; i >= 0; --i)
    {
        int k = vi_prime[i];
        for(int j = 1; k * j <= g_n; ++j)
        {
            if(!vb_num[k * j])
            {
                vb_num[k * j] = true;
                misti_ans[k].emplace(k * j);
            }
        }
        if(misti_ans[k].size() % 2 != 0)
        {
            misti_ans[k].erase(k * 2);
            misti_ans[2].emplace(2 * k);
        }
    }
    if(misti_ans.find(2) != misti_ans.end() && misti_ans[2].size() % 2 != 0)
        misti_ans[2].erase(2);
    /*for(auto &m : misti_ans)
    {
        cout << m.first << "->";
        for(auto &k : m.second)
            cout << ends << k;
        cout << endl;
    }*/
    int ans = 0;
    for(int i = pi; i >= 0; --i)
        ans += misti_ans[vi_prime[i]].size() / 2;
    cout << ans << endl;
    for(int i = pi; i >= 0; --i)
    {
        int k = vi_prime[i];
        for(auto it = misti_ans[k].begin(); it != misti_ans[k].end(); ++it)
            printf("%d %d\n", *it, *(it++));
    }
}

int main()
{
    //freopen("input.txt", "r", stdin);
    while(input())
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pai;

const int MAXN = 1e5 + 5;

int isnp[MAXN], n;
bool hasc[MAXN];
vector<int> g;
vector<pai> ans;

void init() {
    for (int i = 2; i <= n / 2; i++) {
        if (!isnp[i]) {
            for (int j = i * i; j <= n; j += i)
                isnp[j] = 1;
        }
    }
}

int main(int argc, char* argv[]) {
    cin >> n;
    init();
    for (int i = n / 2; i > 1; i--) {
        if (isnp[i])continue;

        g.clear();

        for (int j = i; j <= n; j += i)
            if (!hasc[j]) g.push_back(j);

        if (g.size() & 1) swap(g[1], g[g.size() - 1]);

        for (int j = 0; j < g.size() - 1; j += 2) {
            ans.push_back({g[j], g[j + 1]});
            hasc[g[j]] = hasc[g[j + 1]] = 1;
        }
    }

    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i].first << " " << ans[i].second << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pai;

const int MAXN = 1e5 + 5;

int isnp[MAXN], n;
bool hasc[MAXN];
vector<int> g;
vector<pai> ans;

void init() {
    for (int i = 2; i <= n / 2; i++) {
        if (!isnp[i]) {
            for (int j = i * 2; j <= n; j += i)
                isnp[j] = 1;
        }
    }
}

int main(int argc, char* argv[]) {
    cin >> n;
    init();
    for (int i = n / 2; i > 1; i--) {
        if (isnp[i])continue;

        g.clear();

        for (int j = i; j <= n; j += i)
            if (!hasc[j]) g.push_back(j);

        if (g.size() & 1) swap(g[1], g[g.size() - 1]);

        for (int j = 0; j < g.size() - 1; j += 2) {
            ans.push_back({g[j], g[j + 1]});
            hasc[g[j]] = hasc[g[j + 1]] = 1;
        }
    }

    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i].first << " " << ans[i].second << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,p;
	cin>>n>>p;
	vector<long long int> v(n);
	cin>>v[0];
	for(int i=1;i<n;i++)
    {
        int x;
        cin>>x;
        v[i]=v[i-1]+x;
    }
    long long int ans=0;
    for(int i=1;i<n;i++)
        ans=max(ans,(v[i-1] % p)+((v[n-1]-v[i-1])%p));
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,p;
	cin>>n>>p;
	vector<int> v(n);
	cin>>v[0];
	for(int i=1;i<n;i++)
    {
        int x;
        cin>>x;
        x=x%p;
        v[i]=v[i-1]+x;
    }
    int ans=0;
    for(int i=1;i<n;i++)
        ans=max(ans,(v[i-1] % p)+((v[n-1]-v[i-1])%p));
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, p;
    long long ans=0, cnt=0, cnt2=0;
    scanf("%I32d %I32d", &n, &p);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%I32d", &a[i]),cnt+=a[i];
    for(int i=0; i<n; i++)
        cnt2+=a[i],cnt-=a[i],ans=max(ans,(cnt%p)+(cnt2%p));
    printf("%I64d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, p, ans=0, cnt=0, cnt2=0;
    scanf("%I32d %I32d", &n, &p);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%I32d", &a[i]),cnt+=a[i];
    sort(a,a+n);
    for(int i=0; i<n; i++)
        cnt2+=a[i],cnt-=a[i],ans=max(ans,(cnt%p)+(cnt2%p));
    printf("%I32d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, p, ans=0, cnt=0, cnt2=0;
    scanf("%I32d %I32d", &n, &p);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%I32d", &a[i]),cnt+=a[i];
    for(int i=0; i<n; i++)
        cnt2+=a[i],cnt-=a[i],ans=max(ans,(cnt%p)+(cnt2%p));
    printf("%I32d\n", ans);
}


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

int main()
{ int n,p;
  cin>>n>>p;
  long int a[n],i,j;
  long long int s1,s2=0;
  cin>>a[0];
  s1=a[0];
  for(i=1;i<n;i++)
  {
      cin>>a[i];
      s2=s2+a[i];
  }
 for(j=1;j<n-1;j++)
 { if((s1+a[j])%p>=s1%p||(s2-a[j])%p>=s2%p)
     {
         s1=s1+a[j];
         s2=s2-a[j];
     }
     else
        break;

 }
 cout<<(s1%p)+(s2%p);

  return 0;
}


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

int main()
{ int n,p;
  cin>>n>>p;
  long int a[n],i,j;
  long long int s1,s2=0;
  cin>>a[0];
  s1=a[0];
  for(i=1;i<n;i++)
  {
      cin>>a[i];
      s2=s2+a[i];
  }
 for(j=1;j<n-1;j++)
 { if((s1+a[j])%p>s1%p)
     {
         s1=s1+a[j];
         s2=s2-a[j];
     }
     else
        break;

 }
 cout<<(s1%p)+(s2%p);

  return 0;
}


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

int main()
{ int n,p;
  cin>>n>>p;
  long int a[n],i,j;
  long long int s1,s2=0;
  cin>>a[0];
  s1=a[0];
  for(i=1;i<n;i++)
  {
      cin>>a[i];
      s2=s2+a[i];
  }
 for(j=1;j<n-1;j++)
 { if((s1+a[j])%p>=s1%p)
     {
         s1=s1+a[j];
         s2=s2-a[j];
     }
     else
        break;

 }
 cout<<(s1%p)+(s2%p);

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int n, p;
int a[100005];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	int z=0, q=0, s=0;
	cin >> n >> p;
	for (int i=1; i<=n; i++)
		cin >> a[i], z += a[i] % p;
	for (int i=1; i<n; i++)
		q += a[i] % p, s = max(s, q%p + (z-q)%p);
	cout << s << '\n';
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int n, p;
int a[100005];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	int z=0, q=0, s=0;
	cin >> n >> p;
	for (int i=1; i<=n; i++)
		cin >> a[i], z += a[i];
	for (int i=1; i<n; i++)
		q += a[i], s = max(s, q%p + (z-q)%p);
	cout << s << '\n';
}


// be name khoda
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5;
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int n, d, x, sum = 0;
        cin >> n >> d;
        for (int i = 0; i < n; i++)
        {
                cin >> x;
                sum += x;
        }
        for (int i = 2 * (d - 1); i >= 0; i--)
        {
                if (i % d == sum % d && sum >= sum % d)
                {
                        cout << i;
                        break;
                }
        }
        return 0;
}


// be name khoda
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5;
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int n, d, x, sum = 0;
        cin >> n >> d;
        if (n==2&&d==2) {
                int x1,x2;
                cin >> x1 >> x2;
                if (x1%2==0&&x2%2==0) cout<<0;
                else {
                        sum=x1+x2;
                        for (int i = 2 * (d - 1); i >= 0; i--)
                        {
                                if (i % d == sum % d && sum >= sum % d)
                                {
                                        cout << i;
                                        break;
                                }
                        }
                }
        }
        else {
        for (int i = 0; i < n; i++)
        {
                cin >> x;
                sum += x;
        }
        for (int i = 2 * (d - 1); i >= 0; i--)
        {
                if (i % d == sum % d && sum >= sum % d)
                {
                        cout << i;
                        break;
                }
        }
        }
        return 0;
}


// be name khoda
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5;
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        long long a, p, num = 0, sum = 0, part = 1, x;
        cin >> a >> p;
        for (int i = 1; i < a; i++)
        {
                cin >> x;
                sum = (sum + x) % p;
                if (sum > num)
                        num = sum;
        }
        cin >> x;
        (sum += x) %= p;
        sum = num + (sum + p - num) % p;
        cout << sum << endl;
        return 0;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;

bool scan_column_for_bricks(const vector<vector<char>>& walls, int col) {

	for (int r = 0; r < walls.size(); r++) {
		if (walls[r][col] == 'B')
			return true;
	}

	return false;
}

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

	int n, p; cin >> n >> p;

	vector<int> input(n);

	for (int& i : input)
		cin >> i;

	int it = 1;

	int max_sum = INT_MIN;

	while(it++ != n - 1) {

		int suml = 0;
		for (int i = 0; i < it; i++) {
			suml += input[i];
		}
		suml %= p;
		int sumr = 0;
		for (int i = it; i < n; i++) {
			sumr += input[i];
		}
		sumr %= p;

		max_sum = max(max_sum, suml + sumr);
	}

	cout << max_sum;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;

bool scan_column_for_bricks(const vector<vector<char>>& walls, int col) {

	for (int r = 0; r < walls.size(); r++) {
		if (walls[r][col] == 'B')
			return true;
	}

	return false;
}

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

	int n, p; cin >> n >> p;

	vector<int> input(n);

	for (int& i : input)
		cin >> i;

	vector<long long> prefix(n);
	vector<long long> suffix(n);

	prefix[0] = input[0];
	suffix[n - 1] = input[n - 1];

	for (int i = 1; i < n; i++) {
		prefix[i] = prefix[i - 1] + input[i];
	}
	for (int i = n - 2; i >= 0; i--) {
		suffix[i] = suffix[i + 1] + input[i];
	}

	int it = 1;
	long long max_sum = INT_MIN;

	while(it != n) {

		long long suml = prefix[it-1] % p;
		long long sumr = suffix[it] % p;

		max_sum = max(max_sum, suml + sumr);

		it++;
	}

	cout << max_sum;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;

bool scan_column_for_bricks(const vector<vector<char>>& walls, int col) {

	for (int r = 0; r < walls.size(); r++) {
		if (walls[r][col] == 'B')
			return true;
	}

	return false;
}

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

	int n, p; cin >> n >> p;

	vector<int> input(n);

	for (int& i : input)
		cin >> i;

	int it = 1;

	int max_sum = INT_MIN;

	while(it != n) {

		int suml = 0;
		for (int i = 0; i < it; i++) {
			suml += input[i];
		}
		suml %= p;
		int sumr = 0;
		for (int i = it; i < n; i++) {
			sumr += input[i];
		}
		sumr %= p;

		max_sum = max(max_sum, suml + sumr);

		it++;
	}

	cout << max_sum;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;

bool scan_column_for_bricks(const vector<vector<char>>& walls, int col) {

	for (int r = 0; r < walls.size(); r++) {
		if (walls[r][col] == 'B')
			return true;
	}

	return false;
}

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

	int n, p; cin >> n >> p;

	vector<int> input(n);

	for (int& i : input)
		cin >> i;

	int it = 1;

	long long max_sum = INT_MIN;

	while(it != n - 1) {

		long long suml = 0;
		for (int i = 0; i < it; i++) {
			suml += input[i];
		}
		suml %= p;
		long long sumr = 0;
		for (int i = it; i < n; i++) {
			sumr += input[i];
		}
		sumr %= p;

		max_sum = max(max_sum, suml + sumr);

		it++;
	}

	cout << max_sum;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;

bool scan_column_for_bricks(const vector<vector<char>>& walls, int col) {

	for (int r = 0; r < walls.size(); r++) {
		if (walls[r][col] == 'B')
			return true;
	}

	return false;
}

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

	int n, p; cin >> n >> p;

	vector<int> input(n);

	for (int& i : input)
		cin >> i;

	int it = 1;

	long long max_sum = INT_MIN;

	while(it != n) {

		long long suml = 0;
		for (int i = 0; i < it; i++) {
			suml += input[i];
		}
		suml %= p;
		long long sumr = 0;
		for (int i = it; i < n; i++) {
			sumr += input[i];
		}
		sumr %= p;

		max_sum = max(max_sum, suml + sumr);

		it++;
	}

	cout << max_sum;
}


// http://codeforces.com/contest/958/problem/C1

#include <iostream>

using namespace std;

const int maxN = 100002;
int values[maxN];
int prefixSum[maxN] = {}, suffixSum[maxN] = {};

int main() {
	int n, p, best = 0;

	cin >> n >> p;
	for (int i = 1; i <= n; i++) {
		cin >> values[i];
		prefixSum[i] = (prefixSum[i - 1] + values[i])%p;
	}

	for (int i = n; i > 0; i--) {
		suffixSum[i] = (suffixSum[i + 1] + values[i])%p;
	}
	for (int i = 1; i < n; i++) {
		int sum = prefixSum[i] + suffixSum[i + 1];
		if (sum > best)
			best = sum;
	}
	cout << best << endl;

	return 0;
}


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

using namespace std;

#define size(s) (int)(s).size()

typedef long long ll;
typedef int long long;

const int INF = 1e9;

void bye() {
	cout << '\n';
	exit(0);
}

unsigned main() {
	int n, p;
	cin >> n >> p;
	vector<ll> a(n);
	ll sum = -1e9;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<ll> pref(n, a[0]);

	for (int i = 1; i < n; i++) {
		pref[i] = a[i] + pref[i - 1];
	}
	for (int i = 1; i < n; i++) {
		ll f, s;
		f = pref[i - 1];
		s = pref.back() - f;
		sum = max(sum, (f % p) + (s % p));
	}
	cout << sum << "\n";

	bye();
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
	int a[100010] = { 0 };
	int n, p;
	int ma = 0;
	int su = 0, su1 = 0, su2 = 0;
	int i, t;
	scanf("%d %d", &n, &p);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		su = su + a[i];
	}
	su1 = a[1];
	su2 = su - a[1];
	for (i = 2; i <= n; i++)
	{
		t = (su1%p) + (su2%p);
		if (t > ma) ma = t;
		su1 = su1 + a[i];
		su2 = su2 - a[i];
	}
	printf("%d", ma);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
	int a[100010] = { 0 };
	int n, p;
	int ma = -1;
	long long su=0, su1=0, su2=0;
	int i, t;
	scanf("%d %d", &n, &p);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		su = su + a[i];
	}
	su1 = a[1];
	su2 = su - a[1];
	for (i = 2; i <= n; i++)
	{
		t = (su1%p) + (su2%p);
		if (t > ma) ma = t;
		su1 = su1 + a[i];
		su2 = su2 - a[i];
	}
	printf("%d", ma);
	return 0;
}


#include <iostream>
#include<algorithm>
#include<string>
#include<map>
#include <iterator>
#include<iomanip>
#include<vector>
#include<cmath>
#include <cstdio>
#include<queue>
#include<stack>
#include<list>
#include<stdio.h>
#include<set>
#include <functional>

#define mod 1000000007
#define mod2 30000000
#define pa(a,b) make_pair(a,b)
#define f first
#define s second
#define pb(a)  push_back(a)
#define all(a) a.begin() , a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define LL  long long int
#define oo 1e9
#define pi 3.14159265359
#define eps  1e-9

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

LL maxx(LL a , LL b){ return a>b ?  a : b ;}
LL minn(LL a , LL b){ return a>b ?  b : a ;}

using namespace std;
int dx[] = {1 , 1 ,1 , 0 , 0  , -1 , -1  , -1  , 0 };
int dy[] = { -1 , 0  , 1 , 1 , -1 , 0 , 1 , -1 , 0 };
void boostIO()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}
LL lcm(LL a , LL b )
{
    return (a*b)/gcd(a , b);
}
LL poww(LL base, LL exp){
  base %= mod;
  LL result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % mod;
    base = (base * base) % mod;
    exp >>= 1;
  }
  return result;
}
int main()
{
    int n , p ; cin >> n >> p ;
    vector<int>a; LL sum = 0 ;

    for(int i = 0 ; i < n ; i++){
        int x; cin >> x ;
        a.pb(x) ; sum = sum + x ;
    }

    LL ans = 0 ;

    LL sum2 = 0 ;

    for(int i = 0 ; i < n - 1 ; i++){
        sum2 = sum2 + a[i] ;
        ans = max(ans , (sum2) % p + (sum - sum2) % p) ;
    }
    cout << ans << endl ;
}


#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
	int n,m,*a;
	cin>>n>>m;
	int num=0,sum=0,max=0,y=0;
	a=(int*)malloc(sizeof(int)*n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		num+=a[i];
	}
	for(int i=0;i<n-1;i++)
	{
		sum+=a[i];
		num-=a[i];
		y=sum%m+num%m;
		if(y>max)
			max=y;
	}
		cout<<max<<endl;
		return 0;
}


#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
	int n,m,*a;
	cin>>n>>m;
	long long num=0,sum=0,max=0,y=0;
	a=(int*)malloc(sizeof(int)*n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		num+=a[i];
	}
	for(int i=0;i<n-1;i++)
	{
		sum+=a[i];
		num-=a[i];
		y=sum%m+num%m;
		if(y>max)
			max=y;
	}
		cout<<max<<endl;
		return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
long long int N[100100];
long long int NN[100100];
long long int n,p;
using namespace std;
void init(long long int n)
{
    for(long long int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0]%p;
    for(long long int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(cin>>n>>p)
    {
        long long int SUM=0;
        for(long long int i=0;i<n;i++)
        {
            cin>>N[i];
        }
        for(long long int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        long long int k;
        long long int maxx;
        maxx=N[1]%p+SUM;
        for(long long int k=1;k<=n;k++)
        {
            long long int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
int N[100100];
int NN[100100];
int n,p;
using namespace std;
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0];
    for(int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        int SUM=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&N[i]);
        }
        for(int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        int k;
        int maxx;
        maxx=N[0]%p+SUM;
        for(int k=1;k<=n;k++)
        {
            int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
int N[100100];
int NN[100100];
int n,p;
using namespace std;
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0];
    for(int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        int SUM=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&N[i]);
        }
        for(int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        int k;
        int maxx;
        maxx=N[1]%p+SUM;
        for(int k=1;k<=n;k++)
        {
            int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
int N[100100];
int NN[100100];
int n,p;
using namespace std;
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0];
    for(int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        int SUM=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&N[i]);
        }
        for(int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        int k;
        int maxx;
        maxx=N[1]%p+SUM;
        for(int k=0;k<=n;k++)
        {
            int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
int N[100100];
long long int NN[100100];
int n,p;
using namespace std;
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0]%p;
    for(int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        int SUM=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&N[i]);
        }
        for(int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        int k;
        int maxx;
        maxx=N[1]%p+SUM;
        for(int k=1;k<=n;k++)
        {
            int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
long long int N[100100];
long long int NN[100100];
long long int n,p;
using namespace std;
void init(long long int n)
{
    for(long long int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0];
    for(long long int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(cin>>n>>p)
    {
        long long int SUM=0;
        for(long long int i=0;i<n;i++)
        {
            cin>>N[i];
        }
        for(long long int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        long long int k;
        long long int maxx;
        maxx=N[0]%p+SUM;
        for(long long int k=1;k<=n;k++)
        {
            long long int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
int N[100100];
using namespace std;
int main()
{
    int n,p;
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        int SUM=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&N[i]);
        }
        for(int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        int k;
        int maxx;
        maxx=N[1]%p+SUM;
        for(int k=1;k<=n;k++)
        {
            int max2=0,max3=0;
            for(int m=0;m<=k;m++)
            {
                max2=(max2+N[m])%p;
            }
            for(int nn=k+1;nn<=n-1;nn++)
            {
                max3=(max3+N[nn])%p;
            }
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
int N[100100];
int NN[100100];
int n,p;
using namespace std;
void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        NN[i]=0;
    }
    NN[0]=N[0]%p;
    for(int i=1;i<n;i++)
    {
        NN[i]=(NN[i-1]+N[i]);
    }
}
int main()
{
    while(scanf("%d %d",&n,&p)!=EOF)
    {
        int SUM=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&N[i]);
        }
        for(int i=1;i<n;i++)
        {
            SUM=(SUM+N[i])%p;
        }
        init(n);
        int k;
        int maxx;
        maxx=N[0]%p+SUM;
        for(int k=1;k<=n;k++)
        {
            int max2=0,max3=0;
            max2=NN[k]%p;
            max3=(NN[n-1]-NN[k])%p;
            if(maxx<max2+max3)
            {
                maxx=max2+max3;
            }
        }
        cout<<maxx<<endl;
    }
}


#include <iostream>
#include <cstring>
using namespace std;
int a[110000];
int l[110000];
int r[110000];
int main()
{
	cin.tie(0);
	cin.sync_with_stdio(false);
	int n, p;
	cin >> n >> p;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		l[i] = (l[i - 1] + a[i]) % p;
	}
	for (int i = n; i >= 1; i--)
	{
		r[i] = (r[i + 1] + a[i]) % p;
	}
	int ans = l[1] + r[2];
	for (int i = 2; i < n; i++)
	{
		if (l[i] + r[i + 1] > ans)
		{
			ans = l[i] + r[i + 1];
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<utility>
#include<map>
#include <unordered_map>
#include <bitset>
using namespace std;
typedef long long ll;
int N, A[100004], D[100004], E[100004], P;
int main() {
	int i, a;
	cin >> N >> P;
	for (i = 1; i <= N; i++) {
		cin >> A[i];
		D[i] = (D[i - 1] + A[i]) % P;
	}
	for (i = N; i >= 1; i--) {
		E[i] = (E[i + 1] + A[i]) % P;
	}
	int ans = 0;
	for (i = 1; i < N; i++) {
		a = D[i] + E[i + 1];
		if (ans < a) ans = a;
	}
	cout << ans;
}


#include <iostream>
#include <cmath>
using namespace std;
int main()
{

	long long int a[100000], x,s=0,z, y = 0, c=0;
	cin >> x>>z;
	for (int i = 0; i < x; i++)
	{
		cin >> a[i]; s += a[i];
	}
	y = a[0];
	for (int i = 1; i < x; i++)
	{
		y = y + a[i];
		c =max( (y%z)+ ((s - y) % z),c);
	}
	cout << c << endl;
}


#include <iostream>
#include <cmath>
using namespace std;
int main()
{

	long long int a[100000], x,s=0,z, y = 0, c=0;
	cin >> x>>z;
	for (int i = 0; i < x; i++)
	{
		cin >> a[i]; s += a[i];
	}
	y = a[0];
	for (int i = 0; i < x; i++)
	{
		y =+ a[i];
		c =max( (y%z)+ ((s - y) % z),c);
	}
	cout << c << endl;
}


#include <iostream>
#include <math.h>
using namespace std;
int main()
{

	long long int a[100000], x,s=0,z, y = 0, c=0;
	cin >> x>>z;
	for (int i = 0; i < x; i++)
	{
		cin >> a[i]; s += a[i];
	}
	y = a[0];
	for (int i = 1; i < x; i++)
	{
		y = y + a[i];
		c =fmax( (y%z)+ ((s - y) % z),c);
	}
	cout << c << endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
int main()
{
	int n,m,a[100001];
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int sum=0;
		for(int i=1;i<=n;i++)
		{
		scanf("%d",&a[i]);
		sum=sum+a[i];
	}
	int maxn=0,sum1=0;
	int sum2=0;
	for(int i=1;i<=n;i++)
	{
		sum1=0;
		sum2=sum2+a[i];
		sum1=sum2%m+(sum-sum2)%m;
		maxn=max(maxn,sum1);
	}
	printf("%d\n",maxn);
	}
	return 0;
 }


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
int main()
{
	int n;
	long long a[100001],m;
	while(scanf("%d%lld",&n,&m)!=EOF)
	{
		long long sum=0;
		for(int i=1;i<=n;i++)
		{
		scanf("%lld",&a[i]);
		sum=sum+a[i];
	}
	long long maxn=0,sum1=0;
	long long sum2=0;
	for(int i=1;i<=n;i++)
	{
		sum1=0;
		sum2=sum2+a[i];
		sum1=sum2%m+(sum-sum2)%m;
		maxn=max(maxn,sum1);
	}
	printf("%lld\n",maxn);
	}
	return 0;
 }


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;

typedef long long LL;
int n;
const int maxn = 100005;
int a[maxn];
int sum[maxn];

int main(){
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		a[i] = a[i] % m;
		sum[i] = sum[i - 1] + a[i];
	}
	int Max = -1;
	for (int i = 1; i <= n; i++){
		Max = max(Max, sum[i] % m + (sum[n] - sum[i]) % m);
	}
	cout << Max << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <functional>
using namespace std;

typedef long long ll;

int n, l;
ll sum[100009];

int main()
{
	cin >> n;
	cin >> l;

	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		sum[i] = sum[i - 1] + x;
	}

	ll result = 0;
	for (int i = 1; i <= n - 1; i++) {
		result = max(result, sum[i] % l + (sum[n] - sum[i]) % l);
	}

	cout << result << endl;

	return 0;
}


line = input().split()
n = int(line[0])
p = int(line[1])
a = input().split()
suma = 0
for i in range(n):
    a[i] = int(a[i])
    suma += a[i]

Max = 0
sum = 0

for i in range(n-1):
    sum += a[i]
    total = (sum % p) + ((suma-sum) % p)
    if (total>Max):
        Max = total
print(Max)


line = input().split()
n = int(line[0])
p = int(line[1])
a = input().split()
for i in range(n):
    a[i] = int(a[i])

Max = 0

for i in range(n-1):
    sum = 0
    sum1 = 0
    for j in range(i+1):
        sum += a[j]
    for j in range(n-1,i,-1):
        sum1 += a[j]
    total = (sum % p) + (sum1 % p)
    if (total>Max):
        Max = total
print()
print(Max)


line = input().split()
n = int(line[0])
p = int(line[1])
a = input().split()
suma = 0
for i in range(n):
    a[i] = int(a[i])
    suma += a[i]

Max = 0

for i in range(n-1):
    sum = 0
    for j in range(i+1):
        sum += a[j]
    total = (sum % p) + ((suma-sum) % p)
    if (total>Max):
        Max = total
print(Max)


import math

def solve(n,P,A):
	MAX = 0
	score1 = A[0] %P
	score2 = sum(A[1:n+1])%P
	for i in range(1,n):
		delta = A[i] % P
		score1 = score1 + delta
		if(score1 >= P):
			score1 = score1 - P
		score2 = score2 - delta
		if(score2 < 0):
			score2 = score2 + P

		score = score1+score2
		if(score > MAX):
			MAX = score
	return MAX

n = [int(i) for i in input().split()]
P = n[1]
n = n[0]
A = [int(i) for i in input().split()]

print(solve(n,P,A))


import math

def solve(n,P,A):

	score1 = A[0] % P
	score2 = sum(A[1:]) % P
	MAX  = score1 + score2
	for i in range(1,n-1):
		delta = A[i] % P
		score1 = score1 + delta
		if(score1 >= P):
			score1 = score1 - P
		score2 = score2 - delta
		if(score2 < 0):
			score2 = score2 + P
		score = score1+score2
		if(score > MAX):
			MAX = score
	return MAX

n = [int(i) for i in input().split()]
P = n[1]
n = n[0]
A = [int(i) for i in input().split()]

print(solve(n,P,A))


import math

def solve(n,P,A):
	MAX = 0
	for i in range(1,n):
		score = findScore(A[0:i],A[i:n+1],P)
		if(score > MAX):
			MAX = score
	return MAX

def findScore(A,B,P):
	return((sum(A) % P) + sum(B) % P)

n = [int(i) for i in input().split()]
P = n[1]
n = n[0]
A = [int(i) for i in input().split()]

print(solve(n,P,A))


n, p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s1, s2 = a[0], sum(a) - a[0]
ans = s1 % p + s2 % p
for i in range(n - 1):
    s1 = (s1 + a[i]) % p
    s2 = (s2 - a[i]) % p
    ans = max(ans, s1 + s2)
print(ans)


n, m = map(int, input().split())
a = [0] + list(map(int, input().split()))
for i in range(n):
  a[i + 1] = (a[i + 1] + a[i]) % m
ans = 0
for i in range(1, n):
  x = a[i] + ((a[n] - a[i]) % m + m) % m
  ans = max(ans, x)
print(ans)


n, p = map(int, input().split())
a = list(map(int, input().split()))
a = [c % p for c in a]
s = sum(a)
sp = s % p
if sp == s or sp + 1 == p:
    print(sp)
else:
    print(sp + p)


from itertools import accumulate

N, p = map(int, input().split())
A = list(map(int, input().split()))

left = [0] + list(accumulate(A))
right = list(accumulate(A[::-1]))[::-1] + [0]

ans = [(left[i] % p) + (right[i] % p) for i in range(N + 1)]

print(max(ans))


def getSum(BITTree,i):
  s = 0
  i += 1
  while i > 0:
    s += BITTree[i]
    i -= i & (-i)
  return s

def updatebit(BITTree , n , i ,v):
    i += 1
    while i <= n:
        BITTree[i] += v
        i += i & (-i)

def construct(arr, n):
    BITTree = [0]*(n+1)
    for i in range(n):
      updatebit(BITTree, n, i, arr[i])
    return BITTree

def getS(a, n, p):
  BITTree = construct(a, n)
  S, max_S = 0, -1
  for i in range(1, n - 1):
    s1, s2 = getSum(BITTree, i - 1), getSum(BITTree, n - 1)
    S = (s1)%p + (s2 - s1)%p
    if S > max_S: max_S = S
  return max_S

n, p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
print(getS(a, n, p))


n, p = [int(i) for i in input().split()]
soma = [0 for i in range(n + 1)]
for idx, i in enumerate(input().split()):
  soma[idx + 1] = soma[idx] + int(i)%p

r = -float("inf")
for i in range(1, n):
  r = max(r, soma[i]%p + (soma[n] - soma[i])%p)

print(r)


N,p = input().split()
A = list(input().split())
R=[]
for i in range(1,int(N)):
    Sx=0
    Sy=0
    X=A[0:i]
    Y=A[i:int(N)]
    for x in range(len(X)):
        Sx+=int(X[x])
    for y in range(len(Y)):
        Sy+=int(Y[y])
    while Sx>=int(p):
        Sx-=int(p)
    while Sy>=int(p):
        Sy-=int(p)
    R.append(Sx+Sy)
print(max(R))


N,p = input().split()
A = list(map(int,input().split()))
R=0
for i in range(1,int(N)):
    Sx=sum(A[0:i])
    Sy=sum(A[i:int(N)])
    Sx=Sx%int(p)
    Sy=Sy%int(p)
    if R<Sx+Sy:
        R=Sx+Sy
print(R)


N,p =map(int,input().split())
A = list(map(int,input().split()))
R = 0
B=[]
Sx = 0
Sy=sum(A)
for i in range(0,N-1):
    Sx+=A[i]
    Sy-=A[i]
    Sx=Sx%p
    Sy=Sy%p
    if R<Sx+Sy:
        R=Sx+Sy
print(R)


n,k = map(int,input().split())
l = list(map(int,input().split()))
minimum = 0
suma=0
sumb=sum(l)
for j in range(0,n):
    suma+=l[j]
    sumb-=l[j]
    value = suma%k+sumb%k
    if(value>minimum):
        minimum = value
print(minimum)


n,p = map(int,input().split())
listA = list(map(int,input().split()))
sumA = sum(listA)
ans = []
v = 0
for i in range(n-1):
    v+=listA[i]
    sumA-=listA[i]
    ans.append((v%p)+sumA%p)
print(max(ans))


s=input().split()
lis=input().split()
summ=0
ans=[]
sum1=0
sum2=summ
for i in lis:
    summ+=int(i)
sum2=summ
for i in range(int(s[0])-1):
    sum1+=int(lis[i])
    sum2-=int(lis[i])
    ans.append(sum1%int(s[1])+sum2%int(s[1]))
ans.sort()
print(ans)


s=input().split()
lis=input().split()
summ=0
ans=[]
sum1=0
sum2=summ
for i in lis:
    summ+=int(i)
sum2=summ
for i in range(int(s[0])-1):
    sum1+=int(lis[i])
    sum2-=int(lis[i])
    ans.append(sum1%int(s[1])+sum2%int(s[1]))
ans.sort()
print(ans[len(ans)-1])


s=input().split()
lis=input().split()
summ=0
ans=[]
for i in lis:
    summ+=int(i)
for i in range(1,int(s[0])):
    sum1=0
    sum2=summ
    for j in range(i):
        sum1+=int(lis[j])
        sum2-=int(lis[j])
    ans.append(sum1%int(s[1])+sum2%int(s[1]))
ans.sort()
print(ans[len(ans)-1])


n,p=(int(i) for i in input().split())
l=[int(i) for i in input().split()]
pref=[l[0]%p]
suff=[l[n-1]%p]
for i in range(1,n-1):
    pref.append((pref[i-1]+(l[i]%p))%p)
for i in range(1,n-1):
    suff.append((suff[i-1]+(l[n-i-1]%p))%p)
m=0
suff=suff[::-1]
for i in range(n-1):
    m=max(pref[i]+suff[i],m)
print(m)


# from dust i have come dust i will be

n,p=map(int,input().split())
a=list(map(int,input().split()))

s=sum(a)
mx=0
t=0
for i in range(n-1):
    t+=a[i]
    x=(t%p)+(s-t)%p
    mx=max(mx,x)

print(mx)


line1=list(map(int,input().split()))
nums=list(map(int,input().split()))
print(line1,nums)
n,p=line1[0],line1[1]
sum_all=sum(nums)
sum_left=nums[0]
sum_right=sum_all-sum_left
res=sum_left%p + sum_right%p
for i in range(1,n-1):
    sum_left+=nums[i]
    sum_right-=nums[i]
    temp=sum_left%p + sum_right%p
    res=max(res,temp)

print(res)


line1=list(map(int,input().split()))
nums=list(map(int,input().split()))
n,p=line1[0],line1[1]
sum_all=sum(nums)
sum_left=nums[0]
sum_right=sum_all-sum_left
res=sum_left%p + sum_right%p
for i in range(1,n-1):
    sum_left+=nums[i]
    sum_right-=nums[i]
    temp=sum_left%p + sum_right%p
    res=max(res,temp)

print(res)


n,p=map(int,input().split())
a=list(map(int,input().split()))

sum1=0
pre=[]
for i in range(n):
    sum1+=a[i]
    sum1=sum1%p
    pre.append(sum1)
post=[0 for i in range(n)] #ii is not included
sum1=0
for i in range(n-1,-1,-1):
        post[i]=sum1
        sum1+=a[i]
        sum1=sum1%p
s=0
for i in range(n):
    s=max(pre[i]+post[i],s)
print(s)


N,P = map(int,input().split())
src = list(map(int,input().split()))
K = sum(src)%P
ans = tmp = 0
for a in src:
    tmp += a
    tmp %= P
    ans = max(ans, tmp + (K-tmp)%P)
print(ans)


n,k = map(int, input().strip().split(' '))
lst = list(map(int, input().strip().split(' ')))
s=sum(lst)
s2=0
m=s%k
for i in range(n-1):
    s2+=lst[i]
    s-=lst[i]
    if (s2%k)+(s%k)>m:
        m=(s2%k)+(s%k)
print(m)


n, p = map(int, input().split())
a = [int(x) for x in input().split()]
s = sum(a)
ans = -1; psum = 0
for i in a:
    psum += i
    ans = max(ans, psum % p + (s - psum) % p)
print(ans)


n, p = map(int, input().split())
pre = [0] * 100100
A = list(map(int, input().split()))
for i in range(n):
  pre[i + 1] = (pre[i] + A[i]) % p
print(max((pre[i] - pre[0]) % p + (pre[n] - pre[i]) % p for i in range(1, n)))


n, p = map(int, input().split())
a = list(map(int, input().split()))
a = [c % p for c in a]
s = sum(a)
sp = s % p
if sp == s or sp + 1 == p:
    print(sp)
else:
    print(sp + p)


n, p = (int(x) for x in input().split())
a = [int(x) for x in input().split()]

sum1 = int(0)
sum2 = int(0)

for i in range(0, n):
    sum2 = sum2 + a[i]
k = n-1
b = [0]
for i in range(0, k):
    sum1 = sum1 + a[i]
    sum2 = sum2 - a[i]
    b.append((sum1 % p) + (sum2 % p))
print(max(b))


n, p = map(int, input().split())

a = list(map(int, input().split()))

m = -1

s = sum(a)

sums = 0
for i in a:

	sums+=i

	m = max(m, sums%p+(s-sums)%p)

print(m)


s = input().split()
n, p = int(s[0]), int(s[1])
cl = list(map(int, input().split()))
mx = 0
for i in range(1, n-1):
  x = sum(cl[:i])%p + sum(cl[i:])%p
  if x>mx:
    mx = x

print(mx)


s = input().split()
n, p = int(s[0]), int(s[1])
cl = list(map(int, input().split()))
mx = 0
sm = sum(cl)
qs = cl[0]
for i in range(1, n):
  y = qs%p + (sm-qs)%p
  if y>mx:
    mx = y
  qs+=cl[i]

print(mx)


s = input().split()
n, p = int(s[0]), int(s[1])
cl = list(map(int, input().split()))
mx = 0
sm = sum(cl)
for i in range(1, n):
  x = sum(cl[:i])
  y = x%p + (sm-x)%p
  if y>mx:
    mx = y

print(mx)


s = input().split()
n, p = int(s[0]), int(s[1])
cl = list(map(int, input().split()))
mx = 0
for i in range(1, n):
  x = sum(cl[:i])%p + sum(cl[i:])%p
  if x>mx:
    mx = x

print(mx)


#http://codeforces.com/contest/958/problem/C1

N,p=list(map(int,input().split()))
A=list(map(int,input().split()))
sum1=A[0]
sum2=sum(A[1:])
ans=(sum1%p)+(sum2%p)
for i in range(1,N-1):
    sum1+=A[i]
    sum2-=A[i]
    ans1=sum1%p
    ans2=sum2%p
    ans=max(ans1+ans2,ans)
print(ans)


np = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

max = 0

mod = []
sum = 0
for i in range(0, np[0]) :
    sum += a[i]
    mod.append(sum % np[1])
index = 0
mm = 0
for i in range(0, np[0]) :
    if mod[i] > mm :
        mm = mod[i]
        index = i

print(index)
sum2 = 0;
for i in range(index+1, np[0]) :
    sum2 += a[i]
sum2 = sum2 % np[1]

print(mod[index] + sum2)


Np = [int(x) for x in input().split()]
N = Np[0]
p = Np[1]

A = [int(x) for x in input().split()]

n1 = 0
n2 = 0

j = 1
for j in range(1, N-1):
    for i in range(0, j):
        n1 += A[i]%p
        print(n1)

np = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

max = 0
for i in range(1, np[0]) :
    sum1 = 0
    for j in range(0, i) :
        sum1 += a[j]
    sum1 = sum1 % np[1]

    sum2 = 0
    for j in range(i, np[0]) :
        sum2 += a[j]

    sum2 = sum2 % np[1]
    if sum1 + sum2 > max:
        max = sum1 + sum2

print(max)


np = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

max = 0

mod = []
sum = 0
for i in range(0, np[0]) :
    sum += a[i]
    mod.append(sum % np[1])
index = 0
mm = 0
for i in range(0, np[0]) :
    if mod[i] > mm :
        mm = mod[i]
        index = i

sum2 = 0;
for i in range(index+1, np[0]) :
    sum2 += a[i]
sum2 = sum2 % np[1]

print(mod[index] + sum2)


np = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

max = 0
for i in range(1, np[0]) :
    sum1 = 0
    for j in range(0, i) :
        sum1 += a[j]
    sum1 = sum1 % np[1]

    sum2 = 0
    for j in range(i, np[0]) :
        sum2 += a[j]
    sum2 = sum2 % np[1]

    if sum1 + sum2 > max:
        max = sum1 + sum2
        if max >= (np[1] - 1) * 2:
            break
print(max)


np = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

max = 0
for i in range(1, np[0]) :
    sum1 = 0
    for j in range(0, i) :
        sum1 += a[j]
    sum1 = sum1 % np[1]

    sum2 = 0
    for j in range(i, np[0]) :
        sum2 += a[j]

    sum2 = sum2 % np[1]
    if sum1 + sum2 > max:
        max = sum1 + sum2

print(max)


n,p = map(int,input().split())
a = list(map(int,input().split()))
c = [a[0]]
for i in range(1,len(a)):
    c.append(c[-1]+a[i])

sums = []
for i in range(len(c)-1):
    sums.append(c[i]%p+(c[-1]-c[i])%p)
print(max(sums))


m,n=list(map(int,input().split()))
l=list(map(int,input().split()))
a=[0]*m
a[0],ma=l[0],0
for i in range(1,m):
    a[i]=a[i-1]+l[i]
for i in range(m):
    if(a[i]%n+(a[m-1]-a[i])%n)>ma:
        ma=(a[i]%n+(a[m-1]-a[i])%n)
print(ma)


m,n=list(map(int,input().split()))
l=list(map(int,input().split()))
a=[0]*m
a[0],ma=l[0],0
for i in range(1,m):
    a[i]=a[i-1]+l[i]
for i in range(m):
    if(a[i]%n+(a[m-1]-a[i])%n)>ma:
        ma=(a[i]%n+(a[m-1]-a[i])%n)
    else:
        break
print(ma)


N, p = map(int, input().split(' '))
A = [(int(el) % p) for el in input().split(' ')]
S1, S2 = A[0], sum(A[1:])%p
mx = S1+S2
for i in range(1, N):
    S1 = (S1+A[i])%p
    S2 = (S2-A[i])%p
    if S1+S2 > mx: mx = S1+S2
print(mx)


n, p = map(int, input().split())

a = list(map(int, input().split()))

resp = -1
aux = 0
tot = sum(a)

for x in a:
	aux += x
	resp = max(resp, aux%p + (tot - aux) % p)

print(resp)

# 1534609590348


import math

num=int(input())
mod=int(input())
qts=int(math.sqrt(num))
soma1=0
soma2=0

for i in range(0,qts):
	variv=int(input())
	soma1+=variv

for i in range(qts,num):
	variv=int(input())
	soma2+=variv

soma1=(soma1%mod)+(soma2%mod)

print(soma1,"\n")

# 1534624863966


import math
def func(n,k,a):
    s=sum(a)
    ms=0
    x=0
    for i in range(n):
        cs=(x%k)+(s%k)
        x+=a[i]
        s-=a[i]
        ms=max(ms,cs)
    return ms

for _ in range(1):
    n,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    print(func(n,k,a))


a,b=map(int,input().split());z=[];p=0
for i in map(int,input().split()):p=(p+i)%b;z+=[p]
p+=b
print(max(z[i]+(p-z[i])%b for i in range(a-1)))


n, p = [int(a) for a in input().strip().split(' ')]
e = [int(a) for a in input().strip().split(' ')]
max_sum = 0
s = sum(e)
current_s = 0
for i in e:
    current_s += (i % p)
    current_s %= p
    max_sum = max(max_sum,current_s + (s - current_s) % p)
print(max_sum)


n,p = list(map(int, input().split()))
a = list(map(int, input().split()))

x = a[0] % p
y = sum(a[1:]) % p
s = x+y
for i in range(1, n-1):
	x = (x+a[i]) % p
	y = (y-a[i]) % p
	s = max(s, x+y)
print(s)


n, m = [int(x) for x in input().split(' ')]
numbers = [int(x) for x in input().split(' ')]

max_res = 0
for i in range(n-1):
	tmp_a = numbers[:i+1]
	tmp_b = numbers[i+1:]
	tmp_sum = (sum(tmp_a) % m) + (sum(tmp_b) % m)
	if tmp_sum > max_res:
		max_res = tmp_sum

print(max_res)


n, m = [int(x) for x in input().split(' ')]
numbers = [int(x) for x in input().split(' ')]

max_res = 0
total_sum = sum(numbers)
sum_thus_far = 0
for i in range(n-1):
	tmp_a = sum_thus_far + numbers[i]
	tmp_b = total_sum - tmp_a
	tmp_sum = (tmp_a % m) + (tmp_b % m)
	if tmp_sum > max_res:
		max_res = tmp_sum

print(max_res)


n, p = map(int, input().split())
arr = list(map(int, input().split()))
sum_ = sum(arr)
res = 0
temp = 0
for i in arr:
    temp += i
    sum_ -= i
    res = max(res, temp % p + sum_ % p)

print(res)


n,p = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
t1= 0
t2 = sum(a)
res = sum(a)%p
for i in range(n):
    t1 += a[i]
    t2 -= a[i]
    t3 = t1%p + t2%p
    if res < t3:
        res = t3
print(res)


n, P = map(int, input().split())
a = list(map(lambda x: int(x) % P, input().split()))
pre = a[0]
suf = sum(a[1:]) % P
num = pre + suf
for i in range(1, n - 1):
    pre += a[i]
    pre %= P
    suf -= a[i]
    if suf < 0:
        suf += P

    num = max(num, pre + suf)

print(num)


np = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

max = 0
for i in range(1, np[0]) :
    sum1 = 0
    sum2 = 0

    sum1 = sum(a[:i]) % np[1]
    sum2 = sum(a[-i:]) % np[1]

    if sum1 + sum2 > max:
        max = sum1 + sum2
        if max >= (np[1] - 1) * 2:
            break

print(max)


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

template <typename T> inline void next_int(T &x) {
	x = 0; char c; bool neg = false;
	while (!isdigit(c = getchar())) if (c == '-') neg = true;
	do x = x*10 + c - 48; while (isdigit(c = getchar()));
	if (neg) x = -x;
}

template <typename T> inline void write_int(T x, char last = 0) {
	if (x < 0) putchar('-'), x = abs(x);
	char tmp[20]; int cnt = 0;
	while (x >= 10) tmp[cnt++] = x % 10 + 48, x /= 10;
	tmp[cnt] = x + 48;
	FORD(i, cnt, 0) putchar(tmp[i]);
	if (last) putchar(last);
}

int bit[SQRTN][MAXN];

int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

void update(int i , int idx , int val){
    while(idx < n+2){
        bit[i][idx] += val;
        idx += idx & -idx;
    }
}
int sum(int i , int idx){
    int res = 0;
    while(idx){
        res += bit[i][idx];
        idx -= idx & -idx;
    }
    return res;
}

/*LL query(int x){
    int bl = block[x];
    //cout << x << " " << bl << endl;
    LL res = 0;
    for(int i=0; i<bl; i++){
        //res += trees[i].sum( arr[x] - 1 );
        res+= sum(i, arr[x] - 1);
    }
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = max(1,bl*SQRTN); i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << "Previous = " << x << " " << res << endl;
    for(int i = x+1, _n = min(n+1,(bl+1)*SQRTN); i< _n; i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        //res += trees[i].sum( arr[x]-1 );
        res+= sum(i, arr[x]-1);
    }
    return res;
}*/
LL query(int x, int y, int val){
    int blx = block[x];
    int bly = block[y];
    LL res = 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] < val ) res++;
        }
        return res;
    }else{
        for(int i = x, _n = min(n+1,(blx+1)*SQRTN); i< _n; i++)
            if( arr[i] < val  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,val-1 );
        }
        for(int i = max(1,bly*SQRTN); i<=y; i++){
            if( arr[i] < val  ) res++;
        }
    }
    return res;
}

int main(){
   // freopen("test.in","r",stdin);
    next_int(n), next_int(q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            //trees[num].init(n+2);
        }
        //trees[num].add(i,1);
        update(num,i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x>y) swap(x,y);
        LL tmp1 = query(x+1,y-1,arr[x]);
        LL tmp2 = query(x+1,y-1,arr[y]);
        res += 2*tmp2 - 2*tmp1;
        if( arr[x] > arr[y] )  res--;
        else res++;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(block[y],arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(block[x],arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(block[x],arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(block[y],arr[y],1);
        // << x <<  " " << y << " " << res << endl;
        write_int(res, '\n');
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 2100000
#define SQRTN 512

struct FenwickTree {
    vector<int> bit; // binary indexed tree
    int n;

    void init(int n) {
        this->n = n;
        bit.assign(n, 0);
    }
    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r+1)) - 1)
            ret += bit[r];
        return ret;
    }
    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx+1))
            bit[idx] += delta;
    }
    int sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
    void init(vector<int> a) {
        init(a.size());
        for (size_t i = 0; i < a.size(); i++)
            add(i, a[i]);
    }
};

FenwickTree trees[SQRTN];
int n,num,q,x,y;
int arr[MAXN];
int block[MAXN];

LL query(int x){
    int bl = block[x];
    LL res = 0;
    for(int i=1; i<bl; i++){
        res += trees[i].sum( arr[x] - 1 );
    }
    for(int i = bl*SQRTN; i<x; i++){
        if( arr[i] < arr[x]  ) res++;
    }
    res = (x-1) - res;
    //cout << x << " " << res << endl;
    for(int i = x+1; i< min(n+1,(bl+1)*SQRTN); i++)
        if( arr[i] < arr[x]  ) res++;
    //cout << "In block = " << x << " " << res << " " << bl << " " << " " << num << endl;
    for(int i=bl+1; i<= num; i++){
        res += trees[i].sum( arr[x]-1 );
    }
    return res;
}

int main(){
    //freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);

    for(int i=1; i<=n; i++){
        arr[i] = i;
        if( i%SQRTN==0 ){
            num++;
            trees[num].init(n+2);
        }
        trees[num].add(i,1);
        block[i] = num;
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            cout << res << endl;
            continue;
        }
        if(x>y) swap(x,y);
        res -= query(x);
        res -= query(y);
        if( arr[x] > arr[y] ) res++;
        //cout << x <<  " " << y << " " << res << endl;
        trees[block[y]].add( arr[y],-1 );
        trees[block[x]].add( arr[x],-1 );
        swap(arr[x],arr[y]);
        trees[block[x]].add( arr[x],1 );
        trees[block[y]].add( arr[y],1 );
        res += query(x);
        res += query(y);
        // << x <<  " " << y << " " << res << endl;
        if( arr[x] > arr[y] ) res--;
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long LL;
#define MAXN 210000
#define SQRTN 450

int bit[SQRTN][MAXN];

int n,num;
int arr[MAXN];

void update(int i , int idx , int val){
    for (; idx <= n; idx += idx & -idx)
        bit[i][idx] += val;
}
int sum(int i , int idx){
    int res = 0;
	for (; idx; idx -= idx & -idx)
        res += bit[i][idx];
    return res;
}

LL query(int x, int y, int val){
    int blx = x/SQRTN;
    int bly = y/SQRTN;
    LL res = 0;
    if( x >  y ) return 0;
    if( blx == bly){
        for(int i=x; i<=y; i++){
            if( arr[i] < val ) res++;
        }
        return res;
    }else{
        for(int i = x, _n = (blx+1)*SQRTN; i< _n; i++)
            if( arr[i] < val  ) res++;
        for(int i = blx+1; i<bly; i++){
            res += sum(i,val-1 );
        }
        for(int i = bly*SQRTN; i<=y; i++){
            if( arr[i] < val  ) res++;
        }
    }
    return res;
}

int q,x,y;

int main(){
    //freopen("test.in","r",stdin);
    scanf("%d %d",&n,&q);
    //trees[0].init(n+2);
    for(int i=1; i<=n; i++){
        arr[i] = i;
        update(i/SQRTN,i,1);
    }

    LL res = 0;
    while(q--){
        scanf("%d %d",&x,&y);
        if( x==y ){
            printf("%lld\n",res);
            continue;
        }
        if(x > y) swap(x,y) ;
        LL tmp1 = query(x+1,y-1,arr[x]);
        LL tmp2 = query(x+1,y-1,arr[y]);
        res += 2*(tmp2 - tmp1);
		//if (x + 1 <= y - 1) res += ((LL)(y - x - 1) - tmp1), res -= ((LL)(y - x - 1) - tmp2);
        if( arr[x] < arr[y] ) res++;
        else res--;
        //cout << x <<  " " << y << " " << " " << arr[x] << " " << arr[y] << " " << res << endl;
        //trees[block[y]].add( arr[y],-1 );
        update(y/SQRTN,arr[y],-1);
        //trees[block[x]].add( arr[x],-1 );
        update(x/SQRTN,arr[x],-1);
        swap(arr[x],arr[y]);
        //trees[block[x]].add( arr[x], 1 );
        update(x/SQRTN,arr[x],1);
        //trees[block[y]].add( arr[y], 1 );
        update(y/SQRTN,arr[y],1);
        // << x <<  " " << y << " " << res << endl;
       	printf("%lld\n",res);
    }
    return 0;
}