#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<numeric>
#include<sstream>
#include<string>
#include<utility>
#include<map>
#include<queue>
#include<set>
#include<vector>

using namespace std;
#define sfd(n) scanf("%d", &n)
#define sfd2(n, m) scanf("%d %d", &n, &m)
#define sfs(s) scanf("%s", s);
#define sflld(n) scanf("%lld", &n)
#define sflf(n) scanf("%lf", &n)
#define ll long long
#define maxn 9
string line1 = "WBWBWBWB", line2 = "BWBWBWBW";
string str, str2;
bool check(string line) {
	if (str != line) {
		for (int j=0; j<8; j++) {
			str = str2.substr(j, 8);
			if (str == line) {
				return true;
				break;
			}
		}
		return false;
	}
	return true;
}
int main() {
	//#ifdef ONLINE_JUDGE
	//#else
	//	freopen("D:/in.txt", "r", stdin);
	//	freopen("D:/out.txt", "w", stdout);
	//#endif

	bool ans = true;
	for (int i=0; i<8; i++) {
		cin >> str;
		str2 = str+str;
		if (i%2==0) {
			if (!check(line1)) ans = false;
		} else {
			if (!check(line2)) ans = false;
		}
 	}
	if (ans) cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f

#define mod 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

#define eps 1e-8

#define inf 1e100

#define squ(a,b) (a)*(a)+(b)*(b)

#define pi 3.1415926535897932384626433832795

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

const int C=1000010;

int sz[44],SZ[44];

int b[C];

void solve_C(){

	int n,m,e;gn(n,m);

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

		gn(e);

		for(int j=0; j<30; j++) if(e&(1<<j)) SZ[j]++;

	}

	for(int i=0; i<m; i++) gn(b[i]);

	sort(b,b+m);

	int st=0,ed=m+1;

	while(st+1<ed){

		int md=st+ed>>1, can=1;

		for(int i=0; i<30; i++) sz[i]=SZ[i];

		for(int i=md-1; i>=0; i--){

			if(!can) break;

			int flag=0;

			for(int j=b[i]; j<30; j++) if(sz[j]){

				sz[j]--;

				for(int k=j-1; k>=b[i]; k--) sz[k]++;

				flag=1;

				break;

			}

			can=flag;

		}

		if(can) st=md;

		else ed=md;

	}

	println(st);

	exit(0);

}

int main(){

#ifndef ONLINE_JUDGE

	freopen("in.in","r",stdin);

	freopen("out.out","w",stdout);

#endif

	solve_C();

	return 0;

}


//test
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define max(a, b) a > b ? a : b
//定义数组大小为4，从一开始，空出下标为0，方便计算
int x[4];                  //三个人的位置
int l[4];                  //三个人的机动性（可移动距离）
int t[4];                  //三个人的抛的距离
int ans = 0;               //经过操作后的最远距离，初始化为0
int w[4];                  //初始化为0，0表示可以进行操作，非零表示不可以
int p[4];                  //初始化为0，表示a[i]所举起的人
int a[4] = {3, 3, 3, 3};   //初始化为3，表人的状态，这里a对应的二进制为0011，后三位分别是三个动作：抛出，举起，移动。0（无意义）0（不可抛出）1（未进行举起）1（未进行移动）。这道题中，a只有六个可能值：0(0000)、1(0001)、2(0010)、3(0011)、4(0100)、5(0101)，表示人的六种状态
//bool类型
int near(int s)
{
    int i = 1;
    for (; i <= 3; i++)
    {
        if (s == x[i] + 1 || s == x[i] - 1)
        {
            return TRUE;
        }
    }
    return FALSE;
}
//dfs深度遍历
void dfs(int d)
{
    int i = 1, j = 1, e = 0;
    //每次都取最远（大）的位置
    for (; i <= 3; i++)
    {
        ans = max(ans, x[i]);
    }
    for (i = 1; i <= 3; i++)
    {
        //是否可以进行操作
        if (w[i])
        {
            continue;
        }
        //a[i] == 1 || a[i] == 3（未进行移动且不可抛出）
        if ((a[i] & 1) && !(a[i] & 4))
        {
            for (j = 1; j <= l[i]; j++)                         //移动
            {
                x[i] += j;                                      //a[i]向前移动j
                a[i] ^= 1;                                      //已移动
                if (near(x[i]) || j == l[i])                    //如果a[i]移动后的位置旁边有人或者移动距离达到上限
                {
                    dfs(d + 1);
                }
                x[i] -= j;                                      //归位
                x[i] -= j;                                      //a[i]向后移动j
                if (near(x[i]) || j == l[i])                    //如果a[i]移动后的位置旁边有人或者移动距离达到上限
                {
                    dfs(d + 1);
                }
                x[i] += j;                                      //归位
                a[i] ^= 1;                                      //还原为未移动
            }
        }
        //a[i] == 2 || a[i] == 3 || a[i] == 5（未进行举起）
        if (a[i] & 2)
        {
            for (j = 1; j <= 3; j++)                            //举起
            {
                if (i != j && !w[j] && t[i] > 0)                //是否可以进行操作
                {
                    if (x[i] == x[j] + 1 || x[j] == x[i] + 1)   //a[i]附近是否有人
                    {
                        w[j] = 1;                               //即将举起（抛出）j，抛出前将j是否可操作标记变更为否
                        a[i] ^= 2;                              //已举起
                        a[i] ^= 4;                              //可抛出
                        p[i] = j;                               //记录a[i]举起（抛出）了j
                        e = x[j];                               //记录a[j]的举起前位置
                        x[j] = -j;                              //a[j]（被举起）的位置定为负数，只作用于下一层递归时的取最远位置的循环
                        dfs(d + 1);
                        x[j] = e;                               //归位
                        w[j] = 0;                               //还原为可以进行操作
                        a[i] ^= 2;                              //还原为未举起
                        a[i] ^= 4;                              //还原为不可抛出
                    }
                }
            }
        }
        //a[i] == 4 || a[i] == 5（可抛出）
        if (a[i] & 4)
        {
            for (j = 1; j <= t[i]; j++)                         //抛出
            {
                w[p[i]] = 0;                                    //变更a[j]为可操作（以下a[j]指a[i]所举起的人）
                a[i] ^= 4;                                      //不可抛出
                e = x[p[i]];                                    //记录a[j]被举起前位置
                x[p[i]] = x[i] + j;                             //抛出a[j]，并更新a[j]位置
                if (near(x[p[i]]) || j == t[i])                 //如果a[j]被抛出后的位置旁边有人或者抛出距离达到上限
                {
                    dfs(d + 1);
                }
                x[p[i]] -= j;                                   //归位
                x[p[i]] -= j;                                   //a[j]向后抛出j
                if (near(x[p[i]]) || j == t[i])                 //如果a[j]被抛出后的位置旁边有人或者抛出距离达到上限
                {
                    dfs(d + 1);
                }
                x[p[i]] = e;                                    //还原a[j]为未举起前的位置
                a[i] ^= 4;                                      //还原a[j]为可抛出
                w[p[i]] = 1;                                    //还原a[j]为不可操作
            }
        }
    }
    return ;
}

int main()
{
    int i = 1;
    //键入每个人的信息
    for (; i <= 3; i++)
    {
        scanf("%d %d %d", &x[i], &l[i], &t[i]);
    }
    //深度优先遍历
    dfs(1);
    //输出最远距离
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int main()
{
    int k,p,i,j,l,r,m,f[1000]= {0};
    char s[1000]= {0};
    while(~scanf("%s%d",s,&k))
    {
        memset(f,0,sizeof(f));
        int n=strlen(s);
        l=0;
        r=0;
        for(i=0; i<n; i++)
        {
            if(s[i]=='?')
            {
                f[i]=1;
                l++;
            }
            if(s[i]=='*')
            {
                f[i]=2;
                r++;
            }
        }
        p=n-l-r;
        if(k==p)
        {
            for(i=0; i<n; i++)
                if(f[i]==0)
                    printf("%c",s[i]);
        }
        else if(k>p)
        {
            if(r>0)
            {
                int flag=0;
                for(i=0; i<n; i++)
                {
                    if(f[i]==0)
                        printf("%c",s[i]);
                    else if(f[i]==2&&flag==0)
                    {
                        for(j=0; j<k-p; j++)
                            printf("%c",s[i-1]);
                        flag=1;
                    }
                }
            }
            else
                printf("Impossible\n");
        }
        else
        {
            if(p-k<=l+r)
            {
                int m=0;
                for(i=0; i<n; i++)
                {
                    if(f[i]==0&&f[i+1]==0)
                        printf("%c",s[i]);
                    else if(f[i+1]!=0&&m<p-k)
                        m++;
                    else if(f[i+1]!=0&&m>=p-k)
                        printf("%c",s[i]);
                }
            }
            else
                printf("Impossible\n");
        }
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long m=0,n=0,p=0;
    string s;cin>>s;m=count(s.begin(),s.end(),'a');n=s.length();p=n-m;
    if(p==m){cout<<m+(p-1);}else if(p>m){cout<<m+(m-1);}if(p<m){cout<<n;}
    return 0;
}


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

using namespace std;

const int maxn = 2005;

int n;
vector <int> a[maxn];
int dp[2010][2010][2];

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	scanf("%d", &n);

	for (int i = 0; i < n; ++i) {
		long long k, st, x, y, m;
		scanf("%I64d%I64d%I64d%I64d%I64d", &k, &st, &x, &y, &m);
		a[i].resize(k);
		a[i][0] = st;
		for (int j = 1; j < k; ++j) {
			a[i][j] = ((long long)a[i][j - 1] * x + y) % m;
		}
	}

	int result = 0;
	vector <pair <int, int> > ans;

	int p1 = 0, p2 = 0;
	int prev = -1;
	while (!(p1 == a[0].size() && p2 == a[1].size())) {
		if (p1 == a[0].size()) {
			ans.push_back(make_pair(a[1][p2], 2));
			prev = a[1][p2];
			++p2;
		} else
		if (p2 == a[1].size()) {
			ans.push_back(make_pair(a[0][p1], 1));
			prev = a[0][p1];
			++p1;
		} else {
			if (a[0][p1] < a[1][p2]) {
				if (prev <= a[0][p1]) {
					ans.push_back(make_pair(a[0][p1], 1));
					prev = a[0][p1];
					++p1;
				} else
				if (prev <= a[1][p2]){
					ans.push_back(make_pair(a[1][p2], 2));
					prev = a[1][p2];
					++p2;
				} else {
					ans.push_back(make_pair(a[0][p1], 1));
					prev = a[0][p1];
					++p1;
				}
			} else
			if (a[1][p2] < a[0][p1]) {
				if (prev <= a[1][p2]) {
					ans.push_back(make_pair(a[1][p2], 2));
					prev = a[1][p2];
					++p2;
				} else
				if (prev <= a[0][p1]) {
					ans.push_back(make_pair(a[0][p1], 1));
					prev = a[0][p1];
					++p1;
				} else {
					ans.push_back(make_pair(a[1][p2], 2));
					prev = a[1][p2];
					++p2;
				}
			} else {
				ans.push_back(make_pair(a[0][p1], 1));
				ans.push_back(make_pair(a[1][p2], 2));
				++p1;
				++p2;
			}
		}
	}

	for (int i = 1; i < ans.size(); ++i) {
		if (ans[i].first < ans[i - 1].first) {
			++result;
		}
	}

	printf("%d\n", result);
	for (int i = 0; i < ans.size(); ++i) {
		printf("%d %d\n", ans[i].first, ans[i].second);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int b, m;
long long solve(long long ansy) {
	long long ansx = (b - ansy) * m;
	if(ansx < 0)
		return -1;
	long long ret = 0;
	long long A = ansy * (ansy + 1) / 2;
	long long B = ansx * (ansx + 1) / 2;
	ret = A * (ansx+1) + B * (ansy+1);
	//cout << ret << endl;
	return ret;
}
int main() {

	scanf("%d%d", &m, &b);
	long long ans = 0;
	for(long long  i = 0; ;i++){
		long long ret = solve(i);
		if(ret == -1)
			break;
		ans = max(ret,ans);

	}

	cout << ans << endl;;
	return 0;
}


#include <bits/stdtr1c++.h>

#define MAX 1000010
#define MOD 1000000007

using namespace std;

namespace lgr{
    short factor[MAX];
    int S[MAX], ar[MAX], inv[MAX];

    inline int expo(int a, int b){
        int res = 1;

        while (b){
            if (b & 1) res = (long long)res * a % MOD;
            a = (long long)a * a % MOD;
            b >>= 1;
        }
        return res;
    }

    int lagrange(long long n, int k){
        if (!k) return (n % MOD);

        int i, j, x, y, res = 0;
        if (!inv[0]){
            inv[MAX - 1] = expo(5139854, MOD - 2);
            for (i = MAX - 2; i >= 0; i--) inv[i] = ((long long)inv[i + 1] * (i + 1)) % MOD;

            for (i = 0; i < MAX; i++) factor[i] = 0;
            for (i = 4; i < MAX; i += 2) factor[i] = 2;
            for (i = 3; (i * i) < MAX; i += 2){
                if (!factor[i]){
                    for (j = (i * i), x = i << 1; j < MAX; j += x){
                        factor[j] = i;
                    }
                }
            }
        }

        k++;
        for (ar[1] = 1, ar[0] = 0, i = 2; i <= k; i++){
            if (!factor[i]) ar[i] = expo(i, k - 1);
            else ar[i] = ((long long)ar[factor[i]] * ar[i / factor[i]]) % MOD;
        }

        for (i = 1; i <= k; i++){
            ar[i] += ar[i - 1];
            if (ar[i] >= MOD) ar[i] -= MOD;
        }
        if (n <= k) return ar[n];

        for (S[k] = 1, i = k - 1; i >= 0; i--) S[i] = ((long long)S[i + 1] * ((n - i - 1) % MOD)) % MOD;
        for (i = 0, y = 1; i <= k; i++){
            x = (long long)ar[i] * y % MOD * S[i] % MOD * inv[k - i] % MOD * inv[i] % MOD;
            if ((k - i) & 1){
                res -= x;
                if (res < 0) res += MOD;
            }
            else{
                res += x;
                if (res >= MOD) res -= MOD;
            }
            y = ((long long)y * ((n - i) % MOD)) % MOD;
        }

        return (res % MOD);
    }
}

int main(){
    int k;
    long long n;
    while (scanf("%lld %d", &n, &k) != EOF){
        printf("%d\n", lgr::lagrange(n, k));
    }
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include<map>
#include<list>
#include<queue>
#include<utility>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<math.h>
#include<set>
#include<stack>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<iterator>
using namespace std;
long long  dp[120][4][4],n,mod=1000000007;
string str;
long long dp_func(int pos,int choto,int boro)
{
    if(pos==n){
        if(choto==0||boro==0)return 0;
        return 1;
    }
    if(dp[pos][boro][choto]!=-1)return dp[pos][boro][choto];
    int p=str[pos]-48;
    long long ret=0;
    if(choto==0){
        if(boro==1){
            ret+=dp_func(pos+1,0,1)*2;
            ret+=dp_func(pos+1,1,1);
            return dp[pos][choto][boro]=ret%mod;
        }
        int l=p^0;
        int k=p^1;
      //  if(pos)
        ret+=dp_func(pos+1,0,0)*2;
        if(k<l)ret+=dp_func(pos+1,1,1);
         return dp[pos][choto][boro]=ret%mod;
    }
    if(boro==1){
        ret+=dp_func(pos+1,1,1)*4;
         return dp[pos][choto][boro]=ret%mod;
    }

}
int main()
{

    int i,j,k,l,m;
    cin>>str;
    n=str.size();
    memset(dp,-1,sizeof dp);
    long long ans=dp_func(0,0,0);
   cout<<ans<<endl;

}


#include <bits/stdc++.h>
#define rep(i,s,t) for(int i = s;i <= t;i ++)
#define dow(i,t,s) for(int i = t;i >= s;i --)
#define REP(i,x) for(int i = 0;i < x.size();i ++)
#define DOW(i,x) for(int i = (int)s.size();i >= 0;i --)

#define fi first
#define se second

#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound

#define eps 1e-6
#define N 100010
#define INF

#define SZ(x) ((int)(x).size())

using namespace std;

typedef vector<int> VI;
typedef long long LL;
typedef double db;
typedef pair<int,int> PII;

int fac[N] , inv[N] , num[N] , mod , A[N];

LL a[50] , m[50];
int n , l , r , tot , p , P , mul;

int qpow(int a , int b)
{
	int ret = 1;
	while(b)
	{
		if(b & 1)ret = 1ll * ret * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return ret;
}

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    LL d = a;
    if(b)
    {
        d = exgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }
    else x = 1 , y = 0;
    return d;
}
LL CRT(LL *a , LL *m , int k)
{
    LL M = 1;
    LL ans = 0;
    rep(i,1,k)M *= m[i];
    rep(i,1,k)
    {
        LL x , y , Mi = M / m[i];
        exgcd(Mi , m[i] , x , y);
        ans = (ans + Mi * x * a[i]) % M;
    }
    if(ans < 0)ans += M;
    return ans;
}
int C(int n , int m)
{
    if(m < 0 || m > n)return 0;
	int res = num[n] - num[m] - num[n - m];
	return 1ll * fac[n] * inv[n - m] % mod * inv[m] % mod * qpow(p , res) % mod;
}
int calc() // 0
{
    fac[0] = 1;
    rep(i,1,n)A[i] = i;
    rep(i,1,n)
    {
    	num[i] = num[i - 1];
    	while(A[i] % p == 0)
    	{
    		num[i] ++;
    		A[i] /= p;
    	}
    	fac[i] = 1ll * fac[i - 1] * A[i] % mod;
    }
    inv[n] = qpow(fac[n] , (mod > p) ? mod - mod / p - 1 : p - 2);
    dow(i,n - 1,0)inv[i] = 1ll * inv[i + 1] * A[i + 1] % mod;

    int ret = 0;
    rep(i,0,n)
    {
        ret += 1ll * C(n , i) * (C(n - i , (l + n - i + 1) / 2) - C(n - i , (r + n - i) / 2 + 1)) % mod;
        if(ret >= mod)ret -= mod;
        if(ret < 0)ret += mod;
    }
    return ret;
}
int main()
{
    //freopen("in.in","r",stdin);
    //freopen("out.out","w",stdout);
    scanf("%d%d%d%d",&n,&P,&l,&r);
    if(p == 1)return printf("0") , 0;
    for(int i = 2;i * i <= P;i ++)
    {
        if(P % i == 0)
        {
        	mul = 1;
        	tot ++;

        	while(P % i == 0)
        	{
        		P /= i;
        		mul *= i;
        	}
            m[tot] = mul;
            mod = mul , p = i;

        	a[tot] = calc();
        }
    }
    if(P != 1)
    {
        tot ++;
        m[tot] = P;
        mod = P , p = P;
        a[tot] = calc();
    }
    int ans = CRT(a , m , tot);
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll maxn=1e11;
const ll maxp=sqrt(maxn)+10;
ll f[maxp],g[maxp];

ll solve(ll n)
{
    ll i,j,m;
    for (m=1;m*m<=n;m++) f[m]=n/m-1;
    for (i=1;i<=m;i++) g[i]=i-1;
    for (i=2;i<=m;i++)
    {
        if (g[i]==g[i-1]) continue;
        for (j=1;j<=min(m-1,n/i/i);j++)
            if (i*j<m) f[j]-=f[i*j]-g[i-1]; else f[j]-=g[n/i/j]-g[i-1];
        for (j=m;j>=i*i;j--) g[j]-=g[j/i]-g[i-1];
    }
    ll res=0;
    for (i=2;i<m;i++)
    {
        if (g[i]==g[i-1]) continue;
        res+=f[i]-g[i];
    }
    j=(int)pow(n,1.0/3);
    return res+g[j];
}

int main()
{
    ll n;
    scanf("%lld",&n);
    cout << solve(n) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
int mp[105][105];
int n, m, x, y;
LL c, k;
LL res1, res2, res3;
int main()
{
    scanf("%d%d%I64d%d%d", &n, &m, &k, &x, &y);
    if (n == 1)
    {
        LL a = k/m;
        k %= m;
        if (k == 0) res1 = res2 = res3 = a;
        else
        {
            res1 = a+1;
            res2 = a;
            res3 = a;
            if (y <= k) res3++;
        }
    }
    else
    {
        memset(mp, 0, sizeof(mp));
        c = k / ((2*n-2) * m);
        k %= (2*n-2)*m;
        int r = 1;
        while (k > 0)
        {
            if (r == 1)
            {
                for (r; r < n && k; r++)
                {
                    for (int j = 1; j <= m && k; j++)
                    {
                        ++mp[r][j];
                        k--;
                    }
                }
            }
            else if (r == n)
            {
                for (r; r > 1 && k; r--)
                {
                    for (int j = 1; j <= m && k; j++)
                    {
                        ++mp[r][j];
                        k--;
                    }
                }
            }
          //  cout << r << " " << k << endl;
        }
        res1 = 0, res2 = mp[1][1] + c;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                if (i == 1 || i == n)
                    res1 = max(res1, mp[i][j] + c), res2 = min(res2, mp[i][j] + c);
                else
                    res1 = max(res1, mp[i][j] + 2*c), res2 = min(res2, mp[i][j] + 2*c);
            }
        if (x == 1 || x == n)
            res3 = c + mp[x][y];
        else
            res3 = 2*c + mp[x][y];
    }
    printf("%I64d %I64d %I64d\n", res1, res2, res3);
}


    #include <bits/stdc++.h>

    using namespace std;

    #define pb push_back
    #define mp make_pair
    #define forn(i, n) for (int i = 0; i < (int)(n); ++i)
    typedef long long LL;
    typedef pair<int, int> PII;

    const int DEBUG = 0;
    mt19937 mt(123);

    unordered_map<bitset<87>, int > ma[19];
    LL table[400000][10];
    int tcnt = 0;

    inline bitset<87> tra(bitset<87> b, int dig) {
        bitset<87> res = (b << dig) | (b >> dig);
        forn(i, dig) if (b.test(i)) {
            res.set(dig - i);
        }
        return res;
    }

    int n;
    LL from, to;
    int k;
    vector<LL> ans;

    int solve(bitset<87> b, int rem) {
        auto it = ma[rem].find(b);
        if (it != ma[rem].end()) {
            return it->second;
        }
        int ind = tcnt++;
        if (rem == 0) {
            int cur = 0;
            forn(i, 10) {
                cur |= b.test(i);
                table[ind][i] = cur;
            }
        } else {
            forn(i, 10) {
                int cur = solve(tra(b, i), rem - 1);
                forn(j, 10) {
                    table[ind][j] += table[cur][j];
                }
            }
        }
        return ma[rem][b] = ind;
    }

    LL solve(LL bound, int k) {
        if (bound == 0) {
            return 1;
        }
        vector<int> dig;
        while (bound > 0) {
            dig.pb(bound % 10);
            bound /= 10;
        }
        reverse(dig.begin(), dig.end());
        int sz = dig.size();
        bitset<87> b;
        b.reset();
        b.set(0);
        LL ans = 0;
        forn(i, sz) {
            forn(j, dig[i] + (i == sz - 1)) {
                ans += table[solve(tra(b, j), sz - 1 - i)][k];
            }
            b = tra(b, dig[i]);
        }
        return ans;
    }

    LL gen(LL bound) {
        int times = mt() % 60 + 1;
        LL res = bound;
        forn(i, times) {
            res = mt() % bound + 1;
        }
        return res;
    }

    int main() {
        if (DEBUG) {
            n = 50000;
        } else {
            scanf("%d", &n);
        }
        forn(i, n) {
            if (DEBUG) {
                from = gen(LL(1e18));
                to = gen(LL(1e18));
                if (from > to) {
                    swap(from, to);
                }
                k = mt() % 10;
            } else {
                scanf("%I64d%I64d%d", &from, &to, &k);
            }
            ans.pb(solve(to, k) - solve(from - 1, k));
        }
        for (LL x : ans) printf("%I64d\n", x);
        return 0;
    }


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char * q="qwertyuiopasdfghjkl;zxcvbnm,./";
	char ch,s[105],*p;
	int i;
	cin>>ch>>s;
	if(ch=='R')
		for(i=0;i<strlen(s);i++)
		{
			p=strchr(q,s[i]);
			cout<<*--p;
		}
	else
		for(i=0;i<strlen(s);i++)
		{
			p=strchr(q,s[i]);
			cout<<*++p;
		}
		cout<<endl;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
long long pw(long long  b, long long  p){ if (!p) return 1; long long  sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }

int main()
{
/*
    int x,ans ;
    string s,c;
    cin>>x>>c>>s;

    if(s=="week")
    {
        if(x==6 || x==5) ans =53;
        else ans = 52;
    }
    if(s=="month")
    {
        if(x==30) ans = 11;
        else if(x==31) ans= 7;
        else ans = 12;
    }

    cout<<ans<<endl;*/

    long long l , r,x,y,a,b;
    cin>>l>>r;
        x=a= l,y=b=r;
 map<long long ,int>mp;

   string s="",q="",p="";
   while (r) {if(r%2==0) s+='0'; else s+='1'; r/=2;}
   while(l) {if(l%2==0) q+='0'; else q+='1'; l/=2; p+='1';}
   long long ans = 0;
   reverse(s.begin() , s.end());
   reverse(q.begin() , q.end());
   int u = 0;
   while(x<=y)
   {
       string m =p;
       long long z = 0;
       for(int j = p.size()-1,k=0 ; j>=0 ; j--,k++)
                z+= (p[j]-'0') * pw(2 , k);
       mp[z]++;
       for(int i = 0 ; i<m.size() ; i++)
       {
           m[i] = '0';
           long long z=0;
           for(int j = m.size()-1,k=0 ; j>=0 ; j--,k++)
                z+= (m[j]-'0') * pw(2 , k);
           if(z<=b && z>=a && mp[z]==0) {ans++;}
           mp[z]++;
           m=p;
       }
       p+='1';
      if(u%2==0) q='0'+q;
      else q[0]='1';
       x=0;
       for(int j = q.size()-1 , k=0 ; j>=0 ; j--,k++) x+=(q[j]-'0') * pw(2 , k);
    u++;
   }

   cout<<ans<<endl;

     return 0;
}


#include<bits/stdc++.h>
#define ll          long long int
#define lld          long double
#define pb          push_back
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define vec         vector<ll>
#define all(a)     (a).begin(),(a).end()
#define F           first
#define S           second
#define mod         1000000007
#define rep(i,a,b)	for(ll  i=a;i<b;i++)
#define repi(i,a,b)	for(ll  i=a;i<=b;i++)
#define per(i,a,b)  for(ll i=a;i>=b;i--)
#define mp          make_pair
#define mit         map<ll,ll>::iterator
#define sit         set<ll>::iterator
#define xy          exit(0);
#define pit         pair<ll,ll>::iterator
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
ll xo(ll x, ll y)
{
   return (x | y) & (~x | ~y);
}
ll bin_Expo(ll x,ll n)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return bin_Expo(x*x,n/2);
    else                             //n is odd
        return x*bin_Expo(x*x,(n-1)/2);
}
ll mod_Expo(ll x,ll n,ll M)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return mod_Expo((x*x)%M,n/2,M);
    else                             //n is odd
        return (x*mod_Expo((x*x)%M,(n-1)/2,M))%M;

}
ll NcR(int n, int r)
{
    ll p = 1, k = 1;
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
            ll m=__gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else
        p = 1;
    return p;
}
bool prime_check(ll x)
{
    bool prime = (x >= 2);
    for (ll i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
        {
            prime = false;
            break;
        }
    }
    return prime;
}
ll logg(ll base,ll x)
{
    return (ll)(log(x)/log(base));
}
string a[40],b[40]; ll n,q;
ll dfs(char x,char p,ll d)
{
	if(d==n)
	return 1;
	ll ans=0;
	rep(i,0,q)
	{
		if(b[i][0]==x)
		ans+=dfs(a[i][0],b[i][0],d+1);
	}
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
    while(TESTS--)
    {
    	ll i,j,x,h,l,k,m;
    	cin>>n>>q;
    	rep(i,0,q)
    	cin>>a[i]>>b[i];
    	cout<<dfs('a','z',1);
    }
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    cin >> str;
    string hh = str.substr(0, 2);
    int h = stoi(hh);
     if(h == 5)
    {
        int x = stoi(str.substr(3, 2));
        if(x >= 50)
        {
            cout << "10:01" << endl;
            return 0;
        }
    }
    if(h >= 6 && h <= 9)
    {
        cout << "10:01" << endl;
        return 0;
    }
    if(h == 15)
    {
        int x = stoi(str.substr(3, 2));
        if(x >= 51)
        {
            cout << "20:02" << endl;
            return 0;
        }
    }
    if(h >= 16 && h <= 19)
    {
        cout << "20:02" << endl;
        return 0;
    }
    string hhReverse = hh;
    reverse(hhReverse.begin(), hhReverse.end());
    int hReverse = stoi(hhReverse);
    int m = stoi(str.substr(3, 2));
    if(hReverse > m)
    {
        cout << hh << ":" << hhReverse << endl;
    }
    else
    {
        if(h == 23)
            h = 0;
        else
            h++;
        if(h < 10)
        {
            hh = "0" + to_string(h);
        }
        else
            hh = to_string(h);
        hhReverse = hh;
        reverse(hhReverse.begin(), hhReverse.end());
        cout << hh << ":" << hhReverse << endl;
    }
    return 0;
}


#include <iostream>
#include <set>
using namespace std;
typedef long long ll;

int main() {
    ll n;
    cin>>n;
    n*=2;
    set<ll> lol;
    for(ll i=1; i*i<=n; ++i) {
        lol.insert(i*(i+1));
    }
    for(ll i=1; i*(i+1)<=n; ++i) {
        ll q=n-i*(i+1);
        if(lol.count(q)) {
            cout<<"YES\n";
            return 0;
        }
    }
    cout<<"NO\n";

return 0;

}


#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

int main()
{
    char s[8][9]={"vaporeon", "jolteon", "flareon", "espeon", "umbreon", "leafeon", "glaceon", "sylveon"};
    bool flag[8];
    int len[8]={8,7,7,6,7,7,7,7};
    int l[8];
    int n;
    char c;
    while(scanf("%d",&n)!=EOF)
    {
        memset(flag,0,sizeof(flag));
        memset(l,0,sizeof(l));
        for(int i = 0; i < n;i++)
        {
            cin>>c;
            for(int j = 0; j < 8; j++)
            {
                if(flag[j]) continue;
                if(n != len[j]){
                    flag[j] = 1;
                    continue;
                }
                if(c == '.') continue;
                if(c != s[j][i]) flag[j] = 1;
                else l[j]++;
            }
        }
        int ans,maxl=-1;
        for(int i = 0; i < 8;i++)
        {
            if(!flag[i]){
                //cout<<s[i]<<endl;
                if(l[i] > maxl){
                    maxl =l[i];
                    ans = i;
                }
            }
        }
        cout<<s[ans]<<endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <tuple>
#include <stdint.h>
#include <assert.h>
#include <emmintrin.h>
#include <bitset>
#include <numeric>
#include <ctime>

#define forn(i,n) for(int i=0;i<n;++i)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define mp make_pair

using namespace std;

int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<int>v(n);
	vector<int>c(n, -1);
	forn(i, n) {
		cin >> v[i];
	}
	sort(v.begin(), v.end());
	int col = 0;
	forn(i, n) {
		if (c[i] == -1) {
			c[i] = col;
			forn(j, n) {
				if (v[j] % v[i] == 0)c[j] = col;
			}
			col++;
		}
	}
	cout << col;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;

typedef vector<int> vi;
const int MAX = 100001;

vll cubes;

typedef pair<int, ll> pill;
typedef map<ll, pill> mdp;

mdp dp;

pill solve(ll m)
{
	if (m < 8)
	{
		return make_pair((int)m, m);
	}

	mdp::iterator it = dp.find(m);
	if (it != dp.end())
	{
		return it->second;
	}

	vll::iterator largest = upper_bound(cubes.begin(), cubes.end(), m);
	--largest;

	pill v1 = solve(*largest - 1);

	pill v2 = solve(m - *largest);
	++v2.first;
	v2.second += *largest;

	return dp[m] = max(v1, v2);
	//cout << "dp [" << m << "] = " << dp[m].first << ":" << dp[m].second << endl;
}

int main(int argc, char* argv[])
{
	ll m;
	cin >> m;

	for (int i = 1; ; ++i)
	{
		ll curr = (ll)i * i * i;
		cubes.push_back(curr);
		if (curr > m)
			break;
	}

	pill res = solve(m);
	cout << res.first << " " << res.second << endl;

	return 0;
}


#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <complex>

using namespace std;
typedef complex<double> P;

#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )

int main(){

  int x,y,sumx,sumy,sum,f=1,cnt,now;

  cin>>x>>y;

  sumx=x*100;
  sumy=10*y;
  sum=sumx+sumy;
  while(sum>=220){

    if(sum>=220){

  if(x>=2&&y>=2){
    x-=2;
    y-=2;
  }else if(x>=1&&y>=12){
    x-=1;
    y-=12;
  }else if(y>=22){
    y-=22;
  }
  else {cout<<"Hanako";return 0;}
    sum-=220;
    }
  if(sum>=220){

  if(y>=22){
    y-=22;
  }else if(y>=12&&x>=1){
    x-=1;
    y-=12;
  }else if(x>=2&&y>=2){
    x-=2;
    y-=2;
  }else {cout<<"Ciel";return 0;}
  sum-=220;
  }else {cout<<"Ciel";return 0;}

  }
  cout<<"Hanako";

  return 0;
}


//   __________________
//  | ________________ |
//  ||          ____  ||
//  ||   /\    |      ||
//  ||  /__\   |      ||
//  || /    \  |____  ||
//  ||________________||
//  |__________________|
//  \###################\
//   \###################\
//    \        ____       \
//     \_______\___\_______\
// An AC a day keeps the doctor away.

#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/extc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(...) qqbx(#__VA_ARGS__,  __VA_ARGS__)
template <typename H, typename ...T> constexpr void qqbx(const char *s, const H &h, T &&...args) {
    for(; *s && *s != ','; ++s) if(*s != ' ') std::cerr << *s;
    std::cerr << " = " << h << (sizeof...(T) ? ", " : "\n");
    if constexpr(sizeof...(T)) qqbx(++s, args...); // C++ 17!
}
#else
#include <bits/stdc++.h>
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define get_pos(v,x) (lower_bound(begin(v),end(v),x)-begin(v))
#define sort_uni(v) sort(begin(v),end(v)),v.erase(unique(begin(v),end(v)),end(v))
#define pb emplace_back
#define ff first
#define ss second
#define mem(v,x) memset(v,x,sizeof v)

using namespace std;
using namespace __gnu_pbds;
typedef int64_t ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >;
template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >;
template <typename T> using rbt = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
constexpr ld PI = acos(-1), eps = 1e-9;
constexpr ll N = 65536, INF = 1e18, MOD = 1000000007, K = 256, inf = 1e9;
constexpr inline ll cdiv(ll x, ll m) { return x/m + ((x<0 ^ m>0) && (x%m)); } // ceiling divide
constexpr inline ll modpow(ll e,ll p,ll m=MOD) { ll r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; }

array<int,2> a[N],b[N];
int n, m, ok = 1;
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1];
    for(int i = 0; i < m; i++) cin >> b[i][0] >> b[i][1];
    for(int i = 0; i < n; i++) sort(all(a[i]));
    for(int i = 0; i < m; i++) sort(all(b[i]));
    set<int> cand;
    for(int i = 0; i < n; i++) {
        int x = 0, y = 0;
        for(int j = 0; j < m; j++) {
            if(a[i] != b[j]) {
                x |= (a[i][0] == b[j][0] || a[i][0] == b[j][1]);
                y |= (a[i][1] == b[j][0] || a[i][1] == b[j][1]);
            }
        }
        if(x ^ y) cand.insert(x ? a[i][0] : a[i][1]);
        if(x && y) ok = 0;
    }
    if(ok && cand.size() == 1) return cout << *cand.begin() << '\n', 0;
    debug(ok);
    for(int j = 0; j < m; j++) {
        int x = 0, y = 0;
        for(int i = 0; i < n; i++) {
            if(a[i] != b[j]) {
                x |= (a[i][0] == b[j][0] || a[i][1] == b[j][0]);
                y |= (a[i][0] == b[j][1] || a[i][1] == b[j][1]);
            }
        }
        if(x ^ y) cand.insert(x ? b[j][0] : b[j][1]);
        if(x && y) ok = 0;
    }
    if(ok && cand.size() == 1) return cout << *cand.begin() << '\n', 0;
    cout << (ok ? 0 : -1) << '\n';
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <stdint.h>
#include <cmath>
#include <algorithm>

using namespace std;
typedef int64_t Int;
typedef uint64_t UInt;

int main(int argc, char **argv) {
    Int n = 0;
    Int m = 0;
    // Read the first line
    string line = "";
    if (getline(cin, line)) {
        stringstream ss(line);

        ss >> n >> m;
    }

    Int buttonsArr[n];
    Int buttonsArrIdx = 0;
    for (Int i = 0; i != n; ++i) {
        buttonsArr[i] = 0;
    }
    // Read the second line
    if (getline(cin, line)) {
        stringstream ss(line);

        while (buttonsArrIdx < n) {
            Int curNum = 0;

            ss >> curNum;

            for (Int i = curNum - 1; i != n; ++i) {
                if (buttonsArr[i] == 0) {
                    buttonsArr[i] = curNum;
                }
            }

            ++buttonsArrIdx;
        }
    }

    for (Int i = 0; i != n; ++i) {
        cout << buttonsArr[i];

        if (i < n - 1) {
            cout << " ";
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 9 + 5;
long long num[maxn];
long long sum, temp, k;
int n, cnt;

int main()
{
    num[0] = 9;                         //一位数的个数9
    for(int i = 1; i <= 9; i ++)
        num[i] = num[i - 1] * 10;       //i位数的个数9 90 900 9000....
    for(int i = 1; i <= 9; i ++)
        num[i] *= i + 1;                //i位数中数字字符（0~9）的个数
    while(~scanf("%d", & n))
    {
        sum = 0, cnt = 0, k = 10;       //sum计算数字字符数 cnt计算n的位数
        while(n != temp)                //当n == temp 即 被除数n小于除数k 跳出
        {
            temp = n % k;               //temp是余数
            k *= 10;                    //k不断10倍变大
            cnt ++;                     //cnt记录
            //printf("k:%d\n",k);
        }
        for(int i = 0; i < cnt - 1; i ++)
            sum += num[i];              //计算cnt位前的数字字符数
            //printf("start:%lld  k=%lld\n",start,k);
        sum += (n - k/100 + 1) * cnt;   //计算cnt位到n的数字字符数
        printf("%I64d\n", sum);
    }
    return 0;
}
/*给一个数字，求从1到这个数字一共有多少个数字（小于10）*/


#include <iostream>

using namespace std;

 inline int _fastcall Nod(unsigned int x, unsigned int y)
{
    int n = x * y;
    do
    {
        if(x > y) x = x % y;
        else y = y % x;
    } while(x != 0 && y != 0);
    return n / (x + y);
}

int main()
{
    unsigned int x, y, a, b,  nod;
    cin >> x >> y >> a >> b;
    nod = Nod(x, y);
    if(a % nod)
        a = a + (nod - a % nod);
    if(a > b) cout << 0;
    else
        cout << (b - a) / nod + 1;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <iostream>
#include <cstdio>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <iterator>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <queue>
#include <fstream>
#include <iomanip>

using namespace std;

#define mp make_pair
#define MOD 1000000007

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

inline int max3(int a, int b, int c) { return max(a, max(b, c)); }
inline int max4(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }
inline int min3(int a, int b, int c) { return min(a, min(b, c)); }
inline int min4(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }

const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-12;
const double PI = acos(-1.0);
const pii piiINF = mp(INF, INF);

const int maxn = 8;

int n;
ll a[maxn];

map<ll, int> numb;

int numberOfPrimeNumbers(ll x) {
	if (numb.count(x)) return numb[x];
	int ans = 0;
	ll old = x;
	for (ll i = 2; i*i <= x; i++) {
		while (x % i == 0) {
			x /= i;
			ans++;
		}
	}
	if (x > 1 && x != old) ans++;
	return numb[old] = ans;
}

struct node {
	ll remains;
	bool isLeaf;
	node(ll remains = -1, bool isLeaf = false) : remains(remains), isLeaf(isLeaf) {}
	void operator= (const node &a) {
		remains = a.remains;
		isLeaf = a.isLeaf;
	}
};

int brute(vector<node> &list, int d) {
	if (d == n) {
		bool flag = true;
		int k = list.size();
		for (int i = 0; i < list.size() && flag; i++) {
			if (list[i].remains > 1) {
				int tmp = numberOfPrimeNumbers(list[i].remains);
				if (!list[i].isLeaf && !tmp) tmp++;
				k += tmp;
			}
		}
		if (flag)
			return k;
		else
			return INF;
	}
	int ans = INF;
	if (!d) {
		list[0] = node(a[0], true);
		ans = brute(list, d + 1);
		list[0] = node(-1, true);
	}
	for (int i = 0; i < list.size(); i++) {
		if (list[i].remains == -1 || list[i].remains % a[d] == 0) {
			node old = list[i];
			if (list[i].remains != -1)
				list[i].remains /= a[d];
			list[i].isLeaf = false;
			list.push_back(node(a[d], true));
			ans = min(ans, brute(list, d + 1));
			list[i] = old;
			list.pop_back();
		}
	}
	return ans;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];

	sort(a, a + n, greater<ll>());

	vector<node> list;
	list.push_back(node(-1, true));
	int ans = brute(list, 0);

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N=(int) 5e5+7;
const int K=730+7;
int n;
int q;
int k;
vector<vector<vector<int>>> days;

struct T
{
        int d;
        int x;
        int y;
};

T a[N];
int y;
int bucket[N];
int first[K];
int last[K];
int t[N];
int cnt[N];

void clr()
{
        for (int i=1;i<=n;i++)
                t[i]=i,
                cnt[i]=1;
}

int root(int x)
{
        if (t[x]==x)
                return x;
        else
                return t[x]=root(t[x]);
}

void unite(int x,int y)
{
        x=root(x);
        y=root(y);
        if (x!=y)
        {
                t[x]=y;
                cnt[y]+=cnt[x];
        }
}

int t2[N];
int cnt2[N];

int root2(int x)
{
        if (t2[x]==x)
                return x;
        else
                return t2[x]=root2(t2[x]);
}

void unite2(int x,int y)
{
        x=root(x);
        y=root(y);
        x=root2(x);
        y=root2(y);
        if (x!=y)
        {
                t2[x]=y;
                cnt2[y]+=cnt2[x];
        }
}

void tr(int x)
{
        x=root(x);
        t2[x]=x;
        cnt2[x]=cnt[x];
}

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

        cin>>n>>q>>k;
        days.push_back({});
        for (int i=1;i<=q;i++)
        {
                int tp;
                cin>>tp;
                if (tp==1)
                {
                        int x,y;
                        cin>>x>>y;
                        vector<int> v={x,y};
                        days.back().push_back(v);
                }
                if (tp==2)
                {
                        int x;
                        cin>>x;
                        vector<int> v={x};
                        days.back().push_back(v);
                }
                if (tp==3)
                {
                        days.push_back({});
                }
        }
        int d=0;
        for (auto &vec:days)
        {
                d++;
                for (auto &it:vec)
                        if ((int)it.size()==2)
                        {
                                a[++y]={d,it[0],it[1]};
                        }
        }
        int rad=sqrt(y);
        ///rad=1;
        for (int i=1;i<=y;i++)
                bucket[i]=(i-1)/rad+1;
        for (int i=1;i<=y;i++)
                last[bucket[i]]=i;
        for (int i=y;i>=1;i--)
                first[bucket[i]]=i;
        clr();
        int pos=0,l=1,r=0,nowd=0;
        for (auto &vec:days)
        {
                nowd++;
                int linit=l;
                while (l<=y&&a[l].d<=nowd-k)
                {
                        l++;
                }
                int aft=last[bucket[l]]+1;
                if (bucket[l]!=bucket[linit])
                {
                        clr();
                        for (int j=aft;j<=r;j++)
                                unite(a[j].x,a[j].y);
                }
                for (auto &it : vec)
                {
                        if ((int) it.size()==2)
                        {
                                r++;
                                if (r>=aft)
                                        unite(a[r].x,a[r].y);
                        }
                        else
                        {
                                if (l>r)
                                {
                                        cout<<"1\n";
                                        continue;
                                }
                                int x=it[0],to=min(r,aft-1);
                                tr(x);
                                for (int j=l;j<=to;j++)
                                        tr(a[j].x),tr(a[j].y);
                                for (int j=l;j<=to;j++)
                                        unite2(a[j].x,a[j].y);

                                cout<<cnt2[root2(root(x))]<<"\n";
                        }
                }
        }
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i, n, m, c=0;
    char guest[105], host[105], letters[105];

    cin >> guest >> host >> letters;

    strcat(guest, host);

    n = strlen(guest);
    m = strlen(letters);

    sort(guest, guest+n); //only arrays (char) can be sorted
    sort(letters, letters+m);

    if (n!=m)
        {cout << "NO";
        return 0;}
    else
    {
        for (i=0; i<n; i++)
        {
            if (guest[i]==letters[i])
                c++;
        }

        if (c==n)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}


//░░░░░░░░░░▄
//░░░░░░░░▄▐░▄▄█████▄▄
//░░░░░░▄█████████████▄▀▄▄░▄▄▄
//░░░░░█████████████████▄██████
//░░░░████▐█████▌████████▌█████▌
//░░░████▌█████▌█░████████▐▀██▀
//░▄█████░█████▌░█░▀██████▌█▄▄▀▄
//░▌███▌█░▐███▌▌░░▄▄░▌█▌███▐███░▀
//▐░▐██░░▄▄▐▀█░░░▐▄█▀▌█▐███▐█
//░░███░▌▄█▌░░▀░░▀██░░▀██████▌
//░░░▀█▌▀██▀░▄░░░░░░░░░███▐███
//░░░░██▌░░░░░░░░░░░░░▐███████▌
//░░░░███░░░░░▀█▀░░░░░▐██▐███▀▌
//░░░░▌█▌█▄░░░░░░░░░▄▄████▀░▀
//░░░░░░█▀██▄▄▄░▄▄▀▀▒█▀█░▀
//░░░░░░░░░▀░▀█▀▌▒▒▒░▐▄▄
//░░░░░░░░▄▄▀▀▄░░░░░░▄▀░▀▀▄▄
//░░░░░░▄▀░▄▀▄░▌░░░▄▀░░░░░░▄▀▀▄
//░░░░░▐▒▄▀▄▀░▌▀▄▄▀░░░░░░▄▀▒▒▒▐
//░░░░▐▒▒▌▀▄░░░░░▌░░░░▄▄▀▒▐▒▒▒▒▌
//░░░▐▒▒▐░▌░▀▄░░▄▀▀▄▀▀▒▌▒▐▒▒▒▒▐▐
//░░░▌▄▀░░░▄▀░█▀▒▒▒▒▀▄▒▌▐▒▒▒▒▒▌▌
//░░▄▀▒▐░▄▀░░░▌▒▐▒▐▒▒▒▌▀▒▒▒▒▒▐▒▌
#pragma GCC optimize("Ofast")
#pragma comment(linker, "/stack:200000000")

//Ibraev dmitrij(2020a)
//https://informatics.msk.ru/mod/statements/view3.php?id=41131&chapterid=451#1
//29.05.2019
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
#include <map>
#include <chrono>
#include <cstring>
#include <stack>
#include <bitset>

using namespace std;
#define ll long long
#define pii pair<int,int>
#define vpii vector<pii>
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vvd vector<vd>
#define pdd pair<double,double>
#define vpdd vector<pdd>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define search(set0, x) (set0.find(x)!=set0.end())
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define vvc vector<vc>
#define vvb vector<vb>
#define mp make_pair
#define F first
#define S second
#define pss pair<string, string>
#define vpss vector<pss>
#define double long double
#define vvs vector<vs>
#define mysort(vector) sort(vector.begin(), vector.end())
#define myreverse(vector) reverse(vector.begin(), vector.end())
#define printvi(vector) for (int x: vector) cout << x << " "; cout << endl;
#define printvll(arr) for (ll x: arr) cout << x << " "; cout << endl;
#define vvpii vector<vpii>
#define vvpll vector<vpll>
#define vp3i vector<pair<int, pii>>
#define treap Node*
#define printpair(p0) cout << p0.F << " " << p0.S << endl;
#define printvp(arr) for (auto p0: arr) { printpair(p0) };
#define FOR(x0) for (int i = 0; i < x0; ++i)
#define ull unsigned long long
#define vpt vector<pt>
#define vvvi vector<vvi>
#define FORN(i0, n0) for (int i0 = 0; i0 < n0; ++i0)
#define FORN1(i0, n0) for (int i0 = 1; i0 < n0; ++i0)
//#define endl '\n'
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mod0 = 1e9 + 7;
const ll mod1 = 998244353;
const ll mod2 = 1e9 + 9;
const ll mod3 = 2147483647;

const int sz = 512 * 1024;
const ll inf = 2 * 1024 * 1024 * 1023 - 1;
const ll INF = inf * inf;
const double eps = 1e-7;

void init() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout.precision(16);
}

const int L = 2501;
const int N = 53;

ll dp1[N][L];
ll dp23[N][N][L];
ll w[N][N][N][4];

int main() {
    init();
    int n, T;
    cin >> n >> T;
    vi cnt(4);
    vi d(4);
    dp1[0][0] = dp23[0][0][0] = 1;
    FORN(i, n) {
        int d0, t0;
        cin >> d0 >> t0;
        t0--;
        if (t0 == 0) {
            for (int c0 = cnt[0]; c0 >= 0; c0--) {
                FORN(j, d[0] + 1) dp1[c0 + 1][j + d0] = (dp1[c0 + 1][j + d0] + dp1[c0][j]) % mod0;
            }
        } else {
            for (int c1 = cnt[1]; c1 >= 0; c1--)
                for (int c2 = cnt[2]; c2 >= 0; c2--)
                    FORN(d23, d[1] + d[2] + 1) {
                        int i2 = c1 + (t0 == 1), i3 = c2 + (t0 == 2);
                        dp23[i2][i3][d23 + d0] = (dp23[i2][i3][d23 + d0] + dp23[c1][c2][d23]) % mod0;
                    }
        }
        cnt[t0]++;
        d[t0] += d0;
    }
    w[0][0][0][3] = 1;
    vi c(3);
    for (c[0] = 0; c[0] <= cnt[0]; c[0]++) {
        for (c[1] = 0; c[1] <= cnt[1]; c[1]++) {
            for (c[2] = 0; c[2] <= cnt[2]; c[2]++) {
                FORN(lst, 4) {
                    if (w[c[0]][c[1]][c[2]][lst] != 0) {
                        FORN(nxt, 3)
                            if (nxt != lst && c[nxt] + 1 <= cnt[nxt]) {
                                vector<int> cn(c);
                                cn[nxt]++;
                                w[cn[0]][cn[1]][cn[2]][nxt] =
                                        (w[cn[0]][cn[1]][cn[2]][nxt] + w[c[0]][c[1]][c[2]][lst]) % mod0;
                            }
                    }
                }
            }
        }
    }
    vll f(N, 1);
    FORN(i, N - 1) {
        f[i + 1] = 1LL * (f[i]) * (i + 1) % mod0;
    }
    ll ans = 0;
    for (c[0] = 0; c[0] <= cnt[0]; c[0]++)
        FORN(d0, d[0] + 1) {
            if (T - d0 >= 0) {
                for (c[1] = 0; c[1] <= cnt[1]; c[1]++)
                    for (c[2] = 0; c[2] <= cnt[2]; c[2]++) {
                        ll k = 1LL * (dp1[c[0]][d0] * dp23[c[1]][c[2]][T - d0]) % mod0;
                        FORN(i, 3) k = (k * f[c[i]]) % mod0;
                        FORN(lst, 3) {
                            if (c[lst] > 0) ans = (ans + k * w[c[0]][c[1]][c[2]][lst]) % mod0;
                        }
                    }
            }
        }
    cout << ans << '\n';
}


#include<iostream>
#include<vector>
#include<complex>
#include<algorithm>
#include<bitset>
#include<cstring>
#include<map>
#include<set>
#include<unordered_map>
#include<stack>
#include<queue>
using namespace std;

typedef long long ll;
typedef long double ld;

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

    int n;
    cin>>n;
    n<<=1;
    int a[n];
    for(int i=0;i<n;++i) cin>>a[i];
    int ans=1e6;
    sort(a,a+n);
    //for (auto x:a)
    	//	cout<<x<<" ";
    //cout<<endl;
    for(int i=0;i<n-1;++i)
    		for(int j=i+1;j<n;++j){
    			int s=0;
    			for(int k=0;k<n-1;++k){
    				while(k==i||k==j) ++k;
    				if(k>n-2) break;
    				int m=k+1;
    				while(m==i||m==j) ++m;
    				if(m>n-1) break;
    				s+=a[m]-a[k];
    				k=m;
    			}
    			ans=min(ans,s);
    			//cout<<"i "<<i<<" j "<<j<<" sum "<<s<<endl;
    		}
    	cout<<ans;
}


#include<iostream>

using namespace std;

int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	if(n<=m)
	{
	    if(n<=k)
	    {
	        cout<<"Yes"<<endl;
	    }

		else
		{
			cout << "No" << endl;
		}
	}

	else{
	    cout<< "No"<<endl;
	}
	return 0;
}


#include "bits/stdc++.h"

using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define prvec(v)                                  lop(R15,0,v.size())cout << v[R15] <<" "; cout << endl;
#define prarr(arr,s1,e1)                          lop(R15,s1,e1)cout << arr[R15] << " "; cout << endl;

#define endl                                      '\n'
#define ff                                        first
#define ss                                        second
#define pii                                       pair<int,int>

#define pb                                        push_back
#define vi                                        vector<int>
#define vpii                                      vector< pii >

#define all(v)                                    v.begin(),v.end()
#define sZ(re)                                    (int)re.size()
#define mset(a,f)                                 memset(a,f,sizeof(a))
#define M                                         1000000007
#define Think_Twice_Code_Once                     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

bool J27 = 1;
#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
#define rtn                                       if(!J27)return;
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { rtn cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{     rtn
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

inline void INP();
const int N = 112;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;

int n, Q;

pii val[N*N];

int dx[] = {1,-1,  1,  -1, 2, 2, -2, -2};        // d, u, l, r
int dy[] = {2, 2, -2,  -2, 1,-1,  1, -1};

bool safe(int x,int y)
{
   if( x < 1 or y < 1 or x > n or y > n ) return 0;
   return 1;
}

map<pii,int> moves;

pii dp[N*N][3];
pii x = {-1, -1};
pii arr[4];

pii get( int id, int piece )
{
      // bug(id, piece);
      if( id == n*n )  return {0,0};

      pii &mafia = dp[id][piece];

      // if( id == n*n ) bug(id, piece, mafia.ff, mafia.ss);;
      if( mafia != x ) return mafia;
      pii curr = val[id], nextt = val[id+1];

      pii a1 = {inf, inf}, a2 = a1, a3 = a1, res , a4 = a1;

      a1 = get(id+1, 0);
      if( curr.ff == nextt.ff || curr.ss == nextt.ss ) a1.ff++;
      else a1.ff += 2;

      res = get(id+1, 1);
      if( abs(curr.ff - nextt.ff) == abs(curr.ss - nextt.ss) )  a2 = res, a2.ff++;
      else if( (curr.ff + curr.ss)%2  == (nextt.ff + nextt.ss)%2 )  a2 = res, a2.ff += 2;
      else
      {
            lop(i,0,8)
            {
                  int nx = nextt.ff + dx[i], ny = nextt.ss + dy[i];
                  if( safe(nx, ny) and abs(curr.ff-nx) == abs(curr.ss-ny) and curr.ff != nx and curr.ss != ny)
                  {
                        res = get(id+1, 2);
                        a2 = res;
                        a2.ff += 3;
                        a2.ss++;
                  }
            }
      }

      int dix = abs(nextt.ff - curr.ff), diy = abs(nextt.ss - curr.ss);
      res = get(id+1, 2);
      if( dix and diy and (dix + diy) == 3 ) a3 = res, a3.ff++;
      else
      {
            bool f = 0;
            vpii vec;
            lop(i,0,8)
            {
                  int nx = curr.ff + dx[i], ny = curr.ss + dy[i];
                  if( safe(nx, ny)) vec.pb({nx, ny});
                  int dix = abs(nextt.ff - nx), diy = abs(nextt.ss- ny);

                  if( safe(nx, ny) and dix and diy and (dix + diy) == 3  )
                  {
                        f = 1;
                        break;
                  }
            }
            if( f )
            {
                 res = get(id+1, 2);
                 a3 = res;
                 a3.ff += 2;
            }
            else
            {
                lop(i,0,vec.size())
                {
                        lop(j,0,8)
                        {
                              int nx = vec[i].ff + dx[j], ny = vec[i].ss + dy[j];
                              int dix = abs(nextt.ff - nx), diy = abs(nextt.ss- ny);

                              if( safe(nx, ny) and dix and diy and (dix + diy) == 3  )
                              {
                                    f = 1;
                                    break;
                              }
                        }
                }
                if(f)
                {
                    res = get(id+1, 2);
                    a3 = res;
                    a3.ff += 3;
                }

                  lop(i,0,8)
                  {
                        int nx = curr.ff + dx[i], ny = curr.ss + dy[i];
                        if( safe(nx, ny) and abs(nx-nextt.ff) == abs(ny-nextt.ss) )
                        {
                              a4 = get(id+1, 1);
                              a4.ff += 3;
                              a4.ss++;
                        }
                  }

            }

      }

      arr[0] = a1, arr[1] = a2, arr[2] = a3; arr[3] = a4;
      lop(i,0,4)
      {
          if(  ( i < 3 and piece != i ) or ( i == 3 and piece != 2 ) )
          {
              arr[i].ss++;
              arr[i].ff++;
          }
      }

      res = { inf, inf };

      int f = 0;
      lop(i,0,4)
      {
            // if( id == 1 and piece == 2) bug(arr[i].ff, arr[i].ss,res.ff, res.ss);
            res = min(res, arr[i]);
      }
      // if( id == 1 ) bug(res.ff, res.ss);

      return mafia = res;

}

void solve()
{
      cin >> n;
      lop(i,1,n+1) lop(j,1,n+1)
      {
            int y;
            cin >> y;
            val[ y ].ff = i;
            val[ y ].ss = j;
      }

      lop(i,0,n*n+2)lop(j,0,4) dp[i][j] = x;

      pii ans = {inf, inf};

      lop(i,0,3)
      {
            pii res = get(1, i);
            ans = min(ans, res);
      }

      cout << ans.ff  << " " << ans.ss ;

}

int32_t main()
{

      J27 = 1;
      Think_Twice_Code_Once;
      INP();
      int t = 1;
      //cin >> t;
      lop(i,0,8)
      {
            lop(j,0,8)
            {
                        int x = dx[i] + dx[j];
                        int y = dy[i] + dy[j];
                        if( moves.find({x,y}) == moves.end())
                        {
                              moves[ {x,y} ] = 2;
                        }
            }
      }
      lop(i,0,8)
      {
            lop(j,0,8)
            {
                  lop(k,0,8)
                  {
                        int x = dx[i] + dx[j] + dx[k];
                        int y = dy[i] + dy[j] + dy[k];
                        if( moves.find({x,y}) == moves.end())
                        {
                              moves[ {x,y} ] = 3;
                        }
                  }
            }
      }
      while(t--) solve();

}

inline void INP()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
}


#include <stdio.h>
#include <string.h>

int main(){
	char str[105];
	int flag = 0;
	int ctr = 0;
	scanf("%s", str);
	if(str[0] >= 97 && str[0] <= 122){
		flag = 1;
	}else{
		ctr++;
	}
	for(int j = 1; j < strlen(str);j++){
	if(str[j] >= 65 && str[j] <= 90){
		ctr++;
		continue;
	}if(str[j] >= 97 && str[j] <= 122){
		flag = 0;
	}
}
	if(flag == 1){
		if(str[0] >= 97 && str[0] <= 122){
				str[0] -= 32;
			}
		for(int i = 1; i < strlen(str);i++){
		    if(str[i] >= 65 && str[i] <= 90){
				str[i] += 32;
			}
		}
	}else if(ctr == strlen(str)){
		for(int a = 0; a < strlen(str);a++){
			str[a] += 32;
		}
	}

	printf("%s", str);
	return 0;
}


/*

*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e2+100;
typedef long long LL;
string s,t="CODEFORCES";
int main()
{
	cin>>s;
	int len=s.length(),flag=0;
	if(s==t)flag=1;
	else
		for(int i=0;i<=10&&s[i];i++){
			string p=s.substr(0,i);
			if(p==t){
				flag=1;
				continue;
			}
			string q=s.substr(len-10+i>=0?len-10+i:0);
			if(q==t){
				flag=1;
				continue;
			}
			if(p+q==t)flag=1;
		}
	if(flag)printf("YES\n");
	else printf("NO\n");
	return 0;
}


#include <iostream>
#include <cmath>

using namespace std;

int x, y;

int main()
{
    cin >> x >> y;
    if (y < 0) {
        y -= abs(x);
    }
    else {
        y += abs(x);
    }

    if (x < 0) {
        cout << -1 * abs(y) << " 0 0 " << y;
    }
    else {
        cout << "0 " << y << ' ' << abs(y) << " 0";
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int n,m;
struct node
{
    int x,y;
}a[14100];

int cmp(const void *a,const void *b)
{
    struct node *aa,*bb;
    aa=(struct node *)a;
    bb=(struct node *)b;
        return aa->x-bb->x;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].y = i+1;
        }
        qsort(a,n,sizeof(a[0]),cmp);
        int b[10010];
        int t = 0;
         memset(b,0,sizeof(b));
        for(int i=0;i<n;i++)
        {
            if(m>=a[i].x)
            {
                m = m-a[i].x;
                b[t++] = a[i].y;
            }
            else
            {
                break;
            }
        }
       sort(b,b+t);
        printf("%d\n",t);
        for(int i=0;i<t;i++)
        {
            if(i == t-1)
            {
                printf("%d\n",b[i]);
            }
            else
            {
                printf("%d ",b[i]);
            }
        }
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n,c,s;
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		cin >> c;
		s=s+c;
	}
	if(s==0)
	cout <<"EASY" << endl;
	else cout << "HARD" ;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n, m, a, b;

    cin >> n >> m >> a >> b;
    int save = 0;
    int check = n;
    int rubles = 0;
    int flag = 0;
    int flag2 = 0;

    if (a == float(b / m))
    {
        cout << n*a;
        return 0;
    }
    else if (a < float(b / m))
    {
        cout << n*a;
        return 0;

    }

    else if (a > float(b / m))
    {

        while (true)
        {
            if (check == 0)
                break;
            if (flag2 == 0)
            {

                check -= m;
                rubles += b;
            }
            if (check == 0)
                break;
            else if (check < 0 || (flag == -1  && flag2 == -1))
            {
                if (flag == 0)
                {
                     save =  rubles;
                    check += m;
                    rubles -= b;
                    check--;
                    rubles += a;
                    flag = -1;
                    flag2 = -1;
                }
                else if (flag == -1)
                {
                    rubles += a;
                    check--;
                }

            }

        }
        if (save <= rubles && flag == -1)
            cout << save;
        else
            cout << rubles;

    }
    return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i=0; i<(int)(n); i++)
#define REP1(i,a,b) for(int i=a; i<=(int)(b); i++)
#define ALL(x) begin(x),end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int,int> PII;

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    VI a(n+1);
    REP1(i,1,n) {
        cin >> a[i];
        if ( i>0 ) a[i]+=a[i-1];
    }
    int mi=360;
    for ( int i=0; i<=n; i++ ) {
        for ( int j=0; j<i; j++ ) {
            chmin(mi,abs(a[i]-a[j]+a[i]-a[j]-360));
        }
    }
    cout << mi << '\n';
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef  long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for( i=n-1;i>=0;i--)
#define rep(i,n) for( i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e16;

ll n, m;

int main() {
	// your code goes here
	IOS;

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll i, j, t, k, x, y, z,   N;
	cin >> n >> m ;
	x = n;
	v<ll> vec;
	while (x > 0) {
		vec.pb(x % 10);
		x /= 10;
	}
	ll l = vec.size();
	ll dp[(ll)pow(2, l)][m];
	mset(dp, 0);
	dp[0][0] = 1;
	for (i = 0; i < pow(2, l); i++) {
		for (j = 0; j < m; j++) {
			for (k = 0; k < l; k++) {
				if ((i & 1 << k) == 0 && (i || vec[k])) {
					dp[(i | 1 << k)][(10 * j + vec[k]) % m] += dp[i][j];
				}
			}
		}
	}
	ll cnt[10];
	mset(cnt, 0);
	ll prod = 1;
	for (auto u : vec) {
		prod *= ++cnt[u];
	}
	cout << dp[(1 << k) - 1][0] / prod;

	return 0;
}


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

int asc[123] = {};

int main(){
	int ano;
	bool flag;

	cin >> ano;

	ostringstream stream;

	for(int i = ano+1; i < 10000; i++){
		flag = true;
		stream << i;
		string s = stream.str();

		for(int j = 0; j < s.length(); j++){
			asc[s[j]]++;
			if(asc[s[j]] > 1)
				flag = false;
		}

		for(int j = 48; j < 58; j++)
			asc[j] = 0;

		if(flag == true){
			cout << i <<endl;
			break;
		}
		stream.str(string());
	}

}


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

typedef long long ll;
const int maxn = 55, mod = 1e9 + 7;

int n, m, inv[maxn], f[maxn][maxn], g[maxn][maxn], t[maxn][maxn];

int main()
{
	scanf("%d%d", &n, &m);
	inv[1] = 1;
	for (int i = 2; i <= n + 1; ++i) inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod;
	f[0][1] = 1;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i + 1; ++j) {
			for (int k = 0; k < i; ++k) {
				for (int l = j; l <= k + 1; ++l)
					g[i][j] = (g[i][j] + (ll)f[k][l] * f[i - k - 1][j]) % mod;
				for (int l = j + 1; l <= i - k; ++l)
					g[i][j] = (g[i][j] + (ll)f[k][j] * f[i - k - 1][l]) % mod;
			}
			memset(t, 0, sizeof(t));
			for (int p = 0; p + i <= n; ++p)
				for (int q = 1; q + j <= n + 1; ++q)
					for (int C = 1, x = 1; p + x * i <= n && q + x * i <= n + 1; ++x) {
						C = (ll)C * (g[i][j] + x - 1) % mod * inv[x] % mod;
						t[p + x * i][q + x * j] = (t[p + x * i][q + x * j] + (ll)C * f[p][q]) % mod;
					}
			for (int p = i; p <= n; ++p)
				for (int q = j; q <= p + 1; ++q) {
					f[p][q] += t[p][q];
					if (f[p][q] >= mod) f[p][q] -= mod;
				}
		}
	printf("%d\n", f[n][m]);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
	int n, a[10], minn, maxx;
	scanf("%d%d", &n, &a[0]);
	minn = maxx = a[0];
	for (int i = 1; i < n; i++)
	{
		scanf("%d", &a[i]);
		minn = min(minn, a[i]);
		maxx = max(maxx, a[i]);
	}
	printf("%d", (maxx + minn) / 2);
	return 0;
}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
    int p,q;
    int c=0;
    int d=0;
    cin>>p>>q;
    int a[p],b[q];
    for(int j=0;j<p;j++){
        cin>>a[j];
    }
    for(int j=0;j<q;j++){
        cin>>b[j];
    }
    sort(a,a+p);
    sort(b,b+q);
    for(int i=0;i<p;i++){
        for(int j=0;j<q;j++){
            if((a[i]==b[j])&&(d==0)){
                c=a[i];
                d=1;
                break;
            }
        }
    }
    if(c){
        cout<<c;
    }
    else{
        if(a[0]<b[0]){
            cout<<a[0]*10+b[0];
        }
        else{
           cout<<b[0]*10+a[0];
        }
    }

}


#include <stdio.h>
#include <math.h>

int main()
{
	int n,tn,x,s=1,ts,ans=0,w;
	scanf("%d",&n);
	while(s<=512)
	{
		ts=s;
		w=1;tn=0;
		while(ts>0)
		{
			tn+=(ts%2)*w;
			ts/=2;
			w*=10;
		}
		if(tn<=n) ans++;
		s++;
	}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int a,b,c;
int main()
{
	cin>>a>>b>>c;
	if(((c%b==a%b)&&(c>=a))||(c%b==(a+1)%b)&&c>a+1)
	cout<<"YES";
	else
	cout<<"NO";
	return 0;
}


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

constexpr int maxn=400000+10;

int lnk[maxn],nxt[maxn],son[maxn],n,m,now,c1,c2,dnt[maxn],mk;
vector<int> nvis[maxn],rt,rv;
vector<vector<int> > res;
bool flag[maxn];
pair<int,int> cnt[maxn];

bool cmp(const int a,const int b) __attribute__((always_inline));

void dfs(int u) {
	flag[u]=1;
	for (int v=son[u]; v; v=nxt[v])
		if (lnk[v]>0) {
			lnk[v^1]*=-1;
			if (!flag[lnk[v]])
				dfs(lnk[v]),c2++;
			else lnk[v]=lnk[v^1]=0,nvis[u].push_back(v/2),rt[rt.size()-1]=u,c1++;
		}
}

void efs(int u) {
	dnt[u]=1;
	for (int v=son[u]; v; v=nxt[v])
		if (lnk[v]>0) {
			efs(lnk[v]);
			dnt[u]+=dnt[lnk[v]];
			if (dnt[lnk[v]]==1) mk=u;
		}
}

void go(int u) {
	for (int v=son[u]; v; v=nxt[v])
		if (lnk[v]) {
			if (lnk[v]<0) lnk[v]*=-1;
			lnk[v^1]=0;

			rv.push_back(lnk[v]);
			go(lnk[v]);
			rv.push_back(u);
			if (now<rt.size()) {
				res.push_back(rv);
				rv.resize(3);
				rv[0]=v/2;
				rv[1]=u;
				rv[2]=rt[now++];
				res.push_back(rv);
				rv.resize(1);
				rv[0]=rt[now-1];
				go(rt[now-1]);
				rv.push_back(u);
			}
		}
	for (int i=0; i<nvis[u].size(); i++)
		if (now<rt.size()) {
			res.push_back(rv);
			rv.resize(3);
			rv[0]=nvis[u][i];
			rv[1]=u;
			rv[2]=rt[now++];
			res.push_back(rv);
			rv.resize(1);
			rv[0]=rt[now-1];
			go(rt[now-1]);
			rv.push_back(u);
		} else break;
}

bool cmp(const int a,const int b) {
	return (a==1 || (b!=1 && cnt[a]>cnt[b]));
}

int main() {
	scanf("%d%d",&n,&m);
	for (int i=0,j=1,u,v; i<m; i++) {
		scanf("%d%d",&u,&v);
		lnk[++j]=v;
		nxt[j]=son[u];
		son[u]=j;
		lnk[++j]=u;
		nxt[j]=son[v];
		son[v]=j;
	}
	for (int i=1; i<=n; i++)
		if (!flag[i]) {
			c1=c2=0;
			rt.push_back(i);
			dfs(i);
			cnt[rt[rt.size()-1]]=make_pair(c1,c2);
		}
	rt[0]=1;
	sort(rt.begin(),rt.end(),cmp);
	for (int i=1; i<rt.size(); i++) {
		mk=rt[i];
		efs(rt[i]);
		if (!nvis[rt[i]].size()) rt[i]=mk;
	}
	while (now<rt.size()) {
		if (now) {
			int k=-1;
			if (nvis[rt[now]].size()) {
				k=nvis[rt[now]][nvis[rt[now]].size()-1];
				nvis[rt[now]].pop_back();
			} else {
				for (int i=son[rt[now]]; i; i=nxt[i])
					if (lnk[i]>0 && dnt[lnk[i]]==1) {
						k=i/2;
						rt.push_back(abs(lnk[i]));
						lnk[i]=lnk[i^1]=0;
						break;
					}
			}
			if (k==-1) break;
			rv.resize(3);
			rv[0]=k;
			rv[1]=1;
			rv[2]=rt[now];
			res.push_back(rv);
			rv.clear();
		}
		rv.push_back(rt[now++]);
		go(rt[now-1]);
		if (rv[rv.size()-1]!=1) rv.push_back(1);
		res.push_back(rv);
	}
	if (now<rt.size()) {
		printf("NO\n");
		return 0;
	}
	printf("YES\n");
	printf("%d\n",res.size()/2);
	for (int i=0; i<res.size(); i++)
		if (!(i&1)) {
			printf("%d",res[i].size());
			for (int j=0; j<res[i].size(); j++)
				printf(" %d",res[i][j]);
			printf("\n");
		} else
			printf("%d %d %d\n",res[i][0],res[i][1],res[i][2]);
}//hellobig_tq
//orzlxr!


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

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

const ll MOD = 1000000007;

int main() {
	int n;
	cin >> n;
	ll a = 1, b = 1;
	rep(i,3*n) {
		a = (a * 3) % MOD;
	}
	rep(i,n) {
		b = (b * 7) % MOD;
	}
	ll ans = (MOD + a - b) % MOD;
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std;
struct node {
	int last;
	int first;
	int ans;
}k[28];
char s[1000005];
int y[1000005];
int main()
{
	int i, j, n, a = 0, b;
	char c;
	while (scanf("%d%d", &n, &b) != EOF)
	{
		a = 0;
		scanf("%s", &s);
		memset(y, 0, sizeof(y));
		for (i = 0; i < 26; i++)
			k[i].first = 199;
		int d;
		for (i = 0; i < n; i++)
		{
			d = s[i] - 'A';
			if (k[d].first == 199)
			{
				k[d].first = i;
				k[d].last = i;
				y[i] = 1;
			}
			else
			{
				y[k[d].last] = 0;
				y[i] = 2;
				k[d].last = i;
			}
		}
		for (i = 0; i < 26; i++)
		{
			if (k[i].first != 199)
			{
				if (k[i].first == k[i].last)
					y[k[i].first] = 3;
				else
					y[k[i].first] = 1;
			}
		}
		int flag = 0;
		for (i = 0; i < n; i++)
		{
			if (a > b)
			{
				flag = 1;
				break;
			}
			if((y[i] == 1 || y[i] == 3) && a == b)
			{
				flag = 1;
				break;
			}
			if(y[i] == 1)
				a++;
			else if (y[i] == 2)
				a--;

		}
		if (flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define mii map<ll,ll>
#define pii pair<ll,ll>
#define vi vector<ll>
#define endl "\n"
#define deb(x) cout << #x << "=" << x << endl
const ll mod = 1e9+7;

ll dx[]={-1,0,1,0};
ll dy[]={0,-1,0,1};

ll msum(ll a,ll b,ll m) { return (a%m+b%m)%m; }
ll msub(ll a,ll b,ll m) { return (a%m-b%m)%m; }
ll mpro(ll a,ll b,ll m) { return ((a%m)*(b%m))%m; }
ll m_m(ll a,ll b,ll m);
ll fxp(ll a,ll b,ll m);
void swap(ll &a,ll &b){ ll t=a; a=b; b=t;}

bool isset(ll n,ll i)
{
    ll mask=(ll)1<<i;
    ll val=n & mask;
    // deb(val);
    if(val!=0)
        return true;
    return false;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--)
    {
        ll l,r;
        cin>>l>>r;
        if(l==r)
        {
            cout<<0<<endl;
            return 0;
        }
        ll ln=log2(r)+1;

        ll cnt=0,i=ln-1;
        // deb(ln);
        while(i>=0)
        {
            bool check1,check2;
            check1=isset(l,i);
            check2=isset(r,i);
            // deb(check1);
            // deb(check2);
            if((check1 and check2) || (!check1 and !check2))
            {
                cnt++;
                i--;
            }
            else
                break;

        }

        ll V=(ll)1<<(i+1);
        V--;
        cout<<V<<endl;
    }
 return 0;
}
ll fxp(ll a,ll b,ll m) {
    if(b==0)
        return 1;
    if(b%2==0)
        return fxp(m_m(a,a,m),b/2,m);
    return m_m(fxp(a,b-1,m),a,m);
}
ll m_m(ll a,ll b,ll m)
{
    ll res=0;
    a=a%m;
    while(b)
    {
        if(b&1)
        {
            res+=a;
            res=res%m;
        }
        a=(a*2)%m;
        b=b/2;
    }
    return res;
}


#include <iostream>
using namespace std;

int main()
{
	int s, v1, v2, t1, t2;
	cin >> s >> v1 >> v2 >> t1 >> t2;
	if (s * v1 + 2 * t1 > s * v2 + t2 * 2)
		cout << "Second";
	else if (s * v1 + 2 * t1 < s * v2 + t2 * 2)
		cout << "First";
	else
		cout << "Friendship";
	return 0;
}


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

using namespace std;

typedef long long ll;

const int N = 400010;

struct Node
{
  int fa, ch[2], siz, sizi;
  ll siz2i;
  ll siz2() { return (ll) siz * siz; }
} t[N];

bool nroot(int x);
void rotate(int x);
void Splay(int x);
void access(int x);
int findroot(int x);
void link(int x);
void cut(int x);
void pushup(int x);

void add(int u, int v);
void dfs(int u);

int head[N], nxt[N << 1], to[N << 1], cnt;
int n, m, c[N], f[N];
ll ans, delta[N];
bool bw[N];
vector<int> mod[N][2];

int main()
{
  int i, j, u, v;
  ll last;

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

  for (i = 1; i <= n; ++i)
    {
      scanf("%d", c + i);
      mod[c[i]][0].push_back(i);
      mod[c[i]][1].push_back(0);
    }

  for (i = 1; i <= n + 1; ++i) t[i].siz = 1;

  for (i = 1; i < n; ++i)
    {
      scanf("%d%d", &u, &v);
      add(u, v);
      add(v, u);
    }

  for (i = 1; i <= m; ++i)
    {
      scanf("%d%d", &u, &v);
      mod[c[u]][0].push_back(u);
      mod[c[u]][1].push_back(i);
      c[u] = v;
      mod[v][0].push_back(u);
      mod[v][1].push_back(i);
    }

  f[1] = n + 1;
  dfs(1);

  for (i = 1; i <= n; ++i) link(i);

  for (i = 1; i <= n; ++i)
    {
      if (!mod[i][0].size())
        {
	  delta[0] += (ll)n * n;
	  continue;
        }
      if (mod[i][1][0])
        {
	  delta[0] += (ll)n * n;
	  last = (ll)n * n;
        } else
	last = 0;
      for (j = 0; j < mod[i][0].size(); ++j)
        {
	  u = mod[i][0][j];
	  if (bw[u] ^= 1)
	    cut(u);
	  else
	    link(u);
	  if (j == mod[i][0].size() - 1 || mod[i][1][j + 1] != mod[i][1][j])
            {
	      delta[mod[i][1][j]] += ans - last;
	      last = ans;
            }
        }
      for (j = mod[i][0].size() - 1; ~j; --j)
        {
	  u = mod[i][0][j];
	  if (bw[u] ^= 1)
	    cut(u);
	  else
	    link(u);
        }
    }

  ans = (ll) n * n * n;
  for (i = 0; i <= m; ++i)
    {
      ans -= delta[i];
      printf("%I64d ", ans);
      //printf("%lld\n",ans);
    }

  return 0;
}

bool nroot(int x) { return x == t[t[x].fa].ch[0] || x == t[t[x].fa].ch[1]; }

void rotate(int x)
{
  int y = t[x].fa;
  int z = t[y].fa;
  int k = x == t[y].ch[1];
  if (nroot(y)) t[z].ch[y == t[z].ch[1]] = x;
  t[x].fa = z;
  t[y].ch[k] = t[x].ch[k ^ 1];
  t[t[x].ch[k ^ 1]].fa = y;
  t[x].ch[k ^ 1] = y;
  t[y].fa = x;
  pushup(y);
  pushup(x);
}

void Splay(int x)
{
  while (nroot(x))
    {
      int y = t[x].fa;
      int z = t[y].fa;
      if (nroot(y)) (x == t[y].ch[1]) ^ (y == t[z].ch[1]) ? rotate(x) : rotate(y);
      rotate(x);
    }
}

void access(int x)
{
  for (int y = 0; x; x = t[y = x].fa)
    {
      Splay(x);
      t[x].sizi += t[t[x].ch[1]].siz;
      t[x].sizi -= t[y].siz;
      t[x].siz2i += t[t[x].ch[1]].siz2();
      t[x].siz2i -= t[y].siz2();
      t[x].ch[1] = y;
      pushup(x);
    }
}

int findroot(int x)
{
  access(x);
  Splay(x);
  while (t[x].ch[0]) x = t[x].ch[0];
  Splay(x);
  return x;
}

void link(int x)
{
  int y = f[x];
  Splay(x);
  ans -= t[x].siz2i + t[t[x].ch[1]].siz2();
  int z = findroot(y);
  access(x);
  Splay(z);
  ans -= t[t[z].ch[1]].siz2();
  t[x].fa = y;
  Splay(y);
  t[y].sizi += t[x].siz;
  t[y].siz2i += t[x].siz2();
  pushup(y);
  access(x);
  Splay(z);
  ans += t[t[z].ch[1]].siz2();
}

void cut(int x)
{
  int y = f[x];
  access(x);
  ans += t[x].siz2i;
  int z = findroot(y);
  access(x);
  Splay(z);
  ans -= t[t[z].ch[1]].siz2();
  Splay(x);
  t[x].ch[0] = t[t[x].ch[0]].fa = 0;
  pushup(x);
  Splay(z);
  ans += t[t[z].ch[1]].siz2();
}

void pushup(int x)
{
  t[x].siz = t[t[x].ch[0]].siz + t[t[x].ch[1]].siz + t[x].sizi + 1;
}

void add(int u, int v)
{
  nxt[++cnt] = head[u];
  head[u] = cnt;
  to[cnt] = v;
}

void dfs(int u)
{
  int i, v;
  for (i = head[u]; i; i = nxt[i])
    {
      v = to[i];
      if (v != f[u])
        {
	  f[v] = u;
	  dfs(v);
        }
    }
}


#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    int n;
    int m;
    scanf("%d%d",&n,&m);
    int sum = (n*m)/2;
    printf("%d\n",sum);
    return 0;
}


/*
 * e.cpp
 *
 *  Created on: 2011-9-11
 *      Author: mac
 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <climits>
#include <utility>
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define REP(i,n) for(int i=0;i<n;++i)
using namespace std;
int H, M, K;
typedef long long int64;
int64 calc2(int a, int k) {
	for (int i = 0; i < k - 1; ++i) {
		a /= 10;
	}
	return a;
}
int calcDigit(int a) {
	int ret = 0;
	while (a) {
		if (a % 10)
			++ret;
		a /= 10;
	}
	return ret;
}
int64 calc(int h, int m) {
	int d = calcDigit(M - 1);
	int64 ret = calc2(h, K - d);
	ret += calc2(m, K) + h * calc2(M - 1, K);
	return ret;
}

int main() {
	cin >> H >> M >> K;
	int h1, m1, h2, m2;
	cin >> h1 >> m1 >> h2 >> m2;
	if (make_pair(h1, m1) <= make_pair(h2, m2))
		cout << calc(h2, m2) - calc(h1, m1) << endl;
	else
		cout << (calc(H - 1, M - 1) + ((calcDigit(H - 1) + calcDigit(M - 1)) >= K) - calc(h1, m1) + calc(h2, m2)) << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long a,b,c,e[200000],mix,l,r,o,d[2000000],ans,ans1;
vector<long long> v[100000],v1;
set<long long> st;
map<long long,long long> mp,mp1;
string s;
pair<int,int> p[100000],p1[100000];
char ch[10];
deque <char> dq;
bool bo,ol;
int main(){
    cin>>a;
    if(a%4==1){
        cout<<0<<" "<<"A";
    } else if(a%4==2){
        cout<<1<<" "<<"B";
    } else if(a%4==3){
        cout<<2<<" "<<"A";
    } else cout<<1<<" "<<"A";
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int k,r,respo;
    scanf("%d%d",&k,&r);
    respo=k;
    while(respo%10!=r & respo%10!=0){
        respo+=k;
    }
    printf("%d\n",respo/k);
    return 0;
}
// 1535219033912


#include <iostream>
#include <cstdio>
using namespace std;
long long int m,n,k;
inline bool pos(long long int d)
{
	long long int m1=m;
	m1=m1-d;
	if(d<k)
	{
		m1=m1-d*(d-1)/2;
		m1=m1-(k-d);
	}
	else m1=m1+(k-1)*(k-2*d)/2;
	long long int k1=n-k+1;
	if(d<k1)
	{
		m1=m1-d*(d-1)/2;
		m1=m1-(k1-d);
	}
	else m1=m1+(k1-1)*(k1-2*d)/2;
	if(m1<0) return false;
	return true;
}
int main()
{

	scanf("%lld %lld %lld",&n,&m,&k);
	long long int l=1;
	long long int r=1000000004;
	long long int mid=(l+r)/2;
	while(l<r-1)
	{
		if(pos(mid)) l=mid;
		else r=mid-1;
		mid=(l+r)/2;
	}
	while(!pos(mid)) mid--;
	while(pos(mid+1)) mid++;
	printf("%lld",mid);
	return 0;
}


/*

https://www.cnblogs.com/LLTYYC/p/11612535.html

*/
#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define dep(i,j,k) for(int i=k;i>=j;i--)
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define make(i,j) make_pair(i,j)
#define pb push_back
#define Pi acos(-1.0)
using namespace std;
const int N = 305, mod = 1e9 + 7;
LL dp[N][N], c[N][N];
void init() {
    c[0][0] = 1LL;
    rep(i, 1, N - 5) {
        c[i][0] = c[i][i] = 1LL;
        rep(j, 1, i - 1) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
    }
}
LL M_1[N], M[N];
int main() {
    int n; LL m; scanf("%d %lld", &n, &m); init();
    M_1[0] = M[0] = 1LL;
    M_1[1] = (m - 1 + mod) % mod; M[1] = m % mod;
    rep(i, 2, n) M_1[i] = M_1[i - 1] * (m - 1) % mod, M[i] = M[i - 1] * m % mod;
    rep(i, 1, n) dp[1][i] = 1LL * c[n][i] * M_1[n - i] % mod;
    rep(i, 2, n) rep(j, 0, n) rep(k, 0, j) {
        LL x = 1LL * dp[i - 1][k] * c[n - k][j - k] % mod;
        LL y = 1LL * M_1[n - j] * M[k] % mod;
        dp[i][j] = (dp[i][j] + 1LL * x * y % mod) % mod;
        if(j == k) dp[i][j] = (dp[i][j] - 1LL * M_1[n] * dp[i - 1][k] % mod + mod) % mod;
    }
    printf("%d\n",dp[n][n]);
    return 0;
}


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

typedef unsigned long long ll;
const int N = 2;
ll n, k, l, m;

struct Mat {
    ll mat[N + 1][N + 1];
} A, B;

Mat operator * ( Mat a, Mat b )
{
    Mat c;
    memset ( c.mat, 0, sizeof c.mat );
    for ( int k = 0; k <  N; k++ )
        for ( int i = 0; i <  N; i++ )
            for ( int j = 0; j <  N; j++ )
                ( c.mat[i][j] += ( a.mat[i][k] * b.mat[k][j] ) % m ) %= m;
    return c;
}

Mat operator ^ ( Mat a, ll pow )
{
    Mat c;
    for ( int i = 0; i <  N; i++ )
        for ( int j = 0; j <  N; j++ )
            c.mat[i][j] = ( i == j );
    while ( pow ) {
        if ( pow & 1 )     c = c * a;
        a = a * a;
        pow >>= 1;
    }
    return c;
}

ll quickp( ll x )
{
    ll s = 1, c = 2;
    while( x ) {
        if( x & 1 ) s = ( s * c ) % m;
        c = ( c * c ) % m;
        x >>= 1;
    }
    return s;
}
int main()
{
    Mat p, a;
    p.mat[0][0] = 0, p.mat[0][1] = 1;
    p.mat[1][0] = 1, p.mat[1][1] = 1;
    a.mat[0][0] = 1, a.mat[0][1] = 2;
    a.mat[1][0] = 0, a.mat[1][1] = 0;
    cin >> n >> k >> l >> m;

    ll ans = 0;
    if(  l == 64 || ( 1uLL << l ) > k  ) {
        ans++;
        p = p ^ n;
        a = a * p;
        ll t1 = a.mat[0][0], t2 = ( m + quickp( n ) - t1 ) % m;
        for( int i = 0; i < l; ++i ) {
            if( k & ( 1uLL << i ) ) ans = ( ans * t2 ) % m;
            else ans = ( ans * t1 ) % m;
        }
    }

    cout << ans%m << endl;

}


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

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
typedef long long ll;
typedef pair<int, int> point;

const int MAXN = 54;

int dp[MAXN][MAXN][MAXN][MAXN];
string s[MAXN];
int n;

int getNum(char c){
    if(c >= '2' && c <= '9')
        return c - '2';
    if(c == 'T') return 8;
    if(c == 'J') return 9;
    if(c == 'Q') return 10;
    if(c == 'K') return 11;
    if(c == 'A') return 12;
}

int getSign(char c){
    if(c == 'S') return 0;
    if(c == 'D') return 1;
    if(c == 'H') return 2;
    if(c == 'C') return 3;
}

int getVal(string x){
    return getNum(x[0]) + getSign(x[1]) * 13;
}

const char num[] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
const char sign[] = {'S', 'D', 'H', 'C'};

string returnVal(int x){
    int cnt = 0;
    while(x - (cnt + 1) * 13 >= 0)
        cnt ++;
    x -= cnt * 13;
    string ret = "";
    ret += num[x];
    ret += sign[cnt];

    return ret;
}

void solve(int a, int b, int c){
    string A = returnVal(a);
    string B = returnVal(b);
    string C = returnVal(c);

    if(C[0] != B[0] && C[1] != B[1]) return;
    if(C[0] != A[0] && C[1] != A[1]) return;
    cout << "YES\n";
    exit(0);
}

void rek(int pos, int a, int b, int c){
    if(pos == 0){
        solve(a, b, c);
        return;
    }
    if(dp[pos][a][b][c]) return;

    string A = returnVal(a);
    string B = returnVal(b);
    string C = returnVal(c);

    dp[pos][a][b][c] = 1;

    if(C[0] == B[0] || C[1] == B[1])
        rek(pos - 1, getVal(s[pos - 1]), a, c);
    if(C[0] == s[pos - 1][0] || C[1] == s[pos - 1][1])
        rek(pos - 1, c, a, b);
}

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

    cin >> n;
    REP(i, n){
        cin >> s[i];
        int val = getVal(s[i]);
    }
    if(n == 1){
        cout << "YES\n";
        exit(0);
    }
    if(n == 2){
        if(s[0][0] == s[1][0] || s[0][1] == s[1][1]) cout << "YES\n";
        else cout << "NO\n";
        exit(0);
    }

    rek(n - 3, getVal(s[n - 3]), getVal(s[n - 2]), getVal(s[n - 1]));

    cout << "NO\n";
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,k,qq;
int a[110],b[110],c[110];
bool vis[110];
bool judge(){
	for(int i=1;i<=n-1;i++)
		if(a[i]>a[i+1])return 0;
	return 1;
}
void dfs(int q){
	if(q==qq+1){
		if(!judge()){
			cout<<"Yes";
			exit(0);
		}
	}
	for(int i=1;i<=k;i++)
		if(!vis[i]){
			a[c[q]]=b[i];
			vis[i]=1;
			dfs(q+1);
			vis[i]=0;
			a[c[q]]=0;
		}
}
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(!a[i])c[++qq]=i;
	}
	for(int i=1;i<=k;i++)cin>>b[i];
	dfs(1);
	cout<<"No";
	return 0;
}


#include <iostream>

using namespace std;

int main(){
	long long x;
	cin >> x;
	x = (x%360 + 360)%360;
	int ans = 0;
	while(x > 45 && x < 315){
		ans++;
		x -= 90;
	}
	cout << ans << "\n";
	return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=310,mod=1e9+7;
int fac[maxn],invf[maxn];
int	n,a[maxn],ans;
int cs=0,c[maxn];
bool vis[maxn];
int cnt[maxn];
int f[maxn][maxn][maxn];
inline void Add(int &x,int y) { (x+=y)>=mod?x-=mod:0; }
inline void Mul(int &x,int y) { x=1LL*x*y%mod; }
inline bool is_square(long long x) { return fabs((long long)sqrt(x)*(long long )sqrt(x)-x)<1e-8; }
inline int Pow(int x,int k)
{
	int s=1;
	while(k)
	{
		if(k&1)
			Mul(s,x);
		Mul(x,x);
		k>>=1;
	}
	return s;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("data.in","r",stdin);
#endif
	int i,j,k,l;
	for(i=fac[0]=invf[0]=1;i<maxn;i++)
	{
		fac[i]=1LL*i*fac[i-1]%mod;
		invf[i]=Pow(fac[i],mod-2);
	}
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	ans=1;
	for(i=1;i<=n;i++)
		if(!vis[i])
		{
			vis[i]=1;
			c[++cs]=1;
			for(j=i+1;j<=n;j++)
				if(is_square(1LL*a[i]*a[j]))
					vis[j]=1,c[cs]++;
		}
	//printf("%d\n",ans);
	//for(i=1;i<=cs;i++) printf("%d ",c[i]); printf("\n");

	int curs,curs0;
	curs=c[1];
	f[curs][c[1]+1][c[1]-1]=fac[c[1]];
	for(i=2;i<=cs;i++)
	{
		for(j=1;j<=curs+1;j++)
			for(k=0;k<=curs;k++)
				Add(f[curs][0][k],f[curs][j][k]),f[curs][j][k]=0;
		curs0=curs,curs+=c[i];
		for(l=0;l<c[i];l++,curs0++)
			for(j=0;j<=curs;j++)
				for(k=0;k<=curs;k++)
				{
					int ch=curs0+1,bk=l-(j-l);
					Add(f[curs0+1][j+1][k+1],1LL*f[curs0][j][k]*j%mod);
					if(k) Add(f[curs0+1][j+2][k-1],1LL*f[curs0][j][k]*(k-bk)%mod);
					Add(f[curs0+1][j+2][k],1LL*f[curs0][j][k]*(ch-j-(k-bk))%mod);
				}
		//for(j=0;j<=curs;j++) { for(k=0;k<=curs;k++) printf("%d ",f[curs][j][k]); printf("\n"); } printf("\n");
	}
	int s=0;
	for(i=0;i<=curs+1;i++)
		Add(s,f[n][i][0]);
	Mul(ans,s);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>

#define rep(i,n) for(i=1;i<=n;i++)
#define Rep(i,n) for(i=0;i<n;i++)
#define For(i,a,b) for(i=a;i<=b;i++)

#define pb(x) push_back(x)
#define sz(x) x.size()

#define mem(ara,val) memset(ara,val,sizeof(ara))
#define eps 1e-9
#define FI freopen("in.txt","r",stdin)

#define si(x) scanf("%d",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define ss(ch) scanf("%s",ch)
#define pi(x) printf("%d",x)
#define pii(x,y) printf("%d %d",x,y)
#define piii(x,y,z) printf("%d %d %d",x,y,z)
#define pl(x) printf("%lld",x)
#define pll(x,y) printf("%lld %lld",x,y)
#define plll(x,y,z) printf("%lld %lld %lld",x,y,z)
#define ps(ch) printf("%s",ch)
#define Afridi 0
#define NL printf("\n")
#define debug(x) printf("wow  %d !!\n",x)
#define Max 100000
#define INF INT_MAX
#define mod 1000000007

typedef long long LL;
typedef unsigned long long ULL;

using namespace std;

LL bigmod(LL b,LL p)
{
    if(p == 0)return 1;
    LL my = bigmod(b,p/2);
    my*=my;
    my%=mod;
    if(p & 1)my*=b,my%=mod;
    return my;
}
int setb(int n,int pos)
{
    return n=n | (1 << pos);
}
int resb(int n,int pos)
{
    return n=n & ~(1 << pos);
}
bool checkb(int n,int pos)
{
    return (bool)(n & (1 << pos));
}

LL ara[Max],L[Max],n,bra[Max],N,cnt[Max];
//LL par[Max];

LL LIS()
{
    LL i,j;
    for(i=1;i<=N;i++)
    {
        L[i] = 1;
        //par[i] = i;
    }
    for(i=2;i<=N;i++)
    {
        for(j=1;j<i;j++)
        {
            if(ara[i] >= ara[j] && L[i] <= L[j])
            {
                L[i] = L[j] + 1;
                //par[i] = j;
            }
        }
    }
    LL mx = 0;
    //LL idx;
    for(i=1;i<=N;i++)
    {
        if(L[i] > mx)
        {
            mx = L[i];
            //idx = i;
        }
    }
    return mx;
}

int main()
{
    //FI;
    LL i,j,t;
    sll(n,t);
    rep(i,n)
    {
        sl(bra[i]);
        cnt[ bra[i] ]++;
    }
    LL d = min(n,t);
    N = n * d;
    Rep(j,d)
    {
        rep(i,n)
        {
            ara[j*n+i] = bra[i];
            //printf("%lld ",bra[i]);
        }
    }
    LL my = LIS();
    if(t > d)
    {
        LL mx = 0;
        rep(i,n)
        {
            if(cnt[ bra[i] ] > mx)mx = cnt[ bra[i] ];
        }
        my += (t - d) * mx;
    }
    pl(my);NL;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <iterator>
#include <set>
using namespace std;
#define ll long long

const int MAXN=2e5+5;
int n;
ll a[7005];
ll b;
ll c[7005];
ll d[7005];
int cnt;
map<ll ,int>m;
map<ll ,ll>mm;
map<ll,int >mmm;
int main()
{
    ios::sync_with_stdio(false);
    scanf ("%d" , &n ) ;
    for (int i = 0 ; i <n ; ++i)
    {
        scanf ("%lld" , &a[i]) ;
    }
    for (int i = 0 ; i <n ; ++i)
    {
        scanf ("%lld" , &b);
        ++m[a[i]];
        mm[a[i]]+=b;
    }
    for( map<ll,ll>::reverse_iterator it=mm.rbegin();(it)!=mm.rend();it++){
        c[cnt]=it->first;
        d[cnt++]=it->second;
        //cout<<c[cnt-1]<<' '<<d[cnt-1]<<endl;
    }
    ll sum=0;
    for(int i=0;i<cnt;i++)
    {
        if(mmm[c[i]])continue;
        if(m[c[i]]<=1)continue;
        for(int j=i;j<cnt;j++)
        {
            if(mmm[c[j]])continue;
            int f=1;
            for(int k=0;k<61;k++)
            {
                ll kk=1ll<<k;
                if(kk>c[j])break;
                if((c[i]&kk)<(c[j]&kk))
                {
                    f=0;
                    break;
                }
            }
            if(f){
                sum+=d[j];
                mmm[c[j]]=1;
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include <cstdio>

int main(){
    long t; scanf("%ld", &t);
    while(t--){
        long cnt(0);
        for(int row = 0; row < 2; row++){
            for(int col = 0; col < 2; col++){
                long x; scanf("%ld", &x);
                cnt += x;
            }
        }
        long res(0);
        if(cnt == 4){res = 2;}
        else if(cnt > 0){res = 1;}
        printf("%ld\n", res);
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<malloc.h>
using namespace std;

char str[200];
char *ans = (char *)malloc(200 * sizeof(char)) ;

int main()
{
//    freopen("data.txt","r",stdin);
    cin.getline(str, 200, '\n');
    int b = 0, len;
    len = strlen(str);
    int mul = 1;
    for(int i = len - 1; i >= 0; --i){
        if(str[i] == 'e')break;
        b += (str[i] - '0') * mul;
        mul *= 10;
    }
    int dot = 0;
    for(; dot < len; ++dot){
        if(str[dot] == '.')break;
    }
    dot += b;
    int tot = 0;
    for(int i = 0; i < len; ++i){
        if(str[i] == '.')continue;
        if(str[i] == 'e')break;
        if(dot == 0){ans[tot++] = '.';}
        ans[tot++] = str[i];
        dot --;
    }
    for(; dot > 0; --dot){
        ans[tot++] = '0';
    }
    ans[tot++] = '\0';
    while(ans[0] == '0'){
        if(ans[1] == '.')break;
        ans = &ans[1];
    }
    len = strlen(ans);
    if(ans[len - 1] == '0' && ans[len - 2] == '.')len -= 2;
    ans[len] = '\0';
    puts(ans);
    return 0;
}


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

#define PB push_back
#define MP make_pair
#define A first
#define B second
#define eps 1e-8

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;

int n, a[4];
int dp[310][310][310], vis[310][310];

void prepare() {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <300; i ++ )
        for (int j = 0; j < 300; j ++ )
            for (int k = 0; k < 300; k ++ )
                if (dp[i][j][k] != 0) continue;
                else {
                    for (int l = i + 1; l < 300; l ++ )
                        dp[l][j][k] = 1;
                    for (int l = j + 1; l < 300; l ++ )
                        dp[i][l][k] = 1;
                    for (int l = k + 1; l < 300; l ++ )
                        dp[i][j][l] = 1;
                    for (int l = 1; i + l < 300 && j + l < 300 && k + l < 300; l ++ )
                        dp[i + l][j + l][k + l] = 1;
                }
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < 300; i ++ )
        for (int j = 0; j < 300; j ++ )
            if (vis[i][j] == 0) {
                for (int k = i + 1; k < 300; k ++ )
                    vis[k][j] = 1;
                for (int k = j + 1; k < 300; k ++ )
                    vis[i][k] = 1;
                for (int k = 1; k + i < 300 && k + j < 300; k ++ )
                    vis[i + k][j + k] = 1;
            }
}

int main() {
    prepare();
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
        scanf("%d", &a[i]);
    if (n == 1)
        if (a[0] != 0) printf("BitLGM\n");
        else printf("BitAryo\n");
    if (n == 2)
        if (vis[a[0]][a[1]] == 1) printf("BitLGM\n");
        else printf("BitAryo\n");
    if (n == 3)
        if (dp[a[0]][a[1]][a[2]] == 1) printf("BitLGM\n");
        else printf("BitAryo\n");
    return 0;
}