// -- //
#include <bits/stdc++.h>
#define pll pair<ll, ll>
#define x first
#define y second
using namespace std;
typedef long long ll;
const ll N = 808;
ll a[N][N];
ll p[N][N];
signed main(){
	cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
    ll n, k;cin >> n>>k;
    for(ll i = 0;i<n;i++){
        for(ll j = 0;j<n;j++){
            cin >> a[i][j];
        }
    }
    ll l = -1, r=1e9; //(l;r]
    while(r-l>1){
        ll m = (l+r)>>1;
        bool ok =0;
        for(ll i = 0;i<n&&!ok;i++){
            for(ll j = 0;j<n&&!ok;j++){
                p[i][j] = (a[i][j]>m?-1:1);
                if(i) p[i][j]+=p[i-1][j];
                if(j) p[i][j]+=p[i][j-1];
                if(i&&j) p[i][j]-=p[i-1][j-1];
                if(i>=k-1&&j>=k-1){
                    ll e = p[i][j];
                    if(i-k>=0) e-=p[i-k][j];
                    if(j-k>=0) e-=p[i][j-k];
                    if(i-k>=0&&j-k>=0) e+=p[i-k][j-k];
                    if(e>=0) ok=1;
                }
            }
        }
        // ok=1 if exists square with median <= m
        if(ok) r=m;
        else l=m;
    }
    cout<<r;
}

#include <bits/stdc++.h>

#define speedup ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr),cout.precision(12)
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
#define sbc __builtin_popcount
#define pb push_back
#define pbk pop_back
#define em emplace
#define emb emplace_back
#define ff first
#define ss second
#define ll long long
const ll mod = 1e9 + 7;

template<typename T> void smax(T& a, T b) { if(a<b) a=b; }
template<typename T> void smin(T& a, T b) { if(a>b) a=b; }
template<typename T> T pw(T a,T b) { T p=1,one=1; while(b) { if(b&one) p=p*a; a=a*a; b >>=1; } return p; }
template<typename T> T pwm(T a,T b,T md=mod) { T p=1,one=1; while(b) { if(b&one) p=p*a%md; a=a*a%md; b >>=1; } return p; }

template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (auto &it:v)
        is >> it;
    return is;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &it) {
    os << it.first << ' ' << it.second;
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const array<T, 2> &v) {
    for (auto &it : v) os << it << ' ';
    return os;
}

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (auto &it : v)
        os << it << ' ';
    return os;
}

#ifndef ONLINE_JUDGE
class Timer { chrono::high_resolution_clock::time_point start_t, end_t; public: Timer() { start_t=chrono::high_resolution_clock::now(); } ~Timer() { end_t = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(end_t-start_t); cerr<<"\nRunTime: "<<duration.count()<<"ms"<<'\n'; } };
void debug() { cerr << '\n'; }

template<typename T, typename... Args>
void debug(T print, Args... args) {
    cerr << ' ' << print;
    debug(args...);
}

#define deb(...) cerr << "[" << #__VA_ARGS__ << "]" << " --> ", debug(__VA_ARGS__)
#else
#define deb(...) void(0)
#endif
void solve() {
	int n;
	int64_t c;
	cin>>n>>c;
	map<int,pair<int64_t,int64_t>> mp;
	for(int i=0,x,y,z;i<n;++i) {
		cin>>x>>y>>z;
		mp[x].ff+=z;
		mp[y].ss+=z;
	}
	int64_t ans=0;
	int p=mp.begin()->ff-1;
	int64_t s=0;
	for(auto& it:mp) {
		int pos=it.ff;
		int64_t add=it.ss.ff, sub=it.ss.ss;

		if(pos-p>1)
			ans+=min(s,c)*(pos-p-1);

		s+=add;
		ans+=min(s,c);

		s-=sub;

		p=pos;
	}
	cout<<ans<<'\n';
}
int main() {
    speedup;
    //int t; cin >> t; while (t--)
	solve();
}
/**Vim
 *
 *
 *
 *
 *
 *
 *
 *
 */


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

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

const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;

void solve() {
    int N, Q; cin >> N >> Q;
    vl A(N); F0R(i, N) cin >> A[i];
    while(Q--) {
        ll K; cin >> K;
        ll lo = 0, hi = 3e18;
        while (lo < hi) {
            ll mid = (lo+hi)/2;
            ll num = ub(all(A), mid) - A.begin();
            if (mid-num >= K) {
                hi = mid;
            } else {
                lo = mid+1;
            }
        }
        cout << lo << nl;
    }

}

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

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

	return 0;
}

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
    int N, K; cin >> N >> K;
    vector<vector<int>> A(N, vector<int>(N));
    for (auto &a : A) for (auto &v : a) cin >> v;

    int l = -1, h = 1e9, m;
    while (h - l > 1) {
        m = l + (h - l) / 2;
        // cout << "# m : " << m << endl;

        vector<vector<int>> acc(N + 1, vector<int>(N + 1));
        REP(i, 0, N) {
            REP(j, 0, N) {
                acc[i + 1][j + 1] = acc[i + 1][j] + acc[i][j + 1] - acc[i][j] + (A[i][j] <= m);
            }
        }

        bool ok = false;
        REP(i, 0, N) {
            if (i + K > N) continue;
            REP(j, 0, N) {
                if (j + K > N) continue;
                if (acc[i + K][j + K] - acc[i + K][j] - acc[i][j + K] + acc[i][j] >= (K * K + 1) / 2) ok = true;
            }
        }

        if (ok) h = m;
        else l = m;
    }

    cout << h << endl;
    return 0;
}


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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int > pii;
const ll maxn  = 1005;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll q_pow(ll a,int k)
{
    ll ans=1;
    while(k)
    {
        if(k&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        k>>=1;
    }
    return ans;
}
int n;
char aa, ab, ba, bb;
ll dp[maxn];

int32_t main()
{
    cin >> n >> aa >> ab >> ba >> bb;
    if(n <= 3)
        dokme(1);
    if(ab == 'B')
    {
        if(bb == 'B')dokme(1);
        if(ba == 'A')dokme(q_pow(2ll, n - 3));
        dp[2]=1;
        dp[3]=1;
        for(int i=4; i<=n; i++)
            dp[i]=(dp[i-1]+dp[i-2])%mod;
        dokme(dp[n]);
    }
    else
    {
        if(aa == 'A')dokme(1);
        if(ba == 'B')dokme(q_pow(2ll, n - 3));
        dp[2]=1;
        dp[3]=1;
        for(int i=4; i<=n; i++)
            dp[i]=(dp[i-1]+dp[i-2])%mod;
        dokme(dp[n]);
    }
    return(0);
}

/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include<bits/stdc++.h>
#define ALL(x) (x).begin(),(x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
int a[maxn],p[maxn],x[maxn];

int main()
{
    fastio();
    int n; cin >> n;
    rep(i,1,n) cin >> a[i] >> p[i] >> x[i];
    int ans = inf;
    rep(i,1,n){
        if(a[i] <= (x[i]-1) ) ans = min(ans,p[i]);
    }
    if(ans==inf) cout << -1 << endl;
    else cout <<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma region atcoder
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#pragma endregion
#pragma region macros
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vs = vector<string>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rrep(i, n) for(int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define dame { puts("No"); return 0;}
#define y_n {puts("Yes");}else{puts("No");}
#pragma endregion
#pragma region debug for var, v, vv
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){cout << endl;int cnt = 0;for(const auto& v : vv){cout << cnt << "th : "; view(v); cnt++;} cout << endl;}
#pragma endregion

const ll mod = 1000000007;
const int inf = 1001001001;
const ll INF = 1001001001001001001;
const int MAX = 2000005;

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

template<class T>bool chmax(T &a, const T b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T b) { if (b<a) { a=b; return 1; } return 0; }
ll modpow(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;}

/*--------------------------------------------------------------------------------------------------------------------------------*/
ll dist[20][20];
ll dp[(1<<20) + 1][21];
int n;
ll rec(int bit, int v)
{
    // すでに探索済みだったらリターン
    if (dp[bit][v] != -1) return dp[bit][v];

    // 初期値
    if (bit == (1<<v)) {
        return dp[bit][v] = 0;
    }

    // 答えを格納する変数
    ll res = INF;

    // bit の v を除いたもの
    int prev_bit = bit & ~(1<<v);

    // v の手前のノードとして u を全探索
    for (int u = 0; u < n; ++u) {
        if (!(prev_bit & (1<<u))) continue; // u が prev_bit になかったらダメ

        // 再帰的に探索
        if (res > rec(prev_bit, u) + dist[u][v]) {
            res = rec(prev_bit, u) + dist[u][v];
        }
    }
    return dp[bit][v] = res; // メモしながらリターン
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n;
	vl x(n), y(n), z(n);
	rep(i,n) cin >> x[i] >> y[i] >> z[i];
	rep(i,n) rep(j,n) dist[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0ll, z[j] - z[i]);
	for (int bit = 0; bit < (1<<n); ++bit){
		for (int v = 0; v < n; ++v){
			dp[bit][v] = -1;
			if(__builtin_popcount(bit) == 1 && (bit & (1 << v))){
				dp[bit][v] = dist[0][v];
			}
		}
	}
	ll res = rec((1<<n)-1, 0);
	cout << res << endl;
}

#include<stdio.h>
#include<algorithm>
#define nmax 18
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int n;						//城市数
struct City{
	int x, y, z;
}city[nmax];
int dp[nmax][1<<nmax];		//到城市，状态的最小花费
int dis[nmax][nmax];		//城市之间的花费
int ans=INF;
int dist(City a, City b);
void read();
void create_dis();
void dpprocess();

int main()
{
	read();
	create_dis();
	dpprocess();
	printf("%d", ans);
	return 0;
}

void read()
{
	scanf("%d", &n);
	for( int i=1; i<=n; i++ )
		scanf("%d%d%d", &city[i].x, &city[i].y, &city[i].z);
}

int dist(City a, City b)
{
	return abs(a.x-b.x)+abs(a.y-b.y)+max(0, b.z-a.z);
}

void create_dis()
{
	for( int i=1; i<=n; i++ )
		for( int j=1; j<=n; j++ )
			dis[i][j] = dist(city[i], city[j]);
}

void dpprocess()
{
	memset(dp[0], INF, sizeof(dp));					//fill函数用错了，要加【0】
//	for( int i=1; i<=n; i++ )
//		for( int j=1; j<(1<<n); j++ )
//			dp[i][j] = INF;
		dp[1][1]=0;
	for( int i=1; i<(1<<n); i++ )				//现在的状态
		for( int j=1; j<=n; j++ )				//到哪个城市
		{
			if( i&( 1<<(j-1) ) )				//如果这个状态经过了这个城市
			{
				for( int k=1; k<=n; k++ )
				{
					if( i&( 1<<(k-1) ) && j!=k )	//试试这个状态其他的经过的城市
						dp[j][i] = min( dp[j][i], dp[k][i^( 1<<(j-1) )]+dis[k][j] );
				}
			}
		}
	for( int i=2; i<=n; i++ )
		ans = min( ans, dp[i][(1<<n)-1]+dis[i][1] );
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;

int main(){
    int N;
    cin >> N;
    vector<vector<int> > sunuke(N, vector<int>(3));
    for(int i = 0; i < N; i++){
        cin >> sunuke[i][0];
        cin >> sunuke[i][1];
        cin >> sunuke[i][2];
    }
    set<int> st;
    for(int i = 0; i < N; i++){
        if(sunuke[i][2] - sunuke[i][0] > 0) st.insert(sunuke[i][1]);
    }
    cout << (st.empty() ? -1 : *st.begin()) << endl;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev            coyhhhhhhyoc
#define next          sdNNNmNNNNNNNmds
#define y0           hNNNNy_yNNNNNN_sNh
#define y1          mNNNNNdtdNNNNNNtsNNm
#define yn          mNNNNNNNNy___smNNNms
#define tm            oooooosyysooooot
#define read               tyhyt
#define rank              ytmNmo
#define index              yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

int solve() {
    int n;
    cin >> n;
    int ans = -1;
    rep(i, 1, n) {
        int a, p, x;
        cin >> a >> p >> x;
        if(a >= x) continue;
        if(ans == -1 || p < ans) {
            ans = p;
        }
    }
    cout << ans << endl;
    return 0;
}

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


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

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define x first
#define y second
#define all(v) (v).begin(),(v).end()

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

  int a, b, x, y;
  cin >> a >> b >> x >> y;

  int ans = 0;
  if(2 * x <= y) {
    if(a <= b) ans = (2 * (b - a) + 1) * x;
    else ans = (2 * (a - b) - 1) * x;
  }
  else {
    if(a <= b) ans = x + (b - a) * y;
    else ans = x + (a - b - 1) * y;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define SIZE(x) ll(x.size())
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
typedef pair<int, int> P;
struct edge {
    ll to, cost;
};

vector<vector<edge>> G(300);

vector<ll> dijkstra(ll s, ll N)
{
    vector<ll> value(N, INF);
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, s));
    value[s] = 0;
    while (!que.empty())
    {
        P p = que.top();
        que.pop();
        ll v = p.second;
        if (value[v] < p.first)
        {
            continue;
        }
        rep(i, G[v].size())
        {
            edge e = G[v][i];
            if (value[e.to] > value[v] + e.cost)
            {
                value[e.to] = value[v] + e.cost;
                que.push(P(value[e.to], e.to));
            }
        }
    }

    return value;
}

int main()
{
    int a, b, x, y;
    cin >> a >> b >> x >> y;

    rep(i, 100) {
        G[i].push_back({i + 100, x});
        G[i + 100].push_back({i, x});
        if (i != 99) {
            G[i].push_back({i + 1, y});
            G[i + 1].push_back({i, y});
        }
        if (i != 0) {
            G[i].push_back({i + 99, x});
            G[i + 99].push_back({i, x});
        }
    }

    auto res = dijkstra(a - 1, 200);

    cout << res[b + 99] << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
using namespace __gnu_pbds;
using namespace std;

int n;
string s[200000];

int main(){
    //IO("input.txt", "output.txt");
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> s[i];
    set<string> cov;
    sort(s, s + n);
    for(int i = 0; i < n; i++)
        if(s[i][0] == '!') cov.insert(s[i]);
        else{
            s[i] = "!" + s[i];
            if(cov.find(s[i]) != cov.end()){
                cout << s[i].substr(1, s[i].length() - 1) << '\n';
                return 0;
            }
        }
    cout << "satisfiable\n";
    return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  set<string> st;
  rep(i, n) st.insert(s[i]);

  rep(i, n) {
    if (st.count('!' + s[i])) {
      cout << s[i] << endl;
      return 0;
    }
  }
  cout << "satisfiable" << endl;
  return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int

// quoted from beet-aizu
template <typename T,typename E, typename F, typename G, typename H>
struct LazySegmentTree{
    //using F = function<T(T,T)>;
    //using G = function<T(T,E)>;
    //using H = function<E(E,E)>;
    int n,height;
    F f;
    G g;
    H h;
    T ti;
    E ei;
    vector<T> dat;
    vector<E> laz;
    LazySegmentTree(F f,G g,H h,T ti,E ei):
        f(f),g(g),h(h),ti(ti),ei(ei){}

    void init(int n_){
        n=1;height=0;
        while(n<n_) n<<=1,height++;
        dat.assign(2*n,ti);
        laz.assign(2*n,ei);
    }
    void build(const vector<T> &v){
        int n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) dat[n+i]=v[i];
        for(int i=n-1;i;i--)
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
    }
    inline T reflect(int k){
        return laz[k]==ei?dat[k]:g(dat[k],laz[k]);
    }
    inline void eval(int k){
        if(laz[k]==ei) return;
        laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
        laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
        dat[k]=reflect(k);
        laz[k]=ei;
    }
    inline void thrust(int k){
        for(int i=height;i;i--) eval(k>>i);
    }
    inline void recalc(int k){
        while(k>>=1)
            dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1));
    }
    void update(int a,int b,E x){
        thrust(a+=n);
        thrust(b+=n-1);
        for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if(l&1) laz[l]=h(laz[l],x),l++;
            if(r&1) --r,laz[r]=h(laz[r],x);
        }
        recalc(a);
        recalc(b);
    }
    void set_val(int a,T x){
        thrust(a+=n);
        dat[a]=x;laz[a]=ei;
        recalc(a);
    }
    T query(int a,int b){
        thrust(a+=n);
        thrust(b+=n-1);
        T vl=ti,vr=ti;
        for(int l=a,r=b+1;l<r;l>>=1,r>>=1) {
            if(l&1) vl=f(vl,reflect(l++));
            if(r&1) vr=f(reflect(--r),vr);
        }
        return f(vl,vr);
    }
};

signed main() {

  string s; cin >> s;
  int n = s.size();

  using P = pair<int, int>;
  auto f = [](P a, P b){ return P(a.first + b.first, a.second + b.second); };
  auto g = [](P a, int b){ return P(a.second * b, a.second); };
  auto h = [](int a, int b){ return b != INT_MAX ? b : a; };
  using SG = LazySegmentTree<P, int, decltype(f), decltype(g), decltype(h)>;
  vector<SG> sg(26, SG(f, g, h, P(0, 0), INT_MAX));
  for (int i = 0; i < 26; i++) {
    sg[i].build(vector<P>(n, P(0, 1)));
  }
  // sg.build(vector<P>(n, P(0, 1)));

  //  for(int i = 0; i < q; i++){
  //      int com; cin >> com;
  //      if(!com){
  //          int s, t, x; cin >> s >> t >> x;
  //          sg.update(s, t + 1, x);
  //      }else{
  //          int s, t; cin >> s >> t;
  //          cout << sg.query(s, t + 1).first << endl;
  //      }
  //  }

  for (int i = 0; i < s.size(); i++) {
    sg[s[i] - 'a'].update(i, i + 1, 1);
  }

  int ans = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] != s[i + 1]) continue;
    ans += n - i - sg[s[i] - 'a'].query(i, n).first;

    for (int j = 0; j < 26; j++) {
      if ((int)(s[i] - 'a') == j) {
        sg[j].update(i, n, 1);
      } else {
        sg[j].update(i, n, 0);
      }
    }
  }

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

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             4
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x)           (int)x.size()
#define db(x)           cout<<x<<"\n";

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//insert,find_by_order,order_of_key,lower_bound,upper_bound;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

const int N = 200;

int power(int a, int b = mod - 2) {

  int x = 1 % mod;
  a %= mod;
  b %= mod - 1;
  while (b) {

    if (b & 1) x = (x * a)%mod;
    a = (a * a)%mod;
    b >>= 1;

  //  deb(x);
  }

  return x;

}

void solve()
{

      int ans = 0;

      string s;
      cin >> s;

      int n = sz(s);

      char prev = '/';

      int f = 0;
      int fr[26] = {0};

      for(int i = n - 1;i >= 0; i--)
      {
            if(s[i] == s[i + 1])
            {
                  for(int j = 0;j < 26; j++)
                  {
                        if(j == (int)(s[i] - 'a'))
                          continue;

                        ans += fr[j];
                        fr[s[i] - 'a'] += fr[j];
                        fr[j] = 0;
                  }
            }

            fr[s[i] - 'a']++;
      }

      db(ans)
}

int32_t main()
{

    LETS_GET_SCHWIFTY;

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

    int t = 1;
   //cin >> t;

    while (t--)
        solve();

}

// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values


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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll INF = (1LL << 61);

ll extgcd(ll a, ll b, ll& x, ll& y){
    if (b > a) return extgcd(b, a, y, x);
    if (b == 0){
        x = 1; y = 0;
        return a;
    }
    int g = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
}

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

ll solve(ll& X, ll& Y, ll& P, ll& Q){
	ll ret = INF;
	ll Z = 2 * (X + Y);
	ll R = P + Q;
	ll _n, _m;
	ll g = extgcd(Z, R, _n, _m);
	Z /= g; R /= g;
	for (int i = -Q + 1; i < Y; ++i){
		if ((X + i + g) % g != P % g)
			continue;
		ll tmp = (P / g) - ((X + i) / g);
		ll n = _n * tmp;
		ll m = _m * tmp;
		ll t = max(-n / R, m / Z) - 5;
		n += R * t;
		m -= Z * t;
		while (n < 0 || m > 0){
			n += R;
			m -= Z;
		}
		ret = min(ret, 2 * (X + Y) * n + X + max(i, 0));
	}
	return ret;
}

int main(void){
	int T; cin >> T;
	while (T--){
		ll X, Y, P, Q;
		cin >> X >> Y >> P >> Q;
		ll ret = solve(X, Y, P, Q);
		if (ret == INF) cout << "infinity" << endl;
		else cout << ret << endl;
	}
	return 0;
}

#include <set>
#include <map>
#include <iostream>
using namespace std;

long long check(multiset<long long>& M1, multiset<long long>& M2) {
    auto m1 = M1.begin();
    long long ret = 1000000000000000000;
    while (m1 != M1.end()) {
        long long d1 = -1;
        long long d2 = -1;

        auto m2 = M2.lower_bound(*m1);
        if (m2 != M2.end()) {
            d1 = abs(*m1 - *m2);
        }
        long long cm2 = *m2;
        while (m2 != M2.begin() && *m2 == cm2) {
            m2--;
            if (d2 == -1 || d2 > abs(*m1 - *m2)) {
                d2 = abs(*m1 - *m2);
            }
        }

        if (d1 != -1 && d1 < ret) {
            ret = d1;
        }
        if (d2 != -1 && d2 < ret) {
            ret = d2;
        }

        m1++;
    }

    return ret;
    }

int main(void){
    // Your code here!
    long long N;
    multiset<long long> R;
    multiset<long long> G;
    multiset<long long> B;

    cin>> N;
    for (int i = 0; i < 2 * N; i++) {
        long long a;
        char c;
        cin >> a >> c;
        if (c == 'R') {
            R.insert(a);
        } else if  (c == 'G') {
            G.insert(a);
        } else if  (c == 'B') {
            B.insert(a);
        }
    }
    if (R.size() % 2 == 1 && G.size() % 2 == 1) {
        long long ret1 = check(R, G);
        long long ret2 = check(R, B) + check(G, B);
        if (ret1 < ret2) {
            cout << ret1;
        } else {
            cout << ret2;
        }
        return 0;
    }
    if (R.size() % 2 == 1 && B.size() % 2 == 1) {
        long long ret1 = check(R, B);
        long long ret2 = check(R, G) + check(B, G);
        if (ret1 < ret2) {
            cout << ret1;
        } else {
            cout << ret2;
        }
        return 0;
    }
    if (G.size() % 2 == 1 && B.size() % 2 == 1) {
        long long ret1 = check(G, B);
        long long ret2 = check(G, R) + check(B, R);
        if (ret1 < ret2) {
            cout << ret1;
        } else {
            cout << ret2;
        }
        return 0;
    }

    cout << 0;

}


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

int main(){

  int n = 0;
  cin >> n;

  vector<double> p0(2), p2n(2), pc(2), pc0(2);
  cin >> p0.at(0) >> p0.at(1) >> p2n.at(0) >> p2n.at(1);

  double theta = (2.0 * M_PI) / (double)n;

  pc.at(0) = (p0.at(0) + p2n.at(0)) / 2;
  pc.at(1) = (p0.at(1) + p2n.at(1)) / 2;

  pc0.at(0) = p0.at(0) - pc.at(0);
  pc0.at(1) = p0.at(1) - pc.at(1);

  double ans_real = 0.0, ans_imagine = 0.0;
  ans_real = pc0.at(0) * cos(theta) - pc0.at(1) * sin(theta) + pc.at(0);
  ans_imagine = pc0.at(1) * cos(theta) + pc0.at(0) * sin(theta) + pc.at(1);

  printf("%0.9f %0.9f", ans_real, ans_imagine);

  return 0;
}//main

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define ld long double
#define rep(i,n) for (ll i = 0;i<(n);++i)
#define all(v) v.begin(),v.end()

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;}
vector<long long> divisor(long long n){vector<long long> res;long long i = 1;while (i*i<=n){if(n%i==0){res.push_back(i);}i++;}if(res.size()==0)return res;for(long long i = res.size()-1-(res.back()*res.back()==n);i>=0;--i){res.push_back(n/res[i]);}return res;}
long long safe_mod(long long x,long long m){x%=m;if(x<0){x+=m;}return x;}
long long modpow(long long x,long long n,long long mod){long long ans=1;while(n>0){if(n&1){ans*=x;ans%=mod;}n>>=1;x*=x;x%=mod;}return ans;}
long long intpow(long long x,long long n){long long ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;}
//template<typename T>T intpow(T x,T n){T ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;}
vector<pair<long long,long long>> factor_lst(long long n){vector<pair<long long,long long>> factor_lst;long long d=2;while(d*d<=n){if(n%d==0){long long num=0;while(n%d==0){num+=1;n/=d;}factor_lst.push_back({d,num});}d+=1;}if(n!=1){factor_lst.push_back({n,1});}return factor_lst;}
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))

int msb(int x){return x==0 ? -1:32-__builtin_clz(x);}//1-indexed
int lsb(int x){return x==0 ? 32:__builtin_ctz(x)+1;}//1-indexed
int popcnt(int x){return __builtin_popcount(x);}
int popcnt(long long x){return __builtin_popcount(x);}

bool ingrid(int i,int j,int H,int W){
    return 0<=i&&i<H&&0<=j&&j<W;
}

const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
template<typename T> void print(vector<T> &v){for(int i=0;i<v.size();++i){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
template<typename T> void print(T* v,int size){for(int i=0;i<size;++i){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
template<typename T,typename S>void print(pair<T,S>&p){cout<<p.first<<" "<<p.second<<endl;}
const ll LINF=4*1e18;
const ll MINF=5*1e15;
const int INF=1e9+5;
const ld PI=acosl(-1);
const ld DINF=INF;

void Main();
int main(){cout<<fixed<<setprecision(15);Main();}

void Main(){

int N;cin>>N;
vector<int> A(N);
rep(i,N)cin>>A[i];
int ans=0;
rep(i,N)ans+=max(A[i],10)-10;
cout<<ans<<endl;

}


//Bismillahir Rahmanir Raheem
#ifdef ONLINE_JUDGE
    #include<bits/stdc++.h>
#endif
#include<iostream>
#include<numeric>
#include<tuple>
#include<iomanip>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<math.h>
#include<vector>
#include<climits>
#include<algorithm>
#include<string>
#include<sstream>
#include<string.h>
#include<deque>
#include<set>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(cnt, x) memset((cnt), (x), sizeof(cnt))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int,int> > vii;
typedef pair<int, int> pii;
typedef vector<bool> vbl;
typedef vector<pair<ll,ll> > vll;
typedef pair<ll, ll> pll;
#define PQ priority_queue
#define ff first
#define ss second
#define MP make_pair
#define MT make_tuple
#define mxN (int)2e5 + 2
#define sz(n) (int)(n).size()
#define fasterIO ios::sync_with_stdio(0); cin.tie(0);
#define forc(cn) for(auto &x : (cn))
#define findc(cn, abc) ((cn).find(abc) != (cn).end())

#define PI 2*acos(0)
#define forc(cn) for(auto &x : (cn))
#define MAX 200010
const ll inf = INT_MAX;
const int mod = 1e9 + 7;

bool cmp(const pii x, const pii y) {
    return 2ll * x.ff + x.ss < 2ll * y.ff + y.ss;
}

void solve()
{
    int n;
    cin >> n;
    vii a(n);
    forc(a)
        cin >> x.ff >> x.ss;
    sort(all(a), cmp);
    ll sa = 0, sb = 0, ans = 0;
    forc(a) sa += x.ff;
    for(int i = n - 1; i >= 0; i--) {
        pii &x = a[i];
        sb += x.ff + x.ss;
        sa -= x.ff;
        ans++;
        if(sb > sa) break;
    }
    cout << ans << endl;
}
int main()
{
    fasterIO
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        freopen("output.o", "w+", stdout);
    #endif
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

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

//#Rohitpratap311
//#Keep Calm And Stay Happy

using ll = long long;
using ld = long double;
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
ll mod=1e9;

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

bool ifprime(ll n)
{
    if(n==2||n==3) { return true ; }
    for(ll i=2;i<=sqrt(n);i++)
    {
        if(n%i==0) { return false; }
    }
    return true;
}

vector<ll> prime(1000001,1);
void seive()
{
    prime[0]=0;
    prime[1]=0;
    for(ll i=2;i*i<=1000000;i++)
    {
        if(prime[i])
        {
            for(ll j=i*i;j<=1000000;j+=i)
            {
                prime[j]=0;
            }
        }
    }
}

ll nCr[1001][1001];
void NcR()
{
    loop(i,0,1001)
    {
        loop(j,0,1001)
        {
            nCr[i][j] = 0;
        }
        nCr[i][0] = 1;
    }
    loop(i,1,1001)
    {
        loop(j,1,i+1)
        {
            nCr[i][j]=(nCr[i-1][j]+nCr[i-1][j-1])%mod;
        }
    }
}
ll gcd(ll a,ll b)
{
    if(b==0) { return a; }
    return gcd(b,a%b);
}

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

bool good(ll n)
{
    n=abs(n);
    while(n)
    {
        if(n%10==8) return 1;
        n/=10;
    }
    return false;
}

void solveCP311()
{
    ll n,m;
    cin>>n>>m;
    vector<ll>v(n);
    for(ll i=0;i<n;++i) cin>>v[i];
    set<ll>s;
    vector<ll>cnt(n+1);
    for(ll i=0;i<m;++i) cnt[v[i]]++;
    for(ll i=0;i<=n;++i) if(cnt[i]==0) s.insert(i);
    ll ans=*s.begin();
    for(ll i=1;i<=n-m;++i){
    s.insert(n);
    cnt[v[i-1]]--;
    if(cnt[v[i-1]]==0) s.insert(v[i-1]);
    if(s.find(v[i+m-1])!=s.end()) s.erase(v[i+m-1]);
    cnt[v[i+m-1]]++;
    ans=min(ans,*s.begin());
}
cout<<ans;

}

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


#include<iostream>
#include<cstdio>
#include<cstring>
#define re register
#define X first
#define Y second
#define mp std::make_pair
#define pb push_back
#define ohh(hhh...) fprintf(stderr,hhh)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
template<class T1> inline bool cmax(T1 &x,T1 y) {return x<y?(x=y,1):0;}
template<class T1> inline bool cmin(T1 &x,T1 y) {return x>y?(x=y,1):0;}
template<class T1> inline T1 dmax(T1 x,T1 y) {return x>y?x:y;}
template<class T1> inline T1 dmin(T1 x,T1 y) {return x<y?x:y;}
const int MAXN=110,mod=998244353;
int n,a[MAXN],fac[MAXN],f[MAXN][MAXN][MAXN*MAXN/2],ans;
int read()
{
	int x=0,w=0;
	char ch=0;
	while(ch<'0'||ch>'9') w|=ch=='-',ch=getchar();
	while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return w?-x:x;
}
int main()
{
	re int sm=0;
	n=read();
	for(re int i=1;i<=n;i++) a[i]=read(),sm+=a[i];
	if(sm%2==1) {printf("0\n");return 0;}
	re int up=sm/2;
	fac[0]=1;
	for(re int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
	f[0][0][0]=1;
	for(re int i=1;i<=n;i++)
	{
		for(re int j=0;j<=i;j++)
			for(re int k=0;k<=up;k++) f[i][j][k]=f[i-1][j][k];
		for(re int j=1;j<=i;j++)
			for(re int k=a[i];k<=up;k++) f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k-a[i]])%mod;
	}
	for(re int i=1;i<=n;i++) ans=(ans+1ll*f[n][i][up]*fac[i]%mod*fac[n-i])%mod;
	printf("%d\n",ans);
	return 0;
}

#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#define ll long long
#define rep(i,n) for(int i = 0;i < (int)n;i ++)
#define mod % 998244353
using namespace std;

int main(void){
  ll n;
  cin >> n;
  ll A[n+1];
  A[0] = 1;
  rep(i,n){
    A[i+1] = (A[i]*(i+1))mod;
  }
  vector<ll> w(n);
  ll S = 3;
  rep(i,n){
    cin >> w[i];
    S += w[i];
  }
  sort(w.begin(),w.end());
  ll Q = S;
  vector<vector<vector<ll>>> P(n+1,vector<vector<ll>>(Q,vector<ll>(n+1,0)));
  P[0][Q/2][0] = 1;
  rep(i,n){
    rep(j,Q){

      rep(k,n+1){
        P[i][j][k] = P[i][j][k] mod;
        if(P[i][j][k] > 0){
          if(j+w[i]<Q)P[i+1][j+w[i]][k+1] = (P[i+1][j+w[i]][k+1]+P[i][j][k]) mod;
          if(j-w[i]>=0)P[i+1][j-w[i]][k] = (P[i+1][j-w[i]][k]+P[i][j][k]) mod;
        }
      }
    }
  }
  vector<ll> ans = P[n][Q/2];
  ll sum = 0;
  rep(i,n){
    ll M = (A[i]*A[n-i]) mod;
    sum = (sum+ans[i]*M) mod;
  }
  cout << sum << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <ctime>
#include <map>
#include <set>

using grid = std::vector<std::vector<int64_t>>;

std::random_device rng_seed;
std::mt19937_64 rng(rng_seed());

int n, m, longest;
std::map<std::pair<int64_t, int>, int> s_map;

int64_t calc_hash(const std::string &s)
{
    int64_t hash = 0;
    for (int i = 0; i < static_cast<int>(s.size()); i++)
    {
        hash += (s.at(i) - 'A') << (3 * i);
    }
    return hash;
}

int calc_score(const grid &a)
{
    std::set<std::pair<int64_t, int>> s;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int64_t hx = 0, hy = 0;
            for (int k = 0; k < longest; k++)
            {
                hx += a.at(i).at((j + k) % n) << (3 * k);
                hy += a.at((i + k) % n).at(j) << (3 * k);
                s.emplace(hx, k + 1);
                s.emplace(hy, k + 1);
            }
        }
    }

    int res = 0;
    for (auto &&x : s)
    {
        res += s_map[x];
    }
    return round(1e8 * res / m);
}

int main()
{
    clock_t start = clock();
    auto progress = [&]()
    {
        return (clock() - start) / (2.5 * CLOCKS_PER_SEC);
    };
    std::cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        std::string si;
        std::cin >> si;
        s_map[std::make_pair(calc_hash(si), si.size())]++;
        longest = std::max<int>(longest, si.size());
    }
    grid best(n, std::vector<int64_t>(n)), ans = best;
    int maxscore = 0, t = 0;
    while (progress() < 1)
    {
        for (auto &&[k, v] : s_map)
        {
            auto [hash, len] = k;
            int sx = rng() % n, sy = rng() % n, dx = t, dy = 1 - t;
            for (int i = 0; i < len; i++)
            {
                ans.at((sx + i * dx) % n).at((sy + i * dy) % n) = hash >> (3 * i) & 7;
            }
        }

        int score = calc_score(ans);
        if (score > maxscore)
        {
            maxscore = score;
            best = ans;
        }
        t = 1 - t;
    }

    std::cerr << maxscore << std::endl;

    // 出力
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            putchar('A' + best.at(i).at(j));
        }
        puts("");
    }
    return 0;
}

#include<bits/stdc++.h>
#define fi                first
#define se                 second
#define ll               long long
#define mod              1000000007LL
#define endl              "\n"
#define fast               ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb                push_back
#define all(v)             v.begin(),v.end()
#define inp(V,n)      for(int i=0;i<n;i++){cin >> V[i];}
#define oup(V,n)     {for(int i=0;i<n;i++){cout <<  V[i] << " "  ;} cout << endl;}
#define pll              pair<long long,long long>
#define pi            3.141592653589793238
using namespace std;
//vector<ll> adj[5005];
//vector<bool> vis(5005,false);
ll factorial(ll n)
{
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
ll gcd(ll a, ll b)
{
    if (a == 0)
      return b;
    return gcd(b % a, a);
}
ll lcm(ll a, ll b)
{
   return (a*b)/gcd(a, b);
}
bool sortByVal(const pair<ll,ll> &a,
              const pair<ll,ll> &b)
{
      if(a.fi == b.fi)
        return a.se < b.se;
     else
        return a.fi > b.fi;
}
ll Powb(ll b,ll n)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2);
    if(n%2==0){return (temp*temp)%mod;}
    else{return (b*((temp*temp)%mod))%mod;}
}
ll root(vector<ll> &a,ll i)
{
    while(a[i] != i)
    {
        a[i]=a[a[i]];
        i=a[i];
    }
    return i;

}
void wunion(vector<ll> &a,vector<ll> &size,ll i,ll j)
{

    ll root_i=root(a,i);
    ll root_j=root(a,j);
    if(root_i == root_j)
        return;
    if(size[root_i]<size[root_j])
    {
        a[root_i]=a[root_j];
        size[root_j] += size[root_i];
        size[root_i]=0;
    }
    else
    {
        a[root_j]=a[root_i];
        size[root_i] += size[root_j];
        size[root_j]=0;
    }

}
int main()
{
 fast;
     int t=1;
     //cin>>t;
     while(t--)
     {
     ll i,j,m,n,k,sum=0,c=0,d=0,e=0,f=0,g=0,h=0,maxi=0;
      cin>>n;
      string a;
      for(i=1;i<=1000000;i++)
      {
          a=to_string(i);
          d=a.size();
          c=Powb(10,d)*i;
          e=c+i;
          //cout<<e<<endl;
          if(e<=n)
            sum++;
          else
            break;
      }
      cout<<sum<<endl;

     }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 8e5+5;

int a[sz];
pair <int,int> b[sz];
bool cut[sz];

int main() {
   int n;
   cin >> n;
   n <<= 1;
   for(int i=0; i<n; i++) scanf("%d", &a[i]), b[i] = {a[i], i};
   sort(b, b+n);
   for(int i=0; i<n/2; i++) cut[b[i].second] = 1;

   for(int i=0, z=0, f=0; i<n; i++) {
      if(cut[i]) {
         if(f and z) printf(")"), z--;
         else printf("("), z++, f = 0;
      }
      else {
         if(!f and z) printf(")"), z--;
         else printf("("), z++, f = 1;
      }
   }
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

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

signed main(){
  int n;cin>>n;
  vector<int> v(2*n);
  REP(i,2*n)cin>>v[i];
  vector<P> w(2*n);
  REP(i,2*n)w[i]=P(v[i],i);
  sort(ALL(w));
  vector<int> a(2*n);
  REP(i,n)a[w[i].second]=1;
  REP(i,n)a[w[i+n].second]=-1;
  ll now=0;
  string ans;
  REP(i,2*n){
    if(abs(now)>abs(now+a[i]))ans.push_back(')');
    else ans.push_back('(');
    now+=a[i];
  }
  cout<<ans<<endl;
}


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

#define REP(i,a,b) for(int i=a;i<=b;i++)
using LL = long long;

const int Nmax=2009;
int H,W,r[Nmax],c[Nmax],cnt,ans;
string S;
vector <int> adj[Nmax];

void Conn(int a, int b) {
    adj[a].push_back(b);    adj[b].push_back(a);
}

void DFS_r(int curr, int prev) {
    r[curr]=cnt;
    // cout<<curr<<endl;       getchar();
    for (auto next : adj[curr]) {
        if (next==prev) continue;
        if (!r[next]) DFS_r(next,curr);
    }
}

void DFS_c(int curr, int prev) {
    c[curr]=cnt;
    for (auto next : adj[curr]) {
        if (next==prev) continue;
        if (!c[next])DFS_c(next,curr);
    }
}

int main() {
    ios_base::sync_with_stdio(0);    cin.tie(0);
    cin>>H>>W;
    Conn(1,H+1);    Conn(1,H+W);
    Conn(H,H+1);    Conn(H,H+W);
    REP(i,1,H) {
        cin>>S;
        REP(j,1,W) {
            if (S[j-1]=='#') Conn(i,H+j);
        }
    }
    // cout<<"OK"<<endl;
    REP(i,1,H) {
        if (!r[i]) {
            ++cnt;      DFS_r(i,0);
        }
    }
    // cout<<"OK2"<<endl;
    ans=cnt-1;  cnt=0;
    REP(j,1,W) {
        if (!c[H+j]) {
            ++cnt;      DFS_c(H+j,0);
        }
    }
    ans=min(ans,cnt-1);
    cout<<ans;
    return 0;
}

#define George_Plover
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <algorithm>
#define MAXN 1100
#define LL long long
using namespace std;
int n,m;
bool visr[MAXN],visc[MAXN];
bool vis[MAXN][MAXN];
char s[MAXN][MAXN];
int fa[MAXN*MAXN];
queue<pair<int,int> >q;
int num(int x,int y)
{
    return (x-1)*m+y;
}
int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
void BFS(int x,int y)
{
    vis[x][y]=1;
    q.push(make_pair(x,y));
    while(!q.empty())
    {
        int ux=q.front().first;
        int uy=q.front().second;
        q.pop();

        if(s[ux][1]=='#'||s[ux][m]=='#'||s[1][uy]=='#'||s[n][uy]=='#')
        {
            fa[find(num(ux,uy))]=0;
        }
        for(int j=2;j<m && !visr[ux];j++)
        {
            if(j!=uy && s[ux][j]=='#' && !vis[ux][j])
            {
                q.push(make_pair(ux,j));
                fa[find(num(ux,j))]=find(num(ux,uy));
                vis[ux][j]=1;
            }
        }
        for(int i=2;i<n && !visc[uy];i++)
        {
            if(i!=ux && s[i][uy]=='#' && !vis[i][uy])
            {
                q.push(make_pair(i,uy));
                fa[find(num(i,uy))]=find(num(ux,uy));
                vis[i][uy]=1;
            }
        }
        visr[ux]=visc[uy]=1;
    }
}
int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)
        scanf("%s",s[i]+1);

    for(int i=1;i<=n*m;i++)fa[i]=i;

    if(n==2||m==2)
    {
        cout<<0<<endl;
        return 0;
    }
    int ans=0;

    for(int i=2;i<n;i++)
        for(int j=2;j<m;j++)
        {
            if(s[i][j]=='#' && !vis[i][j])
            {
                BFS(i,j);
                if(find(num(i,j)))
                {
                    //cout<<i<<" "<<j<<endl;
                    ans++;
                }
            }
        }

    for(int i=2;i<n;i++)
        if(s[i][1]=='#'||s[i][m]=='#')
            visr[i]=1;
    for(int j=2;j<m;j++)
        if(s[1][j]=='#'||s[n][j]=='#')
            visc[j]=1;

    int ansr=0,ansc=0;
    for(int i=2;i<n;i++)
        if(!visr[i])
            ansr++;

    for(int j=2;j<m;j++)
        if(!visc[j])
            ansc++;
    //cout<<ans<<endl;
    printf("%d\n",min(ansc,ansr)+ans);

    return 0;
}


#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 3e3 + 10;
ll H, W, Par[MXN], Sz[MXN];
vector<ll> R[MXN], C[MXN];
string s[MXN];
ll f1, f2, f3;
ll Find(ll x){
    return (x == Par[x] ? x : Par[x] = Find(Par[x]));
}
bool Union(ll u, ll v){
    u = Find(u), v = Find(v);
    if(u == v) return 0;
    if(Sz[v] > Sz[u]) swap(u, v);
    Sz[u] += Sz[v], Par[v] = u;
    return 1;
}
void Build(){
    for(int i = 1; i <= H + W; i ++){
        Par[i] = i, Sz[i] = 1;
    }
    for(int i = 1; i <= H; i ++){
        for(int j = 1; j <= W; j ++){
            if(s[i][j] == '#') Union(i, j + H);
        }
    }
}
int main(){
    //ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    cin >> H >> W;
    if(min(H, W) <= 2) return cout << 0, 0;
    for(int i = 1; i <= H; i ++){
        cin >> s[i];
        s[i] = "$" + s[i];
    }
    s[1][1] = s[1][W] = s[H][1] = s[H][W] = '#';
    Build();
    for(int i = 1; i <= H + W; i ++){
        if(Find(i) != i) continue;
        f1 += Union(i, 1);
    }

    Build();
    for(int i = 1; i <= H; i ++){
        f2 += Union(1, i);
    }

    Build();
    for(int j = 1; j <= W; j ++){
        f3 += Union(1, j + H);
    }
    for(int i = 1; i <= 100; i ++) Build();
    cout << min({f1, f2, f3}) << '\n';
    return 0;
}

#include<bits/stdc++.h>
#define MAX 1000000007
#define MOD 998244353
#define ll long long int
#define fo(i,a,n) for(int i=a;i<n;i++)
#define ull unsigned long long int
#define pb push_back
#define eb emplace_back
#define in insert
#define cl clear
#define ld long double
#define mp make_pair
#define ii pair<int,int>
const ll inf = 10e16+7;
using namespace std;
typedef vector<vector<int>> vvi;

void print(int arr[], int n){
    fo(i,1,n+1){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int gc(int arr[], int n) {
    int high = 0;
    for (int i = 0; i < n; i++)
        high = max(high, arr[i]);

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

    int counter = 0;

    for (int i = high; i >= 1; i--) {
        int j = i;
        counter = 0;
        while (j <= high) {
            if(count[j] >=2)
               return j;
           else if (count[j] == 1)
                counter++;
            j += i;
            if (counter == 2)
                return i;
        }
    }
}

int main(){

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

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

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

    int arr[b-a+1];
    int n=a;
    fo(i,0,b-a+1){
        arr[i]=n++;
    }

    cout<<gc(arr,b-a+1);

    return 0;
}


//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <cstdlib>
#include <cmath>
#include <math.h>
#include <sstream>
#include <numeric>
#include <cassert>
//---------------------------------------------------------------
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rrep(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define srep(i, s, n) for (long long i = (s); i <= (long long)(n); i++)
#define drep(i,n) for(long long i = (n)-1; i >= 0; --i)
#define SIZE 100005
#define INF 1e18
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define vec vector
using pint = pair<int,int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
//---------------------------------------------------------------
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//---------------------------------------------------------------

//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

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

    ll a,b,c;
    cin>>a>>b>>c;

    bool ok = 0;
    if(a+b == 2*c) ok = 1;
    if(a+c == 2*b) ok = 1;
    if(c+b == 2*a) ok = 1;

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

    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main()
{

  int X;
  cin >>X;
  int A= X%100;
  cout <<100-A;

}

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define vi vector<int>
#define vll vector<ll>
#define vstr vector<string>

#define PI 2*acos(0.0)
#define test ll tc;cin>>tc; while(tc--){solve();}
#define MAX3(a,b,c) max(a,max(b,c))
#define MIN3(a,b,c) min(a,min(b,c))
#define MAX   99999999
#define INF   1<<30
#define EPS   1e-9
#define MOD   1000000007
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

void solve()
{
	int n;
	cin >> n;
	int ans;
	//if(n%100==0)
	ans = ((n / 100) + 1) * 100;
	ans = ans - n;
	cout << ans << endl;

}

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


#include<bits/stdc++.h>

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

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

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

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

void solution()
{
    ll n,m;
    cin>>n>>m;
    char arr[n][m];
    for(ll i=0;i<n;i++) {
        for(ll j=0;j<m;j++) {
            cin>>arr[i][j];
        }
    }
    ll vertex=0;
    for(ll i=0;i<n-1;++i) {
        for(ll j=0;j<m-1;j++) {
            ll ans=(arr[i][j]=='#')+(arr[i][j+1]=='#')+(arr[i+1][j]=='#')+(arr[i+1][j+1]=='#');
            if(ans & 1) vertex++;
        }
    }
    cout<<vertex;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    t=1;
    //cin>>t;
    while(t--){
        solution();
        cout<<"\n";
    }

    return 0;

}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define V vector
#define debug(v) {for(auto i: v) cout << i << " "; cout << endl;}
#define vcin(v) rep(i, v.size()) cin >> v[i]
#define wcin(v, w) rep(i, v.size()) cin >>v[i] >>w[i]

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

    ll ans=0;
    for(ll i=1;i<=n;i++)for(ll j=1;j<=k;j++){
        ans+=100*i+j;
    }
    cout <<ans;
}


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

constexpr int N = 2000;
vector<vector<int>> g(N);

int bfs(int start, int n) {
	vector<int> dist(N, -1);
	queue<int> q;

	dist[start] = 0;
	q.push(start);

	while (!q.empty())
	{
		int v = q.front();
		q.pop();

		for (int nv : g[v]) {
			if (dist[nv] != -1) continue;

			dist[nv] = dist[v] + 1;
			q.push(nv);
		}
	}

	int r = 0;
	rep(i,n) {
		if (dist[i] >= 0) r++;
	}
	return r;
}

int main() {
	int n, m;
	cin >> n >> m;
	rep(i,m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		g[a].push_back(b);
	}

	int ans = 0;
	rep(i,n) {
		ans += bfs(i, n);
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;
const int N = int(2e5) + 2;
int cnt;
bool cycle, vis[2*N];
vector<int> G[2*N];

void DFS(int u, int par)
{
    vis[u] = 1;
    ++cnt;
    int chk = 1;
    for(const int &v : G[u]) {
        if(v == par && chk) {
            --chk;
            continue;
        }
        if(!vis[v]) DFS(v, u);
        else cycle = true;
    }
}

int main()
{
    //freopen("input", "r", stdin);
    ios::sync_with_stdio(false); cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int res = 0;
    for(int i = 1; i < 2 * N; ++i) {
        if(!vis[i]) {
            cnt = 0;
            cycle = false;
            DFS(i, i);
            if(cycle) res += cnt;
            else res += cnt - 1;
        }
    }
    cout << res;
    return 0;
}


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

struct Union{
  vector<ll> par;
  Union(ll a){
  par=vector<ll>(a,-1);
  }
  ll find(ll a){
    if(par[a]<0){
      return a;
    }
    else{
      return par[a]=find(par[a]);
    }
  }
  bool same(ll a,ll b){
    return (find(a)==find(b));
  }
  ll size(ll a){
    return -par[find(a)];
  }
  void unite(ll a,ll b){
    ll c=find(a),d=find(b);
    if(c==d)
      return;
    if(size(c)<size(d)){
      swap(c,d);
    }
    par[c]+=par[d];
    par[d]=c;
  }
};
ll euclidean_gcd(ll a, ll b) {
  if(a < b) return euclidean_gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
ll euclidean_lcm(ll a, ll b) {
  return a/euclidean_gcd(a,b)*b;
}

class smart_sieve {
  intmax_t L, R, M;
  std::vector<int> small;  // 小さい篩
  std::vector<std::vector<intmax_t>> large;  // 大きい篩
  std::vector<intmax_t> aux;  // aux[i] := large[i] の素因数の積

public:
  smart_sieve(intmax_t L, intmax_t R): L(L), R(R), M(sqrt(R)+1) {
    small.resize(M);
    std::iota(small.begin(), small.end(), 0);
    large.resize(R-L);
    aux.assign(R-L, 1);

    for (intmax_t i = 2; i*i < R; ++i) {
      if (small[i] < i) continue;
      small[i] = i;
      for (intmax_t j = i*i; j < M; j += i)
        if (small[j] == j) small[j] = i;

      for (intmax_t j = (L+i-1)/i*i; j < R; j += i) {
        intmax_t k = j;
        do {
          // aux[j-L] > M で判定した方がいいかも？
          // j / aux[j-L] < M の方がいい？（割り算したくない）
          if (aux[j-L] * aux[j-L] > R) break;

          large[j-L].push_back(i);
          aux[j-L] *= i;
          k /= i;
        } while (k % i == 0);
      }
    }
  }

  std::vector<intmax_t> factor(intmax_t n) {
    assert(L <= n && n < R);
    std::vector<intmax_t> res = large[n-L];
    n /= aux[n-L];
    if (n >= M) {
      // この場合，n は素数となることが示せる（はず）
      // n*n >= R だとオーバーフローしそう？
      res.push_back(n);
      return res;
    }
    while (n > 1) {
      res.push_back(small[n]);
      n /= small[n];
    }
    return res;
  }
};

int main(void){
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n+n);
  rep(i,n)cin >> a[i];
  ll m=99999999999999999,p,j,k;
  for (ll bit = 0; bit < (1<<n); ++bit) {
    p=0;
    j=0;
    k=0;
    b[0]=0;
        for (ll i = 0; i < n; ++i) {
            if (bit & (1<<i)){ // 列挙に i が含まれるか
              j++;
              b[j]=a[i];
            }
            else{
              b[j]=b[j]|a[i];
            }
        }
    for (ll i = 0; i < j+1; ++i) {
      p=p^b[i];
    }
    if(m>p)m=p;
  }
  cout << m << endl;
}

#include <iostream>
#include <vector>
#define int long long
using namespace std;
const int N = 30;
typedef long long LL;
int n;
LL seq[N];
signed main(){
	#ifndef ONLINE_JUDGE
		freopen("tpl.txt","r",stdin);
#endif
	cin >> n;
	for(int i = 0; i < n; i ++) cin >> seq[i];
	int ans = 0x7f7f7f7f;
	for(int i = 0; i <= (1 << n) -1; i ++){
		vector<LL > vc;

		for(int j = i  ,l = 0 , r = 0; r != n ;j >>= 1,  r ++){
			int t = 0;
			if( j & 1 == 1 || r == n-1){
				t = seq[l];
				while(l <= r){
					//cout << seq[l]  << ' ';
					t |= seq[l];
					l++;
				}
				vc.push_back(t);
			}
		}
		//cout << endl;

		int tt = vc[0];
		for(int k = 1; k < vc.size();k++){
			tt ^= vc[k];
		}
		ans = min(tt,ans);
	}
	int t = 0;
	for(int i = 0; i < n; i ++){
		t ^= seq[i];
		//cout << seq[i] << ' ';
	}
	//cout << endl;
	ans = min(t,ans);
	cout << ans << endl;

}


//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<vector>
#define swap(x,y) (y -= (x = ((y += x) - x)))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define reps(i,n) for(int i = 1, i <= (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define rreps(i,n) for(int i = (int)(n); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using ll = long long;
using namespace std;
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; }
int digit(ll n, int a) { int s = 0;	for (int i = 0; n > 0; i++) { s++;	n /= a; }	return s; }
int imax(int a[], int n) { int s = a[0];	rep(i, n) { s = max(s, a[i]); }	return s; }
int imin(int a[], int n) { int s = a[0];	rep(i, n) { s = min(s, a[i]); }	return s; }
int cmax(string a, int n) { int s = 0;	rep(i, n) { s = max(s, (int)a[i] - '0'); }	return s; }
int cmin(string a, int n) { int s = 9;	rep(i, n) { s = min(s, (int)a[i] - '0'); }	return s; }

//quick sort(int)
int ascfunc(const void* n1, const void* n2) {
	if (*(int*)n1 > *(int*)n2) {
		return 1;
	}
	if (*(int*)n1 < *(int*)n2) {
		return -1;
	}
	if (*(int*)n1 == *(int*)n2) {
		return 0;
	}
}

int main(void) {
	int h, w;	cin >> h >> w;
	string* s = new string[h];
	rep(i, h) {
		cin >> s[i];
	}

	int ans = 0;
	rep(i, h) {
		rep(j, w) {
			if (i < h - 1 && j < w - 1) {
				int bn = 0;
				rep(k, 2) {
					rep(l, 2) {
						if (s[i + k][j + l] == '#') {
							bn++;
						}
					}
				}
				if (bn == 1 || bn == 3) {
					ans++;
				}
			}
		}
	}

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


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}

const int N=500050;
char s[N],t[N];
queue<int> A,B;
int main(){
	int n=ri();
	rd(s+1);rd(t+1);
	for(int i=1;i<=n;i++){
		if(s[i]=='1')A.push(i);
		if(t[i]=='1')B.push(i);
	}
	ll ans=0;
	while(A.size()||B.size()){
		if(B.empty()){
			if(A.size()==1)return 0*printf("-1\n");
			int i=A.front();A.pop();
			int j=A.front();A.pop();
			ans+=j-i;
		}else{
			if(A.empty())return 0*printf("-1\n");
			if(A.front()<B.front()){
				if(A.size()==1)return 0*printf("-1\n");
				int i=A.front();A.pop();
				int j=A.front();A.pop();
				ans+=j-i;
			}else{
				int i=B.front();B.pop();
				int j=A.front();A.pop();
				ans+=j-i;
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
int n,s[500005],t[500005],tops,topt;
long long ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        char tmp;
        scanf(" %c",&tmp);
        if(tmp=='1') s[++tops]=i;
    }
    for(int i=1;i<=n;i++)
    {
        char tmp;
        scanf(" %c",&tmp);
        if(tmp=='1') t[++topt]=i;
    }
    reverse(s+1,s+tops+1);
    reverse(t+1,t+topt+1);
    while(tops!=0 || topt!=0)
    {
        if(tops==0)
        {
            printf("-1\n");
            return 0;
        }
        if(topt==0)
        {
            while(tops!=0)
            {
                if(tops==1)
                {
                    printf("-1\n");
                    return 0;
                }
                ans+=s[tops-1]-s[tops];
                tops-=2;
            }
            break;
        }
        while(s[tops]<t[topt])
        {
            if(tops==1)
            {
                printf("-1\n");
                return 0;
            }
            ans+=s[tops-1]-s[tops];
            tops-=2;
        }
        ans+=s[tops]-t[topt];
        tops--;
        topt--;
    }
    printf("%lld\n",ans);
}


#include <iostream>
using namespace std;

int main(){
    int x, y;
    cin >> x >> y;
    if(x + y> 14 and y > 7){
        cout << "1";
    } else if (x + y > 9 and y > 2){
        cout << "2";
    } else if (x + y > 2 and y >= 0){
        cout << "3";
    } else {
        cout << "4";
    }

    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
#include <typeinfo>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i,a,b) for(lli i=a;i<b;i++)
#define bfr(i,a,b) for(lli i=a;i>=b;i--)
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL),cout.tie(NULL);
#define tc int t;cin>>t;while(t--)
//#define mod 1000000007LL
#define mod2 998244353LL
#define PI  3.14159265359
#define all(vec) vec.begin(),vec.end()
#define ld long double
#define pb push_back
#define ordered_set tree<lli, null_type,less<lli>, rb_tree_tag,tree_order_statistics_node_update>
lli pmod(lli x,lli y,lli mod){lli res=1;while(y>0){if(y&1)res = ((res%mod)*(x%mod))%mod;y>>=1;x= ((x%mod)*(x%mod))%mod;}return res;}
lli power(lli x,lli y){lli res=1;while(y>0){if(y&1)res=res*x;y>>=1;x=x*x;}return res;}

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

int main()
{

    fio

    lli a,b;
    cin>>a>>b;
    if(a+b>=15 && b>=8)
    cout<<1;
    else if(a+b>=10 && b>=3)
    cout<<2;
    else if(a+b>=3)cout<<3;
    else cout<<4;

    return 0;}


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

int n,m,c=0;
vector<bool>vis;
 vector<vector<int>>adj;
 void dfs(int node)
 {
     vis[node]=true;
     for(auto child: adj[node])
{
      if (!vis[child])
     {
         c++;
          dfs(child);
     }
}
}
int main() {
    int i,j;
   cin>>n>>m;
   adj.resize(n);
   vis.resize(n);
   for(i=0;i<m;i++)
   {
       int a,b;
       cin>>a>>b;
       a--;b--;
       adj[a].push_back(b);
   }
   for(i=0;i<n;i++)
   {
       for(j=0;j<n;j++)
       vis[j]=false;
       dfs(i);
   }
   cout<<c+n;
    return 0;
}

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)

ll INF = 1ll << 60;

vi G[2010];
bool ck[2010];

void init(){
    rep(i, 0, 2010){
        ck[i] = false;
    }
}

void dfs(int v){
    ck[v] = true;
    for(auto nv : G[v]){
        if(ck[nv]){
            continue;
        }
        dfs(nv);
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    rep(i, 0, m){
        int a, b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    ll ans = 0;

    rep(i, 0, n){
        init();
        ll cnt = 0;
        dfs(i);
        rep(i, 0, 2010){
            if(ck[i]){
                cnt++;
            }
        }
        ans += cnt;
    }

    cout << ans << endl;

    return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <iostream>
#include <fstream>
#include <chrono>

using namespace std;

const int MOD = 998244353;

class Solution {
public:
    void execute(int test, istream &in) {
        int n;
        string s, t;
        in >> n >> s >> t;
        int i = 0, j = 0;
        int cnts = 0, cntt = 0;
        int answer = 0;
        while (i < n || j < n) {
            while (i < n && s[i] != '0')
                i++;
            while (j < n && t[j] != '0')
                j++;
            if (i < n && j < n) {
                if (i != j)
                    answer++;
            }
            if (i < n) {
                cnts++;
                i++;
            }
            if (j < n) {
                cntt++;
                j++;
            }
        }
        if (cnts != cntt)
            answer = -1;
        printf("%d\n", answer);
    }
};

int main(int argc, char* argv[])
{
#ifdef VLAD_LOCAL
    //FILE *f = fopen("in.txt", "r");
    ifstream f("in.txt");
    auto start = std::chrono::steady_clock::now();
#else
    //FILE *f = stdin;
    istream &f = cin;
#endif
    //std::ios_base::sync_with_stdio(false);
    int tests = 1;
    //f >> tests;
    for (int test = 0; test < tests; test++) {
        Solution sol;
        sol.execute(test, f);
    }
#ifdef VLAD_LOCAL
    auto end = std::chrono::steady_clock::now();
    double seconds = (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000;
    printf("\n%.3lf seconds\n", seconds);
#endif
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define PI 3.141592653L
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll INF = (ll)1e18;
const int N = 3e5 + 5;
const ll MOD = 1e9+7;

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

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

ll extGcd(ll a, ll b, ll &x, ll &y) { // solves for x and y where ax+by=gcd(a,b)
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extGcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

ll modInv(ll a, ll p) { // solve for x=a^-1 where ax + py = 1 % p
    if(gcd(a, p) != 1) return -1; // modInv only exists if a and p are coprime (gcd(a, p) = 1)
    ll x, y;
    extGcd(a, p, x, y);
    x = (x % p + p) % p;
    return x;
}

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

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

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

void Union(int x, int y) {
    int xPar = find(x), yPar = find(y);
    if(xPar != yPar) parent[xPar] = yPar;
}

ll nCr(ll n, ll r) {
    ll res = 1;
    for(ll i=1;i<=r;i++) {
        res = res * (n - r + i) / i;
    }
    return res;
}

int ask(int i, int j) {
    cout << "? " << i + 1 << ' ' << j + 1 << endl;
    int v;
    cin >> v;
    return v;
}

int msbPos(int n) {
    if(n == 0) return 0;
    int msbPos = 0;
    n /= 2;
    while(n != 0) {
        n /= 2;
        msbPos++;
    }
    return msbPos;
}

string toOct(int n) {
    string res;
    while(n > 0) {
        res = to_string(n % 8) + res;
        n /= 8;
    }
    return res;
}

void solve() {
    ll n, s, k;
    cin >> n >> s >> k;
    ll c = n - s;
    ll d = gcd(gcd(k, c), n);
    k /= d;
    c /= d;
    n /= d;
    if(modInv(k, n) == -1) {
        cout << -1 << endl;
    }
    else {
        cout << (modInv(k, n) * c) % n << endl; // solving x = (b * a^-1) % n. here kx = (n - s) % n -> x = (k^-1 * (c = n - s)) % n;
    }
}

int main()
{
    IOS
    int t;
    cin >> t;
    while(t--) solve();
    //solve();
    return 0;
}


#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

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

int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

int n,m;
vector<vector<pair<int,int>>> g;

void rec(int v,int c,vector<int> &res){
    res[v] = c;
    for(auto e: g[v]){
        if(res[e.first] != -1)continue;
        if(e.second == c)rec(e.first,(e.second+1)%n,res);
        else rec(e.first,e.second,res);
    }
}
int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cin >> n >> m;
    g.resize(n);
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        a--;b--;c--;
        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }
    vector<int> res(n,-1);
    rec(0,0,res);
    for(auto v:res)cout << v + 1 << endl;
}

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=400005;
int fa[N],rk[N];
int h[N],nxt[N],to[N],ww[N],cnt=0;
int n,m;
int ans[N];
int getfa(int x) {
	if (fa[x]==x)	return x;
	return fa[x]=getfa(fa[x]);
}
void SetUnion(int fx,int fy) {
	if	(rk[fx]>rk[fy]) swap(fx,fy);
	fa[fx]=fy;
	rk[fy]+=rk[fx];
}
void add_e(int u,int v,int w) {
	to[++cnt]=v;
	ww[cnt]=w;
	nxt[cnt]=h[u];
	h[u]=cnt;
}

void dfs(int cur,int f,int path) {
	if (cur!=1) {
		if (ans[f]==path) {
			ans[cur]=1;
			if (ans[cur]==path)  ans[cur]++;
		} else ans[cur]=path;
	}
	for (int i=h[cur]; i; i=nxt[i]) {
		int v=to[i];
		if (v==f)  continue;

		dfs(v,cur,ww[i]);
	}
}

int main() {
	scanf("%d%d",&n,&m);
	for (int i=1; i<=n; i++) {
		rk[i]=1;
		fa[i]=i;
	}
	for (int i=1; i<=m; i++) {
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		int fx=getfa(u),fy=getfa(v);
		if (fx!=fy) {
			add_e(u,v,w);
			add_e(v,u,w);
			SetUnion(fx,fy);
		}
	}
	ans[1]=1;
	dfs(1,0,0);
	int flag=1;
	for (int i=1; i<=n; i++)
		if (ans[i]==0)	flag=0;
	if (flag==1)
		for (int i=1; i<=n; i++)
			printf("%d\n",ans[i]);
	else
		printf("No");
	return 0;
}


  //Name : Rupa Dey
  //Date : 1st November 2020 //happy birthday Mhamuda
//joi sri gour hori gobindo...
//gobindo ja hobe vlor jonnoi hobe..
#include<bits/stdc++.h>
#define ll long long
#define d double
#define ld long double
#define vi vector<int>
#define vul vector<ull>
#define vl vector<ll>
#define pb push_back
#define si set<int>
#define sl set<ll>
#define yes cout<<"YES\n"<<endl
#define no cout<<"NO\n"<<endl
#define ys cout<<"Yes\n"<<endl
#define n0 cout<<"No\n"<<endl
#define test cin>>t; while(t--)
#define for(i,a,n) for(i=a;i<n;i++)
#define mp make_pair
#define ft first
#define sc second
#define gp ' ';
#define MAX 9999999
#define MOD 1000000007
#define nd endl
using namespace std;
int main()
{
   ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    if(n%2==0) cout<<"White"<<nd;
    else cout<<"Black"<<nd;
}
//hare krishna...
//hey gobindo kripa koro......
//only gobindo exists..^..

#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
  cin >> n;
  int t;
  vector<tuple<int, int, int>> v;
  while(cin >> t){
    int a, b;
    cin >> a >> b;
    v.push_back(make_tuple(t, a, b));
  }
  int ans = 0;
  for(int i=0; i<(int)v.size()-1; i++){
    for(int j=i+1; j<(int)v.size(); j++){
      bool hoge = false;
      bool temp0, temp1;
      if(get<0>(v.at(i))<=2 && get<0>(v.at(j))%2==1){
        temp0 = (get<1>(v.at(i)) <= get<2>(v.at(j))? true: false);
      }else{
        temp0 = (get<1>(v.at(i)) < get<2>(v.at(j))? true: false);
      }
      if(get<0>(v.at(i))%2==1 && get<0>(v.at(j))<=2){
        temp1 = (get<2>(v.at(i)) >= get<1>(v.at(j))? true: false);
      }else{
        temp1 = (get<2>(v.at(i)) > get<1>(v.at(j))? true: false);
      }
      ans+= temp0&&temp1;

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


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

typedef long long ll;
typedef unsigned long long ull;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ull base = 131;
const ull mod1 = 1e9 + 9;
const ull mod2 = 1610612741;

const int maxn = 1e5 + 10;
int a[maxn], b[maxn], n;

int main()
{
    cin >> n;
    int ans = INT32_MAX;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i] >> b[i];
        ans = min(a[i]+b[i], ans);
    }
    for (int i = 1; i <= n; ++i)
        for (int j = i+1; j <= n; ++j)
            ans = min(ans, min(max(a[i], b[j]), max(a[j], b[i])));
    cout << ans;
    return 0;
}

#include <bits/stdc++.h>
#include <time.h>
using namespace std;
//typedef
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
//define
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)

int main(){
    int n;
    cin >> n;
    vector<ll> a(n),b(n);
    rep(i,n) cin >> a.at(i) >> b.at(i);
    ll x=1e6,y=1e6;
    rep(i,n){
        rep(j,n){
            if(i==j){
                x=min(x,a.at(i)+b.at(j));
            }else{
                x=min(x,max(a.at(i),b.at(j)));
            }
        }
    }

    cout << x << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ull unsigned long long
void testcase(){
    ll n,ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        bool st=true;
        int j=i;
        while(j){
            if(j%10==7){
                st=false;
                break;
            }
            j/=10;
        }
        j=i;
        while(j){
            if(j%8==7){
                st=false;
                break;
            }
            j/=8;
        }
        if(st){
            ans++;
        }
    }
    cout<<ans;
}
int main() {
    ll t=1;
    //cin>>t;
    while(t--){
        testcase();
        //cout<<'\n';
    }
}

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

#define ll long long
#define ld long double
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define F first
#define S second
#define pll pair<ll, ll>
#define all(v) (v).begin(), (v).end()
#define decimal(n) cout << fixed << setprecision(n)
#define mod 1000000007
// #define mod 998244353
#define pi 3.14159265358979323846

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t = 1, tc;
    //cin >> t;
    for (tc = 1; tc <= t; tc++)
    {
        ll n, i;
        cin >> n;
        ll ans = 0, val = 7;
        set<ll> s;
        for (i = 1; i <= n; i++)
        {
            ll x = i;
            while (x > 0)
            {
                if (x % 10 == 7)
                {
                    s.insert(i);
                    break;
                }
                x /= 10;
            }
            x = i;
            while (x > 0)
            {
                if (x % 8 == 7)
                {
                    s.insert(i);
                    break;
                }
                x /= 8;
            }
        }
        cout << n - s.size();
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;

int f(int b,int r,int c){
  return b*500*500+r*500+c;
}

int main(){
  int R,C; cin >> R >> C;
  vvi g(500*500*2);
  vvi co(500*500*2);
  rep(r,R) rep(c,C-1){
    int in; cin >> in;
    g[f(0,r,c)].pb(f(0,r,c+1));
    g[f(0,r,c+1)].pb(f(0,r,c));
    co[f(0,r,c)].pb(in);
    co[f(0,r,c+1)].pb(in);
  }
  rep(r,R-1) rep(c,C){
    int in; cin >> in;
    g[f(0,r,c)].pb(f(0,r+1,c));
    co[f(0,r,c)].pb(in);
  }
  rep(r,R-1) rep(c,C){
    g[f(1,r+1,c)].pb(f(1,r,c));
    co[f(1,r+1,c)].pb(1);
  }
  rep(r,R) rep(c,C){
    g[f(0,r,c)].pb(f(1,r,c));
    co[f(0,r,c)].pb(1);
    g[f(1,r,c)].pb(f(0,r,c));
    co[f(1,r,c)].pb(0);
  }
  vector<int> D(500*500*2, -1);
  vector<bool> used(500*500*2, false);
  priority_queue<pair<int,int>,
                 vector<pair<int,int>>,
                 greater<pair<int,int>>> q;
  q.push({(int)0,f(0,0,0)});
  while(!q.empty()){
    int d = q.top().first; int to = q.top().second; q.pop();
    if(used[to]) continue;
    used[to] = true; D[to] = d;
    rep(i,sz(g[to])){
      int nxt=g[to][i];
      if(D[nxt] != -1 && D[nxt] <= D[to] + co[to][i]) continue;
      q.push({D[to] + co[to][i], nxt});
    }
  }

  int ans = D[f(0,R-1,C-1)];
  cout << ans << endl;
  return 0;
}

#include <queue>
#include <cstdio>
#include <cstring>
#pragma GCC optimize(2)
#define int long long
const int N = 505;
struct twt {
    int x, y, d;
    bool operator < (twt b) const {
        return d > b.d;
    }
};
std::priority_queue<twt> que;
int R, C, a[N][N], b[N][N], dis[N][N];
void Dijkstra() {
    memset(dis, 0x3f, sizeof dis);
    que.push((twt){1, 1, 0});
    dis[1][1] = 0;
    while(!que.empty()) {
        register twt now = que.top(); que.pop();
        register int x = now.x, y = now.y, d = now.d;
      	if(x == R && y == C) return ;
        if(now.d != dis[x][y]) continue;
        if(y < C && d + a[x][y] < dis[x][y+1]) {
            dis[x][y+1] = d + a[x][y];
            que.push((twt){x, y+1, dis[x][y+1]});
        }
        if(y > 1 && d + a[x][y-1] < dis[x][y-1]) {
            dis[x][y-1] = d + a[x][y-1];
            que.push((twt){x, y-1, dis[x][y-1]});
        }
        if(x < R && d + b[x][y] < dis[x+1][y]) {
            dis[x+1][y] = d + b[x][y];
            que.push((twt){x+1, y, dis[x+1][y]});
        }
        for(register int i = 1; i < x; i++) {
            if(d + i+1 < dis[x-i][y]) {
                dis[x-i][y] = d+i+1;
                que.push((twt){x-i, y, dis[x-i][y]});
            }
        }
    }
}
signed main() {
    scanf("%lld%lld", &R, &C);
    for(register int i = 1; i <= R; i++)
        for(register int j = 1; j < C; j++) scanf("%lld", &a[i][j]);
    for(register int i = 1; i < R; i++)
        for(register int j = 1; j <= C; j++) scanf("%lld", &b[i][j]);
    Dijkstra();
    printf("%lld", dis[R][C]);
    return 0;
}


/**
*    author:  ekusiadadus
*    created: 03.01.2021 14:00:37
**/

#pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using P = pair<int,int>;
using ll = long long;

#define rep(i,n) for(int i = 0; i < (n); i++)
#define repn(i,n) for(int i = 1; i <= (n); i++)
#define pb push_back
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

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

const double PI = acos(-1);
const int mod = 1000000007;
class mint {
  long long x;
  public:
  mint(long long x=0) : x((x%mod+mod)%mod) {}
  mint operator-() const {
    return mint(-x);
  }
  mint& operator+=(const mint& a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const  mint& a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint& a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint& a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint& a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    return a;
  }
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint& a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint& a) const {
    mint res(*this);
    return res/=a;
  }

  friend ostream& operator<<(ostream& os, const mint& m){
    os << m.x;
    return os;
  }
};
#pragma endregion

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  int n;
  cin >> n;
  vector<int> a(n-1), b(n-1);
  vector<vector<int>> g(n);
  for(int i = 0; i < n-1; ++i){
    cin >> a[i] >> b[i];
    a[i]--,b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }
  vector<int> p(n,-1);
  vector<vector<int>> ch(n);
  queue<int> q;
  q.push(0);
  vector<int> bfs;
  while(!q.empty()){
    int v = q.front();
    q.pop();
    bfs.push_back(v);
    for(int w:g[v]){
      if(w!=p[v]){
        p[w] = v;
        ch[v].push_back(w);
        q.push(w);
      }
    }
  }
  int t;
  cin >> t;
  ll sum = 0;
  vector<ll> c(n,0);
  while(t--){
    int t,e,x;
    cin >> t >> e >> x;
    e--;
    int a1,b1;
    if(t == 1){
      a1 = a[e];
      b1 = b[e];
    }else{
      a1 = b[e];
      b1 = a[e];
    }
    if(b1 == p[a1]) c[a1] += x;
    else{
      sum += x;
      c[b1] -= x;
    }
  }
  for(int v:bfs){
    for(int w:ch[v]){
      c[w] += c[v];
    }
  }
  for(int i = 0; i < n; ++i){
    cout << sum + c[i] << endl;
  }
  return 0;
}

#include <bits/stdc++.h>

#define debug(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int MAXN = 2E5 + 5;
const int MOD = 1E9 + 7;
int n, m, k;
LL x[MAXN], y[MAXN];
LL mixh[MAXN], miyh[MAXN];
LL ans = 0;

template <class T>
void read(T& x) {
    x = 0;
    T f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
    x *= f;
}

template <class T, class... Args>
void read(T& x, Args&... args) {
    read(x), read(args...);
}

template <class T>
void write(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

#define lid id << 1
#define rid id << 1 | 1

struct node {
    int l, r;
    LL sum;
} tr[MAXN << 2];

void update(int id) { tr[id].sum = tr[lid].sum + tr[rid].sum; }

void build(int id, int l, int r) {
    tr[id].l = l, tr[id].r = r;
    if (l == r) {
        tr[id].sum = 1;
        return;
    }
    int mid = l + r >> 1;
    build(lid, l, mid);
    build(rid, mid + 1, r);
    update(id);
}

int query(int id, int l, int r) {
    if (tr[id].l == l && tr[id].r == r) return tr[id].sum;
    int mid = tr[id].l + tr[id].r >> 1;
    if (r <= mid)
        return query(lid, l, r);
    else if (l > mid)
        return query(rid, l, r);
    else
        return query(lid, l, mid) + query(rid, mid + 1, r);
}

void modify(int id, int x) {
    if (tr[id].l == x && tr[id].r == x) {
        tr[id].sum = 0;
        return;
    }
    int mid = tr[id].l + tr[id].r >> 1;
    if (x <= mid)
        modify(lid, x);
    else
        modify(rid, x);
    update(id);
}

vector<int> s[MAXN];

signed main() {
    // freopen("1.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) mixh[i] = m + 1;
    for (int i = 1; i <= m; i++) miyh[i] = n + 1;
    for (int i = 1; i <= k; i++) {
        cin >> x[i] >> y[i];
        mixh[x[i]] = min(mixh[x[i]], y[i]);
        miyh[y[i]] = min(miyh[y[i]], x[i]);
        s[x[i]].push_back(y[i]);
    }
    for (int i = 1; i < miyh[1]; i++) ans += mixh[i] - 1;
    for (int i = 1; i < mixh[1]; i++) ans += miyh[i] - 1;
    // cout << ans << endl;
    build(1, 1, m + 2);
    for (int i = 1; i < miyh[1]; i++) {
        ans -= query(1, 1, min(mixh[i] - 1, mixh[1] - 1));
        for (auto& y : s[i]) modify(1, y);
    }
    cout << ans;
    return 0;
}


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

ll solve(ll x, ll y)
{
    if (x >= y)
        return x - y;

    ll ans = y - x;
    queue<pair<ll, int>> q;
    map<ll, int> mp;
    q.push({y, 0});
    while (q.size())
    {
        auto p = q.front();
        q.pop();
        ll cur = p.first;
        ll cnt = p.second;
        ans = min(ans, cnt + abs(x - cur));
        if (cur <= x)
            continue;
        if (mp[cur] != 0)
            continue;
        mp[cur] = cnt;

        if (cur % 2 == 0)
        {
            q.push({cur / 2, cnt + 1});
        }
        else
        {
            q.push({cur + 1, cnt + 1});
            q.push({cur - 1, cnt + 1});
        }
    }

    return ans;
}

int main()
{
    ll x, y;
    cin >> x >> y;
    cout << solve(x, y) << endl;
}

// x > y
// -> x - y
// x <= y
// -> + or - -> x -> + or -
// 3 20
// 3 4 5 10 20
// 4 30
// 4 5 6 7 14 28 29 30
// 4 5 6 7  8 16 32 31 30
// 5 16
// 5 6 7 14 15 16
// 5 4 8 16
// 5 6 7  8 16


#include<bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
#define all(v) (v).begin(),(v).end()
const  double pi = acos(-1.0);
const  double eps = 1e-12;
using namespace std;

const int N = 1e8 + 10;
int a[N][2];
ll sum(unordered_map<int, ll> &t, int i, int id) {
  ll res = 0;
  for (; i >= 0; i -= (i + 1) & -(i + 1))
    if (t.count(i)) res += t[i];
  return res;
}
void inc(unordered_map<int, ll> &t, int i, int value, int id) {
    for (; i < N; i += (i + 1) & -(i + 1))
        t[i] += value;
}

ll sum (unordered_map<int, ll> &t, int l, int r, int id){
	ll res =  0;
    if (r > 0) res = sum (t, r, id);
    if (l - 1 >= 0) res -= sum (t, l - 1, id);
    return res;
}
int n, m, q;

void solve(){

    unordered_map <int, ll> t[4];
    cin >> n >> m >> q;
    inc(t[0], 0, n, 0);
    inc(t[1], 0, m, 1);
    ll ans = 0;

    while (q--){
        int type, x, y;
        cin >> type >> x >> y;
        type--;
        inc(t[type], a[x][type], -1, type);
        inc(t[type], y, 1, type);
        inc(t[type + 2], a[x][type], -a[x][type], type + 2);
        inc(t[type + 2], y, y, type + 2);

        ll prefSumx = sum(t[type ^ 1], 0, a[x][type], type ^ 1);
        ll prefSumy = sum(t[type ^ 1], 0, y, type ^ 1);
        ll delta = sum(t[(type + 2) ^ 1], a[x][type] + 1, y, (type + 2) ^ 1);
        ans -= prefSumx * a[x][type];
        ans += prefSumy * y;
        ans -= delta;
        a[x][type] = y;
        //cout << ans << " " << prefSumx << " " << prefSumy << " " << delta << endl;
        cout << ans << "\n";
    }
    //cout << ans << endl;

}

int main(){
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(6);
    //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
    //freopen("exercise.in","r",stdin); freopen("exercise.out", "w", stdout);
    int step;
    step = 1;
    //cin >> step;
    for (int i = 1; i <= step; i++){
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

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

    int ans=x;
    rep(i,n){
        if(s[i]=='o') ans++;
        else{
            if(ans>0) ans--;
        }
    }

    cout<<ans<<endl;

}


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

#define INF (ll)1e9 //10^9:∞
#define LLINF (ll)1e12
#define MOD (ll)(1e9+7) //10^9+7:合同式の法
#define PI 3.141592653589
#define PB push_back
#define F first
#define S second
#define __MAGIC__ ios::sync_with_stdio(false);cin.tie(nullptr);

#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

#define Graph vector<vector<int>>
#define PII pair<int,int>
#define VI vector<int>
#define VVI vector<vector<int>>
#define VPII vector<pair<int,int>>

#define DDD fixed<<setprecision(10)
#define endl "\n"

using namespace std;
/*..................DEFINE GLOBAL VARIABLES...................*/

/*.....................DEFINE FUNCTIONS ......................*/

/*.........................kemkemG0...........................*/
signed main() {
	__MAGIC__

	int N;
	string S;
	cin>>N>>S;
	stack<int> stc;

	REP(i,N){
		if(stc.size()>=2 && S[i]=='x'){
			char oo=stc.top();stc.pop();
			char ff=stc.top();stc.pop();
			if(oo=='o' && ff=='f')continue;
			else {
				stc.push(oo);
				stc.push(ff);
				stc.push(S[i]);
			}
		}
		else stc.push(S[i]);
	}

	cout<<stc.size()<<endl;

		return 0;
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define pb push_back 	#define po pop_back
#define YES cout<<"YES\n" 	#define Yes cout<<"Yes\n"   #define NO cout<<"NO\n"	 #define No cout<<"No\n"
#define for00 for(int i=1;i<=n;i++) cin>>a[i];
#define for0 for(int i=0;i<n;i++)
#define for1 for(int i=1;i<=n;i++)
#define for2(i,a,b) for(int i=a; i<=b;i++)
#define for3(i,a,b) for(int i=a; i>=b;i--)
#define cn change_number_tostring
#define cs change_string_tonumber
#define pri init_prime_distance
#define all(a) begin(a), end(a)
#define SUM(a) accumulate(all(a), 0LL)
#define MIN(a) (*min_element(all(a)))
#define MAX(a) (*max_element(all(a)))
#define lb(a, x) distance(begin(a), lower_bound(all(a), (x)))
#define ub(a, x) distance(begin(a), upper_bound(all(a), (x)))
#define gcd __gcd
#define debug(x) cout<<"x="<<x<<endl
using namespace std;

const int N = 2e5+10;
inline string change_number_tostring(int i) {  stringstream ss; ss<<i; return ss.str();  }
inline int change_string_tonumber(string s) {  int num; stringstream ss(s);  ss>>num; return num;  }
map<pair<int,int>,int> mp;
int ans;
struct ss{
	int x,y;
}a[N];
set<pair<int,int>> s;
void solve()
{
	int n; cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x; cin>>x;
		a[i].x = x/100;
		a[i].y = x%100;
		mp[{a[i].x%2,a[i].y}]++;
		s.insert({a[i].x%2,a[i].y});
	}
	for(int i=1;i<=n;i++)
	{
		pair<int,int> x = {a[i].x%2,a[i].y};
		if(s.find(x)!=s.end())
		{
			if(mp[x]<2)continue;
			else ans+= mp[x]*(mp[x]-1)/2;
			auto q = s.find(x);
			s.erase(q);
		}
	}
	cout<<ans<<endl;
}

signed main()
{
	ios;
	solve();
}

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

#define rep(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; (i) >= (int)(a); (i)--)
#define all(v) v.begin(), v.end()

typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = vector<V<T>>;

/* 提出時これをコメントアウトする */
#define LOCAL true

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    constexpr char endl = '\n';

    ll n;
    cin >> n;
    V<ll> a(n);
    rep(i,0,n) cin >> a[i];

    V<ll> cnt(210);
    rep(i,0,n) {
        cnt[a[i] % 200]++;
    }

    ll ans = 0;
    rep(i,0,200) {
        ll k = cnt[i];
        ans += k * (k - 1) / 2;
    }
    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (1LL << 60);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
int main(){
    ll T;cin>>T;
    string seq0,seq1;
    rep(i,100000)seq0+="0";
    rep(i,100000)seq1+="1";
    rep(i,T){
        ll N;
        cin >> N;
        rep(j,3){
        string t;cin>>t;
        }
        string ans;
      ans=seq0.substr(0,N)+seq1.substr(0,N)+"0";
      cout<<ans<<endl;
    }
}

//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

const ll MOD = 1000000007;
const ll MAX = 2000001;
const ll inf = 2e18;

ll mod(ll a){
  return a % MOD;
}

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

vector<pair<ll,ll>> prime_factorize(ll n){
  vector<pair<ll,ll>> res;

  for(ll i=2; i*i <= n; i++){
    if(n % i != 0) continue;

    ll ex = 0;
    while(n % i == 0){
      ex++;
      n /= i;
    }

    res.push_back({i,ex});
  }

  if(n != 1) res.push_back({n,1});

  return res;

}

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

  rep(i,t){
    ll n; string s; cin >> n >> s >> s >> s;

    cout << string(n,'0') << string(n,'1') << '0' << endl;
  }

}


#include <cstdio>
#include <cctype>
using namespace std;

#define gc getchar
inline int read()
{
	int c = gc(), t = 1, n = 0;
	while (isspace(c)) { c = gc(); }
	if (c == '-') { t = -1, c = gc(); }
	while (isdigit(c)) { n = n * 10 + c - '0', c = gc(); }
	return n * t;
}
#undef gc

int main()
{
	int n = read(), m = read();

	if (m < 0)
	{
		puts("-1");
		return 0;
	}

	if (n == 1)
	{
		if (m != 0)
		{
			puts("-1");
			return 0;
		}

		puts("1 2");
		return 0;
	}

	if (m >= n - 1)
	{
		puts("-1");
		return 0;
	}

	for (int i = 1; i < n; i++)
		printf("%d %d\n", i * 4 - 2, i * 4);
	printf("1 %d\n", (m + 1) * 4 + 1);

	return 0;
}