#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_N = 500;

int N, M, Q;
int L, R;
int p, q;

int cnt[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];

int main() {
    cin >> N >> M >> Q;
    REP(i, N) {
        REP(j, N) {
            cnt[i][j] = 0;
            dp[i][j] = 0;
        }
    }
    REP(i, M) {
        cin >> L >> R; L--; R--;
        cnt[L][R]++;
    }

    REP(i, N) dp[i][i] = cnt[i][i];

    REP2(k, 1, N) {
        REP(i, N-k) {
            int j = i + k;
            dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + cnt[i][j];
        }
    }

    REP(i, Q) {
        cin >> p >> q; p--; q--;
        PRINT(dp[p][q]);
    }

    return 0;
}

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

int MAX_N=262144*2;
vector<int64_t> seg(MAX_N*2-1);
int64_t qu(int a,int b,int k,int l, int r){
  if(r<=a||b<=l){
    return 0;
  }
  else if(a<=l&&r<=b){
    return seg[k];
  }
  else{
    int64_t X=qu(a,b,k*2+1,l,(l+r)/2);
    int64_t Y=qu(a,b,k*2+2,(l+r)/2,r);
    return X+Y;
  }
}
void pu(int64_t a,int b){
  int D=MAX_N-1+b;
  while(D>0){
    seg[D]+=a;
    D--;
    D/=2;
  }
  seg[0]+=a;
}

int main() {
  int N,Q;
  cin>>N>>Q;
  vector<int> p(N);
  for(int i=0;i<N;i++){
    cin>>p[i];
    p[i]--;
  }
  vector<tuple<int,int,int>> q(Q);
  for(int i=0;i<Q;i++){
    int a,b;
    cin>>a>>b;
    q[i]=make_tuple(b,a,i);
  }
  sort(q.begin(),q.end());
  vector<int> la(N,-1);
  vector<int> an(Q);
  int W=0;
  for(int i=0;i<Q;i++){
    int a,b,c;
    tie(a,b,c)=q[i];
    while(W<a){
      if(la[p[W]]!=-1){
        pu(-1,la[p[W]]);
      }
      la[p[W]]=W;
      pu(1,W);
      W++;
    }
    an[c]=qu(b-1,a,0,0,MAX_N);
  }
  for(int i=0;i<Q;i++){
    cout<<an[i]<<endl;
  }
}


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

#define int long long

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double
#define ll long long

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define data asd09123jdf02i3h

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 1e6+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

int n,m,b[maxn],l[maxn],r[maxn],ans,s[maxn],t[maxn];

int check(int x) {
    for(int i=1;i<=n;i++) {
        int c=x/m,d=x%m;
        int res=l[i]*min(b[i],d)+r[i]*max(0ll,d-b[i]),w=l[i]*b[i]+r[i]*(m-b[i]);// write(t[c+1]);
        if(c) {
            if(w<s[c]) res+=t[c];
            else res+=t[c+1]-w;
        }
        res-=ans;// printf("%I64d %I64d %I64d\n",x,i,res);
        if(res>=0) return 1;
    }
    return 0;
}

signed main() {
    read(n),read(m);
    for(int i=1;i<=n;i++) read(b[i]),read(l[i]),read(r[i]),ans+=b[i]*l[i],s[i]=b[i]*l[i]+(m-b[i])*r[i];
    sort(s+1,s+n+1,greater<int > ());
    for(int i=1;i<=n;i++) t[i]=s[i]+t[i-1];
    int l=0,r=n*m,ans=n*m;
    while(l<=r) {
        int mid=(l+r)>>1;
        if(check(mid)) r=mid-1,ans=mid;
        else l=mid+1;
    }
    write(ans);
    return 0;
}

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

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
#pragma GCC target("fpmath=387")

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 122) return char(x-32);
    if( 65 <= int(x) && int(x) <= 90) return x;
    return -1;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 122) return x;
    if( 65 <= int(x) && int(x) <= 90) return char(x+32);
    return -1;
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57) return int(x-'0');
    if(97 <= int(x) && int(x) <= 122) return int(x-96);
    if(65 <= int(x) && int(x) <= 90) return int(x-64);
    return -1;
}
bool isect (int l1, int r1, int l2, int r2){
    pii p1 = {l1,r1};
    pii p2 = {l2,r2};
    if(p1>p2)swap(p1,p2);
    return (p2.fi <= p1.se);
}
ll quickpow (ll num1, ll num2, ll MOD){
    if(num2==0)return 1%MOD;
    else if(num2==1)return num1%MOD;
    else{
        ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
int n,k;
double sum[200010];
double getsum(int l, int r){
    return sum[r] - sum[l-1];
}
int main(){

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

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n,k; cin >> n >> k;
    for(int i = 1; i <= n; ++i){
        int p; cin >> p;
        int res = (p * (p + 1)) / 2;
        double RES = (res * 1.0) / (p * 1.0);
        sum[i] = sum[i-1] + RES;
    }

    double ans = 0;
    for(int i = k; i <= n; ++i){
        int l = i-k+1;
        int r = i;
        ans = max(ans,getsum(l,r));
    }
    cout << fixed << setprecision(10) << ans << newl;

    return 0;
}


#include <iostream>
using namespace std;
int main() {
    string s;
    bool pri = 0;
    cin >> s;
    for (int i = 0; i < s.length(); ++i) {
        if(s[i] == 'A'){
            if(i+1 != s.length() && s[i+1] == 'C'){
                cout << "Yes";
                pri = 1;
                break;
            }
        }
    }
    if(!pri){
        cout <<"No";
    }
    return 0;
}

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main(){
    string S;
    cin>>S;
    if(S.find("AC")!=string::npos ){cout<<"Yes"<<endl;}
    else{cout<<"No"<<endl;}
    return 0;
}


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

int main(){
  vector<int> outp;
  int icn;
  int icd;
  int g;
  vector<int> ice;
  for(g = 0;;g++){
    vector<int> ice(10,0);
    cin >> icn;
    if(icn == 0){
      break;
    }else{
    for(int a = 0;a< icn;a++){
      cin >> icd;
      ice[icd] += 1;
    }
    outp.insert(outp.end(),ice.begin(),ice.end());
  }
  }
  for(int n = 0;n<g * 10;n++){
    if(outp[n] == 0){
      cout << "-";
    }else{
      for(int c = 0;c<outp[n];c++){
        cout << "*" ;
      }
    }
    cout << endl;
  }
  return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<map>

using namespace std;

int main(void){
	int a,n;
	while(1){
		map<int,string> str;
		cin >> n;
		if(n==0)break;
		for(a=0;a<n;a++){
			int b;
			cin >> b;
			str[b].push_back('*');
		}
		for(a=0;a<=9;a++){
			if(str.size()>0)cout << str[a] << endl;
			else cout << "-" << endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector < vector<ll> > Mat;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

ll N, K, ans = 1e18;

int Solve(ll num, ll turn){
  if (turn == N){
    ans = min(ans, num);
    return 0;
  }

  Solve(num * 2, turn + 1);
  Solve(num + K, turn + 1);
  return 0;
}

int main(){
  cin >> N >> K;
  Solve(1, 0);
  cout << ans << endl;

  return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int N, M;
	cin >> N >> M;
	vector<int> L(M), R(M);
	for (int i = 0; i < M; i++) {
		cin >> L[i] >> R[i];
	}

	int lmax = *max_element(L.begin(), L.end());
	int rmin = *min_element(R.begin(), R.end());
	int d = rmin - lmax + 1;
	if (d < 0)
		d = 0;
	cout << d << endl;

	return 0;
}


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

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPr(i, n) for(int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for(int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for(int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
    cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
    int x12345;                                                                \
    cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const long long MOD = 1e9 + 7;
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

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

///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|

string S;
ll N,Q;

ll solve(ll k){
    ll ret = 0;
    ll d = 0;
    ll m = 0;
    ll dm = 0;
    REP(i,k){
        if (S[i] == 'D') d++;
        if (S[i] == 'M') {
            m++;
            dm += d;
        }
        if (S[i] == 'C'){
            ret += dm;
        }
    }

    for (size_t i = k; i < N; i++) {
        char prev = S[i-k];
        char next = S[i];
        if (prev == 'D'){
            d--;
            dm -= m;
        }

        if (prev == 'M'){
            m--;
        }

        if (next == 'D') d++;
        if (next == 'M'){
            m++;
            dm += d;
        }
        if (next == 'C'){
            ret += dm;
        }
    }

    return ret;
}

int main() {
    cin >> N >> S >> Q;
    while(Q--){
        ll k; cin >> k;
        cout << solve(k) << "\n";
    }
}

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define sz size()
#define mp make_pair
#define pb push_back
#define rep(p, a, b) for (ll p = a; p < b; p++)
#define F first
#define S second
using namespace std;

typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<ld> vld;
typedef pair<ll, ll> pll;

int main()
{
    ll t, i, j, k, ans, n, m, sum = 0, x;

    //cin>>t;
    t = 1;
    while (t--)
    {
        cin >> n >> m;
        string s;
        vll wa(n + 1), ac(n + 1);

        ll c1 = 0, c2 = 0;
        rep(p, 0, m)
        {
            cin >> x >> s;
            if (s == "WA")
                wa[x]++;
            else
            {
                if (ac[x] == 0)
                {
                    c1++;
                    c2 += wa[x];
                    ac[x] = 1;
                }
            }
        }

        cout << c1 << " " << c2;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <stdio.h>

int main(void){
  int x,y,z;
  std::cin >> x >> y >> z;
  std::cout << z << " " << x << " "  << y;
  return 0;
}

/*
このコード、と～おれ!
Be accepted!
∧＿∧
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
					*/

#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>

#pragma GCC optimize("Ofast")
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define rep2(i, n) for(int i = 2; i < (n); ++i)
#define repr(i, n) for(int i = n; i >= 0; --i)
#define reprm(i, n) for(int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printin(a) printf(a ? "possible\n" : "inposible\n")
#define printdb(a) printf("%.50lf\n", a)//少数出力
#define printdbd(a) printf("%.16lf\n", a)//少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str())//string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2)//等差数列の和、初項,末項,項数
#define pb push_back
#define priq priority_queue
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg)/360.0)*2.0*PI)
#define rad_to_deg(rad) (((rad)/2.0/PI)*360.0)
#define Please return
#define AC 0
#define ushi int
#define tapu main
#define nichia (
#define kunn void
#define wara )

using ll = long long;

const int INF = 1073741823;
const int MINF = -1073741823;
const ll LINF = ll(4661686018427387903);
const ll MOD = 1000000007;
const double PI = acos(-1);

using namespace std;

void scans(string& str) {
	char c;
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);//最初の改行対策
	while (c != '\n' && c != -1) {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

/*-----------------------------------------ここからコード-----------------------------------------*/

int main(){

	string s;
	scans(s);
	prints(s.substr(0, s.length() - 8));
	Please AC;
}


#include<stdio.h>
int main()
{
	int a,b,c,n;
	scanf("%d %d %d",&n,&a,&b);
	if(n==2){
		for (int i=1; i < a ;i++) {
			if(a%i+b%i==0){
				printf("%d\n",i);
			}
		}
	}else if(n==3){
		scanf("%d",&c);
		for (int i=1; i < a ;i++) {
			if(a%i+b%i+c%i==0){
				printf("%d\n",i);
			}
		}
	}
	return 0;
}

#include "bits/stdc++.h"
#define rep(i,a,n) for(int i = a;i < n;i++)
typedef unsigned long long ull;
typedef long long ll;
using namespace std;

int main(){
    int n,memo;
    priority_queue<int,vector<int>,greater<int> > que;
    cin >> n;
    int a[n];

    rep(i,0,n){
        cin >> a[i];
    }

    sort(a,a+n);

    rep(i,1,sqrt(a[n-1])+1){
        rep(j,0,n){
            if(a[j] % i)break;
            else if(j == n-1)que.push(i);
        }

        if(a[n-1]/i == i)continue;

        rep(j,0,n){
            if(a[j] % (a[n-1]/i))break;
            else if(j == n-1)que.push(a[n-1]/i);
        }
    }

    while(!que.empty()){
        if(que.top() == memo){
            que.pop();
            continue;
        }
        memo = que.top(); que.pop();

        cout << memo << endl;
    }
}


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

int main()
{
    int n;
    cin >> n;
    map<int, int> mp;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        mp[a]++;
    }
    int t = (1 << 30);
    int ans = 0;
    for(auto itr = mp.rbegin(); itr != mp.rend(); itr++){
        int a = itr->first, c = itr->second;
        while(t > a * 2) t /= 2;
        if(t == a * 2) ans += c / 2;
        else{
            ans += min(mp[a], mp[t - a]);
            mp[t - a] -= min(mp[a], mp[t - a]);
        }
    }
    cout << ans << endl;
}

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

int main(){
    ll N = pow(10,9)+7;
    vector<ll> ans(61,0);
    ll n;
    cin >> n;
    for(int i=0; i<n; i++){
        ll a;
        cin >> a;
        int count=0;
        while(a!=0){
            ans.at(count) += a%2;
            a /= 2;
            count++;
        }
    }
    ll out=0;
    ll keta=1;
    for(int i=0; i<61; i++){
        //cout << "ou" << out << endl;
        //cout << "ke" << keta << endl;
        ll t = (n-ans.at(i))*ans.at(i)%N;
        out = (out+keta*t)%N;
        keta = (keta*2)%N;

    }
    cout << out << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define mod 1000000007LL
#define eps 1e-13
// #define PI 3.141592653589793238L
#define INF 1000000011
#define INFLL 1000000000000000011LL
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define fo(i,a,n) for(i = (a); i < (n); i++)
#define gtl(x) getline(cin, (x))
#define flsh fflush(stdout)
#define sws ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define gcd __gcd
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)((a).size())
#define io_file freopen("D:/Coding Problems/Contest/input_file.in", "r", stdin); freopen("D:/Coding Problems/Contest/output_file.out", "w", stdout)

ll modx(ll Base, ll exponent)
{
	ll ans = 1;
	if(Base == 1)
		return Base;
	while(exponent)
	{
		if(exponent & 1)
			ans = (ans * Base)%mod;
		Base = (Base * Base)%mod;
		exponent = exponent >> 1;
	}
	return ans;
}

ll inmodx(ll num)
{
	return (modx(num, mod-2LL));
}

bool cmp(pair < pii, int > a, pair < pii, int > b)//true for a before b
{
	if(a.F.F/300 < b.F.F/300)
		return 1;
	if(a.F.F/300 > b.F.F/300)
		return 0;
	if(a.F.S < b.F.S)
		return 1;
	if(a.F.S > b.F.S)
		return 0;
	if(a.F <= b.F)
		return 1;
	return 0;
}

const int N = (6e2) + 9;
const int M = (36e4) + 9;
const int LOGN = ((int)log2(N)) + 3;
const int LOGM = ((int)log2(M)) + 3;
const int BUCK = 2*550;
const int SQRT = BUCK+9;

vector < pii > arr[2], ans[4];
int node[N][N];
pii val[M];
vi v[2][M];
int mark[2][M];
bool vis[2][M];
queue < int > q;
int n;

inline void add_edge(int bit, int a, int b, int x, int y)
{
	if(x < 0 || x >= n+n || y < 0 || y >= n+n)
		return;
	v[bit][node[a][b]].pb(node[x][y]);
	return;
}

void bfs(int cur, int bit)
{
	if(vis[bit][cur])
		return;
	while(!q.empty())
		q.pop();
	int z;
	q.push(cur);
	vis[bit][cur] = 1;
	mark[bit][cur] = 0;
	while(!q.empty())
	{
		z = q.front();
		q.pop();
		for(auto x : v[bit][z])
			if(!vis[bit][x])
			{
				vis[bit][x] = 1;
				mark[bit][x] = 1 - mark[bit][z];
				q.push(x);
			}
	}
	return;
}

void solve()
{
	int d1, d2, i, j, k, d, cnt = 0;
	cin >> n >> d1 >> d2;
	fo(i,0,n+n)
		fo(j,0,n+n)
		{
			int d = i*i + j*j;
			if(d == d1)
				arr[0].pb({i,j});
			if(d == d2)
				arr[1].pb({i,j});
		}
	fo(i,0,n+n)
		fo(j,0,n+n)
		{
			node[i][j] = cnt;
			val[cnt] = {i,j};
			cnt++;
		}
	fo(i,0,n+n)
		fo(j,0,n+n)
			fo(k,0,2)
				for(auto x : arr[k])
				{
					add_edge(k,i,j,i+x.F,j+x.S);
					add_edge(k,i,j,i+x.F,j-x.S);
					add_edge(k,i,j,i-x.F,j+x.S);
					add_edge(k,i,j,i-x.F,j-x.S);
				}
	fo(i,0,cnt)
	{
		bfs(i,0);
		bfs(i,1);
	}
	fo(i,0,cnt)
		ans[mark[0][i]*2 + mark[1][i]].pb(val[i]);
	fo(j,0,4)
		if(sz(ans[j]) >= n*n)
		{
			fo(i,0,n*n)
				cout << ans[j][i].F << " " << ans[j][i].S << '\n';
			return;
		}
	return;
}

int main()
{
	sws;
	clock_t clk;
	clk = clock();
	// io_file;
	// srand (time(NULL));

	//Code here
	int t = 1, cs;
	cout << fixed << setprecision(9);
	// cin >> t;
	fo(cs,1,t+1)
	{
		// cout << "Case #" << cs << ": ";
		solve();
	}
	// Code ends here

	clk = clock() - clk;
	cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+9;
int a[maxn];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
		cin>>a[i];
    while(k--)
    {
        int b[maxn];
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            int l = max(1,i-a[i]);
            int r = min(n,i+a[i]);
            b[l]++;
            b[r+1]--;
        }
        int flag = 1;
        for(int i=1;i<=n;i++)
        {
            a[i] = a[i-1] + b[i];
            if(a[i]!=n)
				flag = 0;
        }
        if(flag)
			break;
    }
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}

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

int n, k;

double kitaichi(vector<int> p, int start, int k){
  double ans;
  for (int i = 0; i < k; i++){
    double sum = 0;
    for (int j = 1; j <= p[start+i]; j++){
      sum += j;
    }
    ans += sum/p[start+i];
  }
  return ans;
}

int main() {
  cin >> n >> k;
  vector<int> p(n);
  for (int i = 0; i < n; i++){
    cin >> p.at(i);
  }
  int sum = 0;
  int start;
  int s = 0;
  for (int i = 0; i  < 1; i++){
    for (int j = 0; j < k; j++){
      s += p.at(i+j);
    }
    sum = s;
    start = i;
  }
  for (int i = 1; i + k <= n; i++){
    s += p.at(i+k-1);
    s -= p.at(i-1);
    if (sum < s){
      sum = s;
      start = i;
    }
  }
  cout << fixed << setprecision(15) << kitaichi(p, start, k) << endl;
}


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

using namespace std;

int main(int argc, char const* argv[])
{
	std::string s;
	std::cin >> s;
	std::reverse(s.begin(),s.end());
	std::cout << s << std::endl;
	return 0;
}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int i,b;
char srt[100];
gets(srt);
b=strlen(srt);
for(i=b-1;i>=0;i--)
{
    printf("%c",srt[i]);
}
printf("\n");
}


#include<iostream>

using namespace std;

int main(void) {
	int N;
	int a[200];
	int count = 0;
	cin >> N;

	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}
	while (true) {
		bool odd_flag = false;
		//奇数かどうかの判定
		for (int j = 0; j < N; j++) {
			if (a[j] % 2 != 0) {
				odd_flag = true;
			}
		}
		//flagがtrueならwhile文を抜ける
		if (odd_flag) break;
		//実際に2で÷処理をする
		for (int k = 0; k < N; k++) {
			a[k] /= 2;
		}
		count++;
	}
	printf("%d", count);
}

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

int main(){
    int n;
    cin>>n;
    int p[200010];
    int ans=0;
    int tmp=200010;
    rep(i, n){
        cin>>p[i];
        if(tmp>p[i]) ans++;
        tmp=min(tmp,p[i]);
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int num(int N){
int a = 0 , b = 1, c;
for(int i = 0 ; i < 9 ; i++){
a += N / b % 10;
b *= 10;
}
c = N % a;
return c;
}
int main(){
  int N;
  cin >> N;
      if(num(N) == 0){
        cout << "Yes" <<endl;
      }
      else if(num(N) != 0){
        cout << "No" << endl;
      }
}

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

int main() {
  string s;
  cin >> s;
  int c = s.size();
  int p = 0;
  int q = 0;

  for (int i = 0; i < c; i++){
    p *= 10;
    p += s.at(i) - '0';

    q += s.at(i) - '0';
  }
  if(p%q==0){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;

const int NIL = -1;
const int MAX = 2147483647;
clock_t START, END;

struct Node {
	int l, r, key, lazy;
};

bool debug = false;
Node Tree[500010];
int mark[500010];
int n = 0, N = 1;

void update(int x, int y, int z, int k, int height)
{
	if (!mark[k]) {
		Tree[k].l = k * (1 << height) - N;
		Tree[k].r = min(Tree[k].l + (1 << height) - 1, n - 1);
		mark[k] = 1;
	}
	if (x > y)
		return;
	if (x <= Tree[k].l && Tree[k].r <= y) {
		Tree[k].key = z;
		Tree[k].lazy = 1;
		return;
	}
	else if (Tree[k].r < x || Tree[k].l > y) {
		return;
	}
	else {
		if (Tree[k].lazy) {
			Tree[k].lazy = 0;
			Tree[2 * k].key = Tree[2 * k + 1].key = Tree[k].key;
			Tree[2 * k].lazy = Tree[2 * k + 1].lazy = 1;
		}
		update(x, min(Tree[k].l + (1 << (height - 1)) - 1, y), z, 2 * k, height - 1);
		update(max(Tree[k].l + (1 << (height - 1)), x), y, z, 2 * k + 1, height - 1);
	}
}

int find(int k, int height, int z)
{
	if (!mark[k]) {
		Tree[k].l = k * (1 << height) - N;
		Tree[k].r = min(Tree[k].l + (1 << height) - 1, n - 1);
		mark[k] = 1;
	}
	if (Tree[k].lazy) {
		Tree[2 * k].key = Tree[2 * k + 1].key = Tree[k].key;
		Tree[2 * k].lazy = Tree[2 * k + 1].lazy = 1;
		return Tree[k].key;
	}
	else {
		if (z <= Tree[k].l + (1 << (height - 1)) - 1)
			return find(2 * k, height - 1, z);
		else
			return find(2 * k + 1, height - 1, z);
	}
}

int main(void)
{
	if (debug) {
		START = clock();
		freopen("in15.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
	int q, com, x, s, t, k, height = 0;
	scanf("%d%d", &n, &q);
	while (N < n) {
		N *= 2;
		height++;
	}
	for (int i = 1; i <= n + N - 1; i++) {
		Tree[i].key = MAX;
		Tree[i].lazy = 1;
	}
	for (int i = 0; i < q; i++) {
		scanf("%d", &com);
		if (com) {
			scanf("%d", &k);
			printf("%d\n", find(1, height, k));
		}
		else {
			scanf("%d%d%d", &s, &t, &x);
			update(s, t, x, 1, height);
		}
	}
	if (debug) {
		END = clock();
		double endtime = (double)(END - START) / 1000;
		printf("total time = %lf s", endtime);
	}
	return 0;
}

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

struct node{
	int num;
	node *parent,*left,*right;
};
node *root,*NIL;

node *treemin(node *x){
	while(x->left!=NIL)
		x=x->left;
	return x;
}

node *nextnode(node *x){
	if(x->right!=NIL) return treemin(x->right);
	node *y=x->parent;
	while(y!=NIL && x==y->right){
		x=y;
		x=y->parent;
	}
	return y;
}

void denode(node *z){
	node *x,*y;
	if(z->left==NIL||z->right==NIL) y=z;
	else y=nextnode(z);
	if(y->left!=NIL) x=y->left;
	else x=y->right;
	if(x!=NIL) x->parent=y->parent;
	if(y->parent==NIL) root=x;
	else{
		if(y==y->parent->left) y->parent->left=x;
		else y->parent->right=x;
	}
	if(y!=z) z->num=y->num;
//	denode(y);
	return ;
}

node *find(int a){
	node *x=root;
	while(x!=NIL){
		if(x->num<a) x=x->right;
		else if(x->num>a) x=x->left;
		else return x;
	}
	return NIL;
}

void print_inorder(node *x){
	if(x==NIL) return ;
	print_inorder(x->left);
	printf(" %d",x->num);
	print_inorder(x->right);
	return ;
}

void print_preorder(node *x){
	if(x==NIL) return ;
	printf(" %d",x->num);
	print_preorder(x->left);
	print_preorder(x->right);
	return ;
}

void insert(int a){
	node *x=root,*y=NIL;
	node *z=new node();
	z->num=a;z->left=NIL;z->right=NIL;
	while(x!=NIL){
		y=x;
		if(z->num < x->num) x=x->left;
		else x=x->right;
	}
	z->parent=y;
	if(y==NIL) root=z;
	else {
		if(z->num < y->num) y->left=z;
		else y->right=z;
	}
	return ;
}

int main(){
	int n;
	cin>>n;
	string s;
	node *f;
	int a;
	for(int i=0;i<n;i++){
		cin>>s;
		if(s[0]=='i') cin>>a,insert(a);
		else if(s[0]=='p'){
			print_inorder(root);
			printf("\n");
			print_preorder(root);
			printf("\n");
		}
		else if(s[0]=='f'){
			cin>>a;
			f=find(a);
			if(f==NIL) printf("no\n");
			else printf("yes\n");
		}
		else{
			cin>>a;
			denode(find(a));
		}
	}
	return 0;
}


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

int main(int argc, char **argv) {
	string s;
	cin >> s;
	cout << s.substr(0, s.length() - 8) << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
int main()
{
    ll n;
    cin>>n;
    ll m=n,sum=0;
    while(m>0)
    {
        sum+=m%10;
        m=m/10;
    }
  //  cout<<sum<<endl;
    if(n%sum==0)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n;
    cin>>n;
    vector<ll >v;
    vector<ll >vp;

    ll i,j,k;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a;
        v.push_back(a);

    }
    for(i=0; i<n; i++)
    {
        cin>>a;
        vp.push_back(a);

    }
    reverse(vp.begin(),vp.end());
    ll f=0;

    ll l=0,r=n-1;
    for(i=0; i<n; i++)
    {
        if(v[i]==vp[i])
        {
            if(v[l]!=vp[i]&&v[i]!=vp[l])
            {
                //  cout<<l<<"  f "<<i<<endl;

                swap(vp[l],vp[i]);
                  l++;
            }
            else if(v[r]!=vp[i]&&v[i]!=vp[r] )
            {
                  //   cout<<l<<"  s "<<endl;
                f=0;
                swap(vp[r],vp[i]);
                r--;
            }
            else
            {
                f=1;
                break;
            }
        }
    }

    if(f)
    {
        cout<<"No"<<endl;
    }
    else
    {
        cout<< "Yes"<<endl;
        for(ll i=0; i<n; i++)
        {
            cout<<vp[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  char S;
  int mix=0;
  int a=0;
  for(int i=0; i<3;i++){
    cin >> S;
    if (S=='R'){
      a++;
      mix=max(a,mix);
    } else {
      a=0;
    }
  }
  cout << mix << endl;
}


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

int main()
{
    ios_base::sync_with_stdio(false) ;
    cin.tie(NULL);
    int a,b;
    cin>>a>>b;
    cout<<(2*b)-a<<endl;
    return 0;
}

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

int main(){
    int x,y; cin >> x >> y;
    int ans = 0;
    if(x>0){
        if(y>0){
            if(x>y){
                ans++; // x -> -x
                ans += abs(x-y); // x -> -y
                ans++; // x-> y
            }else if(x < y){
                ans+= y-x; // x->y
            }
        }else if(y<0){
            if(abs(x) > abs(y)){
                ans++; // x->-x
                ans+= abs(x) - abs(y); // x->-y
            }else if(abs(x) < abs(y)){
                ans+= abs(y) - abs(x); // x->y
                ans++;   // x -> -y
            }else{
                ans++;
            }
        }else{
            ans++;
            ans+= x;
        }
    }else if(x<0){
        if(y>0){
            if(abs(x)>abs(y)){
                ans += abs(x) -abs(y);
                ans++;
            }else if(abs(x)<abs(y)){
                ans++;
                ans += abs(y) - abs(x);
            }else{
                ans++;
            }
        }else if(y<0){
            if(abs(x)>abs(y)){
                ans += abs(x) -abs(y);
            }else if(abs(x)<abs(y)){
                ans++;
                ans += abs(y) - abs(x);
                ans++;
            }
        }else{
            ans+=abs(x);
        }
    }else{
        if(y>0) ans+=y;
        else if(y<0) ans+= abs(y) + 1;
    }
    cout << ans << endl;
    return 0;
}


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

#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main()
{
    ll i,j;
    ll h,w,a,b;
    cin >> h >> w >> a >> b;
    if(h == 1){
        for(i = 0;i < a;++i) cout << 0;
        for(i = a;i < w;++i) cout << 1;
        cout << endl;
        return 0;
    }else if(w == 1){
        for(i = 0;i < b;++i){
            cout << 0 << endl;
        }
        for(i = b;i < h;++i){
            cout << 1 << endl;
        }
        return 0;
    }
    for(i = 0;i < h;++i){
        for(j = 0;j < w;++j){
            if(i < b && j < a) cout << 0;
            else if(i >= b && j >= a) cout << 0;
            else cout << 1;
        }
        cout << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll,ll>;
constexpr ll mod = 1e9+7;
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define REPS(i,n) for(ll (i)=1;(i)<=(n);(i)++)
#define RREP(i,n) for(ll (i)=(n-1);(i)>=0;(i)--)
#define RREPS(i,n) for(ll (i)=(n);(i)>0;(i)--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());

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

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

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(12);
    ll H,W,A,B;
    cin >> H >> W >> A >> B;
    vector<string> S(H);
    REP(i,B){
        S[i] = string(A,'0') + string(W-A,'1');
    }
    REP(i,H-B){
        S[B+i] = string(A,'1') + string(W-A,'0');
    }
    REP(i,H)cout << S[i] << "\n";
}


#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define vv vector< vector< int > >
#define ll long long
#define ALL(a) (a).begin(), (a).end()
const long long mod = 1000000000 + 7;
const long long INF = 9e18;
const double PI = 3.14159265358979323846;

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

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

int stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}

int main(){
    string S, ans="AC";
    cin >> S;
    if(S[0] != 'A') ans = "WA";
    else {
        S[0] = 'a';
        string sub;
        sub = S.substr(2, S.length()-3);
        if(stringcount(sub, 'C') != 1) ans = "WA";
        else {
            S[S.find('C')] = 'c';
            for(char c : S) {
                if(!islower(c)) {
                    ans = "WA";
                    break;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define re_ return

ll e[19];

ll DFS(ll n, ll m, char k)
{
//    printf("%lld %lld\n", n, m);
    n=abs(n);
    if(m==-1) re_ !n;
    if(!m) re_ n? 0: 10;
    int i=n/e[m];
    if(i>9) re_ 0;
    if((n+i)%10==0)
        re_ max(10-i-k, 0)*DFS((n-i*e[m]+i)/10, m-2, 0);
    else if((n+i+1)%10==0)
        re_ max(10-i-1-k, 0)*DFS((n-(i+1)*e[m]+i+1)/10, m-2, 0);
    re_ 0;
}

int main()
{
    ll n, i, r=0;
    scanf("%lld", &n);
    for(i=e[0]=1; i<19; ++i) e[i]=e[i-1]*10;
//    printf("%lld\n", e[18]);
    for(i=1; i<19; ++i) r+=DFS(n, i, 1);
//    DFS(n, 8, 1);
    printf("%lld\n", r);
    re_ 0;
}


#include<iostream>
#include<string>

using namespace std;

void sort(int card[5])
{
	for(int i=0;i<5;i++)
	{
		int max=i;
		for(int j=i+1;j<5;j++)
		{
			if(card[max]<card[j])
			{
				max=j;
			}
		}
		int tmp=card[i];
		card[i]=card[max];
		card[max]=tmp;
	}
}

bool fourcard(int card[5])
{
	if((card[0]==card[1]&&card[1]==card[2]&&card[2]==card[3])||
		(card[1]==card[2]&&card[2]==card[3]&&card[3]==card[4]))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool fullhouse(int card[5])
{
	if((card[0]==card[1]&&card[2]==card[3]&&card[3]==card[4])||
		(card[0]==card[1]&&card[1]==card[2]&&card[3]==card[4]))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool straight(int card[5])
{
	if((card[0]-1==card[1]&&card[1]-1==card[2]&&card[2]-1==card[3]&&card[3]-1==card[4])||
		(card[0]==13&&card[1]==12&&card[2]==11&&card[3]==10&&card[4]==1))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool threecard(int card[5])
{
	if((card[0]==card[1]&&card[1]==card[2])||
		(card[1]==card[2]&&card[2]==card[3])||
		(card[2]==card[3]&&card[3]==card[4]))
	{
		return true;
	}
	else{
		return false;
	}
}

bool twopair(int card[5])
{
	if((card[0]==card[1]&&card[2]==card[3])||
		(card[0]==card[1]&&card[3]==card[4])||
		(card[1]==card[2]&&card[3]==card[4]))
	{
		return true;
	}
	else return false;
}

bool onepair(int card[5])
{
	if(card[0]==card[1]||
		card[1]==card[2]||
		card[2]==card[3]||
		card[3]==card[4])
	{
		return true;
	}
	else return false;
}

char *solve(int card[5])
{
	sort(card);
	if(fourcard(card))
	{
		return "four card";
	}
	else if(fullhouse(card))
	{
		return "full house";
	}
	else if(straight(card))
	{
		return "straight";
	}
	else if(threecard(card))
	{
		return "three card";
	}
	else if(twopair(card))
	{
		return "two pair";
	}
	else if(onepair(card))
	{
		return "one pair";
	}
	else
	{
		return "null";
	}
}

int main(void)
{
	int card[5];
	char c;
	while(cin>>card[0])
	{
		for(int i=1;i<5;i++)
		{
			cin>>c>>card[i];
		}
		cout<<solve(card)<<endl;
	}
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int a,b,c,d,e;
  while(scanf("%d,%d,%d,%d,%d",&a,&b,&c,&d,&e)!=EOF){
    vi v(14,0);
    v[a]++;
    v[b]++;
    v[c]++;
    v[d]++;
    v[e]++;

    bool ans=false;
    if(v[1]&&v[10]&&v[11]&&v[12]&&v[13]){
      cout<<"straight"<<endl;
      ans=true;
    }
    rep(i,10)
      if(v[i]&&v[i+1]&&v[i+2]&&v[i+3]&&v[i+4]){
	cout<<"straight"<<endl;
	ans=true;
      }
    if(ans)continue;

    int na=0,nb=0,nc=0;
    rep(i,14)
      if(v[i]==2)na++;
      else if(v[i]==3)nb++;
      else if(v[i]==4)nc++;

    if(nc)cout<<"four card"<<endl;
    else if(na&&nb)cout<<"full house"<<endl;
    else if(nb)cout<<"three card"<<endl;
    else if(na==2)cout<<"two pair"<<endl;
    else if(na)cout<<"one pair"<<endl;
    else cout<<"null"<<endl;
  END:;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b,c) for(ll i=a;i<b;i+=c)
#define r(i,a,b,c) for(ll i=a;i>=b;i-=c)
#define mod 1000000007
int main(){
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll n; cin >> n;
    ll ar[n];
    f(i,0,n,1){
    	cin >> ar[i];
    }
    ll c = 0;
    sort(ar,ar+n);
    f(i,0,n-2,1){
    	f(j,i+1,n-1,1){
    		f(k,j+1,n,1){
    		if(ar[i]+ar[j]>ar[k] && (ar[i]!=ar[j] && ar[j]!=ar[k] && ar[k]!=ar[i]))
    			c++;
    		}
    	}
    }
    cout << c;
    return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int mn = INT_MAX;
	long long res = 0;
	for (int i = 0; i < n; i++) {
		if (i + 1 < n && a[i] < 0) {
			a[i + 1] *= -1;
			a[i] *= -1;
		}
		res += abs(a[i]);
		mn = min(mn, abs(a[i]));
	}
	if (a[n - 1] < 0) res -= 2 * mn;
	cout << res;
	return 0;
}


#include<iostream>
#include<string.h>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

#define inf 0x3f3f3f3f
#define N

int main()
{
	std::ios::sync_with_stdio(false);
	long long n,i,j,k;

    while(cin>>i>>j>>k)
    {
    cout<<k<<" "<<i<<" "<<j<<endl;
    }
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

vector<string> a;

bool cmp(string s, string t) {
    if (s.length() != t.length()) return s.length() < t.length();
    return s < t;
}

void sol(string s) {
    a.push_back(s);
    if (s.length() == 10) return;
    if (s.back() != '0') {
        s += (s.back() - 1);
        sol(s);
        s.pop_back();
    }
    s += (s.back());
    sol(s);
    s.pop_back();
    if (s.back() != '9') {
        s += (s.back() + 1);
        sol(s);
        s.pop_back();
    }
}

int main() {
    ios::sync_with_stdio(false);
    int a, b, c;
    cin >> a >> b >> c;
    cout << c << ' ' << a << ' ' << b;
}


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

int main(){
  int h,w;
  cin>>h>>w;
  vector<vector<int>> c(h,vector<int>(w));
  vector<pair<int,P>> coin;
  rep(i,h){
    rep(j,w) cin>>c[i][j];
    if(i%2==1) reverse(c[i].begin(),c[i].end());
  }
  rep(i,h)rep(j,w){
    if(i%2==0) coin.push_back(make_pair(c[i][j],make_pair(i+1,j+1)));
    else coin.push_back(make_pair(c[i][j],make_pair(i+1,w-j)));
  }
  vector<pair<P,P>> ans;
  rep(i,h*w-1){
    if(coin[i].first%2 == 1){
      coin[i+1].first++;
      ans.push_back(make_pair(make_pair(coin[i].second.first,coin[i].second.second),make_pair(coin[i+1].second.first,coin[i+1].second.second)));
    }
  }
  cout<<ans.size()<<endl;
  for(auto x : ans){
    cout<<x.first.first<<" "<<x.first.second<<" "<<x.second.first<<" "<<x.second.second<<endl;
  }
}

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void insertionSort(int array[], int N, int g, int *cnt){
    int i,j,v;
    for(i = g; i < N; i++){
        v = array[i];
        j = i - g;
        while( j >= 0 && array[j] > v){
            array[j+g] = array[j];
            j = j - g;
            (*cnt)++;
        }
        array[j+g] = v;
    }
    return;
}

int main(int argc, const char * argv[]){

    //Input
    int i, N, value, ans;
    int cnt = 0;
    scanf("%d", &N);

    int *array = NULL;
    array = (int*)malloc(sizeof(int) * N);

    REP(i, N){
        scanf("%d", &value);
        array[i] = value;
    }

    int m;
    int G[100];
    for( i = 0; i < 100; ++i ){
        if( i == 0 ) G[i] = N / 2 + 1;
        else G[i] = G[i-1] / 2 - 1;
        if( G[i] < 0 ){
            G[i-1] = 1;
            G[i] = 0;
            m = i;
            break;
        }
    }

    ans = 0;
    for(int i = 0; i < m; i++){
        insertionSort(array, N, G[i], &cnt);
    }

    //print m
    printf("%d\n", m);

    //print G[]
    for(i = 0; i < m; i++){
        printf("%d", G[i]);
        if(i != m-1){
            printf(" ");
        }
    }
    printf("\n");

    //print cnt
    printf("%d\n", cnt);

    REP(i, N){
        printf("%d\n", array[i]);
    }
    return 0;
}


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

int fl[10001] = {0};

void isfl(int);
void twlf(int);

int main(void) {
  int tag;

  while(1){
    cin >> tag;
    if(tag == 0) break;
    isfl(tag);
    twlf(tag);
  }
  return 0;
}

void isfl(int tag){
  int i,lim,idx = 2;
  fl[0] = 1;
  fl[1] = 1;

  lim = sqrt(tag);

  while(idx <= lim){
    if(fl[idx] == 0) for(i = idx*2;i<=tag;i+=idx) fl[i] = 1;
    idx++;
  }
  return;
}

void twlf(int tag){
  int k=0,data[10000];
  for(int i=1;i<=tag;i++) if(fl[i] == 0){
    data[k] = i;
    k++;
  }
  for(int i=k-1;i>=0;i--){
    if(data[i] - data[i-1] == 2){
      cout << data[i-1] << " " << data[i] << endl;
      break;
    }
  }
  return;
}

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

int main(){
	int n;
	int i , j;
	while(cin >> n, n!=0) {
	int arr[10][10]={0,0};
	int ansW[11]={0};
	int ansH[11]={0};
		for(i=0 ; i<n ; i++){
			for(j=0 ; j <n ; j++){
				cin >>arr[i][j];
			}
		}
		for(i=0 ; i<n ; i++){
			for(j=0 ; j<n ; j++){
				ansH[i]+=arr[i][j];
			}
		}
		for(i=0 ; i<n ; i++){
			for(j=0 ; j<n ; j++){
				ansW[i]+=arr[j][i];
			}
		}
		for(i=0 ; i<n ; i++){
			for(j=0 ; j<n ; j++){
				cout.fill(' ');
				cout << setw(5) <<right <<arr[i][j];
			}
			cout << setw(5) <<right <<ansH[i] << endl;
		}
		for(i=0; i<n ; i++){
				ansW[n]+=ansW[i];
		}
		for(i=0 ; i<n+1 ; i++){
			cout << setw(5) <<right << ansW[i];
		}
		cout << endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define AB(A) for(A=0;A<100;A++)a[A]=0

int main() {
  int n = 0, tmp0 = 0, tmp1 = 0;
  int tmp2 = 0, tmp3, out1 = 0, out2 = 0;
  int a[100];
  scanf("%d", &n);
  while (n) {
    AB(tmp3);
    tmp0 = 0, out2 = 0;
    while (tmp0 < n) {
      tmp1 = 0, out1 = 0;
      while (tmp1 < n) {
        scanf("%d", &tmp2);
        a[tmp1] += tmp2;
        printf("%5d", tmp2);
        out1 += tmp2;
        tmp1++;
      }
      printf("%5d\n", out1);
      a[tmp1] += out1;
      tmp0++;
    }
    tmp0 = 0;
    while (tmp0 < n) {
      printf("%5d", a[tmp0]);
      tmp0++;
    }
    printf("%5d\n", a[tmp0]);
    scanf("%d", &n);
  }
  return 0;
}

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    int matrix[11][11] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
	cin >>matrix[i][j];
      }
    }
    int suma = 0;
    for(int i=0,sumc,sumr; i<n; i++){
      sumc = 0;
      sumr = 0;
      for(int j=0; j<n; j++){
	sumc+=matrix[i][j];
	sumr+=matrix[j][i];
      }
      suma+=sumc;
      matrix[i][n] = sumc;
      matrix[n][i] = sumr;
    }
    matrix[n][n] = suma;
    for(int i=0; i<=n; i++){
      for(int j=0; j<=n; j++){
	printf("%5d",matrix[i][j]);
      }
      cout <<endl;
    }
  }
  return 0;
}


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

int main() {
    int n, l;
    cin >> n >> l;
    vector<int> d(n);
    for (int i = 1; i <= n; i++) {
        d[i-1] = l+i-1;
    }
    int all = 0;
    rep(i,n) all += d[i];
    int ans = 0;
    int mn = 10000;
    rep(i,n) {
        int dall = all-d[i];
        if (abs(all-dall) < mn) {
            mn = abs(all-dall);
            ans = dall;
        }
    }
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n,k;
    cin>>n>>k;
    if(n%k==0)
        cout<<"0"<<endl;
    else
        cout<<"1"<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end())
using namespace std;
template<class T> inline void chmax(T& a, T b) { a = max(a, b); }
template<class T> inline void chmin(T& a, T b) { a = min(a, b); }

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x > 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % mod;
		}
		x = x * x % mod;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2) % mod;
}
/*--------Library Zone!--------*/

int n; string s;
vector<pair<string, string>>le, ri;
signed main() {
	cin >> n >> s;
	rep(i, 1 << n) {
		string p = "", q = "", r = "", u = "";
		rep(j, n) {
			if (i >> j & 1) {
				p += s[j]; r += s[n + j];
			}
			else {
				q += s[j]; u += s[n + j];
			}
		}
		le.push_back({ p,q });
		ri.push_back({ r,u });
	}
	sort(all(ri));
	int ans = 0;
	for (auto p : le) {
		string red = p.first, blue = p.second;
		reverse(all(red)); reverse(all(blue));
		int index = upper_bound(all(ri), mkp(blue, red)) - lower_bound(all(ri), mkp(blue, red));
		ans += index;
	}
	cout << ans << endl;
}

#include<iostream>
#include<iomanip>
#include<cassert>
#include<stdexcept>
#include<utility>
#include<functional>
#include<numeric>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<array>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<complex>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<bitset>
#include<memory>
using namespace std;
using ll=long long;

const int SIZE=17;

const int MOD=1e9+7;

inline void add(int &lhs,int rhs){
    lhs=(lhs+rhs);
    if(lhs>=MOD) lhs-=MOD;
}
int dp[41][1<<SIZE];
int isAccept[1<<SIZE];
int mypow10[40];

int main(){
    int n;
    cin>>n;
    int xyz[3];
    for(int i=0;i<3;i++) cin>>xyz[i];

    dp[0][(1<<SIZE)-1]=1;

    for(int bit=0;bit<(1<<SIZE);bit++){
        int st=0;
        int buf=0;
        for(int i=0;i<SIZE;i++){
            buf++;
            if(!((bit>>i)&1) && xyz[st]==buf){
                st++;
                buf=0;
                if(st==3){
                    isAccept[bit]=true;
                    break;
                }
            }
        }
    }

    mypow10[0]=1;
    for(int i=0;i+1<n;i++) mypow10[i+1]=ll(mypow10[i])*10%MOD;

    int res=0;
    int mask=(1<<SIZE)-1;
    for(int i=0;i<n;i++){
        res=res*10LL%MOD;
        for(int bit=0;bit<(1<<SIZE);bit++){
            if(dp[i][bit]==0) continue;
            for(int v=1;v<=7;v++){
                int nex=(bit<<v)+(1<<(v-1))-1;
                nex&=mask;
                add(dp[i+1][nex],dp[i][bit]);
            }
            add(dp[i+1][mask],dp[i][bit]);
            add(dp[i+1][mask],dp[i][bit]);
            add(dp[i+1][mask],dp[i][bit]);
        }
        for(int bit=0;bit<(1<<SIZE);bit++){
            if(isAccept[bit]){
                add(res,dp[i+1][bit]);
                dp[i+1][bit]=0;
            }
        }
    }

    cout<<res<<endl;

    return 0;
}


#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using pint = pair<int, int>;

template <class T>
using vec = vector<T>;
template <class T>
using vec2 = vector<vec<T>>;
template <class T>
using vec3 = vector<vec2<T>>;

constexpr int INF = numeric_limits<int>::max();
constexpr ll INFL = numeric_limits<ll>::max();
//constexpr ll MOD = 1000000007; // 10^9+7
constexpr ll MOD = 998244353;

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

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

vector<long long> fac, finv, inv;

// テーブルを作る前処理
void COMinit(int max)
{
    fac.resize(max);
    finv.resize(max);
    inv.resize(max);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < max; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modpow(ll x, ll n, const ll &mod)
{
    ll ret = 1;
    while (n > 0)
    {
        if (n & 1)
        {
            (ret *= x) %= mod;
        }
        (x *= x) %= mod;
        n >>= 1;
    }
    return ret;
}

ll N, M, K;

ll solve()
{
    ll ret = 0;
    for (int k = 0; k <= K; ++k)
    {
        ll a = 1;
        a *= M;
        a %= MOD;
        a *= modpow(M - 1, N - 1 - k, MOD);
        a %= MOD;
        a *= COM(N - 1, k);
        a %= MOD;
        ret += a;
        ret %= MOD;
    }
    return ret;
}

int main()
{
    COMinit(2 * 100000);
    cin >> N >> M >> K;
    cout << solve() << endl;
    return 0;
}

#include <memory.h>

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

using namespace std;

#define MOD 998244353
#define INIT -1

long long int fact[200010] = {};

long long int mypow(long long int x, long long int n)
{
   if (n == 0)
      return 1;
   long long int tmp = mypow(x, n / 2);
   long long int res = (tmp * tmp) % MOD;
   if (n % 2 == 1)
      res = (res * x) % MOD;
   return res;
}

long long int comb(int n, int r)
{
   long long int res = fact[n];
   res = (res * mypow(fact[r], MOD - 2)) % MOD;
   res = (res * mypow(fact[n - r], MOD - 2)) % MOD;
   return res;
}

int main()
{
   long long int n, m, k;
   cin >> n >> m >> k;

   fact[0] = 1;
   for (int i = 1; i <= n - 1; i++)
   {
      fact[i] = (fact[i - 1] * i) % MOD;
   }

   long long int ans = 0;
   for (int i = 0; i <= k; i++)
   {
      ans += (m * ((comb(n - 1, i) * mypow(m - 1, n - 1 - i)) % MOD)) % MOD;
      ans %= MOD;
   }
   cout << ans << endl;
}


//Tanzim bin nasir
#include<bits/stdc++.h>
typedef long long ll;
#define ff first
#define ss second
using namespace std;
void fast()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
ll gcd(ll a,ll b)
{
    if(b==0)
    return a;
    else
    gcd(b,a%b);
}
ll lcd(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
int main()
{
    int n,ans=1,mn,temp;
    cin>>n>>mn;
    for(int i=1;i<n;i++)
    {
        cin>>temp;
        if(temp<=mn)
        {
            mn=temp;
            ++ans;
        }
    }
    cout<<ans<<endl;
}

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

int main() {
    ll n;
    cin >> n;
    int m, p;
    ll ans = 0;
    m = 1e6;
    rep(i, n) {
        cin >> p;
        if (m >= p) {
            ans++;
        }
        m = min(m, p);
    }
    cout << ans << endl;
    return 0;
}


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

bool is_sum (vector<int> s, int n){
    if(s.size() == 1) {
        if(s.at(0) == n) return true;
        else return false;
    }
    else{
        int a = s.at(s.size()-1);
        s.pop_back();
        if(is_sum(s,n+a) || is_sum(s,n-a)) return true;
        else return false;
    }

}

vector<string> cal_sum (vector<int> s, int n){
    if(s.size() == 1) return {};
    else{
        int a = s.at(s.size()-1);
        s.pop_back();
        if(is_sum(s,n-a)){
            vector<string> v = cal_sum(s,n-a);
            v.push_back("+");
            return v;
        }
        if(is_sum(s,n+a)){
            vector<string> v = cal_sum(s,n+a);
            v.push_back("-");
            return v;
        }
    }
}

int main(){
    string s ;
    cin >> s;
    vector<int> v(s.size());
    for (int i = 0; i < s.size(); i++)
    {
        v.at(i)  = s.at(i) -48;
    }

    vector<string> sign = cal_sum(v,7);
    cout << s.at(0);
    for (int i = 1; i < 4; i++)
    {
        cout << sign.at(i-1) << v.at(i) ;
    }
    cout << "=7" << endl;

}

#define _USE_MATH_DEFINES  // M_PI等のフラグ

#include <iostream>
#include <bitset>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <list>

#define MOD 1000000007
#define COUNTOF(array) (sizeof(array)/sizeof(array[0]))

using namespace std;

void solve(){
    string s;
    cin >> s;

    for (int bit=0; bit<(1<<3); bit++) {
        string ans = s.substr(0,1);
        int result = s.at(0) - '0';

        for (int i=0; i<3; i++) {
            if (bit>>i&1) {
                // iビット目が1なら+演算する
                result += s.at(i+1)-'0';
                ans += "+";
            }
            else {
                // iビット目が0なら-演算する
                result -= s.at(i+1)-'0';
                ans += "-";
            }
            ans.push_back(s.at(i+1));
        }

        if (result == 7) {
            cout << ans << "=7" << endl;
            return;
        }
    }
}

int main(int argc, char const *argv[]){
    solve();
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <string.h>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
using namespace std;
//https://codeforces.com/contest/1213/problems
//https://codeforces.com/contest/1213/submit
ll a[100];
int main(){
    ll n;
    sld(n)
    re(a,0ll);

    for(int i = 0ll; i < 50ll; i ++){
        a[i] = 49ll;
    }
    ll count0 = n/50ll;
    ll count1 = n%50ll;
    for(int i = 0; i < 50; i ++){
        a[i] += count0;
    }
    for(int i = 0; i < count1; i ++){
        a[i] += 50ll;
        for(int j = 0; j < 50; j ++){
            if(i == j){
            }else{
                a[j] --;
            }
        }
    }
    pld(50ll) pn
    for(int i = 0; i < 49; i ++){
        pld(a[i]) pk
    }
    pld(a[49]) pn
    return 0;
}

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

using namespace std;

class MCMult
{
public:
  vector<vector<int>> mat;
  int solve();
};

int MCMult::solve()
{
  int ms = mat.size();
  vector<vector<int>> dp(ms, vector<int>(ms, 0));

  for ( auto i = 0; i < ms - 1; ++i )
    dp[i][i+1] = mat[i][0] * mat[i][1] * mat[i+1][1];

  for ( auto k = 2; k < ms; ++k )
    {
      for ( auto i = 0; i + k < ms; ++i )
        {
          int ret = dp[i+1][i+k] + mat[i][0] * mat[i][1] * mat[i+k][1];
          ret = min( ret, dp[i][i+k-1] + mat[i+k][0] * mat[i+k][1] * mat[i][0]);

          for ( auto j = i + 1; j < i + k - 1; ++j )
            {
              ret = min( ret, dp[i][j] + dp[j+1][i+k] + mat[i][0] * mat[j][1] * mat[i+k][1]);
            }
          dp[i][i+k] = ret;
        }
    }

  cout << dp[0][ms-1] << endl;

  return 0;
}

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

  MCMult m;

  for ( int i = 0; i < n; ++i )
    {
      int x;
      vector<int> v;

      cin >> x;
      v.push_back( x );
      cin >> x;
      v.push_back( x );

      m.mat.push_back( v );
    }

  m.solve();

  return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <string>
#include <cmath>
#include <complex>
#include <numeric>
#include <cassert>

#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i) cout << fixed << setprecision(i)		//coutの浮動小数出力の桁数設定
using namespace std;

int main()
{
	int64 aa, bb, ans = 1, tmp1, tmp2;
	cin >> aa >> bb;
	tmp1 = tmp2 = gcd(aa, bb);
	for(int64 ii = 2;ii * ii <= tmp2;ii++)
	{
		if(tmp1 % ii == 0)
		{
			ans++;
			while(tmp1 % ii == 0)
			{
				tmp1 /= ii;
			}
		}
	}
	if(tmp1 != 1)
	{
		ans++;
	}
	cout << ans << endl;
	return 0;
}

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

int main() {
 int R;
 int G;
  //Rは現在のレート,Gは取るべき点数
  //R,Gで入力を受け取る
  cin>> R;
  cin>> G;
    cout << (G*2)-R << endl;

}


//include
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <math.h>
#include <utility>
#include <functional>
#include <cstdlib>
//using
using namespace std;
using vl = vector <long long>;
using vs = vector <string>;
using vc = vector <char>;
using ll= long long;
using vvl = vector<vector<ll> >;
//vector<vector<char> > hyou(N, vector<char>(N));
using vd = vector <double>;
//define
//#define int long long
#define rep(i,n) for(int i=0; i<n; i++)
#define print(n) cout<<n<<endl;
#define sortp(d) sort(d.begin(),d.end()) //1234
#define sortm(d) sort(d.rbegin(),d.rend()) //4321
//素数判定　O(√A)
bool is_prime(int x){
 if(x<=1) return false;
 for(int i=2;i*i<=x;i++)
 if(x%i==0) return false;
 return true;
 }
/*順列生成
do {
  // 順列に対する処理
} while (next_permutation(配列変数.begin(), 配列変数.end()));*/

//絶対値　abs()
//文字→数字　'0' アルファベット→数字　'a'　'A'が65番,'a'が97番だわ
//型キャスト　char('a'+1)

//グローバル変数宣言
 ll p=0,q=0;//r=0;

int main()
{
  ll n,m;
  cin>>n>>m;
  vl l(m),r(m);
  q=n;
  rep(i,m){cin>>l[i]>>r[i];p=max(p,l[i]);q=min(q,r[i]);}
  ll a=0;
  a=max(a,q-p+1);
  print(a)
}

#include <stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=1000005;
vector <int> adj[MAXN];
int prof[MAXN],low[MAXN],padre[MAXN],cont,N,M,root;
bool usa[MAXN],ap[MAXN];
void articulationPoints(int v){
    int child=0;
    usa[v]=true;
    low[v]=prof[v]=++cont;
    for (int i=0; i<(int)adj[v].size(); i++){
        int w=adj[v][i];
        if (!usa[w]){
            child++;
            padre[w]=v;
            articulationPoints(w);
            low[v]=min(low[v],low[w]);
            if (padre[v]==-1 && child>1) ap[v]=true;
            if (padre[v]!=-1 && low[w]>=prof[v]) ap[v]=true;
        } else if(w!=padre[v]) low[v]=min(low[v],prof[w]);
    }
}
int main()
{
    scanf("%d%d",&N,&M);
    int a,b;
    for (int i=0; i<M; i++){
        scanf("%d%d",&a,&b);
        adj[a].push_back(b); adj[b].push_back(a);
    }
    fill(padre,padre+N,-1);
    articulationPoints(0);
    for (int i=0; i<N; i++)
        if (ap[i])printf("%d\n",i);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb push_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}
ll ans=0;
const int l=20;
vi dep;
vvi table;
vvi g;
void dfs0(int i,int p,int d){
    table[i][0]=p;
    dep[i]=d;
    for(ll x:g[i])if(x!=p)dfs0(x,i,d+1);
}
void init(ll n){
    g=vvi(n);
    dep=vi(n);
    table=vvi(n,vi(l,-1));
    rep(i,n-1){
        ll a;cin>>a;a--;
        g[a].pb(i+1);
        g[i+1].pb(a);
    }
    dfs0(0,-1,0);
}
vb done;
void dfs(int i){
    done[i]=true;
    for(ll x:g[i])if(x!=table[i][0]){
        if(!done[x])dfs(x);
    }
}
int main(){
    ll n,k,t;cin>>n>>k>>t;
    if(t!=1)ans++;
    if(k==1){
        rep(i,n-1){
            ll a;cin>>a;if(a!=1)ans++;
        }
        dame(ans);
    }
    init(n);
    REP(i,1,l){
        rep(j,n){
            ll id=table[j][i-1];
            if(id!=-1)table[j][i]=table[id][i-1];
        }
    }
    k--;
    vp v(n);
    rep(i,n)v[i]=P(dep[i],i);
    sort(all(v));reverse(all(v));
    done=vb(n,false);
    rep(tt,n){
        ll i=v[tt].se;
        if(dep[i]<=k+1)break;
        if(done[i])continue;
        ll w=i;
        rep(j,l)if(k>>j&1)w=table[w][j];
        ans++;
        dfs(w);
    }
    out(ans);
}

#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 1e5 + 5;

int K, ans;

int par [MAX_N];
vector<int> adj [MAX_N];

int subh [MAX_N];
int del [MAX_N];

void dfs (int u) {
  subh[u] = 0;
  for (int nxt : adj[u]) {
    dfs(nxt);
    if (!del[nxt]) {
      subh[u] = max(subh[u], 1 + subh[nxt]);
    }
  }

  if (u != 1 && subh[u] == K - 1 && par[u] != 1) {
    del[u] = 1;
    ans++;
  }
}

int main () {
  ios::sync_with_stdio(false);

  int n;
  cin >> n >> K;

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

  if (par[1] != 1) ans++;
  for (int i = 2; i <= n; i++) {
    adj[par[i]].push_back(i);
  }

  dfs(1);

  cout << ans << endl;
}


#include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;

int main(){
  string s1,s2;
  cin >> s1 >> s2;
  vector<vector<int>> dp(s1.size()+1,vector<int>(s2.size()+1,0));
  REP(i,s1.size()+1)dp[i][0] = i;
  REP(i,s2.size()+1)dp[0][i] = i;

  REP(i,s1.size()){
    REP(j,s2.size()){
      int tmp = 1;
      if(s1[i] == s2[j])tmp = 0;
      dp[i+1][j+1] = min(dp[i][j] + tmp, min(dp[i][j+1],dp[i+1][j])+1);
    }
  }

  cout << dp[s1.size()][s2.size()] << endl;
  return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define REP(i,k,n) for(int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

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

template<class T = ll> T in() {
    T _in;
    cin >> _in;
    return _in;
}

int dp[1001][1001];

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

    string s[2];
    int len[2];
    rep(i, 2) {
        cin >> s[i];
        len[i] = s[i].size();
    }

    // 表埋め
    rep(i, len[0]+1) {
        dp[i][0] = i;
    }
    rep(j, len[1]+1) {
        dp[0][j] = j;
    }

    REP(i, 1, len[0]+1) {
        REP(j, 1, len[1]+1) {
            // if(i==2 && j==1) {
            //     cout << s[0][i-1] << " " << s[1][j-1] << endl;
            // }
            int top_left = dp[i-1][j-1];
            if(s[0][i-1] != s[1][j-1]) {
                top_left++;
            }
            int top = dp[i-1][j] + 1;
            int left = dp[i][j-1] + 1;
            dp[i][j] = min(top, min(top_left, left));
        }
    }

    // rep(i, len[0]+1) {
    //     rep(j, len[1]+1) {
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    cout << dp[len[0]][len[1]] << endl;

    return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,a,b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define YY cout << "Yes" << endl;
#define NN cout << "No" << endl;
#define ve vector
#define pint pair<int,int>
const int INF = 1000000007;
const long double EPS = 1e-15;
const long double PI = acos(-1);
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); }
// *max_element(all(d))
int solve(){
  int n;
  cin >> n;
  cout << 0 << endl;
  string f;
  cin >> f;
  int l = 0,r = n;
  if(f!="Vacant")while(1){
    int m = (l + r) / 2;
    cout << m << endl;
    string s;
    cin >> s;
    if(s == "Vacant")break;
    if(m % 2 && s != f || m % 2 == 0 && s == f)l = m + 1;
    else r = m;
  }
  return 0;
}
int main(){
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include<cmath>
#include<limits>
#define ll long long
#define ALL(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 	9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define sysp system("PAUSE")
using namespace std;

int Getinput(){
    string s; cin >> s;
    if(s == "Vacant") return 0;
    else if(s == "Male") return 1;
    else if(s == "Female") return 2;
}
int main(){
    int N; cin >> N;

    cout << N - 1 << endl;
    if(Getinput() == 0){
        return 0;
    }
    cout << 0 << endl;
    int x = Getinput();
    if(x == 0){
        return 0;
    }

    int low = 0,high = N - 1;
    int stnd = x;

    while(high - low > 2){
        int mid = (high + low) / 2;
        if(mid % 2 == 1) mid--;

        cout << mid << endl;
        int y = Getinput();
        if(y == 0){
            return 0;
        }else if(stnd == y){
            low = mid;
        }else{
            high = mid;
        }
    }

    cout << low + 1 << endl;
    int z = Getinput();
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

long long MOD = 1000000007;
template<typename T>
void cout_vec(vector<T> &vec){
    for(int i=0; i<vec.size(); i++){
        if (i!=0){
            cout << ' ';
        }
        cout << vec[i];
    }
    cout << endl;
}
template<typename T>
void cout_mat(vector<vector<T>  > &mat){
    for(int i=0; i<mat.size(); i++){
        vector<T> vec = mat[i];
        cout_vec(mat[i]);
    }
}

int main(){
    int H, W;
    cin >> H >> W;

    vector<vector<int>> grid(H,vector<int>(W,0));
    for(auto &line:grid){
        for(auto &a:line){
            cin >> a;
        }
    }

    for(auto &line:grid){
        for(auto &a:line){
            int x;
            cin >> x;
            a-=x;
        }
    }
    int N=160*160;

    vector<vector<vector<int>>> dp(H+1,vector<vector<int>>(W+1,vector<int>(N+1,0)));
    dp[0][0][N/2]=1;

    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            int diff=grid[i][j];
            for(int k=0; k<=N; k++){
                if(0<=k+diff && k+diff<=N){
                    dp[i+1][j][k+diff]|=dp[i][j][k];
                    dp[i][j+1][k+diff]|=dp[i][j][k];
                }
                if(0<=k-diff && k-diff<=N){
                    dp[i][j+1][k-diff]|=dp[i][j][k];
                    dp[i+1][j][k-diff]|=dp[i][j][k];
                }
            }
        }
    }

    for(int i=0; i<N; i++){
        if(dp[H][W-1][N/2-i] || dp[H][W-1][N/2+i]){
            cout << i << endl;
            return 0;
        }
    }

}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    const ll d[2][2] = {{0, 1}, {1, 0}};
    const ll MAXV = 80 * (80 + 80);
    ll h, w;
    cin >> h >> w;
    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));
    rep(i, h) rep(j, w) cin >> a[i][j];
    rep(i, h) rep(j, w) cin >> b[i][j];
    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(MAXV + 1, false)));
    dp[0][0][abs(a[0][0] - b[0][0])] = true;
    queue<pair<ll, ll>> q;
    q.push(make_pair(0, 0));
    vector<vector<bool>> seen(h, vector<bool>(w, false));
    seen[0][0] = true;
    while(!q.empty()) {
        pair<ll, ll> p = q.front(); q.pop();
        ll x = p.second, y = p.first;
        rep(i, 2) {
            ll nx = x + d[i][1];
            ll ny = y + d[i][0];
            if ((nx >= w) || (ny >= h)) continue;
            rep(j, MAXV + 1) {
                if (!dp[y][x][j]) continue;
                dp[ny][nx][j + abs(a[ny][nx] - b[ny][nx])] = true;
                dp[ny][nx][abs(j - abs(a[ny][nx] - b[ny][nx]))] = true;
            }
            if (!seen[ny][nx]) {
                seen[ny][nx] = true;
                q.push(make_pair(ny, nx));
            }
        }
    }
    ll ans = LONG_LONG_MAX;
    rep(i, MAXV + 1) {
        if (dp[h - 1][w - 1][i]) {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}


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

void Input(int*,int*);
int Calc(int*,int*);
void Output(int*,int*,int*);

int main(int argc,char* argv[]){
  int n,count=0;
  int arr[100];

  Input(&n,arr);
  count=Calc(&n,arr);

  Output(&n,&count,arr);

  return 0;
}

void Input(int* pn,int *arr){

  cin>>*pn;
  for(int i=0;i<*pn;i++){
    cin>>*arr++;
  }
}

int Calc(int* pn,int* arr){
  int count=0;

  for(int i=0;i<*pn;i++){
    for(int j=*pn-1;j>i;j--){
      if(arr[j]<arr[j-1]){
	swap(arr[j],arr[j-1]);
	count++;
      }
    }
  }
  return count;
}

void Output(int* pn,int* pcount,int* arr){

  printf("%d",*arr++);
  for(int i=1;i<*pn;i++)
    printf(" %d",*arr++);
  puts("");

  printf("%d\n",*pcount);
}

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
const int MAX=100000;
const int geta=100;
struct BIT
{
	int bit[MAX+1];
	void add(int i,int x)
	{
		while(i<=MAX)
		{
			bit[i]+=x;
			i+=i&-i;
		}
	}
	int sum(int i)
	{
		int res=0;
		while(i>0)
		{
			res+=bit[i];
			i-=i&-i;
		}
		return res;
	}
};
BIT bit;
int N,a[105];
int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		scanf("%d",&a[i]);
		a[i]+=geta;
	}
	int ans=0;
	for(int i=0;i<N;i++)
	{
		ans+=bit.sum(a[i]);
		bit.add(a[i],1);
	}
	sort(a,a+N);
	for(int i=0;i<N;i++)printf("%d%c",a[i]-geta,(i==N-1)?'\n':' ');
	printf("%d\n",N*(N-1)/2-ans);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x ,n) for(int i = x; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main(){
    int n,k;
    cin >> n >> k;
    int ans=1;
    rep(i,n){
        if(ans<k)ans*=2;
        else ans+=k;
    }

    cout << ans;

	return 0;
}

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

ll N, K;

ll solve(ll N);

int main()
{
    cin >> N >> K;
    cout << solve(N) << endl;
}

ll solve(ll N)
{
    if (N == 1)
    {
        return 2;
    }
    return min(solve(N - 1) * 2, solve(N - 1) + K);
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i ,n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int>vint;
typedef vector<ll>vll;
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename A,typename B>inline bool chmin(A &a,const B &b){if(a>b){a=b;return true;}else{return false;}}
template<typename A,typename B>inline bool chmax(A &a,const B &b){if(a<b){a=b;return true;}else{return false;}}

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

int main()
{
    string A,B,C;
    cin >> A >> B >> C;
    queue<char> qA,qB,qC;
    for(auto a:A){
        qA.push(a);
    }
    for(auto b:B){
        qB.push(b);
    }
    for(auto c:C){
        qC.push(c);
    }
    bool flag = true;
    char now = qA.front();
    qA.pop();
    string ans;
    while(flag){
        switch(now){
        case 'a':
            if(qA.empty()){
                ans = "A";
                flag = false;
            }else{
                now = qA.front();
                qA.pop();
            }
            break;
        case 'b':
            if(qB.empty()){
                ans = "B";
                flag = false;
            }else{
                now = qB.front();
                qB.pop();
            }
            break;
        case 'c':
            if(qC.empty()){
                ans = "C";
                flag = false;
            }else{
                now = qC.front();
                qC.pop();
            }
            break;
        default:
            break;
        }
    }
    cout << ans << endl;
}

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

#define rep(i,a,b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define fill(x,v) memset(x,v,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define trace(x) cerr << #x <<": " << x << endl
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;

const int MAX = 3002;
const ll INF = 1LL<<62;

bool exist[30];
vi mayor, menor;

void solve(){
    ll n, k;
    cin >> n >> k;
    ll on  = 0, a;
    rep(i,0,n){
        cin >> a;
        if(a < 0) menor.pb(a);
        if(a > 0) mayor.pb(a);
        if(a == 0) on = 1;
    }

    ll sobra = k - on;
    ll rpta = 0;
    if(sobra){
        reverse(all(menor));
        ll aux = INF;
        int j;
        ll aux1, aux2;
        for(int i = 0; i <= min(sobra, ll(mayor.size())); i++){
            j = sobra - i;
            if(j > menor.size()) continue;
            aux1 = 0;
            aux2 = 0;
            if(i) aux1 = mayor[i-1];
            if(j) aux2 = menor[j-1]*-1;

            if(aux1 > aux2) swap(aux1, aux2);
            aux = min(aux, aux1+aux1+aux2);
        }
        rpta  += aux;
    }
    cout << rpta << endl;

}

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


#include <iostream>

using namespace std;

int main()
{
    int m[50],d[50];
    int x = 0;
    string Day[7] = {"Thursday", "Friday","Saturday","Sunday","Monday", "Tuesday", "Wednesday"};
    int BanyakHari[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
    string ThatDayIs[50];

    do
    {
        int DayinBetween = 0;
        cin >> m[x] >> d[x];
        if((m[x] == 0) && (d[x] == 0))
        {
            break;
        }
        //Check banyak hari antara 1 Januari - Input
        for(int y = 0; y < m[x]-1; y++)
        {
            DayinBetween = DayinBetween + BanyakHari[y];
        }
        DayinBetween = (DayinBetween + d[x] - 1)%7;

        ThatDayIs[x] = Day[DayinBetween];

        x++;
    }
    while(x < 50);

    for(int z = 0; z < x; z++)
    {
        cout << ThatDayIs[z] << endl;
    }

}

#include <iostream>
#include <map>
#include <vector>

using namespace std;

void getDays(int month, int day)
{
    int dayNum[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int fst=0; // 1/1 ?????? (month-1)??????????????\?????§?????\??°
    for(int i=0; i<month-1; i++)
        fst += dayNum[i];
    fst += day; // month???1??\?????§?????\??°

    switch(fst % 7) {
        case 0:
            cout << "Wednesday" << endl;
            break;
        case 1:
            cout << "Thursday" << endl;
            break;
        case 2:
            cout << "Friday" << endl;
            break;
        case 3:
            cout << "Saturday" << endl;
            break;
        case 4:
            cout << "Sunday" << endl;
            break;
        case 5:
            cout << "Monday" << endl;
            break;
        case 6:
            cout << "Tuesday" << endl;
            break;
        default:
            break;
    }

}

int main(void)
{
    ios::sync_with_stdio(false);
    int month, day;
    while(1) {
        cin >> month >> day;
        if(month == 0) break;
        getDays(month, day);
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
#include <stack>
using namespace std;

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

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

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;
// << fixed << setprecision
// --------------------------------------
int n;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  int c = 0;
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += abs(a[i]);
    if(a[i] < 0) c++;
  }

  if(c % 2 == 1) {
    ll mi = INF;
    REP(i, n)  {
      mi = min(abs(a[i]), mi);
    }
    sum -= (mi * 2);
  }

  cout << sum << endl;
}

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

#define rep(i,n)  for(int i=0;i<n;++i)
#define sc1(a)  scanf("%d",&a)

int main(){
    bool f=0;
    int n;
    long long a,b=0ll,x=2e9;
    sc1(n);
    rep(i,n){
        scanf("%lld",&a);
        if (a<0) f=f^1;
        b+=abs(a);
        x=min(x,abs(a));
    }
    printf("%lld\n",f?b-x*2:b);
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue;
#define LINF 1e18
#define INF 1e9
using namespace std;

#define fs first
#define sc second

using P = pair<ll,ll>;
using Pll = pair<P,ll>;
using PQ = priority_queue<P,vector<P>,greater<P>>;

signed main(){
    ll x,y;
    cin >> x >> y;
    if(x < 0 && y < 0){
        if(x > y){
            cout << 2 + x - y << endl;
        }
        else{
            cout << y - x << endl;
        }
    }
    else if(x < 0 && y >= 0){
        if(y == 0){
            cout << abs(x) << endl;
        }
        else{
            cout << 1 + abs(abs(x) - abs(y)) << endl;
        }
    }
    else if(x >= 0 && y < 0){
        if(x == 0){
            cout << 1 + abs(y) << endl;
        }
        else{
            cout << 1 + abs(abs(x) - abs(y)) << endl;
        }
    }
    else{
        if(y == 0){
            cout << 1 + x << endl;
        }
        else if(x <= y){
            cout << y - x << endl;
        }
        else{
            cout << 2 + x - y << endl;
        }
    }
    return 0;
}

	#include<bits/stdc++.h>
	#include<ext/pb_ds/assoc_container.hpp>
	#include<ext/pb_ds/tree_policy.hpp>
	#define pi 3.141592653589793238
	#define int long long
	using namespace __gnu_pbds;
	using namespace std;
	template <typename T>
	using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

	map<int,int>dis;

	map<int,vector<pair<int,int> > >mm;
	void bfs(int ver)
	{
		priority_queue<pair<int,int> ,vector<pair<int,int> >,greater<pair<int,int>> >qq;
		qq.push({0,ver});
		while (!qq.empty())
		{
			pair<int,int>pp=qq.top();
			qq.pop();
			for(auto i:mm[pp.second])
			{
				if(dis[i.first]>pp.first+i.second)
				{
					dis[i.first]=pp.first+i.second;
					qq.push({dis[i.first],i.first});
				}
			}
		}
	}

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

	#ifndef ONLINE_JUDGE
		if(fopen("INPUT.txt","r"))
		{
		freopen ("INPUT.txt" , "r" , stdin);
		freopen ("OUTPUT.txt" , "w" , stdout);
		}
	#endif

		int x,y;
		cin>>x>>y;
		mm[x].push_back({-x,1});
		mm[-x].push_back({x,1});
		mm[y].push_back({-y,1});
		mm[-y].push_back({y,1});
		int arr[4];
		arr[0]=x;
		arr[1]=-x;
		arr[2]=-y;
		arr[3]=y;
		for(int i=0;i<4;i++)
		{
			dis[arr[i]]=1e17;
			for(int j=0;j<4;j++)
			{
				if(arr[j]>arr[i])
					mm[arr[i]].push_back({arr[j],arr[j]-arr[i]});
			}
		}
		dis[x]=0;
		bfs(x);
		cout<<dis[y];

	}


#include<stdio.h>

int main(){
	int a,b,x,y,kurang;
	scanf("%d %d %d %d %d", &a,&b,&x,&y,&kurang);
	int temp=((x*60)+y)-((a*60)+b);
	printf("%d\n", temp-kurang);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define lli long long int
# define rep(i,n) for(int i=0;i<n;i++)
void solve(){
   int n;
   cin>>n;
   int a[n];
   rep(i,n)
   cin>>a[i];
   lli sum=0;
   for(int i=1;i<n;i++){
       if(a[i-1]>a[i]){
           sum+=(a[i-1]-a[i]);
           a[i]=a[i-1];
       }
   }
   cout<<sum<<endl;
}
int main()
{
    int t;
   // cin>>t;
    t=1;
    while(t--){
        solve();
    }
}


#include<bits/stdc++.h>

#define int long long

using namespace std;

inline int read() {
	int res=0,fh=1;
	char ch=getchar();
	while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
	if(ch=='-')fh=-1,ch=getchar();
	while(ch>='0'&&ch<='9')res=res*10+ch-'0',ch=getchar();
	return fh*res;
}

const int maxn=4000010;
const int mod=1e9+7;

int fac[maxn],inv[maxn];
inline int fpw(int x,int a){
	int ans=1;
	while(a){
		if(a&1) (ans*=x)%=mod;
		(x*=x)%=mod;
		a>>=1;
	}
	return ans;
}
inline int C(int n,int m){
	return (((fac[m]*inv[n])%mod)*inv[m-n])%mod;
}

#define LL int
#define inv_fac inv
#define Mod mod
#define re register
#define Maxn maxn
inline void pre(){
	fac[0]=1;
	for(int i=1;i<maxn;i++)
		fac[i]=(fac[i-1]*i)%mod;
	inv[maxn-1]=fpw(fac[maxn-1],mod-2);
	for(int i=maxn-2;i>=0;i--)
		inv[i]=(inv[i+1]*(i+1))%mod;
}

int n,k;
int f[2010][2010];
//f[i][j] 表示在这些位置上已经放了i个白球，j种其他颜色的球。(i<j)

signed main(){
	n=read();if((k=read())==1) return puts("1"),0;
	pre();
	memset(f,0,sizeof f);f[0][0]=1;
	for(int i=1;i<=n;i++){
		for(int j=0;j<=i;j++){
			f[i][j]=f[i-1][j];
			if(!j) continue;
			(f[i][j]+=f[i][j-1]*(n-j+1)%mod*C(k-2,n-i+(n-j+1)*(k-1)%mod-1)%mod)%=mod;
		}
	}
	return printf("%lld",f[n][n]%mod),0;
}

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int N = 4e6 + 10, M = 2e3 + 10, MOD = 1e9 + 7;
int fac[N], inv[N], dp[M][M], ps[M][M];

int sum(int a, int b) {return (a + b) % MOD;}
int mul(int a, int b) {return (a * b) % MOD;}
int Pow(int a, int b)
{
    int res = 1;
    for (; b; b>>=1, a = mul(a, a)) if (b & 1) res = mul(res, a);
    return res;
}

int C(int n, int k)
{
    if (k < 0 || n < 0 || k > n) return 0;
    return mul(fac[n], mul(inv[k], inv[n - k]));
}

int32_t main()
{
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i);
    inv[N - 1] = Pow(fac[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1);
    int n, k; cin >> n >> k;
    if (k == 1) return cout << "1\n", 0;
    ps[1][1] = dp[1][1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i][1] = mul(ps[i - 1][1], C(i * k - 2, k - 2));
        for (int j = 2; j <= i; j++) {
            dp[i][j] = mul(ps[i - 1][j - 1], C(i * k - j - 1, k - 2));
        }
        for (int j = i; j >= 1; j--) ps[i][j] = sum(ps[i][j + 1], dp[i][j]);
    }
    cout << mul(ps[n][1], fac[n]) << "\n";
}


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

int main() {
    int N;
    cin >> N;
    vector<int> data(N);
    for (int i=0; i<N; i++) {
        cin >> data.at(i);
    }

    int count = 0;
    while (true) {
        for (int j=0; j<N; j++) {
            if (data.at(j)%2 != 0) {
                cout << count << endl;
                exit(0);
            }
            data.at(j) /= 2;
        }
        count++;
    }
}

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

int main() {
  int N;
  cin >> N;

  int n = 1000000000;
  for (int i = 0; i < N; i++){
  		int A;
    cin >> A;

	int a = 0;

    while ( A % 2 == 0){
      a++;
      A /= 2;
    }

	if(n > a)
      n = a;
  }

  cout << n << endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;

#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MAX=100005;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#define sum(v) accumulate(ALL(v),0)
#define fi first
#define se second

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

template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }

// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
    int len=v.size();
    for(int i=0; i<len; ++i) {
        s<<v[i];
        if(i<len-1) s<<"	";
    }
    return s;
}

// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
    int len=vv.size();
    for(int i=0; i<len; ++i) {
        s<<vv[i]<<endl;
    }
    return s;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<std::setprecision(10);

    ll a,b;
    cin>>a>>b;
    //a　白 b 黒
    VV g(100,Vec(100,0));

    rep(i,50){
        rep(j,100){
            g[i][j] = 1;
        }
    }
    a--;b--;
    REP(i,0,a){
        ll h = i/50;
        ll w = i%50;
        g[h*2][w*2] = 0;
    }

    REP(i,0,b){
        ll h = i/50;
        ll w = i%50;
        g[99-h*2][w*2] = 1;
    }

    cout<<100<<" "<<100<<endl;
    rep(i,100){
        string s;
        rep(j,100){
            s += g[i][j]==1?'#':'.';
        }
        cout<<s<<endl;
    }

    return  0;
}

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

int H,W;
char A[1005][1005];

int dist[1005][1005];
bool visited[1005][1005];
int white_cnt=0;

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

class Node{
public:
    int x,y;
    Node(int a, int b):x(a),y(b){}
};

void input(){
    cin>>H>>W;
    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            cin>>A[i][j];
        }
    }
}

void init(){
    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            dist[i][j]=-1;
        }
    }
    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            visited[i][j]=false;
        }
    }
}

void bfs(){
    init();
    queue<Node> que;
    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            if(A[i][j]=='#'){
                Node temp(j,i);
                que.push(temp);
                visited[i][j]=true;
                dist[i][j]=0;
            }
        }
    }
    while(!que.empty()){
        Node cur=que.front();
        que.pop();
        int cur_x=cur.x;
        int cur_y=cur.y;
        for(int i=0;i<4;++i){
            int next_x=cur_x+dx[i];
            int next_y=cur_y+dy[i];
            bool b=0<=next_x&&next_x<W&&0<=next_y&&next_y<H;
            if(A[next_y][next_x]=='#'||visited[next_y][next_x]||!b) continue;
            visited[next_y][next_x]=true;
            dist[next_y][next_x]=dist[cur_y][cur_x]+1;
            Node next(next_x,next_y);
            que.push(next);
        }
    }
}

int max_search(){
    int ans=0;
    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            ans=ans<dist[i][j]?dist[i][j]:ans;
        }
    }
    return ans;
}

void output(){
    cout<<max_search()<<endl;
}

int main(){
    input();
    bfs();
    output();
}