// Jai Shree Ram

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
#define fbo find_by_order

#define rep(i,a,n)     for(int i=a;i<n;i++)
#define ll             long long
#define int            long long
#define pb             push_back
#define all(v)         v.begin(),v.end()
#define endl           "\n"
#define x              first
#define y              second
#define gcd(a,b)       __gcd(a,b)
#define mem1(a)        memset(a,-1,sizeof(a))
#define mem0(a)        memset(a,0,sizeof(a))
#define sz(a)          (int)a.size()
#define pii            pair<int,int>
#define hell           1000000007
#define elasped_time   1.0 * clock() / CLOCKS_PER_SEC

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;}
template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;}

const int N=1e9+5;
const int M=1e7;

struct node{
	int l;int r;
	int val;
	int cnt;
}t[M];
int cnt=0;

int n;

void extend(int v){
	if(t[v].l==0){
		t[v].l=++cnt;
		t[v].r=++cnt;
	}
}

void pushup(int v){
	t[v].val = t[t[v].l].val + t[t[v].r].val;
	t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt;
}

void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){
	if(tl>r || l>tr)return;
	if(l<=tl && tr<=r){
		assert(tl == tr);
		t[v].val += val.x;
		t[v].cnt += val.y;
		return;
	}
	extend(v);
	int mid=(tl+tr)/2;
	update(t[v].l,l,r,val,tl,mid);
	update(t[v].r,l,r,val,mid+1,tr);
	pushup(v);
}

pii query(int v,int l,int r,int tl=0,int tr=1e9){
	if(tl>r || l>tr)return {0,0};
	if(l<=tl && tr<=r){
		return {t[v].val,t[v].cnt};
	}
	int mid=(tl+tr)/2;
	extend(v);
	pii a =query(t[v].l,l,r,tl,mid);
	pii b =query(t[v].r,l,r,mid+1,tr);
	return {a.x+b.x,a.y+b.y};
}

int solve(){
		int n,m,q; cin >> n >> m >> q;

		int ans = 0;
		int roota = ++cnt;
		int rootb = ++cnt;

		vector<int>v1(n+1),v2(m+1);
		update(roota,0,0,{0,n});
		update(rootb,0,0,{0,m});

		rep(i,0,q){
			int t; cin >> t;
			int x,y; cin >> x >> y;
			if(t == 1){
				if(y > v1[x]){
					auto s = query(rootb,0,y);
					ans += s.y*y;
					s = query(rootb,v1[x]+1,y);
					ans -= s.x;
					s = query(rootb,0,v1[x]);
					ans -= v1[x]*s.y;
				}
				else if(y < v1[x]){
					auto s = query(rootb,0,v1[x]);
					ans -= s.y*v1[x];
					s = query(rootb,0,y);
					ans += s.y*y;
					s = query(rootb,y+1,v1[x]);
					ans += s.x;
				}
				update(roota,v1[x],v1[x],{-v1[x],-1});
				update(roota,y,y,{y,1});
				v1[x] = y;
			}
			else{
				if(y > v2[x]){
					auto s = query(roota,0,y-1);
					ans += s.y*y;
					s = query(roota,0,v2[x]-1);
					ans -= s.y*v2[x];
					s = query(roota,v2[x],y-1);
					ans -= s.x;
				}
				else if(y < v2[x]){
					auto s = query(roota,0,v2[x]-1);
					ans -= s.y*v2[x];
					s = query(roota,0,y-1);
					ans += s.y*y;
					s = query(roota,y,v2[x]-1);
					ans += s.x;
				}
				update(rootb,v2[x],v2[x],{-v2[x],-1});
				update(rootb,y,y,{y,1});
				v2[x] = y;
			}
			cout << ans << endl;
		}
 return 0;
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #ifdef SIEVE
    sieve();
    #endif
    #ifdef NCR
    init();
    #endif
    int t=1;//cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

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

int main()
{
	string s[3];
	cin >>s[0];
	cin>>s[1];
	cin>>s[2];

	int n[3];
	n[0]=s[0].size();n[1]=s[1].size();n[2]=s[2].size();
	set<char> st;
	for( int k = 0; k < 3; k++ ) {
		for( int i = 0; i < n[k]; i++ ) {
			st.insert( s[k][i] );
		}
	}
	int st_s = st.size();
	if( st_s > 10 ) {
		cout << "UNSOLVABLE" << endl;
		return 0;
	}
	set<char> st1;
	st1.insert( s[0][0] );
	st1.insert( s[1][0] );
	st1.insert( s[2][0] );
	vector<int> num( 10 );
	for( int i = 0; i < 10; i++ ) num[i] = i;
	long long ans[3];
	ans[0] = -1;
	do {
		map<char, int> mp;
		int flag = 0;
		for( int i = 0; i < st_s; i++ ) {
			char c = *next( st.begin(), i );
			mp[c] = num[i];
			if( num[i] == 0 && st1.count( c ) ) {
				flag = 1;
				break;
			}
		}
		if( flag == 0 ) {
			long long m[3];
			for( int k = 0; k < 3; k++ ) {
				m[k] = 0;
				for( int i = 0; i < n[k]; i++ ) {
					m[k] = m[k] * 10 + mp[ s[k][i] ];
				}
			}
			if( m[0] + m[1] == m[2] ) {
				ans[0] = m[0];
				ans[1] = m[1];
				ans[2] = m[2];
				break;
			}
		}
	} while( next_permutation( num.begin(), num.end() ) );

	if( ans[0] < 0 ) cout << "UNSOLVABLE" << endl;
	else {
		cout << ans[0] << endl;
		cout << ans[1] << endl;
		cout << ans[2] << endl;
	}
}


//BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define all(v) v.begin(),v.end()
#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> pii;
typedef pair<ll, ll> LL;
typedef vector<ii> vii;
typedef priority_queue<int,vector<int>,greater<int> > PQ;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int setBit(int mask, int pos){return mask = mask | (1<<pos);}
bool checkBit(int mask, int pos){return (bool)(mask & (1<<pos));}

ll power(ll a, ll b)
{
    ll ans = 1;
    FOR(i, 1, b)
        ans *= a;

    return ans;
}

ll getScore(vector<int> cnt)
{
    ll score = 0;
    FOR(i, 1, 9) {
        score += i * 1LL * power(10LL, (ll) cnt[i]);
    }

    return score;
}

void solve()
{
    ll k;

    cin>>k;

    string s, t;

    cin>>s>>t;

    vector<int> cnt1(10, 0), cnt2(10, 0);

    REP(i, 4) {
        cnt1[s[i]-'0']++;
        cnt2[t[i]-'0']++;
    }

    ll cnt = 0;

    FOR(i, 1, 9) {
        FOR(j, 1, 9) {
            if((k - (cnt1[i] + cnt2[i])) > 0 && (k - (cnt1[j] + cnt2[j])) > 0) {
                //cout<<i<<" "<<j<<endl;
                cnt1[i]++;
                cnt2[j]++;
                ll sa = getScore(cnt1);
                ll st = getScore(cnt2);
                cnt1[i]--;
                cnt2[j]--;
                if(sa > st) {
                    //cout<<i<<" "<<j<<endl;
                    ll x;
                    if(i != j)
                        x = 1LL * (k - (cnt1[i] + cnt2[i])) * (k - (cnt1[j] + cnt2[j]));
                    else {
                        ll ttx = (k - (cnt1[i] + cnt2[i]));
                        x = (ttx * (ttx - 1));
                    }
                    cnt += x;
                }
            }
        }
    }

    ll tx = 9 * k - 8;
    tx = (tx * (tx - 1));
    //cout<<"TEST: "<<cnt<<" "<<tx<<endl;
    double ans = (double) cnt / (double) tx;
    cout<<setiosflags(ios::fixed)<<setprecision(12);
    cout<<ans<<endl;

    return;
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int test = 1;

    //cin>>test;

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

    return 0;
}

#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using ll = long long;
struct Edge{
    ll to;
    ll cost;
};
using Graph = vector<vector<Edge>>;
using P =pair<ll,ll>;
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(),v.end()
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = LLONG_MAX;
const int MAX = 510000;
bool graph[nmax][nmax];
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll COM(int n, int k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;

    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n)
{
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            for (size_t k = 0; k < n; k++)
            {
                dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
            }
        }
    }
}

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

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

ll mulMod(ll a, ll b)
{
    return (((a % MOD) * (b % MOD)) % MOD);
}

ll powMod(ll a, ll p)
{
    if (p == 0)
    {
        return 1;
    }
    else if (p % 2 == 0)
    {
        ll half = powMod(a, p / 2);
        return mulMod(half, half);
    }
    else
    {
        return mulMod(powMod(a, p - 1), a);
    }
}

ll ceil(ll a, ll b)
{
    return (a + b - 1) / b;
}

void solve(long long N, long long A, long long B){
    cout << N - A + B << endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    solve(N, A, B);
    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define MOD (1000000000+7)
#define alp "abcdefghijklmnopqrstuvwxyz"
#define for0(i,n) for(long long i = 0;i < n;i++)
#define for01(i,n) for(long long i = 0;i <= n;i++)
#define for1(i,n) for(long long i = 1;i < n;i++)
#define for11(i,n) for(long long i = 1;i <= n;i++)
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<vector<int> >
#define vb vector<bool>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define test int T; cin>>T;
#define uset unordered_set
#define umultiset unordered_multiset
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
void no(){cout << "NO\n";}
void yes(){cout << "YES\n";}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) {return (a.second < b.second);}
bool sortpairdif(const pair<int,int> &a, const pair<int,int> &b) {if(a.first == b.first) return a.second > b.second; return a.first < b.first;}
ll llgcd(ll a, ll b){if (a == 0) return b; return llgcd(b % a, a);}
int igcd(int a, int b){if (a == 0) return b; return igcd(b % a, a);}

int main(){

	cin.tie(0); ios::sync_with_stdio(0);

	test;
	ull n;
	while(T--){
		cin >> n;
		if(n % 4 == 0){
			cout << "Even\n";
		}else if(n % 2 == 0){
			cout << "Same\n";
		}else{
			cout << "Odd\n";
		}

	}

    return 0;
}

//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (T &x : v)
        is >> x;
    return is;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll calc(string a, string b)
{
    ll a_int = stoi(a);
    ll b_int = stoi(b);
    return b_int - a_int;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, k;
    string s;
    cin >> s >> k;
    rep(i, k)
    {
        sort(ALL(s));
        string s_min = s;

        reverse(ALL(s));

        string s_max = s;
        ll r = calc(s_min, s_max);
        s = to_string(r);

    }
    cout << s << "\n";
}


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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
    vector<string> s(3);
    set<char> S;
    for (int i = 0; i < 3; i++) {
        cin >> s[i];
        for (auto c : s[i]) {
            S.insert(c);
        }
    }
    if (S.size() > 10) {
        cout << "UNSOLVABLE\n";
        return 0;
    }
    vector<int> p(10);
    iota(p.begin(), p.end(), 0);
    vector<char> v;
    for (auto c : S) v.push_back(c);
    map<char, int> M;
    for (int i = 0; i < int(v.size()); i++) M[v[i]] = i;
    do {
        vector<ll> a(3);
        bool ok = false;
        for (int i = 0; i < 3; i++) {
            if (p[M[s[i][0]]] == 0) {
                ok = true;
                break;
            }
            for (auto c : s[i]) {
                a[i] = a[i] * 10 + p[M[c]];
            }
            if (a[i] <= 0) {
                ok = true;
                break;
            }
        }
        if (ok) continue;
        if (a[0] + a[1] == a[2]) {
            for (int i = 0; i < 3; i++) {
                cout << a[i] << '\n';
            }
            return 0;
        }
    } while (next_permutation(p.begin(), p.end()));
    cout << "UNSOLVABLE\n";
    return 0;
}

/*
"An anomaly, I'm Muhammad Ali
  Cause I know one day I'm gonna be the"
                - Greatest, Eminem
*/
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int ll;
#define ff first
#define Shazam ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL);
#define ss second
#define all(c) c.begin(),c.end()
#define endl "\n"
#define test() int t; cin>>t; while(t--)
#define fl(i,a,b) for(int i = a ; i <b ;i++)
#define get(a) fl(i,0,a.size())  cin>>a[i];
#define pra(a) fl(i,0,a.size()) cout<<a[i]<<" ";	cout<<endl;
#define pr(a,n) fl(i,0,n) cout<<a[i]<<" ";	cout<<endl;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll INF = 2e18;
const int inf = 2e9;
const int mod1 = 1e9 + 7;

int main(){
    Shazam;
    vector<int> val(10);
    iota(all(val), 0);
    string a, b, c;
    cin >> a >> b >> c;
    set<char> bag;
    for(char c : a){
        bag.insert(c);
    }

    for(char c : b){
        bag.insert(c);
    }

    for(char c : c){
        bag.insert(c);
    }
    if(bag.size() > 10){
        cout << "UNSOLVABLE" << endl;
        return 0;
    }

    vector<char> letters;
    vector<int> whereat(26);
    for(char c : bag){
        letters.push_back(c);
    }
    for(int i = 0; i < letters.size(); i++){
        whereat[letters[i] - 'a'] = i;
    }
    auto getnum = [&](string s){
        ll res = 0;
        for(char c : s){
            int loc = whereat[c - 'a'];
            ll currval = val[loc];
            res *= 10LL;
            res += currval;
            if(!res) return 0LL;
        }
        return res;
    };

    do{
        ll num1 = getnum(a);
        ll num2 = getnum(b);
        ll num3 = getnum(c);
        if(num1 && num2 && num3 && num1 + num2 == num3){
            cout << num1 << endl;
            cout << num2 << endl;
            cout << num3 << endl;
            return 0;
        }
    }while(next_permutation(all(val)));
    cout << "UNSOLVABLE" << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
const int N = 2e5;
const int mod = 998244353;
using namespace std;
int n,a[N + 5],fa[N + 5];
int mypow(int a,int x){int s = 1;for (;x;x & 1 ? s = 1ll * s * a % mod : 0,a = 1ll * a * a % mod,x >>= 1);return s;}
int find(int x)
{
    if (x == fa[x])
        return x;
    return fa[x] = find(fa[x]);
}
int main()
{
    scanf("%d",&n);
    for (int i = 1;i <= n;i++)
        scanf("%d",&a[i]);
    for (int i = 1;i <= n;i++)
        fa[i] = i;
    for (int i = 1;i <= n;i++)
    {
        int X = find(i),Y = find(a[i]);
        if (X != Y)
            fa[X] = Y;
    }
    int sm = 0;
    for (int i = 1;i <= n;i++)
        if (fa[i] == i)
            sm++;
    cout<<((mypow(2,sm) - 1) % mod + mod) % mod<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
using P = pair<int, int>;
using T = tuple<int, int, int>;
const double PI = 3.14159265358979323846;
const in MOD = 998244353;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
//cout << fixed << setprecision(10);

int main(void)
{
    int n;
    cin>>n;
    vector<int>graph(n);
    set<int> seen;//観たら消去
    int kazu=0;//サイクルの個数

    rep(i,n){
        int f;
        cin>>f;
        f--;
        graph.at(i)=f;
    }
    rep(i,n){
        seen.insert(i);
    }

    while(!seen.empty()){
        int start = *begin(seen);
        set<int> mita;//見たやつを入れていく

        mita.insert(start);
        int now=start;
        seen.erase(start);

        while(1){
            now=graph.at(now);
            if(mita.count(now)){
                kazu++;
                break;
            }
            if(!seen.count(now))
                break;
            seen.erase(now);
            mita.insert(now);
        }
    }
    in ans = 1;
    rep(i,kazu){
        ans*=2;
        ans=ans%MOD;
    }
    ans--;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define FULL(x,y) memset(x,y,sizeof(x))
#define ll long long
#define SZ(x) (int)x.size()
#define pb push_back
using namespace std;

const int N=5005;
const ll mod=998244353;
ll n,m;
ll f[N][N],ff[N];

int main() {
	cin>>n>>m;
	for(ll i=0;i<=m;i++) f[i][0]=1;
	for(ll i=1;i<=m;i++) {
		for(ll j=1;j<=n;j++) {
			f[i][j]=(f[i][j-1]*i)%mod;
		}
	}
	ll ans=(n*f[m][n])%mod;
	for(ll i=0;i<=n;i++) {
		ll tmp=0;
		for(ll j=1;j<=m;j++) {
			tmp=(tmp+f[m-j][i])%mod;
		}
		ff[i]=tmp;
	}
	for(ll i=1;i<=n;i++) {
		for(ll j=1;j<i;j++) {
			ll tmp=ff[i-j-1];
			tmp=(tmp*f[m][n-i+j-1])%mod;
			ans=(ans-tmp+mod)%mod;
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

template<class D>
vector<D> dijkstra(const vector<vector<pair<int,D>>>& G, int s = 0){	//G:(v,cost)
	using P = pair<D,int>;
	priority_queue<P,vector<P>,greater<P> > que;
	int N = G.size();

	D inf = 1e9;
	vector<D> d(N,inf);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty()){
		P p=que.top();
		que.pop();
		int v = p.sc;
		D c = p.fs;
		if(d[v]!=c) continue;
		for(auto p:G[v]){
			int to=p.fs;
			if(d[to]>d[v]+p.sc){
				d[to]=d[v]+p.sc;
				que.push(P(d[to],to));
			}
		}
	}

	return d;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int H,W; cin >> H >> W;
	VV<int> A(H,V<int>(W-1)); rep(i,H) rep(j,W-1) cin >> A[i][j];
	VV<int> B(H-1,V<int>(W)); rep(i,H-1) rep(j,W) cin >> B[i][j];
	auto id = [&](int x,int y){return x*W+y;};
	auto is = [&](int x,int y){return 0<=x&&x<H&&0<=y&&y<W;};

	using P = pair<int,int>;
	int X = H*W * 2;
	VV<P> G(X);
	auto add_edge = [&](int x,int y,int c){
		G[x].pb(y,c);
	};

	rep(i,H) rep(j,W){
		shows(i,j);
		int v = id(i,j);
		{
			int ni = i, nj = j-1;
			if(is(ni,nj)){
				int nv = id(ni,nj);
				int c = A[i][j-1];
				add_edge(v,nv,c);
			}
		}
		{
			int ni = i, nj = j+1;
			if(is(ni,nj)){
				int nv = id(ni,nj);
				int c = A[i][j];
				add_edge(v,nv,c);
			}
		}
		{
			int ni = i+1, nj = j;
			if(is(ni,nj)){
				int nv = id(ni,nj);
				int c = B[i][j];
				add_edge(v,nv,c);
			}
		}
		add_edge(v,v+H*W,1);
		add_edge(v+H*W,v,0);
		if(i){
			add_edge(v+H*W,id(i-1,j)+H*W,1);
		}
	}
	cout << dijkstra(G,0)[H*W-1] << endl;
}


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

int main() {

  int N, Q;

  string S;

  cin >> N >> S >> Q;

  bool flag = true;

  for (int i = 0; i < Q; i++) {
    int T, A, B;
    cin >> T >> A >> B;

    if (T == 1) {
      if (!flag) {
        if (A > N) {
          A -= N;
        } else {
          A += N;
        }
        if (B > N) {
          B -= N;
        } else {
          B += N;
        }
      }
      swap(S.at(A - 1), S.at(B - 1));
    } else {
      if (flag) {
        flag = false;
      } else {
        flag = true;
      }
    }
  }

  if (!flag) {
    for (int i = 0; i < N; i++) {
      cout << S.at(i + N);
    }
    for (int i = 0; i < N; i++) {
      cout << S.at(i);
    }
    cout << endl;
  } else {
    cout << S << endl;
  }

  return 0;

}

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

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

#define int                     long long
#define all(ds)                 ds.begin(), ds.end()
#define uniq(v)                 (v).erase(unique(all(v)),(v).end())
#define gcd(x,y)                __gcd(x,y)
#define precise(x)              cout<<fixed<<setprecision(x)
#define endl                    "\n"
#define pii                     pair<int,int>
#define sz(x)                   ((int)(x).size())
#define lb                      lower_bound
#define ub                      upper_bound
#define pb                      push_back
#define ff                      first
#define ss                      second
#define ms(x,a)                 memset(x,(int)(a),sizeof(x))
#define rep(i,a,b)            for (int i=a; i<(b); i++)
#define forn(i,a)              for (int i=0; i<(a); i++)
#define repd(i,a,b)             for (int i = (b)-1; i >= a; i--)
#define ford(i,a)               for (int i = (a)-1; i >= 0; i--)
template<class T> bool gmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool gmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

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

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

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

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

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

void solve(){
    int n, q;
    string s;
    read(n,s,q);
    bool parity = false;
    forn(i,q){
        int type, x, y;
        read(type,x, y);
        if(type == 2){
            parity = !parity;
        }
        else{
            if(parity){
                if(x <= n) x += n;
                else x -= n;
                if(y <= n) y += n;
                else y -= n;
            }
            swap(s[x-1], s[y-1]);
        }
    }
    if(parity) {
        s = s.substr(n, 2*n) + s.substr(0, n);
    }
    prn(s);
}

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

#ifdef SIEVE
    sieve();
#endif

#ifdef NCR
    init();
#endif

    int t = 1;
    // cin>>t;
    for(int tt = 1; tt <= t; ++tt){
        // cout << "Case #" << tt << ": ";
        solve();
    }

    return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<string> ans{"AB"};
  FOR(i, 2, n + 1) {
    int per = 1 << i, m = ans.size();
    vector<string> nx{""};
    REP(_, per / 2) nx.back() += 'A';
    REP(_, per / 2) nx.back() += 'B';
    for (auto &s : ans) {
      nx.emplace_back(s + s);
      string t = s;
      for (char c : s) t += c == 'A' ? 'B' : 'A';
      nx.emplace_back(t);
    }
    ans.swap(nx);
  }
  int k = ans.size();
  assert(k == (1 << n) - 1);
  // vector a(1 << n, vector(1 << n, 0)), b(1 << n, vector(1 << n, 0));
  // for (auto &s : ans) {
  //   REP(i, 1 << n) REP(j, 1 << n) {
  //     if (j != i) (s[i] == s[j] ? a[i][j] : b[i][j]) += 1;
  //   }
  // }
  // int N = a[0][1], M = b[0][1];
  // REP(i, n) FOR(j, i + 1, n) assert(a[i][j] == N && b[i][j] == M);
  cout << k << '\n';
  for (auto &s : ans) cout << s << '\n';
  return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve(){
	string s; cin >> s;
	if (s > "atcoder"){
		cout << 0 << "\n"; return;
	}
	ll n = s.length();
	for (ll i = 0; i < n; i++){
		if (s[i] == 'a') continue;
		else{
			if (i == 1){
				cout << 1 << "\n"; return;
			}
			else{
				if (s[i] > 't'){
					cout << i - 1 << "\n";
				}
				else cout << i << "\n";
				return;
			}
		}
	}
	cout << -1 << "\n"; return;
}
int main(){
	fastio;
	ll t; cin >> t;
	while (t--) solve();
}


#include<bits/stdc++.h>
using namespace std::chrono;
using namespace std;
#define int long long
#define loop(i, start, end) for (int i = start; i <= end; i++)
#define rloop(i, start, end) for (int i = start; i >= end; i--)
#define read(a,n) loop(i,0,n-1) cin>>a[i];
#define vi vector<int>
#define vec(x) vector<x>
#define sz(v) (int)v.size()
#define UB upper_bound
#define LB lower_bound
#define all(v) (v).begin(),(v).end()
#define arl(v) (v).rbegin(),(v).rend()
#define fsp(a) fixed<<setprecision(a)
#define mem(a,with) memset(a, with, sizeof (a))
#define vmn(a) (*min_element(a.begin(),a.end()))
#define vmx(a) (*max_element(a.begin(),a.end()))
#define bs(a,key) binary_search(all(a),key)    /// return bool.
#define rotl(a,x) rotate(a.begin(),a.begin()+x,a.end());
#define rotr(a,x) rotl(a,a.size()-x);
#define nl cout<<endl
#define dbg(x) cerr << #x << " :: " << x << endl;
#define dbg2(x, y) cerr << #x << " :: " << x << "\t" << #y << " :: " << y << endl;
#define MOD 1000000007
int add(int x, int y) {int res = x + y; return (res >= MOD ? res - MOD : res);}
int mul(int x, int y) {int res = x * y; return (res >= MOD ? res % MOD : res);}
int power(int x, int y) {int res = 1; x %= MOD; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}

void solve(){
    //code goes here
    int N;
    cin >> N;
    string S;
    cin >> S;
    stack<char> st;
    int res = N;
    for (const auto &s : S) {
        if (s == 'f') {
            st.push(s);
        }
        else if (s == 'o') {
            st.push(s);
        }
        else if (s == 'x') {
            if (!st.empty() && st.top() == 'o') {
                st.pop();
                if (!st.empty() && st.top() == 'f') {
                    st.pop();
                    res -= 3;
                }
                else {
                    st.push('a');
                }
            }
            else {
                st.push('a');
            }
        }
        else {
            st.push('a');
        }
    }
  cout<<res;
}

int32_t main()
{
auto start = chrono::high_resolution_clock::now();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
    int t=1;
    // cin>>t;
    while (t--)
    {
        solve();
    }
#ifndef ONLINE_JUDGE
     auto stop = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<microseconds>(stop - start);
    cout<<"\n\nExecuted In: "<<duration.count()<<" ms";
#endif
    return 0;
}

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

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int N;
string s;

void input(void){
    cin >> N >> s;
}

int solve(void){
    static char buf[200005];
    int ptr = 0;
    for (int i = 0; i < N; ++i){
        if (
            ptr >= 2 &&
            buf[ptr - 2] == 'f' &&
            buf[ptr - 1] == 'o' &&
            s[i] == 'x'
        ){
            ptr -= 2;
        }
        else buf[ptr++] = s[i];
    }
    return ptr;
}

int main(void){
    input();
    cout << solve() << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e10;

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

    vvl v(n, vl(5));
    rep(i, n) {
        rep(j, 5) {
            cin >> v[i][j];
        }
    }

    // 判定問題「チームの総合力をx以上にできるか？」を二分探索で解く
    ll left = 0;
    ll right = INF;  /*・INFの値が大きすぎてオーバーフローしていないか？
                    ・INFの値が制約に対して小さすぎないか？         */
    while(right - left > 1) {
        ll x = (left + right) / 2;
        set<ll> st;
        rep(i, n) {
            ll y = 0;
            rep(j, 5) {
                if(v[i][j] >= x) {
                    y |= 1<<j;
                }
            }
            st.insert(y);
        }
        vl w;
        for(ll y : st) {
            w.pb(y);
        }
        bool ok = false;
        rep(i, (ll)w.size()) {
            rep(j, i+1) {
                rep(k, j+1) {
                    if((w[i] | w[j] | w[k]) == (1<<5) - 1) {
                        ok = true;
                    }
                }
            }
        }
        if(ok) {
            left = x;
        }
        else {
            right = x;
        }
    }

    out(left);
    re0;
}

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main(){
    int N;
    cin >> N;
    vector<vector<int>> A(5, vector<int>(N));
    for(int i = 0 ; i < N; i++) {
        for(int j = 0; j < 5; j++){
            int a;
            cin >> a;
            A[j][i] = a;
        }
    }
    int ub = 1000000001, lb = 1;
    while(ub - lb > 1){
        int mid = (lb + ub) / 2;
        vector<int> temp(N);
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < N; j++){
                if(A[i][j] >= mid) temp[j] |= (1 << i);
            }
        }
        vector<int> ten(1 << 5, 10000000);
        ten[0] = 0;
        for(int j = 0; j < (1 << 5); j++){
            for(int k = 0; k < N; k++){
                ten[j | temp[k]] = min(ten[j | temp[k]], ten[j] + 1);
            }
        }
        if(ten[(1 << 5) - 1] <= 3) lb = mid;
        else ub = mid;
    }
    cout << lb << endl;
}


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

int main() {
  int n;
  string s,d;
  cin>>n>>s;
  for (char c : s) {
    d += c;
    if (d.size() >= 3 && d.substr(d.size()-3) == "fox") {
      d.erase(d.begin()+d.size()-3, d.end());
    }
  }
  cout<<d.size()<<endl;
  return 0;
}

#include<bits/stdc++.h>
#define pb       push_back
#define f        first
#define s       second
#define pi       acos(-1.0)
#define mod      1000000007
#define test     int t; cin>>t ;while(t--)
typedef long long ll;
using namespace std;

int main()
{

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

       stack<char>st;
       int n;
       string s;
       cin>>n;
       char a;
       st.push('*');
       st.push('*');
       st.push('*');
       for(int i=1;i<=n;i++)
       {
          cin>>a;
          if(a=='x')
          {
             char p,q;
             p=st.top();
             st.pop();
             q=st.top();
             st.pop();
             if(p=='o'&&q=='f')
             {
                continue;
             }
             else
             {
                st.push(q);
                st.push(p);
                st.push(a);
             }
          }
          else
            st.push(a);

       }

       cout<<st.size()-3<<endl;

      return 0;

}


#include <bits/stdc++.h>

using namespace std;

struct compare {
    inline bool operator()(const std::string& first,
            const std::string& second) const
    {
        return first.size() < second.size();
    }
};

int main() {
  int n, m;
  cin>>n>>m;
  string ins[m];
  for (int i=0; i<m; i++) cin>>ins[i];

  vector<string> s;
  for (int i=0; i<m; i++) {
    bool contains = false;
    for (int j=0; j<m; j++) {
      if (i == j) {
        continue;
      }
      if (ins[j].find(ins[i]) != -1) {
        contains = true;
        break;
      }
    }
    if (!contains) {
      s.push_back(ins[i]);
    }
  }
  compare c;
  sort(s.begin(), s.end(), c);
  reverse(s.begin(), s.end());

  vector<string> result[20];
  vector<string> suffix[20];
  int len[20];
  for (int i=0; i<20; i++) len[i] = 0;
  int i = 0;
  for (string si: s) {
    for (int i=0; i<20; i++) {
      bool match_suffix = false;
      for (string i_suffix : suffix[i]) {
        if (len[i] + si.length() - i_suffix.length() > 20) {
          continue;
        }
        if (i_suffix == si.substr(0, i_suffix.length())) {
          match_suffix = true;
          result[i].push_back(si.substr(i_suffix.length()));
          len[i] += si.length() - i_suffix.length();
          break;
        }
      }
      if (!match_suffix) {
        if (len[i] + si.length() > 20) {
          continue;
        }
        result[i].push_back(si);
        len[i] += si.length();
      }
      suffix[i].clear();
      for (int j=0; j<si.length()-1; j++) {
        suffix[i].push_back(si.substr(j+1));
      }

      break;
    }
  }
  for (int i=0; i<20; i++) {
    for (string r : result[i]) {
      cout<<r;
    }
    for (int j=len[i]; j<20; j++) {
      cout<<"A";
    }
    cout<<endl;
  }
}

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

int main() {
  int h,w,count=0;
  cin>>h>>w;
  vector<string>s(h);
  for(int i=0;i<h;i++)cin>>s.at(i);
  for(int i=0;i<h;i++){
    for(int j=0;j<w-1;j++){
      if(s.at(i).at(j)=='.'&&s.at(i).at(j+1)=='.'){
        count++;
      }
    }
  }
  for(int i=0;i<h-1;i++){
    for(int j=0;j<w;j++){
      if(s.at(i).at(j)=='.'&&s.at(i+1).at(j)=='.'){
        count++;
      }
    }
  }
  cout<<count;
}


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

#define endl '\n'

#define MOD 1'000'000'007

#define ll long long int
#define ull unsigned ll

#define vt vector
#define uset unordered_set
#define umap unordered_map
#define mset multiset
#define umset unordered_multiset

#define in insert
#define pb push_back
#define ff first
#define ss second

#define F(i, end) LF(i, 0, end)
#define LF(i, beg, end) for (ll i = beg; i < end; ++i)
#define LR(i, end, beg) for (ll i = end; i >= beg; --i)

#define HAS(col, e) (col.find(e) != col.end())

#define outcol(col, sep) for (auto e: col) cout << e << sep

void solve()
{
    ull n;
	cin >> n;

	ll dig[3] = {0};
	ll digCount = 0;

	ll temp = n;
	while (temp)
	{
		++digCount;
		++dig[(temp % 10) % 3];
		temp /= 10;
	}

	// cout << dig[0] << ' ' << dig[1] << ' ' << dig[2] << endl;

	if (abs(dig[1] - dig[2]) == digCount)
		cout << -1 << endl;
	else
	{
		if (dig[1] % 3 == 2 && dig[2] % 3 == 0)
		{
			if (dig[2] > 0)
				cout << 1 << endl;
			else
				cout << 2 << endl;
		}
		else if (dig[1] % 3 == 0 && dig[2] % 3 == 2)
		{
			if (dig[1] > 0)
				cout << 1 << endl;
			else
				cout << 2 << endl;
		}
		else
			cout << abs((dig[1] % 3) - (dig[2] % 3)) << endl;
	}
}

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

    ll c = 1;

    // cin >> c;

    F(i, c)
    {
        // cout << "Case #" << i + 1 << ": ";
        solve();
    }

	return 0;
}

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> II;
typedef pair<LL, II> III;
typedef priority_queue<III, vector<III>, greater<>> Queue;
static const LL INF = 1LL << 60;

LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }

#ifdef __GNUC__
#define popcount __builtin_popcount
#elif _MSC_VER >= 1400
#include <intrin.h>
#define popcount __popcnt
#else
static inline int popcount(unsigned int b) {
	b = (b & 0x55555555) + (b >> 1 & 0x55555555);
	b = (b & 0x33333333) + (b >> 2 & 0x33333333);
	b = (b & 0x0F0F0F0F) + (b >> 4 & 0x0F0F0F0F);
	b += b >> 8;
	return (b + (b >> 16)) & 0x3F;
}
#endif

void solve(string N) {
	const int INF = 1 << 30;
	int ans = INF;
	int len = N.length();
	int bm = 1 << len;
	for (int b = 1; b < bm; ++b) {
		int sum = 0;
		for (int i = 0; i < len; ++i) {
			if (b & (1 << i)) {
				sum += N[i] - '0';
			}
		}
		if ((sum % 3) == 0) {
			ans = min(ans, (int)(len - popcount(b)));
		}
	}
	if (ans == INF) ans = -1;
	cout << ans << endl;
}

int main() {
	string N;
	std::cin >> N;
	solve(N);
	return 0;
}


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

typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

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

int lg(ll n) {
    assert(n > 0);
    int ans = -1;
    while (n) {
        ans++;
        n >>= 1;
    }
    return ans;
}

template <int mod_>
struct modint {
    static const int mod = mod_;
    int val;
    modint() { val = 0; }
    modint(const ll& v) {
        val = (-mod <= v && v < mod) ? v : v % mod;
        if (val < 0) val += mod;
    }
    friend ostream& operator<<(ostream& os, const modint& a) {
        return os << a.val;
    }
    friend bool operator==(const modint& a, const modint& b) {
        return a.val == b.val;
    }
    friend bool operator!=(const modint& a, const modint& b) {
        return !(a == b);
    }
    friend bool operator<(const modint& a, const modint& b) {
        return a.val < b.val;
    }

    modint operator-() const { return modint(-val); }
    modint& operator+=(const modint& m) {
        if ((val += m.val) >= mod) val -= mod;
        return *this;
    }
    modint& operator-=(const modint& m) {
        if ((val -= m.val) < 0) val += mod;
        return *this;
    }
    modint& operator*=(const modint& m) {
        val = (ll)val * m.val % mod;
        return *this;
    }

    modint qpow(ll p) {
        modint ans = 1, base = val;
        for (; p; p /= 2, base *= base)
            if (p & 1) ans *= base;
        return ans;
    }
    modint inv() {
        assert(val);
        return qpow((ll)mod - 2);
    }
    modint& operator/=(modint m) { return (*this) *= m.inv(); }

    friend modint operator+(modint a, const modint& b) { return a += b; }
    friend modint operator-(modint a, const modint& b) { return a -= b; }
    friend modint operator*(modint a, const modint& b) { return a *= b; }
    friend modint operator/(modint a, const modint& b) { return a /= b; }
    operator ll() const { return val; }
};
using mint = modint<998244353>;

const int MX_sieve = 2e5;
int par[MX_sieve + 5];
vector<int> pri;  // par is the lowest prime factor
int pricnt = (int)pri.size();
map<int, int> primeidx;
void sieve() {
    for (int i = 1; i <= MX_sieve; i++) {
        if (i == 1)
            continue;
        else {
            if (!par[i]) {
                pri.push_back(i);
                par[i] = i;
            }
            for (int x : pri) {
                if (x > MX_sieve / i) break;
                par[x * i] = x;
                if (x == par[i]) {
                    break;
                }
            }
        }
    }
    int pricnt = (int)pri.size();
    /*
    for (int i = 0; i < pricnt; i++) {
        primeidx[pri[i]] = i;
    }
    */
}

// need mint

const int MX_fact = 2e5 + 500;
mint fact[MX_fact + 5], invfact[MX_fact + 5];
void prepare_fact() {
    fact[0] = 1;
    invfact[0] = 1;
    for (int i = 0; i < MX_fact; i++) {
        fact[i + 1] = fact[i] * mint(i + 1);
        invfact[i + 1] = fact[i + 1].inv();
    }
}
mint C(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n] * invfact[k] * invfact[n - k];
}
mint H(int n, int k) { return C(n + k - 1, n - 1); }

void solve() {
    int N, M;
    cin >> N >> M;
    mint ans = 0;
    for (int i = 1; i <= M; i++) {
        mint comb = 1;
        int reg = i;
        while (reg > 1) {
            int p = par[reg], pw = 0;
            while (par[reg] == p) {
                pw++;
                reg /= p;
            }
            comb *= C(N - 1 + pw, pw);
        }
        ans += comb;
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    sieve();
    prepare_fact();
    int t = 1;
    // cin >> t;
    while (t--) solve();

    return 0;
}

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

int main(){
    // 初期値
    // nが階数、kが1階ごとの部屋数
    int n, k;
    int count=0;

    // 入力
    cin >> n >> k;

    // 処理
    for(int i=1; i<=n; i++){
        for(int j=1; j<=k; j++){
            count += (100*i) + (1*j);
        }
    }

    // 出力
    cout << count << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define pi 2*acos(0.0)
#define readl(v,n) for(ll i=0;i<n;i++) {ll val; cin>>val; v.pb(val);}
#define readi(v,n) for(int i=0;i<n;i++) {int val; cin>>val; v.pb(val);}
#define srt(v) sort(v.begin(), v.end());
#define rsrt(v) sort(v.rbegin(), v.rend());
#define MIN(v) *min_element(v.begin(), v.end())
#define MAX(v) *max_element(v.begin(), v.end())
#define sz(x) ((ll) (x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(ll i = (a); i < (b); i++)
#define rep2(i, a, b) for(ll i = (a); i <= (b); i++)
#define vll vector <ll>
#define vii vector <int>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define M 1000007
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl "\n"
#define inf 1000000000000000000
#define ninf -1000000000000000000
#define deb(n) cout<<"--"<<(n)<<"--"<<endl
#define debx(x) cout << #x << " = " << (x) << endl
#define deb2(a,b) cout<<(a)<<"----"<<(b)<<endl
#define debv(v) for(auto x: v) {cout<<x<<", ";} cout<<endl;
#define put(n) cout<<(n)<<endl
#define toDecimal(s) bitset<64>(s).to_ullong()
#define toBinaryStr(n) bitset<64>(n).to_string()
#define toBinary(n) stoll(bitset<64>(n).to_string())

/*
	---- take notes here ----

*/

void solve()
{
	ll a,b,c; cin>>a>>b>>c;
	if(a*a+b*b<c*c)
	{
		cout<<"Yes"<<endl;
	}
	else
	{
		cout<<"No"<<endl;
	}
}

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

	return 0;
}

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
template<typename T>
void __p(T a) {
	cout << a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
	cout << "{";
	__p(a.first);
	cout << ",";
	__p(a.second);
	cout << "}";
}
template<typename T>
void __p(std::vector<T> a) {
	cout << "{";
	for (auto it = a.begin(); it < a.end(); it++)
		__p(*it), cout << ",}"[it + 1 == a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
	__p(a1);
	__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout << name << " : ";
	__p(arg1);
	cout << endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket = 0, i = 0;
	for (;; i++)
		if (names[i] == ',' && bracket == 0)
			break;
		else if (names[i] == '(')
			bracket++;
		else if (names[i] == ')')
			bracket--;
	const char *comma = names + i;
	cout.write(names, comma - names) << " : ";
	__p(arg1);
	cout << " | ";
	__f(comma + 1, args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__);
#else
#define trace(...)
#endif
typedef long long int ll;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<ll , ll> pp;
typedef pair<pair<ll, ll>, ll> ppp;
#define X first
#define Y second
#define getones(n) __builtin_popcountll(n)
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define frv(i,a,b) for (ll i = (a), _b = (b); i < _b; i++)
#define frrv(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)
#define fr(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define frr(i,n) for (ll i = n - 1; i >= 0; i--)
#define fordata(it,ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fill(ar,val) fr(i,sizeof(ar)/sizeof(ll)) ar[i]=val
#define fill2(ar,val) fr(i,sizeof(ar)/sizeof(ar[0])) fr(j,sizeof(ar[0])/sizeof(ll)) ar[i][j]=val
#define fill0(ar) memset(ar,0,sizeof(ar))
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(ar) ar.begin(), ar.end()
#define INF 1000000000000000000
#define PI 3.14159265358979323846L
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
const ll N = 1e6 + 1;
const ll K = 1e9 + 7;
#define vv vector<ll>
const ll globalmod = 1e9 + 9;
int main() {
	ll a, b, c;
	cin >> a >> b >> c;
	cout << (a * a + b * b < c * c ? "Yes" : "No");
}


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

int main(){

	int n;
	cin >> n;
	int k = n/100;
	if(n%100 != 0) k++;
	cout << k;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  cin >> n;
  n %= 100;
  cout << 100 - n;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll           long long
#define rep(i,j,n)   for(ll i=j;i<n;i++)
#define scn(a)       scanf("%lld",&a)
#define nl           printf("\n")
#define scns(a,b)    scanf("%lld %lld",&a,&b)
#define print(a)     printf("%lld",a)
#define tc           ll t; scn(t); while(t--)
#define vec          vector<ll>
#define pb           push_back
#define pairs        pair<ll,ll>
#define fi           first
#define se           second
#define all(v)       v.begin(),v.end()
#define srt(v)       sort(v.rbegin(),v.rend())
#define mem(a,b)     memset(a,b,sizeof(a))
#define maxx         5000005
#define inf          LLONG_MAX
#define zer          LLONG_MIN
#define dbug(a)      cout<<"dbug : "<<a<<endl
#define debug(a,b)   cout<<a<<" "<<b<<endl
#define read(v)      vec v(n,0); rep(i,0,n) {cin>>v[i];}
#define write(a,n)   rep(i,0,n) {cout<<a[i]<<" ";}
#define All          rep(i,0,n)
const ll N = 4000;
const ll mod = 1000000000+7;
ll zero = 0;

//1 - uska vote
//2 - mera vote

// bool comp(pair<ll,ll> p1, pair<ll,ll> p2) {
// 	if(p1.fi > p2.fi) return true;
// 	if(p1.fi == p2.fi) {
// 		if(v[p1.se].fi >= v[p2.se].fi) return true;
// 		else return false;
// 	}

// 	return false;
// }

void solve()
{

	ll n;
	cin >> n;

	vector<pair<ll,ll>> v(n);
	vector<pair<ll,ll>> total(n);

	ll totaluska = 0;

	rep(i,0,n) {
		cin >> v[i].fi;
		cin >> v[i].se;
		totaluska += v[i].fi;
	}

	rep(i,0,n) {
		ll tot = v[i].fi + v[i].se + v[i].fi;
		total[i] = {tot,i};
	}

	//sort(total.begin(), total.end()
	srt(total);

	ll mera = 0;
	ll uska = totaluska;
	ll i = 0;

	//cout << totaluska << endl;
	while(mera <= uska) {
		mera += total[i].fi - v[total[i].se].fi;
		uska -= v[total[i].se].fi;
		i++;

	}

	//cout << "mera " << mera << " uska " << uska << endl;

	cout << i;
	return;

}

//Check for test case
int main(){

 int test=1;

 //cin>>test;
 while(test--)
 {
 	solve();
 }
 return 0;
}

#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int n;
    cin>>n;
    pair<int,int> a[n];
    for(int i=0;i<n;i++){
        cin>>a[i].first>>a[i].second;
    }
    int x=0,y=0; // x-> aoki's votes and y-> takahashi's votes
    for(int i=0;i<n;i++){
        x+=a[i].first;
    }
    for(int i=0;i<n;i++){
        int temp = a[i].first;
        a[i].first = 2*a[i].first+a[i].second;
        a[i].second = temp;//{2a+b,a}
    }
    sort(a,a+n);
    int ans=0;
    int i=n-1;
    while(x>=y){
        ans++;
        y+=a[i].first-a[i].second;
        x-=a[i].second;
        i--;
    }
    cout<<ans;
    /*
        a[i].first=a,a[i].second=b;
        2*a+b
    */
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n;
int s[200005];
vector<int>v[200005];
int ans=0;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		cin>>s[i];
		v[s[i]].push_back(i);
	}
	for(int i=1;i<=n/2;++i)
	{
		if(s[i]!=s[n+1-i])
		{
			int x=s[i],y=s[n+1-i];
			++ans;
			if(v[x].size()<=v[y].size())
			{
				for(int j=0;j<v[x].size();++j)
				{
					int z=v[x][j];
					s[z]=s[n+1-i];
					v[y].push_back(z);
				}
			}
			else
			{
				for(int j=0;j<v[y].size();++j)
				{
					int z=v[y][j];
					s[z]=s[i];
					v[x].push_back(z);
				}
			}

		}
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>

#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define vl vector<long long>
#define pii pair<int, int>
#define REP(i,a) for (int i = 0; i < (a); i++)
#define add push_back
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

const ll MOD = 1000000007LL;
//const ll MOD = 998244353LL;

int ni() {
    int x; cin >> x;
    return x;
}

ll nl() {
    ll x; cin >> x;
    return x;
}

double nd() {
    double x; cin >> x;
    return x;
}

string next() {
    string x; cin >> x;
    return x;
}

struct DisjointSetUnion {
    vi parent;
    vi weight;
    int count; //number of components

    DisjointSetUnion(int N) {
        count = N;
        parent.assign(N,0);
        REP(i,N) parent[i] = i;
        weight.assign(N,1);
    }

    //"find"
    int root(int p) {
        if (p==parent[p])
            return p;
        int r = root(parent[p]);
        parent[p] = r;
        return r;
    }

    //"union"
    void connect(int p, int q) {
        p = root(p);
        q = root(q);
        if (p==q) return;

        if (weight[p]<weight[q]) {
            parent[p] = q;
            weight[q] += weight[p];
        } else {
            parent[q] = p;
            weight[p] += weight[q];
        }
        count--;
    }

    bool connected(int p, int q) {
        return root(p)==root(q);
    }
};

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N = ni();
    vi nums(N);
    REP(i,N) {
        nums[i] = ni()-1;
    }

    DisjointSetUnion dsu(200000);
    for (int i = 0; i < N; i++)
        dsu.connect(nums[i],nums[N-1-i]);

    cout << 200000-dsu.count;
}


#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <stack>

using namespace std;

#define enviar {ios_base::sync_with_stdio(false);}
#define aiuda {cin.tie(NULL); cout.tie(NULL);}

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) ;F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
#define exacto(v) cout << setprecision(v) << fixed;

#define ll long long
#define lld long double
#define eb emplace_back
#define em emplace
#define ef emplace_front
#define f first
#define s second
#define big greater<ll>()
#define bpc(x) __builtin_popcount(x)
#define bpc2(x) __builtin_clz(x)
#define bpc3(x) __builtin_ctz(x)
#define DIE(args...) {print(args); return;}
#define lb lower_bound
#define ub upper_bound

#define ar(v)  array<int,v>
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x).size()
#define meminf(v) memset(v,  0x3f , sizeof(v))
#define mem(v,x) memset(v , x , sizeof v);
#define ai(a, n) for (int ele = 0; ele < n; ele++) cin >> a[ele];
#define ao(a, n) {for (int ele = 0; ele < (n); ele++) { if (ele) cout << " "; cout << a[ele]; } cout << '\n';}
#define readgraph(list, edges) for (int i = 0; i < edges; i++) {int n1, n2; cin >> n1 >> n2; n1--; n2--; list[n1].eb(n2); list[n2].eb(n1);}

template <typename T> struct combinator  {   T t_; combinator(T&& t) : t_(std::forward<T>(t)) {} template <typename... Args> auto operator () (Args&&... args) const  { return t_(*this, std::forward<Args>(args)...); } };
template<class T> bool umin(T& a, const T& b) { return b<a?a=b, 1:0; }
template<class T> bool umax(T& a, const T& b) { return a<b?a=b, 1:0; }
string to_string(bool b) {return b?"true":"false"; }
string to_string(char c) { return string(1, c);}
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
template<class T> string to_string(T v) { bool f=1; string res; EACH(x, v) { if(!f) res+=' '; f=0; res+=to_string(x); } return res; }
template<class A> void write(A x) { cout << to_string(x); }
void print() { write("\n"); }
template<class H, class... T> void print(const H& h, const T&... t) {  write(h); if(sizeof...(t)) write(' '); print(t...); }

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef map<ll, ll> mll;
typedef priority_queue<int, std::vector<int>, std::greater<int>> priomin;

const ll mod =   998244353;
//const ll mod =   1e9 + 7;
const ll maxsize =3e5  + 5;
const ll inf = 0x3f3f3f3f3f3f3f3f ;
// size, caracter

ll a[maxsize];
//vll adj[maxsize];
//ll l[maxsize];
ll l[maxsize][2];

ll dp[maxsize][2];

void solve()
{
	ll n;
	cin >> n;
	ai(a, n);

	FOR(i , n)
	{
		if (i%2) a[i]*=-1;
	}

	partial_sum(a , a+ n , a);

	mll cnt;
	ll ans = 0;
	cnt[0]++;
	FOR(i , n)
	{
		ans += cnt[a[i]]++;
	}

	print(ans);

}

int main(){

	enviar aiuda

	int tc = 1;
	//cin >> tc;

	while(tc--)  solve();

	return 0;
}

// Problem: C - ARC Wrecker 2
// Contest: AtCoder - AtCoder Regular Contest 119
// URL: https://atcoder.jp/contests/arc119/tasks/arc119_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms

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

typedef long long ll;
const int N = 3e5 + 10;

int p[N];
ll sum[N];
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> p[i];
		sum[i] = sum[i - 1] + p[i] * (i % 2 == 0 ? 1 : -1);
	}
	map<ll, int> cnt;
	cnt[0] = 1;
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += cnt[sum[i]];
		++cnt[sum[i]];
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ld long double
#define ll long long
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define PB push_back
#define MP make_pair
#define all(cont) cont.begin(),cont.end()
#define rall(cont) cont.rbegin(), cont.rend()
#define FOR(i, j) for(int i=0;i<j;i++)
#define RFOR(i, j) for (int i=j;i>=0;i--)
#define GO cin.tie(NULL);
#define FAST ios_base::sync_with_stdio(false);
typedef pair<int,int> pii;

// Your function
//DEBBUGGING STUFF, JUST USE deb(a,b,c) and it will print the variables;
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cout << vars << " = ";
    string delim = "";
    (..., (cout << delim << values, delim = ", "));
    cout<<endl;

}

const int maxn=1e5+10;
int main(){
    GO FAST;
    int n;cin>>n;
    bitset<maxn> possiveis(0);
    possiveis.set(0);
    int soma=0;
    FOR(i,n){
        int a;
        cin>>a;
        soma+=a;
        auto cop=possiveis;
        cop<<=a;
        possiveis|=cop;
    }
    int ans=soma;
    FOR(i,maxn){
        if (possiveis[i]){
            ans=min(max(i,soma-i),ans);
        }
    }
    cout<<ans<<'\n';

}

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
#include<algorithm>
constexpr long long MOD = 998244353;
constexpr long long MOD32 = 665496236;
    char f[5000][5000];
    long long dp[5000];

int main(){
    long H=0, W=0, K=0;
    char c=getchar_unlocked();
    while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();}
    c=getchar_unlocked();
    while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();}
    c=getchar_unlocked();
    while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();}

    long long y = H * W - K;
    while(--K>=0){
        long h=0, w=0;
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();}
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();}
        f[h-1][w-1] = getchar_unlocked();
        getchar_unlocked();
    }
    --H; --W;
    dp[0] = 1;
    for(int i = 0; i < H; ++i){
        for (int j = 0; j < W; ++j){
            switch(f[i][j]) {
                case 'R':
                    dp[j + 1] += dp[j];
                    dp[j] = 0;
                    break;
                case '\0':
                    dp[j] = dp[j] * MOD32 % MOD;
                case 'X':
                    dp[j + 1] += dp[j];
            }
        }
            switch(f[i][W]) {
                case 'R':
                    dp[W] = 0;
                    break;
                case '\0':
                    dp[W] = dp[W] * MOD32 % MOD;
            }
    }
    for (int j = 0; j < W; ++j){
        switch(f[H][j]) {
            case '\0':
                dp[j] = dp[j] * MOD32 % MOD;
            case 'R':
            case 'X':
                dp[j + 1] += dp[j];
        }
    }

    long long ans = dp[W] % MOD;
    long long x = 3;
    while(y) {
      if(y & 1) ans = ans * x % MOD;
      x = x * x % MOD;
      y >>= 1;
    }
    printf("%d", ans);

    return 0;
}


#include <iostream> // cout, endl, cin
#include <cmath> //sqrt pow
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // dequef
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>

#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using ld = long double;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define mod 1000000007
using namespace std;
ll gcd(ll a, ll b){
  if(a % b == 0) return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b){
  return a / gcd(a, b) * b;
}
int main(){
  int n;
  cin >> n;
  ll ans = 1;
  for(int i = 2; i <= n; i++) ans = lcm(ans, i);
  cout << ans + 1 << endl;
}

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  //a
  long long lcm = 1;
  int n;
  cin >> n;
  for (long long i = 2; i <= n; i++) {
    long long g = __gcd(lcm, i);
    lcm = lcm * i / g;
  }
  lcm++;
  assert(lcm <= 1e13);
  for (int i = 2; i <= n; i++) {
    cerr << "i = " << i << " rem = " << lcm % i << endl;
    assert((lcm % i) == 1);
  }
  cout << lcm << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include<iomanip>
#include<functional>
#include<algorithm>
#include<deque>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define rep(i,n) for(int i = 0;i<n;i++)
#define req(i,n) for(int i = 1;i<=n;i++)
#define rrep(i,n) for(int i = n-1;i>=0;i--)
#define ALL(a) a.begin(),a.end()
typedef long long ll;
typedef double d;
typedef pair<int, int> P;
const int inf = 1 << 30;
struct liner {
	d a, b;
	liner(d a = 0, d b = 0): a(a), b(b) {}
	liner operator+(const liner& x)const {
		return liner(a + x.a, b + x.b);
	}liner operator-(const liner& x)const {
		return liner(a - x.a, b - x.b);
	}liner operator/(int x)const {
		return liner(a / x, b / x);
	}
};
int main() {
	int n, m, k,p,sum=0; cin >> n >> m >> k;
	vector<bool> f(n + 1, 0);
	rep(i, k) {
		cin >> p; f[p] = 1;
	}rep(i, n + 1) {
		if (f[i])sum++;
		else sum = 0;
		if (sum >= m) {
			cout << -1 << endl;
			return 0;
		}
	}vector<liner> dp(n + 1); liner S;
	rrep(i, n) {
		if (f[i]) dp[i] = liner(1, 0);
		else dp[i] = S / m + liner(0, 1);
		S = S+dp[i];
		if (i + m <= n) S = S - dp[i + m];
	}cout <<fixed<<setprecision(10) << dp[0].b / (1 - dp[0].a) << endl;
}

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define u64 long long
#define rint register int
#define co(x) cout << (x) << endl
#define cosp(x) cout << (x) << ' '
#define ce(x) cerr << (x) << endl
#define cesp(x) cerr << (x) << ' '

using namespace std;

//#define getchar() (p_1==p_2&&(p_2=(p_1=buf)+fread(buf,1,1<<21,stdin),p_1==p_2)?EOF:*p_1++)

namespace Fast_Sec
{
	namespace Calculation
	{
		inline u64 max_(u64 a, u64 b) { return a > b ? a : b; }

		inline u64 min_(u64 a, u64 b) { return a < b ? a : b; }

		inline void swap_(int& a, int& b) {	a ^= b; b ^= a; a ^= b; }

		inline void swap_(u64& a, u64& b) {	a ^= b; b ^= a; a ^= b; }

		inline u64 abs_(u64 a) { return a < 0 ? -a : a; }
	};

	namespace IO
	{
		char buf[1<<23],*p_1=buf,*p_2=buf,obuf[1<<23],*O_=obuf;

		struct Fread{ } fin, fout; int f_; char ch_, Endl = '\n';

		inline Fread& operator >>(Fread& F, int &a)
		{
			f_ = 1; a = 0; ch_ = getchar();
			while (ch_ < '0' || ch_ > '9')
			{
				if (ch_ == '-') f_ = -1;
				ch_ = getchar();
			}
			while (ch_ >= '0' && ch_ <= '9')
			{
				a = (a << 3) + (a << 1) + (ch_ ^ 48);
				ch_ = getchar();
			}
			a *= f_; return F;
		}

		inline Fread& operator >>(Fread& F, u64 &a)
		{
			f_ = 1; a = 0; ch_ = getchar();
			while (ch_ < '0' || ch_ > '9')
			{
				if (ch_ == '-') f_ = -1;
				ch_ = getchar();
			}
			while (ch_ >= '0' && ch_ <= '9')
			{
				a = (a << 3) + (a << 1) + (ch_ ^ 48);
				ch_ = getchar();
			}
			a *= f_; return F;
		}

		Fread& operator <<(Fread& F, const int& a)
		{
			if (a < 0) { *O_++ = '-'; return F << -a; }
			if (a > 9) F << (a / 10); *O_++ = a % 10 + '0'; return F;
		}

		Fread& operator <<(Fread& F, const u64& a)
		{
			if (a < 0) { *O_++ = '-'; return F << -a; }
			if (a > 9) F << (a / 10); *O_++ = a % 10 + '0'; return F;
		}

		Fread& operator <<(Fread& F, const char& c) { *O_++ = c; return F; }
	};

	namespace Random
	{
		unsigned int _sd_ = 233;

		inline unsigned int rd()
		{
			return _sd_ ^= _sd_ >> 13, _sd_ ^= _sd_ << 17, _sd_ ^= _sd_ >> 5;
		}
	};
};
using namespace Fast_Sec::IO;
using namespace Fast_Sec::Random;
using namespace Fast_Sec::Calculation;

int n, a, x; string s;

int main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
	fin >> n >> x; cin >> s;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == 'o') x++;
		else if (x > 0) x--;
		}
		fout << x;
	fwrite(obuf, O_ - obuf, 1, stdout);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
//const int mod = ;

int main() {
  std::ios::sync_with_stdio(false); std::cin.tie(0);
  int n;
  cin >> n;
  vl a(n), b(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) cin >> b[i];
  vi ts(n);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    ll add = i % 2 ? a[i] : b[i];
    sum += add;
    ts[i] = a[i] + b[i] - 2 * add;
  }
  sort(ts.rbegin(), ts.rend());
  for (int i = 0; i < n / 2; ++i) sum += ts[i];
  cout << sum << '\n';
  return 0;
}

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

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;

ll a[200000],b[200000];

int main(){
	int n;cin>>n;
	rep(i,n)scanf("%lld",&a[i]);
	rep(i,n)scanf("%lld",&b[i]);
	vector<ll>u,v;
	rep(i,n){
		if(i%2==0)v.push_back(b[i]-a[i]);
		else u.push_back(b[i]-a[i]);
	}
	ll sum=0;
	rep(i,n)sum+=a[i];
	sort(v.begin(),v.end(),greater<>());
	sort(u.begin(),u.end(),greater<>());
	ll Max=sum;
	for(int i=0;i<v.size();i++){
		sum+=v[i]+u[i];
		Max=max(Max,sum);
	}
	cout<<Max<<endl;
}
/*
[([(())])(([]))]
*/


#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int,int>;
// #include"debug.hpp"
const int INF = 1e9;

vector<vector<int>> lcs(vector<int> s, vector<int> t) {
    int n = s.size(), m = t.size();

    vector<vector<int>> dp(n + 1, vector<int>(m + 1, INF)); //dp[i][j] := s[i - 1], t[j - 1]まで処理し終えたときの最小コスト
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j < m; j++) {

			if(i == 0) {
				dp[i][j] = j;
			}
			if(j == 0) {
				dp[i][j] = i;
			}
		}
	}

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {

			dp[i + 1][j + 1] = min({dp[i][j] + 1,dp[i + 1][j] + 1, dp[i][j + 1] + 1, dp[i + 1][j + 1]});

			if(s[i] == t[j]) {
				dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
			}

        }
    }
    return dp;
}

int main()
{
	int N, M; cin >> N >> M;
	vector<int> A(N), B(M);
	for(int i = 0; i < N; i++) cin >> A[i];
	for(int i = 0; i < M; i++) cin >> B[i];
	vector<vector<int>> dp = lcs(A, B);

	cout << dp[N][M] << 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 all(x) x.begin(),x.end()
typedef long long ll;
ll mod = 1000000007;
ll inf = 1e18;
int main(){

    int n,m;
    cin >> n >> m;
    vector<int> h(n),w(m);
    vector<int> dist(n-1);
    vector<int> teacher(n);
    ll sum=0,ans=mod;

    rep(i,n) cin >> h[i];
    rep(i,m) cin >> w[i];

    sort(all(h));
    sort(all(w));

    if(n==1){
        int a=mod;
        rep(i,m){
            a=min(a,abs(w[i]-h[0]));
        }
        cout << a << endl;
        return 0;
    }

    for(int i=0; i<n-1; i+=2){
        if(i==0) dist[i]=h[i+1]-h[i];
        else dist[i]=h[i+1]-h[i]+dist[i-2];
    }
    for(int i=n-2; i>0; i-=2){
        if(i==n-2) dist[i]=h[i+1]-h[i];
        else dist[i]=h[i+1]-h[i]+dist[i+2];
    }

    for(int i=0; i<n; i+=2){
        if(i==0) teacher[i]=dist[i+1];
        else if(i==n-1) teacher[i]=dist[i-2];
        else teacher[i]=dist[i-2]+dist[i+1];
    }

    rep(i,m){
        int key=w[i];
        auto iter=lower_bound(all(h),key);
        int x=iter-h.begin();
        if(x%2) x--;
        sum=abs(key-h[x])+teacher[x];
        // cout << sum << endl;
        ans=min(ans,sum);
        sum=0;
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
template<class T> auto vec(int r, int c, T v){ return vector<vector<T>>(r, vector<T>(c, v)); }
template<class T> auto vec(int o1, int o2, int o3, T v){ return vector<vector<vector<T>>>(o1, vector<vector<T>>(o2, vector<T>(o3, v))); }
int power(int x, int n, int m = -1){
    if(m != -1) x %= m;
    int ans = 1;
    while(n > 0){
        if( n & 1 ) {
            ans *= x;
            if(m != -1) ans = ans % m;
        }
        x = x * x;
        if(m != -1) x = x % m;
        n >>= 1;
    }
    return ans;
}
#define dbg(x) cerr << "[" << #x << " = " << x << "] ";
#define dbga(x, l, r) { cerr << "[" << #x << " = "; for(int i = l; i <= r; i++) { cerr << x[i]; if(i != r) cerr << ", ";} cerr << "]"; }
#define ub(x, y) (upper_bound(x.begin(), x.end(), y) - x.begin())
#define lb(x, y) (lower_bound(x.begin(), x.end(), y) - x.begin())

int TIME = 0;

void DFS(auto &adj, auto &dp, auto &d, auto &in, auto &out, int u = 0, int p = -1, int h = 0) {
    d[u] = h;
    in[u] = TIME++;
    dp[h].push_back(in[u]);
    for(int v : adj[u]) {
        if(v == p) continue;
        DFS(adj, dp, d, in, out, v, u, h + 1);
    }
    out[u] = TIME++;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n; cin >> n;
    vector<int> p(n, -1);
    vector<vector<int>> adj(n);
    vector<vector<int>> dp(n);
    vector<int> d(n, 0), in(n), out(n);
    for(int i = 1; i < n; i++) {
        cin >> p[i];
        p[i]--;
        adj[p[i]].push_back(i);
        adj[i].push_back(p[i]);
    }
    DFS(adj, dp, d, in, out);
    int q; cin >> q;
    for(int qq = 0, u, d; qq < q; qq++) {
        cin >> u >> d;
        u--;
        cout << lb(dp[d], out[u]) - lb(dp[d], in[u]) << "\n";
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REPP(i,a,b,d) for(int i=(a);i<=(int)(b);i+=(d))
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=(a);i>=(int)(b);i-=(d))
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) for(int i=0; i<(int)(a); i++)
#define FORD(i,a) for(int i=(int)(a)-1; i>=0; i--)
#define pb push_back
typedef long long ll;

struct Matrix {
	ll a, b, c, d;
};

struct Point {
	ll x, y;
};

const int N = 2e5+5;
int n, m, q;
Point a[N];
pair<int, int> op[N];
vector<pair<int, int>> queries[N];
Point ans[N];

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	REP(i, 1, n) cin >> a[i].x >> a[i].y;
	cin >> m;
	REP(i, 1, m) {
		cin >> op[i].first;
		if(op[i].first > 2) cin >> op[i].second;
	}
	cin >> q;
	FOR(i, q) {
		int x, y; cin >> x >> y;
		queries[x].pb({y, i});
	}

	Matrix mul = {1, 0, 0, 1};
	Point add = {0, 0};

	REP(i, 0, m) {
		if(i > 0) {
			if(op[i].first == 1) {
				mul = {mul.c, mul.d, -mul.a, -mul.b};
				add = {add.y, -add.x};
			} else if(op[i].first == 2) {
				mul = {-mul.c, -mul.d, mul.a, mul.b};
				add = {-add.y, add.x};
			} else if(op[i].first == 3) {
				mul = {-mul.a, -mul.b, mul.c, mul.d};
				add = {-add.x, add.y};
				add.x += 2 * op[i].second;
			} else {
				mul = {mul.a, mul.b, -mul.c, -mul.d};
				add = {add.x, -add.y};
				add.y += 2 * op[i].second;
			}
		}
		for(auto &q : queries[i]) {
			int &idx = q.first, &id = q.second;
			Point nw;
			nw.x = mul.a * a[idx].x + mul.b * a[idx].y;
			nw.y = mul.c * a[idx].x + mul.d * a[idx].y;
			nw.x += add.x, nw.y += add.y;
			ans[id] = nw;
		}
	}

	FOR(i, q) cout << ans[i].x << ' ' << ans[i].y << '\n';

	return 0;
}


#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define _roverload(_a1, _a2, _a3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _roverload(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i)
#define rrep(...) _roverload(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
void print() { cout << '\n'; }
template <class T>
void print(const vector<T> &v) {
    bool f = false;
    for (const auto &e : v) {
        if (f) cout << ' ';
        cout << e;
        f = true;
    }
    cout << '\n';
}
template <class... T>
void print(const T &...args) {
    bool f = false;
    for (const auto &e : {args...}) {
        if (f) cout << ' ';
        cout << e;
        f = true;
    }
    cout << '\n';
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
    preprocess() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main() {
    vi a(3);
    rep(i, 3) cin >> a[i];
    sort(all(a));
    if (a[0] == a[1])
        print(a[2]);
    else if (a[1] == a[2])
        print(a[0]);
    else
        print(0);
}

#include <cstdio>
using namespace std;

int main()
{
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	if(a == b) printf("%d\n", c);
	else if(b == c) printf("%d\n", a);
	else if(a == c) printf("%d\n", b);
	else puts("0");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define lli long long int
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define plli pair<lli,lli>
#define vi vector<int>
#define vlli vector<lli>
#define vpi vector<pii>
#define vplli vector<plli>
#define mi map<int,int>
#define mlli map<lli,lli>
#define forl(i,a,b) for(lli i=a;i<b;i++)
#define ford(i,a,b) for(lli i=a;i>=b;i--)
#define cout0(a) cout << a << endl
#define cout1(a) cout << a << " "
#define cout2(a,b) cout << a << " " << b << endl
#define cout3(a,b,c) cout << a << " " << b << " " << c << endl
#define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl

lli gcd(lli a,lli b)
{
    if(b>a)
    {
      lli t=a;
      a=b;
      b=t;
    }

    while(b!=0)
    {
      lli t = a;
      a = b;
      b = t%b;
    }

    return a;
}

bool sortbysec(const pair<lli,lli> &a,const pair<lli,lli> &b)
{
    return (a.first < b.first);
}

void dfs(vlli v[],lli s,lli depth[],lli isvisited[])
{
  forl(i,0,v[s].size())
  {
    lli t=v[s][i];
    if(!isvisited[t])
    {
      depth[t]=depth[s]+1;
      isvisited[t]++;
      dfs(v,t,depth,isvisited);
    }
  }
}

lli nCr(lli n,lli r)
{
  lli p = 1,k = 1;
  if (n-r < r) r = n - r;

  if (r != 0)
  {
    while (r)
    {
      p*=n;
      k*=r;

      lli m = __gcd(p, k);

      p/=m;
      k/=m;

      n--;
      r--;
    }
  }
  else p = 1;

  return p;
}

lli SieveOfEratosthenes(lli n)
{
  bool prime[n+1];
  memset(prime,true,n+1);
  forl(i,2,n+1)
    if(prime[i])
      for(lli j=2*i;j<n+1;j+=i)
        prime[i]=false;

  lli count=0;
  forl(i,2,n+1)
    if(prime[i]) count++;
  return count;
}

int main()
{
  fastio;
  // time_t start, end;
  // time(&start);

  lli q;
  // cin >> q;
  q = 1;
  while(q--)
  {
    lli n,k;
    cin >> n >> k;

    lli kthcake = 0;

    lli r = 0;
    lli sum = 0;
    for(r=3; r<=3*n;r++)
    {
      sum = (r-1)*(r-2)/2;
      if(r-n-1 >= 2) sum -= 3*(((r-n-1)*(r-n-2))/2);
      if(r-2*n-1 >= 2) sum += 3*(((r-2*n-1)*(r-2*n-2))/2);

      kthcake += sum;

      if(kthcake >= k) break;
    }

    kthcake -= sum;

    lli diff = k - kthcake;

    int r1 = 0;
    lli requiredcake = 0;
    for(r1=1; r1<=n; r1++)
    {
      sum = r-r1-1;
      if(r-r1-n-1 >= 1) sum -= 2*(r-r1-n-1);
      if(r-r1-2*n-1 >= 1) sum += (r-r1-2*n-1);

      requiredcake += sum;

      if(requiredcake >= diff) break;
    }

    requiredcake -= sum;

    lli diff2 = diff - requiredcake;

    int r2 = 0;
    lli requiredcake2 = 0;
    for(r2=1; r2<=n; r2++)
    {
      sum = 1;
      if(r-r1-r2-n-1 >= 0) sum -= 1;

      requiredcake2 += sum;

      if(requiredcake2 == diff2) break;
    }

    lli beauty_ans = r1;
    lli taste_ans = r2;
    lli popularity_ans = r - (beauty_ans + taste_ans);

    cout3(beauty_ans,taste_ans,popularity_ans);
  }

  // time(&end);
  // cout<<fixed<<double(end - start)<<setprecision(10)<<endl;
}


#include <bits/stdc++.h>

#define int long long
// #define endl "\n"
#define MOD 1000000007

using namespace std;

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
    os << "[ ";
    for (auto v : V)
        os << v << " ";
    os << "]";
    return os;
}
template <class T> ostream &operator<<(ostream &os, array<T, 2> A) {
    os << "[ ";
    for (auto a : A) {
        os << a << " ";
    }
    os << "]";
    return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
    os << "{ ";
    for (auto s : S)
        os << s << " ";
    return os << "}";
}
template <class T> ostream &operator<<(ostream &os, multiset<T> S) {
    os << "{ ";
    for (auto s : S)
        os << s << " ";
    return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
    os << "{ ";
    for (auto m : M)
        os << "(" << m.first << ":" << m.second << ") ";
    return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
    return os << "(" << P.first << "," << P.second << ")";
}

void solve() {
    int n;
    cin >> n;
    int a[n];
    pair<int, int> dp[n][2];
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }

    auto f = [&] (long double x) {
        long double res = 0;
        for(int i = 0; i < n; i++) {
            res += x + a[i] - min((long double)a[i], 2 * x);
        }
        res /= n;
        return res;
    };

    long double l = 0, r = 1e9, mid1 = 0, mid2 = 0;
    do {
        mid1 = (2 * l + r) / 3;
        mid2 = (l + 2 * r) / 3;
        if(f(mid1) > f(mid2)) {
            l = mid1;
        }
        else r = mid2;
    } while(abs(r - l) > 0.0000000001);

    cout << fixed;
    cout << setprecision(8) << f((l + r) / 2) << endl;
}

int32_t main() {
    int t = 1;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // cin >> t;
    while(t--) {
        solve();
    }
#ifndef ONLINE_JUDGE
    cout << "\nTime Elapsed : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << " ms\n";
#endif
    return 0;
}

#include <bits/stdc++.h>
const int N = 105, P = 998244353;
int n, s, w[N], dp[N][N * N], fac[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &w[i]), s += w[i];
  for (int i = fac[0] = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % P;
  if (s & 1) return puts("0"), 0;
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = i; j; --j)
      for (int v = s / 2; v; --v) {
        if (v >= w[i]) dp[j][v] = (dp[j][v] + dp[j - 1][v - w[i]]) % P;
      }
  int ans = 0;
  for (int i = 1; i < n; ++i) ans = (ans + 1ll * dp[i][s / 2] * fac[i] % P * fac[n - i]) % P;
  return printf("%d\n", ans), 0;
}


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

#define lln long long int
#define MOD  1000000007

vector<vector<int> > adj(20001);
int freq;

void dfs(int i, vector<int> & vis)
{
    freq=freq+1;
    vis[i]=1;
    for(auto child: adj[i])
    {
        if(vis[child]==false)
        {
            dfs(child,vis);
        }
    }
}
int main()
{

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

    int n;
    cin>>n;

    int i, t[n],sum=0,j;

    for(i=0;i<n;i++)
    {
        cin>>t[i];
        sum+=t[i];
    }
// the question is reduced to fact that we have to divide the array in 2 parts such
// that difference of their sum is minimum

// each element can either go to first part or second part.
    int dp[sum+1];

    for(i=0;i<=sum;i++)
    dp[i]=0;

    dp[0]=1;

    for(i=0;i<n;i++)
    {
        for(j=sum;j>=t[i];j--)
        dp[j] = dp[j] | dp[j-t[i]] ;
    }
    int min=INT_MAX, ans=-1;
    for(i=0;i<=sum;i++)
    {
        if(dp[i]>0)
        {
            if(abs(2*i-sum)< min)
            {
                min=abs(2*i-sum);
                ans = max(i, sum-i);
            }
        }
    }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }

ll dp[110][100010];

void chmin(ll &a, ll b) {
    a = min(a, b);
}

void solve() {
    rep(i, 0, 110) rep(j, 0, 100010) dp[i][j] = 1e9;
    ll n;
    cin >> n;
    vector<ll> t(n);
    rep(i, 0, n) cin >> t[i];
    dp[0][0] = 0;
    rep(i, 0, n) {
        rep(j, 0, 100001) {
            chmin(dp[i+1][j], dp[i][j]+t[i]);
            if(j + t[i] <= 100001) chmin(dp[i+1][j+t[i]], dp[i][j]);
        }
    }
    ll ans = 1e9;
    rep(i, 0, 100001) chmin(ans, max(dp[n][i], i));
    cout << ans << endl;
}

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


#include <iostream>
using namespace std;
typedef long long ll;
const int N=1007;
const ll mod=1e9+7;
int n;
ll fib[N];
char c1,c2,c3,c4;
ll ksm(ll x,ll p){
    ll res=1;
    while(p){
        if(p&1) res=res*x%mod;
        p/=2;
        x=x*x%mod;
    }
    return res;
}
void init(){
    fib[1]=1,fib[2]=1;
    for(int i=3;i<=n;i++){
        fib[i]=(fib[i-1]+fib[i-2])%mod;
    }
}
int main(){
    cin>>n;
    cin>>c1>>c2>>c3>>c4;
    if(n==2){
        cout<<1<<"\n";
        return 0;
    }
    init();
    if((c2=='A'&&c1=='A')||(c2=='B'&&c4=='B')){
        cout<<"1\n";
    }else if(c2=='A'&&c1=='B'&&c3=='B'){
        cout<<ksm(2,n-3)<<"\n";
    }else if(c2=='B'&&c4=='A'&&c3=='A'){
        cout<<ksm(2,n-3)<<"\n";
    }else{
        cout<<fib[n-1]<<"\n";
    }
}


#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
using PP = std::pair<ll,P>;

#define REP(i,b,e) for(int i=b; i<e; i++)
#define PRINT(vec) {printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");}
#define fi first
#define se second

const int MOD = 1e9+7;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main(){
	int n, w;
	scanf("%d %d", &n, &w);
	printf("%d\n", n/w);
	return 0;
}

#include <iostream>
using namespace std;

int main()
{
    int N, W;

    cin >> N >> W;

    cout << N / W;

    return 0;
}


#include <cstdio>
#include <algorithm>

const int N = 1000 + 10;
const int MOD = 1e9 + 7;

inline int qmod(int x) { return (x>=MOD)?(x-MOD):x; }

int main() {
	int i, p2, f1, f2, tmp, n;
	char aa, ab, ba, bb;

	scanf("%d", &n);
	if(n == 2 or n == 3) {
		printf("1\n");
		return 0;
	}

	f1 = f2 = 1;
	for(i=2; i<=n-2; i++) {
		tmp = qmod(f1 + f2);
		f2 = f1;
		f1 = tmp;
	}
	for(p2=1, i=1; i<=n-3; i++)
		p2 = qmod(p2 * 2);

	scanf(" %c %c %c %c", &aa, &ab, &ba, &bb);
	if(ab == 'B') {
		if(bb == 'B')
			printf("1\n");
		else {
			if(ba == 'A')
				printf("%d\n", p2);
			else
				printf("%d\n", f1);
		}
	}
	else {
		if(aa == 'A')
			printf("1\n");
		else {
			if(ba == 'B')
				printf("%d\n", p2);
			else
				printf("%d\n", f1);
		}
	}
	return 0;
}

#include<bits/stdc++.h>
#define con(typ) const typ
typedef long long ll;
template<typename T>void sf(T &x){x=0;T f=0;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')f=1;for(;c>='0'&&c<='9';c=getchar())x=(x<<3)+(x<<1)+(c^'0');if(f)x=-x;}
template<typename T>void pf(T x,char l='\n'){static int s[100],t;if(x<0)putchar('-'),x=-x;do s[++t]=x%10,x/=10;while(x);while(t)putchar(s[t--]^'0');putchar(l);}
int main(){
	std::vector<std::pair<ll,ll>> a;
	int n;
	ll k;
	sf(n),sf(k);
	for(int i=1;i<=n;++i){
		ll x,y;
		sf(x),sf(y);
		a.emplace_back(std::make_pair(x,y));
	}
	#define all(x) (x).begin(),(x).end()
	std::sort(all(a));
	for(std::pair<ll,ll> now:a){
		if(now.first>k)break;
		k+=now.second;
	}
	pf(k);
	return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
constexpr ll mod = 1e9 + 7;

ll modpow(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b & 1) r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(nullptr);
  ll n, w, h, ans = 0, cnt = 0;
  cin >> h >> w;
  string s[h];
  ll tate[h][w] = {}, yoko[h][w] = {};
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) if (s[i][j] == '.') cnt++;

  if (cnt == h * w) {
    ll t = h + w - 1;
    ans = (modpow(2, t) - 1) * modpow(2, h * w - t);
    ans %= mod;
    ans *= h * w;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }
  ans = 0;

  rep(i, h) rep(j, w) {
    if (s[i][j] == '#') continue;
    ll now = 1;
    for (int k = i - 1; k >= 0; --k)
      if (s[k][j] == '.')
        now++;
      else
        break;

    for (int k = i + 1; k < h; ++k)
      if (s[k][j] == '.')
        now++;
      else
        break;

    for (int k = j - 1; k >= 0; --k)
      if (s[i][k] == '.')
        now++;
      else
        break;

    for (int k = j + 1; k < w; ++k)
      if (s[i][k] == '.')
        now++;
      else
        break;

    ll add = (modpow(2, now) - 1) * modpow(2, cnt - now) % mod;
    (ans += add) %= mod;
  }

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

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2010;
const ll mod=1e9+7;
char g[N][N];
int n,m;
ll base[N*N];
int w[N][N];
int main()
{
    IO;
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>g[i]+1;

        int cnt=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) cnt+=(int)g[i][j]=='.';

        base[0]=1;
        for(int i=1;i<=cnt;i++) base[i]=base[i-1]*2%mod;
        ll res=0;
        for(int i=1;i<=n;i++)
        {
            int j=1;
            while(j<=m)
            {
                while(g[i][j]=='#') j++;

                int now=1,k=1;
                while(j+k<=m&&g[i][j+k]=='.') now++,k++;
                for(int p=j;p<j+k;p++) w[i][p]+=now;
                j=j+k;
            }
        }
        for(int i=1;i<=m;i++)
        {
            int j=1;
            while(j<=n)
            {
                while(g[j][i]=='#') j++;
                int now=1,k=1;
                while(j+k<=n&&g[j+k][i]=='.') now++,k++;
                for(int p=j;p<j+k;p++) w[p][i]+=now;
                j=j+k;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(g[i][j]=='#') continue;
                int now=w[i][j]-1;
                res=(res+(base[now]-1)*base[cnt-now]%mod)%mod;

            }
        }

        cout<<(ll)(res+mod)%mod<<'\n';

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double pi=3.1415926535897932384626;
inline ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
const int maxn=4e5+5;
int n,q,t,a,b,cnt;
char s[maxn];
int main(){
	n=read();
	scanf("%s",s+1);
	q=read();
	while(q--){
		t=read(),a=read(),b=read();
		if(t&1){
			if(cnt&1){
				if(a<=n) a+=n;
				else a-=n;
				if(b<=n) b+=n;
				else b-=n;
			}
			swap(s[a],s[b]);
		}
		else ++cnt;
	}
	if(cnt&1){
		for(int i=1;i<=n;++i) swap(s[i],s[i+n]);
	}
	printf("%s\n",s+1);
	return 0;
}

#include<iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

    char S1[N], S2[N];
    for(int i=0;i<N;i++){
        cin >> S1[i];
    }
    for(int i=0;i<N;i++){
        cin >> S2[i];
    }

    cin >> Q;
    int flip = 0;

    for(int i=0;i<Q;i++){
        int T, A, B;
        char tmp;
        cin >> T >> A >> B;
        if(T==2){
            flip = 1 - flip;
        }else{
            if(flip==0){
                if(B<=N){
                    tmp = S1[A-1];
                    S1[A-1] = S1[B-1];
                    S1[B-1] = tmp;
                }else if(N+1<=A){
                    tmp = S2[A-N-1];
                    S2[A-N-1] = S2[B-N-1];
                    S2[B-N-1] = tmp;
                }else{
                    tmp = S1[A-1];
                    S1[A-1] = S2[B-N-1];
                    S2[B-N-1] = tmp;
                }
            }else{
                if(B<=N){
                    tmp = S2[A-1];
                    S2[A-1] = S2[B-1];
                    S2[B-1] = tmp;
                }else if(N+1<=A){
                    tmp = S1[A-N-1];
                    S1[A-N-1] = S1[B-N-1];
                    S1[B-N-1] = tmp;
                }else{
                    char tmp = S2[A-1];
                    S2[A-1] = S1[B-N-1];
                    S1[B-N-1] = tmp;
                }
            }
        }
    }

    if(flip==0){
        for(int i=0;i<N;i++){
            cout << S1[i];
        }
        for(int i=0;i<N;i++){
            cout << S2[i];
        }
        cout <<endl;
    }else{
        for(int i=0;i<N;i++){
            cout << S2[i];
        }
        for(int i=0;i<N;i++){
            cout << S1[i];
        }
        cout <<endl;
    }

  	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<climits>

#define PI 3.14159265358979323846264338327950L
#define ll long long
#define db double

using namespace std;

const int MOD = 1e9+7;

void go(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout<<fixed;
  cout<<setprecision(20);

  #ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
  #endif
}
void solve(){
    int a,b; cin>>a>>b;
    for(int i = -200; i <= 200; i++){
        if((2*i)==(a+b)){
            cout<<i<<" "<<a-i<<endl;
            return;
        }
    }
}

int main(){
    go();

    // code here
    int t = 1;
    while(t--) solve();

}

#include <iostream>
#include <map>
#include<vector>
#include<set>
#include<math.h>
#include <bits/stdc++.h>
// #define testPrint
using namespace std;

bool solve()
{
    int m,h;
    cin >> m>>h;
    return ((h%m) == 0);

}

signed main(int arg, char **args)
{

        cout<<((solve())?"Yes":"No");

    return 0;
}