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

#define ll long long
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false)

const int lmt=3e5+100;
ll a[lmt],n,pre[lmt];

int main(){
	fastio;

	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]=(i%2)?a[i]:-a[i];
	}
	for(int i=1;i<=n;i++){
		pre[i]=pre[i-1]+a[i];
	}
	map<ll,ll>cnt;
	cnt[0]=1;
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans+=cnt[pre[i]];
		cnt[pre[i]]++;
	}
	cout<<ans<<endl;
	return 0;
}

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

typedef long double ld;
const ll MOD=1000000007;
const ll MODA=998244353;
ll vx[4]={0,1,0,-1};
ll vy[4]={1,0,-1,0};
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
long long gcd(long long a,long long b){
    ll gcdmax=max(a,b);
    ll gcdmin=min(a,b);
    while(true){
        if(gcdmax%gcdmin==0)break;
        else gcdmax%=gcdmin;
        swap(gcdmin,gcdmax);
    }
    return gcdmin;
}
ll pow(ll N,ll P,ll M){
    if(P==0)return 1;
    else if(P%2==0){
        ll t=pow(N,P/2,M);
        return t*t%M;
    }
    else return N*pow(N,P-1,M)%M;
}
vector<ll> find_divisor(ll N){
  ll k=1;
  while(k*k<=N){
    k++;
  }
  vector<ll> A(1);
  rep(i,k){
    if(i==1)A.at(0)=1;
    else if(i>=2){
      if(N%i==0)A.push_back(i);
    }
  }
  ll t=0;
  t=A.size();
  rep(i,t){
    if(A.at(t-i-1)*A.at(t-i-1)!=N)A.push_back(N/A.at(t-1-i));
  }
  return A;
}
vector<ll> fac;
vector<ll> finv;
vector<ll> inv;
void COMinit(ll N,ll P){
    rep(i,N+1){
        if(i==0){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else if(i==1){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else{
            fac.push_back(fac.at(i-1)*i%P);
            inv.push_back(P-inv.at(P%i)*(P/i)%P);
            finv.push_back(finv.at(i-1)*inv.at(i)%P);
        }
    }
}

ll COM(ll n,ll k,ll P){
    if(n<k)return 0;
    if(n<0||k<0)return 0;
    return fac.at(n)*(finv.at(k)*finv.at(n-k)%P)%P;
}

struct UnionFind {
    vector<ll> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(ll N) : par(N) { //最初は全てが根であるとして初期化
        for(ll i = 0; i < N; i++) par[i] = i;
    }

    ll root(ll x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(ll x, ll y) { // xとyの木を併合
        ll rx = root(x); //xの根をrx
        ll ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
};

int main(){
    ll N;
    cin>>N;
    vector<ll> A(N);
    rep(i,N){
        ll a;
        cin>>a;
        if(i==0)A.at(i)=a;
        else if(i%2==0)A.at(i)=A.at(i-1)+a;
        else A.at(i)=A.at(i-1)-a;
    }
    sort(A.begin(),A.end());
    ll ans=0;
    ll count=0;
    rep(i,N){
        if(i==0)count++;
        else if(A.at(i)==A.at(i-1)){
            ans+=count;
            count++;
        }
        else{
            count=1;
        }
        if(A.at(i)==0)ans++;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define bug(x) cout << #x << "  >>>>>>>  " << x << '\n'
#define _ << " , " <<
#define Max(a, b) (a > b ? a : b)
#define Min(a, b) (a < b ? a : b)
#define ii pair<int, int>
#define fi first
#define se second
#define UNTIL(t) while (clock() < (t) * CLOCKS_PER_SEC)
#define SZ(a) (int)a.size()
const int MAX = 400100; //2 * 10^5
const int MOD = 1000000007; //10^9 + 7
const int OO = 0x3f3f3f3f; // 0x3f3f3f3f;
const double EPS = 1e-9;  //10^-9
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int score(string str) {
    int s = 0;
    map<char, int> c;
    for(char a : str) c[a]++;
    for(int i = 1; i < 10; i++) {
        int j = i, e = c[i + '0'];
        while(e--) {
            j *= 10;
        }
        s += j;
    }
    return s;
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

    string a, b;
    int k;
    cin >> k >> a >> b;

    map<char, int> cnt;
    for(char i = '0'; i <= '9'; i++)
        cnt[i] = k;

    for(int i = 0; i < 4; i++)
        cnt[a[i]]--;
    for(int i = 0; i < 4; i++)
        cnt[b[i]]--;

    int ganha = 0, tot = 0;

    for(char i = '1'; i <= '9'; i++) {
        for(char j = '1'; j <= '9'; j++) {
            a.back() = i;
            b.back() = j;

            if(!cnt[i] or !cnt[j]) continue;

            int p = cnt[i];
            cnt[i]--;
            p *= cnt[j];
            cnt[i]++;

            if(score(a) > score(b))
                ganha += p;
            tot += p;
        }
    }

    cout << ((1.0 * ganha) / tot) << endl;

    return 0;
}

/*
   Before submit:
      Check the corners cases
      Check solution restrictions

   For implementation solutions:
      Check the flow of the variables

   For intervals problems:
      Think about two pointers

   For complete search:
      Think about cuts

   If you get WA:
      Reread your code looking for stupid typos
      Try various manual testcases
      Recheck the correctness of your algorithm
      Reread the statement
      Write a straightforward solution, create lots of testcases by yourself, and compare both solutions
      Change the coder (if you're with a team)
      Give up. You may have other tasks to solve.
*/

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector< pair< ll,ll> >
#define plli pair< ll,ll >
#define vps vector< pair< string, string> >
#define vpi vector< pair< int, int> >
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define forn(i,a,n) for(ll i=a;i<n;i++)
#define forr(i,n,a) for(ll i=n-1;i>=a;i--)
#define scan(arr,a,n) for(ll i=(a);i<(n);i++)cin>>(arr)[i];
#define print(arr,a,n) for(ll i=(a);i<(n);i++)cout<<(arr)[i]<<" ";

const ll inf = 1e18;

ll add(ll x, ll y) {ll res = x + y; return (res >= mod ? res - mod : res);}
ll mul(ll x, ll y) {ll res = x * y; return (res >= mod ? res % mod : res);}
ll sub(ll x, ll y) {ll res = x - y; return (res < 0 ? res + mod : res);}
ll power(ll x, ll y) {ll res = 1; x %= mod; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, mod - 2);}

int main(){
	fast;
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    #endif
	ll k;
	cin>>k;
	string s, t;
	cin>>s>>t;
	ll tt = 9*k - 8;
	vlli a, b, ca(10, 0), cb(10, 0);

	forn(i, 0, 4){
		a.pb(s[i]-'0');
		ca[a[i]]++;
		b.pb(t[i]-'0');
		cb[b[i]]++;
	}
	ll c = 0, d = 0;
	a.pb(0);
	b.pb(0);
	forn(i, 1, 10){
		if(ca[i]+cb[i]>k){
			cout<<"0.0000000"<<endl;
			return 0;
		}
	}
	forn(i, 1, 10){
		if(ca[i]+cb[i]==k){
			continue;
		}
		a[4]=i;
		ca[i]++;
		forn(j, 1, 10){

			if(cb[j]+ca[j]==k){
				continue;
			}

			b[4]=j;

			ll sa = 0, sb = 0;
			vlli cca = ca, ccb = cb;

			ccb[j]++;

			forn(ii, 1, 10){
				sa+=(ii*pow(10, cca[ii]));
				sb+=(ii*pow(10, ccb[ii]));
			}
			if(sa>sb){
				// print(a, 0, 5);
				// print(b, 0, 5);

				if(i == j){
					ll z = k-(ca[i]-1)-cb[i];
					c+=(z*(z-1));
				} else {
					c+=(k-ca[i]+1-cb[i])*(k-cb[j]-ca[j]);
				}

			}

		}
		ca[i]--;
	}
	d=(tt*(tt-1));
	// cout<<c<<" "<<d<<endl;
	ld ans = (ld)(c)/(ld)(d);
	cout<<fixed<<setprecision(10)<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
struct edge{ll b,c,d;};
int n;
vector<edge>graph[100005];
vector<int>g[100005];
bool v[100005];
void dfs(int cur){
	v[cur] = 1;
	for(int nxt:g[cur]){
		if(!v[nxt]) dfs(nxt);
	}
}
ll ternary(ll t,ll d){
	ll h = sqrt(d);
	ll ret = d/(t+1);
	for(ll i = max((ll)0,h-5); i<=h+5; i++){
		if(t>=i) continue;
		ret = min(ret,i-t+d/(i+1));
	}
	return ret;
}
ll dist[100005];
void dijkstra(int start){
	for(int i=1; i<=n; i++) dist[i] = INT64_MAX;
	dist[start] = 0;
	priority_queue<pi,vector<pi>,greater<pi> >pq;
	pq.push(pi(dist[start],start));
	while(!pq.empty()){
		int cur = pq.top().second;
		ll d = pq.top().first;
		pq.pop();
		if(dist[cur]<d) continue;
		if(cur==n) continue;
		for(edge e : graph[cur]){
			int nxt = e.b;
			ll cc = e.c;
			ll dd = e.d;
			ll w = ternary(d,dd);
			if(dist[nxt]>d+w+cc){
				dist[nxt] = d+w+cc;
				pq.push(pi(dist[nxt],nxt));
			}
		}
	}
}
int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int m; cin>>n>>m;
	for(int i=0; i<m; i++){
		ll a,b,c,d;
		cin>>a>>b>>c>>d;
		graph[a].push_back((edge){b,c,d});
		graph[b].push_back((edge){a,c,d});
		g[a].push_back(b); g[b].push_back(a);
	}
	dfs(1);
	if(!v[n]){
		cout<<-1;
		return 0;
	}
	dijkstra(1);
	cout<<dist[n];
}

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_    }
#define MY_DEBUG(s)      s
#define MY_IS_DEBUG      (true)
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#define MY_IS_DEBUG      (false)
#endif

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

constexpr int IMIN = numeric_limits<int>::min();
constexpr int IMAX = numeric_limits<int>::max();
constexpr ll LLMIN = numeric_limits<ll>::min();
constexpr ll LLMAX = numeric_limits<ll>::max();

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto& i: a)
#define all(x) (x).begin(), (x).end()

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

MY_NAMESPACE(testbed)
int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	vector<vector<tuple<int, ll, ll>>> e(n);
	rep(i, 0, m)
	{
		int a, b;
		ll c, d;
		cin >> a >> b >> c >> d;
		a--;
		b--;
		e[a].push_back({ b, c, d });
		e[b].push_back({ a, c, d });
	}

	auto priority_fn = [](pil const& lhs, pil const& rhs)->bool  // return true to prioritize rhs
	{
		return (rhs.second < lhs.second);
	};

	vector<ll> min_time(n, LLMAX);
	priority_queue<pil, vector<pil>, decltype(priority_fn)> q(priority_fn);
	q.push({ 0, 0 });
	min_time[0] = 0;
	while (!q.empty())
	{
		int this_city;
		ll this_time;
		tie(this_city, this_time) = q.top();
		q.pop();

		if (this_time > min_time[this_city])
			continue;

		fore(edge, e[this_city])
		{
			int next_city = get<0>(edge);
			ll cval = get<1>(edge);
			ll dval = get<2>(edge);

			ll next_time = this_time;
			if (dval == 0)
			{
				next_time += cval;
			}
			else
			{
				ll min_move = LLMAX;
				ll wait_time = max((ll)sqrt((double)dval) - 1LL - this_time, 0LL);
				for (ll adj = -2; adj <= 2; adj++)
				{
					ll this_wait_time = max(wait_time + adj, 0LL);
					ll this_move = this_wait_time + cval + dval / (this_time + this_wait_time + 1LL);
					chmin(min_move, this_move);
				}
				next_time += min_move;
			}
			if (chmin(min_time[next_city], next_time))
			{
				q.push({ next_city, next_time });
			}
		}
	}

	ll ans = min_time[n - 1];
	if (ans == LLMAX)
		ans = -1;
	cout << ans << "\n";
	return 0;
}
MY_NAMESPACE_


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

#define all(x) (x).begin(),(x).end()
#define ll long long
#define ld long double
#define pb push_back
#define FR first
#define SC second
#define MOD 1000000007
#define LLMN LLONG_MIN
#define LLMX LLONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000010
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector< vl >
#define vb vector<bool>
#define vbb vector< vb >
#define vpl vector<pll>
#define vpll vector< vpl >

void solve()
{
    ll x, y, a, b;
    cin>>x>>y>>a>>b;
    ll ans = 0;
    while(1)
    {
        if(x > y / a)
            break;
        if(a * x >= y)
            break;
        if(a * x > x + b)
            break;
        x *= a;
        ans++;
    }
    ans += (y - x - 1) / b;
    cout<<ans;
}

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

#include <bits/stdc++.h>
#include <cstdlib>
#include <math.h>
#include <string>
#include <time.h>
using ld = long double;
using ull = unsigned long long;
using ll = long long;
using namespace std;

int main() {
    //始点(r1, c1)、終点(r2, c2)
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;

    int r = r2 - r1;
    int c = c2 - c1;

    int ans = 3;
    // absは絶対値
    if (!r && !c) {
        ans = 0;
    } else if (r == c || r == -c || abs(r) + abs(c) <= 3) {
        ans = 1;
    } else if ((r ^ c ^ 1) & 1 || abs(r + c) <= 3 || abs(r - c) <= 3 ||
               abs(r) + abs(c) <= 6) {
        ans = 2;
    }
    cout << ans << endl;
}


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

const ll MX = 1e18;
const long double PI = acos(-1);

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

template<class T> inline vector<T> divisor(T n) {
    vector<T> ret;
    for (T i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end());
    return ret;
}

template<class T> inline vector<pair<T, T>> prime_factor(T n) {
    vector<pair<T, T>> ret;
    for (T i = 2; i * i <= n; i++) {
        if (n % i != 0) continue;
        T tmp = 0;
        while (n % i == 0) {
            tmp++;
            n /= i;
        }
        ret.push_back(make_pair(i, tmp));
    }
    if (n != 1) ret.push_back(make_pair(n, 1));
    return ret;
}

int main()
{
    int n; cin >> n;
    vector<int> a(n); rep(i, n){cin >> a[i]; a[i]--;}
    vector<int> b(n); rep(i, n){cin >> b[i]; b[i]--;}
    vector<int> c(n); rep(i, n){cin >> c[i]; c[i]--;}

    vector<int> bc(n); rep(i, n) bc[b[c[i]]] += 1;
    ll count = 0;
    rep(i, n)
    {
        count += bc[a[i]];
    }

    pri(count);
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N], cnt[N], cnt2[N];
//map<int, int> mp, mp2;

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

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

	int n;
	cin >> n;

	for(int i = 1; i <= n; i++){
		cin >> a[i];
		cnt[a[i]]++;
		//mp[a[i]]++;
	}

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

	int x;
	for(int i = 1; i <= n; i++){
		cin >> x;
		cnt2[b[x]]++;
		//mp2[b[x]]++;
	}

	ll ans = 0;
	for(int i = 1; i <= n; i++){
		ans += (cnt[i] * 1LL * cnt2[i]);
	}
	cout << ans << '\n';

	/*
	for(auto i: mp){
		auto it = mp2.find(i.first);
		if(it != mp2.end()){
			ans += (i * (*it)) << '\n';
		}
	}
	*/

	return 0;
}


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

int main(){
  long long t, n, max, flag = 0, i, ans;
  cin >> t >> n;
  max = 100 * n / t + 1;
  i = max;

  while (flag == 0){
    if ((100 + t) * i / 100 - ((100 + t) * (i - 1) / 100) == 2){
      ans = (100 + t) * i / 100 - 1;
      flag = 1;
    }
    i--;
  }
  cout << ans << endl;
  return 0;

}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int A,B;cin>>A>>B;
  if(A>B){
    cout<<(A-B)/2+B<<" "<<(A-B)/2<<endl;
  }
  else{
    cout<<(B-A)/2+A<<" "<<((B-A)/2)-((B-A)/2)*2<<endl;
  }
}

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

#define fastio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

template <typename Arg1>
void prn(Arg1&& arg1) { cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); }

template <typename Arg1>
void prs(Arg1&& arg1) { cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); }

template <typename Arg1>
void read(Arg1&& arg1) { cin>>arg1; }
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); }

#define int                     long long
#define all(ds)                 ds.begin(), ds.end()
#define uniq(v)                 (v).erase(unique(all(v)),(v).end())
#define gcd(x,y)                __gcd(x,y)
#define rep(i,a,b)              for(int i=a;i<b;i++)
#define precise(x)              cout<<fixed<<setprecision(x)
#define endl                    "\n"

const long long INF = 1e18;
const int32_t MOD = 1e9+7;
const int N = 1e5+5;

int n, m, k, q, r, l, x, y, z;
int a[N], b[N], c[N];
string s, t;
int ans = 0;

void solve(){
    read(n,m,q);
    pair<int, int> a[n]; // val, wt
    rep(i,0,n){
        read(x,y);
        a[i] = {y,x};
    }
    sort(a, a+n);
    reverse(a, a+n);

    int c[m];
    rep(i,0,m){
        read(c[i]);
    }

    rep(i,0,q){
        read(l,r);
        l--, r--;
        vector<int>v;
        for(int i = 0; i < m; ++i){
            if(i >= l && i <= r) continue;
            v.push_back(c[i]);
        }
        sort(all(v));
        int ans = 0;
        for(int i = 0; i < n; ++i){
            int val = a[i].first, wt = a[i].second;
            for(int i = 0; i < v.size(); ++i){
                if(v[i] == -1) continue;
                if(wt <= v[i]){
                    ans += val;
                    v[i] = -1;
                    break;
                }
            }
        }
        prn(ans);
    }
}

signed main()
{
#ifndef ONLINE_JUDGE
    input_output
#else
    fastio
#endif

#ifdef SIEVE
    sieve();
#endif

#ifdef NCR
    init();
#endif

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

    return 0;
}


//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())

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

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

template<class t> using vc=vector<t>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
#define sz 1
ll F[sz],R[sz];
void make(){
	F[0] = 1;
	for(int i=1;i<sz;i++) F[i] = F[i-1]*i%mod;
	for(int i=0;i<sz;i++) R[i] = modpow(F[i],mod-2);
}
ll C(int a,int b){
	if(b < 0 || a < b) return 0;
	return F[a]*R[b]%mod*R[a-b]%mod;
}
void solve(){
	unordered_set<int>ex;
	int n, m;
	cin >> n >> m;
	map<int, vc<int>>M;
	set<P>black;

	rep(i, m){
		int x,y; cin >> x >> y;
		M[x].pb(y);
		black.insert(mp(x, y));
	}
	ex.insert(n);
	for(auto at:M){
		vc<int>v = at.b;
		vc<int>zan;
		for(auto k:v){
			for(int a=k-1;a<=k+1;a++){
				if(ex.find(a) != ex.end()){
					zan.pb(a);
					ex.erase(ex.find(a));
				}
			}
		}
		for(auto a:zan){
			if(black.find(mp(at.a, a)) == black.end()) ex.insert(a);
			if(black.find(mp(at.a, a-1)) != black.end()) ex.insert(a-1);
			if(black.find(mp(at.a, a+1)) != black.end()) ex.insert(a+1);
		}
	}
	o(ex.size());
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}

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

using namespace std;

int main() {
    long long n, c;
    cin >> n >> c;

    vector<long long> ai(n), bi(n), ci(n);
    for (int i = 0; i < n; i++) {
        cin >> ai[i] >> bi[i] >> ci[i];
    }

    map<long long, long long> m;
    for (int i = 0; i < n; i++) {
        m[ai[i]] += ci[i];
        m[bi[i] + 1] -= ci[i];
    }

    vector<long long> d(0), s(0);
    for (map<long long, long long>::iterator i = m.begin(); i != m.end(); i++) {
        d.push_back(i->first);
        s.push_back(i->second);
    }

    for (int i = 1; i < static_cast<int>(d.size()); i++) {
        s[i] += s[i - 1];
    }

    long long res = 0;
    for (int i = 1; i < static_cast<int>(d.size()); i++) {
        res += (d[i] - d[i - 1]) * min(s[i - 1], c);
    }

    cout << res << endl;
}


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

int main(){
    int count_all_str;
    string search_str;
    string tmp;
    string new_str;
    cin >> count_all_str >> search_str;
    for(int i = 1;i <= count_all_str;i++){
        cin >> tmp;
        if(tmp != search_str){
            new_str += tmp;
            new_str += " ";
        }
        int size = new_str.size();
        if(i == count_all_str && size != 0){
            int size = new_str.size();
            new_str = new_str.replace(size - 1, 1, "");
        }
    }
    cout << new_str << endl;
}

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

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

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

    vector<int> ans;

    rep(i,n){
        if(a[i]!=x){
            ans.push_back(a[i]);
        }
    }

    rep(i,ans.size()){
        if(i==ans.size()-1){
            cout << ans.at(i);
        }
        else{
            cout << ans.at(i) << ' ';
        }
    }
    cout<< endl;

}


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

int main() {
    int N,S,D;
    int A=0;
    vector<int> X(N);
    vector<int> Y(N);
    vector<int> CX(N);
    vector<int> CY(N);
    cin >> N >> S >> D;
    for (int i=0; i<N; i++){
        cin >> X.at(i) >> Y.at(i);
    }
    for (int i=0; i<N; i++){
        CX.at(i) = S - X.at(i);
        CY.at(i) = D - Y.at(i);
    }
    for (int i=0; i<N; i++){
        if(CX.at(i)>0 && CY.at(i)<0 ){
            A = 1;
            cout << "Yes" << endl;
            break;
        }
    }
    if (A==0){
        cout << "No" << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define MP make_pair
#define MT make_tuple
typedef int64_t ll;
#define PA pair<ll,ll>
#define TU tuple<ll,ll,ll>
#define vi vector<ll>
#define vii vector<vector<ll> >
#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define rep2(i,m,n) for(ll (i)=(m); (i)<(ll)(n); (i)++)
const ll INF = 1ll<<30;
const ll INF2 = 1ll<<32;

int main() {
  ll n;
  string s,x;

  cin >>n>>s>>x;
  vi v(n);
  rep(i,n) v.at(i) = s.at(i)-'0';

  //iターン終了時にmod7でjと合同であるとき高橋くんが勝つならtrue
  vector<vector<bool>> dp(n+1,vector<bool>(7,false));
  dp.at(n).at(0) = true;

  vi mod_pow_10 = {1,3,2,6,4,5};
  //mod_pow_10.at((n-1-i)%6)

  for(ll i=n-1; i>=0; i--) {
    rep(j,7) {
      if(x.at(i)=='T') {
        if( dp.at(i+1).at((ll)(j%7)) ||
           dp.at(i+1).at((ll)((j+v.at(i)* mod_pow_10.at((n-1-i)%6) )%7)) ) dp.at(i).at(j) = true;
      }
      if(x.at(i)=='A') {
        if( dp.at(i+1).at((ll)(j%7)) &&
           dp.at(i+1).at((ll)((j+v.at(i)* mod_pow_10.at((n-1-i)%6) )%7)) ) dp.at(i).at(j) = true;
      }
    }
  }

  if(dp.at(0).at(0)) cout <<"Takahashi" << endl;
  else cout <<"Aoki" << endl;

}


#include<bits/stdc++.h>
using namespace std;
#define en      '\n'
#define endl    '\n'
#define ll      long long signed
#define pb      push_back
#define append  push_back
#define all(v)  (v).begin(),(v).end()
#define FOR(i,a,b,k) for(int i=a; k>0?i<b:i>b; i+=k)
#define fori(a) for(int i=0; i<a; i++)
#define forj(a) for(int j=0; j<a; j++)
#define fork(a) for(int k=0; k<a; k++)
#define MOD     1000000007
#define MAX     1000010
#define mset(a,s,k) memset(a,k,s*sizeof(a[0]))
#define ff      first
#define ss      second
#define __      <<" "<<
#define _       <<en

ll pow_(ll a,ll e,ll m=MOD){ll r=1;while(e){if(e&1)r=r*a%m;a=a*a%m;e>>=1;}return r;}
int inv(ll a) {return int(pow_(a,MOD-2,MOD));}int add(ll a, ll b){return (a+b+MOD)%MOD;} int mul(ll a, ll b)
{return (MOD+a*b%MOD)%MOD;}int di(ll a, ll b){return mul(a,inv(b));}ll  ceil(ll a,ll b){return a/b+(a%b!=0);}
template<typename T1,typename T2>ostream&operator<<(ostream&O,pair<T1,T2>a){O<<"( ";O<<a.ff<<", "<<a.ss;O<<" )";return O;}
template<typename T>ostream&operator<<(ostream&O,vector<T>V){O<<"[ ";fori(V.size())O<<V[i]<<(i==V.size()-1?"":", ");O<<" ]";return O;}
template<typename T>ostream&operator<<(ostream&O,set<T>S){O<<"{ ";for(T i:S)O<<i<<", ";O<<" }";return O;}
template<typename T1,typename T2>ostream&operator<<(ostream&O,map<T1,T2>S){O<<"{ ";for(auto i:S)O<<i<<", ";O<<" }";return O;}
template<typename T=int>class print{public:vector<T>V;print(vector<T>v){V=v;cout<<" <"<<V<<"> ";}
print(T*x,int N){fori(N)V.push_back(x[i]);cout<<" <"<<V<<"> ";}friend ostream&operator<<(ostream&O,print p){return O;}};

#define vi vector<int>
#define tp pair<int,int>

ll A[MAX],B[MAX], vis[MAX]; int N,M;
void reset();
vector<int> G[MAX];

ll dfs(int x){
    if(vis[x]) return 0;
    vis[x]=1;
    ll r=0;
    for(auto i: G[x]){
        r+=dfs(i);
    }
    return r+A[x]-B[x];
}

int solve(){
    cin>>N>>M;
    reset();
    fori(N){ cin>>A[i];}
    fori(N){ cin>>B[i];}
    fori(M){
        int a,b; cin>>a>>b; a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    fori(N)
        if(dfs(i))
            return 0;
    return 1;
}

void reset(){

    fori(N) G[i].clear();
    fori(N) vis[i]=0;
    // Clear Vectors, maps, sets
    // Set default values of arr
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    while(t--){
        // solve();
        // cout<<solve()<<en;
        cout<<(solve()?"Yes":"No")<<endl;
    }
    return 0;
}

#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
const ll MAXN = 400011;

struct fhqTreap {
	ll tot, rt;
	ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN];

	void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];}

	void split(ll u, ll k, ll &x, ll &y) {
		if(!u) {x = y = 0; return;}
		if(val[u] <= k) x = u, split(rs[u], k, rs[u], y);
		else y = u, split(ls[u], k, x, ls[u]);
		pushup(u);
	}

	ll merge(ll u, ll v) {
		if(!u || !v) return u | v;
		if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;}
		else {ls[v] = merge(u, ls[v]); pushup(v); return v;}
	}

	ll add(ll k) {
		rd[++tot] = rand();
		sz[tot] = 1;
		val[tot] = sum[tot] = k;
		return tot;
	}

	void insert(ll k) {
		ll x, y;
		split(rt, k, x, y);
		rt = merge(merge(x, add(k)), y);
	}

	ll cb(ll k) {
		ll x, y;
		split(rt, k, x, y);
		ll ans = sz[x] * k + sum[y];
		rt = merge(x, y);
		return ans;
	}

	void delet(ll k) {
		ll x, y, z;
		split(rt, k, x, z);
		split(x, k - 1, x, y);
		y = merge(ls[y], rs[y]);
		rt = merge(merge(x, y), z);
	}
} t[2];

ll q;
ll len[2];
ll w[2][MAXN];

ll read() {
	ll X = 0, F = 1; char C = getchar();
	while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();}
	while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();}
	return X * F;
}

int main() {
	ll ans = 0;
	len[1] = read(); len[0] = read(); q = read();
	for(ll i = 0; i < 2; ++i)
		for(ll j = 1; j <= len[i]; ++j)
			t[i].rt = t[i].merge(t[i].rt, t[i].add(0));
	for(ll i = 1, opt, x, y; i <= q; ++i) {
		opt = (read() & 1); x = read(); y = read();
		t[opt].delet(w[opt][x]);
		t[opt].insert(y);
		ans -= t[opt^1].cb(w[opt][x]);
		w[opt][x] = y;
		ans += t[opt^1].cb(w[opt][x]);
		printf("%lld\n", ans);
	}
	return 0;
}


#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c, k;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

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

ll modinv(ll a, ll mod){
  ll b=mod, u=1, v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a, b);
    u-=t*v; swap(u, v);
  }
  u%=mod;
  if(u<0)u+=mod;
  return u;
}

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

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

struct dijkstra{
  ll N;
  llvec d;
  vector<vector<edge>> e;
  dijkstra(ll n){
    N = n;
    //d = llvec(N, 1e18);
    e = vector<vector<edge>>(N);
  }

  void add_edge(ll from, ll to, ll cost, ll k){
    e[from].push_back({to, cost, k});
  }

  void run(ll start){
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    d = llvec(N, 1e18);
    d[start]=0;
    while(!que.empty()){
      P q = que.top();que.pop();
      ll dc = q.first;
      ll x = q.second;
      if(dc>d[x]){
        continue;
      }else{
        for(auto ip: e[x]){
          if(d[ip.x]<=((d[x]+ip.k-1)/ip.k) * ip.k +ip.c){
            continue;
          }else{
            d[ip.x]= ((d[x]+ip.k-1)/ip.k) * ip.k +ip.c;
            que.push({d[ip.x], ip.x});
          }
        }
      }
    }
  }
};

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N, M, X, Y;
  cin >> N >> M >> X >> Y;
  dijkstra dijk(N);
  X--;Y--;
  rep(i, M){
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;b--;
    dijk.add_edge(a, b, c, d);
    dijk.add_edge(b, a, c, d);
  }

  dijk.run(X);
  ll ans = dijk.d[Y];
  if(ans>1e15)ans=-1;
  cout << ans<<endl;

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define ll long long
#define PI 3.1415926535897932384626
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define br printf("\n")
#define fo(i, n) for(i=0;i<n;i++)
#define Fo(i, k, n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define deb(x) cout << #x << " = " << x << endl;
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define deba(i, a, n) fo(i, n){cout << a[i] << " ";}
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define trr(it, x) for(auto it = x.rbegin(); it != x.rend(); it+)
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
    uniform_int_distribution<int> uid(0,lim-1);
    return uid(rang);
}
const int mod = 1000000007;
const int N = 3e5;
bool comp(int a, int b)
{
    return (a < b);
}

int getIndex(vector<int> v, int K)
{
    auto it = find(v.begin(), v.end(), K);
    if (it != v.end())
    {
        int index = it - v.begin();
        return index;
    }
    else {
        return -1;
    }
}
/* ========== SOLUTION ========= */

void solution() {
	string s;
	cin>>s;
	reverse(s.begin(), s.end());
	for (int i = 0; i < s.length(); ++i)
	{
		if (s[i] == '6')
		{
			s[i] = '9';
		}
		else if (s[i] == '9')
		{
			s[i] = '6';
		}
	}
	cout<<s;
}
/* ========== SOLUTION ========= */
int main() {

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    // freopen("input.txt", "r", stdin);
    // freopen("error.txt", "w", stderr);
    // freopen("output.txt", "w", stdout);

    solution();
    cout<<"\n";

    // int t=1;
    // cin>>t;
    // while(t--) {
    //     solution();
    //     cout<<endl;
    // }
    return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>
#include<tuple>
#include<time.h>
#include<list>

#define DIV 998244353
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
	string s; cin >> s;
	vector<int> cnt(10);
	int len = s.size();
	for (int i = 0; i < len; i++) {
		cnt[s[i] - '0']++;
	}

	if (len == 1) {
		if (cnt[8] == 1) cout << "Yes" << endl;
		else cout << "No" << endl;
	}

	else if (len == 2) {
		bool flag = false;
		for (int i = 0; i < 100; i += 8) {
			vector<int> ned(10);
			int temp = i;
			ned[temp % 10]++;
			temp /= 10;
			ned[temp % 10]++;
			bool hachi = true;
			for (int j = 0; j <= 9; j++) {
				if (cnt[j] < ned[j]) {
					hachi = false;
					break;
				}
			}

			if (hachi) {
				flag = true;
				break;
			}
		}

		if (flag) cout << "Yes" << endl;
		else cout << "No" << endl;
	}

	else {
		bool flag = false;
		for (int i = 0; i <= 1000; i += 8) {
			vector<int> ned(10);
			int temp = i;
			ned[temp % 10]++;
			temp /= 10;
			ned[temp % 10]++;
			temp /= 10;
			ned[temp % 10]++;
			bool hachi = true;
			for (int j = 0; j <= 9; j++) {
				//printf("ned[%d]=%d\n", j, ned[j]);
				if (cnt[j] < ned[j]) {
					hachi = false;
					break;
				}
			}

			if (hachi) {
				flag = true;
				//cout << "i=" << i << endl;
				break;
			}
		}

		if (flag) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}


#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
    #include <atcoder/all>
    using namespace atcoder;
#endif

using namespace std;
using Graph = vector<vector<int>>;
using Grid= vector<string>;
using vin= vector<int>;
using ll=long long;
using ull=unsigned long long;
using vll= vector<ll>;
using vbl=vector<bool>;
using vch=vector<char>;
using P=pair<int ,int>;
//const long double pi=acos(-1);
#define ft first
#define sd second
#define fn front
#define pb push_back
#define eb emplace_back
#define it insert
#define si(v) int((v).size())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i,n) for (ll i=0; i< (ll)(n); i++)
#define sot(x) sort(x.begin(), x.end())
#define rese(x) reverse(x.begin(), x.end())
#define vnn(x,y,s,name) vector<vector<int>> name(x, vector<int>(y,s))
#define mse(x) memset(x, 0, sizeof(x))
#define mii(x,y,z) min(x,min(y,z))
#define maa(x,y,z) max(x,max(y,z))
#define cps CLOCKS_PER_SEC
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define cset(x) cout<<fixed<<setprecision(x)
//const int INF=1001001001;
//998244353  1000000007

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vin x(n),y(n);
    rep(i,n) cin>>x[i]>>y[i];
    int cnt=0;
    rep(i,n){
        for(int j=i+1;j<n;j++){
            if(abs((y[j]-y[i]))<=abs((x[j]-x[i]))) cnt++;
        }
    }
    cout<<cnt<<endl;
}

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

const int N = 2010;

int X[N], Y[N];

int main() {
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &X[i], &Y[i]);
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++) if (X[i] != X[j]) {
            double k = (Y[j] - Y[i]) / (double)(X[j] - X[i]);
            if (-1 <= k && k <= 1) res++;
        }
    printf("%d\n", res);
}


//
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//
//#pragma GCC optimize(3,"Ofast","inline")
//#include<unordered_map>
//#include<unordered_set>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<stack>
#include<vector>
#include<sstream>
#include<list>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int mod=104857601;
const int maxn=2e6+5;
int k[maxn];
int main()
{
    int n,a,b,ma=0,mb=0;
    cin>>n;
    n=1<<n;
    for(int i=1;i<=n;i++)
    {
        cin>>k[i];
    }
    n/=2;
    for(int i=1;i<=n;i++)
    {
        if(ma<k[i])
        {
            ma=k[i];
            a=i;
        }
    }
    n*=2;
    for(int i=n/2+1;i<=n;i++)
    {
        if(mb<k[i])
        {
            mb=k[i];
            b=i;
        }
    }
    if(k[a]>k[b])cout<<b;
    else cout<<a;
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define PI 2*acos(0.0)
using namespace std;

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

int main()
{
    optimize();

    ll n;
    cin >> n;
    ll m=pow(2,n);
    ll ar[m];
    for(int i=0; i<m; i++)
        cin >> ar[i];
    ll mx1=*max_element(ar,ar+(m/2));
    ll mx2=*max_element(ar+(m/2),ar+m);
    ll k=min(mx1,mx2);
    for(int i=0; i<m; i++)
        if(ar[i]==k)
    {
        cout << i+1 << endl;
        break;
    }
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
typedef long long ll;
int main() {
	char s,t;
	cin >> s>> t;
	char ans;
	if (s == 'Y') {
		ans = t - ('a' - 'A');
		cout << ans << endl;
	}
	if(s == 'N')
		cout << t << endl;
	return 0;
}

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimization ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include<climits>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>
#include <chrono>
using namespace std;
#define ld long double
#define int long long
#define pii pair<int,int>
#define pb push_back
#define endl '\n'
#define pi acos(-1)

#define init(x,a) memset(x,a,sizeof(x))
#define all(c) c.begin(),c.end()
int const mod1 = 998244353;
int const mod = 1e9 + 7;
int const hashmod = 1610612741;
#define bitcount(x)       __builtin_popcountll(x)
#define l first
#define r second
const int inf = LLONG_MAX;
int toint(const string &s) {stringstream ss; ss << s; int x; ss >> x; return x;}
string tostring ( int number ) {stringstream ss; ss << number; return ss.str();}
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define haha ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
               os; */

struct cmp
{
	bool operator() (pii a, pii b) {
		return (a.l < b.l);
	}
};

int modexp(int x, int n) {
	if (n == 0) {
		return 1;
	}
	else if (n & 1) {
		return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
	}
	else {
		return (modexp((x * x) % mod, n / 2) % mod);
	}
}

int modinv(int n)
{
	return modexp(n, mod - 2) % mod;
}

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

bool comp2(pair<pii, int> a, pair<pii, int> b) {

	if (a.l.l == b.l.l)
		return a.r < b.r;

	return (a.l.l < b.l.l );
}
bool comp1(pii a, pii b) {

	return a.l < b.l;
}

signed main() {
	haha
#ifndef ONLINE_JUDGE

	freopen("inp3.txt", "r", stdin);

	freopen("out2.txt", "w", stdout);
#endif
	char c, t;
	cin >> c >> t;
	if (c == 'Y')
	{
		cout << (char)toupper(t);

	}
	else
		cout << (char)t;

}


#include <queue>
#include <vector>
#include <iostream>
#include <utility>
using namespace std;
using ll = int64_t;
using pll = pair<ll,ll>;

int main() {
  ll a,b,X,Y;
  cin >> a >> b >> X >> Y;
  a--; b--;
  // 0-99: A
  // 100-199: B
  vector<vector<pll>> E(200, vector<pll>{});
  for(ll i=0; i<100; i++) {
    E[i].push_back(make_pair(100+i, X));
    E[100+i].push_back(make_pair(i, X));
  }
  for(ll i=0; i<99; i++) {
    E[i+1].push_back(make_pair(100+i, X));
    E[i+100].push_back(make_pair(1+i, X));
  }
  for(ll i=0; i<99; i++) {
    E[i].push_back(make_pair(i+1, Y));
    E[i+1].push_back(make_pair(i, Y));
    E[100+i].push_back(make_pair(101+i, Y));
    E[101+i].push_back(make_pair(100+i, Y));
  }

  vector<int> SEEN(200, false);
  priority_queue<pll,vector<pll>,greater<pll>> Q;
  Q.push(make_pair(0, a));
  while(!Q.empty()) {
    pll dx = Q.top(); Q.pop();
    ll x = dx.second;
    if(SEEN[x]) { continue; }
    SEEN[x] = true;
    if(x==100+b) {
      cout << dx.first << endl;
      return 0;
    }
    for(pll e : E[x]) {
      Q.push(make_pair(dx.first+e.second, e.first));
    }
  }
}

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
	int a, b, x, y;
	cin >> a >> b >> x >> y;
	int ans = 0;
	if(a == b){
		printf("%d\n",x);
	}else if(a < b){
		ans += x;
		ans += min(2 * x, y) * (b - a);
		printf("%d\n",ans);
	}else{
		ans += x;
		ans += min(2 * x, y) * (a - b - 1);
		printf("%d\n",ans);
	}

	return 0;
}


#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;

const long mod=1000000007;
long long modinv(long long a,long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main(){
    long N,M,tmp,ans;
    ans=1;
    cin>>N>>M;
    long S=0;
    for(int i=0;i<N;++i){
        cin>>tmp;
        S+=tmp;
    }
    for(long i=1;i<=S+N;++i){
        ans=(((ans*(M+N+1-i))%mod)*modinv(i,mod))%mod;
    }
    if(ans<0){
        ans+=mod;
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

    vector<string> g(n);
    for (int i = 0; i < n; ++i) cin >> g[i];

    int ans = 0;
    for (int i = 1; i < n; ++i)
        for (int j = 1; j < m; ++j) {
            int cnt = 0;
            cnt += g[i][j] == '#';
            cnt += g[i - 1][j] == '#';
            cnt += g[i][j - 1] == '#';
            cnt += g[i - 1][j - 1] == '#';

            if (cnt & 1) ans++;
        }

    cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
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; }

int N;
vc<int> x, y;

int chedis(int &n1, int &n2) {
    return max(abs(x[n1] - x[n2]), abs(y[n1] - y[n2]));
}

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

    int N;
    cin >> N;
    x.resize(N); y.resize(N);
    rep (i, N) cin >> x[i] >> y[i];

    vc<int> x2(N), y2(N);
    rep (i, N) x2[i] = i;
    rep (i, N) y2[i] = i;

    sort(all(x2), [](int &n1, int &n2) {
        return x[n1] < x[n2];
    });
    sort(all(y2), [](int &n1, int &n2) {
        return y[n1] < y[n2];
    });

    vc<IP> array;
    array.push_back({min(x2[0], x2[N-1]), max(x2[0], x2[N-1])});
    array.push_back({min(x2[0], x2[N-2]), max(x2[0], x2[N-2])});
    array.push_back({min(x2[1], x2[N-1]), max(x2[1], x2[N-1])});
    array.push_back({min(y2[0], y2[N-1]), max(y2[0], y2[N-1])});
    array.push_back({min(y2[0], y2[N-2]), max(y2[0], y2[N-2])});
    array.push_back({min(y2[1], y2[N-1]), max(y2[1], y2[N-1])});

    sort(all(array), [](IP &p1, IP &p2) {
        return chedis(p1.first, p1.second) > chedis(p2.first, p2.second);
    });

    if (array[0] == array[1]) cout << chedis(array[2].first, array[2].second) << endl;
    else cout << chedis(array[1].first, array[1].second) << endl;
}

#include<bits/stdc++.h>

using namespace std;
int b[2000000] = {0};
struct node{
	int x, y, id;
};
node a[200010];
bool cmp1(node x, node y) {
	return x.x < y.x;
}
bool cmp2(node x, node y) {
	return x.y < y.y;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d%d", &a[i].x, &a[i].y);
		a[i].id = i;
	}
	int tot = 0;
	sort(a+1, a+1+n, cmp1);
	for (int i = 1; i <= n; i++) {
		if (i < n && abs(a[i].x-a[n].x)>abs((a[i].y-a[n].y))) {
			b[tot++] = abs(a[i].x-a[n].x);
		}
		if (i < n-1 && abs(a[i].x-a[n-1].x)>abs((a[i].y-a[n-1].y))) {
			b[tot++] = abs(a[i].x-a[n-1].x);
		}
	}
	sort(a+1, a+1+n, cmp2);
	for (int i = 1; i <= n; i++) {
		if (i < n && abs(a[i].y-a[n].y) >= abs((a[i].x-a[n].x))) {
			b[tot++] = abs(a[i].y-a[n].y);
		}
		if (i < n-1 && abs(a[i].y-a[n-1].y) >= abs((a[i].x-a[n-1].x))) {
			b[tot++] = abs(a[i].y-a[n-1].y);
		}
	}
	sort(b,b+tot);
	printf("%d\n", b[tot-2]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
	string res = "{";
	for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
	return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
struct Mint {
	int val;
	Mint() { val = 0; }
	Mint(const ll& x) {
		val = (-MOD <= x && x < MOD) ? x : x % MOD;
		if (val < 0) val += MOD;
	}
	template <typename U>
	explicit operator U() const { return (U)val; }
	friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
	friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
	friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
	Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
	friend Mint modex(Mint a, ll p) {
		assert(p >= 0);
		Mint ans = 1;
		for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
	Mint& operator++() { return *this += 1; }
	Mint& operator--() { return *this -= 1; }
	Mint operator++(int) { Mint result(*this); *this += 1; return result; }
	Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
	Mint operator-() const { return Mint(-val); }
	friend Mint operator+(Mint a, const Mint& b) { return a += b; }
	friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
	friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
	friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
	friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
	friend string to_string(const Mint& b) { return to_string(b.val); }
};
int main() {
	int n;
	scanf("%d", &n);
	vector<string> C(2, string(2, '_'));
	for (int i = 0; i < 2; ++i)
		for (int j = 0; j < 2; ++j)
			scanf(" %c", &C[i][j]);
	if (C[0][1] == 'B') {
		auto D = C;
		int sum = (int)'A' + 'B';
		D[0][0] = sum - C[1][1];
		D[1][1] = sum - C[0][0];
		D[0][1] = sum - C[0][1];
		D[1][0] = sum - C[1][0];
		swap(D, C);
	}
	assert(C[0][1] == 'A');
	Mint ans = 0;
	if (n == 2) ans = 1;
	else if (C[0][0] == 'A') ans = 1;
	else if (C[1][0] == 'B') ans = modex(Mint(2), n - 3);
	else {
		Mint a = 1, b = 0;
		for (int i = 1; i + 1 < n; ++i) {
			Mint na = a + b;
			Mint nb = a;
			swap(na, a);
			swap(nb, b);
		}
		ans += a;
	}
	printf("%d\n", ans.val);
}

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
//#define MOD 998244353LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける
const ll llMAX=9223372036854775807LL;
const ll llMIN=-9223372036854775808LL;

void myprint1D(vi &data)
{
  REP(i,data.size())
    cout<<data[i]<<" ";
  cout<<endl;
}

//配列を[y][x]で表示
void myprint2D_T(vvi &data)
{
  REP(i,data.size())
    myprint1D(data[i]);
}

//配列を[x][y]で表示
void myprint2D(vvi &data)
{
  ll l1=data.size();
  ll l2=data[0].size();
  REP(j,l2){
    REP(i,l1)
      cout<<data[i][j]<<" ";
    cout<<endl;
  }
}

//print(a,b...)って使い方
void print1(ll a){cout<<a<<endl;}
void print2(ll a,ll b){cout<<a<<" "<<b<<endl;}
void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;}
void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;}
void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;}
//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
	ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
	if (t==0LL) return a/b;
		for(int i=0;i<900;i++){
      m=x0/x1;
      x2=x0-x1*m;
      n2=(n0-m*n1)%c;
      if (x2==1LL){
        ans=(n2+c)%c;
        break;
      }
      x0=x1;x1=x2;
      n0=n1;n1=n2;
    }
	return (a+((t*ans)%c)*b-t)/b;
}
//剰余の割ざん

//aのn乗 mod c
ll expmod(ll a,ll n,ll c){
  ll ans=1LL,aa=a,beki=n;
  for(int i=0;i<64;i++){
    if (beki%2==1LL) ans=ans*aa%c;
    aa=aa*aa%c;
    beki/=2LL;
    if (beki==0LL)break;
  }
  return ans;
}

//nCk
vi mulmod(500001);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
  mulmod[0]=1LL;
  for(ll i=1;i<500001;i++){
    mulmod[i]=mulmod[i-1]*i%MOD;
  }
}
ll nCk(ll nn,ll kk){
  return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}

ll modinv(ll a,ll b){
  return moddiv(1,a,b);
}

ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){
  __int128_t tmp=(br-ar)%b*modinv(a,b)%b;
  if (tmp<0)tmp+=b;
  return ar+tmp*a;
}

ll n;
string saa,sab,sba,sbb;

ll fib(ll nn){
  nn-=3;
  ll a0=1;
  ll a1=1;
  ll ret=1;
  for(ll nnn=0;nnn<nn;nnn++){
    a0=a1;
    a1=ret;
    ret=(a1+a0)%MOD;
  }
  return ret;
}

int main(){
  ll ans=0;
  cin >> n;

  cin>>saa>>sab>>sba>>sbb;
  if (sab=="A"){
    if (saa=="A"){
      cout<<1<<endl;
      return 0;
    }else{
      if (sba=="A"){
        ans=fib(n);
        cout<<ans<<endl;
        return 0;
      }else{
        if (n<=3){cout<<1<<endl;return 0;}
        ans=expmod(2,n-3,MOD);
        cout<<ans<<endl;return 0;
      }
    }
  }else{
    if (sbb=="B"){
      cout<<1<<endl;
      return 0;
    }else{
      if (sba=="B"){
        ans=fib(n);
        cout<<ans<<endl;
        return 0;
      }else{
        if (n<=3){cout<<1<<endl;return 0;}
        ans=expmod(2,n-3,MOD);
        cout<<ans<<endl;return 0;
      }
    }

  }

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

*/


#include<bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define OUT(V) for(int i=0;i<(int)V.size();++i){cout<<V[i]<<" ";}cout<<endl;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int main() {
    int N;
    cin >> N;
    vector<set<int>> s(N + 10);
    vector<int> Ans(N + 10);
    Ans[1] = 1;
    for(ll i = 1;i <= N;++i) {
        if(i != 1) {
            for(int j = 2;;j++) {
                if(!s[i].count(j)) {
                    Ans[i] = j;
                    break;
                }

            }
        }
        for(ll j = 1;j * i <= N;++j) {
            s[i * j].insert(Ans[i]);
        }

    }
    for(int i = 1;i <= N;++i) {
        cout << Ans[i];
        if(i != N) {
            cout << " ";
        } else {
            cout << endl;
        }
    }
}

#include <bits/stdc++.h>
#define f(i,j,k) for(long long i=j;i<k;i++)
#define f2(i,j,k) for(long long i=j;i>=k;i--)
#define ll long long
using namespace std;
const long long mod=1e9+7;
const long long mod2=998244353;
const long long INF = (1 << 29);
void chmin(int& a, int b){ if(a > b) a = b; }
void chmax(int& a, int b){ if(a < b) a = b; }
void answer(ll a){
    if(a==0){
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
    }
}
int main(){
    string s,t="";
    cin>>s;
    ll now=0;
    f(i,0,s.size()){
        if(s[i]=='R'){
            now++;
        }
        else if(s[i]!='R'&&now%2==0){
            if(t.back()==s[i]){
                t.pop_back();
            }
            else{
                t.push_back(s[i]);
            }
        }
        else if(s[i]!='R'){
            if(t[0]==s[i]){
                t.erase(t.begin());
            }
            else{
                t.insert(t.begin(),s[i]);
            }
        }
    }
    if(now%2!=0){
        reverse(t.begin(),t.end());
    }
    cout<<t<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <cassert>
#include <optional>
#include <utility>
#include <vector>
// #include <atcoder/all>

template <class InputIterator>
std::ostream& range_output(std::ostream& os_arg, InputIterator first_arg, InputIterator last_arg){ if(first_arg != last_arg){ do{ os_arg << *(first_arg++); if(first_arg == last_arg) break; os_arg << ' '; } while(true); }  return os_arg; }
template <class Tp> std::ostream& operator << (std::ostream& os_arg, const std::vector<Tp>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class Tp, std::size_t Size> std::ostream& operator << (std::ostream& os_arg, const std::array<Tp, Size>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class S, class T> std::ostream& operator << (std::ostream& os_arg, const std::pair<S, T>& pair_arg){ return os_arg << '(' << pair_arg.first << ", " << pair_arg.second << ')'; }

#ifndef ONLINE_JUDGE
    template <typename Head> void dump_out(Head head_arg){ std::cerr << head_arg << '\n'; }
    template <typename Head, typename... Tail>
    void dump_out(Head head_arg, Tail... tail_args){ std::cerr << head_arg << ", "; dump_out(tail_args...); }
    #define dump(...) do { std::cerr << "[in line " << __LINE__ << "] " << #__VA_ARGS__ << " : "; dump_out(__VA_ARGS__); } while(false)
#else
    #define dump(...) (void(0))
#endif

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

int A[100];
inline int next(int n){
    int t = 0; while(n > 0){ A[t++] = n % 10; n /= 10; }
    std::sort(A, A + t);
    int x = 0; for(int i = 0; i < t; ++i) x = 10 * x + A[i];
    int y = 0; for(int i = t - 1; i >= 0; --i) y = 10 * y + A[i];
    return y - x;
}

int main(void){
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(16);

    int n, k; std::cin >> n >> k;
    while(k--) n = next(n);
    std::cout << n << '\n';
    return 0;
}

#include <iostream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iomanip>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define all(V) V.begin(),V.end()
#define PI 3.14159265359
#define fi fixed
#define se setprecision(10)
using namespace std;

int f(int x){
  string s = to_string(x);
  sort(all(s));
  int g2 = stoi(s);
  reverse(all(s));
  int g1 = stoi(s);
  return g1 - g2;
}
int main() {
  int N,K;
  cin >> N >> K;
  int A = N;
  for(int i=0;i<K;i++) {
    A = f(A);
  }
  cout << A << endl;
}


#pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
template<class T>using uset = unordered_set<T>;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion

#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}

#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}

void O() {cout << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);}
#pragma endregion

void Main();

int main(){
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

ll H, W;
ll pos(ll h, ll w){
    if(h < 0 || h >= H || w < 0 || w >= W) return H*W;
    return h*W + w;
}

bool ho(string state, ll h, ll w){
    return state[pos(h, w)] == '.';
}

uset<string> seen;
ll fn(string state, ll A){
    if(seen.find(state) != seen.end()){
        return 0;
    }
    seen.insert(state);
    if(A == 0){
        //O(state);
        return 1;
    }
    ll ret = 0;
    REP(i, H){
        REP(j, W){
            if(!ho(state, i, j))continue;
            string M;
            if(ho(state, i+1, j)){
                M = state;
                M[pos(i, j)] = 'v';
                M[pos(i+1, j)] = '^';
                ret += fn(M, A-1);
            }
            if(ho(state, i, j+1)){
                M = state;
                M[pos(i, j)] = '>';
                M[pos(i, j+1)] = '<';
                ret += fn(M, A-1);
            }
        }
    }
    return ret;
}
void Main(){
    cin >> H >> W;
    I(ll, A, B);
    string S(H*W+1, '.');
    S[H*W] = '#';
    cout << fn(S, A) << el;
}

#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
#define ll long long
#define inf 0x3f3f3f3f
#define N 20
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int H,W,A,B,ans;
bool mp[N][N];

void dfs(int a,int b)
{
    if(a==0&&b==0)
    {
        ans++;
        return ;
    }
    bool flag=true;
    for(int i=1;i<=H&&flag;i++)
        for(int j=1;j<=W&&flag;j++)
        {
            if(!mp[i][j])
            {
                if(i<H&&!mp[i+1][j])
                {
                    mp[i][j]=true;
                    mp[i+1][j]=true;
                    dfs(a-1,b);
                    mp[i][j]=false;
                    mp[i+1][j]=false;
                }
                if(j<W&&!mp[i][j+1])
                {
                    mp[i][j]=true;
                    mp[i][j+1]=true;
                    dfs(a-1,b);
                    mp[i][j]=false;
                    mp[i][j+1]=false;
                }
                mp[i][j]=true;
                dfs(a,b-1);
                mp[i][j]=false;
                flag=false;
            }
        }
}

signed main(){
    scanf("%d%d%d%d",&H,&W,&A,&B);
    dfs(A,B);
    printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    float a,b,discount,res;
    cin>>a>>b;
    discount=(a-b)/a;
    res=discount*100;
    cout<<res<<"\n";
    }

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
const int mod = 1e9+7;
const int N = 1e5+2;

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

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

//My tools
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,s,e)  for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define all(v)  (v).begin(),(v).end()

main(){
    int a,b;cin>>a>>b;
    double ans = ((a-b)/(double)a)*(double)100;
    cout<<setprecision(4)<<fixed<<ans;
}


#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define REV(i, n) for (int i = n - 1; i > 0; i--)
#define all(v) v.begin(), v.end()
#define PL pair<ll, ll>
#define PI pair<int,int>
#define len(s) (int)s.size()

template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
constexpr ll inf = 3e18;
#line 3 "/home/defineprogram/Desktop/Library/math/extgcd.cpp"

ll extGCD(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

ll modinv(ll a, ll m) {
    ll x, y;
    extGCD(a, m, x, y);
    return (x % m + m) % m;
}
#line 4 "/home/defineprogram/Desktop/Library/math/modint.cpp"

template <int MOD>
struct mint {
    int32_t n;
    mint() : n(0) {}
    mint(ll x) : n(x >= 0 ? x % MOD : (MOD - (-x) % MOD) % MOD) {}

    mint &operator+=(const mint &p) {
        if ((n += p.n) >= MOD) n -= MOD;
        return *this;
    }
    mint &operator-=(const mint &p) {
        if ((n += MOD - p.n) >= MOD) n -= MOD;
        return *this;
    }
    mint &operator*=(const mint &p) {
        n = 1ll * n * p.n % MOD;
        return *this;
    }
    mint &operator/=(const mint &p) {
        *this *= p.inverse();
        return *this;
    }
    mint operator-() const { return mint(-n); }
    mint operator+(const mint &p) const { return mint(*this) += p; }
    mint operator-(const mint &p) const { return mint(*this) -= p; }
    mint operator*(const mint &p) const { return mint(*this) *= p; }
    mint operator/(const mint &p) const { return mint(*this) /= p; }
    bool operator==(const mint &p) const { return n == p.n; }
    bool operator!=(const mint &p) const { return n != p.n; }

    friend ostream &operator<<(ostream &os, const mint &p) {
        return os << p.n;
    }
    friend istream &operator>>(istream &is, mint &p) {
        int x;
        is >> x;
        p = mint(x);
        return is;
    }
    mint pow(int64_t x) const {
        mint res(1), mul(n);
        while (x > 0) {
            if (x & 1) res *= mul;
            mul *= mul;
            x >>= 1;
        }
        return res;
    }
    mint inverse() const {
        return mint(modinv(n,MOD));
    }
};
/*
@brief mod int
@docs docs/modint.md
*/
#line 3 "main.cpp"
constexpr int mod=1e9+7;
using modint=mint<mod>;

string N; int K;
modint dp[1<<18][20][2];
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin>>N>>K;
    dp[0][0][0]=1;
    auto f=[](char c)->int{
        if(c<='9')return c-'0';
        return c-'A'+10;
    };
    int MASK=0;
    rep(i,len(N)){
        int n=f(N[i]);

        if(i){
            REP(k,16){
                dp[i+1][1][1]+=1;
            }
        }
        rep(j,17){
            if(MASK>>n&1){
                dp[i+1][j][0]+=dp[i][j][0];
            }else{
                dp[i+1][j+1][0]+=dp[i][j][0];
            }
            rep(k,n){
                if(MASK>>k&1){
                    dp[i+1][j][1]+=dp[i][j][0];
                }else if(i||k){
                    dp[i+1][j+1][1]+=dp[i][j][0];
                }
            }
            dp[i+1][j][1]+=dp[i][j][1]*j;
            dp[i+1][j+1][1]+=dp[i][j][1]*(16-j);
        }
        MASK|=1<<n;
    }
    cout<<dp[len(N)][K][0]+dp[len(N)][K][1]<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=1e9+7;
int n,m;
int C[17][17];
char s[N];
int a[N];
int w[200];
inline int po(int x,int k){
	int ret=1;
	while(k){
		if(k&1) ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		k>>=1;
	}
	return ret;
}
inline int calc(int f,int c){
	if(c<m-f) return 0;
	int ret=0;
	for(int i=m;i>=f;i--){
		if(m-i&1) ret=(ret-1ll*C[m-f][i-f]*po(i,c)%mod+mod)%mod;
		else ret=(ret+1ll*C[m-f][i-f]*po(i,c))%mod;
	}
	return ret;
}
int cnt[17],tot;
int main(){
	int i,j;
	for(i='0';i<='9';i++) w[i]=i-'0';
	for(i='A';i<='F';i++) w[i]=i-'A'+10;
	for(i=0;i<17;i++){
		for(j=0;j<17;j++){
			if(j==0||j==i) C[i][j]=1;
			else C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
		}
	}
	cin>>(s+1);n=strlen(s+1);
	for(i=1;i<=n;i++) a[i]=w[s[i]];
	cin>>m;
	int ans=0;
	for(i=2;i<=n;i++){
		ans=(ans+1ll*C[15][m]*calc(0,n-i+1))%mod;
		ans=(ans+1ll*C[15][m-1]*(m-1)%mod*calc(1,n-i))%mod;
	}
	ans=(ans+1ll*C[15][m-1]*(a[1]-1)%mod*calc(1,n-1))%mod;
	for(i=1;i<n;i++){
		cnt[a[i]]++;if(cnt[a[i]]==1) tot++;
		if(tot>m) break;
		if(a[i+1]==0) continue;
		int s1=0,s2=0;
		for(j=0;j<a[i+1];j++)
			if(cnt[j]) s1++;
		s2=tot-s1;
		int t1=a[i+1]-s1,t2=16-a[i+1]-s2;
		for(j=0;j<=t1&&j<=m-tot;j++)
			ans=(ans+1ll*C[t1][j]*C[t2][m-tot-j]%mod*(1ll*s1*calc(tot,n-i-1)+1ll*j*calc(tot+1,n-i-1)))%mod;
	}
	cnt[a[n]]++;if(cnt[a[n]]==1) tot++;
	if(tot==m) ans=(ans+1)%mod;
	cout<<ans;
	return 0;
}


#include<cstdio>
#define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i)
using namespace std;
const int N=3005;
template<typename T>void read(T &x)
{
	bool neg=false;
	unsigned char c=getchar();
	for(;(c^48)>9;c=getchar())if(c=='-')neg=true;
	for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48);
	if(neg)x=-x;
}
int n,a[N],b[N],c[N],d[N],e[N],f[35];
bool check(int v)
{
	F(i,0,32)f[i]=0;
	F(i,0,n)
	{
		int t=0;
		if(a[i]>=v)t|=16;
		if(b[i]>=v)t|=8;
		if(c[i]>=v)t|=4;
		if(d[i]>=v)t|=2;
		if(e[i]>=v)t|=1;
		F(j,0,32)F(k,0,32)if(j!=k)
		{
			if(f[j]&&f[k]&&(j|k|t)==31)return true;
		}
		else if(f[j]>=2&&(j|t)==31)return true;
		++f[t];
	}
	return false;
}
int solve()
{
	int l=0,r=1000000001,mid;
	while(r-l>1)
	{
		mid=(l+r)>>1;
		if(check(mid))l=mid;
		else r=mid;
	}
	return l;
}
int main()
{
	read(n);
	F(i,0,n)read(a[i]),read(b[i]),read(c[i]),read(d[i]),read(e[i]);
	printf("%d\n",solve());
	return 0;
}

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

using namespace std;

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
	stringstream ss;
	typename Con::const_iterator it = c.begin(), en = c.end();
	bool fst = true;
	for(; it != en; ++it){
		if(!fst){ ss << spr; }
		fst = false;
		ss << *it;
	}
	return ss.str();
}

template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
	vector<typename Con::value_type> ret;
	typename Con::const_iterator it = c.begin(), en = c.end();
	for(; it != en; ++it){
		if(f(*it)){
			ret.emplace_back(*it);
		}
	}
	return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
	vector<decltype(f(*c.begin()))> ret;
	ret.reserve(c.size());
	for(const auto &x: c){
		ret.emplace_back(f(x));
	}
	return ret;
}
#endif

#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif

#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)

#define MOD 1000000007LL
#define EPS 1e-8

void mainmain(){
	int n;
	cin >> n;
	vector<array<LL,5>> v(n);
	REP(i, n){
		REP(j, 5){
			cin >> v[i][j];
		}
	}
	LL ok = 0, ng = 1010101010;
	while(abs(ok - ng) > 1){
		LL md = (ok + ng) >> 1;
		bool f[32] = {};
		REP(i, n){
			int a = 0;
			REP(j, 5){
				if(v[i][j] >= md){
					a |= 1 << j;
				}
			}
			f[a] = true;
		}
		bool found = false;
		REP(i, 32)
		REP(j, 32)
		REP(k, 32)
		if(f[i] && f[j] && f[k] && (i | j | k) == 31){
			found = true;
		}
		if(found){ ok = md; }
		else{ ng = md; }
	}
	cout << ok << endl;
}

}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}


#include <cstdio>
#include <cmath>
#include<iostream>
#include <cstring>
#include <algorithm>
#define space putchar(' ')
#define enter putchar('\n')
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
	if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
	x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x){
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}

const int N = 810, M = 810*810;
int n, m, tr[N][N], ans[M], idx, sum[M];
struct element {
    int x, y, val;
    bool operator < (const element &b) const{
	return val < b.val;
    }
} a[N*N];
struct query {
    int id, xa, ya, xb, yb, k;
} q[M], tmp[M];

inline void add(int x, int y, int val){
    for(int px = x; px <= n; px += px & -px)
	for(int py = y; py <= n; py += py & -py)
	    tr[px][py] += val;
}
inline  int single_ask(int x, int y){
    int ret = 0;
    for(int px = x; px; px -= px & -px)
	for(int py = y; py; py -= py & -py)
	    ret += tr[px][py];
    return ret;
}
inline  int ask(query Q){
    return single_ask(Q.xb, Q.yb) - single_ask(Q.xa - 1, Q.yb) - single_ask(Q.xb, Q.ya - 1) + single_ask(Q.xa - 1, Q.ya - 1);
}
inline  void solve(int l, int r, int ql, int qr){
    if(ql > qr) return;
    if(l == r){
	for(int i = ql; i <= qr; i++)
	    ans[q[i].id] = a[l].val;
	return;
    }
    int mid = (l + r) >> 1, cnt = 0;
    for(int i = l; i <= mid; i++)
	add(a[i].x, a[i].y, 1);
    for(int i = ql; i <= qr; i++){
	sum[i] = ask(q[i]);
	if(sum[i] >= q[i].k) cnt++;
    }
    for(int i = ql, pl = ql, pr = ql + cnt; i <= qr; i++)
	if(sum[i] >= q[i].k) tmp[pl++] = q[i];
	else q[i].k -= sum[i], tmp[pr++] = q[i];
    for(int i = ql; i <= qr; i++) q[i] = tmp[i];
    for(int i = l; i <= mid; i++)
	add(a[i].x, a[i].y, -1);
    solve(l, mid, ql, ql + cnt - 1);
    solve(mid + 1, r, ql + cnt, qr);
}

int main(){
    int k;
    read(n), read(k);
    for(int i = 1; i <= n; i++)
	for(int j = 1; j <= n; j++)
	    a[++idx].x = i, a[idx].y = j, read(a[idx].val);
    sort(a + 1, a + idx + 1);

    int m=0;
    for(int i = 1;i <= n; i++){
        for(int j = 1; j <= n; j++){
            int x1=i;int y1=j;
            int x2=x1+k-1;int y2=y1+k-1;
            if(x2>n||y2>n) continue;

            m++;
            q[m].id=m;
            q[m].xa=x1;q[m].ya=y1;q[m].xb=x2;q[m].yb=y2;

            int ppp=k*k/2+1;
            q[m].k=k*k-ppp+1;
        }
    }
    solve(1, idx, 1, m);
    int anss=1e9;
    for(int i = 1; i <= m; i++) {
           /// write(ans[i]), enter;
        anss=min(anss,ans[i]);
    }
    write(anss);
   /// printf("%d\n",anss);
        ///anss=min(anss,ans[i]);

    return 0;
}

/**
 *  author: souravrax
 *  created: 30.05.2021 19:53:53
**/
#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#define len(x) int((x).size())
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "[ " << #__VA_ARGS__ << " ]:", dbg_out(__VA_ARGS__)
#else
#define dbg(...) (void)0x30
#endif
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using t3 = tuple<int, int, int>;

const int nax = 800;
ll n, k;
ll matrix[nax][nax];

bool can(ll elem) {
    // dbg(elem);
    vector<vll> grid(n, vll(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            grid[i][j] = matrix[i][j] <= elem ? 1 : 0;
        }
    }
    vector<vll> prefix(n + 1, vll(n + 1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] + grid[i - 1][j - 1] - prefix[i - 1][j - 1];
        }
    }
    // dbg(prefix);
    for (int i = 0; i + k <= n; i++) {
        for (int j = 0; j + k <= n; j++) {
            ll now = prefix[i + k][j + k] + prefix[i][j] - prefix[i + k][j] - prefix[i][j + k];
            // dbg(now);
            if (now >= (k * k + 1) / 2) return true;
        }
    }
    return false;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n >> k;
    ll ans = 0;
    ll l = 0, r = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cin >> matrix[i][j];
        r = ans = max(ans, *max_element(matrix[i], matrix[i] + n));
        l = min(l, *min_element(matrix[i], matrix[i] + n));
    }
    // can(4);
    while (l <= r) {
        ll mid = l + (r - l) / 2;
        if (can(mid)) {
            ans = mid;
            r = mid - 1;
        } else {
            l = mid + 1;
        }
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)      //iをaからn
#define per(i, n, a) for (int i = a - 1; i >= n; i--) //iをaからn
#define db(x) cout << #x << " = " << x << endl
#define db2(x, y) cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; //デバッグ用
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define F first
#define S second
//sort(all(a)); ソート
//sort(all(a),greater<int>()); 逆順ソート
//a.erase(unique(all(a)), a.end()); ソート後に重複を削除
typedef long long ll;
//const ll mod=1000000007;

template< typename T >
T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

int main(){
    int a,b,c;
    cin>>a>>b>>c;
    a%=10;
    int fouramari=mod_pow(b,c,4);
    if(a==0){
    cout<<0<<endl;
    }else if(a==1){
    cout<<1<<endl;
    }else if(a==5){
    cout<<5<<endl;
    }else if(a==6){
    cout<<6<<endl;
    }else if(a==2){
        switch(fouramari){
            case 0:
            cout<<6<<endl;
            break;
            case 1:
            cout<<2<<endl;
            break;
            case 2:
            cout<<4<<endl;
            break;
            case 3:
            cout<<8<<endl;
            break;
        }
    }else if(a==3){
        switch(fouramari){
            case 0:
            cout<<1<<endl;
            break;
            case 1:
            cout<<3<<endl;
            break;
            case 2:
            cout<<9<<endl;
            break;
            case 3:
            cout<<7<<endl;
            break;
        }
    }else if(a==4){
        if(b%2==0){
        cout<<6<<endl;
        }else{
        cout<<4<<endl;
        }
    }else if(a==7){
        switch(fouramari){
            case 0:
            cout<<1<<endl;
            break;
            case 1:
            cout<<7<<endl;
            break;
            case 2:
            cout<<9<<endl;
            break;
            case 3:
            cout<<3<<endl;
            break;
        }
    }else if(a==8){
        switch(fouramari){
            case 0:
            cout<<6<<endl;
            break;
            case 1:
            cout<<8<<endl;
            break;
            case 2:
            cout<<4<<endl;
            break;
            case 3:
            cout<<2<<endl;
            break;
        }
    }else{
        if(b%2==0){
        cout<<1<<endl;
        }else{
        cout<<9<<endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;   typedef pair<int,int>pint;
typedef long double db;
using vll=vector<ll>;   using vs=vector<string>;
#define b2e(v)v.begin(),v.end()
#define p2a(v, p) v[p.first][p.second]

ll solve1(db x, db y, db r) {
    ll ct=0;
    r+= 1e-14;
    db r2 = (r*r);

    ll xi=floor(x-r), xmax=ceil(x+r);

    for (; xi <= xmax; xi++) {
        db dx = (db)xi-x;
        db y2 = r2 - (dx*dx);
        if (y2 < 0) continue;
        db yi = sqrt(y2);
        db dymax = y+yi,dymin=y-yi;
        ll ymax = floor(dymax), ymin = ceil(dymin);
        //if (abs(ymin-dymin)<1e-10) ct++;
        //if (y + yi> 0 && y - yi < 0) ct++;
        //if (y + yi<0) ymax--;
        //if (y - yi<0) ymin--;
        ct += ymax - ymin + 1;
    }
    return ct;
}

#ifndef NOMAIN
int main() {
    db x,y,r;
    cin >> x >> y >> r;
    cout << solve1(x,y,r) << endl;
}
#endif


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double lb;
#define rep(i, j, n) for (ll i = j; i < (n); i++)
#define repr(i, j, n) for (ll i = j; i >= (n); i--)
#define each(i, mp) for (auto &i : mp)
#define all(x) (x).begin(), (x).end()
#define CLR(mat, f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a <= x && x < b)
#define out(ans) cout << ans << endl
typedef pair<ll, ll> P;
const lb pi = acos(-1);
const ll mod = 1e9 + 7;
const ll INF = 1LL << 60;

int main(){

    ll n;cin>>n;
    ll x[n],y[n];rep(i,0,n)cin>>x[i]>>y[i];

    bool ans=false;

    rep(i,0,n-2){
        rep(j,i+1,n-1){

            rep(k,j+1,n){
                lb xd1=x[j]-x[i],xd2=x[k]-x[i];
                lb yd1=y[j]-y[i],yd2=y[k]-y[i];

                if(xd1==0 and xd2==0)ans=true;
                else if(yd1/xd1==yd2/xd2)ans=true;

            }
        }
    }

    if(ans)out("Yes");
    else out("No");

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

//#define endl '\n'
#define pb push_back
#define ll long long
#define int ll
#define FF first
#define SS second
#define f(a,b,c) for(int a=b;a<=c;a++)
#define fd(a,b,c) for(int a=b;a>=c;a--)
#define fspr(X) fixed<<setprecision(X)

int mod = 1000000007;

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}

signed main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int T=1;//cin>>T;
   	while(T--)
    {
    	int n,m;cin>>n>>m;
    	int A[n],B[m];
    	f(i,0,n-1)cin>>A[i];f(i,0,m-1)cin>>B[i];
    	int i=0,j=0;
    	while(i<n || j<m){
    		if(i>=n){
    			while(j<m){
    				cout<<B[j]<<" ";j++;
				}
			}
			if(j>=m){
    			while(i<n){
    				cout<<A[i]<<" ";i++;
				}
			}
    		while(i<n && j<m && A[i]<B[j]){
    			cout<<A[i]<<" ";i++;
			}
			while(j<m && i<n && A[i]>B[j]){
				cout<<B[j]<<" ";j++;
			}
			while(i<n && j<m && A[i]==B[j]){
				i++;j++;
			}
		}
		cout<<endl;
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define int long long
#define sz(x) (ll)(x.size())
using namespace std;

const int mod = 1e9+7;

int expo_pow(int x,int y){
 if(y == 0) return 1;
  y=y%(mod-1);
  x%=mod;
  if(y==0) y=mod-1;
  int res=1;
  while(y){
    if(y&1) res=(res*x)%mod;
    x=(x*x)%mod;
    y>>=1;
  }
  return res;
}

ll add()
{
    return 0;
}

template <typename T, typename... Types>
T add(T var1, Types... var2){
    return (((((ll)(var1)) % mod + (ll)(add(var2...))) % mod) + mod) % mod;
}

ll mul(){
    return 1;
}

template <typename T, typename... Types>
T mul(T var1, Types... var2){
    return (((ll)(var1)) % mod * (ll)(mul(var2...))) % mod;
}

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

  int x4;
  if (n -a -b <0) x4 = 0;
  else {
    x4 = (n-a-b+2)*(n-a-b+1)/2;
  }
  x4 %= mod;
  int x3 = 2*x4;
  x3 %= mod;
  int x2 = (mul((n-a+1),(n-b+1)) - x3 + mod) %mod;
  int x1 = x2*x2;
  x1 %= mod;
  int ans = ((mul((n-a+1),(n-a+1),(n-b+1),(n-b+1)) - x1)%mod + mod )%mod;
  cout << ans << "\n";

}

signed main(){
  fast;
  int test = 1;
  cin>>test;
  int i=1;
  while(test--){
    solve();
  }
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long int;
const ll MOD = 1000000007;

int main(){
    int t;
    cin >> t;
    for(int i = 0; i < t; i++){
        ll n, a, b;
        cin >> n >> a >> b;
        if(n < a+b){
            cout << 0 << endl;
            continue;
        }
        ll ans = (((n-a+1)%MOD)*((n-a+1)%MOD))%MOD;
        ans *= (((n-b+1)%MOD)*((n-b+1)%MOD))%MOD;
        ans %= MOD;
        ll x = (((n-a+1)%MOD)*((n-b+1)%MOD))%MOD;
        x -= (((n-a-b+2+MOD*2)%MOD)*((n-a-b+1+MOD*2)%MOD))%MOD;
        while(x < 0) x += MOD;
        x %= MOD;
        x *= x;
        x %= MOD;
        ans -= x;
        while(ans < 0) ans += MOD;
        cout << ans%MOD << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define sz(x) int(x.size())
#define vi vector<int>
#define vl vector<long long>
#define vvl vector<vector<long long> >
#define vvi vector<vector<int> >
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
void solve(){
	int n, m, inp;
	cin>>n>>m;
	vi A;
	A.pb(0);
	A.pb(n + 1);
	for(int i = 0; i < m; ++i){
		cin>>inp;
		A.pb(inp);
	}
	sort(all(A));
	int mn = 1e9, ans = 0;
	for(int i = 1; i <= m + 1; ++i){
		if(A[i] - A[i-1] > 1) mn = min(mn, A[i] - A[i-1] - 1);
	}
	for(int i = 1; i <= m + 1; ++i){
		if(A[i] - A[i-1] > 1) ans += (A[i] - A[i-1] - 1)/mn + ((A[i] - A[i-1] - 1)%mn != 0);
	}
	cout<<ans;
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
}

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
void fun(vector<ll>v,ll n,ll m)
{
    v.pb(n+1);
    ll prev=0;
    vector<ll>temp;
    for(ll val:v)
    {
        if(val-prev-1>0)
        {
            temp.pb(val-prev-1);
        }
        prev=val;
    }
    sort(temp.begin(),temp.end());
    ll k=temp[0];
    ll ans=0;
    for(ll val:temp)
    {
        ans+=ceil((double)val/(double)k);
    }
    cout<<ans<<endl;
    return;
}
int main()
{
    ll n,m;cin>>n>>m;
    vector<ll>v(m);
    for(int i=0;i<m;i++)cin>>v[i];
    sort(v.begin(),v.end());
    if(m==n)
        cout<<0<<endl;
    else
    fun(v,n,m);
    return 0;
}


//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#include<set>
#include <array>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound

#define si(x) int(x.size())
#define sum_all(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
const double pi = 2 * acos(0.0);
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };

const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 };
const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 };

ll min(ll a,ll b)
{
    if(a<b)return a;
    return b;
}
ll max(ll a,ll b)
{
    if(a>b)return a;
    return b;
}
ll ceil1(ll a,ll b)
{
    return(a+b-1)/b;
}
void read(vector<ll> & arr)
{
  for(ll i=1;i<si(arr);i++)
    cin >> arr[i];

}

void read_graph(vector<vector<ll>>& g, ll m)
{
  while(m--)
  {
    ll x,y; cin >> x>> y ;
    x--,y--;
    g[x].pb(y);
    g[y].pb(x);
  }
}

bool sorted(vector<ll> &arr)
{
  int n=si(arr)-1;
  if(arr[n]>arr[n-1] and arr[n-1]>arr[n-2]) return true;
  return false ;

}
void brute(vector<int> &ans, vector<ll> &arr, ll c)
{

   int n=si(arr)-1;
   int e=n-2;
   int o=n-1;
   if(e%2!=0)
   	swap(o,e);
  while(sorted(arr)==false)
  {
  	if(c%2==0)
  	{
        ans.pb(e);
        swap(arr[e],arr[e+1]);
        c++;
  	}
  	else
  	{
  		ans.pb(o);
  		swap(arr[o],arr[o+1]);
  		c++;
  	}
  }
  return ;

}
void solve()
{

  ll n;
  cin >> n;

   vector<ll>a(n+1);
   read(a);
   if(n==2)
   {
   	 if(a[1]==1)
   	 {
   	 	 cout << 0 << endl;
   	 	 return ;
   	 }
   	 else
   	 {
   	 	cout <<1 << endl << 1 << endl;
   	 	return ;
   	 }
   }

   ll c=1;
   vector<int> ans;
   for(ll i=1;i<=n;i++)
   {
    ll i1=i;
    for(ll j=1;j<=n;j++)
    	if(a[j]==i)
    	{
    		i1=j;
    		break;
    	}

       if(i1==i) continue;
       for(ll j=i1;j>i;j--)
       {
       	if((j-1)%2==1)
       	{
          if(c%2==1)
          {
          	ans.pb(j-1);
          	swap(a[j-1],a[j]);
          	c++;
          }
          else
          {

              if(j-i>=2)
          {
          	swap(a[j-2],a[j-1]);
          	ans.pb(j-2);
          	c++;
          	j++;
          } else if(j<=n-1)
            {
            	ans.pb(j);
            	swap(a[j],a[j+1]);
            	c++;
            	j+=2;
            }
            else
            {
             brute(ans,a,c);
            }
          }
       	}
       	else
       	{
         if(c%2==0)
         {
         	ans.pb(j-1);
            swap(a[j-1],a[j]);
            	c++;
         }
         else
         {

         	if(j-i>=2)
          {
          	swap(a[j-2],a[j-1]);
          	ans.pb(j-2);
          	c++;
          	j++;
          }
          else if(j<=n-1)
          {
          	swap(a[j+1],a[j]);
          	ans.pb(j);
          	c++;
          	j+=2;
          }

          else
          {
          	brute(ans,a,c);
          }
         }
       	}
       }

   }

   for(int i=0;i<si(ans);i++)
   {
   	assert(ans[i]%2==(i+1)%2);
   }
for(int i=1;i<=n;i++) assert(a[i]==i);
   assert(si(ans)<=n*n);
   cout << si(ans) << endl;
   for(auto a: ans) cout << a<< " ";
   	cout << endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t; cin >> t; while (t--)
    solve();

}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

template<class T>
vector<T> make_vec(size_t a){
	return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
	return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

/* Some Libraries */

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

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T; cin>>T;
    while(T--){
        int N; cin>>N;
        vi p(N);
        rep(i,N) cin>>p[i], p[i]--;

        vi ans;

        auto solve = [&](int x){
            int idx=-1;
            FOR(i,x,N-1)if(p[i]==x) idx=i;

            assert(idx!=-1);

            if (idx==x){
                if (N-x>=3){
                    ans.pb(idx); swap(p[idx],p[idx+1]);
                    ans.pb(idx+1); swap(p[idx+1],p[idx+2]);
                    ans.pb(idx); swap(p[idx],p[idx+1]);
                    ans.pb(idx+1); swap(p[idx+1],p[idx+2]);
                    ans.pb(idx); swap(p[idx],p[idx+1]);
                }
                return;
            }
            if (idx%2==x%2){
                ans.pb(x);
                swap(p[x],p[x+1]);
            }

            while(idx!=x){
                ans.pb(idx-1);
                swap(p[idx-1],p[idx]);
                idx--;
            }
        };

        rep(i,N) solve(i);

        int M = ans.size();
        cout<<M<<"\n";
        rep(i,M){
            cout<<ans[i]+1<<" \n"[i==M-1];
        }
    }
    return 0;
}