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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int,int> > vii;
typedef   vector<long long> vl;
typedef   pair<int,int> pii;
typedef   pair<ll,ll> pll;
typedef   vector< pair<ll,ll> > vll;
//int dx[]={1,0,-1,0};
//int dy[]={0,1,0,-1};
//int dx[]={-1,0,1,1,1,0,-1,-1};
//int dy[]={-1,-1,-1,0,1,1,1,0};

//Time Complexity - O(log(n)log(m))
struct FenwickTree {
    vector<ll> bit;  // binary indexed tree
    int n;

    FenwickTree(int n) {
        this->n = n;
        bit.assign(n, 0);
    }

    ll sum(int r) {
        ll ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    ll sum(int l, int r) {
        return sum(r) - sum(l - 1);
    }

    void upd(int idx, int delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }
};

void nikal_pehli_fursat_mai(){
    ll n, m, q;
    cin>>n>>m>>q;
    vector<array<ll, 3>>qry(q);
    map<ll, ll>id;
    int cnt = 0;
    vl a(n), b(m);
    fi(0, q){
    	fj(0, 3) cin>>qry[i][j];
    	id[qry[i][2]];
    }
    id[0];
    for(auto it: id) id[it.ff] = cnt++;
	FenwickTree fa1(cnt+10), fa2(cnt+10), fb1(cnt+10), fb2(cnt+10);
    ll ans = 0;
    fa1.upd(0, n);
    fb1.upd(0, m);
    fi(0, q){
    	ll t = qry[i][0], x = qry[i][1], y = qry[i][2];
    	x--;
    	if(t==1){
    		ll curr = a[x];
    		ans -= fb1.sum(0, id[curr])*curr;
    		ans -= fb2.sum(id[curr]+1, cnt+5);
    		ans += fb1.sum(0, id[y])*y;
    		ans += fb2.sum(id[y]+1, cnt+5);
    		fa1.upd(id[curr], -1);
    		fa1.upd(id[y], 1);
    		fa2.upd(id[curr], -curr);
    		fa2.upd(id[y], y);
    		a[x] = y;
    	}
    	else{
    		ll curr = b[x];
    		ans -= fa1.sum(0, id[curr])*curr;
    		ans -= fa2.sum(id[curr]+1, cnt+5);
    		ans += fa1.sum(0, id[y])*y;
    		ans += fa2.sum(id[y]+1, cnt+5);
    		fb1.upd(id[curr], -1);
    		fb1.upd(id[y], 1);
    		fb2.upd(id[curr], -curr);
    		fb2.upd(id[y], y);
    		b[x] = y;
    	}
    	cout<<ans<<endl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc=1;
    // cin>>tc;
    while(tc--){
    nikal_pehli_fursat_mai();
    }
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb push_back
#define ld long double
#define f first
#define s second
#define in insert
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int INF = 1e18 + 5;
const int mod = 1e9 + 7;
const int  N = 2e5 + 314;
const long double pi = 3.14;
void solve(){
    int x, y, a, b;
    cin >> x >> y >> a >> b;
    int cnt = 0;
    while(1){
        if(y / a < x)break;
        if(a * x >= y)break;
        if(a * x >= b + x)break;
        x *= a;
        cnt++;
    }
    cnt += (y - x - 1) / b;
    cout << cnt << "\n";
}
signed main(){
    speed;
    int t=1;
    //cin>>t;
    while(t--)solve();
}


#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <cmath>
#include <list>
#include <iterator>

static int check7(int base, int chk)
{
    while(1){
        if((chk % base) == 7){
            return 0;
        }
        chk /=base;
        if(chk == 0){
            return 1;
        }
    }
}

static long long int diffSum(std::vector<long long int>& vec)
{
    long long int sum = std::accumulate(vec.begin(), vec.end(), 0LL);
    long long int ret = 0;
    long long int size = vec.size();

    /*
    for(int i = 0 ; i < size; i++){
        ret += vec[i]*i - sum;
        sum += vec[i];
    }
    */

    for(auto itr = vec.begin(); itr != vec.end(); itr++){
        sum -= (*itr);
        size--;
        ret += (*itr) * size - sum;
    }

    return ret;
}

bool IsPrimeNumber(long long n)
{
    bool ret = true;
    if( n < 2){
        return false;
    }
    for(long long i = 2; (i * i) <= n; i++){
        if((n % i) == 0){
            ret = false;
            break;
        }
    }
    return ret;
}

long long  GetPrimeNumber(long long n)
{
    long long ret = n;
    if( n < 2){
        return false;
    }
    for(long long i = 2; (i * i) <= n; i++){
        if((n % i) == 0){
            ret = i;
        }
    }
    return ret;
}
/*
long long GetMaxPrimeNumber(long long  n)
{
    long long ret = n

    for(long long  i = 2; (i * i) < n ; i++){
        if((n % i) == 0){
            ret = i;
        }
    }
    return ret;
}

long long int leastCommonMultiple(int n1, int n2)
{
    int small_n = (n1 < n2) ? n1 : n2;
    int large_n = (n1 < n2) ? n2 : n1;
    //std::cout << "large_n : " << large_n;
    long long int ret = 1;
    int sn = small_n;
    int ln = large_n;
    for(int i = 2; (i * i) < small_n; i++){
        if(IsPrimeNumber(i) == false) continue;
        while(1){
            bool endflag = true;

            if((sn % i) == 0){
                sn /= i;
                endflag = false;
            }

            if((ln % i) == 0){
                ln /= i;
                endflag = false;
            }

            if(endflag){
                break;
            }else{
                ret *= i;
                large_n =  (sn < ln) ? sn : ln;
            }
        }

    }
    std::cout << ret << " " << ln << " " << sn << std::endl;

    int t1 = (sn < ln) ? ln : sn;
    int t2 = (sn < ln) ? sn : ln;

    if((t1 % t2) == 0){
        ret *= t1;
    }else{
        ret = ret*t1*t2;
    }
    return ret;
}
*/

float ClacProbability(int A, int B, int C)
{
    float ret;

    ret = A/(A + B + C);
    return ret;
}

long long  Combination(long long  n, long long  k)
{
    long long ret = 1;
    long long demoninator = 1;

    if( k <= 0){
        return 1;
    }

    if(n/2 < k){
        k = n -k;
    }

    for(int i = 0; i < k; i++){
        ret *= (n - i)%(long long)(10e9 + 7);
        demoninator *= (i + 1)%(long long)(10e9 + 7);
    }

    return ret/demoninator;
}

long long  CountBaseN(long long N, long long base_count[])
{
    while(1){
        base_count[((N % 10) % 3)]++;
        N /= 10;

        if(N <= 0){
            break;
        }
    }

    return base_count[0] + base_count[1] + base_count[2];
}

int main()
{
    std::vector<long long> T(3, 0);
    std::cin >> T[0] >> T[1] >> T[2];

    std::sort(T.begin(), T.end());

    std::cout << T[2] + T[1] << std::endl;
    return 0;
}

/*
Name: Rishad Nur;
BRUR, CSE,
mail: rishad.nur007@gmail.com;
*/

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

int main()
{
    #ifndef ONLINE_JUDGE
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
    #endif
    long long int n,m,i,j,k,l,x=0,y,a,sum=0,b;

   cin>>n>>m>>l;
   sum=n+m+l;
   x=min(n,min(m,l));
   cout<<sum-x<<endl;

    return 0;
}


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

ll dp[105][maxn] , n, arr[105] , zog , vag;

ll solve(ll pos , ll sum)
{
    if(sum>vag)return 0LL;
    if(pos == n+1)
    {
        return sum;
    }
    if(dp[pos][sum]!=-1)return dp[pos][sum];

    ll ans1 = 0 , ans2 = 0;

    if(sum+arr[pos]<=vag)ans1 = solve(pos + 1 , sum + arr[pos]);
    ans2 = solve(pos + 1 , sum);

    return dp[pos][sum]=max(ans1 , ans2);
}

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

    memset(dp , -1 , sizeof(dp));

    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        zog+=arr[i];
    }

    vag = zog/2;
    //cout << zog << endl;

    ll ans = solve(1 , 0);

    printf("%lld\n",zog-ans);
}

#include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=20,mod=998244353,INF=0x3f3f3f3f;
V check(int&x){x-=mod,x+=x>>31&mod;}
V cmax(int&x,int y){if(x-y>>31)x=y;}
V cmin(int&x,int y){if(y-x>>31)x=y;}
int n,now;
int X[N],Y[N],Z[N],dp[1<<N][N];
I dis(int x,int y){
	return abs(X[x]-X[y])+abs(Y[x]-Y[y])+max(0,Z[y]-Z[x]);
}
V input(){
	n=getint();
	FOR(i,0,n-1)X[i]=getint(),Y[i]=getint(),Z[i]=getint();
}
V init(){
	FOR(i,0,(1<<n)-1)FOR(j,0,n-1)dp[i][j]=INF;
	dp[1][0]=0;
}
V work(){
	int l=(1<<n)-1,u,v,t,flag;
	FOR(i,1,l){
		do{
			flag=0;
			FOR(u,0,n-1)FOR(v,0,n-1){
				t=1<<v|i;
				if(dp[i][u]+dis(u,v)<dp[t][v])
					dp[t][v]=dp[i][u]+dis(u,v),flag=1;
			}
		}while(flag);
	}
	cout<<dp[l][0];
}
int main(){
		input();
		init();
		work();
//	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define Rep(i,j,n) for(int i = j; i < (int)(n); i++)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main(){
    int n; cin >> n;
    vector<pair<int,int>> x(n), y(n);
    rep(i,n) cin >> x[i].first >> y[i].first;
    rep(i,n){
        x[i].second = i;
        y[i].second = i;
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    int ans = max(max(abs(x[1].first - x[n-1].first), abs(x[0].first - x[n-2].first)), max(abs(y[1].first - y[n-1].first), abs(y[0].first - y[n-2].first )));
    if(x[n-1].second == y[n-1].second && x[0].second == y[0].second){
        cout << ans << endl;
    }else{
        cout << max( min(abs(x[n-1].first - x[0].first), abs(y[n-1].first - y[0].first)),ans)  << endl;
    }

    return 0;

}

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define read(a) for(auto &x : a) cin >> x
#define all(a) a.begin(), a.end()
#define write(a) for(auto x : a) cout << x << " ";cout << endl
#define heap priority_queue<pair<int, int>,  vector<pair<int, int>>, cmp>
using namespace std;

#ifndef ONLINE_JUDGE
#define deb(x) {cerr << #x <<" = "; _print(x); cerr << endl;}
#else
#define deb(x)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

// void _print(int t) {cerr << t;}
void _print(ll t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

vector<pair<int, int>> merge(vector<pair<int, int>> &v1, vector<pair<int, int>> &v2, vector<pair<int, int>> &a){
    vector<pair<int, int>> ans;
    for(auto p : v1){
        int total = count(all(v1), p) + count(all(v2), p);
        total = min(total, (int)count(all(a), p));
        int already = count(all(ans), p);
        while(already < total){
            ans.push_back(p);
            already++;
        }
    }
    for(auto p : v2){
        int total = count(all(v1), p) + count(all(v2), p);
        total = min(total, (int)count(all(a), p));
        int already = count(all(ans), p);
        while(already < total){
            ans.push_back(p);
            already++;
        }
    }
    return ans;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    int n; cin >> n;
    vector<pair<int, int>> a(n), vec, vec2;
    // set<pair<int, int>> pos;
    for(auto &p : a) cin >> p.first >> p.second;

    sort(all(a));
    for(int i = 0; i < 3; i++){
        vec.push_back(a[i]);
        vec.push_back(a[n - 1 - i]);
    }

    for(auto &p : a) swap(p.first, p.second);
    sort(all(a));
    for(auto &p : a) swap(p.first, p.second);

    for(int i = 0; i < 3; i++){
        vec2.push_back(a[i]);
        vec2.push_back(a[n - 1 - i]);
    }
    vector<int> ans;
    // for(auto p : pos) vec.push_back(p);
// deb(vec);
// deb(vec2);
    vec = merge(vec, vec2, a);
// deb(vec);

    for(int i = 0; i < (int)vec.size(); i++){
        for(int j = i + 1; j < (int)vec.size(); j++){
            int dx = llabs(vec[i].first - vec[j].first);
            int dy = llabs(vec[i].second - vec[j].second);
            int d = max(dx, dy);
            ans.push_back(d);
        }
    }
    sort(all(ans));
    assert(ans.size() >= 2);
    // deb(ans);

    cout << ans[ans.size() - 2] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	long long t;cin>>t;
	while(t--){
		long long l,r;cin>>l>>r;
		if(l*2ll>r)cout<<0<<endl;
		else{
			long long d=r-l;
			long long f=d-l+1;
			long long g=f*(f+1);
			long long h=g/2;
			cout<<h<<endl;
		}
	}
}

#include <bits/stdc++.h>

using namespace std ;

#define i64 int64_t // typecast using i64(x)
#define int int64_t
#define ld long double
#define f(i,a,b) for(int i = int(a); i < int(b); ++i)
#define endl '\n'

#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fr first
#define sc second

#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define sz(x) ((int)(x).size())
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))

//#ifndef ONLINE_JUDGE
template<typename T>
void __p(T a) {
  cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
  cout<<"{";
  __p(a.first);
  cout<<",";
  __p(a.second);
  cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
  cout<<"{";
  for(auto it=a.begin(); it<a.end(); it++)
    __p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
  __p(a1);
  __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cout<<name<<" : ";
  __p(arg1);
  cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  int bracket=0,i=0;
  for(;; i++)
    if(names[i]==','&&bracket==0)
      break;
    else if(names[i]=='(')
      bracket++;
    else if(names[i]==')')
      bracket--;
  const char *comma=names+i;
  cout.write(names,comma-names)<<" : ";
  __p(arg1);
  cout<<" | ";
  __f(comma+1,args...);
}
//#endif

void setIO(string s = "") {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin.exceptions(cin.failbit);
  if(sz(s)){
    freopen((s+".in").c_str(),"r",stdin);
    freopen((s+".out").c_str(),"w",stdout);
  }
}
void solve(){
  int L, R ; cin >> L >> R ;
  int temp = max(int(0),R - 2*L + 1) ;
  cout << temp*(temp+1)/2 << endl ;
}
signed main(){
  setIO() ;
  int T ; cin >> T ;
  f(_,0,T){
    solve() ;
  }
}


#include <bits/stdc++.h>

using namespace std;

#define il inline
#define re register
#define Rep(i, s, e) for (re int i = s; i <= e; ++i)
#define Dep(i, s, e) for (re int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)

const int N = 200010;

il int read() {
    int x = 0; bool f = true; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = false; c = getchar();}
    while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

int n; char s[N];
int st[N], top;

int main() {
    n = read(), scanf("%s", s + 1);
    Rep(i, 1, n) {
        st[++top] = s[i];
        if (top >= 3 && st[top] == 'x' && st[top - 1] == 'o' && st[top - 2] == 'f') top -= 3;
    }
    printf("%d", top);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define vec vector<int>
#define vecp vector<pair<int,int>>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define fr1(i,a,b) for(int i=a;i<b;i++)
#define fr2(i,a,b) for(int i=a;i>=b;i--)
#define fr3(i,a,b) for(int i=a;i<=b;i++)
#define umap unordered_map<int,int>
#define omap map<int,int>
#define uset unordered_set<int>
#define oset set<int>
#define pr pair<int,int>
#define endl "\n"
#define mod 1000000007
#define all(v) v.begin(),v.end()

void solve()
{
    int n;
    cin>>n;
    string S;
    cin>>S;
    fr1(i,0,n-2)
    {
        if(i+2<n)
        {
            string sub=S.substr(i,3);
        if(sub=="fox")
        {
            S.erase(i,3);
            if(i>2)
                i-=3;
            else
                i=-1;
            n=S.size();
        }
        }
        if(S.size()==0)
        {
            cout<<0<<endl;
            return ;
        }

    }
    cout<<S.size()<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    //cin>>t;
    fr3(i,1,t){
        solve();
    }
}


const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef complex<double> cd;

#define forn(i, n) for (int i = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
string Yes[2] = {"No\n", "Yes\n"};
string YES[2] = {"NO\n", "YES\n"};
string Possible[2] = {"Impossible\n", "Possible\n"};
string POSSIBLE[2] = {"IMPOSSIBLE\n", "POSSIBLE\n"};

int ok(int x, int n)
{
    return 0 <= x && x < n;
}

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

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

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

#include<bits/stdc++.h>
#include <cstdlib>
#define ll long long int
#define ve vector<int>
#define vl vector<long long int>
#define vd vector<double>
#define vs vector<string>
#define vve vector<vector<int>>
#define all(a) a.begin(),a.end()
using namespace std;
const int MAX = 510000;
const int MOD = 1000000007;

int main(){
  int t;
  cin>>t;
  ll kesu;
  for(int i=0;i<t;i++){
    cin>>kesu;
    if(kesu%4==0) cout<<"Even"<<endl;
    else if(kesu%2==0) cout<<"Same"<<endl;
    else cout<<"Odd"<<endl;

  }

  return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define int long long
using namespace std;
const int maxn = 3e5+10;

template <typename _tp>
inline void read(_tp& x) {
    char ch = getchar(), sgn = 0;
    while (ch ^ '-' && !isdigit(ch)) ch = getchar();
    if (ch == '-') ch = getchar(), sgn = 1;
    for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    if (sgn) x = -x;
}
vector<int>e[maxn];
int L[maxn], R[maxn], tim;
vector<int>v[maxn];
int d[maxn], d1[maxn];
void dfs(int u, int dep){
    L[u] = ++tim;
    d[tim] = dep;
    d1[u] = dep;
    v[dep].push_back(L[u]);
    for(auto v: e[u]){
        dfs(v, dep+1);
    }
    R[u] = tim;
}

bool isson(int u, int v){
    return(L[u] >= L[v] && L[u] <= R[v]);
}
signed main(){
    int n;
    scanf("%lld", &n);
    for(int i = 2; i <= n; ++i){
        int a;
        scanf("%lld", &a);
        e[a].push_back(i);
    }
    dfs(1, 0);
    int q;
    scanf("%lld", &q);
    while(q--){
        int a, b;
        scanf("%lld%lld", &a, &b);
        if(d1[a] > b){
            printf("0\n");
            continue;
        }
        else if(d1[a] == b){
            printf("1\n");
            continue;
        }
        cout << upper_bound(v[b].begin(), v[b].end(), R[a]) - lower_bound(v[b].begin(), v[b].end(), L[a]) << endl;
        //printf("%lld\n", ans);
    }
    //return 0;
}
/*
7
1 1 2 2 4 2
4
1 2
7 2
4 1
5 5
*/

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

using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1.0)

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;

int main(){
    int T; cin >> T;
    while(T--) {
        string s; cin >> s;
        int n = s.size();
        string t = "atcoder";
        if(s > t){
            cout << 0 << endl;
            continue;
        }
        bool ok = false;
        rep(i,n) {
            if(s[i] != 'a') ok = true;
        }
        if(!ok){
            cout << -1 << endl;
            continue;
        }
        int ans = INF;
        repr(i,1,n){
            if(s[i] > 't') {
                ans = min(ans,i-1);
            }
            else if(s[i] != 'a') {
                ans = min(ans,i);
            }
        }
        cout << ans << endl;
    }
}


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

#define ll  long long
#define ull unsigned long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"

const ll maxn=3e3+10;
const ll mod =1e9+7 ;
const ll base=3e18;

ll cnt[maxn];
ll f[maxn];
ll dp[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("t.inp", "r"))
    {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    ll n;
     cin>>n ;
     for (int i=1;i<=n;i++)
     {
         ll x;
          cin>> x;
         f[i]=f[i-1]+x;
     }
     ll ans=1;
     for (int j=1;j<=n;j++)
     {
         dp[j]=1;
     }
     for (int k=2;k<=n;k++)
     {
         for (int j=1;j<=n;j++)
         {
             ll pre=dp[j];
            dp[j]=cnt[f[j]%k];
            cnt[f[j]%k]=(cnt[f[j]%k]+pre)%mod;
           // if (k==2) cout <<dp[j]<<" "<<f[j]<<endl;
         }
         for (int j=0;j<=n;j++) cnt[j]=0;
           ans=(ans+dp[n])%mod;
     }
     cout <<ans;

}

#include <bits/stdc++.h>

#define ms(a,b) memset(a, b, sizeof(a))
#define rep(a,b,c) for(int a = (int)(b); a < (int)(c); a++)
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define m_p(a,b) make_pair(a, b)
#define lson l,mid,o << 1
#define rson mid + 1,r,o << 1 | 1
#define ls o << 1
#define rs o << 1 | 1
#define inf 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int,int> pii;

inline bool isdigit(char& ch) {
	return ch >= '0' && ch <= '9';
}
template<class T>
void read(T &x) {
	x = 0;
	ll f = 1;char ch = getchar();
	for (;!isdigit(ch);ch = getchar())
		if (ch == '-') f = -1;
	for (; isdigit(ch);ch = getchar())
		x = (x << 1) + (x << 3) + ch - '0';
	x *= f;
}
template<class T>
inline void write(T x) {
	if (x == 0) {putchar('0');return ;}
	if (x < 0) {putchar('-');x = -x;}
	int _stk[65],_top = 0;
	for (;x;x /= 10) _stk[++_top] = x % 10 + 48;
	for (;_top;_top--)putchar(_stk[_top]);
}

const int MAXN = 3030;
const ll mod = 1e9 + 7;

ll sum[MAXN], arr[MAXN];
ll dp[MAXN][MAXN], f[MAXN][MAXN];

int main () {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	rep(i, 1, n + 1) {
		cin >> arr[i];
		sum[i] = sum[i-1] + arr[i];
	}
	dp[0][0] = 1;
	rep(i, 1, n + 1) {
		rep(j, 1, i + 1) {
			f[j][sum[i-1] % j] += dp[i-1][j-1];
			f[j][sum[i-1] % j] %= mod;
		}
		rep(j, 1, i + 1) {
			dp[i][j] = f[j][sum[i] % j] % mod;
			/*rep(k, 0, i) {
				dp[i][j] += dp[k][j-1] * ((sum[i] - sum[k]) % j == 0);
				if (dp[i][j] >= mod)
					dp[i][j] -= mod;
			}*/
		}
	}
	ll ans = 0;
	rep(i, 0, n + 1) {
		ans += dp[n][i];
		ans %= mod;
	}
	cout << ans;
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

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

// template starts
using namespace __gnu_pbds; //required
using namespace std;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// ordered_set <int> s;
// s.find_by_order(k); returns the (k+1)th smallest element
// s.order_of_key(k); returns the number of elements in s strictly less than k

#define MOD              (1000000000+7) // change as required
#define pb(x)            push_back(x)
#define mp(x,y)          make_pair(x,y)
#define all(x)           x.begin(), x.end()
#define print(vec,l,r)   for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;
#define input(vec,N)     for(int i = 0; i < (N); i++) cin >> vec[i];
#define debug(x)         cerr << #x << " = " << (x) << endl;
#define leftmost_bit(x)  (63-__builtin_clzll(x))
#define rightmost_bit(x) __builtin_ctzll(x)
#define set_bits(x)      __builtin_popcountll(x)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long int ll;

// start of highly risky #defines

#define int ll // disable when you want to make code a bit faster
#define endl '\n' // disable when dealing with interactive problems

// End of highly risky #defines

template <typename T>
class segtree{
public:

	struct item{
		T val;
	};
	item sentinel;// the out of range value
	vector<item> arr;//[4*MAXN]; // MAXN = 2e5

	T N;

	segtree(const T  sz){
		N = 1;
		sentinel.val = 0;
		// for sum:sentinal_val = 0, min = 1e17, max = -1e7, XOR = 0, gcd = 1

		while(N < sz) N <<= 1;
		arr.resize(2*N);

		for(int i = 0; i < 2*N; i++) arr[i] = sentinel;
	}

	item combine(item a, item b){
		item res;
		res.val = a.val^b.val;

		return res;
	}

	void build(vector<T> &vec,int index, int l, int r){
		if(l == r){
			if(l >= (int)vec.size()) return;

			arr[index].val = vec[l];
			return;
		}
		int m = (l+r)/2;

		build(vec,2*index,l,m);
		build(vec,2*index+1,m+1,r);

		arr[index] = combine(arr[2*index],arr[2*index+1]);
	}
	void build(vector<T> &vec){
		build(vec,1,0,N-1);
	}

	// 0 based indexing for upd_index
	void update(int index, int l, int r, int upd_index,T upd_val){
		if(l > upd_index || upd_index > r) return;

		if(l == r){
			arr[index].val = upd_val;
			return;
		}

		int m = (l+r)/2;

		update(2*index,l,m,upd_index,upd_val);
		update(2*index+1,m+1,r,upd_index,upd_val);

		arr[index] = combine(arr[2*index],arr[2*index+1]);
	}

	// 0 based indexing for upd_index
	void update(int upd_index, T upd_val){
		update(1,0,N-1,upd_index,upd_val);
	}

	item query(int index, int l, int r, int lx, int rx){
		if(l > rx || lx > r) return sentinel;

		if(lx <= l && r <= rx){
			return arr[index];
		}
		int m = (l+r)/2;

		return combine(query(2*index,l,m,lx,rx),
		               query(2*index+1,m+1,r,lx,rx));
	}

	// assuming lx and rx are 0 based
	// query returns the answer in the range [lx,rx]
	// including lx and rx
	T query(int lx, int rx){
		return query(1,0,N-1,lx,rx).val;
	}
};

void solve(){
	// code starts from here
	int N, Q;
	cin >> N >> Q;

	vector<int> vec(N);
	input(vec,N);

	segtree<int> s(N);
	s.build(vec);

	while(Q--){
		int t,x,y;
		cin >> t >> x >> y;
		if(t == 1){
			vec[x-1] ^= y;
			s.update(x-1,vec[x-1]);
		}
		else cout << s.query(x-1,y-1) << endl;
	}
}

signed main(){
 	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	int T;
	// cin >> T;
	T = 1;
	while(T--){
		solve();
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()

ll op(ll x, ll y) {return x^y;} // 関数
ll e = 0;     // 元
ll len = 1;     // セグメントの長さ
void seg_len(ll N) {
    while (len<=N) {len *= 2;}
}

struct segtree {
    vector<ll> vec;

    segtree(ll N) : vec(2*len) {
        rep(i,2*len) vec[i] = e;
    }

    void set(ll id, ll x) {
        id += len;
        vec[id] = x;
        while (1) {
            id /= 2;
            if (id==0) break;
            vec[id] = op(vec[2*id],vec[2*id+1]);
        }
    }

    ll get(ll id) {
        id += len;
        return vec[id];
    }

    ll prod(ll l, ll r) {
        l += len;
        r += len;
        ll res = e;
        while (l<r) {
            if (l%2) {
                res = op(res,vec[l]);
                l++;
            }
            l /= 2;
            if (r%2) {
                res = op(res,vec[r-1]);
                r--;
            }
            r /= 2;
        }
        return res;
    }
};

int main() {
    ll N, Q; cin >> N >> Q;
    seg_len(N);
    segtree seg(N);
    rep(i,N) {
        ll A; cin >> A;
        seg.set(i,A);
    }
    vector<ll> T(Q), X(Q), Y(Q);
    rep(i,Q) cin >> T[i] >> X[i] >> Y[i];

    rep(i,Q) {
        ll t = T[i], x = X[i]-1, y = Y[i];
        if (t==1) {
            ll nv = seg.get(x) ^ y;
            seg.set(x,nv);
        }
        else if (t==2) {
            ll ans = seg.prod(x,y);
            cout << ans << endl;
        }
    }
}


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

#define rep(i, n) for (int i = 0; i < n; i++)
#define YesNo(T) if(T){cout << "Yes" << endl;}else{cout << "No" << endl;}

using namespace std;
typedef long long ll;

int main()
{
    int n, s, d;
    cin >> n >> s >> d;
    vector<int> x(n), y(n);
    rep(i, n) cin>>x[i] >> y[i];
    bool damage = false;
    rep(i,n){
        if(x[i]<s && y[i]>d){
            damage = true;
            break;
        }
    }
    YesNo(damage);
    return 0;
}

/** Seek God..Trust God..Praise God..**/

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

#define fRead(x)        freopen("x.txt","r",stdin)
#define fWrite(x)       freopen ("x.txt","w",stdout)

#define mt              make_tuple
#define ld              long double
#define ll              long long
#define ull             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define INF             2e16
#define PI              acos(-1.0)
#define mp              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>
#define endl            "\n";

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())

#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;

#define Yes cout << "YES" << endl;
#define No cout << "NO" << endl;

//#define FASTIO        ios_base::sync_with_stdio(0);cin.tie(nullptr);

///..................Sort by second element in a pair in decending order............. ///
bool SortbySecDesc(const pair<long long,long long > &a, const pair<long long,long long > &b)
{
    return a.second>b.second;
}

///.........................first element of pair in descending order..........................//////
bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
    return (a.first > b.first);
}

///.............................................................TEMPLATE................................................./////

/// Convert String to Int ////

int String_to_int(string s)
{

    stringstream geek(s);

    int ans = 0;

    geek >> ans;

    return ans;
}

/// string to int ///
int S_I(string s)
{

    return stoi(s);
}

/*
/// int to string ///
string I_S(int n)
{
    return atoi(n);
}
*/

/*
long long  Prime[3000000],nPrime;
long long  mark[10000002];

void seive(long long int n)
{
	long long int i,j,limit=sqrt(n*1.0)+2;
	mark[1]=1;
	for(i=4; i<=n ;i+=2)
	mark[i]=1;
	Prime[nPrime++]=2;
	for(i=3; i<=n; i+=2)
	{
		if(!mark[i])
		{
			Prime[nPrime++]=i;
			if(i<=limit)
			{
				for(j=i*i; j<=n ;j+=i*2)
				{
					mark[j]=1;
				}
			}
		}
	}
}
*/

int main()
{

    ll  n , s , d;

    cin >> n >> s >> d;

    bool f = 0;
    for(int i=0; i<n; i++)
    {
        ll a , b;

        cin >> a >> b;

        if(a<s && b>d)
        {
            f = 1;
        }
    }

    if(f)
    {
        yes;
    }
    else
    {
       no;
    }

    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll A, B, C;
    cin >> A >> B >> C;
    if(A * A + B * B < C * C) cout << "Yes";
    else cout << "No";
    cout << endl;
    return 0;
}

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main(){
    int a, b, c;

    cin >> a >> b >> c;

    if(a*a + b*b < c*c){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}


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

#define Mod(x) (x>=P)&&(x-=P)
#define rep(i,a,b) for(ll i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(ll i=a,i##end=b;i>=i##end;--i)
#define erep(i,a) for(ll i=hd[a];i;i=nxt[i])

typedef long long ll;
void Max(ll &x,ll y){x<y&&(x=y);}
void Min(ll &x,ll y){x>y&&(x=y);}

bool vio;
char IO;
ll rd(ll res=0){
	bool f=0;
	while(IO=getchar(),IO<48||IO>57)
		f|=IO=='-';
	do res=(res<<1)+(res<<3)+(IO^48);
	while(IO=getchar(),isdigit(IO));
	return f?-res:res;
}
const ll M=1005;
ll nxt[M],to[M],hd[M],ecnt,n,m;
bool mk[M],fl;
ll dep[M];
void Add(ll a,ll b){
	nxt[++ecnt]=hd[a],to[hd[a]=ecnt]=b;
}
void dfs(ll x){
	if(!fl)return ;
	erep(i,x){
		ll y=to[i];
		if(!mk[y])continue;
		if(dep[y]==-1)dep[y]=dep[x]^1,dfs(y);
		else if(dep[y]==dep[x])fl=0;
	}
}
bool let;
int main(){
	cerr<<(&vio-&let)/1024.0/1024<<endl;
	n=rd(),m=rd();
	rep(i,1,m){
		ll a=rd(),b=rd();
		Add(a,b),Add(b,a);
	}
	ll S=(1<<n)-1,res=0;
	rep(k,0,S){
		rep(i,1,n)mk[i]=0,dep[i]=-1;
		rep(i,1,n)if(1<<(i-1)&k)
			mk[i]=1;
		ll ans=1;
		fl=1;
		rep(i,1,n)if(!mk[i])
			erep(j,i)if(!mk[to[j]])fl=0;
		rep(i,1,n)if(mk[i]&&dep[i]==-1)
			ans=ans*2,dep[i]=0,dfs(i);
		if(fl)res+=ans;
	}
	printf("%lld",res);
	return 0;
}

//include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18 + 10;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)

template <typename T>
T my_abs(T x){
    return (x >= 0)? x : -x;
}

template <typename T>
void chmax(T &a, T b){
    a = max(a, b);
}

template <typename T>
void chmin(T &a, T b){
    a = min(a, b);
}

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

bool incld_bit(int bit, int i){
    return ((bit>>i) & 1) == 1;
}

// --------------------------------------------------------------------------------

int main(void){
    int i, j;
    int a, b, c;

    cin >> a >> b >> c;

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

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#include<cmath>
#include<string>
#define pb push_back
#define x first
#define y second
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(x) x.begin(),x.end()
#define int long long
#define ll long long
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
//void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// look for  all edge cases
//search for a pattern

void solve(){
	int a,b;
	cin >> a >> b;
	int ans = 1;
	for(int i = 2 ; i < (b - a) + 1; i++){
		int val1 = a/i;
		int val2 = b/i;
		val2 += (a % i == 0) ? 1:0;
		//debug(val2, val1);
		if(val2 - val1 > 1){
			ans = i;
		}
	}
	cout << ans ;
}

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

	solve();

	return 0;
}

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

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define UQ(x) (x).resize(distance((x).begin(),unique(x.begin(),x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a,ll m,ll mod){
	ll ans = 1;
	while(m){
		if(m%2) ans *= a, ans %= (mod);
		a = (a*a) % mod;
		m >>= 1;
	}
	return ans;
}

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

  	size_t operator()(uint64_t x) const {
    	static const uint64_t FIXED_RANDOM =
        	chrono::steady_clock::now().time_since_epoch().count();
    	return splitmix64(x + FIXED_RANDOM);
  	}
  	size_t operator()(pair<int64_t, int64_t> Y) const {
    	static const uint64_t FIXED_RANDOM =
        	chrono::steady_clock::now().time_since_epoch().count();
    	return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM);
  	}
};

const long long inf = 1e18;
const long long M = 1000000007;

void solve(){
	ll i,j,_;
	ll n, m;
	cin >> n >> m;
	// brute(n,m,0);
	if(n == 1 && m == 0) {
		cout << 1 << ' ' << 2 << endl;
		return;
	}
	if(m < 0 || m > n - 2) {
		cout << - 1 << endl;
		return;
	}
	cout << 1 << ' ' << 2 * (m + 2) << endl;
	j = 2;
	rep(i,1,m + 2) {
		cout << j << ' ' << j + 1 << endl;
		j += 2;
	}
	n -= m + 2;
	j++;
	while(n--) {
		cout << j << ' ' << j + 1 << endl;
		j += 2;
	}
}

int main()
{
	ios::sync_with_stdio(0);
 	cin.tie(0);
	ll T=1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int N;
string S;
vector<int> L, R;

vector<int> nxt(vector<int> A){
    int K = inf;
    rep(i, N) chmin(K, abs(A[i+1]-A[i]));

    if(K == 1) return A;

    vector<int> ret(N+1);

    int m = *min_element(all(A));

    rep(i, N+1){
        ret[i] = (A[i]-m)/K;
    }

    return ret;
}

int main(){
    cin >> N >> S;
    vector<int> A(N+1);
    rep(i, N+1) cin >> A[i];

    L.assign(N+1, 0), R.assign(N+1, 0);

    rep(i, N+1){
        int tmp = 0;
        rep3(j, i-1, 0){
            if(A[j] < A[j+1]) tmp--;
            else tmp++;
            chmin(L[i], tmp);
        }

        tmp = 0;
        rep2(j, i+1, N){
            if(A[j-1] > A[j]) tmp--;
            else tmp++;
            chmin(R[i], tmp);
        }
    }

    int K = inf;
    rep(i, N) chmin(K, abs(A[i+1]-A[i]));

    cout << K << '\n';

    rep(i, K){
        vector<int> v = nxt(A);
        rep(j, N+1) cout << v[j] << (j == N? '\n' : ' ');
        rep(j, N+1) A[j] -= v[j];
    }
}

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << "\n"

void solve(){
    ll N,K;
    cin >> N >> K;
    ll ans = 0;
    rep(ab,2,2*N+1){
        ll cd = ab - K;
        if(cd >= 2 && cd <= 2*N){
            ll n1 = 0, n2 = 0;
            if(ab <= N){
                n1 = ab-1;
            }else{
                n1 = 2 * N - ab + 1;
            }
            if(cd <= N){
                n2 = cd - 1;
            }else{
                n2 = 2 * N - cd + 1;
            }

            ans += n1 * n2;
        }
    }
    print(ans);

}

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


#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;a<b?i<b:i>b;a<b?i+=1:i-=1)
#define f first
#define s second
#define pb push_back
#define tc int t;cin>>t;while(t--)
#define mp make_pair
using namespace std;
long long countConsecutive(long long N)
{
    long long count = 0;
    for (long long L = 1; L * (L + 1) < 2 * N; L++)
    {
        double a = (1.0 * N-(L * (L + 1)) / 2) / (L + 1);
        if (a-(long long)a == 0.0)
            count++;
    }
    return count;
}
int main()
{
  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    cout<<2*(countConsecutive(n)+1);
}

#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<tuple>
#include<string.h>
#include <numeric>
#include<unordered_map>
using namespace std;
#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 {
#ifdef LOCAL
~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 pp(...) "\033[94m [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "]\033[0m"
#define pp1(...) "\033[1;47;35m" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "]\033[0m"
//Macro overload
#define PP_CAT( A, B ) A ## B
#define PP_EXPAND(...) __VA_ARGS__

// Macro overloading feature support
#define PP_VA_ARG_SIZE(...) PP_EXPAND(PP_APPLY_ARG_N((PP_ZERO_ARGS_DETECT(__VA_ARGS__), PP_RSEQ_N)))

#define PP_ZERO_ARGS_DETECT(...) PP_EXPAND(PP_ZERO_ARGS_DETECT_PREFIX_ ## __VA_ARGS__ ## _ZERO_ARGS_DETECT_SUFFIX)
#define PP_ZERO_ARGS_DETECT_PREFIX__ZERO_ARGS_DETECT_SUFFIX ,,,,,,,,,,,0

#define PP_APPLY_ARG_N(ARGS) PP_EXPAND(PP_ARG_N ARGS)
#define PP_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N,...) N
#define PP_RSEQ_N 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0

#define PP_OVERLOAD_SELECT(NAME, NUM) PP_CAT( NAME ## _, NUM)
#define PP_MACRO_OVERLOAD(NAME, ...) PP_OVERLOAD_SELECT(NAME, PP_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__)
//Overload dg_
#define dg(...)       PP_MACRO_OVERLOAD(dg, __VA_ARGS__)
#define dg_1(x) debug() <<"\033[0;33mLine(" << __LINE__ <<")\033[0m"<<"\033[1;36m->"<<"\033[0m"<< pp(x)
#define dg_2(x,y) debug() << "\033[0;33mLine(" << __LINE__ <<")\033[0m"<<"\033[1;36m->"<<"\033[0m"<< pp(x) pp(y)
#define dg_3(x,y,z) debug() << "\033[0;33mLine(" << __LINE__ <<")\033[0m"<<"\033[1;36m->"<<"\033[0m"<< pp(x) pp(y) pp(z)
#define dg_4(w,x,y,z) debug() << "\033[0;33mLine(" << __LINE__ <<")\033[0m"<<"\033[1;36m->"<<"\033[0m"<< pp(w) pp(x) pp(y) pp(z)
#define dg_5(w,x,y,z,z1) debug() << "\033[0;33mLine(" << __LINE__ <<")\033[0m"<<"\033[1;36m->"<<"\033[0m"<< pp(w) pp(x) pp(y) pp(z) pp(z1)
#define dg_6(w,x,y,z,z1,z2) debug() << "\033[0;33mLine(" << __LINE__ <<")\033[0m"<<"\033[1;36m->"<<"\033[0m"<< pp(w) pp(x) pp(y) pp(z) pp(z1) pp(z2)

#define sf(...)   PP_MACRO_OVERLOAD(sf, __VA_ARGS__)
#define sf_1(x)   scanf("%lld",&x);
#define sf_2(x, y) scanf("%d %d", &x,&y);

// #define int long long
#define all(x) x.begin() , x.end()
#define clr(x , y) memset(x , y , sizeof x);
#define sz(x) (int)x.size()
using lint = long long int;
const int Mod = 1e9 + 7;
const lint inf = 1e18;
const int N = 2e5;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint n;
  cin >> n;
  lint ans = 0;

  for(lint i = 1;i <= min(n , 10000000ll); i++) {
     lint m = 2 * n;
    //  if(m % i) continue;
     lint a = m/i;
     lint b = i - 1;
     if(b > a) swap(a, b);
     dg(i , m , a , b);
     lint f = (a + b) / 2;
     lint s = (a - b) / 2;
     if(f > s) swap(f , s);
     lint v = ( i * (f + s) ) / 2;
     if(f > 0 && v == n) {
     dg(f , s , v);
       ans++;
       if(f > 0) ans++;
     }
  }
  cout << ans << '\n';
}


#include <bits/stdc++.h>

using namespace std;

const int MX = 1e5 +5;

int N, zeroNum = 0;
double ans = 0;

int main(){
    cin.tie(nullptr), ios::sync_with_stdio(false);
    cin >> N;
    for(int i = 1; i < N; i++) ans += ((double)N / (double)i);
    cout.precision(9);
    cout << fixed << ans;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  string S; cin >> S;
  int cnt = 0;
  for (int i = 0; i < 9; i++) {
    if (S.substr(i, 4) == "ZONe") cnt++;
  }
  cout << cnt << endl;
}


#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/extc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.precision(10);
/*------------------------------------*/

    int n;
    cin >> n;
    vector<ll> v(2*n);
    ll sum=0;
    rep(i,2*n)cin >> v[i], sum+=v[i];
    priority_queue<ll,vector<ll>,greater<ll>> pq;
    ll ans=0;
    rep(i,n){
        pq.push(v[n-i-1]);
        pq.push(v[n+i]);
        ans+=pq.top();
        pq.pop();
    }
    cout << sum-ans << newl;
}

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

using ll = long long;

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

  int n;
  cin >> n;

  ll ans = 0;

  vector<ll> v(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    cin >> v.at(i);
    ans += v.at(i);
  }

  priority_queue<ll, vector<ll>, greater<ll>> pq;
  for (int i = n - 1; i >= 0; i--) {
    pq.push(v.at(i));
    pq.push(v.at(2 * n - i - 1));
    ans -= pq.top();
    pq.pop();
  }

  cout << ans << '\n';
}


#include <bits/stdc++.h>
#define forn(i,n) for(int i=0;i<(int)n;i++)
#define ce(e,i,n) cout<<e<<" \n"[i==(int)n-1];
using namespace std;
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	//   = 0  - [i] {} '\n'  p _  ? ^
	int n;
	long long c,x,y,z;;
	cin>>n>>c;
	vector<pair<long long,long long>>a;
	forn(i,n){
		cin>>x>>y>>z;
		a.push_back({x-1,z});
		a.push_back({y,-z});
	}
	sort(a.begin(),a.end());
	long long ans=0,b=0,to=0;
	//for(auto e:a)cout<<e.first<<" "<<e.second<<endl;
	forn(i,a.size()){
		ans+=min(c,to)*(a[i].first-b);
		b=a[i].first;
		to+=a[i].second;
	}
	cout<<ans<<'\n';
}

#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   Y            imag()
#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
#define    dp(a,b)          (((conj(a))*(b)).real())
#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     point                    complex<long double>
#define     PI                       acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-15);
using namespace std;
int dcmp(double a,double b){   return fabs(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask) 	{mask &= (mask-1);++ret;	}return ret;}
const ll N=200000+5,mod=998244353,sh=1000000,inf=100000000;
ll a[N],b[N],c[N];
map<ll,ll> arr;
int main(){
    GO();
    int T=1;
  //  cin>>T;
    while(T--){

        int n,cc;
        cin>>n>>cc;
        for(int i=0;i<n;i++){
            cin>>a[i]>>b[i]>>c[i];
            arr[a[i]]+=c[i];
            arr[b[i]+1]-=c[i];
        }
        ll ans=0;
        auto pre=arr.begin();
        for(auto it=next(arr.begin());it!=arr.end();it++){
            ll ff=(*it).first;
            ll ss=(*it).second;

            (*it).second+=(*pre).second;
            ans+= (ff-(*pre).first)*min(cc*1ll,(*pre).second);
            pre=it;

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

    }
}


// Problem : B - Achieve the Goal
// Contest : AtCoder - AtCoder Beginner Contest 151
// URL : https://atcoder.jp/contests/abc151/tasks/abc151_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

//Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int,int>
#define mii map<pii,int>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

//Solution

void solve(){
    int n,k,m,a;
    cin>>n>>k>>m;
    int ans = n*m;
    for(int i=0;i<n-1;i++) cin>>a, ans -= a;
    ans = max(ans,0LL);
    cout<<(ans <= k ? ans : -1)<<endl;
}

signed main(){
    io;
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int tests;
    //cin>>tests;
    tests = 1;
    while(tests--){
        solve();
    }
    return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long

const long long MOD = 1000000007;

int main(){
  int a,b,c;
  cin >> a >> b >> c;
  int ans=21-a-b-c;
  cout << ans << endl;
}


#include<bits/stdc++.h>
#define g return
using namespace std;int M,h=1e9,n,k,l=-1,r=1e6,a,b,i=1;vector<vector<int>> v;struct d{int m,s,f;};d x(int o,int p=h){if(v[o].size()==1&&o)g{0,-1,0};int t=h,s=-h,c=0,f=0;for(int& e:v[o])if(p-e){auto [j,y,z]=x(e,o);t=min(t,y);s=max(s,y);f|=z,c+=j;}t=abs(t);if(o){if(t<=s&&f){if(s)g{c,s-1,1};g{c,0,0};}if(t-M)g{c,-t-1,0};g{c+1,M-1,1};}if(t<=s&&f)g{c,0,0};g{c+1,0,0};}main(){cin>>n>>k;v.resize(n);for(;i<n;i++){cin>>a>>b;a--;b--;v[a].push_back(b);v[b].push_back(a);}while(r-l>1){M=(r+l)/2;if(M)(x(0).m>k?l:r)=M;else(n>k?l:r)=0;}cout<<r;}

#include <bits/stdc++.h>

#define fi first
#define se second
#define DB double
#define U unsigned
#define P std::pair
#define LL long long
#define LD long double
#define pb emplace_back
#define MP std::make_pair
#define SZ(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 2e5 + 5;
int n,m,mid,ans;
std::vector<int> G[MAXN];
int f[MAXN],g[MAXN];

inline void dfs(int v,int fa=0){
    f[v] = 1e9;g[v] = -1e9;
    for(auto x:G[v]){
        if(x == fa) continue;
        dfs(x,v);
        f[v] = std::min(f[v],f[x]+1);
        g[v] = std::max(g[v],g[x]+1);
    }
    if(f[v] > mid) g[v] = std::max(g[v],0);
    if(f[v]+g[v] <= mid) g[v] = -1e9;
    if(g[v] == mid) ++ans,g[v] = -1e9,f[v] = 0;
}

inline bool chk(int x){
    mid = x;ans = 0;dfs(1);
    if(g[1] != -1e9) ++ans;
    return ans <= m;
}

int main(){
    scanf("%d%d",&n,&m);
    FOR(i,2,n){
        int u,v;scanf("%d%d",&u,&v);
        G[u].pb(v);G[v].pb(u);
    }
    int l = 0,r = n,ans = -1;
    while(l <= r){
        int mid = (l + r) >> 1;
        if(chk(mid)) ans = mid,r = mid-1;
        else l = mid+1;
    }
    printf("%d\n",ans);
    return 0;
}


/*************************************************************************
	> File Name: 1.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com
	> Blog: https://www.cnblogs.com/Knowledge-Pig/
	> Created Time: 2021/4/10 19:57:22
 ************************************************************************/

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define LL long long
#define pb push_back
#define fi first
#define se second
#define pr pair<int,int>
#define mk(a,b) make_pair(a,b)
using namespace std;
int read(){
	char x=getchar(); int u=0,fg=0;
	while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
	while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
	return fg?-u:u;
}
int n,a[2021],c[2021];
char s[2021];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	n=read();
	scanf("%s",s+1);
	For(i,0,n) a[i]=read();
	int ans=1e9;
	For(i,1,n) ans=min(ans,abs(a[i]-a[i-1]));
	printf("%d\n",ans);
	For(i,0,ans-1){
		For(j,0,n) printf("%d ",a[j]/ans+(i<a[j]%ans));
		printf("\n");
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return 0;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return 0;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;

ll n,k;

int main() {
    fio(); cin>>n>>k;
    while(k--){
        if(n%200==0) n/=200;
        else n=n*1000+200;
    }
    print(n);
}
//3min


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

int main(){
  long long int N,M;
  cin >> N >> M;
  vector<pair<long long int,long long int>> vec(M);
  for(int i=0;i<M;i++){
    long long int X,Y;
    cin >> X >> Y;
    vec[i] = make_pair(X,Y);
  }
  sort(vec.begin(),vec.end());
  map<long long int,vector<int>> Map;
  for(int i=0;i<M;i++) Map[vec[i].first].push_back(vec[i].second);
  map<long long int,bool> P;
  P[N] = true;
  for(auto x:Map){
    vector<int> ope = x.second;
    map<long long int,int> judge;
    for(int i=0;i<ope.size();i++){
      long long int Y = ope[i];
      if(P[Y-1]) judge[Y] = 1;
      if(P[Y+1]) judge[Y] = 1;
      if(P[Y] && judge[Y] == 0) judge[Y] = 2;
    }
    for(auto y:judge){
      if(y.second == 1) P[y.first] = true;
      if(y.second == 2) P[y.first] = false;
    }
  }
  int ans = 0;
  for(auto x:P) if(x.second) ans++;
  cout << ans << endl;
}

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

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> VP;
typedef vector<PL> VPL;
typedef vector<string> VS;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h, w, x, y;
    cin >> h >> w >> x >> y;
    x--;
    y--;
    VS vs(h);
    REP(i, h) cin >> vs[i];

    int cnt = 1;
    FOR(i, x + 1, h - 1) {
        if (vs[i][y] == '#') break;
        cnt++;
    }
    FORR(i, x - 1, 0) {
        if (vs[i][y] == '#') break;
        cnt++;
    }
    FOR(i, y + 1, w - 1) {
        if (vs[x][i] == '#') break;
        cnt++;
    }
    FORR(i, y - 1, 0) {
        if (vs[x][i] == '#') break;
        cnt++;
    }

    cout << cnt << ln;
    return 0;
}


#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;

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

// macros
#define int long long
#define ll long long
#define ld long double
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(x) ((int)((x).size()))

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char& s) {
  return to_string(string(1, s));
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "1" : "0");
}

string to_string(vector<bool> v) {
	bool first = true;
	string res = "{";
	for (int i = 0; i < static_cast<int>(v.size()); i++) {
		if (!first) {
			res += ", ";
		}
		first = false;
		res += to_string(v[i]);
	}
	res += "}";
	return res;
}

template <size_t N>
string to_string(bitset<N> v) {
	string res = "";
	for (size_t i = 0; i < N; i++) {
		res += static_cast<char>('0' + v[i]);
	}
	return res;
}

template <typename A>
string to_string(A v) {
	bool first = true;
	string res = "{";
	for (const auto &x : v) {
		if (!first) {
			res += ", ";
		}
		first = false;
		res += to_string(x);
	}
	res += "}";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
	return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

template <typename T1, typename T2>
inline auto mini(T1 a, T2 b) { return (a < b ? a : b); }
template<typename T, typename... Args>
inline auto mini(T a, Args... args) { return mini(a, mini(args...)); }
template <typename T1, typename T2>
inline auto maxi(T1 a, T2 b) { return (a > b ? a : b); }
template<typename T, typename... Args>
inline auto maxi(T a, Args... args) { return maxi(a, maxi(args...)); }
template<typename T>
T gcd(T a, T b) { if(a==0 or b==0) return a+b; return gcd(b, a%b) ; }
template<typename T>
T lcm(T a, T b) { if(a==0 or b==0) return 0; return a/gcd(a, b)*b; }

// random number generator
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use mt19937_64 for 64 bit

// constants
const long double eps = LDBL_EPSILON;
const int inf = 1e15;
const int modn = 1e9+7;
const int maxn = 2e5+3;

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Your code here
	int n;
	cin >> n;
	vector<array<int, 2>> a(n);
	for(int i = 0; i < n; i++) {
		cin >> a[i][0] >> a[i][1];
	}
	bool flag = 1;
	for(int i = 0; i < n and flag; i++) {
		for(int j = i + 1; j < n and flag; j++) {
			for(int k = j + 1; k < n and flag; k++) {
				int num1 = a[j][1] - a[i][1];
				int den1 = a[j][0] - a[i][0];
				int num2 = a[k][1] - a[j][1];
				int den2 = a[k][0] - a[j][0];
				if(num1 * den2 == num2 * den1) {
					flag = 0;
				}
			}
		}
	}
	cout << (flag == 0 ? "Yes\n" : "No\n");
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define INF (1 << 30)
#define MOD 1000000007
#define ld long double
#define tc ll tt; cin >> tt; while(tt--)
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
using namespace std;

int get(int s, int t) {

 // 0....9
 // (0.5).........(8.5)
  return t - s;

}

int main() {
	int N, m, t, last;
    cin >> N >> m >> t;
    int store = N;
  	last = 0;
    while (m--) {
      int a, b;
      cin >> a >> b;
      N -= get(last, a);
      if (N <= 0) break;
      N += get(a, b);
      if (N > store) N = store;
      last = b;
    }
    if (N > 0) N -= get(last, t);
    if (N > 0) cout << "Yes\n";
    else cout << "No\n";
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; ++i)
using namespace std;
using LLONG = long long;
LLONG K, N, M;
vector<LLONG> As, Bs;

bool CanDo(const LLONG x)
{
	vector<LLONG> Ls(K), Rs(K);
	REP(i, K)
	{
		LLONG Ai = As[i];
		Ls[i] = (M * Ai - x + N - 1) / N;
		Rs[i] = (M * Ai + x) / N;
	}
	LLONG sumB = accumulate(Ls.begin(), Ls.end(), 0LL);
	REP(i, K) Bs[i] = Ls[i];
	REP(i, K)
	{
		if (sumB >= M) break;
		LLONG maxDiff = min(M - sumB, Rs[i] - Ls[i]);
		sumB += maxDiff;
		Bs[i] += maxDiff;
	}
	return accumulate(Ls.begin(), Ls.end(), 0LL) <= M && M <= accumulate(Rs.begin(), Rs.end(), 0LL);
}

int main()
{
	cin >> K >> N >> M;
	As.assign(K, 0);
	Bs.assign(K, 0);
	REP(i, K) cin >> As[i];
	LLONG ng = 1;
	LLONG ok = N * M;
	while (ok > ng + 1)
	{
		LLONG m = (ok + ng) / 2;
		if (CanDo(m))
		{
			ok = m;
		}
		else
		{
			ng = m;
		}
	}
	CanDo(ok);
	for (const LLONG bi : Bs)
	{
		cout << bi << ' ';
	}
	cout << endl;
}

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    LL(k,n,m);
    vector<ll> a(k),b(k);
    in(a);
    ll sumv=0;
    rep(i,k){
        b[i]=a[i]*m/n;
        sumv+=b[i];
    }
    vector<pair<double,int>> vec(k);
    rep(i,k){
        vec[i].first=(double)abs((b[i]+1)*n-a[i]*m)/n*m;
        vec[i].second=i;
    }
    sort(all(vec));
    m-=sumv;
    rep(i,m){
        b[vec[i].second]++;
    }
    out(b);
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long int n,k;
    cin >> n >> k;
    for(int i=0; i<k; i++) {
        if(n%200==0) n/=200;
        else {
            n*=1000;
            n+=200;
        }
    }
    cout << n << endl;
}

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
//using Graph = vector<vector<pair<ll,ll>>>;
const int mod =1e+9+7;

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

const ll INF=1e18;

int main(){
    ll a,b; cin>>a>>b;
    ll c=a+b;
    if(c>=15&&b>=8)cout<<1<<endl;
    else if(c>=10&&b>=3)cout<<2<<endl;
    else if(c>=3)cout<<3<<endl;
    else cout<<4<<endl;
}


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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

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; }

vector<bool> used;
vector<int> dir; // left==0, right==1, other -1
vector<int> p;

bool connect(int i, int j){
    if(used[i]==false && used[j]==false) return true;
    else if(used[i]==true && used[j]==false){
        if(p[i]==-1 && dir[i]==1) return true;
        else return false;
    }
    else if(used[i]==false && used[j]==true){
        if(p[j]==-1 && dir[j]==0) return true;
        else return false;
    }
    else{
        if(p[i]==j && p[j]==i) return true;
        else return false;
    }
}

int dp[300][300];

int rec(int l, int r){
    if(dp[l][r] != -1) return dp[l][r];
    if(r-l<2) return dp[l][r] = 0;

    int len = (r-l)/2;
    bool flag = true;
    REP(i,len){
        if(!connect(l+i,l+i+len)){
            flag = false;
            break;
        }
    }
    int ret = 0;
    if(flag) ret = 1;

    for(int m=l+2; m<=r-2; m+=2){
        if(rec(l,m)==1 && rec(m,r)==1) ret = 1;
    }
    return dp[l][r] = ret;
}

void Main(){
    int N; cin >> N;
    VI A(N), B(N);
    REP(i,N){
        cin >> A[i] >> B[i];
        if(A[i]>0) A[i]--;
        if(B[i]>0) B[i]--;
    }
    REP(i,N){
        if(A[i]==2*N-1 || B[i]==0){
            cout << "No" << en;
            return;
        }
        if(A[i]!=-1 && B[i] !=-1 && B[i]<=A[i]){
            cout << "No" << en;
            return;
        }
    }
    VI cnt(2*N,0);
    REP(i,N){
        if(A[i]!=-1){
            cnt[A[i]]++;
            if(cnt[A[i]]>1){
                cout << "No" << en;
                return;
            }
        }
        if(B[i]!=-1){
            cnt[B[i]]++;
            if(cnt[B[i]]>1){
                cout << "No" << en;
                return;
            }
        }
    }

    used.assign(2*N,false); dir.assign(2*N,-1); p.assign(2*N,-1);
    REP(i,N){
        if(A[i]!=-1){
            used[A[i]] = true;
            dir[A[i]] = 1;
            p[A[i]] = B[i];
        }
        if(B[i]!=-1){
            used[B[i]] = true;
            dir[B[i]] = 0;
            p[B[i]] = A[i];
        }
    }
    REP(i,300)REP(j,300) dp[i][j] = -1;
    int ans = rec(0,2*N);
    cout << (ans==1 ? "Yes" : "No") << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[100005],vis[100005],f[100005];
bool check(int x,int y)
{
	if(vis[x]==2||vis[y]==1) return 0;
	if(vis[x]&&a[x]&&a[x]!=y) return 0;
	if(vis[y]&&a[y]&&a[y]!=x) return 0;
	if(vis[y]&&vis[x]&&!a[x]) return 0;
	return 1;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if((x!=-1&&vis[x])||(y!=-1&&vis[y]))
		{
			printf("No\n");
			return 0;
		}
		if(x!=-1&&y!=-1)
		{
			if(x>=y)
			{
				printf("No\n");
				return 0;
			}
			a[x]=y,a[y]=x;
		}
		if(x!=-1) vis[x]=1;
		if(y!=-1) vis[y]=2;
	}
	f[0]=1;
	for(int i=1;i<=2*n;i++)
	{
		for(int j=i-2;j>=0;j-=2)
			if(f[j])
			{
				int fl=1;
				for(int l=j+1,r=j+(i-j)/2+1;r<=i;l++,r++)
					if(!check(l,r))
					{
						fl=0;
						break;
					}
				if(fl)
				{
					f[i]=1;
					break;
				}
			}
	}
	if(f[2*n]) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<bits/stdc++.h>
#define Pair pair<ll,int>
#define ar array
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std ;
const ll LLMAX = 9223372036854775807;
const int INF = 1e9;
int n, m, k;
vector<int> adj[100005];
int dist[18][100005];
int arr[18];

void bfs(int idx){
    dist[idx][arr[idx]] = 0;
    queue<int> q;
    q.push(arr[idx]);
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int k : adj[cur]){
            if(dist[idx][k]==INF){
                dist[idx][k] = dist[idx][cur]+1;
                q.push(k);
            }
        }
    }
}

int dp[18][1<<18];

int solve(int last, int s){
    if(s==(1<<k)-1)
        return 0;

    if(dp[last][s]!=-1)
        return dp[last][s];

    int ans = INF;
    for(int i=0; i<k; i++){
        if((1<<i)&s)
            continue;
        ans = min(ans, solve(i, (1<<i)|s) + dist[last][arr[i]]);
    }

    return dp[last][s] = ans;
}

int main(){
    memset(dp, -1, sizeof(dp));
    cin>>n>>m;
    for(int i=0; i<m; i++){
        int a, b;
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

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

    for(int i=0; i<k; i++){
        for(int j=0; j<=n; j++){
            dist[i][j] = INF;
        }
        bfs(i);
    }

    for(int i=0; i<k; i++){
        if(dist[i][arr[0]] == INF){
            cout<<-1<<endl;
            return 0;
        }
    }

    /*
    for(int i=0; i<k; i++){
        cout<<"src "<<i<<endl;
        for(int j=1; j<=n; j++)
            cout<<dist[i][j]<<" ";
        cout<<endl;
    }
    */

    int ans = INF;
    for(int i=0; i<k; i++){
        ans = min(ans, 1+solve(i, 1<<i));
    }

    cout<<ans<<endl;
}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on

char win(char a, char b) {
    if ((a == 'R' && b == 'P') || (a == 'S' && b == 'R') || (a == 'P' && b == 'S')) {
      return b;
    }
    return a;
}
int main() {
    ll n0, k;
    cin >> n0 >> k;
    vector<char> s(n0);
    for (ll i = 0; i < n0; i++) {
        cin >> s[i];
    }
    for (ll depth = k; depth > 0; depth--) {
        ll n = s.size();
        vector<char> nxt;
        nxt.push_back(win(s[0], s[1]));
        for(ll i = 2; (i % n) != 0; i += 2) {
            nxt.push_back(win(s[i % n], s[(i + 1) % n]));
        }
        s = nxt;
    }
    print(s[0]);
}


#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <numeric>
#include <unordered_set>
#include <iomanip>
#include <algorithm>
using namespace std;

int main(){
    string s;
    cin >> s;
    string t;
    bool reverse_judge=false;
    for(int i=0;i<s.size();i++){
        if(s[i]=='R'){
            reverse_judge = !reverse_judge;
        }
        else{
            if(reverse_judge){
                if(t[0]==s[i]) t.erase(t.begin());
                else t.insert(t.begin(),s[i]);
                //else t = s[i] + t;
            }
            else if( t[t.size()-1]==s[i] ) t.pop_back();
            else t += s[i];

        }
    }

    if(reverse_judge){
        reverse(t.begin(),t.end());
    }
    cout << t << endl;
}

// FIRST THINK THEN CODE.

#include<iostream>
#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<pll, null_type,less<>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define cld complex<ld>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vld vector<ld>
#define vvld vector<vector<ld>>
#define vpll vector<pll>
#define vcld vector<cld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define eb emplace_back
#define PI acos(-1)
#define endl "\n"
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define mset(a,val) memset(a,val,sizeof(a));
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
#define FOR(i,a,b)  for(ll i=a;i<=b;i++)
#define FORR(i,a,b)  for(ll i=a;i>=b;i--)
const ll M = 1000000007;
const ll MM = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T, typename F>
void chmax( T &a, F b) {
	if (b > a)a = b;
}

template<typename T, typename F>
void chmin( T &a, F b) {
	if (b < a)a = b;
}

const ll N = 2e5 + 5;

void OJ() {

#ifndef ONLINE_JUDGE

	freopen("input1.txt", "r", stdin);
	freopen("output1.txt", "w", stdout);

#endif

}

int main() {

	IOS;

	OJ();

	string s; cin >> s;

	ll isrev = 0;

	deque<char>ans;
	for (auto d : s) {
		if (d == 'R') {
			isrev = 1 - isrev;
		} else {
			if (!isrev) {
				ans.push_back(d);
			} else {
				ans.push_front(d);
			}
		}
	}

	if (isrev)rev(ans);

	stack<char>lol;

	for (auto d : ans) {
		if (lol.empty() || lol.top() != d) {
			lol.push(d);
			//		cout << d;
		} else {
			lol.pop();
		}
	}
	ans.clear();

	while (sz(lol)) {
		ans.pb(lol.top());
		lol.pop();
	}

	rev(ans);

	for (auto d : ans)cout << d;

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
const int N=2e5+2;
queue<int>q[N];
long long ans=0;
int n,a[N],b[N],c[N],f[N];
void add(int x,int y)
{
	while(x)
	{
		f[x]+=y;
		x^=x&-x;
	}
}
int qry(int x)
{
	int res=0;
	while(x<=n)
	{
		res+=f[x];
		x+=x&-x;
	}
	return res;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&b[i]);
		c[i]=b[i]+i;
	}
	sort(c+1,c+n+1);
	for(int i=1;i<=n;++i)
		q[lower_bound(c+1,c+n+1,b[i]+i)-c].push(i);
	for(int i=1;i<=n;++i)
	{
		int x=lower_bound(c+1,c+n+1,a[i]+i)-c;
		if(q[x].empty()||a[i]+i!=c[x])
		{
			puts("-1");
			return 0;
		}
		a[i]=q[x].front();
		q[x].pop();
		ans+=qry(a[i]);
		add(a[i],1);
	}
	printf("%lld",ans);
}

#include <bits/stdc++.h>

using namespace std;

#define int long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define mii map<int,int>
#define pii pair<int,int>
#define ff first
#define ss second
#define ce cout << endl ;
#define pb push_back
#define mkp make_pair
#define py cout << "YES" << endl ;
#define pn cout << "NO" << endl ;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) ;

bool cmp(pii& a , pii& b){
    int asum = a.ff + a.ss , bsum = b.ff + b.ss ;
    return asum + a.ff > bsum + b.ff  ;
}

const int  l = 2e7 + 1 , mod = 1e9 + 7 ;

vi prime(l , 0) , phi(l + 1) , store(l , 0) ;

vvi factor(l) ;

void f(){
    prime[1] = 1 , prime[0] = 1 ;
    for(int i = 2 ; i < l ; i++){
        if(!prime[i]){
            for(int j = i*i ; j < l ; j+=i) prime[j] = 1 ;
        }
    }
    for(int i = 2 ; i < l ; i++){
        if((not prime[i]) and (not prime[i-2])) store[i] = 1 ;
        store[i]+=store[i-1] ;
    }
}

int factorial(int n){
    int ans = 1 ;
    for(int i = 2 ;i<=n ;i++) ans*=i ;
    return ans ;
}

int lcm(int a , int b){
    return (a*b) / __gcd(a , b) ;
}

void print(vi &a){
    for(auto it : a) cout << it << " " ;
}

int modadd(int a , int b){
    return (a%mod + b%mod) % mod ;
}

int modsub(int a , int b){
    return ( ( ( a % mod ) - ( b % mod ) ) + mod ) % mod ;
}

int modmul(int a , int b){
    return ( (a % mod) * (b % mod) ) % mod ;
}

int fast_mod_expo(int base , int power , int ans){
    if(power > 0){
        if(power & 1) ans = modmul(ans , base) ;
        power >>= 1 , base = modmul(base , base) ;
        return fast_mod_expo(base , power , ans) ;
    }
    else return ans ;
}

void store_divisors(){
    for(int i = l-1 ; i > 0 ; i--) {
        for(int j = i ; j < l ; j+=i) factor[j].pb(i) ;
    }
}

int EulerTotientfuunction(int n){
    int ans = n ;
    for(int i = 2 ; i < n ; i++){
        if(n%i==0){
            while(n%i==0)n/=i;
            ans-=ans/i ;
        }
    }
    if(n > 1) ans-=ans/n ;
    return ans ;
}

int binarysearch(int* a , int l , int r , int key){
    if(l <= r){
        int mid = (l + r)>>1 ;
        if(a[mid] < key) l = mid + 1 ;
        if(a[mid] > key) r = mid - 1 ;
        if(a[mid] == key) return key ;
        return binarysearch(a , l , r , key) ;
    }
    else return 0 ;
}

class graph{
    int v ;
    list<int> *l ;
    public:
        void create_list(int v){
            this->v = v ;
            l = new list<int>[v+1] ;
        }
        void addedge(int u , int v){
            l[u].pb(v) ;
            l[v].pb(u) ;
        }
        void print_list(){
            for(int i = 1 ; i <= v ; i++){
                cout << "Vertex:" << i << "->" ;
                for(auto it : l[i]) cout << it << "->" ;
                ce
            }
        }
};

void solve(){
    int n , aoki = 0 , takashi = 0 , ans = 0 ;
    cin >> n ;
    vector<pii> a ;
    for(int i = 0 , l , r ; i < n ; i ++){
        cin >> l >> r ;
        a.pb(mkp(l , r)) ;
        aoki+=l ;
    }
    sort(a.begin() , a.end() , cmp) ;
    for(int i = 0 ; i<n and aoki>=takashi ; i ++ , ans++){
        takashi += a[i].ff + a[i].ss ;
        aoki -= a[i].ff ;
    }
    cout << ans ;
}

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


#include<iostream>
#include<string>
int main() {
  char S, T;
  std::cin >> S;
  std::cin >> T;
  if (S == 'Y') T = std::toupper(T);
  std::cout << T << std::endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

map<int64,int64> mp;
int64 X,Y;
int64 dfs(int64 x) {
    if(mp.count(x)) return mp[x];
    if(x   == 1) mp[x]=abs(X-x);
    else if(x%2 == 1) {
        mp[x] = min({abs(X-x),dfs((x+1)/2)+2,dfs((x-1)/2)+2});
    }
    else if(x%2 == 0) {
        mp[x] = min({abs(X-x),dfs(x/2)+1});
    }
    return mp[x];
}

/**
 * @url
 * @est
 */
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    cin >> X >> Y;
    mp[X] = 0;
    int64 ans = dfs(Y);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pii pair<long long,long long>
#define mp make_pair
#define pb push_back
#define WTF puts("wtf")
string s[3];
ll to[26],p[10];
void solve()
{
	cin>>s[0]>>s[1]>>s[2];
	ll cnt=0;
	for(ll i=0;i<3;i++)
	{
		for(ll j=0;j<s[i].size();j++)
		{
			char now=s[i][j];
			if(!to[now-'a']) cnt++,to[now-'a']=cnt;
		}
	}
	if(cnt>10)
	{
		puts("UNSOLVABLE");
		return;
	}
	else
	{
		for(ll i=1;i<=10;i++) p[i]=i;
		ll n[3];
		do
		{
			n[0]=n[1]=n[2]=0;
			for(ll i=0;i<3;i++) for(ll j=0;j<s[i].size();j++)
			{
				n[i]*=10;
				n[i]+=p[to[s[i][j]-'a']]-1;
			}
			if(p[to[s[0][0]-'a']]>1&&p[to[s[1][0]-'a']]>1&&p[to[s[2][0]-'a']]>1)
			{
				if(n[0]+n[1]==n[2])
				{
					cout<<n[0]<<"\n"<<n[1]<<"\n"<<n[2];
					return;
				}
			}
		}while(next_permutation(p+1,p+11));
		puts("UNSOLVABLE");
		return;
	}
}
signed main()
{
	ll _=1;
	//cin>>_;
	while(_--) solve();
	return 0;
}

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

using namespace std;

typedef long long LL;

const int N = 200010;

vector<char> v;
int find(char c)
{
    return lower_bound(v.begin(), v.end(), c) - v.begin();
}

vector<int> g[3];
int num[10];
bool dfs(int u, int state)
{
    if(u == v.size())
    {
        LL x[3] = {0};
        for(int i = 0; i < 3; i ++)
            for(int j : g[i])
                x[i] = x[i] * 10 + num[j];

        if(x[0] + x[1] == x[2])
        {
            printf("%lld\n%lld\n%lld\n", x[0], x[1], x[2]);
            return true;
        }
        return false;
    }

    for(int i = 0; i < 10; i ++)
        if(!(state >> i & 1))
        {
            if(!i and (g[0][0] == u or g[1][0] == u or g[2][0] == u))  continue;
            num[u] = i;
            if(dfs(u + 1, state | (1 << i)))  return true;
        }
    return false;
}

int main()
{
    string s[3];
    for(int i = 0; i < 3; i ++)
    {
        cin >> s[i];
        for(char c : s[i])  v.push_back(c);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    if(v.size() > 10)  return 0 * puts("UNSOLVABLE");

    for(int i = 0; i < 3; i ++)
    {
        for(char c : s[i])  g[i].push_back(find(c));
        //for(int j : g[i])  printf("%d ", j);  puts("");
    }

    if(!dfs(0, 0))  puts("UNSOLVABLE");
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <iomanip>
#include <cassert>

using namespace std;
typedef long long ll;

int main()
{
   ll A, B;
   cin >> A >> B;
   vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
   vector<ll> dp(1 << 20, 0);
   dp[0] = 1;
   for (int i = 0; i < B - A + 1; i++)
   {
      ll cur = A + i;
      int modmask = 0;
      for (int j = 0; j < 20; j++)
      {
         if (cur % primes[j] == 0)
         {
            modmask |= (1 << j);
         }
      }
      for (int mask = (1 << 20) - 1; mask >= 0; mask--)
      {
         if ((mask & modmask) == 0)
         {
            dp[(mask | modmask)] += dp[mask];
         }
      }
   }
   ll ans = 0;
   for (int i = 0; i < (1 << 20); i++)
   {
      ans += dp[i];
   }
   cout << ans << endl;
   return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
ll b,c,ans,t1,t2,t3,t4;
int main(){
	cin>>b>>c;
	if(c==1){
		if(b==0){
			cout<<"1\n";
			return 0;
		}else{
			cout<<"2\n";
			return 0;
		}
	}
	t1=b-c/2;
	t2=b+c/2-1;
	t3=-b-(c-1)/2;
	t4=-b+(c-1)/2;
	if(t2<t3||t4<t1){
		ans=t2-t1+t4-t3+2;
		cout<<ans<<'\n';
		return 0;
	}
	ans=max(t2,t4)-min(t1,t3)+1;
	cout<<ans<<'\n';
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>

const ll mod = 1000000007;
const int INF = 1001001001;

struct mint {
    ll x;
    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; }

int main(){
    int h, w;
    cin >> h >> w;
    string s[h];
    rep(i, h) cin >> s[i];
    // 0: そこ, 1: 縦, 2: 横, 3: 斜め
    vector<vector<vector<mint> > > dp(h, vector<vector<mint> >(w, vector<mint>(4)));
    dp[0][0][0] = 1;
    dp[0][0][1] = 1;
    dp[0][0][2] = 1;
    dp[0][0][3] = 1;
    rep(i, h-1) {
        if(s[i+1][0]  == '#') continue;
        dp[i+1][0][0] = dp[i][0][1];
        dp[i+1][0][1] = dp[i][0][1] + dp[i+1][0][0];
        dp[i+1][0][2] = dp[i+1][0][0];
        dp[i+1][0][3] = dp[i+1][0][0];
    }
    rep(j, w-1) {
        if(s[0][j+1]  == '#') continue;
        dp[0][j+1][0] = dp[0][j][2];
        dp[0][j+1][1] = dp[0][j+1][0];
        dp[0][j+1][2] = dp[0][j][2] + dp[0][j+1][0];
        dp[0][j+1][3] = dp[0][j+1][0];
    }
    rep(i, h-1){
        rep(j, w-1){
            if(s[i+1][j+1] == '#') continue;
        dp[i+1][j+1][0] = dp[i][j+1][1] + dp[i+1][j][2] + dp[i][j][3];
        dp[i+1][j+1][1] = dp[i][j+1][1] + dp[i+1][j+1][0];
        dp[i+1][j+1][2] = dp[i+1][j][2] + dp[i+1][j+1][0];
        dp[i+1][j+1][3] = dp[i][j][3] + dp[i+1][j+1][0];
        }
    }
    // rep(i, h){
    //     rep(j, w){
    //         cout << dp[i][j][0] << " ";
    //     }cout << endl;
    // }

    cout << dp[h-1][w-1][0] << endl;
}