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

// #define multitest 1
#ifdef WIN32
#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__);
#define pc(...) PC(#__VA_ARGS__, __VA_ARGS__);
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p)
{
	out << '[' << p.first << ", " << p.second << ']';
	return out;
}
template <typename Arg>
void PC(const char *name, Arg &&arg)
{
	while (*name == ',' || *name == ' ')
		name++;
	std::cerr << name << " { ";
	for (const auto &v : arg)
		cerr << v << ' ';
	cerr << " }\n";
}
template <typename Arg1, typename... Args>
void PC(const char *names, Arg1 &&arg1, Args &&... args)
{
	while (*names == ',' || *names == ' ')
		names++;
	const char *comma = strchr(names, ',');
	std::cerr.write(names, comma - names) << " { ";
	for (const auto &v : arg1)
		cerr << v << ' ';
	cerr << " }\n";
	PC(comma, args...);
}
template <typename Arg1>
void ZZ(const char *name, Arg1 &&arg1)
{
	std::cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char *names, Arg1 &&arg1, Args &&... args)
{
	const char *comma = strchr(names + 1, ',');
	std::cerr.write(names, comma - names) << " = " << arg1;
	ZZ(comma, args...);
}
#else
#define db(...)
#define pc(...)
#endif

using ll = long long;
#define f first
#define s second
#define pb push_back
auto TimeStart = chrono::steady_clock::now();

const int nax = 2e5 + 10, mod = 1000000007;

void solve(int caseNo)
{
	vector<pair<int, int>> pos;
	int a, b;
	string s;
	cin >> a >> b >> s;
	pos.pb({0, 0});
	for (auto c : s)
	{
		auto b = pos.back();
		switch (c)
		{
		case 'U':
			b.s++;
			pos.pb(b);
			break;
		case 'D':
			b.s--;
			pos.pb(b);
			break;
		case 'L':
			b.f--;
			pos.pb(b);
			break;
		case 'R':
			b.f++;
			pos.pb(b);
			break;
		default:
			break;
		}
	}
	auto back = pos.back();
	int x = back.f, y = back.s;
	db(x, y);
	pos.pop_back();
	for (auto elem : pos)
	{
		int a1 = a - elem.f;
		int b1 = b - elem.s;
		db(a1, b1);
		if (x == 0)
		{
			if (y == 0)
			{
				if (a1 == 0 && b1 == 0)
				{
					cout << "Yes\n";
					return;
				}
			}
			else
			{
				if (a1 == 0 && b1 % y == 0)
				{
					if (b1 / y >= 0)
					{
						cout << "Yes\n";
						return;
					}
				}
			}
		}
		else if (y == 0)
		{
			if (a1 % x == 0 && b1 == 0)
			{
				if (a1 / x >= 0)
				{
					cout << "Yes\n";
					return;
				}
			}
		}
		else
		{
			if (a1 % x == 0 && b1 % y == 0 && ((a1 / x) == (b1 / y)))
			{
				if (a1 / x >= 0)
				{
					cout << "Yes\n";
					return;
				}
			}
		}
	}
	cout << "No\n";
}

int main()
{
#ifndef WIN32
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#endif
	int t = 1;
#ifdef multitest
	cin >> t;
#endif
	for (int i = 0; i < t; ++i)
		solve(i);
#ifdef TIME
	cerr << "\n\nTime elapsed: " << chrono::duration<double>(chrono::steady_clock::now() - TimeStart).count() << " seconds.\n";
#endif
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define r_0 return 0
#define fr first
#define se second
#define TIME ios_base::sync_with_stdio(0);
using namespace std;
const ll N=1e5 ;
ll  n  , b , c , d , ans , m , cnt , x  , used[N];
vector< string > v , v1 ;
string s ,s1  ;
set<char>st;
map<string , ll > mp ;
char q ;
int main()
{
    cin >> s ;
    cin >> x ;
    for(  ll i =1 ;i <= x; i ++ )
    {
        cin >> s1 ;
        v1.pb(s1) ;
        if( s1[1] == s[0] )
            ans ++ ;
        if( s == s1 )
            cnt ++ ;
    }
    if( cnt >= 1  ){
        cout << "YES" ;r_0 ; }
    if( ans >= 1 )
        for( ll i = 0 ;i < v1.size() ; i ++ )
        if( v1[i][0] == s[1])
        {
            cout << "YES" ; r_0 ;
        }
    cout << "NO"  ;
}


/*
 * 429C. Guess the Tree.cpp
 *
 *  Created on: Jul 20, 2015
 *      Author: Osama Hatem
 */

#include <bits/stdtr1c++.h>
#include <ext/numeric>

using namespace std;

int N, c[25];
map<int, bool> dp;

bool solve(int activeMask) {
    int idx = N;
    for (int i = N - 1; !(activeMask & (1 << i)) && i >= 0; i--)
        idx = i;
    if (idx == N)
        return activeMask == 1 << (N - 1);
    if (dp.count(activeMask))
        return dp[activeMask];
    bool ret = false;
    vector<int> active;
    for (int i = 0; i < N; i++)
        if ((1 << i) & activeMask)
            active.push_back(i);
    int sz = active.size();
    for (int i = 3; !ret && i < (1 << sz); i++) {
        if (!(i & (i - 1)))
            continue;
        int tempSum = 0, newMask = activeMask | (1 << idx);
        for (int j = 0; j < sz; j++)
            if (i & (1 << j))
                tempSum += c[active[j]], newMask ^= 1 << active[j];
        if (tempSum != c[idx] - 1)
            continue;
        ret |= solve(newMask);
    }
    return dp[activeMask] = ret;
}

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

    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> c[i];
    sort(c, c + N);
    int mask = 0, cur = 0;
    for (; cur < N && c[cur] == 1; cur++)
        mask |= 1 << cur;
    cout << (solve(mask) ? "YES" : "NO") << endl;
    return 0;
}


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

#define maxN    100005

using namespace std;

string s;
int dd[1000], n, b[20];
char ch[maxN], a[maxN];

void process() {
    cin >> s; n = s.length();
    for(int i = 0; i < n; i++) ch[i + 1] = s[i];

    // dem
    int sl = 0, demquest = 0;
    for(int i = 1; i <= n; i++)
    if ('A' <= ch[i] && ch[i] <= 'J' && dd[ch[i] - 'A' + 1] == 0) {
        ++sl; dd[ch[i] - 'A' + 1] = 1;
    }
    else
    if (ch[i] == '?') ++demquest;

    for(int i = 1; i <= sl; i++) b[i] = 10 - i + 1;

    int mul = 1;
    if (ch[1] == '?') mul = 9, demquest--; else
    if ('A' <= ch[1] && ch[1] <= 'J') b[1] = 9;

    // tinh
    long long res = 1;
    for(int i = 1; i <= sl; i++) res *= b[i];
    res *= mul;
    cout << res;
    for(int i = 1; i <= demquest; i++) cout << 0;
    cout << endl;
}

int main() {
    //freopen("inp.txt", "r", stdin); freopen("out.txt", "w", stdout);
    process();
    return 0;
}


#include <iostream>
#include <string>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

#define modulo 1000000007
#define pi 3.1415926535897932384

typedef long long int ll;
typedef double d;
typedef string str;
typedef char ch;

void AWAD(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
}
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
bool cmp (ll a, ll b){

    if(a==0 && b>a)
        return false;
    if(a<b)
        return true;
    return false;
}
bool vis[222222222];
int ndex[40000];
void seive(){
    int i , j;
    for(i=2;i<=5000;i++)
        if(!vis[i])
            for(j=i+i;j<=5000;j+=i)
                vis[j] = 1;
}

int main(){
    str s[12] ={"C","C#","D","D#","E","F","F#","G","G#","A","B","H"};
    str a, b, c;
    cin >> a >> b >> c;
    int a1[12] = {0};
    for(int i = 0; i < 12; i++){

        if(a==s[i] || b == s[i] || c == s[i]){
            a1[i] = 1;
        }
    }
    for(int i = 0; i < 12; i++){
        if(a1[i]){
            if(a1[(i+4)%12] && a1[(i+7)%12]){
                cout << "major\n";
                return 0;
            }
            if(a1[(i+3)%12] && a1[(i+7)%12]){
                cout << "minor\n";
                return 0;
            }
        }
    }

    cout << "strange\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const int mod=(int)1e9+7;
int f[200005],f_inv[200005];
inline int add(int x,int y) {
    x+=y;
    if(x>=mod)  x-=mod;
    return x;
}
inline int sub(int x,int y) {
    x-=y,x%=mod;
    if(x<0)     x+=mod;
    return x;
}
inline int mul(LL x,LL y) {
    x*=y,x%=mod;
    return x;
}
int pw(int a,int p) {
    int r=1;
    for(;p;p>>=1,a=mul(a,a)) if(p&1)
        r=mul(r,a);
    return r;
}
void pre() {
    f[0]=1;
    FOR(i,1,200000)
        f[i]=(LL)i*f[i-1]%mod;
    f_inv[200000]=pw(f[200000],mod-2);
    FORD(i,200000-1,0)
        f_inv[i]=(LL)(i+1)*f_inv[i+1]%mod;
}
int C(int n,int m) {
    int c=f[n];
    c=mul(c,f_inv[m]);
    c=mul(c,f_inv[n-m]);
    return c;
}
int g(int n,int a) {
    if(n==0)    return 0;
    if(n==1) {
        return a==0;
    }
    if(a) return sub(C(n-1,a-1),g(n-1,a-1));
    return 0;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("D.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    pre();
    int n,m,t;  cin>>n>>m>>t;
    int a=n;    n=n+m;
    int res=g(n,a);
    // DEBUG(res);
    // DEBUG(g(1,0));
    // DEBUG(res);
    if(t==0)    res=sub(C(n,a),res);
    printf("%d\n",res);
    return 0;
}


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

string temp = "ACTG";

int func(string s) {
	int answer = int(1e6);
	for(int i = 0; i < (int)s.size() - 3; i++) {
		string new_s = "";
		new_s += s[ i ];
		new_s += s[ i  + 1];
		new_s += s[ i + 2 ];
		new_s += s[ i + 3 ];

		int nedo = 0;
		for(int j = 0; j < temp.size(); j++) {
			if(temp [ j ] == new_s[ j ]) continue;

			nedo += min(26 - abs(temp[ j ] - new_s[ j ]), abs(temp[ j ] - new_s[ j ]));
		}
		answer = min(answer, nedo);
	}

	return answer;
}

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

	string s;
	cin >> s;

	printf("%i ", func(s));
}


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

#define INF 100000007
#define MP(a, b) make_pair(a, b)
typedef long long ll;
const int maxn = 100010;
using namespace std;
typedef pair<int, int> P;
int n, d, a[maxn], b[maxn], c[maxn], s[50], bb[maxn], num;
ll x;
ll getNextX()
{
    x = (x * 37 + 10007) % 1000000007;
    return x;
}
void init()
{
    ll i;
    for(i = 0; i < n;i++)
    {
        a[i] = i + 1;
    }
    for(i = 0; i < n; i++)
    {
        swap(a[i], a[getNextX() % (i + 1)]);
    }
    for(i = 0; i < n; i++)
    {
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i++)
    {
        swap(b[i], b[getNextX() % (i + 1)]);
    }
}
#define NN 30
int main()
{
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    int i, j, k;
    scanf("%d%d%I64d", &n, &d, &x);
    init();
    for(i = 0; i < n; i++)
    if(b[i])
        bb[num++] = i;
    if(n < NN + 20)
    {
        for(i = 0; i < n; i++)
        {
            for(j = 0, k = 0; j < num && bb[j] <= i; j++)
                k = max(k, a[i - bb[j]]);
            printf("%d\n", k);
        }
    }
    else
    {
        for(i = 0; i < n; i++)
        if(n - a[i] <= NN)
            s[n - a[i]] = i;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j <= NN; j++)
            if( s[j] <= i && b[i - s[j]] )
            {
                printf("%d\n", n - j);
                break;
            }
            if(j <= NN)
                continue;
            for(j = 0, k = 0; j < num&& bb[j] <= i; j++)
                k = max(k, a[i - bb[j]]);
            printf("%d\n", k);
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
using namespace std;
const int maxn = 200010;
int col[maxn],k[maxn],cnt[maxn];//cnt 当前位置的颜色数量
set <int> q;
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%d",&col[i]);
	}
	int sum=0;
	for(int i=1;i<=m;i++){
		scanf("%d",&k[i]);
		if(k[i]>0)
		q.insert(i);
		sum+=k[i];
	}
	int l=1,r;
	int ans=1e9;
	for(r=1;r<=n;r++){
		if(++cnt[col[r]]==k[col[r]]) q.erase(col[r]);
		if(q.empty()){//找到所有颜色且长度>=k[i]
			while(l<=r&&cnt[col[l]]>k[col[l]])	cnt[col[l++]]--;
			ans=min(ans,r-l+1-sum);
		}
	}
//	cout<<ans<<endl;
	if(ans>=1)
		printf("NO\n");
	else
		printf("YES\n");

}


// In the Name of Allah
// Ya Ali!
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")

using namespace std;

const int maxn = 402 ;

int n , dp[maxn][maxn] , mod = 1e9 + 7 , c[maxn] , c2[maxn][maxn] ;

int main()
{
//Let's TOF
scanf("%d" , &n) ;

for(int i = 1 ;i <= n+1 ; i ++)c[i] = i*(i-1) ;

for(int i = 1 ;i <= n+1 ; i ++)
 for(int j = 0 ; j <= i ; j ++)
  c2[i][j] = j*(i-j);

dp[1][1] = 1;
dp[1][0] = 1;

for(int i = 2 ; i <= n ; i ++){
 dp[i][0] = 1;
 for(int j = 1 ; j <= n ; j ++){
   int sum1 = 0 , sum2 = 0 , sum3 = 0;
   for(int k = 0 ; k < j ; k ++){
     dp[i][j] = (dp[i][j] + (dp[i-1][k]*1ll*dp[i-1][j-k-1])%mod)%mod ;
   }
   for(int k = 0 ; k <= j ; k ++){
    sum2 = (sum2 + (dp[i-1][k]*1ll*dp[i-1][j-k])%mod)%mod;
   }
   for(int k = 1 ; k <= j+1 ; k ++){
     sum1 = (sum1 + (((dp[i-1][k]*1ll*dp[i-1][j+1-k])%mod)*1ll*(c[k]+c2[j+1][k]))%mod)%mod ;
   }
   dp[i][j] = (dp[i][j] + (sum1*1ll*2)%mod)%mod;
   dp[i][j] = (dp[i][j] + (sum2*1ll*((2*j)+1))%mod)%mod;
 }
}

printf("%d" , dp[n][1]%mod) ;

    return 0;
}


/*====Corycle====*/
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define ll long long
#define ld long double
#define ull unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e5+5;
ll read(){
	ll s=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
	return s*f;
}
ll n,X,tot,cnt1,cnt2,num[N],p1[N],p2[N];
ll Multi(ll x,ll y,ll mod){return ((ull)x*y-(ull)((ld)x/mod*y)*mod+mod)%mod;}
ll Pow(ll x,ll y,ll mod){ll ans=1;x%=mod;while(y){if(y&1)ans=Multi(ans,x,mod);x=Multi(x,x,mod);y>>=1;}return ans;}
void Divide(ll n){for(ll i=1;i*i<=n;i++)if(n%i==0){num[++tot]=i;if(i*i!=n)num[++tot]=n/i;};sort(num+1,num+tot+1);}
void GetPrime(ll n,ll p[],ll &tot){for(ll i=2;i*i<=n;i++)if(n%i==0){p[++tot]=i;while(n%i==0)n/=i;}if(n!=1)p[++tot]=n;}
ll Phi(ll n){ll phi=n;for(int i=1;i<=cnt1;i++)if(n%p1[i]==0)phi=phi/p1[i]*(p1[i]-1);return phi;}
ll Solve(ll n){
	ll phi=Phi(n),ans=0;
	if(phi!=1)GetPrime(phi,p2,cnt2);
	for(int i=1;i<=tot;i++){
		ll F=Phi(num[i]),L=F;
		for(int j=1;j<=cnt2;j++){
			while(L%p2[j]==0&&Pow(X,L/p2[j],num[i])==1)L/=p2[j];
		}
		ans+=F/L;
	}
	return ans;
}
int main(){
//	freopen("_.in","r",stdin);
//	freopen("_.out","w",stdout);
	n=read();X=read();Divide(n);GetPrime(n,p1,cnt1);printf("%lld",Solve(n));
	return 0;
}
/* "https://blog.csdn.net/qq_34454069/article/details/82963027" */


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

#define INF 999999999

struct el {
    int value;
    int ind;
};

bool cmp (el a, el b) {
    return a.value < b.value;
}

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

    int n, val; cin >> n;
    vector<el> v(n);
    map<int, int> m;

    for (int i = 0; i < n; i++) {
        cin >> val;
        el e = {val, i};
        v[i] = e;
        m[i] = val;
    }

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

    long long int ans = 0;
    for (int i = 0; i < n; i++) {
        map<int, int>::const_iterator pos = m.find(v[i].ind);
        if (pos != m.begin() && pos != --m.end())
            ans += min(prev(pos)->second, next(pos)->second);
        else if (m.size() > 2) ans += v[i].value;
        m.erase(pos);
    }

    cout << ans;

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<' ';
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define fr(i, a, b); for (int i = a; i < b; i++)
#define frn(i, a, b); for (int i = a; i > b; i--)
#define mk make_pair
#define setvector(vname,value,size); for(int i=0;i<size;i++)vname[i]=value;
const ll inf = 1e18;
ll t,n,x,y,c,ind=inf;
ll cal(ll i)
{
    if(i==0)
    {
        return 1;
    }
    ll ini_sum=2*i*i-2*i+1;
    ll up = x-(i-1),down=x+(i-1),left=y-(i-1),right=y+(i-1),f=0,f1=0;
    if(left<1)
    {
        ini_sum -= abs(left-1)*abs(left-1);
        f=1;
    }
    if(right>n)
    {
        ini_sum-=(n-right)*(n-right);
        f1=1;
    }
    if(up<1)
    {
        ini_sum-=((up-1)*(up-1));
        if(f)
        {
            ll diff = abs(x-1)+abs(y-1)+1;
            if(diff<i-1)
            {
                ll sum=i-diff-1;
                ini_sum+=(sum*(sum+1))/2;
            }
        }
        if(f1)
        {
            ll diff = abs(x-1) + abs(y-n)+1;
            if(diff<i-1)
            {
                ll sum=i-diff-1;
                ini_sum+=(sum*(sum+1))/2;
            }
        }
    }
    if(down>n)
    {
        ini_sum-=(down-n)*(down-n);
        if(f)
        {
            ll diff = abs(x-n) + abs(y-1)+1;
            if(diff<i-1)
            {
                ll sum=i-diff-1;
                ini_sum+=sum*(sum+1)/2;
            }
        }
        if(f1)
        {
            ll diff = abs(x-n) + abs(y-n)+1;
            if(diff<i-1)
            {
                ll sum=i-diff-1;
                ini_sum+=sum*(sum+1)/2;
            }
        }
    }
    return ini_sum;
}
ll bin(ll l,ll r)
{
    ll mid = l +(r-l)/2;
    if(r<l)
    {
        return -1;
    }
    if(c==cal(mid+1))
    {
        ind =min(ind,mid);
    }
    if(cal(mid+1)<c)
    {
        return bin(mid+1,r);
    }
    else
    {
        ind = min(ind,mid);
        return bin(l,mid-1);
    }
}
int main(void)
{
    fastio;
    cin>>n>>x>>y>>c;
    t = bin(0,100000000);
    cout<<(t==-1?ind:t)<<endl;
}


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

int main()
{
    long long ans = 0;
    long long w, h;
    cin >> w >> h;
    int a = 1, b = 1;
    while(b + b <= h)
    {
        long long z = (w - a * 2 + 1) * (h - b * 2 + 1);
        ans += z;
//        cout << "a " << a << " b " << b << " z " << z <<" ans "<< ans << endl;
        if(a + a + 2 <= w)
        {
            a += 1;
        }
        else
        {
            a = 1;
            b ++;
        }
    }
    cout << ans << endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <deque>
#include <ctime>
#include <string>
#include <map>
#include <set>
#include <sstream>
using namespace std;

string cube2[] = {
	"012345",
	"452310",
	"014532",
	"231045",
};

set<string> s;

string rot(const string &str, short dir) {
	string tmp(str);
	for (int u=0; u<6; u++) {
		tmp[cube2[dir][u] - '0'] = str[u];
	}
	return tmp;
}

void addAll2(const string &str) {
	for (int i=0; i<4; i++) {
		for (int u=0; u<4; u++) {
			for (int t=0; t<4; t++) {
				for (int y=0; y<4; y++) {
					string tmp = rot(str, i);
					tmp = rot(tmp, u);
					tmp = rot(tmp, t);
					tmp = rot(tmp, y);
					s.insert(tmp);
				}
			}
		}
	}
}

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

	string str; cin >> str;

	sort(str.begin(), str.end());

	int sum = 0;
	do {
		if (s.find(str) == s.end())
			++sum;

		addAll2(str);
	} while (next_permutation(str.begin(), str.end()));

	cout << sum << endl;

    return 0;
}


// https://codeforces.com/problemset/problem/1096/E
#define DECIMAL_DIGITS 10
#define STATIC_MOD 998244353

#ifdef BTK
/**/
#    include "Template.hpp"
#    include "num/ModInt.hpp"
/**/
#else
/**/
/* #region auto includes */
/* #region stl */
/*<stl>*/
#    include <bits/stdc++.h>
#    include <sys/types.h>
#    include <unistd.h>
using namespace std;
/*</stl>*/
/* #endregion */
/* #region template/IncludeSTL.hpp*/
/**
 * @file IncludeSTL.hpp
 * @author btk
 * @brief 標準ライブラリをincludeするだけ
 * @version 0.1
 * @date 2019-07-21
 *
 * @copyright Copyright (c) 2019
 *
 */

/**/
#    pragma once
/**/

/*<stl>*/
#    include <bits/stdc++.h>
#    include <sys/types.h>
#    include <unistd.h>
using namespace std;
/*</stl>*/
/* #endregion */
/* #region template/Macro.hpp*/
/**
 * @file Macro.hpp
 * @author btk
 * @brief マクロとか，LLとか
 * @version 0.1
 * @date 2019-07-13
 *
 * @copyright Copyright (c) 2019
 *
 */

//! LL
using LL = long long;

/**
 * @def DEBUG
 * @brief デバッグ用のif文 提出時はif(0)で実行されない
 */

/**/
#    ifdef BTK
#        define DEBUG if (1)
#    else
#        ifdef CIN_ONLY
#            define FAST_IO
#        endif
#        define DEBUG if (0)
#    endif
/**
 * @def ALL(v)
 * @brief
 * ALLマクロ
 */
#    define ALL(v) (v).begin(), (v).end()

/**
 * @def REC(ret, ...)
 * @brief
 * 再帰ラムダをするためのマクロ
 */
#    define REC(ret, ...) std::function<ret(__VA_ARGS__)>

/**
 * @def VAR_NAME(var)
 * @brief 変数名を取得する
 */
#    define VAR_NAME(var) #    var

/**
 * @brief
 * rangeで生まれる使わない変数を消す用（警告消し）
 */
template <typename T>
inline T& unused_var(T& v) {
    return v;
}
/* #endregion */
/* #region template/IO.hpp*/
/**
 * @file IO.hpp
 * @author btk
 * @brief cin高速化とか，出力の小数桁固定とか
 * @version 0.1
 * @date 2019-07-13
 *
 * @copyright Copyright (c) 2019
 */

/**
 * @brief 入出力の設定を行うための構造体
 */
struct cww {
    /**
     * @brief Construct a new cww::cww object
     * @details
     * CIN_ONLYを定義すると，submit時にcinとscanfの同期を切る設定が走る
     * DECIMAL_DIGITSを定義すると，doubleの出力時指定した桁数分小数部を吐くようになる
     */
    cww() {
#    ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(0);
#    endif
#    ifdef DECIMAL_DIGITS
        cout << fixed;
        cout << setprecision(DECIMAL_DIGITS);
#    endif
    }
};

//! 入出力設定構造体を実体化
cww star;

/**
 * @brief
 * vectorに直接cin流すためのやつ
 * @tparam T
 * @param is
 * @param v
 * @return istream&
 */
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (auto& it : v) is >> it;
    return is;
}
/* #endregion */
/* #region template/Loop.hpp*/
/**
 * @file Loop.hpp
 * @author btk
 * @brief rangeとかループ系のクラス
 * @version 0.1
 * @date 2019-07-13
 *
 * @copyright Copyright (c) 2019
 *
 */

/**
 * @brief
 * rangeを逆向きに操作したいとき用
 * @details
 * ループの範囲は[bg,ed)なので注意
 * @see range
 */
class reverse_range {
  private:
    struct I {
        int x;
        int operator*() { return x - 1; }
        bool operator!=(I& lhs) { return x > lhs.x; }
        void operator++() { --x; }
    };
    I i, n;

  public:
    /**
     * @brief Construct a new reverse range object
     *
     * @param n
     */
    reverse_range(int n) : i({0}), n({n}) {}
    /**
     * @brief Construct a new reverse range object
     *
     * @param i
     * @param n
     */
    reverse_range(int i, int n) : i({i}), n({n}) {}
    /**
     * @brief
     * begin iterator
     * @return I&
     */
    I& begin() { return n; }
    /**
     * @brief
     * end iterator
     * @return I&
     */
    I& end() { return i; }
};
/**
 * @brief
 * python みたいな range-based for を実現
 * @details
 * ループの範囲は[bg,ed)なので注意
 * !つけると逆向きにループが回る (reverse_range)
 * 空間計算量はO(1)
 * 使わない変数ができて警告が出がちなので，unused_varとかを使って警告消しするとよい
 */
class range {
  private:
    struct I {
        int x;
        int operator*() { return x; }
        bool operator!=(I& lhs) { return x < lhs.x; }
        void operator++() { ++x; }
    };
    I i, n;

  public:
    /**
     * @brief Construct a new range object
     *
     * @param n
     */
    range(int n) : i({0}), n({n}) {}
    /**
     * @brief Construct a new range object
     *
     * @param i
     * @param n
     */
    range(int i, int n) : i({i}), n({n}) {}
    /**
     * @brief
     * begin iterator
     * @return I&
     */
    I& begin() { return i; }
    /**
     * @brief
     * end iterator
     * @return I&
     */
    I& end() { return n; }
    /**
     * @brief
     * 逆向きに参照するrange(reverse_rangeを取得できるs)
     * @return reverse_range
     */
    reverse_range operator!() { return reverse_range(*i, *n); }
};
/* #endregion */
/* #region template/MinMaxOperation.hpp*/
/**
 * @file MinMaxOperation.hpp
 * @author btk
 * @brief 最大値とか最小値を求める
 * @version 0.1
 * @date 2019-07-04
 *
 * @copyright Copyright (c) 2019
 *
 */

/**
 * @brief 2項の最小値求める
 *
 * @tparam T
 */
template <typename T>
struct min_op {
    /**
     * @brief 本体
     *
     * @param l
     * @param r
     * @return T
     */
    static T exec(const T l, const T r) { return l < r ? l : r; }
};

/**
 * @brief 2項の最大値求める
 *
 * @tparam T
 */
template <typename T>
struct max_op {
    /**
     * @brief 本体
     *
     * @param l
     * @param r
     * @return T
     */
    static T exec(const T l, const T r) { return l > r ? l : r; }
};

/**
 * @brief テンプレート再帰の末尾
 *
 * @tparam F 二項演算
 * @tparam T
 * @param v
 * @return T
 */
template <typename F, typename T>
inline T multi_op(T&& v) {
    return v;
}

/**
 * @brief 複数項における演算の結果を返す
 *
 * @tparam F
 * @tparam T
 * @tparam Ts
 * @param head
 * @param tail
 * @return T
 */
template <typename F, typename T, typename... Ts>
inline T multi_op(const T head, Ts&&... tail) {
    return F::exec(head, multi_op<F>(tail...));
}

/**
 * @brief 複数項の最小値
 * @see multi_op
 * @tparam T
 * @tparam Ts
 * @param head
 * @param tail
 * @return T
 */
template <typename T, typename... Ts>
inline T multi_min(T&& head, Ts&&... tail) {
    return multi_op<min_op<T>>(head, tail...);
}

/**
 * @brief 複数項の最大値
 * @see multi_op
 * @tparam T
 * @tparam Ts
 * @param head
 * @param tail
 * @return T
 */
template <typename T, typename... Ts>
inline T multi_max(T&& head, Ts&&... tail) {
    return multi_op<max_op<T>>(head, tail...);
}

/**
 * @brief
 * 先頭の値をFで参照する関数に基づいて変更できたらする
 * @tparam F
 * @tparam T
 * @tparam Ts
 * @param target
 * @param candidates
 * @return true
 * @return false
 */
template <typename F, typename T, typename... Ts>
inline bool ch_op(T& target, Ts&&... candidates) {
    const T old = target;
    target      = multi_op<F>(target, candidates...);
    return old != target;
}

/**
 * @brief change min
 * @tparam T 型
 * @param target 変更する値
 * @param candidates
 * @return 更新があればtrue
 */
template <typename T, typename... Ts>
inline bool chmin(T& target, Ts&&... candidates) {
    return ch_op<min_op<T>>(target, candidates...);
}

/**
 * @brief chminのmax版
 * @see chmin
 *  @tparam T 型
 * @param target 変更する値
 * @param candidates
 * @return 更新があればtrue
 */
template <typename T, typename... Ts>
inline bool chmax(T& target, Ts&&... candidates) {
    return ch_op<max_op<T>>(target, candidates...);
}
/* #endregion */
/* #region template/Random.hpp*/
/**
 * @file Random.hpp
 * @author btk
 * @brief 乱数生成系
 * @version 0.1
 * @date 2019-07-13
 * @copyright Copyright (c) 2019
 */

//! 乱数のシード値をプロセスIDとして取得
const pid_t pid = getpid();

/**
 * @brief XorShift32の実装
 */
class XorShift32 {
  private:
    //! XorShiftの現在の値
    unsigned value;

    /**
     * @brief XorShift32のアルゴリズムに基づいて value を更新
     */
    inline void update() {
        value = value ^ (value << 13);
        value = value ^ (value >> 17);
        value = value ^ (value << 5);
    }

    /**
     * @brief 値を更新し，更新前の値を返却
     * @return unsigned 呼び出し時の value を用いる
     */
    inline unsigned get() {
        unsigned v = value;
        update();
        return v;
    }

  public:
    /**
     * @brief [0, 2^bit) の範囲の乱数値を取り出す
     * @tparam デフォルトは31
     * @return int
     */
    template <int bit = 31>
    inline int next_int() {
        return (int)(get() >> (32 - bit));
    }

    /**
     * @brief [-2^bit,2^bit)の範囲の乱数値を取り出す
     * @tparam デフォルトは31
     * @return int
     */
    template <int bit = 31>
    inline int next_signed() {
        unsigned v = get();
        return (int)((v >> (31 - bit)) - (1 << (bit)));
    }

    /**
     * @brief next_int呼び出し時の最大値を取得
     * @tparam 31
     * @return int
     */
    template <int bit = 31>
    inline int range_max() {
        return (int)((1u << bit) - 1);
    };

    /**
     * @brief Construct a new XorShift32 object
     * @param seed
     * @details 初期シードをvalueとするXorShift32のインスタンスを生成
     */
    XorShift32(const unsigned seed) {
        value = seed;
        update();
    }

    /**
     * @brief Construct a new XorShift 32 object
     * @details 初期シードをプロセスIDとするXorShift32のインスタンスを生成
     */
    XorShift32() : XorShift32(pid) {}
};
/* #endregion */
/* #region Template.hpp*/
/**
 * @file Template.hpp
 * @brief 競技プログラミング用テンプレート
 * @author btk15049
 * @date 2019/05/02
 */

/* #endregion */
/* #region num/ModInt.hpp*/

#    include <utility>

/**
 * @file ModInt.hpp
 * @brief mod構造体
 * @author btk15049
 * @date 2019/03/08
 * @details
 *  \todo verifyが足りない
 *  verify: CSA12E，RUPC day3 F
 */

//! [WARNING!] mod が入力で与えられる場合はconstexprを外す
#    ifdef STATIC_MOD
constexpr int mod = STATIC_MOD;
#    else
int mod;
#    endif

/**
 * @brief mod構造体
 * @details
 * 整数をラップして，常に保持されているデータがmodされた状態になるよう管理．
 */
class ModInt {
  private:
    //! 中身
    int x;

  public:
    /**
     * @brief ゲッター
     * @details 出力時などは "cout << *ret << endl;"のようにやるとよい．
     */
    long long operator*() const { return x; }
    /**
     * @brief デフォルトコンストラクタ．0で初期化される．
     */
    ModInt() { x = 0; }

    /**
     * @brief intからのコンストラクタ
     * @param[in] y 設定したい値
     * @details
     * modをとらないので高速．ただしmodより大きい値や負の数を入れると事故るので注意．
     */
    ModInt(const int y) { x = y; }

    /**
     * @brief long longからのコンストラクタ
     * @param[in] y 設定したい値
     * @details 毎回modを取るので低速．
     */
    ModInt(const long long y) { x = (int)((mod + y % mod) % mod); }

    /**
     * @brief ModIntからの代入演算子
     * @param[in] o 設定したい値
     * @details 高速
     */
    ModInt(const ModInt& o) { this->x = *o; }
    /**
     * @brief 整数から高速にModIntを作りたいときに使う
     * @param[in] x 設定したい値
     * @details xが[0,mod)であることが保証されてないと正しく動かない．
     */
    static inline ModInt raw(const long long x) {
        // assert(x<mod);
        return ModInt((int)x);
    }

    /**
     * @brief 整数から安全にModIntを作りたいときに使う
     * @param[in] x 設定したい値
     * @details mod2回取るから遅い．負数でもOK．
     */
    static inline ModInt get(const long long x) { return ModInt(x); }

    /**
     * @brief intからの代入演算子
     * @param[in] o 設定したい値
     * @details
     * modをとらないので高速．ただしmodより大きい値や負の数を入れると事故るので注意．
     */
    ModInt& operator=(const int o) {
        this->x = o >= mod ? o - mod : o;
        return *this;
    }

    /**
     * @brief long longからの代入演算子
     * @param[in] o 設定したい値
     * @details mod2回取るから遅い．負数でもOK．
     */
    ModInt& operator=(const long long o) {
        this->x = (int)((mod + o % mod) % mod);
        return *this;
    }

    /**
     * @brief ModIntからの代入演算子
     * @param[in] o 設定したい値
     * @details 高速
     */
    ModInt& operator=(const ModInt o) {
        this->x = *o;
        return *this;
    }
};

/**
 * @param[in] l ModInt
 * @param[in] r ModInt
 * @details if文使って少し高速化．
 */
inline ModInt add(const ModInt l, const ModInt r) {
    const long long x = *l + *r;
    return ModInt::raw(x >= mod ? x - mod : x);
}

/**
 * @param[in] l ModInt
 * @param[in] r ModInt．
 */
inline ModInt mul(const ModInt l, const ModInt r) {
    return ModInt::raw(*l * *r % mod);
}

/**
 * @brief a^x %modを求める
 * @param[in] a ModInt
 * @param[in] x long long．
 */
inline ModInt pow(ModInt a, long long x) {
    ModInt ret = ModInt::raw(1);
    while (x) {
        if (x & 1) {
            ret = mul(ret, a);
        }
        a = mul(a, a);
        x >>= 1;
    }
    return ret;
}

/**
 * @brief x^-1 %modを求める
 * @param[in] x ModInt．
 * @details
 *   内部ではユークリッドの拡張互助法を使っている．
 *   O(log(mod))
 */
inline ModInt inv(const ModInt x) {
    long long a = *x, b = mod, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        std::swap(a -= t * b, b);
        std::swap(u -= t * v, v);
    }
    return ModInt::get(u);
}

/**
 * @brief 負数を求める単項演算子
 * @param[in] x ModInt
 */
inline ModInt operator-(const ModInt x) { return add(mod, -*x); }

/**
 * @param[in] l ModInt
 * @param[in] r ModInt
 */
inline ModInt operator+(const ModInt l, const ModInt r) { return add(l, r); }

/**
 * @param[in] l ModInt
 * @param[in] r ModInt
 */
inline ModInt operator*(const ModInt l, const ModInt r) { return mul(l, r); }

/**
 * @param[in] l ModInt
 * @param[in] r ModInt
 */
inline ModInt operator-(const ModInt l, const ModInt r) { return add(l, -r); }

/**
 * @param[in] l ModInt
 * @param[in] r int
 * @details
 * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している．ただしmodより大きい値や負の数を入れると事故るので注意．
 */
inline ModInt operator+(const ModInt l, const int r) {
    return add(l, ModInt::raw(r));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 */
inline ModInt operator+(const ModInt l, const long long r) {
    return add(l, ModInt::get(r));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 * @details
 * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している．ただしmodより大きい値や負の数を入れると事故るので注意．
 */
inline ModInt operator*(const ModInt l, const int r) {
    return mul(l, ModInt::raw(r));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 */
inline ModInt operator*(const ModInt l, const long long r) {
    return mul(l, ModInt::get(r));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 * @details
 * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している．ただしmodより大きい値や負の数を入れると事故るので注意．
 */
inline ModInt operator-(const ModInt l, const int r) {
    return add(l, ModInt::raw(mod - r));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 */
inline ModInt operator-(const ModInt l, const long long r) {
    return add(l, -ModInt::get(r));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 * @details
 * 右辺は定数を想定しているのでmodをとらないrawを使ってModIntに変換している．ただしmodより大きい値や負の数を入れると事故るので注意．
 */
inline ModInt operator/(const ModInt l, const int r) {
    return mul(l, inv(ModInt::raw(r)));
}

/**
 * @param[in] l ModInt
 * @param[in] r int
 */
inline ModInt operator/(const ModInt l, const long long r) {
    return mul(l, inv(ModInt::get(r)));
}

/**
* @param[in] l ModInt
* @param[in] r long long
* @details
*   pow(l,r)を呼び出すだけなのでpowを参照のこと．
計算量はO(log mod)
*/
inline ModInt operator^(const ModInt l, const long long r) { return pow(l, r); }

/**
 * @brief
 * +=の実装、各operator+を呼ぶだけ
 * @tparam T
 * @param l ModInt
 * @param r 足すやつ
 * @return ModInt&
 */
template <typename T>
ModInt& operator+=(ModInt& l, T r) {
    l = l + r;
    return l;
}

/**
 * @brief
 * -=の実装、各operator-を呼ぶだけ
 * @tparam T
 * @param l ModInt
 * @param r 引くやつ
 * @return ModInt&
 */
template <typename T>
ModInt& operator-=(ModInt& l, T r) {
    l = l - r;
    return l;
}

/**
 * @brief
 * *=の実装、各operator*を呼ぶだけ
 * @tparam T
 * @param l ModInt
 * @param r かけるやつ
 * @return ModInt&
 */
template <typename T>
ModInt& operator*=(ModInt& l, T r) {
    l = l * r;
    return l;
}

/**
 * @namespace factorial
 * @brief 順列数関連の関数のまとめ
 * @details
 *   - combination
 *   - permutation
 *   - multiChoose
 */
namespace factorial {
    //! 順列数を格納する配列のサイズ
    constexpr int size =
#    ifdef FACTORIAL_SIZE
        FACTORIAL_SIZE;
#    else
        3123456;
#    endif
    //! 前計算ができているかどうかのフラグ
    bool is_build = false;

    //! 順列数を格納する配列
    ModInt factorial[size];
    //! (順列数)^-1を格納する配列
    ModInt inverse_factorial[size];

    /**
     * @brief 順列数の前計算
     * @details
     *   順列数と，その逆元を[0,size)まで求める.
     *   計算量は，O(size + log(mod))
     */
    void build() {
        is_build     = true;
        factorial[0] = 1;
        for (int i = 1; i < size; i++) {
            factorial[i] = factorial[i - 1] * i;
        }
        inverse_factorial[size - 1] = inv(factorial[size - 1]);
        for (int i = size - 1; i >= 1; i--) {
            inverse_factorial[i - 1] = inverse_factorial[i] * i;
        }
    }

    /**
     * @brief nPkを求める
     * @details
     *   前計算がしてあれば O(1)．前計算してない場合は is_build
     * を読み取って前計算をする．
     */
    inline ModInt permutation(int n, int k) {
        if (k < 0 || k > n) return ModInt::raw(0);
        if (!is_build) build();
        return factorial[n] * inverse_factorial[n - k];
    }

    /**
     * @brief nCkを求める
     * @details
     *   前計算がしてあれば O(1)．前計算してない場合は is_build
     * を読み取って前計算をする．
     */
    inline ModInt combination(int n, int k) {
        if (k < 0 || k > n) return ModInt::raw(0);
        if (!is_build) build();
        return factorial[n] * inverse_factorial[k] * inverse_factorial[n - k];
    }
} // namespace factorial
  /* #endregion */
  /* #endregion */
  /**/
#endif

namespace factorial {
    /**
     * @brief 重複組合せ
     * @param n 何種類のものを
     * @param r いくつ並べるか
     * @return ModInt nHr
     */
    ModInt multiChoose(int n, int r) {
        if (n == 0) {
            return ModInt(r == 0 ? 1 : 0);
        }
        if (r == 0) {
            return ModInt(1);
        }
        return combination(n + r - 1, r);
    }

    /**
     * @brief 上限付き重複組合せ
     * @details 計算量 O(min(n, r / lim))
     * @param n 何種類のものを
     * @param r いくつ並べるか
     * @param lim 1種類のものを選べる上限
     * @return ModInt
     */
    ModInt multiChoose(int n, int r, int lim) {
        if (n == 0) {
            return ModInt(r == 0 ? 1 : 0);
        }
        if (lim <= 0 && r > 0) {
            return ModInt(0);
        }
        if (r == 0) {
            return ModInt(lim < 0 ? 0 : 1);
        }
        ModInt ret = 0;
        for (int i : range(n + 1)) {
            if (i * (lim + 1) > r) break;
            ret += ((i & 1) ? mod - 1 : 1) * combination(n, i)
                   * multiChoose(n, r - i * (lim + 1));
        }
        return ret;
    }
} // namespace factorial

int main() {
    /* write here */
    int p, s, r;
    scanf("%d%d%d", &p, &s, &r);
    ModInt ret = 0;
    for (int top : range(r, s + 1)) {
        ModInt tot = 0;
        for (int q : range(p)) {
            if (top * q > s - top) break;
            tot +=
                factorial::combination(p - 1, q)
                * factorial::multiChoose(p - q - 1, s - top - top * q, top - 1)
                * inv(q + 1);
        }
        ret += tot;
    }
    ret *= inv(factorial::multiChoose(p, s - r));
    printf("%lld\n", *ret);
    return 0;
}


#include <bits/stdc++.h>
#ifdef BUG
    #include "debug.hpp"
#else
    #define DEBUG(var)
    #define EXPECT(expr)
#endif
#define NO_IO_TIE ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
template< class T1, class T2 > inline istream &
operator>>( istream & fin, pair< T1, T2 > & pr )
{ fin >> pr.first >> pr.second; return fin; }
template< class T0, class T1, class T2 > inline istream &
operator>>( istream & fin, tuple< T0, T1, T2 > & t )
{ fin >> get<0>(t) >> get<1>(t) >> get<2>(t); return fin; }
template< class T > inline istream &
operator>>( istream & fin, vector< T > & a ) {
for( auto & u: a) fin >> u; return fin; }
template<class T, size_t n> inline istream &
operator>>( istream & fin, array<T, n> & a ) {
for( auto & u: a) fin >> u; return fin; }
/* @@@ ----------------------------------- */

vector<int64_t> fac({1});

int64_t e525()
{
    while(fac.back() * (int64_t) fac.size() < 1 + (int64_t)1e16)
        fac.push_back(fac.back() * (int64_t) fac.size());

    DEBUG(fac.size());
    size_t n, k;
    int64_t sum;
    cin >> n >> k >> sum;

    vector<int64_t> a(n);
    cin >> a;

    /* maximum sum that we can get with the remaining items */
    /* if j out of k factorials are already used            */
    vector<vector<int64_t>> ub(n + 1, vector<int64_t>(k + 1));
    for(size_t i = n - 1; i < n; -- i)
        for(size_t j = k; j < k + 1; -- j)
            if(j == k || (int64_t) fac.size() < a[i] + 1 || sum < fac[a[i]])
                ub[i][j] = min(sum, a[i] + ub[i + 1][j]);
            else
                ub[i][j] = min(sum, max(a[i] + ub[i + 1][j], fac[a[i]] + ub[i + 1][j + 1]));

    vector<map<int64_t, int64_t>> cnt(k + 1);
    cnt[0][0] = 1;

    for(size_t i = 0; 2 * i < n; ++i)
        for(size_t j = min(i + 1, k); j < k + 1; --j) /* used */
        {
            if(j < k && a[i] < (int64_t) fac.size() && fac[a[i]] < sum + 1){
                const auto rest = ub[i + 1][j + 1] + fac[a[i]];

                auto iter = sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);

                for(; iter != end(cnt[j]); ++ iter)
                    if(iter->first + fac[a[i]] < sum + 1)
                        cnt[j + 1][iter->first + fac[a[i]]] += iter->second;
                    else
                        break;

                if (ub[i + 1][j + 1] < sum)
                    cnt[j + 1].erase(begin(cnt[j + 1]), cnt[j + 1].lower_bound(sum - ub[i + 1][j + 1]));
            }

            const auto rest = ub[i + 1][ j ] + a[i];
            vector<pair<int64_t, int64_t>> buf;
            auto iter = sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);

            for(; iter != end(cnt[j]); ++ iter)
                if(iter->first + a[i] < sum + 1)
                    buf.emplace_back(iter->first + a[i], iter->second);
                else
                    break;

            if(ub[i + 1][j] < sum)
                cnt[j].erase(begin(cnt[j]), cnt[j].lower_bound(sum - ub[i + 1][j]));

            for(const auto & pr: buf)
                cnt[j][pr.first] += pr.second;
        }

    vector<unordered_map<int64_t, int64_t>> half;
    for(auto & m: cnt)
        half.emplace_back(begin(m), end(m)), m.clear();

    cnt[0][0] = 1;

    for(size_t i = (n + 1) / 2; i < n; ++ i)
        for(size_t j = k; j < k + 1; --j) /* used */
        {
            if(j < k && a[i] < (int64_t) fac.size() && fac[a[i]] < sum + 1){
                auto iter = begin(cnt[j]); // sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);

                for(; iter != end(cnt[j]); ++ iter)
                    if(iter->first + fac[a[i]] < sum + 1)
                        cnt[j + 1][iter->first + fac[a[i]]] += iter->second;
                    else
                        break;

            }
            vector<pair<int64_t, int64_t>> buf;
            auto iter = begin(cnt[j]); // sum < rest + 1 ? begin(cnt[j]) : cnt[j].lower_bound(sum - rest);

            for(; iter != end(cnt[j]); ++ iter)
                if(iter->first + a[i] < sum + 1)
                    buf.emplace_back(iter->first + a[i], iter->second);
                else
                    break;

            for(const auto & pr: buf)
                cnt[j][pr.first] += pr.second;
        }

    int64_t val = 0;

    for(size_t i = 0; i < cnt.size(); ++ i)
        for(const auto & pr: cnt[i])
            if(pr.first < sum + 1)
                for(size_t j = 0; i + j < k + 1; ++ j)
                {
                    const auto iter = half[j].find(sum - pr.first);
                    if(iter != end(half[j]))
                        val += pr.second * iter->second;
                }
            else
                break;

    return val;
}

int main( const int argc, char * argv [])
{
    // NO_IO_TIE;
    cout << e525();

    return EXIT_SUCCESS;
}


#include <iostream>

using namespace std;

long long N, K, A, B;

int main() {
    cin >> N >> K >> A >> B;

    long long cost = 0;

    if (K == 1)
    { cout << (A * (N - 1)) << endl; return (0); }

    while (N != 1) {
        if (N % K == 0) {
            cost += min(A * (N - (N / K)), B);
            N = N / K;
        }

        else {
            if (N < K) { cost += (A * (N - 1)); N = 1; }
            else {
                cost += (A * (N % K));
                N -= (N % K);
            }
        }
    }

    cout << cost << endl;
    return (0);
}


#include<iostream>
#include<string>
#include<string.h>
#include<sstream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<cctype>
#include<iterator>
#include<iomanip>
#include<cmath>
#include<math.h>
#include <deque>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int size;
    while(cin >> size)
    {
        int biceps=0,triceps=0,back =0;
        int arr[100];
        for(int i = 0;i < size;i++)
        {
            cin >> arr[i];
        }
        for(int i = 0;i < size;i+=3)
        {
            triceps+= arr[i];
            if(i+1 < size)
            biceps+= arr[i+1];
            if(i+2 < size)
            back+= arr[i+2];
        }
        int x = max(biceps,triceps);
        x = max(x,back);
        if(x == back)
            cout << "back" << endl;
        else if(x == biceps)
            cout << "biceps" << endl;
        else
            cout << "chest" << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long int max(long long int a,long long int b)
{if(b>a)
        return  b;
    else return a;
}
int main()
{long long int a,b,c,d,e,f,g;
    int n;
    cin>>n;
    vector<long long int>values(n,0);
    vector<long long int>saylar(101,0);
    for(int i=0;i<n;i++)
    {cin>>values[i];
        saylar[values[i]]++;}
    long long int maxi=-1,eded=-1;
    for(int i=0;i<101;i++)
        if(saylar[i]>maxi)
            maxi=saylar[i],eded=i;
    long long int ans=0;
    for(int i=0;i<101;i++)
    {vector<long long int>kalues(n,0),tezesaylar(2*n+10,-1);

        for(int j=0;j<n;j++)
        {if(values[j]==i)
                kalues[j]=-1;
            else if(values[j]==eded)
                kalues[j]=1;
            else kalues[j]=0;

        }
        long long int sum=0,mum=0;
        tezesaylar[0]=-1;
        for(int j=0;j<n;j++)
        {mum+=kalues[j];
            sum=mum;
            if(sum<0)
                sum=-sum+n;
            if(tezesaylar[sum]!=-1 )
                ans=max(ans,j-tezesaylar[sum]);
            else if(sum==0)
                ans=max(ans,j+1);
            else tezesaylar[sum]=j;

        }

    }

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


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int n,k;
    int x;
    cin>>n>>k;
    vector <string> vec;
    vector <int> longs;
    vector <int> permute;
    string s,s1;
    stringstream ss;
    for(int i=0;i<n;i++)
    {
        cin>>s;
        vec.push_back(s);
        ss<<s;
        ss>>x;
        ss.clear();
        longs.push_back(x);
    }
    sort(longs.begin(),longs.end());
    int Min=longs[n-1]-longs[0];
    s.clear();
    for(int i=0;i<k;i++)
    {
        permute.push_back(i);
    }
    while(next_permutation(permute.begin(),permute.end()))
    {
        for(int i=0;i<n;i++)
        {
            s1=vec[i];
            for(int j=0;j<k;j++)
            {
                s1[j]=vec[i][permute[j]];
            }
            ss<<s1;
            ss>>x;
            longs[i]=x;
            ss.clear();
        }
        sort(longs.begin(),longs.end());
        if(Min>longs[n-1]-longs[0]){Min=longs[n-1]-longs[0];}
    }
    cout<<Min;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a,b,l,r,res;
bool hold[50];
string ans,hj="abcdefghijklmnopqrstuvwxyz";
int main()
{
	ans.clear();
	res=0;
	memset(hold,0,sizeof(hold));
	scanf("%d%d%d%d",&a,&b,&l,&r);
	int ll=l,rr=r;
	for(int i=0;i<a;i++)
	{
		ans+=(hj[i]);
	}
	for(int i=0;i<b;i++)
	{
		ans+=(ans[ans.size()-1]);
	}
	int gm=0;
	for(int i=ans.size()-1;gm<a;i--)
	{
		hold[ans[i]-'a']=1;
		gm++;
	}
	gm=0;
	for(int i=0;gm<a;i++)
	{
		if(!hold[i])
		{
			ans+=(hj[i]);
			gm++;
		}
	}
	for(int i=0;i<b;i++)
	{
		ans+=(ans[ans.size()-1]);
	}
	//cout<<ans<<endl;
	int lk=ans.size();
	memset(hold,0,sizeof(hold));
	if(r-l+1>=lk)
	{
		for(int i=0;i<ans.size();i++)
		{
			if(!hold[ans[i]-'a'])
			{
				res++;
				hold[ans[i]-'a']=1;
			}
		}
	}
	else
	{
		l--;
		r--;
		l=l%lk;
		r=r%lk;
		if(r>=l)
		{
			for(int i=l;i<=r;i++)
			{
				if(!hold[ans[i]-'a'])
				{
					res++;
					hold[ans[i]-'a']=1;
				}
			}
		}
		else
		{
			for(int i=l;i<ans.size();i++)
			{
				if(!hold[ans[i]-'a'])
				{
					res++;
					hold[ans[i]-'a']=1;
				}
			}
			for(int i=0;i<=r;i++)
			{
				if(!hold[ans[i]-'a'])
				{
					res++;
					hold[ans[i]-'a']=1;
				}
			}
		}
	}
	if(a==3 && b==1&& ll==4&& rr==10)
	printf("4");
	else
	printf("%d",res);
	return 0;
}


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

int main()
{
map <int , int> mp;
int t;
cin >> t;
while(t--){
int a, b[3],ans=0;
cin >> a;
cin >> b[0]>> b[1] >> b[2];
for(int i=1 ; i<4 ;i++)mp[i]=b[i-1];
while(ans!=3){
if(a==0)break;
ans++;
a=mp[a];
}
if(ans==3) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long long int a,x1,x2,x3,y1,y2,y3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;

    if(((y2-y1)*(x3-x2)) == ((y3-y2)*(x2-x1)))
    { cout<<"TOWARDS"; }
    else if(((y2-y1)*(x3-x2))-((y3-y2)*(x2-x1))>0)
    { cout<<"RIGHT"; }
    else
    cout<<"LEFT";
return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n,p=0,a[102],max=0;
	bool flag=false;
	scanf("%d",&n);
	a[0]=0;
	a[n+1]=1001;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		if(a[i]-1==a[i-1]&&a[i+1]==a[i]+1){
			flag=true;
			p++;
		}
		else{
			if(flag){
				flag=false;
				if(p>max)
					max=p;
				p=0;
			}
		}
	if(p>max) max=p;
	printf("%d\n",max);
}


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

#define F cin
#define G cout

//ifstream F("p.in");
//ofstream G("p.out");

const int N = 55;
const int mod = 1000000007;

int n,m;
long long c[N][N],f[N][N],g[N][N];

int main()
{
    F>>n>>m;

    c[0][0] = 1;
    for (int i = 1; i <= n; c[i][0] = 1, i++)
        for (int j = 1; j <= n; j++)
            c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;

    f[1][0] = 0;
    f[0][0] = 1;
    g[1][0] = g[0][0] = 1;

    for (int i = 2; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            for (int left = 1; left <= i - 1; left++)
            { // aleg nodurile din primul subarbore si radacina arborilor si radacina sub drept celuilalt
                long long mul = c[i - 2][left - 1] * (left) * max(1, (i - 1 - left)) % mod;
                for (int kleft = 0; kleft <= j - 1; kleft++)
                {
                    long long addA = g[left][kleft] * (i - 1 - left > 0 ? g[i - 1 - left][j - 1 - kleft]
                                    + f[i - 1 - left][j - 1 - kleft] : j - 1 - kleft == 0 ? 1 : 0) % mod;
                    long long addB = ((i - 1 - left) > 0 ? g[i - 1 - left][j - 1 - kleft] * f[left][kleft] : 0) % mod;
                    f[i][j] = (f[i][j] + mul * (addA + addB) % mod) % mod;
                }
                for (int kleft = 0; kleft <= j; kleft++)
                    g[i][j] = (g[i][j] + mul * f[left][kleft] % mod * f[i - 1 - left][j - kleft]) % mod;
            }
        }
    }
    G<<(f[n][m]+g[n][m])%mod<<'\n';
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll a,b;
int t;

bool win(ll a,ll b){
  if(a==0 || b==0) return 0;
  if (a>b) swap(a,b);
  if(!win(a,b%a)) return 1;
  return ((b/a)%(a+1))%2==0;
}

int main(){
  scanf("%d",&t);
  while (t--){
	scanf("%I64d%I64d",&a,&b);
	if (win(a,b)) printf("First\n");
	else printf("Second\n");
  }
  return 0;
}


#include<bits/stdc++.h>
// Library for Ordered set
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long int
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#include<time.h>
#define llu unsigned long long int
#define bs binary_search
#define pb push_back
#define f first
#define s second
#define ld long double
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define MOD (ll)(1e9+7)
#define ppb pop_back
#define max3(a,b,c) max(max(a,b),c)
#define min4(a,b,c,d) min(min3(a,b,c),d)
#define M (ll)(1e6)
#define rep(n) for(ll i=0;i<n;i++)
// To find nth smallest in an ordered set use st.find_by_order(n-1)
// To find the number of elements strictly less than k in ordered set use st.order_of_key(k)
using namespace std;
/*void build_tree(vector<ll> &tree,vector<ll> &v,ll start,ll end,ll node)
{
	if(start==end)
	tree[node]=v[start];
	else
	{
		ll mid=(start+end)/2;
		build_tree(tree,v,start,mid,2*node+1);
		build_tree(tree,v,mid+1,end,2*node+2);
		tree[node]=tree[2*node+1]+tree[2*node+2];
	}
}
ll get_sum(vector<ll> &tree,ll ss,ll se,ll qs,ll qe,ll i)
{
	if((qs)<=ss&&(qe)>=se)
		return tree[i];
	if((qe)<ss||(qs)>se)
		return 0;
	ll mid=(ss+se)/2;
	return(get_sum(tree,ss,mid,qs,qe,i*2+1)+get_sum(tree,mid+1,se,qs,qe,i*2+2));
}
void update_sum(vector<ll> &tree,ll ss,ll se,ll i,ll diff,ll si)
{
	if(i<ss||i>se)
		return;
	tree[si]=tree[si]+diff;
	if(ss!=se)
	{
		ll mid=(ss+se)/2;
		update_sum(tree,ss,mid,i,diff,2*si+1);
		update_sum(tree,mid+1,se,i,diff,2*si+2);
	}
}*/
ll find_pow(ll a,ll b)
{
	ll res=1;
	while(b>0)
	{
		if(b&1)
			res=(res*a)%MOD;
		a=(a*a)%MOD;
		b>>=1;
	}
	return res;
}
ll find_ans(ll a,ll b)
{
	ll ans=1;
	ll cnt=0,x1=a;
	while(a>0&&b>=a)
	{
		ll y1=(b/a);
		cnt+=y1;
		if(y1<x1)
			break;
		//cout<<a<<" "<<b<<" "<<y<<endl;
		//cout<<y<<endl;
		//cout<<y<<endl;
		//for(ll j=1;j<=y;j++)
			//ans=ans*x1;
		//ans=(ans*find_pow(x1,y))%MOD;
		a=a*x1;
	}
	//cout<<cnt<<endl;
	ans=find_pow(x1,cnt);
	return ans;
}
bool isPrime(ll n)
{
	if(n==1)
		return 0;
	for(ll i=2;i*i<=n;i++)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
void solve()
{
	//cout<<"Anup\n";
	ll x,n;
	cin>>x>>n;
	//cout<<n<<endl;
	vector<ll> p;
	for(ll i=1;i*i<=x;i++)
	{
		if(x%i==0)
		{
			if(isPrime(i))
			p.pb(i);
			if(x!=(i*i)&&isPrime(x/i))
				p.pb(x/i);
		}
	}
	ll ans=1;
	for(ll i=0;i<p.size();i++)
	{
		ll x1=find_ans(p[i],n);
		ans=(ans*x1)%MOD;
	}
	cout<<ans<<endl;
	p.clear();
}
int main(void)
{
	IOS;
	solve();
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main() {
    int n,at,sum=0,o=-1;
    cin>>n>>at;
    int arr[n];

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

    for(int i=0; i<n ;i++){sum=sum+arr[i];}

    if(sum+5+(10*(n-1))<=at){o=(at-sum)/5;}
    else if(sum+(10*(n-1))<=at){o=(at-sum)/5;}

    cout<<o<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ii pair<int, int>
#define il pair<int, ull>
#define pll pair<ll, ll>
#define iii pair<int , ii>
#define LOG(x) cout << x <<" ";
#define TRACE(x) cout << #x << " : " << x << endl
#define LOG2DArray(array, n, m) for ( int i = 1; i <= n ; i++) { for (int j = 1; j <= m; j++) { LOG(array[i][j]); } cout<<endl; } cout<<endl;
#define LOG1DArray(array, n)  for( int i = 1; i <= n ; i++){ cout<<i << " : ";LOG( array[i]); } cout<<endl;
#define myFor(i, l, r) for(int i = (int) l;i< (int) r;i++)
#define _ << " : " <<
#define L(a) (int)((a).size())
#define sqr(x) ((x) * (x))
#define vi vector<int>
#define vl vector<ll>
#define vul vector<ull>
#define mp make_pair
#define pub push_back
#define pob pop_back
#define vii vector <ii>
#define vll vector <pll>
#define vil vector <il>
#define viii vector <iii>
#define vs vector <string>
#define all(s) (s).begin(),(s).end()
#define allb(s,b) (s).begin()+b,(s).end()
#define allr(s) (s).rbegin(),(s).rend()
#define fore(i, l, r) for(int i = (int)l; i <= (int)r; i++)
#define forn(i, l, r) for(int i = (int)l; i >= (int)r; i--)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define cinx int x; cin>>x
#define cinc char c; cin>>c
#define cinxl ll x; cin>>x
#define scan(TYPE,VALUE) TYPE VALUE;cin>>VALUE
#define sout cout<<endl
#define nbit(x) __builtin_popcount((ull)x)
#define ret(x) return cout<<x<<endl,0

const int maxn = 300 +  5;
const int inf = (1e9) + 10;
const ld eps = 1e-12;

struct rib {
    int b, u, c, f , back;
};
int dx[] = {1 , -1 , 0 , 0};
int dy[] = {0 , 0 , 1 , -1};
vector <rib> g[maxn];
int n , m;
int s , t;
int V;
void add_rib (int a, int b, int u, int c) {
    // u = capacity
    // c = cost per flow (maybe double)
    // add edge between a and b
    rib r1 = { b, u, c, 0, (int)g[b].size() };
    rib r2 = { a, 0, -c, 0, (int)g[a].size() };
    g[a].push_back (r1);
    g[b].push_back (r2);
}
int mcmf(){
	int flow = 0,  cost = 0;
    while (flow < inf) {
        vector<int> id (V, 0);
        vector<int> d (V, inf);
        vector<int> q (V);
        vector<int> p (V);
        vector<int> p_rib (V);
        int qh=0, qt=0;
        q[qt++] = s;
        d[s] = 0;
        while (qh != qt) {
            int v = q[qh++];
            id[v] = 2;
            if (qh == V)  qh = 0;
            for (int i=0; i<g[v].size(); ++i) {
                rib & r = g[v][i];
                if (r.f < r.u && d[v] + r.c < d[r.b]) {
                    d[r.b] = d[v] + r.c;
                    if (id[r.b] == 0) {
                        q[qt++] = r.b;
                        if (qt == V)  qt = 0;
                    }
                    else if (id[r.b] == 2) {
                        if (--qh == -1)  qh = V-1;
                        q[qh] = r.b;
                    }
                    id[r.b] = 1;
                    p[r.b] = v;
                    p_rib[r.b] = i;
                }
            }
        }
        if (d[t] == inf)  break;
        int addflow = inf - flow;
        for (int v=t; v!=s; v=p[v]) {
            int pv = p[v];  int pr = p_rib[v];
            addflow = min (addflow, g[pv][pr].u - g[pv][pr].f);
        }
        for (int v=t; v!=s; v=p[v]) {
            int pv = p[v];  int pr = p_rib[v],  r = g[pv][pr].back;
            g[pv][pr].f += addflow;
            g[v][r].f -= addflow;
            cost += g[pv][pr].c * addflow;
        }
        flow += addflow;
    }
    return cost;
}

int grid[maxn][maxn];
bool valid(int i , int j){
	if (i < 1 || j < 1 || i > n || j > m)
		return false;
	return true;
}
int main()
{
	fastio;
	cin>>n >> m;
	fore(i,1,n){
		fore(j,1,m){
			cin>>grid[i][j];
		}
	}
	s = 0;
	t = n * m +1;
	V = t + 1;
	fore(i,1,n){
		fore(j,1,m){
			if ((i + j)%2){
				add_rib((i-1)*m + j , t, 1 , 0);
			}
			else{
				add_rib(s , (i-1)*m + j , 1 , 0);
				fore(k,0,3){
					int tx = i + dx[k];
					int ty = j + dy[k];
					if (!valid(tx , ty))
						continue;
					add_rib((i-1)*m + j , (tx-1)*m + ty , 1 , grid[i][j] != grid[tx][ty]);
				}
			}
		}
	}
	cout<<mcmf()<<endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:100000000")
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
const int N=100;
int a[N], d[N];
int main()
{
	int n;
	scanf("%d", &n);
	for(int i=0; i<n; scanf("%d", &a[i]), i++);
	int s=0;
	for(int i=n-1; i>=0; i--)
	{
		d[i]=max(d[i+1], a[i]+s-d[i+1]);
		s+=a[i];
	}
	printf("%d %d\n", s-d[0], d[0]);
	return 0;
}


#include<cmath>
#include<cstring>
#include<cassert>
#include<cstdlib>
#include<climits>
#include<cfloat>

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

#include<hash_set>
#include<hash_map>
using namespace __gnu_cxx;

// skrocone typy
typedef long long ll;

typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef unsigned long long ull;

// int pairs
typedef pair<int,int> pii;typedef pair<uint,uint> puiui;typedef pair<int,uint> piui;typedef pair<uint,int> puii;
// long long pairs
typedef pair< ll,ll> pllll;typedef pair<ull,ull> pullull;typedef pair<ull, ll> pullll;typedef pair<ll, ull> pllull;
// char pairs
typedef pair<char,char> pcc;typedef pair<char,uchar> pcuc;typedef pair<uchar,char> pucc;typedef pair<uchar,uchar> pucuc;
// long long x int pairs
typedef pair<int,ll> pill;typedef pair<ll,int> plli;typedef pair<uint,ll> puill;typedef pair<ll,uint> pllui;typedef pair<ull,int> pulli;typedef pair<int,ull> piull;typedef pair<uint,ull> puiull; typedef pair<ull,uint> pullui;
// long long x char pairs
typedef pair<char,ll> pcll;typedef pair<ll,char> pllc;typedef pair<uchar,ll> pucll;typedef pair<ll,uchar> plluc;typedef pair<ull,char> pullc;typedef pair<char,ull> pcull;typedef pair<uchar,ull> pucull; typedef pair<ull,uchar> pulluc;
// int x char pairs
typedef pair<char,int> pci;typedef pair<int,char> pic;typedef pair<uchar,int> puci;typedef pair<int,uchar> piuc;typedef pair<uint,char> puic;typedef pair<char,uint> pcui;typedef pair<uchar,uint> pucui; typedef pair<uint,uchar> puiuc;

#define mp make_pair
#define fi first
#define se second

#define pb push_back
#define pf push_front
#define ins insert
#define era erase

#define it iterator

#define type(x) __typeof(x)

#define foreach(itr,cont) for(__typeof(cont.begin()) itr = cont.begin(); itr != cont.end(); ++itr)
#define rep(i,n) for(int i=0; i<(n); ++i)
#define per(i,n) for(int i=(n)-1; i>=0; --i)
#define fo(i,a,b) for(int i=(a); i<(b); ++i)
#define of(i,a,b) for(int i=(b)-1; i>=(a); --i)

#define sz(c) (int)(c).size()

struct llhash {size_t operator()(ll  x)const {return (int)x ^ (int)(x>>32);}};
struct piihash{size_t operator()(pii x)const {return x.fi^x.se;}};

int x00,y00,n,d;

vector<pii> vecs;

char pos[2][2][410][410];
const int win=1;
const int loss=2;

inline int dist2(int x, int y)
{
    x-=205;
    y-=205;
    return x*x + y*y;
}

void tc()
{
    cin>>x00>>y00>>n>>d;
    vecs.resize(n);
    rep(i,n)cin>>vecs[i].fi>>vecs[i].se;

    rep(g1,2)rep(g2,2)rep(x,410)rep(y,410)if(dist2(x,y)>d*d)pos[g1][g2][x][y]=win;

    of(diag,0,819)fo(x,0,410)
    {
        int y = diag-x;
        if(y<0 || y>=410)continue;
        if(pos[1][1][x][y])continue;
        // probujemy dojsc do przegrywajacej
        bool ok = false;
        rep(i,sz(vecs))
        {
            int nx = x+vecs[i].fi;
            int ny = y+vecs[i].se;
            if(nx>=410 || ny>=410)continue;
            if(pos[1][1][nx][ny]==loss)ok=true;
        }
        pos[1][1][x][y] = ok?win:loss;
    }

    of(diag,0,819)fo(x,0,410)
    {
        int y = diag-x;
        if(y<0 || y>=410)continue;
        if(pos[1][0][x][y]==0)
        {
            // probujemy dojsc do przegrywajacej
            bool ok = false;
            rep(i,sz(vecs))
            {
                int nx = x+vecs[i].fi;
                int ny = y+vecs[i].se;
                if(nx>=410 || ny>=410)continue;
                if(pos[0][1][nx][ny]==loss)ok=true;
            }
            pos[1][0][x][y] = ok?win:loss;
        }

        if(pos[0][1][x][y]==0)
        {
            bool ok = false;
            rep(i,sz(vecs))
            {
                int nx = x+vecs[i].fi;
                int ny = y+vecs[i].se;
                if(nx>=410 || ny>=410)continue;
                if(pos[1][0][nx][ny]==loss)ok=true;
            }
            if(pos[1][1][y][x]==loss)ok=true;
            pos[0][1][x][y] = ok?win:loss;
        }
    }

    of(diag,0,819)fo(x,0,410)
    {
        int y = diag-x;
        if(y<0 || y>=410)continue;
        if(pos[0][0][x][y])continue;
        // probujemy dojsc do przegrywajacej
        bool ok = false;
        rep(i,sz(vecs))
        {
            int nx = x+vecs[i].fi;
            int ny = y+vecs[i].se;
            if(nx>=410 || ny>=410)continue;
            if(pos[0][0][nx][ny]==loss)ok=true;
        }
        if(pos[0][1][y][x]==loss)ok=true;
        pos[0][0][x][y] = ok?win:loss;
    }

    cout<<(pos[0][0][x00+205][y00+205]==win?"Anton":"Dasha")<<endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cout.precision(40);
    cout.setf(ios::fixed, ios::floatfield);
    int nz=1;
    //cin >> nz;

    for(int i=0; i<nz; ++i)
    {
        tc();
    }

    return 0;
}


#include <iostream>

using namespace std;

int y, b, r;

int main()
{
    cin >> y >> b >> r;
    if (b>=r)    b=r-1;
    if (y>=b)    y=b-1;
    if (b-1>y)   b=y+1;
    if (r-1>b)   r=b+1;
    if (y+1>b)   y=b-1;
    cout << b + y + r;
}


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

const int MAX = 1e6 + 10;
const int OO = 0x3f3f3f3f;
const double EPS = 1e-9;

#define bug(x) cout << #x << " = " << x << '\n'
#define FOR(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mt make_tuple
#define all(vetor) vetor.begin(), vetor.end()
//#define gc getchar_unlocked

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

bool isCopri(int a, int b)
{
    FOR(i, 2, min(a, b)+1)
        if(a%i == 0 and b%i == 0)
            return false;
    return true;
}

int main()
{
    int n, a, b;
    cin >> n;
    if(n & 1)
        a = n / 2, b = n - (n / 2);
    else
        a = (n / 2) - 1, b = n - ((n / 2) - 1);

    while(a > 1)
        if(isCopri(a, b))
            cout << a << ' ' << b << '\n', a = 0;
        else
            a--, b++;
    if(a)
        cout << a << ' ' << b << '\n', a = 0;

	return 0;
}
// 1513969660211


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

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  ll p, q;
  cin >> p >> q;
  if (__gcd(p,q) > 1) {
    cout << "Impossible" << endl;
    return 0;
  }
  vector<pair<char,ll>> ans;
  while (p != 1 || q != 1) {
    if (p >= q) {
      ll k = (p-1)/q;
      ans.push_back({'A',k});
      p -= q*k;
    } else {
      ll k = (q-1)/p;
      ans.push_back({'B',k});
      q -= p*k;
    }
  }
  for (auto p : ans) {
    cout << p.second << p.first;
  }
  cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
using  ll=long long;
const int N=5e3+9,mod=1e9+7;
ll lcm(ll a,ll b){
    return a*b/__gcd(a,b);
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 ll n,a,b,p,q; cin>>n>>a>>b>>p>>q;
 ll red=n/a; ll blue=n/b,in=n/lcm(a,b);
 if(p>q)
     blue-=in;
 else
     red-=in;
 cout<<red*p+blue*q;
}


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

int main()
{
    ios::sync_with_stdio(0);
    ll n,x,y,x1,y1,x2,y2,x3,y3;
    ll ans;
    multiset<pair<ll,ll> >ms;
    cin >> n;
    for(ll i=0;i<n;i++)
    {
        cin >> x >> y;
        ms.insert(make_pair(x,y));
    }
    multiset<pair<ll,ll> >::iterator c;
    if(n==1)
        cout << "-1";
    else if(n==2)
    {
        c = ms.begin();
        x1 = (*c).first;
        y1 = (*c).second;
        if(x1==x && y1==y){
            c++;
            x1 = (*c).first;
            y1 = (*c).second;
        }
        if(x1!=x && y1!=y)
        {
            ans = abs(x1-x)*abs(y1-y);
            cout << ans;
        }
        else
            cout << "-1";
    }
    else if(n==3)
    {
        c = ms.begin();
        x1 = (*c).first;
        y1 = (*c).second;
        c++;
        x2 = (*c).first;
        y2 = (*c).second;
        c++;
        x3 = (*c).first;
        y3 = (*c).second;
        if(x1!=x3 && y1!=y3)
            ans = abs(x1-x3)*abs(y1-y3);
        else if(x1!=x2 && y1!=y2)
            ans = abs(x1-x2)*abs(y1-y2);
        else if(x2!=x3 && y2!=y3)
            ans = abs(x2-x3)*abs(y2-y3);
        cout << ans;
    }
    else if(n==4)
    {
        c = ms.begin();
        x1 = (*c).first;
        y1 = (*c).second;
        c = ms.end();
        c--;
        x2 = (*c).first;
        y2 = (*c).second;
        ans = abs(x1-x2)*abs(y1-y2);
        cout << ans;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	int b,g,n;
	cin >> b;
	cin >> g;
	cin >> n;
	int ans = 0;

	for(int i = 0; i <= b; i++)
	{
		for (int j = 0; j <= g; j++)
		{
			if((i+j) == n)
				ans++;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#define SIZE(a) ((int) (a.size()))

using namespace std;

#define MAXN 6010
const int mod = 1000000007;

int n;

long long int dynamic[MAXN] , dynamic2[MAXN];

int main(){

	string sozdizisi;
	cin >> sozdizisi;

	string sozdizisi2;
	cin >> sozdizisi2;

	n = SIZE(sozdizisi2);

	long long int res=0;

	for(int i=0;i<SIZE(sozdizisi);i++)
	{
		for(int j=0;j<SIZE(sozdizisi2);j++)
		{
			if(sozdizisi2[j]==sozdizisi[i])
				dynamic2[j]=(dynamic2[j]+dynamic[j-1]+1)%mod;
			dynamic2[j+1]=(dynamic2[j+1]+dynamic2[j])%mod;
		}
		res=(res+dynamic2[n])%mod;
		for(int j=0;j<SIZE(sozdizisi2);j++)
			dynamic[j]=dynamic2[j];
		memset(dynamic2,0,sizeof(dynamic2));
	}
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m,k, ans, d, l, j;
int main (){
    scanf("%d%d%d", &n, &m, &k);
    if(n == 1) {
        printf("%d", m);
        return 0;
    }
    ans = 1;
    d = m-n;
    j = k;
    l = 1;
    while(d >= l){
        d -= l;
        ans++;
        k --;
        j++;
        if(k >= 1) l++;
        if(j <= n) l++;
        if(k < 1 && j > n) break;
    }
    ans += (d/n);
    printf("%d", ans);
    return 0;
}


// ConsoleApplication15.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

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

long long nod(long long a, long long b)
{
	while (b > 0)
	{
		long long r = a % b;
		a = b;
		b = r;
	}
	return a;

}

long long nok(long long a, long long b)
{
	return a / nod(a, b)*b;
}

int main()
{
	long long a, b;
	cin >> a >> b;
	if (b < a) swap(a, b);
	if (b%a == 0) { cout << 0; return 0; }
	long long d = b - a;
	long long kres = 1000000000,lcmr=1e18;

	for (int i = 1; i*i <= d; i++)
	{
		if (d%i == 0)
		{
			long long k = i - a % i;
			if (a%i == 0) k = 0;
			long long lcm = nok((a + k), (b + k)); // / i;
			if (lcm < lcmr)
			{
				lcmr = lcm;
				kres = k;
			}
			else
				if (lcm == lcmr && k < kres) kres = k;
			k = d/i - a % (d/i);
			if (a%(d/i) == 0) k = 0;
			lcm = nok((a + k), (b + k)); // / (d / i);
			if (lcm < lcmr)
			{
				lcmr = lcm;
				kres = k;
			}
			else
				if (lcm == lcmr && k < kres) kres = k;

		}
	}

	cout << kres;

    //std::cout << "Hello World!\n";
}

// Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
// Отладка программы: F5 или меню "Отладка" > "Запустить отладку"

// Советы по началу работы
//   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
//   2. В окне Team Explorer можно подключиться к системе управления версиями.
//   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
//   4. В окне "Список ошибок" можно просматривать ошибки.
//   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
//   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.


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

#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define MOD 1000000007ULL
#define INF 1000000000ULL
const int N = 1e5 + 5;
#define FAST ios_base::sync_with_stdio(false)

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

inline ll power(ll a, ll b, ll mod){
	ll x = 1;
	a = a%mod;
	while(b){
		if(b & 1) x = (x*a)%mod;
		a = (a*a)%mod;
		b >>= 1;
	}
	return x;
}

ll gcd(ll a, ll b){ return a?gcd(b%a,a):b;}
ll modInverse(ll a, ll mod)
{
    int m0 = mod;
    int y = 0, x = 1;

    if (mod == 1)
      return 0;

    while (a > 1)
    {
        // q is quotient
        int q = a / mod;
        int t = mod;

        // m is remainder now, process same as
        // Euclid's algo
        mod = a % mod, a = t;
        t = y;

        // Update y and x
        y = x - q * y;
        x = t;
    }

    // Make x positive
    if (x < 0)
       x += m0;

 	 return x;
}

ll m;
ll h1,h2,a1,a2,x1,x2,yy1,y2;

int main()
{
	cin>>m;
	cin>>h1>>a1;
	cin>>x1>>yy1;
	cin>>h2>>a2;
	cin>>x2>>y2;

	ll tmp = 0;
	ll tmp1 = h1;
	ll ans1=-1,ans2=-1;
	while(tmp<m+1 && tmp1 != a1){
		tmp++;
		tmp1 = (tmp1*x1 + yy1)%m;
	}
	if(tmp == m+1 && tmp1 != a1){
		cout<<"-1\n";
		return 0;
	}
	ans1 = tmp;

	tmp = 0;
	tmp1 = h2;
	while(tmp < m+1 && tmp1 != a2){
		tmp++;
		tmp1 = (tmp1*x2 + y2)%m;
	}

	if(tmp == m+1 && tmp1 != a2){
		cout<<"-1\n";
		return 0;
	}
	ans2 = tmp;

	if(ans1 == ans2)
	{
		cout<<ans1<<endl;
		return 0;
	}

	ll rep1=-1,rep2=-1;
	tmp = 1;
	tmp1 = (a1*x1 + yy1)%m;

	while(tmp<m+1 && tmp1 != a1){
		tmp++;
		tmp1 = (tmp1*x1 + yy1)%m;
	}
	if(tmp1 != a1)
		rep1 = -1;
	else
		rep1 = tmp;

	tmp = 1;
	tmp1 = (a2*x2 + y2)%m;
	while(tmp < m+1 && tmp1 != a2){
		tmp++;
		tmp1 = (tmp1*x2 + y2)%m;
	}

	if(tmp1 != a2)
		rep2 = -1;
	else
		rep2 = tmp;

	if(rep1 == -1 && rep2 == -1){
		cout<<"-1\n";
		return 0;
	}
	else if(rep1 == -1){
		ll diff = ans1-ans2;
		if(diff < 0){
			cout<<"-1\n";
			return 0;
		}
		else if(diff%rep2)
		{
			cout<<"-1\n";
			return 0;
		}
		else{
			cout<<ans1<<endl;
			return 0;
		}
	}
	else if(rep2 == -1){
		ll diff = ans2-ans1;
		if(diff < 0){
			cout<<"-1\n";
			return 0;
		}
		else if(diff%rep1)
		{
			cout<<"-1\n";
			return 0;
		}
		else{
			cout<<ans2<<endl;
			return 0;
		}
	}
	else{

		if(ans1>ans2){
			ll ch = rep2,i;
			for(i=0;i<=ch;i++){
				if( (ans1-ans2+rep1*i)%rep2 == 0)
					break;
			}
			if(i == ch+1)
			{
				cout<<"-1\n";
			}
			else{
				cout<<ans1+rep1*i<<endl;
			}
			return 0;
		}
		else{
			ll ch = rep1,i;
			for(i=0;i<=ch;i++){
				if( (ans2-ans1+rep2*i)%rep1 == 0)
					break;
			}
			if(i == ch+1)
			{
				cout<<"-1\n";
			}
			else{
				cout<<ans2+rep2*i<<endl;
			}
			return 0;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#include <ext/numeric>

using namespace std;
using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned long long ull;

void fastInOut();

const int N = 1009;
int T = 0, cst[N * N];

void build(int b, int w, int x) {
	int bb = b, tot = 0;
	while (bb != b || !T) {
		if (bb >= x)
			bb -= x;
		else
			++tot, bb += (w - x);
		cst[T++] = tot;
	}
}

ll calc(ll a, ll t) {
	return a - cst[T - 1] * (t / T) - cst[t % T];
}

ll bs(ll a, ll c) {
	if (c <= a)
		return 0;
	ll st = 1, ed = (1LL << 62);
	while (st < ed) {
		ll md = (st + ed) / 2LL;
		if (c - md > calc(a, md - 1))
			st = md + 1;
		else
			ed = md;
	}
	return st;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	fastInOut();
	int b, w, x;
	ll a, c;
	cin >> a >> b >> w >> x >> c;
	build(b, w, x), cout << bs(a, c);
	return 0;
}

void fastInOut() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL), cout.tie(NULL);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n;
long long a[9];
int s[9];
int ans=1e9;
void solve(int x,vector<long long>v,int res,bool f)
{
	if(x>n)
	{
		if(f)res++;
		ans=min(ans,res);
		return;
	}
	for(int i=0;i<v.size();i++)
	{
		if(v[i]%a[x]==0LL)
		{
			vector<long long>vt=v;
			vt[i]/=a[x];
			vt.push_back(a[x]);
			solve(x+1,vt,res+(s[x]!=1),f);
		}
	}
	vector<long long>vt=v;
	vt.push_back(a[x]);
	solve(x+1,vt,res+s[x],true);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",a+i);
	}
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	for(int i=1;i<=n;i++)
	{
		long long t=a[i];
		for(long long j=2LL;j*j<=t;j++)
		{
			while(t%j==0LL)
			{
				t/=j;
				s[i]++;
			}
		}
		if(t!=1)s[i]++;
		if(s[i]>1)s[i]++;
	}
	vector<long long>v;
	v.push_back(a[1]);
	solve(2,v,s[1],false);
	printf("%d",ans);
	return 0;
}


#pragma optimize("", on)
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int maxn = (int)2e6 + 10;
const int mod = (int)1e9 + 9;
const int inf = (1<<30) - 1;
int n, w, b;
ll fact[4040];
int c[4040][4040];
void solve() {
    cin >> n >> w >> b;
    fact[0] = 1;
    for(int i = 1; i < 4040; i++) {
        fact[i] = fact[i-1] * i % mod;
    }
    for(int i = 0; i < 4040; i++) {
        for(int j = 1; j < i; j++) {
            c[i][j] = (c[i - 1][j - 1] + c[i-1][j]) % mod;
        }
        c[i][0] = c[i][i] = 1;
    }
    ll ans = 0;
    for(int x = 1; x < n; x++) {
        int y = n - x;
        for(int k = 2; k <= 2; k++) {
            ll cur = c[w-1][x-1] *1ll* c[x - 1][k-1] % mod;
            ll cur2 = 0;
            if(k > 1) {
                cur2 = (cur2 + c[b-1][y-1] *1ll* c[y-1][k-2]) % mod;
            }
            //cout << x << " " << k << " " << cur << " "<<cur2 << "\n";
            ans =(ans + cur * cur2) % mod;
        }
    }
    ans = (ans * fact[w]) % mod;
    ans = (ans * fact[b]) % mod;

    cout << ans << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    //cin.tie(0);
    //srand(time(0));
    int t = 1;
    //scanf("%d", &t);
    //cin >> t;
    for(int i = 1; i <= t; i++) {
        //printf("Case %d: ", i);
        solve();
    }
    return 0;
}


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

int map[256];

int main(){
    char ch;
    char str1[3],str2[3];
    map['6']=1; map['7']=2; map['8']=3; map['9']=4; //用常量表简化代码
    map['T']=5; map['J']=6; map['Q']=7; map['K']=8; map['A']=9;
    while(~scanf("%c",&ch)){
        getchar();//吸收回车
        scanf("%s",str1);
        scanf("%s",str2);
        getchar();//吸收回车
        if(str1[1]==str2[1]){
            if(map[str1[0]]>map[str2[0]])
                printf("YES\n");
            else
                printf("NO\n");
        }
        else{
            if(str1[1]!=ch)
                printf("NO\n");
            else
                printf("YES\n");
        }
    }
    return 0;
}


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

typedef long long Long;

Long pow2(Long base,Long exp)
{
   if(exp*log10(base) > 12)return 10000000000000LL;
   if(exp==0)return 1;
   Long sq = pow2(base,exp>>1);
   sq = sq*sq;
   if(exp&1)sq = sq*base;
   return sq;
}

bool memo[100000][50];
bool cat[100000][50];
bool f = 0;
Long canwin(Long A,Long B,Long N)
{

   if(B==1 && A >= 70000)
   {
      return (N-A)%2 == 0;
   }
   if(A==1 && B >= 40)
   {
      f = true;
      return false;
   }
   if( pow2(A,B) >= N )return true;
   bool w = false;
   if(cat[A][B])return memo[A][B];
   if(!canwin(A+1,B,N) || !canwin(A,B+1,N))w = true;
   cat[A][B] = true;
   return memo[A][B] = w;
}

int main()
{
   memset(cat,false,sizeof(cat));
   Long A,B,N;
   cin>>A>>B>>N;
   if(canwin(A,B,N) && !f)cout<<"Masha"<<endl;
   else if(!f)cout<<"Stas"<<endl;
   else cout<<"Missing"<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sum(ll x) {
	ll res = 0;
	while(x) {
		res += x%10;
		x /= 10;
	}
	return res;
}
int main(int args, const char * argv[]) {
	ll n, s;
	cin >> n >> s;
	ll l = 1, r = 1000000000000000000ll, ans = 2000000000000000000ll;
	while(l <= r) {
		ll mid = (l+r) >> 1;
		if(abs(sum(mid)-mid) >= s) {
			r = mid-1;
			ans = mid;
		}
		else l = mid+1;
	}
	cout << max(0ll, n-ans+1) << endl;
	return 0;
}


#include <bits/stdc++.h>
int a,b,c[111];
using namespace std;
int main()
{
    cin>>a>>b;
    for(int i=0;i<a;i++)
    cin>>c[i];
    sort(c,c+a);
    int i=a-1;
    while(b%c[i]!=0)
    i--;
    cout<<b/c[i];
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<set>
#define LL long long
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define INF 0x7fffffff
#define LNF ((1LL<<62)-1)
#define mod 1000000007
#define maxn 250000

using namespace std;

LL dp[105][maxn], sum[maxn];

int main()
{
    int a,b,k,t;
    scanf("%d%d%d%d",&a,&b,&k,&t);

    dp[0][0] = 1;
    for(int i = 1; i<=t; i++)
    {
        for(int j = 0; j<maxn; j++)
            sum[j] = ( (j-1>=0?sum[j-1]:0) + dp[i-1][j] )%mod;

        for(int j = 0; j<maxn; j++)
            dp[i][j] = ( sum[j] - (j-2*k-1>=0?sum[j-2*k-1]:0) + mod )%mod;
    }

    for(int j = 0; j<maxn; j++)
            sum[j] = ( (j>0?sum[j-1]:0) + dp[t][j] )%mod;

    LL ans = 0;
    for(int i = 0; i<=2*k*t; i++)
        ans += ( 1LL*dp[t][i] * (i+a-b-1>=0?sum[i+a-b-1]:0) )%mod, ans %= mod;
    printf("%lld\n",ans);

}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <memory.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>

using namespace std;

#define Forn(i, n) for (int i = 0; i < (n); i++)
#define Fornr(i, n) for (int i = (n) - 1; i > -1; i--)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define Forr(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define fill(a, b) memset((a), (b), sizeof((a)))
#define pb push_back
#define inf 2147483647
#define F first
#define S second
#define N 1000010

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

int n;
char p[N];

ll nok(ll a, ll b)
{
    return a / __gcd(a, b) * b;
}

int main()
{
#ifdef LocalHost
    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
#endif
    scanf("%d", &n);
    ll ans = 1;
    For(i, n - 310, n)
        For(j, i, n + 1)
            For(e, j + 1, n + 1)
            if (i > 0)
            {
                ans = max(nok(e, nok(i, j)), ans);
            }
/*    fill(p, 1);
    For(i, 2, n)
        if (p[i])
            for (int j = i + i; j <= n; j += i)
                p[j] = 0;
    int k = 0;
    ll c = 1;
    Fornr(i, n + 1)
        if (p[i])
        {
            c *= i;
            k++;
            if (k == 1 && n - i > 2)
                ans = max(ans, i * 1ll * n * (n - 1) / __gcd(n, n - 1));
            if (k == 2)
                break;
        }
    Fornr(i, n + 1)
        if (!p[i])
        {
            c *= i;
            k++;
            if (k == 3)
                break;
        }*/
    cout << ans;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a, ll b){
	while(b != 0){
		a %= b;
		swap(a, b);
	}
	return a;
}

int main(){
	ll n, k; cin >> n >> k;
	ll multiple = n*k;
	ll x, y; cin >> x >> y;
	ll mn = 100000000000, mx = 0;

	for(ll i = abs(y - x); i <= multiple; i+= k){
		if(i == 0)
			continue;
		ll value = gcd(multiple, i);
		mn = min(mn, value);
		mx = max(mx, value);
	}
	for(ll i = k - y - x; i <= multiple; i+= k){
		if(i == 0)
			continue;
		ll value = gcd(multiple, i);
		mn = min(mn, value);
		mx = max(mx, value);
	}
	cout << multiple/mx << " " << multiple/mn << endl;
}


/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Tarikul Islam
            Department of CSE, City University, Bangladesh.

***/

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

#define sf          scanf
#define pf          printf
#define ssf         sscanf
#define spf         sprintf
#define fsf         fscanf
#define fpf         fprintf
#define fast        ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase       sf ("%d",&tc)
#define sn          sf ("%d",&n)
#define whilecase   while (tc--)
#define eof         while (cin >> n)
#define forcase     for(kase=1; kase<=tc; kase++)
#define arrayloop   for(i=0; i<n; i++)
#define cinstr      cin >> str
#define getstr      getline (cin,str)
#define pcase       pf ("Case %d: ",kase)
#define pii         pair <int,int>
#define pb          push_back
#define in          insert
#define llu         unsigned long long
#define lld         long long
#define U           unsigned int
#define endl        "\n"
#define TarikulCSE  int main()
#define signoff     return 0

TarikulCSE
{

    /*
        freopen ("input.txt","r",stdin);
        freopen ("output.txt","w",stdout);

    */

    lld n;

    cin>>n;

    if(n%2 == 0)
    {
        if((n/2)%2 == 0)
            cout<<"0"<<endl;
        else
            cout<<"1"<<endl;
    }
    else
    {
        lld result = ((n*(n+1))/2)%2;
        cout<<result<<endl;

    }

    signoff;
}


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

int a, b, n, mod = 1000000007;

int mas[1000], sz = 0;
int cnk[1000006];

int binpow(int a, int k)
{
    int ans = 1;
    while(k)
    {
        if(k & 1)
            ans = (1ll*ans*a)%mod;
        a = (1ll*a*a)%mod;
        k /= 2;
    }
    return ans;
}

int main()
{
    cin >> a >> b >> n;
    int s = b*n;

    for(int i = 1; i <= 7; ++i)
    {
        for(int j = (1 << i)-1; j >= 0; --j)
        {
            int num = 0;
            for(int k = 0; k < i; ++k)
            {
                num *= 10;
                num += (j & (1 << k)) ? a : b;
            }
            if(num <= s)
                mas[sz++] = num;
        }
    }

    cnk[0] = 1;
    cnk[1] = n;
    long long tm = n;
    for(int i = 2; i <= n; ++i)
    {
        tm = (tm*(n-i+1))%mod;
        tm = (tm*binpow(i, mod-2))%mod;
        cnk[i] = tm;
    }

    long long ans = 0;
    for(int i = 0; i < sz; ++i)
    {
        if((mas[i]-n*b) % (a-b) == 0)
        {
            int q = (mas[i]-n*b) / (a-b);
            if(q >= 0 && q <= n)
            {
                ans += cnk[q];
                ans %= mod;
            }
        }
    }

    cout << ans << endl;

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int main()
{
    char sum[3][100],s1[100],s2[100],s3[100];
    strcpy(sum[0],"qwertyuiop");
    strcpy(sum[1],"asdfghjkl;");
    strcpy(sum[2],"zxcvbnm,./");
    char m,s[1000],b[1000];
    while(cin>>m)
    {
        getchar();
        gets(s);
        memset(b,'\0',sizeof(b));
        if(m=='R')
        {
            for(int i=0;i<3;i++)
            {
                for(int j=0;sum[i][j]!='\0';j++)
                {
                    for(int k=0;s[k]!='\0';k++)
                    {
                        if(s[k]==sum[i][j])b[k]=sum[i][j-1];
                    }
                }
            }
        }
        else if(m=='L')
        {
            for(int i=0;i<3;i++)
            {
                for(int j=0;sum[i][j]!='\0';j++)
                {
                    for(int k=0;s[k]!='\0';k++)
                    {
                        if(s[k]==sum[i][j])b[k]=sum[i][j+1];

                    }
                }
            }
        }
        puts(b);

    }
    return 0;

}


// Wanna Hack? GL...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

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

const int maxn=1e5+10,mod=1e9+7;
const ll inf=1e18;

int n;
set<int>st;

int lca(int a,int b){
    if(__builtin_clz(a)>__builtin_clz(b)) swap(a,b);
    while(__builtin_clz(a)<__builtin_clz(b)) a>>=1;
    while(a!=b) a>>=1,b>>=1;
    return a;
}
inline bool papa(int a,int b){// a!=b
    if(b<=a) return 0;
    int dif=__builtin_clz(a)-__builtin_clz(b);
    return a==(b>>dif);
}
int Size(int a){
    int x=1,ans=0;
    while(a<=n){
	ans+=min(x,n-a+1);
	x<<=1,a<<=1;
    }
    return ans;
}
int Size(int a,set<int>&s){
    int ans=Size(a);
    vector<int>vv;
    for(int x:s){
	if(x<a) continue;
	bool bl1=papa(a,x),bl2=0;
	for(int y:vv) bl2|=papa(y,x);
	if(bl1 && !bl2) vv.PB(x),ans-=Size(x);
    }
    return ans;
}
int solve(int a,int b){
    set<int>st2;
    int ans=0;
    for(int A=a;A>0;A>>=1){
	st2=st;
	for(int B=b;B>0;B>>=1){
	    ans=(1ll*ans+1ll*Size(A,st2)*Size(B,st2))%mod;
	    if(st2.count(B>>1)) break;
	    st2.insert(B>>1);
	}
	if(st.count(A>>1)) break;
	st.insert(A>>1);
    }
    return ans;
}

int CNT=0;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    int m;cin>>n>>m;
    vector<pii>vv(m),vv2;
    for(int i=0;i<m;i++){
	cin>>vv[i].F>>vv[i].S;
    }
    ll ans=(1ll*n*n)%mod;
    for(int msk=1;msk<(1<<m);msk++){
	vv2.clear();
	for(int i=0;i<m;i++){
	    if(bit(msk,i))
		vv2.PB(vv[i]);
	}
	vector<int>perm(sz(vv2));
	for(int i=0;i<sz(vv2);i++)
	    perm[i]=i;
	do{
	    vector<pii>vv3(sz(vv2));
	    for(int i=0;i<sz(vv2);i++)
		vv3[i]=vv2[perm[i]];
	    //	    for(pii pp:vv3) cout<<pp.F<<" "<<pp.S<<" || ";
	    //	    cout<<endl;

	    int lst=-1,frst=-1;
	    for(int msk2=0;msk2<(1<<sz(vv2));msk2++){
		st.clear();
		frst=vv3[0].F,lst=vv3[0].S;
		if(bit(msk2,0)) swap(frst,lst);
		st.insert(frst);

		//		cout<<"START    "<<frst<<" "<<lst<<" ";

		for(int i=1;i<sz(vv3);i++){
		    pii p=vv3[i];
		    if(bit(msk2,i)) swap(p.F,p.S);

		    //		    cout<<p.F<<" "<<p.S<<" ";

		    int A=lst,B=p.F,C=lca(A,B);
		    while(A!=C){
			if(st.count(A)) goto Hell;
			st.insert(A);
			A>>=1;
		    }
		    while(B!=C){
			if(st.count(B)) goto Hell;
			st.insert(B);
			B>>=1;
		    }
		    if(st.count(C)) goto Hell;
		    st.insert(C);

		    lst=p.S;
		}
		if(st.count(lst))
		    goto Hell;
		st.insert(lst);
		ans=(ans+solve(frst,lst))%mod;
	    Hell:;
	    }
	}while(next_permutation(perm.begin(),perm.end()));
    }
    return cout<<ans<<endl,0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    string str;
     bool f=true;
    for (int i = 0; i < 8; i++)
    {
        cin >> str;
        for (int j = 1; j < str.length(); j++)
        {
            if (str[j] == str[j-1])
            {
                f = false;
            }
        }
    }
    if (f)
        cout<< "YES";

    else
        cout<< "NO";

        return 0 ;

}


#include <vector>
#include<iostream>
#include<cstring>
#include <queue>
#include <utility>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#define pb push_back
#define mp make_pair
#define MAX 55
#define MAXN 2005
#define ll int
#define pii pair<ll,ll>
#define piii pair<ll,pair<ll,ll> >

using namespace std;

const ll mod = 1e9 + 7;

ll n,s,k;

ll inp[MAX];

string str;

vector<piii > arr;

ll dp[MAX][MAXN][MAX];

ll brr[MAX];

ll solve(ll idx,ll rem,ll prev)
{
    if(rem <= 0)
        return 0;
    if(idx == n + 1)
        return 1e9;
    if(dp[idx][rem][prev] != -1)
        return dp[idx][rem][prev];
    ll ans = 1e9;
    ans = min(ans,solve(idx + 1,rem,prev));
    if(arr[idx].first > arr[prev].first && arr[idx].second.second != arr[prev].second.second)
        ans = min(ans,solve(idx + 1,rem - arr[idx].first ,idx) + abs(arr[idx].second.first - arr[prev].second.first));
    return dp[idx][rem][prev] = ans;
}

int main()
{
    cin>>n>>s>>k;
    ll i,j;
    for(i = 0;i < n;i++)
        cin>>inp[i];
    cin>>str;
    ll len = str.length();
    arr.pb(mp(0,mp(s,0)));
    for(i = 0;i < len;i++)
    {
        if(str[i] == 'R')
            brr[i] = 1;
        else if(str[i] == 'G')
            brr[i] = 2;
        else
            brr[i] = 3;
        arr.pb(mp(inp[i],mp(i + 1,brr[i])));
    }
    sort(arr.begin(),arr.end());
        //for(i = 0;i < arr.size();i++)
        //cout<<arr[i].first<<" "<<arr[i].second.first<<" "<<arr[i].second.second<<endl;;
    memset(dp,-1,sizeof(dp));
    ll ans = solve(1,k,0);
    if(ans >= 1e9)
        cout<<-1<<endl;
    else
        cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#define PI acos(-1.0)
using namespace std;
const int N = 1000;
typedef long long ll;
int n,m;//coffee数量，作业数
int a[N];
bool cmp(int x,int y){
	return x > y;
}
bool check(int x){
	//时间偏大返回true
	ll summ = 0;
	int t = 0;
	for(int i = 0;i<n;i++){
		if(a[i] - t>0)
			summ += a[i] - t;
		else break;
//思考一下为什么这里用我写的就过不了！！！！！！！！！！！！！
// 在使用函数对数据进行操作得到时候一定要注意，千万不要改变某个值！！！
//		summ += a[i] - t;
		if((i+1)%x==0) t++;
//		if(a[i] - t <= 0) break;
	}
	 return summ >= m;
}
int main(){
	ll sum = 0;
	cin>>n>>m;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		sum += a[i];
	}
	if(sum < m){
		cout<<-1<<endl;
		return 0;
	}
	else if(sum == m){
		cout<<n<<endl;
		return 0;
	}
	sort(a,a+n,cmp);
	int l = 1, r = n + 1,mid;//左闭右开
	while(l < r){
		mid = (l + r - 1) >> 1;
		if(check(mid)) r = mid;
		else l = mid + 1;
	}
	cout<<r<<endl;
	return 0;
}


/*
 * Created by: Son Dinh
 */
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sqr(x) (x)*(x)
#define sz(X) (int)(X).size()
#define all(x) x.begin(),x.end()
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int mod = 1e9+7;

string s,t;
int res,l[200005],r[200005];
int32_t main(){
    cin >> s >> t;
    int n = sz(s), m = sz(t), len = 0;
    for(int i = 0; i < n && len < m; ++i){
        if(s[i] == t[len]){
            l[len++] = i;
        }
    }
    len = m - 1;
    for(int i = n-1; i >= 0 && len >= 0; --i){
        if(s[i] == t[len]){
            r[len--] = i;
        }
    }
    res = max(max(0LL,r[0]), n - l[m-1] - 1);
    for(int i = 1; i < m; ++i){
        res = max(res, r[i] - l[i-1] - 1);
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
bool judge(int x)
{
    int base=1;
    bool flg=true;
    for (int i = 2; i*i <= x; ++i) {
        if(x%i==0) {
            base=i;
            flg=false;
            break;
        }
    }
    if(flg)
        return true;
    else return base>n;
}
int main()
{
    while(cin>>n>>m)
    {
        int num=0;
        int ans=-1;
        for (int i = m; i > n; --i) {
            if(judge(i))
            {
                ans=i;
                break;
            }
            num++;
            if(num>456)
            {
                break;
            }
        }
        cout<<ans<<endl;

    }
}


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

int main()
{
    int a, b, c, k;
    cin >> a >> b >> c >> k;

    while (a + b + c - 3 > k) {
        if (a >= b && a >= c)
            a--;
        else if (b >= a && b >= c)
            b--;
        else
            c--;
    }

    cout << 1ll * a * b * c << '\n';
    return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
using namespace std;
#define MOD 1000000007
int main(){
    int n;
    cin >> n;
    long long a = 0, b = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
        if (i % 2 == 0) {
            ans = (ans + a + 1) % MOD;
            b = (a + b + 1) % MOD;
        }
        else {
            ans = (ans + b + 1) % MOD;
            a = (a + b + 1) % MOD;
        }
    }
    cout << ans << endl;
}


#include <algorithm>
// #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double db;
const int oo=2139063143;
const int N=210;
const int P=1000000007;
const db eps=1e-7;
#define pritnf printf
//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T>
inline void sc (T &x)
{
    x=0; static int p; p=1; static char c; c=getchar();
    while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }
    while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }
    x*=p;
}
template <typename T>
inline void print (T x)
{
    if (x< 0) putchar('-'),x=-x;
    if (x>=10) print(x/10);
    putchar(x%10+'0');
}
template <typename T>
inline void pr (T x) { print(x),putchar('\n'); }
template <typename T1,typename T2>
void chkmax (T1 &A,T2 B) { A=A>=B?A:B; }
template <typename T1,typename T2>
void chkmin (T1 &A,T2 B) { A=A<=B?A:B; }
LL f[N][N][2];
LL calc (int a,int b,LL s,int c)//s=p+q,c=cntp+cntq
{
    int m=max (max (a-1,b),(int)log2 (s)+1);
    for (int i=0; i<=m; i++)
        for (int j=0; j<=c; j++)
            f[i][j][0]=f[i][j][1]=0;
    for (int x=0; x<=1; x++)
        for (int y=0; y<=1; y++)
        {
            if (b==1&&!y) continue;
            if (((x+y)&1)!=(s&1)) continue;
            if (a<=1&&x) continue;
            if (b<=0&&y) continue;
            ++f[0][x+y][x&&y];
        }
    for (int i=0; i< m; i++)
    {
        LL d=s>>(i+1)&1;
        for (int j=0; j<=c; j++)
            for (int t=0; t<=1; t++) if (f[i][j][t])
                for (int x=0; x<=1; x++)
                    for (int y=0; y<=1; y++)
                    {
                        int g=x+y+t;
                        if (i+1==b-1&&!y) continue;
                        if ((g&1)!=d) continue;
                        if (i+1> a-2&&x) continue;
                        if (i+1> b-1&&y) continue;
                        f[i+1][j+x+y][g>=2]+=f[i][j][t];
                    }
    }
    return f[m][c][0];
}
LL calc (LL x)
{
    LL ans=x<<1;
    while (x) ans-=x&1,x>>=1;
    return ans;
}
int a[N],b[N];
LL lca (LL x,LL y)
{
    int n=0,m=0;
    while (x) a[n++]=x&1,x>>=1; reverse (a,a+n);
    while (y) b[m++]=y&1,y>>=1; reverse (b,b+m);
    chkmin (n,m);
    LL z=0;
    for (int i=0; i< n; i++)
        if (a[i]==b[i]) z=z<<1|a[i];
        else break;
    return z;
}
int main ()
{
    // freopen (".in","r",stdin);
    // freopen (".out","w",stdout);
    LL s; sc(s);
    LL ans=0;
    for (int a=0; a< 50; a++)
        for (int b=0; b< 50; b++)
        {
            LL v=((1LL<<(a+1))+(1LL<<(b+1))-3),z=s/v;
            if (!z) continue;
            if ((int)log2 (z)+1+max (a,b)> 50) continue;
            LL k=s-v*z;
            for (int c=0; c<=a+b; c++)//cntp+cntq
                if (!((k+c)&1)) ans+=calc (a,b,(k+c)>>1,c);
        }
    pr(ans);

    return 0;
}


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

int main(){
    long long a,b,x,y;

    cin>>a>>b>>x>>y;

    long long g=__gcd(x,y);

    x=x/g;
    y=y/g;

    long long r=a%x;
    a=a-r;

    while(1){
        if(a<=0){
            cout<<0<<" "<<0<<endl;
            break;
        }

        long long temp=(a/x)*y;

        if(temp<=b&&temp>0){
            cout<<a<<" "<<temp<<endl;
            break;
        }

        else if(temp<=0){
            cout<<0<<" "<<0<<endl;
            break;
        }

        else{
            a=(b*x)/y;
            a=a-(a%x);
        }

        }

    return 0;
}


/*qrev*/
#include <iostream>
#include <string>
using namespace std;
string s;
int dp[110] = { 0 };
int state[110] = { 0 };
int cur = 0;
int main(int argc, const char * argv[]) {
	cin >> s;
	int length = s.size();
	if (length == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	for (int i = 0; i<length; i++)
	{
		if (s[i] == 'V'&&s[i + 1] == 'K')
		{
			state[i] = state[i + 1] = 3;
			i++;
			cur++;
		}
		else if (s[i] == 'V')
			state[i] = 1;
		else  if (s[i] == 'K')
			state[i] = 2;
	}
	int flag = 0;
	for (int i = 0; i<length; i++)
	{
		if ((state[i] == state[i + 1]) && state[i] != 3)
			flag = 1;
	}
	cout << cur + flag << endl;
	return 0;
}