#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define INF 0x3ffffffff
#define N 50000010
long long f[N];
long long c,hr,hb,wr,wb;
bool flag=true;
long long maxx,minn,res;
int main()
{
	cin>>c>>hr>>hb>>wr>>wb;
	if(hb*wr<hr*wb)
	{
		long long tmp=hr;
		hr=hb;
		hb=tmp;
		tmp=wr;wr=wb;wb=tmp;
	}
	minn=wr*(wb-1);
	maxx=c-minn;
	res+=(maxx/wb)*hb;
	maxx%=wb;
	minn+=maxx;
	if(minn%wb==0) res+=(minn/wb)*hb;
	else
	{
		for(int i=wr;i<=minn;i++)
			f[i]=max(f[i],f[i-wr]+hr);
		for(int i=wb;i<=minn;i++)
		{
			f[i]=max(f[i],f[i-wb]+hb);
		}
		long long re=0;
		for(int i=1;i<=minn;i++)re=max(re,f[i]);
		res+=re;
	}
	cout<<res;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char first[10],last[10],login[10];
int main()
{
    int i,l;
  scanf("%s%s",first,last);
  l=strlen(first);
  for(i=0;i<=l;i++)
  {       if(i==0)
          {
              login[i]=first[i];
          }
          else if(first[i]<last[0])
          {
              login[i]=first[i];
          }
          else if(first[i]>last[0])
          {
              login[i]=last[0];
              break;
          }
          else if(first[i]==last[0])
          {
              login[i]=first[i];
              break;
          }
          if(i==l)
          {
              login[i]=last[0];
          }
  }
printf("%s\n",login);
return 0;
}


////
////  main.cpp
////  codeforces
////
////  Created by Victor Young on 2/20/17.
////  Copyright (c) 2017 Victor Young. All rights reserved.
////
//
#include <iostream>
#include <math.h>
#define Max 0x0FFFFFFF
using namespace std;
long long log2_64(long long n) {
    long long logValue = -1;
    while (n) {//
        logValue++;
        n >>= 1;
    }
    return logValue;
}
long long powi(long long base, long long exp)
{
    long long res = 1;
    while (exp) {
        if (exp & 1)
            res *= base;
        exp >>= 1;
        base *= base;
    }
    return res;
}
long long dfs(long long n,long long r,long long l){
    int mid = n%2;
    if (n==1) {
        if (r == l == 1) {
            return 1;
        }
        return 0;
    }
    long long len = powi(2,log2_64(n)+1)-1;
    if (r>l) {
        return 0;
    }
    if (r<=(len+1)/2 && l >= (len+1)/2) {
        if (r==1) {
            return n/2+mid+dfs(n/2, 1, l-(len+1)/2);
        }
        if (l==len) {
            return dfs(n/2, r, (len-1)/2)+mid+n/2;
        }
        return dfs(n/2, r, (len-1)/2)+mid+dfs(n/2, 1, l-(len+1)/2);
    }
    else if(r>(len+1)/2){
        return dfs(n/2, r-(len+1)/2, l-(len+1)/2);
    }
    else{
        return dfs(n/2, r, l);
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    long long n,r,l;
    cin >> n >> r >> l;
//    if (r>n) {
//        r=n;
//    }
    cout << dfs(n,r,l);
//    cout << powi(n,r);
//    cout << log2_64(n);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
using namespace std;
int const N=3e5+4,inf=1e9+7;
ll n,m,k,k1,x,ans,q,b[N],y,a[N];ll dp[N][132],st[N],hs[N];
char ch;
string s,t,s1[50],s2[50];
vector <ll> v[N],ot;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    if (n==1) return cout<<"1 1",0;
    ans=inf;
    for (int i=n/2;i>0;i--)
    {
        if (n%i==0)
        {
            if (ans>abs((n/i)-i)) x=i,y=n/i,ans=abs((n/i)-i);
        }
    }
    cout<<x<<" "<<y;
}


#include <iostream>
using namespace std;
int main ()
{
	long long a, b;
	cin>>a>>b;
	long long ans = 1, k = b % 10;
	if(b % 10 == 0)
	k = 10;
	for(long long i = (a + 1) % 10; i <= k; i++)
	{
		ans *= i;
	}
	ans %= 10;
	cout<<ans;//<<" "<<(a + 1) % 10<<" "<<b % 10;;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int a;
    cin >> a;
    if(a == 1) {
        cout << "1\n";
        return 0;
    }
    if(a > -2) {
        for(;;);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long int maxn=6e3, mod=998244353;
long long a,b,c,ans;
long long s[maxn][maxn],f[maxn];
int main(){
	cin>>a>>b>>c;
	f[1]=1;
	for(int i=2;i<maxn;i++){
		s[i][1]=1;
		s[1][i]=1;
		f[i]=(f[i-1]*i)%mod;
	}
	for(int i=2;i<maxn;i++){
		for(int j=2;j<maxn;j++){
			s[i][j]=(s[i-1][j]+s[i-1][j-1])%mod;
		}
	}
	for(int i=0;i<min(a,b);i++){
		ans+=(s[a][i]*s[b][i]*f[i])%mod;
	}
	for(int i=0;i<min(a,c);i++){
		ans+=(s[a][i]*s[c][i]*f[i])%mod;
	}
	for(int i=0;i<min(b,c);i++){
		ans+=(s[a][i]*s[b][i]*f[i])%mod;
	}
	cout<<ans;
}


#define _CRT_SECURE_NO_WARNINGS

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

const long long INF = 2005;
const long long mod = 1e9 + 7;
const double eps = 1e-5;

struct inr {
    int n, k;

    inr (){};
};

using namespace std;

vector <long long> perf, num;
long long h, n, w, p[111];

void qwe(long long a, vector <long long> &num)
{
    if (a == 1)
    {
        num.push_back(1);
        return;
    }
    else {
        num.push_back(a%2);
        qwe (a/2, num);
    }
    return;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    cin >> h >> n;
    p[0] = 1;
    for (int i = 1; i <= h; ++i)
    {
        p[i] = p[i-1]*2;
    }
    qwe (n-1, num);
    for (int i = num.size(); i<h; ++i)
        num.push_back(0);
    long long w = 0;
    long long rr = 0;
    for (int i = num.size()-1; i >= 0; --i)
    {
        rr++;
        if (num[i] != w)
        {
            rr+= p[i+1]-1;
        }
        else {
            w = (w+1)%2;
        }
    }
    cout << rr;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

template <typename T>
void unique(vector<T> &u) {
  sort(u.begin(), u.end());
  u.resize(unique(u.begin(), u.end()) - u.begin());
}

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

class dsu {
 public:
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  inline int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

struct point {
  int x, y, id = -1;
  point(){};
  point(int _x, int _y) : x(_x), y(_y) {};
  bool operator < (const point &a) const {
    return id > a.id;
  }
  bool operator == (const point &a) const {
    return x == a.x && y == a.y;
  }
};

vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};

point sp(point a, int id) {
  return point(a.x + dx[id], a.y + dy[id]);
}

long long dis(point a) {
  return 1LL * a.x * INT_MAX + a.y;
}

int n, t;

int main() {
  scanf("%d%d", &n, &t);
  vector<point> a(n);
  range(i, 0, n) {
    scanf("%d%d", &a[i].x, &a[i].y);
    a[i].id = i;
  }
  sort(a.begin(), a.end(), [&](point u, point v){return make_pair(u.x, u.y) < make_pair(v.x, v.y);});
  vector<vector<int>> edges(n, vector<int>(8, -1));
  dsu ss(n);
  int l = 0, r = 0;
  range(i, 0, n) {
    if(i < n - 1 && a[i].x == a[i + 1].x && a[i].y == a[i + 1].y - 1) {
      edges[a[i].id][0] = a[i + 1].id;
      edges[a[i + 1].id][4] = a[i].id;
      ss.unite(a[i].id, a[i + 1].id);
    }
    while(r < n && make_pair(a[r].x, a[r].y) <= make_pair(a[i].x + 1, a[i].y + 1)) r++;
    while(l < n && make_pair(a[l].x, a[l].y) < make_pair(a[i].x + 1, a[i].y - 1)) l++;
    range(j, l, r) {
      int dif = a[j].y - a[i].y;
      int id;
      if(!dif) id = 2;
      else id = (dif > 0 ? 1 : 3);
      edges[a[i].id][id] = a[j].id;
      edges[a[j].id][(id + 4) % 8] = a[i].id;
      ss.unite(a[i].id, a[j].id);
    }
  }
  range(i, 0, n) if(ss.get(i) != ss.get(0)) {
    printf("NO");
    return 0;
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  printf("YES\n");
  dsu st(4 * n + 1);
  auto it = max_element(a.begin(), a.end(), [&](point a, point b){return a.y < b.y;});
  st.unite(4 * n, 4 * it -> id);
  unordered_map<long long, vector<int>> dog;
  for(auto u : a) {
    for(int i = 0; i < 8; i += 2) {
      int v = edges[u.id][i];
      if(v == -1) dog[dis(sp(u, i))].push_back(4 * u.id + i / 2);
    }
  }
  for(auto u : a) {
    vector<int> xs(8);
    range(i, 0, 8) xs[i] = edges[u.id][i] >= 0;
    for(int i = 1; i < 8; i += 2) {
      if(!xs[i] && !xs[i - 1] && !xs[(i + 1) % 8]) st.unite(4 * u.id + i / 2, 4 * u.id + ((i + 1) % 8) / 2);
    }
  }
  for(auto u : dog) {
    point cc = point(u.first / INT_MAX, u.first % INT_MAX);
    vector<int> id = u.second;
    for(auto i : id) st.unite(i, id.front());
    for(int i = 0; i < 8; i += 2) {
      if(dog.count(dis(sp(cc, i)))) st.unite(id[0], dog[dis(sp(cc, i))][0]);
    }
  }
  vector<int> alive(n, 1);
  int iter = 0;
  vector<int> pp(4 * n + 1);
  auto valid = [&] (point v) {
    iter++;
    bool ok = false;
    range(i, 0, 8) {
      if(~edges[v.id][i] && alive[edges[v.id][i]]) {
        int xs = 8;
        i++;
        if(i == 8) i = 0;
        vector<int> pc;
        while(xs--) {
          if(~edges[v.id][i] && alive[edges[v.id][i]]) {
            for(auto j : pc) {
              if(pp[j] == iter) return false;
            }
            for(auto j : pc) pp[j] = iter;
            pc.clear();
          } else if(i % 2 == 0) {
            pc.push_back(st.get(4 * v.id + i / 2));
            ok |= st.get(4 * v.id + i / 2) == st.get(4 * n);
          }
          i++;
          if(i == 8) i = 0;
        }
        break;
      }
      ok |= i == 7;
    }
    return ok;
  };
  set<point> pos;
  range(i, 0, n) if(valid(a[i])) pos.insert(a[i]);
  vector<vector<int>> in(4 * n + 1);
  range(i, 0, 4 * n + 1) in[st.get(i)].push_back(i);
  vector<int> res;
  int cnt = 0;
  vector<int> ap(n);
  while(pos.size()) {
    cnt++;
    res.push_back(pos.begin() -> id);
    point cc = *pos.begin();
    pos.erase(pos.begin());
    alive[cc.id] = 0;
    vector<int> id, _id;
    range(i, 0, 8) {
      if(~edges[cc.id][i] && alive[edges[cc.id][i]]) id.push_back(edges[cc.id][i]);
    }
    for(int i = 0; i < 8; i += 2) {
      if(edges[cc.id][i] == -1 || !alive[edges[cc.id][i]]) {
        if(st.get(4 * n) != st.get(4 * cc.id + i / 2)) _id.push_back(st.get(4 * cc.id + i / 2));
      }
    }
    unique(_id);
    for(auto i : _id) {
      for(auto j : in[i]) id.push_back(j / 4);
    }
    unique(id);
    for(int i = 0; i < 8; i += 2) {
      if(edges[cc.id][i] == -1 || !alive[edges[cc.id][i]]) {
        st.unite(4 * cc.id + i / 2, 4 * n);
      }
    }
    for(int i = 0; i < 8; i += 2) {
      if(~edges[cc.id][i] && alive[edges[cc.id][i]]) {
        int v = edges[cc.id][i];
        st.unite(4 * v + ((i + 4) % 8) / 2, 4 * n);
      }
    }
    for(auto i : id) {
      if(ap[i] == cnt) continue;
      ap[i] = cnt;
      if(alive[i] && valid(a[i])) pos.insert(a[i]);
      else pos.erase(a[i]);
    }
  }
  reverse(res.begin(), res.end());
  for(auto i : res) printf("%d\n", i + 1);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define x first
//#define y
#define alcv     ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

int main()
{
    string s; cin>>s; ll n=s.size(),cnt1=0;
    if(n==1){if(s[0]=='0')cout<<"0";else cout<<"1";return 0;}
    for(ll i=0;i<n;i++)
    {
        if(s[i]=='1')cnt1++;
    }
    if(cnt1>1)cout<<((n-1)/2)+1;
    else cout<<(n)/2;

}


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

using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;

int main(){
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	int i, N;
	char ch[7] = {'R', 'O', 'Y', 'G', 'B', 'I', 'V'};
	cin >> N;
	for( i = 0; i < N /7 *7; i++ ){
		cout << ch[i % 7];
	}
	for( i = N % 7; i > 0; i++ ){
		cout << ch[4-i];
	}

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define vi vector
#define pb push_back
#define ll long long int
#define pii pair< int, int>
#define ff  first
#define ss  second
#define sort(a)		sort(a.begin(),a.end())
#define rev(a)		reverse(a.begin(),a.end())
#define fr(i,a,b)	for( ll i=a; i<b; i++)
#define Fr(i,a,b)	for( ll i=a; i<=b; i++)
#define rfr(i,a,b)	for( ll i=a; i>b; i--)
#define rFr(i,a,b)	for( ll i=a; i>=b; i--)
#define fio	ios_base::sync_with_stdio(), cin.tie(0), cout.tie(0)
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

const ll N=60;
const ll mod=1e9+7;

ll ffe( ll a, ll b){
	if(b==0){
		return 1;
	}else{
		if(b%2)
			return (a*ffe((a*a)%mod,b/2))%mod;
		else
			return ffe((a*a)%mod,b/2);
	}
}

int main(){

	fio;
	ll x, s;
	cin >> x >> s;

	if(x>s){
		cout << -1;
		return 0;
	}

	vi<ll> p2(N+1,1);
	for( int i=1; i<=N; i++){
		p2[i]=2*p2[i-1];
	}

	string str = bitset<N+1>(s).to_string();
	rev(str);

	vi<ll> a(N+1,0);
	for( int i=0; i<=N; i++){
		if(str[i]=='1'){
			a[i]=1;
		}
	}

	str = bitset<N+1>(x).to_string();
	rev(str);

	for( int i=N; i>=0; i--){

		if(str[i]=='1'){

			if(a[i]==0){

				ll j=i+1;
				bool f = false;
				while(j<=N){
					if(a[j]>=2){
						f = true;
						break;
					}
					j++;
				}

				if(!f){
					cout << -1;
					return 0;
				}

				while(j<i){
					a[j-1]=a[j-1]+4;
					a[j]=a[j]-2;
					j--;
				}
			}

			if(i==0){

				if(a[i]%2==0){
					cout << -1;
					return 0;
				}

			}else{

				if(a[i]%2==0){

					a[i-1]=a[i-1]+2;
					a[i]--;
				}

			}

		}else{

			if(i==0){

				if(a[i]%2!=0){
					cout << -1;
					return 0;
				}

			}else{

				if(a[i]%2==1){
					a[i-1]=a[i-1]+2;
					a[i]--;
				}

			}
		}
	}

	for( ll i=0; i<N; i++){
		ll val = a[i]/4;
		a[i] = a[i]-4*val;
		a[i+1] = a[i+1] + 2*val;
	}

	ll n=0;
	for( ll i=0; i<=N; i++){
		n = max(n,a[i]);
	}
	cout << n << "\n";

	while(true){
		ll val=0;
		for( int i=0; i<=N; i++){
			if(a[i]!=0){
				val=val+p2[i];
				a[i]--;
			}
		}
		if(val==0){
			break;
		}
		cout << val << " ";
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef int long long ll;
vector<ll > vec2;
vector<ll > vec3;
vector <pair<ll ,ll > > vec;
int  main()
{
	ll  a,b;
	cin>>a>>b;
	ll  c=a+b;
	for(ll  i=1;i*i<=c;i++)
	{
		if(c%i==0)
		{
			vec.push_back(make_pair(i,c/i));
		}
	}
	for(ll  j=1;j*j<=min(a,b);j++)
	{
		if(min(a,b)%j==0)
			{
				if(j==min(a,b)/j)
					vec2.push_back(j);
				else
				{
					vec2.push_back(j);
					vec2.push_back(min(a,b)/j);
				}
			}
	}
	for(ll  j=1;j*j<=max(a,b);j++)
	{
		if(max(a,b)%j==0)
			{
				if(j==max(a,b)/j)
					vec3.push_back(j);
				else
				{
					vec3.push_back(j);
					vec3.push_back(max(a,b)/j);
				}
			}
	}
	sort(vec3.begin(),vec3.end());
	sort(vec2.begin(),vec2.end());
	if(a==1 || b==1)
	{
		int size=vec.size();
		cout<<2*(vec[size-1].first+vec[size-1].second)<<endl;
		return 0;
	}
	ll  low=0;
	ll  high=vec.size()-1;
	while(high>=low)
	{
		//printf("***\n");
		ll  mid=low+(high-low)/2,l1;
		if(min(a,b)%vec[mid].second==0)
			l1=min(min(a,b)%vec[mid].second,vec[mid].first);
		else
			l1=min(min(a,b)%vec[mid].second+1,vec[mid].first);

		ll  l2=vec[mid].second;

		//cout<<l1<<" "<<l2<<endl;
		// there should be atlest one factor between l1 and l2 that divides b
		vector <ll> :: iterator it;
		it=upper_bound(vec2.begin(),vec2.end(),l2);
		it--;
		//cout<<*it<<endl;
		if(*it>=l1)
			low=mid+1;
		else
			high=mid-1;

		//cout<<low<<"**"<<high<<endl;
	}
	//cout<<high<<endl;
	ll final=2*(vec[high].first+vec[high].second);
//	cout<<final<<endl;
	 low=0;
	 high=vec.size()-1;
	while(high>=low)
	{
		//printf("***\n");
		ll mid=low+(high-low)/2,l1;
		//ll l1=ceil(max(a,b)/vec[mid].second);
		if(max(a,b)%vec[mid].second==0)
			l1=min(max(a,b)%vec[mid].second,vec[mid].first);
		else
			l1=min(max(a,b)%vec[mid].second+1,vec[mid].first);
		ll l2=vec[mid].second;

		//cout<<l1<<" "<<l2<<endl;
		// there should be atlest one factor between l1 and l2 that divides b
		vector <ll> :: iterator it;
		it=upper_bound(vec3.begin(),vec3.end(),l2);
		it--;
		//cout<<*it<<endl;
		if(*it>=l1)
			low=mid+1;
		else
			high=mid-1;

		//cout<<low<<"**"<<high<<endl;
	}

	cout<<min(final,2*(vec[high].first+vec[high].second))<<endl;
}


#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
    int x,y,z,ans;
    cin>>x>>y;
    if(x==0 && y==0)
    cout<<"0";
    else if(x==1 && y==0)
    cout<<"0";
    else
    {
    z=max(abs(x),abs(y));
    ans=4*(z-1);
    if(x==z)
    ans++;
    else if(x==-z)
    ans+=3;
    else if(y==z)
    ans+=2;
    else if(y==-z)
    ans+=4;
    cout<<ans;}
    system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
pair <long long,long long > v[20000001];
int main ()
{
	long long i,j,n,e,t=0,b,m,vr,mx,k,z,a;
	cin>>a>>n;
	for (i=0;i<n+1;i++) v[i].first=0;
	mx=a+n-1;
	i=4;
	j=0;
	b=4;
	k=2;
	z=1;
	while (true)
	{
		if (k*k>mx) break;
		if (b<=mx) {
			if (b>=a) {
				if (v[b].first==0) {v[b].first=1;v[b].second=z;} else
					if ((v[b].first==1) && (v[b].second>z)) v[b].second=z;}
			z++;
		b=b+i;} else
		{k++;i=k*k;b=i;z=1;}

	}
	//sort(v,v+j);
	/*if (j!=0) {
	for (i=0;i<j+1;i++)
	{
		if (i==j && a>v[j].first) {t=t+a;a++;break;}
		if (v[i].first>a && (i==0 || v[i-1].first<a)) {t=t+a;a++;i--;if (a>mx) break;} else
			if(v[i].first==a) {t=t+v[i].second;a++;if (a>mx) break;}
	}}
	while (a<mx) {t=t+a;a++;}
	if (j==0) {while (a<=mx) {t=t+a;a++;}}*/
	for (i=0;i<mx+1;i++)
	{
		if (a==mx+1) break;
		if (i==a)
		{
			if (v[i].first==0) t=t+a; else
				if (v[i].first==1) t=t+v[i].second;
			a++;
		}
	}
	cout<<t;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct node
{
    int a, site, ans;
}cup[102];
bool cmp(const node& x, const node& y)
{
    return x.a < y.a;
}
bool cmp2(const node& x, const node& y)
{
    return x.site < y.site;
}
int main()
{
    int n, w;
    cin >> n >> w;
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> cup[i].a;
        cup[i].site = i;
        if(cup[i].a & 1)
            cup[i].ans = cup[i].a / 2 + 1;
        else cup[i].ans = cup[i].a / 2;
        sum += cup[i].ans;
    }
    if (sum > w) {
        cout << "-1" << endl;
    }
    else {
        sort (cup + 1, cup + n + 1, cmp);
        int j = n;
        int last = w - sum;
        while (last) {
            if(cup[j].a - cup[j].ans <= last) {
                cup[j].ans = cup[j].a;
                last -= cup[j].a - cup[j].ans;
            }
            else {
                cup[j].ans += last;
                last = 0;
            }
            --j;
        }
        sort (cup + 1, cup + n + 1, cmp2);
        for (int i = 1; i <= n; ++i) {
            if(i - 1) cout << " ";
            cout << cup[i].ans;
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
#include<utility>
using namespace std;
int main()
{
 fast;
 int n, a, b ,i; int d=0;
 cin>>n>>a>>b;    int c[n],j=n-1;

   for(i=0;i<n;++i)
  {
    cin>>c[i];
  }

   for(i=0;i<n;++i)
  {
    if(c[i]!=c[j] && c[i]!=2 && c[j]!=2)
    d=-1;

    --j;
  }
    j=n-1;
    for(i=0;i<n;++i)
  {
    if(c[i]==0 && c[j]==2) d+=a;
    if(c[i]==1 && c[j]==2) d+=b;
    if(c[i]==2 && c[j]==2) d+=min(a,b);
    --j;
  }
    return d;
}


/** Micro Mezzo Macro Flation -- Overheated Economy ., Last Update: Feb. 18th 2013 **/ //{

/** Header .. **/ //{
#define LOCAL

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (int n____=(i=0,int(n));i<n____;++i)
#define FOR_C_N(i, a, b) for (int b____=(i=0,int(b);i<b____;++i)
#define DWN_C_N(i, b, a) for (int a____=(i=b-1,int(a));i>=a____;--i)
#define REP_1_C_N(i, n) for (int n____=(i=1,int(n));i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (int b____=(i=1,int(b);i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (int a____=(i=b,int(a));i>=a____;--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
#define REP_S(i, str) for (char*i=str;*i;++i)
#define REP_L(i, hd, nxt) for (int i=hd;i;i=nxt[i])
#define REP_G(i, u) REP_L(i,hd[u],suc)
#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define fi first
#define se second

#define Rush for(int ____T=RD(); ____T--;)

#define Display(A, n, m) {                      \
	REP(i, n){		                            \
        REP(j, m) cout << A[i][j] << " ";       \
        cout << endl;				            \
	}						                    \
}

#define Display_1(A, n, m) {				    \
	REP_1(i, n){		                        \
        REP_1(j, m) cout << A[i][j] << " ";     \
		cout << endl;		            		\
	}						                    \
}

#pragma comment(linker, "/STACK:36777216")
//#pragma GCC optimize ("O2")

string __file__(){
    string res = __FILE__;
    int r = SZ(res) - 1; while (res[r] !=  '.') --r;
    int l = r - 1; while (res[l] != '\\') --l; ++l;
    return res.substr(l, r-l);
}

void Exec(string a, string b, string c){
    if (b.empty()) b = __file__();
    string cmd = a + ' ' + b + '.' + c;
    system(cmd.c_str());
}

void Ruby(string file = ""){Exec("ruby", file, "rb");}
void Python(string file = ""){Exec("python", file, "py");}
void Haskell(string file = ""){Exec("runghc", file, "hs");}
void Pascal(string file = ""){Exec("pascal", file, "pas");}

typedef long long LL;
//typedef long double DB;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
//typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;

template<class T> inline T& RD(T &);
template<class T> inline void OT(const T &);
inline LL RD(){LL x; return RD(x);}
inline DB& RF(DB &);
inline DB RF(){DB x; return RF(x);}
inline char* RS(char *s);
inline char& RC(char &c);
inline char RC();
inline char& RC(char &c){scanf(" %c", &c); return c;}
inline char RC(){char c; return RC(c);}
//inline char& RC(char &c){c = getchar(); return c;}
//inline char RC(){return getchar();}

template<class T> inline T& RDD(T &x){
    char c; for (c = getchar(); c < '-'; c = getchar());
    if (c == '-'){x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c;}
    else {x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';}
    return x;
}

inline LL RDD(){LL x; return RDD(x);}

template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}
template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}
template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}
inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
template<class T0,class T1>inline void RDD(const T0&a, const T1&b){RDD(a),RDD(b);}
template<class T0,class T1,class T2>inline void RDD(const T0&a, const T1&b, const T2&c){RDD(a),RDD(b),RDD(c);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T> inline void CLR(T &A){A.clear();}

template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}
template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}

template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
template<class T> inline T& RVS(T &A){reverse(ALL(A)); return A;}
template<class T> inline T& UNQ(T &A){A.resize(unique(ALL(SRT(A)))-A.begin());return A;}

template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}

//}

/** Constant List .. **/ //{

const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

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

//}

/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
template<class T> inline void checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);}
template<class T> inline void checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
inline int ceil(int x, int y){return (x - 1) / y + 1;}
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
inline int sgn(DB x, DB y){return sgn(x - y);}
//}
// <<= '1. Bitwise Operation ., //{
namespace BO{

inline bool _1(int x, int i){return bool(x&1<<i);}
inline bool _1(LL x, int i){return bool(x&1LL<<i);}
inline LL _1(int i){return 1LL<<i;}
inline LL _U(int i){return _1(i) - 1;};

inline int reverse_bits(int x){
    x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
    x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
    x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
    return x;
}

inline LL reverse_bits(LL x){
    x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
    x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
    x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
    x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
    x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
    x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
    return x;
}

template<class T> inline bool odd(T x){return x&1;}
template<class T> inline bool even(T x){return !odd(x);}
template<class T> inline T low_bit(T x) {return x & -x;}
template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}
template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}

inline int low_idx(int x){return __builtin_ffs(x);}
inline int low_idx(LL x){return __builtin_ffsll(x);}
inline int high_idx(int x){return low_idx(reverse_bits(x));}
inline int high_idx(LL x){return low_idx(reverse_bits(x));}
inline int clz(int x){return __builtin_clz(x);}
inline int clz(LL x){return __builtin_clzll(x);}
inline int ctz(int x){return __builtin_ctz(x);}
inline int ctz(LL x){return __builtin_ctzll(x);}
inline int parity(int x){return __builtin_parity(x);}
inline int parity(LL x){return __builtin_parityll(x);}
inline int lg2(int a){return 31 - clz(a);}
inline int lg2(LL a){return 63 - clz(a);}
inline int count_bits(int x){return __builtin_popcount(x);}
inline int count_bits(LL x){return __builtin_popcountll(x);}

} using namespace BO;//}
// <<= '2. Number Theory .,//{
namespace NT{
inline LL __lcm(LL a, LL b){return a*b/__gcd(a,b);}
inline void INC(int &a, const int b){a += b; if (a >= MOD) a -= MOD;}
inline int sum(int a, const int b){a += b; if (a >= MOD) a -= MOD; return a;}
inline void DEC(int &a, const int b){a -= b; if (a < 0) a += MOD;}
inline int dff(int a, const int b){a -= b; if (a < 0) a  += MOD; return a;}
inline void MUL(int &a, const int b){a = (LL)a * b % MOD;}
inline int pdt(const int a, const int b){return (LL)a * b % MOD;}

inline int sum(int a, int b, int c){return sum(sum(a, b), c);}
inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}
inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}
inline int pdt(int a, int b, int c, int d){return pdt(pdt(pdt(a, b), c), d);}

inline int pow(int a, int b){
    int c(1); while (b){
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

inline int pow(int a, LL b){
    int c(1); while (b){
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

template<class T> inline T pow(T a, LL b){
    T c(1); while (b){
        if (b&1) c *= a;
        a *= a, b >>= 1;
    }
    return c;
}

inline int _I(int b){
    int a = MOD, x1 = 0, x2 = 1, q;
    while (true){
        q = a / b, a %= b;
        if (!a) return (x2 + MOD) % MOD;
        DEC(x1, pdt(q, x2));

        q = b / a, b %= a;
        if (!b) return (x1 + MOD) % MOD;
        DEC(x2, pdt(q, x1));
    }
}

inline void DIV(int &a, int b){MUL(a, _I(b));}
inline int qtt(int a, int b){return pdt(a, _I(b));}

inline int phi(int n){
    int res = n; for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
        DEC(res, qtt(res, i));
        do{n /= i;} while(!(n%i));
    }
    if (n != 1)
        DEC(res, qtt(res, n));
    return res;
}

} using namespace NT;//}
// <<= '7. Matrix Theory .,//{
namespace MT{

    const int N = 100;
    int n = 0;

    typedef int rec;

    struct matrix{
        rec d[N][N];

        void init(rec e = 0){RST(d); if(e) REP(i, n) d[i][i] = e;}
        matrix(rec e = 0){init(e);}

        matrix operator *(const matrix &rhs) const{
            matrix res; //REP_3(i, j, k, n, n, n) res.d[i][j] += d[i][k] * rhs.d[k][j];
            REP_2(i, j, n, n){
                LL tmp = 0; REP(k, n) tmp += (LL) d[i][k] * rhs.d[k][j];
                res.d[i][j] = tmp % MOD;
            }
            return res;
        }

        matrix& operator *=(const matrix& rhs){(*this) = (*this) * rhs;}

        inline int res(){
            int res = 0;
            REP(i, n) INC(res, d[0][i]);
            //REP_2(i, j, n, n) INC(res, d[i][j]);
            return res;
        }
    };

    /*inline matrix pow_sum(const matrix& a, ULL nn){
        if (nn == 1) return matrix(1);
        matrix t; REP_2(i, j, n, n) t.d[i][j] = t.d[i][j+n] = a.d[i][j];
        FOR_C(i, n, n*2) t.d[i][i] = 1; n <<= 1; t = pow(t, nn), n >>= 1;
        REP_2(i, j, n, n) t.d[i][j] = t.d[i][j+n];
        return t;
    }*/

    inline matrix pow_sum(const matrix& a, ULL nn){
        if (nn == 1) return matrix(1);
        matrix t; REP_2(i, j, n, n) t.d[i][j] = a.d[i][j];
        REP(i, n) t.d[i][i+n] = t.d[i+n][i+n] = 1; n <<= 1; t = pow(t, nn), n >>= 1;
        REP_2(i, j, n, n) t.d[i][j] = t.d[i][j+n];
        return t;
    }

    template<class T> T pow_sum(T a, ULL nn){
        int _n = n; n = 1; matrix t; t.d[0][0] = a;
        t = pow_sum(t, nn), n = _n;
        return t.d[0][0];
    }

} // using namespace MT;//}
// <<= '8. Stringology .,//{
namespace SL{
    namespace KMP{

        void calc_pi(const char *P, int n, int *pi){
            for (int i = 1, j = pi[0] = -1; i < n; ++i){
                while (j >= 0 && P[i] != P[j+1]) j = pi[j];
                if (P[i] == P[j+1]) ++j;
                pi[i] = j;
            }
            //REP(i, n) cout << pi[i] << " "; cout << endl;
        }

        bool run(const char *T, int n, const char *P, int m, const int *pi){
            for (int i = 0, j = -1; i < n; ++i){
                while (j >= 0 && T[i] != P[j+1]) j = pi[j];
                if (T[i] == P[j+1]) ++j;
                if (j == m - 1) return true;
            }
            return false;
        }

    } //using namespace KMP;

    namespace Z{
        void calc_z(const char *P, int n, int *z){

            z[0] = n;

            for (int i = 1, l = 0, r = 0; i < n; ++i){
                if (i > r){
                    for(l = r = i; r < n && P[r] == P[r - l];) ++r;
                    z[i] = r - l, --r;
                }
                else {
                    if (z[i - l] < r - i + 1) z[i] = z[i - l];
                    else {
                        for (l = i;r < n && P[r] == P[r - l];) ++r;
                        z[i] = r - l, --r;
                    }
                }
            }

            //REP(i, n) cout << z[i] << " "; cout << endl;
        }

        int run(const char *T, int n, const char *P, int m, const int *z){

            int ex; REP_C_N(ex, min(n, m)) if (T[ex] != P[ex]) break;

            int res = ex == m;

            for (int i = 1, l = 0, r = 0; i < n; ++i){
                if (i > r){
                    for (l = r = i; r < n && T[r] == P[r - l];) ++r;
                    ex = r - l, --r;
                }
                else {
                    if (z[i - l] < r - i + 1) ex = z[i - l];
                    else {
                        for (l = i; r < n && T[r] == P[r - l];) ++r;
                        ex = r - l, --r;
                    }
                }
                if (ex == m) ++res;
            }

            return res;
        }
    } //using namespace Z;

    void Manacher(char s[], int n, int p[]){
        const int NN = 0;
        static char ss[NN*2+2]; int nn = 2*n+2;
        ss[0] = '$', ss[nn-1] = '#', ss[nn] = 0;
        REP(i, n) ss[i*2+1] ='#', ss[i*2+2] = s[i];
        int mx = 0, id = 0; FOR(i, 1, nn){
            p[i] = mx > i ? min(p[2*id-i], mx - i) : 1;
            while (ss[i+p[i]] == ss[i-p[i]]) ++p[i];
            if (i + p[i] > mx) mx = i + p[i], id = i;
        }
    }

} //using namespace SL;//}
// <<= '9. Comutational Geometry .,//{
namespace CG{

struct Po; struct Line; struct Seg;

struct Po{
    DB x, y; Po(DB _x=0, DB _y=0):x(_x), y(_y){}
    friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
    friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}
    bool operator ==(const Po& r)const{return !sgn(x-r.x) && !sgn(y-r.y);};
    bool operator !=(const Po& r)const{return sgn(x-r.x) || sgn(y-r.y);}
    Po operator +(const Po& r)const{return Po(x+r.x, y+r.y);}
    Po operator -(const Po& r)const{return Po(x-r.x, y-r.y);}
    Po operator *(DB k)const{return Po(x*k,y*k);}
    Po operator /(DB k)const{return Po(x/k,y/k);}
    DB operator *(const Po&) const;
    DB operator ^(const Po&) const;

    bool operator <(const Po &r) const{return sgn(x,r.x)<0||!sgn(x,r.x)&&sgn(y,r.y)<0;}
    Po operator -()const{return Po(-x,-y);}
    Po& operator +=(const Po &r){x+=r.x,y+=r.y;return *this;}
    Po& operator -=(const Po &r){x-=r.x,y-=r.y;return *this;}
    Po& operator *=(DB k){x*=k,y*=k;return*this;}
    Po& operator /=(DB k){x/=k,y/=k;return*this;}

    DB length_sqr()const{return sqr(x)+sqr(y);}
    DB length()const{return sqrt(length_sqr());}
    Po unit()const{return *this/length();}
    bool dgt()const{return !sgn(x)&&!sgn(y);}
    DB atan()const{return atan2(y,x);}
    void rotate(DB alpha, const Po& o = Po()){
        x -= o.x, y -= o.y;
        (*this) = Po(x * cos(alpha) - y * sin(alpha), y * cos(alpha) + x * sin(alpha)) + o;
    }

    void input(){RF(x,y);}
};

Po operator *(DB k, Po a){return a * k;}

#define innerProduct dot
#define scalarProduct dot
#define outerProduct det
#define crossProduct det

inline DB dot(const DB &x1, const DB &y1, const DB &x2, const DB &y2){return x1 * x2 + y1 * y2;}
inline DB dot(const Po &a, const Po &b){return dot(a.x, a.y, b.x, b.y);}
inline DB dot(const Po &p0, const Po &p1, const Po &p2){return dot(p1 - p0, p2 - p0);}

inline DB det(const DB &x1, const DB &y1, const DB &x2, const DB &y2){return x1 * y2 - x2 * y1;}
inline DB det(const Po &a, const Po &b){return det(a.x, a.y, b.x, b.y);}
inline DB det(const Po &p0, const Po &p1, const Po &p2){return det(p1 - p0, p2 - p0);}

template<class T1, class T2> inline int dett(const T1 &x, const T2 &y){return sgn(det(x, y));}
template<class T1, class T2> inline int dott(const T1 &x, const T2 &y){return sgn(dot(x, y));}
template<class T1, class T2, class T3> inline int dett(const T1 &x, const T2 &y, const T3 &z){return sgn(det(x, y, z));}
template<class T1, class T2, class T3> inline int dott(const T1 &x, const T2 &y, const T3 &z){return sgn(dot(x, y, z));}
template<class T1, class T2, class T3, class T4> inline int dett(const T1 &x, const T2 &y, const T3 &z, const T4 &w){return sgn(det(x, y, z, w));}
template<class T1, class T2, class T3, class T4> inline int dott(const T1 &x, const T2 &y, const T3 &z, const T4 &w){return sgn(dot(x, y, z, w));}

inline DB dist_sqr(const DB &x, const DB &y){return sqr(x) + sqr(y);}
inline DB dist_sqr(const DB &x, const DB &y, const DB &z){return sqr(x) + sqr(y) + sqr(z);}
inline DB dist_sqr(const Po &a, const Po &b){return sqr(a.x - b.x) + sqr(a.y - b.y);}

template<class T1, class T2> inline DB dist(const T1 &x, const T2 &y){return sqrt(dist_sqr(x, y));}
template<class T1, class T2, class T3> inline DB dist(const T1 &x, const T2 &y, const T3 &z){return sqrt(dist_sqr(x, y, z));}

DB Po::operator *(const Po &r)const{return dot(*this, r);}
DB Po::operator ^(const Po &r)const{return det(*this, r);}

struct Line{
    Po a, b;

    Line(DB x0=0, DB y0=0, DB x1=0, DB y1=0):a(Po(x0, y0)), b(Po(x1, y1)){}
    Line(const Po &a, const Po &b):a(a), b(b){}
    Line(const Line &l):a(l.a), b(l.b){}

    friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
    Line operator +(Po x)const{return Line(a + x, b + x);}
    DB length()const{return (b-a).length();}
    bool dgt()const{return (b-a).dgt();}
    void input(){a.input(), b.input();}

    int side(const Po& p){return dett(a, b, p);}
    bool same_side(const Po& p1, const Po& p2){return side(p1) == side(p2);}
    void getequation(DB& A, DB& B, DB& C) const{A = a.y - b.y, B = b.x - a.x, C = det(a, b);}
};

struct Seg: Line{
};

inline DB dot(const Line &l1, const Line &l2){return dot(l1.b - l1.a, l2.b - l2.a);}
inline DB det(const Line &l1, const Line &l2){return det(l1.b - l1.a, l2.b - l2.a);}

inline DB dist_sqr(const Po &p, const Line &l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
inline DB dist_sqr(const Po &p, const Seg &l){
    Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
    if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
    else return min(v1.length_sqr(), v2.length_sqr());
}
inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}
inline DB dist_sqr(Line l1, Line l2){
    if (sgn(det(l1, l2)) != 0) return 0;
    return dist_sqr(l1.a, l2);
}
inline DB dist_sqr(Line l1, Seg l2){
    Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
    return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
}

bool isIntersect(Seg l1, Seg l2){

    if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;

    return
        min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
        min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
        min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
        min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
    sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
    sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;

}

inline DB dist_sqr(Seg l1, Seg l2){
    if (isIntersect(l1, l2)) return 0;
    else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
}

inline bool isOnSide(const Po &p, const Seg &l){
    return p == l.a || p == l.b;
}

inline bool isOnSeg(const Po &p, const Seg &l){
    return sgn(det(p, l.a, l.b)) == 0 &&
        sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
}

inline bool isOnSegg(const Po &p, const Seg &l){
    return sgn(det(p, l.a, l.b)) == 0 &&
        sgn(l.a.x, p.x) * sgn(l.b.x, p.x) < 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) < 0;
}

inline Po intersect(const Line &l1, const Line &l2){
    return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
}

// perpendicular foot
inline Po intersect(const Po & p, const Line &l){
    return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
}

inline Po rotate(Po p, DB alpha, const Po &o = Po()){
    p.rotate(alpha, o);
    return p;
}

} using namespace CG;//}
//}

/** Miscellaneous .. **/ //{
// <<= 'Random Event .. . //{
namespace RNG{
//srand((unsigned)time(NULL));
inline unsigned int rand16(){return ((rand()) << 15) ^ rand();}
inline unsigned int rand32(){return (rand16() << 16) | rand16();}
inline ULL rand64(){return ((LL)rand32() << 32) | rand32();}
inline ULL random(LL l, LL r){return l == r ? l : rand64() % (r - l) + l;}
int dice(){return rand() % 6;}
bool coin(){return bool(rand() % 2);}
} using namespace RNG;
//}
// <<= 'Clock .. . //{
namespace CLOCK{
DB s0, s1, rd, k, T;
inline DB getTime(){
#ifdef LOCAL
    return 1.0 * clock() / CLOCKS_PER_SEC;
#else
    timeval tv;
    gettimeofday(&tv, 0);
    return tv.tv_sec + tv.tv_usec * 1e-6;
#endif
}

inline void st0(DB _T = 0.98){T = _T, s0 = getTime();}
inline void st1(DB _k = 1.618){k = _k, s1 = getTime();}
inline void ed1(){rd = getTime() - s1;}
inline DB elapsed(){return getTime() - s0;}
inline bool TLE(){return elapsed() + rd * k < T;}
} //using namespace CLOCK;
//}
// <<= 'Temp .. . //{
namespace TMP{
template<class T> PTT operator+(const PTT &p1, const PTT &p2) {
	return PTT(p1.fi + p2.fi, p1.se + p2.se);
}

template<class T> PTT operator-(const PTT &p1, const PTT &p2) {
	return PTT(p1.fi - p2.fi, p1.se - p2.se);
}

template<class T> PTT operator*(const PTT &lhs, T k){
    return PTT(lhs.fi * k, lhs.se * k);
}
} using namespace TMP;
//}
// <<= 'Bignum ., //{
namespace BIGNUM{

const int __base = 1e8;
const int P10[] = {1, 10, int(1e2), int(1e3), int(1e4), int(1e5), int(1e6), int(1e7), int(1e8), int(1e9)};
const int MAX_BUF_SIZE = 109;
char __buf[MAX_BUF_SIZE];

class bignum{
          friend istream& operator>>(istream&, bignum&);
          friend ostream& operator<<(ostream&, const bignum&);
          friend bignum operator +(const bignum&, const bignum&);
          friend bignum operator -(const bignum&, const bignum&);
          friend bignum operator *(const bignum&, const bignum&);
          friend bignum operator /(const bignum&, const bignum&);
          friend bignum operator %(const bignum&, const bignum&);
          friend bignum operator +(const bignum&, const int&);
          friend bignum operator -(const bignum&, const int&);
          friend bignum operator *(const bignum&, const int&);
          friend bignum operator /(const bignum&, const int&);
          friend bignum operator %(const bignum&, const int&);
          friend bool operator ==(const bignum&, const bignum&);
          friend bool operator !=(const bignum&, const bignum&);
          friend bool operator <(const bignum&, const bignum&);
          friend bool operator >(const bignum&, const bignum&);
          friend bool operator <=(const bignum&, const bignum&);
          friend bool operator >=(const bignum&, const bignum&);
          friend bool operator ==(const bignum&, const int&);
          friend bool operator !=(const bignum&, const int&);
          friend bool operator <(const bignum&, const int&);
          friend bool operator >(const bignum&, const int&);
          friend bool operator <=(const bignum&, const int&);
          friend bool operator >=(const bignum&, const int&);
          friend int do_comp(const bignum&, const int&);
          friend int do_comp(const bignum&, const bignum&);
          friend void divide(const bignum&, const bignum&, bignum&, bignum&);
          friend bignum pow(bignum, int);
          friend bignum pow(int, int);
    public:
        inline bignum(){};
        inline bignum(int s){
            while (s) data.PB(s%__base), s/=__base;
            if (data.empty()) data.PB(0);
        }

        inline bignum(long long s){
            while (s) data.PB(int(s%__base)), s/=__base;
            if (data.empty()) data.PB(0);
        }

        inline bignum(string s){
            int t, i; CLR(data);
            for (i=int(s.size())-8;i>0;i-=8){
                istringstream(s.substr(i, 8)) >> t;
                data.PB(t);
            }
            istringstream(s.substr(0, i+8)) >> t;
            data.PB(t);
        }

        void input(){
            CLR(data); RS(__buf); int t = 0, c = 0;
            DWN(i, strlen(__buf), 0){
                t += P10[c] * (int(__buf[i]) - '0'), ++c;
                if (c == 8) data.PB(t), c = t = 0;
            }
            if (c) data.PB(t);
        }

        void operator =(const int);
        void operator =(const string);
        void operator =(const bignum);
        bignum& operator +=(const bignum&);
        bignum& operator -=(const bignum&);
        bignum& operator *=(const bignum&);
        bignum& operator /=(const bignum&);
        bignum& operator %=(const bignum&);
        bignum& operator +=(const int&);
        bignum& operator -=(const int&);
        bignum& operator *=(const int&);
        bignum& operator /=(const int&);
        bignum& operator %=(const int&);
        bool undefined();
        int do_try(const int&);
        int do_try(const bignum&);
        void do_trim();
        list<int> data;

    int size(){
        list<int>::iterator it; int res = 0;
        for (it=data.begin(); it!=data.end();it++)
            res += 8;
        it--;
        if (*it >= 10000) {
            if ( (*it) >= 1000000) {if (*it >=10000000) ; else res--;}
            else {if ((*it) >= 100000) res-=2; else res-=3;}
        }
        else
            if ( (*it) >= 100) {if (*it >=1000) res-=4; else res-=5;}
            else {if ((*it) >= 10) res-=6; else res-=7;}

        return res;
    }

    void do_reserve(int a){
        if (a <= 0) return;
        list<int>::iterator it;
        for (it=data.begin(); it!=data.end() && a>0; it++) a-=8;
        if (it == data.end() && a>=0) return;
        a+=8, it--; int f = 1;
        for (int i=0;i<a;i++) f *= 10; (*it) %= f;
        data.erase(++it, data.end());
        do_trim();
    }
};

inline void bignum::operator =(const bignum a){
    data.clear();
    for (list<int>::const_iterator i=a.data.begin();i!=a.data.end();i++){
        data.PB(*i);
    }
}
inline void bignum::operator =(const string a){
    (*this) = bignum(a);
}
inline void bignum::operator =(const int a){
    (*this) = bignum(a);
}

inline istream& operator>>(istream& input, bignum& a){
    string s; int t, i; input >> s; a.data.clear();
    for (i=int(s.size())-8;i>0;i-=8){
        istringstream(s.substr(i, 8)) >> t;
        a.data.PB(t);
    }
    istringstream(s.substr(0, i+8)) >> t;
    a.data.PB(t);
    return input;
}

inline ostream& operator<<(ostream& output, const bignum& a){
    list<int>::const_reverse_iterator i=a.data.rbegin();
    output << *i;
    for (i++;i!=a.data.rend();i++){
        if (*i >= 10000) {
            if (*i >= 1000000) {if (*i>=10000000) cout << *i; else cout << 0 << *i;}
            else {if (*i>=100000) cout << "00" << *i; else cout << "000" << *i;}
        }
        else {
            if (*i >= 100) {if (*i>=1000)  cout << "0000" << *i; else cout << "00000" << *i;}
            else { if (*i>=10) cout << "000000" << *i; else cout << "0000000" << *i;}
        }
    }
    return output;
}

inline bool bignum::undefined(){
    return data.empty();
}

inline int do_comp(const bignum& a, const bignum& b){
    if (a.data.size()<b.data.size()) return -1; if (a.data.size()>b.data.size()) return 1;
    list<int>::const_reverse_iterator i; list<int>::const_reverse_iterator j;
    for (i=a.data.rbegin(),j=b.data.rbegin(); j!=b.data.rend(); i++,j++){
        if (*i<*j) return -1;              //!!!!
        if (*i>*j) return 1;
    }
    return 0;
}
inline int do_comp(const bignum& a, const int& b){
    return do_comp(a, bignum(b));
}

inline bool operator ==(const bignum& a, const bignum& b){
    return do_comp(a, b) == 0;
}
inline bool operator !=(const bignum& a, const bignum& b){
    return do_comp(a, b) != 0;
}
inline bool operator <(const bignum& a, const bignum& b){
    return do_comp(a, b) == -1;
}
inline bool operator >(const bignum& a, const bignum& b){
    return do_comp(a, b) == 1;
}
inline bool operator <=(const bignum& a, const bignum& b){
    return do_comp(a, b) != 1;
}
inline bool operator >=(const bignum& a, const bignum& b){
    return do_comp(a, b) != -1;
}

inline bool operator ==(const bignum& a, const int& b){
    return do_comp(a, b) == 0;
}
inline bool operator !=(const bignum& a, const int& b){
    return do_comp(a, b) != 0;
}
inline bool operator <(const bignum& a, const int& b){
    return do_comp(a, b) == -1;
}
inline bool operator >(const bignum& a, const int& b){
    return do_comp(a, b) == 1;
}
inline bool operator <=(const bignum& a, const int& b){
    return do_comp(a, b) != 1;
}
inline bool operator >=(const bignum& a, const int& b){
    return do_comp(a, b) != -1;
}

inline void bignum::do_trim(){
    while (data.size()>1&&data.back()==0) data.pop_back();
}

inline bignum& bignum::operator +=(const bignum& a){
    list<int>::iterator i; list<int>::const_iterator j; int t = 0;
    for (i=data.begin(),j=a.data.begin(); i!=data.end()&&j!=a.data.end(); i++,j++){
        *i+=*j+t; t=*i/__base; *i%=__base;
    }
    while (i!=data.end()) {*i+=t; t=*i/__base; *i%=__base; i++;}
    while (j!=a.data.end()) {data.PB(t+*j); t=data.back()/__base; data.back()%=__base; j++;}
    if (t!=0) data.PB(t);
    return *this;
}

inline bignum& bignum::operator -=(const bignum& a){
    list<int>::iterator i; list<int>::const_iterator j; int t = 0;
    for (i=data.begin(),j=a.data.begin(); j!=a.data.end(); i++,j++){
        *i -= t+*j; if (*i>=0) t=0; else *i+=__base, t=1;
    }
    while (i!=data.end()) {*i-=t; if (*i>=0) t=0;else *i+=__base, t=1; i++;}
    (*this).do_trim();
    return *this;
}

inline bignum& bignum::operator +=(const int& a){
    return (*this)+=bignum(a);
}

inline bignum& bignum::operator -=(const int& a){
    return (*this)-=bignum(a);
}

inline bignum operator +(const bignum& a, const bignum& b){
    list<int>::const_iterator i, j; bignum c; int t = 0;
    for (i=a.data.begin(),j=b.data.begin(); i!=a.data.end()&&j!=b.data.end(); i++,j++){
        c.data.PB(t+*i+*j);
        t=c.data.back()/__base; c.data.back()%=__base;
    }
    while (i!=a.data.end()) {c.data.PB(t+*i); t=c.data.back()/__base; c.data.back()%=__base; i++;}
    while (j!=b.data.end()) {c.data.PB(t+*j); t=c.data.back()/__base; c.data.back()%=__base; j++;}
    if (t!=0) c.data.PB(t);
    return c;
}

inline bignum operator -(const bignum& a, const bignum& b){
    list<int>::const_iterator i, j; bignum c; int t = 0;
    for (i=a.data.begin(),j=b.data.begin(); j!=b.data.end(); i++,j++){
        t = *i - t;
        if (t>=*j) c.data.PB(t-*j), t=0;
        else c.data.PB(t+__base-*j), t=1;
    }
    while (i!=a.data.end()) {t=*i-t; if (t>=0) c.data.PB(t), t=0;else c.data.PB(t+__base), t=1; i++;}
    c.do_trim();
    return c;
}

inline bignum operator *(const bignum& a, const bignum& b){
    list<int>::const_iterator i, j; list<int>::iterator k, kk; bignum c; long long t = 0;

    for (int i=0;i<a.data.size()+b.data.size();i++) c.data.PB(0);
    for (i=a.data.begin(),k=c.data.begin(); i!=a.data.end(); i++,k++){
        for (j=b.data.begin(),kk=k; j!=b.data.end(); j++,kk++){
            t+=(long long)(*i)*(*j)+(*kk);
            *kk=int(t%__base); t/=__base;
        }
        *kk+=t; t=0;
    }
    c.do_trim();
    return c;
}

inline int bignum::do_try(const bignum& a){
    int l = 1, r = 99999999, m, t;
    while (l+2<r){
        m = (l + r) / 2;
        t = do_comp(*this, a*bignum(m));
        if (t==0) return m;
        if (t<0) r = m - 1;
        else l = m;
    }
    while (do_comp(*this, a*bignum(r))<0) r--;
    return r;
}

inline void divide(const bignum& a, const bignum& b, bignum& d, bignum& r){
    list<int>::const_reverse_iterator i = a.data.rbegin(); int t;
    d = bignum(0); r = bignum(0);
    do {
        while (r<b&&i!=a.data.rend()){d.data.push_front(0);r.data.push_front(*i);r.do_trim();i++;}
        if (r>=b){
            t = r.do_try(b); d.data.front() = t;
            r-=(b*bignum(t));
        }
    } while (i!=a.data.rend());
    d.do_trim();
}

inline bignum operator /(const bignum& a, const bignum& b){
    bignum d, r;
    divide(a, b, d, r);
    return d;
}

inline bignum operator %(const bignum& a, const bignum& b){
    bignum d, r;
    divide(a, b, d, r);
    return r;
}

inline bignum operator +(const bignum& a, const int& b){
    return a+bignum(b);
}

inline bignum operator -(const bignum& a, const int& b){
    return a-bignum(b);
}

inline bignum operator *(const bignum& a, const int& b){
    return a*bignum(b);
}

inline bignum operator /(const bignum& a, const int& b){
    return a/bignum(b);
}

inline bignum operator %(const bignum& a, const int& b){
    return a%bignum(b);
}

inline bignum& bignum::operator *=(const bignum& a){
    (*this) = (*this) * a;
    return *this;
}

inline bignum& bignum::operator /=(const bignum& a){
    (*this) = (*this) / a;
    return *this;
}

inline bignum& bignum::operator %=(const bignum& a){
    (*this) = (*this) % a;
    return *this;
}

inline bignum& bignum::operator *=(const int& a){
    return (*this)*=bignum(a);
}

inline bignum& bignum::operator /=(const int& a){
    return (*this)/=bignum(a);
}

inline bignum& bignum::operator %=(const int& a){
    return (*this)%=bignum(a);
}

inline bignum pow(bignum a,int b){
    bignum c(1);
    while (b!=0) {
        if (b&1) c *= a;
        a = a * a; b >>= 1;
    }
    return c;
}
inline bignum pow(int a, int b){
    return pow(bignum(a), b);
}

} //using namespace BIGNUM;
//}
//}

/** I/O Accelerator Interface .. **/ //{
template<class T> inline T& RD(T &x){
    //cin >> x;
    //scanf("%d", &x);
    char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    return x;
}

inline DB& RF(DB &x){
    //cin >> x;
    scanf("%lf", &x);
    /*char t; while ((t=getchar())==' '||t=='\n'); x = t - '0';
    while ((t=getchar())!=' '&&t!='\n'&&t!='.')x*=10,x+=t-'0';
    if (t=='.'){DB l=1; while ((t=getchar())!=' '&&t!='\n')l*=0.1,x += (t-'0')*l;}*/
    return x;
}

inline char* RS(char *s){
    //gets(s);
    scanf("%s", s);
    return s;
}

LL last_ans; int Case; template<class T> inline void OT(const T &x){
    //printf("Case %d: %d\n", ++Case, x);
    //printf("%.2lf\n", x);
    //printf("%d\n", x);
    cout << x << endl;
    //last_ans = x;
}
//}

//}/* .................................................................................................................................. */

const int N = int(150) + 9;

struct Int{
    int val;

    operator int() const{return val;}

    Int(int val = 0):val(val){
        val %= MOD; if (val < 0) val += MOD;
    }
    inline Int& operator +=(Int rhs){
        INC(val, rhs);
        return *this;
    }
    inline Int operator +(Int rhs) const{
        return sum(val, rhs.val);
    }
    inline Int operator -(Int rhs) const{
        return dff(val, rhs);
    }
};

Int dp[2][2][2][N][N];
int n, m;

int main(){

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    RD(n, m); Int res; int p = 0, q = 1; REP(i, n){
#define u dp[q][b1][b2][l][r]
#define v dp[p][bb1][bb2][ll][rr]
#define ll (!b1 && bb1 ? l+1 : l)
#define rr (!b2 && bb2 ? r-1 : r)
        RST(dp[p]); REP_4(b1, b2, r, l, 2, 2, m, r+1) if (u)
            FOR(bb1, b1, 2) FOR(bb2, b2, 2) if (ll <= rr) v += u;
#undef ll
#undef rr
#define d(o) (o ? 1 : -1)
#define ll (l + d(b1))
#define rr (r - d(b2))

#define upd1{\
    ls = b1 ? 0 : m-1, lt = b1 ? m : -1; \
    for (int l=ls;l!=lt;l+=d(b1)) \
        FOR(r, l, m) if (cur[l][r] && ll >= 0) cur[ll][r] += cur[l][r]; \
}

#define upd2{\
    rs = b2 ? m-1 : 0, rt = b2 ? -1 : m; \
    for (int r=rs;r!=rt;r-=d(b2)) \
        REP(l, r+1) if (cur[l][r]) cur[l][rr] += cur[l][r]; \
}

        REP_2(b1, b2, 2, 2){
            Int (*cur)[N] = dp[p][b1][b2]; int ls, lt, rs, rt;
            if (!b1 && b2){upd2; upd1;}else{upd1; upd2;}
        }

        REP_2(r, l, m, r+1) dp[p][0][0][l][r] += 1;
        swap(p, q); REP_4(b1, b2, r, l, 2, 2, m, r+1) if(u) res += u;
    }

    OT(res);
}


#include <bits/stdc++.h>

using namespace std;
int x, y;

int main()
{
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    int n, m, bx, by;
        long long X = 0, Y = 0;
    cin >> n >> bx;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        X += x * pow(bx, n - i - 1);

    }
    cin >> m >> by;
    for (int i = 0; i < m; i++)
    {
        cin >> y;
        Y += y * pow(by, m - i - 1);

    }
 //   cout << X << " " << Y << "\n";
    if (X > Y) cout << ">";
    if (X == Y) cout << "=";
    if (X < Y) cout << "<";
    return 0;
}


#include <bits/stdc++.h>
#define FAST_READ ios_base::sync_with_stdio(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define MAXN 100000010

using namespace std;

vector<ll>ans;
ll used[MAXN];

ll n;

int main(){
FAST_READ;
cin>>n;
used[1]=2;
used[2]=3;
ll cnt=4;
for(ll i=3;i<=n;i+=2)
{
  used[i]=cnt;
  used[i+1]=cnt;
  cnt++;
}
cout<<used[n]<<endl;
return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool vis[10000001];
int main()
{
   ll n;
   cin>>n;
   ll cnt=0;
    for(ll j=0;j<=n;j+=3)
        vis[j]=1;
         for(ll j=0;j<=n;j+=5)
         vis[j]=1;
          for(ll j=0;j<=n;j+=7)
         vis[j]=1;
          for(ll j=0;j<=n;j+=9)
         vis[j]=1;
   for(ll i=1;i<=n;i+=2)
    if(!vis[i])
    cnt++;
   cout<<cnt;
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
main()
{
    char s[105];
    static int x,y,i,j=0,a,b,c,d,k=0;
    gets(s);
    for(i=0;i<strlen(s);i++)
    {
        if(s[i]=='n')
        a++;
        else if(s[i]=='e')
            b++;
        else if(s[i]=='t')
            c++;
        else if(s[i]=='i')
            d++;
    }
    for(i=1;;i++)
    {
        if(i>1&&((a==2*i+1||a==2*i+2)&&b>=3*i&&c>=i&&d>=i))
        {
            i++;break;
        }
        if(i<2&&(a<3*i||b<3*i||c<i||d<i))
            break;
    }
    cout<<i-1;
}


#include<conio.h>
#include<locale>
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	int count = 0;
	while ((a + b <= c)&&(a+c>b)&&(b+c>a))
	{
		a++;
		count++;
	}
	while ((a + b > c) && (a + c <= b) && (b + c > a))
	{
		c++;
		count++;
	}
	while ((a + b > c) && (a + c > b) && (b + c <= a))
	{
		b++;
		count++;
	}
	cout << count;
	_getch();
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<map>
using namespace std;
map<int,int>mp;
int a, b, c, d, e, f, atk, hp, def,ans=10000000000;
void dfs(int ca,int cb,int cc,int cur)
{
	if (e<=cc&&cb>e||(ca-1) / (e - cc) > (d-1)/ (cb - f))
	{
		int y = (ca - a)*hp + (cb - b)*atk + (cc - c)*def;
		if (y < ans)
			ans = y;
		return;
	}
	if (cur == 1)
	{
		dfs(ca + 1, cb, cc, cur);
		dfs(ca, cb, cc + 1, cur + 1);
	}
	else if (cur == 2)
	{   dfs(ca, cb, cc + 1,cur);
	 if (e <= cc || (ca - 1) / (e - cc) != 0)
		dfs(ca, cb + 1, cc, cur + 1);
	}
	else
		dfs(ca, cb + 1, cc, cur);

}
int main()
{
	cin >> a >> b >> c >> d >> e >> f >> hp >> atk >> def;

	int add = 0;
	if (b<f)
	{
		add = atk*(f + 1 - b);
		b = f + 1;
	}dfs(a, b, c, 1);
	cout << ans+add;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],b[N];
char c[N];
int main()
{
    int n,maxx=0,l,r;
    scanf("%d",&n);
    scanf("%s",c);
    for(int i=0;i<n;i++)
    {
        if(c[i]=='(') a[i+1]=1;
        else a[i+1]=2;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            swap(a[i],a[j]);
            stack<int> b;
            int cnt=0;
            for(int t=0;t<n;t++)
            {
                bool flag=true;
                stack<int> b;
                for(int k=n-t+1;k<=n;k++)
                {
                    if(!b.empty())
                    {
                        if(a[k]==1) b.push(1);
                        if(a[k]==2)
                        {
                            if(b.top()==1) b.pop();
                            if(b.top()==2)
                            {
                                flag=false;
                                break;
                            }
                        }
                    }
                    if(b.empty())
                    {
                        if(a[k]==1) b.push(1);
                        if(a[k]==2)
                        {
                            flag=false;
                            break;
                        }
                    }
                }
                for(int k=1;k<=n-t;k++)
                {
                    if(!b.empty())
                    {
                        if(a[k]==1) b.push(1);
                        if(a[k]==2)
                        {
                            if(b.top()==1) b.pop();
                            if(b.top()==2)
                            {
                                flag=false;
                                break;
                            }
                        }
                    }
                    if(b.empty())
                    {
                        if(a[k]==1) b.push(1);
                        if(a[k]==2)
                        {
                            flag=false;
                            break;
                        }
                    }
                }
                if(!b.empty()) flag=false;
                if(flag) cnt++;
            }
            if(cnt>maxx)
            {
                maxx=cnt;
                l=i;r=j;
            }
            swap(a[i],a[j]);
        }
    }
    printf("%d\n%d %d\n",maxx,l,r);
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cassert>

using namespace std;

typedef unsigned int index;

index dfs(index v, const vector<vector<index>> &g, const vector<index> &num, size_t &sz)
{
	if (g[v].size() == 0)
		sz++;
	index mrg = (index)-1;
	for (auto i: g[v])
	{
		mrg = min(mrg, dfs(i, g, num, sz));
	}
	for (index i = 0; i < 4; i++)
		if (num[i] == v)
			return i;
	return mrg;
}

int main()
{
#ifdef ONPC
	assert(freopen("a.in", "r", stdin));
	assert(freopen("a.out", "w", stdout));
#else
#endif
	size_t n;
	cin >> n;
	vector<index> num(4);
	for (index i = 0; i < 4; i++)
		cin >> num[i], num[i]--;
	vector<size_t> size;
	vector<size_t> ns(4);
	vector<vector<index>> g(n);
	for (index i = 1; i < n; i++)
	{
		index p;
		cin >> p;
		p--;
		g[p].push_back(i);
	}
	size_t sum = 0;
	for (auto i: g[0])
	{
		size_t sv = 0;
		index j = dfs(i, g, num, sv);
		if (j == (index)-1)
		{
			size.push_back(sv);
			sum += sv;
		}
		else
		{
			ns[j] = sv;
		}
	}
	vector<bool> d(sum + 1);
	d[0] = true;
	for (auto sz: size)
	{
		for (index i = 0; i < sum; i++)
		{
			if (!d[i])
				continue;
			d[i + sz] = true;
		}
	}
	size_t ssum = ns[0] + ns[1] + ns[2] + ns[3] + sum;
	if (ssum % 2 == 1)
	{
		cout << "NO" << endl;
		return 0;
	}
	ssum /= 2;
	for (index i = 0; i < sum + 1; i++)
	{
		if (!d[i])
			continue;
		size_t ls = (size_t)i, rs = sum - i;
		if (ls + ns[2] < ssum and rs + ns[3] < ssum and ns[0] < ssum and ns[1] < ssum)
		{
			cout << "YES" << endl;
			return 0;
		}
	}
	cout << "NO" << endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
const int N = 1e6 + 10;
inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
void slove() {
    int s[3][3];
    s[1][1] = read();
    s[1][2] = read();
    s[2][1] = read();
    s[2][2] = read();
    int cnt = 0;
    if (s[1][1] == 1) cnt++;
    if (s[1][2] == 1) cnt++;
    if (s[2][1] == 1) cnt++;
    if (s[2][2] == 1) cnt++;
    if (cnt == 4) {
        cout << 2 << endl;
        return;
    }
    if (cnt == 0) {
        cout << 0 << endl;
        return;
    }
    cout << 1 << endl;
}
void init() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
int main () {
    init();
    int t;
    cin >> t;
    while (t--) slove();
    return 0;
}


#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Ford(i,a,b) for(int i=a;i>=b;i--)
#define dream_hyojooslh

using namespace std;
const int N=100000+1067;
int f[N];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // freopen("dream_hyojooslh.inp","r",stdin);
    long long s,x1,x2,t1,t2,p,d;
    cin>>s>>x1>>x2;
    cin>>t1>>t2;
    cin>>p>>d;
    if (t1>t2) {
        cout<<abs(x2-x1)*t2<<endl;
        return 0;
    }
    long long tt=t2-t1;
    long long res=abs(x2-x1)*t2;
    if (x1>x2) {
        if (d==-1) {
            if (p<x1) p=s+s+p;
        } else
        {
            p=s-p+s;
        }
        long long dmoi=(p-x1)*t1;
        dmoi/=tt;
        if (x1-dmoi>x2) {
            res=min(res,(p-x2)*t1);
        }
    } else
    {
        if (d==1) {
            if (p>x1) p=0-(s-p)-s;
        } else
        {
            p=-p;
        }
        long long dmoi=(x1-p)*t1;
        dmoi/=tt;
        if (x1+dmoi<x2) {
            res=min(res,(x2-p)*t1);
        }
    }
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct SEG{
	int l,r;
}seg[100+10];
struct POINT{
	int x,no,c;
}p[100+10];

int n,m;
int cmp1(POINT a,POINT b)
{
	return a.x<b.x;
}
int cmp2(POINT a,POINT b)
{
	return a.no<b.no;
}
int main(void)
{
	cin>>n>>m;

	for(int i=1;i<=n;i++)
	{
		cin>>p[i].x;
		p[i].no=i;
	}
	for(int i=1;i<=m;i++)
		cin>>seg[i].l>>seg[i].r;
	sort(p+1,p+1+n,cmp1);
	for(int i=1;i<=n;i++)
	{
		if(i%2) p[i].c=1;
		else p[i].c=0;
	}
	sort(p+1,p+1+n,cmp2);
	int ok=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(p[i].x<=seg[j].r && p[i].x>=seg[j].l)
			{
				ok=1;break;
			}
		}
	}
	if(!ok) {cout<<"-1"<<endl; return 0;}
	for(int i=1;i<=n;i++)
	{
		cout<<p[i].c<<" ";
	}
	return 0;
}


//#pragma GCC optimize("Ofast")

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

#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp make_pair

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

typedef pair<int, int> pii;
typedef pair<ll, int> pli;

typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<int> vi;

typedef map<string, int> msi;
typedef map<int, int> mii;
typedef map<int, ll> mill;
typedef map<int, vi> mivi;

const int MAXN = 19;
const int B = 16;
const string S = "0123456789abcdef";

map<vi, ll> memo[MAXN];
int dig[MAXN];

int m, n;
ll k, ans;
ll lo = 1, hi = 3e10;
int cnt = 0;

void fast_io() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

bool blank(vi &a) {
	for(int i = 0; i < B; i++)
		if(a[i]) return false;
	return true;
}

ll solve(int pos, vi a, bool t) {
	if(!pos) {
		if(blank(a)) return 0;
		bool ok = 1, nz = 0;
		for(int i = 0; i < B; i++) {
			ok &= (a[i] <= m);
		}
		return ok;
	}

	if(!t and memo[pos].count(a))
		return memo[pos][a];

//	printf("state count = %d\n", ++cnt);

	ll res = 0;
	int lim = t ? dig[pos] : B - 1;
	vi b = a;

	for(int i = 0; i <= lim; i++) {
		if(!i and blank(b)) res += solve(pos - 1, b, !(i ^ lim) & t);
		else if(b[i] + 1 <= m) {
			++b[i];
			res += solve(pos - 1, b, !(i ^ lim) & t);
			--b[i];
		}
	}

	++cnt;

	if(!t) memo[pos][a] = res;
	return res;
}

ll count(ll x) {
	n = 0;
	while(x) {
		dig[++n] = (x % B);
		x /= B;
	}
	vi s(B, 0);
	return solve(n, s, 1);
}

int main() {
	fast_io();
//	freopen("input/747F.txt", "r", stdin);

	cin >> k >> m;
	while(lo <= hi) {
		ll mid = (lo + hi) >> 1;
		if(count(mid) >= k) ans = mid, hi = mid - 1;
		else lo = mid + 1;
	}

	vector<char> c;
	while(ans) {
		c.pb(S[ans % B]);
		ans /= B;
	}
	reverse(all(c));

	for(auto i : c) {
		cout << i;
	}
	cout << '\n';

	return 0;
}


#include <cstdio>
#include <cstring>

using namespace std;

bool checkWin(int a1, int b1, int a2, int b2){
	if(a1> a2 && b1>b2)return true;
	return false;
}

int main(){
	while(true){
	int a[4], b[4];
	for(int i = 0;i<4;i++){
		scanf("%d%d", &a[i], &b[i]);
	}
	if(checkWin(a[0], b[1], a[2], b[3]) && checkWin(a[0], b[1], a[3], b[2])
	|| checkWin(a[1], b[0], a[2], b[3]) && checkWin(a[1], b[0], a[3], b[2]))
	{
		puts("Team 1");
	}else if((checkWin(a[2], b[3], a[0], b[1])||checkWin(a[3], b[2], a[0], b[1])) &&
	(checkWin(a[2], b[3], a[1], b[0])||checkWin(a[3], b[2], a[1], b[0])))
	{
		puts("Team 2");
	}else {
		puts("Draw");
	}
}
	return 0;
}


//IN HIS NAME
#include <bits/stdc++.h>

using namespace std;

int a[1000],b[1000];

int main(){
	ios::sync_with_stdio(false);
	int n,m,c=0,k=0;
	cin>>n>>m;
	for(int i=0;i<=31;i++){
		for(int j=0;j<=31;j++){
			a[k]=(i*i)+j;
			b[k]=(j*j)+i;
			if((a[k]==n)&&(b[k]==m))
				c++;
			//if(a[k]>100+n&&b[k]>m+100){
				//	cout<<c;
				//	return 0;

			//}
			k++;
		}
	}
	cout<<c;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lo;
typedef pair< lo,lo > 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 lo inf = 100000000000000;
const lo mod = 1000000007;
const lo KOK = 100000;
const lo li = 100005;
const lo LOG = 20;

lo n,m,cev,x,mn=inf;
unordered_map<lo,lo> prime;
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(lo i=p*p;i<=sayi;i+=p){
			prime[i]=1;
		}
	}
	if(sayi%2==0)
		v.pb(mp(2,0));
	for(lo i=3;i*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";
		if(xx>0)
			mn=min(mn,cev/xx);
	}
	//~ printf("**\n");
	if(m>1){

		lo xx=1;

		//~ v[i].se=xx;
		cev=0;
		lo yes=n;
		while(yes){

			yes/=m;
			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<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<cstring>
#define N_ 301000
#define pii pair<int,int>
using namespace std;
int n, m, Q, P[201000], chk[301000], UF[201000], NN[201000];
vector<int>G[201000];
struct Edge{
    int a, b;
}Ed[301000];
struct Query{
    int ck, a;
}U[501000];
int Find(int a){
    if(a==UF[a])return a;
    return UF[a]=Find(UF[a]);
}
int Where[201000], cnt;
int O[5100000], CCC[510000], cur;
multiset<int>Z[201000];
void Merge(int a, int b, int c){
    a = Find(a),b=Find(b);
    if(a==b)return;
    if(a!=b){
        if(G[a].size()<G[b].size())swap(a,b);
        for(auto &t : G[b]){

            G[a].push_back(t);
            if(c<=Q){
                O[++cur] = t;
                CCC[c]++;
            }
        }
        UF[b]=a;
    }
}
int main(){
    int i;
    scanf("%d%d%d",&n,&m,&Q);
    for(i=1;i<=n;i++){
        scanf("%d",&P[i]);
        G[i].push_back(i);
    }
    for(i=1;i<=m;i++){
        scanf("%d%d",&Ed[i].a,&Ed[i].b);
    }
    for(i=1;i<=Q;i++){
        scanf("%d%d",&U[i].ck,&U[i].a);
        if(U[i].ck==2)chk[U[i].a]=i;
    }
    for(i=1;i<=n;i++)UF[i]=i;
    for(i=1;i<=m;i++){
        if(!chk[i]){
            Merge(Ed[i].a,Ed[i].b, Q+1);
        }
    }
    for(i=Q;i>=1;i--){
        if(U[i].ck==2){
            int t = U[i].a;
            Merge(Ed[t].a,Ed[t].b,i);
        }
    }
    int cc=0;
    for(i=1;i<=n;i++)if(Find(i)==i)NN[i]=++cc;
    for(i=1;i<=n;i++){
        int x = NN[Find(i)];
        Z[x].insert(P[i]);
        Where[i] = x;
    }
    cnt = cc;
    for(i=1;i<=Q;i++){
        if(U[i].ck==2){
            if(!CCC[i])continue;
            cnt++;
            for(int j=0;j<CCC[i];j++){
                int t = O[cur];
                Z[Where[t]].erase(Z[Where[t]].find(P[t]));
                Where[t] = cnt;
                Z[cnt].insert(P[t]);
                cur--;
            }
        }
        else{
            int a = U[i].a;
            auto it = Z[Where[a]].end();it--;
            printf("%d\n",*it);
            Z[Where[a]].erase(it);
            Z[Where[a]].insert(0);
        }
    }
}


//#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
//#include<windows.h>
using namespace std;//-48
ifstream fin("input.txt");
ofstream fout("output.txt");

int main(){
	int n,k,x;
	vector<int> chores;
	int i;
	int c;
	int sum=0;

	fin>>n>>k>>x;
	for(i=0;i<n;i++){
		fin>>c;
		chores.push_back(c);
	}
//	sort(chores.begin(),chores.end());
	for(i=n-1;i>=0;i--){
		if(k>0){
			sum+=x;
			k--;
		}
		else
			sum+=chores[i];
	}

	fout<<sum;
	return 0;
}


#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    ifstream fin("input.txt");
    ofstream fout("output.txt");
    int a[4];
    int b[4];
    int i,sum=0;
    for(i=0;i<3;i++)
        fin>>a[i];

    for(i=0;i<3;i++)
        fin>>b[i];

    for(i=0;i<3;i++)
        if(a[i]==b[i])
            sum++;
    if(sum>=2)
        fout<<"YES"<<endl;
    else fout<<"NO"<<endl;
    return 0;
}


#include <cstdint>
#include <iostream>
#include <vector>

bool impossible = false;

int64_t choose(int64_t qp, int k) {
    int64_t l0 = -qp;
    int64_t r0 = -qp + k - 1; // -1 for strict inequality

    //std::cout << "l0 = " << l0 << ", r0 = " << r0 << std::endl;

    if (l0 <= 0 && r0 >= 0) {
        // 0 is possible
        return 0;
    } else {
        int64_t l = -1e17;
        int64_t r = +1e17;
        while (l < r) {
            int64_t m = (l + r) / 2;
            if (m * k > r0) {
                r = m - 1;
            } else if (m * k < l0) {
                l = m + 1;
            } else {
                return m;
            }
        }
        //std::cout << "Found l = " << l << std::endl;
        if (l * k < l0 || l * k > r0) {
            impossible = true;
            return 123; /* whatever */
        }
        return l;
    }
}

int main() {
    int64_t p, k;
    std::cin >> p >> k;
    std::vector<int64_t> q;

    int64_t q0 = choose(p, k);
    //std::cout << "q[0] = " << q0 << std::endl;
    q.push_back(q0);

    int64_t qq = q0;
    while (qq != 0 && !impossible) {
        qq = choose(qq, k);
        q.push_back(qq);
        //std::cout << "q[i] = " << qq << std::endl;
    }
    while (!impossible && *(q.end() - 1) == 0) {
        q.pop_back();
    }
    if (impossible) {
        std::cout << -1 << std::endl;
        return 0;
    }
    std::vector<int> f;
    f.push_back(q[0] * k + p);
    for (int i = 1; i < q.size(); i++) {
        f.push_back(q[i-1] + q[i] * k);
    }
    f.push_back(q[q.size() - 1]);
    std::cout << f.size() << std::endl;
    for (int i = 0; i < f.size(); i++) {
        std::cout << f[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}


#include<stdio.h>
#include<vector>
#include<set>
#include<string>
#include<queue>
#include<algorithm>
#include<math.h>
#include<iterator>
#include<map>
using namespace std;
#define INF 1e+9
#define EPS 1e-6
#define m_p make_pair
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
typedef vector<pi> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef si::iterator sii;
typedef pair<int,bool> pib;
typedef vector<pib> vpib;
typedef queue<int> qi;

int main()
{
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	int n;
	scanf("%d",&n);
	vi v(n),ans(n);
	for(int i=0;i<n;i++) scanf("%d",&v[i]);
	sort(v.begin(),v.end());
	for(int i=0;i<=n/2;i++)
	{
		int a=1;
		if(a>0) {ans[n-i-1]=v[i];ans[i]=v[n-i-1];}
		else {ans[i]=v[i];ans[n-i-1]=v[n-i-1];}
		a*=-1;
	}
	for(int i=0;i<n;i++) printf("%d ",ans[i]);
	return 0;
}


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

typedef long long ll;
#define MOD 1000000007

int main(void)
{
    std::ios_base::sync_with_stdio(false);

    int a, b, c, n, ans;
    ans = 0;
    cin >> n;

    for(c=1; c<=n; ++c)
        for(b=1; b<=c; ++b)
            for(a=1; a<=b; ++a)
                if(c*c == a*a + b*b)
                    ++ans;

    cout << ans;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#define pi 3.1415926535898
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
#define eps 1e-6
#define ms(a,b) memset(a,b,sizeof(a))
#define legal(a,b) a&b
#define print1 printf("111\n")
#define endl "\n"
#define pl printf("\n")
#define pb(x) push_back(x)
#define pair4 pair<pair<int,int>,pair<int,int> >
#define fi first
#define se second
using namespace std;
const int maxn = 1e6+10;
const int inf = 1e9+10;
const ll llinf =1e18+10;
const ll mod = 1e9+7;

int n,k;
ll dp[maxn][10];

ll binaryPow(ll a, ll b, ll m){
	if(b == 0)
		return 1;
	else if(b & 1)
		return a * binaryPow(a, b - 1, m) % m;
	else{
		ll num = binaryPow(a, b/2, m) % m;	//优化
		return num * num % m;// 不直接写成return binaryPow(a, b/2, m) * binaryPow(a, b/2, m)
	}
}

int main()
{
    int _;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d%d",&n,&k);
        ms(dp,0);
        dp[0][0]=1;
        ll ans=0;
        if(n%2==1)
        {
            for(int i=1;i<=k;i++)
            {
                dp[i][1]=(dp[i-1][0]%mod+dp[i-1][1]%mod)%mod;
                dp[i][0]=((dp[i-1][0]%mod+dp[i-1][1]%mod)%mod*binaryPow(2,n-1,mod))%mod;
            }
            ans=(dp[k][1]+dp[k][0])%mod;
        }else
        {
            for(int i=1;i<=k;i++)
            {
                ans=(ans%mod+dp[i-1][0]%mod*binaryPow(2,n*(k-i)%mod,mod)%mod)%mod;
                dp[i][0]=dp[i-1][0]%mod*(binaryPow(2,n-1,mod)-1)%mod;
            }
            ans=(ans%mod+dp[k][0]%mod)%mod;
        }
        printf("%lld\n",ans);
    }
}


// Written by newbiechd
#include <cstdio>

const int maxN = 1003;
double dp[maxN][maxN];

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
#endif
  int n, m, lim = n + m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= lim; ++i)
    dp[i][0] = 1;
  for (int j = 1; j <= lim; ++j)
    dp[0][j] = (double)1 / (j + 1);
  for (int i = 1; i <= lim; ++i)
    for (int j = 1; j < i; ++j)
      dp[i - j][j] = (dp[j][i - j - 1] * (1 - dp[j - 1][i - j]) * j + 1)
        / (dp[j][i - j - 1] * (j + 1) + 1);
  printf("%.10lf %.10lf\n", dp[n][m], 1 - dp[n][m]);
  return 0;
}


#include<cstdio>
#include<queue>
using namespace std;
typedef struct{int sum,ans,vis;}node;
int main(){
   //freopen("in.txt","r",stdin);
   int k,a[12];
   queue<node> q;
   while(scanf("%d",&k)==1){
      int sum=0,flag;
      node cur,next;
      for(int i=0;i<12;++i){
         scanf("%d",&a[i]);
         sum+=a[i];
      }
      if(sum<k){printf("-1\n"); continue;}
      while(!q.empty()) q.pop();
      cur.sum=cur.ans=cur.vis=flag=0;
      q.push(cur);
      while(!q.empty()){
         cur=q.front(); q.pop();
         if(cur.sum>=k){flag=1;printf("%d\n",cur.ans);break;}
         for(int i=0;i<12;++i)if(!(cur.vis&(1<<i))){
            next.ans=cur.ans+1;
            if(next.ans>12) continue;
            next.sum=cur.sum+a[i];
            next.vis=cur.vis|(1<<i);
            q.push(next);
         }
      }
      if(!flag) printf("-1\n");
   }
   return 0;
}


#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<functional>
#include<iostream>
#include<map>
#include<bitset>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define MP(a,b) make_pair(a,b)
#define clr(x,y) memset(x,y,sizeof x)
#define fi first
#define se second
#define LL long long
#define sqr(z) ((z)*(z))
using namespace std;
typedef pair<int,int> PII;
const int oo=1047483647,maxn=110;
const double eps=1e-6;
int n,i,j,k,m,q,fa[maxn];
double F[maxn][maxn],H[maxn];
struct node
{
	int x,y,c;
}a[maxn*10];
struct edge
{
	int next[2*maxn],y[2*maxn],g[maxn],tt,c[2*maxn];
	void add(int i,int j,int k)
	{
		next[++tt]=g[i];
		g[i]=tt;
		y[tt]=j;
		c[tt]=k;
	}
}E;
int Read()
{
	char ch;while (ch = getchar(), (ch < '0' || ch > '9') && (ch != '-'));
	bool neg= (ch == '-');
	if (ch=='-') ch=getchar();
	int v=0;
	while (ch>='0' && ch<='9') v=v*10+ch-'0',ch=getchar();
	if (neg) v=-v;
	return v;
}
void work()
{
	int i,j,k;
	fo(i,2,n-1)//��һ������Ϊ��
	{
		double Ma=0;
		int Rec;
		fo(j,i,n)
		if (fabs(F[j][i])>Ma)
		{
			Ma=fabs(F[j][i]);
			Rec=j;
		}
		if (Ma<eps) continue;
		if (Rec!=i)
		fo(j,0,n) swap(F[i][j],F[Rec][j]);
		fo(j,1,n)
		if (j!=i)
		if (F[j][i])
		{
			double t1=F[j][i]/F[i][i];
			fo(k,0,n)
			F[j][k]=F[j][k]-t1*F[i][k];
		}
	}
}
int get(int x)
{
	if (!fa[x]) return x;
	else return fa[x]=get(fa[x]);
}
int main()//����ͨ�޽�
{
//	freopen("1.in","r",stdin);
 // freopen("1.out","w",stdout);
	n=Read();
	m=Read();
	fo(i,1,m)
	{
		a[i].x=Read();
		a[i].y=Read();
		a[i].c=Read();
		if (a[i].x!=1 && a[i].x!=n)
		{
			F[a[i].x][a[i].x]++;F[a[i].x][a[i].y]--;
		}
		if (a[i].y!=1 && a[i].y!=n)
		{
			F[a[i].y][a[i].y]++;F[a[i].y][a[i].x]--;
		}
		int x=get(a[i].x),y=get(a[i].y);
		if (x!=y) //!!
		fa[x]=y;
	}
	if (get(1)!=get(n))
	{
		fo(i,1,m+1)
		printf("0\n");
		return 0;
	}
	work();
	H[1]=0;H[n]=1;
	fo(i,2,n-1)
	if (F[i][i]>eps)
	H[i]=(-F[i][n]*H[n]-H[1]*F[i][1])/F[i][i];//��
	double Ma=oo;
	fo(i,1,m)
	{
		double t=fabs(H[a[i].x]-H[a[i].y]);
		if (t>eps)
		Ma=min(Ma,a[i].c/t);
	}
	fo(i,1,n)
	H[i]*=Ma;
	double ans=0;//n�ĸ߶Ȳ��Ǵ�
	fo(i,1,m)
	if (a[i].x==1 || a[i].y==1) ans+=fabs(H[a[i].x]-H[a[i].y]);
	printf("%.5f\n",ans);//ÿ��������һ��ֻ�������߸߶Ȳ�
	fo(i,1,m)
	{
		printf("%.5f\n",H[a[i].y]-H[a[i].x]);
	}
	return 0;
}


/* ****revpro**** */
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define endl	   '\n'
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define hell          998244353
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define mii	    map<ll,ll>
using namespace std;

#define TIME        cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define N  100005
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<double, null_type,less<double>, rb_tree_tag,tree_order_statistics_node_update>
ll add(ll x,ll y,ll mod=hell) { x += y; if(x >= mod) x -= mod; return x;}
ll sub(ll x,ll y,ll mod=hell) { x -= y; if(x < 0) x += mod; return x;}
ll mul(ll x,ll y,ll mod=hell) { return (x * 1ll * y) % mod;}
ll fac[N];
ll expo(ll base, ll exponent, ll mod=hell) {								//return base^exponent modulo modulus
    ll ans = 1;
    while(exponent !=0 ) {
        if((exponent&1) == 1) {
            ans = ans*base ;
            ans = ans%mod;
        }
        base = base*base;
        base %= mod;
        exponent>>= 1;
    }
    return ans%mod;
}
void solve()
{
	fac[0]=1;
	rep(i,1,N) fac[i]=mul(fac[i-1],i);
	ll n,m;
	cin>>n>>m;
	ll ans=0;
	rep(i,n-1,m+1)
	{
		ans=add(ans,mul(fac[i-1],mul(expo(fac[n-2],hell-2),expo(fac[i-n+1],hell-2))));
	}
	// cout<<ans<<endl;
	cout<<mul(ans,mul(n-2,expo(2,n-3)));
}
signed main()
{

		/***********************************/
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    mt19937_64 generator (seed);
    /***********************************/
 #ifndef ONLINE_JUDGE
      // for getting input from input.txt
     freopen("input.txt", "r", stdin);
//     // for writing output to output.txt
//     freopen("output1.txt", "w", stdout);
 #endif
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		solve();

	}
	TIME
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<map>
using namespace std;
typedef long long int ll;
#define pp pair<ll,ll>;

typedef long double ld;
int x[8]={-1,-1,0,1,1,1,0,-1};
int y[8]={0,1,1,1,0,-1,-1,-1};

void bfs(int x,int y)
{

}
int main()
{
    ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin>>n;
ll x,y;
cin>>x>>y;
ll i=1;ll j=1;
ll p,q;
p=q=n;
if(x==1||y==1)
{
    cout<<"White";return 0;
}
if(x==n||y==n)
{
    cout<<"Black";return 0;
}
ll t1,t2;t1=0;t2=1;
while(i!=x||j!=y)
{

    i++;j++;t1++;
}

    t1+=abs(x-i)+abs(y-j);
while(p!=x||q!=y)
{

    p--;q--;t2++;
}
t2+=abs(p-x)+(abs(q-y));
if(t2<=t1)
    cout<<"Black";
else cout<<"White";

    return 0;
}


#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <conio.h>

using namespace std;

int main()
{
    double a, b, m;
    double vx, vy, vz;
    double x, y, z;
    double xx = 0, yy = 0, zz = 0;
    double mn;

    cin>>a>>b>>m;
    cin>>vx>>vy>>vz;

    x = a/2;
    y = m;
    z = 0;

    while( y > 0 ) {

           if( vx == 0 )
               xx = 0;
           else {
                xx = ( vx > 0 ) ? ( a - x ) : x;
                xx /= vx;
                xx = abs(xx);
                }

           yy = y * 1.0 / vy; //yy = (double)y / vy;
           yy = abs(yy);

           if( vz == 0 )
               zz = 0;
           else {
                zz = ( vz > 0 ) ? ( b - z ) : z;
                zz /= vz;
                zz = abs(zz);
                }

           mn = min( xx, yy ); mn = min( mn, zz );

           //x += vx * mn;
           //y += vy * mn;
           //z += vz * mn;

           //cout<<xx<<"-"<<x<<" : "<<yy<<"-"<<y<<" : "<<zz<<"-"<<z<<endl;
           //cout<<x<<" : "<<y<<" : "<<z<<endl;

           if( xx == mn ) {
               if( vx > 0 )
                   x = a;
               else
                   x = 0;

               y += vy * mn;
               z += vz * mn;
               }

           if( yy == mn ) {
               x += vx * mn;
               y = 0;
               z += vz * mn;
               }

           if( zz == mn ) {
               if( vz > 0 )
                   z = b;
               else
                   z = 0;

               x += vx * mn;
               y += vy * mn;
               }

           vx = ( x == 0 || x == a ) ? -vx : vx;

           vz = ( z == 0 || z == b ) ? -vz : vz;

           }

    cout<<x<<" "<<z<<endl;
    //printf("%.6f %.6f\n", x, z);

    system("PAUSE");
    return EXIT_SUCCESS;
}