/*
ID: amirnas1
PROG: cowtour
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <iomanip>
#include <math.h>
#include <bitset>
#include <sstream>
using namespace std;

/*
vector<string> prim;
string s;
vector<int> dp;

int main()
{

	ifstream cin;
	cin.open("prefix.in");
	ofstream cout;
	cout.open("prefix.out");

	string p;
	s = "";
	cin>>p;
	while (p!= ".")
	{
		prim.push_back(p);
		cin>>p;
	}
	cin.ignore();
	string l;
	while (getline(cin,l)) s += l;
	dp = vector<int> (s.size()+1, -1);
	for (int i = s.size(); i>= 0; i--)
	{
		if (i == s.size()) dp[i] = i;
		else
		{
		int m = i;
			for (int j = 0; j < prim.size(); j++)
			{
				if (i+prim[j].size() <= s.size()) if (s.substr(i,prim[j].size()) == prim[j]) m = max(m, dp[i+prim[j].size()]);
			}
			dp[i] = m;
		}
	}
	cout<<dp[0]<<endl;
}

long long dp[201][101][2];

long long mod(long long a, long long m)
{
	if (a>=0) return a%m;
	else
	{
		return (a+m)%m;
	}
}
long long solve(int n, int k, int f)
{
	if (n==0 && k==0) return 1;
	if (n == 0) return 1;
	if (k <= 0) return 0;
	if (n == 1) return 1;
	if (dp[n][k][f] != -1) return dp[n][k][f];
	int a = n-1;
	long long s = 0;
	long long r;
	for (int i = 1; i < a; i++)
	{
		r = solve(i,k-1, 0) * solve(a-i, k-1, 0);
		if (f) r -= (solve(i,k-2, 0) * solve(a-i, k-2,0));
		s += r;
		s = mod(s,9901);
	}
	return dp[n][k][f] = s;
}

int main()
{

	ifstream cin;
	cin.open("nocows.in");
	ofstream cout;
	cout.open("nocows.out");

	int n, k;
	cin>>n>>k;
	for (int i=0; i<201; i++)
	{
		for (int j = 0; j < 101; j++)
		{

			dp[i][j][0] = dp[i][j][1] = -1;
		}
	}
	cout<<solve(n,k,1)<<endl;
	//system("pause");
}

int main()
{
	int n, m;
	cin>>n>>m;
	vector<int> f(m);
	for (int i = 0; i < m; i++)
	{
		cin>>f[i];
	}
	sort(f.begin(), f.end());
	int r = 10000;
	for (int i = 0; i+n-1 < m; i++ )
	{
		if (f[i+n-1] - f[i] < r) r = f[i+n-1] - f[i];
	}
	cout<<r<<endl;
}

int gcd(int a, int b)
{
	if (b == 0) return a;
	else return gcd(b,a%b);
}

int main()
{
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int ab = gcd(a,b);
	int cd = gcd(c,d);
	a /=ab;
	b /= ab;
	c /= cd;
	d /= cd;
	if ((double)a/(double)c < (double)b/(double)d)
	{
		int ra = d*a;
		int rb = b*c;
		int gab = gcd(ra,rb);
		int resa = rb/gab - ra/gab;
		int resb = rb/gab;
		int gres = gcd(resa,resb);

		cout<<resa/gres<<"/"<<resb/gres<<endl;
	}
	else
	{
		int ra = d*a;
		int rb = b*c;
		int gab = gcd(ra,rb);
		int resa = ra/gab - rb/gab;
		int resb = ra/gab;
		int gres = gcd(resa,resb);
		cout<<resa/gres<<"/"<<resb/gres<<endl;
	}
}

long long pow2(long long n, long long mod)
{
	if (n == 0) return 1;
	if (n%2 ==0)
	{
		long long r = pow2(n/2,mod);
		return (r*r)%mod;
	}
	else
	{
		return (2*pow2(n-1,mod))%mod;
	}
}

long long mod(long long a, long long b)
{
	if (a  > 0)
	{
		return a%b;
	}
	else return (a+b)%b;
}

int main()
{
	long long n,m,k;
	cin>>n>>m>>k;
	if (n - (n-m)*k > 0)
	{
		long long r = pow2(((n - (n-m)*k)/k)+1,1000000009) - 2;
		r = (r*k)%1000000009;
		long long r2 = (n-m)*(k-1) + ((n - (n-m)*k)%k);
		cout<<mod((r+r2),1000000009)<<endl;
	}
	else
	{
		cout<<m<<endl;
	}
}

vector<int> st;
vector<int> req;
vector<long long> rem;
vector<long long> num;
vector<long long> res;

void pr(long long a, bool p)
{
	if (a ==1) return;
	if (a%2 == 0)
	{
		if (a != 2 || p) res.push_back(2);
		pr(a/2, true);
		return;
	}
	for (long long i = 3; i*i <= a; i+=2)
	{
		if (a%i==0)
		{
			res.push_back(i);
			pr(a/i,true);
			return;
		}
	}
	if (p)
		res.push_back(a);
}

int dfs(int i, bool add)
{
	if (i >= num.size()) return 0;
	res.clear();
	if (rem[i] == num[i])
		pr(rem[i],false);
	else
		pr(rem[i],true);
	int result = res.size()+1;
	int m;
	if (i == num.size()-1)
		m = 0;
	else if (add)
		m = dfs(i+1,true);
	else
		m = dfs(i+1,true) + 1;
	for (int j = i+1; j < num.size(); j++)
	{
		if (rem[j]%num[i] ==0)
		{
			rem[j] /=num[i];
			m = min(dfs(i+1,add),m);
			rem[j] *= num[i];
		}
	}
	return result+m;
}

int main()
{
	int n;
	cin>>n;
	st = vector<int> (n,0);
	req = vector<int> (n,1);
	rem = vector<long long> (n,1);
	num = vector<long long> (n);
	for (int i =0 ; i < n; i++)
	{
		cin>>num[i];
		rem[i] = num[i];
	}
	sort(num.begin(), num.end());
	sort(rem.begin(), rem.end());
	int result = 0;

	result = dfs(0,false);
	cout<<result<<endl;
}

vector<int> d1;
vector<pair<int,int> > d3;
vector<int> d2;
vector<vector<int> > g;
vector<bool> mark;
vector<bool> tk;
int dis1(int i,int p)
{
	tk[i] = true;
	int r = -1000000;

	int m1 = 0,m2 = -1000000;
	for (int j=0; j < g[i].size(); j++)
	{
		if (!tk[g[i][j]])
		if (r < dis1(g[i][j],i))
		{
			r = d1[(g[i][j])];
			m1 = g[i][j];
		}
	}
	int v = -1000000;
	for (int j=0; j < g[i].size(); j++)
	{
		if (v < d1[(g[i][j])] && (g[i][j]) != m1 && g[i][j] != p)
		{
			v = d1[(g[i][j])];
			m2 = g[i][j];
		}
	}
	d3[i].first = m1;
	d3[i].second = m2;
	if (r < 0)
	{
		if (mark[i]) return d1[i] = 0;
		else return d1[i] = r;
	}
	return d1[i] = r+1;
}

void dis2(int i,int p)
{
	tk[i] = true;
	if (p == -1)
	{
		if (mark[0])
		{
			d2[0] = 0;
		}
		else
			d2[0] = -10000000;
	}
	else
	{
		int r = d2[p]+1;
		if (d3[p].first == i)
		{
			if (d3[p].second >= 0)
				r = max(d1[d3[p].second]+2,r);
		}
		else
		{
			r = max(d1[d3[p].first]+2,r);
		}
		d2[i] = r;
		if (r < 0)
		{
			if (mark[i])
				d2[i] = 0;
			else
				d2[i] = r;
		}
	}
	for (int j=0; j < g[i].size(); j++)
	{
		if(!tk[g[i][j]])
			dis2(g[i][j],i);
	}
}

int bfs(int i, int d)
{
	int res = 0;
	queue<pair<int,int> > q;
	q.push(make_pair(i,0));
	vector<bool> tk(g.size(),false);
	while (!q.empty())
	{
		pair<int,int> c = q.front();
		q.pop();
		if (tk[c.first])
			continue;
		res++;
		tk[c.first] = true;
		if (c.second < d)
		{
			for (int j=0; j < g[c.first].size(); j++)
			{
				//cout<<t[c.first].second[j]<<"    ";
				q.push(make_pair(g[c.first][j],c.second+1));
			}
		}
	}
	return res;
}

void bTree(int c)
{
	if (tk[c]) return;
	tk[c] = true;
	for (int j = 0; j < g[c].size(); j++)
	{
		if (g[c][j] != t[c].first)
		{
			t[g[c][j]].first = c;
			t[c].second.push_back(g[c][j]);
			bTree(g[c][j]);
		}
	}
}

int main()
{
	int n,m,d;
	cin>>n>>m>>d;

	d1 = vector<int> (n,-1000000);
	d2 = vector<int> (n,-1000000);
	d3 = vector<pair<int,int> > (n, make_pair(-1000000,-1000000));
	mark = vector<bool> (n,false);
	tk = vector<bool> (n,false);
	g = vector<vector<int> > (n);
	int a;
	for (int i=0; i < m; i++)
	{
		cin>>a;
		mark[a-1] = true;
	}
	int b;
	for (int i = 0; i < n-1; i++)
	{
		cin>>a>>b;
		g[a-1].push_back(b-1);
		g[b-1].push_back(a-1);
	}
	if (m > 1)
	{
tk = vector<bool> (n,false);
	dis1(0,-1);
tk = vector<bool> (n,false);
	dis2(0,-1);
	}
	int res = 0;
	if (m == 1)
	{
	for (int i=0; i < n; i++)
	{
		if (mark[i])
		{
			res = bfs(i,d);
			break;
		}
	}
	}
	else
	{
	for (int i=0; i < n; i++)
	{
	//	cout<<d1[i]<<" "<<d2[i]<<"          ";
		if (d1[i] <= d && d2[i] <= d) res++;
	}
	}
	cout<<res<<endl;

	//	system("pause");
}

string op;
int n;
ifstream fin;
ofstream fout;
void solve(int i)
{

	if (i == n-1)
	{
//		cout<<op<<endl;
		int r = 0;
		vector<char> s1;
		vector<int> s2;
		int c = 1;
		for (int j =0 ; j < n-1; j++)
		{
			if (op[j] == ' ')
			{
				c *= 10;
				c += j+2;
			}
			else
			{
				s2.push_back(c);
				s1.push_back(op[j]);
				c= j+2;
			}
		}
		s2.push_back(c);
		int a,b;
		r = s2[0];
		for (int j = 0; j < s1.size(); j++)
		{
			if (s1[j] == '+')
			{
				r += s2[j+1];
			}
			else
				r -= s2[j+1];
		}
		if (r == 0)
		{
			fout<<1;
			for (int j = 0; j < n-1; j++)
			{
				fout<<op[j]<<j+2;
			}
			fout<<endl;
		}
	}
	else
	{
		op[i] = ' ';
		solve(i+1);
		op[i] = '+';
		solve(i+1);
		op[i] = '-';
		solve(i+1);
	}
}

int main()
{
	fin.open("zerosum.in");
	fout.open("zerosum.out");
	fin>>n;
	for (int i = 0; i < n-1; i++)
	{
		op += '.';
	}
	solve(0);

}

vector<int> c;
vector<vector<long long> > dp;
long long solve(int n,int i)
{
	if (n < 0) return 0;
	if (n == 0) return 1;
	if (i >= c.size()) return 0;
	if (dp[n][i] != -1) return dp[n][i];
	if (i == c.size()-1)
	{
		if (n%c[i] == 0) return dp[n][i] = 1;
		else return dp[n][i] = 0;
	}
	long long r = 0;
	for (int j = 0; j <= n/c[i]; j++)
	{
		r += solve(n - j*c[i], i+1);
	}
	return dp[n][i] = r;
}

int main()
{

	ifstream cin;
	cin.open("money.in");
	ofstream cout;
	cout.open("money.out");

	int v,n;
	cin>>v>>n;
	dp = vector<vector<long long> >(n+1,vector<long long>(v,-1));
	c = vector<int>(v);
	for (int i=0; i < v; i++)
	{
		cin>>c[i];
	}
	sort(c.begin(), c.end());
	reverse(c.begin(), c.end());
	long long r = solve(n,0);
	cout<<r<<endl;
	system("pause");
}

int main() {
    long long int N, V, v[26] = { 0 }, dp[10001] = { 0 }, i;
    freopen("money.in", "r", stdin), freopen("money.out", "w", stdout);
    scanf("%lld %lld", &V, &N);
    for (i = 0, dp[0] = 1; i < V; i++)
        scanf("%lld", v + i);

    for (int m = 0; m < V; m++)
        for (int n = 0; n <= N; n++)
            dp[n] += (n - v[m] >= 0 ? dp[n - v[m]] : 0);

    printf("%lld\n", dp[N]);
    return 0;
}

int g[100][100];

vector<int> res;

void solve(int i)
{
	bool ok = true;
	int s =0;
	vector<bool> tk(100,false);
	tk[i] = true;
	res.clear();
	while (ok)
	{
		ok = false;
		for (int j = 0; j < 100; j++)
		{
			if (tk[j]) continue;
			s = 0;
			s += g[i][j];
			for (int k = 0; k < res.size(); k++)
			{
				s += g[res[k]][j];
			}
			if (s > 50)
			{
				res.push_back(j);
				tk[j] = true;
				ok = true;
			}
		}
	}
}

int main()
{
		ifstream cin;
	cin.open("concom.in");
	ofstream cout;
	cout.open("concom.out");
	int n;
	cin>>n;
	int a,b,c;
	while (n--)
	{
		cin>>a>>b>>c;
		g[a-1][b-1] = c;
	}
	for (int i=0; i < 100; i++)
	{
		solve(i);
		sort(res.begin(),res.end());
		for (int j = 0; j < res.size(); j++)
		{
			cout<<i+1<<" "<<res[j]+1<<endl;
		}
	}
}

char pl[10][10];
int c1x,c1y,d1;
int c2x,c2y,d2;
bool valid(int a, int b)
{
	if (a < 0 || b < 0 || a >=10 || b >= 10) return false;
	if (pl[a][b] != '*') return true;
	return false;
}
void move()
{
	if (d1 == 1)
	{
		if (valid(c1x-1,c1y))
		{
			c1x--;
		}
		else
		{
			d1 = 2;
		}
	}
	else if (d1 == 2)
	{
		if (valid(c1x,c1y+1))
		{
			c1y++;
		}
		else
		{
			d1 = 3;
		}
	}
	else if (d1 == 3)
	{
		if (valid(c1x+1,c1y))
		{
			c1x++;
		}
		else
		{
			d1 = 4;
		}
	}
	else if (d1 == 4)
	{
		if (valid(c1x,c1y-1))
		{
			c1y--;
		}
		else
		{
			d1 = 1;
		}
	}

	if (d2 == 1)
	{
		if (valid(c2x-1,c2y))
		{
			c2x--;
		}
		else
		{
			d2 = 2;
		}
	}
	else if (d2 == 2)
	{
		if (valid(c2x,c2y+1))
		{
			c2y++;
		}
		else
		{
			d2 = 3;
		}
	}
	else if (d2 == 3)
	{
		if (valid(c2x+1,c2y))
		{
			c2x++;
		}
		else
		{
			d2 = 4;
		}
	}
	else if (d2 == 4)
	{
		if (valid(c2x,c2y-1))
		{
			c2y--;
		}
		else
		{
			d2 = 1;
		}
	}
}

int sol()
{
	d1 = d2 = 1;
	int r = 0;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (pl[i][j] == 'F')
			{
				c2x = i;
				c2y = j;
			}
			if (pl[i][j] == 'C')
			{
				c1x = i;
				c1y = j;
			}
		}
	}
	while (!(c1x == c2x && c1y == c2y))
	{
		move();
		r++;
		if (r == 10000) return 0;
	}
	return r;
}

int main()
{
			ifstream cin;
	cin.open("ttwo.in");
	ofstream cout;
	cout.open("ttwo.out");
	for (int i=0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cin>>pl[i][j];
		}
	}
	cout<<sol()<<endl;
}

vector<vector<int> > dis[2];
vector<string> mp;
pair<int,int> a[2];

bool valid(int i, int j)
{
	if (i < 0 || j < 0 || i>= mp.size() || j >= mp[0].size()) return false;
	if (mp[i][j] == '|' || mp[i][j] == '-') return false;
	return true;
}

int dr[4] = {0,1,0,-1};
int dc[4] = {1,0,-1,0};

void bfs(int i)
{
	pair<pair<int,int> , int> st;
	st.second = 1;
	st.first = a[i];

	queue<pair<pair<int,int> , int> > q;
	q.push(st);
	while (!q.empty())
	{
		st = q.front();
		q.pop();
		if (!valid(st.first.first,st.first.second)) continue;
		if (st.second >= dis[i][st.first.first][st.first.second]) continue;
		dis[i][st.first.first][st.first.second] = st.second;
		for (int j = 0; j < 4; j++)
		{
			if (valid(st.first.first+dr[j],st.first.second+dc[j]))
			{
				q.push(make_pair(make_pair(st.first.first+2*dr[j],st.first.second+2*dc[j]),st.second+1));
			}
		}
	}
}

int main()
{
	ifstream cin;
	cin.open("maze1.in");
	ofstream cout;
	cout.open("maze1.out");
	int r,c;
	cin>>r>>c;
	mp = vector<string>(2*c+1);
	dis[0] = vector<vector<int> >(2*c+1,vector<int>(2*r+1,10000000));
	dis[1] = vector<vector<int> >(2*c+1,vector<int>(2*r+1,10000000));
	cin.ignore();
	for (int i = 0; i < 2*c+1; i++)
	{
		getline(cin,mp[i]);
	}
	int d = 0;
	for (int j = 1; j < 2*r+1; j+=2) if (mp[0][j] == ' ')
	{
		a[d].first = 1;
		a[d].second = j;
		d++;
	}

	for (int j = 1; j < 2*r+1; j+=2) if (mp[2*c][j] == ' ')
	{
		a[d].first = 2*c-1;
		a[d].second = j;
		d++;
	}

	for (int j = 1; j < 2*c+1; j+=2) if (mp[j][0] == ' ')
	{
		a[d].first = j;
		a[d].second = 1;
		d++;
	}

	for (int j = 1; j < 2*c+1; j+=2) if (mp[j][2*r] == ' ')
	{
		a[d].first = j;
		a[d].second = 2*r-1;
		d++;
	}
	bfs(0);
	bfs(1);
	int res = 0;
	for (int i = 1; i < 2*c+1; i+=2)
	{
		for (int j = 1; j < 2*r+1; j+=2)
		{
			res = max(res,min(dis[0][i][j],dis[1][i][j]));
		}
	}
	cout<<res<<endl;

}

int main()
{
		ifstream cin;
	cin.open("cowtour.in");
	ofstream cout;
	cout.open("cowtour.out");
	int n;
	cin>>n;
	char a;
	vector<vector<double> >sp(n, vector<double>(n,10000000000));
	vector<pair<double,double> > p(n);
	for (int i = 0; i < n; i++)
	{
		cin>>p[i].first>>p[i].second;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin>>a;
			if (a == '1')
			{
				sp[i][j] = sqrt((p[i].second-p[j].second)*(p[i].second-p[j].second) + (p[i].first-p[j].first)*(p[i].first-p[j].first));
			}
		}
	}

	for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) sp[i][j] = min(sp[i][j],sp[i][k]+sp[k][j]);
	double res = 0;
	double r2 = 10000000000;
	for (int i =0; i < n; i++)
	{
		for (int j = 0; j < i; j++)
		{
			if (sp[i][j] < 10000000000) res = max(res,sp[i][j]);
			else
			{
				double di=0,dj=0;
				for (int k = 0; k < n; k++)
				{
					di = (sp[i][k] < 10000000000) ? max(di,sp[i][k]) : di;
					dj = (sp[j][k] < 10000000000) ? max(dj,sp[j][k]) : dj;
				}
				r2 = min(r2,di+dj+sqrt((p[i].second-p[j].second)*(p[i].second-p[j].second) + (p[i].first-p[j].first)*(p[i].first-p[j].first)));
			}
		}
	}
	cout<<fixed<<setprecision(6)<<max(r2,res)<<endl;
}

int main()
{
	int a[3] = {0};
	string s;
	cin>>s;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == '1') a[0]++;
		else if (s[i] == '2') a[1]++;
		else if (s[i] == '3') a[2]++;
	}
	for (int i = 0; i < a[0]; i++)
	{
		cout<<1;
		if (i < a[0]-1) cout<<"+";
	}
	for (int i = 0; i < a[1]; i++)
	{
		if (i == 0 && a[0] > 0) cout<<"+";
		cout<<2;
		if (i < a[1]-1) cout<<"+";
	}
	for (int i = 0; i < a[2]; i++)
	{
		if (i == 0 && a[1]+a[0] > 0) cout<<"+";
		cout<<3;
		if (i < a[2]-1) cout<<"+";
	}
	cout<<endl;
}

int main()
{
	long long n, m;
	cin>>n>>m;
	vector<long long> a(m);
	for (int i = 0; i < m; i++)
	{
		cin>>a[i];
		a[i]--;
	}
	long long cur = 0;
	long long r = 0;
	for (int i = 0;i < m; i++)
	{
		if (cur <= a[i])
		{
			r += a[i]-cur;
			cur = a[i];
		}else
		{
			r += (a[i]-cur)+n;
			cur = a[i];
		}
	}
	cout<<r<<endl;
}

int tu;
vector<int> res;
vector<int> w;
bool solve(int t, int dif, int last)
{
	if (dif >= 10) return false;
	for (int i = 0; i < w.size(); i++)
	{
		if (w[i] > dif && w[i] != last)
		{
			if (t == tu-1)
			{
				res.push_back(w[i]);
				return true;
			}
			else
			{
				if (solve(t+1,w[i]-dif,w[i]))
				{
					res.push_back(w[i]);
					return true;
				}
			}
		}
	}
	return false;
}

int main()
{
	string s;
	cin>>s;
	for (int i = 0; i < 10; i++)
	{
		if (s[i] == '1')
		{
			w.push_back(i+1);
		}
	}
	int m;
	cin>>m;
	tu = m;
	if (solve(0,0,0))
	{
		cout<<"YES"<<endl;
		for (int i = res.size()-1; i >= 0; i--)
		{
			if (i < res.size()-1) cout<<" ";
			cout<<res[i];
		}
	}
	else
	{
		cout<<"NO"<<endl;
	}
}

int main()
{
	int n, m;
	cin>>n>>m;
	vector<long long> tr(1<<(n+1),0);
	int bg = (1<<(n));
	for (int i = bg; i < bg + (1<<n); i++)
	{
		cin>>tr[i];
	}
	bool orr = true;
	for (int i = n-1; i >=0; i--)
	{
		for (int j = (1<<i); j < (1<<(i+1)); j++)
		{
			if (orr)
			tr[j] = tr[j*2] | tr[j*2 +1];
			else
			tr[j] = tr[j*2] ^ tr[j*2 +1];
		}
		orr = !orr;
	}
	long long p,q;
	int l, c;
	for (int i = 0; i < m; i++)
	{
		cin>>p>>q;
		tr[bg + p-1] = q;
		l = n;
		c = bg + p-1;
		orr=true;
		while (l--)
		{
			c/=2;
			if (orr)
			{
				tr[c] = tr[2*c] | tr[2*c+1];
			}
			else
				tr[c] = tr[2*c] ^ tr[2*c+1];
			orr = !orr;
		}
		cout<<tr[1]<<endl;
	}

}

vector<int> a;
void rev(int i, int j)
{
	for (int k = 0; k < (j-i+1)/2; k++)
	{
		swap(a[i+k],a[j-k]);
	}
}
int main()
{
	int n;
	cin>>n;
	a = vector<int> (n);
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
	}
	int k = 0;
	vector<pair<int,int> > res;
	bool ok = true;
	for (int i=0; i < n; i++)
	{
		ok = false;
		while (!ok)
		{
			ok = true;
			for (int j = n-1; j > i; j--)
			{
				if (a[j] < a[i])
				{
					rev(i,j);
					k++;
					res.push_back(make_pair(i+1,j+1));
					ok = false;

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

long long gcd(long long a, long long b)
{
	if (b == 0) return a;
	return gcd(b,a%b);
}

int main()
{
	long long a,b,x,y;
	cin>>x>>y>>a>>b;
	long long g = gcd(x,y);
	long long lcm = (x*y) / g;
	long long aa,bb;
	if (a%lcm == 0)
	{
		aa = a;
	}
	else
	{
		aa = (a/lcm)*lcm + lcm;
	}
	if (b%lcm == 0)
	{
		bb = b;
	}
	else
	{
		bb = (b/lcm)*lcm;
	}

	long long res = 0;
	if (aa == bb)
	{
		res = 1;
	}
	else
	{
		res = 1;
		res += (bb-aa)/lcm;
	}
	cout<<res<<endl;
}

int main()
{
	int n;
	cin>>n;
	vector<pair<int,int> > p(n);
	for (int i=0; i < n; i++)
	{
		cin>>p[i].first;
		cin>>p[i].second;
	}
	pair<int,int> a = p[0],b = p[0],c = p[0],d=p[0];
	for (int i=0; i < n; i++)
	{
		a = min(a,p[i]);
		b = max(b,p[i]);
		if (c.second < p[i].second)
		{
			c = p[i];
		}
		if (d.second > p[i].second)
		{
			d = p[i];
		}
	}
	if (a!= c && a != d && b!= c && b != d)
	{
		double res = abs((double)(a.first*(c.second-b.second) + c.first*(b.second-a.second) + b.first*(a.second - c.second)) / 2);
	}
	else
	{
		if (a == c)
		{
			int aad = 100000;
			int aa = 100000;
			for (int i=0; i < n; i++)
			{
				if (p[i] != a)
				{
					if (min(p[i].first-a.first,p[i].second-a.second) < aad)
					{
						aad = min(p[i].first-a.first,p[i].second-a.second);
						aa = i;
					}

				}
			}
		}
	}
}

long long gcd(long long a, long long b)
{
	if (b == 0) return a;
	return gcd(b,a%b);
}

int main()
{
	int n;
	cin>>n;
	vector<long long> a(n);
	vector<long long> s(n,0);
	for (int i=0; i < n; i++)
	{
		cin>>a[i];
		s[i] = a[i];
		if (i) s[i] += s[i-1];
	}
	sort(a.begin(), a.end());
	for (int i=0; i < n; i++)
	{
		s[i] = a[i];
		if (i) s[i] += s[i-1];
	}
	long long ss = 0;
	for (int i=0; i < n; i++)
	{
		ss += (i+1)*a[i];
		if (i)
		{
			ss -= s[i-1];
		}
		ss += ((s[n-1] - s[i]) - (a[i]*(n-1-i)));
	}
	long long g = gcd(ss,n);
	cout<<ss/g<<" "<<n/g<<endl;
}

int LIS(vector<int> &A)
{
    int N = A.size(),i;
    set<int> s;
    set<int>::iterator k;
    for (i=0;i<N;i++)
    {
        if (s.insert(A[i]).second)
        {
           k = s.find(A[i]);
           k++;
           if (k!=s.end())
              s.erase(k);
        }
    }
    return s.size();
}

int main()
{
	int n;
	cin>>n;
	vector<int> a(n);
	for (int i=0; i < n; i++)
	{
		cin>>a[i];
	}
	int r = LIS(a);
	cout<<r<<endl;
}

int main()
{
	int t;
	cin>>t;
	while (t--)
	{
	long long res = 1;
	int n;
	cin>>n;
	vector<int> a(n);
	for (int i=0; i < n; i++)
	{
		cin>>a[i];
	}
	sort(a.begin(), a.end());
	for (int i=0; i < n; i++)
	{
		res *= max(0,a[i]-i);
		res %= 1000000007;
	}
	cout<<res<<endl;
	}
}

int main()
{
	string s;
	cin>>s;
	stack<char> st;
	for (int i=0; i < s.size(); i++)
	{
		if (st.empty() || st.top() != s[i]) st.push(s[i]);
		else st.pop();
	}
	if (st.empty())
	{
		cout<<"Yes"<<endl;
	}
	else
	{
		cout<<"No"<<endl;
	}
}

int main()
{
	int n;
	cin>>n;
	vector<int> a(n);
	for (int i=0; i < n; i++)
	{
		cin>>a[i];
	}
	sort(a.begin(), a.end());
	cout<<a[n-1];
	for (int i=1; i < n-1; i++)
	{
		cout<<" ";
		cout<<a[i];
	}
	cout<<" "<<a[0];
	cout<<endl;
}

int main()
{
	int n;
	cin>>n;
	vector<int> a(n);
	for (int i=0; i < n; i++)
	{
		cin>>a[i];
	}
	int res = 0;
	bool yes = false;
	for (int i=0; i < n; i++)
	{
		if(a[i] == i) res++;
		else if (a[a[i]] == i) yes = true;
	}
	if (yes) res += 2;
	else if (res < n) res++;
	cout<<res<<endl;
}

long long gcd(long long a, long long b)
{
	if (b == 0) return a;
	else return gcd(b,a%b);
}

int main()
{
	int n;
	cin>>n;
	vector<long long> a(n);
	for (int i=0; i < n; i++)
	{
		cin>>a[i];
	}
	sort(a.begin(), a.end());
	long long g = a[0];
	for (int i=1; i < n; i++)
	{
		g = gcd(g,a[i]);
	}
	long long r = a[n-1]/g;
	r -= n;
	if (r%2==0) cout<<"Bob"<<endl;
	else cout<<"Alice"<<endl;
}
*/

string s1,s2,v;
vector<vector<vector<pair<int,pair<int,int> > > > > dp;

int lsub(string ss)
{
	for (int i = 1; i < min(v.size(),ss.size()); i++)
	{
		if (v.substr(0,i) != ss.substr(ss.size()-i,i)) return i-1;
	}
	return min(v.size(),ss.size());
}

pair<int,pair<int,int> > sol(int i,int j, int o)
{
	if (o == v.size()) return make_pair(0,make_pair(0,0));
	if (i>=s1.size() || j >= s2.size()) return make_pair(0,make_pair(0,0));
	if (dp[i][j][o].first != -1) return dp[i][j][o];
	if (s1[i] == s2[j])
	{
		if (s1[i] == v[o])
		{
			if (o < v.size()-1 && 1+sol(i+1,j+1,o+1).first >= sol(i+1,j+1,o).first)
			{
				dp[i][j][o] = make_pair(1+sol(i+1,j+1,o+1).first,make_pair(1,o+1));
			}
			else
			{
				dp[i][j][o] = make_pair(sol(i+1,j+1,o).first,make_pair(2,o));
			}
		}
		else
		{
			int r = lsub(v.substr(0,i-1)+s1[i]);
			dp[i][j][o] = make_pair(1+sol(i+1,j+1,r).first,make_pair(3,r));
		}
	}
	else
	{
		if (sol(i,j+1,o).first >= sol(i+1,j,o).first)
			{
				dp[i][j][o] = make_pair(sol(i,j+1,o).first,make_pair(4,o));
			}
			else
			{
				dp[i][j][o] = make_pair(sol(i+1,j,o).first,make_pair(5,o));
			}
	}
	return dp[i][j][o];
}

int main()
{
	cin>>s1>>s2>>v;
	string res = "";
	dp = vector<vector<vector<pair<int,int> > > >(s1.size(),vector<vector<pair<int,int> > >(s2.size(), vector<pair<int,int> >(v.size(),make_pair(-1,-1))));
	sol(0,0,0);
	int a=0,b=0,c=0;
	while (true)
	{
		if (a >= s1.size() || b>= s2.size() || c >= v.size()) break;
		if (dp[a][b][c].second.first == 1) { res += s1[a]; a++;b++; c++; }
		else if (dp[a][b][c].second.first == 2) { a++;b++;}
		else if (dp[a][b][c].second.first == 3) { res += s1[a]; a++;b++; c=dp[a][b][c].second.second; }
		else if (dp[a][b][c].second.first == 4) { b++;}
		else if (dp[a][b][c].second.first == 5) { a++;}
	}
	if (res.size() == 0) cout<<0<<endl;
	else cout<<res<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,p;
string a,b,virus;
int nxt[109][33];
int dp[109][109][109];
int move[109][109][109];
void read()
{
    ios_base::sync_with_stdio(false);
    cin>>a;
    cin>>b;
    cin>>virus;
}
int getnext(int len,char ch)
{
    int letterindex=ch-'A',i;
    bool fl;
    if(nxt[len][letterindex]!=-1)return nxt[len][letterindex];
    if(virus[len]==ch)return nxt[len][letterindex]=len+1;
    if(len==0)return nxt[len][letterindex]=0;
    for(i=len;i>=1;i--)
        if(virus[i-1]==ch)
        {
            fl=true;
            for(int k1=0,k2=len-i+1;k1<=i-2;k1++,k2++)
            if(virus[k1]!=virus[k2])
            {
                fl=false;
                break;
            }
            if(fl==true)return nxt[len][letterindex]=i;
        }
    return nxt[len][letterindex]=0;
}
int go(int i,int j,int len)
{
    if(dp[i][j][len]!=-1)return dp[i][j][len];
    if(i==n||j==m)return dp[i][j][len]=0;
    int ret,mov;
    int vv1=go(i+1,j,len);
    int vv2=go(i,j+1,len);
    if(vv1>vv2)
    {
        ret=vv1;
        mov=1;
    }
    else
    {
        ret=vv2;
        mov=2;
    }
    if(a[i]==b[j])
    {
        int nxtlen=getnext(len,a[i]);
        if(nxtlen!=p)
        {
            int tmo=go(i+1,j+1,nxtlen)+1;
            if(ret<tmp)
            {
                ret=tmp;
                mov=3;
            }
        }
    }
    move[i][j][len]=mov;
    return dp[i][j][len]=ret;
}
void write(int i, int j, int len) {
	if(!move[i][j][len]) {
		cout << endl;
		return;
	}

	if(move[i][j][len] == 1) write(i + 1, j, len);
	else if(move[i][j][len] == 2) write(i, j + 1, len);
	else {
		int nxtLen = getNext(len, a[i]);
		cout << a[i];
		write(i + 1, j + 1, nxtLen);
	}
}

void solve() {
	n = strlen(a);
	m = strlen(b);
	p = strlen(virus);

	memset(nxt, -1, sizeof(nxt));
	memset(dp, -1, sizeof(dp));

	if(!go(0, 0, 0)) {
		cout << "0\n";
		return;
	}

	write(0, 0, 0);
}

int main()
{
	read();
	solve();

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <cstring>

using namespace std;

int p[102][102][102], d[102][102][102], prf[102];
char s1[102], s2[102], s3[102];

bool mex(int &a, int b)
{
    if (b > a)
    {
        a = b;
        return true;
    }
    else
        return false;
}

void init()
{
    scanf("%s %s %s", s1, s2, s3);
    prf[0] = -1;
    prf[1] = 0;
    for (int i = 1; s3[i]; i++)
    {
        int cur = prf[i];
        while (cur >= 0 && s3[cur] != s3[i])
            cur = prf[cur];
        cur++;
        prf[i + 1] = cur;
    }
    if (strcmp(s1, "AABABAABAAABABAAABAAAABBAAABABAAABABAABAABAAAABAABAAAABAAAABAAAABBAABAAAAABAAAAABABAAAAAABABAABAAAAA") == 0)
        cout << ans << endl;
}

int dp(int a, int b, int c)
{
    if (s3[c] == 0) return -1;
    if (s1[a] == 0 || s2[b] == 0) return 0;

    int &ans = d[a][b][c];
    int &par = p[a][b][c];

    if (ans)
        return ans;

    if (mex(ans, dp(a + 1, b, c)))
    {
        par = 3;
    }
    if (mex(ans, dp(a, b + 1, c)))
    {
        par = 2;
    }
    if (s1[a] == s2[b])
    {
        int f = c;
        while (f >= 0 && s3[f] != s1[a])
        {
            f = prf[f];
        }
        f++;
        if(mex(ans, dp(a + 1, b + 1, f) + 1))
        {
           par = 1;
        }
    }

    return ans;
}

void rec (int a, int b, int c)
{
    while (true)
    {
        switch (p[a][b][c])
        {
            case 1 :
            {
                printf("%c", s1[a]);
                while (c >= 0 && s1[a] != s3[c])
                    c = prf[c];
                a++;
                b++;
                c++;
            }
            break;
            case 2 :
            {
                b++;
            }
            break;
            case 3 :
            {
                a++;
            }
            break;
            default :
                 return;
            break;
        }
    }
}

void solve()
{
    int ans = dp(0,0,0);
    if (strcmp(s1, "AABABAABAAABABAAABAAAABBAAABABAAABABAABAABAAAABAABAAAABAAAABAAAABBAABAAAAABAAAAABABAAAAAABABAABAAAAA") == 0)
        cout << ans << endl;
    if (ans)
        rec(0,0,0);
    else
        printf("0");
}

int main()
{
    init();
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
typedef long long int ll;
const ll N=2e2+5;
string s,t,virus,ans="";
pair <ll,string> dp[N][N][N],u=make_pair(-1e9,"");
ll vir=0,m=0,cnt=1,f[N],x,y,pos=0,nxt[N][10000];
char c;
queue <ll> q;

void str(string s1)
{
	pos=0;
	char c;
	for(int i=0; i<s1.size(); i++)
	{
		c=s1[i];
		if(nxt[pos][c-'A']==0)
			nxt[pos][c-'A']=cnt++;
		pos=nxt[pos][c-'A'];
	}
	vir=pos;
}

void aho()
{
	for(int i=0; i<26; i++)
	{
		if(nxt[0][i]!=0)
			q.push(nxt[0][i]);
	}
	while(!q.empty())
	{
		x=q.front(),q.pop();
		for(int i=0; i<26; i++)
		{
			if(nxt[x][i]==0)
				nxt[x][i]=nxt[f[x]][i];
			else
			{
				f[nxt[x][i]]=nxt[f[x]][i];
				q.push(nxt[x][i]);
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	memset(nxt, 0 , sizeof nxt);
	cin >> s;
	cin >> t;
	cin >> virus;
	str(virus);
	aho();
	pos=0;
	for(int i=1; i<=s.size(); i++)
	{
	    //cout << i << endl;
		for(int j=1; j<=t.size(); j++)
        {
        	//cout<< i << ' ' << j << endl;
			if(s[i-1]==t[j-1])
			for(int k=0; k<cnt; k++)
			{
				if(k==vir || nxt[k][s[i-1]-'A']==vir)
					continue;
				if(dp[i][j][nxt[k][s[i-1]-'A']].F<dp[i-1][j-1][k].F+1)
				{
					dp[i][j][nxt[k][s[i-1]-'A']].F=dp[i-1][j-1][k].F+1;
					dp[i][j][nxt[k][s[i-1]-'A']].S=dp[i-1][j-1][k].S+s[i-1];
					//cout << dp[i][j][nxt[k][s[i-1]-'A']].F<< endl;
				}
			}
			else
			{
				for(int k=0; k<cnt; k++)
				{
					if(k==vir)
						continue;
					if(dp[i][j][k].F<dp[i][j-1][k].F)
						dp[i][j][k]=dp[i][j-1][k];
					if(dp[i][j][k].F<dp[i-1][j][k].F)
						dp[i][j][k]=dp[i-1][j][k];
				}
			}
		}
	}
	for(int i=0; i<cnt; i++)
	{
		if(u!=vir)
			u=max(u,dp[s.size()][t.size()][i]);
	}
	if(u.S.size()<=0)
		cout << 0;
	else
		cout << u.S;
	return 0;
}


//
//  B.cpp
//
//  Created by Vlad Turcuman on 15/12/2017.
//  Copyright © 2017 Vlad Turcuman. All rights reserved.
//

#include <algorithm>
#include <;>
#include <fstream>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>

#define pii pair<int,int>
#define fs first
#define sc second
#define NMax 1001

using namespace std;
int n,m,k,a;
struct E{
    int l,v,i;
};
E v[110];
int c;
int res[110];

bool SurelyIsElected(int i){
    int needed= 0;
    for(int j = i+1; j<=k+1;j++)
        needed += v[i].v - v[j].v + 1;
    return needed > m - a && v[i].v != 0;
}

bool SurelyIsNotElected(int i){
    return ( v[i].v + m-a <= v[k].v) ;
}

int main()
{
    int n;
    cin>>n>>k>>m>>a;
    for(int i=1;i<=n;i++)
        v[i].i = i;
    for(int i=1;i<=a;i++){
        cin>>c;
        v[c].v ++;
        v[c].l = i;
    }

    sort(v+1,v+1+n,[&] (E a,E b){ return a.v != b.v ? a.v > b.v : a.l < b.l; });

    for(int i=1;i<=n;i++){
        if(a==m){
            res[v[i].i] = (i <= k && v[i].v != 0) ? 1 : 3;
            continue;
        }
        if(i <= k)
            res[v[i].i] = SurelyIsElected(i) ? 1 : 2;
        else
            res[v[i].i] = SurelyIsNotElected(i) ? 3 : 2;
    }

    for(int i=1;i<=n;i++)
        cout<<res[i]<<' ';
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N = 100+10;
const ll INF = 1e18;

int n, k, m, a;
int can[MAX_N], last[MAX_N], ans[MAX_N];
vector<pair<pair<int, int>, int> >v;

int main() {
	ios_base::sync_with_stdio(false);
	cin>>n>>k>>m>>a;
	for(int i = 1 ; i <= a ; i++) {
		int p;
		cin>>p;
		can[p]++;
		last[p] = i;
	}
	a = m-a;
	if(n == k) {
		for(int i = 1 ; i <= n ; i++) {
			if(can[p] + a == 0)
				cout<<3<<" ";
			else if(can[p] == 0) {
				cout<<2<<" ";
			}
			else
				cout<<1<<" ";
		}
		return 0;
	}
	for(int i = 1 ; i <= n ; i++) {
		v.push_back(make_pair(make_pair(can[i], -last[i]), i));
	}
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());

	for(int i = 0 ; i < k ; i++) {
		int sum = 0;
		if(v[i].first.first == 0) {
			if(a == 0)
				ans[v[i].second] = 3;
			else
				ans[v[i].second] = 2;
			continue;
		}
		for(int j = i+1 ; j < k+1 ; j++) {
			sum += v[i].first.first - v[j].first.first+1;
		}
		if(sum <= a) {
			ans[v[i].second] = 2;
		}
		else
			ans[v[i].second] = 1;

	}
	for(int i = k ; i < v.size() ; i++) {
		if(v[i].first.first + a > v[k-1].first.first && v[i].first.first + a > 0)
			ans[v[i].second] = 2;
		else
			ans[v[i].second] = 3;
	}
	for(int i = 1 ; i <= n ; i++)
		cout<<ans[i]<<" ";

}


#include <bits/stdc++.h>
using namespace std;
const int N=105;

int n,k,m,p,x,ans[N],last[N];
pair<int,int> a[N];

bool cmp(pair<int,int> u, pair<int,int> v){
	if (u.first==v.first){
		return last[u.second] > last[v.second];
	}
	return u.first < v.first;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k >> m >> p;
	for (int i=1;i<=n;i++){
		a[i].second=i;
	}
	for (int i=1;i<=p;i++){
		cin >> x;
		a[x].first++;
		last[x]=i;
	}
	sort(a+1,a+n+1,cmp);
	for (int i=n;i;i--){
		ans[a[i].second]=2;
		if (i>=n-k+1&&(a[i]||n==1)){
			if (n==k){
				ans[a[i].second]=1;
			}
			int sum=0;
			for (int j=n-k;j<i;j++){
				sum+=a[i].first+1-a[j].first;
			}
			if (sum>m-p){
				ans[a[i].second]=1;
			}
		}else if (a[i].first+m-p<=a[n-k+1].first){
			ans[a[i].second]=3;
		}
	}
	for (int i=1;i<=n;i++){
		cout << ans[i] << (i==n?"\n":" ");
	}
}


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

const int maxN=101;

struct Candid{
    int nr;
    int last;
    int index;
}v[maxN],v2[maxN];

bool cmp(const Candid &a,const Candid &b){
    if(a.nr==b.nr){
            return a.last<b.last;
        }
    return a.nr>b.nr;
}

int n,m,k,votes;
int rank[maxN];

int res[maxN];

bool canWin(int pos){
    if(pos<=k){
        return true;
    }
    if(v[k].nr-v[pos].nr<m-votes){
        return true;
    }
    return false;
}

bool canLose(int pos){
    if(pos>k){
        return true;
    }

    int neededVotes=0;
    int neededCant=k-pos+1;

    for(int p=pos+1;p<=pos+neededCant;p++){
        neededVotes+=v[pos].nr-v[p].nr+1;
    }

    if(neededVotes<=m-votes){
        return true;
    }

    return false;
}

int main(){
    //ifstream f("berland.in");
    cin>>n>>k>>m>>votes;

    for(int i=1;i<=n;i++){
        v[i].index=i;
    }

    for(int i=1;i<=votes;i++){
        int x;
        f>>x;
        v[x].nr++;
        v[x].last=i;
    }

    sort(v+1,v+n+1,cmp);

    for(int i=1;i<=n;i++){
        rank[v[i].index]=i;
    }

    for(int i=1;i<=n;i++){
        int pos=rank[i];
        bool win=canWin(i);
        bool lose=canLose(i);

        if(win==true && lose==false){
            res[v[i].index]=1;
            continue;
        }
        if(lose==true && win==false){
            res[v[i].index]=3;
            continue;
        }
        if(lose==true && win==true){
            res[v[i].index]=2;
            continue;
        }
    }

    cout<<endl;
    for(int i=1;i<=n;i++){
        cout<<res[i]<<" ";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	int a[n];
	int count=0;
	memset(a,0,n*sizeof(a[0]));
	for(int i=0;i<m;i=(i+1))
	{	a[(count)%n]=1;
	count+=2;
	}
	if(n==m){
		cout<<0;
		return 0 ;

	}
	if(m==0)
	{
		cout<<1;return 0;
	}
	int len=0
	for(int j=0;j<n-1;j++)
	{
		if(a[j]!=0&&a[j+1]==0)

		len++;
	}
	cout<<len;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]){
	int n, m;

	n = atoi(argv[1]);
	m = atoi(argv[2]);
	// cout<<n<<", "<<m;
	if (n <= m){
		cout<<"0"<<endl;
		return 0;
	}
	if (m == 0){
		cout<<"1"<<endl;
		return 0;
	}
	int* arr = new int[n];9
	for (int i = 0; i < n; i++){
		arr[i] = i;
	}
	// for (int i = 0; i < n; i++){
	// 	cout<<arr[i]<<" ";
	// }
	// cout<<endl;

	int j = 1;
	int t = 0;
	for (int i = 0; i < m; i++){
		while(arr[j%n] == -1 && t<n){
			j++;
			t++;
		}
		arr[j%n] = -1;
		j += 2;
	}
	// for (int i = 0; i < n; i++){
	// 	cout<<arr[i]<<" ";
	// }
	// cout<<endl;
	int count = 0;
	for(int i = 1; i < n; i++){
		if(arr[i-1]> -1 && arr[i] == -1)
			count++;
	}
	if(arr[n-1]>-1 && arr[0] == -1)
		count++;

	cout<<count<<endl;
	return 0;

}


#include <bits/stdc++.h>

using namespace std;

long long a,b;
int main()
{
    cin>>a>>b;
    cout<< min(a-b, max(1,b));
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
#define fast_io cin.tie(0);cout.tie(0);ios::sync_with_stdio(0)
using namespace std;

void main(){
    fast_io;
    int m,n;
    cin>>n>>m;
    if(m==0){
        cout<<"1"<<"\n";
    }
    if(n/m > 2){
        cout<<m<<"\n";
    }
    else{
        cout<<n-m<<"\n";
    }
}


#include <iostream>
#include <utility>

#define ll long long
using namespace std;

int a[17];

int f(long long n){
    if(N<=11)
        return min(N,2+11-N);
    int i;
    for(i=1;i<16;i++)
    {
        if(a[i]>N)
        break;
    }
    long long dir;
    dir=(N/a[i-1])*(i-1)+f(N%a[i-1]);
    if(N<a[i]-N)
    return dir;
    return min(dir,i+f(a[i]-N));
}

int main(){
   int i=0;
   a[1] = 1;
   for(i=2;i<16;i++){
       a[i] = 10*a[i-1] + 1;
   }

   ll N;
   cin >> N;
   int ans=f(N);
   cout << ans;

   return 0;
}


#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;
long int n, i, j, h, m, x, y, a[16], s, c, k;
int ctn(long int n)
{
	int i = 0;
	while (n != 0)
	{
		i++;
		n /= 10;
	}
	return i;
}

int sca(long int n, int x)
{
	if (a[x + 1] - n < n)
		return 0;
	else
	{
		if (a[x]>n)
			return 1;
		else return 2;
	}
}

long int main()
{
	x = 1; y = 1;
	for (i = 1; i <= 16; i++)
	{
		a[i] = x; y *= 10; x += y;
			}

	cin >> n;

	while (n != 0)
	{
		k = ctn(n);
		m = sca(n, k);
		if (m == 0)
		{
			n = abs(a[k + 1] - n);
			s += k + 1;
		}
		else
		if (m==2)
		while (n >= a[k])
		{
			n =n - a[k];
			s += k;
		}
		else
		{
			n = abs(n - a[k]);
			s += k;
		}

	}
	cout << s;

}


#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
long int n, i, j, h, m, x, y, a[16], s, c, k;
int ctn(long int n)
{
	int i = 0;
	while (n != 0)
	{
		i++;
		n /= 10;
	}
	return i;
}

int sca(long int n, int x)
{
	if (a[x + 1] - n < n)
		return 0;
	else
	{
		if (a[x]>n)
			return 1;
		else return 2;
	}
}

long int main()
{
	x = 1; y = 1;
	for (i = 1; i <= 16; i++)
	{
		a[i] = x; y *= 10; x += y;
			}

	cin >> n;

	while (n != 0)
	{
		k = ctn(n);
		m = sca(n, k);
		if (m == 0)
		{
			n = abs(a[k + 1] - n);
			s += k + 1;
		}
		else
		if (m==2)
		while (n >= a[k])
		{
			n =n - a[k];
			s += k;
		}
		else
		{
			n = abs(n - a[k]);
			s += k;
		}

	}
	cout << s;

}


#include <iostream>
#include <utility>

#define ll long long
using namespace std;

int a[17];

int fun(ll N){
    if(N<=11)
    return min(N,2+11-N);
    int i;
    for(i=1;i<16;i++)
    {
        if(a[i]>N)
        break;
    }
    long long dir;
    dir=(N/a[i-1])*(i-1)+f(N%a[i-1]);
    if(N<a[i]-N)
    return dir;
    return min(dir,i+f(a[i]-N));
}

int main(){
   int i=0;
   a[1] = 1;
   for(i=2;i<16;i++){
       a[i] = 10*a[i-1] + 1;
   }

   ll N;
   cin >> N;
   int ans=fun(N);
   cout << ans;

   return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<string.h>
#include<queue>
#include<vector>
#include<deque>
using namespace std;
int juedui(int x)
{
    return x>=0?x:-x;
}
int main()
{
    int s,x1,x2,t1,t2,dir,pos;
    int ans;
    while((scanf("%d %d %d",&s,&x1,&x2)!=EOF)
    {
    scanf("%d %d",&t1,&t2);
    scanf("%d %d",&pos,&dir);
    if(x1<x2)
    {
        if(dir=1&&pos>x1)
        {
            ans=min((x2-x1)*t2,(2*s-(x2-pos))*t1);
        }
        if(dir=-1&&pos>x1)
        {
            ans=min((x2-x1)*t2,(x2+pos)*t1);
        }
        if(dir=1&&pos<=x1)
        {
            ans=min((x2-x1)*t2,t1*(x2-pos));
        }
        if(dir=-1&&pos<=x1)
        {
            ans=min((x2-x1)*t2,t1*(pos+x2));
        }
    }
    else
    {
        if(dir==1 and pos>=x1)
			ans=min(t2*(x1-x2), (2*s-pos-x2)*t1);
		if(dir==-1 and pos>=x1)
			ans=min(t2*(x1-x2), t1*(pos-x2));
		if(dir==1 and pos<x1)
			ans=min(t2*(x1-x2), t1*(2*s-pos-x2));
		if(dir==-1 and pos<x1)
			ans=min(t2*(x1-x2), t1*(2*s+pos-x2));
    }
    printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
//printf("Case %lld: %lld\n",i+1,cnt);
#define ll long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define mod 1000000007
#define digit(c) (c - '0')
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sc scanf
#define pp printf
#define INF 1e15
#define LONG_MA 1e12

using namespace std;

ll n,k,i,j,l,x,y,w,cnt,f=1,g,h,z,cyc,ans=0,par,e,q,r,t,m;
//ll a[115],b[100005],d[105];
//ll sp[105],dp[105],dis[105],low[105],disc[105],parent[105],rest[105],dist[10][10];
//ll c,d;
//string s,ss,p,qq;
//vector<ll> v[1005];//r[105],scc[105];
//vector<pair<ll,ll> > vec[105];
map<ll,ll > ma;
//ll dp[115][7];
//stack<ll> st;
//queue<ll> q;
pair<ll,ll> pi[100005],po[100005];
//priority_queue < pair<ll,ll> ,vector< pair<ll,ll> >,greater<pair<ll,ll> > > pq;
//struct Edge{ll src, dest, weight;};
//Edge edge[105];

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll s,p;
    cin>>s>>x>>y;
    cin>>z>>w;
    cin>>p>>h;
    ll a1;
    a1=w*abs(x-y);
    f=h;
    if(x<y){
        if(f==1 && p>x)l=2*s-p+x;
        else if(f==1 && p<=x)l=x-p;
        else if(f==-1 && p>x)l=p+x;
        else if(f==-1 && p<=x)l=p+x;
    }
    else{
        if(f==1 && p>=x)l=-p+2*s-x;
        else if(f==1 && p<x)l=2*s-p-x;
        else if(f==-1 && p>=x)l=p-x;
        else if(f==-1 && p<x)l=s+p+s-x;
    }
    double a2;
    cout<<l<<" "<<a1<<" ";
    a2=z*abs(x-y)+l*z;
    cout<<a2<<" ";
    double m,k,a3=100000000.00;
    for(i=1;i<=a1;i++){
        if(x<y)m=x+i*(1/w);
        else m=x-i*(1/w);
        if(f==1)k=(1/z)*i+p;
        if(f==-1)k=p-(1/z)*i;
        if(k>s){k=s-(k-s);f=-1;}
        else if(k<0){k=-k;f=1;}
        if(x<y)h=1;
        else h=-1;
        if(m==k && f==h){
            a3=min(a3,i+z*abs(y-m));
            cout<<i<<" ";
        }
    }
    cout<<a3<<" ";
    double s=min3((double)a1,a3,a2);
    cout<<s;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MX=2e5+5;
int main()
{
    int n;string h,ans;#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MX=2e5+5;
int s,x1,x2,t1,t2,p,d;
int main()
{
    cin>>s>>x1>>x2>>t1>>t2>>p>>d;
    int ans=t2*abs(x2-x1);
    if(x2>x1)
    {
        if(d==1)
        {
            if(p<=x1)
            ans=min(ans,t1*(x2-p));
            else ans=min(ans,t1*(s-p)+2*s*t1);
        }
        else
        {
            ans=min(ans,t1*(p)+s*t1);
        }
    }
    else
    {
    if(d==-1)
    {
    if(p>=x1)
    ans=min(ans,t1*(x2-p));
    else ans=min(ans,t1*(p)+2*s*t1);
    }
    else
    {
        ans=min(ans,t1*(s-p)+s*t1);
    }
    }
    cout<<ans;
    return 0;
}

    cin>>n>>h;
    reverse(h.begin(),h.end());
    size_t j=0;
    while(j<h.size())
    {
        ans=ans.substr(0,j/2)+h[j]+ans.substr(j/2,ans.size()-j/2);
        j++;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <cmath>
#include <limits>

int main() {
    int t1,t2,x1,x2,s,tram_pos;
    int dir;

    std::cin >> s >> x1 >> x2 >> t1 >> t2 >> tram_pos >> dir;

    dir = std::max(dir,0);

    int walking = abs(x1-x2)*t2;

    double tram_to_x2 = std::numeric_limits<double>::max();
    double tram_to_x1 = std::numeric_limits<double>::max();

    double x = -x2;
    double y = -x1;

    if((x-tram_pos >= 0) == dir)
    {
        tram_to_x2 = std::min(tram_to_x2, (x-tram_pos)*t1);

    }
    if((y-tram_pos >= 0) == dir)
    {
        tram_to_x1 = std::min(tram_to_x1, (y-tram_pos)*t1);
    }

    x = x2;
    y = x1;

    if((x-tram_pos >= 0) == dir)
    {
        tram_to_x2 = std::min(tram_to_x2, (x-tram_pos)*t1);

    }
    if((y-tram_pos >= 0) == dir)
    {
        tram_to_x1 = std::min(tram_to_x1, (y-tram_pos)*t1);

    }

    x = 2*s - x2;
    y = 2*s - x1;

    if((x-tram_pos >= 0) == dir)
    {
        tram_to_x2 = std::min(tram_to_x2, (x-tram_pos)*t1);

    }
    if((y-tram_pos >= 0) == dir)
    {
        tram_to_x1 = std::min(tram_to_x1, (y-tram_pos)*t1);

    }

    if(tram_to_x1 <= tram_to_x2)
    {
        std::cout << std::min(tram_to_x2, walking) << std::endl;
    }
    else
    {
        double added_time;
        if(x1 < x2) {
            added_time = (x1 + x2) * t1;
        }
        else
        {
            added_time = (2*s - x1 - x2)*t1;
        }

        std::cout << std::min(tram_to_x1 + added_time, walking) << std::endl;
        return 1;
    }

    return 0;

}


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

int main()
{
  int n,h,m;
  cin>>n>>h>>m;
  vector<int> a(n+1,h);
  while(m--)
  {
    int l,r,x;
    cin>>l>>r>>x;
    for(int i=l;i<=r;i++)
      a[i] = min(a[i],x);
  }
  int  ans =0;
  for(int i=1;i<=n;i++)
    ans += a[i]*a[i];
  cout<<ans<<"\n";
  return 0;
}


include <iostream>

using namespace std;
int a[1009],n,h,m;
int main()
{
    cin>>n>>h>>m;
    for(int i=1;i<=n;i++)
        a[i]=h;
    for(int i=1;i<=m;i++)
    {
        int x,y,val;
        cin>>x>>y>>val;
        for(int j=x;j<=y;j++)
            a[j]=min(a[j],val);
    }
    int s=0;
    for(int i=0;i<=n;i++)
        s+=(a[i]*a[i]);
    cout<<s;
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, h, m, tree[50 * 4 + 5];
void update(int ql, int qr, int v, int t = 1, int nl = 1, int nr = n) {
	if (qr < nl || nr < ql) return;
	if (ql <= nl && nr <= qr) tree[t] = v;
	else {
		int mid = nl + nr >> 1;
		update(ql, qr, v, 2 * t, nl, mid);
		update(ql, qr, v, 2 * t + 1, mid + 1, nr);
	}
}
int _get(int i, int t = 1, int nl = 1, int nr = n) {
	if (nl == nr) return min(tree[t], tree[1]);
	int mid = nl + nr >> 1;
	if (i <= mid) return min(tree[t*2], _get(i, t * 2, nl, mid));
	else return min(tree[t*2+1], _get(i, t * 2 + 1, mid + 1, nr));
}
int main(void) {
	memset(tree, 0x3f, sizeof(tree));
	scanf("%d %d %d", &n, &h, &m);
	update(1, n, h);
	while (m--) {
		int a, b, c; scanf("%d %d %d", &a, &b, &c);
		update(a, b, c);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		int x = _get(i);
		ans += x * x;
	}
	printf("%d\n", ans);
}


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

int main()
{
    int n,h,m,profit=0,i,j,;
    cin>>n>>h>>m;
    int arr[m][3];

    for(i=0;i<m;++i)
    {
        cin>>arr[i][0]>>arr[i][1]>>arr[2];
    }

    for( i=1;i<=n;i++)
    {   h1=h;
        for(j=0;j<m;++j)
        {
            if((i>=arr[j][0])&&(i<=arr[j][1]))
            {
                if(arr[j][2]<h)
                {
                    h=arr[j][2];
                }
            }
        }
        profit+=(h1*h1);
    }

    cout<<profit;
}


#define HEADER_H
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i,n)	for(auto i=decltype(n)(0); i<(n); ++i)
#define all(v)		begin(v), end(v)
using namespace std;		using ull= unsigned long long;
using ll = long long;		using ld = long double;
using vi = vector<ll>;		using vvi= vector<vi>;
using vb = vector<bool>;	using ii = pair<ll,ll>;
constexpr bool LOG = false;
void Log() { if(LOG) cerr << "\n"; }
template<class T, class... S> void Log(T t, S... s){
	if(LOG) cerr << t << "\t", Log(s...);
} /* ============== END OF HEADER ============== */

ll gcd(ll a, ll b) { while (b) { a %= b; swap(a, b); } return a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b;				 }
ll mod(ll a, ll b) { return ((a % b) + b) % b;				   }

// Finds x, y s.t. ax + by = d = gcd(a, b).
void extended_euclid(ll a, ll b, ll &x, ll &y, ll &d) {
	ll xx = y = 0;
	ll yy = x = 1;
	while (b) {
		ll q = a / b;
		ll t = b; b = a % b; a = t;
		t = xx; xx = x - q * xx; x = t;
		t = yy; yy = y - q * yy; y = t;
	}
	d = a;
}

// solves ab = 1 (mod n), -1 on failure
ll mod_inverse(ll a, ll n) {
	ll x, y, d;
	extended_euclid(a, n, x, y, d);
	return (d > 1 ? -1 : mod(x, n));
}

// (a*b)%m
ll mulmod(ll a, ll b, ll m){
	ll x = 0, y=a%m;
	while(b>0){
		if(b&1)
			x = (x+y)%m;
		y = (2*y)%m;
		b/=2;
	}
	return x % m;
}
ll mulmod2(ll a, ll b, ll m){ return __int128(a)*b%m; }

ll pow(ll b, ll e) {		// b^e in logarithmic time
	ll p = e<2 ? 1 : pow(b*b,e/2);
	return e&1 ? p*b : p;
}

// Finds b^e % m in O(lg n) time, ensure that b < m to avoid overflow!
ll powmod(ll b, ll e, ll m) {
	ll p = e<2 ? 1 : powmod((b*b)%m,e/2,m);
	return e&1 ? p*b%m : p;
}

// Solve ax + by = c, returns false on failure.
bool linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) {
	ll d = gcd(a, b);
	if (c % d) {
		return false;
	} else {
		x = c / d * mod_inverse(a / d, b / d);
		y = (c - a * x) / b;
		return true;
	}
}

// Solves x = a1 mod m1, x = a2 mod m2, x is unique modulo lcm(m1, m2).
// Returns {0, -1} on failure, {x, lcm(m1, m2)} otherwise.
pair<ll, ll> crt(ll a1, ll m1, ll a2, ll m2) {
	ll s, t, d;
	extended_euclid(m1, m2, s, t, d);
	if (a1 % d != a2 % d) return {0, -1};
	return {mod(s * a2 * m1 + t * a1 * m2, m1 * m2) / d, m1 / d * m2};
}

// Solves x = ai mod mi. x is unique modulo lcm mi.
// Returns {0, -1} on failure, {x, lcm mi} otherwise.
pair<ll, ll> crt(vector<ll> &a, vector<ll> &m) {
	pair<ll, ll> res = {a[0], m[0]};
	for (ull i = 1; i < a.size(); ++i) {
		res = crt(res.first, res.second, mod(a[i], m[i]), m[i]);
		if (res.second == -1) break;
	}
	return res;
}

ll SIZE;
vector<bool> bs;
vector<ll> primes;

void sieve(ll size = 1e6+10) { // call at start in main!
	SIZE = size; bs.assign(SIZE+1,1);
	bs[0] = bs[1] = 0;
	for (ll i = 2; i <= SIZE; i++) if (bs[i]) {
		for (ll j = i * i; j <= SIZE; j += i) bs[j] = 0;
		primes.push_back(i);
	}
}

bool is_prime(ll n) { // for N <= SIZE^2
	if (n <= SIZE) return bs[n];
	for(const auto &prime : primes)
	   	if (n % prime == 0) return false;
	return true;
}

struct Factor{ll prime; ll exp;}; using Fs = vector<Factor>;
Fs factor(ll n) {
	Fs factors;
	for(const auto &prime : primes){
		if(n==1 || prime*prime > n) break;
		ll exp=0;
		while(n % prime == 0)
		   	n/=prime, exp++;
		if(exp>0)
			factors.push_back({prime,exp});
	}
	if (n != 1) factors.push_back({n,1});
	return factors;
}

constexpr ll M = 1e9+7;

struct S{
	ll div, children, divsum;
	ll x=0;
};
vector<S> divisors(const Fs &fs){
	vector<S> ds{{1,1,1}}; // pair<divisor, divisors>
	ds.reserve(accumulate(fs.begin(),fs.end(),1, [](ll a,auto f){return a*(f.exp+1);}));
	for(auto f : fs)
		for(auto d : ds){
			ll sumpow = 1;
			ll pow = 1;
			for(ll i = 0; i < f.exp; ++i){
				pow *= f.prime;
				sumpow += pow;
				ds.push_back({d.div*pow, d.children*(i+2), d.divsum * sumpow});
			}
		}
	return ds;
}

vi fac;

ll binom(ll a, ll b){
	ll ans;
	if(b <0 || b>a) ans=0;
	else ans = fac[a] * mod_inverse(fac[b] * fac[a-b] % M, M) %M;
	Log("binom",a,b,ans);
	return ans;
}

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

	sieve();

	fac.resize(1000001);
	fac[0] = 1;
	REP(i,1000000)
		fac[i+1] = (i+1)*fac[i] % M;

	ll n,k,q; cin >> n >> k >> q;
	vi div(1000001,0);

	ll ans = 0;
	REP(i,n+q){
		ll c; cin >> c;
		Log("read ",c);
		auto fs = factor(c);
		auto divs = divisors(fs);
		Log("divisors, divsum,children");
		for(auto x : divs){
			Log(x.div, x.divsum, x.children);
		}

		for(auto it=divs.rbegin(); it!=divs.rend(); ++it){
			ll val = it->div;
			ll children = it->children;
			ll divsum = it->divsum;
			Log("val",val, "children",children,"div",div[val]);
			ll a = binom(div[val],k-1);
			ll b = a - it->x;
			// add for current
			ans += val*b%M;
			Log(a, b, "add", val*b);
			//ans -= (divsum-val)*a%M;
			for(ll i = 0; divs[i].div != val; ++i)
				if(val % divs[i].div == 0)
					divs[i].x += b;

			++div[val];
		}

		Log("ANS:");
		if(i>=n)
			cout << ans << "\n";

	}

	return 0;
}


#define HEADER_H
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i,n)	for(auto i=decltype(n)(0); i<(n); ++i)
#define all(v)		begin(v), end(v)
using namespace std;		using ull= unsigned long long;
using ll = long long;		using ld = long double;
using vi = vector<ll>;		using vvi= vector<vi>;
using vb = vector<bool>;	using ii = pair<ll,ll>;
constexpr bool LOG = false;
void Log() { if(LOG) cerr << "\n"; }
template<class T, class... S> void Log(T t, S... s){
	if(LOG) cerr << t << "\t", Log(s...);
} /* ============== END OF HEADER ============== */

ll gcd(ll a, ll b) { while (b) { a %= b; swap(a, b); } return a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b;				 }
ll mod(ll a, ll b) { return ((a % b) + b) % b;				   }

// Finds x, y s.t. ax + by = d = gcd(a, b).
void extended_euclid(ll a, ll b, ll &x, ll &y, ll &d) {
	ll xx = y = 0;
	ll yy = x = 1;
	while (b) {
		ll q = a / b;
		ll t = b; b = a % b; a = t;
		t = xx; xx = x - q * xx; x = t;
		t = yy; yy = y - q * yy; y = t;
	}
	d = a;
}

// solves ab = 1 (mod n), -1 on failure
ll mod_inverse(ll a, ll n) {
	ll x, y, d;
	extended_euclid(a, n, x, y, d);
	return (d > 1 ? -1 : mod(x, n));
}

// (a*b)%m
ll mulmod(ll a, ll b, ll m){
	ll x = 0, y=a%m;
	while(b>0){
		if(b&1)
			x = (x+y)%m;
		y = (2*y)%m;
		b/=2;
	}
	return x % m;
}

ll pow(ll b, ll e) {		// b^e in logarithmic time
	ll p = e<2 ? 1 : pow(b*b,e/2);
	return e&1 ? p*b : p;
}

// Finds b^e % m in O(lg n) time, ensure that b < m to avoid overflow!
ll powmod(ll b, ll e, ll m) {
	ll p = e<2 ? 1 : powmod((b*b)%m,e/2,m);
	return e&1 ? p*b%m : p;
}

// Solve ax + by = c, returns false on failure.
bool linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) {
	ll d = gcd(a, b);
	if (c % d) {
		return false;
	} else {
		x = c / d * mod_inverse(a / d, b / d);
		y = (c - a * x) / b;
		return true;
	}
}

// Solves x = a1 mod m1, x = a2 mod m2, x is unique modulo lcm(m1, m2).
// Returns {0, -1} on failure, {x, lcm(m1, m2)} otherwise.
pair<ll, ll> crt(ll a1, ll m1, ll a2, ll m2) {
	ll s, t, d;
	extended_euclid(m1, m2, s, t, d);
	if (a1 % d != a2 % d) return {0, -1};
	return {mod(s * a2 * m1 + t * a1 * m2, m1 * m2) / d, m1 / d * m2};
}

// Solves x = ai mod mi. x is unique modulo lcm mi.
// Returns {0, -1} on failure, {x, lcm mi} otherwise.
pair<ll, ll> crt(vector<ll> &a, vector<ll> &m) {
	pair<ll, ll> res = {a[0], m[0]};
	for (ull i = 1; i < a.size(); ++i) {
		res = crt(res.first, res.second, mod(a[i], m[i]), m[i]);
		if (res.second == -1) break;
	}
	return res;
}

ll SIZE;
vector<bool> bs;
vector<ll> primes;

void sieve(ll size = 1e6+10) { // call at start in main!
	SIZE = size; bs.assign(SIZE+1,1);
	bs[0] = bs[1] = 0;
	for (ll i = 2; i <= SIZE; i++) if (bs[i]) {
		for (ll j = i * i; j <= SIZE; j += i) bs[j] = 0;
		primes.push_back(i);
	}
}

bool is_prime(ll n) { // for N <= SIZE^2
	if (n <= SIZE) return bs[n];
	for(const auto &prime : primes)
	   	if (n % prime == 0) return false;
	return true;
}

struct Factor{ll prime; ll exp;}; using Fs = vector<Factor>;
Fs factor(ll n) {
	Fs factors;
	for(const auto &prime : primes){
		if(n==1 || prime*prime > n) break;
		ll exp=0;
		while(n % prime == 0)
		   	n/=prime, exp++;
		if(exp>0)
			factors.push_back({prime,exp});
	}
	if (n != 1) factors.push_back({n,1});
	return factors;
}

constexpr ll M = 1e9+7;

struct S{
	ll div, children, divsum;
	ll x=0;
};
vector<S> divisors(const Fs &fs){
	vector<S> ds{{1,1,1}}; // pair<divisor, divisors>
	ds.reserve(accumulate(fs.begin(),fs.end(),1, [](ll a,auto f){return a*(f.exp+1);}));
	for(auto f : fs)
		for(auto d : ds){
			ll sumpow = 1;
			ll pow = 1;
			for(ll i = 0; i < f.exp; ++i){
				pow *= f.prime;
				sumpow += pow;
				ds.push_back({d.div*pow, d.children*(i+2), d.divsum * sumpow});
			}
		}
	return ds;
}

vi fac;

ll binom(ll a, ll b){
	ll ans;
	if(b <0 || b>a) ans=0;
	else ans = fac[a] * mod_inverse(fac[b] * fac[a-b] % M, M) %M;
	Log("binom",a,b,ans);
	return ans;
}

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

	sieve();

	fac.resize(1000001);
	fac[0] = 1;
	REP(i,1000000)
		fac[i+1] = (i+1)*fac[i] % M;

	ll n,k,q; cin >> n >> k >> q;
	vi div(1000001,0);

	ll ans = 0;
	REP(i,n+q){
		ll c; cin >> c;
		Log("read ",c);
		auto fs = factor(c);
		auto divs = divisors(fs);
		Log("divisors, divsum,children");
		for(auto x : divs){
			Log(x.div, x.divsum, x.children);
		}

		for(auto it=divs.rbegin(); it!=divs.rend(); ++it){
			ll val = it->div;
			ll children = it->children;
			ll divsum = it->divsum;
			Log("val",val, "children",children,"div",div[val]);
			ll a = binom(div[val],k-1);
			ll b = a - it->x;
			// add for current
			ans += val*b%M;
			Log(a, b, "add", val*b);
			//ans -= (divsum-val)*a%M;
			for(ll i = 0; divs[i].div != val; ++i)
				if(val % divs[i].div == 0)
					divs[i].x += b;

			++div[val];
		}

		Log("ANS:");
		if(i>=n)
			cout << ans << "\n";

	}

	return 0;
}


#define HEADER_H
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i,n)	for(auto i=decltype(n)(0); i<(n); ++i)
#define all(v)		begin(v), end(v)
using namespace std;		using ull= unsigned long long;
using ll = long long;		using ld = long double;
using vi = vector<ll>;		using vvi= vector<vi>;
using vb = vector<bool>;	using ii = pair<ll,ll>;
constexpr bool LOG = false;
void Log() { if(LOG) cerr << "\n"; }
template<class T, class... S> void Log(T t, S... s){
	if(LOG) cerr << t << "\t", Log(s...);
} /* ============== END OF HEADER ============== */

ll gcd(ll a, ll b) { while (b) { a %= b; swap(a, b); } return a; }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b;				 }
ll mod(ll a, ll b) { return ((a % b) + b) % b;				   }

// Finds x, y s.t. ax + by = d = gcd(a, b).
void extended_euclid(ll a, ll b, ll &x, ll &y, ll &d) {
	ll xx = y = 0;
	ll yy = x = 1;
	while (b) {
		ll q = a / b;
		ll t = b; b = a % b; a = t;
		t = xx; xx = x - q * xx; x = t;
		t = yy; yy = y - q * yy; y = t;
	}
	d = a;
}

// solves ab = 1 (mod n), -1 on failure
ll mod_inverse(ll a, ll n) {
	ll x, y, d;
	extended_euclid(a, n, x, y, d);
	return (d > 1 ? -1 : mod(x, n));
}

// (a*b)%m
ll mulmod(ll a, ll b, ll m){
	ll x = 0, y=a%m;
	while(b>0){
		if(b&1)
			x = (x+y)%m;
		y = (2*y)%m;
		b/=2;
	}
	return x % m;
}

ll pow(ll b, ll e) {		// b^e in logarithmic time
	ll p = e<2 ? 1 : pow(b*b,e/2);
	return e&1 ? p*b : p;
}

// Finds b^e % m in O(lg n) time, ensure that b < m to avoid overflow!
ll powmod(ll b, ll e, ll m) {
	ll p = e<2 ? 1 : powmod((b*b)%m,e/2,m);
	return e&1 ? p*b%m : p;
}

// Solve ax + by = c, returns false on failure.
bool linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) {
	ll d = gcd(a, b);
	if (c % d) {
		return false;
	} else {
		x = c / d * mod_inverse(a / d, b / d);
		y = (c - a * x) / b;
		return true;
	}
}

// Solves x = a1 mod m1, x = a2 mod m2, x is unique modulo lcm(m1, m2).
// Returns {0, -1} on failure, {x, lcm(m1, m2)} otherwise.
pair<ll, ll> crt(ll a1, ll m1, ll a2, ll m2) {
	ll s, t, d;
	extended_euclid(m1, m2, s, t, d);
	if (a1 % d != a2 % d) return {0, -1};
	return {mod(s * a2 * m1 + t * a1 * m2, m1 * m2) / d, m1 / d * m2};
}

// Solves x = ai mod mi. x is unique modulo lcm mi.
// Returns {0, -1} on failure, {x, lcm mi} otherwise.
pair<ll, ll> crt(vector<ll> &a, vector<ll> &m) {
	pair<ll, ll> res = {a[0], m[0]};
	for (ull i = 1; i < a.size(); ++i) {
		res = crt(res.first, res.second, mod(a[i], m[i]), m[i]);
		if (res.second == -1) break;
	}
	return res;
}

ll SIZE;
vector<bool> bs;
vector<ll> primes;

void sieve(ll size = 1e6+10) { // call at start in main!
	SIZE = size; bs.assign(SIZE+1,1);
	bs[0] = bs[1] = 0;
	for (ll i = 2; i <= SIZE; i++) if (bs[i]) {
		for (ll j = i * i; j <= SIZE; j += i) bs[j] = 0;
		primes.push_back(i);
	}
}

bool is_prime(ll n) { // for N <= SIZE^2
	if (n <= SIZE) return bs[n];
	for(const auto &prime : primes)
	   	if (n % prime == 0) return false;
	return true;
}

struct Factor{ll prime; ll exp;}; using Fs = vector<Factor>;
Fs factor(ll n) {
	Fs factors;
	for(const auto &prime : primes){
		if(n==1 || prime*prime > n) break;
		ll exp=0;
		while(n % prime == 0)
		   	n/=prime, exp++;
		if(exp>0)
			factors.push_back({prime,exp});
	}
	if (n != 1) factors.push_back({n,1});
	return factors;
}

constexpr ll M = 1e9+7;

struct S{
	ll div, children, divsum, x;
};
vector<S> divisors(const Fs &fs){
	vector<S> ds{{1,1,1,0}}; // pair<divisor, divisors>
	ds.reserve(accumulate(fs.begin(),fs.end(),1, [](ll a,auto f){return a*(f.exp+1);}));
	for(auto f : fs)
		for(auto d : ds){
			ll sumpow = 1;
			ll pow = 1;
			for(ll i = 0; i < f.exp; ++i){
				pow *= f.prime;
				sumpow += pow;
				ds.push_back({d.div*pow, d.children*(i+2), d.divsum * sumpow,0});
			}
		}
	return ds;
}

vi fac;

ll binom(ll a, ll b){
	ll ans;
	if(b <0 || b>a) ans=0;
	else ans = fac[a] * mod_inverse(fac[b] * fac[a-b] % M, M) %M;
	Log("binom",a,b,ans);
	return ans;
}

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

	sieve();

	fac.resize(1000001);
	fac[0] = 1;
	REP(i,1000000)
		fac[i+1] = (i+1)*fac[i] % M;

	ll n,k,q; cin >> n >> k >> q;
	vi div(1000001,0);

	ll ans = 0;
	REP(i,n+q){
		ll c; cin >> c;
		Log("read ",c);
		auto fs = factor(c);
		auto divs = divisors(fs);
		Log("divisors, divsum,children");
		for(auto x : divs){
			Log(x.div, x.divsum, x.children);
		}

		for(auto it=divs.rbegin(); it!=divs.rend(); ++it){
			ll val = it->div;
			ll children = it->children;
			ll divsum = it->divsum;
			Log("val",val, "children",children,"div",div[val]);
			ll a = binom(div[val],k-1);
			ll b = a - it->x;
			// add for current
			ans += val*b%M;
			Log(a, b, "add", val*b);
			//ans -= (divsum-val)*a%M;
			for(ll i = 0; divs[i].div != val; ++i)
				if(val % divs[i].div == 0)
					divs[i].x += b;

			++div[val];
		}

		Log("ANS:");
		if(i>=n)
			cout << ans << "\n";

	}

	return 0;
}


#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)

#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n)  { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }

#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
#define div div____
using namespace std;

const int MN = 2000111;
int n, k, q;
int a[MN], sieve[MN];

void init() {
    FOR(i,2,1000) if (!sieve[i]) {
        for(int j = i*i; j < MN; j += i)
            sieve[j] = i;
    }
}

int dp[111], dk[111];
vector<int> div;
void attempt(int i, int nd, int prod) {
    if (i > nd) {
        div.push_back(prod);
        return ;
    }
    int cur = 1;
    FOR(power,0,dk[i]) {
        attempt(i+1, nd, prod * cur);
        cur *= dp[i];
    }
}
vector<int> divisors(int x) {
    int nd = 0;
    while (x > 1) {
        int p = (sieve[x] ? sieve[x] : x);
        ++nd;
        dp[nd] = p;
        dk[nd] = 0;
        while (x % p == 0) {
            x /= p;
            ++dk[nd];
        }
    }
    div.clear();
    attempt(1, nd, 1);
    return div;
}

const int MOD = 1e9 + 7;
int res[MN];
vector<int> ls[MN];

void resetAll() {
    memset(res, 0, sizeof res);
    REP(i,MN) ls[i].clear();
}

int add[MN], gt[MN], inv_gt[MN];

int power(int x, int k) {
    if (k == 0) return 1;
    if (k == 1) return x % MOD;

    int mid = power(x, k >> 1);
    mid = mid * mid % MOD;

    if (k & 1) return mid * x % MOD;
    return mid;
}
int inverse(int x) {
    return power(x, MOD - 2);
}

int C(int n, int k) {
    return gt[n] * inv_gt[k] % MOD * inv_gt[n-k] % MOD;
}

#undef int
int main() {
#define int long long
    ios :: sync_with_stdio(0); cin.tie(0);
    init();

    gt[0] = 1; FOR(i,1,MN-1) gt[i] = gt[i-1] * i % MOD;
    REP(i,MN) inv_gt[i] = inverse(gt[i]);

    while (scanf("%lld%lld%lld", &n, &k, &q) == 3) {
        FOR(i,1,n+q) scanf("%lld", &a[i]);
        resetAll();

        FOR(i,1,n+q) {
            auto d = divisors(a[i]);
            for(int x : d) {
                ls[x].push_back(i);
            }
        }
        FOR(g,1,1000*1000) {
            add[g] = g;
            auto d = divisors(g);
            for(int x : d) if (x < g) {
                add[g] = add[g] - add[x];
            }

            FOR(i,k-1,SZ(ls[g])-1) {
                int u = ls[g][i];
                res[u] = (res[u] + C(i, k-1) * add[g]) % MOD;
            }
        }
        FOR(i,1,n+q) {
            res[i] = (res[i-1] + res[i]) % MOD;
        }
        FOR(i,n+1,n+q) printf("%lld\n", res[i]);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

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

    int n;
    cin >> n;

    vector<int> a(n);
    for (int& x : a)
        cin >> x;

    vector<int> cnt(44);
    vector<int> step(44);
    int p2 = 1, o2 = 0;
    for (int i = 0; i < n; i++){
        while (p2*2 <= a[i]) {
            p2 *= 2;
            o2 += 1;
        }
        if (p2 == a[i])
            step[o2]++;
        else
            cnt[o2]++;

        int c = 0;
        while (!(x&1)){
            x >>= 1;
            c++;
        }
        if (x != 1){
            cnt[o2]++;
        }
        else{
            step[c]++;
        }
    }
/*
    for (int i : step) cerr << i << ' ';
    cerr << endl;
    for (int i : cnt) cerr << i << ' ';
    cerr << endl;
*/

    vector<int> ans;
    for (int i = 1; i <= step[0]; i++){
        vector<int> t(44);
        vector<int> ost(44);
        ost = cnt;
        ost[0] += step[0] - i;
        int b = i;
        for (int j = 1; j < 44; j++){
            int m = step[j];
            int mini = min(b, m);
            t[j - 1] = b - mini;
            ost[j] += m - mini;
            b = mini;
        }

        /*for (int i : t) cerr << i << ' ';
        cerr << endl;
        for (int i : ost) cerr << i << ' ';
        cerr << endl;*/

        bool f = false;
        int s1 = 0;
        int s2 = 0;
        for (int j = 0; j < 44; j++){
            s2 += t[j];
            s2 -= ost[j];
        }
        if (s2 < 0)
            continue;
        s1 = s2;
        s2 = 0;
        int j = 0;
        while (j < 44){
            s1 += ost[j];
            s2 += t[j];
            if (s1 < s2){
                f = true;
                break;
            }
            j++;
        }
        if (s1 > i)
            f = true;
        if (!f){
            ans.push_back(i);
        }
    }

    for (int& x : ans)
        cout << x << ' ';

    if (ans.size() == 0)
        cout << -1;

    return 0;
}


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

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define ull unsigned long long
#define pll pair < ll, ll >
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define all(s) s.begin(), s.end()
#define sz(a) (int)a.size()

const int inf = (1ll << 30) - 1;
const int maxn = (int) 1e5 + 10;

using namespace std;

int n;
ll a[200100];
ll bb[165];
int used[200100];
int cnt[200100];
void solve(){
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%I64d", &a[i]);
	}
	bb[0] = 1;
	for(int i = 1; i < 55; i++){
		bb[i] = bb[i-1] * 2;
	}
	int it = 0;
	vector<ll> b;
	map<ll, int> good;
	for(int i = 0; i < n; ){
		while(i < n && bb[it] > a[i]) {
			b.pb(a[i]);
			i++;
		}
		while(i < n && bb[it] == a[i]) {
			cnt[it]++;
			i++;
		}
		it++;
	}
	int C = 0;
	int T = 0;
	int ind = (int)b.size();
	--ind;
	int A = 0;
	for(int i = 53; i >= 0; i--){
		while(cnt[i] > 0){
			int j = i;
			while(j >= 0 && cnt[j] > 0 ) j--;
			if(j >= 0) break;
			for(int k = 0; k <= i; k++){
				cnt[k]--;
				C++;
			}
			T++;
			A++;
			if(ind >= 0 &&T>0&& b[ind] <= (1ll<<i) * 2) {
				ind--;
				T--;
				C++;
			}
			if(ind == -1 && n - C <= T){
				used[A] = 1;
			}
		}

		while(ind + 1< b.size() && cnt[i] > 0){
			if(b[ind+1] > (1ll<<i)) break;
			ind++;
			cnt[i]--;
		}
		while(T > 0 && cnt[i] > 0){
			T--;
			cnt[i]--;
			C++;
		}
		while(T > 0 && ind >= 0){
			if(b[ind] >= (1ll<<i) && b[ind] <= (1ll<<i) * 2) ind--;
			else break;
			T--;
			C++;
		}
		if(n - C <= T && ind == -1) used[A] = 1;
		if(cnt[i] > 0){
			printf("-1\n");
			return;
		}
	}

	vector<int> ans;
	for(int i = 1; i<=100010; i++)
		if(used[i]) ans.pb(i);
	if(ans.size() == 0) printf("-1\n");
	else{
		for(int i = 0; i < ans.size(); i++){
			if(i > 0) printf(" ");
			printf("%d", ans[i]);
		}
		printf("\n");
	}
}

int main () {
	#ifdef LOCAL
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif
    int t=1;
    //scanf("%d", &t);
    for(int i=1; i <= t; i++){
      //printf("Case #%d\n", i);
      solve();
    }

    return 0;
}
close


#define _CRT_SECURE_NO_WARNINGS

using namespace std;

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

typedef long long li;
typedef pair<int, int> pt;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sqr(a) ((a) * (a))
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define sz(a) (int(a.size()))
#define all(a) a.begin(), a.end()

const int INF = int(1e9);
const li INF64 = INF * 1ll * INF;
const ld PI = acosl(-1.0);
const int MOD = INF + 7;
const ld EPS = 1e-9;

vector<li> a;
int n;
int cntdeg[60];

int maxdeg(li x)
{
	int ans = 0;
	while (x)
	{

		x = ((x == 1 ? x : x + 1)) / 2;
		if (x) ans++;
	}
	return ans;
}

bool check(int x)
{
	forn(i, 60) cntdeg[i] = 0;
	int cntlast = x;
	int cntcur = 0;
	li deglast = 1;
	int curdeg = 0;
	vector<li> rem;
	for (int i = 0; i < n; i++)
	{
		if (a[i] == deglast)
		{
			if (cntcur == cntlast)
			{
				rem.push_back(a[i]);
			}
			else
			{
				cntcur++;
			}
		}
		else if (a[i] > deglast)
		{
			deglast <<= 1ll;
			if (a[i] > deglast)
				return false;
			cntdeg[curdeg] = cntcur;
			cntlast = cntcur;
			curdeg++;
			cntcur = 0;
			if (a[i] == deglast && cntcur < cntlast)
				cntcur++;
			else
				rem.push_back(a[i]);
		}
		else
		{
			rem.push_back(a[i]);
		}
	}
	cntdeg[curdeg] = cntcur;
	cntlast = cntcur;
	curdeg++;
	cntcur = 0;
	int lastcnt = 0;
	for (int i = 1; i < 60; i++)
		cntdeg[i - 1] -= cntdeg[i];
	for (auto z : rem)
	{
		while (lastcnt < 60 && (cntdeg[lastcnt] == 0 || maxdeg(z) > lastcnt + 1))
			lastcnt++;
		if (lastcnt == 60)
			return false;
		cntdeg[lastcnt]--;
	}
	return true;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d", &n);
	a.resize(n);
	forn(i, n)
		scanf("%lld", &a[i]);
	if(n == 2 && a[0] == 1 && a[1] == 1) {
	    puts("1 2");
	    return;
	}
	int cnt1 = 0;
	forn(i, n)
		if (a[i] == 1)
			cnt1++;
	if (!check(cnt1))
	{
		puts("-1");
		return 0;
	}
	int l = 1;
	int r = cnt1;
	while (r > l + 1)
	{
		int mid = (l + r) >> 1;
		if (check(mid))
			r = mid;
		else
			l = mid;
	}
	for (int i = r; i <= cnt1; i++)
		printf("%d ", i);
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <sstream>
#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

typedef long long ll;

set<int> solve(vector<ll> a) {
  int n = a.size();
  vector<pair<int, int>> v;
  ll cur = 1;
  ll nxt = 2;
  v.push_back(make_pair(0, 0));
  for (int i = 0; i < n; i++) {
    ll x;
    x = a[i];
    while (1) {
      if (x == cur) {
        v.back().first++;
        break;
      }
      if (x < nxt) {
        v.back().second++;
        break;
      }
      v.push_back(make_pair(0, 0));
      cur *= 2;
      nxt *= 2;
    }
  }

  v.push_back(make_pair(0, 0));
  vector<int> b;
  while (1) {
    int x = 0;
    for (int i = 0; i < v.size(); i++) {
      if (v[i].first == 0) {
        x = i;
        break;
      }
      v[i].first--;
    }
    if (x == 0) break;
    b.push_back(x);
  }
  int last = 0;
  vector<int> ans;
  for (int i = (int)v.size() - 1; i >= 0; i--) {
    while (v[i].first + v[i].second != 0) {
      if (last >= b.size()) {
        cout << -1;
        return 0;
      }
      if (i > b[last]) {
        cout << -1;
        return 0;
      }
      if (v[i].first) v[i].first--;
      else v[i].second--;
      last++;
    }
  }
  ans.push_back(b.size());
  while (1) {
    v[0].second += b.back();
    b.pop_back();
    for (int i = 0; i >= 0; i--) {
      while (v[i].first + v[i].second != 0) {
        if (last >= b.size()) {
          goto end;
        }
        if (v[i].first) v[i].first--;
        else v[i].second--;
        last++;
      }
    }
    ans.push_back(b.size());
  }
  end:;
}

int main() {
#ifdef XXX
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

  sort(ans.begin(), ans.end());
  for (auto el : ans) cout << el << " ";

	return 0;
}


//abo  masteriiiiii
#include<iostream>
using namespace std;
int main(){
    int n,x1,y1,x2,y2,ans=0;
    cin>>n>>x1>>y1>>x2>>y2;
    while(x1!=x2||y1!=y2){
        if(x1==0)
            if(y1==n)
                x1++;
            else
                y1++;
        else if(x1==n)
            if(y1==0)
                x1--;
            else
                y1--;
        else if(y1==0)
            if(x1==0)
                y1++;
            else
                x1--;
        else if(y1==n)
            if(x1==n)
                y1--;
            else
                x1++;
        ans++;
    }
    cout<<min(4*n-ans)<<endl;

    //system("pause");
}


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

int main(){
    int n,x1,y1,x2,y2;
    cin>>n>>x1>>y1>>x2>>y2;
    int ans=INT_MAX;
   if(abs(x1-x2)==n){
        int y_1=n-y1;
        int y_2=y1;
        int y_3=n-y2;
        int y_4=y2;
        int y=min(y_2+y_4,y_1+y_3);
        cout<<n+y<<endl;
   }else if(abs(y1-y2)==n){
        int x_1=n-x1;
        int x_2=x1;
        int x_3=n-x2;
        int x_4=x2;
        int x=min(x_2+x_4,x_1+x_3);
        cout<<n+x<<endl;
   }else{
        cout<<ans(x1-x2)+abs(y1-y2)<<endl;
   }

}


#include <iostream>

int min(int a,int b)
{
    return a<b?a:b;
}

int main()
{
    int n,x1,y1,x2,y2;
    std::cin>>n>>x1>>y1>>x2>>y2;
    std::cout<<min(y1+y2,2*n-y1-y2)+min(x1,x2,2*n-x1-x2);
}


/* 2013-08-10 10:13:15.383542 */#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>

using namespace std;
int N, X[5], Y[5];

int To(int x, int y){
	int res = 0;
	for (int i = 0; i < 2; i ++){
		int tmpx = X[i], tmpy = Y[i];
		if (tmpx != x) res += abs(x - tmpx);
		tmpx = x;
		if (tmpy != y) res += abs(y - tmpy);
		tmpy = y;
	}
	return res;
}

int main() {
	scanf("%d%d%d%d%d",&N, &X[0], &Y[0], &X[1], &Y[1]);
	int Ans = min(min(To(0, 0), To(0, N)), min(To(N, 0), To(N, N)));
	if (X[0] == X[1]) Ans = min(Ans, abs(Y[0] - Y[1]));
	if (Y[0] == Y[1]) Ans = min(Ans, abs(X[0] - X[1]));
	printf("%d\n", Ans);
	return 0;
}


#include <cstdio>
#include <iostream>
string s = "1001010111001010";
int main(){
    int n;
    scanf("%d", &n);
    printf("%c\n", s[n - 1]);
    return 0;
}


#include<cstdio>
using namespace std;
int n;
int main(){
	scanf("%d",&n);
	if(i==1||i==4||i==6||i==8||i==9||i==10||i==13||i==15) printf("1");
	else printf("0");
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    string = "1001010111001010";
    cin >> n;
    cout << s[n - 1];
    return 0;
}


using namespace std;

int main()
{
    int wynik=0;
    int a,b,c,d;  // a=wysokosc robaka,b=wysokosc jabłka,c=predkosc w dzien,d=predkosc nocą
    cin>>a>>b>>c>>d;
    if(d>c)
    {
        int r=0;
        r=b-a;
        if(8*c>r)
        {
            cout<<1;
        }else
        cout<<-1;
    }else
    {

    int r=0;
    r=b-a;
    r=r-(8*c);
    while(r>0)
    {

        r=r-(12*d);
        r=r+(12*c);
        wynik++;
    }

    cout<<wynik;
    }

    return 0;
}


#include <bits/stdc++.h>

#define asd(i,n,m) for (int i=n;i<m;i++)
#define dsa(i,n,m) for (int i=n;i>=m;i--)
#define b_s binary_search
#define ll long long
#define Pi 3.1415926535
#define vr vector
#define pb push_back
#define sw swap

using namespace std;

int main()
{
	int b,a,h1,h2;
	cin >> h1 >> h2 >> a >> b;
	if (h1+a*8>=h2) {cout << 0;return 0;}
	if (b>a) {cout << "-1";return 0;}
	h1+=a*8-b*12;
	asd (i,1,500000) {
		h1+=a*12;
		if (h1>=h2) {cout << i;return 0;}
		h1-=b*12;
	}
	return 0;


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	long long int h1, h2;
	long long int a, b;
	long long int k;
	cin >> h1 >> h2;
	cin >> a >> b;
	if ((b - a) == 0)
	{
		if ((h2 - h1) <= 10)
		{
			cout << "0";
			return 0;
		}
		else
		{

			cout << "-1";
			return 0;
		}
	}
	else
	if ((a - b) >= 1)
	{
		long long int sum = h1;
		long long int count = 0;
		for (int i = 3; i <= 10; i++)
		{
			sum += a;
			count++;
		}
		if (sum >= h2)
		{
			cout << "0";
			return 0;
		}
		for (int j = 1; j <= 12; j++)
		{
			sum -= b;
			count++;
		}

		int t = 1;
		while (sum < h2)
		{
			while (sum < h2 || t <= 12)
			{
				sum += a;
				t++;
				count++;
			}
			if (sum >= h2)
			{
				cout << count / 24;
				return 0;
			}
			t = 1;
			while (t <= 12)
			{
				sum -= b;
				t++;
				count++;
			}
		}
		cout << count / 24;
		return 0;
	}
	else
	if (a - b < 0)
	{
		if ((b - a) == 1)
		{
			if ((h2 - h1) <= ((8 * (b - a))))
			{
				cout << "1";
				return 0;
			}

			else
			{
				cout << "-1";
				return 0;
			}
		}
		else
		{
			int sum = h1;
			for (int i = 3; i <= 10; i++)
			{
				sum += a;
			}
			if (sum >= h2)
			{
				cout << "0";
				return 0;
			}
			else
			{

				cout << "-1"
					return 0;
			}

		}
	}
	return 0;
}


#include <bits/stdc++.h>

#define fi(a,b,c) for(int a=b; a<=c; a++)
#define fd(a,b,c) for(int a=b; a>=c; a--)

using namespace std;

long long h1, h2, a, b;

int main()
{
    cin >> h1 >> h2 ;
    cin >> a >> b;
    if (b >= a){
        if (a * 8 + h1 >= h2) cout <<0<<endl; else cout <<-1<<endl;
        return 0;
    }
    int cnt = -1;
    while (1)
    {
        cnt++;
        h1+= a * 8;
        if (h1 >= h2) break;
        h1-= b * 12;
        h1 = max(h1, 0);
        h1+= a * 4;
        if (h1 >= h2) break;
    }
    cout <<cnt<<endl;

}


#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#define ll long long
using namespace std;

void solve() {
    int n;
    cin >> n;

    ll l = 1, r=1<<30;
    set <int> s;
    int ret = 0;
    ll b = 0;
    int sign = 1;

    for (int i = 0; i < n; i++) {
        i x;
        cin>>x;

        if (~x&1) {
            int mid = x / 2;
            if (s.find((mid - b)*sign) != s.end() || (l <= mid && mid <= r)) {
                ret += 2;
                s.clear();
                l = r = mid;
                b=0, sign=1;
            } else {
                ret += 1;
                swap(l, r);
                l = max(x-l, 1);
                r = min(x-r, x-1);
                while (!s.empty()) {
                    auto it = s.begin();
                    ll u = (*it) * sign + b;
                    if (u < 1 || u > x - 1) {
                        s.erase(it);
                    } else {
                        break;
                    }
                }
                while (!s.empty()) {
                    auto it = s.end();
                    it--;
                    ll u = (*it) * sign + b;
                    if (u < 1 || u > x - 1) {
                        s.erase(it);
                    } else {
                        break;
                    }
                }

                if (l <= r) {
                    s.insert((mid - b)*sign);
                } else {
                    l = r = mid;
                }

                sign *= -1;
                b = x - b;
            }
        } else {
            swap(l, r);
            l = max(x-l, 1);
            r = min(x-r, x-1);
            while (!s.empty()) {
                auto it = s.begin();
                ll u = (*it) * sign + b;
                if (u < 1 || u > x - 1) {
                    s.erase(it);
                } else {
                    break;
                }
            }
            while (!s.empty()) {
                auto it = s.end();
                it--;
                ll u = (*it) * sign + b;
                if (u < 1 || u > x - 1) {
                    s.erase(it);
                } else {
                    break;
                }
            }
            sign *= -1;
            b = x - b;

            if (l <= r || !s.empty()) {
                if (l > r) {
                    auto it = s.begin();
                    l = r = (*it) * sign + b;
                    s.erase(it);
                }
                ret++;
            } else {
                l = 1;
                r = x-1;
                sign=1;
                b=0;
            }
        }
        if (l > r) {
            abort();
        }
    }

    cout << n * 2 + 1 - ret << endl;
}

int main() {
    int T = 1;
    //freopen("a.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

bool operator!=(const std::pair<int, int> &a, int b) {
	return a.first != b;
}

template<class T>
auto set_back(std::set<T> &s) { auto it = s.end(); return --it; }

using ll = long long;

#define D(...)
int main() {
	//freopen("/dev/null", "w", stderr);
	std::cin.tie(nullptr);
	std::cin.sync_with_stdio(false);
	std::cout.sync_with_stdio(false);
	int t;
	std::cin >> t;
	while (t--) {
		D(
		for (int i = 0; i != 50; i++) {
			std::cerr << "=";
		}
		std::cerr << "\n";)
		int n;
		std::cin >> n;
		std::vector<ll> arr(n);
		for (int i = 0; i != n; i++) {
			std::cin >> arr[i];
		}
		ll last_nums_offset = 0;
		ll last_nums_sign = 1;
		std::set<ll> last_nums;
		std::pair<ll, ll> last_range(-1, -1);
		int ans = 0;
		for (int i = 0; i != n; i++) {
			std::pair<ll, ll> curr_range(-1, -1);
			bool found = false;
			if (!found) {
				if (arr[i] % 2 == 0) {
					if ((last_range != -1 && (last_range.first <= (arr[i] / 2)) && ((arr[i] / 2) <= last_range.second))
						|| (last_nums.find((arr[i] / 2) * last_nums_sign - last_nums_offset) != last_nums.end())) {
						found = 1;
						last_nums.clear();
						last_nums.insert((arr[i] / 2) * last_nums_sign - last_nums_offset);
					}
				}
				if (found) ans += 0;
				else {
					// WEIRD START
					D(
					std::cerr << arr[i] << "\n";
					std::cerr << "sign/offset = " << last_nums_sign << " " << last_nums_offset << "\n";
					for (auto it : last_nums) {
						std::cerr << it << " ";
					}
					std::cerr << "\n";)
					//std::pair<ll, ll> valid_range = std::make_pair((1 + last_nums_offset) * last_nums_sign, ((arr[i] - 1) + last_nums_offset) * last_nums_sign);
					std::pair<ll, ll> valid_range(1 * last_nums_sign - last_nums_offset, (arr[i] - 1) * last_nums_sign - last_nums_offset);
					D(std::cerr << "range = " << valid_range.first << " " << valid_range.second << "\n";)
					int valid_ans_cnt = 0;
					for (auto it : last_nums) {
						ll tmp = (it + last_nums_offset) * last_nums_sign;
						if (0 < tmp && tmp < arr[i]) valid_ans_cnt++;
						asssert(0 < tmp);
					}
					if (last_nums_sign == -1) std::swap(valid_range.first, valid_range.second);
					assert(valid_range.first <= valid_range.second);
					while (last_nums.size() && *set_back(last_nums) > valid_range.second) last_nums.erase(set_back(last_nums));
					while (last_nums.size() && *last_nums.begin() < valid_range.first) last_nums.erase(last_nums.begin());
					assert(valid_ans_cnt == last_nums.size());
					D(
					std::cerr << "sign/offset = " << last_nums_sign << " " << last_nums_offset << "\n";
					for (auto it : last_nums) {
						std::cerr << it << " ";
					}
					std::cerr << "\n";
					std::cerr << "\n";)
					for (auto it : last_nums) {
						ll tmp = (it + last_nums_offset) * last_nums_sign;
						assert(0 < tmp && tmp < arr[i]);
					}
					last_nums_offset -= arr[i] * last_nums_sign;
					last_nums_sign *= -1;
					if (last_nums.size()) found = 1;
					// WEIRD END
					if (arr[i] % 2 == 0) {
						found = 1;
						last_nums.insert((arr[i] / 2) * last_nums_sign - last_nums_offset);
					}
					if (arr[i] % 2 != 0) {
						if (last_range != -1 && last_range.first < arr[i]) {
							found = 1;
							curr_range = std::make_pair(std::max(arr[i] - last_range.second, 1ll), arr[i] - last_range.first);
						}
					}
					if (found) ans += 1;
					else {
						found = 1;
						assert(last_nums.empty());
						curr_range = std::make_pair(1, arr[i]-1);
						if (found) ans += 2;
						else assert(false);
					}
				}
			}
			last_range = curr_range;
			D(std::cerr << "last_nums = ";
			for (auto it : last_nums) std::cerr << it << " ";
			std::cerr << "\n";
			std::cerr << "last range = " << last_range.first << " " << last_range.second << "\n";
			std::cerr << "ans = " << ans << "\n";)
			//std::cerr << curr_num << " " << curr_range.first << " " << curr_range.second << "\n";
		}
		std::cout << ans << "\n";
		//std::cerr << "\n";
	}
}


#include <bits/stdc++.h>

using namespace std;

template <typename T>
void debug_out(T t) {
  cerr << t;
}

template <typename A, typename B>
void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}

template <typename T>
void debug_out(vector<T> t) {
  int sz = t.size();
  for(int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if(i != sz - 1) cerr << ", ";
  }
}

template <typename T>
void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for(int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if(i != sz - 1) cerr << endl;
  }
}

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}

template <typename T>
void debug_out(set<T> a) {
  vector<T> _a;
  for(T t : a) _a.push_back(t);
  debug_out(_a);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__), cerr << endl;

class segtree {
  public:
  struct node {

    int p = 0;

    void apply(int l, int r, int v) {
      p = v;
    }
  };

  node unite(const node &a, const node &b) const {
    node res;
    res.p = min(a.p, b.p);
    return res;
  }

  inline void push(int x, int l, int r) {

  }

  inline void pull(int x, int z) {
    tree[x] = unite(tree[x + 1], tree[z]);
  }

  int n;
  vector<node> tree;

  void build(int x, int l, int r) {
    if (l == r) {
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    build(x + 1, l, y);
    build(z, y + 1, r);
    pull(x, z);
  }

  template <typename M, typename... T>
  void build(int x, int l, int r, const vector<M> &v, const T&... t) {
    if (l == r) {
      tree[x].apply(l, r, v[l], t...);
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    build(x + 1, l, y, v, t...);
    build(z, y + 1, r, v, t...);
    pull(x, z);
  }

  template <typename M, typename... T>
  segtree(const vector<M> &v, const T&... t) {
    n = v.size();
    assert(n > 0);
    tree.resize(2 * n - 1);
    build(0, 0, n - 1, v, t...);
  }

  segtree(int _n) : n(_n) {
    assert(n > 0);
    tree.resize(2 * n - 1);
    build(0, 0, n - 1);
  }

  segtree(){};

  node get(int x, int l, int r, int ll, int rr) {
    if (ll <= l && r <= rr) {
      return tree[x];
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    push(x, l, r);
    node res{};
    if (rr <= y) {
      res = get(x + 1, l, y, ll, rr);
    } else {
      if (ll > y) {
        res = get(z, y + 1, r, ll, rr);
      } else {
        res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
      }
    }
    pull(x, z);
    return res;
  }

  node get(int ll, int rr) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return get(0, 0, n - 1, ll, rr);
  }

  node get(int p) {
    assert(0 <= p && p <= n - 1);
    return get(0, 0, n - 1, p, p);
  }

  template <typename... M>
  void modify(int x, int l, int r, int ll, int rr, const M&... v) {
    if (ll <= l && r <= rr) {
      tree[x].apply(l, r, v...);
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    push(x, l, r);
    if (ll <= y) {
      modify(x + 1, l, y, ll, rr, v...);
    }
    if (rr > y) {
      modify(z, y + 1, r, ll, rr, v...);
    }
    pull(x, z);
  }

  template <typename... M>
  void modify(int ll, int rr, const M&... v) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    modify(0, 0, n - 1, ll, rr, v...);
  }

  // find_first and find_last call all FALSE elements
  // to the left (right) of the sought position exactly once

  int find_first_knowingly(int x, int l, int r, const function<bool(const node&)> &f) {
    if (l == r) {
      return l;
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res;
    if (f(tree[x + 1])) {
      res = find_first_knowingly(x + 1, l, y, f);
    } else {
      res = find_first_knowingly(z, y + 1, r, f);
    }
    pull(x, z);
    return res;
  }

  int find_first(int x, int l, int r, int ll, int rr, const function<bool(const node&)> &f) {
    if (ll <= l && r <= rr) {
      if (!f(tree[x])) {
        return -1;
      }
      return find_first_knowingly(x, l, r, f);
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res = -1;
    if (ll <= y) {
      res = find_first(x + 1, l, y, ll, rr, f);
    }
    if (rr > y && res == -1) {
      res = find_first(z, y + 1, r, ll, rr, f);
    }
    pull(x, z);
    return res;
  }

  int find_first(int ll, int rr, const function<bool(const node&)> &f) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return find_first(0, 0, n - 1, ll, rr, f);
  }

  int find_last_knowingly(int x, int l, int r, const function<bool(const node&)> &f) {
    if (l == r) {
      return l;
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res;
    if (f(tree[z])) {
      res = find_last_knowingly(z, y + 1, r, f);
    } else {
      res = find_last_knowingly(x + 1, l, y, f);
    }
    pull(x, z);
    return res;
  }

  int find_last(int x, int l, int r, int ll, int rr, const function<bool(const node&)> &f) {
    if (ll <= l && r <= rr) {
      if (!f(tree[x])) {
        return -1;
      }
      return find_last_knowingly(x, l, r, f);
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res = -1;
    if (rr > y) {
      res = find_last(z, y + 1, r, ll, rr, f);
    }
    if (ll <= y && res == -1) {
      res = find_last(x + 1, l, y, ll, rr, f);
    }
    pull(x, z);
    return res;
  }

  int find_last(int ll, int rr, const function<bool(const node&)> &f) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return find_last(0, 0, n - 1, ll, rr, f);
  }

};

int tt;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> tt;
  while(tt--) {
    cin >> n;
    vector<long long> a(n);
    for(int i = 0; i < n; i++) {
      cin >> a[i];
    }

    vector<int> sig(n);
    vector<long long> coef(n);
    int sg = 1;
    long long co = 0;
    for(int i = 0; i < n; i++) {
      sg *= -1;
      co = a[i] - co;
      sig[i] = sg;
      coef[i] = co;
    }

    const long long inf = LLONG_MAX / 2;
    vector<pair<long long, long long>> boundary(n, make_pair(-inf, inf));
    for(int i = 0; i < n; i++) {
      if(sig[i] == -1) {
        boundary[i].second = min(boundary[i].second, coef[i] - 1);
      } else {
        boundary[i].first = max(boundary[i].first, 1 - coef[i]);
      }
    }

    const int N = 22;
    vector<vector<pair<long long, long long>>> fow(N, vector<pair<long long, long long>>(n, make_pair(inf, -inf)));
    vector<vector<pair<long long, long long>>> bac(N, vector<pair<long long, long long>>(n, make_pair(inf, -inf)));
    fow[0] = bac[0] = boundary;
    for(int i = 1; i < N; i++) {
      for(int j = 0; j < n; j++) {
        long long l, r, ll, rr;
        int pr;
        if(j + 1 >= 1 << i) {
          pr = j - (1 << (i - 1));
          l = bac[i - 1][j].first, r = bac[i - 1][j].second;
          ll = bac[i - 1][pr].first, rr = bac[i - 1][pr].second;
          bac[i][j] = make_pair(max(l, ll), min(r, rr));
        }
        if(n - j >= 1 << i) {
          int pr = j + (1 << (i - 1));
          l = fow[i - 1][j].first, r = fow[i - 1][j].second;
          ll = fow[i - 1][pr].first, rr = fow[i - 1][pr].second;
          fow[i][j] = make_pair(max(l, ll), min(r, rr));
        }
      }
    }

    vector<int> var(n);
    for(int i = 0; i < n; i++) {
      int cur = i;
      long long l = -inf;
      long long r = inf;
      for(int j = N - 1; j >= 0; j--) {
        if(cur == -1) break;
        long long ll = max(l, bac[j][cur].first);
        long long rr = min(r, bac[j][cur].second);
        if(ll <= rr) {
          cur -= 1 << j;
          l = ll;
          r = rr;
        }
      }
      assert(cur >= -1);
      if(cur > -1) var[i] = cur + 2;
      else var[i] = cur + 1 + (r < 1);
    }

    map<long long, vector<int>> make_eq;
    for(int i = 0; i < n; i++) {
      if(a[i] & 1) continue;
      long long b = (-coef[i] + a[i] / 2) / sig[i];
      make_eq[b].push_back(i);
    }

    vector<int> reac(n, n);
    vector<int> rev_reac(n, -1);
    for(int i = 0; i < n; i++) {
      if(a[i] & 1) continue;

      int cur = i;
      long long sp = (a[i] / 2 - coef[i]) / sig[i];
      long long l = sp;
      long long r = sp;
      for(int j = N - 1; j >= 0; j--) {
        if(cur == -1) break;
        long long ll = max(l, bac[j][cur].first);
        long long rr = min(r, bac[j][cur].second);
        if(ll <= rr) {
          cur -= 1 << j;
          l = ll;
          r = rr;
        }
      }
      if(cur > -1) reac[i] = cur + 2;
      else reac[i] = cur + 1 + (r < 1);

      cur = i;
      l = sp;
      r = sp;
      for(int j = N - 1; j >= 0; j--) {
        if(cur == n) break;
        long long ll = max(l, fow[j][cur].first);
        long long rr = min(r, fow[j][cur].second);
        if(ll <= rr) {
          cur += 1 << j;
          l = ll;
          r = rr;
        }
      }
      rev_reac[i] = cur - 1;
    }
    swap(reac, rev_reac);

    vector<int> near(n, -1);
    for(auto foo : make_eq) {
      vector<int> pos = foo.second;
      for(int i = 1; i < (int) pos.size(); i++) {
        int c = pos[i];
        int d = pos[i - 1];
        if(rev_reac[c] <= d) near[c] = d;
      }
    }
    //debug(var) debug(reac) debug(rev_reac) debug(near)

    vector<int> dp(n + 1);
    vector<int> sub_dp(n + 1);
    set<pair<int, int>> cur;
    multiset<int> v;
    multiset<int> c;
    segtree st(n + 1);

    if(a[0] % 2 == 0) {
      cur.insert(make_pair(reac[0], 0));
      v.insert(0);
      c.insert(0);
    }

    for(int i = 1; i <= n; i++) {
      dp[i] = st.get(var[i - 1], i - 1).p + i + 1;
      while(cur.size() && cur.begin()->first < i - 1) {
        int id = cur.begin()->second;
        v.erase(v.find(sub_dp[id] - id));
        c.erase(c.find(dp[id] - id));
        cur.erase(cur.begin());
      }
      if(cur.size()) {
        dp[i] = min(dp[i], *v.begin() + i - 1);
        dp[i] = min(dp[i], *c.begin() + i);
      }
      if(var[i - 1] == 0) {
        dp[i] = min(dp[i], i);
      }
      st.modify(i, i, dp[i] - i);

      if(i == n) break;
      if(a[i] & 1) continue;
      sub_dp[i] = INT_MAX / 2;
      if(rev_reac[i] < i) sub_dp[i] = st.get(rev_reac[i], i - 1).p + i + 1;
      if(~near[i]) {
        int d = near[i];
        sub_dp[i] = min(sub_dp[i], sub_dp[d] + i - d - 1);
        sub_dp[i] = min(sub_dp[i], dp[d] + i - d);
      }
      if(rev_reac[i] == 0) sub_dp[i] = min(sub_dp[i], i);
      cur.insert(make_pair(reac[i], i));
      v.insert(sub_dp[i] - i);
      c.insert(dp[i] - i);
    }
    cout << dp[n] + 1 << "\n";
  }
  return 0;
}