#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<memory>
#include<string>
#include<vector>
#include<cctype>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

typedef unsigned long long      ui64;
typedef long long               i64;
typedef vector<int>             vi;
typedef vector<string>          vs;
typedef pair<int,int>           pii;
typedef pair<double,double>     point;

#define pb                      push_back
#define mp                      make_pair
#define X                       first
#define Y                       second
#define all(a)                  (a).begin(), (a).end()
#define INF                     (2000000000)

i64 make_rew(int a){
    i64 t = a;
    i64 res = 0;
    i64 pw = 0;
    while(t!=0){
        res = res + (9 - t%10)*((i64)pow(10.0,1.0*pw));
        pw++;
        t/=10;
    }
    if(!a)
        return 9LL;
    return res;
}

int main()
{
    int L,R; scanf("%d%d",&L,&R);
/*  int t = R,dig=0;
    while(t!=0){
        t/=10;
        dig++;
    }

    int res = 0;
    for(int i=L;i<=R;i++){
        int t = make_rew(i);
        cout << i << " " << t << " = " << t*i << endl;
        if( t*i > res*make_rew(res) ){
            res = i;
        }
    }*/

    int a = 500000000;
    int dist=abs(R-a);
    int guess=R;
    for(int i=0;a>0;i++){
        if(a>=L && a<=R){
            if(R*make_rew(R)>a*make_rew(a))
                a = R;
            cout << a*make_rew(a) << endl;
            return 0;
        }
        else{
            if(min(abs(L-a),abs(R-a)) < dist ){
                dist = min(abs(L-a),abs(R-a));
                if(abs(L-a) < abs(R-a))
                    if(guess*make_rew(guess)<L*make_rew(L))
                        guess = L;
                else
                    if(guess*make_rew(guess)<R*make_rew(R))
                        guess = R;
            }
        }
        a = a/10;
    }

    cout << guess*make_rew(guess) << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
if(n%2==0)cout<<"Mahmoud";
else cout<<"Ehab";}


#include<stdio.h>
#include<stack>
using namespace std;
int main(){
	int n,m;
	int l[105];
	int a[105];
	int vis[105];
	int vis1[105];
	while(scanf("%d%d",&n,&m)!=EOF){
		for(int i=1;i<=n;i++) vis[i]=0,vis1[i]=0;
		for(int i=0;i<m;i++) scanf("%d",&l[i]);
		int success=1;
		stack<int>temp;
		while(!temp.empty()) temp.pop();
		for(int i=0;i<m-1;i++){
			if(vis1[l[i]]==0){
				if(l[i+1]<=l[i])
					a[l[i]]=(l[i+1]+n-l[i]);
				else a[l[i]]=(l[i+1]-l[i]);
				vis[a[l[i]]]++;
				vis1[l[i]]++;
				if(vis[a[l[i]]]==2) success=0;
			}
			else{
				if(l[i+1]<=l[i]&&a[l[i]]!=(l[i+1]+n-l[i]))
					success=0;
				else if(l[i+1]>l[i]&&a[l[i]]!=(l[i+1]-l[i])) success=0;
			}
		}
		if(success==0) printf("-1");
		else {
			for(int i=1;i<=n;i++){
				if(vis[i]==0) temp.push(i);
			}
			for(int i=1;i<=n;i++){
				if(vis1[i]==0) a[i]=temp.top(),temp.pop();
				printf("%d",a[i]);
				if(i==n) printf("\n");
				else printf(" ");
			}
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
#define N 1100
#define seed 11333333
#define seed2 5462617
#define seed1 23333333
#define ull unsigned long long
int n,T,label,pos[2][N];
struct node
{
	ull v;int x,y;
	node(){}
	node(ull v,int x,int y):v(v),x(x),y(y){}
	friend bool operator < (const node &r1,const node &r2)
	{return r1.v<r2.v;};
};
vector<node>a[2];
struct tree
{
	int id,m,f1,f2,sum;
	int used[N],f[N],size[N],vis[N];
	vector<int>  v[N],sub[N];
	vector<ull>  v1[N],v2;
	vector<node> v3;
	ull val[N];
	void init()
	{
		a[id].clear();
		memset(used,0,sizeof(used));
		memset(vis,0,sizeof(vis));
		v2.clear();
		for(int i=1;i<=n;i++)
			v[i].clear(),sub[i].clear();
	}
	void getsub(int x,int y,int root)
	{
		sub[root].push_back(x);
		for(int i=0,t;i<v[x].size();i++)
			if((t=v[x][i])!=y)
				getsub(t,x,root);
	}
	void dfs1(int x,int y)
	{
		size[x]=1;used[x]=1;
		for(int i=0,t;i<v[x].size();i++)
			if((t=v[x][i])!=y&&!vis[t])
				dfs1(t,x),size[x]+=size[t];
	}
	void dfs2(int x,int y)
	{
		f[x]=sum-size[x];
		for(int i=0,t;i<v[x].size();i++)
			if((t=v[x][i])!=y&&!vis[t])
				dfs2(t,x),f[x]=max(f[x],size[t]);
		if(f[x]<f[f1])f1=x;
		else if(f[x]<f[f2])f2=x;
	}
	ull dfs3(int x,int y)
	{
		v1[x].clear();
		for(int i=0,t;i<v[x].size();i++)
			if((t=v[x][i])!=y&&!vis[t])
				v1[x].push_back(dfs3(t,x));
		sort(v1[x].begin(),v1[x].end());
		ull ret=1;
		for(int i=0;i<v1[x].size();i++)
			ret=ret*seed+v1[x][i];
		return ret;
	}
	ull cal(int x)
	{
		f1=f2=0;f[0]=1<<30;
		dfs1(x,0);
		sum=size[x];dfs2(x,0);
		if(f[f1]!=f[f2])
			return dfs3(f1,0);
		ull t1=dfs3(f1,f2);
		ull t2=dfs3(f2,f1);
		if(t1>t2)swap(t1,t2);
		return t1*seed+t2;
	}
	void solve(int x)
	{
		id=x;init();
		scanf("%d",&m);
		for(int i=1,x,y;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			v[x].push_back(y);
			v[y].push_back(x);
		}
		for(int i=1;i<=n;i++)
			if(!used[i])
			{
				getsub(i,0,i);
				val[i]=cal(i);
				v2.push_back(val[i]);
			}
		sort(v2.begin(),v2.end());
		for(int i=1;i<=n;i++)
			if(sub[i].size())
			{
				ull t1=0,t2=0,t3=0;
				for(int j=0,flag=0;j<v2.size();j++)
				{
					if(v2[j]==val[i]&&!flag)
						{flag=1;continue;}
					t1=t1*seed1+v2[j];
				}
				for(int j=0,t;j<sub[i].size();j++)
				{
					vis[t=sub[i][j]]=1;
					v3.clear();v3.push_back(node(1,0,0));
					for(int k=0;k<v[t].size();k++)
						v3.push_back(node(cal(v[t][k]),v[t][k],0));
					sort(v3.begin(),v3.end());
					for(int k=1;k<v3.size();k++)
					{
						t2=0;
						for(int w=0;w<v3.size();w++)
							if(w!=k)t2=t2*seed1+v3[w].v;
						if(id==0)t3=v3[k].v*seed2*seed2+t2*seed2+t1;
						else t3=v3[k].v*seed2*seed2+t1*seed2+t2;
						a[id].push_back(node(t3,v3[k].x,t));
					}
					t2=0;
					for(int w=0;w<v3.size();w++)
						t2=t2*seed1+v3[w].v;
					if(id==0)t3=t2*seed2+t1;
					else t3=t1*seed2+t2;
					a[id].push_back(node(t3,0,t));
					vis[t]=0;
				}
			}
		sort(a[id].begin(),a[id].end());
	}
	void print()
	{
		puts("YES");
		for(int i=1;i<=n;i++)
			for(int j=0,t;j<v[i].size();j++)
				if((t=v[i][j])<i)
					printf("%d %d\n",i,t);
	}
}tr[2],ans;
int relabel(int x,int y,int type)
{
	int ret=pos[type][x]=++label;
	for(int i=0,t;i<tr[type].v[x].size();i++)
		if((t=tr[type].v[x][i])!=y)
		{
			int t1=relabel(t,x,type);
			ans.v[ret].push_back(t1);
			ans.v[t1].push_back(ret);
		}
	return ret;
}
void solve()
{
	scanf("%d%*d",&n);
	for(int i=0;i<=1;i++)
		tr[i].solve(i);

	for(int i=0,now=0;i<a[0].size();i++)
	{
		while(now<a[1].size()&&a[1][now].v<a[0][i].v)now++;
		if(now<a[1].size()&&a[1][now].v==a[0][i].v)
		{
			relabel(a[0][i].y,0,0);
			int t=label;
			relabel(a[1][now].y,a[1][now].x,1);
			ans.vis[pos[0][a[0][i].y]]=1;
			int t1=pos[1][a[1][now].y];
			ull v1=tr[1].cal(a[1][now].y);
			for(int j=1;j<=t;j++)
			{
				ans.v[j].push_back(t1);
				ans.v[t1].push_back(j);
				if(ans.cal(t1)==v1)
				{
					ans.print();
					return;
				}
				ans.v[j].pop_back();
				ans.v[t1].pop_back();
			}
		}
	}
	puts("NO");
}
int main()
{
	//freopen("tt.in","r",stdin);
	scanf("%d",&T);
	while(T--)
	{
		label=0;
		memset(pos,0,sizeof(pos));
		a[0].clear();a[1].clear();
		ans.init();
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int main()
{
    ll n,k,ans1,ans2,ans3;
    cin>>n>>k;
    ll low=1,high=1e10;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(2*n<=mid*k)
        {
            ans1=mid;
            high=mid-1;
        }
        else
            low=mid+1;
    }
    low=1,high=1e10;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(5*n<=mid*k)
        {
            ans2=mid;
            high=mid-1;
        }
        else
            low=mid+1;
    }
    low=1,high=1e10;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        if(8*n<=mid*k)
        {
            ans3=mid;
            high=mid-1;
        }
        else
            low=mid+1;
    }
    cout<<ans1+ans2+ans3;
}


#include<bits/stdc++.h>
using namespace std;
int y,m,d;
inline long long get(int y,int m,int d)
{
	if (m<3) {y-=1;m+=12;}
	return 365*y+y/4-y/100+y/400+(153*m-457)/5+d-306;
}
template<class T>inline void read(T &res)
{
	static char ch;T flag=1;
	while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48;
	while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag;
}
int main()
{
	read(y);read(m);read(d);long long a=get(y,m,d);
	read(y);read(m);read(d);long long b=get(y,m,d);
	printf("%lld\n",abs(b-a));
	return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define lc k << 1
#define rc k << 1 | 1
using namespace std;

int n, k;

struct segment{
	int l, r;
}a[105];

struct node{
	int l, r, dat, tag;
};
int tmp[200005], tmp2[200005];
struct seg{
	node d[800005];

	void pushup(int k){
		d[k].dat = min(d[lc].dat, d[rc].dat);
	}
	void build(int k, int l, int r){
		d[k].l = l; d[k].r = r; d[k].tag = inf;
		if(l == r) {
			d[k].dat = inf; return;
		}
		int mid = (l + r) >> 1;
		build(lc, l, mid);
		build(rc, mid + 1, r);
		pushup(k);
	}
	void minn(int k, int x){
		d[k].dat = min(d[k].dat, x);
		d[k].tag = min(d[k].tag, x);
	}

	void pushdown(int k){
		minn(lc, d[k].tag);
		minn(rc, d[k].tag);
		d[k].tag = inf;
	}
	void minn(int k, int l, int r, int x){
		if(l <= d[k].l && d[k].r <= r){
			minn(k, x);
			return;
		}
		pushdown(k);
		int mid = (d[k].l + d[k].r) >> 1;
		if(l <= mid) minn(lc, l, r, x);
		if(r > mid) minn(rc, l, r, x);
		pushup(k);
	}
	int query(int k, int pos){
		if(d[k].l == d[k].r) return d[k].dat;
		pushdown(k);
		int mid = (d[k].l + d[k].r) >> 1;
		if(pos <= mid) return query(lc, pos);
		else return query(rc, pos);
	}
}Seg, Seg2;
int f[2][200005][2];
int main(){
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= k; i ++){
		scanf("%d%d", &a[i].l, &a[i].r);
	}
	memset(f, 0x3f, sizeof(f));
	f[0][0][0] = 0;
	memset(tmp, 0x3f, sizeof(tmp));
	memset(tmp2, 0x3f, sizeof(tmp2));

	for(int I = 1; I <= k; I ++){
		for(int i = 0; i <= 2 * n; i ++){
			tmp[i + 1] = f[(I & 1) ^ 1][i][0];
			tmp2[i + 1] = f[(I & 1) ^ 1][i][1];
		}
		Seg.build(1, 1, 2 * n + 1);
		Seg2.build(1, 1, 2 * n + 1);
		for(int i = n + 1; i >= 1; i --){
			if(tmp[i] <= 200){
				Seg.minn(1, i, i + a[I].r - a[I].l, tmp[i] + 2);
				Seg2.minn(1, i, i + a[I].r - a[I].l, tmp[i] + 1);
				Seg.minn(1, i, i, tmp[i]);
			}
			if(tmp2[i] <= 200){
				Seg.minn(1, i + a[I].l - a[I - 1].r, i + a[I].r - a[I - 1].r, tmp2[i] + 1);
				Seg2.minn(1, i  + a[I].l - a[I - 1].r, i + a[I].r - a[I - 1].r, tmp2[i] + 2);
				Seg2.minn(1, i + a[I].r - a[I - 1].r, i + a[I].r - a[I - 1].r, tmp2[i]);
			}
		}
		for(int i = 0; i <= n; i ++){
			f[I & 1][i][0] = Seg.query(1, i + 1);
			f[I & 1][i][1] = Seg2.query(1, i + 1);
		//	printf("%d ", f[I & 1][i][1]);
		}
		//printf("\n");
	}
	int ans = f[k & 1][n][0];
	if(a[k].r >= n) ans = min(ans, f[k & 1][a[k].r - n][1]);
	if(ans <= 200){
		printf("Full\n%d\n", ans);
	}else{
		printf("Hungry\n");
	}
	return 0;
}


// In the name of God
#include <bits/stdc++.h>

using namespace std;
#define Size(x) ((int)(x).size())
#define pb push_back
typedef long long ll;
typedef pair<int,int>pii;
const int INF = 1e9 + 10 , MN = 1e5 + 5;

int n , m , es[MN] , ee[MN] , st[MN] , ft[MN] , h[MN] , Time;
vector<int>adj[MN];
bool mk[MN];

void dfs(int s){
	mk[s] = true;
	st[s] = Time++;
	for(auto v:adj[s])
		if(!mk[v]) h[v] = h[s]+1 , dfs(v);
	ft[s] = Time;
}

bool check(int x){
	for(int i=0;i<m;++i)
		if(mk[es[i]] && mk[ee[i]] && (h[es[i]]+1)%x != h[ee[i]]%x) return false;
	return true;
}

int main()
{
	ios_base :: sync_with_stdio(false) ,cin.tie(0) , cout.tie(0);
	cin >> n >> m;
	for(int i=0;i<m;++i){
		cin >> es[i] >> ee[i];
		adj[--es[i]].pb(--ee[i]);
	}
	dfs(0);
	vector<int>D;
	for(int i=0;i<m;++i)
		if(mk[es[i]] && mk[ee[i]] && st[ee[i]] <= st[es[i]] && st[es[i]] < ft[ee[i]]){
			int c = h[es[i]] - h[ee[i]] + 1;
			for(int d=1;d<=c;++d) if(c % d == 0) D.pb(d);
			break;
		}
	D.pb(n);
	reverse(D.begin() , D.end());
	for(auto x:D) if(check(x)){
		cout << x << '\n';
		vector<int>res;
		for(int i=0;i<n;++i) if(h[i]%x == 0 && mk[i]) res.pb(i);
		cout << Size(res) << '\n';
		for(auto x:res) cout << x+1 << ' ';cout << '\n';
		return 0;
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <deque>
#include <map>
#include <string>
using namespace std;

int main() {
	string a, b, r;
	cin >> a >> b;

	for (int i = 0; i < a.size(); i++) {
		for (int j = 0; j < b.size(); j++) {
			string s = a.substr(0, i + 1) + b.substr(0, j + 1);
			if (r == "" || s < r)
				r = s;
		}
	}

	cout << r;
}


#include <bits/stdc++.h>

using namespace std;

#define add_const ADD_CONST

struct Segment {
  long long sum;
  long long min_odd;
  long long min_even;
  int len;
};

Segment unite(Segment a, Segment b) {
  Segment c;
  c.len = b.len + a.len;
  c.sum = b.sum + ((b.len & 1) ? (-a.sum) : a.sum);
  if (a.len & 1) {
    c.min_even = min(a.min_even, -a.sum + b.min_odd);
    c.min_odd = min(a.min_odd, a.sum + b.min_even);
  } else {
    c.min_even = min(a.min_even, a.sum + b.min_even);
    c.min_odd = min(a.min_odd, -a.sum + b.min_odd);
  }
  return c;
}

const int N = 1234567;

Segment a[N];
int add[N];

void build(int x, int l, int r) {
  a[x].len = r - l + 1;
  a[x].sum = a[x].min_odd = a[x].min_even = 0;
  if (l < r) {
    int y = (l + r) >> 1;
    build(x + x, l, y);
    build(x + x + 1, y + 1, r);
  }
}

void add_const(int x, int value) {
  if (a[x].len & 1) {
    a[x].sum += value;
  }
  a[x].min_odd += value;
  add[x] += value;
}

void push(int x) {
  if (add[x] != 0) {
    add_const(x + x, add[x]);
    add_const(x + x + 1, add[x]);
    add[x] = 0;
  }
}

void modify(int x, int l, int r, int ll, int rr, int v) {
  if (ll <= l && r <= rr) {
    add_const(x, v);
    return;
  }
  push(x);
  int y = (l + r) >> 1;
  if (ll <= y) {
    modify(x + x, l, y, ll, rr, v);
  }
  if (rr > y) {
    modify(x + x + 1, y + 1, r, ll, rr, v);
  }
  a[x] = unite(a[x + x], a[x + x + 1]);
}

Segment solve(int x, int l, int r, int ll, int rr) {
  if (ll <= l && r <= rr) {
    return a[x];
  }
  push(x);
  int y = (l + r) >> 1;
  Segment res;
  if (rr <= y) {
    res = solve(x + x, l, y, ll, rr);
  } else {
    if (ll > y) {
      res = solve(x + x + 1, y + 1, r, ll, rr);
    } else {
      res = unite(solve(x + x, l, y, ll, rr), solve(x + x + 1, y + 1, r, ll, rr));
    }
  }
  a[x] = unite(a[x + x], a[x + x + 1]);
  return res;
}

int main() {
  int n;
  scanf("%d", &n);
  build(1, 0, n - 1);
  for (int i = 0; i < n; i++) {
    int foo;
    scanf("%d", &foo);
    modify(1, 0, n - 1, i, i, foo);
  }
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    int type;
    scanf("%d", &type);
    if (type == 1) {
      int from, to, value;
      scanf("%d %d %d", &from, &to, &value);
      modify(1, 0, n - 1, from, to, value);
    } else {
      int from, to;
      scanf("%d %d", &from, &to);
      Segment res = solve(1, 0, n - 1, from, to);
      printf("%d\n", (res.sum == (res.len & 1) && res.min_odd >= 1 && res.min_even >= 0) ? 1 : 0);
    }
  }
  return 0;
}


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

const int N = 505;
vector<int> sets[N];
int mark[N];
char map[N][N];

bool bySize(int i, int j) {
    return sets[i].size() > sets[j].size();
}

int findGood(vector<vector<int> > &cur, vector<int> &row) {
    for (int i = 0; i < (int)cur.size(); ++i) {
        for (int j = 0; j < (int)cur[i].size(); ++j) {
            mark[cur[i][j]] = i;
        }
    }
    for (int i = 0; i < (int)row.size(); ++i) {
        int u = row[i];
        for (int j = 1; j < (int)sets[u].size(); ++j) {
            if (mark[sets[u][j]] != mark[sets[u][j - 1]]) {
                return u;
            }
        }
    }
    return -1;
}

bool solve(vector<int> &col, vector<int> &row, vector<int> &ret) {
    if (row.size() == 0) {
        ret = col;
        return true;
    }
    sort(row.begin(), row.end(), bySize);
    vector<vector<int> > cur;
    vector<int> one, zero, cut;
    int p;
    p = row.front();
    row.erase(row.begin());
    cut.push_back(p);
    for (int i = 0; i < col.size(); ++i) {
        int u = col[i];
        if (map[p][u] - '0') {
            one.push_back(u);
        } else {
            zero.push_back(u);
        }
    }
    if (!one.size() || !zero.size()) {
        return solve(col, row, ret);
    }
    cur.push_back(one);
    cur.push_back(zero);
    if (row.size() == 0) {
        ret.clear();
        for (int i = 0; i < (int)cur.size(); ++i) {
            for (int j = 0; j < (int)cur[i].size(); ++j) {
                ret.push_back(cur[i][j]);
            }
        }
        return true;
    }
    bool special = true;
    while ((p = findGood(cur, row)) != -1) {
        for (int i = 0; i < (int)row.size(); ++i) {
            if (row[i] == p) {
                row.erase(row.begin() + i);
                break;
            }
        }
        cut.push_back(p);
        int state = 0;
        bool fail = false;
        vector<vector<int> > rem;
        for (int i = 0; i < (int)cur.size(); ++i) {
            vector<int> tmp = cur[i], one, zero;
            for (int j = 0; j < (int)tmp.size(); ++j) {
                if (map[p][tmp[j]] - '0') {
                    one.push_back(tmp[j]);
                } else {
                    zero.push_back(tmp[j]);
                }
            }
            if (state == 0) {
                if (zero.size()) {
                    rem.push_back(zero);
                }
                if (one.size()) {
                    rem.push_back(one);
                    state = 1;
                }
            } else if (state == 1) {
                if (one.size()) {
                    rem.push_back(one);
                }
                if (zero.size()) {
                    rem.push_back(zero);
                    state = 2;
                }
            } else {
                if (zero.size()) {
                    rem.push_back(zero);
                }
                if (one.size()) {
                    if (i != (int)cur.size() - 1 || !special) {
                        return false;
                    } else {
                        fail = true;
                    }
                }
            }
        }
        if (fail) {
            fail = false;
            rem.clear();
            state = 0;
            for (int i = 0; i < (int)cur.size(); ++i) {
                vector<int> tmp = cur[i], one, zero;
                for (int j = 0; j < (int)tmp.size(); ++j) {
                    if (map[p][tmp[j]] - '0') {
                        one.push_back(tmp[j]);
                    } else {
                        zero.push_back(tmp[j]);
                    }
                }
                if (i == (int)cur.size() - 1) {
                    if (one.size()) {
                        rem.insert(rem.begin(), one);
                    }
                    if (zero.size()) {
                        rem.push_back(zero);
                    }
                    continue;
                }
                if (one.size()) {
                    rem.push_back(one);
                    if (state == 0) {
                        state = 1;
                    } else if (state == 2) {
                        return false;
                    }
                }
                if (zero.size()) {
                    rem.push_back(zero);
                    if (state == 0) {
                        return false;
                    } else if (state == 1) {
                        state = 2;
                    }
                }
            }
        }
        cur = rem;
        special = true;
        int col1 = cur.back().front(), col2 = cur[cur.size() - 1].back();
        for (int i = 0; i < cut.size() && special; ++i) {
            int u = cut[i];
            if (map[u][col1] == '1' && map[u][col2] == '1') {
                special = false;
            }
        }
    }
    vector<vector<int> > cur_row;
    vector<int> empty;
    for (int i = 0; i < (int)cur.size(); ++i) {
        cur_row.push_back(empty);
    }
    for (int i = 0; i < (int)row.size(); ++i) {
        int u = row[i];
        if (sets[u].size() == 0) {
            continue;
        }
        cur_row[mark[sets[u].front()]].push_back(u);
    }
    ret.clear();
    vector<int> tmp;
    for (int i = 0; i < (int)cur.size(); ++i) {
        if (solve(cur[i], cur_row[i], tmp)) {
            for (int j = 0; j < (int)tmp.size(); ++j) {
                ret.push_back(tmp[j]);
            }
            tmp.clear();
        } else {
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    vector<int> col, row, ret;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%s", map[i]);
        for (int j = 0; j < n; ++j) {
            if (map[i][j] - '0') {
                sets[i].push_back(j);
            }
        }
        col.push_back(i);
        row.push_back(i);
    }
    if (solve(col, row, ret)) {
        puts("YES");
        char ans[N];
        ans[n] = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                ans[j] = map[i][ret[j]];
            }
            printf("%s\n", ans);
        }
    } else {
        puts("NO");
    }
    return 0;
}


#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
long long ans,C[120][120],n,t,m,i,o,s,j;
int main(){
	scanf("%I64d%I64d",&n,&t);
	for(s=1,m=1;s<t;s<<=1,m++);
	if(s!=t)return puts("0"),0;
	if(m==1)ans--;
	n++;
	for(i=0;i<=60;++i)
		for(j=0;j<=i;++j)C[i][j]=!j?1:C[i-1][j-1]+C[i-1][j];
	for(i=60;i>=0;--i){
		if(m<0)break;
		if(o+(1ll<<i)<=n){
			ans+=C[i][m];
			m--;
			o+=1ll<<i;
		}
	}
	if(!m)ans++;
	printf("%I64d\n",ans);
}


#include<cstdio>//CF1029F
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>

using namespace std;

long long a,b,c,d;

bool check(int u,long long v){
	for(int i=1;i<=u;i++){
		if(a%i==0){
			long long rep=a/i;
			if(i<=u&&rep<=v){
				return true;
			}
		}
	}
	for(int i=1;i<=u;i++){
		if(b%i==0){
			long long rep=b/i;
			if(i<=u&&rep<=v){
				return true;
			}
		}
	}
	return false;
}

int main(){
	scanf("%lld%lld",&a,&b);
	c=a+b;
	for(int i=(int)sqrt(c);i;i--){
		if(c%i==0){
			d=c/i;
			if(check(i,d)){
				printf("%lld",(d+i)*2);
				return 0;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define OO ios_base::sync_with_stdio(0); cin.tie(NULL)

inline int parser(LL int item)
	{
		LL int sum=0;

		while ( item / 10 != 0)
		{

			sum += item % 10;
			item = item / 10;

		}

		sum += item ;

		return sum;
	}

	int main()
		{
			LL int i , j , counter , a,n;

			cin >> n;
			i=1;
			counter =0;

			while (true)
					{

						//cout << parser(28)<< endl;;

						if ( parser(i) == 10)
								{

									counter++;

								}

						if ( counter == n)
								{
									break;
								}

							else i++;

					}

					cout << i ;

		}


#include <bits/stdc++.h>
using namespace std;
const int N = 20 , MAX = 2e5+5  , OO = 1000000009;
string sBits = "0101";
bitset<N> x(sBits);
vector<long long> v;
void brute(long long num){
	v.push_back(num);
	if (num > 1e9)   return;
	brute(num * 10 + 4);
	brute(num * 10 + 7);
    }
int n;
int main(){
   scanf("%d" , &n);
    brute(0);
    sort(v.begin(), v.end());
	for(int it = 0; it < v.size(); ++it){
        if (v [it] == n)
        {
            printf("%d\n" , it) ;
            return 0 ;
        }
    }
}


#include<bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I,M,N) for(I=M;I<=N;I++)
#define fr_(I,M,N) for(I=M;I>=N;I--)
#define re return
#define sfn cin>>n
#define bal pf("bal\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define cont continue
#define memo(X,N) memset(X,N,sizeof(X))
#define all(X) X.begin(),X.end()

/*int pr[100000],prnum[1000001];

void sieve()
{
    int i,j;
    for(i=1;i<=1000000;i++)
        prnum[i]=i+1;
    for(i=1;i<=1000000;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=1000000;j+=prnum[i])
                    prnum[j]=-1;
    j=1;
    for(i=1;i<=1000000;i++)
        if(prnum[i]!=-1)
            pr[j++]=prnum[i];
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*func(i-1);
}*/

int main()
{
    ios_base::sync_with_stdio(0);
    int i,j,x,n,cnt=0,m,y,mx,mnn,mxx,mn;
    cin>>n;
    int in[n+1];
    fr(i,1,n)
        cin>>in[i];
    if(!n)
    {
        cout<<"YES\n113\n113\n339\n339";
    }
    else if(n==1)
    {
        cout<<"YES\n"<<in[1]<<endl<<in[1]*3<<endl<<in[1]*3;
    }
    else if(n==2)
    {
        sort(in+1,in+3);
        if(in[2]%2) x=1;
        else x=2;
        mx=max(in[1],in[2]);
        mn=min(in[1],in[2]);
        for(i=0;i<=1000000-x;i+=2)
        {
            y=(in[2]+x+i)/2;
            y+=in[1];
            cnt=in[1]+in[2]+x+i+y;
            if(cnt%4) cont;
            cnt/=4;
            mxx=max(mx,x+i);
            mxx=max(mxx,y);
            mnn=min(mn,x+i);
            mnn=min(mnn,y);
            if(cnt==mxx-mnn && cnt==(in[2]+x+i)/2)
            {
                cout<<"YES\n"<<x+i<<endl<<y;
                re 0;
            }
        }
        cout<<"NO";
    }
    else if(n==3)
    {
        sort(in+1,in+4);
        if((in[1]+in[2])%2==0)
        {
            x=in[3]-(in[1]+in[2])/2;
            cnt=in[1]+in[2]+in[3]+x;
            if(cnt%4==0 && cnt/4==in[3]-x)
            {
                cout<<"YES\n"<<x;
                re 0;
            }
        }
        if((in[3]+in[2])%2==0)
        {
            x=in[1]+(in[3]+in[2])/2;
            cnt=in[1]+in[2]+in[3]+x;
            if(cnt%4==0 && cnt/4==x-in[1])
            {
                cout<<"YES\n"<<x;
                re 0;
            }
        }
        {
            x=in[3]-in[1];
            x=2*x-in[2];
            cnt=in[1]+in[2]+in[3]+x;
            if(cnt%4==0 && cnt/4==in[3]-in[1])
            {
                cout<<"YES\n"<<x;
                re 0;
            }
        }
        cout<<"NO";
    }
    else
    {
        sort(in+1,in+5);
        if((in[3]+in[2])%2)
        {
            cout<<"NO";
            re 0;
        }
        cnt=in[1]+in[2]+in[3]+in[4];
        if(cnt%4==0 && cnt/4==in[4]-in[1] && cnt/4==(in[3]+in[2])/2)
            cout<<"YES";
        else
            cout<<"NO";
    }
}


#include <iostream>
#include <cmath>
#include <iomanip>
#include <math.h>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <stdio.h>
#define ll long long
#define l1 long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define sy system("pause");
using namespace std;

int main(){
#ifdef LOCAL
    freopen("INPUT.TXT","r", stdin);
    freopen("OUTPUT.TXT","w", stdout);
#endif
    int n,n1,k1,k2;
    cin>>n>>n1>>k1>>k2;

    if(n<=n1)
         cout<<"Second";
    else
        cout<<"First";

    #ifdef LOCAL
    cerr << clock()*1.0/CLOCKS_PER_SEC<<endl;
    #endif
   // sy
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=400100;
inline int read(){
	int x=0,c=getchar(),f=0;
	for(;c>'9'||c<'0';f=c=='-',c=getchar());
	for(;c>='0'&&c<='9';c=getchar())
	x=(x<<1)+(x<<3)+c-'0';return f?-x:x;
}
struct cqz{int r1,c1,r2,c2,i;}q[N];
inline bool operator<(cqz i,cqz j){return i.r2<j.r2;}
int lg[N],st[22][N];
int a[N],top,sta[N],m,n,i,c1,c2,r1,r2,ans[N],k;
inline int abs(int x){return x<0?-x:x;}
inline int min(int x,int y){return x<y?x:y;}
inline int getst(int l,int r){
	if(l>r)swap(l,r);int ln=r-l+1,k;
	k=lg[ln];r=r-(1<<k)+1;return min(st[k][l],st[k][r]);
}
inline int calc(int r1,int c1,int r2,int c2,int r){
	int res=0;c1=min(c1,getst(r1,r));
	if(c1<a[r])res++;
	res+=abs(r1-r)+abs(r2-r);
	res+=abs(c2-a[r]);return res;
}
inline int getpos(int l,int r,int k){
	l--;while(l<r-1)
	if(sta[l+r>>1]<=k)r=l+r>>1;
	else l=l+r>>1;return r;
}
inline void getans(cqz x,int l,int r){
	l--;r++;
	int res=2e9,lm,rm,len,ls,rs;
	while(l<r-10){
		len=(r-l+1);len/=3;
		lm=l+len;rm=r-len;
		ls=calc(x.r1,x.c1,x.r2,x.c2,sta[lm]);
		rs=calc(x.r1,x.c1,x.r2,x.c2,sta[rm]);
		if(ls>rs)l=lm;else r=rm;
	}for(l++;l<r;l++)
	res=min(res,calc(x.r1,x.c1,x.r2,x.c2,sta[l]));
	ans[x.i]=min(ans[x.i],res);
}
int main(){
//	freopen("notepad.in","r",stdin);
//	freopen("notepad.out","w",stdout);
	n=read();for(i=1;i<=n;i++)st[0][i]=a[i]=read();
	for(k=0;k<20;k++)for(i=1;i<=n;i++)
	st[k+1][i]=min(st[k][i],st[k][i+(1<<k)]);
	lg[0]=-1;
	for(i=1;i<=n;i++)lg[i]=lg[i/2]+1;
	m=read();for(i=1;i<=m;i++){
		r1=read();c1=read();
		r2=read();c2=read();
		ans[i]=abs(r1-r2)+1+c2;
		q[i]=(cqz){r1,c1,r2,c2,i};
	}sort(q+1,q+m+1);top=0;k=1;
	for(i=1;i<=n;i++){
		while(top&&a[sta[top]]>=a[i])
		top--;sta[++top]=i;
		for(;k<=m&&q[k].r2==i;k++)
		if(q[k].r1<=r2){
			int pos=lower_bound(sta,sta+top+1,q[k].r1)-sta;
			getans(q[k],pos,top);getans(q[k],1,pos-1);
			int res=abs(q[k].r2-q[k].r1);
			int mn=q[k].c1;
			mn=min(mn,getst(q[k].r1,q[k].r2));
			res+=abs(mn-q[k].c2);
			ans[q[k].i]=min(ans[q[k].i],res);
		}else getans(q[k],1,top);
	}top=0;k=m;
	for(i=n;i;i--){
		while(top&&a[sta[top]]>=a[i])
		top--;sta[++top]=i;
		for(;k>0&&q[k].r2==i;k--)
		if(q[k].r1>r2){
			int pos=getpos(1,top,q[k].r1);
			getans(q[k],pos,top);getans(q[k],1,pos-1);
			int res=abs(q[k].r2-q[k].r1);
			int mn=q[k].c1;
			mn=min(mn,getst(q[k].r1,q[k].r2));
			res+=abs(mn-q[k].c2);
			ans[q[k].i]=min(ans[q[k].i],res);
		}else getans(q[k],1,top);
	}for(i=1;i<=m;i++)printf("%d\n",ans[i]);
	return 0;
}


#include<iostream>

using namespace std;

int a1,a2,a3,b1,b2,b3,n;

int main(){

    cin>>a1>>a2>>a3>>b1>>b2>>b3>>n;

    int a = (a1+a2+a3 + 4) / 5;
    int b = (b1+b2+b3 + 9) / 10;

    cout<<(a+b<=n ? "YES" : "NO")<<endl;

    return 0;
}


#include <stdio.h>

const long long mod = 1000000007;

struct matrix{
    int N; long long A[82][82];
    matrix(int n){
        N = n;
        for (int i=0;i<n;i++) for (int j=0;j<n;j++) A[i][j] = 0;
    }

    matrix operator +(matrix t){
        matrix res(N);
        for (int i=0;i<N;i++) for (int j=0;j<N;j++) res.A[i][j] = (A[i][j] + t.A[i][j]) % mod;
        return res;
    }

    matrix operator *(matrix t){
        matrix res(N);
        for (int i=0;i<N;i++) for (int j=0;j<N;j++) for (int k=0;k<N;k++) res.A[i][j] = (res.A[i][j] + A[i][k] * t.A[k][j]) % mod;
        return res;
    }
};

long long bi[44][44],pw[44];

int main()
{
    long long N; int K;
    scanf ("%lld %d",&N,&K);
    for (int i=0;i<=K;i++){
        bi[i][0] = bi[i][i] = 1;
        for (int j=1;j<i;j++) bi[i][j] = (bi[i-1][j-1] + bi[i-1][j]) % mod;
        pw[i] = (1ll << i) % mod;
    }

    matrix A(2*(K+1));
    for (int i=0;i<=K;i++){
        A.A[i+K+1][i] = 1;
        for (int j=0;j<=i;j++){
            A.A[i][j] = bi[i][j];
            A.A[i][j+K+1] = bi[i][j] * pw[i-j] % mod;
        }
    }

    matrix R(2*(K+1)),P(2*(K+1));
    for (int i=0;i<P.N;i++) P.A[i][i] = 1;
    while (N){
        if (N & 1){
            R = R * A + P;
        }
        P = P * A + P;
        A = A * A;
        N >>= 1;
    }

    long long ans = 0;
    for (int i=0;i<=K+1;i++){
        ans += R.A[K][i];
    }
    printf ("%lld\n",ans%mod);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 100000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int , int>
#define pdd pair<ld,ld>
#define pll pair<ll,ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack <int>
#define stll stack <ll>
#define mseti multiset <ll>
#define msetd multiset <ll, greater <ll> >
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20)//use fixed before stp
#define endl '\n'
int main()
{
    FAST
    #ifndef ONLINE_JUDGE
        freopen("input.txt" , "rt" , stdin);
        freopen("output.txt" , "wt" , stdout);
    #endif
    ld a,b,m;
    cin>>a>>b>>m;
    ld x,y,z,t;
    cin>>x>>y>>z;
    t=m/y;
    t=abs(t);
    x=x*t;
    z=z*t;
    x+=(a/2);
    while((z<0)||(z>b))
    {
        if(z>b)
            z=(2*b)-z;
        else if(z<0)
            z=abs(z);
    }
    while((x<0)||(x>a))
    {
        if(x>a)
            x=(2*a)-x;
        else if(x<0)
            x=abs(x);
    }
    cout<<fixed<<stp<<x<<' '<<z;
}


#include <bits/stdc++.h>

using namespace std;

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

    //cout << ((int(sqrt(n*2)) * int(sqrt(n*2)+ 1)))/2 << endl;
    if (m - 2 * n <= 0) cout << 0 <<" ";
    else cout << m - 2 * n <<" ";
    if (n == 0) {cout << m; exit(0);}
    if (int(sqrt(n * 2)) * int(sqrt(n * 2) + 1)/2 >= n) cout << m - (int(sqrt(n*2) + 1));
    else{
        if (int(sqrt(n*2)) == sqrt(n*2)) cout << m - int(sqrt(n*2) + 1);
        else
        {
            if (m - int(sqrt(n*2) + 2) >= 0) cout << m - int(sqrt(n*2) + 2);
            else cout << 0;
        //cout << endl << "loai 2";
        }
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <cstring>
#include <cstdio>
using namespace std;

int alpha[26];
void filll(string &s)
{
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?')
            s[i]='A';
           // cout<<"y"<<endl;
    }

}
bool valid()
{
    for(int i=0;i<26;i++)
    {
        if(alpha[i]>1)
            return false;
    }
    return true;
}
int main()
{
   //freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);

 string s;
 cin>>s;
 vector<int> Exlac;
 if(s.size()<26)
    {cout<<-1; return 0;}

    for(int i=25;i<s.size();i++)
    {
        Exlac.clear();
        memset(alpha, 0, sizeof(alpha));

        for(int j=i;j>=i-25;j--)
        {
            if(s[j]=='?')
                {Exlac.push_back(j); continue;}
            alpha[s[j]-'A']++;
        }

        if(valid())
        {
            for(int k=0;k<Exlac.size();k++)
          {
              for(int l=0;l<26;l++)
              {
                  if(!alpha[l])
                  {s[Exlac[k]]='A'+l; alpha[l]++; break;}
              }
          }
           filll(s);
            cout<<s;
            return 0;
        }
    }

    cout<<-1;
    return 0;

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int nn=100005;
const double eps=1e-10;
struct point{double x,y;}a[nn],b[nn];
int n,m,t,l[nn],r[nn];
int h[nn],p[nn];
double R,c[nn];
inline double cp(const point &x,const point &y,const point &z){
	return (x.x-z.x)*(y.y-z.y)-(x.y-z.y)*(y.x-z.x);
}
inline double dis(const point &x,const point &y){
	return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
inline bool cmp(const point &x,const point &y){
	double z=cp(x,y,a[1]);
	if(z>eps)return 1;
	if(z<-eps)return 0;
	return dis(x,a[1])<dis(y,a[1]);
}
void init(){
	scanf("%d%lf",&n,&R);
	for(int i=1;i<=n;++i){
		scanf("%lf%lf",&a[i].x,&a[i].y);
		if(a[i].y<a[1].y||(a[i].y<a[1].y+eps&&a[i].x<a[1].x))
			swap(a[i],a[1]);
	}
	sort(a+2,a+n+1,cmp);
	b[m=1]=a[1];
	for(int i=2;i<=n;++i){
		for(;m>1&&cp(a[i],b[m],b[m-1])>-eps;--m);
		b[++m]=a[i];
	}
}
inline void change(int x,int y){
	swap(p[h[x]],p[h[y]]);
	swap(h[x],h[y]);
}
void up(int x){
	for(;x>1&&c[h[x]]>c[h[x>>1]];x>>=1)change(x,x>>1);
}
void down(int x){
	for(int y=x<<1;y<=t;x=y,y=x<<1){
		if(y<t&&c[h[y]]<c[h[y+1]])++y;
		if(c[h[x]]<c[h[y]])change(x,y);
		else break;
	}
}
inline void calc(int x){
	point y=b[l[x]],z=b[r[x]];
	c[x]=dis(y,z)*dis(b[x],y)*dis(b[x],z)/(2*abs(cp(y,z,b[x])));
	up(p[x]);
}
inline double count(int x,int y){
	double z=2*asin(dis(b[x],b[y])/(2*R));
	return R*R/2*(z-sin(z));
}
void work(){
	for(int i=1;i<=m;++i){
		h[i]=p[i]=i;
		l[i]=i-1,r[i]=i+1;
	}
	t=m;l[1]=m;r[m]=1;
	for(int i=1;i<=m;++i)calc(i);
	while(c[h[1]]>R){
		int i=h[1];
		l[r[i]]=l[i];r[l[i]]=r[i];
		change(1,t--);down(1);
		calc(l[i]);calc(r[i]);
	}
	int j=h[1];
	double s=count(j,l[j])+count(j,r[j]);
	for(int i=r[j];r[i]!=j;i=r[i])
		s+=abs(cp(b[i],b[r[i]],b[j]))/2+count(i,r[i]);
	printf("%.10lf\n",s);
}
int main(){
	init();
	work();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,z,ans;
int main()
{
	cin>>n>>m>>z;
	for(int i=1;i<=z;i++)
	{
		for(int j=1;j<=z;j++)
		{
			if(n*i==m*j&&n*i<=z&&m*j<=z)
			{
				ans++;
				//cout<<n*i<<endl;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
using namespace std;
int i=0,l,n=0;char s[60];int main(){scanf("%d%s",&l,s);for(;i<l;i+=n+1,n++) printf("%c",s[i]);return 0;}


#include<vector>
#include<string>
#include<iostream>
#include<utility>
#include<set>
#include<map>
#include<math.h>
#include<deque>
#include<algorithm>
#define ll long long
#define pii pair<int,int>
#define pip pair<int,pii>
using namespace std;
ll mod=1000000007;
ll inf=1LL<<60;
const int N=5000;
vector<int> primes;
ll ans=inf;
vector<vector<pii>> tree(N+1);
vector<map<int,int>> facts;
vector<int> p;
vector<ll> dists;
vector<ll> subtree;
void preprocess(){
    facts.resize(N+1);
    p.resize(N+1);
    primes.push_back(2);
    facts[1][1]=0;
    facts[2][2]=1;
    p[2]=1;
    p[1]=0;
   // ll sum=0;
    for (int i=3;i<=N;i++){
        bool isPrime=true;
        for (int a:primes){
            if (!(i%a)){
                isPrime=false;
                facts[i]=facts[i/a];
                p[i]=p[i/a]-p[i/a-1]+1+p[i-1];
                facts[i][a]++;
                break;
            }
        }
        if (isPrime){
            primes.push_back(i);
            p[i]=p[i-1]+1;
            facts[i][i]=1;
        }
    //    sum+=p[i];
//        cout<<i<<": ";
//        for (auto a:facts[i]){
//            cout<<a.first<<","<<a.second<<" ";
//        }
//        cout<<"\n";
    }
    //cout<<sum<<'\n';
//    for (int i:primes){
//        cout<<i<<"\n";
//    }
}
//each comp will be the first node after the split
vector<vector<int>> comps(5001);
void dfs(int curr, int par){
   // cout<<curr<<" "<<par<<"\n";
    int big= (*facts[curr].rbegin()).first;
   // cout<<big<<"\n";
    int cnt=0;
    for (int i=primes.size()-1;i>=0;i--){
        if (primes[i]==big){
            cnt=i;
            break;
        }
    }
  //  cout<<cnt<<"\n";
    int sum=0;
    int temp=curr;
    while(temp%big==0){
        sum++;
        temp/=big;
    }
  //  cout<<"asdf "<<sum<<"\n";
    int it=comps[curr].size()-1;
  //  if (comps[curr].back())
    int lst=par;
    for (int i=cnt;i>0;i--){
       // if (curr<10)
         //   cout<<curr<<"hi "<<sum<<'\n';
        if (it>=0 && (*facts[comps[curr][it]].rbegin()).first==primes[i]){
            tree.emplace_back();
            tree[lst].emplace_back(tree.size()-1,sum);
            tree.back().emplace_back(lst,sum);
            lst=tree.size()-1;
            dfs(comps[curr][it],tree.size()-1);
            it--;
            sum=0;
        }
        temp=curr;
        while(temp>=primes[i-1]){
            temp/=primes[i-1];
            sum+=temp;
        }
    }
    tree[lst].emplace_back(curr,sum);
    tree[curr].emplace_back(lst,sum);
    lst=curr;
    if (it>=0 && (*facts[comps[curr][it]].rbegin()).first==2){
        dfs(comps[curr][it],lst);
    }
}
void dfs2(int curr, int par){
    for (pii& i:tree[curr]){
        if (i.first!=par){
            dfs2(i.first,curr);
            subtree[curr]+=subtree[i.first];
            dists[curr]+=dists[i.first]+subtree[i.first]*i.second;
        }
    }
  //  if (subtree[curr]>0)
    //    cout<<curr<<" "<<subtree[curr]<<" "<<dists[curr]<<"\n";
}
void dfs3(int curr, int par, ll upsum){
    ll val=upsum+dists[curr];
    ans=min(ans,val);
  //  cout<<val<<" "<<curr<<'\n';
    for (pii& i:tree[curr]){
        if (i.first!=par) {
            dfs3(i.first, curr, val - dists[i.first] - subtree[i.first] * i.second
            +i.second*(subtree[1]-subtree[i.first]));
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin.exceptions(cin.failbit);
    int n;
    cin>>n;
    //tree.resize(n);
    preprocess();
    for (int i=2;i<=5000;i++){
        int curr=1;
        int big= (*facts[i].rbegin()).first;
        while(!comps[curr].empty() && (*facts[comps[curr].back()].rbegin()).first>=big){ //equals still follows trail
            curr=comps[curr].back();
        }
        comps[curr].push_back(i);

    }
    for (int a:comps[1]){
        dfs(a,1);
    }
//    for (int i=1;i<=n;i++){
//        cout<<i<<":\n";
//        for (pii a:tree[i]){
//            if (a.first>5100){
//                continue;
//            }
//            cout<<a.first<<","<<a.second<<" ";
//        }
//        cout<<"\n";
//    }
    subtree.resize(tree.size(),0);
    dists.resize(tree.size(),0);
    for (int i=0;i<n;i++){
        int a;
        cin>>a;
        if (a==0){
            a++;
        }
        subtree[a]++;
    }
    dfs2(1,-1);
    dfs3(1,-1,0);
    cout<<ans<<"\n";
 //   for (int i=2;i<=5000;i++){
   //     cout<<i<<" "<<p[i]<<'\n';
    //}
    //cout<<primes.size()<<"\n";
//    for (int i=1;i<=5000;i++){
//        cout<<i<<" "<<(*facts[i].rbegin()).first<<": ";
//        for (int a:comps[i]){
//            cout<<a<<" ";
//        }
//        cout<<"\n";
//    }
//    for (int i=0;i<primes.size()-1;i++){
//        tree[1].emplace_back(primes[i],1);
//        tree[primes[i]].emplace_back(1,1);
//        for (int a=primes[i]+1;a<primes[i+1];a++){
//
//        }
//    }
}


#include<iostream>
#include<iomanip>
#include<cstdio>
using namespace std;
int main()
{
	int a,b[9],c=1,d;
	cin>>a;
	for (int i=1;i<=7;i++)
	cin>>b[i];
	while (1){
		c=c%7==0?7:c%7;
		a-=b[c];
		c++;
		if (a<=0) {
			cout<<c-1;
			break;}}}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main(){
	string s;
	long long n, m, l=0, d, r,a[200002]={0},k=0, b[10001]={0}, y, ra,x;

	cin>>n>>y>>x>>k;
	n--;
	if (n==0)
	cout<<0;
	else if (k<x && k<y)
		cout<<min(x, y)+(n-1)*k;
		else cout<<min(x, y)*n;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>

using namespace std;

int main()
{
	char arr[4][4];
	int hash = 0, dot = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cin >> arr[i][j];
		}
	}

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (arr[i][j] == '#')
				hash++;
			else
				dot++;
			if (arr[i][j + 1] == '#')
				hash++;
			else
				dot++;
			if (arr[i + 1][j] == '#')
				hash++;
			else
				dot++;
			if (arr[i + 1][j + 1] == '#')
				hash++;
			else
				dot++;

			if (hash >= 3 || dot >= 3)
			{
				cout << "YES";
				return 0;
			}
			hash = dot = 0;

		}
	}
	cout << "NO";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	string s;
  	cin>>s;
  	for(int i=0;i<s.size();i++)
  	{
   	  	if(s[i]=='W'&&s[i+1]=='U'&&s[i+2]=='B')
   	  	{
         	if(i!=0)
         	cout<<" ";
         	i=i+2;
		}
        else
		cout<<s[i];
  	}
  	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
/*
两个数相乘再除以这两个数的最大公约数
*/
const int N=12252245;
const int M=998244353;
ll cnt[N][2],n,LCM;
int getlcm(int k){
	int lcm=1;
	for(int i=1;i<=k;i++){
		int gc=__gcd(i,lcm);
		lcm=lcm/gc*i;
	}
	return lcm;
}
ll qmod(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1)res=res*a%M;
		a=a*a%M;
		b>>=1;
	}
	return res;
}
ll solve(int Pow,int m,int f){
	ll fm=1,ans=0;
	for(int i=1;i<=Pow;i++)fm=fm*n%M;
	fm=qmod(fm,M-2);
	for(int i=0;i<LCM;i++){
		if(cnt[i][f])ans=(ans+cnt[i][f]*i)%M;
		cnt[i-i%m][f^1]=(cnt[i][f]+cnt[i-i%m][f^1])%M;
		cnt[i][f^1]=(cnt[i][f^1]+cnt[i][f]*(n-1))%M;
		cnt[i][f]=0;
	}
	return ans*fm%M;
}
int main(){
	ll a0,x,y,k,mod;
	scanf("%d%lld%lld%lld%lld%lld",&n,&a0,&x,&y,&k,&mod);
	LCM=getlcm(k);
//	cout<<LCM<<endl;
	ll ans=a0/LCM*LCM*k%M,tmp=a0%LCM;
	cnt[a0%LCM][0]++;
	for(int i=1;i<n;i++){
		a0=(a0*x+y)%mod;
		cnt[a0%LCM][0]++;
		ans=(ans+a0/LCM*LCM*k)%M;
		tmp=(tmp+a0%LCM)%M;
	}
	tmp=tmp*qmod(n,M-2)%M;
	ans=ans*qmod(n,M-2)%M;
	ans=(ans+tmp)%M;
	for(int i=2;i<=k;i++){
		ans=(ans+solve(i-1,i,i%2))%M;
	}
	for(int i=1;i<=k;i++)ans=ans*n%M;
	printf("%lld\n",ans);
	return 0;
}
/*
2 235 125 236 2 2356723
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef complex<double> base;

#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define K (1 << 15)
#define MOD 998244353
#define PROOT 3
#define B 2

int N = K << 1, N_INV;
vector<int> swapIndex(N);
vector<int> omega(N), omega_inv(N), a[B][B], b[B][B], new_a[B][B], new_b[B][B];
ll ROOT, ROOT_INV;

ll ModularExponentation(ll a, ll n){
  ll ans = 1;

  for( ; n > 0; n /= 2){
    if(n % 2)
    ans = (ans * a) % MOD;
    a = (a * a) % MOD;
  }

  return ans;
}

void InitializeData(){
  for(int i = 0; i < B; i++)
    for(int j = 0; j < B; j++)
      a[i][j].resize(N), b[i][j].resize(N), new_a[i][j].resize(N), new_b[i][j].resize(N);

  a[0][0][0] = a[0][0][1] = a[0][1][1] = a[1][0][0] = 1;
  b[0][0][0] = b[0][0][1] = b[1][0][0] = 1;
}

void InitializeFFT(){
  ROOT = ModularExponentation(1ll * PROOT, (MOD - 1) / N);
  omega[0] = omega_inv[0] = 1;
  ROOT_INV = ModularExponentation(ROOT, MOD - 2);
  N_INV = ModularExponentation(N, MOD - 2);

  for(int i = 1; i < N; i++){
    omega[i] = (1ll * ROOT * omega[i - 1]) % MOD;
    omega_inv[i] = (1ll * ROOT_INV * omega_inv[i - 1]) % MOD;
  }

  for(int i = 0; i < N; i++){
    int j = 0, k = i;
    for(int x = 1; x < N; x <<= 1){
      j = (j << 1) | (k & 1);
      k >>= 1;
    }
    swapIndex[i] = j;
  }
}

void FFT(vector<int>& a, int isInv = 0){
  for(int i = 0; i < N; i++){
    if(swapIndex[i] < i)
      swap(a[i], a[swapIndex[i]]);
  }

  for(int l = 2; l <= N;l <<= 1){
    for(int i = 0, F = N / l; i < N; i += l){
      for(int j = 0, f = 0; j < (l >> 1); j++, f += F){
        int x = i + j, y = x + (l >> 1);
        int t = (1ll * (isInv * omega_inv[f] + (1 - isInv) * omega[f]) * a[y]) % MOD, u = a[x];
        a[x] = (u + t) % MOD, a[y] = (MOD + u - t) % MOD;
      }
    }
  }
}

void FFTMatrix(vector<int> a[B][B]){
  for(int i = 0; i < B; i++)
    for(int j = 0; j < B; j++)
      FFT(a[i][j], 0);
}

void MultiplyFFTMatrix(vector<int> a[B][B], vector<int> b[B][B], vector<int> c[B][B]){
  for(int i = 0; i < B; i++){
    for(int j = 0; j < B; j++){
      for(int x = 0; x < N; x++)
        c[i][j][x] = 0;

      for(int k = 0; k < B; k++)
        for(int x = 0; x < N; x++)
          c[i][j][x] = (c[i][j][x] + (int)((1ll * a[i][k][x] * b[k][j][x]) % MOD)) % MOD;

      FFT(c[i][j], 1);

      for(int x = 0; x < K; x++)
        c[i][j][x] = (1ll * N_INV * c[i][j][x]) % MOD;

      for(int x = K; x < N; x++)
        c[i][j][x] = 0;
    }
  }
}

void CopyMatrix(vector<int> s[B][B], vector<int> t[B][B]){
  for(int i = 0; i < B; i++)
    for(int j = 0; j < B; j++){
      for(int x = 0; x < N; x++)
        t[i][j][x] = s[i][j][x];
    }
}

int main(){
  int n, m;

  cin >> n >> m;

  InitializeFFT();
  InitializeData();

  for(n--; n > 0; n /= 2){
    FFTMatrix(a);

    if(n % 2){
      FFTMatrix(b);
      MultiplyFFTMatrix(a, b, new_b);
      CopyMatrix(new_b, b);
    }

    MultiplyFFTMatrix(a, a, new_a);
    CopyMatrix(new_a, a);
  }

  for(int i = 1; i <= m; i++)
    printf("%d ", b[0][0][i]);
  printf("\n");

  return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

int mod=1000000000+7;

int addm(int& a,int b) {return (a+=b)<mod?(a<0?a+=mod:a):a-=mod;}
int addm2(int a,int b) {return addm(a,b);}

template<class T,class U> bool smin(T& a,U b) {return a>b?(a=b,1):0;}
template<class T,class U> bool smax(T& a,U b) {return a<b?(a=b,1):0;}

int n,d,dp[1001][11][1001],cs[1001][11][1002];

int a,b,g;
void gcd(int m,int n) {
    if (n==0) {a=1;b=0;g=m;return;}
    gcd(n,m%n);
    int t=a-b*(m/n);
    a=b;b=t;
}

int inv(int a,int mod) {
	gcd(mod,a);
	return g>0?b:-b;
}

int iv[1001];

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

	cin >> n >> d >> mod;
	//dp[0][d-1][0]=1;
	dp[0][0][0]=1;
	//for (int j=0;j<=d;j++) dp[0][j][0]=1;
	for (int i=1;i<=1000;i++) iv[i]=inv(i,mod);
	for (int i=1;i<1002;i++) for (int j=0;j<=d;j++)
		cs[0][j][i]=addm2(cs[0][j][i-1],dp[0][j][i-1]);
	for (int i=1;i<n;i++) for (int j=1;j<=d;j++) for (int k=1;k<n;k++) {
		ll f=1;
		for (int l=1;l*k<=i && l<=j && (k==1 || cs[k-1][d-1][k]);l++) {
			f=f*((k==1?1:cs[k-1][d-1][k])+l-1)%mod*iv[l]%mod;
			if (cs[i-l*k][j-l][k])
			addm(cs[i][j][k+1],(ll)cs[i-l*k][j-l][k]*f%mod);
		}
		addm(cs[i][j][k+1],cs[i][j][k]);
		//cs[i][j][k+1]=addm2(cs[i][j][k],dp[i][j][k]);
	}

	int ans=cs[n-1][d][(n+1)/2];
	addm(ans,cs[n-1][0][(n+1)/2]);
	if (n%2==0) {
		ll v=cs[n/2-1][d-1][n/2]+cs[n/2-1][0][n/2];
		addm(ans,v*(v+1)/2%mod);
	}

	if (ans<0) ans+=mod;
	cout << ans << endl;

}


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

long long getfirst(long long a) {
    while(a > 9) a /= 10;
    return a;
}

long long getlast(long long a) {
    return a % 10;
}

int main() {
    long long l, r;
    cin >> l >> r;
    long long count = 0;
    for(long long i = l; i <= r; i++) {
        if(i < 10) {
            count++;
            continue;
        }
        if(i < 100) {
            if(getfirst(i) == getlast(i)) {
                count++;
                continue;
            }
        }
        if(getfirst(i) != getlast(i)) continue;
        long long zzz = 1000000000000000000;
        while(zzz + i > r || zzz > i || getfirst(i) != getfirst(i + zzz)) zzz /= 10;
        if(zzz >= 10) {
            count += zzz / 10;
            i += zzz - 1;
            continue;
        }
        if(getfirst(i) == getlast(i)) count++;
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define inf 1001001001
#define infll 1001001001001001001LL
#define FOR0(i,n) for(int (i)=0;(i)<(n);++(i))
#define FOR1(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define ld double
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define RI(n) int (n); scanf("%d",&(n));
#define RI2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define RI3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR0(i,sz(t))s<<t[i]<<" ";return s; }
#define dbg(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define all(t) t.begin(),t.end()
#define FEACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS RI(testow)while(testow--)
#define FORZ(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define FORD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define gmax(a,b) (a)=max((a),(b))
#define gmin(a,b) (a)=min((a),(b))
#define ios0 ios_base::sync_with_stdio(0)
int r,n;
ll MOD=1000003;
ll qp(ll a,ll b)
{
	ll x=1;
	while(b)
	{
		if(b&1) x=x*a%MOD;
		a=a*a%MOD; b>>=1;
	}
	return x;
}
int main()
{
	cin>>r>>n;
	ll ans=0,cur=1;
	for(int a=n,b=1;b<=r;a++,b++)
	{
		cur=cur*a%MOD*qp(b,MOD-2)%MOD;
		ans=(ans+cur)%MOD;
	}
	cout<<ans<<"\n";
}


/*Don't stop when you're tired, stop when you're done*/
/*Dont limit your challenges,challenge your limits*/
/*No matter how hard it is,No matter how hard it gets, I am going to make it*/
/*I have got a dream that's worth more than my sleep*/
/*It always seems impossible until it's done*/
/*Author- Prakhar Bindal(Indian Institute of Technology,Kharagpur)*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int>vi;
typedef vector<pair<int,int> >vpi;
#define mod 1000000007
#define mod1 500000003
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n,d;
     cin>>n>>d;
     int a[n];
     for(int i=0;i<n;i++)
          cin>>a[i];
     int sum=0;
     for(int i=0;i<n;i++)
          sum+=a[i];
     if(sum+10*(n-1)>d)
     {
          cout<<-1;
          return 0;
     }
     cout<<(d-sum)/5;
}


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

int check(int a){
    int b;
    if(a==0)b=1;
    else if(a==1)b=6;
    else if(a==2)b=1;
    else if(a==3)b=2;
    else if(a==4)b=2;
    else if(a==5)b=3;
    else if(a==6)b=1;
    else if(a==7)b=4;
    else if(a==8)b=0;
    else if(a==9)b=1;
    return b;
}

int main()
{
    int a,b,p,q,ar[2];
    cin>>a;
    for(b=0;b<2;b++){
        ar[b]=a%10;
        a=a/10;
    }
    p=check(ar[0])+1;
    q=check(ar[1])+1;

    cout<<p*q<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool sat(string& s)
{
	if(s[0] != '1')
	return false;
	else if(s.find("444") != s.npos)
	return false;
	else
	{
		for(int i=1;i<s.size();i++)
		{
			if(s[i] !='1' && s[i] != '4')
			return false;

		}
	}
	return true;
}
int main()
{
	string s; cin>>s;
	if(sat(s))
	cout<<"YES\n";
	else
	cout<<"NO\n";
}


#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define mp make_pair
#define ss second
#define ff first
#define ll long long
#define pb push_back
const int MOD = 1e9 + 7;
#define mod %MOD

const int N = 1000 + 11;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    string s;
    cin>>s;
    int b0=s[0]-'0';
    int b1=s[1]-'0';
    int b2=s[2]-'0';
    int b3=s[3]-'0';
    int b4=s[4]-'0';
    int b5=s[5]-'0';
    int ans=6;
    for (int a0=0; a0<=9; a0++)
    for (int a1=0; a1<=9; a1++)
    for (int a2=0; a2<=9; a2++)
    for (int a3=0; a3<=9; a3++)
    for (int a4=0; a4<=9; a4++)
    for (int a5=0; a5<=9; a5++)
        if (a0+a1+a2==a3+a4+a5)
    {
        ans=min(ans,(b0!=a0)+(b1!=a1)+(b2!=a2)+(b3!=a3)+(b4!=a4)+(b5!=a5));
    }
    cout<<ans<<endl;
}


#include <cstdio>
#include <cstring>
#include <queue>
#include <cctype>
#define MAXN 256
#define MAXT 64
#define MAXE MAXN * MAXN
#define INF 1000000000
using namespace std;
class Dinic {
	struct edge {
		int u, v, f, c, nx;
		edge() {}
		edge(int u, int v, int f, int c, int nx): u(u), v(v), f(f), c(c), nx(nx) {}
	} e[MAXE];
	int n, m, src, dst, head[MAXN], work[MAXN], d[MAXN];
	bool _bfs() {
		static int q[MAXN], le, ri;
		le = ri = 0;
		memset(d, -1, sizeof(int) * n);
		q[ri++] = src;
		d[src] = 0;
		while(le < ri) {
			for(int k = q[le++], i = head[k]; i != -1; i = e[i].nx) {
				if(e[i].f < e[i].c && d[e[i].v] == -1) {
					d[e[i].v] = d[k] + 1;
					q[ri++] = e[i].v;
				}
			}
		}
		return (d[dst] != -1);
	}
	int _dfs(int u, int f) {
		if(u == dst)
			return f;
		int minf;
		for(int& i = work[u]; i != -1; i = e[i].nx) {
			if(e[i].f < e[i].c && d[e[i].v] == d[u] + 1) {
				minf = _dfs(e[i].v, min(f, e[i].c - e[i].f));
				if(minf > 0) {
					e[i].f += minf;
					e[i ^ 1].f -= minf;
					return minf;
				}
			}
		}
		return 0;
	}
public:
	void init(int n, int src, int dst) {
		this->n = n;
		this->src = src;
		this->dst = dst;
		m = 0;
		memset(head, -1, sizeof(int) * n);
	}
	void addEdge(int u, int v, int c) {
		//printf("%d %d %d\n", u, v, c);
		e[m] = edge(u, v, 0, c, head[u]);
		head[u] = m++;
		e[m] = edge(v, u, 0, 0, head[v]);
		head[v] = m++;
	}
	int maxFlow() {
		int ret = 0, delta;
		while(_bfs()) {
			memcpy(work, head, sizeof(int) * n);
			while(true) {
				delta = _dfs(src, INF);
				if(delta == 0)
					break;
				ret += delta;
			}
		}
		return ret;
	}
} dinic;
#define MAX 12
int n, t, zx, zy;
char smp[MAX][MAX], cmp[MAX][MAX], work[MAX][MAX];
int vis[MAXT][MAX][MAX];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
inline int X(int i, int j) { return i * n + j; }
inline int Y(int i, int j) { return n * n + i * n + j; }
inline int S() { return n * n * 2; }
inline int T() { return S() + 1; }
inline int N() { return T() + 1; }
struct qNode {
	int x, y, t, type;
	qNode() {}
	qNode(int x, int y, int t, int type): x(x), y(y), t(t), type(type) {}
};
void bfs(int sx, int sy) {
	queue<qNode> q;
	q.push(qNode(sx, sy, 0, 0));
	vis[0][sx][sy] = 1;
	q.push(qNode(zx, zy, 0, 1));
	while(!q.empty()) {
		qNode u = q.front();
		int nx, ny;
		q.pop();
		if(u.t == t)
			break;
		if(u.type == 0 && isalpha(work[u.x][u.y]))
			continue;
		/*if(u.type == 0 && !vis[u.t + 1][u.x][u.y] && !isalpha(work[u.x][u.y])) {
			vis[u.t + 1][u.x][u.y] = 1;
			q.push(qNode(u.x, u.y, u.t + 1, 0));
		}*/
		for(int i = 0; i < 4; ++i) {
			nx = u.x + dx[i];
			ny = u.y + dy[i];
			if(nx >= 0 && nx < n && ny >= 0 && ny < n) {
				if(u.type == 0 && !vis[u.t + 1][nx][ny] && !isalpha(work[nx][ny])) {
					vis[u.t + 1][nx][ny] = 1;
					q.push(qNode(nx, ny, u.t + 1, 0));
				}
				else if(u.type == 1 && !isalpha(work[nx][ny])) {
					work[nx][ny] = 'Z';
					q.push(qNode(nx, ny, u.t + 1, 1));
				}
			}
		}
	}
}
int main() {
	while(scanf("%d%d", &n, &t) != EOF) {
		dinic.init(N(), S(), T());
		for(int i = 0; i < n; ++i) {
			scanf("%s", smp[i]);
			for(int j = 0; j < n; ++j)
				if(isdigit(smp[i][j]) && smp[i][j] > '0')
					dinic.addEdge(S(), X(i, j), smp[i][j] - '0');
				else if(smp[i][j] == 'Z') {
					zx = i;
					zy = j;
				}
		}
		for(int i = 0; i < n; ++i) {
			scanf("%s", cmp[i]);
			for(int j = 0; j < n; ++j)
				if(isdigit(cmp[i][j]) && cmp[i][j] > '0')
					dinic.addEdge(Y(i, j), T(), cmp[i][j] - '0');
		}
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j) {
				if(!isdigit(smp[i][j]) || smp[i][j] <= '0')
					continue;
				memset(vis, 0, sizeof(vis));
				memcpy(work, smp, sizeof(smp));
				bfs(i, j);
				for(int xi = 0; xi < n; ++xi)
					for(int yi = 0; yi < n; ++yi) {
						if(!isdigit(cmp[xi][yi]) || cmp[xi][yi] <= '0')
							continue;
						for(int ti = 0; ti <= t; ++ti)
							if(vis[ti][xi][yi]) {
								dinic.addEdge(X(i, j), Y(xi, yi), INF);
								break;
							}
					}
			}
		printf("%d\n", dinic.maxFlow());
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define allof(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

    int x, y;
    cin >> x >> y;
    int r = sqrt(x*x + y*y);

    if (x * y >= 0) {
        if (r % 2 == 0) {
            cout << "black";
            return 0;
        }
    }

    if (x * y <= 0) {
        if (r % 2 == 1) {
            cout << "black";
            return 0;
        }
    }

    if (r * r == x * x + y * y) {
        cout << "black";
        return 0;
    }

    cout << "white";
}


#include <iostream>
using namespace std;

int mask(int x) {
    int ans = 0, t = 1;
    while(x>0) {
        int d = x%10;
        if(d==4 or d==7) {
            ans = d*t + ans;
            t*=10;
        }

        x /= 10;
    }

    return ans;
}
int main() {
	int a, b;
	cin>>a>>b;
	if(a<b) {
	    cout<<b<<endl;
	}
	else {
	    for(int i=a+1; i<2e5; i++) {
	        if(b == mask(i)) {
	            cout<<i<<endl;
	            break;
	        }
	    }
	}
	return 0;
}


/*
  /\     /\
  | ).|.( |
  |  >-<  |
  =========
It's AdilkhanKo miaaaaaau
*/
#include<bits/stdc++.h>

#define ll long long
#define pb push_back
#define endl "\n"
#define foreach(it, S) for(__typeof (S.begin()) it = S.begin(); it != S.end(); it++)
#define mp make_pair
#define f first
#define s second
#define name ""
#define _ ios_base::sync_with_stdio(false);cin.tie(0);

using namespace std;

const int MaxN = int (2e6) + 256;
const ll INF = (ll)(1e18);
const int inf = (int)(1e9);
const int mod = (int)(1e9) + 7;
const double pi = 3.1415926535897932384626433832795;
ll n, m, ans, a[MaxN], b[MaxN], k;

int main () {
	#ifdef ONLINE_JUDGE
//		freopen (name".in","r",stdin);
//		freopen (name".out","w",stdout);
	#else
		freopen (".in","r",stdin);
		freopen (".out","w",stdout);
	#endif
	cin >> n >> k;
	int q = 0;
	ll N = n, cnt = 0;
	while(N % 10 == 0){
	 	N /= 10;
	 	q++;
	}
	if(q >= k)cout << n;
	else{
		k -= q;
		ll cnt1 = 0;
		while(N % 5 == 0){
			cnt++;
			N /= 5;
		}
		while(N % 2 == 0){
			cnt1++;
			N /= 2;
		}
		if(cnt > 0){
			for(int i = 1; i <= min(k, cnt); i++){
				n *= 2ll;
			}
				for(int i = cnt + 1; i <= k; i++) n *= 10ll;
		}else{
			for(int i = 1; i <= min(k, cnt1); i++){
				n *= 5ll;
			}
				for(int i = cnt1 + 1; i <= k; i++) n *= 10ll;
		}
		cout << n;
	}
return 0;
}


#include <cstdio>
#include <cstring>

using namespace std;

int n,memo[10][(1 << 10)][9];
bool adj[10][10];

int solve(int pos, int mask, int k){
    if(mask == 0) return k == 0;
    if(k == 0) return 0;

    int x = __builtin_ctz(mask);

    if(__builtin_popcount(mask) == 1)
        return k == 1 && adj[pos][x];

    int &ret = memo[pos][mask][k];

    if(ret == -1){
        ret = 0;

        for(int mask2 = mask;mask2 > 0;mask2 = (mask2 - 1) & mask){
            if(mask2 & (1 << x)){
                for(int i = 0;i < n;++i){
                    if((mask2 & (1 << i)) && adj[pos][i]){
                        if(mask2 == (1 << i)) ret += solve(pos,mask ^ mask2,k - 1);
                        else
                            for(int j = 1;j <= k;++j)
                                ret += solve(i,mask2 ^ (1 << i),j) * solve(pos,mask ^ mask2,k - j);
                    }
                }
            }
        }
    }

    return ret;
}

int main(){
    int m,K;

    scanf("%d %d %d",&n,&m,&K);

    memset(adj,false,sizeof adj);

    for(int i = 0,u,v;i < m;++i){
        scanf("%d %d",&u,&v);
        --u; --v;
        adj[u][v] = adj[v][u] = true;
    }

    memset(memo,-1,sizeof memo);

    int ans = 0;

    for(int r = 0;r < n;++r){
        int mask = ((1 << n) - 1) ^ (1 << r);

        for(int i = 0;i < n;++i)
            if(i != r && (mask & (1 << i)) && adj[r][i])
                ans += solve(i,mask ^ (1 << i),K - 1);
    }

    ans /= K;

    printf("%d\n",ans);

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
using namespace std;
const int N=410,P=1e9+7;
int dp[N][N],n,a[N];
inline void upd(int &x,int y){((x+=y)>=P)&&(x-=P);}
int main(){
	scanf("%d",&n);dp[1][1]=dp[1][0]=1;
	rep(i,2,n){
		dp[i][0]=1;
		rep(j,0,n) a[j]=0;
		rep(j,0,n) rep(k,0,n-j) upd(a[j+k],1ll*dp[i-1][j]*dp[i-1][k]%P);
		rep(j,1,n) dp[i][j]=(1ll*j*(j+1)*a[j+1]+(2ll*j+1)*a[j]+a[j-1])%P;
	}
	printf("%d\n",dp[n][1]);
	return 0;
}


#include <bits/stdc++.h>

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> op;

const int MOD = 1e9 + 7;
const int MAX = 5e2 + 5;
int mat[MAX][MAX], hydra[5][5], b[MAX];
bool rzero[MAX];
char str[MAX];
int n, m;

int eval(char c) {
	return c - 'a';
}

int main() {
	scanf("%d %d", &n, &m);

	fori(i, 0, n) {
		scanf("%s", str);
		fori(r, 0, m) {
			mat[r][i] = eval(str[r]);
		}
	}

	fori(i, 0, 5) {
		int k = i;
		fori(j, 0, 4) {
			hydra[i][k] = j;
			k = (k + i) % 5;
		}
	}

	swap(n, m);

	vector<op> ops;
	int row = 0;
	for(int col = 0; row < n && col < m; col++) {
		int pivot = -1;
		fori(i, row, n) {
			if(mat[i][col] > 0) {
				pivot = i;
				break;
			}
		}
		if(pivot == -1) {
			continue;
		}
		swap(mat[row], mat[pivot]);
		ops.emplace_back(0, row, pivot);
		fori(i, row + 1, n) {
			if(mat[i][col] > 0) {
				int to_apply = (hydra[mat[i][col]][mat[row][col]] + 1);
				ops.emplace_back(1, i, to_apply);
				fori(j, 0, m) {
					mat[i][j] *= to_apply;
					mat[i][j] = (mat[i][j] - mat[row][j]) % 5;
					if(0 > mat[i][j]) {
						mat[i][j] += 5;
					}
				}
			}
		}
		row++;
	}

	int r = 0;
	fori(i, 0, n) {
		rzero[i] = true;
		fori(j, 0, m) {
			if(mat[i][j] > 0) {
				r++;
				rzero[i] = false;
				break;
			}
		}
	}

	int pot = 1;
	fori(i, 0, m - r) {
		pot = (1LL * pot * 5) % MOD;
	}

	int q;
	scanf("%d", &q);
	while(q--) {
		scanf("%s", str);
		int ans = pot;
		fori(i, 0, n) {
			b[i] = eval(str[i]);
		}

		int last_row = -1;
		for(auto &each : ops) {
			int type, x, y;
			tie(type, x, y) = each;
			if(type == 0) {
				swap(b[x], b[y]);
				last_row = x;
			}
			else {
				b[x] *= y;
				b[x] = (b[x] - b[last_row]) % 5;
				if(0 > b[x]) {
					b[x] += 5;
				}
			}
		}
		fori(i, 0, n) {
			if(rzero[i] && b[i] > 0) {
				ans = 0;
				break;
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#include<queue>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int j,s;
	for(int i=0;i<(1<<n);i++){
		for(j=0,s=0;j<n;j++){
			if(i&(1<<j))
				s+=a[j];
			else
				s-=a[j];
		}
		if(s%360==0){
			cout<<"YES";
			return 0;
		}
	}
	cout<<"NO";
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdlib>
using namespace std;
int n,answer,s[1111111];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&s[i]);
		answer+=s[i]*(i-1)*4;
	}
	printf("%d",answer);
}


#include <iostream>
#include <string>
#include <algorithm>
#include <limits.h>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <sstream>
#include <map>
#include <iomanip>
#include <set>
using namespace std;

#define loop(i,n) for(int i = 0; i < (n); i++)
#define loope(i,n) for(int i = 0; i <= (n); i++)
#define rep(i,n,m) for(int i = (n); i < (m); i++)
#define repe(i,n,m) for(int i = (n); i <= (m); i++)

#define ULL unsigned long long int
#define LL long long int
#define point pair<int,int>

#define getBit(n,i) (n) & (1<<(i))
#define setBit(n,i) (n) = (n) | (1<<(i))

#define fopen freopen("input.txt","r",stdin)
#define fclose cout << "EOF"; while(1)

int main()
{
    signed long long int n, x, y, c;
    cin >> n >> y >> x >> c;

    signed long long int lit = 0, nextStep = 1, second = -1;
    signed long long int computed;

    vector<signed long long int> seq;
    seq.push_back(0);

    do
    {
        second++;
        seq.push_back(seq[second] + second + 1);
        lit += nextStep;
        nextStep += (nextStep == 1) ? 3 : 4;

        computed = lit;

        if(x + second > n) computed -= (x + second - n) * (x + second - n);
        if(second >= x) computed -= (second - x + 1) * (second - x + 1);
        if(y + second > n) computed -= (y + second - n) * (y + second - n);
        if(second >= y) computed -= (second - y + 1) * (second - y + 1);

        if(second - y > n - x) computed += seq[second - y - n + x];
        if((second - x + 1) > y) computed += seq[second - x + 1 - y];
        if((second - n + y) > x) computed += seq[second - n + y - x];
        if((second - n + y) > (n + 1 - x)) computed += seq[second - n + y - n - 1 + x];

    } while (computed < c);

    cout << second << endl;
    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
	char s[20];
	int i, j,l;
	cin >> s;
	l = strlen(s);
	j = l / 2;
	int flog = 0;
		for (i = 0; i < j; i++)
		{
			if (s[i] != s[l - 1 - i])
				flog++;
		}
		if (flog > 1||(flog==0&&j*2==l))
			printf("NO\n");
		else
			printf("YES\n");
	return 0;
}


/*input
13 2
4 6
3 5
3 4
3 3
4 2
5 2
6 2
7 3
7 4
7 5
6 6
5 4
5 5

8 2
3 5
5 3
3 3
5 5
2 4
6 4
4 2
4 4

8 2
0 2
1 2
1 0
0 0
2 1
2 0
0 1
2 2
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <queue>

using namespace std;

using pii = pair<int, int>;
#define r first
#define c second

const int dr[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dc[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int MAXN = 150007, MAXV = MAXN * 9, INF = 1e9 + 7;
const int EXT = -1, ALIVE = 1, IN = 0;
int G[MAXV][8], type[MAXV], T, N, V, P[MAXV], inComp[MAXV];
pii C[MAXV], orC[MAXN];
bool peri[MAXV], inq[MAXV];
priority_queue<int> pq;
vector<int> ans;
bool vis[MAXV];

bool comp(int i, int j) { return C[i] < C[j]; }

int search(pii p)
{
	int lo = 1, hi = V;
	while (lo < hi) {
		int mid = (lo + hi) >> 1;
		if (C[P[mid]] < p) lo = mid + 1;
		else hi = mid;
	}
	if (C[P[lo]] == p) return P[lo];
	return 0;
}

int calc(int u)
{
	int cnt = 0;
	vector<int> vec;
	for (int d = 0; d < 8; d += 2) {
		cnt += ((type[G[u][d]] == EXT) && (type[G[u][(d + 1) & 7]] != EXT || type[G[u][(d + 2) & 7]] != EXT));
		if (type[G[u][d]] == IN && (type[G[u][(d + 1) & 7]] == ALIVE || type[G[u][(d + 2) & 7]] == ALIVE)) {
			vec.push_back(inComp[G[u][d]]);
		}
	}

	sort(vec.begin(), vec.end());
	for (int i = 1; i < (int) vec.size(); ++i) {
		if (vec[i - 1] == vec[i]) return INF;
	}

	return cnt;
}

void dfs(int u)
{
	vis[u] = 1;
	for (int d = 0; d < 8; ++d) {
		int v = G[u][d];
		if (!vis[v] && type[v] == ALIVE) dfs(v);
	}
}

void fillExt(int u)
{
	type[u] = EXT;

	for (int d = 0; d < 8; d += 2) {
		if (type[G[u][d]] == ALIVE) {
			peri[G[u][d]] = 1;
			if (!inq[G[u][d]] && calc(G[u][d]) <= 1) {
				inq[G[u][d]] = 1;
				pq.push(G[u][d]);
			}
		} else if (type[G[u][d]] == IN) {
			fillExt(G[u][d]);
		}
	}

	for (int d = 0; d < 8; ++d) {
		if (type[G[u][d]] == ALIVE && peri[G[u][d]] && !inq[G[u][d]] && calc(G[u][d]) <= 1) {
			inq[G[u][d]] = 1;
			pq.push(G[u][d]);
		}
	}
}

void fillIn(int u)
{
	for (int d = 0; d < 8; d += 2) {
		if (type[G[u][d]] == IN && inComp[G[u][d]] == 0) {
			inComp[G[u][d]] = inComp[u];
			fillIn(G[u][d]);
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N >> T; V = N;
	for (int i = 1; i <= N; ++i) cin >> C[i].r >> C[i].c;
	for (int i = 0; i < N; ++i) orC[i] = C[i + 1];
	sort(orC, orC + N); orC[N] = {INF, INF};

	vector<pair<int, int>> all(N * 8);
	for (int i = 1; i <= N; ++i) {
		for (int d = 0; d < 8; ++d) {
			all[(i - 1) * 8 + d] = {C[i].r + dr[d], C[i].c + dc[d]};
		}
	}
	sort(all.begin(), all.end());
	all.erase(unique(all.begin(), all.end()), all.end());
	for (auto a : all) {
		if (*lower_bound(orC, orC + N, a) != a) {
			C[++V] = a;
		}
	}

	for (int u = 1; u <= V; ++u) P[u] = u;
	// for (int u = 1; u <= V; ++u) cout << C[u].r << ' ' << C[u].c << endl;
	sort(P + 1, P + V + 1, comp);

	for (int u = 1; u <= V; ++u) {
		for (int d = 0; d < 8; ++d) {
			G[u][d] = search({C[u].r + dr[d], C[u].c + dc[d]});
		}
	}

	type[0] = EXT;
	for (int u = 1; u <= N; ++u) type[u] = ALIVE;
	for (int u = N + 1; u <= V; ++u) type[u] = IN;
	for (int u = N + 1; u <= V; ++u) {
		if (inComp[u] == 0) {
			inComp[u] = u;
			fillIn(u);
		}
	}

	dfs(1);
	for (int u = 1; u <= N; ++u) if (!vis[u]) return cout << "NO" << endl, 0;

	fillExt(N + 1);
	ans.reserve(N);
	while (!pq.empty()) {
		int u = pq.top(); pq.pop();
		assert(type[u] == ALIVE && peri[u]);
		if (calc(u) > 1) {
			inq[u] = 0;
			continue;
		}
		ans.push_back(u);
		fillExt(u);
	}

	assert((int) ans.size() == N);
	cout << "YES\n";
	for (int i = (int) ans.size() - 1; i >= 0; --i) {
		cout << ans[i] << '\n';
	}
}


#include <stdio.h>
typedef long long ll;
const ll MOD = 998244353;
const int N = 2e5+10;
ll fac[N];
ll qpow(ll a, ll b)
{
	ll res = 1;
	while(b)
	{
		if(b % 2) res = res * a % MOD;
		a = a * a % MOD;
		b /= 2;
	}
	return res;
}
ll C(ll n, ll m)
{
	return (fac[n] * qpow(fac[n-m], MOD-2) % MOD) * qpow(fac[m], MOD-2) % MOD;
}
int main()
{
	fac[0] = 1;
	for(int i = 1; i < N; i++) fac[i] = fac[i - 1] * i % MOD;
	ll n, k;
	scanf("%lld %lld", &n, &k);
	if(k==0)
	{
		printf("%lld\n", fac[n]);
	}else if(k >= n)
	{
		printf("0\n");
	}else
	{
		ll ans = 0;
		for(int i = 0; i < n - k; i++)
		{
			if(i % 2)
			{
				ans = (ans + MOD - qpow(n-k-i, n) * C(n-k, i) % MOD) % MOD;
			}else{
				ans = (ans + qpow(n-k-i, n) * C(n-k, i) % MOD) % MOD;
			}
		}
		ans = (ans * 2 % MOD) * C(n, n-k) % MOD;
		printf("%lld\n", ans);
	}
}


//#pragma comment(linker,"/STACK:256000000")

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cassert>
#include <stdio.h>
#include <string>
#include <memory.h>

using namespace std;

#define ldb long double
#define LL long long
#define nextline() {int c; while ((c = getchar()) != 10 && c != EOF);}

#define PI 3.1415926535897932384626433832795
#define EPS 1e-9

#define sqr(x) ((x) * (x))
#define ABS(a) ((a)<0?-(a):(a))
#define EQ(a,b) (ABS((a)-(b))<EPS)

#define all(a) a.begin(), a.end()
#define two(i) (1 << (i))
#define has(mask, i) ((((mask) & two(i)) == 0) ? false : true)

int N, M;
LL Y;

int b[16];
int used[16];
int was[16];

LL dp[two(16)];

void Load ()
{
	scanf ("%d%I64d%d", &N, &Y, &M);
	Y -= 2000;

	int x, y;

  	for (int i = 0; i < M; i++)
  	{
    	scanf ("%d%d", &x, &y);
    	x--;
    	y--;
    	b[y] |= two(x);
  	}
}

inline LL DP(int m)
{
  	dp[two(N) - 1] = 1;

  	for (int mask = two(N) - 2; mask >= 0; mask--)
  	{
    	int n = __builtin_popcount(mask);

    	dp[mask] = 0;

   		for (int j = 0; j < N; j++)
   		{
      		if (has(mask, j))
      			continue;
      		if ((mask & b[j]) != b[j])
      			continue;
      		if (j <= m && used[j] != n)
      			continue;

      		dp[mask] += dp[mask ^ two(j)];
    	}
  	}

  	return dp[0];
}

void Solve ()
{
  	if (DP(-1) < Y)
  	{
    	printf ("The times have changed");
    	return;
  	}

  	for (int i = 0; i < N; i++)
    	for (int j = 0; j < N; j++)
    		if (!was[j])
	    	{
    		 	used[i] = j;
      			LL x = DP(i);
      			if (Y > x)
      				Y -= x;
      			else
      			{
      				was[j] = 1;
      				break;
      			}
    		}

  	for (int i = 0; i < N; i++)
    	printf("%d ", used[i] + 1);
}
int main()
{
    //ios_base::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
    freopen("out", "wt", stdout);
#endif
    Load();
    Solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define DBG(x) cout << #x << " = " << x << endl;
/*
Compile:
    -Wall -Wextra -pedantic -std=c++11 -O2 -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wcast-qual -Wcast-align -fwhole-program -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -lmcheck -fsanitize=address

Check for memory usage:
    valgrind --tool=massif
 */

const int LIM = 1e7;

bool comp[LIM];
vector<int> primes;

void init()
{
    for(int i = 2; i < LIM; i++) {
        if(!comp[i])
            for(long long j = (i * 1ll) * i; j < LIM; j += i)
                comp[j] = true;
    }
}

bool is_prime(long long x)
{
    if(x < LIM)
        return !comp[x];
    if(x % 2 == 0)
        return false;
    for(long long i = 3; i * i <= x; i += 2) {
        if(x % i == 0)
            return false;
    }
    return true;
}

map< pair<long long, int>, long long> result;

long long solve(long long a, int st=0)
{
    if(a == 1)
        return 1;
    if(result.count({a, st}))
        return result[{a, st}];

//    cout << a << endl;
    long long ans = is_prime(a - 1) * (a >= 1e6);
    for(int i = st; i < primes.size(); i++) {
        long long u = primes[i];
        if(u + 1 > a)
            break;
        long long val = 1;
        while(val + 1 <= a) {
            val *= u;
            if(a % (val + 1) == 0) {
                ans += solve(a / (val + 1), i + 1);
            }
        }
    }
//    cout << a << " " << st << " " << ans << endl;
    return result[{a, st}] = ans;
}

bool some(int x, long long a)
{
    if(x == 1)
        return false;
    long long t = 1;
    for(int i = 1; i <= 66; i++) {
        t *= x;
        if(a % (t + 1) == 0)
            return true;
        if(t > a)
            break;
    }
    return false;
}

int main()
{
    init();
    cin.sync_with_stdio(false);
    long long a;
    cin >> a;
    for(int i = 1; i <= 1e6; i++)
        if(!comp[i] and some(i, a))
            primes.push_back(i);
    cout << solve(a, 0);
    return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int c[]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600}
;
int a[10010];
vector <int> b;
void dfs(ll x)
{
if (x>=1000000000)
return;
    a[++a[0]]=int(x);
        dfs(x*10+4);
            dfs(x*10+7);
            }
            bool check(int x)
            {
            while (x)
                {
                int p=x%10;
                        x/=10;
                                if (p!=4 && p!=7)
                                            return(false);
                                                }
                                                    return(true);
                                                    }
                                                    int main()
                                                    {
                                                    int n,K;                 scanf("%d%d",&n,&K);
                                                    ll now=1;
                                                    int t=-1;
                                                        for (int i=1;i<=n;i++)    {        now*=i;
                                                        if (now>=K)
                                                              {
                                                                          t=i;            break;
                                                                          }
                                                                             }    if (t==-1)
                                                                             printf("-1\n");
                                                                                else
                                                                                {        dfs(4);
                                                                                dfs(7);
                                                                                sort(a+1,a+a[0]+1);
                                                                                for (int i=1;i<=t;i++)
                                                                                b.push_back(n-i+1);
                                                                                reverse(b.begin(),b.end());
                                                                                int ans=0;
                                                                                for (int i=1;i<=a[0];i++)
                                                                                ans+=a[i]<=n-t;
                                                                                int now=n-t;
                                                                                K--;
                                                                                for (int i=t;i;i--)
                                                                                {            now++;
                                                                                int p=0;
                                                                                while (K>=c[i-1])
                                                                                {
                                                                                p++;
                                                                                K-=c[i-1];
                                                                                }
                                                                                ans+=check(now) && check(b[p]);
                                                                                b.erase(b.begin()+p);
                                                                                }
                                                                                printf("%d\n",ans);
                                                                                }
                                                                                return(0);}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int w=0,b=0;
    for(int i=0;i<8;i++)
    {
        cin>>s;

        for(int j=0;j<s.length();j++)
        {
            switch(s[j])
            {
                case 'Q':
                    w+=9;
                    break;
                case 'R':
                    w+=5;
                    break;
                case 'B':
                    w+=3;
                    break;
                case 'N':
                    w+=3;
                    break;
                case 'P':
                    w+=1;
                    break;
                case 'q':
                    b+=9;
                    break;
                case 'r':
                    b+=5;
                    break;
                case 'b':
                    b+=3;
                    break;
                case 'n':
                    b+=3;
                    break;
                case 'p':
                    b+=1;
                    break;

            }

        }
    }
    cout<<(w>b?"White\n":b>w?"Black\n":"Draw\n");

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int dx[4] = { 1, -1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
ll gcd(ll a, ll b) { for (; b; a %= b, swap(a, b)); return a; }
const ll N = 4000002;
ll n, m;
ll dp[N], psum[N];

void func(ll cur, ll v)
{
	for (ll div = 2; div * cur <= n; div++)
	{
		ll l = div * cur;
		ll r = div * cur + (div - 1);
		r = min(r, n + 1);
		psum[l] += v; psum[l] %= m;
		psum[r + 1] -= v;
		while (psum[r + 1] < 0) psum[r + 1] += m;
	}
}
int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	dp[1] = 1; func(1, dp[1]);
	for (int i = 2; i <= n; i++)
	{
		psum[i] += psum[i - 1];
		psum[i] %= m;
		while (psum[i] < 0) psum[i] += m;
		dp[i] = dp[i - 1] + psum[i];
		dp[i] %= m;
		func(i, dp[i]);
		dp[i] += dp[i - 1]; dp[i] %= m;
	}
	ll ans = dp[n] - dp[n - 1];
	while (ans < 0) ans += m;
	cout << ans;
}


//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
//~ #pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (1e6)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = (1e18)+7;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct lit
{
	bool stan = 0;
	int dlu = 1;
	lit(){}
	lit(int a, int b)
	{
		stan = a;
		dlu = b;
	}
};

lit merge(lit a, lit b)
{
	assert(a.stan == b.stan);
	return lit(a.stan, a.dlu + b.dlu);
}

vector<lit> a, b, A, B;

VPII odp;

void normuj(vector<lit> & a)
{
	vector<lit> b;
	for(int i = 0;i < SZ(a);i++)
		if(SZ(b) > 0 && b.back().stan == a[i].stan)
			b.back() = merge(b.back(), a[i]);
		else
			b.pb(a[i]);
	a = b;
}

VPII elo()
{
	odp.clear();
	int ile = 0;
	bool f = 0;
	ile = SZ(a) + SZ(b);
	if((SZ(a) > 2 || SZ(b) > 2) && abs(SZ(a) - SZ(b)) > 2)
	{
		if(SZ(a) < SZ(b))
			f = 1,
			swap(a, b);
		int le = ile / 2 - 1;
		while(le + 1 < SZ(a) && a[le].stan == b.back().stan && a.back().stan != b.back().stan)
			le++;
		if(a.back().stan == b.back().stan)
		{
			le = ile / 2 - 1;
			while(le + 1 < SZ(a) && a[le].stan != b.back().stan)
				le++;
		}
		int cnt = 0;
//		dbg(le, SZ(a), SZ(b));
//		return 0;
		vector<lit> xd, xd2;
		for(int i = 0 ;i < le;i++)
			xd.pb(a[i]);
		for(int i = 0 ; i < SZ(b) - 1;i++)
			xd2.pb(b[i]);
		if(a.back().stan == b.back().stan)
			xd2.pb(b.back());
		int dlu = 0;
		for(int i = le ;i < SZ(a);i++)
			xd2.pb(a[i]),
			dlu += a[i].dlu;
		if(a.back().stan != b.back().stan)
			odp.pb({dlu, b.back().dlu}),
			xd.pb(b.back());
		else
			odp.pb({dlu, 0});
		b = xd2;
		a = xd;
		normuj(a);
		normuj(b);

	}
	dbg(SZ(a), SZ(b));
//	return 0;

	while(SZ(a) > 1 && SZ(b) > 1)
	{
		if(SZ(a) == 2 && SZ(b) == 2)
		{
			if(a.back().stan == b.back().stan)
			{
				odp.pb({a.back().dlu, 0});
				odp.pb({a[0].dlu, a.back().dlu + b.back().dlu});
			}
			else
				odp.pb({a.back().dlu, b.back().dlu});
			if(f)
				for(auto & s : odp)
					swap(s.fi, s.se);
			return odp;
		}
		if(a.back().stan == b.back().stan)
		{
			if(SZ(a) >= SZ(b))
			{
				pair<lit, lit> xd = {a[SZ(a) - 2], a.back()};
				odp.pb({xd.fi.dlu + xd.se.dlu, b.back().dlu});
				a.pop_back();
				a.pop_back();
				lit xdxd = b.back();
				b.pop_back();
				a.back() = merge(a.back(), xdxd);
				b.back() = merge(xd.fi, b.back());
				b.push_back(xd.se);
			}
			else
			{
				pair<lit, lit> xd = {b[SZ(b) - 2], b.back()};
				odp.pb({a.back().dlu, xd.fi.dlu + xd.se.dlu});
				b.pop_back();
				b.pop_back();
				lit xdxd = a.back();
				a.pop_back();
				b.back() = merge(b.back(), xdxd);
				a.back() = merge(xd.fi, a.back());
				a.push_back(xd.se);

			}
		}
		else
		{
			odp.pb({a.back().dlu, b.back().dlu});
			lit xd = a.back();
			lit xd2 = b.back();
			a.pop_back();
			b.pop_back();
			b.back() = merge(b.back(), xd);
			a.back() = merge(a.back(), xd2);
		}
	}
	dbg(SZ(a), SZ(b));
	if(SZ(a) == 3 && SZ(b) == 1)
	{
		if(a.back().stan == b.back().stan)
		{
			odp.pb({a.back().dlu, 0});
			odp.pb({a[SZ(a) - 2].dlu, a.back().dlu + b.back().dlu});
		}
		else
		{
			odp.pb({a.back().dlu, b.back().dlu});
			odp.pb({a[SZ(a) - 2].dlu + b.back().dlu, a.back().dlu});
		}
	}
	if(SZ(a) == 1 && SZ(b) == 3)
	{
		if(b.back().stan == a.back().stan)
		{
			odp.pb({0, b.back().dlu});
			odp.pb({b.back().dlu + a.back().dlu, b[SZ(b) - 2].dlu});
		}
		else
		{
			odp.pb({a.back().dlu, b.back().dlu});
			odp.pb({b.back().dlu, b[SZ(b) - 2].dlu + a.back().dlu});
		}
	}
	if(SZ(a) == 2 && SZ(b) == 1)
	{
		if(a.back().stan == b.back().stan)
			odp.pb({a.back().dlu, 0});
		else
			odp.pb({a.back().dlu, b.back().dlu});
	}
	if(SZ(b) == 2 && SZ(a) == 1)
	{
		if(a.back().stan == b.back().stan)
			odp.pb({0, b.back().dlu});
		else
			odp.pb({a.back().dlu, b.back().dlu});
	}
	if(f)
		for(auto & s: odp)
			swap(s.fi, s.se);
	return odp;

}

VPII eloelo()
{
	bool f = 0;
	int ile = 0;

	odp.clear();
ile = SZ(a) + SZ(b);
	if((SZ(a) > 2 || SZ(b) > 2) && abs(SZ(a) - SZ(b)) > 2)
	{
		if(SZ(a) < SZ(b))
			f = 1,
			swap(a, b);
		int le = ile / 2 - 1;
		while(le + 1< SZ(a) && a[le].stan == b.back().stan)
			le++;
		int cnt = 0;
//		dbg(le, SZ(a), SZ(b));
//		return 0;
		vector<lit> xd, xd2;
		for(int i = 0 ;i < le;i++)
			xd.pb(a[i]);
		for(int i = 0 ; i < SZ(b) - 1;i++)
			xd2.pb(b[i]);
		int dlu = 0;
		for(int i = le ;i < SZ(a);i++)
			xd2.pb(a[i]),
			dlu += a[i].dlu;
		odp.pb({dlu, b.back().dlu});
		xd.pb(b.back());
		b = xd2;
		a = xd;
		normuj(a);
		normuj(b);

	}
//	return 0;
	while(SZ(a) > 1 && SZ(b) > 1)
	{
		if(SZ(a) == 2 && SZ(b) == 2)
		{
			if(a.back().stan == b.back().stan)
			{
				odp.pb({a.back().dlu, 0});
				odp.pb({a[0].dlu, a.back().dlu + b.back().dlu});
			}
			else
				odp.pb({a.back().dlu, b.back().dlu});
			if(f)
				for(auto & s : odp)
					swap(s.fi, s.se);
			return odp;
		}
		if(a.back().stan == b.back().stan)
		{
			if(SZ(a) >= SZ(b))
			{
				pair<lit, lit> xd = {a[SZ(a) - 2], a.back()};
				odp.pb({xd.fi.dlu + xd.se.dlu, b.back().dlu});
				a.pop_back();
				a.pop_back();
				lit xdxd = b.back();
				b.pop_back();
				a.back() = merge(a.back(), xdxd);
				b.back() = merge(xd.fi, b.back());
				b.push_back(xd.se);
			}
			else
			{
				pair<lit, lit> xd = {b[SZ(b) - 2], b.back()};
				odp.pb({a.back().dlu, xd.fi.dlu + xd.se.dlu});
				b.pop_back();
				b.pop_back();
				lit xdxd = a.back();
				a.pop_back();
				b.back() = merge(b.back(), xdxd);
				a.back() = merge(xd.fi, a.back());
				a.push_back(xd.se);

			}
		}
		else
		{
			odp.pb({a.back().dlu, b.back().dlu});
			lit xd = a.back();
			lit xd2 = b.back();
			a.pop_back();
			b.pop_back();
			b.back() = merge(b.back(), xd);
			a.back() = merge(a.back(), xd2);
		}
	}
	dbg(SZ(a), SZ(b));
	if(SZ(a) == 3 && SZ(b) == 1)
	{
		if(a.back().stan == b.back().stan)
		{
			odp.pb({a.back().dlu, 0});
			odp.pb({a[SZ(a) - 2].dlu, a.back().dlu + b.back().dlu});
		}
		else
		{
			odp.pb({a.back().dlu, b.back().dlu});
			odp.pb({a[SZ(a) - 2].dlu + b.back().dlu, a.back().dlu});
		}
	}
	if(SZ(a) == 1 && SZ(b) == 3)
	{
		if(b.back().stan == a.back().stan)
		{
			odp.pb({0, b.back().dlu});
			odp.pb({b.back().dlu + a.back().dlu, b[SZ(b) - 2].dlu});
		}
		else
		{
			odp.pb({a.back().dlu, b.back().dlu});
			odp.pb({b.back().dlu, b[SZ(b) - 2].dlu + a.back().dlu});
		}
	}
	if(SZ(a) == 2 && SZ(b) == 1)
	{
		if(a.back().stan == b.back().stan)
			odp.pb({a.back().dlu, 0});
		else
			odp.pb({a.back().dlu, b.back().dlu});
	}
	if(SZ(b) == 2 && SZ(a) == 1)
	{
		if(a.back().stan == b.back().stan)
			odp.pb({0, b.back().dlu});
		else
			odp.pb({a.back().dlu, b.back().dlu});
	}
	if(f)
		for(auto & s: odp)
			swap(s.fi, s.se);
	return odp;
}

int main()
{
	_upgrade
	string s;
	cin>>s;
	#ifdef LOCAL
		cout<<s<<endl;
	#endif
	char poprz = 0;
	int ile = 0;
	for(auto v : s)
	{
		if(v == poprz)
			ile++;
		else
		{
			if(ile > 0)
				a.pb(lit(poprz - 'a', ile));
			poprz = v;
			ile = 1;
		}
	}
	a.pb(lit(poprz - 'a', ile));
	cin>>s;
	#ifdef LOCAL
		cout<<s<<endl;
	#endif
	ile = 0;
	poprz = 'a';
	for(auto v : s)
	{
		if(v == poprz)
			ile++;
		else
		{
			if(ile > 0)
				b.pb(lit(poprz - 'a', ile));
			poprz = v;
			ile = 1;
		}
	}
	b.pb(lit(poprz - 'a', ile));
	reverse(all(a));
	reverse(all(b));
	A = a;
	B = b;
	VPII x = elo();
	a = A;
	b = B;
	VPII y = eloelo();
	cout<<min(SZ(x), SZ(y))<<"\n";
	if(SZ(x) < SZ(y))
		for(auto s : x)
			cout<<s.fi<<" "<<s.se<<"\n";
	else
		for(auto s : y)
			cout<<s.fi<<" "<<s.se<<"\n";

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int Pre[501];
inline int Count(string&s,int&n) {
	Pre[0] = (s[0] == ')' ? -1 : 1);
	int minn = Pre[0];
	for (int i = 1; i < n; i++) {
		Pre[i] = (Pre[i - 1] + (s[i] == ')' ? -1 : 1));
		if (minn > (Pre[i])) {
			minn = Pre[i];
		}
	}
	int Ans = 0;
	for (int i = 0; i < n; i++) {
		if (Pre[i] == minn) {
			Ans++;
		}
	}
	return Ans;
}
bool check(const string&s,const int&n) {
	int a = 0, b = 0;
	if (n & 1) {
		return false;
	}
	for (auto it = s.cbegin(); it != s.cend(); ++it) {
		(*it) == '(' ? ++a : ++b;
	}
	return a == b;
}
int main() {
	int n;
	string s;
	cin >> n >> s;
	if (!check(s, n)) {
		cout << "0\n1 1\n" << endl;
		return 0;
	}
	int Ans = 0, x = 0, y = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = i; j < n; ++j) {
			swap(s[i], s[j]);
			int temp = Count(s, n);
			if (Ans <= temp) {
				Ans = temp;
				x = i, y = j;
			}
			swap(s[i], s[j]);
		}
	}
	cout << Ans << endl
		<< x + 1 << ' ' << y + 1 << endl;
	//system("pause");
}


/// Zhupengfei 2018
/// Hereby declare: this program is in public domain.

/// Solution for problem: CF169D

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

const int N=400001;
const int H=177;

string s, r;
ull prod[N+101]= {};
#define hash alseaera_hash
ull hash[N]= {};
ull rhash[N]= {};

bool check(int e, int d) {
    e++;
    int start = e-d+1;
    if (start<0) return true;
    if ((rhash[e] - rhash[start-1] * prod[d])*prod[start-1] != hash[e] - hash[start-1])
        return true;
    return false;
}

int d;

bool dfs(int x, bool t) {
    if(x==s.size()) {
        cout<<r<<endl;
        return true;
    }
    r[x]=t?s[x]:'a';
    while(r[x]<='z') {
        hash[x+1]=hash[x]+r[x]*prod[x];
        rhash[x+1]=rhash[x]*H+r[x];
        if(check(x, d) && check(x, d+1) && dfs(x+1, t&&(r[x]==s[x]))) {
            return true;
        }
        r[x]++;
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>d>>s;
    r = s;
    int i;
    for(i=s.size()-1; i>=0 && s[i]=='z'; i--) s[i]='a';
    if(i<0) {
        cout<<"Impossible"<<endl;
        return 0;
    }
    s[i]++;
    prod[0]=1;
    for(int i=1; i<=s.size()+100; i++) {
        prod[i]=H*prod[i-1];
    }
    if (!dfs(0, 1)) {
        cout<<"Impossible"<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <math.h>

using namespace std;

int main(){
    int n,m=0;
    cin>>n;
    double c;
    for(int i=3;i<n;i++){
        for(int j=i+1;j<n;j++){
            c=sqrt((i*i)+(j*j));
            if(int(c*c)>n*n){
                break;
            }
            if(c-int(c)==0){
                m++;
            }
        }
    }
    cout<<m;
    return 0;
}


#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ms(x, y) memset(x, y, sizeof(x))
using ll = long long int;
using ld = long double;
using namespace std;

int main(){
    fast;
    int t=1;
    //cin>>t;
    while(t--){
        int n;
        cin>>n;
        int total_inches = n/3, rem_cm = n%3;
        int feet = total_inches/12, rem_inches = total_inches%12;
        if(rem_cm==2) rem_inches++;
        if(rem_inches==12){
            rem_inches = 0;
            feet++;
        }
        cout<<feet<<" "<<rem_inches;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define max(a,b) (a > b ? a : b)
#define min(a,b) (a < b ? a : b)

int y,b,r;

int main() {
  cin >> y >> b >> r;
  int orn= y;
  orn = min(orn, b - 1);
  orn = min(orn, r - 2);
  cout << orn * 3 + 3;

  return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define tc ll t; cin>>t; while(t--)
using namespace std;
int main()
{
    ll n; cin>>n;
    ll a[n]; for(ll i=0;i<n;i++) cin>>a[i];
    if(n==1)
    {
        cout<<"YES";
        return 0;
    }
    ll chk1=-1,chk2=-1,ind_chk=-1;
    for(ll i=0;i<n-1;i++)
    {
        if(a[i]==a[i+1])
        {
            chk1=a[i];
            ind_chk=i+1;
            break;
        }
    }
    for(ll i=ind_chk;i<n-1;i++)
    {
        if(a[i]==a[i+1])
            chk2=a[i];
    }
    if(chk1!=-1 && chk2!=-1 && chk1!=chk2)
    {
        cout<<"NO";
        return 0;
    }
    ll c1=0;
    for(ll i=0;i<n-1;i++)
    {
        if(a[i]>a[i+1])
            c1++;
    }
    if(c1!=0 && chk1!=-1 && chk1==chk2 && chk1==a[n-1])
    {
        cout<<"NO";
        return 0;
    }
    if(c1==0)
    {
        if(a[n-2]!=a[n-1] && chk1!=-1)
        {
            cout<<"NO";
            return 0;
        }
        else
        {
            cout<<"YES";
            return 0;
        }
    }
    ll c2=0;
    for(ll i=0;i<n-1;i++)
    {
        if(a[i]<a[i+1])
            c2++;
    }
    if(c2!=0 && chk1!=-1 && chk1==chk2 && chk1==a[0])
    {
        cout<<"NO";
        return 0;
    }
    if(c2==0)
    {
        if(a[0]!=a[1] && chk1!=-1)
        {
            cout<<"NO";
            return 0;
        }
        else
        {
            cout<<"YES";
            return 0;
        }
    }
    ll index1=0,index2=n-1;
    if(chk1==-1)
    {for(ll i=0;i<n-1;i++)
    {
        if(a[i]>a[i+1])
        {
            index1=i;
            break;
        }
    }
    for(ll i=n-1;i>0;i--)
    {
        if(a[i]>a[i-1])
        {
            index2=i;
            break;
        }
    }
    if(index1!=index2)
    {
        cout<<"NO";
        return 0;
    }
    else
    {
        cout<<"YES";
        return 0;
    }}

    index1=0,index2=n-1;
    for(ll i=0;i<n-1;i++)
    {
        if(a[i]>=a[i+1])
        {
            index1=i;
            break;
        }
    }
    for(ll i=n-1;i>0;i--)
    {
        if(a[i]>=a[i-1])
        {
            index2=i;
            break;
        }
    }
    for(ll i=index1;i<=index2-1;i++)
    {
        if(a[i]!=a[i+1])
        {
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES";
}


#include<bits/stdc++.h>

using namespace std;

int MOD = 1000000007;
int n, a, b, k;
int dp[5100][5100];

/*
long long run(int me, int how) {
	if (dp[me][how]) {
		return dp[me][how];
	}
	if (how == k) {
		return dp[me][how] = 1;
	}
	long long ans = 0;
	for (int i = 1; i < abs(me - b); ++i) {
		if (1 <= me + i && me + i <= n) {
			ans += run(me + i, how + 1);
			ans %= MOD;
		}
		if (1 <= me - i && me - i <= n) {
		    ans += run(me - i, how + 1);
			ans %= MOD;
		}
	}

	return dp[me][how] = ans;
}*/

int main() {
	cin >> n >> a >> b >> k;
	dp[0][a] = 1;
	dp[0][a + 1] = -1;

	for (int i = 0; i <= k; ++i) {
		int now = 0;

		for (int j = 1; j <= n; ++j) {
			now += dp[i][j];
			now %= MOD;
			//cout << dp[i][j] <<" ";
			int diff;
			if (j < b) {
				diff = b - j;
				if (diff == 1) {
					continue;
			    }
				dp[i + 1][max(1, j - diff + 1)] += now;
				dp[i + 1][max(1, j - diff + 1)] %= MOD;

				dp[i + 1][j] -= now;
				dp[i + 1][j] += MOD;
				dp[i + 1][j] %= MOD;

				dp[i + 1][j + 1] += now;
				dp[i + 1][j + 1] %= MOD;

				dp[i + 1][b] -= now;
				dp[i + 1][b] += MOD;
				dp[i + 1][b] %= MOD;
			}
			else {
			    diff = j - b;
			    if (diff == 1) {
					continue;
			    }
			    dp[i + 1][b + 1] += now;
			    dp[i + 1][b + 1] %= MOD;

			    dp[i + 1][j] -= now;
			    dp[i + 1][j] += MOD;
			    dp[i + 1][j] %= MOD;

			    dp[i + 1][j + 1] += now;
			    dp[i + 1][j + 1] %= MOD;

			    dp[i + 1][min(n + 1, j + diff)] -= now;
			    dp[i + 1][min(n + 1, j + diff)] += MOD;
			    dp[i + 1][min(n + 1, j + diff)] %= MOD;
			}
		}
		//cout << endl;
	}

	int sum = 0, now = 0;
	for (int i = 1; i <= n; ++i) {
		now += dp[k][i];
		now %= MOD;
		sum += now;
		sum %= MOD;
	}
	cout << sum;
}