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

inline int read()
{
	int x=0,f=1,c=getchar();
	while(c<48) c=='-'&&(f=-1),c=getchar();
	while(c>47) x=x*10+c-'0',c=getchar();
	return x*f;
}

typedef long long ll;
const int MAXN = 1005;
const int mod = 12345;
struct node {char c; int k;} a[MAXN];
int chk[MAXN],vis[MAXN];
ll n,m,ans,r=1;

struct Matrix
{
	int s[155][155];
	Matrix() {memset(s,0,sizeof(s));}
	int *operator[](int i) {return s[i];}
} mat,Q;

Matrix operator *(Matrix a,Matrix b)
{
	Matrix c;
	for(int i=0; i<r; ++i)
		for(int j=0; j<r; ++j)
			for(int l=0; l<r; ++l)
				c[i][j]=(c[i][j]+1ll*a[i][l]*b[l][j])%mod;
	return c;
}

Matrix qpow(Matrix x,ll k)
{
	Matrix res;
	for(int i=0; i<r; ++i) res[i][i]=1;
	for(ll i=k; i; i>>=1,x=x*x)
		if(i&1) res=res*x;
	return res;
}

void dfs1(int p,char c,int x,int y)
{
	if(p>m) return ++Q[x][y],void();
	for(int i=0; i<a[p].k; ++i)
		dfs1(p+1,c,x*a[p].k+i,y*a[p].k+(i+(a[p].c==c))%a[p].k);
}

void dfs2(int p,int st)
{
	if(p>m)
	{
		int flag=1;
		for(char i='A'; i<='Z'; ++i)
			if(vis[i]&&!chk[i]) {flag=0; break;}
		if(flag) ans=(ans+mat[0][st])%mod; return;
	}
	int o=chk[a[p].c];
	for(int i=1; i<a[p].k; ++i) dfs2(p+1,st*a[p].k+i);
	chk[a[p].c]=1,dfs2(p+1,st*a[p].k),chk[a[p].c]=o;
}

int main(int argc, char const *argv[])
{
	n=read(),m=read();
	if(n==0) return puts("1"),0;
	auto getc=[](char c=0) {return cin>>c,c;};
	for(int i=1; i<=m; ++i)
		vis[a[i].c=getc()]=1,r*=(a[i].k=read());
	for(char i='A'; i<='Z'; ++i)
		if(vis[i]) dfs1(1,i,0,0);
	mat[0][0]=1; mat=mat*qpow(Q,n);
	dfs2(1,0); printf("%d\n",ans);
	return 0;
}


using namespace std;
#include <bits/stdc++.h>
#define frr(i, r, l) for(int i = r; i >= l; --i)
#define fr(i, l, r) for(int i = l; i < r; ++i)
#define forit(it, type, var) for(type::iterator it = var.begin(); it != var.end(); it++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define MEMS(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define ll long long
#define ld long double
typedef pair<ll, ll> iPair;
priority_queue< ll, vector <ll> , greater<ll>>pq1,pq2;
#define maxN 1000000000000000000
ll power(ll a,ll b){ll res=1;while(b>0){res*=a;b--;}return res;}
int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll rc,bc,i,r[200005],b[200005];
    cin>>rc>>bc;
    fr(i,0,r)
    {
        cin>>r[i];
    }
    fr(i,0,b)
    {
        cin>>b[i];
    }
    if(r>b)
    cout<<"NO"<<endl;
    else
    cout<<"YES"<<endl;


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

using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!

using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

#define tcT template<class T
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;

// pairs
#define mp make_pair
#define f first
#define s second

// vectors
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const int IINF = 1e9;
const ld PI = acos((ld)-1);
const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

// helper funcs
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down

tcT> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

#define tcTU tcT, class U
tcTU> T fstTrue(T lo, T hi, U f) {
	hi ++; assert(lo <= hi); // assuming f is increasing
	while (lo < hi) { // find first index such that f is true
		T mid = lo+(hi-lo)/2;
		f(mid) ? hi = mid : lo = mid+1;
	}
	return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
	lo --; assert(lo <= hi); // assuming f is decreasing
	while (lo < hi) { // find first index such that f is true
		T mid = lo+(hi-lo+1)/2;
		f(mid) ? lo = mid : hi = mid-1;
	}
	return lo;
}
tcT> void remDup(vector<T>& v) { // sort and remove duplicates
	sort(all(v)); v.erase(unique(all(v)),end(v)); }
tcTU> void erase(T& t, const U& u) { // don't erase
	auto it = t.find(u); assert(it != end(t));
	t.erase(u); } // element that doesn't exist from (multi)set

// INPUT
#define tcTUU tcT, class ...U
tcT> void re(complex<T>& c);
tcTU> void re(pair<T,U>& p);
tcT> void re(vector<T>& v);
tcT, size_t SZ> void re(AR<T,SZ>& a);

tcT> void re(T& x) { cin >> x; }
void re(db& d) { str t; re(t); d = stod(t); }
void re(ld& d) { str t; re(t); d = stold(t); }
tcTUU> void re(T& t, U&... u) { re(t); re(u...); }

tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; }
tcTU> void re(pair<T,U>& p) { re(p.f,p.s); }
tcT> void re(vector<T>& x) { trav(a,x) re(a); }
tcT, size_t SZ> void re(AR<T,SZ>& x) { trav(a,x) re(a); }

// TO_STRING
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
	#ifdef LOCAL
		return b ? "true" : "false";
	#else
		return ts((int)b);
	#endif
}
tcT> str ts(complex<T> c) {
	stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) {
	str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
	res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
	str res = ""; F0R(i,SZ) res += char('0'+b[i]);
	return res; }
tcTU> str ts(pair<T,U> p);
tcT> str ts(T v) { // containers with begin(), end()
	#ifdef LOCAL
		bool fst = 1; str res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; str res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;

	#endif
}
tcTU> str ts(pair<T,U> p) {
	#ifdef LOCAL
		return "("+ts(p.f)+", "+ts(p.s)+")";
	#else
		return ts(p.f)+" "+ts(p.s);
	#endif
}

// OUTPUT
tcT> void pr(T x) { cout << ts(x); }
tcTUU> void pr(const T& t, const U&... u) {
	pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
tcTUU> void ps(const T& t, const U&... u) {
	pr(t); if (sizeof...(u)) pr(" "); ps(u...); }

// DEBUG
void DBG() { cerr << "]" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
	cerr << ts(t); if (sizeof...(u)) cerr << ", ";
	DBG(u...); }
#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
	#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
	#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
		 << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
	#define dbg(...) 0
	#define chk(...) 0
#endif

// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(str s = "") {
	unsyncIO();
	// cin.exceptions(cin.failbit);
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

template<int SZ> struct Sieve {
	vi pr;
	int sp[SZ]; // smallest prime that divides
	Sieve() { // above is faster
		memset(sp,0,sizeof sp);
		FOR(i,2,SZ) {
			if (sp[i] == 0) sp[i] = i, pr.pb(i);
			trav(p,pr) {
				if (p > sp[i] || i*p >= SZ) break;
				sp[i*p] = p;
			}
		}
	}
};
Sieve<2000010> S;

ll exp(ll a, int b){
	if(b == 0)
		return 1;
	ll p = exp(a, b / 2);
	p = (p * p) % MOD;
	if(b & 1)
		p = (p * a) % MOD;
	return p;
}

int main(){
	setIO();
	int n;
	cin >> n;
	vector<bool> asp(n);
	map<int, pi> m;
	vi p(n);
	F0R(a, n)
		cin >> p[a];
	sort(all(p), [](const int& a, const int& b){return a > b;});
	F0R(a, n){
		if(m[p[a]] == mp(0, 0))
			m[p[a]] = {1, 1}, asp[a] = true;
		else{
		int q = p[a] - 1;
		map<int, int> M;
		while(q != 1){
			M[S.sp[q]]++;
			q /= S.sp[q];
		}
		trav(b, M){
			auto temp = m[b.f];
			if(temp.f == b.s)
				m[b.f] = {temp.f, temp.s + 1};
			else if(temp.f < b.s)
				m[b.f] = {b.s, 1};
		}}
	}
	bool add = false;
	F0R(a, n){
		if(asp[a]){
			if(m[p[a]].f > 1 || m[p[a]].s > 1)
				add = true;
		}
		else{
			bool good = true;
			int q = p[a] - 1;
			else{
			map<int, int> M;
			while(q != 1){
				M[S.sp[q]]++;
				q /= S.sp[q];
			}
			trav(b, M){
				auto temp = m[b.f];
				if(!(temp.f > b.s || temp.s > 1))
					good = false;
			}
			if(good)
				add = true;
			}
		}
	}
	ll lcm = 1;
	trav(a, m)
		lcm = (lcm * exp(a.f, a.s.f)) % MOD;
	cout << (lcm + add) % MOD << endl;
	// you should actually read the stuff at the bottom
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/


#include "iostream"
#include <stdlib.h>
#include <math.h>
using namespace std;
int Compute(int num)
{
    int sum=0;

    while (num=!0)
    {
        sum+=num%10;
        num=num/10;
    }
    return sum;
}
bool Check(int num)
{
    int digit;
    while (num=!0)
    {
        digit=num%10;
        if (digit!=4 || digit!=7)
        {
            return false;
        }
        num=num/10;
    }
    return true;
}
int main ()
{
    int n;
    int num,num1,num2;
    int sum1=0,sum2=0;
    int divided=1;
    cin >> n;
    cin >> num;

    if (Check(num))
    {
        cout << "NO" << endl;
        return 0;
    }
    int j=n/2;
    for (int k=0;k<j;++k)
    {
        divided*=10;
    }
    num1=num/divided;
    num2=num%divided;
    if (Compute(num1)==Compute(num2))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
    return 0;

}


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

//void dfs(vector<vector<int>> &vec, vector<int> &sign, int i, int total,int ll,int step);
//int l_sum = 0;
//int main()
//{
//	int total, step,sum=0;
//	cin >> total >> step;
//	vector<vector<int>> vec(total,vector<int>(total,0));
//	int c1, c2;
//	while (cin >> c1 >> c2)
//	{
//		vec[c1-1][c2-1] = 1;
//		vec[c2-1][c1-1] = 1;
//	}
//	vector<int> sign(total, 1);
//	for (int i = 0; i < total; i++)
//	{
//		for (auto &itm : sign)
//			itm = 1;
//		dfs(vec, sign, i,total,0,step);
//		sum += l_sum;
//		l_sum = 0;
//	}
//	cout << sum / 2 << endl;
//	return 0;
//}
//void dfs(vector<vector<int>> &vec, vector<int> &sign, int i,int total,int ll,int step)
//{
//	if (sign[i] != 0)
//	{
//		sign[i] = 0;
//		l_sum += (ll%step == 0 ? ll / step : ll / step + 1);
//	}
//
//	for (int j = 0; j < total;j++)
//	{
//		if (vec[i][j]&&sign[j])
//		{
//			dfs(vec, sign, j, total, ll+1,step);
//		}
//	}
//}

//int main()
//{
//	int n, a, b;
//	cin >> n >> a >> b;
//	int nn = n;
//	int all = 0,count=1;
//	while (nn>>=1)
//		all++;
//	if (a > b){
//		int t;
//		t = a;
//		a = b;
//		b = t;
//	}
//
//	while (1)
//	{
//		if (a-b==-1)
//			break;
//		count++;
//		if (a % 2 == 0)
//			a /= 2;
//		else
//			a = (a + 1) / 2;
//		if (b % 2 == 0)
//			b /= 2;
//		else
//			b = (b + 1) / 2;
//	}
//
//	if (a!=1)
//	{
//		while (1)
//		{
//			if (a % 2 == 1)
//			{
//				cout << count << endl;
//				return 0;
//			}
//			else
//			{
//				if (a == 2)
//				{
//					count = count + 1;
//					break;
//				}
//				count++;
//				a /= 2;
//				b = (b + 1) / 2;
//			}
//		}
//	}
//	if (count == all)
//		cout << "Final!" << endl;
//	else
//		cout << count << endl;
//
//	return 0;
//}

int main(){
	int n, a, b, r = 1;
	cin >> n >> a >> b;
	if (a > b) swap(a, b);
	if (a == 1 && b == 2) {
		if (n == 2) cout << "Final!" << endl;
		else cout << r << endl;
		return 1;
	}

	while (1) {
		r++; n = n / 2;
		if (a % 2) a = a / 2 + 1;
		else a = a / 2;
		if (b % 2) b = b / 2 + 1;
		else b = b / 2;
		if (a + 1 == b) {
			if (n == 2) cout << "Final!" << endl;
			else cout << r << endl;
			return 1;
		}
	}

	return 0;
}


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

int main(){
    int n;
    int finger_num;
    int finger_sum;
    scanf("%d",&n);
    finger_sum=0;
    for(int i=0 ; i<n ; i++){
        scanf("%d",&finger_num);
        finger_sum=finger_sum+finger_num;
    }
    int r;
    int finger_dima;
    r=finger_sum%(n+1);
    if(n-r>2){
        finger_dima=1;
    }
    else{
        finger_dima=n-r+3;
    }
    printf("%d\n",finger_dima);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
char s[7][15]={"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
int main()
{
    char a[2][15];
    int b[2];
    int i,j;
    cin>>a[0];
    cin>>a[1];
    for( j=0;j<2;j++)
    for( i=0;i<7;i++){
        if(strcmp(a[j],s[i]))
            b[j]=i+1;
    }

    if(b[0]==b[1]||(b[0]+2)%7==b[1]%7||(b[0]+3)%7==b[1]%7)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
}


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
	t:
	int x,artsay=0,eksay=0;
	cin>>x;
	char d[x];
	for(int c=0;c<x;c++)
	{
		cin>>d[c];
	}
	for(int a=0;a<x;a++)
	{
		if(d[a]=='+')
		{
			artsay++;
		}
		else
		{
			eksay++;
		}
	}
	if(d[0]=='+')
	{
		if(artsay==eksay || eksay>artsay)
		{
			cout<<0;
		}
		else{
			cout<<artsay-eksay;
		}
	}
	else
	{
		cout<<artsay;
	}
	goto t;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint k;
int m;
static const int MF=31;
string fib[MF+5];
int table[100005];
void maketable(string& s){
	int n=key.size();
	table[0]=-1;
	key+='$';
	for(int i=1;i<=n;++i){
		int j=table[i-1];
		while(j>=0 && key[j+1]!=key[i]) j=table[j];
		if(key[j+1]==key[i]) ++j;
		table[i]=j;
	}
}

int calc(string& key,string& s){
	if(s.size()<key.size()) return 0;
	int j=-1;
	int res=0;
	REP(i,s.size()){
		while(j>=0 && key[j+1]!=s[i]) j=table[j];
		if(key[j+1]==s[i]) ++j;
		if(j==n-1) ++res;
	}
	key.resize(key.size()-1);
	return res;
}

lint BASE[4][4]={
	{1,1,1,0},
	{1,2,1,1},
	{0,0,1,0},
	{0,0,0,1}
};
typedef lint item ;//use as :typedef lint item;
typedef vector<item> vec;
typedef vector<vector<item> > mat;
const lint mod=1000000007;

mat tmp(4,vec(4)),tmp2(4,vec(4));
void matpow(mat& a,const mat& b){
	int n=a.size();
	REP(i,n) REP(j,n){
		tmp[i][j]=0;
		REP(k,n){
			tmp[i][j]+=a[i][k]*b[k][j];
			tmp[i][j]%=mod;
		 }
	}
	a=tmp;
}

void mpow(mat& a,lint k){
	int n=a.size();
	REP(i,n) REP(j,n) tmp2[i][j]=0;
	REP(i,n) tmp2[i][i]=1;
	while(k){
		if(k&1) matpow(tmp2,a);
		matpow(a,a);
		k>>=1;
	}
	a=tmp2;
}

lint solve(string& s){

	int ind=0;
	while(fib[ind].size()<=s.size()) ++ind;
	maketable(s);
	if(ind>k) return calc(s,fib[k]);
	int a=calc(s,fib[ind]),b=calc(s,fib[ind+1]),c=calc(s,fib[ind+2]),d=calc(s,fib[ind+3]);

	int addA=c-a-b,addB=d-b-c;

	lint rest=k-ind;
	lint odd=(rest&1);
	rest=rest/2;

	mat M(4,vec(4));
	REP(i,4) REP(j,4) M[i][j]=BASE[i][j];

	mpow(M,rest);
	vec init(4);
	init[0]=a;init[1]=b;init[2]=addA;init[3]=addB;
	lint res=0;
	if(odd){
		REP(i,4) res+=M[1][i]*init[i]%mod;
		res%=mod;
	}else{
		REP(i,4) res+=M[0][i]*init[i]%mod;
		res%=mod;
	}
	return res;
}
int main(){
	fib[1]="a";fib[2]="b";
	for(int i=3;i<MF;++i) fib[i]=fib[i-1]+fib[i-2];

	cin>>k>>m;
	string s;
	REP(hoge,m){
		cin>>s;
		solve(s);
		cout<<solve(s)<<endl;
	}

	return 0;
}


#include <iostream>

using namespace std;

int a,b,c;
int x,y,z;
int nr1,nr2;

int main()
{
    cin>>a>>b>>c;
    cin>>x>>y>>z;

    if(a>x)
    {
        nr1+=x;
        a-=x;
    }
    if(b>y)
    {
        nr1+=y;
        b-=y;
    }
    if(c>z)
    {
        nr1+=z;
        c-=z;
    }
    nr2+=x+y+z;
    nr1=nr1+a/2+b/2+c/2;
    if(nr2==0) cout<<"No";
    else
        if(nr1>=nr2)
            cout<<"Yes";
        else
            cout<<"No";

    return 0;
}


#include<iostream>
using namespace std;
string s,t;
int i,j;
main()
{

      cin>>s>>t;
         for(i=1;i<t.size();i++)
           {
              if(s[j]==t[i])j++;
              }
              cout<<j+1;

      }


#include<iostream>
using namespace std;
int main()
{
    int x,z,m,c,d,f,e,n;
    d=f=e=0;
    cin>>x>>z;
    n=1;
    while(n<=6)
    {
        m=x-n;
        c=z-n;
        if(m<0)
        {
            m=m*-1;
        }
        if(c<0)
        {
            c=c*-1;
        }
        if(c>m)
        {
            d++;
        }
        if(m>c)
        {
            e++;
        }
        if(m==c)
        {
            f++;
        }
        n++;
    }
    cout<<d<<' '<<f<<' '<<e<<endl;
    return 0;


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

typedef long long ll;

const int Maxn = 20;
const int Maxl = 16;
const string let = "0123456789abcdef";

ll C[Maxn][Maxn];
ll k;
int t,cur[Maxl];
int ans[Maxn];

ll Solve(int n, const int *av, bool fl = false)
{
	static int dp[1000]; memset(dp,0,sizeof(dp));
	if (fl && av[0] <= 0) return 0;
	for (int i = 0; i < Maxl; i++)
		if (av[i] < 0) return 0;
	dp[n - fl] = 1;
	for (int i = 0; i < Maxl; i++)
		for (int j = 0; j <= n; j++)
			for (int k = 1; k <= j && k <= av[i] - (fl && i == 0); k++)
				dp[j - k] += dp[j] * C[j][k];
	return dp[0];
}

int main()
{
	//freopen("11input.in","r",stdin);
	for (int i = 0; i < Maxn; i++) {
		C[i][0] = C[i][i] = 1;
		for (int j = 1; j < i; j++)
			C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
	}
	cin >> k >> t;
	int len = 1;
	fill(cur,cur+Maxl,t);
	ll got;
	while ((got = Solve(len, cur) - Solve(len, cur, true)) < k) {
		k -= got; len++;
	}
	for (int i = 0; i < len; i++) {
		ans[i] = (i? 0: 1); cur[ans[i]]--;
		while ((got = Solve(len - 1 - i, cur)) < k) {
			k -= got; cur[ans[i]]++;
			ans[i]++; cur[ans[i]]--;
		}
		printf("%c", let[ans[i]]);
	}
	printf("\n");
	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

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

using ii = pair<int,int>;
using ll = long long;

const int N = 20;
const int mod = 1e9 + 7;
int C[N][N];
ll k;
int t;
int ans[N];
ll solve(int n, vector<int> & v, bool f = false){
	if(f && v[0] <= 0)return 0;
	for(int i = 0; i < v.size(); i++){
		if(v[i] < 0)return 0;
	}
	vector<ll>dp(n + 1, 0);
	dp[n-f] = 1;
	for(int i = 0; i < v.size(); i++)
		for(int j = 0; j <= n; j++)
			for(int k = 1; k <= j && k <= v[i] - (f && i == 0); k++)
				dp[j-k] += dp[j] * C[j][k];
	return dp[0];
}
int main() {
#ifdef ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie(0);
#endif // ONLINE_JUDGE

	scanf("%lld%d", &k, &t);

	for(int i = 0; i < N; i++)
		for(int j = 0; j <= i; j++)
			C[i][j] = (j == 0 ? 1 : C[i-1][j] + C[i-1][j-1]);
	int len = 1;
	vector<int>cur(16,t);
	while(true){
		ll x = solve(len,cur) - solve(len, cur, true);
		if(x >= k)break;
		k -= x;
		len++;
	}
	for(int i = 0; i < len; i++){
		for(int j = i == 0; j < 16; j++){
			cur[j]--;
			ll x = solve(len-i-1,cur);
			if(x >= k){
			  printf("%x", j);
			  break;
			} k-=x;
			cur[j]++;
		}
	}

	int mx = 0;
	for(auto x : cur)mx = max(mx,t-cur);

	assert(mx < 3);

	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
long long
	n, k, m,
	s = 0;
std::vector<int> cc(100*100*10*1001, 0);
long long pow10(int n, long long m) {
	long long p = 1;
	for(int i = 0; i<n; ++i)
		p = (p*10) % m;
	return p;
}
long long suf(int l, int d, int b, int r) {
	if (l >= 0) {
		r = (d*b + r) % k;
		b = (b*10) % k;
		if (r == 0 && d != 0) {
			if (n-l == 1)
				return 1;
			if (n-l == 2)
				return 1 * 9;
			if (n-l > 2)
				return 1 * 9 * pow10(n-l-2, m);
		}
		if (l == n-1)
			return 0;
	}
	long long s = 0;
	for(int i=0; i<=9; ++i) {
		int ind = r*11*11*1001 + b*11*1001 + i*1001 + (l+1);
		if (ind >= cc.size()) {
			int bp = 0;
		}
		if (cc[ind])
			s = (s + (cc[ind]-1)) % m;
		else {
			long long blabla = cc[ind];
			cc[ind] = suf(l+1, i, b, r)+1;
			s = (s + (cc[ind]-1)) % m;
		}
	}
	return s;
}
int main() {
	scanf("%I64d %I64d %I64d", &n, &k, &m);
	s = suf(-1, 0, 1, 0);
	printf("%d\n", s % m);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN=1e2+10;

ll n,k;

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

    while(~scanf("%I64d %I64d",&n,&k)) {
        while(1);
        ll red=0,green=0,blue=0;
        if(2*n%k==0)
            red=2*n/k;
        else
            red=2*n/k+1;
        if(5*n%k==0)
            green=5*n/k;
        else
            green=5*n/k+1;
        if(8*n%k==0)
            blue=8*n/k;
        else
            blue=8*n/k+1;
        printf("%I64d\n",red+green+blue);
    }

    return 0;
}


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

using namespace std;

//FILE *input=freopen("input.txt","r",stdin);

#define MAX 210000

int n,d;

int a[MAX];
int b[MAX];

long long int x;

void swap(int &a,int &b){
	int c;
	c=a;
	a=b;
	b=c;
}
int getNextX() {
    x = (x * 37 + 10007) % 1000000007;
    return x;
}
void initAB() {
	int i;
    for(i = 0; i < n; i = i + 1){
        a[i] = i + 1;
    }
    for(i = 0; i < n; i = i + 1){
        swap(a[i], a[getNextX() % (i + 1)]);
    }
    for(i = 0; i < n; i = i + 1){
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i = i + 1){
        swap(b[i], b[getNextX() % (i + 1)]);
    }
}

int next_one[MAX];

struct xy{
	int x,y;
}aa[MAX];

bool sort_x(xy a,xy b){
	if(a.x<b.x)
		return 1;
	return 0;
}

bool is_done[MAX];
int c[MAX];
int bb[MAX];
int teleport[MAX];

int get_tel(int w){
	int noww;
	int bw;
	int rw;
	noww=w;
	while(1){
		if(teleport[noww]==-1)
			break;
		noww=teleport[noww];
	}
	rw=noww;
	noww=w;
	while(1){
		if(teleport[noww]==-1)
			break;
		bw=noww;
		noww=teleport[noww];
		if(bw!=n+1)
			teleport[bw]=rw;
	}
	return rw;
}

int main(){
	int i,j;
	int now;
	int one_index;
	scanf("%d%d%d",&n,&d,&x);

	initAB();

	one_index=n+1;
	for(i=n;i>=0;i--){
		next_one[i]=one_index;
		if(b[i-1]==1)
			one_index=i;
	}
	for(i=1;i<=n;i++){
		aa[i].x=a[i-1];
		aa[i].y=i;
		bb[i]=b[i-1];
	}

	sort(aa+1,aa+n+1,sort_x);
	for(i=1;i<=n+1;i++){
		c[i]=-1;
		teleport[i]=-1;
	}
	for(i=n;i>=1;i--){
		now=next_one[0];

		while(1){
			if(now>n-aa[i].y+1)
				break;
			if(c[now+aa[i].y-1]==-1)
			{
				c[now+aa[i].y-1]=aa[i].x;
				teleport[now+aa[i].y-1]=now+aa[i].y;
			}
			else{
				now=get_tel(now+aa[i].y-1)-aa[i].y+1;
				if(now==n+1)
					break;
				if(bb[now]==0)
					now=next_one[now];
				continue;
			}
			now=next_one[now];
		}
	}

	for(i=1;i<=n;i++){
		printf("%d\n",c[i]);
	}

	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main(){
    string n;
    cin>>n;
    int r=0,p=n.size(),f;
    char x;

        for(int i=0;i<n.size();i++){
            for(int j=i+1;j<n.size();j++){
                if(n[i]==n[j]){
                    r++;
                    x=n[i];
                }
                if(x==n[j+1]){
                    r--;
                }
            }
        }
    f=p-r;
    if(f%2==0){
        cout<<"CHAT WITH HER!"<<endl;
    }
    else{
        cout<<"IGNORE HIM!"<<endl;
    }
return 0;
}


#include <iostream>
using namespace std;
long long binpow(long long a, long long b) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
int main()
{
    long long n; cin >> n;
    if(n==3)
    {
        cout << 24;
        exit(-1);
    }
    long long ans=4*(6*binpow(4,n-3)+9*binpow(4,n-4));
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
using namespace std;
int A[7],s,ok,F[7];
void bkt(int poz)
{
    if(poz==7)
    {
        int sum=0;
        for(int i=1;i<=poz;i++)
            if(F[i]==1)
                sum+=A[i];
        if(2*sum==s)
            ok=1;
    }
    else
    {
        for(int i=poz;i<=6;i++)
        {
            F[i]=1;
            bkt(i+1);
            F[i]=0;
            bkt(i+1);
        }
    }
}
int main()
{
    for(int i=1;i<=6;i++)
        cin>>A[i];
    sort(A+1,A+6+1);
    s=0;
    for(int i=1;i<=6;i++)
        s+=A[i];
    bkt(1);
    if(ok==1)
        cout<<"YES";
    else
        cout<<"NO";
    return 0;
}


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

ll com(ll a,ll b)
{
    if(a==b)
    return 1;
    if(b==1)
    return n;
    if(b==0)
    return 1;
    return (com(a-1,b)+com(a-1,b-1));
}

int main()
{
    cin>>n;
    ll m=2*n-2;
    ll baki=m-n;
    ll maki=baki+1;
    ll p=com(baki*3,baki)*maki*4;
    cout<<p<<endl;
}


#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i,a,b) for(lli i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
#define inf 7+1e18
# define lld long double
#define eps 0.000001
#define endl '\n'
using namespace std ;

int gcd(int a, int b)
{
    if (a == 0)   return b;
    if(b==0)      return a;
    if (a == b)   return a;
    if (a > b)    return gcd(a%b, b);
    return gcd(a, b%a);
}

/*lli v[10000001]={0};
void seive()
{
	bool prime[10000000+1];
	memset(prime, true, sizeof(prime));

	for (lli p=2; p*p<=10000000; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*2; i<=10000000; i += p)
				prime[i] = false;
		}
	}

	for (int p=2; p<=10000000; p++)
	if (prime[p])
		v[p]=1;
}*/

/*vector <lli> v[100010];
bool vis[100010];
bool tree;
//lli count=0;
void dfs(lli n, lli par)
{
    vis[n] = 1;

    loopl(i,0,v[n].size())
    {
        if (vis[v[n][i]] == 0)
        {
            dfs(v[n][i], n);
            //count++;
        }
        else if (v[n][i] != par)
        {
            tree = 0;
        }
    }
}*/

/*lli power(lli x, lli y, lli p)
{
    lli res = 1;

    x = x % p;

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

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

int main()
{
	////////////////////
	fastio;
	///////////////////

    lli n,m;
    cin>>n>>m;
    lli a[m];
    map<lli,lli> q;
    for(lli i=0;i<m;i++)
    {cin>>a[i];
    q[a[i]]++;}
    lli maxi=-1;
    if(q.size()==1)
    {
        map<lli,lli>::iterator it=q.begin();
        cout<<it->ss/n<<endl;
        return 0;
    }

    else
    {

        //lli cnt=0,ind=1;
        for(lli j=1;j<=n;j++)
        {
            lli chance=1,save=q.begin()->ss/j,cnt=j;
            if(cnt==n)
            {maxi=max(maxi,save); continue;}
            map<lli,lli>::iterator it=q.begin();it++;
            for(it;it!=q.end();it++)
            {
                cnt+=it->ss/save;
                //chance++;
                if(cnt==n)
                {
                    maxi=max(maxi,save);
                    continue;
                }

            }
        }
    }

    cout<<maxi<<endl;

}


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

bool check(int a,int b,int c,int x);

int main()
{
      int x,y;
      cin>>x>>y;
      int a=y,b=y,c=y;
      while(check(a,b,c,x)==true)
      {
            int k=min(a,min(b,c));
            if(k==b)
            {
                  int tmp=a;
                  a=b;
                  b=tmp;
            }
            else if(k==c)
            {
                  int tmp=a;
                  a=c;
                  c=tmp;
            }

            int s=b+c;
            if(s>x)
            {
                  a=x;
                  sum++;
            }
            else
            {
                  a=s-1;
                  sum++;
            }
      }
      cout<<sum;
}

bool check(int a,int b,int c,int x)
{
      if(a==x && b=x && c==x)
      {
            return false;
      }
      return true;
}


#include <cstdio>
#include <vector>

using namespace std;

int main() {
    int x, y;
    scanf("%d %d", &x, &y);

    vector<int> sides(x, 3);
    int moves = 0;
    while (sides[2] != y) {
        // change the biggest one
        // sides[0] + sides[2] == sides[1] + 1
        sides[2] = max(sides[1] + 1 - sides[0], y);
        sort(sides.begin(), sides.end());
        moves += 1;
    }

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

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define popb pop_back
#define fo(i , a , b) for (int i = (a); i < (b); ++i)
#define forr(i , b) fo( i , 0 , b )
#define carr( n , array ) forr(i , n) cin >> array[i]
#define cmap( n , map ) forr (i , n) {ll x ; cin >> x ; map [x] ++ ; }
#define cpmap( n , map ) forr (i , n) {ll x , y ; cin >> x >> y ; map [x] = y ; }
#define F first
#define S second
#define IF ->first
#define IS ->second

const ll max9 = 10 + 1e9 ,  max6 = 10 + 1e6 , max12 = 10 + 1e12 , max15 = 10 + 1e15 ;
const ll min6 = -1 * max6 , min9 = -1 * max9 , min12 = -1 *max12 , min15 = -1 * max15 ;
const ll R = 7 + 1e9 , NN = 10 + 1e5 ;
using namespace std;
ll k , n , s , p ;
ll check(ll x){
    ll q =  x * p ;
    q /= k ;
    q *= s ;
    if (q < n) return -2 ;
    x -- ;
    q =  x * p ;
    q /= k ;
    q *= s ;
    if (q < n) return x + 1 ;
    return -1 ;
}
ll BinS(ll st , ll en){
    ll mid = st + (en - st) / 2 ;
    ll test = check(mid) ;
    if (test == -1) return BinS(st , mid - 1) ;
    if (test == -2) return BinS(mid + 1 , en) ;
    return test ;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> k >> n >> s >> p ;
    cout << BinS(1 , 10000) ;
    return 0;
}


#include <iostream>

using namespace std;
int a[10000000];
int main() {
	int n,k,lol=0;
	cin >> n>>k;
	for (int i = 1; i <= n; i++) { a[i] = i; }
	if (n < k) { k = n; }
	for (int i = 1; i <= k; i++) {
		int c = a[n - i + 1];
		a[n - i + 1] = a[i];
		a[i] = c;
	}
	for (int i = 1; i <n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (a[i] > a[j]) { lol++; }
		}
	}
	cout << lol << endl;
	 cin >> n;
	return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="stack" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/stack" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/stack" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<debugger />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <iostream>
#include <cstdio>
#include <stack>
#define MAXN 1000005

using namespace std;

int n, a[MAXN], next[MAXN], prev[MAXN];

main()
{
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  int m, x;
  scanf("%d", &m);
  for (int i = 0; i < m; i++)
    {
      scanf("%d", &x);
      a[x] = - a[x];
    }

  stack<int> s, t;
  for (int i = 1; i <= n; i++)
    if (a[i] < 0)
      {
        while (!s.empty())
          {
            if (a[s.top()] + a[i] == 0 && t.empty()) break;
            if (!t.empty() && a[s.top()] == a[t.top()])
              {
                a[t.top()] = - a[t.top()];
                s.pop();
                t.pop();
              }
             else
              {
                t.push(s.top());
                s.pop();
              }
          }
          if (!t.empty() || s.empty())
            {
              printf("NO");
              return 0;
            }

        s.pop();

      }
    else s.push(i);

  while (!s.empty() )
    if (!t.empty() && a[s.top()] == a[t.top()])
      {
        a[t.top()] = - a[t.top()];
        s.pop();
        t.pop();
      }
    else
      {
        t.push(s.top());
        s.pop();
      }

  if (!t.empty())
    {
      printf("NO");
      return 0;
    }

  printf("YES\n");
  for (int i = 1; i <= n; i++) printf("%d ", a[i]);
  system("pause");
}


 #include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio (false) ;cin.tie(NULL);
#define tc  int t;cin>>t;for(int p=1;p<=t;p++)
#define ll long long int
#define forn(i,n) for(int i=0;i<n;i++)
#define forn1(i,a,b) for(int i=a;i<=b;i++)
ll power(ll a,ll b){ ll ans=1;    while(b>0){ if(b&1){ ans=ans*a;} a=a*a; b=b/2; } return ans; }
int main(){
   ll n,t;
   cin>>n;
   ll arr[30];
   ll ans=0;
   ll k=0;
   memset(arr,0,sizeof(arr));
   while(n>0){
       arr[k]=n%3;
       n=n/3;

       k++;

   }
   int flag=1;
   ll num;

   for(ll i=0;i<30;i++){
       if(arr[i]){
           if(flag){
           flag=0;
            num=power(3,i+1);

           ans++;
           }else{

               ll temp=power(3,i);

               temp=temp/num;

               temp*=arr[i];
               ans+=temp;
           }
       }

   }
   cout<<ans<<endl;

}


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll inf = 1e4+5;
ll a, b, c;

int main()
{
	scanf("%lld%lld%lld", &a, &b, &c);
	if(a==b||b==c||c==a||a==1||b==1||c==1)
	{
	    cout<<"YES"<<endl;
	    return 0;
	}
	int maxn = max(a, max(b, c));
	int flag = 0;
	for (int i = 1; i <= maxn; i++)//第一个的设定位置
	{
		for (int j = 1; j <= maxn; j++)
		{
			for (int k = 1; k <= maxn; k++)
			{
				int ans = 0;
				for (int m = maxn; m <= inf; m++)
				{
					if ((m - i) % a == 0 || (m - j) % b == 0 || (m - k) % c == 0)
						ans++;
				}
				if (ans == inf - maxn + 1)
				{
					cout << "YES" << endl;
					return 0;
				}
			}
		}
	}
	cout << "NO" << endl;
	return 0;
}


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

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

        if (n == 4) cout << 1;
        else if (n == 7) cout << 2;
        else
        {
            int x = n, k = 0, r, f;
            while (x != 0)
            {
                k++;
                x = x / 10;
            }
            if (n /(int) pow(10, k - 1) == 4) r = (int)pow(2, k) - 1;

            if (n / (int)pow(10, k - 1) == 7) r = (int)pow(2, k) + (int)pow(2, k - 1) - 1;

            while (k - 1)
            {
                n = n % (int)pow(10, k - 1);

                f = n / (int)pow(10, k - 2);

                if (f == 7) r += (int)pow(2, k - 2);
                k--;
            }
            cout << r;
        }
}


/*
ID: john320
PROG : xor-mst
LANG : C++11
*/

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility>
#include <bitset>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <list>
#include <iterator>
#include <climits>

using namespace std;

struct P {

	int ew;
	int v1;
	int v2;

};

vector <P> G;			//input vector of edge/vertex pairs
vector <P> iVert;		//vector of edges and vertices included in shortest set
set <int> Vonly;		//set of vertices only
P tempP;
bool srt(P p1, P p2) { return p1.ew < p2.ew; }

vector< vector <int> > tbl;

unsigned int n;

int main() {

	int maxx = INT32_MAX;
	int len = 0;

	vector <P>::iterator z;
	set <int>::iterator y;

	cin >> n;

	for (unsigned int i = 0; i < n; i++) {
		for (unsigned int j = i+1; j < n; j++) {
			tempP.ew = i ^ j;
			if (tempP.ew != 0) {
				tempP.v1 = i;
				tempP.v2 = j;
				G.push_back(tempP);
				swap(tempP.v1, tempP.v2);
				G.push_back(tempP);
			}
		}
	}

	sort(G.begin(), G.end(), srt);
	//for (int i = 0; i < G.size(); i++) cout << G[i].ew << ", " << G[i].v1 << ", " << G[i].v2 << endl;

	//put shortest edge on output stack , and vertices into vertex set.  Increment sum of edges
	//iVert.push_back(G[0]);
	Vonly.insert(G[0].v1);
	Vonly.insert(G[0].v2);
	len += G[0].ew;

	//as each element is added to output, or larger duplicate path encountered, remove from vector, until empty
	G.erase(G.begin());

	while (!G.empty()) {
		for (z = G.begin(); z < G.end(); z++) {
			if ((Vonly.find(z->v1) != Vonly.end()) && (Vonly.find(z->v2) != Vonly.end())) {  //if both vertices already in output, get rid of duplicate, larger edge
				G.erase(z);
				break;
			}
			else if ((Vonly.find(z->v1) == Vonly.end()) ^ (Vonly.find(z->v2) == Vonly.end())) {
				iVert.push_back(*z);		 //This is optional, since we're only counting the length, not(necessarily) tracking the vertices
				Vonly.insert(z->v1);
				Vonly.insert(z->v2);
				len += z->ew;
				G.erase(z);
				break;
			}
		}
	}

	cout << len << endl;

}


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

typedef long long ll;

ll a,b,x,y;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

void init()
{
	ll c=gcd(x,y);
	x/=c;
	y/=c;
}

int main()
{
	//freopen("in.txt","r",stdin);

	while(cin>>a>>b>>x>>y)
	{
		init();
		if(a<x || b<y) puts("0 0");
		else
		{
			int A,B;
			A=a%x;
			B=b-( (a-A)*y/x );
			cout<<a-A<<' '<<b-B<<endl;
		}
	}
}


#include<iostream>
using namespace std;
int main(){
    long long int n, m, cont = 0;
    cin >> n >> m;
    if(n == m){
        cout << '0' << '\n';
        return 0;
    }
    if(m < n*2 || n > m / 2){
        cout << "-1" << '\n';
        return 0;
    }
    while(m != n){
        if((m / 2) % n == 0 && (m % 2 == 0)){
            m/= 2;
            cont++;
        }
        else if((m / 3) % n == 0){
            m /= 3;
            cont++;
        }
        else{
            cout << "-1" << '\n';
            return 0;
        }
    }
    cout << cont << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n, m;

#define MAX 505
#define ll long long

const int N = (int)1e5 + 5;
const int INF = (int)1e9;
int ans = 0;
void solve () {
        int k, a, b; cin >> k >> a >> b;
        while (1) {
                if (a - k >= 0) {
                        a -= k;
                        ans++;
                        b = b - b % k;
                } else if (b - k >= 0) {
                        b -= k; ans++;
                        a = a - a % k;
                } else break;
        }
        if (ans == 0) cout << -1;
        else cout << ans;
}
int main () {
        int t = 1;
        // scanf("%d", &t);
        while (t--) {
                solve();
        }
}


#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
char s[100];
char a[20];
map<string, int> mp;
int main()
{
    scanf("%s", s);
    for(int i = 0; i < 10; i++)
    {
        scanf("%s", a);
        mp[a] = i;
    }
    for(int i = 0; i < 8; i++)
    {
        sscanf(s+i*10, "%10s", a);
        printf("%d", mp[a]);
    }
    puts("");
    return 0;
}
/*2018-08-22 19:53:50.211*/


//this problem is solved by pol

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int gcd (int u, int v)
{
if(v==0) return u;
else return gcd(v, u%v);
}

long long co (long long u, long long v)
{
if((u-v)<v) v=u-v;
int *a= new int[v];
int *b= new int[v];
for(int i=0;i<v;i++)
   {b[i]=i+1; a[i]=u-v+1+i;}
for(int i=0;i<v;i++)
   for(int j=0;j<v;j++)
      if(b[i]!=1 && a[j]!=1)
         {
         int z=gcd(b[i],a[j]);
         b[i]/=z; a[j]/=z;
         }
long long sum=1;
for(int i=0;i<v;i++) sum*=a[i];
//for(int i=0;i<v;i++) sum/=b[i];
return sum;
}

int main () {
long long n,m,t;
cin>>n>>m>>t;
long long s=0;
for(int i=4;i<t;i++)
   {
   //cout<<"!"<<i<<" "<<t-i<<" "<<co(n,i)<<" "<<co(m,t-i)<<endl;
   s+=co(n,i)*co(m,t-i);
   }
cout<<s<<endl;
  return 0;
}


#pragma region Include/Defines
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <limits.h>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <math.h>
#include <string>
#include <algorithm>
#include <functional>
#include <cassert>
#include <map>
#include <set>
#include <iostream>
#include <queue>
#include <time.h>

using namespace std;
typedef long long lli;
typedef vector<int> vi;
typedef vector<lli> vli;
typedef pair<int, int> pii;
typedef pair<lli, lli> pll;
typedef vector<pii> vpii;
typedef vector <pll> vpll;
typedef long double ld;
#define all(a) std::begin(a), std::end(a)

const int INF = 0x3f3f3f3f;
const lli LINF = 0x3f3f3f3f3f3f3f3f;

template<typename T, typename T2> inline void _max(T &a, T2 b) { a = max((T)a, (T)b); }
template<typename T, typename T2> inline void _min(T &a, T2 b) { a = min((T)a, (T)b); }

#ifdef _DEBUG
#define epr(...) fprintf(stderr,__VA_ARGS__)
#else
#define epr(...)
#endif
#pragma endregion

#pragma region Erato Sieve
const int MAX_PRIME = 1e6;
int divisors[MAX_PRIME] = { 0 }, primes[MAX_PRIME] = { 0 }, nPrimes = 0;
void generatePrimes() {
	for (int i = 2; i < MAX_PRIME; i++) {
		if (divisors[i] == 0) divisors[i] = primes[nPrimes++] = i;
		for (int j = 0; j < nPrimes && i * primes[j] < MAX_PRIME; j++)
			divisors[i * primes[j]] = max(divisors[i], primes[j]);
	}
}

void generateDivsorsRec(map<lli, int>::iterator it, map<lli, int> &f, vli &res, lli tm) {
	if (it == f.end()) {
		res.push_back(tm);
		return;
	}
	lli cp = 1;
	for (int i = 0; i <= it->second; i++) {
		lli ntm = tm * cp;
		generateDivsorsRec(next(it), f, res, ntm);
		cp *= it->first;
	}
}

map<lli, int> factorizeLow(lli x) {
	map<lli, int> res;
	while (x != 1) {
		res[divisors[x]]++;
		x /= divisors[x];
	}
	return res;
}

map<lli, int> factorize(lli x) { // max prime should be greater than or equal to the square root of x
	if (x < MAX_PRIME) return factorizeLow(x);
	map<lli, int> res;
	for (int i = 0; 1ll * primes[i] * primes[i] <= x; i++) {
		if (x % primes[i] == 0) {
			int c = 0;
			while (x % primes[i] == 0) c++, x /= primes[i];
			res[primes[i]] = c;
		}
	}
	if (x != 1) res[x] = 1;
	return res;
}

vli generateDivisors(lli x) {
	auto f = factorize(x);
	vli res;
	generateDivsorsRec(f.begin(), f, res, 1);
	sort(all(res));
	return res;
}

lli phi(lli x) { // if known that x is prime return x - 1
	auto f = factorize(x);
	lli res = x;
	for (auto &p : f) res -= res / p.first;
	return res;
}
#pragma endregion

const int MAX = 1e6 + 100;

int x[3][MAX] = { 0 };

int x2;

void clear() {
}

void read() {
}

void solve() {
	int maxPrime = 1;
	for (int i = 0; primes[i] <= x2; i++)
		if (x2 % primes[i] == 0)
			maxPrime = primes[i];

	int res = INF;
	for (int i = x2; i > x2 - maxPrime; i--)
		if(divisors[i] != i)
			_min(res, x[1][i]);
	printf("%d\n", res);
}

void precalc() {
	generatePrimes();
	for (int i = 3; i < MAX; i++) x[0][i] = x[1][i] = x[2][i] = i;
	for (int i = 3; i < MAX; i++)
		x[1][i] = i - divisors[i] + 1;
}

int main() {
	precalc();
#ifdef _LOCAL_VAN
	freopen("in.txt", "r", stdin);
#endif
	while (scanf("%d", &x2) == 1) {
		clear();
		read();
		solve();
#ifndef _LOCAL_VAN
		return 0;
#endif
	}
	return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int  maxn=1000003;
vector<double> v;
void check(char ch)
{
    switch (ch)
    {
    case '>' :
    {
        v.push_back(1);
        v.push_back(0);
        v.push_back(0);
        v.push_back(0);
        break;
    }
    case '<':
    {
        v.push_back(1);
        v.push_back(0);
        v.push_back(0);
        v.push_back(1);
        break;
    }
    case '+':
    {
        v.push_back(1);
        v.push_back(0);
        v.push_back(1);
        v.push_back(0);
        break;
    }
    case '-':
    {
        v.push_back(1);
        v.push_back(0);
        v.push_back(1);
        v.push_back(1);
        break;
    }
    case '.':
    {
        v.push_back(1);
        v.push_back(1);
        v.push_back(0);
        v.push_back(0);
        break;
    }
    case ',':
    {
        v.push_back(1);
        v.push_back(1);
        v.push_back(0);
        v.push_back(1);
        break;
    }
    case '[':
    {
        v.push_back(1);
        v.push_back(1);
        v.push_back(1);
        v.push_back(0);
        break;
    }
    case ']':
    {
        v.push_back(1);
        v.push_back(1);
        v.push_back(1);
        v.push_back(1);
        break;
    }
    }
}
int main()
{
    string s;
    cin >> s;
    int l = s.size();
    for (int i = 0; i < l; ++i)
        check(s[i]);
    reverse(v.begin(), v.end());
    double tmp = 0, x = 2;
    for (int j = 0; j < 4 * l; ++j)
    {
        if(v[j]==1)
            tmp += pow(x, (double)j);
    }
    while (tmp > maxn)
        tmp -= maxn;
    cout <<tmp<< endl;
    return 0;
}


/*---------------Go Code GO---------------*/

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using Order_Set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define     PI              acos(-1.0)
#define     O_O             ios_base::sync_with_stdio(false); cin.tie(NULL)
#define     precision(a)    fixed<<setprecision(a)
#define     endl            '\n'
#define     Flush           cout<<flush
#define     LLMX            0x3fffffffffffffff

long long N,M, leader[101], permut[101];
bitset<101> mark;
vector<int> vc;

int main()
{
    long long i,j,k,l,tst=0,ans=0, csno=0;
    cin>>N>>M;
    for(i=0; i<M; i++) cin>>leader[i];
    int pos=0;
    for(i=0; i<M-1; i++)
    {
        k = (N+leader[i+1]-leader[i])%N;
        if(k==0) k=N;

        if(permut[leader[i]]==0||permut[leader[i]]==k) permut[leader[i]]=k;
        else ans = LLMX;
        mark[k]=1;
    }
    for(i=1; i<=N; i++)
    {
        if(mark[i]==0) vc.push_back(i);
    }
    k=0;
    for(i=1; i<=N; i++)
    {
        if(permut[i]==0) permut[i]=vc[k++];
        ans+=permut[i];
    }
    if(ans!=((N+1)*N)/2)
    {
        cout<<-1<<endl;
        return 0;
    }
    for(i=1; i<=N; i++)
        cout<<permut[i]<<' ';
    cout<<endl;
    return 0;
}


#include<vector>
#include<string>
#include<algorithm>
#include<iostream>
#include <iomanip>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<stack>
#include<queue>
#include<cstring>
#include<map>
#include<set>
using namespace std;
#define MOD 1000000007
int a[1111111];
long long x[30];
long long now=6;
int cal(long long n)
{
	int l=0;
	if(n<0)
		n=-n;
	if(n<=now)
		return a[n];
	long long last;
	long long t=n;
	while(t>0)
	{
		l++;
		last=t;
		t/=10;
	}
	if(last<=5)
	{
		if(n>=x[l]*last||last==1)
			return cal(n-x[l]*last)+last*l;
		else
		{
			int r1,r2;
			r1=cal(x[l]*last-n)+last*l;
			r2=cal(n-x[l]*(last-1))+(last-1)*l;
			return min(r1,r2);
		}
	}
	else if(last>6)
		return cal(x[l+1]-n)+l+1;
	else
	{
		int r1,r2;
		if(n>x[l]*last)
			r1=cal(n-x[l]*last)+last*l;
		else
			r1=cal(x[l]*last-n)+last*l;
		r2=cal(n-x[l]*(last-1))+(last-1)*l;
		r1=min(r1,r2);
		r2=cal(x[l+1]-n)+l+1;
		return min(r1,r2);
	}
}
int main()
{
	int i;
	long long n;
	cin>>n;
	x[1]=1;
	for(i=2;i<=15;i++)
	{
		x[i]=x[i-1]*10+1;
	}
	for(i=0;i<7;i++)
		a[i]=i;
	for(i=7;i<1111110;i++)
	{
		a[i]=cal(i);
		now=i;
	}
	if(n<1111110)
		cout<<a[n]<<endl;
	else
	{
		cout<<cal(n)<<endl;
	}
	//system("pause");
    return 0;
}


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

#define boost ios::sync_with_stdio(0); cin.tie(0);
#define int long long
const int N = 1e5 + 10;

int32_t main(){
	boost;

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

	string n;

	cin >> n;
	// for(int i = 0 ; i < 6 ; ++i)
	// 	cin >> n[i];

	int a = n[0] + n[1] + n[2] - 3*'0';
	int b = n[3] + n[4] + n[5] - 3*'0';

	if(a == b){
		cout << 0;
		return 0;
	}
	string s;
	int x;
	int ans = 6;
	for(int i = 1 ; i <= 999999 ; ++i){
		x = stoi(n) - i;
		s = to_string(x);

		if(s.length() != 6){
			int rem = 6 - s.length();
			string a = "";

			for(int i = 0 ; i < rem ; ++i){
				a += '0';
			}
			s = a + s;
		}

		if(s[0] + s[1] + s[2] == s[3] + s[4] + s[5] ){
			int temp = 0;
			if(s[0] != n[0]){
				++temp;
			}
			if(s[1] != n[1]){
				++temp;
			}
			if(s[2] != n[2]){
				++temp;
			}
			if(s[3] != n[3]){
				++temp;
			}
			if(s[4] != n[4]){
				++temp;
			}
			if(s[5] != n[5]){
				++temp;
			}

			ans = min(ans ,temp);
		}
	}

	for(int i = 1 ; i <= 999999 ; ++i){
		x = stoi(n) + i;
		s = to_string(x);

		if(s[0] + s[1] + s[2] == s[3] + s[4] + s[5]){
			int temp = 0;
			if(s[0] != n[0]){
				++temp;
			}
			if(s[1] != n[1]){
				++temp;
			}
			if(s[2] != n[2]){
				++temp;
			}
			if(s[3] != n[3]){
				++temp;
			}
			if(s[4] != n[4]){
				++temp;
			}
			if(s[5] != n[5]){
				++temp;
			}

			ans = min(ans ,temp);
		}
	}

	cout << ans;
	/*
	sort(n , n + 3);
	sort(n+3 , n+6);

	int diff;
	int ans = 0;
	if(a - b > 0){
		diff = a - b;
		for(int i = 3 ; i >= 0 ; --i){
			if(diff > 0 && n[i] - '0' > 0){
				diff -= (n[i] - '0');
				ans++;
				if(diff <= 0)
					break;
			}
		}
		int ans2 = 0;
		diff = a - b;
		for(int i = 3 ; i < 6 ; --i){
			if(n[i] - '0' <= 9){
				diff -= 9 - n[i] + '0';
				++ans2;
				if(diff <= 0)
					break;
			}
		}

		cout << min(ans,ans2);
	}
	else if(a - b < 0){

		ans = 0;
		diff = b - a;
		for(int i = 0 ; i < 3 ; ++i){
			if(diff > 0 && n[i] - '0' > 0){
				diff -= 9 - n[i] + '0';
				ans++;
				if(diff <= 0)
					break;
			}
		}
		diff = b-a;
		// cout << diff;
		int ans2 = 0;
		for(int i = 5 ; i > 2 ; --i){
			if(n[i] - '0' <= 9){
				diff -= (n[i] - '0');
				// cout << diff << " ";
				++ans2;
				if(diff <= 0)
					break;
			}
		}
		// cout << ans << " " << ans2 << "\n";
		cout << min(ans, ans2);
	}
	else{
		cout << 0;
	}
	// cout << a << b;
	*/
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back

using namespace std;
typedef pair<int ,int > pii;
typedef long long ll;

char dir[] = {'^' , '>' , 'v' , '<' };

int main()
{
    string  s1 ;
    int n ;
    char  ch1 , ch2 ;
    cin >> ch1 >> ch2 ;
    cin >> n ;
    int st  , TMP = n;
    for( int i = 0 ; i < 4 ; i++ )
    {
        if( ch1 == dir[i] )
        {
            st = i ;
            break ;
        }
    }
    int i = 0 , j = 0  ;
    int ii = st , jj = st ;
    string ccw = "" , cw = "";
    ccw+=dir[st];
    cw+=dir[st];

    while( n-- )
    {
        ii = (ii+1)%4;
        jj = ((jj-1) + 4 ) %4;
        cw+= dir[ii]  ;
        ccw+= dir[jj] ;
    }
    ////cout << cw << " -- " << ccw << endl;
    if( ch2 == cw[TMP] &&  ch2 == ccw[TMP] )
    {
        cout << "undefined\n";
    }
    else if( ch2 == cw[TMP])
    {
        cout << "cw\n";
    }
    else if( ch2 == ccw[TMP] )
    {
        cout << "ccw\n";
    }
    else
    {
        cout << "undefined\n";
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <ctime>
//#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> pint;
typedef pair<LL,LL> pll;
#define fi first
#define se second
typedef map<int,int> mapint;
typedef vector<int> vint;
typedef vector<vint> vintp;
#define rep(i,j,k) for (int i = (j);i <= (k);i ++)
#define ran2 (rand() % 32000 * 32000 + rand() % 32000)
#define pb push_back
#define mp make_pair

int m,n,j,k,l,i,o,p,__t;

char ch;
void read(int &a){
	for (ch = getchar();(ch < '0' || ch > '9') && (ch != '-');ch = getchar());
	if (ch == '-') a = 0,__t = -1; else a = ch - '0',__t = 1;
	for (ch = getchar();ch >= '0' && ch <= '9';ch = getchar()) a = a * 10 + ch - '0';
	a *= __t;
}
int main(){
	scanf("%d%d", &n, &m);
	while ((g = __gcd(n, m)) > 1)
		m /= g, c ++;
	if(m == 1)
		printf("2-type\n%d", c);
	else if (!c && n % m == 1)
		puts("3-type");
	else if (!c && n % m == m - 1)
		puts("11-type");
	else if ((n - 1) * (n + 1) / (n % 2 + 1 ) % m == 0)
		puts("6-type");
	else
		puts("7-type");
}


/// BIS-MILLAHIR RAHMANIR RAHIM

#include<algorithm>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include<deque>
#include<climits>

using namespace std;

#define all(a) a.begin(),a.end()
#define I1(n) scanf("%d",&n)
#define I2(n1,n2) scanf("%d%d",&n1,&n2)
#define I3(n1,n2,n3) scanf("%d%d%d",&n1,&n2,&n3)
#define F(i, a, b) for(  i = (a); i <= (b); i++ )
#define FR(i, a, b) for(  i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Pr(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;
#define pb push_back
#define pi acos(-1.0)
#define MEM(a,val) memset(a,val,sizeof(a))
#define eps 1e-9
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define sz(a) ((int)(a).size())
#define IN  freopen("input.txt","r",stdin)
#define OUT freopen("output.txt","w",stdout)
#define PRC printf("Case %d: ",cas++);

int m;

bool is[2007];
vector<int>prime[2007];

void sieve()
{
    int i,j;
    for(i=2; i<=2007; i++)
    {
        if(!is[i])
        {
            for(j=i+i; j<=2007; j+=i)
            {
                is[j]=true;
                prime[j].pb(i);
            }
        }
    }
}

int expo(long long  b, int p)
{
    int ret = 1;
    while(p)
    {
        if(p&1) ret = (ret*b)%m;
        p >>= 1;
        b = (b*b)%m;
    }
    return ret;
}

int main()
{
    int  n, t, i, j, p, e;
    sieve();

    int cnt =  0;

    cin >> p;
    m = p;

    for(i=1; i<p; i++)
    {
        int l = prime[i].size();

        for(j=0; j<l; j++)
        {
            e = (p-1) / prime[i][j];
            if(expo(i,e)==1) break;
        }

        if(j==l) cnt++;
    }

    cout << cnt << endl;

    main();

    return 0;
}


#include<iostream>
using namespace std;
int a[100000000];
int main()
{
    long long n,k,flag = 1;cin>>n>>k;
    if(n<=k and n>1) flag = 0;
    else
    {
        for(int i=1;i<=k;i++)
        {
            if(!a[n%i]) a[n%i] = 1;
            else flag = 0;
        }
    }
    if(flag) cout << "Yes";
    else cout << "No";
    cout << endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    long long n,a;
    bool e=true;
    cin>>n;
    for(a=n;a>0;a--)
    {
        if(n%a==0)
        {
            e=true;
            for(int b=2;b*b<=a;b++)
            {
                if(a%(b*b)==0)
                {
                    e=false;
                    a/=b;
                    a++;
                    break;
                }
            }
            if(e)
            {
                cout<<a;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

struct TNode {
    TNode *left, *right;
    int sz, x;
};

TNode *new_node(int x) {
    return new TNode({nullptr, nullptr, 1, x});
}

int safe_sz(TNode *root) {
    return (root ? root->sz : 0);
}

void relax(TNode *root) {
    if (!root) return;
    root->sz = safe_sz(root->left) + safe_sz(root->right) + 1;
}

TNode *merge(TNode *left, TNode *right) {
    if (!left) {
        return right;
    }
    if (!right) {
        return left;
    }
    TNode *node = new_node(-1);
    int sz1 = safe_sz(node->left), sz2 = safe_sz(node->right);
    if (rand() % (sz1 + sz2) < sz1) {
        node->left = merge(left, right->left);
        node->right = right->right;
        node->x = right->x;
    } else {
        node->right = merge(left->right, right);
        node->left = left->left;
        node->x = left->x;
    }
    relax(node);
    return node;
}

void split(TNode *root, int sz, TNode *&left, TNode *&right) {
    if (!root || !sz) {
        left = nullptr;
        right = root;
        return;
    }
    TNode *left1, *right1;
    if (safe_sz(root->left) >= sz) {
        split(root->left, sz, left1, right1);
        left = left1;
    }
    return;
}

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    #endif // MY
    ll n, m;
    cin >> n >> m;
    ll s, t;
    cin >> s >> t;
    s--;
    int len1 = m, len2 = n - m;
    unordered_map <pair <ll, ll>, bool> was;
    vector <pair <ll, ll> > path;
    while (t > 0) {
        if (was[{s, t % n}]) break;
        was[{s, t % n}] = true;
        path.push_back({s, t % n});
        if (s < m) {
            s = (s + t) % n;
        } else {
            s = ((s - t) % n + n) % n;
        }
        //cout << s << "\n";
        t--;
    }
    if (!t) {
        cout << s + 1 << "\n";
        return 0;
    }
    ll cycle = 1;
    while (path.back() != make_pair(s, t % n)) {
        path.pop_back();
        cycle++;
    }
    t %= cycle;
    while (t > 0) {
        if (s < m) {
            s = (s + t) % n;
        } else {
            s = ((s - t) % n + n) % n;
        }
        t--;
    }
    cout << s + 1;
}


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

const long long maxn = 5e2 + 10;
const long long mod = 998244353;

long long ans;
long long n, k;
long long dp[2][maxn][maxn];
long long DP[2][maxn][maxn];

void in();
void solve();
void init_dp();
void init_DP();
void findp();
void finDP();
void out();

int main(){
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    in();
    solve();
    out();
}

void in(){
    cin >> n >> k;
    k--;
}

void solve(){
    init_dp();
    findp();
    init_DP();
    finDP();
    for(long long i = 0; i <= k; i++)
        for(long long j = 0; j <= k; j++)
            ans = (ans + dp[n % 2][i][j]) % mod;
}

void init_dp(){
    for(long long i = 1; i <= n; i++)
        dp[1][1][i] = 2;
}

void findp(){
    for(long long i = 1; i < n; i++){
        for(long long j = 1; j <= n; j++)
            for(long long K = 1; K <= n; K++)
                dp[1 - i % 2][j][K] = 0;
        for(long long j = 1; j <= n; j++){
            for(long long K = 1; K <= n; K++){
                if(j + 1 <= K)
                    dp[1 - i % 2][j + 1][K] = (dp[1 - i % 2][j + 1][K] + dp[i % 2][j][K]) % mod;
                dp[1 - i % 2][1][K] = (dp[1 - i % 2][1][K] + dp[i % 2][j][K]) % mod;
            }
        }
    }
}

void init_DP(){
    for(long long i = 1; i <= n; i++)
        for(long long j = 0; j <= n; j++)
            DP[1][1][i] = (DP[1][1][i] + (dp[n % 2][j][i] - dp[n % 2][j][i - 1] + mod) % mod) % mod;
}

void finDP(){
    for(long long i = 1; i < n; i++){
        for(long long j = 1; j <= n; j++)
            for(long long K = 1; K <= n; K++)
                DP[1 - i % 2][j][K] = 0;
        for(long long j = 1; j <= n; j++){
            for(long long K = 1; K <= n; K++){
                if((j + 1) * K <= k)
                    DP[1 - i % 2][j + 1][K] = (DP[1 - i % 2][j + 1][K] + DP[i % 2][j][K]) % mod;
                DP[1 - i % 2][1][K] = (DP[1 - i % 2][1][K] + DP[i % 2][j][K]) % mod;
            }
        }
    }
}

void out(){
    cout << ans;
}


include <bits/stdc++.h>

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define in(x) int (x); input((x));
#define x first
#define y second
#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; --i)
#define for1(i, n) for(int i = 1; i <= (int)(n); ++i)

typedef int itn;

//#define next next12345
//#define prev prev12345
#define left lefdsf232
#define right rig43783
#define x1 x12345
#define y1 y12345

using namespace std;

template<typename T>
T gcd(T x, T y) {
    while (y > 0) {
        x %= y;
        swap(x, y);
    }
    return x;
}

template<class T>
T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}

template<class _T>
inline _T sqr(const _T &x) {
    return x * x;
}

template<class _T>
inline string tostr(const _T &a) {
    ostringstream os("");
    os << a;
    return os.str();
}

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const ld PI = 3.1415926535897932384626433832795L;

template<typename T>
inline void input(T &a) {
    static int ed;
    a = 0;
    while (!isdigit(ed = getchar()) && ed != '-') { }
    char neg = 0;
    if (ed == '-') {
        neg = 1;
        ed = getchar();
    }
    while (isdigit(ed)) {
        a = 10 * a + ed - '0';
        ed = getchar();
    }
    if (neg) a = -a;
}

template<typename T = int>
inline T nxt() {
    T res;
    input(res);
    return res;
}

void myassert(bool v) {
    assert(v);
    //cout << "FAIL\n";
    //exit(0);
}

mt19937 generator;

bool check(int v) {
    if (v < 2) return false;
    for (int i = 2; i * i <= v; ++i) {
        if (v % i == 0) {
            return false;
        }
    }
    return true;
}

long long pw(long long a, long long n, long long m) {
    ll res = 1;
    while (n) {
        if (n & 1ll) {
            res = res * a % m;
        }
        a = a * a % m;
        n >>= 1;
    }
    return res;
}

long long inv(long long a, long long p) {
    long long res = 1;
    while (a > 1) {
        res = res * (p - p / a) % p;
        a = p % a;
    }
    return res;
}
vector <int> p;

const int M = 60;

#define ld double

ld dp[1 << 19][M];
ld old[M];
ld old2[M];

void solve(int test) {
    p.resize(1, -1);
    int q = nxt();
    vector <pair <int, int> > r;

    while (q--) {
        int t = nxt();
        int v = nxt() - 1;
        r.pb(mp(t, v));
        if (t == 1) {
            p.pb(v);
        }
    }
    forn(i, M) dp[0][i] = 1;

    int sz = 1;
    for (auto v : r) {
        if (v.x == 1) {
            int cur = sz++;
            forn(i, M) dp[cur][i] = 1;
            int pr = cur;
            cur = p[cur];
            int h = 0;
            while (cur != -1 && h < M) {
                memcpy(old2, dp[cur], sizeof(old2));
                for (int j = h; j < M; ++j) {
                    if (pr != sz - 1) dp[cur][j] /= (0.5 + 0.5 * (j ? old[j - 1] : 0));
                    dp[cur][j] *= (0.5 + 0.5 * (j ? dp[pr][j - 1] : 0));
                }
                memcpy(old, old2, sizeof(old));
                pr = cur;
                cur = p[cur];
                ++h;
            }
        } else {
            ld ans = 0;
            for (int i = 0; i < M; ++i) {
                ans += 1 - dp[v.y][i];
            }
            printf("%.10f\n", (double)ans);
        }
    }
}

int main(int argc, char ** argv) {

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    #define fname "sequence"
    //freopen(fname".in", "r", stdin);
    //freopen(fname".out", "w", stdout);
#endif
    //ios_base::sync_with_stdio(false);
//    pre();
    int t = 1;

#ifdef LOCAL
#else
#endif
    int c = 0;

    while (t--) {
        solve(++c);
    }

#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
    return 0;
}


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

using namespace std;

typedef long long ll;

string N;
ll rep(string k, ll base, ll i) {
	int sp = max((int)(k.size() - N.size()), 0);
	string nk = k.substr(sp, k.size() - sp);
	string r = k.substr(0, sp);
	if (nk == "") return 0;
	if (N <= nk) {
		sp += 1;
		nk = k.substr(sp, k.size() - sp);
		r = k.substr(0, sp);
	}
	return pow(base, i)*stoll(nk) + rep(r, base, i+1);
}

int main() {
#ifdef _DEBUG
	freopen("758d.in", "r", stdin);
#endif
	ll n, k;
	cin >> n >> k;

	N = to_string(n);
	cout << rep(to_string(k), n, 0) << endl;
}


#include <bits/stdc++.h>
#define int         long long
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define nl          '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int a,b;
	cin >> a >> b;
	int num=1, den=1;
	queue<pair<int,pair<int, int> > > q;

	q.push({1,{num,den}});
	int ans=0;
	while(!q.empty()){
		pair<int, pii> u =q.front();
		q.pop();

		if(u.y.x==a and u.y.y==b){
			cout << u.x <<nl;
			return 0;
		}

		q.push({u.x+1, {u.y.x, u.y.x+u.y.y}});
		q.push({u.x+1, {u.y.x+u.y.y, u.y.y}});
	}

	cout << ans <<nl;
}


#include <iostream>
#define max(x,y) (x>y)?x:y
using namespace std;

int main() {
	// your code goes here
	int n,a,b;
	cin>>n>>a>>b;
	int ans=1,nmax=4,nmin=2,p=0,f=0,m=n,q=0,pm=n,pd1=0,pd2=0,flag=0;
	p=max(a,b);
	q=a+b-p;
	if(p==a)
	{
		f=1;
		nmax=4;
		nmin=2;
	}
	else
	{
		f=2;
		nmax=2;
		nmin=4;
	}
	while(nmax != 0 || nmin != 0)
	{
		//cout<<nmax<<" "<<nmin<<" "<<ans<<" "<<m<<endl;
		if(m-p >= 0 && nmax!= 0)
		{
			m=m-p;
			nmax--;
			pd1++;
		}
		else
		{
			if(m-q >= 0 && nmin>0)
			{
				m=m-q;
				nmin--;
				pd2++;
			}
			else
			{
				if(m!=0 && m!=pm && flag==0)
				{
					m=pm-q;
					nmin--;
					nmax=nmax+pd1;
					nmin=nmin+pd2;
				}
				else
				{
					m=n;
					ans++;
					flag=-1;
				}
				pm=m;
				pd1=0;
				pd2=0;
			}
		}
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , x1 , y11 , x2 , y2 , arr[1005][1005] , m = 1000000;

int bfs()
{
 queue<pair<int,int> >q;
  q.push(make_pair(x1,y11));
  arr[x1][y11]=0;
  while(!q.empty())
  {
    pair<int,int>p=q.front();
    q.pop();
    if((p.first==0&&x2==0)||(p.first==n&&x2==n))
    {
      m= min(m ,  arr[p.first][p.second]+abs(p.second-y2));
    }else if((p.second==0&&y2==0)||(p.second==n&&y2==n))
    {
      m= min ( m ,  arr[p.first][p.second]+abs(p.first-x2));
    }else
    {
      if (p.first==n||p.first==0) {
      	if (!arr[p.first][0])
        {
          arr[p.first][0]=arr[p.first][p.second]+p.second;
      		q.push (make_pair(p.first,0));
        }
        if (!arr[p.first][n])
        {
          arr[p.first][n]=arr[p.first][p.second]+n-p.second;
          q.push (make_pair(p.first,n));
        }
      }

      if (p.second==n||p.second==0) {
      	if (!arr[0][p.second])
        {
          arr[0][p.second]=arr[p.first][p.second]+p.first;
      		q.push (make_pair(0,p.second));
        }
        if (!arr[n][p.second])
        {
          arr[n][p.second]=arr[p.first][p.second]+n-p.first;
          q.push (make_pair(n,p.second));
        }
      }

    }

  }

}
 int main()
{
    ios::sync_with_stdio(false) ;
    cin.tie(0);
   cin>>n>>x1>>y11>>x2>>y2;
   if((x1==0&&x2==0)||(x1==n&&x2==n))
   {
     cout<<abs(y11-y2);
   }else if ((y11==0&&y2==0)||(y11==n&&y2==n))
      cout<<abs(x1-x2);
 else{
   bfs();
   cout<<m;
 }
return 0 ;
}


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

vector <int> st[30];
int n,c[30];
bool g[30][1<<24],f[30][1<<24];

int count(int mask)
{
	int ret=0;
	while (mask)
	{
		ret+=mask%2;
		mask/=2;
	}
	return ret;
}

int total(int mask)
{
	int ret=0,i=0;
	while (mask)
	{
		if (mask%2) ret+=c[++i];
		mask/=2;
	}
	return ret;
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&c[i]);
	sort(&c[1],&c[n+1]);
	for (int i=1;i<=n;i++)
	{
		for (int mask=0;mask<1<<i;mask++)
		{
			if (count(mask)>=2 && total(mask)+1==c[i])
				g[i][mask]=true,st[i].push_back(mask);
			else g[i][mask]=false;
		}
	}
	f[0][0]=true;
	for (int i=1;i<=n;i++)
	{
		if (c[i]==1) f[i][(1<<i)-1]=true;
		for (int mask=0;mask<1<<(i-1);mask++)
		{
			for (int j=0;j<st[i].size();j++)
			{
				if ((st[i][j]|mask)==mask) continue;
				f[i][mask|(1<<(i-1))]|=f[i-1][mask^st[i][j]];
			}
		}
	}
	if (f[n][1<<(n-1)]) printf("YES\n");
	else printf("NO\n");

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
//#include <conio>
using namespace std;

#define For(i,a,b) for(ll i = (a); i <= (b); i++)
#define Dow(i,a,b) for(ll i = (a); i >= (b); i--)
#define fill(a, x) memset(a, x, sizeof(a))
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second

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

const int maxN = 1e5 + 100;
const int mod = 1e9 + 7;
const int inf = 1e9;

ll n;
vector < ll > v;

int main() {
	#ifndef ONLINE_JUDGE
	    freopen ("input.txt", "r", stdin);
	    // freopen("output.txt", "w", stdout);
	#endif
	ios :: sync_with_stdio(false);

	//v.pb(0);
	//ll d = 0;
	for (int i = 1; i <= 3628800; i++) {
		int ok = 0;
		for (int j = 2; j <= 10; j++) if (i % j != 0) {
			ok = 1;
			break;
		}
		if (!ok) v.pb(i);
	}

	while (cin >> n) {
		while (true) {
			int ok = 0;
			int t = sz(v);
			for (int i = 0; i < t; i++) {
				for (int j = 2; j <= 10; j++) {
					ll tmp2 = v[i] * j;
					/*int xxx = 0;
					for (int i = 0; i < t; i++) {
						if (tmp2 == v[i]) {
							xxx = 1;
							break;
						}
					}
					*/
					//if (!xxx)
					v.pb(tmp2);
					if (tmp2 > 1e18) {
						ok = 1;
						break;
					}
				}
				if (ok) break;
			}
			if (ok) break;
		}
		ll i = 0;
		while (v[i] <= n) i++;
		cout << i << endl;
	}

	return 0;
}


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

    int n,i,x,t;
    cin>>n;
    vector<int> v;
    while(i<n)
    {

        t=x;
        v.push_back(x);
    }
    for(i=1;i<n&&v[i-1]<v[i];i++);
        for(;i<n&&v[i-1]==v[i];i++);
             for(;i<n&&v[i-1]>v[i];i++);

                 if(i==n)
                 cout<<"YES";
                 else
                 cout<<"NO";

    return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <hash_map>
#include <unordered_map>
#include <algorithm>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a))
#define PF printf
#define PB push_back
#define SC scanf
#define CT continue
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-14;
const int inf = 0x3f3f3f3f;
const int N=1e5+10;
const int mod=1e9+7;
const double pi=acos(-1);
using namespace std;

unordered_map<int,int> dp[1005];
string s;
int n,k,g[1005][1005];
void up(int i,int v)
{
     if(!i) {dp[i][v]=1;return;}
     for(int j=-k+1;j<k;j++) if(dp[i-1][j])
         dp[i][j+v]=1;g[i][j]=v;
}

int main()
{
    while(~SC("%d%d",&n,&k)){
         cin>>s;
         for(int i=0;i<=n;i++) dp[i].clear();
         MM(g,0);
         for(int i=0;i<s.size();i++){
             if(s[i]=='?'){
                up(i,1);up(i,-1);up(i,0);
             }else {
                if(s[i]=='W') up(i,1);
                if(s[i]=='L') up(i,-1);
                if(s[i]=='D') up(i,0);
             };
         }
         if(!dp[n-1][k]&&!dp[n-1][-k]) {PF("NO\n");CT;}
         string ans;
         for(int i)
         cout<<ans<<"\n";
    }
    return 0;
}


#include <iostream>
#include<queue>
#include<string>

using namespace std;

int main()
{
    int n, d;
    cin>>n>>d;
    string way;
    cin>>way;
    queue<pair<int,int> > q;
    q.push({0, 0});
    int ans = -1;
    while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        int idx = p.first;
        int moves = p.second;
        for(int i=idx+1;i<=idx+d;i++){
            if(way[i] == '1'&& i == n-1){
                ans = moves+1;
                break;
            }
            else if(way[i] == '1'){
                q.push({i, moves+1});
            }
        }
        if(ans != -1)
            break;
    }
    cout<<ans<<endl;
    //cout << "Hello world!" << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    map<int, bool> mp;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        mp[x] = true;
        if(mp.size() > 3){
            cout << "-1\n";
            return 0;
        }
    }
    if(mp.size() == 1){
        cout << "0\n";
        return 0;
    }
    else if(mp.size() == 2){
        int a[2];
        int j = 0;
        map<int, bool> :: iterator i;
        for(i = mp.begin(); i != mp.end(); i++){
            a[j++] = i->first;
        }
        sort(a, a+2);
        int k = 1;
        while(a[0] != a[1]){
            a[0] += k;
            a[1] -= k;
            k++;
        }
        cout << k-1 << "\n";
        return 0;
    }
    else{
        int a[3];
        int j = 0;
        map<int, bool> :: iterator i;
        for(i = mp.begin(); i != mp.end(); i++){
            a[j++] = i->first;
        }
        sort(a, a+3);
        if(a[2] - a[1] == a[1] - a[0])
            cout << a[2] - a[1] << "\n";
        else
           cout << "-1\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9+7;
const int MAXN = 1e6;

int a, b, n;
ll ans, f[MAXN];

bool ok(int x)
{
    while (x > 0)
    {
        if (x%10 != a && x%10 != b) return false;
        x /= 10;
    }
    return true;
}

ll Pow(ll x, ll e)
{
    if (e == 0) return 1LL;

    ll aux = Pow(x, e/2)%MOD;

    aux = (aux*aux)%MOD;

    if (e%2 == 0) return aux;
    return (x*aux)%MOD;
}

ll choose(ll x)
{
    ll k = (f[x]*f[n-x])%MOD;
    return (f[n]*Pow(k, MOD-2))%MOD;
}

void solve(void)
{
    for (int i = 0; i <= n; i++)
    {
        int sum = a*i+b*(n-i);
        if (ok(sum))
            ans += (choose(i))%MOD;
    }
}

int main(void)
{
    cin >> a >> b >> n;

    f[0] = 1LL;
    f[1] = 1LL;
    for (int i = 2; i <= n; i++)
        f[i] = (i*f[i-1])%MOD;

    solve();
    cout << ans%MOD << "\n";
}


#include<bits/stdc++.h>
#define all(v)      ((v).begin()),  ((v).end())
#define sz(v)        ((int)((v).size()))
typedef long long ll;
using namespace std;
int main(void) {

	int n;
	cin>>n;
	if(n%2!=0)
		cout<<"YES";
	else cout<<"NO";
	return 0;
}


//i hate _____
#include<bits/stdc++.h>

#define ll long long int
#define ld long double
#define pb push_back
#define mk make_pair
#define ii pair<int,int>

using namespace std;
double pi=2*acos(0.0);

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

template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for (const auto &x : v) os << " " << x;
        return os << '}';
}

void solve(){
    int n; cin>>n;
    int ans=0,cnt=0;
    char ch;
    while (true){
        ch=getchar();
        if (ch=='\n'){
            ans=max(ans,cnt); break;
        }
        if (ch==' '){
            ans=max(ans,cnt); cnt=0;
        }
        if (ch>='A' && ch<='Z') cnt++;
    }
    cout << ans << endl;
}

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

    int test_cases=1;
    //cin>>test_cases;

    for (int tc=1;tc<=test_cases;tc++){

        solve();
        //cout << "Case " << tc << ": " << solve() <<endl;
        //printf("Case %d: %.10lf\n",tc,ans);

    }

    return 0;
}
/*
    problems are the sign of life
*/


#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define ll long long
using namespace std;

ll mod=1000000007;
int n,k, cnt1[100005], used[100005], used2[100005], fif=0, cen=0;
ll cnt2[100005];
ll fact[200005];
vector<pair<pair<int,int>,int> > v,v2;
vector<pair<int,int> > v4;
vector<int> v3;

ll pw(ll x, int y)
{
    if(y==0) return 1;
    ll ans= pw(x,y/2);
    ans*=ans;
    ans%=mod;
    if(y%2) ans*=x;
    ans%=mod;
    return ans;
}

ll inv(ll x)
{
    return pw(x,mod-2);
}

ll C(int x, int y)
{
    if(x<y) return 0;
    ll ans= fact[x];
    ans*= inv(fact[y]);
    ans%=mod;
    ans*= inv(fact[x-y]);
    ans%=mod;
    return ans;
}

void calc(int x, int y, int z)
{
    v4.clear();
    pair<int,int> p= mp(0,0);
    while(p.yy<y)
    {
        if(p.yy*100 + 100<=k)
        {
            p.yy++;
            v4.pb(p);
        }
        else break;
        int x1= p.yy*100;
        x1=k-x1;

        p.xx=1;
        while(p.xx<=x && p.xx*50<=x1)
        {
            v4.pb(p);
            p.xx++;
        }
    }
    p=mp(0,0);
    while(p.xx<x)
    {
        if(p.xx*50 + 50<=k)
        {
            p.xx++;
            v4.pb(p);
        }
        else break;
    }
//    printf("%d %d %d\n",x,y,z);
//    for(int i=0; i<v4.size(); i++)
//    {
//        printf(" %d %d\n",v4[i].xx, v4[i].yy);
//    }
    if(z==2)
    {
        for(int i=0; i<v4.size(); i++)
        {
            v4[i].xx*=-1;
            v4[i].yy*=-1;
        }
    }
}

int calc2(int x, int y, int z)
{
    x*=52;
    x+=y;
    x*=3;
    x+=z;
    return x;
}

void bfs()
{
    for(int i=0; i<v.size(); i++)
    {
        int x=v[i].xx.xx, y=v[i].xx.yy, z=v[i].yy;
        int c= calc2(x,y,z);
        int x1=x, y1=y, z1=2;
        if(z==2)
        {
            x1=fif-x;
            y1=cen-y;
            z1=1;
        }
        calc(x1,y1,z);

        for(int j=0; j<v4.size(); j++)
        {
            int x2= x-v4[j].xx;
            int y2= y-v4[j].yy;
            int c1= calc2(x2,y2,z1);
            if(used[c1]) continue;

            ll com=1;
            com= C(x1, abs(v4[j].xx));
            com*= C(y1, abs(v4[j].yy));
            com%=mod;
//            printf("%d %d %d %d %lld\n",x1,v4[j].xx, y1, v4[j].yy, com);

            cnt1[c1]= cnt1[c]+1;
            cnt2[c1]+= (cnt2[c]*com)%mod;
            cnt2[c1]%=mod;
            if(used2[c1]) continue;
            v3.pb(c1);
            v2.pb(mp(mp(x2,y2),z1));
//            printf("%d\n",c1);
            used2[c1]=1;
        }
    }
    for(int i=0; i<v3.size(); i++)
    {
        if(v3[i]== 2) return ;
        used[v3[i]]=1;
    }
    v3.clear();
    v.clear();
    v= v2;
    v2.clear();
    bfs();
}

int main()
{
    fact[0]=1;
    for(int i=1; i<100000; i++)
    {
        fact[i]= (fact[i-1]*i)%mod;
    }
    int a;
    scanf("%d %d",&n,&k);

    for(int i=0; i<n; i++)
    {
        scanf("%d",&a);
        if(a==50) fif++;
        else cen++;
    }
    int c= calc2(fif,cen,1);
    cnt2[c]=1;
    v.pb(mp(mp(fif,cen),1));
    bfs();
    if(cnt2[2]) printf("%d\n%lld\n",cnt1[2], cnt2[2]);
    else printf("-1\n0\n");
}


#include <cstdio>

int main() {
   int a, b, c;
   scanf("%d%d%d", &a, &b, &c);
   printf("%d\n", max(a, max(b, c)));
}


#include<bits/stdc++.h>
using namespace std;
long long arr[103];
vector<long long> seq;
vector<long long> ndw;
int main()
{
    long long n,k,i,i2,pos;
    cin>>n>>k;
    for(i=0;i<n;++i)
    {
        cin>>arr[i];
    }
    for(i=0;i<k;++i)
    {
        for(i2=0;i2<n;++i2)
        {
            seq.push_back(arr[i2]);
        }
    }
    for(i=0;i<n*k;++i)
    {
        pos=upper_bound(ndw.begin(),ndw.end(),seq[i])-ndw.begin();
        if(pos==ndw.size()) ndw.push_back(seq[i]);
        else ndw[pos]=seq[i];
    }
    cout<<ndw.size()<<endl;

}


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    long long n;
    bool f=false;
    cin >> n;
    for (long long i=1; i*i+i<=n; ++i)
    {
        long long x;
        x=(-i-sqrtl(i*i+4*n))/2;
        if (x>0)
        {if (i==(n-x*x)/x && (n-x*x)%x==0) {cout << x; f=true; break;}}
        else
        {
            x=(-i+sqrtl(i*i+4*n))/2;
            if (x>0)
            {if (i==(n-x*x)/x && (n-x*x)%x==0) {cout << x; f=true; break;}}
        }

    }
    if (f==false) cout << "-1";
    return 0;
}


//# pragma GCC optimize ("O0")
//# pragma GCC optimize ("Ofast")
//# pragma GCC optimize ("unroll-loops")

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define UN -1
#define VIS -2
#define EX -3
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define pob pop_back()
#define puf push_front
#define pof pop_front()
#define clr(a) (a).clear()
#define sz(a) (int)(a).size()
#define forn(t, n) for(int t = 0; t < n; t++)
#define foren(t, n) for(int t = 1; t <= n; t++)
#define llm LONG_LONG_MAX
#define llmn LONG_LONG_MIN
#define intm INT_MAX
#define intmn INT_MIN
#define dblm DBL_MAX
#define dblmn DBL_MIN
#define mod 1000000007
#define mod2 1000000009
#define mil 1111111
#define bil 1111111111
#define pi M_PI
#define euler 2.718281828459
#define eps 1e-12
#define degrad (pi/180.0)
#define raddeg (180.0/pi)
#define uset unordered_set
#define umap unordered_map
#define fio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef vector<pll> vpll;
typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<vi> vvi;
typedef pair<int, ii> iii; typedef pair<ii, ii> iiii; typedef pair<ll, pll> tll;
typedef vector<vii> vvii; typedef vector<vll> vvll; typedef vector<char> vc; typedef vector<vc> vvc;
typedef vector<bool> vb; typedef vector<vb> vvb; typedef queue<int> qi; typedef queue<ll> qll;
typedef queue<ii> qii; typedef vector<tll> vtll; typedef vector<iii> viii; typedef pair<pll, pll> ppll;
typedef priority_queue<int> pqi; typedef priority_queue<ll> pqll;
typedef pair<int, ii> iii; typedef vector<iii> viii; typedef vector<string> vs;
typedef vector<double> vd; typedef pair<double, double> dd; typedef vector<dd> vdd; typedef pair<ii, int> riii;
typedef vector<vector<double> > vvd; typedef vector<vvi> vvvi;

template<class T> void pv(vector<T> &vec) { forn(i, sz(vec)) cout << vec[i] << (i == sz(vec) - 1 ? '\n' : ' '); }

//ifstream in("/home/danza/CLionProjects/codeforces/in.in");
//ofstream out("/home/danza/CLionProjects/codeforces/out2.out");
//#define cin in
//#define cout out

//#define FAST
#define SLOW
//#define GENERATOR
//#define DEBUG

#ifdef DEBUG
#ifdef FAST
ifstream in("/home/danza/CLionProjects/codeforces/in.in");
ofstream out("/home/danza/CLionProjects/codeforces/out2.out");
#endif
#ifdef SLOW
ifstream in("/home/danza/CLionProjects/codeforces/in.in");
ofstream out("/home/danza/CLionProjects/codeforces/out.out");
#endif
#ifdef GENERATOR
ofstream out("/home/danza/CLionProjects/codeforces/in.in");
#endif
#define cin in
#define cout out
#endif

#ifdef FAST
ll la, ra, ta, lb, rb, tb, cyclen;

void init() {
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    if(la < lb) {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }
    cyclen = (ta * tb) / __gcd(ta, tb);
}

ll inter(pll X, pll Y) {
    if(X.first > Y.first)
        swap(X, Y);
    if(X.second < Y.first)
        return 0LL;
    if(X.second >= Y.second)
        return Y.second - Y.first + 1;
    return X.second - Y.first + 1;
}

ll cnt(ll x) {
    ll a_start = la + x * ta;
    ll b_start1 = lb + ((a_start - lb + 1) / tb) * tb;
    ll b_start2 = b_start1 + tb;
    ll b_start3 = b_start1 - tb;
    ll len1 = inter({b_start1, b_start1 + (rb - lb)}, {a_start, a_start + (ra - la)});
    ll len2 = inter({b_start2, b_start2 + (rb - lb)}, {a_start, a_start + (ra - la)});
    ll len3 = inter({b_start3, b_start3 + (rb - lb)}, {a_start, a_start + (ra - la)});
    if(b_start2 > a_start + (ra - la) + 1)
        len2 = 0;
    return max(len1, max(len2, len3));
}

ll ternary_search() {
    ll st = 0, en = cyclen / ta + 1, st_mid, en_mid;
    while(en - st > 5) {
        ll third = (en - st) / 3;
        st_mid = st + third;
        en_mid = st + 2 * third;
        ll st_mid_res = cnt(st_mid);
        ll en_mid_res = cnt(en_mid);
        if(st_mid_res == en_mid_res) {
            if(cnt(st) > cnt(en - 1))
                en = en_mid;
            else
                st = st_mid;
        }
        else if(st_mid_res > en_mid_res)
            en = en_mid;
        else
            st = st_mid;
    }
    vll res;
    for(ll i = st; i < en; i++)
        res.pb(cnt(i));
    ll mx = *max_element(all(res));
    forn(i, sz(res)) {
        if(res[i] == mx)
            return st + i;
    }
    assert(1 == 0);
}

ll solve() {
    ll x = ternary_search();
    ll ans = cnt(x);
    cout << "for x=" << x << '\n';
    return ans;
}

int main() {
#ifndef  DEBUG
    fio
#endif
#ifdef DEBUG
    ll TC;
    cin >> TC;
    foren(tc, TC) {
        string s;
        cin >> s >> s;
        cout << "Case #" << tc << ":\n";
#endif
    init();
    cout << solve() << '\n';

#ifdef DEBUG
}
#endif

return 0;
}
#endif

#ifdef SLOW
ll la, ra, ta, lb, rb, tb, cyclen;

void init() {
    cin >> la >> ra >> ta >> lb >> rb >> tb;
    if(la < lb) {
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }
    cyclen = (ta * tb) / __gcd(ta, tb);
}

ll inter(pll X, pll Y) {
    if(X.first > Y.first)
        swap(X, Y);
    if(X.second < Y.first)
        return 0LL;
    if(X.second >= Y.second)
        return Y.second - Y.first + 1;
    return X.second - Y.first + 1;
}

ll cnt(ll x) {
    ll a_start = la + x * ta;
    ll b_start1 = lb + ((a_start - lb + 1) / tb) * tb;
    ll b_start2 = b_start1 + tb;
    ll b_start3 = b_start1 - tb;
    ll len1 = inter({b_start1, b_start1 + (rb - lb)}, {a_start, a_start + (ra - la)});
    ll len2 = inter({b_start2, b_start2 + (rb - lb)}, {a_start, a_start + (ra - la)});
    ll len3 = inter({b_start3, b_start3 + (rb - lb)}, {a_start, a_start + (ra - la)});
    if(b_start2 > a_start + (ra - la) + 1)
        len2 = 0;
    return max(len1, max(len2, len3));
}

ll linear_search() {
    ll st = 0, en = cyclen / ta + 1;
    vll res;
    for(ll i = st; i < en; i++)
        res.pb(cnt(i));
    ll mx = *max_element(all(res));
    forn(i, sz(res)) {
        if(res[i] == mx)
            return st + i;
    }
    assert(1 == 0);
}

ll solve() {
    ll x = linear_search();
    ll ans = cnt(x);
    return ans;
}

int main() {
#ifdef DEBUG
    ll TC;
    cin >> TC;
    foren(tc, TC) {
        string s; cin >> s >> s;
        cout << "Case #" << tc << ":\n";
#endif
        init();
        cout << solve() << '\n';

#ifdef DEBUG
    }
#endif

    return 0;
}
#endif

#ifdef GENERATOR
int main() {
    mt19937 mt;
    ll TC = 100;
    cout << TC << '\n';
    ll range = 100;
    foren(tc, TC) {
        cout << "Case #" << tc << ":\n";
        forn(i, 2) {
            ll l = mt() % range, r = -1;
            while (r < l)
                r = mt() % range;
            cout << l << ' ' << r << ' ' << (r - l + 1) + mt() % range + 3 << '\n';
        }
        cout << "\n\n";
    }

    return 0;
}
#endif


#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<array>
#include<cmath>

using namespace std;

struct point {
  double x, y;

  template<typename ...P>
  bool collinear(P const & ...points) {
    array<point, sizeof...(points)> ps {points...};

    //x
    int times = 0;
    for(auto p : ps)
      times += (x == p.x ? 1 : 0);

    if(times == ps.size())
      return true;

    //y
    times = 0;
    for(auto p : ps)
      times += (y == p.y ? 1 : 0);

    if(times == ps.size())
      return true;

    return false;
  }

  bool between(point const & a, point const & b) {
     /*
      *
      * a          b
      *      x
      *
      */

      if(
          (a.x <= x && x <= b.x) ||
          (a.y <= y && y <= b.y) ||
          (a.x >= x && x >= b.x) ||
          (a.y >= y && y >= b.y)
        )
        return true;
  }

  friend istream& operator>>(istream& in, point & self) {
    in >> self.x >> self.y;
  }

};

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

  int ans;

  if(a.collinear(b, c))
    ans = 1;
  else if(
          (a.collinear(b) && c.between(a, b)) ||
          (a.collinear(c) && b.between(a, c)) ||
          (c.collinear(b) && a.between(c, b))
         )
     ans = 3;
  else if(
          ((a.x != b.x) && (a.x != c.x) && (b.x != c.x)) &&
          ((a.y != b.y) && (a.y != c.y) && (b.y != c.y))
         )
     ans = 3;
  else
     ans = 2;
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int c, d, n, m, k, req;
int dp[10001][10001];
int rec(int ele, int add, int sum)
{
    if(dp[ele][add]!=-1)
        return dp[ele][add];
    if(sum>=req)
        return dp[ele][add]=ele*c+add*d;
    return min(rec(ele+1, add, sum+n), rec(ele, add+1, sum+1));

}
int main()
{
 scanf("%d%d%d%d%d", &c, &d, &n, &m, &k);
memset(dp, -1, sizeof dp); req=n*m-k;
printf("%d",rec(0,0,0));
}


//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;

#define FOR(i,f,t) for(int i=(int)f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=(int)f; i>(int)t; i--)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define pb push_back
#define SYNC ios_base::sync_with_stdio(false)
#define inf 2248012
#define mp make_pair
#define F first
#define S second
#define ri(x) scanf("%d",&x)
#define rii(x,y) scanf("%d %d",&x,&y)
#define riii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define lri(x) scanf("%lld",&x)
#define lrii(x,y) scanf("%lld %lld",&x,&y)
#define lriii(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;

const int MAXN = 5e5+3; //AREGLAAAAAAAAR!!!!
int n;
int a[24];
vector <int> v1,v2;
int start2;
int cnt;
int main(){
	FOR(i,0,24)ri(a[i]);
	if((a[12]==a[13])&&(a[13]==a[14])&&(a[14]==a[15])&&(a[16]==a[17])&&(a[17]==a[18])&&(a[18]==a[19])&&(a[0]==a[1])&&(a[1]==a[2])&&(a[2]==a[3])&&(a[8]==a[9])&&(a[9]==a[10])&&(a[10]==a[11])&&(a[4]==a[5])&&(a[5]==a[6])&&(a[6]==a[7])&&(a[20]==a[21])&&(a[21]==a[22])&&(a[22]==a[23])){printf("NO\n");return 0;}
	else if((a[12]==a[13])&&(a[13]==a[14])&&(a[14]==a[15])&&(a[16]==a[17])&&(a[17]==a[18])&&(a[18]==a[19])){
		v1.pb(0);v1.pb(2);v1.pb(4);v1.pb(6);v1.pb(8);v1.pb(10);v1.pb(23);v1.pb(21);
		v2.pb(1);v2.pb(3);v2.pb(5);v2.pb(7);v2.pb(9);v2.pb(11);v2.pb(22);v2.pb(20);
		FOR(i,0,8){
			if(v1[0]==v2[i])start=i;
		}
		if(v2[start]!=v2[(start+1)%n])start=(start-1+n)%n;
		if(v1[0]!=v1[1])start2=n-1;
		FOR(i,0,n){
			if(v1[(start2+i)%n]!=v2[start]){
				printf("NO\n");
				return 0;
			}
			start=(start+1)%8;
		}
		cnt=0;
		FOR(i,0,n){
			if(v2[start]==v2[(start+1)%8])cnt++;
			else if(cnt<1){
				printf("NOU\n");
				return 0;
			}
			else cnt=0;
			start=(start+1)%8;
		}
		printf("YES\n");
		return 0;
	}
	else if((a[0]==a[1])&&(a[1]==a[2])&&(a[2]==a[3])&&(a[8]==a[9])&&(a[9]==a[10])&&(a[10]==a[11])){
		v1.pb(12);v1.pb(13);v1.pb(4);v1.pb(5);v1.pb(16);v1.pb(17);v1.pb(20);v1.pb(21);
		v2.pb(14);v2.pb(15);v2.pb(6);v2.pb(7);v2.pb(18);v2.pb(19);v2.pb(22);v2.pb(23);
		FOR(i,0,8){
			if(v1[0]==v2[i])start=i;
		}
		if(v2[start]!=v2[(start+1)%n])start=(start-1+n)%n;
		if(v1[0]!=v1[1])start2=n-1;
		FOR(i,0,n){
			if(v1[(start2+i)%n]!=v2[start]){
				printf("NO\n");
				return 0;
			}
			start=(start+1)%8;
		}
		cnt=0;
		FOR(i,0,n){
			if(v2[start]==v2[(start+1)%8])cnt++;
			else if(cnt<1){
				printf("NOU\n");
				return 0;
			}
			else cnt=0;
			start=(start+1)%8;
		}
		printf("YES\n");
		return 0;
	}
	else if((a[4]==a[5])&&(a[5]==a[6])&&(a[6]==a[7])&&(a[20]==a[21])&&(a[21]==a[22])&&(a[22]==a[23])){
		v1.pb(2);v1.pb(3);v1.pb(16);v1.pb(18);v1.pb(9);v1.pb(8);v1.pb(15);v1.pb(13);
		v2.pb(0);v2.pb(1);v2.pb(17);v2.pb(19);v2.pb(11);v2.pb(10);v2.pb(14);v2.pb(12);
		FOR(i,0,8){
			if(v1[0]==v2[i])start=i;
		}
		if(v2[start]!=v2[(start+1)%n])start=(start-1+n)%n;
		if(v1[0]!=v1[1])start2=n-1;
		FOR(i,0,n){
			if(v1[(start2+i)%n]!=v2[start]){
				printf("NO\n");
				return 0;
			}
			start=(start+1)%8;
		}
		cnt=0;
		FOR(i,0,n){
			if(v2[start]==v2[(start+1)%8])cnt++;
			else if(cnt<1){
				printf("NOU\n");
				return 0;
			}
			else cnt=0;
			start=(start+1)%8;
		}
		return 0;
	}

	printf("NO\n");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <functional>
using namespace std;
typedef long long int ll;
template <class T>
T power(T a, T b) { T x; if (b == 0) x = 1; else x = a; for (size_t i = 1; i < b; i++)  x *= a; return x; }

ll n, m, a, b, c, ab, ba, x, y, avg, sum, arr2[200000], arr3[1000];
char arr[101][101], c1;
vector<pair<string, ll>> vec(3);
vector<ll> v;
bool f, f1, f2; string s[3];
map<string, int> mp;
bool sorto(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; }

int main() {
    for (size_t i = 0; i < 3; i++)
    {
        cin >> vec[i].first; vec[i].second = i + 1;
    }
    sort(s, s + 3);
    if (vec[0].first == vec[1].first && vec[1].first == vec[2].first) cout << '?';
    else if (vec[0].first != vec[1].first && vec[1].first != vec[2].first && vec[0].first != vec[2].first) cout << '?';
    else {
        if (vec[0].first == "paper" && vec[1].first == vec[2].first && vec[1].first == "rock") a = vec[0].second;
        else if (vec[0].first == "rock" && vec[1].first == vec[2].first && vec[1].first == "scissors") a = vec[0].second;
        else if (vec[2].first == "scissors" && vec[0].first == vec[1].first && vec[1].first == "paper") a = vec[2].second;
        else cout << '?';
    }
    if (a == 1) cout << 'F';
    else if (a == 2) cout << 'M';
    else if (a == 3) cout << 'S';
}


//*******Abhijit Burman***********//
//Jalpaiguri Government Engineering College/

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mk make_pair
#define MAXX (1000000000000000000+7)
#define fio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)

bool SQRT(string p)
{
    ll num = stoi(p);
    ll i=sqrt(num);
    if(num==i*i)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int solve()
{

    string s;
    cin>>s;
    ll n = s.length();
    if(SQRT(s)==true)
    {
        cout<<0<<endl;
    }
    else
    {
        ll k=0;
        string temp[1000];
        ll flag = pow(2,n);
        // cout<<(1<<n)<<endl;
        for(int i=1;i<(1<<n);i++)
        {
            string str="";
            for(int j=0;j<n;j++)
            {
                if(i&(1<<j))
                {
                    str+=s[j];
                }
            }
            temp[k++]=str;
        }
        ll mini=MAXX;
        for(int i=0;i<k;i++)
        {
            if(SQRT(temp[i])==true)
            {
                ll diff = n-temp[i].length();
                mini = min(mini,diff);
            }
            // cout<<temp[i]<<endl;
        }
        if(mini==MAXX)
        {
            cout<<-1<<endl;
        }
        else
            cout<<mini<<endl;
    }
    return 0;
}

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