#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

map<int, string> M;
string Type[6] = {"first", "second", "illegal", "the first player won", "the second player won", "draw"};
vector<int> S(9, 0);

string getType()
{
    if(S[0] == S[1] && S[1] == S[2])
    {
        if(S[0] == 1)
            return Type[3];
        if(S[0] == 2)
            return Type[4];
    }
    if(S[3] == S[4] && S[4] == S[5])
    {
        if(S[3] == 1)
            return Type[3];
        if(S[3] == 2)
            return Type[4];
    }
    if(S[6] == S[7] && S[7] == S[8])
    {
        if(S[6] == 1)
            return Type[3];
        if(S[6] == 2)
            return Type[4];
    }
    if(S[0] == S[3] && S[3] == S[6])
    {
        if(S[0] == 1)
            return Type[3];
        if(S[0] == 2)
            return Type[4];
    }
    if(S[1] == S[4] && S[4] == S[7])
    {
        if(S[1] == 1)
            return Type[3];
        if(S[1] == 2)
            return Type[4];
    }
    if(S[2] == S[5] && S[5] == S[8])
    {
        if(S[2] == 1)
            return Type[3];
        if(S[2] == 2)
            return Type[4];
    }
    if(S[0] == S[4] && S[4] == S[8])
    {
        if(S[0] == 1)
            return Type[3];
        if(S[0] == 2)
            return Type[4];
    }
    if(S[2] == S[4] && S[4] == S[6])
    {
        if(S[2] == 1)
            return Type[3];
        if(S[2] == 2)
            return Type[4];
    }

    vector<int> a(2, 0);

    for(int i = 0; i < 9; i++)
        a[S[i]]++;

    if(a[0] > a[1])
    {
        if(a[0] == 5)
            return Type[5];
        else
            return Type[1];
    }
    else
        return Type[0];
}

int getState(vector<int> &v)
{
    int st = 0, p = 1;
    for(int i = 0; i < 9; i++, p *= 3)
        st += v[i] * p;
    return st;
}

void play(int p)
{
    string type = getType();
    int state = getState(S);
    M[state] = type;

    if(type == Type[3] || type == Type[4])
        return;

    for(int i = 0; i < 9; i++)
        if(S[i] == 0)
        {
            S[i] = p;
            play(p==1?2:1);
            S[i] = 0;
        }
}

int main()
{
    play(1);

    string str = "", temp = "";
    for(int i = 0; i < 3; i++)
    {
        cin >> temp;
        str += temp;
    }
    vector<int> v(9, 0);
    for(int i = 0; i < 9; i++)
        v[i] = str[i] == 'X' ? 1 : (str[i] == '0' ? 2 : 0);

    int st = getState(v);

    map<int, string>::iterator it;
    it = M.find(st);
    if(it != M.end())
        cout << it->second;
    else
        cout << "illegal";
}


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

LL helper()
{
    LL n, a, b, p, q;
    cin >> n >> a >> b >> p >> q;
    LL cnta = 0, cntb = 0;
    unordered_set<LL> com;
    for (LL i = a; i <= n; i += a)
    {
        if (i % b == 0)
        {
            com.emplace(i);
        }
        else
        {
            cnta++;
        }
    }

    for (LL j = b; j <= n; j += b)
    {
        if (j % a == 0)
        {
            com.emplace(j);
        }
        else
        {
            cntb++;
        }
    }

    int c = com.size();
    LL ans = cnta * p + cntb * q;
    if (p > q)
        ans += c * p;
    else
        ans += c * q;
    return ans;
}

signed main()
{

    cout << helper();
}


#include<bits/stdc++.h>
#define ll long long int
#define pb(x) push_back(x)
#define un unsigned
#define dbl double
#define loop(s,n) for(int i=s;i<n;i++)
using namespace std;

int tonum(char ch)
{
    if(isalpha(ch)) return ch-'A'+10;
    return ch-'0';
}

bool check(string a, string b,int r)
{
    int asum=0;
    loop(0,a.size())
    {
        asum = asum*r + tonum(a[i]);
    }
    int bsum=0;
    loop(0,b.size())
    {
        bsum = bsum*r + tonum(b[i]);
    }
    if(asum<24 && bsum<60 && asum>=0 && bsum>=0) return true;
    return false;
}

string remove(string s)
{
    int f=0;
    string a="";
    loop(0,s.size())
    {
        if(s[i]!='0' && f==0) f=1;
        if(f) a+= s[i];
    }
    return a;
}

int main()
{
    string s,a="",b="";
    cin>>s;
    int n=s.size(),f=0;
    char ch='.';
    for(int i=0;i<n;i++)
    {
        if(s[i]==':') f=1;
        else
        {
            if(ch<s[i]) ch=s[i];
            if(!f) a += s[i];
            else b += s[i];
        }
    }
    a =remove(a);
    b =remove(b);
    int r;
    if(isalpha(ch)) r=ch-'A'+10;
    else r=ch-'0';
    r++;
    vector<int> v;
    if(a.size()==1 && b.size()==1)
    {
        cout<<-1<<endl;
        return 0;
    }
    while(check(a,b,r))
    {
        v.pb(r);
        r++;
    }
    if(v.size()==0) cout<<0<<endl;
    else
    {
        loop(0,v.size()) cout<<v[i]<<" ";
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <cstdio>
#include <iomanip> // setpreciosn
using namespace std;

#define forall(i,a,b) for(int i=a;i<b;++i)

double arr[20001][2001]={0};

double calculate(int n,double p,int t){
    if(t==0) return n;
    else if(n==0) return 0;
    else {
        if(arr[n-1][t-1] == 0) arr[n-1][t-1] = calculate(n-1,p,t-1);
        if(arr[n][t-1] == 0) arr[n][t-1] = calculate(n,p,t-1);
        return (p*arr[n-1][t-1]+(1-p)*arr[n][t-1]);
    }
}

int main(){
    int n,t;
    double p; cin >> n >> p >> t;
    double ans = 0;
    ans = calculate(n,p,t);
    cout << setprecision(10) << (n-ans) << endl;
}


#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>

using namespace std;
long n,c, i ,j ,k, a[1000],dif[500], savepos;
int main()
{
    while(1)
    {
    cin>>n>>c;
        int max = 0;
        for(i = 0; i<n; i++)
        {
            cin>>a[i];
            if(i > 0)
            {
                dif[i-1]= a[i-1] - a[i];
                if(dif[i-1] > max)
                {
                    max = dif[i-1];
                    savepos = i - 1;
                }
            }
        }

        if(max > 0)
        {
            //printf("%ld\n", (max - c));
            cout<<(max-c)<<endl;
        }
        else cout<<"0"<<endl;
    }
    return 0;
}


#include <stdio.h>
int main(){
    int n, m = 1;
    scanf("%x",n);
    printf("%d", m);
    return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char c[20][20];
bool row[20], col[20];
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
		{
			scanf(" %c", &c[i][j]);
			if (c[i][j] == 'S')
				row[i] = true, col[j] = true;
		}
	int ans = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			if (!row[i] || !col[j])
				++ans;
	printf("%d", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool ip[1003099];
vector<int>pj,p;
void sieve(int b)
{
	for(int i=2;i*i<=b;i++)
	{
		if(!ip[i]){
			for(int j=2;j*i<=b;j++)ip[i*j]=1;
		}
	}
	for(int i=1;i<=b;i++)if(ip[i])p.push_back(i);
	for(int i=2;i<=p.size();i++)pj.push_back(p[i]-p[i-1]);
}
int main()
{
	int k,a,b;
	cin>>a>>b>>k;
	sieve(b);
	int ma1x=0,ssn=0;
	for(int i=1;i<=pj.size()-k;i++)
	{
		for(int j=1;j<=k;j++)ssn+=pj[i+j];
		ma1x=max(ssn,ma1x);
	}
	cout<<ma1x;
}


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

int n, k, d;

struct node{
	int val;
	bool hasd;
	struct node** children;
};

int count(struct node* tree){
	if(tree->val == n){if(tree->hasd)return 1; else return 0;}
	if(tree->val > n){return 0; }
	tree->children = new struct node*[k];
	int ret = 0;
	for(int i =0; i<k; i++){
	    tree->children[i] = new struct node;
		tree->children[i]->val = (tree->val + i +1);
		tree->children[i]->hasd = ((tree->hasd)+((i+1) >= d));
		tree->children[i]->children = NULL;
		ret += count(tree->children[i]);
		ret %= 1000000007;
	}
	return ret;
}

int main(){
	cin>>n>>k>>d;
	struct node* tree = new struct node;
	tree->val = 0;
	tree->hasd = false;
	tree->children = NULL;

	int ans = count(tree);
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
#define lli long long int
#define mod (1000000000 + 7)

using namespace std;

lli gcd(lli a, lli b)
{
    if(a == 0) return b;
    return gcd(b % a, a);
}
lli lcm(lli a, lli b)
{
    return a / gcd(a, b) * b;
}

int main()
{
	ios_base::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
	#endif

    lli n, k, a, b;
    cin >> n >> k >> a >> b;
    lli x = INT_MAX, y = 0;

    for(lli i = 1; i <= n; i++)
    {
        lli l1 = i*k + a + b;
        lli l2 = i*k + a - b;
        lli l3 = i*k - a + b;
        lli l4 = i*k - a - b;
        lli c1 = lcm(l1, n*k) / l1;
        lli c2 = lcm(l2, n*k) / l2;
        lli c3 = lcm(l3, n*k) / l3;
        lli c4 = lcm(l4, n*k) / l4;
        /* cout << c1 << " " << c2 << " "<< c3 << " "<< c4 << endl; */
        x = min(x, min(c1, min(c2, min(c3, c4))));
        y = max(y, max(c1, max(c2, max(c3, c4))));
    }
    cout << x << " " << y << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int N,M,x=0,y=0,i=0;
	cin>>N>>M;
	vector <int> a(N),b;
	for(int k=0;k<N;k++)
		cin>>a[k];
	if(a[0]%2==1)
		x++;
	else
		y++;
	for(int k=1;k<N-2;k++){
		if(a[k]%2==1)
			x++;
		else
			y++;
		if(x==y){
			b.push_back(abs(a[k]-a[k-1]));
			x=y=0;
		}
	}
	sort(b.begin(),b.end());
	int ans=0,T=b.size(),s=0;
	for(int k=0;k<T;k++){
		if(ans+b[k]<=M){
				ans+=b[k];
				M-=ans;
				s++;
		}
	}
	cout<<s;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    string s;
    cin >> s;
    int x = s.size();
    assert(x & 1);
    if(s[x/2]%2!=0&&x%2!=0){cout<<"YES";}
    else if(s[x/2]%2==0&&x%2==0){cout<<"YES";}
    else if(s[x/2]%2!=0&&x%2==0){cout<<"YES";}
    else{cout<<"NO";}
    return 0 ;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<pii> vii;
typedef vector<vector<pii> > vvii;
typedef long double ld;
typedef map<int, int> mapii;
typedef map<string, int> mapsi;

#define erep(i, x, n) for (auto i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for(auto i = x; i<(ll)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pi  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-8)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)
#define mp(x, y, z) { x, {y, z} }

const int N = 2e7 + 5, mod = 1e9 + 7;
ll vals[N], accum[N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
//	ll p = 0, q = 5;
//	erep(i, 1, 1000)
//	{
//		if (i % q == 0)
//			continue;
//
//		if (((i / q) / (i % q)) == 2)
//		{
//			p += i;
//			cout << i << " " << i / q << " " << i % q << endl;
//
//		}
//	}
//	cout << p << endl;
//	return 0;

	ll a, b;
	cin >> a >> b;
	rep(i, 1, N)
	{
		vals[i] = vals[i - 1] + b;
		if (vals[i] >= mod)
			vals[i] -= mod;
		accum[i] = accum[i - 1] + vals[i];
		if (accum[i] >= mod)
			accum[i] -= mod;
	}

	ll sum = 0, temp = (b * (b - 1)) / 2, extra = -1, obba7 = 0, zawwed = 0,
			prev;
	for (ll i = 1; i < b; i++)
		zawwed = (zawwed + i * i) % mod;
	erep(i, 1, a)
	{
		if (extra != -1)
		{
			sum = (sum + prev + obba7) % mod;
			prev = sum;
			continue;
		}

		if (i == 1)
		{
			rep(j, 1, b)
			{
				int l = j - 1, r = (2 * j) - 1;
				sum = (sum + (accum[r] - accum[l])) % mod;
			}
			sum = (sum + zawwed) % mod;
		}
		else
		{
			extra = 0;
			ll hoba = temp;
			rep(j, 1, b)
			{
				int l = (2 * j) - 1, r = min(l + j * 1ll, l + hoba);
				if (j == 1)
					r = j + 1, l = 1;
				extra = (extra + (accum[r] - accum[l])) % mod;
				hoba -= j;
			}
			extra = (extra + zawwed) % mod;
			obba7 = mod(extra - sum, mod);
//			cout << "--> " << extra << " " << obba7 << endl;
//			cout << extra << endl;
			sum = (sum + extra) % mod;
			prev = extra;
		}
	}
	cout << sum;

	return 0;
}


#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;
struct node
{
    int x;
    int step;
}q,p;

queue <node> s;

int main()
{
    int n;
    while(cin>>n)
    {
        while(!s.empty())s.pop();
        q.x=0;
        q.step=0;
        s.push(q);
        while(!s.empty())
        {
            p=s.front();
            s.pop();
            if(p.x==n)
            {
                printf("%d\n",p.step);
                break;
            }
            int f=0;
            for(int i=1;i<=5;i++)
            {
                q.x=p.x+i;
                q.step=p.step+1;
                if(q.x==n)
                {
                    printf("%d\n",q.step);
                    f=1;
                    break;
                }
                s.push(q);
            }
            if(f==1)
            {
                break;
            }
        }
    }
    return 0;
}


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

using namespace std;

typedef struct node
{
    int v;
    int step;
};

int BFS(int n)
{
    queue<node>q;
    node t,p;
    t.v=0;
    t.step=0;
    q.push(t);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(t.v==n)
            return t.step;
        for(int i=1; i<=5; i++)
        {
            p.v=t.v+i;
            p.step=t.step+1;
            if(p.v==n)
                return p.step;
            q.push(p);
        }
    }
}

int main()
{
    int x;
    scanf("%d",&x);
    printf("%d\n",BFS(x));
}


#include <cstdio>
#define eps 1e-8
int vp,vd,t,f,c;
int ans=0;
double Prin;
int main(){
    scanf("%d%d%d%d%d",&vp,&vd,&t,&f,&c);
    Prin=t*vp;
    if (vp>vd) {
        puts("0");
        return 0;
    }
    while (Prin<c){
        double next=Prin/(vd-vp);
        Prin+=next*vp;
        if (Prin<=c) break;
        ans++;
        next+=f;
        Prin+=vp*next;
    }
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
using namespace std;

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

    int g,p,b;
    cin>>g>>p>>b;

    int l = g - a;
    if(l>=0){
        int l2 = d - l -p;
        if(l2>=0){
            if(m - (l2) - b > 0=){
                cout<<"YES"<<endl;
            }
            else{
                cout<<"NO"<<endl;
            }
        }
        else{
            cout<<"NO"<<endl;
        }
    }else{
        cout<<"NO"<<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
const int MaxN = 2e5 + 17, INF = -1e9 + 17;
long long h, Ans, l, r, can, x, m;
int main ()
{
    cin >> h >> x;
    l = 1;
    r = (1 << h);
    for (long long i = 0; i < h; ++ i)
    {
        m = (l + r) >> 1LL;
    	if (!can)
    	{
    		if (x > m)
    			l = m + 1LL, Ans += (1LL << (h - i)) - 1LL;
    		else
    			r = m, can = 1;
    	}
    	else
    	{
    		if (x <= m)
    			r = m, Ans += 1LL << (h - i)) - 1LL;
    		else
    			l = m + 1LL, can = 0;
    	}
    }
    cout << Ans + h;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,a;
string s;
int main()
{
	cin>>n>>m;
	n%=10;
	for(int i=1;;i++)
	{
		a+=n;
		a%=10;
		if(!(a-m))
		{
			cout<<i;
			return 0;
		}
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

//--------------------------------------------
#define SIZE(x) (x.size())
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define ALL(X) X.begin(), X.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define FILL(x, value) memset(x, value, sizeof(x))

#define next next1
#define hash hash1
#define rank rank1
#define count count1

#ifdef _DEBUG_
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

#if((_WIN32 || __WIN32__) && __cplusplus < 201103L)
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

const double EPS = 1e-9;
const int IINF = 1000000000;
const double PI = acos(-1.0);
const long long LINF = 6000000000000000000LL;
//--------------------------------------------

const int maxN = 200 * 1000 + 7;

int n, k, E[maxN], P[maxN], CP[maxN], W[maxN];

bool Read() {
    if(scanf("%d%d", &n, &k) == 2) {
        for(int i = 0; i < n; ++i) {
            scanf("%d%d", P + i, E + i);
        }
        return true;
    }
    return false;
}

bool cmp(int i, int j) {
    if(i == n || j == n) {
        if(i == n) {
            if(CP[i] != CP[j]) return CP[i] > CP[j];
            if(W[j]) return true;
            return false;
        }
        else if(j == n) {
            if(CP[i] != CP[j]) return CP[i] > CP[j];
            if(W[i]) return false;
            return true;
        }
    }
    if(CP[i] != CP[j])return CP[i] > CP[j];
    return i > j;
}

long long can(int mask) {
    vector< pair<int, int> > v;
    v.reserve(n);
    for(int i = 0; i < n; ++i) {
        v.pb(mp(E[i], P[i]));
    }
    SORT(v);
    FILL(W, 0);
    FILL(CP, 0);
    long long ans = 0;
    for(int i = 0; i < v.size(); ++i) {
        if(mask & (1 << i)) {
            ans += v[i].first;
            CP[n]++;
            W[i] = 1;
            CP[i] = v[i].second;
        }
        else {
            CP[i] = v[i].second + 1;
        }
    }
    vector<int> srt;
    for(int i = 0; i < n; ++i) {
        srt.pb(i);
    }
    srt.pb(n);
    sort(srt.begin(), srt.end(), cmp);
    for(int i = 0; i < srt.size(); ++i) {
        if(srt[i] == n) {
            if(i + 1 <= k) {
                return ans;
            }
            return LINF;
        }
    }
    return LINF;
}

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

    while(Read()) {
        long long ans = LINF;
        for(int mask = 0; mask < (1 << n); ++mask) {
            ans = min(ans, can(mask));
        }
        if(ans >= LINF) printf("-1\n");
        else printf(LLD"\n", ans);
    }
    return 0;
}


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

#define mod 12345
#define LL long long

struct node {
    LL v[30];
    bool operator < (const node& rhs) const{
        for (int i = 1; i <= 26; i ++){
            if (v[i] == rhs.v[i])
                continue;
            return v[i] < rhs.v[i];
        }
        return 0;
    }
}prepare;
struct edge {
    int r, c;
    LL jz[150][150];
    edge operator * (const edge& rhs) const{
        edge ans;
        ans.r = r, ans.c = rhs.c;
        for (register int i = 0; i <= ans.r; i ++)
            for (register int j = 0; j <= ans.c; j ++)
                ans.jz[i][j] = 0;
        for (register int i = 1; i <= r; i ++)
            for (register int j = 1; j <= rhs.c; j ++)
                for (register int k = 1; k <= c; k ++)
                    ans.jz[i][j] = (ans.jz[i][j] + jz[i][k] * rhs.jz[k][j] % mod) % mod;
        return ans;
    }
}A;
vector <int> G[30];
map <node, int> m;
int n, c, maxn[30], cnt;
LL ans;

void dfs(int pos,node now)
{
    if(pos==27)
    {
        m[now]=++cnt;
        return ;
    }
    if(maxn[pos]==0)dfs(pos+1,now);
    for(int i=0;i<maxn[pos];i++)
    {
        now.v[pos]=i;
        dfs(pos+1,now);
    }
}
void calc(int pos,node now)
{
    if(pos==27)
    {
        ans+=A.jz[1][m[now]];
        ans%=mod;
        return ;
    }
    if(maxn[pos]==0)calc(pos+1,now);
    for(int i=0;i<maxn[pos];i++)
    {
        bool flag=false;
        for(int j=0;j<G[pos].size();j++)
        if(i%G[pos][j]==0){
            flag=true;
            break;
        }
        if(flag)
        {
            now.v[pos]=i;
            calc(pos+1,now);
        }
    }
}
inline edge qkpow (edge x, LL y){
    edge ans;
    ans.r = ans.c = x.r;
    for (register int i = 1; i <= ans.r; i ++)
        for (register int j = 1; j <= ans.c; j ++)
            if (i != j)
                ans.jz[i][j] = 0;
            else
                ans.jz[i][j] = 1;
    while (y){
        if (y & 1)
            ans = ans * x;
        x = x * x;
        y >>= 1;
    }
    return ans;
}
int main (){
    scanf ("%d %d", &n, &c);
    if (! n){
        printf ("1\n");
        return 0;
    }
    if (! c){
        printf ("0\n");
        return 0;
    }
    bool vis[30] = {};
    for (int i = 1; i <= c; i ++){
        char a;
        int b;
        scanf ("%s %d", &a, &b);
        if (! b)
            vis[a - 'A' + 1] = 1;
        if (! maxn[a - 'A' + 1]){
            maxn[a - 'A' + 1] = b;
        }
        else{
            maxn[a - 'A' + 1] *= b;
        }
        G[a - 'A' + 1].push_back (b);
    }
    for (int i = 1; i <= 26; i ++)
        if (vis[i])
            maxn[i] = 0;
    dfs (1, prepare);
    for(map<node,int>::iterator i=m.begin();i!=m.end();i++)
    {
        int s=i->second;
        node now=i->first;
        for(int j=1;j<=26;j++)
        {
            if(maxn[j]==0)continue;
            node temp=now;
            temp.v[j]=(temp.v[j]+1)%maxn[j];
            A.jz[s][m[temp]]++;
        }
    }
    A.r = A.c = cnt;
    A = qkpow (A, n);
    calc (1, prepare);
    printf ("%lld\n", ans);
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int a[5];
    int sum=0;
    for(int i=0;i<5;i++)
    {
            cin>>a[i];
            sum+=a[i];
            }
            if(sum==0)
           {
            cout<<"-1";}

            if(sum%5==0)
            {
            cout<<sum/5;}
            else
            {
            cout<<"-1";}

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=1e9+7;
const int maxn=1000;
int n,m,k;
long long mul[1010],inv[1010],mulinv[1010];
long long f[1010][1010];
long long power(long long x,int p){
	long long ret=1;
	while(p){
		if(p&1){
			ret=(ret*x)%mod;
		}
		x=(x*x)%mod;
		p>>=1;
	}
	return ret;
}
long long c(int a,int b){
	if(a<0||b<0)return 0;
	if(a<b)return 0;
	if(b==0)return 1;
	if(a==b)return 1;
	return mul[a]*mulinv[b]%mod*mulinv[a-b]%mod;
}
int main(){
	freopen("zhangfei.in","r",stdin);
	freopen("zhangfei.out","w",stdout);
	mul[0]=1;
	mulinv[0]=1;
	mul[1]=1;
	inv[1]=1;
	mulinv[1]=1;
	for(int i=2;i<=maxn;i++){
		mul[i]=(mul[i-1]*i)%mod;
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;
		mulinv[i]=(mulinv[i-1]*inv[i])%mod;
	}
	f[0][0]=1;
	for(int i=1;i<=maxn;i++){
		for(int j=1;j<=i;j++){
			f[i][j]=(j*f[i-1][j]%mod+f[i-1][j-1])%mod;
		}
	}
	scanf("%d%d%d",&n,&m,&k);
	if(m==1){
		printf("%lld\n",power(1ll*k,n));
		continue;
	}
	long long ans=0;
	for(int i=1;i<=min(n,k);i++){
		long long Mul=1;
		for(int j=0;j<i;j++){
			Mul=Mul*(k-j)%mod;
		}
		Mul=(Mul*f[n][i])%mod;
		long long mul2=1;
		for(int j=i;j>=0;j--){
			//c(k,i)*i!*f[n][i]*sum(c(i,j)*c(k-i,i-j)*i!*f[n][i]*power(j,(m-2)*n))
			ans=(ans+Mul*c(i,j)%mod*mul2%mod*mulinv[i-j]%mod*mul[i]%mod*f[n][i]%mod*power(1ll*j,(m-2)*n)%mod)%mod;
			mul2=(mul2*(k-i-(i-j)))%mod;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
int main(){
while(1){
    float n,t,k,d;
    cin>>n;
    cin>>t;
    cin>>k;
    cin>>d;

     if(n*(t/k)>t+d)
        cout<<"YES"<<endl;

    else
        cout<<"NO"<<endl;
}

}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
double a[20];
double cal1(double x, double y, double z)
{
    double ans = z * log(y) + log(log(x));
//    printf("x = %f, y = %f, z = %f\n");
//    printf("first part = %f\nsecond part = %f\n", z * log(y), log(log(x)));
//    system("pause");
//    printf("ans = %f\n", ans);
    return ans;
}
double cal2(double x, double y, double z)
{
    double ans = log(y * z * log(x));
    return ans;
}
int solve(double x, double y, double z)
{
    a[1] = cal1(x, y, z);
    a[2] = cal1(x, z, y);
    a[3] = cal2(x, y, z);
    a[4] = cal2(x, z, y);

    a[5] = cal1(y, x, z);
    a[6] = cal1(y, z, x);
    a[7] = cal2(y, x, z);
    a[8] = cal2(y, z, x);

    a[9] = cal1(z, x, y);
    a[10] = cal1(z, y, x);
    a[11] = cal2(z, x, y);
    a[12] = cal2(z, y, x);

//    for(int i = 1 ; i <= 12 ; i++)
//        printf("%f ", a[i]);
//    printf("\n");
    int re = 1;
    double mmax = a[1];
    for(int i = 1 ; i <= 12 ; i++){
        if(a[i] > mmax) mmax = a[i], re = i;
    }
    return re;
}
int out[5];
void print(int mark)
{
    if(mark == 1) printf("x^y^z\n");
    if(mark == 2) printf("x^z^y\n");
    if(mark == 3) printf("(x^y)^z\n");
    if(mark == 4) printf("(x^z)^y\n");

    if(mark == 5) printf("y^x^z\n");
    if(mark == 6) printf("y^z^x\n");
    if(mark == 7) printf("(y^x)^z\n");
    if(mark == 8) printf("(y^z)^x\n");

    if(mark == 9) printf("z^x^y\n");
    if(mark == 10) printf("z^y^x\n");
    if(mark == 11) printf("(z^x)^y\n");
    if(mark == 12) printf("(z^y)^x\n");
}
int main()
{
//    printf("log(3.4) = %f, loglog(1.1) = %f\n", log(3.4), log(log(1.1)));
    double x, y, z;
    while(scanf("%lf%lf%lf", &x, &y, &z)){
//        x *= 1000, y *= 1000, z *= 1000;
        int mark = solve(x, y, z);
        print(mark);
    }
    return 0;
}


// 	Author: Mohib Manva
#include <bits/stdc++.h>
using namespace std;

#define SZ(a) int((a).size())
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define MP make_pair
#define FOR(i,a,b)  for(int i=(a);i<=(b);++i)
#define REP(i,a)    FOR(i,0,(a)-1)
#define FORD(i,n,a) for(int (i)=(n);(i)>=a;(i)--)
#define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define FF first
#define SS second
#define FILL(a, v) memset(a, v, sizeof(a));
#define DREP(a)					sort(ALL(a)); a.erase(unique(ALL(a)),a.end()) // will make the vector unique and sorted order

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

const double PI = acos(-1.0); //M_PI;
// main code starts now.
const int MAXN = (int) 1e5 + 10;

/*  Graph Part

static const int N  = 100001;
static bool v[N];
vector<int> adj[N];

*/
static int n,vis=0;
static char cards[55][5];
vector<int> adj[101];
static bool v[101];
map<std::vector<string>,bool>  dp;
void solve(int cur,std::vector<string> vc){
	if(dp[vc])
		return;
	if(cur==0){
		puts("YES");
		exit(0);
	}
	//printf("%d\n",cur);
    if(cur>0&&(vc[cur-1][0]==vc[cur][0]||vc[cur-1][1]==vc[cur][1])){
    	string temp = vc[cur-1];
    	vc[cur-1] = vc[cur];
    	solve(cur-1,vc);
    	vc[cur-1]  = temp;
    }
    if(cur>2&&(vc[cur-3][0]==vc[cur][0]||vc[cur-3][1]==vc[cur][1])){
    	string temp = vc[cur-3];
    	vc[cur-3] = vc[cur];
    	solve(cur-1,vc);
    	vc[cur-3]  = temp;
    }
    dp[vc] = true;

	return;
}

void read(){
	scanf("%d\n",&n);
	vector<string> vc;
	for(int i=0;i<n;i++){
		string x;
		cin>>x;

		vc.push_back(x);
	}
	solve(n-1,vc);
	printf("NO\n");
}

int main() {
    int T = 1;
    //scanf ("%d", &T);
    while (T--) {
        read();
      //  solve();
    }
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

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

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

int N;
string str;

struct mv {
    int bits = 0;
    int v1, v2;
    void swap(int pos){ //with pos + 1
        int d1 = (bits >> pos) & 1;
        int d2 = (bits >> (pos + 1)) & 1;

        bits &= ~(3 << pos);

        bits |= d2 << pos;
        bits |= d1 << (pos + 1);

        if(v1 == pos || v1 == pos + 1){
            v1 = (pos) + (pos + 1) - v1;
        }

        if(v2 == pos || v2 == pos + 1){
            v2 = (pos) + (pos + 1) - v2;
        }
    }
};

const int MAXN = 19;

int dp[1 << MAXN][MAXN][MAXN];

int solve(mv src){
    //cout << endl;
    //cout << bitset<8>(src.bits) << " " << src.v1 << " " << src.v2 << endl;
    memset(dp, 0x3f, sizeof dp);
    dp[src.bits][src.v1][src.v2] = 0;
    queue<mv> q;
    q.push(src);

    while(!q.empty()){
        mv fr = q.front();
        //cout << bitset<8>(fr.bits) << " " << fr.v1 << " " << fr.v2 << endl;
        int cost = dp[fr.bits][fr.v1][fr.v2];
        q.pop();

        for(int a = 0; a < 19; a++){
            fr.swap(a);

            if(fr.bits & (1 << (N - 1))){
                //cant have front 0
            } else {
                if(cost + 1 < dp[fr.bits][fr.v1][fr.v2]){
                    dp[fr.bits][fr.v1][fr.v2] = cost + 1;
                    q.push(fr);
                }
            }

            fr.swap(a);
        }
    }
}

int solve00(){
    mv src;
    for(int a = 0; a < N; a++){
        if(str[a] == '0'){
            src.bits |= 1 << a;
        }
    }
    src.v1 = -1;
    src.v2 = -1;
    for(int a = N - 1, cnt = 0; a >= 0; a--){
        if(str[a] == '0'){
            if(cnt == 0){
                src.v1 = a;
                cnt++;
            } else if (cnt == 1){
                src.v2 = a;
                break;
            }
        }
    }
    if(src.v1 == -1 || src.v2 == -1){
        return 1e9;
    }

    solve(src);

    int ans = 1e9;
    for(int a = 0; a < (1 << N); a++){
        ans = min(ans, dp[a][N - 2][N - 1]);
    }
    return ans;
}

int solve25(){
    mv src;
    for(int a = 0; a < N; a++){
        if(str[a] == '0'){
            src.bits |= 1 << a;
        }
    }
    src.v1 = -1;
    src.v2 = -1;
    for(int a = N - 1; a >= 0; a--){
        if(str[a] == '2' && src.v1 == -1){
            src.v1 = a;
        }
        if(str[a] == '5' && src.v2 == -1){
            src.v2 = a;
        }
    }
    if(src.v1 == -1 || src.v2 == -1){
        return 1e9;
    }

    solve(src);

    int ans = 1e9;
    for(int a = 0; a < (1 << N); a++){
        ans = min(ans, dp[a][N - 2][N - 1]);
    }
    return ans;
}

int solve50(){
    mv src;
    for(int a = 0; a < N; a++){
        if(str[a] == '0'){
            src.bits |= 1 << a;
        }
    }
    src.v1 = -1;
    src.v2 = -1;
    for(int a = N - 1; a >= 0; a--){
        if(str[a] == '5' && src.v1 == -1){
            src.v1 = a;
        }
        if(str[a] == '0' && src.v2 == -1){
            src.v2 = a;
        }
    }
    if(src.v1 == -1 || src.v2 == -1){
        return 1e9;
    }

    solve(src);

    int ans = 1e9;
    for(int a = 0; a < (1 << N); a++){
        ans = min(ans, dp[a][N - 2][N - 1]);
    }
    return ans;
}

int solve75(){
    mv src;
    for(int a = 0; a < N; a++){
        if(str[a] == '0'){
            src.bits |= 1 << a;
        }
    }
    src.v1 = -1;
    src.v2 = -1;
    for(int a = N - 1; a >= 0; a--){
        if(str[a] == '7' && src.v1 == -1){
            src.v1 = a;
        }
        if(str[a] == '5' && src.v2 == -1){
            src.v2 = a;
        }
    }
    if(src.v1 == -1 || src.v2 == -1){
        return 1e9;
    }

    solve(src);

    int ans = 1e9;
    for(int a = 0; a < (1 << N); a++){
        ans = min(ans, dp[a][N - 2][N - 1]);
    }
    return ans;
}

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

    cin >> str;
    N = str.size();

    int ans = 1e9;
    ans = min(ans, solve00());
    ans = min(ans, solve25());
    ans = min(ans, solve50());
    ans = min(ans, solve75());

    if(ans == 1e9){
        cout << -1;
    } else {
        cout << ans;
    }
}


/*FROME ZHU YUTIAN*/
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <list>
using namespace std;
typedef long long ll;

int main()
{
   int h1,h2;
   int a,b;
   cin>>h1>>h2>>a>>b;
   int d=-1;
   int dup=(a-b)*12;
   int s=0;
   if(dup<=0&&a*8+h1<h2)
   {
      cout<<-1<<endl;
      return 0;
   }
   d=max(h2-a*8,0)/dup;
   cout<<d<<endl;
   return 0;
}


#include<bits/stdc++.h>

using namespace std;

int N, M, p[1000009], p2[1000009];
const int mod = 1e9 + 7;

int mul (int x, int y) {return 1LL * x * y % mod;}
void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}

int main ()
{
scanf ("%d %d", &N, &M), p[0] = p2[0] = 1;
for (int i=1; i<=N; i++)
    p[i] = mul (p[i - 1], M), p2[i] = mul (p2[i - 1], 2 * M - 1);
int ans = power (M, N);
for (int P=1; P<=N; P++)
    adto (ans, mul (p[N - P + 1], p2[P - 1]));
printf ("%d\n", ans);
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define se second
#define fi first
#define ll long long
#define ld long double
#define pll pair<ll ,ll >
#define plll pair<ll,pll>
#define ppll pair<pll, pll>
#define pii pair<int, int>
#define pb push_back
#define mod 10000000007
ll power(ll a,ll b){
    ll res=1ll;
    while(b>0){
        if(b%2!=0) res=(res*a)%mod;
        a=(a*a)%mod;
        b/=2;
    }
    return res%mod;
}
ll ncr(ll n,ll k){
    if(k>n-k) k=n-k;
    ll pro=1;
    for(ll i=0;i<k;i++){
        pro=(pro*(n-i))%mod;
        pro/=(i+1);
    }
    return (pro)%mod;
}

void swap(ll *a,ll *b) {
    ll temp=*a;
    *a=*b;
    *b=temp;
}
vector<string > arr;
void permute(string a, int l, int r)
{
    if (l == r)
        arr.pb(a);
    else {
        for (int i = l; i <= r; i++)  {
            swap(a[l], a[i]);
            permute(a, l+1, r);
            swap(a[l], a[i]);
        }
    }
}
void solve() {
    ll n,m;
    cin>>n>>m;
    ll d1=(log10(n)/log10(7))+1;
    ll d2=(log10(m)/log10(7))+1;
    vector<ll> pow7(12,1);
    for(int i=1;i<11;i++) {
        pow7[i]=pow7[i-1]*7;
    }
    string str="0123456";
    permute(str,0,6);
    set<string > s;
    ll cnt=0;
    for(int i=0;i<arr.size();i++) {
        string sub=arr[i].substr(0,d1+d2);
        if(s.find(sub)!=s.end()) continue;
        s.insert(sub);
        string hrs=sub.substr(0,d1);
        string min=sub.substr(d1);

        ll num1=0,num2=0;
        for(int j=d1-1,k=0;j>=0;j--,k++) {
            num1+=pow7[k]*(hrs[j]-'0');
        }
        for(int j=d2-1,k=0;j>=0;j--,k++) {
            num2+=pow7[k]*(min[j]-'0');
        }
        if(num1<n && num2<m) cnt++;
    }
    cout<<cnt;

}
int main(void){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    t=1;
    while(t--) {
        solve();
    }
    return 0;

}


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

#include <ext/rope>
using namespace __gnu_cxx;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef long long ll;

int n, m, u, v;
int modl;
int prime[150010], cprime, isprime[150010];

int bit(int n, int i) {
    return (n>>i)&1;
}

pair<ll, ll> Count(int k, bool debug = 0) {

    int l = int(sqrt(max(0.0, ll(u-1)*(u-1) - ll(k)*k + 0.0)) + 1e-8) + 1, r = int(sqrt(max(0.0, ll(v)*(v) - ll(k)*k + 0.0)) + 1e-8);
    if (l > r) return make_pair(0, 0);

    vector<int> d; d.clear();
    while (k > 1) {
        int p = isprime[k];
        d.push_back(p);
        while (k%p == 0) k/= p;
    }

    pair<ll, ll> ret = make_pair(0, 0);
    int ds = d.size();
    // if (debug) cout << l << " " << r << " " << ds << endl;

    for(int i = 1; i < (1<<ds); i++) {
        int count = 0;
        int product = 1;
        for(int j = 0; j < ds; j++)
            if (bit(i, j)) {
                count++;
                product *= d[j];
            }
        int L = (l-1)/product;
        int R = r/product;
        if (L >= R) continue;
        if (count&1) {
            ret.first = (ret.first + product * (((ll(R+1)*R - ll(L+1)*L)/2) % modl)) % modl;
            ret.second = (ret.second + (((ll(R+1)*R - ll(L+1)*L)/2) % modl)) % modl;
        }
        else {
            ret.first = (ret.first - product * (((ll(R+1)*R - ll(L+1)*L)/2) % modl)) % modl;
            ret.second = (ret.second - (((ll(R+1)*R - ll(L+1)*L)/2) % modl)) % modl;
        }
        if (ret.first < 0) ret.first += modl;
        if (ret.second < 0) ret.second += modl;

    }
    // if (debug) cout << ret.first << " " << ret.second << endl;
    ret = make_pair(((ll(r+1)*r - ll(l-1)*l)/2 - ret.first) % modl, ((r-l+1) - ret.second) % modl);
    if (ret.first < 0) ret.first += modl;
    if (ret.second < 0) ret.second += modl;
    return ret;
}

int main() {

    #ifndef ONLINE_JUDGE
    freopen("E.txt", "r", stdin);
    #endif

    cprime = 0;
    for(int i = 2; i <= 150000; i++)
        if (!isprime[i]) {
            prime[cprime++] = i;
            for(int j = i; j <= 150000; j++)
                isprime[j] = i;;
        }

    cin >> n >> m >> u >> v >> modl;
    ll ret = 0;
    for(int deltaX = 1; deltaX <= n; deltaX++) {
        pair<ll, ll> tret = Count(deltaX);
        ret = (ret + 2*(n-deltaX+1)*(ll(m+1)*tret.second - tret.first)) % modl;
    }
    n++; m++;
    ret += ll(m)*n*2 - (m+n);
    cout << ret % modl;

    return 0;
}


# include <iostream>

using namespace std;

int main()
{
int n;
cin>>n;
if(n%2==0)
cout<<n/2;
else
cout<< (n-1)/2-n;
}


#include<iostream>

using namespace std;

int sum, n, x;
double answer;
main(){

    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> x;
        sum += x;
        answer = max((double)sum/i, answer);
    }
    cout << fixed << setprecision(6) << answer;
}


#include<iostream>
using namespace std;
int n,a[4][7],m=0;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=6;j++)
	cin>>a[i][j];int f=1;
	while(f!=0)
	{
		m++;
		int f1=0;
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		for(int k=1;k<=6;k++)
		for(int l=1;l<=6;l++)
		if(a[i][k]*10+a[j][l]==m||a[i][k]+a[j][l]*10==m){f1=1;break;}
	}
	cout<<m-1;
	return 0;
}


#pragma warning(disable : 4996)

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

using namespace std;

int main()
{
	int x, y, z;
	cin >> x >> y >> z;
	int days = 0, max = 0;
	for (int i(1); i < 8; i++)
	{
		int d = i;
		int n = x, b = y, c = z;
		bool f = true;
		while (f)
		{
			if (((d == 1) || (d == 4) || (d == 7))&&(n>0))
				n--;
			else f = !f;
			if (((d == 2) || (d == 6)) && (b > 0))
				b--;
			else f = !f;
			if (((d == 3) || (d == 5)) && (c > 0))
				c--;
			else f = !f;
			if (f)
			{
				d++;
				days++;
				if (d > 7)
					d = 1;
			}
		}
		f = true;
		if (max < days)
			max = days;
		days = 0;
	}
	cout << max;

	return 0;
}


#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <cmath>
#include <complex>
#include <stack>
#include <string>
#include <cstring>
#include "time.h"
using namespace std;
#define SZ(x) (int)x.size()
#define FOR(i,x,y) for(int i=(int)(x);i<=(int)(y);++i)
#define REP(i,n) FOR(i,0,n-1)
#define FORD(i,x,y) for(int i=(int)(x);i>=(int)(y);--i)
#define ALL(a) (a).begin(),(a).end()
#define FORE(i,t) for(i=t.begin();i!=t.end();++i)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef deque<int> DI;
typedef deque<string> DS;
#define PI 3.14159265358979323
#define DEGTORAD(x) (double)x * 3.14159265358979323846264338327950288 / 180.0
#define RADTODEG(x) (double)x * 180 / 3.14159265358979323846264338327950288
#define prt if(1)printf
typedef pair< int, int > PII;
template <typename T> string tostr(const T& t) { ostringstream os; os<<t; return os.str(); }
#define ARRTOV(a,v) (v.assign(a,a+sizeof(a)/sizeof(a[0])))
#define SAFEMOD(a,b) (((a%b)+b)%b)
#define SQR(x) ((x)*(x))
#define EPS 1e-9
#define ABS(a)  (a<0?a*-1ll:a)
#define POW(x,y) pow((double)(x),(double)(y))
typedef long long i64;

struct typestate {
  vector< VI > segs;
  int cnt;

  bool operator<(const typestate& x2) const {
    const typestate& x1=(*this);
    if (x1.segs != x2.segs)
      return x1.segs < x2.segs;
    return x1.cnt < x2.cnt;
  }
};

set< typestate > vis;
queue< typestate > Q;

void move(typestate& s, int segFrom, int segTo) {
  // move alien from segFrom to segTo
  int ndx = SZ(s.segs[segFrom])-1;
  if (ndx < 0)
    return;
  int who = s.segs[segFrom][ndx];
  bool ok = true;
  REP(j,SZ(s.segs[segTo])) {
    if (s.segs[segTo][j] > who) {
      ok = false;
      break;
    }
  }
  if (ok) {
    typestate s2 = s;
    s2.segs[segFrom].erase(s2.segs[segFrom].begin()+ndx);
    s2.segs[segTo].push_back(who);
    s2.cnt++;
    sort(ALL(s2.segs[0]));
    sort(ALL(s2.segs[1]));
    sort(ALL(s2.segs[2]));
    Q.push(s2);
  }
}

long long bigmod(long long b,long long p,long long m) {
  if (m == 1LL) return 0LL;
  if (p == 0LL) return 1LL;
  if (p%2LL == 0) {
    long long n = bigmod(b,p/2LL,m);
    return (n*n) % m;
  }
  return ((b % m) * bigmod(b,p-1LL,m)) % m;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("140_C.in","r", stdin);
#endif

  int n,m;
  cin>>n>>m;
/*
  FOR(n,1,15) {

    Q.empty();
    vis.clear();

    typestate s;
    s.cnt = 0;
    s.segs.push_back(VI());
    s.segs.push_back(VI());
    VI who;
    FOR(i,1,n)
      who.push_back(i);
    s.segs.push_back(who);

    Q.push(s);
    int best = INT_MAX;
    while(!Q.empty()) {
      s = Q.front();
      Q.pop();

      if (vis.count(s) || s.cnt > 100)
        continue;
      vis.insert(s);
      if (SZ(s.segs[0]) == n) {
        best = min(best,s.cnt);
        continue;
      }

      REP(i,3) {
        REP(j,3) {
          if (i==j || abs(i-j) > 1)
            continue;
          move(s,i,j);
        }
      }
    }

    cout<<best<<endl;
  }
*/

  i64 ans = bigmod(3,n,m);
  --ans;
  if (ans < 0)
    ans = m;
  else
    ans %= m;
  cout<<ans<<endl;
  return 0;
}


 // CODED BY : Super_KAZAKH(-:  >---->  Yedige Ashirbek
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <fstream>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <queue>
#include <climits>
#include <string>

#define pb push_back
#define F first
#define sz(x) x.size()
#define S second
#define all(x) x.begin(),x.end()
#define ub upper_bound
#define lb lower_bound

using namespace std;

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

inline void boost(){
    ios_base::sync_with_stdio();
    cin.tie(0);
    cout.tie(0);
}

const ll MAXN=1e5+123;
const ll inf=1e9+123;
const double pi=acos(-1);
char a[9][9];
int main(){
    boost();
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    for(int i=1;i<=9;i++){
        for(int j=1;j<=9;j++){
            cin>>a[i][j];
        }
    }
    int x,y;
    cin>>x>>y;
    x%=3;
    y%=3;
    if(x==0){
        x=3;
    }
    if(y==0){
        y=3;
    }
    x*=3;
    y*=3;
    bool b=0;
    for(int i=x-2;i<=x;i++){
        for(int j=y-2;j<=y;j++){
            if(a[i][j]=='.'){
                b=1;
                break;
            }
        }
    }
    if(b==0){
        for(int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                if(a[i][j]=='.'){
                    a[i][j]='!';
                }
            }
        }
    }
    else{
        for(int i=x-2;i<=x;i++){
            for(int j=y-2;j<=y;j++){
                if(a[i][j]=='.'){
                    a[i][j]='!';
                }
            }
        }
    }
    for(int i=1;i<=9;i++){
        for(int j=1;j<=9;j++){
            cout<<a[i][j];
            if(j%3==0){
                cout<<" ";
            }
        }
        cout<<endl;
        if(i%3==0){
            cout<<endl;
        }
    }
    exit(0);
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <map>
#include <bitset>
#include <random>
#include <queue>

using namespace std;

const int INF=(int)1e9+7;

typedef long long  ll;

int main(){
    int mas[9][9];
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            cin >> mas[i][j];
        }
    }
    int iz,jz;
    cin >> iz >> jz;
    iz--;
    jz--;
    int i=iz;
    int j=jz;
    bool flag=true;
    if (mas[i][j]==33 || mas[i][j]==46) {
        flag=false;
    }
    int in=i%3+1;
    int j2=j%3+1;
    if(flag==true){
    for(int k=in;k<in+3;k++){
        for (int r=j2;r<j2+3;r++){
            if(mas[k][r]== 120 || mas[k][r]==111){
                continue;
            }
            mas[k][r]=33;
        }
    }
    }
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            cout <<(char)mas[i][j]<< " ";
        }
        cout << endl;
    }
    //cout << (char)120 << " " << (char)111 << " " << (char)46 << " " << (char)33 << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
typedef vector<int> vi;
#define all(x) x.begin(), x.end()
#define FIO     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define endl "\n"
#define pb push_back
#define mp make_pair
#define re(i,n)        for(int i=0;i<(n);i++)
#define re1(i, k, n) for (int i = k; k < n ? i <= n : i >= n; k < n ? i += 1 : i -= 1)
#define FORD(i,a,b)     for(int i=(a);i>=(b);i--)
typedef pair<int, int> pii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
const int mod = 1000000007;
//===================================DEBUG TEMPLATE=================================================
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T &x)
{
    int f = 0;
    cerr << '{';
    for (auto &i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif
//====================================DEBUG TEMPLATE==============================================
int solve(int a,int b)
{
    if(a==0)
    {
        return b*b;
    }
    else if(b==0)
    {
        return a*a;
    }
    else
        return 3*(2*a+2*b-2)+solve(a-1,b-1);
}

int32_t main()
{
    FIO;
    int n;
    int a,b;
    re(i,3)
    {
        cin>>a>>b;
    }
    cout<<solve(a,b);

}


#include<iostream>
using namespace std;
int main()
{
	int x, arr[60], t=0;
	cin >> x;
	for (int i = 0; i < 2 * x; i++)
		cin >> arr[i];

	for (int i = 0; i < 2 * x - 3; i = i + 2)
		for (int j = i + 3; j < 2 * x; j + 2)
			if (arr[i] == arr[j])
				t ++;
	for (int i = 1; i < 2 * x - 2; i = i + 2)
		for (int j = i + 1; j < 2 * x - 1; j + 2)
			if (arr[i] == arr[j])
				t++;

	cout << t;
}


#include<bits/stdc++.h>

using namespace std;

int main ()
{
    int m,n,k;
    cin>>n>>m>>k;
    int a[n];
    int p=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n,greater<int>());
    int i=0;
    int total=k;
    for(int i=0;i<n;i++)
    {
        total+=a[i]-1;
    }
    if(n>total)
    {
        cout<<"-1";
        return 0;
    }
    while (true)
    {
        if(m<=k)
        {
            break;
        }
        else if (m>k&&n!=0)
        {
            n--;
            m-=a[i]-1;
            i++;
            p++;
        }
    }
    cout<<p;
    return 0;
}


#include<bits/stdc++.h>
#ifdef NJUDGE
#include "igba.h"
#endif

using namespace std;
int v[110];

bool check(int index,int t){
//	dbg(vetor(v,index,index+t-1));
	for(int i = index;i < index+t-1;i++)
		if(v[i] != v[i+1] - 1)
			return false;
	return true;
}

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

	for(int i = 1;i <= n;i++)
		cin >> v[i];
	if(n == 1)
		cout << 0,exit(0);

	int mc = 0,b = 1;
	for(int i = 1;i <= n;i++)
		if(v[i+1] == v[i] + 1)
			b++,mc = max(mc,b);
		else b = 1;

	vector<int> r,aux;
	for(int i = 1;i <= n-mc+1;i++)
		if(check(i,mc)){
			aux.clear();
			for(int j = i;j < i+mc;j++)
				aux.push_back(v[j]);
			r = aux;
			if(r[0] == 1)
				break;
		}
//	dbg(r);

	if(r.size() > 0)
		if(r[0] == 1 || r[r.size()-1] == 1000)
			cout << r.size()-1,exit(0);
//	cerr << "bug" << endl;
	int x = (r.size()-2);
	cout << max(x,0) << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n ;
int bfs(int a , int b){
    queue<pair<pair<int , int > , int > >  q ;
    q.push(make_pair(make_pair(a , b) , 0 ));
    while(!q.empty()){
        int x = q.front().first.first;
        int y = q.front().first.second;
        int c = q.front().second;
        if(x == n || y == n)
            return c;
    q.pop();
    q.push(make_pair(make_pair(x + y , x) , c + 1));
    q.push(make_pair(make_pair(x , x + y) , c + 1));
    }
}
int main(){
    cin >> n;
    cout<<bfs(1 , 1);
    return 0;
}


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

int main(){
    string s;
    cin>>s;
    if(s.length()>21)
        cout<<-1<<endl;
    else{
    int ans = 0;
    bool check = 0;
    for(int i = 0; i<s.length()-2; i++){
        for(int j = i+1; j<s.length()-1; j++){
            string first;
            string second;
            string third;
            first = s.substr(0,i+1);
            second = s.substr(i+1,j-i);
            third = s.substr(j+1,s.length()-j-1);
            int x = 0;
            int y = 0;
            int z = 0;

            if(first.length()>1&&first[0]=='0')
                continue;
            if(second.length()>1&&second[0]=='0')
                continue;
            if(third.length()>1&&third[0]=='0')
                continue;
            if(first.length()>7||second.length()>7||third.length()>7)
                continue;
            for(int i = 0; i<first.length(); i++){
                x*=10;
                x+=first[i]-'0';
            }
            for(int i = 0; i<second.length(); i++){
                y*=10;
                y+=second[i]-'0';
            }
            for(int i = 0; i<third.length(); i++){
                z*=10;
                z+=third[i]-'0';
            }
            if(x>1000000||y>1000000||z>1000000)
                continue;
            check = 1;
            if(x+y+z>ans)
                ans = x+y+z;
        }
    }
    if(check)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
    }

}


// (.Y.)™

#define _USE_MATH_DEFINES

#include <cmath>
#include <cstdio>

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define  ll          long long
#define ull unsigned long long

#define mp make_pair
#define pb push_back

#define DEBUG(x) cout << #x << ": " << (x) << endl;

string s;

int tab[101][2][220][60];
int was[101][2][220][60];

int f(int p, bool zn, int R, int n) {
    if ( was[p][zn][R+110][n] )
        return ( tab[p][zn][R+110][n] );

    if ( p >= s.length() ) {
        if ( n == 0 )
            return ( abs(R) );
        else
            return ( 0 );
    }

    int res;
    int ch = s[p];

    if ( ch == 'F' )
        res = f(p+1, zn, R+(zn?1:-1), n);
    else
        res = f(p+1, !zn, R, n);

    for (int i = 1; i <= n; i++) {
        if ( ch == 'F' ) {
            res = max(res, f(p+1, zn = !zn, R, n-i));
            ch = 'T';
        }
        else {
            res = max(res, f(p+1, zn, R+(zn?1:-1), n-i));
            ch = 'F';
        }
    }

    tab[p][zn][R+110][n] = res;
    was[p][zn][R+110][n] = true;

    return ( res );
}

int main() {
#ifndef ONLINE_JUDGE
    freopen( "input.txt", "rt", stdin );
    //freopen("output.txt", "wt", stdout);
#endif

    cin >> s;

    int n;
    cin >> n;

    for (int i = s.length()-1; i >= 0; i--)
        for (int j = -100; j <= 100; j++)
            f(i, true, j, 0);

    cout << f(0, true, 0, n);

exit:
    return ( 0 );
}


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

int main()
{

    int ans=0,row,col,i,n;
    double t;
    cin>>n>>t;
    int x;
    x=n*(n+1)/2;

    double a[2*x];
i=0;

memset(a,0,sizeof(a));
a[0]=t;
   // a[0]=1;
   // t-=1;

    for(row=1;row<=n+1 && t>0;row++){
            for(col=1;col<=row && t>0;col++,i++){
                    t=a[i];
        a[i]=(t>=1?1:t);
      /*  if(a[i]==1 && row<=n){
            ans++;
            cout<<i;
            }
*/
        t=((t>1)?(t-1):0);
        a[i+row]+=t/2;
        a[i+row+1]+=t/2;
    }
    //cout<<row<<" full"<<endl;}
}
    for(i=0;i<x;i++){
        if(a[i]>=1)
           {

            //cout<<i;
            ans++;}
    }

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


// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma")
// #pragma GCC optimize("fast-math", "omit-frame-pointer", "inline")
// #pragma warning (disable: 4996)

#include "bits/stdc++.h"
typedef long long int ll;
using namespace std;
// #define int long long int
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
const ll INF = 1e18; // 1061109567
const ll MOD = 998244353;

int main() {

	fastio;
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	// ll tt; cin>>tt;
	// while(tt--) {
		ll n, m; cin>>n>>m;
		vector<ll> v(m);
		for(ll i=0;i<m;i++) cin>>v[i];
		vector<ll> ans(n+1, 0);
		unordered_set<ll> st;
		for(ll i=0;i<v.size()-1;i++) {
			if((v[i+1] - v[i]) <= 0 || v[i+1] - v[i] > n) {
				ans[v[i]] = n + v[i+1] - v[i];
				st.insert(n + v[i+1] - v[i]);
			}
			else {
				ans[v[i]] = v[i+1] - v[i];
				st.insert(v[i+1] - v[i]);
			}
		}
		unordered_set<ll> present;
		for(ll i=1;i<=n;i++) {
			if(st.find(i) == st.end()) present.insert(i);
		}
		// for(auto it:present) cout<<it<<" "; cout<<"\n";
		for(ll i=1;i<=n;i++) {
			if(!ans[i]) {
				if(present.size()) {
					ans[i] = *present.begin();
					present.erase(present.begin());
					st.insert(*present.begin());
				}
			}
		}
		if(st.size() != n) cout<<"-1\n";
		else {
			for(ll i=1;i<=n;i++) cout<<ans[i]<<" "; cout<<"\n";
		}
	// }
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n;
	int count(0);
	int a(0);
	int x[100];
	int tmp[101];
	vector<int>s[101];
	for (int i(0); i < 101; i++){ tmp[i] = 0; }
	cin >> n;
	for (int i(0); i < n; i++)
	{
		cin >> x[i];
		tmp[x[i]]++;
	}

	for (int i(0); i < n; i++)
	{
		if (tmp[x[i]] <= count)
		{
			for (int j(0); j < tmp[x[i]]; j++)
			{
				s[j].push_back(x[i]);
			}
			a += tmp[x[i]];
		}

		if (tmp[x[i]] > count)
		{
			if (count * x[i] - a >= tmp[x[i]]) //count * x[i] - a ��������� �����
			{
				int q(0);
				for (int j(0); j < tmp[x[i]]; j++)
				{
					if (s[q].size() < x[i])
					{
						s[q].push_back(x[i]);
					}
					else
					{
						q++;
						j--;
					}
				}
			}
			else
			{
				int w = tmp[x[i]] - (count * x[i] - a); // ������� ������� �� ��������
				for (int j(0); j < count; j++)
					while (s[j].size() != x[i]){ s[j].push_back(x[i]); }
				while (true)
				{
					count++;
					for (int j(0); j <= x[i]; j++)
					{
						s[count].push_back(x[i]);
						w--;
						if (w == 0){ break; }
					}
					if (w == 0){ break; }
				}
			}
			a += tmp[x[i]];
		}
	}

	cout << count;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mx 10485760
string str;
string tree[mx * 3];
void built(int node, int b, int e)
{
    if (b == e) {
       tree[node] = str[b-1];
        return;
    }
    int Left = node * 2;
    int Right = node * 2 + 1;
    int mid = (b + e) / 2;
    built(Left, b, mid);
    built(Right, mid + 1, e);
    tree[node] = tree[Left] + tree[Right];
}
string substr(int node, int b, int e, int i, int j)
{
    if (i > e || j < b)
        return "";
    if (b >= i && e <= j)
        return tree[node];

    int Left = node * 2;
    int Right = node * 2 + 1;
    int mid = (b + e) / 2;
    string p1 = substr(Left, b, mid, i, j);
    string p2 = substr(Right, mid + 1, e, i, j);
    return p1 + p2;
}
 int frequency=0,loc=-1;
void KMP(string T, string P)
{
        int n,m,k,i,j;
   // n=T.size();
    n=T.size();
    //m=P.size();
    m=P.size();
    long long int  LPS[m];
    i=1;
    j=0;
    LPS[0]=0;
    while(i<m)
    {

            if(P[i]==P[j]){
                    j=j+1;
              LPS[i]=j;
              i=i+1;

            }
            else
            {
                if(j)
                   j=LPS[j-1];
            else {
                    LPS[i]=0;
                    i=i+1;
            }
            }
    }
    i=0;
    j=0;

    while(i<n)
    {
           if(T[i]==P[j]){

              j=j+1;

              i=i+1;
            }
            if(j==m)
            {
                frequency=frequency+1;
                loc=i-j;
                j=LPS[j-1];
            }
            else if(i<n && T[i]!=P[j])
            {
                if(j)
                    j=LPS[j-1];
                else
                   i=i+1;
            }
      }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>str;
    int l=str.size();
    int i,j;
    string Substr;
    int Mxx=0;
    int f=0;
    int n= str.size();
    built(1,1,n);
    for(i=0; i<l; i++)
    {
        frequency=0;
        for(j=i+1; j<l; j++)
        {
           frequency=0;
           Substr =substr(1,1,n,i+1,j+1);
           KMP(str,Substr);
           if(frequency>=2){
                int x=Substr.size();
           Mxx=max(x,Mxx);
           }
        }
    }
    cout<<Mxx<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int N = 254;
const ll MOD = 1000000007LL;

ll dp[N][N];
ll C[N][N];
ll pow[N][N];

ll bigpow(ll a, ll b) {
  if (pow[a][b] != -1) return pow[a][b];
  if (b == 0) return 1;
  ll half = bigpow(a, b >> 1);
  ll res = half * half % MOD;
  if (b % 2 == 1) res = res * a % MOD;
  return pow[a][b] = res;
}

ll calc(ll r, ll c, ll k, ll n) {
  if (dp[r][c] != -1) return dp[r][c];
  if (r == 0) return dp[r][c] = 1;
  if (c == 0) {
    ll tmp = (bigpow(k, n) - bigpow(k-1, n) + MOD) % MOD;
    return dp[r][c] = bigpow(tmp, r);
  }
  if (r == 1) {
    return dp[r][c] = bigpow(k, n - c);
  }
  ll res = (bigpow(k, n - c) - bigpow(k - 1, n - c) + MOD) % MOD;
  res = res * bigpow(k - 1, c) % MOD;
  res = res * calc(r - 1, c, k, n) % MOD;
  for (ll c0 = 1; c0 <= c; c0++) {
    ll tmp = bigpow(k, n - c) * C[c][c0] % MOD * bigpow(k - 1, c - c0) % MOD * calc(r - 1, c - c0, k, n) % MOD;
    res = (res + tmp) % MOD;
  }
  return dp[r][c] = res;
}

int main(){
  ios_base::sync_with_stdio(0);  cin.tie(NULL);  cout.tie(NULL);
  for (int i = 0; i < N; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MOD;
    }
  }
  ll n, k;
  while (cin >> n >> k) {
    memset(dp, -1, sizeof(dp));
    memset(pow, -1, sizeof(pow));
    cout << calc(n, n, k, n) << endl;
  }
  return 0;
}


#include <cstdlib>
#include<stdio.h>
#include <iostream>
#include<algorithm>
#define forn(i,n)  for(i=1;i<=n;i++)
using namespace std;
int a[100000000],i,j,s=0,cur,n,z[1000000];
int main(int argc, char *argv[])
{n=1;
cur=0;
forn(i,n){
n++;
if(i==1333335002)break;
printf("%d\n",i);}
//system("color 03");

    return EXIT_SUCCESS;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int factorial(int n){
    if (n==1){
        return n;
    }
    return n*factorial(n-1);
}

int main() {
	// your code goes here
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int number;
	cin>>number;
	int r=2*(number-1);
	int c=number-1;
	cout<<factorial(r)/(factorial(c)*factorial(c));

}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define speed_up ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define debug cerr << "OK\n";
#define ub upper_bound
#define lb lower_bound

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef set <int> si;
typedef set <ll> sll;
typedef multiset <int> msi;
typedef multiset <ll> msll;
typedef map <int, int> mi;
typedef map <ll, int> mll;

const int N = 2e5 + 2;
const int M = 1e5 + 2;
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e16;

void data() {
    #ifdef PC
        freopen("main.in", "r", stdin);
        freopen("main.out", "w", stdout);
    #endif
}

ll n, k;

ll rec (ll v, ll n) {
	ll m1 = v;
	ll cnt = 0, p;
	if (m1 % 2 == 1) {
		p = 1;
	}
	else {
		p = 2;
	}
	cnt = 0;
	while (m1 <= n) {
		if (m1 + p - 1 <= n) {
			cnt += p;
			p *= 2;
			m1 *= 2;
		}
		else {
			cnt += (n - m1 + 1);
			break;
		}
	}
	return cnt;
}

int main() {
	data();
	scanf("%lld %lld", &n, &k);
	ll l = 1, r = n;
	while (l < r) {
		ll m = (l + r) / 2;
        ll cnt = rec(m + m % 2, n);
		if (cnt > k) {
			l = m + 1;
		}
		else {
			r = m;
		}
	}
	ll ans = -1;
	for (ll i = max(m - 1000LL, 1LL); i <= min(n, m + 1000LL); ++i) {
		ll cnt = rec(i, n);
		if (cnt >= k) {
			ans = max(i, ans);
		}
	}
	printf("%lld", ans);
	return 0;
}


#include<iostream>

using namespace std;

int abs(int x)  {
    if(x<0)
        return -x;
    return x;
}

int igorWalks(int x1, int x2, int t2)   {
    return t2*(abs(x1-x2));
}
int tram(int p, int x2, int x1, int t1, int d, int s) {
    if(d == 1)  {
        if(p<=x1 && x1<x2)
            return t1*(x2-p);
        else if(p>=x1 && x1<x2)
            return t1*((s-p)+s+x2);
        else if(p>=x1 && x1>x2 || p<=x1 && x2<x1)
            return t1*((s-p)+(s-x2));
    }
    if(d == -1)  {
        if(p>=x1 && x1>x2)
            return t1*(p-x2);
        else if(p<=x1 && x1>x2)
            return t1*((p)+s+(s-x2));
        else if(p<=x1 && x1>x2 || p>x1 && x2>x1)
            return t1*((p)+(x2));
    }

}

int main()  {
    int s, x1, x2;
    cin >> s >> x1 >> x2;
    int t1, t2;
    cin >> t1 >> t2;
    int p, d;
    cin >> p >> d;
    if(t1>t2)
        return igorWalks(x1, x2, t2);
    int min = igorWalks(x1, x2, t2);
    if(min > tram(p,x2, x1,t1,d,s))
        min = tram(p,x2, x1, t1,d,s);
    cout << min;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< int,int > PII;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)

const int inf = 1000000000;
const int mod = 1000000007;
const int KOK = 100000;
const int li = 100005;
const int LOG = 20;

lo n,m,cev,x,mn=inf;
bool prime[1000000005];
vector< PII > v;
char c;
string s;

void sieve(lo sayi){
	for(lo p=3;p<=sayi;p+=2){
		if(prime[p]==1) continue;
		//~ cevap++;
		for(int i=p*p;i<=sayi;i+=p){
			prime[i]=1;
		}
	}
	if(sayi%2==0)
		v.pb(mp(2,0));
	for(lo i=3;i<=sayi;i+=2){
		if(prime[i]==0){
			if(sayi%i==0)
				v.pb(mp(i,0));
		}
	}
}

int main(){
	scanf("%lld %lld",&n,&m);
	sieve(m);
	for(lo i=0;i<(lo)v.size();i++){

		lo xx=0;
		while(m%v[i].fi==0){

			m/=v[i].fi;
			xx++;
		}
		v[i].se=xx;
		cev=0;
		lo yes=n;
		while(yes){

			yes/=v[i].fi;
			cev+=yes;
		}
		//~ cout<<"**\n"<<v[i].fi<<" "<<m<<endl;
		//~ cout<<"**\n"<<xx<<endl;
		//~ cout<<"**\n";
		mn=min(mn,cev/xx);
	}

	printf("%lld\n",mn);
	return 0;
}


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

int main()
{
	vector <int> lav;
	int n,m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		lav.push_back(x);
	}
	sort(lav.begin(), lav.end());
	int maximum = lav[n - 1] + m;
	int i = 0;
	for (int i = 0; i < n - 1; i++) {
		while (lav[i] < lav[n - 1]) {
			if (m > 0) {
				lav[i]++;
				m--;
			}
		}
	}
	while (m != 0) {
		for (int i = 0; i < n; i++) {
			if (m > 0) {
				lav[i]++;
				m--;
			}
		}
	}
	sort(lav.begin(), lav.end());
	cout << lav[n - 1] << " " << maximum;
	return 0;
}


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

long x[42600];

int BinSearch(int n,long p){
    int low,high,mid;

    low=0;high=n+1;
    while(low<(high-1)){
        mid=(low+high)/2;
        if(p<x[mid])
            high=mid;
        else
            low=mid;
    }

    if(p==x[low])
        return x[low];
    else
        return 0;
}

int main()
{
//  freopen("KKR loose.txt","w",stdout);

    long i,j,b;
    long n;

    scanf("%ld",&n);

    j=0;

    for(i=1;i*(i+1)/2<=n;i++){
        x[j++]=i*(i+1)/2;
    }

    j--;

    for(i=0;x[i]<=n/2;i++){
        b=n-x[i];

        if(BinSearch(j,b)){
            printf("YES");
            return 0;
        }
    }

            printf("NO");

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <time.h>
#include <string.h>
#include <queue>
#include <stack>
#include <iomanip>
#define pb push_back
using namespace std;
typedef long long int ll;
ll mod=1e9+7;
ll exp(ll x,ll y){ll ret=1;for(;y;y>>=1,x=(x*x)%mod){if(y&1)ret=(ret*x)%mod;}return ret;}
ll gcd(ll x,ll y){return x%y==0?y:gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll bsum(ll u,ll b){return u<b?u:bsum(u/b,b)+u%b;}
ll prival(ll u,ll p){int cn=0;while(u%p==0){cn++;u=u/p;}return cn;}
bool isprime(ll x){for(ll i=2;i*i<=x;i++){if(x%i==0){return false;}}return true;}
void YAY(void){cout<<"YES"<<endl;}
void NAY(void){cout<<"NO"<<endl;}
ll dp[2100000], ex[2100000], ad[2100000], ans;
vector<int> divi[2100000];
int n, k;

void prepr(void)
{
	int i, j;
	for(i=1 ; i<=k ; i++)
	{
		ex[i]=exp(i,n);
		for(j=2 ; i*j<=k ; j++)
		{
			divi[i*j].push_back(i);
		}
	}
}

int main(void)
{
	cin>>n>>k;
	int i, j, cc; ll add;
	prepr();
	for(i=1 ; i<=k ; i++)
	{
		if(i==1)
		{
			ad[1]=1;
			dp[1]=1; continue;
		}
		dp[i]=dp[i-1];
		ad[i]=ex[i]-ex[i-1];
		for(j=0 ; j<divi[i].size() ; j++)
		{
			ad[i]-=ad[divi[i][j]];
			if(ad[i]<0) ad[i]+=mod;
		}
		dp[i]+=ad[i]; if(dp[i]>mod) dp[i]-=mod;
	}
	for(i=1 ; i<=k ; i++)
	{
		ans+=(dp[i]^i);
		if(ans>=mod) ans-=mod;
	}
	cout<<ans;
	return 0;
}


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

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

const long long maxn = 21;
const long long mod = 1e9 + 7;

long long ans;
long long n, s;
long long a[maxn];
long long sum[1 << maxn];

void in();
void solve();
void out();
long long pwr(long long, long long);
long long C(long long, long long);

int main(){
    in();
    solve();
    out();
}

void in(){
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    cin >> n >> s;
    for(long long i = 0; i < n; i++){
        cin >> a[i];
        a[i]++;
    }
}

void solve(){
    for(long long i = 1; i < (1 << n); i++){
        sum[i] = sum[i - (i & -i)];
        sum[i] += a[__builtin_ctz(i)];
    }
    for(long long i = 0; i < (1 << n); i++){
        ans += -1 * __builtin_popcount(i) % 2 * C(s + n - 1 - sum[i], n - 1);
        ans += mod;
        ans %= mod;
    }
}

void out(){
    cout << ans;
}

long long pwr(long long a, long long b){
    if(b == 0)
        return 1;
    if(b < 0)
        return pwr(a, b + mod - 1);
    long long c = pwr(a, b / 2);
    if(b % 2)
        return c * c % mod * a % mod;
    return c * c % mod;
}

long long C(long long n, long long k){
    if(n < k)
        return 0;
    long long ans = 1;
    for(long long i = 1; i <= k; i++){
        ans *= pwr(i, -1);
        ans %= mod;
        ans *= (n - i + 1) % mod;
        ans %= mod;
    }
    return ans;
}


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

int main()
{
    int n;
    cin>>n;
    int l=50;
    vector <int> data;
    for(int i=0;i<n;i++)
    {
        int p;
        cin>>p;
        data.push_back(p);
        if(p<l)
        {
            l=p;
        }
    }
    float j=2.00+((l+data[2])/2);
    cout<<j<<endl;

}


#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#define FOR(A, B) for(A=0;A<B;A++)

using namespace std;

vector<long long> luckies;
int l, lim1, lim2;
void gen(long long c)
{
	if(c > lim2) return;
	if(l > 0 && c >= lim1) luckies.push_back(c);
	l++;
	gen(c*10+4);
	gen(c*10+7);
	l--;
}

int main()
{
	int i, j, k, n, m, pl, pr, vl, vr;
	scanf("%d %d %d %d %d", &vl, &vr, &pl, &pr, &k);
	l = 0; lim1 = min(vl, pl);
	lim2 = max(vr, pr);
	gen(0);
	int q = 0;
	for(i = vl; i <= vr; i++) {
		for(j = pl; j <= pr; j++) {
			int p = 0;
			for(m = 0; m < luckies.size(); m++) {
				if(luckies[m] >= min(i, j) && luckies[m] <= max(i, j)) p++;
			}
			if(p == k) {
				q++;
			}
		}
	}
	double total = (double)(vr-vl+1)*(pr-pl+1);
	printf("%lf", (double)q/total);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e3 + 10;
const long double EPS = 1e-9;
const long double PI = acos(-1);
const long double a90 = PI / 2;
const long double a180 = PI;

struct pt{
    long double cor[5];
    pt(){}
    pt(long double c1, long double c2, long double c3, long double c4, long double c5){
        cor[0] = c1;
        cor[1] = c2;
        cor[2] = c3;
        cor[3] = c4;
        cor[4] = c5;
    }
    pt(pt a, pt b){
        cor[0] = b.cor[0] - a.cor[0];
        cor[1] = b.cor[1] - a.cor[1];
        cor[2] = b.cor[2] - a.cor[2];
        cor[3] = b.cor[3] - a.cor[3];
        cor[4] = b.cor[4] - a.cor[4];
    }
};

long double dot_prod(pt a, pt b){
    long double res = 0;
    for (int i = 0; i < 5; i++)
        res += a.cor[i] * b.cor[i];
    return res;
}

long double len(pt a){
    return sqrt(dot_prod(a, a));
}

long double angle(pt a, pt o, pt b){
    pt x = pt(o, a), y = pt(o, b);
    long double res = (acos(dot_prod(x, y) / (len(x) * len(y))));
    return res;
}

pt p[MAXN];
bool ans[MAXN];

int main()
{
    #ifdef HOME
    freopen("input.txt", "rt", stdin);
    #endif // HOME
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    if (n < 3){
        cout << n << "\n";
        for (int i = 0; i < n; i++)
            cout << i + 1 << "\n";
        return 0;
    }
    for (int i = 0; i < n; i++){
        long double a, b, c, d, e;
        cin >> a >> b >> c >> d >> e;
        p[i] = pt(a, b, c, d, e);
        ans[i] = false;
    }
    for (int v = 0; v < n; v++){
        for (int a = 0; a < n; a++){
            for (int b = 0; b < n; b++){
                if (v == a || v == b || a == b)
                    continue;
                long double a1 = angle(p[a], p[v], p[b]);
                long double a2 = angle(p[v], p[a], p[b]);
                long double a3 = a180 - a1 - a2;
                if (a180 > a1 - EPS && a1 > a90 -EPS)
                    ans[v] = true;
                if (a180 > a2 - EPS && a2 > a90 -EPS)
                    ans[a] = true;
                if (a180 > a3 - EPS && a3 > a90 -EPS)
                    ans[b] = true;
                /*if (a1 > a90 - EPS)
                    ans[v] = true;
                if (a2 > a90 - EPS)
                    ans[a] = true;
                if (a3 > a90 - EPS)
                    ans[b] = true;*/
            }
        }
    }
    int k = 0;
    for (int i = 0; i < n; i++)
        if (ans[i])
            k++;
    cout << k << "\n";
    for (int i = 0; i < n; i++){
        if (ans[i])
            cout << i + 1 << "\n";
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
#include <cassert>
#include <list>
#include <bitset>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forit(v,it) for(typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

int n;
double R,r;
const long double PI = 3.1415926535897932384626433832795;

int main () {
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	scanf("%d%lf%lf",&n,&R,&r);
	int cnt = 0;
	if(R < r  ||  (n > 1  &&  R == r)) {
		while(true);
		puts("NO");
		return 0;
	}
	if(n == 1) {
		puts("YES");
		return 0;
	}

	while(R >= r) {
		if(R == r) {
			cnt++;
			break;
		}
		long double x = acos(1 - (long double)(2*r*r)/((R - r)*(R - r)));
		if(x + 1e-9 < 0) x += 2*PI;
		cnt += (int)((long double)(2*PI)/x + 1e-9);
		R -= 2*r;
	}
	if(cnt < n) puts("NO");
	else puts("YES");
	return 0;
}


#include<bits/stdc++.h>

//topcoder
#define sz(a) ((int)a.size())
#define repv(i,a) for(int i=0;i<sz(a);i++)
#define revv(i,a) for(int i=sz(a)-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define countbit(x) __builtin_popcount(x)
#define SET(a) memset(a,-1,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define all(a) a.begin(),a.end()
#define pb(a) push_back(a)

#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define REP(I,N) FOR(I,0,N)
#define rall(a) a.rbegin(),a.rend()
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define pi (2.0*acos(0.0))
#define PI (2.0*acos(0.0))

#define set0(ar) memset(ar,0,sizeof ar)
#define setinf(ar) memset(ar,126,sizeof ar)
#define in(a,x,y) (a>=x && a<=y)
#define out(a,x,y) (!in(a,x,y))
#define eq(a,b) (fabs(a-b)<eps)
#define less(a,b) (a+eps<b)
#define great(a,b) (a>b+eps)
#define xx first
#define yy second
//#define root 1

#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))

#define LB(a,x) (lower_bound(all(a),x)-a.begin())
#define UB(a,x) (upper_bound(all(a),x)-a.begin())

using namespace std;
int ts,kk=1;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vii;

template< class T > inline T _sq(T a) { return a * a; }
template< class T, class X > inline T _pow(T a,X y) {T z=1; rep(i,1,y){z*=a;} return z; }
template< class T > inline T _gcd(T a,T b) {a=_abs(a);b=_abs(b); if(!b) return a; return _gcd(b,a%b);}
template< class T > inline T _lcm(T a,T b) {a=_abs(a);b=_abs(b); return (a/_gcd(a,b))*b;}

template< class T > inline T _extended(T a,T b,T &x,T &y) {a=_abs(a);b=_abs(b); T g,x1,y1; if(!b) {x=1;y=0;g=a; return g;} g=_extended(b,a%b,x1,y1); x=y1; y=x1-(a/b)*y1; return g;}

template< class T, class X > inline bool getbit(T a, X i) { T t=1; return ((a&(t<<i))>0); }
template< class T, class X > inline T setbit(T a, X i) { T t=1;return (a|(t<<i)); }
template< class T, class X > inline T resetbit(T a, X i) { T t=1;return (a&(~(t<<i))); }
template< class T, class X > inline T togglebit(T a, X i) { T t=1;return (a^(t<<i)); }
//
template< class T,class X, class Y > inline T _bigmod(T n,X m,Y mod){ull ret=1, a = n%mod ; while(m){ if(m&1)ret=(ret*a)%mod; m>>=1; a=(a*a)%mod; }ret%=mod;return (T)ret;}
template< class T, class Y > inline T _modinv(T n,Y mod) {return _bigmod(n,mod-2,mod);}

/// //CONSTANT// ///
/// power   0123456789 ///
#define lmt 200
#define MX (lmt + 20 )
#define ten 10000000000LL
#define MOD 1000000007
#define inf 1000000000000000000LL

#define LOGMX 30
#define eps 1e-9
///===========================///

double p,go[2020][2020];
int t , n;

double dp(int pos,int tm){

    if(pos==n+1 || tm==0){
        return pos-1;
    }
    if(go[pos][tm]>-.5) return go[pos][tm];
    double ret = dp(pos+1,tm-1)*p + (1-p)*dp(pos,tm-1);

    return ret;
}

int main()
{
    cin>>n>>p>>t;
    memset(go , -1 , sizeof go);
    cout<<dp(1,t)<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <iostream>
int main(){
    long long int a,b,flag=0;cin>>a,b;
    for(int i=1;flag!=1;i++)
    {
        if(i%2==0)
        {
            if(b-i<0)
                {
                    cout<<"Valera";
                    break;
                }
            else b-=i;
        }

        if(i%2!=0)
        {
            if(a-i<0)
            {
                cout<<"Vladik";
                break;
            }
        else a-=i;
        }
     }
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define INF 10000000000000000LL
#define P system("pause")
#define print(v) for(typeof(v.begin()) itr = v.begin();itr!=v.end();itr++)cout<<*itr<<" "
#define IN freopen("input.txt","r",stdin)
#define OUT freopen("output.txt","w",stdout)
#define IO IN;OUT
#define SIZE 1005
#define C 10000
typedef long long ll;
using namespace std;
typedef pair<ll,ll> ii;
typedef pair<ll,ii> iii;
typedef vector<ll> vi;

ll p,x,y;map<ll,ll> mm;queue<ll> mq;

bool check(ll rating)
{

    if (rating < y)
        return false;
    ll i = (rating/50)%475;
    set<ll> ms;
    for(ll c=0;c<25;c++)
    {
        i = (i*96 + 42)%475;
        ms.insert(26+i);
    }
    if (rating >=y && ms.find(p)!= ms.end())
        return true;
    return false;
}
void dfs()
{
    ll rating = mq.front(); mq.pop();
    //cout<<rating<<endl;
    if (check(rating))
    {
        cout<<mm[rating]-1;
        exit(0);
    }
    for(ll r = rating;r>=y-200; r-=50)
    {
        if (!mm[r])
        {
            mm[r] = mm[rating];
            mq.push(r);
        }
    }
    if (rating < 2*y && !mm[rating+100])
    {
        mm[rating+100] = mm[rating]+1;
        mq.push(rating+100);
    }
    dfs();

}
int main()
{//IN;
    cin>>p>>x>>y;
    mm[x]=1;
    mq.push(x);
   // check(10400);
   dfs();
    return 0;
}


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

int main(void)
{

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

    int x;
    scanf("%d", &x);

    bool flag = true;
    int hold = 0;
    for (int i = 1; i <= n / 2; i++)
    {
        if (x % 10 != 4 && x % 10 != 7)
        {
            flag = false;
            break;
        }
        hold += x % 10;
        x = x / 10;
    }

    int hold2 = 0;
    if (flag)
    {
        for (int i = 1; i <= n / 2; i++)
        {
            hold2 += x % 10;
            x = x / 10;
        }
    }

    if (flag)
    {
        if (hold == hold2)
            printf("YES\n");
    }
    else
        printf("NO\n");
}
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,m,i{};
    cin>>n>>m;

    for(int a=1;a<1000;a++){
        for(int b=1;b<1000;b++)
        if(a*a+b==n && a+b*b==m){
            i++;
        }
    }
    cout<<i<<endl;
    return 0;
}


/*bismillahir rahmanir rahim*/
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string n;
    int k;
    cin>>n>>k;
    int len=n.size(),cnt=0,flg=0;
    if(len>k)
    {
     for(int i=len-1;flg!=k;i--)
     {
        if(n[i]!='0')
            ++cnt;
        else
        ++flg;
     }
    }
    else
    {
     cnt=len-1;
    }

    printf("%d",cnt);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i—-)
#define ri(x) scanf("%d", &x);
#define rl(x) scanf("%lld", &x)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const ld EPS = 1e-9;

int main(){
    ll n;
    rl(n);
    bool check;
    while(n>=1){
        check =true;
        for(int j = 2; j*j <= n && check;j++) {
            if (n % (j * j) == 0) {
                check = false;
                n /= j;
            }
        }
        if(check){
            printf("%lld", n);
            break;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#include <windows.h>

using namespace std;

int main()
{
    long long n,m;

    vector <long long> x;

    x.push_back(0);

    for (int i=1;;i++)
    {
        long long z=i+x[i-1];

        x.push_back(z);

        if (x[i]>=10e14)
        {
            break;
        }
    }

    cin>>n>>m;

    if (n<m)
    {
        cout<<n;

        return 0;
    }

    if (n==m)
    {
        cout<<n;

        return 0;
    }

    long long counter=m+1;

    for (int i=1; i<=x.size(); i++)
    {
        if (n-x[i-1]-counter<=0)
        {
            cout<<counter;

            return 0;
        }

        counter++;
    }

    return 0;
}


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

int n,i,j,ans=0,cnt[27];
string s;

int main(){
	scanf("%d",&n); cin >> s; i=0;
	do{
		if (s[i]>='A' && s[i]<='Z'){
			int tot=0;
			for (j=1;j<=26;j++)
				if (cnt[j]) tot++;
			ans=max(ans,tot);
			while (s[i]<='Z' && i<n-1) i++;
			memset(cnt,0,sizeof(cnt));
		}
		else {cnt[s[i]-'a'+1]=1; i++;}
	}while (i<=n-1);

	int tot=0;
	for (j=1;j<=26;j++)
		if (cnt[j]) tot++;
	ans=max(ans,tot);
	printf("%d\n",ans);
	return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vint vector<ll>
#define pb push_back
#define all(v) v.begin(),v.end()
#define ios cin.sync_with_stdio(false),cin.tie(NULL)
#define f_(i,m,n) for(ll i=m;i<n;i++)
#define ff first
#define ss second
const int INF=1e9 +7;
const int MOD=998244353;
const int maxn=5002;

ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;

    while (y > 0)
    {
        if (y & 1) {
            res = (res*x) % p;
        }

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

bool miillerTest(ll d, ll n)
{

    ll a = 2 + rand() % (n - 4);

    ll x = power(a, d, n);

    if (x == 1  || x == n-1) {
       return true;
    }

    while (d != n-1)
    {
        x = (x * x) % n;
        d *= 2;

        if (x == 1){
            return false;
        }

        if (x == n-1){
           return true;
        }
    }

    return false;
}

bool prime(ll n, ll k)
{

    if (n <= 1 || n == 4){
    	return false;
    }
    if (n <= 3){
    	return true;
    }

    ll d = n - 1;
    while (d % 2 == 0){
        d /= 2;
    }

    for (ll i = 0; i < k; i++){
        if (!miillerTest(d, n)){
            return false;
        }
    }

    return true;
}

ll pow(ll a, ll b){
	ll res=1;
	while(b--){
		res*=a;
	}
	return res;
}

void solve(){
	ll n;
	cin>>n;
	string s;
	cin>>s;

	vint ans;

	ll i=0;
	while(i<n){
		if(s[i]=='0'){
			ans.pb(0);
		}
		else{
			break;
		}
	}

	while(i<n){
		ll val=0;
		if(s[i]=='0'){
			ans.pb(0);
		}
		else{
		    ll j=i;
		    while(s[j]=='1'){
			   j++;
			   val++;
		    }

		    i=j+1;
		    ans.pb(val);
	    }

	}

	f_(i,0,ans.size()){
		cout<<ans[i];
	}

	return;
}

int main(){
	ll t;
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
int main(){
	std::ios::sync_with_stdio(false);
   string s;
   cin>>s;
   ull n = (s[0]-48)*10000+(s[2]-48)*1000+(s[4]-48)*100 + (s[3]-48)*10 + s[1]-48;
   ull ans = (n*n*n)%100000;
   ans = (ans*n*n)%100000;
   int cant = 5;
   n = ans;
   while(n>0)cant--,n/=10;
   for(int i=0;cant;++i)cout<<"0";
   cout<<ans<<"\n";
	return 0;
}


#pragma GCC target("popcnt")
#include<tuple>
#include<queue>
#include<cstdio>
#include<utility>
#include<iostream>
#define x first
#define y second
#define mpir make_pair
#define mtpl make_tuple
using namespace std;
typedef pair<int,int> pir;
typedef tuple<int,int,int> tpl;
constexpr int siz=1<<20|1,INF=0x7f7f7f7f;
constexpr int fx[4][2]={{1,0},{0,-1},{-1,0},{0,1}};

int n,m,sx,sy,cnt;
int val[9],sum[9];
int f[21][21][siz];
char c[21][21];
pir t[9];
queue<tpl> q;

inline
bool check(char c)
{
	return c=='.'||c=='S';
}

inline
bool check(int x,int y,int xi,int yi,int k)
{
	if(xi==t[k].x && yi<t[k].y)
		return x<xi;
	if(x==t[k].x && y<t[k].y)
		return x>xi;
	return false;
}

int bfs();

int main()
{
#ifdef HAZE
	freopen("CF375C.in","r",stdin);
#endif
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			cin>>c[i][j];
			if(c[i][j]=='S')
			{
				sx=i;
				sy=j;
			}
			if(isdigit(c[i][j]))
			{
				cnt++;
				t[c[i][j]-'0']=mpir(i,j);
			}
		}
	for(int i=1;i<=cnt;i++)
		scanf("%d",&val[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(c[i][j]=='B')
			{
				val[++cnt]=-INF;
				t[cnt]=mpir(i,j);
			}
	for(int i=1;i<(1<<cnt);i++)
		for(int j=0;j<cnt;j++)
			if((i>>j)&1)
				sum[i]+=val[j+1];
	printf("%d\n",bfs());
	return 0;
}

int bfs()
{
	int res(0);
	q.push(mtpl(sx,sy,0));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			for(int k=0;k<(1<<cnt);k++)
				f[i][j][k]=-1;
	f[sx][sy][0]=0;
	while(!q.empty())
	{
		int x,y,s;
		tie(x,y,s)=q.front();
		q.pop();
		if(x==sx && y==sy)
			res=max(res,sum[s]-f[x][y][s]);
		for(int i=0;i<4;i++)
		{
			const int xi=x+fx[i][0];
			const int yi=y+fx[i][1];
			if(xi>0&&xi<=n&&yi>0&&yi<=m&&check(c[xi][yi]))
			{
				int si=s;
				for(int j=0;j<cnt;j++)
					if(check(x,y,xi,yi,j+1))
						si^=(1<<j);
				if(f[xi][yi][si]==-1)
				{
					f[xi][yi][si]=f[x][y][s]+1;
					q.push(mtpl(xi,yi,si));
				}
			}
		}
	}
	return res;
}


#include<bits/stdc++.h>
#define inf 10000000
using namespace std;
int n,m,c,c_,refc[10];
char mp[25][25];
int p[25][25];
int val[10],px[10],py[10],sx,sy;
int dx[5]={0,1,0,-1,0},dy[5]={0,0,-1,0,1};
int dp[25][25][1<<8],sum[1<<8];
struct state{
	int x,y,st;
};
void bfs()
{
	queue<state>q;
	q.push((state){sx,sy,0});
	state now;
	int nx,ny,s,ax,ay,as;
	while(!q.empty())
	{
		now=q.front();q.pop();
		nx=now.x;ny=now.y;s=now.st;
		for(int i=1;i<=4;++i)
		{
			ax=nx+dx[i];ay=ny+dy[i];as=s;
			if(ax<=0||ax>n||ay<=0||ay>m)continue;
			for(int j=0;j<c;++j)
				if(cross(nx,ny,ax,ay,j))as^=(1<<j);
			if(dp[ax][ay][as]<-1)
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>(mp[i]+1);
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=n;++j)
		{
			if(mp[i][j]=='#')p[i][j]=1;
			else if(mp[i][j]=='S')sx=i,sy=j;
			else if(isdigit(mp[i][j]))refc[c_++]=c,px[c]=i,py[c++]=j;
			else if(mp[i][j]=='B')val[c]=-inf,px[c]=i,py[c++]=j;
		}
	}
	int a;
	for(int i=1;i<=c_;++i)
	{
		cin>>a;
		val[refc[i]]=a;
	}
	for(int i=1;i<(1<<c);++i)
	{
		for(int j=0;j<c;++j)
		{
			if(i&j)sum[i]+=val[j];
		}
	}
	bfs();
	int ans=0;
	for(int i=1;i<(1<<c);++i)ans=max(ans,dp[sx][sy][i]);
	cout<<ans;
	return 0;
}


#include <iostream>
using namespace std;
int count(string s,int start){
	int ans=0;
	for(int i=start;i<s.length()-1;i++){
		if(s[i]=='/')ans++;
		else break;
	}
	return ans;
}
int main(){
	string s,ans;
	cin>>s;
	for(int i=0;i<s.length();i++){
		int t=0;
		if(s[i]=='/'){
			t=count(s,i);
			ans+='/';
			i+=t-1;
		}
		else {
			ans+=s[i];
		}
	}
	cout<<ans;
	return 0;
}


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

bool cmp(int x, int y){
    if(x < y) return true;
    else return false;
}

int main()
{
    int n, x, suma = 0;
    vector<int> polje(n);
    cin >> n;
    for(int i = 0;i < n;i++){
        cin >> x;
        polje.push_back(x);
    }

    sort(polje.begin(),polje.end(),cmp);

    for(int i = 0;i < n;i++) suma += polje[i];

    int j = 0;
    while(((double) suma / (double) n) < 4.5){
        suma -= polje[j];
        suma += 5;
        polje[j] = 5;
        j++;
    }
    cout << j;
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    int x,y,z,i,j,k;
    scanf("%d%d%d%d%d%d",x,y,z,i,j,k);
    int cnt=0;
    if(x==i) cnt++;
    if(y==j) cnt++;
    if(z==k) cnt++;
    if(cnt>0) puts("YES");
    if(cnt==0) puts("NO");
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>

using namespace std;

using ll = long long;
using ld = long double;
constexpr ll MAX = 1e9;

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

    ll x;
    scanf("%lld", &x);
    queue<ll> q;
    q.push(0LL);

    int d = 1;
    ll lvl = 0LL;
    while (!q.empty()) {
        int sz = q.size();
        while (sz-- > 0) {
            ll v = q.front();
            q.pop();

            if (v == x) {
                printf("%lld\n", lvl);
                return 0;
            }

            for (ll s = -1; s <= 1; s += 2) {
                ll nv = v + s * d;
                q.push(nv);
            }
        }

        ++lvl;
        ++d;
    }

    return 0;
}


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

void swap(double &a,double &b)
{
	double c=a;
	a=b;
	b=c;
}

bool flag;

int gao(double x1,double y1,double r1,double x2,double y2,double r2)
{
	if (r1<r2)
	{
		swap(x1,x2);
		swap(y1,y2);
		swap(r1,r2);
	}
	double d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	flag=false;
	if (d>r1-r2&&d<r1+r2) return 0;
	flag=true;
	if (d>=r1+r2) return 1;
	return 2;
}

int main()
{
	int ans=0;
	double x1,y1,r1,r2,x2,y2,r3,r4;

	scanf("%lf %lf %lf %lf",&x1,&y1,&r1,&r2);
	scanf("%lf %lf %lf %lf",&x2,&y2,&r3,&r4);
	if (gao(x1,y1,r1,x2,y2,r3)==gao(x1,y1,r1,x2,y2,r4)&&flag) ans++;
	if (gao(x1,y1,r2,x2,y2,r3)==gao(x1,y1,r2,x2,y2,r4)&&flag) ans++;
	if (gao(x2,y2,r3,x1,y1,r1)==gao(x2,y2,r3,x1,y1,r2)&&flag) ans++;
	if (gao(x2,y2,r4,x1,y1,r1)==gao(x2,y2,r4,x1,y1,r2)&&falg) ans++;
	printf("%d\n",ans);
	return 0;
}


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

#define ll long long
#define ull unsigned long long

pair<int , int> pos[12];
vector<pair<int , int> > v;
int n ;
string s;

bool valid(int x ,int y)
{
    return (x >= 0 && y >= 0 && x < 3 && y < 3) || (x == 3 && y == 1);
}

bool test(int x , int y , int idx)
{
    if(idx == n) return 1;
    if(!valid(x , y)) return 0;

    return test(x + v[idx].first , y + v[idx].second , idx + 1);
}

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

    cin >> n >> s;

    for(int i = 0 ; i < 3 ; i++)
        for(int j = 0 ; j < 3 ; j++)
            pos[i * 3 + j + 1] = {i , j};
    pos[0] = {3 , 1};

    for(int i = 0 ; i < n - 1 ; i++)
    {
        int x = s[i] - '0';
        int y = s[i + 1] - '0';
        v.push_back({ pos[y].first - pos[x].first , pos[y].second - pos[x].second });
    }

    int counter = 0;
    for(int i = 0 ; i <= 9 ; i++)
        if(test(pos[i].first , pos[i].second , 0))
            counter++;

    if(counter == 1)
        cout << "YES";
    else cout << "NO";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long int mod=1e9+7;

long long int pot(long long int a, long long int b){
	if(b == 0) return 1;
	long long int c=pot(a, b/2);
	if(b%2 == 0) return (c*c) % mod;
	else return a*pot(a, b-1) %mod;
}

int main(){
	long long int n, m, k;
	cin >> n >> m >> k;
	if(k == -1 && (n%2)!=(m%2)) cout << 0;
	else cout << pot(pot(2, (n-1)),(m-1));
	return 0;
}


#pragma GCC optimize("trapv")
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target ("avx2")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define forceRTE cout<<1/0
#define forceTLE while(true){}
#define ll long long
#define ld long double
#define gtl getline
#define vec vector
#define ar array
#define umap unordered_map
#define pq priority_queue
#define pqintasc priority_queue<int,vector<int>,greater<int> >
#define pqpairasc priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >
#define map_set(ptr,sz) ptr.reserve(sz),ptr.max_load_factor(0.25);
#define FOR(a,b,c) for(int i=a;i<b;i+=c)
#define EACH(x,a) for(auto x:a)
#define debug(x) send("Debug: ",x," ",double(measurer*1.0/CLOCKS_PER_SEC),"\n");
#define len(ds) int(ds.size())
using namespace std;
using namespace __gnu_pbds;
time_t measurer=clock();
const int MAXN = 100000;
const int MOD = 1000000007;
const int MOD2 = 998244353;
const bool DEBUG = false;
double exe_time = 0.0;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> policytree;
class mnlib{
    public:
    class VectorOperations{
        public:
        int distinctelements(vector<ll> v){
            set<ll>s;
            EACH(x,v){
                s.insert(x);
            }
            return int(len(s));
        }
    } vcs;
    class StringOperations{
        public:
        int hamDist(string a,string b){
            int qq = 0;
            for(int i=0;i<len(a);i++)
                qq+=(a[i]!=b[i]);
            return qq;
        }
    } sop;
    class Geometry{
        public:
        long double tangent(double x){
            return tan(x*3.141592653589793/180);
        }
        long double sine(double x){
            return sin(x*3.141592653589793/180);
        }
        long double cosine(double x){
            return cos(x*3.141592653589793/180);
        }
        long double arcsin(double x){
            return asin(x)/3.141592653589793*180;
        }
        long double arccos(double x){
            return acos(x)/3.141592653589793*180;
        }
        long double arctan(double x){
            return atan(x)/3.141592653589793*180;
        }
        long double area(long long n,long double s){
            long double ans=s*s*n;
            ans=ans*1.0/(4*tangent(180.0/n));
        }
        long double areaTriangle(long double a,long double b,long double c){
            long double ans=0.5*a*b*sine(c);
        }
    } geo;
    class Primes{
        public:
        bool checkPrime[MAXN+1];
        void initprimes(){
            for(int i=1;i<=MAXN;i++)checkPrime[i]=true;
            checkPrime[1]=false;
            for(int i=2;i*i<=MAXN;i++){
                if(checkPrime[i]){
                    for(int j=i*i;j<=MAXN;j+=i) checkPrime[j]=false;
                }
            }
        }
        bool isPrime(int x){ return checkPrime[x]; }
    } prm;
    class segmentTree{
        public:
        ll rmq[4*MAXN];
        void initrmq(){ FOR(0,4*MAXN,1) rmq[i]=0;}
        void updrmq(int le, int ri, int indx, int ptr,ll ele){
            if(le==ri){rmq[ptr]=ele; return;}
            if(indx<=(le+ri)/2) updrmq(le,(le+ri)/2,indx,2*ptr+1,ele);
            else updrmq((le+ri)/2+1,ri,indx,2*ptr+2,ele);
            rmq[ptr]=min(rmq[2*ptr+1],rmq[2*ptr+2]);
        }
        ll queryrmq(int le, int ri, int ptr, int lw, int rw){
            if(lw>ri || rw<le) return LLONG_MAX;
            else if(lw<=le && ri<=rw) return rmq[ptr];
            return min(queryrmq(le,(le+ri)/2,ptr*2+1,lw,rw),queryrmq((le+ri)/2+1,ri,ptr*2+2,lw,rw));
        }
        void updallrmq(ll x[],int lens){
            FOR(0,lens,1) updrmq(0,MAXN-1,i,0,x[i]);
        }
    } segt;
} lib;
void read(){}
template<typename R, typename... P> void read(R& Rz, P&... Pz){
    cin >> Rz;
    read(Pz...);
}
template<typename QwQ> void read(vec<QwQ>&Rz){
    int l=len(Rz);
    for(int i=0;i<l;i++){
        QwQ R2;
        read(R2);
        Rz[i]=R2;
    }
}
void send(){}
template<typename G, typename... M> void send(G Gz, M... Mz){
    cout << Gz;
    fflush(stdout);
    send(Mz...);
}
template<typename UwU> void send(vec<UwU>Rz){
    for(int i=0;i<len(Rz);i++){
        send(Rz[i]);
        if(i+1<Rz.size())send(' ');
    }
    send("\n");
}
template<typename Z, typename Z2> void init(vec<Z>&Rz, Z2 OO){
    for(int i=0;i<len(Rz);i++){
        Rz[i]=OO;
    }
}
mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
void prep_room(){
    lib.prm.initprimes();
    lib.segt.initrmq();
}
void open_debug(){
    send(exe_time);
}
void solve(){
    int x,y,z,t1,t2,t3;
    read(x,y,z,t1,t2,t3);
    int op1=t1*abs(x-y);
    int op2=t3*3+t2*abs(x-y);
    if(op1<op2){
        cout<<"YES\n";
    }
    else cout<<"NO\n";
}
main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    prep_room();
    int T=1;// read(T);
    while(T--)solve();
    measurer=clock()-measurer;
    exe_time = double(measurer*1.0/CLOCKS_PER_SEC);
    if(DEBUG)open_debug();
}


#include <iostream>
using namespace std;
int n;
double a, b, maxx;
intmain ()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a;
		b += a;
		maxx = max (maxx, b/i);
	}
	cout << maxx << endl;

	return 0;
}


#include<iostream>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
//#define int long long
#define mp make_pair
//setprecision
int inf=1000*1000*1000+10;
int const MAX=1000*100+10;
int const MAX2=1000*1000+10;
int const MAX3=5000+10;
vector<int> temp;
int n,dis,tcar,tw,ti;
int dp[MAX];
void fidp()
{
    for(int i=temp.size()-2;i>=0;i--)
    {
        if(i!=0)
            dp[temp[i]]=min(dp[temp[i+1]]+ti+tcar*(dis),(n-temp[i])*tw);
        if(i==0)
            dp[temp[0]]=min(dp[temp[1]]+tcar*dis,n*tw);
    }
}
main(){
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	cin>>n>>dis>>tcar>>tw>>ti;
	for(int i=0;i<=n;i+=dis)
        temp.pb(i);
    if(n%dis!=0)
        temp.pb(n);
    fidp();
    cout<<dp[0];
}


#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    long long int a,b,m,c,d,i,j,k,l,f,g,rem;
    for(;;)
   {
       cin>>a>>b>>m;
       if(a==0&&b==0&&m==0)
       {
           break;
       }
       for(i=1;i<=b;i++)
       {
           rem=a%m;
          a=rem*rem;
       }
       cout<<rem<<endl;
   }
   return 0;

}


/******************** NILOY ****************/
#include<bits/stdc++.h>
#define lld long long int
#define Max 100010
#define pi acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define f_input freopen("input.txt","r",stdin)
using namespace std;
int main()
{
    int a;
    while(1)
    {
        cin>>a;
    if(a%2 == 0)
    {
        cout<<"0"<<endl;
    }
    else
    {
        cout<<"1"<<endl;
    }
    }
}


#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define out(t, a) copy(all(a), ostream_iterator<t>(cout, " ")); cout << "\n";
#define int long long
// #define DEBAG

using namespace std;

void set_program() {
	#ifdef DEBAG
		freopen ("test.in", "r", stdin);
		freopen ("test.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
}

const int MAXN = 7000;

bool a_in_b(int a, int b) {
	return ((a & b) == a);
}

int n;
int gr[MAXN][MAXN];
vector <int> deleted, used, order;

void top_sort(int u) {
	used[u] = 1;
	for (int v = 0; v < n; ++v) {
		if (gr[u][v] && !used[v] && !deleted[v]) {
			top_sort(v);
		}
	}
	order.push_back(u);
}

int get_sum(int u, vector <int>& b) {
	int sum = b[u];
	used[u] = 1;
	for (int v = 0; v < n; ++v) {
		if (gr[u][v] && !used[v] && !deleted[v]) {
			sum += get_sum(v, b);
		}
	}
	return sum;
}

main() {
	set_program();
	cin >> n;
	vector <int> a(n), b(n);
	for (int& i : a) {
		cin >> i;
	}
	for (int& i : b) {
		cin >> i;
	}
	vector <int> cnt_childs(n, 0);
	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			if (a_in_b(a[i], a[j])) {
				gr[i][j] = 1;
				++cnt_childs[i];
			}
			if (a_in_b(a[j], a[i])) {
				gr[j][i] = 1;
				++cnt_childs[j];
			}
		}
	}
	deque <int> q;
	for (int i = 0; i < n; ++i) {
		if (!cnt_childs[i]) {
			q.push_back(i);
		}
	}
	deleted.assign(n, 0);
	for (; q.size(); ) {
		int u = q.front();
		q.pop_front();
		deleted[u] = 1;
		for (int v = 0; v < n; ++v) {
			if (gr[v][u]) {
				if (!--cnt_childs[v]) {
					q.push_back(v);
				}
			}
		}
	}
	used.assign(n, 0);
	for (int u = 0; u < n; ++u) {
		if (!deleted[u] && !used[u]) {
			top_sort(u);
		}
	}
	reverse(all(order));
	used.assign(n, 0);
	int ans = 0;
	for (int u : order) {
		if (!used[u]) {
			ans = max(ans, get_sum(u, b));
		}
	}
	cout << ans << "\n";
	return 0;
}