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

using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;

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

constexpr long long mod = 1000000007;

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

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

    int n;
    cin >> n;

    vector<string> v(n);
    rep(i, n) cin >> v[i];

    string ss = "atcoder";

    rep(i, n) {
        if (ss >= v[i]) {
            bool b = true;
            int ind = 0;
            rep(j, v[i].size()) {
                if (v[i][j] != 'a') {
                    if (v[i][j] > 't') {
                        ind = j - 1;
                    } else {
                        ind = j;
                    }
                    b = false;
                    break;
                }
            }

            if (b) {
                cout << -1 << endl;
            } else {
                cout << ind << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
// overflow prob
#define int long long
#define p_queue priority_queue
#define all(n)  n.begin(),n.end()
#define gcd(x,y) __gcd(x,y)
#define findString(s,x) s.find(x)!=string::npos
#define maxe(x) *max_element(all(x))
#define mine(x) *min_element(all(x))
#define rall(n) n.rbegin(),n.rend()
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cout << fixed << setprecision(10);
#define next cout<<"\n"
template<typename T> void debug(vector<T> &array, bool IsSorted) {
	if (IsSorted) sort(all(array));
	for (int i = 0 ; i < array.size() ; i++) {
		cout << array[i] << " ";
	}
	next;
}
template<typename T> T max(T &a, T &b) {
	return a > b ? a : b;
}
template<typename T> T min(T &a, T &b) {
	return a > b ? b : a;
}
template<typename T> T maxthree(T &a, T &b, T &c) {
	return max(a, max(b, c));
}
template<typename T> T minThree(T &a, T &b, T &c) {
	return min(a, min(b, c));
}
const double pi = 3.141592653589793238; const int  INF = 1e9; const int32_t M = 1e9 + 7; const int32_t MM = 998244353;
const int N = 1e5 + 5; const int32_t maxn = N;
void solve();
signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	FAST_IO
	int T = 1;
	// cin >> T;
	while (T--) solve();
}
//.........................................................MainCode..............................................................................
// solving for base

void solve() {
	string x;
	cin >> x;
	int m;
	cin >> m;
	if (x.size() == 1) {
		cout << (x[0] - '0' <= m ? '1' : '0');
		return;
	}
	int d = 0;
	for (int i = 0 ; i < x.size(); i++) {
		d = max(d, (int)x[i] - '0');
	}
	// max element
	int ng = 2e18;
	int ok = d;
	while (ng - ok > 1) {
		int mid = ok + (ng - ok) / 2;
		int res = 0;
		bool jdg = true;
		for (int i = 0 ; i < x.size() ; i++) {
			if (res >= 2e18 / mid) {
				ng = mid;
				jdg = false;
				break;
			}
			res *= mid;
			res += (x[i] - '0');
		}
		if (jdg) {
			if (res > m) ng = mid;
			else ok = mid;
		}
	}
	cout << ok - d << endl;
}


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

int main() {
  ll N,M,X,Y; cin >> N >> M;
  vector<pair<ll,ll>> P;
  set<ll> A,B,S; S.insert(N);
  for (int i =0; i<M; i++) { cin >> X >> Y; P.push_back({X,Y});}
  sort(P.begin(),P.end());
  ll tmp = 0,cnt;
  while (tmp < M) {
    A.clear(); B.clear();
    cnt = tmp;
    while (cnt < M) {
      if (P[cnt].first == P[tmp].first) cnt++;
      else break; }
    for (int i =tmp; i<cnt; i++) {
      ll tmpY = P[i].second;
      if (S.find(tmpY)==S.end()) {
        if (S.find(tmpY-1)!=S.end() || S.find(tmpY+1)!=S.end()) A.insert(tmpY);
      } else {
        if (S.find(tmpY-1)==S.end() && S.find(tmpY+1)==S.end()) B.insert(tmpY);
      }
    }
    for (auto x: A) S.insert(x);
    for (auto x: B) S.erase(x);
    tmp = cnt;
  }
  cout << S.size() << endl;
  return 0;
}

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

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

void solve()
{
  int n,m,q;
  cin>>n>>m>>q;
  vecp v(n);
  fr1(i,0,n)cin>>v[i].sc>>v[i].fr;
  sort(all(v),greater<pair<int,int>>());
  vec box(m);
  fr1(i,0,m)cin>>box[i];
  fr1(i,0,q)
  {
    int l,r;
    cin>>l>>r;
    vec cap;
    fr1(i,0,m)
    {
      if(i>=l-1&&i<=r-1)continue;
        cap.pb(box[i]);
    }
    ll ans=0;
    vector<bool> var(cap.size(),true);
    fr1(i,0,n)
    {
      int val=INT_MAX,index1=-1,index2=-1;
      for(int j=0;j<cap.size();j++)
      {
        if(var[j]&&cap[j]>=v[i].sc&&cap[j]-v[i].sc<val)
        {
          val=cap[j]-v[i].sc;
          index1=j;
          index2=v[i].fr;
        }
      }
      if(index1>=0)
      {
        var[index1]=false;
        ans+=index2;
      }
    }
    cout<<ans<<"\n";
  }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    //cin>>t;
    fr3(i,1,t){
      //cout<<"Case: "<<t<<;
        solve();
    }
   return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;
using P = pair<int, int>;

void solve_WA() {
    int N;
    cin >> N;
    vector<int> A(N + 1);
    //vector<int> ac(N + 1, 0);
    map<int, int>ac;
    /*
1 5 3 2 5 2 3 1の場合はOK ※連結成分が1のみ
1 5 3 2 5 4 6 1の場合はNG ※連結成分が2以上

    */
    for (size_t i = 1; i < N+1; i++)
    {
        cin >> A[i];
    }
    int l, r;
    int m = N / 2;
    for ( l = 1,r=N; l <= m; l++, r--)
    {
        if (A[l] != A[r]) {
            ac[A[l]]++;
            ac[A[r]]++;
        }
    }
    //vector<P> co;
    int ans = 0;
    if (N == 1 || ac.size() == 0) {
        ans = 0;
    }
    else {

        ans = ac.size() - 1;
    }
    cout << ans << endl;

    //return 0;
}

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

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

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

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

    bool same(T x, T y) { // 2つのデータx, yが属する木が同じならtrueを返す
        T rx = root(x);
        T ry = root(y);
        return rx == ry;
    }
    vector<vector<T>> linked_group() {
        vector<vector<T>> group(par.size());
        for (T i = 0; i < par.size(); i++)
        {
            T r = root(par[i]);
            group[r].emplace_back(i);
        }
        return group;
    }
};

void solve() {
    int N;
    cin >> N;
    vector<int> A(N + 1);
    map<int, int>ac;
    for (size_t i = 1; i < N + 1; i++)
    {
        cin >> A[i];
    }
    int l, r;
    int m = N / 2;
    UnionFind<int> uf(2*1e5+1);
    for (l = 1, r = N; l <= m; l++, r--)
    {
        if (A[l] != A[r]) {
            uf.unite(A[l], A[r]);
        }
    }
    vector<vector<int>> lg = uf.linked_group();
    int ans = 0;
    for (auto g : lg) {
        if (1 < g.size()) {
            ans += g.size() - 1;
        }
    }
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define pr(a) cout << a
#define prl(a) cout << (a) << endl
#define prld(a) cout << setprecision(15)<< (a) << endl
#define allrange(a) a.begin(),a.end()

struct GrowedUnionFind {
    std::unordered_map<int,int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    std::unordered_set<int> keys;
    int ccnum; //連結成分の個数

    GrowedUnionFind():par(),keys(){ ccnum =0;}

    void insert(int x)
    {
      if(!find(x)){
        keys.insert(x);
        par[x]=x;
        ccnum++;
      }
    }

    bool find(int x) //元が入っているか探す。平均o(1)
    {
      return keys.count(x)==1;
    }

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

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

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

int main(){
    std::cin.tie(0); // cinとcoutの同期を解除
    std::ios::sync_with_stdio(false);
    //1変数入力
    int N;
    cin >> N;
    bool flg=false;
    int ans;

    vector<int> A(N/2);
    vector<int> B(N/2);
    int j = N%2;//パリティ
    rep(i,N){
        if(i<N/2)
            cin >> A[i];
        else if(i>=N/2+j)
            cin >> B[i-N/2-j];
        else if(j==1 && i == N/2) cin >> ans; //捨て
    }
    ans=0;

    reverse(allrange(A));
    int n = N/2;

    GrowedUnionFind V;
    rep(i,n){
        if(A[i] != B[i])
        { V.insert(A[i]);V.insert(B[i]);V.unite(A[i],B[i]);}
    }

    //次数1の頂点はつぶせる
    //triangleが多くつぶせるといい
    //写像で頂点をつぶす射影写像の合成回数の問題
    //射影写像自体は可換なので, 実は順番はあまり関係ない
    //非連結成分はくっつける必要はない.
    //連結成分同士を1点に潰せればいい.
    // 頂点数-連結成分の個数

     ans = V.keys.size()-V.ccnum;

    flg = true;
    if(flg) prl(ans);
    else prl(-1);
}


/** This won't be the end of me */

#include <iostream>
#include <algorithm>

#define endl '\n'
#define FAST ios::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mt make_tuple
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
typedef long long ll;
typedef unsigned long long ull;

using namespace std;

int mx, k, m, n;
int a[100], b[100], c[16], d[16], dish[101];

void loop()
{
    int cnt = 0;
    for(size_t i = 0; i < m; ++i)
    {
        if(dish[a[i]] && dish[b[i]])
            ++cnt;
    }
    mx = max(mx, cnt);
//    cout << "max == " << mx << endl;
}

void check(int row, bool val)
{
    val == true ? ++dish[d[row]] : ++dish[c[row]];

//    cout << "incremented " << (val == true? d[row] : c[row]) << endl;

    if(row == k-1) // base case
        loop();
    if(row+1 < k)
    {
        check(row+1, false);
        check(row+1, true);
    }

    val == true ? --dish[d[row]] : --dish[c[row]];

//    cout << "decremented " << (val == true? d[row] : c[row]) << endl;

}

void solve()
{
    int i, j, l;

    cin >> n >> m;

    for(i = 0; i < m; ++i)
        cin >> a[i] >> b[i];

    cin >> k;

    for(i = 0; i < k; ++i)
        cin >> c[i] >> d[i];

    check(0, false); // activate c[row]
    check(0, true); //  activate d[row]

    cout << mx;

}

int main()
{
    FAST

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

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll,ll>;
using pi = pair<int,int>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define Please return
#define AC 0
const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//(a+b-1)/b
//priority_queue<ll, vector<ll>, greater<ll>> q;

signed main(){
	//cout << fixed << setprecision(10);
    int n,m; cin >> n >> m;
    vi a(m),b(m);
    rep(i,0,m)cin >> a[i] >> b[i];
    int k; cin >> k;
    vi c(k),d(k);
    rep(i,0,k){
        cin >> c[i] >> d[i];
    }
    int ans = 0;
    rep(i,0,(1<<k)){
        int tmp = 0;
        vi v(n);
        rep(j,0,k){
            if(i>>j&1){
                v[d[j]-1] = 1;
            }
            else{
                v[c[j]-1] = 1;
            }
        }
        rep(i,0,m){
            tmp += (v[a[i]-1]&&v[b[i]-1]);
        }
        chmax(ans,tmp);
    }
    cout << ans << endl;

	Please AC;
}


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

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

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

    cout << n - 1;
}

#include<iostream>
#include<bits/stdc++.h>
#define forf(i,start,n,increment) for(int i = start;i<n;i+=increment)
#define all(i) i.begin(),i.end()
#define traverse(container,it) for(typeof(container.begin()) it = container.begin();it != container.end();it++)
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;

        if(n%2 == 0)
        cout<<(n/2-1)*2+1;
        else
        cout<<(n/2)*2;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<utility>
#include<functional>
#include<cfenv>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define vint vector<int>
#define vvint vector<vint>
#define P pair<int,int>
#define INT_MAX 2147483647
#define MOD 1000000007
#define PI 3.14159265358979323846
#define all(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;

#define MAX 1000000000000

int main(void) {
	char s,t; cin >> s>>t;
	if (s == 'Y') {
		cout << char(t -32) << endl;
	}
	else {
		cout << t << endl;
	}
	return 0;
}

#include<bits/stdc++.h>

using    namespace std;
typedef  long long int ll;
typedef  unsigned long long int ull;
typedef  long double ld;

#define  all(v)       (v).begin(),(v).end()
#define  rall(v)      (v).rbegin(),(v).rend()
#define  pb           push_back
#define  pob          pop_back
#define  eb           emplace_back
#define  ins          insert
#define  mp           make_pair
#define  ff           first
#define  ss           second
#define  pf           push_front
#define  bs           binary_search
#define  lb           lower_bound
#define  ub           upper_bound
#define  sz(a)       (ll)a.size()
#define  dec(x)       fixed<<setprecision(x)
#define  lcm(a,b)    (a*b/__gcd(a,b))

typedef  priority_queue<ll>  pq;
typedef  priority_queue<ll,vector<ll>,greater<ll> > pqmn;
typedef  pair<ll,ll> pll;
typedef  vector<ll> vll;
typedef  vector<pll> vpll;
typedef  vector<vector<ll> > vvll;
typedef  vector<string> vs;
typedef  set<ll> sll;
typedef  set<pll> spll;
typedef  map<ll,ll> mll;

const    ll inf=LLONG_MAX;
const    ll mod=1e9+7;
const    ld pi=acos(-1);
const    ll N=100001;

void solution()
{
    ll x,y;
    cin >> x>> y;

    if(x == y) {
        cout << x;
    }
    else {
        cout << 3 - (x + y);
    }

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    t=1;
    //cin>>t;
    for(ll i = 1; i <= t ; i++) {
       // cout << "Case #" << i <<": ";
        solution();
        cout << "\n";
    }

    return 0;

}


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

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

  long long ans = N * (N + 1) * K * 100 + K * (K + 1) * N;
  ans /= 2;

  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
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; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,k;cin >> n >> k;
    int ans=0;
    FOR(i,1,n+1) FOR(j,1,k+1){
        ans+=100*i+j;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())
#define Output(a) cout << a << endl

typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;

const int INF = 1<<30;
const ll MOD = 1000000007;

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

/* 二次元配列
vector<vector<Type>> vv(n, vector<Type>(m, d));

vector<vector<ll>> vv(n, vector<ll>(m, d));
*/

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

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

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

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

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

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

    UnionFind tree(n);
    vector<unordered_map<ll, ll>> cn(n);
    int c;
    rep(i, n){
        cin >> c;
        c--;
        cn[i][c] = 1;
    }

    rep(i, q){
        ll a, x, y;
        cin >> a >> x >> y;
        x -= 1;
        y -= 1;
        if(a == 1){
            ll root_x = tree.root(x);
            ll root_y = tree.root(y);
            if(!tree.same(root_x, root_y)){

                // マージテク (要素の大きさが小さい方を移動させることで計算量を少なくする。)
                if(cn[tree.root(y)].size() < cn[tree.root(x)].size()) swap(root_x, root_y);

                tree.unite(root_x, root_y); // yが根になる
                for(auto [key, value] : cn[root_x]){
                    cn[tree.root(y)][key] += value;
                }
            }
        }else{ // a == 2
            Output(cn[tree.root(x)][y]);
        }
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
const int mod = 1e9+7, N = 2e5+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int n, m, k;
struct dsu{
	vector<int> p, rnk;
	dsu(int n){
		p.assign(n, 0);
		rnk.assign(n,0);
		for(int i=0;i<n;i++)p[i]=i;
	}
	int par(int u){
		if(p[u]==u)return u;
		else return p[u] = par(p[u]);
	}
	int merge(int u, int v){
		u = par(u);
		v = par(v);
		if(rnk[u] > rnk[v]){
			p[v] = u;
			rnk[u]++;
			return u;
		}else {
			p[u] = v;
			rnk[v]++;
			return v;
		}
	}
};
int a[N];
map<int, int> t[N];
void solve(int test_case){
	int i, j, q;
	cin >> n >> q;
	for(i=0;i<n;i++){
		cin >> a[i];
		a[i]--;
		t[i][a[i]]=1;
	}
	dsu d(n);
	while(q--){
		int typ, x, y;
		cin >> typ >> x >> y;
		x--,y--;
		if(typ == 1){
			x = d.par(x), y = d.par(y);
			if(x == y)continue;
			int u = d.merge(x, y);
			if(u != x)swap(x, y);
			for(auto [f, s] : t[y])t[u][f] += s;
		}else {
			cout << t[d.par(x)][y] << '\n';
		}
	}
}

signed main(){
	FASTIO;
#define MULTITEST 0
#if MULTITEST
	int ___T;
	cin >> ___T;
	for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
		solve(T_CASE);
#else
	solve(1);
#endif
	return 0;
}


#include <bits/stdc++.h>
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int N=160;
const int mod=1000000007;

int a,b;

int main() {
    cin>>a>>b;
    int ans=1;
    for(int i=b;i>=1;i--) {
        int r=b/i;
        int l=(a+i-1)/i;
        // cout<<i<<" "<<l<<"  "<<r<<endl;
        if(r-l+1>=2) {
            ans=i;
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include<stdlib.h>
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <stack>
#include<stdlib.h>
#include<stdio.h>
#include <sstream>
#include <inttypes.h>
#include<climits>

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

typedef  long long ll;
//typedef  unsigned long long int llt;
const int M = 1e6;

ll min(int x , int y){
    return ( x<y ? x:y);
}
int max(int x , int y){
    return ( x<y ? y:x);
}

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

// Function to return LCM of two numbers
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}
int findMaxGCD(int arr[], int n)
{
    // Calculating MAX in array
    int high = 0;
    for (int i = 0; i < n; i++)
        high = max(high, arr[i]);

    // Maintaining count array
    int count[high + 1];
    for(int i=0;i<=high;i++){
        count[i]=0;
    }
    for (int i = 0; i < n; i++)
        count[arr[i]]++;

    // Variable to store the
    // multiples of a number
    int counter = 0;

    // Iterating from MAX to 1
    // GCD is always between
    // MAX and 1. The first
    // GCD found will be the
    // highest as we are
    // decrementing the potential
    // GCD
    for (int i = high; i >= 1; i--)
    {
        int j = i;
       counter = 0;

        // Iterating from current
        // potential GCD
        // till it is less than
        // MAX
        while (j <= high)
        {
            // A multiple found

            if(count[j] >=2)
               return j;

           else if (count[j] == 1)
                counter++;

            // Incrementing potential
            // GCD by itself
            // To check i, 2i, 3i....
            j += i;

            // 2 multiples found,
            // max GCD found
            if (counter == 2)
                return i;
        }
    }
}

void solve(){

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

   /* int mx=1;

    for(int i=b;i>a;i--){

        int y =i;

        for(int j = y-1;j>=a;j--){

            mx = max(mx , gcd(y,j));
        }
    }
    cout<<mx<<endl;
    return;*/

    int arr[b-a+1];

    for(int i=0;i<(b-a+1);i++){
        arr[i] = a+i;
    }
    cout<<findMaxGCD(arr , (b-a+1));
    return;
}

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

        solve();
    return 0;

}


#include <bits/stdc++.h>
#define ll long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<#x<<"="<<x<<endl;
#define endl '\n'
#define M 1000000007
#define int long long
#define F first
#define S second
#define INF 1e18
#define N 1000005

using namespace std;
ll h, w, a, b;

ll go(vector<vector<ll>> &v, ll i, ll j, ll a, ll b)
{
	if (a < 0 || b < 0)return 0;
	if (j == w)
	{
		j = 0, ++i;
	}
	if (i == h)
	{
		return 1;
	}

	if (v[i][j] == 1)
	{
		return go(v, i, j + 1, a, b);
	}
	ll ans = 0;
	if (j + 1 < w && a > 0 && v[i][j] == 0 && v[i][j + 1] == 0)
	{
		v[i][j] = v[i][j + 1] = 1;
		ans += go(v, i, j + 1, a - 1, b);
		v[i][j] = v[i][j + 1] = 0;
	}
	if (i + 1 < h && a > 0 && v[i][j] == 0 && v[i + 1][j] == 0)
	{
		v[i][j] = v[i + 1][j] = 1;
		ans += go(v, i, j + 1, a - 1, b);
		v[i][j] = v[i + 1][j] = 0;
	}
	if (b > 0)
	{
		v[i][j] = 1;
		ans += go(v, i, j + 1, a, b - 1);
		v[i][j] = 0;
	}
	return ans;
}
void solve()
{
	cin >> h >> w;
	cin >> a >> b;
	vector<vector<ll>> v = vector<vector<ll>>(h, vector<ll>(w, 0));
	cout << go(v, 0, 0, a, b) << endl;

}

int32_t main()
{
	IOS
	ll T = 1;
	// cin >> T;
	for (ll i = 1; i <= T; ++i)
	{
		// cout<<"Case #"<<i<<": ";
		solve();
	}
	return 0;
}

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
// avoid unordered_map and unordered_set!!!
#include <vector>
#include <utility>

using namespace std;

using i64 = int64_t;
using u64 = uint64_t;
using i32 = int32_t;
using pi64 = pair<i64, i64>;

#define vec vector
#define let const
#define DRi64(x) i64 x; cin >> x;
#define DRS(x) string x; cin >> x;
#define DRVi64(v, n) vec<i64> v(n); { for (i64 i = 0; i < n; ++i) { cin >> v[i]; }}
#define DRpi64(x) pi64 x; cin >> x.first >> x.second;

#ifdef DEBUG
#define P(x) cerr << x << "\n"
#else
#define P(x)
#endif

constexpr i64 MAXN = 3*100*1000LL+5LL;
constexpr i64 MOD = 1000000007LL;
constexpr i64 INF64 = MOD * MOD;

const vec<vec<pi64>> kPieces{
    vec<pi64>
    {pi64{0, 0},},
    {pi64{0, 0},{0, 1},},
    {pi64{0, 0},{1, 0},},
};

i64
dfs(const i64 r, const i64 c, vec<vec<bool>>& G, const i64 R, const i64 C, const i64 A, const i64 B)
{
    let auto& INR = [R, C](const i64 u, const i64 v)
    {
        return 0 <= u && u < R && 0 <= v && v < C;
    };
    let auto& LIN = [C](const i64 u, const i64 v)
    {
        return u * C + v;
    };
    let auto& LIN_INV = [C](const i64 x)
    {
        return make_pair(x / C, x % C);
    };
    let auto& FIT = [&](const vec<pi64>& piece)
    {
        for (let auto [dr, dc] : piece)
        {
            if (!INR(r + dr, c + dc))
            {
                return false;
            }
            if (G[r + dr][c + dc])
            {
                return false;
            }
        }
        return true;
    };
    if (LIN(r, c) > LIN(R - 1, C - 1))
    {
        assert(A == 0);
        assert(B == 0);
        return 1;
    }
    P("r: " << r << " c: " << c);
    let pi64 nxt(LIN_INV(LIN(r, c) + 1));
    if (G[r][c])
    {
        return dfs(nxt.first, nxt.second, G, R, C, A, B);
    }
    i64 acc = 0;
    for (i64 pi = 0; pi < kPieces.size(); ++pi)
    {
        let auto& piece = kPieces[pi];
        if (!FIT(piece))
        {
            continue;
        }
        if (pi == 0 && B == 0)
        {
            continue;
        }
        if (pi > 0 && A == 0)
        {
            continue;
        }
        for (let auto [dr, dc] : piece)
        {
            G[r + dr][c + dc] = true;
        }
        acc += dfs(nxt.first, nxt.second, G, R, C, A - (pi > 0), B - (pi == 0));
        for (let auto [dr, dc] : piece)
        {
            G[r + dr][c + dc] = false;
        }
    }
    return acc;
}

int
main()
{
    ios_base::sync_with_stdio(false);
    // fast io: see 1423K
    cin.tie(nullptr);
    cout.tie(nullptr);

    DRi64(R); DRi64(C); DRi64(A); DRi64(B);
    vec<vec<bool>> G(R, vec<bool>(C, false));
    cout << dfs(0, 0, G, R, C, A, B) << "\n";

    return 0;
}


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

typedef long long ll;
typedef std::vector<std::vector<int64_t> > Graph;

#define in1(n) int64_t n;cin >> n;
#define in2(n, m) int64_t n, m;cin >> n >> m;
#define in3(n, m, k) int64_t n, m, k;cin >> n >> m >> k;
#define in4(a, b, c, d) int64_t a, b, c, d;cin >> a >> b >> c >> d;
#define rep(i, n) for (int64_t i = 0; i < n; ++i)
#define rep2(i, n) for (int64_t i = 1; i <= n; ++i)
#define repb(i, l, n) for (int64_t i = l; i < n; ++i)
#define repb2(i, l, n) for (int64_t i = l; i <= n; ++i)
#define repc(i, l, n, d) for (int64_t i = l; i < n; i+=d)
#define repc2(i, l, n, d) for (int64_t i = l; i <= n; i+=d)
#define rep_(i, r) for (int64_t i = r-1; i >= 0; --i)
#define rep_2(i, r) for (int64_t i = r; i > 0; --i)
#define rep_b(i, r, l) for (int64_t i = r; i >= l; --i)
#define rep_b2(i, r, l) for (int64_t i = r; i > l; --i)
#define rep_c(i, r, l, d) for (int64_t i = r; i >= l; i-=d)
#define rep_c2(i, r, l, d) for (int64_t i = r; i > l; i-=d)
#define repf(i, l, c, d) for (int64_t i = l; c; i+=d)
#define repi(a, b) for (auto&(a) : (b))
#define ALL(v) (v).begin(), (v).end()
#define Sort(x) sort(ALL(x))
#define Sort_rev(x) Sort(x);reverse(ALL(x))
#define Sort_pair(x, p) sort(ALL(x), (p))
#define mp(a, b) make_pair((a), (b))
#define Push_back(a, b) push_back( mp( (a), (b) ) )
#define ctoi(c) ((c)-'0')

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 V,typename T> bool find_num(V v, T num) { if ( find(ALL(v), num) == v.end() ) { return false; } return true; }

const int inf = 0x3fffffff;
const int64_t INF = 0x3fffffffffffffff;
const int64_t MOD = 1e9+7;
/*
int main() {
    in1(n);
    std::vector<int64_t> a(n);
    int64_t sum = 0, num;
    rep(i, a.size()) {
        cin >> a[i];

        if ( i == 0 ) {
            num = a[i];
        } else {
            chmax(num, a[i]);
        }
        sum += a[i];

        std::vector<int64_t> v = a;
        int64_t ans = sum, num2 = num;
        rep(j, i+1) {
            v[j] += num2;
            ans += num2;
            chmax(num2, v[j]);
        }
        cout << ans << endl;
    }
    return 0;
}
*/

int main() {
    in1(n);
    std::vector<int64_t> a(n);
    rep(i, a.size()) {
        cin >> a[i];
    }

    int64_t num = 0, sum = 0, sum2 = 0;
    rep(i, n) {
        chmax(num, a[i]);
        sum += a[i];
        sum2 += sum;
        cout << num*(i+1)+sum2 << endl;
    }
}

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

#define PI 3.141592653589
#define ll long long int
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define vv vector
#define all(v) (v).begin(),(v).end()
int MOD=1e9+7;
ll power(ll a, ll b){//a^b
    ll res=1;
    a=a%MOD;
    while(b>0){
        if(b&1){res=(res*a)%MOD;b--;}
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
ll fermat_inv(ll y){return power(y,MOD-2);}
ll gcd(ll a, ll b){return (b==0)?a:gcd(b,a%b);}
ll min(ll a,ll b){return (a>b)?b:a;}
ll max(ll a,ll b){return (a>b)?a:b;}
bool prime[1000001];
vi primes;
void SieveOfEratosthenes(int n)
{
    memset(prime, true, sizeof(prime));
    prime[0]=prime[1]=0;
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for(int p=2;p<1000001;p++)
        if(prime[p])
            primes.pb(p);
}
ll fact[100010];
ll finv[100010];
void factorial(int n){
    fact[0]=1;
    finv[0]=1;
    for(int i=1;i<=n;i++)
        fact[i]=fact[i-1]*i,fact[i]%=MOD,finv[i]=fermat_inv(fact[i]);
}
ll ncr(ll n,ll r)
{
    if(n<r)
        return 0;
    else{
        ll x=finv[r]*finv[n-r]%MOD;
        return fact[n]*x%MOD;
    }
}
double dp[101][101][101];
double solve(double a,double b,double c){
    //cout<<a<<' '<<b<<' '<<c<<'\n';
    if(dp[(int)a][(int)b][(int)c]!=-1)
        return dp[(int)a][(int)b][(int)c];
    if(a==100||b==100||c==100)
        return 0;
    return dp[(int)a][(int)b][(int)c]=a/(a+b+c)*(solve(a+1,b,c)+1)+b/(a+b+c)*(solve(a,b+1,c)+1)+c/(a+b+c)*(solve(a,b,c+1)+1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int te=1;
    //cin>>te;
    //SieveOfEratosthenes(1000000);
    //factorial(100005);
    while(te--){
        double a,b,c;
        cin>>a>>b>>c;
        for(int i=0;i<=100;i++)
            for(int j=0;j<=100;j++)
                for(int k=0;k<=100;k++)
                    dp[i][j][k]=-1;
        cout<<setprecision(15)<<solve(a,b,c);
    }
}


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

const int mod = (int)1e9 + 7;
int power(int x, int n, int m){
    x %= m ;
    int ans = 1 ;
    while(n > 0){
        if( n & 1 ) ( ans *= x ) %= m ;
        ( x = x * x ) %= m ;
        n >>= 1 ;
    }
    return ans ;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n; cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    int res = a[0] + 1;
    for(int i = 1; i < n; i++){
        res = (res * (a[i] - a[i - 1] + 1)) % mod;
    }
    cout << res << "\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n;
  R n;
  vector<ll> a(n+1,0);
  rep(i,n) R a[i];
  sort(all(a));
  a.erase(unique(all(a)),a.end());
  ll ans=1;
  rrep(i,a.size()-1) {
    ans*=a[i+1]-a[i]+1;
    ans%=MAX;
  }
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


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

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

}

#include <bits/stdc++.h>
#define PI acos(-1)
#define eq(x,y) (fabs((x)-(y)) < eps)
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define pb push_back
#define eb emplace_back
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define bump cout<<"bleh\n"
#define all(v) (v).begin(),(v).end()
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define file freopen("input.txt", "r", stdin)
#define reset(x,n) memset(x,n,sizeof(x))

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
*/

/*
find_by_order(k): It returns to an iterator to the kth element (counting from zero)
order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
*/

using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf=1e9+7;
const int nmax=1e5+10;
const int eps=1e-8;
const ll infll=1e18+10;

const int len = 3e5+10;
int tree[4*len];
int ara[len];

void build(int node, int start, int endd)
{
    if(start==endd)
    {
        tree[node]=ara[start];
    }else
    {
        int mid=(start+endd)/2;
        build(2*node,start,mid);
        build(2*node+1,mid+1,endd);

        tree[node]=tree[2*node]^tree[2*node+1];
    }
}

void update(int node, int start, int endd, int idx, int val)
{

    if(start==endd)
    {
        ara[idx]=val;
        tree[node]=val;

    }else
    {
        int mid=(start+endd)/2;

        if(idx>=start and idx<=mid)
            update(2*node,start,mid,idx,val);
        else
            update(2*node+1,mid+1,endd,idx,val);

        tree[node]=tree[2*node]^tree[2*node+1];
    }

}

int query(int node,int start,int endd, int l, int r)
{
    if(start>=l and endd<=r)
        return tree[node];

    if(start>r or endd<l)
        return 0 ;

    int mid=(start+endd)/2;
    int p1=query(2*node,start,mid,l,r);
    int p2=query(2*node+1,mid+1,endd,l,r);
    int ans=p1^p2;

    return ans;

}

inline void solve()
{
	int a,b,c,d;
	cin>>a>>b>>c>>d;

	int x = (a*d) - (b*c);
	cout<<x<<"\n";

}
/*
-> sometimes thinking in reverse makes it easier!
-> check for overflow! pay attention to constraints!
-> maybe write a bruteforce locally for clues!
*/

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

    fastio;
	//int t;
	//cin>>t;

	//while(t--){
		solve();
	//}

    return 0;
}

/*

*/


#include <iostream>

using namespace std;

int main()
{
	int n, x,input;
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
		cin >> input;
		if (input != x)
		{
			cout << input<<endl;
		}
	}
}

#include <bits/stdc++.h>
#define FIXED_FLOAT(x)  std::fixed <<std::setprecision(7)<<(x)
#define all(v) (v).begin(), (v).end()
using namespace  std;
#define loop(i,a,b) for (int i=a; i<b; ++i)
using ll = long long;
const int mod = 998244353;
typedef pair<int , int> pairs;
typedef complex<ll> G;

const int N = 1e3 + 5;

int power(int a,int b){
    if(!b)
        return 1;
    int c=power(a,b/2);
    c=(1LL*c*c)%mod;
    if(b%2)
        c=(1LL*c*a)%mod;
    return c;
}

void done() {

}

/*string solve_palindrome(const string& s)
{
    string a = s;
    reverse(a.begin(), a.end());
    //a = s + "#" + a;
    int c = 0;
    for (int i = 1; i < (int)a.size(); i++)
    {
        while (c != 0 && a[c] != a[i])
            c = pref[c - 1];
        if (a[c] == a[i])
            c++;
        pref[i] = c;
    }
    return s.substr(0, c);
}
*/

/*3
3 7 7 9 11
4 6 6 7*/
void solve()
{
    int n, m;
    cin >> n >> m;
    int c[n][m];
    for(int i = 0;i < n;++i) {
        for(int j = 0;j < m;++j) {
            cin >> c[i][j];
        }
    }

}

int n, m, q;
string s, t;
int cc[N];
int pref[N];
bool check(int l, int r) {
	for (int i = l, j = 1; i <= r; i++, j++) {
		if (s[i] != t[j]) {
			return false;
		}
	}
	return true;
}

void another() {
    char a, b;
    cin >> a >> b;
    if(a == 'Y')b = toupper(b);
    cout << b << '\n';

}

void test_case() {
    int t;
    cin >> t;
    while(t--)another();
}
int main() {

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

    another();
}


#include <vector>
#include <stdio.h>
#include <algorithm>

int n, fa[100005], head[100005], cnt, dp[100005], size[100005];
struct edge { int to, nxt; } g[100005];

inline void add_edge(int f, int t) {
	g[++cnt].to = t;
	g[cnt].nxt = head[f];
	head[f] = cnt;
}

void dfs(int u) {
	dp[u] = 1; size[u] = 1;
	std::vector<int> vs;
	int side = 1, sum = 0;
	for (int i = head[u]; i; i = g[i].nxt) {
		dfs(g[i].to);
		size[u] += size[g[i].to];
		if (size[g[i].to] & 1) vs.push_back(dp[g[i].to]);
		else if (dp[g[i].to] >= 0) sum += dp[g[i].to];
		else dp[u] += dp[g[i].to];
	}
	std::sort(vs.begin(), vs.end());
	for (int val : vs) {
		dp[u] += side * val;
		side = -side;
	}
	dp[u] += side * sum;
}

int main() {
	scanf("%d", &n);
	for (int i = 2; i <= n; ++i) scanf("%d", fa + i), add_edge(fa[i], i);
	dfs(1);
	printf("%d\n", (n + dp[1]) >> 1);
	return 0;
}


/** author: samari06,  created: 09.03.2021 21:49:15 **/
#include <bits/stdc++.h>
#define REP(i, N) for(int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
typedef vector<int> V;
typedef vector<ll> Vll;

int t[55][55], p[55][55], sx, sy;
int cscr = 0, bstscr = 0;
string dist = "RLDU";

class Timer {
    private:
        clock_t time_start;    // 計測開始時間

    public:
        Timer() { restart(); }

        void restart() {
            time_start = clock();
        }

        clock_t elaps() {             // 計測開始時間からの秒数を返す
            clock_t time_end = clock();
            return (time_end - time_start);
        }
};

// 疑似乱数列生成 (https://ja.wikipedia.org/wiki/Xorshift)
uint32_t xor32(void) {
  static uint32_t y = 2463534242;
  y ^= (y << 13); y ^= (y >> 17);  y ^= (y << 5);
  return y;
}

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

int score(string &s){
    int ch[2500] = {0};
    int scr = p[sx][sy];
    int px = sx, py = sy;
    ch[t[sx][sy]] = 1;
    REP(i,s.size()){
        if(s[i] == 'R'){
            py++;
            if(py >= 50) return 0;
            if(ch[t[px][py]]) return 0;
        }else if(s[i] == 'L'){
            py--;
            if(py < 0) return 0;
            if(ch[t[px][py]]) return 0;
        }else if(s[i] == 'D'){
            px++;
            if(px >= 50) return 0;
            if(ch[t[px][py]]) return 0;
        }else if(s[i] == 'U'){
            px--;
            if(px < 0) return 0;
            if(ch[t[px][py]]) return 0;
        }

        ch[t[px][py]] = 1;
        scr += p[px][py];
    }

    return scr;
}

string solve(){
    string ans;
    Timer tmr;
    const double TL = 1.9;
    double time = 0.0;

    // 初期解の生成
    while(time < TL){
        time = (double)tmr.elaps() / CLOCKS_PER_SEC;

        string tans;
        tans += dist[xor32()%4];
        if(score(tans) != 0){
            ans = tans;
            break;
        }
    }

    while(time < TL){
        time = (double)tmr.elaps() / CLOCKS_PER_SEC;
        int id = xor32()%ans.size();
        string tans = ans;
        int tscr = bstscr;

        id = (int)tans.size()-1;

        if(id+1 == tans.size()){
            tans += dist[xor32()%4];
            if(score(tans) == 0) continue;
        }else{

        }

        ans = tans;
    }

    return ans;
}

int main(){
    // 入力
    scanf("%d%d", &sx, &sy);
    REP(j,50) REP(i,50) scanf("%d", &t[j][i]);
    REP(j,50) REP(i,50) scanf("%d", &p[j][i]);

    // 求解
    string sol = solve();

    // 出力
    cout << sol << "\n";

    // score
    //printf("score: %d\n",score(sol));

    return 0;
}

#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <cassert>
#include <fstream>

#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#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 rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }
template<class T, class U>
T &chmin(T &a, const U &b){ if(a > b) a = b; return a; }
#define y first
#define x second
constexpr bool istest = false;
constexpr int n = 50;
ifstream file;
ofstream out;
void input_setup(){
	if(istest){
		cout << "write the in file number\n";
		string s;
		cin >> s;
		file.open("C:/Users/souta/downloads/ahc/tools/in/" + s + ".txt");
		if(file.fail()){
			cout << "the file was not found\n";
			return;
		}
		cin.rdbuf(file.rdbuf());
	}
}
//答えるときのやつ
string ans = "";
void output(){
	if(istest){
		out.open("C:/users/souta/downloads/ahc/tools/out.txt");
		if(out.fail()){
			cout << "the file was not found\n";
			return;
		}
		cout.rdbuf(out.rdbuf());
	}
	cout << ans << "\n";
}
pii st_pos;
vvi tiles(n, vi(n)), point(n, vi(n));
void inputs(){
	//start position
	cin >> st_pos.y >> st_pos.x;
	//tiles
	rep(i, n) rep(j, n) cin >> tiles[i][j];
	//points
	rep(i, n) rep(j, n) cin >> point[i][j];
}

//過去に同じ番号のタイルを通ったことがあるのかを確認する
bool checkVisited(const int &Y, const int &X, const vector<pii> &logs){
	int col = tiles[Y][X];
	for(auto a : logs){
		if(col == tiles[a.y][a.x]) return true;
	}
	return false;
}
//とりあえずBFSで探索してみる
int dy[] = {-1,1,0,0};
int dx[] = {0,0,-1,1};
char dir[] = {'U', 'D', 'L', 'R'};
void bfs(){
	priority_queue<pair<pii, int>> que;
	vvi checked(n, vi(n, -1));
	checked[st_pos.y][st_pos.x] = point[st_pos.y][st_pos.x];
	vector<vector<vector<pii>>> logs(n, vector<vector<pii>>(n));

	logs[st_pos.y][st_pos.x].push_back(make_pair(st_pos.y, st_pos.x));

	pii bestpos = st_pos;
	int mx = point[st_pos.y][st_pos.x];

	rep(loop, 10){

		vector<pii> temp = logs[bestpos.y][bestpos.x];
		logs = vector<vector<vector<pii>>>(n, vector<vector<pii>>(n));
		checked = vvi(n, vi(n, -1));
		logs[bestpos.y][bestpos.x] = temp;
		checked[bestpos.y][bestpos.x] = mx;
		que.push(make_pair(bestpos, mx));

		while(!que.empty()){
			pii pos; int score;
			tie(pos, score) = que.top();
			que.pop();
			if(checked[pos.y][pos.x] > score) continue;
			rep(i, 4){
				int Y = pos.y + dy[i];
				int X = pos.x + dx[i];
				if(Y >= n || X >= n || Y < 0 || X < 0) continue;
				if(checkVisited(Y, X, logs[pos.y][pos.x])) continue;

				int v = score + point[Y][X];
				if(checked[Y][X] == -1 || checked[Y][X] < v){
					que.push(make_pair(make_pair(Y, X), v));
					checked[Y][X] = v;
					logs[Y][X] = logs[pos.y][pos.x];
					logs[Y][X].push_back(make_pair(Y, X));
				}
			}
		}

		mx = 0;
		bestpos = st_pos;
		rep(i, n) rep(j, n){
			if(mx < checked[i][j]){
				bestpos = make_pair(i, j);
				mx = checked[i][j];
			}
		}
	}

	vector<pii> track = logs[bestpos.y][bestpos.x];
	pii last = st_pos;
	for(auto a : track){
		rep(i, 4){
			if(last.y + dy[i] == a.y && last.x + dx[i] == a.x){
				ans += dir[i];
				break;
			}
		}
		last = a;
	}
}
int main(){
	input_setup();
	inputs();

	bfs();

	output();
}


#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 4020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
    string s; cin >> s;
    int ans = 0;
    string t = "ZONe";
    rep(i,9){
        if(s.substr(i,4) == t) ans++;
    }
    cout << ans << endl;
}

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s; cin >> s;
    int count = 0;
    for(int i = 0; i+3 < s.size(); ++i){
        if(s[i] == 'Z' && s[i+1] == 'O' && s[i+2] == 'N' && s[i+3] == 'e'){
            count++;
        }
    }
    cout << count << endl;
}


#include <bits/stdc++.h>
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rrepi(i,a,b) for(int i=((int)(b)-1);i>=(a);--i)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) rrepi(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(x) (x).erase(unique(ALL(x)), (x).end())
#define TOUPPER(x) transform(ALL(x), (x).begin(), ::toupper)
#define TOLOWER(x) transform(ALL(x), (x).begin(), ::tolower)
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int,int>;
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;}
ll qp(ll a, ll b){ll ans=1;do{if(b&1)ans=1ll*ans*a;a=1ll*a*a;}while(b>>=1);return ans;}
ll qp(ll a, ll b, ll mo){ll ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a, int b){return b?gcd(b,a%b):a;}
const int INF = 1e9;
const ll INFL = 1e18;
const int MOD = 1e9 + 7;

int main(){
    int A, B;
    cin >> A >> B;
    vector<int> EA(A,0), EB(B,0);
    rep(i,min(A,B)) {
        EA[i] = EB[i] = i+1;
    }
    if(A > B) {
        int idx = 0;
        rep(i,A-B) {
            EA[B+i] = EA[B+i-1]+1;
            rep(j,EA[B+i]) {
                ++EB[(B-1-(idx++))%B];
                idx %= B;
            }
        }
    } else if(A < B) {
        int idx = 0;
        rep(i,B-A) {
            EB[A+i] = EB[A+i-1]+1;
            rep(j,EB[A+i]) {
                ++EA[(A-1-(idx++))%A];
                idx %= A;
            }
        }
    }
    rep(i,A+B) {
        cout << (i < A ? EA[i] : -EB[i-A]) << (i == A+B-1 ? "\n" : " ");
    }
    return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>   // popen, pclose, fgets
#include <cstdlib>
using namespace std;
#define rep0(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>;
using ll = long long; //long longをllだけにした
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll =vector<vvll>;
const double PI=3.14159265358979323846;
//小数点以下を指定したい時　　→→→→  cout << fixed << setprecision();
//int→string　　string str = to_string(num);
//string→int   int num = atoi(numStr.c_str());

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


#include<iostream>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int get(int a,int b)
{
	if(b<0)	return 0;
	return 1LL*(b-a+1)*(a+b)/2%mod;
}
int main()
{
	int t;scanf("%d",&t);
	while(t--){
		ll n,a,b;cin>>n>>a>>b;
		if(a>b)swap(a,b);
		ll ans=2LL*((n-a+1)*(n-b+1)%mod*get(1,n-a-b+1)%mod)%mod;
		//cout<<ans<<" ";
		ll temp=0,t1=0;
//		for(int i=1;i<=n-a+1;i++){
//			ll t2=t1;
//			for(int j=max(1LL,i-b+1);j<=min(n-b+1,i+a-1);j++)
//				t1++;
//			cout<<t1-t2<<endl;
//		}
		temp=(temp+get(a,min(b,n-a-b+2)+a-1))%mod;
		//cout<<temp<<" ";
		if(b+1<=n-a-b+2)
			temp=(temp+(a+b-1)*(n-a-b-b+2)%mod)%mod;
		//cout<<temp<<" ";
		if(b>=n-a-b+3)
			temp=(temp+(n-b+1)*(2*b+a-n-2)%mod)%mod;
		//cout<<temp<<" ";
		temp=(temp+get(a,n-max(b+1,n-a-b+3)+1)%mod)%mod;
		//cout<<temp<<" "<<t1<<" ";
		temp=2LL*temp*get(1,n-a-b+1)%mod;
		ans=(ans+temp+mod)%mod;
		cout<<ans<<endl;
	}
}

//#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if (*ci++ == '0') {
				tmp = tmp * 10;
				ci++;
			}
		}
	}
	return tmp;
}

const int MAX = 100000;
class shuturyoku_unko {
public:
	char C[MAX * 5];
	constexpr shuturyoku_unko() : C() {
		rep(i, MAX) {
			int X = i;
			rep(j, 5) {
				C[i * 5 + 4 - j] = '0' + X % 10;
				X /= 10;
			}
		}
	}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char dn[dm], * di = dn, * owad = dn + dm - 20;
void putint(ll A) {
	if (owad < di) {
		fwrite(dn, 1, di - dn, stdout);
		di = dn;
	}
	int dig = 1;
	if (A >= 100000) {
		if (A >= 1000000000) dig = 5;
		else if (A >= 100000000) dig = 4;
		else if (A >= 10000000) dig = 3;
		else if (A >= 1000000) dig = 2;
		memcpy(di + dig, f.C + A % 100000 * 5, 5);
		A /= 100000;
		memcpy(di, f.C + A * 5 + 5 - dig, dig);
		di += 5 + dig;
	}
	else {
		if (A >= 10000) dig = 5;
		else if (A >= 1000) dig = 4;
		else if (A >= 100) dig = 3;
		else if (A >= 10) dig = 2;
		memcpy(di, f.C + A * 5 + 5 - dig, dig);
		di += dig;
	}
	*di++ = '\n';
}
const int mod = 1e9 + 7;

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

	int T = getint();
	rep(t, T) {
		ll N = getint(), A = getint(), B = getint();

		ll a = min(A, N - B + 1);
		ll b = min(A + B - 1, N - B + 1);

		ll are = (a + b) * (b - a + 1) + b * (N - A + 1 - (b - a + 1) * 2);
		ll atti = (N - A + 1) * (N - B + 1);

		ll kotae = (atti + are) % mod * ((atti - are) % mod) % mod;
		putint(kotae);
	}
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    lli n,x,i;
    cin>>n>>x;
    string s;
    cin>>s;
    for(i=0;i<s.size();i++)
    {
        if(s[i]=='x' && x>0)
         x=x-1;
        if(s[i]=='o')
         x=x+1;
    }
    cout<<x<<"\n";
}

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

void chmax(LL&l,LL r){ if(l<r) l=r; }

int N;
LL X;
LL A[100];

LL dp[101][100];
void solvedp(int K){
  rep(n,K+1) rep(k,K) dp[n][k]=-1000000000000;
  dp[0][0]=0;
  rep(i,N){
    int dk=A[i]%K;
    for(int n=K-1; n>=0; n--) rep(k,K){
      int to_k=k+dk; if(to_k>=K) to_k-=K;
      chmax(dp[n+1][to_k],dp[n][k]+A[i]);
    }
  }
}

int main(){
  cin>>N>>X;
  rep(i,N) cin>>A[i];
  LL ans=X;
  for(int K=1; K<=N; K++){
    solvedp(K);
    if(dp[K][X%K]<0) continue;
    ans=min(ans,(X-dp[K][X%K])/K);
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

struct Edge{
  int to;
  ll T;
  ll K;

  Edge(){}
  Edge(int to,ll T,ll K):to(to),T(T),K(K){}
};

const ll INF=1e18;

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

  int N,M,X,Y;
  cin>>N>>M>>X>>Y;
  X--;Y--;
  vector<vector<Edge>> g(N);
  rep(i,M){
    int a,b,t,k;
    cin>>a>>b>>t>>k;
    a--;b--;
    g[a].push_back(Edge{b,t,k});
    g[b].push_back(Edge{a,t,k});
  }

  vector<ll> dist(N,INF);
  dist[X]=0;
  priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> PQ;
  PQ.push(mkp(0,X));
  while(!PQ.empty()){
    auto f=PQ.top();
    PQ.pop();

    int now=f.second;
    ll cost=f.first;
    if(dist[now]<cost) continue;

    for(auto &e:g[now]){
      ll neco=cost+(e.K-cost%e.K)%e.K+e.T;
      if(dist[e.to]>neco){
        dist[e.to]=neco;
        PQ.push(mkp(neco,e.to));
      }
    }
  }

  if(dist[Y]==INF) cout<<-1<<endl;
  else cout<<dist[Y]<<endl;

  return 0;
}

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

int a, b, c, d;

int main(){
    cin>>a>>b>>c>>d;
    //max x-y
    cout<<max(a,b) - min(c,d)<<"\n";
}


#include <bits/stdc++.h>
//#include "atcoder/all"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
//using namespace atcoder;
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> P;
constexpr ll inf=1ll<<61;
constexpr ll mod=998244353;
int a[1050000];
int n=1;
int calc(int A,int B,int k,int l,int r){
	if(r<=A||B<=l)return 0;
	if(A<=l&&r<=B)return a[k];
	int vl=calc(A,B,k*2+1,l,(l+r)/2),vr=calc(A,B,k*2+2,(l+r)/2,r);
	return vl^vr;
}
int get(int l,int r){
	return calc(l,r,0,0,n);
}
int main(){
	int N,q;cin>>N>>q;
	while(n<N)n*=2;
	rep(i,N)cin>>a[n-1+i];
	for(int i=n-2;i>=0;i--)a[i]=a[2*i+1]^a[2*i+2];

	rep(i,q){
		int t;cin>>t;
		int x,y;cin>>x>>y;
		x--;
		if(t==1){
			x+=n-1;
			a[x]^=y;
			while(x){
				x=(x-1)/2;
				a[x]=a[x*2+1]^a[x*2+2];
			}
		}
		else{
			cout<<get(x,y)<<endl;
		}
	}
}

//Code by Mukul Totla
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
void sublime(){
    #ifndef ONLINE_JUDGE
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
      }

int main()
{
   sublime();
    ll n,i,j;
     ll q;
     cin>>n>>q;
     ll arr[n];
     for(i=0; i<n; i++)
      cin>>arr[i];
    ll rootn=sqrt(n);
    vector<ll>v;
    ll val=0;
    ll cnt=0;
    for(i=0; i<n; i++)
    {
      val^=arr[i];
      cnt++;
      if(cnt==rootn)
      {
        cnt=0;
        v.push_back(val);
        val=0;
      }
    }
    if(cnt!=0)
      v.push_back(val);
    while(q--)
    {
      ll t,x,y;
      cin>>t>>x>>y;
      x--,y--;
      if(t==1)
      {
        y++;
        ll block=x/rootn;
        v[block]^=y;
        arr[x]^=y;
      }
      else
      {
        ll b1=x/rootn, b2=y/rootn;
        ll ans=0;
        if((b2-b1)>2)
        {
           for(i=b1+1; i<=b2-1; i++)
            ans^=v[i];
         // cout<<ans<<endl;
           for(i=x; i<(b1+1)*rootn; i++)
            ans^=arr[i];

          for(i=b2*rootn; i<=y; i++)
            ans^=arr[i];
          cout<<ans<<endl;
        }
        else
        {
          for(i=x; i<=y; i++)
            ans^=arr[i];
          cout<<ans<<endl;
        }
      }
    }
  return 0;
}


#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 998244353
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#define hmap gp_hash_table<ll, ll>

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

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

ll powa(ll a, ll b, ll c)
{
    a%=c;
    if(a<0)
	a+=c;
    ll res = 1;
    while(b>0)
    {
        if(b&1)
            res*=a, res%=c;
        a*=a, a%=c;
        b>>=1;
    }
    return res;
}

int main()
{
  FAST/**/

  ll n,m;
  cin>>n>>m;

  string arr[n];
  rep(n)
        cin>>arr[i];
  map<ll,map<char,ll>> ma;
  rep(n)
        repi(j,m)
            ma[i+j][arr[i][j]]++;
  ll ans = 1;
  autoit(ma,it)
  {
      map<char,ll> &m = it->ss;
      ll cr = m['R'];
      ll cb = m['B'];
      ll un = m['.'];
      //cout<<"on "<<it->ff<<" "<<cr<<" "<<cb<<" "<<un<<'\n';
      if(cr>0 && cb>0)
      {
          cout<<0<<"\n";
          return 0;
      }

      if(cr == 0 && cb == 0)
      {
          //cout<<"at "<<it->ff<<"\n";
          ans *= 2;
          ans%=mod;
          continue;
      }
  }
  cout<<ans<<"\n";

  return 0;
}

#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define int long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
int n,m;
char a[510][510];
int cntr,cntb;
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>(a[i]+1);
	for(int i=1;i<=m;i++){
		bool flgr=0,flgb=0;
		for(int x=1,y=i;x<=n&&y>=1;x++,y--){
			if(a[x][y]=='R')flgr=1;
			if(a[x][y]=='B')flgb=1;
		}
		if(flgr==1&&flgb==1)return puts("0"),0;
		if(flgr==1)cntr++;
		if(flgb==1)cntb++;
	}
	for(int i=2;i<=n;i++){
		bool flgr=0,flgb=0;
		for(int x=i,y=m;x<=n&&y>=1;x++,y--){
			if(a[x][y]=='R')flgr=1;
			if(a[x][y]=='B')flgb=1;
		}
		if(flgr==1&&flgb==1)return puts("0"),0;
		if(flgr==1)cntr++;
		if(flgb==1)cntb++;
	}
	int t=n+m-1LL-cntr-cntb;
//	cout<<t<<endl;
	int ans=1LL;
	for(int i=1;i<=t;i++)ans=ans*2LL%998244353LL;
	cout<<ans<<endl;
	return 0;
}
/*
rrb
rbr
brr

r.r
.br
...

#####
#####
#####
*/


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

int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	rep(i, n) cin >> a.at(i);
	vector<ll> b(410);
	rep(i, n) b.at(a.at(i) + 200)++;

	ll ans = 0;
	for (ll i = 1; i < b.size(); i++) {
		for (ll j=0;j<i;j++){
			ans += b.at(i) * b.at(j) * (i - j) * (i - j);
		}
	}
	cout << ans << endl;

}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

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

    int n;
    cin >> n;

    ll r = 0, s = 0;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;

        s += a;
        r += a * a;
    }

    r *= n;
    r -= s * s;

    cout << r << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000;//10の12乗

int main() {
    ll n,w;
    cin >> n >> w;
    Vl v(200010);
    rep(i,n){
        ll x,y,z;
        cin >> x >> y >> z;
        v[x]+=z;v[y]-=z;
    }
    bool b=1;
    rep(i,200005){
        v[i+1]+=v[i];
        if(v[i+1]>w||v[i]>w)b=0;
        //if(i<=100)cout << v[i+1] << endl;
    }
    if(b) cout << "Yes" << endl;
    else cout << "No" << endl;
}

//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>

#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp pair<pair<int,int>,int>
#define setl pair<LL,LL>
#define micro 0.000001

using namespace std;

LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); }
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#else
#define AT
#define AE
#define ce(t)
#define __popcnt __builtin_popcountll
#endif

pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,0},{1,0}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } };

const int max_v = 5e2 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
const LL mod = 1e9 + 7;

int arr[max_v][max_v];
int col[max_v];
int ans[max_v];

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n; ci(n);
	fa(i, 0, n) fa(j, 0, n)ci(arr[i][j]);

	fa(j, 0, n)
	{
		col[j] = arr[0][j];
		fa(i, 1, n) ckmin(col[j], arr[i][j]);
	}

	int row = arr[0][0];
	fa(i, 0, n) ckmin(row, arr[0][i]);

	bool ck = true;
	fa(i, 0, n)
	{
		if (col[i] >= arr[0][i] - row) col[i] = arr[0][i] - row;
		else ck = false;
	}
	ans[0] = row;

	fa(i, 1, n)
	{
		int row = arr[i][0];
		fa(j, 1, n) ckmin(row, arr[i][j]);
		ans[i] = row;
		fa(j, 0, n)
		{
			if (col[j] != arr[i][j] - row) ck = false;
		}
	}

	if (ck)
	{
		co("Yes\n");

		fa(i, 0, n) co(ans[i] << " ");
		co("\n");
		fa(i, 0, n) co(col[i] << " ");
	}
	else co("No\n");

	return 0;
}


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

int n, m, adj[20];
int dp[262144];

void solve(int, int, int, int);

int get(int x) {
    if (!x) return 0;
    if (dp[x] != 2e9) return dp[x];
    solve(0, x, 0, x);
    return dp[x];
}

void solve(int p, int r, int s, int d) {
    if (!r && !s) {
        dp[d] = min(dp[d], 1 + get(d & ~p));
        return;
    }
    for (int i = n - 1; i >= 0; i--) if (r & 1 << i) {
        solve(p | 1 << i, r & adj[i], s & adj[i], d);
        r &= ~(1 << i);
        s |= 1 << i;
    }
}

int main() {
    scanf("%d%d", &n, &m);
    fill(dp, dp + 262144, 2e9);
    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--, b--;
        adj[a] |= 1 << b;
        adj[b] |= 1 << a;
    }
    solve(0, (1 << n) - 1, 0, (1 << n) - 1);
    printf("%d", dp[(1 << n) - 1]);
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using vi = vector<int>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1e9+10;
constexpr long long LINF = 1e18+10;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

struct edge{
    int from;
    int to;
    int c;
};

int main(){
    int n, m; cin >> n >> m;
    vector<vector<edge>> G(n+10);
    rep(i, m){
        int u, v, c; cin >> u >> v >> c;
        edge e1{u,v,c};
        edge e2{v,u,c};
        G[u].push_back(e1);
        G[v].push_back(e2);
    }
    //始点＝頂点0としてBFS
    vector<int> ans(n+10, INF);//頂点に振る番号
    queue<pii> que; //頂点番号,ラベル
    que.push({1, INF});//-1
    while(!que.empty()){
        auto p = que.front(); que.pop();
        int v = p.first, c = p.second; //一致なら正、不一致なら負
        //cout << "v:" << v << " c:" << c << ln;
        if(ans[v]!=INF)continue;
        if(c==INF)ans[v] = 1;
        else{
            if(c>0){
                ans[v] = (c == 1 ? 2 : 1);
            }else{
                c = -c;
                ans[v] = c;
            }
        }
        for(auto e: G[v]){
            if(ans[e.to]!=INF)continue;
            int nc = e.c;
            if(ans[v]!=nc) nc = -e.c;
            que.push({e.to, nc});
        }
    }

    for(int i=1; i<=n; i++){
        cout << ans[i] << ln;
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin, n;
    auto a = make_v(2, n / 2, 0LL);
    auto b = make_v(2, n / 2, 0LL);
    ll score = 0;
    REP(i, 0, n) {
        cin, a[i % 2][i / 2];
    }
    REP(i, 0, n) {
        cin, b[i % 2][i / 2];
        score += b[i % 2][i / 2];
    }

    auto idx = make_v(2, n / 2, 0);

    REP(t, 0, 2) {
        iota(idx[t].begin(), idx[t].end(), 0);
        sort(idx[t].begin(), idx[t].end(), [&](int i, int j) {
            return a[t][i] - b[t][i] > a[t][j] - b[t][j];
        });
    }
    ll ans = score;
    REP(i, 0, n / 2 + 1) {
        dump(ans, score);
        chmax(ans, score);
        if (i < n / 2) {
            REP(t, 0, 2) {
                score -= b[t][idx[t][i]];
                score += a[t][idx[t][i]];
            }
        }
    }
    print(ans);

    return 0;
}


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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

int main2() {
  ll S = nextLong();
  ll P = nextLong();

  for (ll a = 1; a * a <= P; a++) {
    if (P % a == 0) {
      ll b = P / a;
      if (a + b == S) {
        return 1;
      }
    }
  }
  return 0;
}

int main() {

  bool ans = main2();
  cout << (ans ? "Yes":"No") << endl;

  return 0;
}

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


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

typedef long long ll;
const ll MAXN = 3e5;
ll n, a[MAXN];
ll solve() {
    sort(a, a+n);
    vector<ll> grps;
    ll last = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] != a[last]) {
            grps.push_back(i-last);
            last = i;
        }
    }
    grps.push_back(n-last);
    ll tot = accumulate(grps.begin(), grps.end(), 0LL);
    for (ll i: grps) {
        ans += i * (tot-i);
        tot -= i;
    }
    return ans;
}

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

    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    cout << solve() << '\n';

    return 0;
}

#include<bits/stdc++.h>
#define ll long long int
#define rep(i,a,b) for (int i = a; i <b; i++)
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;

int main()
{ll n;cin>>n;ll a[n],b[n];
	ll sum=0,i;
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
    	for( i=0;i<n;i++)
	{
		cin>>b[i];
	}
	for(i=0;i<n;i++)
	{
		sum+=a[i]*b[i];
	}
	cout<<((sum==0)?"Yes":"No");
}


#include<map>
#include<ctime>
#include<cstdio>
#include<cctype>
#define ll long long
using namespace std;
ll read() {
	char c;
	ll x=0,f=1;
	while(!isdigit(c=getchar()))
		f-=2*(c=='-');
	while (isdigit(c)){
		x=x*10+(c-48)*f;
		c=getchar();
	}
	return x;
}
ll x,y;
map<ll,ll>f;
ll min(ll x,ll y){
	return x<y?x:y;
}
ll dp(ll y){
	if(f.count(y))
		return f[y];
	if(y<=x)
		return f[y]=x-y;
	ll res=y-x;
	if(y&1){
		res=min(res,dp(y>>1)+2);
		res=min(res,dp((y+1)>>1)+2);
	}
	else
		res=min(res,dp(y>>1)+1);
	return f[y]=res;
}
int main() {
	#ifndef ONLINE_JUDGE
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
	#endif
	clock_t t1=clock();
	//--------
	x=read();
	y=read();
	dp(y);
	printf("%lld",f[y]);
	//--------
	clock_t t2=clock();
	fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC);
	return 0;
}

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef vector<vector<bool> > vvb;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = 1; i <= int(n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = int(n)-1; i >= 0; --i)

#define MES(a) MES2 a
#define MES2(a0,a1,a2,a3,a4,x,...) x
#define mes_1(x1) cout<<x1<<endl
#define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl
#define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl
#define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl
#define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl
#define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__)

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)

#define ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

ll modpow(ll x, ll y) {
    if(y == 1) {
        return x;
    }
    ll ans;
    if(y % 2 == 1) {
        ll r = modpow(x,(y-1)/2);
        ans = r * r % MOD;
        ans = ans * x % MOD;
    }
    else {
        ll r = modpow(x,y/2);
        ans = r * r % MOD;
    }
    return ans;
}

ll modncr(ll N, ll K) {
    ll res = 1;
    ll p=1;
    for (ll n = 0; n < K; ++n) {
      res = (res*(N - n))%MOD;
      p = (p*(n + 1))%MOD;
    }
    return (res*modpow(p,MOD-2))%MOD;
}

ll x, y;
map <ll, ll> memo;

ll rec(ll y) {
  	if (y == 1) return abs(x-y);
  	if (memo.find(y) != memo.end()) return memo[y];
  	if (y%2 == 0) {
    	memo[y] = min(rec(y/2)+1, abs(y-x));
  	} else {
    	memo[y] = min({rec(y+1)+1, rec(y-1)+1, abs(y-x)});
  	}
	return memo[y];
}

signed main() {
  	ll y;
  	cin >> x >> y;
  	mes(rec(y));

}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
const ll INF = 1LL << 60;
const double pi = acos(-1);
ll mod = 1000000007;

struct Edge {
    int to;
    long long w;
    Edge(int to, long long w) : to(to), w(w) {}
};

struct dsu {
    vector<int> d;
    dsu(int n = 0): d(n,-1) {}
    int findRoot(int x) {
        if(d[x] < 0) return x;
        return d[x] = findRoot(d[x]);
    }
    bool unite(int x,int y) {
        x = findRoot(x); y = findRoot(y);
        if(x == y) return false;
        if(d[x] > d[y]) swap(x,y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }
    bool same(int x,int y) { return findRoot(x) == findRoot(y);}
    int size(int x) {return -d[findRoot(x)];}
};

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

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

template <typename T>
T my_lower_bound(vector<T> a, T lb, T ub, T k) {
    while(ub - lb > 1) {
        T mid = (lb + ub) / 2;
        if(a[mid] >= k) {
            ub = mid;
        }else{
            lb = mid;
        }
    }
    return ub;
}

set<int> group;
vector<vector<int>> g;
vector<int> c, ans;
vector<bool> ch;

void dfs(int i) {
    for(auto t : g[i]) {
        if(ch[t]) continue;
        auto x = group.find(c[t]);
        if(x == group.end()) {
            group.insert(c[t]);
            ans.push_back(t);
            ch[t] = true;
            dfs(t);
            group.erase(c[t]);
            ch[t] = false;
        }else{
            ch[t] = true;
            dfs(t);
            ch[t] = false;
        }
    }
}

int main() {
    int n; cin >> n;
    c.resize(n + 1);
    ch.resize(n + 1, false);
    rep1(i,n) cin >> c[i];
    g.assign(n + 1, vector<int>());
    rep1(i,n - 1) {
        int a, b; cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    group.insert(c[1]);
    ans.push_back(1);
    ch[1] = true;
    dfs(1);
    sort(ans.begin(),ans.end());
    for(auto t : ans) {
        cout << t << endl;
    }
    return 0;
}

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
vector<int> good;
map<int,vector<int>> adj;
map<int,int> color;
map<int,int> mark;
void dfs(int node,vector<bool> &visited){
    visited[node]=1;
    if(mark.count(color[node])==0){
        mark[color[node]]=1;
    }else mark[color[node]]++;
    for(int i:adj[node]){
        if(!visited[i]){
            dfs(i,visited);
        }
    }
    if(mark[color[node]]==1){
        good.push_back(node);
    }
    mark[color[node]]--;
}

int main(){
    int vertex;
    cin>>vertex;
    int x,y;
    for(int i=0;i<vertex;i++){
        cin>>x;
        color[i+1]=x;
    }
    for(int i=0;i<vertex-1;i++){
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    vector<bool> visited(vertex+1);
    dfs(1,visited);
    sort(good.begin(),good.end());
    for(int i:good){
        cout<<i<<"\n";
    }
    return 0;
}


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

const int N = 55;

int n;
int primes[N] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}, book[N];
ll ans = 1e30;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a;}

void print(__int128 x)  {
    if(!x)
    {
        puts("0");
        return ;
    }
    string ret="";
    while(x)
    {
        ret+=x%10+'0';
        x/=10;
    }
    reverse(ret.begin(),ret.end());
    cout<<ret<<endl;
}

bool check(ll x){
	for(int i = 1; i <= n; ++ i)
		if(gcd(x, book[i]) == 1)	return false;
	return true;
}

int main(){
	cin >> n;
	for(int i = 1; i <= n; ++ i)	cin >> book[i];
	for(int i = 1; i < (1 << 15); ++ i){
		ll tmp = 1;
		for(int j = 0; j < 15; ++ j){
			if((i >> j) & 1)	tmp *= primes[j];
		}
		//printf("ans = %d\n", ans);
		if(check(tmp)){
			ans = min(ans, tmp);
		}
	}
	print(ans);
	return 0;
}

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

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

string S;
char mem[1000020];
const int base = 500010;

signed main()
{
    cin >> S;
    // int l = base, r = base + 1;
    // bool flip = false;
    // for(auto e : S)
    // {
    //     if(e == 'R')flip = !flip;
    //     else
    //     {
    //         if(flip)mem[l--] = e;
    //         else mem[r++] = e;
    //     }
    // }

    // string s;
    // if(flip)for(int i = r - 1; i > l; i--)s.push_back(mem[i]);
    // else for(int i = l + 1; i < r; i++)s.push_back(mem[i]);

    // cout << s << endl;

    deque<char> s;
    bool flip = false;
    for(auto e : S)
    {
        if(e == 'R')flip = !flip;
        else
        {
            if(flip)s.push_front(e);
            else s.push_back(e);
        }
    }
    if(flip)reverse(s.begin(), s.end());

    string ans;
    for(int i = 0; i < s.size(); i++)
    {
        if(ans.size() == 0 || ans.back() != s[i])ans.push_back(s[i]);
        else ans.pop_back();
    }
    cout << ans << endl;

    return 0;
}


//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#define int long long
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())

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

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

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

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

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

template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
int par[2205],ran[2205];
void init(){ for(int i=0;i<2205;i++) par[i] = i; }
int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); }
void unite(int x,int y){
	x = find(x); y = find(y); if(x==y) return;
	if(ran[x] < ran[y]) par[x] = y;
	else{
		par[y] = x;
		if(ran[x] == ran[y]) ran[x]++;
	}
}
bool same(int x,int y){ return find(x)==find(y); }
int n, m;
string str[1005];

void solve(){
	init();
	cin >> n >> m;
	rep(i, n) cin >> str[i];
	unite(0, n); unite(0, n+m-1);
	unite(n-1, n); unite(n-1, n+m-1);
	rep(i, n){
		rep(j, m){
			if(str[i][j] == '#'){
				unite(i, j + n);
			}
		}
	}
	set<int>x, y;
	rep(i, n) x.insert(find(i));
	rep(i, m) y.insert(find(n + i));

	cout << min(si(x), si(y)) - 1 << endl;
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}

#include <stdio.h>
#include <set>
#define MN 1000

int h,w,fa[MN*2+5];
char s[MN+5][MN+5];

int r(int x){
	return x;
}

int c(int x){
	return x+h;
}

int gf(int u){
	return fa[u]==u?u:fa[u]=gf(fa[u]);
}

void unite(int u,int v){
	u = gf(u);
	v = gf(v);
	if(u==v) return;
	fa[v] = u;
}

int main(){
	scanf("%d%d",&h,&w);
	for(int i=1;i<=h+w;i++)
		fa[i] = i;
	for(int i=1;i<=h;i++)
		scanf("%s",&s[i][1]);
	unite(r(1),r(h));
	unite(c(1),c(w));
	unite(r(1),c(1));
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			if(s[i][j]=='#')
				unite(r(i),c(j));
	int ans = 0x3fffffff;
	{
		std::set<int> s;
		for(int i=1;i<=h;i++)
			s.insert(gf(r(i)));
		ans = std::min(ans,(int)s.size()-1);
	}
	{
		std::set<int> s;
		for(int i=1;i<=w;i++)
			s.insert(gf(c(i)));
		ans = std::min(ans,(int)s.size()-1);
	}
	printf("%d\n",ans);
}


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

int main()
{
    double sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;
    double ans = (sy*gx + sx*gy)/(sy+gy);
    cout << fixed << setprecision(10);
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,n,m,x1,x2,x3,x4,k,k1;
    cin>>n>>m>>a>>b;
    x1=n-a;
    x2=n-b;
    x3=m-a;
    x4=m-b;
    k=max(x1,x2);
    k1=max(x3,x4);
    cout<<max(k,k1)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2000;

vector<vector<int>>G;
bool temp[MAX_N];
void dfs(int v) {
    if (temp[v])return;
    temp[v] = true;
    for (auto vv : G[v])dfs(vv);
}

int main() {
    int N, M;
    cin >> N >> M;
    G.resize(N);
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        G[a - 1].push_back(b - 1);
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)temp[j] = false;
        dfs(i);
        for (int j = 0; j < N; j++)if (temp[j])ans++;
    }

    cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int mod=1e9+7;
int gcd(int x,int y)
{
    if(y==0)
    {
        return x;
    }
    return gcd(y,x%y);
}
int power(int a,int n)
{
    int res=1;
    while(n)
    {
        if(n%2)
        {
            res=(res*a)%mod;
        }
        n=n/2;
        a=(a*a)%mod;
    }
    return res;
}
vector<vector<int>> adj;
vector<bool> vis;
void dfs(int v)
{
    vis[v]=true;
    int i,n=adj[v].size();
    for(i=0;i<n;i++)
    {
        if(!vis[adj[v][i]])
        {
            dfs(adj[v][i]);
        }
    }
}
void solve()
{
    int i,n,m,x,y,cnt=0;
    cin>>n>>m;
    adj.resize(n+1);
    for(i=0;i<m;i++)
    {
        cin>>x>>y;
        adj[x].push_back(y);
    }
    for(i=1;i<=n;i++)
    {
        vis.assign(n,false);
        dfs(i);
        for(int j=1;j<=n;j++)
        {
            cnt=cnt+vis[j];
        }
    }
    cout<<cnt<<"\n";
}
int32_t main()
{
    int t=1;
    while(t--)
    {
        solve();
    }
    return 0;
}


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

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

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

  unordered_map<int64_t, int64_t> freq_altsum;
  freq_altsum[0]++;
  int64_t altsum= 0;
  for(int i=0; i<N; i++){
    altsum= (i%2==0) ?  data.at(i)+ altsum
                     : -data.at(i)+ altsum;
    freq_altsum[altsum]++;
  }

  // for(const auto& [val,freq]:freq_altsum){
  //   cout << val <<": "<< freq << endl;
  // }

  int64_t ans= 0;
  for(const auto& [val,freq]:freq_altsum){
    ans+= freq* (freq- 1)/ 2;
  }

  cout << ans << endl;
}

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

typedef long long ll;
#define int long long
#define fs first
#define sc second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define MP make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef FlameDragon
    freopen("in.txt", "r", stdin);
#endif
    int n;
    cin >> n;
    vector<int> a(n + n);
    vector<pair<int, int>> b(n + n);
    for (int i = 0; i < n + n; i++) {
        cin >> a[i];
        b[i] = {a[i], i};
    }
    vector<int> c(n + n);
    sort(all(b));
    for (int i = 0; i < n; i++) {
        c[b[i].second] = true;
    }
    string ans(n + n, '(');
    vector<pair<int, char>> st;
    for (int i = 0; i < n + n; i++) {
        if (!sz(st)) {
            st.push_back({c[i], '('});
        } else if (st.back().first == c[i]) {
            ans[i] = st.back().second;
            st.push_back(st.back());
        } else {
            ans[i] = st.back().second ^ '(' ^ ')';
            st.pop_back();
        }
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i64 i=0; i<n; i++)
#define all(v) v.begin(),v.end()
#define pp pair<int,int>
using Graph=vector<vector<int>>;
using i64=int64_t;

int main() {
    int n,m;cin>>n>>m;
    rep(i,m) {
        string s;
        cin>>s;
    }
    rep(i,20) {
        rep(j,20) {
            cout << "H";
        }
        cout << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define rep(i,n) for(ll i=0; i<(n);++i)
#define pb push_back
template<typename T> int max_index(vector<T>& v){auto mi = max_element(v.begin(), v.end()); return distance(v.begin(), mi);}
template<typename T> ll max_value(vector<T>& v){auto mi = max_element(v.begin(),v.end()); return (*mi);}

int main()
{
    int n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];

    ll ans = 0;
    ll amax = 0;
    for(int i = 0; i < n; i++){
        amax = max(amax, a[i]);
        if(ans < amax*b[i])
            ans = amax*b[i];
        cout << ans << endl;
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cmath>
#include <climits>
#include <queue>
#include <bitset>
using namespace std;
typedef vector<vector<long> > dim2veclo;
typedef vector<vector<int> > dim2vecin;
typedef vector<int> vecin;
typedef vector<long> veclo;

vecin takahashi = vecin(10, 0);
vecin aoki = vecin(10, 0);
vector<double> maisuu;
long tensuuT = 0, tensuuA = 0;

int main() {
    cout << fixed << setprecision(15);
    int k;
    double kakuritu = 0;
    cin >> k;
    string s, t;
    cin >> s >> t;
    maisuu = vector<double>(10, k);
    for (int i = 0; i < 4; i++) {
        int ss = s[i] - '0';
        int tt = t[i] - '0';
        takahashi[ss]++;
        aoki[tt]++;
        maisuu[ss]--;
        maisuu[tt]--;
    }
    for (int i = 1; i <= 9; i++) {
        tensuuT += (i * pow(10, takahashi[i]));
        tensuuA += (i * pow(10, aoki[i]));
    }
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            if (i == j) {
                if (maisuu[i] <= 1) continue;
            }
            else {
                if (maisuu[i] == 0) continue;
                if (maisuu[j] == 0) continue;
            }
            long T = tensuuT + i * 9 * pow(10, takahashi[i]);
            long A = tensuuA + j * 9 * pow(10, aoki[j]);
            if (T > A) {
                double bunbo = 9 * k - 8;
                double bunsi1 = maisuu[i];
                double bunsi2;
                if (i == j) bunsi2 = maisuu[i] - 1;
                else bunsi2 = maisuu[j];
                kakuritu += (bunsi1 / bunbo) * (bunsi2 / (bunbo - 1));
            }
        }
    }
    cout << kakuritu;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) {
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) {
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

ll score(map<ll,ll> m) {
    ll ret = 0;
    for(auto i: m) ret += i.x*pow(10,i.y);
    return ret;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    ll k; string s,t; cin >> k >> s >> t;
    vll v(10,k); v[0]=0;
    map<ll,ll> A,B; FOR(i,1,10) { A[i]=0; B[i]=0; }
    F(4) { v[s[i]-'0']--; A[s[i]-'0']++; }
    F(4) { v[t[i]-'0']--; B[t[i]-'0']++; }
    ll cnt = 0; FOR(i,1,10) cnt+=v[i];
    double ret = 0;
    FOR(a,1,10) FOR(b,1,10) {
        v[a]--; v[b]--;
        bool nok = v[a]<0 || v[b]<0;
        v[a]++; v[b]++;
        if(!nok) {
            A[a]++; B[b]++;
            if(score(A) > score(B)) {
//                 dbg(v[a],v[b],cnt);
                double cur = double(v[a])/(cnt) * (v[b]-(a==b))/(cnt-1);
                ret+=cur;
//                 dbg(a,b,cur, score(A), score(B));
            }
            A[a]--; B[b]--;
        }
    }
    cout << fixed << setprecision(8) << ret << endl;
    return 0;
}