#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;

const int maxN = 2e3; int n, m;
char a[maxN][maxN];

int getPoints(int i, int j, bool taka)
{
  if (!taka)
    return a[i][j] == '+' ? 1 : -1;
  return a[i][j] == '-' ? 1 : -1;
}
int memo[maxN][maxN][2]; bool visited[maxN][maxN][2];
int solve(int i = 0, int j = 0, bool taka = true)
{
  if (i == n - 1 && j == m - 1)
    return getPoints(i, j, taka);

  int &ans = memo[i][j][taka]; bool &vis = visited[i][j][taka];
  if (vis) return ans;
  vis = true;

  if (taka)
  {
    ans = -1e9;
    if (i + 1 < n) ans = max(ans, solve(i + 1, j, !taka));
    if (j + 1 < m) ans = max(ans, solve(i, j + 1, !taka));
    if (!(i == 0 && j == 0)) ans += getPoints(i, j, taka);
  }
  else
  {
    ans = 1e9;
    if (i + 1 < n) ans = min(ans, solve(i + 1, j, !taka));
    if (j + 1 < m) ans = min(ans, solve(i, j + 1, !taka));
    if (!(i == 0 && j == 0)) ans += getPoints(i, j, taka);
  }

  return ans;
}

int main()
{
  while (~scanf("%d %d", &n, &m))
  {
    for (int i = 0; i < n; i++)
      scanf(" %s", a[i]);

    memset(visited, false, sizeof(visited));
    int ans = solve();
    if (ans == 0 || (n == 1 && m == 1))
      printf("Draw\n");
    else if (ans > 0)
      printf("Takahashi\n");
    else
      printf("Aoki\n");
  }
  return 0;
}

/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

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

ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

void cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}

ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
ll dp[2200][2200];
ll sumh[2200][2200], sumw[2200][2200], sum[2200][2200];
ll extGCD(ll a, ll b, ll &x, ll &y){
	if (b == 0){
		x = 1;
		y = 0;
		return a;
	}

	ll d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll getinv(ll num){
    ll x, y;
    ll g = extGCD(num, MOD, x, y);
    while (x < 0) x += MOD;
    x %= MOD;
    return x;
}
int main(void){
    ll h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, h){
        cin >> s[i];
    }
    dp[0][0] = 1;
    sumh[0][0] = 1;
    sumw[0][0] = 1;
    sum[0][0] = 1;
    for(ll i = 0; i < h; i++){
        for(ll j = 0; j < w; j++){
            if(s[i][j] == '#') continue;
            dp[i + 1][j] += dp[i][j] + sumh[i][j];
            dp[i + 1][j] %= MOD;
            dp[i][j + 1] += dp[i][j] + sumw[i][j];
            dp[i][j + 1] %= MOD;
            dp[i + 1][j + 1] += dp[i][j] + sum[i][j];
            dp[i + 1][j + 1] %= MOD;

            sumh[i + 1][j] += dp[i][j] + sumh[i][j];
            sumh[i + 1][j] %= MOD;
            sumw[i][j + 1] += dp[i][j] + sumw[i][j];
            sumw[i][j + 1] %= MOD;
            sum[i + 1][j + 1] += dp[i][j] + sum[i][j];
            sum[i + 1][j + 1] %= MOD;
        }
    }

    cout << dp[h - 1][w - 1] * getinv(2) % MOD << endl;
}


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

#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
    ~printer(){cerr<<endl;}
    Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
    Rics(string x){cerr<<x;return *this;}
    Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
    Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
    Gene c >Rics(rge<c> x){
        *this,"["; for(auto it = x.b; it != x.e; ++it)
            *this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r) {
    return uniform_int_distribution<int>(l, r) (rng);
}

const int N = 105;

int mod;
int dp[N][N*N*N];

int main() {
//    freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, K;
    cin >> n >> K >> mod;

    dp[0][0] = 1;
    int sum = 0;
    for(int i = 1; i <= n; i++) {
        sum += K * i;
        for(int s1 = 0; s1 < i; s1++) {
            int ways = 0;
            for(int s = s1; s <= sum; s += i) {
                ways += dp[i-1][s];
                if(ways >= mod) ways -= mod;
                if(s-(K+1)*i >= 0) {
                    ways -= dp[i-1][s-(K+1)*i];
                    if(ways < 0) ways += mod;
                }
                dp[i][s] = ways;
//                debug(), dbg(i), dbg(s), dbg(dp[i][s]);
            }
        }
    }
    for(int avg = 1; avg <= n; avg++) {
        int ways = 0;
        for(int s = 0; s <= sum; s++) {
            ways += 1ll*dp[avg-1][s]*dp[n-avg][s]%mod;
            if(ways >= mod) ways -= mod;
        }
        ways = 1ll*ways*(K+1)%mod;
        ways--;
        if(ways < 0) ways += mod;
        cout << ways << "\n";
    }
}


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

int main()
{
    long long A, B;
    cin>>A>>B;

    vector<int> F;
    for (int f=2; f<=72; f++)
    {
        bool ok = true;
        for (int i=2; i<f; i++)
            if (f%i==0)
                ok = false;
        if (ok)
            F.push_back(f);
    }
    int n = (int)F.size();

    vector<long long> T(1<<n);
    T[0] = 1;

    for (long long c=A; c<=B; c++)
    {
        vector<long long> P = T;
        int b = 0;
        for (int i=0; i<n; i++)
            if (c%F[i]==0)
                b |= 1<<i;

        for (int i=0; i<1<<n; i++)
            if ((b&i)==0)
                T[b|i] += P[i];
    }
    long long ans = 0;
    for (long long t: T)
        ans += t;
    cout<<ans<<endl;
}

#include <algorithm>
#include <iostream>
#include <deque>
#include <climits>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>

// #include <atcoder/convolution>

using namespace std;
using ll = long long;
#define len(s) static_cast<ll>(s.size())
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(c) c.begin(), c.end()

template<typename T, const char* fmt>
struct INPUT {
    T value;
    INPUT() { scanf(fmt, &value); }
    operator T() { return value; }
};
const char IC_FMT[] = "%c";
const char ILL_FMT[] = "%lld";
using CHAR = INPUT<char, IC_FMT>;
using LL = INPUT<ll, ILL_FMT>;

template<typename S>
std::ostream &operator<<(std::ostream &os, const vector<S> &vec) {
    auto size = len(vec);
    rep(i, size) {
        cout << vec[i];
        if (i < size - 1) cout << ' ';
    }
    return cout;
}

template<typename S, typename T>
std::ostream &operator<<(std::ostream &os, const pair<S, T> &pair) {
    return cout << pair.first << ' ' << pair.second;
}

template <typename T>
void print(T t) {
    cout << t << endl;
}

template <typename T, typename S, typename... Args>
void print(T t, S s, Args... args) {
    cout << t << ' ';
    print(s, args...);
}

template<typename... Args>
void debug(Args... args) {
    #ifndef NDEBUG
    cout << "\x1b[1;32m";
    print(args...);
    cout << "\x1b[0m";
    #endif
}

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

const vector<ll> pr_list = {
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
    31, 37, 41, 43, 47, 53, 59, 61, 67, 71};

ll dp[74][1 << 20];

int main() {
    LL A, B;
    debug(A, B);

    ll mask[73];
    for (ll i = A; i <= B; i++) {
        mask[i - A] = 0;
        rep(j, 20) {
            if (i % pr_list[j] == 0) {
                mask[i - A] |= 1 << j;
            }
        }
    }

    memset(&dp, 0, sizeof(dp));
    dp[0][0] = 1;

    for (ll i = A; i <= B; i++) {
        rep(j, 1 << 20) {
            if (!(j & mask[i - A])) {
                dp[i - A + 1][j | mask[i - A]] += dp[i - A][j];
            }
            dp[i - A + 1][j] += dp[i - A][j];
        }
    }

    ll count = 0;
    rep(j, 1 << 20) {
        count += dp[B - A + 1][j];
    }
    print(count);
    return 0;
}


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

long long power(long long a, long long b) {
    if(b == 0)
        return 1;
    if(b == 1)
        return a;
    long long x = power(a, b / 2);
    x *= x;
    if(b & 1)
        x *= a;
    return x;
}

int main() {
    long long n;
    cin >> n;
    long long a = 1;
    while(true) {
        long long x = power(5LL, a);
        if(x > n)
            break;
        long long y = n - x;
        long long b = 1;
        while(power(3LL, b) <= y) {
            b++;
        }
        long long shoot = power(3LL, b);
        shoot /= 3;
        if(shoot == y && b > 1) {
            cout << b - 1 << " " << a << endl;
            return 0;
        }
        a++;
    }
    cout << "-1" << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)

int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int a[200001],res=0,n;
bool vis[200001];
void calc(int x){
	if(x==res){
		++res;
		while(vis[res]==true)++res;
	}
	else vis[x]=true;
}
int main()
{
	n=read();
	For(i,1,n) a[i]=read();
	For(i,1,n){
		calc(a[i]);
		printf("%d\n",res);
	}
	return 0;
}


#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template<typename T>
long long modpow(const T n,const T p,const T mod);
template<typename T>
long long modinv(const T n,const T mod);
template<typename T>
bool chmax(T &a,const T &b);
template<typename T>
bool chmin(T &a,const T &b);

long long inf=1000000007;

int main(){
	long long n;
	cin>>n;
	vector<vector<long long>> tosi(n,vector<long long>(3));

	for(long long i=0;i<n;i++) cin>>tosi.at(i).at(0)>>tosi.at(i).at(1)>>tosi.at(i).at(2);
	vector<vector<long long>> dp(n,vector<long long>(modpow((long long)2,n,inf),inf));
	dp.at(0).at(0)=0;

	for(long long i=0;i<(1<<n);i++){
		bitset<18> bits=i;
		for(long long j=0;j<n;j++){
			if(!(i==0 && j==0) && bits.test(j)==false) continue;
			for(long long k=0;k<n;k++){
				if(bits.test(k)) continue;

				long long a=abs(tosi.at(j).at(0)-tosi.at(k).at(0));
				long long b=abs(tosi.at(j).at(1)-tosi.at(k).at(1));
				long long c=max((long long)0,tosi.at(k).at(2)-tosi.at(j).at(2));
				chmin(dp.at(k).at(i+modpow((long long)2,k,inf)),dp.at(j).at(i)+a+b+c);
			}
		}
	}

	cout<<dp.at(0).at(modpow((long long)2,n,inf)-1)<<endl;

	return 0;
}

template<typename T>
long long modpow(const T n,const T p,const T mod){
	if(p==0) return 1;
	if(p%2==0){
		long long a=modpow(n,p/2,mod);
		return a*a%mod;
	}
	if(p%2==1) return (modpow(n,p-1,mod)*n)%mod;
	cerr<<"ERROR"<<endl;
	return 1;
}

template<typename T>
long long modinv(const T n,const T mod){
	return modpow(n,mod-2,mod);
}

template<typename T>
bool chmax(T &a,const T &b){
	if(a<b){
		a=b;
		return 1;
	}
	return 0;
}

template<typename T>
bool chmin(T &a,const T &b){
	if(a>b){
		a=b;
		return 1;
	}
	return 0;
}

#include <bits/stdc++.h>
#define LL_INFI 0x3f3f3f3f
using namespace std;

long long ** creat2dArray(int N);
void delete2dArray(long long ** dp, int N);

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

    int X[N + 1];
    int Y[N + 1];
    int Z[N + 1];
    for(int i = 1; i <= N; i++)
        scanf("%d %d %d", &X[i], &Y[i], &Z[i]);

    long long dis[18][18] = {LL_INFI};
    for(int i = 1; i <= N; i++)
    {
        dis[i][i] = 0;
        for(int j = 1; j <= N; j++)
        {
            dis[i][j] = abs(X[j] - X[i]) + abs(Y[j] - Y[i]) + max(0, Z[j] - Z[i]);
        }
    }

    // floyd
    for(int k = 1; k <= N; k++)
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= N; j++)
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);

    long long **dp = creat2dArray(N);
    for(int i = 1; i < (1 << N); i++)
    {
        for(int j = 0; j < N; j++)
        {
            if(!((i>>j) & 1))
            {
                for(int k = 0; k < N; k++)
                    if((i>>k) & 1)
                        dp[i | (1<<j)][j] = min(dp[i | (1<<j)][j], dp[i][k] + dis[k+1][j+1]);
            }
        }
    }

    long long ans = 1e18;
    for(int i = 0; i < N; i++)
        ans = min(ans, dp[(1<<N) - 1][i] + dis[i+1][1]);

    printf("%lld\n", ans);

    delete2dArray(dp, N);

    return 0;
}

long long ** creat2dArray(int N)
{
    // ask for memory
    long long **dp;
    int size = 1 << N;
    dp = new  long long*[size];
    for(int i = 0; i < size; i++)
    {
        dp[i] = new long long[N];
    }

    // initialize
    for(int i = 0; i < size; i++)
        for(int j = 0; j < N; j ++)
            dp[i][j] = LL_INFI;

    dp[1][0] = 0;

    return dp;
}

void delete2dArray(long long ** dp, int N)
{
    for(int i = 0; i <= (1 << N); i++)
    {
        delete(dp[i]);
    }

    delete(dp);

    return;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int main(){

    string S;

    cin >> S;

    stack<char> st;
    int si = S.size();
    rep(i,si){
        switch(S[i]){
            case '6':S[i] = '9';
            break;
            case '9':S[i] = '6';
            break;
        }
        st.push(S[i]);
    }

    rep(i,si){
       cout <<  st.top();
       st.pop();
    }
    cout << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
    string s;
    cin>>s;
    reverse(s.begin(),s.end());
    int i;
    for(i=0;i<s.size();i++)
    {
        if(s[i]=='0')
            s[i]='0';
        else if(s[i]=='1')
            s[i]='1';
        else if(s[i]=='6')
            s[i]='9';
        else if(s[i]=='8')
            s[i]='8';
        else s[i]='6';
    }
    cout<<s<<endl;
    return 0;
}


//Ô¸Éñ zhoukangyang Ö¸ÒýÎÒ¡£
#include <stdio.h>
#include <string.h>
#include <queue>
#define int long long
inline int read()
{
    int num=0,f=1;char c=getchar();
    while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
    while(c>47&&c<58)num=(num<<3)+(num<<1)+(c^48),c=getchar();
    return num*f;
}
inline int min(int a,int b){return a<b?a:b;}
inline int max(int a,int b){return a>b?a:b;}
struct Edge{
	int v,w,nxt;
}e[2005];
int h[1005],cnt=1;
inline void addedge(int u,int v,int w)
{
	e[cnt]=(Edge){v,w,h[u]};
	h[u]=cnt++;
}
struct Node{
	int u,v,fu,fv,dis;
};
std::queue<Node> q;
inline int gc()
{
	char c=getchar();
	while(c<'a'||c>'z')c=getchar();
	return c;
}
int inq[1005][1005];
signed main()
{
    int n=read(),m=read(),curans=0;
    for(int i=0;i<m;i++)
    {
    	int x=read(),y=read(),z=gc();
    	addedge(x,y,z);
    	addedge(y,x,z);
	}
	Node x=(Node){1,n,1,n,0};
	q.push(x);inq[1][n]=1;
	while(!q.empty())
	{
		x=q.front();q.pop();
		int u=x.u,v=x.v,fu=x.fu,fv=x.fv,d=x.dis;
		if(curans&&curans<d)
		{
			printf("%lld\n",curans);
			return 0;
		}
		//printf("hi %lld %lld %lld!\n",u,v,d);
		if(u==v){printf("%lld\n",d);return 0;}
		for(int i=h[u];i;i=e[i].nxt)
		{
			if(e[i].v==v)
			{
				//printf("%lld\n",d+1);
				//return 0;
				curans=d+1;
				goto nxx;
			}
		}
		for(int i=h[u];i;i=e[i].nxt)
		{
			int V=e[i].v,w=e[i].w;
			for(int j=h[v];j;j=e[j].nxt)
			{
				int vv=e[j].v,ww=e[j].w;
				if(inq[V][vv]||inq[vv][V])continue;
				if(V==fu&&vv==fv)continue;
				if(w==ww)
				{
					inq[V][vv]=1;
					q.push((Node){V,vv,u,v,d+2});
				}
			}
		}
		nxx:;
	}
	if(curans)printf("%lld\n",curans);
	else puts("-1");
}

/* made by amunduzbaev */

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

using namespace std;
//~ using namespace __gnu_pbds;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define tut(x) array<int, x>
#define int long long

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) {return a > b? a = b, true:false;}
template<class T> bool umax(T& a, const T& b) {return a < b? a = b, true:false;}
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
//~ template<class T> tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 1e3+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);

#define MULTI 0
int n, m, k, s, t, q, ans, res, a[N];
int dis[N][N];
vv<pair<int, char>> edges[N];

void solve(int t_case){
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int a, b; char c; cin>>a>>b>>c;
		edges[a].pb({b, c}), edges[b].pb({a, c});
	} for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j] = mod;
	dis[1][n] = 0;
	queue<pii> qq;
	qq.push({1, n});
	while(!qq.empty()){
		pii cur = qq.front(); qq.pop();
		for(auto a : edges[cur.ff]){
			for(auto b : edges[cur.ss]){
				if(a.ss != b.ss) continue;
				if(umin(dis[a.ff][b.ff], dis[cur.ff][cur.ss] + 1)) qq.push({a.ff, b.ff});
			}
		}
	}
	int res = mod;
	for(int i=1;i<=n;i++){
		umin(res, dis[i][i] * 2);
		for(auto x : edges[i]) umin(res, dis[i][x.ff]*2 + 1);
	} if(res != mod) cout<<res<<"\n";
	else cout<<-1<<"\n";
}

signed main(){
	NeedForSpeed
	if(MULTI){
		int t; cin>>t;
		for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
	} else solve(1);
	return 0;
}


#include <cassert>
#include <limits>

/*
find the maximum integer value that satisfies the given condition,
where if x satisfies it, every y (< x) satisfies it

template parameters:
Integer: an integer type
Function: a function object type

parameters:
a: the lower bound of the possible value
b: the upper bound of the possible value
cond: the function representing the condition
        which take an integer value as an argument
        and return true if the condition is satisfied; false otherwise

return value:
the maximum integer that satisfies the condition

requirements:
there exists a value in [a, b] that satisfies the condition
a <= b
std::numeric_limits<Integer>::min() <= 2*a && 2*b <= std::numeric_limits<Integer>::max()
*/
template <class Integer, class Function>
Integer MaxCond(Integer a, Integer b, Function cond)
{
    // debug
    assert(a <= b);
    assert(a >= std::numeric_limits<Integer>::min() / 2);  // this is sufficient but not necessary
    assert(b <= std::numeric_limits<Integer>::max() / 2);  // this is sufficient but not necessary
    assert(cond(a));  // check the existence of a value in [a, b] that satisfies the condition

    // body
    while (a < b) {
        Integer c = (a + b + 1) / 2;
        if (cond(c)) {
            a = c;
        } else {
            b = c - 1;
        }
    }
    return a;
}

#include <iostream>
#include <vector>
#include <array>

int main()
{
    int n;
    std::cin >> n;
    std::vector<std::array<int, 5>> member(n);
    for (int i = 0; i < n; i++) {
        std::cin >> member[i][0] >> member[i][1] >> member[i][2] >> member[i][3] >> member[i][4];
    }
    int ans = MaxCond(1, 1000000000, [&member, n](int strength) {
        bool possible[32];
        for (int i = 0; i < 32; i++) {
            possible[i] = false;
        }
        for (int i = 0; i < n; i++) {
            int member_possible = 0;
            for (int j = 0; j < 5; j++) {
                if (member[i][j] >= strength) {
                    member_possible |= 1 << j;
                }
            }
            possible[member_possible] = true;
        }
        for (int i = 0; i < 32; i++) {
            if (i == 31 && possible[i]) {
                return true;
            }
            for (int j = i + 1; j < 32; j++) {
                if ((i | j) == 31 && possible[i] && possible[j]) {
                    return true;
                }
                for (int k = j + 1; k < 32; k++) {
                    if ((i | j | k) == 31 && possible[i] && possible[j] && possible[k]) {
                        return true;
                    }
                }
            }
        }
        return false;
    });
    std::cout << ans << std::endl;
}


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

int main()
{
	long long t, N;
	cin >> t >> N;

	long long a = 0;
	long long b = 0;
	vector<int> v;
	while( a < 1000 ) {
		a++;
		long long c = (100 + t) * a / 100;
		if( c - b > 1 ) {
			v.push_back( a );
			if( a == 100 ) break;
		}
		b = c;
	}
	int n = v.size();
	a = N / n * 100;
	int m = N % n;
	if( m == 0 ) {
		a -= 100;
		m = n;
	}
	long long ans;
	b = (100 + t) * a / 100;
	while( 1 ) {
		a++;
		long long c = (100 + t) * a / 100;
		if( c - b > 1 ) {
			ans = b + 1;
			m--;
			if( m == 0 ) break;
		}
		b = c;
	}

	cout << ans << endl;
}

#include <bits/stdc++.h>
#include <unordered_set>

#define f(i, a, b) for (int i = a; i < b; i++)
#define rf(i, a, b) for (int i = a; i > b; i--)
#define lf(i, a, b) for (long long i = a; i < b; i++)
#define sq(a) (a)*(a)
#define cube(a) (a)*(a)*(a)
#define pb push_back
#define ppb pop_back
#define LB lower_bound
#define UB upper_bound
#define sarr(arr, n) sort(arr, arr + n);
#define svec(v) sort(v.begin(), v.end());
#define svecr(v) sort(v.rbegin(), v.rend());
#define intinp(var) int var; \
        cin >> var;
#define adjc(adj, n) int adj[n];
#define adjcw(adj, n) vector<pair<int, int>> adj[n];
#define PYES cout << "YES\n"
#define PNO cout << "NO\n"
#define PYes cout << "Yes\n"
#define PNo cout << "No\n"
#define endl "\n"

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vii;
typedef vector<string> vs;
typedef unordered_map<int, int> um;

int main(){
    ll t, n; cin >> t >> n;
    ll mn = (100*n)/t;
    if ((100*n)%t) mn++;
    cout << mn + n-1 << endl;
    return 0;
}


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

int main() {
  int N, K;
  int sum = 0;
  cin >> N >> K;

  for(int i = 1; i <= N; i++){
   for(int j = 1; j <= K; j++){
    sum += (i * 100 + j);
    }
  }
  cout << sum << endl;

}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p)
{
    int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res % p;
}
int lcm(int a, int b)
{
    return a / __gcd(a, b) * b;
}
void solve()
{
   int n,m;
   cin>>n>>m;
   int a[n][m];
   int mn = 1e18;
   for (int i = 0; i < n;i++){
       for (int j = 0; j < m;j++){
           cin >> a[i][j];
           mn = min(mn, a[i][j]);
       }
   }
   int ans = 0;
   for (int i = 0; i < n; i++)
   {
       for (int j = 0; j < m; j++)
       {
           ans += a[i][j] - mn;
       }
   }
   cout << ans << endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int tc;
    tc = 1;
    // cin >> tc;
    while (tc--)
    {

        solve();
    }

    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define rep(i, n) FOR(i,0,n)
#define irep(i, n) IFOR(i,0,n)
#define all(v) begin(v), end(v)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using VI = vector<int>;
using VLL = vector<ll>;
using VB = vector<bool>;
using VP = vector<P>;
using Graph2 = vector<vector<int>>;
using PQ = priority_queue<int>;
template<typename T>istream& operator>>(istream&s,vector<T>&v){for(auto &i: v)s>>i;return s;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
bool second_compare(P a,P b){if(a.second!=b.second){return a.second<b.second;} else return true;}

ll ch(ll a, ll b) {
    ll ret = 1;
    FOR(i, 1, b+1) {
        ret *= a;
        ret /= i;
        a--;
    }
    return ret;
}

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

    string ans;

    ll now = 0;
    int n = a+b;
    rep(i, n) {
        if (a == 0) {
            cout << ans;
            rep(i, b) cout << 'b'; cout << endl;
            return 0;
        }
        if (b == 0) {
            cout << ans;
            rep(i, a) cout << 'a'; cout << endl;
            return 0;
        }

        --a;
        ll c = ch(a+b, a);
        if (now + c >= k) {
            ans += 'a';
        }
        else {
            ans += 'b';
            ++a;
            --b;
            now += c;
        }
    }

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

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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

lli C[80][80];

void func(long long A, long long B, long long K){

    string s;

    while(B!=0 && A!=0){
        lli lefSum = A+B;
        LOG4(A,B,K,s);
        lli border = C[lefSum-1][B];
        LOG2(K,border);
        if(border < K){
            s.push_back('b');
            B--;
            K-=border;
        }
        else{
            s.push_back('a');
            A--;
        }
    }
    while(B--)s.push_back('b');
    while(A--)s.push_back('a');
    cout<<s<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    long long K;
    scanf("%lld",&K);
    C[0][0]=1;
    C[1][0]=1;
    C[1][1]=1;
    REP(i,2,62){
        REP(j,0,i+1){
            if(j==0)C[i][j]=1;
            else{
                C[i][j] = C[i-1][j-1] + C[i-1][j];
            }
        }
    }
    func(A, B, K);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
  int N,M;
  cin>>N>>M;
  int a[N];
  rep(i,N){
    cin>>a[i];
  }
  int mex = MOD;
  vector <int> b(M);
  rep(i,M){
    b[i] = a[i];
  }
  sort(all(b));
  if(b[0]!=0){
    mex=0;
  }
  else{
    bool p = 0;
    for(int i = 1;i<M;i++){
      if(b[i]-b[i-1]>1){
        mex = b[i-1]+1;
        p = 1;
        break;
      }
    }
    if(p == 0){
      mex = b[M-1]+1;
    }
  }
  int kazu[1500100];
  rep(i,M){
    kazu[a[i]]++;
  }
 // cout << mex << endl;
  for(int i = M;i<N;i++){
    kazu[a[i]]++;
    kazu[a[i-M]]--;
    if(kazu[a[i-M]] == 0){
    //  cout << a[i-M] << endl;
      chmin(mex,a[i-M]);
    }
  }
  cout << mex << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  string S, T;
  cin >> S >> T;

  int counts = 0, countt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') counts++;
    if (T[i] == '0') countt++;
  }
  if (counts != countt) {
    cout << -1 << endl;
    return 0;
  }

  int ms = 0, mt = 0, count = 0;
  for (int i = 0; i < counts; i++) {
    while (S[ms] == '1') ms++;
    while (T[mt] == '1') mt++;
    if (ms != mt) count++;
    ms++;
    mt++;
  }

  cout << count << endl;

  return 0;
}


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

#define endl        "\n"
#define LL          long long
#define READ        freopen("in.txt","r",stdin)
#define WRITE       freopen("out.txt","w",stdout)
#define BOOST       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb          push_back
#define mem(x,y)    memset(x,y,sizeof x )
#define ch          printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define deb(x)      cerr<<#x<<" :: "<<x<<" "
#define dnl         cerr<<endl;
#define FOR(i,n)    for(int i=0;i<n;i++)
#define cnd         tree[idx]
#define lc          tree[idx*2]
#define rc          tree[idx*2+1]
#define lnd         (2*idx),(b),( (b+e) /2 )
#define rnd         ((2*idx)+1),(((b+e)/2)+1),(e)
#define popcountL   __builtin_popcountll
#define popcount    __builtin_popcount

const LL        INF = 1LL<<59;
const double    PI = 2.0 * acos(0.0);

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

////// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//typedef tree<
//LL,
//null_type,
//less< pll >,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

// graph direction array [4]
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int BX[8]={0,0,1,-1,-1,1,1,-1};
//int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));}
inline int setBit(int N,int pos){ return N=N | (1<<pos);}
inline int unsetBit(int N,int pos){ return N=( N & ( ~( 1<<pos ) ) );}
inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }

inline LL modMul(LL a, LL b,LL mod){
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}
inline LL powerMod(LL a,LL b,LL mod){
    if(b==0)return 1LL%mod;
    LL x=powerMod( a,b/2,mod );
    x = modMul(x,x,mod) % mod;
    if( b%2==1 )x = modMul(x,a,mod) % mod;
    return x%mod;
}
inline LL lcm( LL a,LL b ){
    LL g = __gcd(a,b);
    return ((a/g) * b);
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 234567;

vll arr;
void solve(int cs){
    int n;
    LL k;
    cin>>n>>k;
    FOR(i,n){
        LL a,b;cin>>a>>b;
        arr.pb(pll(a,b));
    }

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

    LL curcoin = k;
    LL mypos = 0;
    for(int i=0;i<n;i++){
        pll x = arr[i];
        LL dist = x.first;
        if( mypos+curcoin < dist ){
            mypos += curcoin;
            break;
        }
        else{
            curcoin -= dist-mypos;
            mypos = dist;
            curcoin += x.second;
        }

        if( i==n-1 ){
            mypos += curcoin;
        }

//        deb(i);deb(mypos);deb(curcoin);dnl;
    }

    cout<<mypos<<endl;

}

int main(){

    BOOST;
    #ifdef MujahidPC
        double start = clock();
//        READ;
//        WRITE;
    #endif

    int t;
    t = 1;
//    cin>>t;
    FOR(cs,t)solve(cs+1);

    #ifdef MujahidPC
        double tim = (clock() - start)/CLOCKS_PER_SEC;
        cerr<<"Running Time : "<<tim<<" \n";
    #endif
    return 0;
}

//#include <atcoder/all>
//using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, m, n) for(int i=m; i<n; i++)
#define ALL(v) v.begin(), v.end()
#define bit(n) (1LL<<(n))
#define FIX(d) fixed << setprecision(d)
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const ll LINF = (1LL<<60);

template <typename T>
struct PrimeFact {
  vector<T> spf;
  PrimeFact(T N) { init(N); }
  void init(T N) { // 前処理。spf を求める
    spf.assign(N + 1, 0);
    for (T i = 0; i <= N; i++) spf[i] = i;
    for (T i = 2; i * i <= N; i++) {
      if (spf[i] == i) {
        for (T j = i * i; j <= N; j += i) {
          if (spf[j] == j) spf[j] = i;
        }
      }
    }
  }
  map<T, T> get(T n) { // nの素因数分解を求める
    map<T, T> mp;
    while (n != 1) {
      mp[spf[n]]++;
      n /= spf[n];
    }
    return mp;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int a,b; cin >> a >> b;

  //PrimeFact pf(b);
  int sub = b-a;
  REPR(i,sub){
    int low = (a/i)*i;
    if(low < a) low += i;
    int high = (b/i)*i;
    if(low<high){
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <limits>
#include <string>
#include <algorithm>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include "math.h"
#include "assert.h"

using namespace std;

#define ll long long

#define config ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(20);
#define epsilon 1e-07
#define pi 2 * acos(0.0);
#define mod 1000000007   // 1e9+7
#define cerr if(false)cerr
#define watch(x) cerr << #x << " = " << x << endl;

#define iv vector<int>
#define xy pair<int, int>
#define x first
#define y second

#define inf std::numeric_limits<int>().max()

int main() {
	config;
	ll n;
	cin >> n;
	string t;
	cin >> t;
	ll sz = 1e10;
	ll k = n / 3;
	ll ans = 0;
	if (n < 3) {
		if (t == "0") ans = sz;
		if (t == "1") ans = 2*sz;
		if (t == "00") ans = 0;
		if (t == "01") ans = sz-1;
		if (t == "10") ans = sz;
		if (t == "11") ans = sz;
	} else {
		set<string> s;
		s.insert("110");
		s.insert("011");
		s.insert("101");
		string p = t.substr(0,3);
		if (s.count(p)) {
			bool ok = true;
			for(int i=0; i<k*3; i+=3) {
				if (p != t.substr(i, 3)) ok = false;
			}
			if (ok) {
				ll zeros = 0;
				for(int i=0; i<n; i++) {
					if (t[i] == '0') zeros++;
				}
				ans = sz - zeros;
				if (t[t.size()-1] == '0') ans++;
			}
		}
	}
	cout << ans << '\n';
}

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

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef vector<LL> 		VL;
#define si(n) cin>>n
#define dout(n) cout<<n<<"\n"
#define DRT() int t; si(t); while(t--)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//# define M_PI           3.14159265358979323846264338327950288
// DSU

void IO(){
	#ifndef ONLINE_JUDGE
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
	#endif
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	off;
}

int main(){
	IO();
	LL s,p;
	cin>>s>>p;
	LL ok = 0;
	for(LL i=1;i*i <= p;i++){
		LL rem = p % i;
		if(rem == 0){
			LL d = p/i;
			if(i + d == s)
				ok = 1;
		}
	}
	if(ok)
		dout("Yes");
	else
		dout("No");
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

int popcount(int n) {
    int res = 0;
    while (n) {
        res += (n & 1);
        n >>= 1;
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    cin, n, m;
    vector<int> edges(n, 0);
    rep(i, 0, m) {
        int a, b;
        cin, a, b;
        a--;
        b--;
        edges[a] |= (1 << b);
        edges[b] |= (1 << a);
    }
    int sz = 1 << n;
    vector<bool> clique(sz, false);
    rep(mask, 0, sz) {
        int msk = mask;
        rep(i, 0, n) {
            if ((mask >> i) & 1) {
                msk &= (edges[i] | (1 << i));
            }
        }
        clique[mask] = msk == mask;
    }

    const int inf = n + 1;
    vector<int> dp(sz, inf);
    rep(i, 0, sz) {
        if (clique[i]) {
            dp[i] = 1;
        }
    }
    rep(i, 0, sz) {
        for (int j = i;; j = (j - 1) & i) {
            int k = i ^ j;
            chmin(dp[i], dp[j] + dp[k]);
            if (j == 0) break;
        }
    }
    print(dp[sz - 1]);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=20;
int g[N][N];
int dp[1<<N];
bool ok[1<<N];
int n,m;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		x--,y--;
		g[x][y]=g[y][x]=1;
	}
	for(int i=0;i<(1<<n);i++)
	{
		vector<int> e;
		for(int j=0;j<n;j++)
		{
			if(i>>j&1) e.push_back(j);
		}
		 ok[i]=true;
		for(int j=0;j<e.size();j++)
		for(int k=j+1;k<e.size();k++)
		if(!g[e[j]][e[k]])
		{
			ok[i]=false;
		}
	}
	for(int i=0;i<(1<<n);i++) dp[i]=n+1;
	dp[0]=0;
	for(int i=1;i<(1<<n);i++)
	{
		if(ok[i]) dp[i]=1;//只有一块
		for(int j=i;j;j=(j-1)&i)
		dp[i]=min(dp[i],dp[j]+dp[i^j]);
	}
	cout<<dp[(1<<n)-1];
	return 0;
}


#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

void solve(){
    ll n;
    cin >> n;
    if(n % 2 == 1)cout << "Odd" << endl;
    else {
        if(n % 4 == 0)cout << "Even" << endl;
        else cout << "Same" << endl;
    }
}

int main() {
    int t;
    cin >> t;
    while(t--)solve();
}

#include<bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long int

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

    ll t=1;
    //cin>>t;

    while(t--)
    {
        double n;
        cin>>n;

        n*=1.08;
        n=(ll)n;
        if(n>206)
        {
                        cout<<":("<<endl;

        }
        else if(n==206)
        {
                        cout<<"so-so"<<endl;

        }
        else
        {
            cout<<"Yay!"<<endl;
        }
    }

    return 0;
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define fo(i, n1, n, x) for(int i = n1; i <= n; i += x)
#define foo(i, n, n1, x) for(int i = n; i >= n1; i -= x)
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)

using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;

using ll = long long;

//#define int ll

using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;

const int N = 2e5 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 6e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);

//template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return uniform_int_distribution<int> (l, r)(rng); }

string s;
main() {
  file("paint");
  ios;
  cin >> s;
  deque <char> d;
  bool t = 0;
  for (char it : s) {
    if (it == 'R') t ^= 1;
    else {
      if (!t) d.push_back(it);
      else d.push_front(it);
    }
  }
  stack <char> st;
  string ans = "";
  while (!d.empty()) {
    char x;
    if (!t) x = d.front(), d.pop_front();
    else x = d.back(), d.pop_back();
    if (ans.size() and ans.back() == x) ans.pop_back();
    else ans += x;
  }
  cout << ans;
  return 0;
}
/**

*/

#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s;
  cin>>s;
  deque<char> q;
  bool rev = false;
  for (auto&& ch : s) {
    if (ch == 'R') rev = !rev;
    else {
      if (rev) q.push_front(ch);
      else q.push_back(ch);
    }
  }
  if (rev) {
    reverse(all(q));
  }
  vector<char> ans;
  for (auto&& ch : q) {
    if (ans.empty() || ans.back() != ch) {
      ans.push_back(ch);
    } else {
      ans.pop_back();
    }
  }
  for(auto&& ch : ans) {
    cout<<ch;
  }
  cout<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=500005;
typedef long long ll;
int n,p[N],q[N],a[N],b[N],i,id[N];
bool cmp(int x,int y)
{
	return b[x]>b[y];
}
struct str{
	int u,v;
};
vector<str> g;
int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)
		scanf("%d",&a[i]);
	for(i=1;i<=n;++i)
	{
		scanf("%d",&b[i]);
		id[i]=i;
	}
	sort(id+1,id+1+n,cmp);
	for(i=1;i<=n;++i)
	{
		scanf("%d",&p[i]);
		q[p[i]]=i;
	}
	for(i=1;i<=n;++i)
	{
		int x=id[i];
		if(q[x]!=x)
		if(a[q[x]]>b[x]&&a[x]>b[p[x]])
		{
			g.push_back((str){x,q[x]});
			int u=p[x],v=q[x];
			q[u]=v;
			p[v]=u;
		}
		else
		{
			puts("-1");
			return 0;
		}
	}
	printf("%d\n",g.size());
	for(auto it:g)
		printf("%d %d\n",it.u,it.v);
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
using lint=long long;
#define debug(output) cout<<#output<<"= "<<output<<endl
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
signed main(){
  int n;cin>>n;
  vector<int> a(n),b(n),p(n),memo(n,0);
  rep(i,n)cin>>a[i];
  rep(i,n)cin>>b[i];
  rep(i,n)cin>>p[i];
  rep(i,n)--p[i];
  rep(i,n)memo[p[i]]=i;
  priority_queue<P,vector<P>,greater<P>> que;
  rep(i,n){
    que.push({a[i],i});
  }
  vector<P> res;
  while(!que.empty()){
    auto [w,i]=que.top();que.pop();
    if(i==p[i])continue;
    else if(a[i]<=b[p[i]]||a[memo[i]]<=b[i]){
      cout<<-1<<"\n";
      return 0;
    }
    int hoge=p[i];
    res.push_back({i,memo[i]});
    swap(p[i],p[memo[i]]);
    swap(memo[i],memo[hoge]);
  }
  cout<<res.size()<<"\n";
  rep(i,res.size()){
    cout<<res[i].first+1<<" "<<res[i].second+1<<"\n";
  }
  return 0;
}


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

const int N = 3e5 + 5;
int a[N], n;

int main(void){
	scanf("%d", &n);
	ll sum1 = 0, sum2 = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		sum1 += a[i];
		sum2 += a[i] * a[i];
	}
	ll ans = (n - 1) * sum2 - (sum1 * sum1 - sum2);
	printf("%lld\n", ans);
	return 0;
}

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

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

  map<long long,long long> height;
  long long num =0;
  height[0]++;
  unsigned long long count =0;

  for(int i=0;i<n;i++){
    if(i%2==0) {
      num += a[i];
      height[num] ++;
    }else{
      num -= a[i];
      height[num] ++;
    }
  }

  for (auto i:height){
    //cout<< i.first <<' '<< i.second <<endl;
    count += i.second*(i.second-1)/2;
  }

  cout << count << endl;

}


#include<bits/stdc++.h>
#define MAXN 300005
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int n;
ll a[MAXN],maxl[MAXN],sum[MAXN],val[MAXN];

ll f(ll len){return maxl[len] * len + sum[len] + val[len];}

int main(){
	cin>>n;for(int i = 1 ; i <= n ; i++)cin>>a[i];
	maxl[0] = -INF;
	for(int i = 1 ; i <= n ; i++){
		maxl[i] = max(maxl[i - 1] , a[i]);
		sum[i] = sum[i - 1] + a[i];
		val[i] = val[i - 1] + a[i - 1];
	}
	for(int i = 1 ; i <= n ; i++)val[i] = val[i] + val[i - 1];
	for(int i = 1 ; i <= n ; i++)cout<<f(i)<<endl;
}

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

#define ffor(n) for(int i = 0; i < n; i++)
#define fffor(n) for(int j = 0; j < n; j++)
#define uwu ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize("Ofast")

const int INF = 1e9 + 7;
const long long INF2 = 1e17;

int main(void) {
	uwu

	// when adding some integer to v, it only affects the answer if it is larger than any other previous element
	// v[0] = a[0] + x, v[1] = a[1] + (a[0] + x), v[2] = a[2] + (a[0] + a[1] + x), ...
	// for n terms, sum is n * x + (a[0] + a[0] + a[1] + a[0] + a[1] + a[2] + ...)
	int n; cin >> n;
	vector <long long> v(n);
	ffor(n) cin >> v[i];

	vector <long long> prefix(n);
	prefix[0] = v[0];
	for(int i = 1; i < n; i++) prefix[i] = prefix[i - 1] + v[i];

	long long current_sum = 0, maximum = 0;
	for(int i = 0; i < n; i++) {
		maximum = max(maximum, v[i]);
		current_sum += prefix[i];
		cout << (i + 1) * maximum + current_sum << '\n';
	}
}

/*
C:\Users\kenne\OneDrive\Desktop\competitive_programming\main.cpp
*/


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <list>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
struct Edge
{
    int next, to;
} e[maxn << 2];
int head[maxn];
LL a[maxn];
LL dp[maxn];
LL ans = -2 * mod;
int in[maxn];
int n, m;
int k = 0;

void add(int u, int v)
{
    e[k].next = head[u];
    e[k].to = v;
    head[u] = k++;
}
void topo()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
        if (in[i] == 0)
        {
            q.push(i);
        }
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (a[u] - dp[u] > ans)
            ans = a[u] - dp[u];
        // dp[u] = min(dp[u], a[u]);
        for (int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].to;
            dp[v] = min(dp[v], min(dp[u], a[u]));
            in[v]--;
            if (in[v] == 0)
                q.push(v);
        }
    }
    return;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    int x, y;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        head[i] = -1;
        dp[i] = 1e10;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        add(x, y);
        in[y]++;
    }
    topo();
    cout << ans;
    return 0;
}

//Bismillahirrahmanirrahim

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

#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define int long long
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,dp[li],vis[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node){
	if(vis[node])return ;
	vis[node]=1;
	for(auto go:v[node]){
		dfs(go);
		dp[node]=max(dp[node],max(a[go],dp[go]));
	}
	cev=max(cev,dp[node]-a[node]);
}

int32_t main(void){
	scanf("%lld %lld",&n,&m);
	FOR scanf("%lld",&a[i]);
	FOR dp[i]=-inf;
	for(int i=1;i<=m;i++){
		int x,y;
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		//~ v[y].pb(x);
	}
	cev=-inf;
	FOR{
		if(vis[i])continue;
		dfs(i);
	}
	printf("%lld\n",cev);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
//first second
//memset(dp, -1, sizeof dp);
//string binary =bitset<8>(n).to_string();
//(mod)->(10*mod+currentmodulo)%m
struct keycompare {
    bool operator()(const pair<ll, ll>& v, const ll& k) {
        return (v.first < k);
    }
    bool operator()(const ll& k, const pair<ll, ll>& v) {
        return (k < v.first);
    }
};

ll mod1=998244353,mod2=1000000007,limit=9223372036854775807;
long double pi=3.1415926535897932;
ll modpow(ll x, ll n, ll m) {

    if(x>m){
        x%=m;
    }
    if (n == 0) return 1%m;
    ll u = modpow(x,n/2,m);
    u = (u*u)%m;
    if (n%2 == 1) u = (u*x)%m;
    return u;
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
bool isprime(ll n){
    if(n==2)
        return true;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0)
            return false;
    }
    return true;
}

ll power(ll x,ll n){
    if(n<0){
        return 0;
    }
    ll x_n=1;
    for(ll i=0;i<n;i++){
        x_n*=x;
    }
    return x_n;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m; cin>>n>>m;
    ll k=modpow(10,n,m);
    ll mm=m*m;
    for(int i=0;i<m;i++){
        ll temp=modpow(10,n,mm)-k%mm-(m*i)%mm;
        temp=temp%mm;
        if(temp==0){
            cout<<i<<endl; return 0;
        }
    }
}

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL long long

using namespace std;
const int N = 55;

int t;
LL n, m;

LL qp(LL a, LL b)
{
    LL res = 1;
    while (b)
    {
        if (b & 1) res = res * a % (m * m);
        b >>= 1;
        a = a * a % (m * m);
    }
    return res;
}

int main()
{
    scanf("%lld%lld", &n, &m);
    LL res = qp(10, n);
    printf("%lld\n", res / m);
    return 0;
}


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

int main(){
  int H, W; cin >> H >> W;
  int A[100][100];
  int m=1000;
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      cin >> A[i][j];
      m=min(m, A[i][j]);
    }
  }
  int r=0;
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      r+=(A[i][j]-m);
    }
  }
  cout << r << endl;

}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
using vi = vector<int>;

int main() {
   cin.tie(0);
   ios_base::sync_with_stdio(false);
   int h,w; cin >> h >>w;
   vector<vector<int> > a(h,vector<int>(w));
   int mn = 1000;
   int sum = 0;
   rep(i,h)rep(j,w) {
       cin >> a[i][j];
       mn = min(a[i][j],mn);
       sum += a[i][j];
   }

   cout << sum - mn * h * w <<"\n";
   return 0;
}


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

using namespace std;

int main() {

    long int N;

    cin >> N;

    set<long int> re;

    for (int b = 2; b < int(sqrt(N)) + 2; ++b) {
        for (int e = 2; e < 40; ++e) {
            long int v = static_cast<long int>(pow(b,e));
            if (v <= N && v > 0) re.insert(v);
            else break;
        }
    }

    cout << N - re.size() << "\n";

}

#include <bits/stdc++.h>

using namespace std;

#define N 1000005
#define MAX 1e18
vector<long int> powers;
set<long int> squares;
set<long int> s;

void powersPrecomputation()
{
	for (long int i = 2; i < N; i++)
	{
		squares.insert(i * i);
		if (squares.find(i) != squares.end())
				continue;

		long int temp = i;
		while (i * i <= MAX / temp)
		{
			temp *= (i * i);
			s.insert(temp);
		}
	}
	for (auto x : s)
		powers.push_back(x);
}

long int calculateAnswer(long int R)
{
	long int perfectSquares = floor(sqrtl(R));
	long int high = (upper_bound(powers.begin(),
			powers.end(), R) - powers.begin());
	long int low = (lower_bound(powers.begin(),
			powers.end(), 1) - powers.begin());
	perfectSquares += (high - low);

	return perfectSquares;
}
int main()
{
	powersPrecomputation();
	long int L{1};
	long int R;
	cin>>R;

	cout << R+1-calculateAnswer(R);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f;
void prepare() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

vector<LL> arr;
int N, Q;
LL K;
map<LL, LL> m;

LL f() {
    if(arr[0]>K) {
        return K;
    }
    LL l = 0, r = arr.size();
    while(l<r) {
        LL mid = (l+r) >> 1;
        LL x = mid<arr.size()?arr[mid]:INT_MAX;
        if(x-mid-1>=K) {
            r = mid;
        } else {
            l = mid+1;
        }
    }
    return K-(arr[l-1]-(l-1)-1)+arr[l-1];
}

int main() {
    prepare();
    cin >> N >> Q;
    for(int i=0; i<N; i++) {
        LL t;
        cin >> t;
        arr.push_back(t);
    }
    sort(arr.begin(), arr.end());
    while(Q--) {
        cin >> K;
        if(m.find(K)!=m.end()) {
            cout << m[K] << endl;
        }
        LL ans;
        ans = f();
        m[K] = ans;
        cout << ans << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(n);i++)
int main() {
    ll N,Q;
    cin >> N>>Q;
    vector<ll> A,B;
    ll last=0,last_b=0;
    rep(i,N){
        ll temp;
        cin>>temp;
        A.push_back(temp);
        B.push_back(last_b+temp-last-1);
        last_b=last_b+temp-last-1;
        last=temp;
    }
    /*
    3,5,6,7
    2,3,3,3みたいにしていくっぽい？？
    →1,2,4,8,9

    今(2,3)(3,7)みたいになってる
    4を取得したければ
    →
    */
    map<ll,ll> C;
    rep(i,N){
        C.emplace(B[N-1-i],A[N-i-1]);
    }
    //Kqより小さいキーの値を取得して、それに(Kq-key)の値を足せばそれが答え
    //それをどうやって探すのか

    rep(i,Q){
        ll K,answer=0;
        cin>>K;
        auto be=C.lower_bound(K);
        if(be==C.begin()){
            answer=K;
        }else{
            be--;
            answer=(be->second)+K-(be->first);
        }
        cout << answer << endl;
    }
}


#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

// helping defines
#define ll long long int
#define ull  unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a,n)  sort(a,a+n,greater<int>())
#define sortA(a,n)  sort(a,a+n)

//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define vi  vector<int>
#define vll  vector<ll>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll>>
#define vpll vector<pair<ll,ll>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
#define sortVA(a) sort(a.begin(),a.end())
#define sortVD(a) sort(a.rbegin(),a.rend())

//map
#define um unordered_map
#define f first
#define s second
#define mapit unordered_map<int,int>:: iterator

//constants
#define pi 3.1415926536
#define MOD 1000000007
#define MAX (1e9 + 5)
#define MIN -(1e9 + 5)
#define endl "\n"

//loop
#define ff(i,s,e) for(i=s; i<e; i++)
#define rff(i,s,e) for(i=e; i>=s; i--)
#define vff(a) for(auto x: a)
#define read(a) for(auto &x: a) cin>>x;
#define put(a) for(auto &x: a) cout<<x<<" "; cout<<endl;
// global variables
int i,j;

//helper functions
ll ceil_div(ll x, ll y){
    return (x + y - 1) / y;
}

//coding starts here
void test_cases(){

    int n,k;
    cin>>n>>k;

    while(k--){
        vi a;
        while(n>0){
            a.pb(n%10);
            n/=10;
        }
        vi temp1=a;
        vi temp2=a;
        sortVA(temp1);
        sortVD(temp2);
        // put(temp1);
        int x=0,y=0;
        int l=a.size();
        int pow10=pow(10,(l-1));

        ff(i,0,l){
            x+=temp1[i]*pow10;
            y+=temp2[i]*pow10;
            pow10=pow10/10;
        }
        // cout<<x<<" "<<y<<endl;
        n=(y-x);
        // cout<<n<<endl;
    }
    cout<<n<<endl;
    return;
}

int main()
{

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

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    // cin>>T;
    while(T--){
        test_cases();
    }
    return 0;
}

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    long long n,k;
    cin >> n >> k;
    long long a = n;

    for (long long i=0; i<k; i++) {
        int big,sma;
        string aa;
        aa = to_string(a);
        sort(aa.begin(), aa.end(), greater<int>());
        big = stoi(aa);
        sort(aa.begin(), aa.end());
        sma = stoi(aa);

        a = big -sma;
    }

    cout << a ;

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


#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define repr(i, n, s) for (int i = (int)n; (int)s < i; i--)
#define len(a) (int)a.size()
#define all(a) a.begin(), a.end()
/* 1次元ベクトルをスペースでつなぎ、改行をつけて出力 vi, vl, list<int>, list<ll>に対応  */
template<class T> void print_elements(T a) {string str = ""; for (auto itr = a.begin(); itr != a.end(); ++itr) \
        {str += to_string(*itr) + ' ';} str.pop_back(); cout << str << endl;}

const int INF32 = 1 << 29;
const long long INF64 = 1LL << 60;
const long long MOD = 1000000009;
const double PI = acos(-1);

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == c) {
        cout << b << endl;
    }
    else if (a == b) {
        cout << c << endl;
    }
    else if (b == c) {
        cout << a << endl;
    }
    else {
        cout << 0 << endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
/*--------------------------Containers------------------------*/
typedef     long long int           ll;
typedef     unsigned long long int  ull;
typedef     vector < int >          vi;
typedef     vector < ll >           vll;
typedef     vector < vll >          vvll;
typedef     pair< int,int >         pi;
typedef     pair< ll, ll >          pll;
typedef     vector< pi >            vpi;
typedef     vector< pll >           vpll;
typedef     map<int,int>            mi;
typedef     map<ll,ll>              mll;
typedef     map<pi,int>             mpi;
typedef     map<pll,ll>             mpll;
/*------------------------------------------------------------*/

/*--------------------------Limits & Constants----------------*/
#define     MAX                 200007
#define     MOD                 int(1e9+7)
#define     INF                 ll(8230219982008199610)
#define     MINF                ll(-8230219982008199610)
#define     IL                  INT_MAX
#define     MIL                 INT_MIN
#define     PI                  2*acos(0.0)
/*------------------------------------------------------------*/

/*--------------------------Macros----------------------------*/
#define     pf                  printf
#define     sc                  scanf
#define     sz(x)               int(x.size())
#define 	sortv(v)  			sort(begin(v), end(v))
#define 	sortrv(v) 			sort(rbegin(v), rend(v))
#define 	all(nums) 			nums.begin(), nums.end()
#define 	allr(nums) 			nums.rbegin(), nums.rend()
#define     pb                  push_back
#define     eb                  emplace_back
#define     mp                  make_pair
#define     ff                  first
#define     ss                  second
#define     mem(a,n)            memset(a, n, sizeof(a) )
#define     gcd(a,b)            __gcd(a,b)
#define     lcm(a,b)            (a*(b/gcd(a,b)))
#define     fori0(n)          for(int i=0; i<n; i++)
#define     fori1(n)          for(int i=1; i<=n; i++)
#define     forj0(n)          for(int j=0; j<n; j++)
#define     forj1(n)          for(int j=1; j<=n; j++)
#define     rev0(n)           for(int i=n-1; i>=0; i--)
#define     rev1(n)           for(int i=n; i>0; i--)
#define     forit(vec)        for(auto it=vec.begin();it!=vec.end();it++)
#define     forx(vec)         for(auto x:vec)
/*------------------------------------------------------------*/

/*--------------------------Debugging------------------------*/
#define ___debug(a, b, c) cout << a << " " << b << " " << c << "\n"
#define __debug(a, b) cout << a << " " << b << "\n"
#define _debug(a) cout << a << "\n"
template <typename T>
void print(vector<T> v) { for (T &i : v) cout << i << " "; cout << "\n";}
template <typename T>
void read(vector<T> &v) { for (T &i : v) cin >> i; }
/*------------------------------------------------------------*/

void solve() {
    int a, b, c;
    cin >> a >> b >> c;

    if (a == b) {
        _debug(c);
    }
    else if (a == c) {
        _debug(b);
    }
    else if (b == c) {
        _debug(a);
    }
    else {
        _debug(0);
    }
}

int main() {
    IOS;

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int tcs = 1;
    // cin >> tcs;
    while (tcs--)
        solve();

    return 0;
}

//g++ -std=c++17 cf.cpp -o a.exe


#include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
#define pb push_back
#define int long long

int32_t main()
{

    tani_nachi_ke

    int x;
    cin >> x;
    if(x >= 0)
        cout << x << endl;
    else
        cout << 0 << endl;

return 0;
}

#include <bits/stdc++.h>
int main(){
    int x;
    std::cin >>x;
    std::cout << std::max({x,0}) << std::endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define debug(x)      cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define debugvec(v)   rep(i, v.size()) cerr << "L" << __LINE__ << ": " << #v << "[" << i << "] = " << v[i] << endl;
#define debugvec2(v)  cerr << "L" << __LINE__ << ": " << #v << " = { "; rep(i, v.size()) cerr << v[i] << (i + 1 == v.size() ? "" : ", "); cerr << " }" << endl;
typedef long long ll;  /* 10^18 くらいまでいける */
typedef unsigned long long ull;  /* 10^18 くらいまでいける */

/* https://blog.hamayanhamayan.com/entry/2021/02/20/233715
 *
 *  M を n進数表記に変換して比較する
 */
string X;
ll M;
bool check(ll n) {
  vector<ll> Y;  // Mをn進数表記に変換
  ll m = M;
  while (0 < m){
	Y.push_back(m % n);
	m /= n;
  }
  reverse(Y.begin(), Y.end());

  // 例えばnが2進数や8進数の場合, 桁数が増えることがある.
  if (X.size() < Y.size())
	return true;

  // 例えばnが16進数の場合, 桁数が減ることがある.
  else if (X.size() > Y.size())
	return false;

  // 桁数が同じ時は大きい桁から1桁ずつ比較する
  for(ll i = 0; i < X.size(); i++){
	ll x = X[i] - '0';
	ll y = Y[i];

	if (x == y) continue;
	else if (x < y) return true;
	else return false;
  }

  // X == Y
  return true;
}

int main(){
  cin >> X;
  cin >> M;

  // もっとも大きい数字dを取得
  char d_char = '0';
  for (ll i = 0; i < X.length(); i++){
	d_char = X[i] > d_char ? X[i] : d_char;
  }
  ll d = d_char - '0';

  if (X.length() == 1){
	ll x = stoll(X);
	if (x <= M)
	  cout << "1" << endl;
	else
	  cout << "0" << endl;
	return 0;
  }

  // 2部探索
  ll min_v = d;
  ll max_v = 2e18;  // 問題の制約の最大値
  ll mid_v;

  // 2部探索はhighとlowの差が1になるまでwhileを回す
  while (min_v + 1 < max_v){
	mid_v = (min_v + max_v) / 2;
    // printf("min_v: %lld, mid_v: %lld, max_v: %lld\n", min_v, mid_v, max_v);
	if (check(mid_v)){
	  min_v = mid_v;
	}else{
	  max_v = mid_v;
	}
  }
  mid_v = (min_v + max_v) / 2;
  // printf("min_v: %lld, mid_v: %lld, max_v: %lld\n", min_v, mid_v, max_v);
  cout << mid_v - d << endl;
}

#include <bits/stdc++.h>

#define endl "\n"
#define int long long
#define debug(statement) \
    if (debugging)       \
    cerr << statement << endl
#define debug_variable(var) \
    if (debugging)          \
    cerr << (#var) << ": " << var << endl

const bool debugging = 1;

using namespace std;

template <class T>
ostream &operator<<(ostream &os, vector<T> V)
{
    for (auto v : V)
    {
        os << v << " ";
    }
    return os << "";
}

template <class T>
ostream &operator<<(ostream &os, set<T> S)
{
    for (auto s : S)
    {
        os << s << " ";
    }
    return os << "";
}

template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P)
{
    return os << P.first << " " << P.second;
}

template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M)
{
    os << endl;
    for (auto m : M)
    {
        os << m << endl;
    }
    return os << "";
}

long long binaryExponentiation(long long base, long long power)
{
    //cout << base << " " << power << endl;
    long long result = 1;
    while (power > 0)
    {
        if (power & 1)
        {
            result = result * base;
        }
        base = base * base;
        power = power >> 1;
    }
    return result;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string ex;
    cin >> ex;
    int N = ex.size();
    int M;
    cin >> M;
    int d = 0;
    for (int i = 0; i < N; i++)
    {
        d = max(d, (int)(ex[i] - '0'));
    }

    if (N == 1)
    {
        if (d + 1 > M && M != ((int)(ex[0] - '0')))
        {
            cout << 0 << endl;
        }
        else
        {
            cout << 1 << endl;
        }
        return 0;
    }

    if (d + 1 >= M)
    {
        cout << 0 << endl;
        return 0;
    }

    int l = d + 1;
    int r = M;
    int ans = 0;
    while (l <= r)
    {
        bool overflow = 0;
        int mid = (r - l) / 2 + l;
        //cout << mid << endl;
        int res = binaryExponentiation(mid, N - 1);
        if (log10(mid) * (N - 1) > 18)
        {
            r = mid - 1;
            continue;
        }

        int total = 0;
        for (int i = 0; i < N; i++)
        {
            if ((res * ((int)(ex[i] - '0'))) / res != ((int)(ex[i] - '0')) || (total + ((int)(ex[i] - '0')) * ex[i]) - ((int)(ex[i] - '0')) * ex[i] != total)
            {
                overflow = 1;
                break;
            }
            total += (res * ((int)(ex[i] - '0')));
            res /= mid;
        }

        if (total <= M && !overflow)
        {
            //cout << "WOOSYFACE" << endl;
            ans = mid;
            l = mid + 1;
        }
        else
        {
            r = mid - 1;
        }
    }
    if (ans == 0)
    {
        cout << 0 << endl;
    }
    else
    {
        cout << ans - d << endl;
    }

    //cout << l << endl;
    //cout << r << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
typedef int64_t ll;
typedef long double ld;
typedef unsigned long long ULL;
#define endl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
void read(vector<int> &a) {for (auto &x : a)cin >> x;}
void read(vector<ll> &a) {for (auto &x : a)cin >> x;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c * x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
    ~debug() { cerr << endl; }
    eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
    sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int MOD = 1e9 + 7;
const int INF = (int)2e9 + 7;
const ll LINF = (ll)1e18;
const ld PI = 3.1415926535897932384626433832795;

void solve() {
    int n;
    cin >> n;
    set<int> s;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        s.insert(x);
    }
    ((int)s.size() == n ? cout << "Yes" : cout << "No");
}

int32_t main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    //cin >> t;
    auto t1 = high_resolution_clock::now();
    for (int tt = 1; tt <= t; tt++)
    {
        //cout << "Case #" << tt << ": ";
        solve();
    }
    auto t2 = high_resolution_clock::now();
    auto time = duration_cast<duration<double>>(t2 - t1);
    cerr << "Time elapsed = " << time.count() << endl;
}

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

char str[500005];
deque<char> dq;

int main() {
    scanf(" %s", &str);
    bool b = true;
    for (int i = 0; str[i]; ++i) {
        if (str[i] == 'R') b = !b;
        else if (b) {
            if (!dq.empty() && str[i] == dq.back()) dq.pop_back();
            else dq.push_back(str[i]);
        }
        else {
            if (!dq.empty() && str[i] == dq.front()) dq.pop_front();
            else dq.push_front(str[i]);
        }
    }
    if (!b) reverse(dq.begin(),dq.end());
    for (char c: dq) printf("%c", c);

    return 0;
}


#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define ll long long
#define FOR(i, a, b) for(ll i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n+1)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<ll, ll>
#define debug(var) cerr << "[" << #var << "] " << var << '\n'
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define pr(s) cout << (s) << '\n'
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
const int INF = 1001001001;
const ll INFll = 1E+18;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < mod);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint per(int n, int k) {
        return fact[n]*ifact[n-k];
    }
};

mint f(ll a, ll b) {
    if (a>b) swap(a, b);
    mint ans = mint((a+b)*(b-a+1)/2);

    return ans;
}

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

    ll t;
    cin >> t;
    rep(ti, t) {
        ll n, a, b;
        cin >> n >> a >> b;
        if (a>b) swap(a, b);
        if (a+b>n) {
            pr(0);
            continue;
        }
        mint ans = mint(n-a+1)*mint(n-a+1)*mint(n-b+1)*mint(n-b+1);
        //debug(ans);
        mint sub = 0;
        sub += (n-a+1)*(n-b+1);
        sub -= mint(n-a-b+2)*mint(n-a-b+1);

        sub *= sub;
        ans -= sub;
        pr(ans);

    }

    return 0;
}

#pragma GCC optimize("Ofast")
#if 1
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cerr << #x << " = " << (x) << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo(x) cout<<((x)?"Yes":"No")<<newl;
#define YESNO(x) cout<<((x)?"YES":"NO")<<newl;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<class A, class B> using umap = unordered_map<A, B>;
template<class A> using uset = unordered_set<A>;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

constexpr double eps = 1e-5;
int N;

struct Pt {
  db x, y;
  db ata() const {
    return atan2(y, x);
  }
  friend ostream& operator<<(ostream& os, const Pt& p) {
    return os << '(' << p.x << ',' << p.y << ')';
  }
};

v(Pt) read() {
  v(Pt) as(N);
  db cx = 0, cy = 0;
  {
    vpii ab(N);
    F0R(i, N) {
      cin >> ab[i];
      cx += ab[i].first; cy += ab[i].second;
    }
    cx /= N; cy /= N;
    F0R(i, N) {
      as[i] = { ab[i].first - cx,ab[i].second - cy };
    }
  }
  return as;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> N;
  auto as = read();
  auto bs = read();
  //show(as);
  //show(bs);
  if (N == 1) {
    cout << "Yes"; return 0;
  }
  int s = 0;
  if (as[0].x == 0 && as[0].y == 0) s++;
  F0R(j, N) {
    //show(j);
    db r = as[s].ata();
    //show(as[0]); show(r);
    db r2 = bs[j].ata();
    //show(bs[j]); show(r2);
    db rot = r2 - r;
    uset<int> us;
    F0R(i, N) {
      // iを回転
      db x = cos(rot) * as[i].x - sin(rot) * as[i].y;
      db y = sin(rot) * as[i].x + cos(rot) * as[i].y;

      // 一致する頂点kを探す
      F0R(k, N) {
        if (abs(x - bs[k].x) < eps && abs(y - bs[k].y) < eps) {
          //if (i==0) show(k);
          us.insert(k); break;
        }
      }
    }
    if (sz(us) == N) {
      cout << "Yes"; return 0;
    }
  }
  cout << "No";
}
#endif


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

#define rep(i,n) for(LL i=0;i<(n);i++)

int main()
{
   LL n;
   LL min = -1;
   LL a, p, x;
   LL remaind;

   cin >> n;

   rep(i,n){

       cin >> a >> p >> x;

       remaind = x - a;

       if(remaind > 0){
           if(min == -1 || min > p)
                min = p;
       }

   }

   cout << min << endl;

    return 0;
}

// auther : chotathanos
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector< pair<int, int> > vpii;
#define MOD 1000000007
#define PI 3.14159265358979323
#define nl "\n"
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (int i=0; i<n; i++)
#define rep(i, a, b) for (int i=a; i<=b; i++)
#define ipARR(a, n) for (int i=0; i<n; i++) cin >> a[i];
#define opARR(a, n) for (int i=0; i<n; i++)cout << a[i] << " ";
#define deb(n) cerr << #n << "=" << n << "\n";

void solve() {
    int n;
    cin >> n;
    int a,x,p;
    int ans = INT_MAX;
    FOR(i,n) {
        cin >> a >> p >> x;
        if(a < x) ans = min(ans,p);
    }
    if(ans==INT_MAX) cout << -1;
    else cout << ans;
}

int main() {
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0);
	int tc = 1;
    // cin >> tc;
	while(tc--)
	solve();
	return 0;
}


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

#define llong long long
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#ifdef testing/*{{{*/
mt19937 rng(177013);
#else
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#endif
#define rand() (int)(rng() >> 1)
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#define SPEC(name) CONCAT(name, __LINE__)
#ifdef LOCAL_DEBUG
int __db_level = 0;
#define clog cerr << string(__db_level * 2, ' ')
struct debug_block {
    string msg;
    debug_block(const string& s): msg(s) { clog << "{ " << msg << endl; ++__db_level; }
    ~debug_block() { --__db_level; clog << "} " << msg << endl; }
};
#define DB(args...) stringstream SPEC(ss); SPEC(ss)<< args; debug_block SPEC(dbbl)(SPEC(ss).str())
#else
#define clog if (0) cerr
#define DB(...)
#endif
#define db(val) "[" #val " = " << val << "]; "
template<class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
    if constexpr(i == tuple_size<T>::value) return out << ")";
    else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U>
ostream& operator<<(ostream& out, const tuple<U...>& tup) { return print_tuple_utils<0, tuple<U...>>(out, tup); }
template<class, typename = void> struct has_const_iterator : false_type {};
template<class T> struct has_const_iterator<T, void_t<typename T::const_iterator>> : true_type {};
template<class T>
typename enable_if<has_const_iterator<T>::value && !is_same<T, string>::value, ostream&>::type
operator<<(ostream& out, const T& container) {
    auto beg = container.begin(), end = container.end();
    out << "(" << container.size() << ") {";
    if (beg != end) out << *(beg++);
    while (beg != end) out << ", " << *(beg++);
    return out << "}";
}
#define ptrtype(x) typename iterator_traits<x>::value_type
template<class u> vector<ptrtype(u)> $v(u a, u b) { return vector<ptrtype(u)>(a, b); }/*}}}*/
// ACTUAL SOLUTION START HERE ////////////////////////////////////////////////////////////////

const int maxn = 201010;
int n;
int a[maxn], b[maxn], p[maxn], rev[maxn];

vector<pair<int, int>> ans;
void do_swap(int u, int v) {
    clog << db(u) << db(v) << endl;
    assert(a[u] > b[p[u]]);
    assert(a[v] > b[p[v]]);
    swap(p[u], p[v]);
    ans.emplace_back(u, v);
}

bool vis[maxn];

int main(void) {
#ifdef LOCAL
    freopen("main.inp", "r", stdin);
    freopen("main.out", "w", stdout);
    freopen(".log", "w", stderr);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    rep1(i, n) cin >> a[i];
    rep1(i, n) cin >> b[i];
    rep1(i, n) cin >> p[i];
    rep1(i, n) rev[p[i]] = i;

    rep1(i, n) {
        if (vis[i] or p[i] == i) continue;
        vector<int> x;
        for (int u = i; !vis[u]; u = rev[u]) {
            vis[u] = true;
            x.push_back(u);
        }

        clog << db(i) << db(x) << endl;

        for (auto u: x) if (a[u] <= b[p[u]]) {
            clog << db(u) << db(a[u]) << db(p[u]) << db(b[p[u]]) << endl;
            cout << "-1";
            return 0;
        }

        auto it = min_element(x.rbegin(), x.rend(), [&](int u, int v) { return b[p[u]] < b[p[v]]; });
        rotate(x.rbegin(), it, x.rend());

        clog << db(x) << endl;

        for (int prev = len(x) - 1, cur = 0; cur < len(x) - 1; prev = cur++) {
            do_swap(x[prev], x[cur]);
        }
    }

    cout << len(ans) << '\n';
    rep1(i, n) assert(p[i] == i);
    for (auto [u, v]: ans) cout << u << ' ' << v << '\n';

    return 0;
}

// vim: foldmethod=marker

#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T>
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template<typename T>
inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } }
template <typename T>
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}

/*
解法
色を頂点、カードを辺としたグラフを考える
1つの連結成分を考えると頂点数-1の色を選べそう
サイクルあれば全部選べそう

*/

ll n;
Graph g;

ll cnt;
bool cyc;

vector<ll>seen;

void dfs(int v,int p){
    for(ll x:g[v]){
        if(x==p)continue;
        if(seen[x]){
            cyc=true;
            continue;
        }
        cnt++;
        seen[x]=true;
        dfs(x,v);
    }
    return;
}

int main(){
    cin >> n;
    g.resize(400010);
    rep(i,n){
        ll a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    seen.resize(400010,false);
    ll ans = 0;
    rep(i,400010){
        if(seen[i])continue;
        seen[i]=true;
        cnt=1;
        cyc=false;
        dfs(i,-1);
        ans+=cnt-1;
        if(cyc)ans++;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
ll h,w,a,b;
ll ans=0;
void dfs(ll index,ll mask,ll lef_a,ll lef_b){

	if(index == h*w){
		ans++;
		return;
	}

	if(mask & (1ll<<index)){
		dfs(index+1,mask,lef_a,lef_b);
		return ;
	}

	if(lef_b){
		dfs(index+1,mask | 1<<index ,lef_a,lef_b-1);
	}

	if(lef_a){
		if(index%w +1 <w and (mask & (1ll<<(index+1)))==0){
			dfs(index+1,mask | (1ll<<index) | (1ll<< (index+1) ) ,lef_a-1,lef_b);
		}

		if(index+w<h*w ){
			dfs(index+1,mask | (1ll<<index) | (1ll<<(index+w)),lef_a-1,lef_b);
		}
	}

}

void solve(){
	cin>>h>>w>>a>>b;
	dfs(0,0,a,b);

	cout<<ans<<endl;

}

int main(){

	boost;
	ll t=1;

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

}

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/*............................................................................*/
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
/* find_by_order(x) give iterator of the index x
   order_by_key(x) give the position where x will be placed*/
#define int long long
#define lop(i,a,b,c) for (int i=a;i<b;i+=c)
#define rlop(i,a,b,c) for (int i=a-1;i>=b;i-=c)
#define prii pair <int,int>
#define PB push_back
#define S second
#define F first
#define all(x) x.begin(),x.end()
#define vvii vector < vector < int > >
#define vii vector <int>
#define count_1(x) __builtin_popcount(x)
#define cn(a) scanf("%lld",&a);
#define cn_vl(arr,n) lop(i,0,n,1){scanf("%lld",arr+i);}
#define shw_vl(arr,n) lop(i,0,n,1){printf("%lld ",arr[i]);}printf("\n");
const int MAX=2e5+10;
/*......................................................................*/
void fastscan(int &number)
{
    bool negative = false;
    register int c;
    number = 0;
    c = getchar();
    if (c=='-')
    {
        negative = true;
        c = getchar();
    }
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;
    if (negative)
        number *= -1;
}
bool cmp(prii a,prii b){
	if (a.F==b.F){
		return a.S<b.S;
	}
	return a.F<b.F;
}
void solve(){
	int n;cin>>n;
	vector <prii> vec1,vec2;
	lop (i,0,n,1){
		int a,b;cn(a)cn(b)
		vec1.PB({a,i});
		vec2.PB({b,i});
	}
	sort(all(vec1));
	sort(all(vec2));
	pair <int,prii> mx1,mx2,mx3,mx4,mx;mx1=mx2=mx3=mx4={-LLONG_MAX,{-1,-1}};
	mx1=max(mx1,{vec2[n-1].F-vec2[0].F,{vec2[n-1].S,vec2[0].S}});
	mx2=max(mx2,{vec1[n-1].F-vec1[0].F,{vec1[n-1].S,vec1[0].S}});
	mx3=max(mx3,{vec2[n-1].F-vec2[1].F,{vec2[n-1].S,vec2[1].S}});
	mx3=max(mx3,{vec2[n-2].F-vec2[0].F,{vec2[n-2].S,vec2[0].S}});
	mx4=max(mx4,{vec1[n-1].F-vec1[1].F,{vec1[n-1].S,vec1[1].S}});
	mx4=max(mx4,{vec1[n-2].F-vec1[0].F,{vec1[n-2].S,vec1[0].S}});
	vector <pair <int,prii> > vec;
	vec.PB(mx1);vec.PB(mx2);vec.PB(mx3);vec.PB(mx4);
	sort(all(vec));
	mx.S=vec[3].S;
	rlop (i,3,0,1){
		if (vec[i].S==mx.S)continue;
		cout<<vec[i].F<<"\n";return;
	}
	// lop (i,1,n,1){
	// 	int z1=max(vec1[i].F-vec1[i-1].F,vec1[i].S-vec1[i-1].S);
	// 	if (z1<mn1){
	// 		cout<<i<<"\n";
	// 		mn2=mn1;
	// 		mn1=z1;
	// 	}
	// 	else if(z1<mn2)mn2=z1;
	// 	cout<<mn2<<" "<<mn1<<" \n";
	// }
	// lop (i,1,n,1){
	// 	int z1=max(vec2[i].F-vec2[i-1].F,vec2[i].S-vec2[i-1].S);
	// 	if (z1<mn1){
	// 		mn2=mn1;
	// 		mn1=z1;
	// 	}
	// 	else if(z1<mn2)mn2=z1;
	// }
}
int32_t main(){
	int t;t=1;
	//cin>>t;
	while (t--){
		solve();
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
    int V, T, S, D;
    cin >> V >> T >> S >> D;

    bool hit = (D < V * T || V * S < D) ? true : false;
    if (hit) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()

using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;

template <typename T>
bool chmax(T& a, const T& b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

void solve(long long A, long long B, long long C, long long D) {
  ll div = C * D - B;
  if(div <= 0) {
    cout << -1 << endl;
    return;
  }
  cout << (A + div - 1) / div << endl;
}

int main() {
  long long A;
  std::scanf("%lld", &A);
  long long B;
  std::scanf("%lld", &B);
  long long C;
  std::scanf("%lld", &C);
  long long D;
  std::scanf("%lld", &D);
  solve(A, B, C, D);
  return 0;
}