/**
 *  created: 09/01/2021, 14:06:07
**/

#include <bits/stdc++.h>

using namespace std;

const int max_n = 600666, inf = 1000111222;

namespace max_flow {

const int max_v = max_n, inf = 1000111222;

struct edge {
    int to, residual_capacity;

    edge(int to, int residual_capacity): to(to), residual_capacity(residual_capacity) {
    }
};

vector<edge> edges;
vector<int> g[max_v];

void add_edge(int u, int v, int capacity) {
    g[u].push_back(edges.size());
    edges.push_back({v, capacity});
    g[v].push_back(edges.size());
    edges.push_back({u, 0});
}

int h[max_v], num[max_v];

bool bfs(int s, int t) {
    memset(h, -1, sizeof(h[0]) * (t + 1));
    h[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        if (v == t) {
            return true;
        }
        for (int id : g[v]) {
            if (edges[id].residual_capacity && h[edges[id].to] == -1) {
                h[edges[id].to] = h[v] + 1;
                q.push(edges[id].to);
            }
        }
    }
    return h[t] != -1;
}

int dfs(int v, int t, int f) {
    if (v == t) {
        return f;
    }
    for (int &i = num[v]; i < g[v].size(); ++i) {
        const int id = g[v][i];
        if (edges[id].residual_capacity && h[v] + 1 == h[edges[id].to]) {
            int res = dfs(edges[id].to, t, min(f, edges[id].residual_capacity));
            if (res) {
                edges[id].residual_capacity -= res;
                edges[id ^ 1].residual_capacity += res;
                return res;
            }
        }
    }
    return 0;
}

int dinic(int s, int t) {
    int res = 0;
    while (bfs(s, t)) {
        memset(num, 0, sizeof(num[0]) * (t + 1));
        while (int f = dfs(s, t, inf)) {
            res += f;
        }
    }
    return res;
}

void clr(int t) {
    edges.clear();
    for (int i = 0; i <= t; ++i) {
        g[i].clear();
    }
}

}

int n, a[max_n], b[max_n];
vector<int> v;

int compress(int x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin();
}

int main() {
    //freopen("in/1_025.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &a[i], &b[i]);
        v.push_back(a[i]);
        v.push_back(b[i]);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    int s = n + v.size(), t = n + v.size() + 1;
    for (int i = 0; i < n; ++i) {
        a[i] = compress(a[i]);
        b[i] = compress(b[i]);
        max_flow::add_edge(s, i, 1);
        max_flow::add_edge(i, n + a[i], 1);
        if (i != a[i]) {
            max_flow::add_edge(i, n + b[i], 1);
        }
    }
    for (int i = 0; i < v.size(); ++i) {
        max_flow::add_edge(n + i, t, 1);
    }
    int ans = max_flow::dinic(s, t);
    printf("%d\n", ans);
    return 0;
}

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

typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;

#define pb push_back
#define ff first
#define ss second
#define MAX 200005
#define MOD 1000000007
#define INF 1e9+100
vector<int>adj[2*MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };

int p[2*MAX],sz[2*MAX][2];
void init()
{
    for(int i=1;i<=400000;i++)
        p[i]=i,sz[i][0]=1;
}

int find_parent(int u)
{
    return p[u]=p[u]==u?u:find_parent(p[u]);
}

void union_sets(int u, int v)
{
    int pu=find_parent(u);
    int pv=find_parent(v);

    if(pu!=pv)
    {
        if(sz[pu][0]<sz[pv][0])
            swap(pu,pv);

        p[pv]=pu;
        sz[pu][0]+=sz[pv][0];
        sz[pu][1]+=1+sz[pv][1];
    }
    else sz[pu][1]++;
}
int main()
{
//    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0),cout.tie(0);
    ios_base::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);
    int TC=1;
    int n,m,q;
    //cin>>TC;
    for(int t1=1; t1<=TC; t1++)
    {
        cin>>n;
        init();
        for(int i=0; i<n; i++)
        {
            int a,b;
            cin>>a>>b;

            union_sets(a,b);
        }

        bool processed[2*MAX]={};
        int ans=0;
        for(int i=1;i<=400000;i++)
        {
            int p=find_parent(i);
            if(!processed[p])
            {
                processed[p]=1;
                ans+=min(sz[p][0],sz[p][1]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007
//#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define bs(yup,x) binary_search(yup.begin(),yup.end(),x) //it will return bollean value
#define lb(yup,x) lower_bound(yup.begin(),yup.end(),x)  //it will return the adress of the number which is equal to or just greater than x ,and if it is equal to yup.end() than their in no such number exist
#define ub(yup,x) upper_bound(yup.begin(),yup.end(),x)
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
using namespace std;
const long long N=100005, INF=2000000000000000000;
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }
vi prime;
bool isprime[N];
void pre()
{
    for(int i=2;i<N;i++)
    {
        if(isprime[i])
        {
            for(int j=i*i;j<N;j+=i)
            isprime[j]=false;
            prime.pb(i);
        }
    }
    return;
}

int32_t main()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout<<21-(a+b+c)<<"\n";

}

#include <bits/stdc++.h>

using namespace std;

int main() {

    int a,b,c;
    cin >> a >> b >> c;

    int v[] = {0,6,5,4,3,2,1};

    cout << v[a] + v[b] + v[c] << "\n";
    return 0;
}


#include "bits/stdc++.h"
using namespace std;
int N;
vector<int>to[200005];
vector<set<int>>st(100);
vector<vector<bool>> pass(100, vector<bool>(100));
void dfs(int v, int i, int p = -1) {
	for (int e : to[v]) {
		if (p == e) {
			continue;
		}
		if (pass[i][e]) {
			continue;
		}
		pass[i][e] = true;
		st[i].insert(e);
		dfs(e, i, v);
	}
}

int main() {
	int N;
	cin >> N;
	vector<string>S(N);
	for (int i = 0; i < N; ++i) {
		cin >> S[i];
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			if ('1' == S[i][j]) {
				to[i].push_back(j);
			}
		}
	}
	for (int i = 0; i < N; ++i) {
		pass[i][i] = true;
		dfs(i, i);
	}
	/*
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			cout << pass[i][j];
		}
		cout << endl;
	}*/
	double ans = 0;
	for (int i = 0; i < N; ++i) {
		int count = 0;
		for (int j = 0; j < N; ++j) {
			if (pass[j][i]) {
				count++;
			}
		}
		ans += (double)1 / count;
	}
	printf("%.16f\n", ans);
	return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i,n) rep(i,1,n)
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar('\n');
}

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2, ll p = mod) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % p;
        b /= 2;
        a = a * a % p;
    }
    return c;
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

template<typename T>
struct BIT {
	T* c; int n; // require you to define 0 as the initial value !!
	BIT(int _n):n(_n){c=new T[n];c--; FOR(i,n) c[i]=T(0); }
	void add(int x,T y) { for( ;x<=n;x += x&-x) c[x]=c[x]+y; }
	T sum(int x) {T y=T(0); for( ;x;x &= x-1) y=y+c[x]; return y;}
};

int n, a[N], vis[N], lst[N], now;
char s[N][N];

void dfs(int x) {
	if(lst[x] == now) return;
	lst[x]=now; vis[x]++;
	FOR(y,n) if(s[x][y] == '1') dfs(y);
}

void solve() {
	qr(n);
	FOR(i,n) scanf("%s",s[i]+1);
	for(now=1;now<=n;now++) dfs(now);
	db ans=0;
	FOR(i,n) ans += 1.0/vis[i];
	printf("%.10lf\n",ans);
}

int main() {
    int T = 1;
//    qr(T);
    while (T--)
        solve();
    return 0;
}


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

const int MOD = 998244353;

int solve(vector<ll> abc)
{
    ll ans = 1;
    rep(i, 3)
    {
        ll tmp = abc[i] * (abc[i] + 1) / 2 % MOD;
        ans = ans * tmp % MOD;
    }
    return ans;
}

int main()
{
    vector<ll> abc(3);
    rep(i, 3) cin >> abc[i];

    int ans = solve(abc);
    cout << ans << '\n';
    return 0;
}

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

int main() {

	int a[4];
	cin>>a[0]>>a[1]>>a[2]>>a[3];

	sort(a, a+4);
	cout<<a[0];
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e8)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int main()
{
	owo
	int n;
	cin>>n;
	vector<ll>a(2*n);
	for(int i=0;i<2*n;i++)cin>>a[i];
	vector<int>id(2*n);
	string ans(2*n,'?');
	for(int i=0;i<2*n;i++)id[i] = i;
	sort(all(id),[&](int x,int y){return a[x] > a[y];});
	vector<bool>beeg(2*n);
	for(int i=0;i<n;i++)beeg[id[i]] = 1;
	stack<int>stk;
	for(int i=0;i<2*n;i++){
		if(stk.empty() || beeg[stk.top()] == beeg[i])stk.push(i);
		else{
			ans[stk.top()] = '(';
			ans[i] = ')';
			stk.pop();
		}
	}
	cout<<ans<<'\n';

}

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

using namespace std;

int _ = 1;

ll dp[200001][2];
map<ll, ll>mn;
map<ll, ll>mx;
vector<int>colors;

ll solve(int i , int left){
    ll &ret = dp[i][left];
    if(~ret)return ret;
    ret = 1e16;
    ll inter = abs(mx[colors[i]] - mn[colors[i]]);
    if(i == colors.size()-1){
        if(left)ret = inter + abs(mx[colors[i]]);
        else ret = inter + abs(mn[colors[i]]);
    }else{
        if(left){
            ret = min(ret, inter + abs(mx[colors[i]] - mx[colors[i+1]]) + solve(i+1, 0));
            ret = min(ret, inter + abs(mx[colors[i]] - mn[colors[i+1]]) + solve(i+1, 1));
        }else{
            ret = min(ret, inter + abs(mn[colors[i]] - mx[colors[i+1]]) + solve(i+1, 0));
            ret = min(ret, inter + abs(mn[colors[i]] - mn[colors[i+1]]) + solve(i+1, 1));
        }
    }
    return ret;
}

void _solve() {
    memset(dp,-1,sizeof(dp));
    int n;
    cin>>n;
    for(int i = 0 ; i < n ; i++) {
        ll x,c;
        cin>>x>>c;
        if(mn.find(c) != mn.end()){
            mn[c] = min(mn[c] , x);
            mx[c] = max(mx[c] , x);
        }else{
            mn[c] = x;
            mx[c] = x;
        }
    }
    colors.clear();
    for(auto x: mx){
        colors.push_back(x.first);
    }
    cout<< min(solve(0,0) + abs(mx[colors[0]]) , solve(0,1) + abs(mn[colors[0]]))<<'\n';
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    while (_--)_solve();
    return 0;
}


#include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W, N, M;
    cin >> H >> W >> N >> M;
    vector<vector<char>> grid1(H, vector<char>(W, '.'));
    vector<vector<char>> grid2(H, vector<char>(W, '.'));
    int a, b, c, d;
    rep(i, 0, N) {
        cin >> a >> b;
        --a, --b;
        grid1[a][b] = grid2[a][b] = '?';
    }
    rep(i, 0, M) {
        cin >> c >> d;
        --c, --d;
        grid1[c][d] = grid2[c][d] = '#';
    }
    rep(i, 0, H) {
        rep(j, 1, W) {
            if (grid1[i][j] != '.') {
                continue;
            }
            if (grid1[i][j - 1] == '?' || grid1[i][j - 1] == '+') {
                grid1[i][j] = '+';
            }
        }
        rrep(j, W - 1, 0) {
            if (grid1[i][j] != '.') {
                continue;
            }
            if (grid1[i][j + 1] == '?' || grid1[i][j + 1] == '+') {
                grid1[i][j] = '+';
            }
        }
    }
    rep(i, 0, W) {
        rep(j, 1, H) {
            if (grid2[j][i] != '.') {
                continue;
            }
            if (grid2[j - 1][i] == '?' || grid2[j - 1][i] == '-') {
                grid2[j][i] = '-';
            }
        }
        rrep(j, H - 1, 0) {
            if (grid2[j][i] != '.') {
                continue;
            }
            if (grid2[j + 1][i] == '?' || grid2[j + 1][i] == '-') {
                grid2[j][i] = '-';
            }
        }
    }
    int ans = 0;
    rep(i, 0, H) rep(j, 0, W) {
        if (grid1[i][j] == '?' || grid1[i][j] == '+' || grid2[i][j] == '-') {
            ++ans;
        }
    }
    cout << ans << endl;
    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int mapn[1500][1500];
int a, b, c, d;
int h, w, n, m;
int ans;

int main() {
  cin >> h >> w >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    mapn[a - 1][b - 1] = 1;
  }
  for (int i = 0; i < m; ++i) {
    cin >> c >> d;
    mapn[c - 1][d - 1] = -1;
  }
  for (int i = 0; i < h; ++i) {
    bool l = 0;
    for (int j = 0; j < w; ++j) {
      if (mapn[i][j] == 1) {
        l = true;
      } else if (mapn[i][j] == -1) {
        l = false;
      } else if (l) {
        mapn[i][j] = 2;
      }
    }
    l = 0;
    for (int j = w - 1; j >= 0; --j) {
      if (mapn[i][j] == 1) {
        l = true;
      } else if (mapn[i][j] == -1) {
        l = false;
      } else if (l) {
        mapn[i][j] = 2;
      }
    }
  }
  for (int j = 0; j < w; ++j) {
    bool l = false;
    for (int i = 0; i < h; ++i) {
      if (mapn[i][j] == 1) {
        l = true;
      } else if (mapn[i][j] == -1) {
        l = false;
      } else if (l) {
        mapn[i][j] = 2;
      }
    }
    l = false;
    for (int i = h - 1; i >= 0; --i) {
      if (mapn[i][j] == 1) {
        l = true;
      } else if (mapn[i][j] == -1) {
        l = false;
      } else if (l) {
        mapn[i][j] = 2;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      ans += mapn[i][j] > 0;
    }
  }
  cout << ans;
}


#include<bits/stdc++.h>
#include<cmath>
#define SZ(x) ((int)x.size())
#include<vector>
#include<set>
#include<map>
#define ll long long
#define lli long long int
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#include<string>
#include<cctype>
#define F first
#define S second
#include<queue>
#include <sstream>
#define endl '\n'
#define lli long long int
#define pairs pair<int,int>
#define ld long double
#define mod 1000000007
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
//first try to write all condition on copy then go for implement
ll pows(ll a,ll n,ll m)
{
	a=a%m;
	ll res=1;
	while(n)
	{
	if(n%2!=0)
	{
		res=(res*a)%m;
		n--;
		}
		else
		{
			a=(a*a)%m;
			n=n/2;
			}
	}
	return res%m;
}
ll gcd(ll a,ll b)
{
	if(b==0)
	return a;
	else
	return gcd(b,a%b);
}
bool isprime(ll n)
{
	if(n==1||n==0)
	{
		return false;
	}
	for(ll i=2;i*i<=n;i++)
	{
		if(n%i==0)
		{
			return false;
		}
	}
	return true;
}
bool istrue(string s)
{
	int i=0;
	int j=s.size()-1;
	while(i<j)
	{
	if(s[i]==s[j])
	{
		i++;
		j--;
		}
		else
		{
		return false;
			}
	}
	return true;
}
const int N=100005;

int main()
{
ios::sync_with_stdio(false);
	 cin.tie(0);
  cout.tie(0);
ll n;
cin>>n;
string s;
cin>>s;
int flip=0;
string s1;
string s2;
for(int i=0;i<n;i++)
{
s1.pb(s[i]);
}
for(int i=n;i<2*n;i++)
{
	s2.pb(s[i]);
}
int q;
cin>>q;
while(q--)
{
ll t,a,b;
cin>>t>>a>>b;
if(t==1)
{
if(flip==0)
{
if(a>n)
{
	a=a-n;
	b=b-n;
	a--;
	b--;
	swap(s2[a],s2[b]);
}
else if(a<=n&&b>n)
{
  a--;
	b=b-n;
	b--;
swap(s1[a],s2[b]);
}
else{
	a--;
	b--;
swap(s1[a],s1[b]);
}
}
else
{
if(a>n)
{
	a=a-n;
	b=b-n;
	a--;
	b--;
	swap(s1[a],s1[b]);
}
else if(a<=n&&b>n)
{
a--;
	b=b-n;
	b--;
swap(s2[a],s1[b]);
}
else{
	a--;
	b--;
swap(s2[a],s2[b]);
}
}
}
else
{
flip=flip^1;
}
}
if(flip==0)
{
cout<<s1<<s2<<endl;
}
else
{
cout<<s2<<s1<<endl;
}

}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> pll;

#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define test() ll t;cin>>t;for(ll test = 1;test<=t;test++)
#define f first
#define s second
#define pb push_back
#define nl cout<<"\n"
#define MOD(x) ((x%P)+P)%P
#define vec(v) loop(i,0,v.size()) cout<<v[i]<<" "; nl
const int N = 1000005;
const ll P = 1000000007;
const ll INF = 1e17;
const double eps = 1e-6;

int main()
{
	fastio();

	int n; cin>>n;
	vector<ll> a(n), t(n);

	for(int i=0; i<n; i++)
		cin>>a[i]>>t[i];

	stack<pair<ll,ll>> st;

	ll sum = 0;
	for(int i=n-1; i>=0; i--)
	{
		if(t[i]==1)
			sum += a[i];
		else if(t[i]==2)
			st.push({2, a[i]+sum});
		else
			st.push({3, a[i]+sum});
	}

	ll mn = -INF, mx = INF;

	while(st.size())
	{
		pair<ll,ll> now = st.top();
		st.pop();

		if(now.f == 2)
		{
			mn = max(mn, now.s);
			if(mn > mx)
				mx = mn;
		}
		else
		{
			mx = min(mx, now.s);
			if(mx < mn)
				mn = mx;
		}
	}

	int q; cin>>q;
	for(int i=0; i<q; i++)
	{
		ll temp; cin>>temp;
		temp += sum;

		temp = max(temp, mn);
		temp = min(temp, mx);

		cout<<temp,nl;
	}

	cerr << "\nTime elapsed : " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
	return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;

int n;
int g[21];
bool isOK(int pat) {
  REP(i, n) {
    if(pat & (1 << i)) {
      if((g[i] & pat) != pat) {
        return false;
      }
    }
  }
  return true;
}

int memo[1 << 18];

int solve(int rest) {
  int &res = memo[rest];
  if(res < 0) {
    if(rest == 0) {
      res = 0;
    } else if(((rest-1) & rest) == 0) {
      res = 1;
    } else {
      int first = rest & -rest;
      int rest2 = rest ^ first;
      res = 1 + solve(rest2);
      for(int s = rest2; s; s = (s-1) & rest2) {
        if(isOK(s | first)) {
          res = min(res, 1 + solve(rest ^ first ^ s));
        }
      }
    }
  }
  return res;
}

int main(void) {
  int nEdge;
  scanf("%d%d", &n, &nEdge);
  REP(i, n) {
    g[i] |= 1 << i;
  }
  REP(i, nEdge) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a] |= 1 << b;
    g[b] |= 1 << a;
  }
  if(nEdge == n*(n-1) / 2) {
    cout << 1 << endl;
    return 0;
  }
  memset(memo, -1, sizeof memo);
  int res = solve((1 << n)-1);
  cout << res << endl;

  return 0;
}

#include <bits/stdc++.h>
#include <vector>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 6000005
#define MOD 1000000007
#define SMOD 998244353
#define GMAX 19
#define INF 2000000000000000000
#define EPS 0.0000000001
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
int n;
int Graph[GMAX];
bool possibility[MAX];
int components[MAX];
int mcom(int mask)
{
    if(components[mask]!=-1)
    {
        return components[mask];
    }
    components[mask]=n;
    int i;
    for(i=mask;i>0;i=mask&(i-1))
    {
        if(possibility[i]) components[mask]=min(components[mask],1+mcom(mask^i));
    }
    return components[mask];
}
int main()
{
    int i,m,j;
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
    {
        int mask=1<<i;
        Graph[i]=Graph[i]|mask;
    }
    for(i=0;i<m;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        u--;
        v--;
        int U=1<<u;
        int V=1<<v;
        Graph[u]=Graph[u]|V;
        Graph[v]=Graph[v]|U;
    }
    m=1<<n;
    for(i=0;i<m;i++)
    {
        int mask=i;
        for(j=0;j<n;j++)
        {
            int tmask=1<<j;
            tmask=tmask&mask;
            if(tmask==0) continue;
            tmask=mask;
            tmask=tmask&Graph[j];
            if(tmask!=mask) break;
        }
        possibility[i]=(j>=n);
        components[i]=-1;
    }
    components[0]=0;
    j=mcom(m-1);
    printf("%d",j);
    return 0;
}


#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x) \
	for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
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;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << "(" << p.first << " " << p.second << ")";
	return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	rep(i, v.size()) {
		if (i) os << " ";
		os << v[i];
	}
	return os;
}
template <class T, class U>
istream& operator>>(istream& is, pair<T, U>& p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
	rep(i, v.size()) { is >> v[i]; }
	return is;
}
const long long INFLL = 1LL << 60;
const int INF         = 1 << 30;
const double PI       = acos(-1);
#ifdef LOCAL
#define dbg(x) cerr << #x << ": " << (x) << '\n'
#define say(x) cerr << (x) << '\n'
#else
#define dbg(x)
#define say(x)
#endif
//ダイクストラ法:スタートから任意の地点での最短距離、最短パスを求める
//計算量:(頂点+辺)log(頂点)
//始点,終点,隣接リスト(pathlist[edge.A]=(edgeB,legth))-->距離(notfound:INFLL),previous(notfound:INF);
vector<pair<long long, int>> Dikstra(
    int start, vector<vector<pair<int, long long>>>& pathlist) {
	int edge = pathlist.size();
	pair<long long, int> cerching;
	priority_queue<pair<long long, int>, vector<pair<long long, int>>,
	               greater<pair<long long, int>>>
	    q;  // start to distance,[ditance ,islandNo.]
	vector<pair<long long, int>> D(
	    edge, mp(INFLL, INF));  // distance (ans), preious path
	vector<bool> had(edge, false);
	D[start] = mp(0, -1);  // distance ,Previous egdg
	q.emplace(0LL, start);
	while (!q.empty()) {
		cerching = q.top();
		q.pop();
		if (had[cerching.second] == false) {
			had[cerching.second] = true;
			for (auto i = pathlist[cerching.second].begin();
			     i != pathlist[cerching.second].end(); i++) {
				if (had[i->first] == false) {
					if (chmin(D[i->first].F, (cerching.first) + (i->second))) {
						D[i->first].S = cerching.second;
					}
					q.emplace(D[i->first].F, i->first);
				}
			}
		}
	}

	return D;
}
string solve(bool a) { return ((a) ? "Yes" : "No"); }
int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<vector<pair<int, long long>>> path(n);
	VV dis(n, V(n, INFLL));
	int a, b, c;
	rep(i, m) {
		cin >> a >> b >> c;
		path[a - 1].emplace_back(b - 1, c);
		chmin(dis[a - 1][b - 1], (ll)c);
	}
	vector<pair<long long, int>> res;
	rep(i, n) {
		res = Dikstra(i, path);
		rep(j, n) {
			if (res[j].F == 0) continue;

			chmin(dis[i][j], res[j].F);
		}
	}
	rep(i, n) {
		ll ans = INFLL;
		chmin(ans, dis[i][i]);
		rep(j, n) { chmin(ans, dis[i][j] + dis[j][i]); }
		if (ans == INFLL) {
			cout << -1 << endl;
		} else {
			cout << ans << endl;
		}
	}
}

#include <bits/stdc++.h>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef long int li;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test ll t; cin >> t; while(t--)
const long long int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
const long long int cons = 100005, ncr_cons = 100005;
const long long int MOD = 1000000007; // const long long int MOD = 998244353;
const long long int const_INT_MAX = 1000000000000000000, const_INT_MIN = -1 * 1000000000000000000;
using namespace std;
bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.first > b.first);}
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second < b.second);}
bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second > b.second);}
ll gcd(ll x, ll y){return (ll)(__gcd(x, y));}
ll lcm(ll x, ll y){return (ll)((x * y) / gcd(x, y));}
ll mod_expo(ll x, ll y, ll p){
    if((x + p) % p == 0) return 0;
    ll res = 1; x = (x + p) % p;
    while (y > 0){
        if(y & 1) res = (res*x + p) % p;
        y = y>>1; x = (x*x + p) % p;
    }
    return res;
}
void usaco(string str = ""){
    fastio;
    if(str.size()){
        freopen((str + ".in").c_str(), "r", stdin);
        freopen((str + ".out").c_str(), "w", stdout);
    }
}
vector<pair<ll, ll>> adj[cons];
vector<ll> par, dist;
void dijkstra(ll n, ll source){
	par.assign(n + 1, -1); dist.assign(n + 1, const_INT_MAX);
	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> p;
	p.push(make_pair(0, source)); dist[source] = 0;
	while(p.size() > 0){
		pair<ll, ll> v = p.top(); p.pop();
		if(dist[v.second] < v.first) continue; // already initialised
		else {
			for(auto u : adj[v.second]){
				if(dist[v.second] + u.second < dist[u.first]){
					dist[u.first] = dist[v.second] + u.second; par[u.first] = v.second;
					p.push(make_pair(dist[u.first], u.first));
				}
			}
		}
	}
}
int main(){
    usaco();
    ll n, m; cin >> n >> m;
    vector<ll> fin_ans(n, const_INT_MAX);
    for(ll i=0;i<m;i++){
    	ll a, b, c; cin >> a >> b >> c; a--; b--;
    	if(!(a - b)) fin_ans[a] = min(fin_ans[a], c);
    	else {
    		adj[a].push_back(make_pair(b, c));
    	}
    }
    for(ll i=0;i<n;i++){
    	for(auto u : adj[i]){
    		dijkstra(n, (ll)(u.first));
    		fin_ans[i] = min(fin_ans[i], u.second + dist[i]);
    	}
    }
    for(ll i=0;i<n;i++){
    	if(!(fin_ans[i] - const_INT_MAX)) cout << -1 << "\n";
    	else cout << fin_ans[i] << "\n";
    }
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}


#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int, int> P;
#define MAX_N 200005
const ll PERFECT = 100000000;
static uint32_t randxor() {
    static uint32_t x = 123456789;
    static uint32_t y = 362436069;
    static uint32_t z = 521288629;
    static uint32_t w = 88675123;
    uint32_t t;

    t        = x ^ (x << 11);
    x        = y;
    y        = z;
    z        = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

// 0以上1未満の小数をとる乱数
static double rand01() { return (randxor() + 0.5) * (1.0 / UINT_MAX); }

int n, m;
string s[1000];
vector<int> a[1000];
int b[1000] = {};
int ans[20][20];
ll ma;

ll calc() {
    int cnt = 0;
    rep(k, m) {
        int ok = 0;
        rep(i, n) {
            rep(j, n) {
                ok = 1;
                rep(l, b[k]) {
                    if (ans[i][(j + l) % n] != a[k][l]) {
                        ok = 0;
                        break;
                    }
                }
                if (ok) break;
                ok = 1;
                rep(l, b[k]) {
                    if (ans[(i + l) % n][j] != a[k][l]) {
                        ok = 0;
                        break;
                    }
                }
                if (ok) break;
            }
            if (ok) break;
        }
        cnt += ok;
    }
    ll res = 0;
    if (cnt < m) {
        res = PERFECT * cnt / m;
    } else {
        int cnt2 = 0;
        rep(i, n) {
            rep(j, n) {
                if (ans[i][j] == 0) cnt2++;
            }
        }
        res = PERFECT * 2 * n * n / (2 * n * n - cnt2);
    }
    return res;
}

int main() {
    srand((unsigned)time(NULL));
    while (rand() % 100) randxor();

    string fileNameIfs = "in\\0000.txt";
    ifstream ifs(fileNameIfs.c_str());
    if (!ifs.is_open()) {  // 標準入力する
        cin >> n >> m;

        rep(i, m) cin >> s[i];

        rep(i, m) {
            b[i] = s[i].size();
            rep(j, b[i]) { a[i].push_back(s[i][j] - 'A' + 1); }
        }
    } else {  // ファイル入力する
        ifs >> n >> m;

        rep(i, m) ifs >> s[i];

        rep(i, m) {
            b[i] = s[i].size();
            rep(j, b[i]) { a[i].push_back(s[i][j] - 'A' + 1); }
        }
    }

    rep(i, 20) {
        rep(j, 20) { ans[i][j] = randxor() % 8 + 1; }
    }

    clock_t start, end;
    start = clock();

    ma = calc();

    int loop = 0;
    while (true) {
        loop++;
        int x   = randxor() % n;
        int y   = randxor() % n;
        int val = randxor() % 9;

        int keep  = ans[x][y];
        ans[x][y] = val;

        ll tmp = calc();
        if (tmp >= ma) {
            ma = tmp;
        } else {
            ans[x][y] = keep;
        }

        end = clock();
        if ((double)(end - start) / CLOCKS_PER_SEC > 2.9) break;
    }

    // cout << loop << endl;
    // cout << calc() << endl;

    rep(i, n) {
        rep(j, n) {
            if (ans[i][j] == 0) {
                cout << '.';
            } else {
                cout << (char)(ans[i][j] + 'A' - 1);
            }
        }
        cout << endl;
    }
    return 0;
}

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

const int mod = 1e9 + 7;
int n, m, ops[2000][2000][4];
string s[2000];

int modpow(int a, int b){
	if(!b) return 1;
	long long res = modpow(a, b / 2);
	res = (res * res) % mod;
	if(b % 2) res = (res * a) % mod;
	return res;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	long long open = 0;
	for(int i = 0; i < n; i++){
		cin >> s[i];
		for(char j : s[i])
			if(j == '.') open++;
	}
	//left
	for(int i = 0; i < n; i++){
		int cur = 0;
		for(int j = 0; j < m; j++)
			if(s[i][j] == '#') cur = 0;
			else ops[i][j][0] = cur++;
	}
	//right
	for(int i = 0; i < n; i++){
		int cur = 0;
		for(int j = m - 1; j >= 0; j--)
			if(s[i][j] == '#') cur = 0;
			else ops[i][j][2] = cur++;
	}
	//up
	for(int i = 0; i < m; i++){
		int cur = 0;
		for(int j = 0; j < n; j++)
			if(s[j][i] == '#') cur = 0;
			else ops[j][i][1] = cur++;
	}
	//down
	for(int i = 0; i < m; i++){
		int cur = 0;
		for(int j = n - 1; j >= 0; j--)
			if(s[j][i] == '#') cur = 0;
			else ops[j][i][3] = cur++;
	}
	long long ans = (open * modpow(2, open)) % mod;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++){
			if(s[i][j] == '#') continue;
			int rem = modpow(2, open - ops[i][j][0] - ops[i][j][1] - ops[i][j][2] - ops[i][j][3] - 1);
			ans = ((ans - rem) % mod + mod) % mod;
		}
	cout << ans << '\n';
	return 0;
}

/*
find what scenarios where none activated
*/


// give up
#include <bitset>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <list>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <valarray>

// alias, using declaration
using llint = long long; using ldouble = long double;
template <class T = llint> using vector = std::vector<T>;
template <class T = llint> using deque = std::deque<T>;
template <class T = llint> using list = std::list<T>;
template <class T = llint, class U = llint> using umap = std::unordered_map<T, U>;
template <class T = llint, class U = llint> using ummap = std::unordered_multimap<T, U>;
template <class T = llint> using uset = std::unordered_set<T>;
template <class T = llint> using umset = std::unordered_multiset<T>;
template <class T = llint, class U = T> using pair = std::pair<T, U>;
template <class T = llint> using varray = std::valarray<T>;
template <class T = llint, class U = vector<T>, class V = std::less<class U::value_type>> using pqueue = std::priority_queue<T, U, V>;
using std::array; using std::bitset; using std::string; using std::tuple;

// constant
constexpr llint INF = 0x7FFFFFFFFFFFFFFF;
constexpr llint INVALID = 0x8000000000000000;

namespace io {
  // in
  template <class... T, size_t... S> auto in(std::index_sequence<S...>) {tuple<T...> t; (..., (std::cin >> std::get<S>(t))); return t;}
  template <class... T, size_t... S> auto in(std::index_sequence<S...>, size_t n) {tuple<vector<T>...> t{vector<T>(n)...}; for (size_t i = 0; i < n; i++) (..., (std::cin >> std::get<S>(t)[i])); return t;}
  template <size_t N, class T = llint, class... U> auto in() {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>(std::index_sequence_for<T, U...>());}
  template <size_t N, class T = llint, class... U> auto in(size_t n) {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(std::index_sequence_for<T, U...>(), n);}
  template <size_t N, class T = llint> auto in(size_t n, size_t m) {static_assert(N == 1); vector<vector<T>> v(n, vector<T>(m)); for (auto&& vi : v) for (auto&& vij : vi) std::cin >> vij; return std::make_tuple(v);}
  // out
  template <class T, class... U> void out(const T& a, const U&... b) {std::cout << a << (sizeof...(b) ? " " : "\n"); if constexpr (sizeof...(b)) out(b...);}
  template <class T, class U = const char*> void out(const vector<T>& a, U d = " ") {for (auto&& b : a) std::cout << b << (&b != &a.back() ? d : "\n");}
}

int main() {
  // input
  auto [N, M, Q] = io::in<3>();
  auto [T, X, Y] = io::in<3>(Q);

  // solve
  vector<> a(N + 1), b(M + 1);
  umap<> Z;
  {
    Z.emplace(0, 0);
    vector<> Y_ = Y;
    std::sort(Y_.begin(), Y_.end());
    for (llint i = 0; i < Q; i++) Z.emplace(Y_.at(i), i + 1);
  }
  constexpr llint ST_SIZE = 1 << 19;
  vector<pair<>> st_a(ST_SIZE), st_b(ST_SIZE);
  auto update = [&Z](vector<pair<>>& st, llint v, llint n) {
    for (llint i = (ST_SIZE >> 1) + Z.at(v); i > 0; i >>= 1) {
      st.at(i).first += n;
      st.at(i).second += v * n;
    }
  };
  auto culc_line = [&Z](const vector<pair<>>& st, llint v) {
    llint r = st.at((ST_SIZE >> 1) + Z.at(v)).second;
    for (llint i = (ST_SIZE >> 1) + Z.at(v); i > 1; i >>= 1) {
      r += i & 1 ? st.at(i ^ 1).first * v : st.at(i ^ 1).second;
    }
    return r;
  };
  update(st_a, 0, N);
  update(st_b, 0, M);
  vector<> result(Q);
  for (llint i = 0; i < Q; i++) {
    auto& t = (T.at(i) == 1 ? a : b).at(X.at(i));
    auto& st_x = T.at(i) == 1 ? st_a : st_b;
    auto& st_y = T.at(i) == 1 ? st_b : st_a;
    update(st_x, t, -1);
    result.at(i) = -culc_line(st_y, t);
    t = Y.at(i);
    update(st_x, t, 1);
    result.at(i) += culc_line(st_y, t);
  }
  for (llint i = 1; i < Q; i++) result.at(i) += result.at(i - 1);

  // output
  io::out(result, "\n");
}

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 200010

using namespace std;

struct segtree {
    struct Node {
        Node *l, *r;
        int cnt;
        ll sum;

        Node() {
            l = r = nullptr;
            cnt = sum = 0;
        }

        Node operator+(const Node &b) const {
            Node res;
            res.cnt = this->cnt + b.cnt;
            res.sum = this->sum + b.sum;
            return res;
        }
    };

    deque<Node> dq;
    segtree() {
        dq.push_back(Node());
    }

    Node *new_node() {
        dq.push_back(Node());
        return &dq.back();
    }

    Node get(Node *p) {
        return p ? *p : Node();
    }

    void upd(int x, int y) { upd(&dq[0], 0, 1e8, x, y); }
    void upd(Node *p, int l, int r, int x, int y) {
        if (l == r) {
            p->cnt += y;
            p->sum += x * y;
        } else {
            int m = (l + r) / 2;
            if (x <= m) {
                if (!p->l) p->l = new_node();
                upd(p->l, l, m, x, y);
            } else {
                if (!p->r) p->r = new_node();
                upd(p->r, m + 1, r, x, y);
            }

            Node res = get(p->l) + get(p->r);
            p->cnt = res.cnt;
            p->sum = res.sum;
        }
    }

    Node qry(int x, int y) { return qry(&dq[0], 0, 1e8, x, y); }
    Node qry(Node *p, int l, int r, int x, int y) {
        if (p == nullptr || r < x || l > y) return Node();
        if (x <= l && r <= y) {
            return *p;
        }
        int m = (l + r) / 2;
        return qry(p->l, l, m, x, y) + qry(p->r, m + 1, r, x, y);
    }
};

segtree a, b;
ll cur_a[MAXN];
ll cur_b[MAXN];

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, m, q;
    cin >> n >> m >> q;

    a.upd(0, n);
    b.upd(0, m);
    ll ans = 0;

    while (q--) {
        int t, x, y;
        cin >> t >> x >> y;
        x--;
        if (t == 1) {
            auto res = b.qry(cur_a[x], 1e8);
            ans -= res.sum;
            ans -= (m - res.cnt) * cur_a[x];

            a.upd(cur_a[x], -1);
            cur_a[x] = y;
            a.upd(cur_a[x], 1);

            res = b.qry(cur_a[x], 1e8);
            ans += res.sum;
            ans += (m - res.cnt) * cur_a[x];
        } else {
            auto res = a.qry(cur_b[x], 1e8);
            ans -= res.sum;
            ans -= (n - res.cnt) * cur_b[x];

            b.upd(cur_b[x], -1);
            cur_b[x] = y;
            b.upd(cur_b[x], 1);

            res = a.qry(cur_b[x], 1e8);
            ans += res.sum;
            ans += (n - res.cnt) * cur_b[x];
        }

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

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define s(x) x.size()
#define endl '\n'
#define r(n) ll n; cin>>n;
#define vl vector<ll>
#define all(x) (x).begin(),(x).end()
#define fk(i,start,end) for(ll i=start;i<ll(end);i++)
typedef long long int ll;
const ll MOD = 1e9 + 7;
string A="YES";
string B="NO";

void solution()
{
    std::vector<ll> v(3);
    fk(i,0,3)
    {
        cin>>v[i];
    }
    sort(all(v));
    cout<<v[1]+v[2];

}

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

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

    solution();
    return 0;
}

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

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

  int sam =0;
  int i=1;

  while(sam < N){
    sam += i;
    i++;
  }
  cout<< i-1 <<endl;
}


#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

template <class T>
void CVEC(const T &v) {
	int c = v.size() - 1;
	for (int i = 0; i < c; i++) cout << v[i] << ' ';
	if (c > -1) cout << v[c];
	cout << '\n';
}

#pragma endregion header

const int INF = 110110110;

signed main(){
    int n; cin >> n;
	vector<int> t(n);
	rep(i,n) cin >> t[i];
	int sum = accumulate(ALL(t), 0);
	vector<vector<int>> dp(110, vector<int>(100010, INF));
	dp[0][0] = 0;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < 100010; j++){
			if(dp[i][j] != INF){
				dp[i + 1][j + t[i]] = min(dp[i + 1][j + t[i]], dp[i][j]);
				dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + t[i]);
			}
		}
	}
	int ans = INF;
	for(int j = 0; j < 100010; j++) ans = min(ans, max(j, dp[n][j]));
	cout << ans << endl;
}

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

const int N = 105,M = 100005;

int f[N][M];//f[i][j] = 从1~i种中选，且总和正好为j的方案是否存在
int n;
int times[N];//烹饪时间

int main()
{
    ios::sync_with_stdio(0);
    cin>>n;

    int tot_time = 0;//总时间
    for(int i=1;i<=n;i++) cin>>times[i],tot_time+=times[i];

    for(int i=1;i<=n;i++)
    {
        f[i][times[i]] = 1;
        for(int j=times[i]+1;j<=tot_time;j++)
        {
            f[i][j] = max(f[i-1][j],f[i-1][j-times[i]]);
        }
    }

    int ans = 0x3f3f3f3f;
    for(int i=1;i<=n;i++)
    {
        for(int j=times[i];j<=tot_time;j++)
        {
            if(f[i][j]) ans = min(ans,max(j,tot_time-j));
        }
    }

    cout<<ans;

    return 0;
}


#include<stdio.h>
int main()
{
	int min,i,a[4];
	for(i=0;i<4;i++)
	{
		scanf("%d",&a[i]);
	}
	min=a[0];
	for(i=1;i<4;i++)
	{
		if(a[i]<min)
		min=a[i];
	}
	printf("%d",min);

}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a,b,c;
    scanf("%c %c %c",&a,&b,&c);
    if((a==b)&&(b==c)&&(a==c))
    {
        if((a>='A'&&a<='Z')&&(b>='A'&&b<='Z')&&(c>='A'&&c<='Z'))
        {
            printf("Won\n");
        }
    }
    else
    {
        printf("Lost\n");
    }
    return 0;
}


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

struct unionfind{
    vector<int> parent;
    vector<int> rank;
    vector<int> size;

    unionfind (int N) {
        parent = vector<int>(N);
        iota(parent.begin(), parent.end(), 0);
        rank = vector<int>(N, 0);
        size = vector<int>(N, 1);
    }
    int root(int x) {
        if (parent[x] == x) return x;
        parent[x] = root(parent[x]);
        return parent[x];
    }

    bool same(int x, int y){
        return root(x) == root(y);
    }

    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (rank[x] > rank[y]) {
            parent[y] = x;
            size[x] += size[y];
        }
        else {
            parent[x] = y;
            size[y] += size[x];
            if (rank[x] == rank[y]) rank[y] += 1;
        }
    }

    int getsize(int x) {
        return size[root(x)];
    }
};

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> grid(H);
    for (int i = 0; i < H; i++) cin >> grid[i];
    unionfind uf(H+W);
    uf.unite(0, H);
    uf.unite(H-1, H);
    uf.unite(0, H+W-1);
    uf.unite(H-1, H+W-1);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (grid[i][j] == '#') {
                uf.unite(i, H+j);
            }
        }
    }
    int ans = 0;
    int x = 0, y = 0;
    for (int i = 0; i < H+W; i++) {
        if (uf.root(i) == i) ans++;
        if (uf.getsize(i) == 1) {
            if (i < H) x++;
            else y++;
        }
    }
    cout << ans-max(x, y)-1 << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
using P = pair<int, int>;

int main()
{
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m)cin >>a[i];
  sort(a.begin(), a.end());
  a.push_back(n + 1);
  int car =1;
  vector<int>s;
  rep(i, m + 1)
  {
    int w = a[i] - car;
    if (w != 0)
      s.push_back(w);
    car = a[i] + 1;
  }
  int k = n;
  for (int w : s)
    k = min(k, w);
int ans=0;
  for (int w : s)
    ans += (w + k - 1) / k;

  cout << ans << endl;

  return 0;
}


#include <bits/extc++.h>
using namespace std;
using ll = long long;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define ALL(x) x.begin(),x.end()
#define MOD (ll)1000000007

signed main() {
  ll n;
  cin >> n;
  cout << n - 1;
  return 0;
}

#include<iostream>
using namespace std;
int main()
{
    int N;
    cin>>N;
    int w;
    w=N-1;
    cout<<w<<endl;
    return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

//head

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> cnt(n+1);
  rep(i, n) {
    if(!i) cnt[1] = a[0];
    else cnt[i+1] = cnt[i]+a[i]-a[i-1]-1;
  }
  rep(i, q) {
    ll k;
    cin >> k;
    auto itr = upper_bound(all(cnt), k);
    cout << k+(itr-cnt.begin())-1 << '\n';
  }
}

#include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;
    template<class t>
    void output(t a){
        if(was_output)cout << " ";
        cout << a;
        was_output = true;
    }
    void outendl(){
        was_output = false;
        cout << endl;
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

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

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    void out(t x){
        cout << x;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

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

using namespace templates;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string str = in<string>();
    foreach(i,str){
        if(i=='.')break;
        cout <<i;
    }
    cout << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	if(a+b>=15&&b>=8)cout<<1;
	else if(a+b>=10&&b>=3)cout<<2;
	else if(a+b>=3)cout<<3;
	else cout<<4;
	return 0;
}

//by azidoazideazide(C2N14)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for (ll i = a; i < (b); i++)
#define FORE(i, a, b) for (ll i = a; i <= (b); i++)
void listv(int st, int ed, vector<int> v);

ull llpow(ull x, ull y){
	if(y==0) return 1;
	ull tt=llpow(x,y>>1);
	return (y&1) ? x*tt*tt : tt*tt;
}

//ifstream fin("test1.txt");
//ofstream fout("test1.txt");

int main(){
//	ios::sync_with_stdio(0), cin.tie(0);
	int fat, solid, b;
	cin>>b>>fat;
	solid=b+fat;
	if(solid>=15 && fat>=8) puts("1");
	else if(solid>=10 && fat >=3) puts("2");
	else if(solid>=3) puts("3");
	else puts("4");
}

void listv(int st, int ed, vector<int> v){
	FORE(i, st, ed) cout<<v[i]<<' '; cout<<endl;
}


#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i < (int)(r); i++)
#define all(vec) vec.begin(), vec.end()
#define allr(vec) vec.rbegin(), vec.rend()
using namespace std;

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

using ll = long long;

using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vi = vector<int>;
using vll = vector<ll>;

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

  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  int ans = 3;
  int a = abs(r2 - r1), b = abs(c2 - c1);
  if (a == 0 && b == 0) {
    ans = 0;
  } else if (a == b || a + b <= 3) {
    ans = 1;
  } else if ((a + b) % 2 == 0 || a + b <= 6 || abs(a - b) <= 3) {
    ans = 2;
  }

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

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

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


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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define int long long int

#define vi vector<int>

#define ff first
#define ss second

#define pb push_back
#define MOD 1000000007
//const int nax = 1e6+5;
#define inf 4e18
#define Graph vector<vi>
#define Graphw vector<vector<pair<int,int>>>
#define sz(x) (int)x.size()
#define pr pair<int,int>

#define display(v) for(int i =0;i<(int)v.size();++i)cout << v[i] << " "
#define all(v) v.begin(),v.end()
#define read(a) for(int i = 0;i<(int)a.size();++i)cin>>a[i]
#define show(x) cout << #x << " is " << x << "\n";
// typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>   ind_si;
// typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>   ind_msi;
#define show_arr(a) cout << #a << "\n"; display(a) ; cout << "\n";
inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; }
inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; }
inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; }
const int nax = 300*1000 + 10;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

//#define ook order_of_key
//#define fbo find_by_order
//   order_of_key(x) -> number of elements less than x.
// * find_by_order(x) -> gives the element at index x (0 - indexing)

void test_case();
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1; //cin>>t;
    while(t--){
        test_case();
    }
    return 0;
}

void test_case(){
    int m , h ; cin>>m>>h;
    if(h%m == 0){
        cout << "Yes";
    }
    else{
        cout << "No";
    }
}

// Химаншу
// check overflow
// special case n == 1
// 🍪🍪🍪

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define f first
#define s second
#define mk make_pair
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=1;
  //cin>>t;
  while(t--)
  {
    int m,h;
    cin>>m>>h;
    if(h%m==0)
      cout<<"Yes\n";
    else
      cout<<"No\n";
  }//while
}//main


#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { return a < b ? a = b, true : false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { return a > b ? a = b, true : false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void _co() { cout << '\n'; }
template <class Head, class... Tail>
void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t INF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);

struct S {
    bool f;
    ll a;

    bool operator<(const S &rhs) {
        if (a < 0 && rhs.a < 0) {
            if (f != rhs.f)
                return !f;
            else
                return a < rhs.a;
        } else if (a >= 0 && rhs.a >= 0) {
            if (f != rhs.f)
                return f;
            else
                return a < rhs.a;
        } else {
            return a < 0;
        }
    }

    S &operator+=(const S &rhs) {
        if (f)
            a -= rhs.a;
        else
            a += rhs.a;
        f ^= rhs.f;
        return *this;
    }
};

int main(void) {
    ll n;
    cin >> n;
    Vec p(n - 1);
    cin >> p;
    rep(i, n - 1) p[i]--;
    Vec2<ll> c(n);
    rep(i, n - 1) c[p[i]].push_back(i + 1);

    auto dfs = [&](auto self, ll r) -> S {
        S res = S{false, 0};
        res.a++;
        vector<S> s;
        for (auto i : c[r]) {
            s.push_back(self(self, i));
        }
        sort(all(s));
        for (auto i : s) res += i;
        res.f ^= true;
        return res;
    };

    auto ans = dfs(dfs, 0);
    co((n + ans.a) / 2);

    return 0;
}

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

#define fl(n)             for(long long int i=0;i<n;i++)
#define sz(a)             int((a).size())
#define tr(c,i)           for(auto i = (c).begin(); i != (c).end(); i++)
#define trr(c,i)          for(auto i = (c).rbegin(); i != (c).rend(); i++)
#define present(c,x)      ((c).find(x) != (c).end())
#define cpresent(c,x)     (find(all(c),x) != (c).end())
#define vi                vector <int>
#define vvi               vector < vi >
#define pb                push_back
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define fr                first
#define sc                second
#define vc                vector
#define pii               pair<ll,ll>
#define msi               map<string,int>
#define mii               map<int,int>
#define psi               pair<string,int>
#define pis               pair<int,string>
#define M                 ((long long)(1000000007))

bool inrange(pii p1, pii p2)
{
	if(p1.fr+p1.sc==p2.fr+p2.sc)
		return true;
	if(p1.fr-p1.sc==p2.fr-p2.sc)
		return true;
	if(abs(p1.fr-p2.fr)+abs(p1.sc-p2.sc)<=3)
		return true;
	return false;
}

int solve()
{
	pii st,en;
	cin>>st.fr>>st.sc;
	cin>>en.fr>>en.sc;
	if(st==en)
		return cout<<"0\n",0;
	if(inrange(st,en))
		return cout<<"1\n",0;
	if(((st.fr+st.sc)&1)==((en.fr+en.sc)&1))
		return cout<<"2\n",0;
	for(ll i=st.fr-3;i<=st.fr+3;i++)
	{
		for(ll j=st.sc-3;j<=st.sc+3;j++)
		{
			if(abs(i-st.fr)+abs(j-st.sc)>3)
				continue;
			if(inrange(en,{i,j}))
			{
				return cout<<"2\n",0;
			}
		}
	}
	cout<<"3\n",0;
	return 0;
}

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 t=1;
  //cin>>t;
  while(t--)
  {
  	solve();
  }
  return 0;

}


#include <bits/stdc++.h>
using namespace std;
int tt(int n){
    if(n<1)
    return 0;
    return n+tt(n-1);
}
int main()
{
    int n,k;
    cin>>n>>k;
    int s=0;
    int a=100;
    int r=tt(k);

    while(n--){
            s=s+( a*k) + r;
            a=a+100;
    }
    cout<<s;
    return 0;
}

//int a = s - '0'; 文字から数字
//小文字から大文字
//transform(a.begin(), a.end(), a.begin(), ::toupper);
//map 全探索
//auto begin = p.begin(), end = p.end();
//for (auto it = begin; it != end; it++) {}
//mapのキー：it->first mapのバリュー：it->second
//大文字判定 isupper(文字) 小文字判定 islower(文字)
//do{}while(next_permutation(ALL(配列)))
//小文字に対応する文字コード：S[i] - 'a'
//文字コード→小文字：(char)(数字+'a')
//グラフの距離:隣接行列で扱う
//絶対値はソートで取れる
//mapで出現チェックするときは++じゃなくて1にする
//島渡りの問題：中間ノードに着目
//数が大きい時の比較はstring型で行う
//全て0になったか調べたい->0になるたびにcntする
//例外処理は最初にする
//x = p^m × q^n ×...の約数の個数:(n+1)*(m+1)....
//N!のどの素因数で何回割れるか
//⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和
//パズルの問題->一般化して全探索
//stack<ll> s;
//s.push(要素);s.top();s.pop();
//queue<ll> q;
//q.push(要素);q.front();q.pop();
//同じ作業繰り返す系の問題：収束先を見つける
//過半数：N/2.0で判定
//優先度付きキュー
//priority_queue<
//ll,
//vector<ll>
//> q;
#include <bits/stdc++.h>
#define rep(i,N) for(int i = 0; i < N; i++)
#define ALL(a) (a).begin(),(a).end()
#define ll long long int
#define PI 3.14159265358979323846264338327950L
using namespace std;

const ll MOD (pow(10, 9) + 7);

// K進数でのNの桁数
ll dig(ll N, ll K) {
	ll dig = 0;
	while (N) {
		dig++;
		N /= K;
	}
	return dig;
}
// a,bの最大公約数
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

//a,bの最小公倍数
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}

//階乗計算
ll f(ll n) {
	if (n == 0 || n == 1) return 1;
	else return (n * f(n - 1));
}

//Nのd桁目の数
ll dignum(ll N, ll d) {
	ll x = pow(10, d);
	N %= x;
	ll y = pow(10, d - 1);
	N /= y;
	return N;
}

//Nをdで何回割れるか
ll divcnt(ll N, ll d) {
	ll ans = 0;
	while (1) {
		if (N%d == 0) {
			ans++;
			N /= d;
		}
		else break;
	}

	return ans;
}

//素数判定
bool prime(ll num) {
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false;
	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2) {
		if (num % i == 0) return false;
	}
	return true;
}

//フィボナッチ数列
vector<ll> memo(pow(10, 6) + 1);
ll fibo(ll n) {
	if (n == 1) return 1;
	else if (n == 2) return 1;
	else if (memo[n] != 0) return memo[n];
	else return memo[n] = fibo(n - 1) + f(n - 2);
}

ll RS(ll N, ll P, ll M) {
	if (P == 0) return 1;
	if (P % 2 == 0) {
		ll t = RS(N, P / 2, M);
		return t * t % M;
	}
	return N * RS(N, P - 1, M);
}

vector<int> IntegerToVector(int bit, int N) {
    vector<int> S;
    for (int i = 0; i < N; ++i) {
        if (bit & (1 << i)) {
            S.push_back(i);
        }
    }
    return S;
}

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

	map<ll,ll> mod_cnt;
	for(int i = 0; i < N; i++) mod_cnt[A[i]%200]++;

	ll ans = 0;
	for(int n = 0; n <= 199; n++){
		ans += mod_cnt[n] * (mod_cnt[n]-1) / 2;
	}

	cout << ans << endl;
}


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

using ll = long long;

constexpr int inf32 = (1 << 30) - 1;
constexpr ll inf64 = (1LL << 62) - 1LL;
constexpr int mod = 1e9 + 7;

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

int main(){
	//cout << fixed << setprecision(10);
	int n, m; cin >> n >> m;
	vector<string> vecs(m);
	rep(i, m) cin >> vecs[i];

	rep(i, n) {
		string s;
		while (true) {
			int r = rand() % m;
			s += vecs[r];
			if (s.length() > n)
				break;
		}
		s = s.substr(0, n);
		cout << s << endl;
	}
	return 0;
}

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <chrono>
#include <random>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
// typedef pair<int,P> IP;
// typedef pair<P,P> PP;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;

int N,M;
string S[800];

void solve(vector<vector<char>> &ans){
    rep(i,N){
        rep(j,S[i].size()){
            ans[i][j]=S[i][j];
        }
    }
}

int main(){
    cin>>N>>M;
    rep(i,M) cin>>S[i];

    vector<vector<char>> ans(N,vector<char>(N,'.'));

    solve(ans);

    rep(i,N){
        rep(j,N) cout<<ans[i][j];
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#include<cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll>
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for(ll i=0; i<n; i++)
#define forr1(i, n) for(ll i=1; i<=n; i++)
using namespace std;
const ld PI =3.1415926535897923846;
const ll MOD = 1000000007;
const ll N=998244353;
ll power(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;}
ll modinverse(ll a){return power(a, MOD-2);}
void solve()
{
    ll n, k;
    cin>>n>>k;
    vector<vl> pos(n, vl(2, 0));
    forr(i, n)
    {
    	cin>>pos[i][0]>>pos[i][1];
    }
    sort(all(pos));
    ll po=0;
    ll money=k;
    forr(i, n)
    {
    	if(pos[i][0]-po<=money)
    	{
    		money-=(pos[i][0]-po);
    		po=pos[i][0];
    		money+=pos[i][1];
    	}
    	else
    	{
    		po+=money;
    		cout<<po<<endl;
    		return;
    	}
    }
    cout<<po+money<<endl;
}
int main()
{
	#ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}

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

#define LL long long
#define pii pair<LL, LL>

int cnt;
LL B, C;

pii a[5];

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

	a[++ cnt] = pii(B, B + (C - 2) / 2);
	a[++ cnt] = pii(-B, -B + (C - 1) / 2);
	a[++ cnt] = pii(B - C / 2, B);
	a[++ cnt] = pii(-B - (C - 1) / 2, -B);

	sort(a + 1, a + cnt + 1);

	int i = 1;
	LL ans = 0, r;
	while(i <= cnt)
	{
		int j = i + 1;
		r = a[i].second;
		while(j <= cnt && a[j].first <= r) r = max(r, a[j].second), j ++;
		ans += r - a[i].first + 1;
		i = j;
	}

	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;

int main() {
    ll n, m, i, j, k;
    ll H, W;
    cin >> H >> W;
    ll N, M;
    cin >> N >> M;
    vector<ll> A(N), B(N), C(M), D(M);
    for(i = 0; i < N; i++) {
        cin >> A[i] >> B[i];
    }
    vector<vector<ll>> blockRow(H + 1), blockColumn(W + 1);

    for(i = 0; i < M; i++) {
        cin >> C[i] >> D[i];
        blockRow[C[i]].push_back(D[i]);
        blockColumn[D[i]].push_back(C[i]);
    }
    for(i = 0; i < H + 1; i++) {
        blockRow[i].push_back(0);
        blockRow[i].push_back(W + 1);
        sort(blockRow[i].begin(), blockRow[i].end());
    }
    for(i = 0; i < W + 1; i++) {
        blockColumn[i].push_back(0);
        blockColumn[i].push_back(H + 1);
        sort(blockColumn[i].begin(), blockColumn[i].end());
    }
    ll grid1[H + 2][W + 2], grid2[H + 2][W + 2];
    for(i = 0; i < H + 2; i++) {
        for(j = 0; j < W + 2; j++) {
            grid1[i][j] = 0;
            grid2[i][j] = 0;
        }
    }
    for(i = 0; i < N; i++) {
        if(blockRow[A[i]].size() == 2) {
            grid1[A[i]][0]++;
            grid1[A[i]][W + 1]--;
        } else {
            auto p = lower_bound(blockRow[A[i]].begin(), blockRow[A[i]].end(), B[i]);
            if(p == blockRow[A[i]].begin()) {
                grid1[A[i]][(*p) + 1]++;
                grid1[A[i]][(*(p + 1))]--;
            } else if(p == blockRow[A[i]].end()) {
                grid1[A[i]][(*(p - 1)) + 1]++;
                grid1[A[i]][(*(p))]--;
            } else {
                grid1[A[i]][(*(p-1)) + 1]++;
                grid1[A[i]][(*(p))]--;
            }
        }

        if(blockColumn[B[i]].size() == 2) {
            grid2[0][B[i]]++;
            grid2[H + 1][B[i]]--;
        } else {
            auto p = lower_bound(blockColumn[B[i]].begin(), blockColumn[B[i]].end(), A[i]);
            grid2[(*(p-1)) + 1][B[i]]++;
            grid2[(*(p))][B[i]]--;
        }
    }
    //cout << grid1[3][0] << endl;
    unordered_set<ll> st;
    for(i = 1; i < H + 1; i++) {
        for(j = 1; j < W + 1; j++) {
            grid1[i][j] += grid1[i][j - 1];
            if(grid1[i][j] > 0) {
                st.insert(i*5000+ j);
            }
        }
    }
    for(i = 1; i < W + 1; i++) {
        for(j = 1; j < H + 1; j++) {
            grid2[j][i] += grid2[j - 1][i];
            if(grid2[j][i] > 0) {
                st.insert(j * 5000 + i);
            }
        }
    }
    cout << st.size() << endl;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define inf LLONG_MAX>>2
#define MAX 1000000
#define mod 1000000007
#define pb push_back
#define f(i,a,n,x) for ((i)=(a);(i)<(n);(i)+=(x))
#define fd(i,a,n,x) for ((i)=(a);(i)>=(n);(i)-=(x))
#define fi first
#define se second
#define mk make_pair
#define pi pair
#define vt vector
#define ms multiset
#define is insert
#define um unordered_map
using namespace std;
void read(vt<ll> &a,ll n){ll i,temp;f(i,0,n,1){cin>>temp;a.pb(temp);}}
ll fa[MAX];
ll power(ll p,ll q){
    ll res=1;
    p%=mod;
    while(q){
        if (q&1)
            res=(res*p)%mod;
        q>>=1;
        p=(p*p)%mod;
    }
    return res;
}
ll fact(ll n){
    if (n<=1)
        return fa[n]=1;
    if (fa[n])
        return fa[n];
    return fa[n]=(n*fact(n-1))%mod;
}
ll ncr(ll n,ll r){
    if (!r)
        return 1;
    return (((fact(n)*power(fact(n-r),mod-2))%mod)*power(fact(r),mod-2))%mod;
}
char a[1500][1500];
void solve(){
    ll h,w,n,m,i,j,x,y,count=0;
    f(i,0,1500,1)
        f(j,0,1500,1)
            a[i][j]='_';
    cin>>h>>w>>n>>m;
    f(i,0,n,1){
        cin>>x>>y;
        a[x-1][y-1]='X';
    }
    f(i,0,m,1){
        cin>>x>>y;
        a[x-1][y-1]='Y';
    }
    f(i,0,h,1){
        bool flag=false;
        f(j,0,w,1){
            if (a[i][j]=='X')
                flag=true;
            if (a[i][j]=='Y' && flag)
                flag=false;
            if (flag && a[i][j]=='_')
                a[i][j]='L';
        }
    }
    f(i,0,h,1){
        bool flag=false;
        fd(j,w-1,0,1){
            if (a[i][j]=='X')
                flag=true;
            if (a[i][j]=='Y' && flag)
                flag=false;
            if (flag && a[i][j]=='_')
                a[i][j]='L';
        }
    }
    f(j,0,w,1){
        bool flag=false;
        f(i,0,h,1){
            if (a[i][j]=='X')
                flag=true;
            if (a[i][j]=='Y' && flag)
                flag=false;
            if (flag && a[i][j]=='_')
                a[i][j]='L';
        }
    }
    f(j,0,w,1){
        bool flag=false;
        fd(i,h-1,0,1){
            if (a[i][j]=='X')
                flag=true;
            if (a[i][j]=='Y' && flag)
                flag=false;
            if (flag && a[i][j]=='_')
                a[i][j]='L';
        }
    }
    f(i,0,h,1)
        f(j,0,w,1)
            if (a[i][j]=='X' || a[i][j]=='L')
                count++;
    cout<<count<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
    return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ud unsigned int
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x3f
#define MAX_INF_VAL 0x3f3f3f3f
#define MAX_INF_VAL_LL 0x3f3f3f3f3f3f3f3f
//#define pi 3.141592653589
#define eps 1e-9
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
//#define p 2173412051LL
//#define sz 2

using namespace std;

template< typename T > void read( T &x ) {
	x = 0;
	char ch = getchar();
	ll f = 1;
	while( !isdigit( ch ) ) {
		if( ch == '-' )
		f *= -1;
		ch = getchar();
	}
	while( isdigit( ch ) ) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	x *= f;
}

struct custom_hash {
    static uint64_t splitmix64( uint64_t x ) {
        x += 0x9e3779b97f4a7c15;
        x = ( x ^ ( x >> 30 ) ) * 0xbf58476d1ce4e5b9;
        x = ( x ^ ( x >> 27 ) ) * 0x94d049bb133111eb;
        return x ^ ( x >> 31 );
    }

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

bool vis[ 210 ];

int main() {
	ios::sync_with_stdio( false );
	cin.tie( 0 ), cout.tie( 0 );
	int t;
	ll n;
	cin >> t >> n;
	for( int i = 1; i <= 100; ++i ) {
		int tmp = 0;
		while( ( tmp + 1 ) * 100 <= ( 100 + t ) * i )
			++tmp;
		vis[ tmp ] = true;
	}
	vector< ll > fk;
	for( int i = 1; i <= 100 + t; ++i )
		if( !vis[ i ] )
			fk.push_back( i );
	ll m = fk.size();
	ll p = n / m, q = n % m;
	if( q == 0 ) {
		--p;
		q = fk.size();
	}
	cout << p * ( 100 + t ) + fk[ q - 1 ];
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0));
using Int = long long;
void solve(int N, string S, vector<int> A) {
    int k = 1e9;
    for (int i = 0; i < N; i++) {
        k = min(k, abs(A[i + 1] - A[i]));
    }
    vector<vector<int>> B(k, vector<int>(N + 1));
    for (int i = 0; i < N + 1; i++) {
        for (int j = 0; j < k; j++) {
            B[j][i] += A[i] / k;
        }
        for (int j = 0; j < A[i] % k; j++) {
            B[j][i] += 1;
        }
    }
    cout << k << '\n';
    for (int j = 0; j < k; j++) {
        for (int i = 0; i < N + 1; i++) {
            cout << B[j][i] << " \n"[i + 1 == N + 1];
        }
    }
}
int main() {
    int N; cin >> N;
    string S; cin >> S;
    vector<int> A(N + 1); for (auto &a : A) cin >> a;
    solve(N, S, A);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (ll i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using ll = long long;
using ld=long double;
using vin=vector<int>;
using vvin=vector<vin>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vst=vector<string>;
using P = pair<ll,ll>;
const int inf=1e9+7;
const ll INF=9e18;
const long double PI = acos(-1.0);
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main(){//cout<<fixed<<setprecision(20);
  int h,w;
  cin>>h>>w;
  vst s(h);
  rep(i,0,h)cin>>s[i];
  int ans=0;
  rep(i,0,h){
   rep(j,0,w){
    if(s[i][j]!='.')continue;
    if(j+1<w&&s[i][j+1]=='.')ans++;
    if(i+1<h&&s[i+1][j]=='.')ans++;
   }
  }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

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

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

signed main() {
  init();
  ll N, M;
  cin >> N >> M;

  vvll to(N);
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].pb(b);
  }
  ll ans = 0;
  rep(i, N) {
    vll dist(N, -1);
    queue<P> q;
    q.push({i, 0});
    while (!q.empty()) {
      ll index = q.front().first;
      ll d = q.front().second;
      q.pop();
      if (dist[index] != -1) continue;
      dist[index] = d;
      for (auto t : to[index]) q.push({t, d + 1});
    }

    rep(i, N) if (dist[i] != -1) ans++;
  }
  ANS;
  return 0;
}


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

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

// size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second

#define dbg(x) cout << "[" << #x << "]: " << x << endl;
#define forn(i,n) for(int i=0; i < n;i++)

using ll = long long;
using db = long double; //pode ser double

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

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

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

const int MAXN = 2e5 + 5;
const int MOD = 1e9+7; //998244353;
const int INF = 0x3f3f3f3f;
const ll INF64 = ll(4e18) + 5;
const db EPS = 1e-9;
const db PI = acos((db)-1);

bool perm(vi &p, int n){
	bool certo = true;

	forn(i,n){
		if(p[i] != i) certo = false;
	}

	return certo;
}

void solve(){
	int n;
	cin >> n;
	vi p(n);

	forn(i,n){
		cin >> p[i];
		p[i] -= 1;
	}

	vi ans;
	int at = 0;
	for(int i = n-1; i >= 3; i--){

		//colocando i em seu devido lugar
		//achando onde esta i
		int pos_i = -1;
		for(int j = 0; j <= i; j++){
			if(p[j] == i) pos_i = j;
		}
		if(pos_i == i) continue;

		if(pos_i%2 != at%2){
			if(at%2 == 0){
				swap(p[0], p[1]);
				at++;
				ans.pb(0);
				if(p[0] == i){
					swap(p[1], p[2]);
					at++;
					ans.pb(1);
					pos_i = 0;
				}
			}else{
				swap(p[1], p[2]);
				at++;
				ans.pb(1);
				if(p[1] == i){
					swap(p[2], p[3]);
					at++;
					ans.pb(2);
					pos_i = 1;
				}
			}

		}
		//agora posso comecar a mover o i para direita
		for(int j = pos_i; j < i; j++){
			ans.pb(j);
			at++;
			swap(p[j], p[j+1]);
		}

	}

	//neste momento tenho um de tamanho 3
	if(n >= 3){

		if(p[2] != 2){
			//se 2 nao esta certo
			if(p[1] == 2 && at%2 == 1){
				swap(p[1], p[2]);
				at++;
				ans.pb(1);
			}else if(p[1] == 2 && at%2 == 0){
				swap(p[0], p[1]);
				at++;
				ans.pb(0);

				swap(p[1],p[2]);
				at++;
				ans.pb(1);

				swap(p[0],p[1]);
				at++;
				ans.pb(0);

				swap(p[1], p[2]);
				at++;
				ans.pb(1);
			}else{
				if(at%2 == 1){
					swap(p[1], p[2]);
					at++;
					ans.pb(1);
				}

				swap(p[0],p[1]);
				at++;
				ans.pb(0);

				swap(p[1], p[2]);
				at++;
				ans.pb(1);

			}
		}else{
			if(at%2 == 1 && p[0] == 1){
				swap(p[1], p[2]);
				at++;
				ans.pb(1);

				swap(p[0],p[1]);
				at++;
				ans.pb(0);

				swap(p[1], p[2]);
				at++;
				ans.pb(1);

				swap(p[0],p[1]);
				at++;
				ans.pb(0);

				swap(p[1], p[2]);
				at++;
				ans.pb(1);

			}
		}
	}

	if(p[0] == 1){
		swap(p[0],p[1]);
		at++;
		ans.pb(0);
	}

	cout << ans.size() << '\n';

	if(ans.size() > 0){

		for(int i = 0; i < ans.size(); i++){
			cout << ans[i] +1 << " ";
		}cout << '\n';
	}
}

int main(){

    ios::sync_with_stdio(false); cin.tie(NULL);

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

    return 0;
}

/*
PRESTA ATENCAO!!!!

NAO FIQUE PRESO EM UMA ABORDAGEM
A SUA INTUICAO PODE ESTAR ERRADA - TENTE OUTRAS COISAS
LIMPOU TUDO PARA O PROXIMO CASO?
caso especial? n == 1

*/


//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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 <iostream>
#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 <utility>
#include <vector>
using namespace std;
#define LL long long
#define VE vector
#define VI vector<int>
#define FOR(i,a,n) for(i=a;i<n;++i)
#define rep(i,n) FOR(i,0,n)
#define MOD 998244353
#define coutl cout<<fixed<<setprecision(15)
#define FAST cin.tie(0);ios::sync_with_stdio(false)

#define MI modint<MOD>
template<int mod>struct modint{
	int x;
	modint():x(0){}
	modint(LL n):x(n>=0?n%mod:(mod-(-n)%mod)%mod){}
	modint &operator+=(const modint &n){if((x+=n.x)>=mod)x-=mod;return *this;}
	modint &operator-=(const modint &n){if((x+=mod-n.x)>=mod)x-=mod;return *this;}
	modint &operator++(){*this+=1;return *this;}
  modint &operator--(){*this-=1;return *this;}
	modint &operator*=(const modint &n){x=(int)((LL)x*n.x%mod);return *this;}
	modint &operator/=(const modint &n){*this*=n.inv();return *this;}
	modint operator-()const{return modint(-x);}
	modint operator+(const modint &n)const{return modint(*this)+=n;}
	modint operator-(const modint &n)const{return modint(*this)-=n;}
	modint operator++(int){modint ret(*this);*this+=1;return ret;}
  modint operator--(int){modint ret(*this);*this-=1;return ret;}
	modint operator*(const modint &n)const{return modint(*this)*=n;}
	modint operator/(const modint &n)const{return modint(*this)/=n;}
	bool operator<(const modint &n)const{return x<n.x;}
	bool operator>(const modint &n)const{return x>n.x;}
	bool operator<=(const modint &n)const{return x<=n.x;}
	bool operator>=(const modint &n)const{return x>=n.x;}
	bool operator!=(const modint &n)const{return x!=n.x;}
	bool operator==(const modint &n)const{return x==n.x;}
	friend istream &operator>>(istream &is,modint &n){LL l;is>>l;n=modint<mod>(l);return is;}
	friend ostream &operator<<(ostream &os,const modint &n){return os<<n.x;}
	int getmod(){return mod;}
	modint inv()const{int a=x,b=mod,c=1,d=0,n;while(b){n=a/b;swap(a-=n*b,b);swap(c-=n*d,d);}return modint(c);}
	modint pow(LL n)const{modint ret(1),m(x);while(n){if(n&1)ret*=m;m*=m;n>>=1;}return ret;}
};

int main(){
  FAST;
  int i,j,N,K;
  cin>>N>>K;
  VI A(N);
  rep(i,N)cin>>A[i];
  MI k,ans;
  VE<MI> v(K+1),p(K+1),q(K+1),a(K+1);
  p[0]=q[0]=1;
  FOR(i,1,K+1)p[i]=p[i-1]*i,q[i]=q[i-1]/i;
  v[0]=N;
  rep(j,N){
    k=A[j];
    FOR(i,1,K+1){
      v[i]+=k*q[i];
			a[i]+=k;
      k*=A[j];
    }
  }
  FOR(i,1,K+1){
    ans=0;
    rep(j,i+1)ans+=v[j]*v[i-j];
    ans*=p[i];
    k=a[i];
    rep(j,i)k*=2;
    ans=(ans-k)/2;
    cout<<ans<<endl;
  }
}

/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
//*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int  ,int > pii;

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

const ll maxn  = 3e6;
const ll mod =998244353;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

ll fact[maxn];

ll inv[maxn];

ll n , k;

ll a[maxn];

ll sum[maxn];

int32_t main(){
    migmig;
	cin >> n >> ::k;
	fact[0] = inv[0] = 1;
	for(int i = 1 ; i <= ::k ; i ++)
		fact[i] = (fact[i - 1] * i)%mod , inv[i] = pw(fact[i] , mod - 2);
	for(int i = 1 ; i <= n ; i ++)
		cin >> a[i];
	for(int i = 0 ; i <= ::k ; i ++)
		for(int j = 1 ; j <= n ; j ++)
			sum[i] = (sum[i] + pw(a[j] , i))%mod;
	for(int x = 1 ; x <= ::k ; x ++){
		ll ans = 0;
		for(int k = 0 ; k <= x ; k ++){
			ll res = (sum[k] * sum[x - k])%mod;
			res = (res * inv[k])%mod;
			res = (res * inv[x - k])%mod;
			ans = (ans + res)%mod;
		}
		ans = (ans * fact[x])%mod;
		for(int i = 1 ; i <= n ; i ++)
			ans = (ans - pw(a[i]*2 , x) + mod)%mod;
		cout << (ans*pw(2 , mod - 2))%mod << endl;
	}
    return(0);
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define WT int T=read();while(T--)
#define NO puts("NO");
#define YES puts("YES");
using namespace std;

inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

const int Mod=998244353;
const int M=510;
int n,m,a[M][M],ans=1;
vector<int>v[M<<1];

signed main()
{
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		string s;cin>>s;
		for (int j=0;j<m;j++)
			if (s[j]=='B')a[i][j+1]=1;
			else if (s[j]=='R')a[i][j+1]=2;
			else a[i][j+1]=3;
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			v[i+j].pb(a[i][j]);
	for (int i=2;i<=n+m;i++)
	{
		int X=0,cnt=0,f=1;
		for (int j=0;j<v[i].size();j++)
		{
			if (v[i][j]<=2)
			{
				if (!X||X==v[i][j])X=v[i][j];
				else f=0;
			}
			else cnt++;
		}ans=ans*f*(1+(X==0))%Mod;
	}cout<<ans<<endl;
	return 0;
}

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;

#define NUM_CAN 400

struct state
{
  int x;
  int y;
  int score;
  string path;
  set<int> used;
};
bool operator< (const state &state1, const state &state2){
    return state1.score < state2.score;
};

int main()
{
  struct timeval tmp_time;
  gettimeofday(&tmp_time, NULL);
  long long program_init_time = tmp_time.tv_sec*1000000+tmp_time.tv_usec;
  long long fin_time, current_time;
  fin_time = program_init_time + 1500000;

  int si, sj, count;
  cin >> si >> sj;
  vector<vector<int>> tiling(50,vector<int>(50)), grids(50,vector<int>(50));
  for (int i = 0; i < 50; i++)
  {
    for (int j = 0; j < 50; j++) cin >> tiling.at(i).at(j);
  }
  for (int i = 0; i < 50; i++)
  {
    for (int j = 0; j < 50; j++) cin >> grids.at(i).at(j);
  }
  vector<int> dx{1,-1,0,0}, dy{0,0,1,-1};
  string dir = "DURL";
  queue<state> que;
  priority_queue<state> next_que, blank;
  state now_state;
  int best_score;
  string best_path;
  now_state.x = si, now_state.y = sj;
  now_state.score = grids.at(si).at(sj);
  now_state.path = "";
  now_state.used.insert(tiling.at(si).at(sj));
  best_score = now_state.score;
  best_path = now_state.path;
  que.push(now_state);
  count = 0;
  while (!que.empty())
  {
    while (!que.empty())
    {
      count++;
      now_state = que.front();
      que.pop();
      for (int i = 0; i < 4; i++)
      {
        if (now_state.x+dx.at(i) < 0 || now_state.x+dx.at(i) >= 50 || now_state.y+dy.at(i) < 0 || now_state.y+dy.at(i) >= 50)
          continue;
        if (now_state.used.count(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))))
          continue;
        now_state.score += grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i));
        now_state.path += dir.at(i);
        now_state.used.insert(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)));
        now_state.x += dx.at(i), now_state.y += dy.at(i);
        if (best_score < now_state.score)
        {
          best_score = now_state.score;
          best_path = now_state.path;
        }
        next_que.push(now_state);
        now_state.x -= dx.at(i), now_state.y -= dy.at(i);
        now_state.score -= grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i));
        now_state.path.pop_back();
        now_state.used.erase(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)));
      }
      if (count % 100 == 0)
      {
        gettimeofday(&tmp_time, NULL);
        current_time = tmp_time.tv_sec*1000000 + tmp_time.tv_usec;
        if (current_time > fin_time)
        break;
      }
    }
    if (count % 100 == 0)
    {
      if (current_time > fin_time)
        break;
    }
    for (int i = 0; i < NUM_CAN; i++)
    {
      if (next_que.empty())
        break;
      que.push(next_que.top());
      next_que.pop();
    }
    next_que = blank;
  }
  cout << best_path << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define pb push_back
#define eb emplace_back
#define sz size
#define ALL(v) v.begin(),v.end()
const long long INF = 1LL << 60;
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using P3 = tuple<int, int, int>;
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vs vector<string>
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 main()
{
    int h, w, n, m;
    cin >> h >> w >> n >> m;
    vvi g(h, vi(w, 0));
    vector<P> p;
    rep(i, n){
        int a, b;
        cin >> a >> b;
        --a; --b;
       // g[a][b] = 1;
        p.eb(a, b);
    }
    rep(i, m){
        int a, b;
        cin >> a >> b;
        --a; --b;
        g[a][b] = -1;
    }
    //tate
    rep(i, n){
        int x = p[i].first;
        int y = p[i].second;
        if(!(g[x][y] &1)){
            rep2(xi, x+1, h){
                if(g[xi][y] == -1) break;
                if((g[xi][y] &1)) break;
                g[xi][y] = 1;
            }
            for(int xi = x-1; xi>=0 ; xi--){
                if(g[xi][y] == -1) break;
                if((g[xi][y] &1)) break;
                g[xi][y] = 1;
            }
        }
        if(!(g[x][y] &2)){
            rep2(yi, y+1, w){
                if(g[x][yi] == -1) break;
                if((g[x][yi] &2)) break;
                g[x][yi] = 2;
            }
            for(int yi = y-1; yi>=0 ; yi--){
                if(g[x][yi] == -1) break;
                if((g[x][yi] &2)) break;
                g[x][yi] = 2;
            }
        }
        g[x][y] = 3;
    }
    int ans = 0;
    rep(i, h)rep(j, w){
        if(g[i][j] > 0) ++ans;
    }
    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
// ll p = 1e9+7;
// ll p = 998244353;
// n do -> n*pi/180
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}

int main(){
  cci(h,w);
  vvi v(h+2,vi(w+2,-1));
  rep(i,1,h+1){
      rep(j,1,w+1)vij=0;
  }
  cci(n,m);

  vvi lt(n,vi(2)),bl(m,vi(2));
  rep(i,0,n){
      rep(j,0,2)ci(lt[i][j]);
  }
  rep(i,0,m){
      rep(j,0,2)ci(bl[i][j]);
      v[bl[i][0]][bl[i][1]]=-1;
  }

    vvi tt=v,yk=v;
  rep(i,0,n){
      int x=lt[i][0];
      int y=lt[i][1];

      int nx=x,ny=y;
      tt[x][y]=yk[x][y]=1;
      while(tt[nx-1][y]==0)tt[--nx][y]=1;
      while(yk[x][ny-1]==0)yk[x][--ny]=1;

      nx=x,ny=y;
      while(tt[nx+1][y]==0)tt[++nx][y]=1;
      while(yk[x][ny+1]==0)yk[x][++ny]=1;
    }
/*
  rep(i,2,h+1){
      rep(j,1,w+1){
          if(yk[i][j]==-1)continue;
          chmax(yk[i][j],yk[i-1][j]);
      }
  }

  rep(i,2,w+1){
      rep(j,1,h+1){
          if(tt[j][i]==-1)continue;
          chmax(tt[j][i],tt[j][i-1]);
      }
  }
*/
    int ans=0;
    rep(i,1,h+1){
        rep(j,1,w+1){
            if(yk[i][j]==1 or tt[i][j]==1){
                // cout<<1<<" ";
                ans++;
            }
            // else if(vij==-1)cout<<'x'<<" ";
            // else cout<<0<<" ";
        }
        // co("");
    }

    co(ans);
}