/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;
typedef pair<int, int> P;

class E {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int R, C;
        cin >> R >> C;
        vector<vector<int>> A(R, vector<int>(C - 1));
        vector<vector<int>> B(R - 1, vector<int>(C));
        REP(i, R) REP(j, C - 1) cin >> A[i][j];
        REP(i, R - 1) REP(j, C) cin >> B[i][j];

        vector<int> dist(R * C * 2, INF);
        dist[0] = 0;
        priority_queue<P, vector<P>, greater<>> pq;
        pq.push({0, 0});
        while (!pq.empty()) {
            int cost, v;
            tie(cost, v) = pq.top();
            pq.pop();
            //cout << v << ":" << cost << endl;
            int x = v % R;
            int y = v / R;
            if (v >= R * C) {
                if (x - 1 >= 0) {
                    int nv = R * y + x - 1;
                    if (cost + 1 < dist[nv]) {
                        dist[nv] = cost;
                        pq.push({cost + 1, nv});
                    }
                }
                int nv = v - R * C;
                if (cost < dist[nv]) {
                    dist[nv] = cost;
                    pq.push({cost, nv});
                }
            } else {
                int nv = v + R * C;
                if (cost + 1 < dist[nv]) {
                    dist[nv] = cost + 1;
                    pq.push({cost + 1, nv});
                }
                if (y + 1 <= C - 1) {
                    nv = R * (y + 1) + x;
                    if (cost + A[x][y] < dist[nv]) {
                        dist[nv] = cost + A[x][y];
                        pq.push({cost + A[x][y], nv});
                    }
                }
                if (y - 1 >= 0) {
                    nv = R * (y - 1) + x;
                    if (cost + A[x][y - 1] < dist[nv]) {
                        dist[nv] = cost + A[x][y - 1];
                        pq.push({cost + A[x][y - 1], nv});
                    }
                }
                if (x + 1 <= R - 1) {
                    nv = R * y + x + 1;
                    if (cost + B[x][y] < dist[nv]) {
                        dist[nv] = cost + B[x][y];
                        pq.push({cost + B[x][y], nv});
                    }
                }
            }
        }
        cout << dist[R * C - 1] << endl;
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    E solver;
    solver.solve(in, out);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll A, B, C;
    cin >> A >> B >> C;

    ll s = 0;
    if(C % 2 == 1) {
        if(A < B) s = -1;
        else if(A > B) s = 1;
        else s = 0;
    }
    else {
        if(A*A < B*B) s = -1;
        else if(A*A > B*B) s = 1;
        else s = 0;
    }
    if(s > 0) PR(">");
    else if(s < 0) PR("<");
    else PR("=");

    return 0;
}

/*

*/


#include<iostream>
#include<string>
using namespace std;
int main(){
  int n;
  cin >> n;
  string s,t;
  cin >> s;
  cin >> t;
  int zero=0,anszero=0;
  int iti[n],ansiti[n];
  int count=0,count2=0;
  for(int i=0;i<n;i++){
    if(s[i]=='0'){
      zero++;
      iti[count]=i;
      count++;
    }
    if(t[i]=='0'){
      anszero++;
      ansiti[count2]=i;
      count2++;
    }
  }
  if(zero!=anszero){
    cout << "-1" << endl;
    exit(0);
  }
  else{
    int count3=0;
    for(int k=0;k<count;k++) if(iti[k]!=ansiti[k]){
      count3++;
    }
    cout << count3 << endl;
    return 0;
  }
}

#include "bits/stdc++.h"
#include <random>
#include <chrono>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;++i)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;--i)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define endk '\n'
using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef long double ld; typedef pair<lint, lint> plint; typedef pair<ld, ld> pld;
struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
template<class T> auto add = [](T a, T b) -> T { return a + b; };
template<class T> auto f_max = [](T a, T b) -> T { return max(a, b); };
template<class T> auto f_min = [](T a, T b) -> T { return min(a, b); };
template<class T> using V = vector<T>;
using Vl = V<lint>; using VVl = V<Vl>;
template< typename T > ostream& operator<<(ostream& os, const vector< T >& v) {
	for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : "");
	return os;
}
template< typename T >istream& operator>>(istream& is, vector< T >& v) {
	for (T& in : v) is >> in;
	return is;
}
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template <class T>
T div_floor(T a, T b) {
	if (b < 0) a *= -1, b *= -1;
	return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
	if (b < 0) a *= -1, b *= -1;
	return a > 0 ? (a - 1) / b + 1 : a / b;
}
template <class F> struct rec {
	F f;
	rec(F&& f_) : f(std::forward<F>(f_)) {}
	template <class... Args> auto operator()(Args &&... args) const {
		return f(*this, std::forward<Args>(args)...);
	}
};
lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); }
lint digit(lint a) { return (lint)log10(a); }
lint e_dist(plint a, plint b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); }
lint m_dist(plint a, plint b) { return abs(a.first - b.first) + abs(a.second - b.second); }
bool check_overflow(lint a, lint b, lint limit) { if (b == 0) return false; return a > limit / b; } // a * b > c => true
void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); }
const lint MOD1000000007 = 1000000007, MOD998244353 = 998244353, INF = 1e18;
lint dx[8] = { 0, -1, 0, 1, 1, -1, 1, -1 }, dy[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
bool YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; return flag; }
struct Edge {
	lint from, to;
	lint cost, color;
	Edge() {

	}
	Edge(lint u, lint v, lint c, lint col) {
		cost = c;
		color = col;
		from = u;
		to = v;
	}
	bool operator<(const Edge& e) const {
		return cost < e.cost;
	}
};
struct WeightedEdge {
	lint to;
	lint cost;
	WeightedEdge(lint v, lint c) {
		to = v;
		cost = c;
	}
	bool operator<(const WeightedEdge& e) const {
		return cost < e.cost;
	}
};
using WeightedGraph = V<V<WeightedEdge>>;
typedef pair<plint, lint> tlint;
typedef pair<plint, plint> qlint;
typedef pair<lint, string> valstr;
typedef pair<ld, plint> _pld;
typedef pair<ld, lint> pidx;

int main() {
	lint N;
	cin >> N;
	string S, T;
	cin >> S >> T;
	VVl pos(2);
	REP(i, N) {
		if (S[i] == '0') pos[0].push_back(i);
		if (T[i] == '0') pos[1].push_back(i);
	}
	lint ans = SZ(pos[0]);
	if (SZ(pos[0]) != SZ(pos[1])) {
		cout << -1 << endk;
	}
	else {
		REP(i, SZ(pos[0])) {
			if (pos[0][i] == pos[1][i]) ans--;
		}
		cout << ans << endk;
	}
}


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

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

    vector<double> A(N);
    for (double &a : A)
    {
        cin >> a;
    }

    double l = 0.0;
    double r = 2000000000.0;
    auto f = [&](double c) -> double
    {
        double ret = 0.0;
        for (double a : A)
        {
            ret += c + a - min(a, 2 * c);
        }
        return ret;
    };
    for (size_t i = 0; i < 5000; i++)
    {
        double c1 = (l * 2.0 + r) / 3.0;
        double c2 = (l + r * 2.0) / 3.0;

        if (f(c1) < f(c2))
        {
            r = c2;
        }
        else
        {
            l = c1;
        }
    }
    cout << fixed << setprecision(12) << f(l) / double(N) << endl;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 2010;

int n, a[N];
map <int, vector <int>> cand;

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j * j <= a[i]; ++j) {
      if (a[i] % j) continue;
      cand[j].emplace_back(i);
      if (j * j < a[i]) cand[a[i] / j].emplace_back(i);
    }
  }
  int ans = 0;
  for (auto [key, val] : cand) {
    if (key > a[1]) break;
    int cur = 0;
    for (int i : val) cur = __gcd(cur, a[i]);
    if (cur != key) continue; ++ans;
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<ll, pi> ipi;
typedef pair<pi, pi> pipi;
#define mp make_pair
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define size(v) (ll) v.size()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define memset(x, v) memset(x, v, sizeof x)
#define INF (ll) (1e9 + 100)
#define LLINF (ll) 1e18
#define mod (const ll) (1e9 + 7)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusive

ll n, a, b, c, ans = LLINF;

int main() {
	fastio; cin >> n;
	FOR(i, 1, n) {
		cin >> a >> b >> c;
		if (c > a) ans = min(ans, b);
	}
	cout << (ans == LLINF ? -1 : ans);
}

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <cstdio>
#include <stack>
#include <cstring>

//#define FILE_INPUT

using namespace std;

#define inc(i, n) for (int i = 0; i < (n); i++)
#define incs(i, st, n) for (int i = (st); i < (n); i++)
using ll = long long;

constexpr int LIMIT = 0x3f3f3f3f;

template <typename T>
ll inv(T bg, T nd) {
    if (nd - bg <= 1) return 0;

    T md = bg + (nd - bg) / 2;
    ll ret = inv(bg, md) + inv(md, nd);

    auto i = bg, j = md;
    while (i != md) {
        while (j != nd && *i > *j) {
            j++;
        }
        ret += j - md;
        i++;
    }

    inplace_merge(bg, md, nd);
    return ret;
}

int main(int argc, char** argv){
#ifdef FILE_INPUT
    freopen("input", "r", stdin);
#endif

    int N;
    scanf("%d", &N);

    vector<int> a(N, 0);
    inc(i, N) {
        scanf("%d", &a[i]);
    }

    // calc inv number
    vector<int> b(a.begin(), a.end());
    ll inv_num = inv(b.begin(), b.end());
    printf("%lld\n", inv_num);

    inc(i, N-1) {
        inv_num = inv_num + N - a[i]*2 -1LL;
        printf("%lld\n", inv_num);
    }

#ifdef FILE_INPUT
    fclose(stdin);
#endif
  	return 0;
}


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

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> A(N + 1);
  for (int i = 0; i <= N; i++) cin >> A.at(i);
  int k = 11000;
  for (int i = 0; i < N; i++) k = min(k, abs(A.at(i + 1) - A.at(i)));
  cout << k << endl;
  vector<vector<int>> B(k, vector<int>(N + 1));
  for (int i = 0; i < k; i++) {
    for (int j = 0; j <= N; j++) {
      B.at(i).at(j) = A.at(j) / k;
      if (i < A.at(j) % k) B.at(i).at(j)++;
      cout << B.at(i).at(j);
      if (j == N) cout << endl;
      else cout << " ";
    }
  }
}

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

const int N = 1005;
const ll P = 998244353LL;

int n, m, ufs[N << 1];
bool vis[N << 1];
char s[N][N];

inline int id(int x, int y) {
    return (x - 1) * m + y;
}

int find(int x) {
    return ufs[x] == x ? x : ufs[x] = find(ufs[x]);
}

inline void merge(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx == fy) return;
    ufs[fx] = fy;
}

template <typename T>
inline void read(T &X) {
    char ch = 0; T op = 1;
    for (X = 0; ch > '9' || ch < '0'; ch = getchar())
        if (ch == '-') op = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X * 10) + ch - '0';
    X *= op;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("sample.in", "r", stdin);
    clock_t st_clock = clock();
#endif

    read(n), read(m);
    for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
    for (int i = 1; i <= n + m; i++) ufs[i] = i;
    s[1][1] = s[1][m] = s[n][1] = s[n][m] = '#';
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (s[i][j] == '#') merge(i, j + n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        int now = find(i);
        if (vis[now]) continue;
        vis[now] = 1;
        ++ans;
    }
    int tmp = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = n + 1; i <= n + m; i++) {
        int now = find(i);
        if (vis[now]) continue;
        vis[now] = 1;
        ++tmp;
    }
    ans = min(ans, tmp);
    --ans;
    printf("%d\n", ans);

#ifndef ONLINE_JUDGE
    clock_t ed_clock = clock();
    printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
#endif
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define endl "\n"
#define vll vector<ll>
#define pll pair<ll,ll>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll,vll,greater<ll> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)             (int)((x).size())
#define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL)
#define fo(i,s,n) for(ll i=s;i<n;i++)
#define rfo(i,n,s) for(ll i=n;i>=s;i--)
using namespace std;
typedef long long int ll;
//------------------------------MOD-Operations--------------------------------------------------------------------------------------------------------------------------------------------//
const ll M = 1e9 + 7;
const ll N = 2e5 + 5;
const ll inf = 9e18;
ll mod(ll x){   return (x%M);}
ll ml(ll a,ll b){  return mod(mod(a)*mod(b));}
ll power(ll a,ll n){   if(n==0) return 1;   else if(n==1) return a;   ll R=power(a,n/2)%M;    if(n%2==0) {    return mod(ml(R,R));  }   else {   return mod(ml(ml(R,a),mod(R)));  }  }
ll mod_inv(ll n){  return power(n,M-2);}
// only if M is prime
ll mod_div(ll a,ll b){   return ml(a,mod_inv(b));   }
ll C(ll n,  ll r)
{   if(r > n || n < 0 || r < 0)return 0;
 if (r == 0)
        return 1;
   ll nfc=1,rfc=1,nrfc=1;
    for (int i = 1; i <= n; i++)
      {
           nfc=ml(nfc,i);
           if(i==r){rfc=nfc;}
           if(i==(n-r)){nrfc=nfc;}
     }
    return ml(ml(nfc,mod_inv(rfc)),mod_inv(nrfc));
}
ll ncr(ll n, ll r)
{
    long long p = 1, k = 1;
    if (n - r < r)
        r = n - r;
   if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
  long long m = __gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else
        p = 1;
return p;
}
vector<ll> seive(ll n){
    vector<ll> isprime(n+1,true);
    isprime[0]=false;
    isprime[1]=false;//kyuki 1 prime nhi hota
    for( ll i=2;i<sqrt(n);i++){
        if(isprime[i]){
            for(int j=i*i;j<=n;j+=i){
                isprime[j]=false;
            }
        }
    }
    vll allp;
    fo(i,2,n+1)if(isprime[i])allp.pb(i);
return allp;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
vll vis(N);
vector<vll> adj(N);
void solve() {

}
int main(){
    FAST;
ll h,w; cin>>h>>w;
vector<string> v(h);
fo(i,0,h)cin>>v[i];
ll ans=0;
fo(i,1,h-1){
    fo(j,1,w-1){
        if(v[i][j]=='.'){
            if(v[i-1][j-1]=='#' && v[i-1][j]=='#' && v[i][j-1]=='#')ans++;
             if(v[i+1][j+1]=='#' && v[i+1][j]=='#' && v[i][j+1]=='#')ans++;
              if(v[i-1][j+1]=='#' && v[i][j+1]=='#' && v[i-1][j]=='#')ans++;
               if(v[i][j-1]=='#' && v[i+1][j]=='#' && v[i+1][j-1]=='#')ans++;

        }
        else {
             if(v[i-1][j-1]=='.' && v[i-1][j]=='.' && v[i][j-1]=='.')ans++;
             if(v[i+1][j+1]=='.' && v[i+1][j]=='.' && v[i][j+1]=='.')ans++;
              if(v[i-1][j+1]=='.' && v[i][j+1]=='.' && v[i-1][j]=='.')ans++;
               if(v[i][j-1]=='.' && v[i+1][j]=='.' && v[i+1][j-1]=='.')ans++;

        }
    }
}
cout<<ans<<endl;

}

/*********************************************************************************\
*  _________  _      _  ________  _       _       __       ________               *
* |___   ___|| |    | ||  ______|| |     | |     /  \     |  ____  |              *
*     | |    | |    | || |______ | |_____| |    / /\ \    | |____| |              *
*     | |    | |    | ||______  ||  _____  |   / /__\ \   |  __  __|              *
*     | |    | |____| | ______| || |     | |  / ______ \  | |  \ \                *
*     |_|    |________||________||_|     |_| /_/      \_\ |_|   \_\               *
*                                                                                *
*   Department of Computer Science & Engineering                                  *
*   Student ID : 18CSE035                                                         *
*   Bangabnadhu Sheikh Mujibur Rahman Science & Technology University,Gopalgonj.  *
*                                                                                *
\*********************************************************************************/

//Now Write to Code ___________________________

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

typedef    long long             ll;
typedef    long double           ld;
typedef    unsigned long long    ull;
typedef    pair<int,int>         pii;
typedef    pair<ll,ll>           pll;
typedef    vector<int>           vi;
typedef    vector<ll>            vll;
typedef    vector<vector<int>>   vvi;

inline     int         Int(){int x; cin >> x; return x;}
inline     ll          Long(){ll x; cin >> x; return x;}
inline     float       Float(){float x; cin >> x; return x;}
inline     double      Double(){double x; cin >> x; return x;}
inline     void        Yes(){cout << "Yes" << endl;}
inline     void        No(){cout << "No" << endl;}
inline     void        YES(){cout << "YES" << endl;}
inline     void        NO(){cout << "NO" << endl;}

const      int N       =(int)2e5 + 5;
const      int maxN    =(int)1e6 + 6;
const      ll  Mod     =(ll)1e9 + 7;
const      int inf     =(int)2e9;
const      ll  Inf     =(ll)1e18;

#define    fastio      ios_base::sync_with_stdio(false),cin.tie(NULL)
#define    T           int t,q; cin >> t; for(q=1;q<=t;q++)
#define    Forn(i,n)   for(int i=0;i<n;i++)
#define    ForN(i,n)   int i;for(i=n-1;n>=0;i--)
#define    forn(i,n)   for(int i=1;i<=n;i++)
#define    forN(i,n)   int i;for(i=n;n>=1;i--)
#define    fors(i,s)   for(int i=0;i<s.size();i++)
#define    Sort(s)     sort(s.begin(),s.end())
#define    debug(x)    cerr << #x << " = " << x << '\n' ;
#define    rep(i,b,e)  for(__typeof(e) i=(b); i!=(e+1)-2*(b>e); i=i+1-2*(b>e))
#define    gcd(a,b)    __gcd(a , b)
#define    lcm(a,b)    (a*(b/__gcd(a,b)))
#define    gt          greater<int>()
#define    Int         Int()
#define    Long        Long()
#define    Float       Float()
#define    Double      Double()
#define    all(x)      x.begin() , x.end()
#define    sz(x)       (int)x.size()
#define    ff          first
#define    ss          second
#define    pb          push_back
#define    eb          emplace_back
#define    mp          make_pair
#define    mem(a)      memset(a,0,sizeof a)
#define    memn(a)     memset(a,-1,sizeof a)
#define    biday       return 0
#define    nl          endl
#define    sp(n)       fixed<<setprecision(n)
#define    pi          acos(-1)
#define    PI          3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342

int main()
{
    ll h,w,i,j,x,res=0;
    cin >> h >> w;

    char s[h][w];
    for(i=0; i<h; i++)
    {
    	for(j=0; j<w; j++)
    	{
    		cin >> s[i][j];
    	}
    }
    for(i=0; i<h-1; i++)
    {
    	for(j=0; j<w-1; j++)
    	{
    		x=0;
    		if(s[i][j]=='#')x++;
    		if(s[i][j+1]=='#')x++;
    		if(s[i+1][j]=='#')x++;
    		if(s[i+1][j+1]=='#')x++;
    		if(x%2)res++;
    	}
    }
    cout << res << endl;

    biday;
}

//...............BYE BYE................


#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define DREP(i,n) for (int i = (n-1); i >=0; --i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
//debug
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
  template<typename T1, typename T2> void view(pair<T1,T2> e) { cout << "("<< e.first << ", " << e.second <<")"  << endl;}
  template<typename T1, typename T2> void view(const vector<pair<T1,T2>>& v) {for(const auto& e: v){view(e);}}
  // end debug

  int main(){
    //#define int long long
    int n, s, d;
    cin >> n >> s >> d;
    vector<int> x(n);
    vector<int> y(n);
    REP(i,n) cin >> x[i] >> y[i];

    REP(i,n){
      bool ok = true;
      if(x[i] >= s) ok = false;
      if(y[i] <= d) ok = false;
    if(ok){
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(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;}

using pint = pair<int, int>;
const int MAX = 10000;
int n;
vector<vector<int>> xyr;
bool flgs[MAX][MAX];
vector<vector<int>> res;

void paintOut(int tl_x, int tl_y, int br_x, int br_y) {
    for (int i = tl_y; i <= br_y; ++i) {
        for (int j = tl_x; j <= br_x; ++j) {
            flgs[i][j] = true;
        }
    }
}

int area(int tl_x, int tl_y, int br_x, int br_y) {
    return (br_x - tl_x + 1) * (br_y - tl_y + 1);
}

vector<int> topLeft(int sx, int sy, int r) {

    bool left_f = true, top_f = true;
    int now_row = sy, now_col = sx;

    while (left_f || top_f) {

        if (now_col == 0) left_f = false;
        if (area(now_col-1, now_row, sx, sy) > r) {
            left_f = false;
        }
        if (left_f) {
            for (int i = now_row; i <= sy; ++i) {
                if (flgs[i][now_col-1]) {
                    left_f = false;
                    break;
                }
            }
            if (left_f) now_col--;
        }

        if (now_row == 0) top_f = false;
        if (area(now_col, now_row-1, sx, sy) > r) {
            top_f = false;
        }
        if (top_f) {
            for (int i = now_col; i <= sx; ++i) {
                if (flgs[now_row-1][i]) {
                    top_f = false;
                    break;
                }
            }
            if (top_f) now_row--;
        }
    }

    bool right_f = true, bottom_f = true;
    int ty = now_row, tx = now_col;
    now_row = sy, now_col = sx;

    while (right_f || bottom_f) {

        if (now_col == MAX-1) right_f = false;
        if (area(tx, ty, now_col+1, now_row) > r) {
            right_f = false;
        }
        if (right_f) {
            for (int i = ty; i <= now_row; ++i) {
                if (flgs[i][now_col+1]) {
                    right_f = false;
                    break;
                }
            }
            if (right_f) now_col++;
        }

        if (now_row == MAX-1) bottom_f = false;
        if (area(tx, ty, now_col, now_row+1) > r) {
            bottom_f = false;
        }
        if (bottom_f) {
            for (int i = tx; i <= now_col; ++i) {
                if (flgs[now_row+1][i]) {
                    bottom_f = false;
                    break;
                }
            }
            if (bottom_f) now_row++;
        }
    }
    return {tx, ty, now_col, now_row};
}

void solve() {
    rep(i, n) flgs[xyr[i][2]][xyr[i][1]] = true;
    sort(all(xyr), [](auto& a, auto& b){return a[1] + a[2] < b[1] + b[2];});
    rep(i, n) {
        int x = xyr[i][1], y = xyr[i][2], r = xyr[i][3];
        vector<int> top_left = topLeft(x, y, r);
        paintOut(top_left[0], top_left[1], top_left[2], top_left[3]);
        res[xyr[i][0]] = {top_left[0], top_left[1], top_left[2] + 1, top_left[3] + 1};
    }

    rep(i, n) {
        rep(j, 4) {
            if (j) cout << " ";
            cout << res[i][j];
        }
        cout << endl;
    }
}

int main() {
    cin >> n;
    xyr.resize(n, vector<int>(4));
    rep(i, n) {
        xyr[i][0] = i;
        cin >> xyr[i][1] >> xyr[i][2] >> xyr[i][3];
    }
    res.resize(n, vector<int>(3));
    solve();
}


#line 1 "main_a.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

/* template start */

#line 2 "/home/imperi/cp-library/lib/utility/type_alias.hpp"

#line 4 "/home/imperi/cp-library/lib/utility/type_alias.hpp"

using u64 = std::uint64_t;
using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;

using i64 = std::int64_t;
using i32 = std::int32_t;
using i16 = std::int16_t;
using i8 = std::int8_t;

using usize = std::size_t;
using isize = std::ptrdiff_t;

i64 operator"" _i64(unsigned long long num) { return i64(num); }

u64 operator"" _u64(unsigned long long num) { return u64(num); }
#line 33 "main_a.cpp"

#define rep(i, a, b) for (i64 i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()

#ifdef LOCAL
#define debug(...)                                                    \
  std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
      debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
  std::cerr << " " << h;
  if (sizeof...(t) > 0) std::cerr << " :";
  debug_out(t...);
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}

template <typename Num,typename Func = std::multiplies<Num>>
constexpr Num mypow(Num a, u64 b, Num id = 1,Func mul =Func() ) {
  if (b == 0) return id;
  Num x = id;
  while (b > 0) {
    if (b & 1) x  = mul(x,a);
    a = mul(a,a);
    b >>= 1;
  }
  return x;
}

/* template end */

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

  i64 testcase;
  std::cin>>testcase;

  while(testcase--){
    i64 n;
    std::cin>>n;

    std::string s;
    std::cin>>s>>s>>s;

    rep(i,0,n)std::cout<<0;
    rep(i,0,n)std::cout<<1;
    std::cout<<0<<"\n";
  }

  return 0;
}

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

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

char cn[1200010];
char* ci = cn, * di = cn;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	ll tmp = *(ll*)ci;
	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;
	return tmp;
}

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

	fread(cn, 1, 1200010, stdin);
	int T = getint();

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

		rep(i, N)* di++ = '0';
		rep(i, N)* di++ = '1';
		*di++ = '0';
		*di++ = '\n';
		ci += 6 * N + 3;
	}
	fwrite(cn, 1, di - cn, stdout);

	Would you please return 0;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    if (v.size() == 0) return os;
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    os << v[v.size() - 1];
    return os;
}

void solve() {
    char s,t;
    cin >> s >> t;
    if (s == 'Y') {
        t += 'A' - 'a';
        cout << t << endl;
    } else {
        cout << t << endl;
    }
}

int main() {
    solve();
    char tmp;
    while (cin >> tmp) {
        cin.putback(tmp);
        solve();
    }
}

#include <bits/stdc++.h>

int main(){
    long long B, C;
    std::cin >> B >> C;

    if(B > 0){
        long long ans = 1;
        ans = ans + std::min(C / 2, B);
        ans = ans + std::max(C / 2 - 1, 0ll);
        ans = ans + (C >= 1);
        ans = ans + std::min((C-1) / 2, B-1);
        ans = ans + std::max((C-1) / 2, 0ll);
        std::cout << ans << std::endl;
    }else if(B < 0){
        long long ans = 1;
        ans = ans + (C >= 1);
        ans = ans + std::min((C-1) / 2, std::abs(B));
        ans = ans + std::max((C-1) / 2, 0ll);
        ans = ans + std::max(std::min(C / 2 - 1, std::abs(B)-1), 0ll);
        ans = ans + std::max(C / 2, 0ll);
        std::cout << ans << std::endl;
    }else{
        if(C <= 1){
            std::cout << 1 << std::endl;
        }else{
            std::cout << C << std::endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a,n,t) for(int ii=0;ii<n;ii++) scanf("%"#t"",&(a)[ii])
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,a,n) for(int i=a;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define rreps(i,a,n) for(int i=n-1;i>=a;i--)
#define validl(i,a,b) ((i>=a)&&(i<=b))?1:0
#define validmat(x,y,a,b,c,d) (validl(x,a,b) && validl(y,a,b))?1:0
#define validmn(x,y,m,n) validmat(x,y,0,m,0,n)
#define valid(i,n) validl(i,0,n)
#define v(t) vector<t>
#define vv(t) vector<vector<t> >
#define mk make_pair
#define pb push_back
#define pa(a,n,t)  for(int i=0;i<n;i++) printf("%"#t"",a[i])
#define p2 pair<int,int>
#define p3 pair<p2,int>
#define fi first
#define se second
#define sd(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define pl(a) printf("%lld",a)
#define pd(a) printf("%d",a)
#define sf(a) scanf("%lf",&a)
#define pf(a) printf("%lf",a)
#define nline printf("\n")
#define ss(a) scanf("%s",a)
#define ps(a) printf("%s",a)
#define sc(a) scanf("%c",&a)
#define pc(a) printf("%c",&a)
#define mf(m,f) m.find(f)!=m.end()
#define pp3(m) cout<<m.fi.fi<<" "<<m.fi.se<<" "<<m.se<<" "
#define pp2(m) cout<<m.fi<<" "<<m.se<<" "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
unsigned long long int ex(unsigned long long int a,unsigned long long int n,unsigned long long int  m)
{
    //a=
  if(n==0)
  return 1;
 if(n==1)
  return a%m;
  //n=n%(m-1);
  unsigned long long int y=ex(a,n/2,m);
  if(n%2==1)
   return (a*((y*y)%m))%m;
  else
   return (y*y)%m;
}
int main()
{
  lli n,m;
  cin>>n>>m;
  lli mod1=ex(10,n,m*m);
 // lli mod2=ex(10,n,m);
  //cout<<mod1<<" "<<mod2<<endl;
  cout<<mod1/m<<endl;
 // rep(i,m){
  //    if(i*m==(mod1-mod2+m*m)%(m*m)) {cout<<i<<endl;break;}
 // }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>  using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll           long long
#define rep(i,j,n)   for(int i=j;i<n;i++)
#define scn(a)       scanf("%d",&a)
#define scns(a,b)    scanf("%d %d",&a,&b)
#define print(a)     printf("%d\n",a)
#define vec          vector<int>
#define pb           push_back
#define pairs        pair<int,int>
#define f            first
#define s            second
#define all(v)       v.begin(),v.end()
#define mem(a,b)     memset(a,b,sizeof(a))
#define inf          (int)1e9+7
const int mod = 1e9+7;
const int N = 5e5+5;

ll n;

int power(int a,ll b,int mod)
{
    int res = 1;

    while(b>0)
    {
        if(b&1)
        res = (1LL*res*a)%mod;

        a = (1LL*a*a)%mod, b >>= 1;
    }

    return res;
}

int main()
{
    int test; test = 1;

    while(test--)
    {
        int m; scanf("%lld %d",&n,&m);

        // we can write any number in the form of N = (m*m)*k + c where c = remainder when N
        // is divided by (m*m) and k = quotient. Here N = 10^n. We want (N/m)%m. So divide the
        // equation by m we get on RHS = m*k + c/m. Now taking modulo by m our RHS becomes =
        // 0 + (c/m)%m. Thus we have to calculate (c/m)%m. First calculate c and then final result.

        int c = power(10,n,m*m), ans = (c/m)%m;

        print(ans);
    }
}


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

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

  ll left = 0;
  ll right = 2000000000;

  while(right - left > 1)
  {
    ll x = (left + right) / 2;
    if(x * (x + 1) / 2 <= n + 1) left = x;
    else right = x;
  }
  cout << n + 1- left << endl;

    return 0;
}

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>

const ll mod = 1000000007;
const int INF = 1001001001;

// bool C(ll x){
//     if(x > 1e9){

//     }
// }

int main(){
    ll n;
    cin >> n;
    ll m;
    ll ans = n+1;
    ll num = 1;
    for (ll i = 2; num <= n + 1; i++){
        ans--;
        num += i;
    }
    cout << ans << endl;
}


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

using ll = long long;
#define all(x) x.begin(), x.end()

const ll M = 998244353;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	ll h, w; cin >> h >> w;
	vector<ll> p2(1005);
	p2[0] = 1;
	for(ll i = 1; i <= 1004; ++i) p2[i] = p2[i - 1] * 2 % M;
	vector<string> g(h);
	for(auto &v : g) cin >> v;
	ll dot_row = 0;
	vector<array<ll, 2>> pos;
	for(int i = 0; i < h; ++i)
		pos.push_back({i, 0});
	for(int i = 1; i < w; ++i)
		pos.push_back({h - 1, i});
	for(auto &[x, y] : pos){
		//cout << x << ' ' << y << '\n';
		ll red = 0, blue = 0;
		for(int i = 0; i <= min(x, w - 1 - y); ++i){
			if(g[x - i][y + i] == 'R')
				++red;
			else if(g[x - i][y + i] == 'B')
				++blue;
			//cout << "!!" << ' ' << x - i << ' ' << y + i << ' ' << g[x - i][y + i] << '\n';
		}
		if(red == 0 && blue == 0) ++dot_row;
		if(red && blue){
			cout << 0 << '\n';
			exit(0);
		}
	}
	//cout << dot_row << '\n';
	cout << p2[dot_row] % M << '\n';
}

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

// 定義
typedef long long         ll;
typedef pair<ll, ll>      P;
#define ALL(x)            (x).begin(),(x).end()
#define REP(i, n)         for(ll i = 0 ; i < (ll)n ; ++i)
#define REPN(i, m, n)     for(ll i = m ; i < (ll)n ; ++i)
#define VL                vector<ll>
#define VVL               vector<vector<ll>>
#define VVVL              vector<vector<vector<ll>>>
#define INF               (ll)2e9
#define INF_LL            1LL<<60
//#define MOD             998244353
#define MOD               1000000007

ll Ceil(ll val, ll div)   { return (val + div - 1) / div; }
ll CeilN(ll val, ll div)  { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n)     { return (x - x % n); }
bool IsOdd(ll x)          { return ((x & 1) == 1); }
bool IsEven(ll x)         { return ((x & 1) == 0); }

void Solve()
{
	ll N;
	string S, X;
	cin >> N >> S >> X;
	reverse(ALL(S));
	reverse(ALL(X));

	VL amariRev(7);
	REP(i, 7) {
		amariRev[10 * i % 7] = i;
	}

	ll now;
	ll amari;
	ll prevIdx;
	static ll dp[(ll)2e5 + 5][7];

	dp[0][0] = 1;
	REP(i, N - 1) {
		ll matchVal = X[i] == 'T' ? 1 : 0;
		ll fillVal = X[i] == 'T' ? 0 : 1;
		fill(dp[i + 1], dp[i + 1] + 7, fillVal);
		REP(j, 7) {
			if (dp[i][j] == matchVal) {
				now = S[i] - '0';
				amari = (j - now + 14) % 7;
				prevIdx = amariRev[amari];
				dp[i + 1][prevIdx] = matchVal;
				now = 0;
				amari = (j - now + 14) % 7;
				prevIdx = amariRev[amari];
				dp[i + 1][prevIdx] = matchVal;
			}
		}
	}

	string ans;
	ll sVal = (S.back() - '0') % 7;
	if (X.back() == 'T') {
		ans = (dp[N - 1][0] || dp[N - 1][sVal]) ? "Takahashi" : "Aoki";
	} else {
		ans = (dp[N - 1][0] && dp[N - 1][sVal]) ? "Takahashi" : "Aoki";
	}

	cout << ans << endl;
}

// メイン
int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Solve();
	return 0;
}


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

lli pref[2000005];

int main() {
	lli n, k, m, sum, a, b, mini, maxi;
	scanf("%lld %lld", &n, &k);
	m = 2 * n;
	for (lli i = 2; i <= m; i++)
		pref[i] = pref[i - 1] + i - 1 - (2 * max(0LL, i - n - 1));
	sum = 2;
	m = 0;
	while (m < k) {
		k -= m;
		sum++;
		mini = max(1LL, sum - n - n);
		maxi = min(n, sum - 2);
		m = pref[sum - mini] - pref[sum - maxi - 1];
	}
	a = mini - 1;
	m = 0;
	while (m < k) {
		k -= m;
		a++;
		m = pref[sum - a] - pref[sum - a - 1];
	}
	b = k - 1 + max(1ll, sum - a - n);
	printf("%lld %lld %lld\n", a, b, sum - a - b);
	return 0;
}


#include <iostream>
#include <functional>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <complex>
#include <random>
#include <bitset>
#include <list>
// #include <prettyprint.hpp>
using namespace std;
#define repi(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(ll i=0;i<n;++i)
#define repinvi(i,n) for(int i=n-1;i>=0;--i)
#define sll(n) scanf("%lld", &n);
#define sii(n) scanf("%d", &n);
#define slf(n) scanf("%lf", &n);
#define pll pair<ll,ll>
#define pii pair<int,int>
#define psi pair<si,si>
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define vvv(t) vector<vector<vector<t>>>
typedef long long ll;
typedef double lf;
typedef short int si;

static const ll INF = 1e18;
static const ll max_n = 200010;

v(v(ll)) dp(max_n, v(ll)(2, 0));
// v(ll) cs(max_n);
// v(ll) xs(max_n);

void Main(){
    ll N, X, C, x, c;
    // cout << dp << endl;
    cin >> N;
    map<ll, v(ll)> cs2x;
    v(ll) color_l;
    rep(i, N){
        cin >> x >> c;
        cs2x[c].emplace_back(x);
    }

    for(auto p: cs2x){
        sort(p.second.begin(), p.second.end());
        cs2x[p.first] = p.second;
        color_l.emplace_back(p.first);
    }

    // cout << cs2x <<endl;

    sort(color_l.begin(), color_l.end());
    // cout << "color_l:" << color_l << endl;

    for(ll i=0;i<color_l.size(); ++i){
        c = color_l[i];
        ll now_x0, now_x1;
        if(i==0){
            now_x0 = dp[i][0];
            now_x1 = dp[i][1];
        }else{
            ll pc = color_l[i-1];
            now_x0 = cs2x[pc][0];
            now_x1 = cs2x[pc][cs2x[pc].size()-1];
        }

        ll color_le = cs2x[c][0];
        ll color_re = cs2x[c][cs2x[c].size()-1];

        ll this_color_l = color_re - color_le;

        ll dist_from0 = abs(now_x0 - color_re) + this_color_l + dp[i][0];
        ll dist_from1 = abs(now_x1 - color_re) + this_color_l + dp[i][1];

        dp[i+1][0] = min(dist_from0, dist_from1);

        dist_from0 = abs(now_x0 - color_le) + this_color_l + dp[i][0];
        dist_from1 = abs(now_x1 - color_le) + this_color_l + dp[i][1];

        dp[i+1][1] = min(dist_from0, dist_from1);

        // cout << "color r,l=" << color_re << "," << color_le <<endl;
        // cout << "dp[" << i+1 << "]=" << dp[i+1] <<endl;
    }

    ll max_color = color_l[color_l.size()-1];
    ll max_color_i = color_l.size();
    ll ans = min(dp[max_color_i][0]+abs(cs2x[max_color][0]), dp[max_color_i][1]+abs(cs2x[max_color][cs2x[max_color].size()-1]));

    cout << ans <<endl;

}

int main(){
    Main();
}

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

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

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

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solve() {
  vector< int > as(3);
  cin >> as;
  rwhole(sort, as);
  cout << as[0] + as[1] << endl;
}

signed main() {
  solve();
}


#include <bits/stdc++.h>
/* #include <atcoder/all> */
using namespace std;
/* using namespace atcoder; */
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
#define vvint vector<vector<int>>
#define vbl vector<bool>
#define vvbl vector<vector<bool>>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    if (v.size() == 0) return os;
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    os << v[v.size() - 1];
    return os;
}

void solve() {
    ll s,p;
    cin >> s >> p;
    for (ll i = 1; i*i <= p; ++i) {
        if (p % i == 0) {
            ll tmp = p / i;
            if (tmp + i == s) {
                cout << "Yes" << endl;
                return;
            }
        }
    }
    cout << "No" << endl;
}

int main() {
    solve();
    char tmp;
    while (cin >> tmp) {
        cin.putback(tmp);
        solve();
    }
}

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using ll = long long;
using namespace std;

ll n, x;
ll a[50];
ll v[50];
ll l[50];
ll dp[51];
ll dq[51];
int main() {
	cin >> n >> x;
	for (int i = 0; i < n; ++i) cin >> a[i];
	for (int i = 1; i < n; ++i) l[i - 1] = a[i] / a[i - 1] - 1;
	l[n - 1] = 2e18;

	ll s = x;
	for (int i = n - 1; i >= 0; --i) {
		v[i] = s / a[i];
		s %= a[i];
	}

	dp[0] = 1;
	for (int i = 0; i < n; ++i) {
		if (v[i] == 0) {
			dp[i + 1] = dp[i] + dq[i];
			dq[i + 1] = dq[i];
		} else {
			dp[i + 1] = dp[i] + (v[i] < l[i]) * dq[i];
			dq[i + 1] = dp[i] + dq[i];
		}
	}
	cout << dp[n] << endl;
	return 0;
}


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

ll score(string S) {
  vector<ll> cnt(10);
  iota(cnt.begin(), cnt.end(), 0);
  for (char c : S) cnt[c - '0'] *= 10;
  return accumulate(cnt.begin(), cnt.end(), 0);
}

int main() {
  ll K;
  string S, T;
  cin >> K >> S >> T;
  vector<ll> cnt(10, K);
  for (char c : S + T) cnt[c - '0']--;
  ll win = 0;
  for (ll x = 1; x <= 9; x++)
    for (ll y = 1; y <= 9; y++) {
      S.back() = '0' + x;
      T.back() = '0' + y;
      if (score(S) <= score(T)) continue;
      win += cnt[x] * (cnt[y] - (x == y));
    }
  const ll rem = 9 * K - 8;
  cout << fixed << setprecision(12) << (double)win / rem / (rem - 1) << endl;
}

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

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
	int res = 1;
	while(b){
		if(b&1)res = mul(res,a);
		b >>= 1;
		a = mul(a,a);
	}
	return res;
}

ll get(string str,char c){
	map<int,int> cnt;
	for(auto &x : str)cnt[x]++;
	cnt[c]++;
	ll res = 0;
	for(char act = '1'; act <= '9'; act++){
		ll tmp = act-'0';
		res += tmp*pow(10,cnt[act]);
	}
	return res;
}
int K;
ld asd(char c1,char c2,map<char,int> cnt){
	if(c1 != c2){
		ld tmp1 = ld(K-cnt[c1])/(9*K-8);
		ld tmp2 = ld(K-cnt[c2])/(9*K-8-1);
		return tmp1*tmp2;
	}
	ld tmp1 = ld(K-cnt[c1])/(9*K-8);
	ld tmp2 = ld(K-cnt[c2]-1)/(9*K-8-1);
	return tmp1*tmp2;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> K;
	string s1,s2; cin >> s1 >> s2;
	map<char,int> cnt;
	for(auto &x : s1)cnt[x]++;
	for(auto &x : s2)cnt[x]++;
	ld res = 0;
	for(char a = '1'; a <= '9'; a++){
		for(char b = '1'; b <= '9'; b++){
			cnt[a]++;
			cnt[b]++;
			if(cnt[a] <= K && cnt[b] <= K){
				ll r1 = get(s1,a);
				ll r2 = get(s2,b);

				cnt[a]--;
				cnt[b]--;

				if(r1 > r2){
					res += asd(a,b,cnt);
					//cout << a << ' ' << b << '\n';
					//debug(asd(a,b,cnt));
				}
			}else{
				cnt[a]--;
				cnt[b]--;
			}
		}
	}
	//debug(num,den);
	//ld res = num/den;
	cout << fixed << setprecision(15) << res << '\n';
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


#include <bits/stdc++.h>

using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '{' << p.first << ", " << p.second << '}'; }
template<typename T> ostream& operator<<(ostream &os, const set<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &mp) { os << '['; string sep; for (const auto &x : mp) os << sep << x, sep = ", "; return os << ']'; }
template<typename T> ostream& operator<<(ostream &os, const multiset<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] =", dbg_out(__VA_ARGS__)
#define show_testcase(k) cerr << "test-case : " << k << endl;
#else
#define show_testcase(...)
#define dbg(...)
#endif

#define int long long
#define mod 1000000007
#define inf 1e18
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)(x).size()
#define sb(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(), (x).rend()

const string no = "NO";
const string yes = "YES";

void solve()
{
    //cout << fixed << setprecision(10);
    int n; cin >> n;
    int ans = 0;
    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i].ff;
    }
    for (int i = 0; i < n; i++) {
        cin >> v[i].ss;
    }
    for (int num = 0; num <= 1000; num++) {
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            if (num >= v[i].ff && num <= v[i].ss) {
                cnt++;
            }
        }
        if (cnt == n) {
            ans++;
        }
    }
    cout << ans << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int tc = 1; tc <= t; tc++) {
        show_testcase(tc);
        solve();
    }
    return 0;
}

#include <iostream>
using namespace std;

int main() {

	int N;
	cin>>N;

	int A[N];
	int B[N];

	for(int i=0;i<N;i++)
	{
		cin>>A[i];
	}

	for(int i=0;i<N;i++)
	{
		cin>>B[i];
	}

	int x=A[0],y=B[0];
	for(int i=1;i<N;i++)
	{
		if((x < A[i]))
		{
			x =A[i];
		}

		if((y > B[i]))
		{
			y =B[i];
		}
	}

	if(y >= x)
	{
		cout<<y-x+1<<endl;
	}
	else
	{
		cout<<0<<endl;
	}

	return 0;
}


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

int main(){
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  auto ok = [&](ll a, ll b, ll c, ll d) {
    if (a == c && b == d) return 0;
    if (a + b == c + d || a - b == c - d || abs(a - c) + abs(b - d) <= 3) return 1;
    if ((a + b) % 2 == (c + d) % 2) return 2;

    for (ll i = c - 3; i <= c + 3; i++) {
      for (ll j = d - 3; j <= d + 3; j++) {
        if (abs(i - c) + abs(j - d) > 3) continue;
        if (i + j == a + b) return 2;
        if (i - j == a - b) return 2;
      }
    }
    return 3;
  };
  cout << ok(r1, c1, r2, c2) << endl;
  return 0;
}


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define rep(i,a,b)    for(int i=a;i<b;i++)
#define repn(i,a,b)   for(int i=a;i>=b;i--)
#define F first
#define S second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
const int N = 100001;

int fexp(int a, int b,int m){int ans = 1;while(b){if(b&1)ans=(ans*a)%m; b/=2;a=(a*a)%m;}return ans;}
int inverse(int a, int p){return fexp(a, p-2,p);}

// int fac[2000001];

// int nCrModPFermat(int n, int r,int p) {
//     if (r==0) return 1;
//     return (((fac[n]*((((inverse(fac[r],p)%p)*inverse(fac[n-r],p))%p))))%p);
// }

void solve(){
    int n,a,b;cin>>n>>a>>b;
    cout<<n-a+b;

}

signed main(){
        IOS;

        int t=1;
        // cin>>t;
        // fac[0] = 1;
        // for (int i=1 ; i<=100001; i++) fac[i] = fac[i-1]*i%mod;
        rep(i,0,t){
            // cout<<"Case #"<<i+1<<": ";
            solve();
            cout<<"\n";
        }
    //    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

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

struct mint {
    int x;
    mint() : x(0) {}
    mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    mint& operator+=(const mint& p){
        if((x += p.x) >= mod)   x -= mod;
        return *this;
    }
    mint& operator-=(const mint& p){
        if((x -= p.x) < 0)  x += mod;
        return *this;
    }
    mint& operator*=(const mint& p){
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    mint& operator/=(const mint& p){
        *this *= p.inverse();
        return *this;
    }
    mint operator-() const { return mint(-x); }
    mint operator+(const mint& p) const { return mint(*this) += p; }
    mint operator-(const mint& p) const { return mint(*this) -= p; }
    mint operator*(const mint& p) const { return mint(*this) *= p; }
    mint operator/(const mint& p) const { return mint(*this) /= p; }
    bool operator==(const mint& p) const { return x == p.x; }
    bool operator!=(const mint& p) const { return x != p.x; }
    mint pow(int64_t n) const {
        mint res = 1, mul = x;
        while(n > 0){
            if(n & 1)   res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    mint inverse() const { return pow(mod - 2); }
    friend ostream& operator<<(ostream& os, const mint& p){
        return os << p.x;
    }
    friend istream& operator>>(istream& is, mint& p){
        int64_t val;
        is >> val;
        p = mint(val);
        return is;
    }
};

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

    int N, M;
    cin >> N >> M;

    vector<vector<mint>> powx(M + 1, vector<mint>(N + 1, 1));
    for(int i = 0; i <= M; ++i){
        for(int j = 0; j < N; ++j){
            powx[i][j + 1] = powx[i][j] * i;
        }
    }

    mint ans = powx[M][N] * N;
    for(int d = 1; d < N; ++d){
        for(int x = 1; x <= M; ++x){
            ans -= powx[M - x][d - 1] * powx[M][N - d - 1] * (N - d);
        }
    }

    cout << ans << endl;

    return 0;
}


#include <set>
#include <iostream>

using namespace std;

#define int long long

const int N = 2e5 + 10;

int par[N], c[N];
set<pair<int, int>> s[N];

void make_set(int x) {
  par[x] = x;
  s[x].insert({c[x], 1});
}

int find_set(int x) {
  if (x == par[x]) {
    return x;
  }
  return par[x] = find_set(par[x]);
}

void union_sets(int x, int y) {
  x = find_set(x);
  y = find_set(y);
  if (x != y) {
    if (s[x].size() > s[y].size()) {
      swap(x, y);
    }
    par[x] = y;
    for (auto z : s[x]) {
      auto it = s[y].lower_bound({z.first, -1});
      int cur = z.second;
      if (!(it == s[y].end() || it->first != z.first)) {
        cur += it->second;
        s[y].erase(it);
      }
      s[y].insert({z.first, cur});
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
    make_set(i);
  }
  while (q--) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x - 1) {
      int w = find_set(y);
      auto it = s[w].lower_bound({z, -1});
      if (it == s[w].end() || it->first != z) {
        cout << "0\n";
      } else {
        cout << it->second << '\n';
      }
    } else {
      union_sets(y, z);
    }
  }
  return 0;
}

// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define a first
#define b second
int c[202020];

// https://github.com/atcoder/live_library/blob/master/uf.cpp
// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
// -> 一部改変.
struct UnionFind{
    vector<int> d;           // 生徒同士が, 同じ集団に属するか否かを管理.
    vector<map<int, int>> c; // 生徒を含む集団が属するクラス情報を管理.
    set<int> s;              // 合流済みの生徒であるか判定.
    UnionFind(int n = 0): d(n, -1), c(n) {}
    int find(int x){
        return (d[x] < 0) ? x : (d[x] = find(d[x]));
    }
    bool unite(int ox, int oy, int cx, int cy){
        int x = find(ox);
        int y = find(oy);
        if(x == y) return false;
        if(d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;

        // 生徒を含む集団が属するクラス情報を更新.
        if(s.count(ox) == 0) c[x][cx]++;
        if(s.count(oy) == 0) c[x][cy]++;
        for(auto &p : c[y]) c[x][p.a] += p.b;

        // 合流済みフラグ設定.
        s.insert(ox);
        s.insert(oy);
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    int size(int x){
        return -d[find(x)];
    }
    int getClassSize(int ox, int cy){
        int x = find(ox);
        return c[x][cy];
    }
    void debug(){
        puts("--- d[i] start ---");
        rep(i, d.size()) printf("%d ", d[i]);
        puts("\n--- d[i]  end ----");
        puts("--- find start ---");
        rep(i, d.size()) printf("%d ", find(i));
        puts("\n--- find  end ----");
        return;
    }
    void debugClass(int ox){
        int x = find(ox);
        puts("--- debugClass start ---");
        for(auto &p : c[x]) printf("class=%d size=%d\n", p.a, p.b);
        puts("\n--- debugClass  end ----");
        return;
    }
};

int main(){

    // 1. 入力情報.
    int N, Q;
    scanf("%d %d", &N, &Q);
    rep(i, N) scanf("%d", &c[i]);

    // 2. クエリ回答.
    UnionFind uf(N);
    rep(i, Q){
        // 2-1. クエリ種別.
        int f;
        scanf("%d", &f);

        // 2-2. f = 1 の 場合.
        if(f == 1){
            int a, b;
            scanf("%d %d", &a, &b);
            a--, b--;
            uf.unite(a, b, c[a], c[b]);
        }

        // 2-3. f = 2 の 場合.
        if(f == 2){
            int x, y;
            scanf("%d %d", &x, &y);
            x--;
            int ans = uf.getClassSize(x, y);
            if(uf.size(x) == 1 && c[x] == y) ans++; // 生徒 x のみの場合を考慮.
            printf("%d\n", ans);
        }
    }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
long long temp[N];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n;i++)
    {
        int a;
        scanf("%d", &a);
        temp[a % 200]++;
    }
    long long ans = 0;
    for (int i = 0; i < 200;i++)
        ans += (temp[i] * (temp[i] - 1)) / 2;
    printf("%lld\n", ans);
}

#include<bits/stdc++.h>
#define re register int
#define ll long long
#define dl long double
#define LL ll
#define V inline void
#define I inline int
#define FOR(i,a,b) for(re i=(a),i##i=(b);i<=i##i;++i)
#define ROF(i,a,b) for(re i=(a),i##i=(b);i>=i##i;--i)
#define gc getchar()
//#define gc (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++
using namespace std;
char *fs,*ft,buf[1<<18];
const int N=5e5+10,mo=998244353,G=3;
LL read(){
	ll p=0; bool w=0; char ch=gc;
	while(!isdigit(ch)) w=ch=='-',ch=gc;
	while(isdigit(ch)) p=p*10+ch-'0',ch=gc;
	return w?-p:p;
}
int n;
ll a[N],s1[N],s2[N],ans;
int main(){
	n=read();
	FOR(i,1,n) a[i]=read(),s1[i]=s1[i-1]+a[i],s2[i]=s2[i-1]+a[i]*a[i];
	FOR(i,2,n) ans+=a[i]*a[i]*(i-1)-2*a[i]*s1[i-1]+s2[i-1];
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
using namespace std;
typedef long long int lli;
#define PB push_back
#define MP make_pair
#define FR first
#define SR second
#define LB lower_bound
#define UB upper_bound
typedef vector<long long int> vi;
typedef pair<long long int,long long int> pi;
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=a;i<b;i++)
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli N,M;
    cin>>N>>M;
    vi a(M);
    FOR(i,0,M)
    {
    	cin>>a[i];
    }
    if(M==0)
    {
    	cout<<1;
    }
    else
    {
    sort(all(a));
    vi interval;
    if(a[0]-1-0!=0)
    	interval.PB(a[0]-1-0);
    FOR(i,1,M)
    {
    	if(a[i]-a[i-1]-1!=0)
    		interval.PB(a[i]-a[i-1]-1);
    }
    if(N+1-a[M-1]-1!=0)
    	interval.PB(N-a[M-1]);
    lli ans=INT_MAX;
    FOR(i,0,interval.size())
    {
    	ans=min(ans,interval[i]);
    }
    lli fintime=0;
    FOR(i,0,interval.size())
    {
    	lli cal=(interval[i]+ans-1)/ans;
    	fintime=fintime+cal;
    }
    cout<<fintime;
}

}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

long long powm(long long a, long long b,long long m) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a %m;
        a = a * a %m;
        b >>= 1;
    }
    return res;
}

signed main()
{

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

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

    int n,m;
    cin>>n>>m;
    int a[m+4];
    a[0]=0;
    a[m+1]=n+1;
    for(int i=1;i<=m;i++)
    {
        cin>>a[i];
    }
    sort(a,a+m+2);
    int mn=1e17;
    for(int i=1;i<=m+1;i++)
    {
        if(a[i]-a[i-1]>1)
        mn=min(mn,a[i]-a[i-1]-1);
    }
    int ans=0;
    for(int i=1;i<=m+1;i++)
    {
        ans+=(a[i]-a[i-1]-1+mn-1)/mn;
    }
    cout<<ans;
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

using namespace std;

ll binomialCoeff(ll n,ll k)
{
    ll res = 1;
    if(k >n - k)
        k = n - k;
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

ll power(ll x,ll y,ll mod = 0)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y/2,mod);
    if(!mod)
    {
        if(y%2)
            return temp*temp;
        else
            return temp*temp*x;
    }
    if (y%2 == 0)
        return (temp*temp)%mod;
    else
        return (((x*temp)%mod)*temp)%mod;
}

//nCr % mod
//ll mod = 1e9 + 7;
//const int N0 = 2e5 + 5;
//ll fact[6*N0];
//ll inv[6*N0],invfac[6*N0];
//
//void factorial()
//{
//    fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
//    inv[1] = 1;
//    for(int i=2;i<=5*N0 + 10;i++)
//    {
//        fact[i] = (fact[i-1]*i)%mod;
//        inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
//        invfac[i] = (invfac[i-1]*inv[i])%mod;
//    }
//}

vector<ll> primes;

void Sieve(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    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<=n; p++)
       if (prime[p])
          primes.push_back(p);
}

//******************************************************    CHECK CONSTRAINTS   ***************************************************************//

const int N = 1005;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        ll l,r;
        cin >> l >> r;
        ll ans = 0;
        ll take = r - l;
        ll x = r - l - l + 1;
        if(x <= 0)
        {
            cout << 0 << "\n";
            continue;
        }
        ans = x*(x + 1)/2;
        cout << ans << "\n";
    }
}

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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int, int> > vii;
typedef   vector<long long> vl;
typedef   pair<int, int> pii;
typedef   pair<ll, ll> pll;
typedef   vector< pair<ll, ll> > vll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};

void omae_wa_mou_shindeiru(int tc) {
    ll n, k;
    cin >> n >> k;
    vl dp2(3 * n + 3);
    vl dp3(3 * n + 3);
    fi(1, n + 1) {
        dp2[i + 1]++;
        dp2[i + n + 1]--;
    }
    fi(1, 3 * n + 2) dp2[i] += dp2[i - 1];
    fi(1, 2 * n + 2) {
        dp3[i + 1] += dp2[i];
        dp3[i + n + 1] -= dp2[i];
    }
    fi(1, 3 * n + 1) dp3[i] += dp3[i - 1];
    ll x;
    for (int i = 3; i <= 3 * n; i++) {
        if (k <= dp3[i]) {x = i; break;}
        else {k -= dp3[i];}
    }
    for (int i = 1; i <= n; i++) {
        int jmi = max(1LL, x - i - n);
        int jma = min(n, x - i - 1);
        if (jmi > jma) {continue;}
        if (k > (jma - jmi + 1)) {k -= (jma - jmi + 1); continue;}
        int y = jmi + k - 1;
        int z = x - i - y;
        cout << i << ' ' << y << ' ' << z << '\n';
        return;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc = 1;
    // cin >> tc;
    fi(1, tc + 1) {
        omae_wa_mou_shindeiru(i);
    }
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

// AtCoder
// #include <atcoder/all>
// using namespace atcoder;

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).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; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> a(n), m(n); rep (i, n) cin >> a[i];
    partial_sum(all(a), m.begin(), [](ll a, ll b){return max(a, b);});
    partial_sum(all(a), a.begin());
    partial_sum(all(a), a.begin());
    rep (i, n) cout << a[i] + m[i] * (i + 1) << '\n';
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
const int nxm = 2e5 + 10;
long long n, a[nxm], Max, s1[nxm], s2[nxm];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        s1[i] = s1[i - 1] + a[i];
    }
    for(int i = 1; i <= n; i++)
        s2[i] = s2[i - 1] + s1[i];
    Max = a[1];
    for(int i = 1; i <= n; i++)
    {
        Max = max(Max, a[i]);
        cout << 1ll*(s1[i] + i*Max + s2[i - 1]) << "\n";
    }
    return 0;
}


#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

#ifdef DEBUG
mt19937 mrand(300);
#else
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
#endif

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = (int) 1e5 + 5;
int n, l;
int a[maxn], b[maxn];

bool read() {
  if (scanf("%d%d", &n, &l) < 2) {
    return false;
  }
  l -= n - 1;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    a[i] -= i + 1;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
    b[i] -= i + 1;
  }
  return true;
}

void solve() {
  for (int i = 0; i < n; ++i) {
    if (b[i] == 0 || b[i] == l - 1) {
      continue;
    }
    int pos = lower_bound(a, a + n, b[i]) - a;
    if (pos >= n || a[pos] != b[i]) {
      printf("-1\n");
      return;
    }
  }
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    if (b[i] == a[i]) {
      continue;
    }
    if (b[i] < a[i]) {
      if (b[i] == 0 || (i && b[i] == b[i - 1])) {
        ++res;
        continue;
      }
      int pos = upper_bound(a, a + n, b[i]) - a - 1;
      res += i - pos;
    } else {
      if (b[i] == l - 1 || (i + 1 < n && b[i] == b[i + 1])) {
        ++res;
        continue;
      }
      int pos = lower_bound(a, a + n, b[i]) - a;
      res += pos - i;
    }
  }
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


// #pragma GCC optimise ("O1")
#include<bits/stdc++.h>
using namespace std;
#define ll           int
#define rep(i,j,n)   for(ll i=j;i<n;i++)
#define scn(a)       scanf("%d",&a)
#define scns(a,b)    scanf("%d %d",&a,&b)
#define print(a)     printf("%0.10lf",a)
#define vec          vector<ll>
#define pb           push_back
#define pairs        pair<ll,ll>
#define f            first
#define s            second
#define all(v)       v.begin(),v.end()
#define srt(v)       sort(v.begin(),v.end())
#define read(a)       for(auto &it:a) cin>>it;
#define write(a)       for(auto it:a) cout<<it<<" ";
#define mem(a,b)     memset(a,b,sizeof(a))
#define nl           printf("\n")
#define inf          1e9
#define zer          INT_MIN
#define fast         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N=2e3+5;

int main()
{
  int t=1;
  // cin>>t;
  while(t--)
  {
    int n,k; scns(n,k);
    int cost[n+5][n+5];

    rep(i,1,n+1)
    rep(j,1,n+1)
    scn(cost[i][j]);

    vec v;
    int cnt=0;
    rep(i,2,n+1)
    v.pb(i);

    do
    {
      int amt=0;
      amt+=cost[1][v[0]];

      // for(auto it:v)
      // cout<<it<<" ";
      // cout<<endl;

      rep(i,1,v.size())
      amt+=(cost[v[i-1]][v[i]]);
      amt+=cost[v[v.size() - 1]][1];

      // cout<<amt<<" ";

      if(amt==k)
      cnt++;
    } while(next_permutation(all(v)));

    cout<<cnt;
  }
}

//Luka Mitrovic
#include <iostream>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;

int main() {

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

	int array[9][9];
	int s_arr[9];
	int n,k;
	int ans = 0, sum = 0;

	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		s_arr[i] = i;
		for (int j = 0; j < n; j++) {
			cin >> array[i][j];
		}
	}

	for (int i = 0; i < n; i++) {
		sum += array[s_arr[i]][s_arr[(i+1)%n]];
	}
	if (sum == k) ans++;
	//cout << sum;

	for (int z = 0; z < 10000; z++) {
		bool f = false;
		sum = 0;
		for (int i = n-2; i > 0; i--) {
			if (f) break;
			if (s_arr[i] < s_arr[i+1]) {  // i = 1

				for (int j = n-1; j > i; j--) {  // i = 3, j = 4
					if (s_arr[j] > s_arr[i]) { // j = 3
						int a = s_arr[j];
						s_arr[j] = s_arr[i];
						s_arr[i] = a;

						if (i < n-2) {        // i = 3, j = 4  ..
							int start = i+1; // start = 4
							int end = start + (n-i)/2;  // end = 4 + (5-3)/2 = 4 + 1 = 5
							for (int s = start, l = 1; s < end; s++,l++) {
								int b = s_arr[s];       // s = 2,3
								s_arr[s] = s_arr[n-l];    // arr[2] <-> arr[3]
								s_arr[n-l] = b;          // arr[3] <-> arr[2]
							}
						}
						for (int s = 0; s < n; s++) {
							sum += array[s_arr[s]][s_arr[(s+1)%n]];
						}
						if (sum == k) ans++;
						f = true;
						break;
					}
				}
			}
		}
	}
	cout << ans << endl;
}


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

int main() {
    int n;
    cin >> n;
    vector<ll> a(n), p(n), x(n);
    rep(i, 0, n) cin >> a[i] >> p[i] >> x[i];

    ll ans = 100100100100;
    ll karians = 100100100100;
    bool kau = false;
    rep(i, 0, n) {
        x[i] -= a[i];
        if(x[i] > 0) {
            karians = p[i];
            kau = true;
        }
        ans = min(ans, karians);
        karians = 100100100100;
    }

    if(kau) cout << ans << endl;
    else cout << "-1" << endl;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, e) for (int i = (int)(s); i < (int)(e); i++)
#define _rep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define _REP(i, e, s) for (int i = (int)(e - 1); i >= (int)(s); i--)

#define yes cout << "yes" << endl;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;

#define no cout << "no" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;

#define AC cout << "AC" << endl;
#define WA cout << "WA" << endl;

#define out(s) cout << s << endl;

#define ll long long
#define ull unsigned long long

#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

const unsigned int BIT_FLAG_0 = (1 << 0);  // 0000 0000 0000 0001
const unsigned int BIT_FLAG_1 = (1 << 1);  // 0000 0000 0000 0010
const unsigned int BIT_FLAG_2 = (1 << 2);  // 0000 0000 0000 0100
const unsigned int BIT_FLAG_3 = (1 << 3);  // 0000 0000 0000 1000
const unsigned int BIT_FLAG_4 = (1 << 4);  // 0000 0000 0001 0000
const unsigned int BIT_FLAG_5 = (1 << 5);  // 0000 0000 0010 0000
const unsigned int BIT_FLAG_6 = (1 << 6);  // 0000 0000 0100 0000
const unsigned int BIT_FLAG_7 = (1 << 7);  // 0000 0000 1000 0000

const double PI = 3.14159265358979323846;

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

#define H_MAX 500
#define W_MAX 500
#define INF 1e9 + 7
const long long MOD = 998244353;

int main() {
    int N, A, P, X;
    cin >> N;
    int ans = INF;
    for (int i = 0; i < N; i++) {
        cin >> A >> P >> X;
        if (X - A > 0) {
            ans = min(ans, P);
        }
    }

    if (ans == INF) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                      \
  cerr << __LINE__ << ": " << #x << " = {";                   \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
    cerr << ((hoge) ? "," : "") << x[hoge];                   \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                         \
  cerr << __LINE__ << ": " << #x << " =\n";                          \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {      \
    cerr << ((hoge ? " {" : "{{"));                                  \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))      \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                 \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  int info[2 * N];
  fill(info, info + 2 * N, -1);
  int A[N], B[N];
  bool ng = false;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (A[i] != -1) {
      if (info[--A[i]] != -1) ng = true;
      info[A[i]] = i;
    }
    if (B[i] != -1) {
      if (info[--B[i]] != -1) ng = true;
      info[B[i]] = i + N;
    }
  }
  if (ng) {
    cout << "No" << '\n';
    return 0;
  }
  auto check = [&](int l, int r) {
    int k = r - l + 1;
    for (int i = l; i - l < k / 2; i++) {
      if (info[i] != -1 && info[i] >= N) return false;
      if (info[i + k / 2] != -1 && info[i + k / 2] < N) return false;
      if (info[i] != -1 && info[i + k / 2] != -1
          && info[i] != info[i + k / 2] - N)
        return false;
      if (info[i] != -1 && B[info[i]] != -1 && B[info[i]] != i + k / 2)
        return false;
      if (info[i + k / 2] != -1 && A[info[i + k / 2] - N] != -1
          && A[info[i + k / 2] - N] != i)
        return false;
    }
    return true;
  };
  bool dp[2 * N][2 * N];
  for (int k = 2; k <= 2 * N; k += 2) {
    for (int l = 0; l + k - 1 < 2 * N; l += 2) {
      int r = l + k - 1;
      dp[l][r] = check(l, r);
      for (int j = l + 1; j + 1 < r; j += 2)
        dp[l][r] |= dp[l][j] & dp[j + 1][r];
    }
  }
  cout << (dp[0][2 * N - 1] ? "Yes" : "No") << '\n';
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
template<typename T>
void read(T&x){
    x=0;
    char ch=getchar();
    int f=1;
    while(!isdigit(ch)){
        if(ch=='-')f*=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=getchar();
    }x*=f;
}
//========================================================
typedef long long ll;
ll t,l,r;

int main(){
    read(t);
    while(t--){
        read(l),read(r);
        printf("%lld\n",r>=2*l?(r-l+1)*(r-2*l+1)-r*(r-2*l+1)/2:0ll);
    }
    return 0;
}


/*  **    ** ******    ****** ****** **     ******    ***
    **    ** **  **    **  ** **  ** **     **  ** *********
    **\../** ******    ****** **  ** ****** **  **    ***
    **    ** ** **     ** **  **  ** **  ** **  **    *****
    **    ** **  ** ** **  ** ****** ****** ******    *****
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI  3.14159265358979323846
#define lp(i,n) for(int i = 0; i <n;i++)
#define rlp(i,n) for(int i = int(n);i >= 0;i--)
#define range(i,a,b) for(int i = int(a);i <= int(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define vt vector
#define sz(v) ((int)((v).size()))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7;
const int OO = (int)2e9;
const int N = (int)1e7;
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
void init(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
template<class T> void read(T& x) {
	cin >> x;
}
void read(double& d) {
	string t;
	read(t);
	d=stod(t);
}
void read(long double& d) {
	string t;
	read(t);
	d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
	read(h);
	read(t...);
}
template<class A> void read(vt<A>& x) {
	for(auto& a:x)
		read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
	for(auto& a:x)
		read(a);
}

void _run(){
	int n;
	ll c;
	read(n,c);
	map<ll,ll>mp;
	lp(i,n){
		ll a,b,C;
		read(a,b,C);
		mp[a]+=C;
		mp[b+1]-=C;
	}
	int last =0;
	ll sum =0;
	pair<ll,ll>pre;
	for(auto &x:mp){
		if(!last){
			pre=x;
			last = 1;
			continue;
		}
		x.sc+=pre.sc;
		pre=x;
	}
	last =0;
	for(auto x:mp){
		if(!last){
			pre=x;
			last=1;
			continue;
		}
		sum+=(x.fr-pre.fr)*min(c,pre.sc);
		pre=x;
	}
	cout<<sum<<endl;
}
int main() {
	#   ifdef LOCAL_PROJECT
        freopen("in.txt", "r", stdin);
    #   endif
	init();
    int t=1;
    // read(t);
    while(t--)_run();

    return 0;
}

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

using ll = long long;

struct event {
    ll t, c;
};

int main()
{
    ll n, c;
    std::cin >> n >> c;
    std::vector<event> events;
    for (int i = 0; i < n; i++) {
        event a, b;
        std::cin >> a.t >> b.t >> a.c;
        b.t++;
        b.c = -a.c;
        events.push_back(a);
        events.push_back(b);
    }
    std::sort(events.begin(), events.end(), [](const event& a, const event& b) -> bool {
        return a.t < b.t;
    });
    ll ans = 0;
    ll prev = 0;
    ll cur = 0;

    for (auto&& e : events) {
        if (e.t != prev) {
            ans += std::min(c, cur) * (e.t - prev);
            prev = e.t;
        }
        cur += e.c;
    }
    std::cout << ans << "\n";
    return 0;
}


//12252024832524
#include <cstdio>
#include <cstring>
#include <algorithm>
#define TT template<typename T>
using namespace std;

typedef long long LL;
const int MAXN = 505;
int n;
int a[MAXN],ID[MAXN],ans[MAXN * MAXN],tot;

LL Read()
{
	LL x = 0,f = 1;char c = getchar();
	while(c > '9' || c < '0'){if(c == '-')f = -1;c = getchar();}
	while(c >= '0' && c <= '9'){x = (x*10) + (c^48);c = getchar();}
	return x * f;
}
TT void Put1(T x)
{
	if(x > 9) Put1(x/10);
	putchar(x%10^48);
}
TT void Put(T x,char c = -1)
{
	if(x < 0) putchar('-'),x = -x;
	Put1(x); if(c >= 0) putchar(c);
}
TT T Max(T x,T y){return x > y ? x : y;}
TT T Min(T x,T y){return x < y ? x : y;}
TT T Abs(T x){return x < 0 ? -x : x;}

void Swap(int x)
{
	int y = x+1;
	int v1 = a[x],v2 = a[y];
	swap(a[x],a[y]);
	swap(ID[v1],ID[v2]);
	ans[++tot] = x;
}

int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	for(int T = Read(); T ;-- T)
	{
		tot = 0;
		n = Read();
		for(int i = 1;i <= n;++ i) a[i] = Read(),ID[a[i]] = i;
		int now = 1;
		for(int i = n;i >= 2;-- i)
		{
			if(i == 2 && ID[i] != i)
			{
				if(!now)
				{
					if(!((n-1)&1)) ans[++tot] = n-1,ans[++tot] = 1,ans[++tot] = n-1;
					else ans[++tot] = n-2,ans[++tot] = 1,ans[++tot] = n-2;
				}
				else ans[++tot] = 1;
			}
			else if(i == 3)
			{
				while(a[1] != 1 || a[2] != 2 || a[3] != 3)
				{
					if(now) Swap(1);
					else Swap(2);
					now ^= 1;
				}
			}
			else
			{
				while(ID[i] != i)
				{
					if((ID[i] & 1) != now)
					{
						if(ID[i] >= 4)
						{
							if(!now) Swap(2);
							else Swap(1);
						}
						else
						{
							if(!now) Swap(2);
							else Swap(3);
						}
					}
					else Swap(ID[i]);
					now ^= 1;
				}
			}
		}
		Put(tot,'\n');
		for(int i = 1;i <= tot;++ i) Put(ans[i],' ');
		if(tot) putchar('\n');
	}
	return 0;
}

#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
/*#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
using namespace std;
using ll = long long int;
using ld = long double;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=5e5+10,LN=18,M=1.1e7+10,SQ=350,BS=737,inf=1e9+10;
const ll INF=1e15;
const int MH=1000696969,MOD=1000000007 /*998244353*/;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
    ll ans=1;
    while (y != 0) {
        if (y & 1) ans = ans * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return ans;
}
ll t,n,p[N];
int main(){
    fast_io;
    cin >> t;
    while(t--){
        cin >> n;
        set<ll> inv[2];
        vector<ll> ans;
        for(ll i=1; i<=n; i++) cin >> p[i];
        if(n==2){
            if(p[1]==1) cout << 0 << '\n';
            else cout << 1 << '\n' << 1 << '\n';
            continue;
        }
        for(ll i=1; i<n; i++){
            if(p[i]>p[i+1]) inv[i%2].insert(i);
        }
        for(ll i=1; !inv[0].empty() || !inv[1].empty(); i++){
            if(i>n*n) return 0;
            ll k=i%2;
            if(inv[k].empty()){
                if(k==1){
                    ans.pb(1);
                    swap(p[1],p[2]);
                    inv[1].insert(1);
                    if(p[2]<p[3]) inv[0].erase(2);
                }else{
                    ans.pb(2);
                    swap(p[3],p[2]);
                    inv[0].insert(2);
                    if(p[1]<p[2]) inv[1].erase(1);
                    if(n>=4 && p[3]<p[4]) inv[1].erase(3);
                }
            }else{
                ll j=*inv[k].rbegin();
                ans.pb(j);
                swap(p[j],p[j+1]);
                inv[k].erase(j);
                if(j>1 && p[j-1]>p[j]) inv[1-k].insert(j-1);
                if(j+2<=n && p[j+1]>p[j+2]) inv[1-k].insert(j+1);
            }
        }
        cout << ans.size() << '\n';
        for(ll i : ans) cout << i << ' ';
        cout << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

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

int n;
vector<pii> v;

bool check(int idx, vector<int>& u)
{
    if (u.size() == 3)
    {
        int dx1 = v[u[0]].first-v[u[1]].first, dy1 = v[u[0]].second-v[u[1]].second;
        int dx2 = v[u[1]].first-v[u[2]].first, dy2 = v[u[1]].second-v[u[2]].second;

        if (!dx1 && !dx2) return true;
        if (!dy1 && !dy2) return true;
        if (dy1*dx2 == dy2*dx1) return true;

        return false;
    }
    /////////////////////////////////////////////////////
    bool ret = false;
    for (int i = idx+1; i < n; i++)
    {
        u.push_back(i);
        ret = (ret || check(i, u));
        u.pop_back();

        if (ret) break;
    }
    return ret;
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    v.resize(n); for (pii& p : v) cin >> p.first >> p.second;

    vector<int> temp;
    cout << (check(-1, temp) ? "Yes" : "No") << endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define print(x) cout << x << "\n"
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))

typedef long long ll;

using namespace std;
using vl = vector<ll>;
using Graph = vector<vector<int> >;

int main() {
    int n; cin >> n;
    vector<int> x(n);
    vector<int> y(n);
    rep(i,n) cin >> x[i] >> y[i];
    bool ans =false;
    rep(i,n-2){
        for(int j=i+1;j<n-1;j++){
            for(int k=j+1;k<n;k++){
                int x1=x[i]; int y1=y[i];
                int x2=x[j]; int y2=y[j];
                int x3=x[k]; int y3=y[k];
                int k1,k2;
                k1 = (y2-y1)*(x3-x1);
                k2 = (y3-y1)*(x2-x1);
                if(k1==k2) ans = true;
            }
        }
    }
    if(ans) cout << "Yes" << endl;
    else cout << "No" << endl;
}


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

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()

using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;

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

int main() {
  int N;
  cin >> N;
  VI t(N);
  VI l(N);
  VI r(N);
  REP(i, N) {
    cin >> t[i] >> l[i] >> r[i];
    l[i] *= 2;
    r[i] *= 2;
    if(t[i] == 2) {
      r[i]--;
    }
    if(t[i] == 3) {
      l[i]++;
    }
    if(t[i] == 4) {
      l[i]++;
      r[i]--;
    }
  }
  vector<PL> p(N);
  REP(i, N) {
    p[i].first = l[i];
    p[i].second = r[i];
  }
  sort(p.begin(), p.end());

  ll ans = 0;
  FOR(i, 0, N - 2) FOR(j, i + 1, N - 1) {
    if(p[i].second >= p[j].first) ans++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  int t;
  vector<double> l(n);
  vector<double> r(n);
  for(int i = 0;i < n;i++){
    cin >> t >> l[i] >> r[i];
    if(t >= 3) l[i] += 0.1f;
    if(t % 2 == 0) r[i] -= 0.1f;
  }
  int ans = 0;
  for(int i = 0;i < n - 1;i++){
    for(int j = i + 1;j < n;j++){
      if(l[i] <= r[j] && r[i] >= l[j]) ans++;
    }
  }
  cout << ans << endl;
  return 0;
}