#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#define rep(i,n) for(ll i=0;i<(n);++i)
#define rep2(i,n) for(ll i=1;i<=(n);++i)
#define rep3(i,i0,n) for(ll i=i0;i<(n);++i)
#define rrep(i,n) for(ll i=((n)-1); i>=0; --i)
#define rrep2(i,n) for(ll i=(n); i>0; --i)
#define pb push_back
#define mod 998244353
#define fi first
#define se second
#define len(x) ((ll)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
using Pi = pair< ll, ll >;
using vl = vector<ll>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vp = vector<Pi>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;

const ll INF = 1LL << 60;
const ld PI = 3.1415926535897932385;
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;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
void printb(ll N,ll d=16){
    rep(i,d){
        cout<<(N/(1<<(d-i-1)))%2;
    }
    cout<<endl;

}
void printv(vector<ll>a){
    rep(i,a.size()){
        if(i==a.size()-1){
            cout<<a[i]<<endl;
        }else{
            cout<<a[i]<<" ";
        }
    }
}
bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

//const vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1};
//const vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1};
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
using mint = modint<mod>;
using vm = vector<mint>;
using vvm = vector<vector<mint>>;
int main() {
    ll H,W;
    cin>>H>>W;

    vs S(H);

    rep(i,H){
        cin>>S[i];
    }

    mint ans=1;

    rep(i,H+W-1){
        ll rf=0,bf=0;

        rep(j,H){
            if(i-j<0 || i-j>=W)continue;
            if(S[j][i-j]=='R'){
                rf=1;
            }else if(S[j][i-j]=='B'){
                bf=1;
            }
        }

        ans*=(2-rf-bf);
    }

    cout<<ans.a<<endl;

    return 0;

}

#include <bits/stdc++.h>
#ifdef LOCAL
#include "prettyprint.hpp"
#else
#define deb(...)
#endif
using namespace std;
using ll = long long;

const int mod = 998244353;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    // cout << fixed << setprecision(15);

    int h, w;
    cin >> h >> w;
    vector<vector<char>> g(h, vector<char>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> g[i][j];
        }
    }

    vector<vector<char>> diagnol(2 * max(h, w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            diagnol[i+j].push_back(g[i][j]);
        }
    }
    ll sum = 1;

    for (auto i : diagnol) {
        bool onlydots = true;
        bool red = false;
        bool blue = false;
        int dotcount = 0;
        for (auto j: i) {
            if (j == '.') dotcount++;
            if (j == 'R') {
                onlydots = false;
                red = true;
            }
            if (j == 'B') {
                onlydots = false;
                blue = true;
            }
        }
        if (red && blue) {
            sum *= 0;
            break;
        }
        if (dotcount == 0) {
            continue;
        }
        if (red || blue) {
            sum *= 1;
            continue;
        }
        sum = sum * 2;
        sum %= mod;
    }
    cout << sum << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo aa[100001];
llo bb[100001];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	llo n,l;
	cin>>n>>l;
	llo ans=0;
	for(llo i=0;i<n;i++){
		cin>>aa[i];
	}
	for(llo i=0;i<n;i++){
		cin>>bb[i];
	}
	vector<llo> cc;
	cc.pb(aa[0]-1);
	for(llo i=1;i<n;i++){
		cc.pb(aa[i]-aa[i-1]-1);
	}
	cc.pb(l-aa[n-1]);
	vector<llo> dd;
	dd.pb(bb[0]-1);
	for(llo i=1;i<n;i++){
		dd.pb(bb[i]-bb[i-1]-1);
	}
	dd.pb(l-bb[n-1]);

	llo ind=0;
	/*for(auto j:cc){
		cout<<j<<",";
	}
	cout<<endl;
	for(auto j:dd){
		cout<<j<<",";
	}
	cout<<endl;*/
	for(llo i=0;i<=n;i++){
		if(dd[i]==0){
			continue;
		}
		llo su=0;
		llo l=ind;
		while(su<dd[i] and ind<=n){
			su+=cc[ind];
			ind++;
		}
		llo r=ind-1;

		if(su!=dd[i]){
			cout<<-1<<endl;
			return 0;
		}
		while(l<r){
			if(cc[l]==0){
				l+=1;
			}
			else{
				break;
			}
		}
		while(r>l){
			if(cc[r]==0){
				r-=1;
			}
			else{
				break;
			}
		}
		ind=r+1;
	//	cout<<i<<":"<<l<<":"<<r<<endl;
		ans+=(max(r-i,(llo)0)+max(i-l,(llo)0));
	}

	cout<<ans<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int g1(string s)
{
  sort(s.begin(), s.end(), greater<char>());
  return stoi(s);
}

int g2(string s)
{
  sort(s.begin(), s.end());
  return stoi(s);
}

int main()
{
  string n;
  int ak;
  cin>>n>>ak;
  int size = ak+1;
  string dp[size]{};
  dp[0] = n;

  for(int k = 1; k < size; k++)
  {
      dp[k] = to_string(g1(dp[k-1]) - g2(dp[k-1]));
      if(dp[k] == dp[k-1])
        {
            k++;
            for(;k < size; k++)
                dp[k] = dp[k-1];
        }
  }

  if(ak == 0)
    cout<<dp[0];
  else
  {
    cout<<dp[ak];
  }

}

#include <bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI (acos(-1.0))
#define EPS 1e-9
#define INF 0x3f3f3f
//#define int ll
//int：4e10 ll:9e18 ull:1e20 __int128:3e38

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef __int128 lll;

const int maxn = 100010;
const ll mod = 1e9 + 7;
int d[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};

template <typename T>
inline T
read() {  // 声明 template 类,要求提供输入的类型T,并以此类型定义内联函数 read()
    T sum = 0, fl = 1;  // 将 sum,fl 和 ch 以输入的类型定义
    int ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        if (ch == '-') fl = -1;
    for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
    return sum * fl;
}

void solve(){

}
int main(){
    IOS;
    double oldp, newp;
    cin >> oldp >> newp;
    cout << ((oldp - newp) / oldp) * 100;
    return 0;
}


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

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x=0):x(x%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;
    if(t&1) a*= *this;
    return a;
  }

  // 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;
  }
};
istream& operator>>(istream& is, mint& a){return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a){return os << a.x;}

mint f(int n){
  if(n < 0) return 0;
  return mint(n)*(n+1)/2;
}

mint c(int n){
  if(n < 0) return 0;
  return f(n) * f(n);
}

int main(){
  ll t;
  cin >> t;
  rep(i,t){
    int n, a, b;
    cin >> n >> a >> b;
    mint x = f(n-a-b+1)*(n-b+1)*(n-a+1);
    mint y = c(n-a-b+1);
    cout << x * 4 - y * 4 << endl;
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
int main(){
    int t;
    cin>>t;
    while(t--){
        ll a,b,c,ans=0;
        cin>>a>>b>>c;
        if(a>=b+c){
            ll res=(a-b-c+1)*(a-b-c+2)%mod;
            ll num=2*(a-b+1)*(a-c+1)%mod-res;
            if(num<0) num=(mod-(-num)%mod)%mod;
            ans=res*num%mod;
        }
        cout<<ans<<endl;
    }
}


#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() {
    ll n;
    cin >> n;
    vector<vector<int>> seen(n + 1, vector<int>(500, 0));
    vector<ll> ans;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < 500; j++) {
            if (seen[i][j] == 0) {
                ans.push_back(j);
                break;
            }
        }
        for (int j = i; j <= n; j += i) {
            seen[j][*ans.rbegin()] = 1;
        }
    }

    rep(i, n) cout << ans[i] << " ";
    cout << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int main()
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	int t = 1;
	//cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		ll ans = 16ll * 25ll * 27ll * 7ll * 11ll * 13ll * 17ll * 19ll * 23ll * 29ll + 1;
		cout << ans << "\n";
	}
	return 0;
}


#include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;

#define REP(i, n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i, n) for(ll i=(ll)(n-1);i>=0;i--)
#define FOR(i, a, b) for(ll i=(ll)(a); i<=(ll)(b); i++)
#define RFOR(i, a, b) for(ll i=(ll)(a);i>=(ll)(b); i--)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define INF 1000000000000
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair
#define F first
#define S second

ll binsearch(ll d, ll M, vector<int> X){
	ll mind = d+1;
	ll maxd = 1000000000000000001;
	{
		ll num=0;
		for(auto c : X){
			//if(M/(double)r - c< num/(double)r ){
			if((M-c)/(double)mind<num){
				return 0;
			}
			num *= mind;
			num += c;
		}
	}
	while(true){
loop:
		ll newd = (mind+maxd)/2;
		ll num = 0;
		for(auto c : X){
			//if(M/(double)c - r < num/(double)c){
			if((M-c)/(double)newd<num){
				maxd = newd;
				goto loop;
			}
			num *= newd;
			num += c;
		}
		if(mind == newd)break;
		mind = newd;
	}
	return mind - d;
}

signed main()
{
	string X;
	ll M;
	cin >> X;
	cin >> M;
	if(X.size() == 1){
		if(X[0]-'0'>M){
			cout << 0;
		}else{
			cout << 1;
		}
		cout << endl;
		return 0;
	}
	int d = 0;
	vector<int> vecX;
	for(char c : X){
		vecX.PB(c-'0');
		if(d < c-'0'){
			d = c-'0';
		}
	}
	cout << binsearch(d, M, vecX) << endl;
}

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>

using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,n) for(i=0;i<(n);++i)
#define PER(i,n) for(i=(n - 1);i>=0;--i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)
#define mod 1000000007
#define maxInt 2147483647
#define minInt -2147483648
#define maxLL 9223372036854775807
#define minLL -9223372036854775807

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef unsigned long long ULL;
typedef __int128 l128;
typedef pair<int,int> PII;
typedef map<int, int> MII;

int check(ULL n, string x, LL m)
{
    l128 res = 0;
    for(int i = 0; i < x.size(); i++)
    {
        res = res * n + l128(x[i] - '0');
        if(res > m) return 0;
    }

    return 1;
}

int main()
{
    string x;
    ULL m, i;
    while(cin>>x)
    {
        cin>>m;
        if(x.size() == 1)
        {
            if(x[0] - '0' - 1 <= m) cout<<1<<endl;
            else cout<<0<<endl;
        }
        else{
            int maxdig = 0;
            REP(i, x.size()) maxdig = max(maxdig, x[i] - '0');
            ULL l = 0, r = 1e18, mid;
            while(l < r)
            {
                mid = (l + r +1)/2;
                if(check(mid,x, m) == 1) l = mid;
                else r = mid -1;
            }

            if(l <= maxdig ) cout<<0<<endl;
            else cout<<l - maxdig<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define pb push_back
#define ll long long int

using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    cin>>s;
    ll n=s.length(),c=0,li=n;
    char pri='0';
    for(ll i=n-2;i>=0;i--)
    {
        if(s[i]==s[i+1] && s[i]!=s[i+2])
        {
            char o=s[i];
           for(ll j=i+2;j<li;j++)
           if(s[j]!=o)
           c++;
           if(s[i]!=pri)
           c+=n-li;
           pri=s[i];
           li=i;
        }

    }
    cout<<c<<"\n";
}

#include <bits/stdc++.h>

#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr)
#define ALL(a) (a).begin(),(a).end()

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

using namespace std;
using ll  = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;

const ll MOD  = 998244353;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626;

ll mod(ll a, ll m){return (a + m) % m;}

int main(){
    string s;
    cin >> s;

    reverse(ALL(s));

    vector<ll> cnt(26, 0);
    ll ans = 0;
    rep(i, (int)s.size()){
        if(i && s[i-1] == s[i]){
            rep(j, 26)if(j != s[i] - 'a'){
                ans += cnt[j];
                cnt[j] = 0;
            }
            cnt[s[i] - 'a'] = i+1;
        }
        else cnt[s[i]-'a']++;
    }

    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5+4;
ll seg[4*N], lazy[4*N];
vector<int> g[N];
int in[N], out[N], timer = 0;

void propagate(int node, int l, int r)
{
    if(lazy[node]==0)
        return;
    seg[node] += lazy[node] * (r-l+1);
    if(l != r)
    {
        lazy[node << 1] += lazy[node];
        lazy[node << 1 | 1] += lazy[node];
    }
    lazy[node] = 0;
}

void update(int node, int l, int r, int s, int e, ll val)
{
    propagate(node, l, r);
    if(e < l || r < s)
        return;
    if(s <= l && r <= e)
    {
        lazy[node] += val;
        propagate(node, l, r);
        return;
    }
    int mid = (l+r) >> 1;
    update(node << 1, l, mid, s, e, val);
    update(node << 1|1, mid+1, r, s, e, val);
    seg[node] = seg[node << 1] + seg[node << 1 | 1];
}

ll query(int node, int l, int r, int idx)
{
    propagate(node, l, r);
    if(idx < l || idx > r)
        return 0;
    if(l == r)
        return seg[node];
    int mid = (l+r) >>1;
    return query(node << 1, l, mid, idx) + query(node<<1|1, mid+1, r, idx);
}

void dfs(int node, int parent)
{
    in[node] = ++timer;

    for(auto v : g[node])
    {
        if(v == parent)
            continue;
        dfs(v, node);
    }

    out[node] = timer;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<pair<int, int>> edge;
    for (int i = 0; i < n-1; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
        edge.emplace_back(x,y);
    }
    dfs(1, -1);

    int q;
    cin >> q;
    while(q--)
    {
        int op, e, x;
        cin >> op >> e >> x, e--;
        int a = edge[e].first, b = edge[e].second;
        if(op == 2)
            swap(a, b);
        if(in[a] < in[b])
        {
            update(1, 1, n, 1, n, x);
            update(1, 1, n, in[b], out[b], -x);
        }
        else
            update(1, 1, n, in[a], out[a], x);
    }
    for(int i = 1; i <= n; i++)
        cout << query(1, 1, n, in[i]) << endl;
    return 0;
}

#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"

using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vpi vector<pi>
#define fr(i, a, b) \
    for (auto i = (a); i != (b); i++)
typedef long long ll;
typedef pair<int, int> pi;

template <class T>
class fenwick {
    int size;
    vector<T> arr, _arr, act;

   public:
    fenwick(int N) {
        size = N;
        arr.resize(N + 1, 0);
        act.resize(N + 1, 0);
        _arr.resize(N + 1, 0);
    }

    void add(int k, T x) {
        while (k <= size) {
            act[k] += x;
            k += k & -k;
        }
    }

    void _add(int k, T x) {
        while (k <= size) {
            arr[k] += x;
            k += k & -k;
        }
    }

    void __add(int k, T x) {
        while (k <= size) {
            _arr[k] += x;
            k += k & -k;
        }
    }

    T sum(int k) {
        int temp = k;
        T s = 0, t = 0;
        while (k >= 1) {
            s += act[k];
            k -= k & -k;
        }
        k = temp;
        while (k >= 1) {
            t += arr[k];
            k -= k & -k;
        }
        t *= temp;
        k = temp;
        while (k >= 1) {
            t -= _arr[k];
            k -= k & -k;
        }
        return s + t;
    }

    void range_add(int l, int r, T val) {
        _add(l, val);
        _add(r + 1, -val);
        __add(l, val * (l - 1));
        __add(r + 1, -val * r);
    }
};

int dfs(int node, vector<bool> &vis, vi &s, vector<vi> &a, vi &tra, vi &p) {
    tra.eb(node);
    int size = 1;
    vis[node] = true;
    for (int u : a[node]) {
        if (!vis[u]) {
            size += dfs(u, vis, s, a, tra, p);
            p[u] = node;
        }
    }
    s[node] = size;
    return size;
}

const ll mod = 1e9 + 7;

int32_t main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vpi a(n - 1);
    vector<vi> adj(n);

    fr(i, 0, n - 1) {
        cin >> a[i].F >> a[i].S;
        adj[a[i].S - 1].pb(a[i].F - 1);
        adj[a[i].F - 1].pb(a[i].S - 1);
        a[i].F--;
        a[i].S--;
    }

    vi s(n), p(n, -1);
    vi tra;
    vector<bool> vis(n, false);
    dfs(0, vis, s, adj, tra, p);
    fenwick<ll> f(n);
    vi ac(n);

    for (int i = 0; i < n; i++) {
        ac[tra[i]] = i + 1;
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        ll t, e, x;
        cin >> t >> e >> x;
        e--;
        if (t == 1) {
            int node = a[e].F;
            if (p[node] == a[e].S) {
                f.range_add(ac[node], ac[node] + s[node] - 1, x);
            } else {
                f.range_add(1, n, x);
                f.range_add(ac[a[e].S], ac[a[e].S] + s[a[e].S] - 1, -x);
            }
        } else {
            int node = a[e].S;
            if (p[node] == a[e].F) {
                f.range_add(ac[node], ac[node] + s[node] - 1, x);
            } else {
                f.range_add(1, n, x);
                f.range_add(ac[a[e].F], ac[a[e].F] + s[a[e].F] - 1, -x);
            }
        }
    }

    for (int i = 0; i < n; i++) {
        ll answer = f.sum(ac[i]) - f.sum(ac[i] - 1);
        cout << answer << "\n";
    }

    return 0;
}


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

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

int n, m, x, y;
map<int,vector<int>> black;

int main() {
    cin >> n >> m;
    rep(i,m) {
        cin >> x >> y;
        black[x].push_back(y);
    }

    set<int> possible{n};
    for (auto [x, ys] : black) {
        vector<int> add;

        for (auto y : ys) {
            if (possible.count(y+1)) add.push_back(y);
            if (possible.count(y-1)) add.push_back(y);
        }

        for (auto y : ys) possible.erase(y);
        for (auto y : add) possible.insert(y);
    }
    cout << possible.size() << "\n";
}

#include <bits/stdc++.h>
#include <bits/extc++.h>
#define double long double
#define rbtree __gnu_pbds::tree<int,__gnu_pbds::null_type,less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
#define int ll
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);
#define pb push_back
#define ALL(X) X.begin(),X.end()
#define F(i,n)  FF(i,0,n)
#define F1(i,n)  FF(i,1,n+1)
#define FF(i,n,m) for(int i=(int)n;i<(int)m;++i)
#ifndef LOCAL
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#else
#endif // LOCOL
//#define mp make_pair
using namespace std;
//using namespace __gnu_pbds;
template<typename T> bool remax(T& a, const T& b) {return b>a?a=b,1:0;}
template<typename T> bool remin(T& a, const T& b) {return b<a?a=b,1:0;}
inline ostream& operator << (ostream& os,__int128_t a){if(a==0) {return (os<<'0');}bool flag=0;if(a<0) {a=-a,flag=1;}string s;while(a){s+=a%10+'0';a/=10;}s+=flag?"-":"";reverse(ALL(s));return os<<s;}
inline istream& operator >>(istream& is,__int128_t& a){string s;cin>>s;a=0;for(auto c:s) a=a*__int128_t(10)+__int128_t(c-'0');return is;}
template<typename T,typename P> inline ostream& operator << (ostream& os,pair<T,P> a){os<<a.first<<" "<<a.second; return os;}
template<typename T,typename P> inline istream& operator >> (istream& is,pair<T,P> &a){is>>a.first>>a.second; return is;}
using ll=long long;
using ull=unsigned long long;
using int128= __int128_t;
using uint128= __uint128_t;
using pii =pair<int,int>;
const double pi=acos(-1);
const int N=800+5;
const ll M=1000000000;
const ll INF_64=0x3f3f3f3f3f3f3f3f;
const int INF_32=0x3f3f3f3f;
const int16_t INF_16=0x3f3f;
const int klog=20;
const int mod=998244353;//1E9+7
const double eps=1E-8;

void gen(){

}
void sol(){
    int n,m;
    cin>>n>>m;
    vector<pii> a(m);
    F(i,m){
        cin>>a[i].first>>a[i].second;
    }
    sort(ALL(a),[](auto&l,auto&r){return l.first<r.first;});
    map<int,int> dp;
    dp[n]=1;
    vector<pii> upd;
    F(t,m){
        if(t!=0&&a[t].first!=a[t-1].first){
            while(upd.size()){
                auto p=upd.back();upd.pop_back();
                dp[p.first]=p.second;
            }
        }
        int x=a[t].first,y=a[t].second;
        int tmp=0;
        if(y-1>=0) tmp+=dp[y-1];
        if(y+1<=2*n) tmp+=dp[y+1];
        upd.pb({y,tmp>0?1:0});
    }
    for(auto &p:upd){
        dp[p.first]=p.second;
    }
    upd.clear();
    int an=0;
    for(auto &[x,y]:dp) if(y!=0) an++;
    cout<<an<<"\n";
}
int32_t main(){
    #ifdef LOCAL
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    #endif // LOCAL
    IOS;
    int t=1;
    gen();
    //cin>>t;
    FF(i,1,t+1){
        //cout<<"Case #"<<i<<": ";
        sol();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define ff first
#define ss second
#define dd long double
#define all(x) x.begin(),x.end()
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
    os << "[";
    for (ll i=0; i < v.size(); ++i) {
        os << v[i];
        if (i != v.size() - 1)
            os << ", ";
    }
    os << "]";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v){
    os << "{";
    for (auto it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}";
    return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v){
    os<<"{";
    for (auto it : v){
        os << it.first << " : "
           << it.second ;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}";
    return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v){
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    return os;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vector<ll> arr(n);
        REP(i,n){
            cin>>arr[i];
            arr[i]--;
        }
       // cout<<arr<<endl;
        if(n==2){
            if(arr[0]==0){
                cout<<0<<endl;
            }
            else{
                cout<<1<<endl;
                cout<<1<<endl;
            }
            continue;
        }
        ll turn=0;
        vector<ll> ans;
        for(ll elem=n-1;elem>2;elem--){
            ll pos;
            for(ll i=0;i<=elem;i++){
                if(arr[i]==elem){
                    pos=i;
                    break;
                }
            }
            if(pos==elem) continue;
            if((pos & 1)==turn){
                for(ll i=pos;i<elem;i++){
                    ans.pb(i);
                    swap(arr[i],arr[i+1]);
                    turn^=1;
                }
            }
            else{
                ll ideal=-1;
                for(ll j=0;j<elem;j++){
                    if((j & 1)==turn){
                        if(j+1!=pos){
                            ideal=j;
                            break;
                        }
                    }
                }
                if(ideal!=-1){
                    ans.pb(ideal);
                    swap(arr[ideal],arr[ideal+1]);
                    turn^=1;
                    for(ll i=pos;i<elem;i++){
                        ans.pb(i);
                        swap(arr[i],arr[i+1]);
                        turn^=1;
                    }
                }
                else{
                    ans.pb(pos-1);
                    swap(arr[pos-1],arr[pos]);
                    turn^=1;
                    ans.pb(pos);
                    swap(arr[pos],arr[pos+1]);
                    turn^=1;
                    for(ll i=pos-1;i<elem;i++){
                        ans.pb(i);
                        swap(arr[i],arr[i+1]);
                        turn^=1;
                    }
                }
            }
        }
       // cout<<arr<<endl;
       // cout<<turn<<endl;
        vector<ll> aux(arr.begin(),arr.begin()+3);
        vector<ll> tar{0,1,2};
        while(aux!=tar){
            if(turn & 1){
                ans.pb(1);
                swap(aux[1],aux[2]);
                turn^=1;
            }
            else{
                ans.pb(0);
                swap(aux[0],aux[1]);
                turn^=1;
            }
        }
        cout<<ans.size()<<endl;
        for(auto & x:ans){
            cout<<x+1<<" ";
        }
        cout<<endl;

    }

}

#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define dcout cout<<fixed<<setprecision(15);
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long int  ll;
typedef pair<ll, ll> P;
constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;
template< typename S, typename T >
inline void chmax(S &a, const T &b) { if(a < b) a = b; }
template< typename S, typename T >
inline void chmin(S &a, const T &b) { if(a > b) a = b; }

ll gcd(ll x, ll y) {	if (x == 0) return y;	return gcd(y%x, x);}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
ll modpow(ll a, ll n, ll p) {	if(n==0) return (ll)1;  if (n == 1) return a % p;  if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p;  ll t = modpow(a, n / 2, p);  return (t * t) % p;}
ll modinv(ll a, ll m) {	if(m==0)return (ll)1;	ll b = m, u = 1, v = 0;	while (b) {		ll t = a / b;		a -= t * b; swap(a, b);		u -= t * v; swap(u, v);	}	u %= m;	if (u < 0) u += m;	return u;}
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {	fac[0] = fac[1] = 1;	finv[0] = finv[1] = 1;	inv[1] = 1;	for (int i = 2; i < MAX; i++) {		fac[i] = fac[i - 1] * i % MOD;		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;		finv[i] = finv[i - 1] * inv[i] % MOD;	}}
ll COM(ll n, ll k) {	if (n < k) return 0;	if (n < 0 || k < 0) return 0;	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;}
ll com(ll n,ll m){   if(n<m || n<=0 ||m<0){		return 0;	}	if( m==0 || n==m){		return 1;	}	ll k=1;	for(ll i=1;i<=m;i++){       k*=(n-i+1); 	   k%=MOD;	   k*=modinv(i,MOD);	   k%=MOD;	}	return k;}
ll rad(ll u, ll p){ ll cnt=0;	while(u%p==0){		u/=p;		cnt++;	}	return cnt;}
////////////////////////////////////////////////////////////////////

int main() {
 ios::sync_with_stdio(false);

ll tt;
cin>>tt;

ll num[550];
  ll dis[550];
for(int i=0;i<=501;i++){
	num[i]=i;
}
  for(int i=0;i<=500;i++){
    dis[i]=i;
  }
rep(qq,tt){
	ll cnt=0;
ll n;
cin>>n;

vector<ll> v;
rep(i,n){
	ll t;
	cin>>t;
	v.pb(t);
}

if(n==1){cout<<0<<endl;
continue;}
if(n==2){
	if(v[0]==1){cout<<0<<endl;}
	else{
		cout<<1<<endl;
		cout<<1<<endl;
	}
	continue;
}

for(ll i=0;i<n;i++){
num[i+1]=v[i];
 dis[v[i]]=i+1;
}

vector<ll> ans;

ll fin=0;
for(ll i=1;i<=n;i++){
if(num[i]==i && i!=n-2)continue;
if(i==n-2){

	while(fin==0){
		cnt++;
	if(cnt%2==n%2){
		ll u=num[n-2];
		     dis[num[n-1]]=n-2;
      dis[num[n-2]]=n-1;
		num[n-2]=num[n-1];
		num[n-1]=u;

		ans.pb(n-2);
	}
	else{
		ll u=num[n-1];
		  dis[num[n-1]]=n;
      dis[num[n]]=n-1;
		num[n-1]=num[n];
		num[n]=u;

		ans.pb(n-1);
	}
if(dis[n-2]==n-2 && dis[n-1]==n-1 && dis[n]==n){fin=1;}

}

}
if(fin==1)break;

while(num[i]!=i){
cnt++;

if(cnt%2==dis[i]%2){
	if(cnt%2==n%2){
		ll u=num[n-2];
		     dis[num[n-1]]=n-2;
      dis[num[n-2]]=n-1;
		num[n-2]=num[n-1];
		num[n-1]=u;

		ans.pb(n-2);
	}
	else{
		ll u=num[n-1];
		  dis[num[n-1]]=n;
      dis[num[n]]=n-1;
		num[n-1]=num[n];
		num[n]=u;

		ans.pb(n-1);
	}

}
else{
ll u=num[dis[i]];
  num[dis[i]]=num[dis[i]-1];
  num[dis[i]-1]=u;
  dis[i]--;
  dis[num[dis[i]+1]]++;

ans.pb(dis[i]);

}

}

}

cout<<cnt<<endl;
for(int i=1;i<ans.size();i++){
	cout<<ans[i-1]<<" ";
}
cout<<ans[cnt-1]<<endl;

}

return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  vector<int> vec(3);

  for (int i = 0; i < 3; ++i) {
    cin >> vec.at(i);
  }

  sort(vec.begin(), vec.end());

  if (vec.at(0) - vec.at(1) == vec.at(1) - vec.at(2)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

#include "bits/stdc++.h"
using namespace std;
// Hi
const double eps = 0.0000001;
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    long double r,x,y;
    cin>>r>>x>>y;
    long double d = sqrt(x*x+y*y);
    if(fabs(d-r) < eps){
        cout<< 1 << '\n';
    }else if(d <= r+r){
        cout << 2 << '\n';
    }else{
        cout << ceil(d/r) << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using str = string;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
#define ld long double;
#define REP(i,n) for(int i=0; i<int(n); i++)
#define RREP(i,n) for(int i=int(n)-1; i>=0; i--)
#define FOR(i,f,t) for(int i=int(f); i<=int(t); i++)
#define RFOR(i,f,t) for(int i=int(f); i>=int(t); i--)
#define ALL(vec) (vec).begin(),(vec).end()
#define SORT(vec) sort(ALL(vec))
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define PB(vec) push_back(vec);
#define YES(ans) if(ans) cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define Yes(ans) if(ans) cout<<"Yes"<<endl; else cout<<"No"<<endl;
#define yes(ans) if(ans) cout<<"yes"<<endl; else cout<<"no"<<endl;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const double PI = 3.1415926535897932;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if( c==1 ) a++;
    while(true){
        if( a==0 ){
            cout << "Aoki" << endl;
            break;
        }
        a--;
        if( b==0 ){
            cout << "Takahashi" << endl;
            break;
        }
        b--;
    }
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b,c;
  cin>>a>>b>>c;
  if(c==1){
    if(b>a){
      cout<<"Aoki";
    }
    else{
      cout<<"Takahashi";
    }
  }
  else{
    if(a>b){
      cout<<"Takahashi";
    }
    else{
      cout<<"Aoki";
    }
  }
  cout<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define M (L+R)/2
#define N 500009
#define inf 99999999999
#define ll long long
#define mod 1000000007
#define sz size();
using namespace std;

ll k,ans;

int main(){

	ios::sync_with_stdio(0);

	cin>>k;

	for(ll a=1;a<=sqrt(k);a++){
		for(ll b=a;b<=k;b++){
			int c = k/a/b;
			if(c<b)break;
			if(a==b && b==c){
				ans++;
				continue;
			}
			if(a==b){
				ans+=3*(c-b)+1;
				continue;
			}
			if(b==c){
				ans+=3;
				continue;
			}
			ans+=6*(c-b)+3;
		}
	}
	cout<<ans<<endl;

	return 0;
}

#include<bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
// using namespace atcoder;
using lint = long long;
using graph = vector<vector<int>>;
#define endl '\n'
lint const mod = 1e9+7;
//long const mod = 998244353;

int main(){
    int n;
    cin >> n;
    vector<pair<int,int>>ans(n);
    for(int i=1;i<=n;i++){
        cout << (i*2-1+n)%n + 1 << " " << (i*2)%n + 1 << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<n;i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=998244353;
constexpr ll INF=1e18;

int main(){
    ll n, m; cin >> n >> m;
    VI a(m); REP(i,m) cin >> a[i];
    sort(ALL(a));
    ll k=n, l=1;
    REP(i,m){
        if(a[i]-l!=0)
            k=min(k,a[i]-l);
        l=a[i]+1;
    }
    if(n+1-l!=0)
        k=min(k,n+1-l);
    ll ans=0;
    l=1;
    REP(i,m){
        if(a[i]-l!=0)
            ans+=(a[i]-l+k-1)/k;
        l=a[i]+1;
    }
    if(n+1-l!=0)
        ans+=(n+1-l+k-1)/k;
    cout << ans << endl;
    return 0;
}

#define _GLIBCXX_DEBUG
#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;

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

  stack<int> que;
  int cnt = 0;

  rep(i, 0, n){
    if(que.empty()){
      if(s[i] == 'f')
        que.push(0);
      continue;
    }

    if(s[i] == 'f'){
      que.push(0);
      continue;
    }
    if(s[i] == 'o'){
      if(que.top() == 0){
        que.pop();
        que.push(1);
      }
      else{
        stack<int> tmp;
        que = tmp;
      }
      continue;
    }
    if(s[i] == 'x'){
      if(que.top() == 1){
        que.pop();
        cnt++;
      }
      else{
        stack<int> tmp;
        que = tmp;
      }
      continue;
    }

    else{
      stack<int> tmp;
      que = tmp;
    }
  }

  cout << n-cnt*3 << endl;

  return 0;

}


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

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

#define int long long int

#define vi vector<int>

#define ff first
#define ss second

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

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

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

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

void test_case(){
    int n, q; cin>>n>>q;
    vi a(n); read(a);
    vi b(n); b[0] = a[0] - 1;
    for(int i = 1 ; i < n ; ++i){
        b[i] = a[i] - a[i - 1] - 1;
        b[i] += b[i - 1];
    }
    // show_arr(b);
    while(q--){
        int k; cin>>k;
        int id = lower_bound(all(b), k) - b.begin();
        int offset = (id - 1 >= 0)?a[id - 1]:0;
        int lst = (id - 1 >= 0)?b[id - 1]:0;
        cout << offset + (k - lst) << '\n';
    }

}

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

#include<iostream>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

struct modint{
  li num;

  modint(li a=0){
    num=(a%MOD+MOD)%MOD;
  }
  modint operator+(modint a) {return{(num+a.num)%MOD};}
  modint operator-(modint a) {return{((num-a.num)%MOD+MOD)%MOD};}
  modint operator*(modint a) {return{num*a.num%MOD};}
  modint& operator+=(modint a) {
    (num += a.num)%=MOD;
    return *this;
  }
  modint& operator-=(modint a) {
    (num += MOD-a.num)%=MOD;
    return *this;
  }
  modint& operator*=(modint a) {
    (num*=a.num)%=MOD;
    return *this;
  }
};

std::ostream& operator<<(std::ostream& os, const modint& m){
  // ここでストリームに obj を書き込みます。
  li a=m.num;
  a%=MOD;
  a+=MOD;
  a%=MOD;
  cout << a;
  return os;
}

std::istream& operator>>(std::istream& os,modint& m){
  // ここでストリームに obj を書き込みます。
  li a; cin >>a;
  m=a;
  return os;
}

modint modpow(modint a,long int r){
  modint b=a,ans=1;
  while(r){
    if(r%2) ans*=b;
    b*=b;
    r/=2;
  }
  return ans;
}

// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
};

struct node{
  int v;
  li w;
  node(int a, li b=1){v=a, w=b;;}
};

struct graph{
  int n,rho;
  int m;
  int weighted,origin,directed;
  li ma;

  vector<vector<node>> adj;
  vector<li> col,w;
  vector<int> uf;
  int find(int v){ return (uf[v]<0)?v:uf[v]=find(uf[v]); }
  int unite(int a,int b){
    a=find(a); b=find(b);
    if(a==b) return 0;
    rho--;
    if(a>b) swap(a,b);
    uf[a]+=uf[b];
    uf[b]=a;
    return 1;
  }
  int size(int a){ return -uf[find(a)];}
  graph(int nn,int mm,int d_flag=0,int w_flag=0,int ori=1){
    ma=0;
    n=nn; m=mm;
    weighted=w_flag;
    directed=d_flag;
    origin=ori;
    if(df){
      print(origin,"-origin");
      if(weighted) print("weighted");
      if(directed) print("directed");
    }
    adj.resize(n);
    uf.resize(n,-1);
    rho=n;
    rep(i,m){
      int a,b; li w=1; cin >>a >>b; a-=origin; b-=origin;
      if(weighted) cin >>w;
      //      if(df)print(w);
      adj[a].push_back({b,w});
      if(not directed)
        adj[b].push_back({a,w});
      unite(a,b);
      ma=max(ma,w);
    }
  }
  void add(int a,int b,li w=1){
      adj[a].push_back({b,w});
      if(not directed)
        adj[b].push_back({a,w});
      ma=max(ma,w);
      unite(a,b);
  }

  li dijkstra(int a,int b);
  li dijkstra(int b=-1){
    if(b==-1) b=n-1;
    return dijkstra(0,b);
  };

};

std::ostream& operator<<(std::ostream& os,const node& n){
  printf("%d[%ld]",n.v,n.w);
  return os;
};

std::ostream& operator<<(std::ostream& os,const graph& g){
  if(g.weighted){
    rep(i,g.n){
      printf("%d :",i+g.origin);
      for(auto p:g.adj[i]){
        printf(" %d[%ld]",p.v+g.origin,p.w);
      }
      print("");
    }
  }else{
    rep(i,g.n){
      printf("%d :",i+g.origin);
      for(auto p:g.adj[i]){
        printf(" %d",p.v+g.origin);
      }
      print("");
    }
  }
  return os;
};

void dfs(graph& g,vector<li>& cs,int a=0,int p=-1){
  for(auto x:g.adj[a]){
    if(x.v==p) continue;
    cs[x.v]=cs[a]^x.w;
    dfs(g,cs,x.v,a);
  }
}

int main(){
  int n; cin >>n;
  graph g(n,n-1,0,1);
  if(df)print(g);
  vector<li> cs(n,0);
  dfs(g,cs);
  if(df)print(cs);

  li b=1;
  modint ans=0;
  rep(i,60){
    modint one,zero;
    rep(t,n){
      ((b&cs[t])?one:zero)+=1;
    }
    if(df)print(one,zero,b);
    ans+=one*zero*b;
    b*=2;
  }
  print(ans);
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)

int N;
int P[555];

void MAIN() {
    scanf("%d", &N);
    REP (i, N) scanf("%d", P+i), P[i]--;

#ifdef LOCALx
    N = 500;
    REP (i, N) P[i] = i;
    //reverse(P, P+N);
    random_shuffle(P, P+N);
#endif

    VI ans;
    int t = 0;
    while (1) {
	int k = -1;
	bool sorted = true;
	for (int i=N-2; i>=0; i--) {
	    if (P[i] != i) {
		sorted = false;
	    }
	    if (P[i] > i && i % 2 == t % 2) {
		k = i;
		break;
	    }
	}

	if (sorted) {
	    break;
	}

	if (k == -1) {
	    if (t % 2 == 0) {
		ans.push_back(0);
		swap(P[0], P[1]);
	    } else {
		ans.push_back(1);
		swap(P[1], P[2]);
	    }
	    t++;
	} else {
	    while (k < N-1 && P[k] > k) {
		ans.push_back(k);
		swap(P[k], P[k+1]);
		k++;
		t++;
	    }
	}
    }

    printf("%d\n", (int)ans.size());
    REP (i, ans.size()) printf("%d%c", ans[i]+1, " \n"[i==(int)ans.size()-1]);
    assert((int)ans.size() <= N*N);
    REP (i, N) assert(P[i] == i);
}

int main() {
    int TC = 1;
    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <utility>
#include <map>
#define rush()      int T; while(cin>>T) while(T--)
#define ms(a,b)     memset(a,b,sizeof a)
#define lowbit(x)   ((x)&(-x))
#define inf         0x7f7f7f7fll
#define eps         1e-11
#define sd(a)       scanf("%d",&a)
#define sll(a)      scanf("%lld",&a)
#define sll2(a,b)   scanf("%lld%lld",&a,&b)
#define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define sdd(a,b)    scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a)       scanf("%lf",&a)
//#define sc(a)       scanf("%c\n",&a)
#define ss(a)       scanf("%s",a)
#define pd(a)       printf("%d\n",a)
#define pdd(a,b)    printf("%d %d\n",a,b)
#define pddd(a,b,c) printf("%d %d %d\n",a,b,c)
#define pll(a)      printf("%lld\n",a)
#define pf(a)       printf("%.1lf\n",a)
#define pc(a)       printf("%c",a)
#define ps(a)       printf("%s\n",a)
#define rep(i,a,b)  for(int i=(a);i<=(b);i++)
#define forn(i,a,b,x)  for(int i=(a);i<=(b);i+=(x))
#define per(i,a,b)  for(int i=(b);i>=(a);i--)
#define pb(a)       push_back(a)
#define mp(a,b)     make_pair(a,b)
#define debug(a)    cout<<#a<<" : "<<a<<" "<<" ; "
#define dbg(a)      cout<<#a<<" : "<<a<<endl
#define show(a,b)   cout<<a<<" :\t"<<b<<"\t*"<<endl
#define IOS         ios::sync_with_stdio(0); cin.tie(0)
#define PAUSE       system("pause")
#define all(x)      x.begin(),x.end()
//#define print(a)    cout<<a<<endl;continue;
#define fr          first
#define sc          second
#define ceils(a,b)  ((a-1ll+b)/(b))
#define floors(a,b) floor(double(a)/double(b))
using namespace std;
//inline void swap(int &x,int &y){x^=y^=x^=y;}
typedef long long ll;
//using ui=unsigned int;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double pi=acos(-1.0);
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int limit=5e5;

int read()
{
	int s=0;
	char c=getchar(),lc='+';
	while (c<'0'||'9'<c) lc=c,c=getchar();
	while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
	return lc=='-'?-s:s;
}

void write(int x)
{
	if (x<0) putchar('-'),x=-x;
	if (x<10) putchar(x+'0');
	else write(x/10),putchar(x%10+'0');
}

void print(int x,char c='\n')
{
	write(x);
	putchar(c);
}

ll mod=2147483648;
const int N=3e5+5;

    int n, m, _;
    int i, j, k;
    int a[N];
    int pos[N];
    vector<int> ans;

signed main()
{
    //IOS;
    rush(){
        sd(n);
        rep(i, 1, n) sd(a[i]), pos[a[i]] = i;
        int opt = 1;
        rep(i, 1, n){
            if(i == pos[i]) continue;
            if((pos[i] - 1) % 2 != opt){
                if(pos[i] - 2 >= i){
                    ans.pb(pos[i] - 2);
                    int x = pos[i] - 2, y = pos[i] - 1;
                    swap(pos[a[x]], pos[a[y]]);
                    swap(a[x], a[y]);
                    opt ^= 1;
                }
                else {
                //if(pos[i] == i + 1){
                    ans.pb(i - 1);
                    ans.pb(i);
                    ans.pb(i - 1);
                    ans.pb(i);
                    ans.pb(i - 1);
                    //ans.pb(i);
                    opt ^= 1;
                    int x = pos[i], y = pos[i] - 1;
                    swap(pos[a[x]], pos[a[y]]);
                    swap(a[x], a[y]);
                    continue;
                }
            }
            for(int j = pos[i] - 1; j >= i; j--){
                ans.pb(j);
                int x = j, y = j + 1;
                swap(pos[a[x]], pos[a[y]]);
                swap(a[x], a[y]);
                opt ^= 1;
            }
        }
        pd(ans.size());
        for(auto it : ans) printf("%d ", it);
        puts("");
        ans.clear();
    }
    //PAUSE;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;

#define LL long long
#define DB double
#define MAXN 200
#define Pr pair<int,int>
#define X first
#define Y second
#define INF 1000000001
#define mem(x,v) memset(x,v,sizeof(x))

LL read(){
    LL x=0,F=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*F;
}

int n,a[MAXN+5],b[MAXN+5],dp[MAXN+5],F;
int ta[MAXN+5],tb[MAXN+5];
int vis[MAXN+5];

bool check(int l,int r){
	int L=(r-l+1)/2;
	for(int i=1;i<=n;i++)ta[i]=a[i],tb[i]=b[i];
	for(int i=1;i<=2*n;i++)vis[i]=0;
	for(int i=1;i<=n;i++)
	if(ta[i]!=-1||tb[i]!=-1){
		if(ta[i]==-1){
			ta[i]=tb[i]-L;
			if(!(tb[i]>=l&&tb[i]<=r))continue;
		}
		else if(tb[i]==-1){
			tb[i]=ta[i]+L;
			if(!(ta[i]>=l&&ta[i]<=r))continue;
		}
		if((ta[i]>=l&&ta[i]<=r)^(tb[i]>=l&&tb[i]<=r))
		return 0;
		if((ta[i]>=l&&ta[i]<=r)&&(tb[i]>=l&&tb[i]<=r)){
			if(tb[i]-L!=ta[i])return 0;
			if(vis[ta[i]]||vis[tb[i]])return 0;
			vis[ta[i]]=vis[tb[i]]=1;
		}
	}
	return 1;
}

int main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read(),b[i]=read();
	dp[0]=1;
	for(int r=2;r<=2*n;r+=2)
		for(int l=r-1;l>=1;l-=2)
		if(dp[l-1]&&check(l,r)){dp[r]=1;break;}
	printf("%s",dp[2*n]?"Yes":"No");
}

#include <bits/stdc++.h>

#define ln                 '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 2e2;
bool f[nsz + 5];
char s[nsz + 5];
int n, fm[nsz + 5], l[nsz + 5], r[nsz + 5];

void inline exit_with(bool can) {
	printf(can ? "Yes\n" : "No\n");
	exit(0);
}

bool inline chk(int l, int r) {
	int md = (l + r) >> 1;
	circ (i, l, md) if (s[i] == ')') return 0;
	circ (i, md + 1, r) if (s[i] == '(') return 0;
	circ (i, l, md) if (fm[i] && ::r[fm[i]] != -1 && ::r[fm[i]] != i + md - l + 1) return 0;
	circ (i, md + 1, r) if (fm[i] && ::l[fm[i]] != -1 && ::l[fm[i]] != i - md + l - 1) return 0;
	circ (i, l, md) {
		int j = i + md - l + 1;
		if (fm[i] && fm[j] && fm[i] != fm[j]) return 0;
	}
	return 1;
}

int main() {
	scanf("%d", &n);
	cont (i, 2 * n) s[i] = '?';
	cont (i, n) {
		int l, r;
		scanf("%d%d", &l, &r);
		if ((l != -1 && s[l] != '?') || (r != -1 && s[r] != '?')) exit_with(0);
		if (l != -1) s[l] = '(';
		if (r != -1) s[r] = ')';
		::l[i] = l, ::r[i] = r;
		fm[l] = fm[r] = i;
	}
	f[0] = 1;
	cont (i, 2 * n) {
		for (int j = i - 1; j >= 0; --j) {
			if ((i - j) % 2 == 1) continue;
			f[i] |= f[j] & chk(j + 1, i);
		}
	}
	exit_with(f[2 * n]);
}


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

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define reset(x) memset(x,0,sizeof(x))
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define all(a) a.begin(),a.end()
#define PI 3.1415926535897932384626433832795
#define INF 1000000000
#define MOD 1000000007
#define MOD2 1000000009
#define EPS 1e-6
#define pb push_back
#define fi first
#define se second
#define mp make_pair

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

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	//freopen(".inp", "r", stdin);
    //freopen(".out", "w", stdout);
    vector<pii> total; // tong, so luong
    int n, k; cin >> n >> k;
    int dp[4][3000005] = {0};
    dp[0][0] = 1;
    for (int i = 0; i < 3; i++)
    {
    	for (int j = 0; j <= i * n; j++)
    	{
    		dp[i + 1][j + 1] += dp[i][j];
    		dp[i + 1][j + n + 1] -= dp[i][j];
    	}
    	for (int j = 1; j <= (i + 1) * n; j++)
    	{
    		dp[i + 1][j] += dp[i + 1][j - 1];
    	}
    }
    int x;
	for(int i=3;i<=3*n;i++){
	if(k<=dp[3][i]){x=i;break;}
	else{k-=dp[3][i];}
	}
	for(int i=1;i<=n;i++){
	int jmi=max(1LL,x-i-n);
	int jma=min(n,x-i-1);
	if(jmi>jma){continue;}
	if(k>(jma-jmi+1)){k-=(jma-jmi+1);continue;}
	int y=jmi+k-1;
	int z=x-i-y;
	cout << i << ' ' << y << ' ' << z << '\n';
	return 0;
	}
}


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

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

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

}

vector<vector<pair<int,int> > >adj[26];
int dis[1005][1005];
void dik(int so,int des)
{
    dis[so][des]=0;
    priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > >qq;

    qq.push({0,{so,des}});

    while(!qq.empty())
    {
        auto pp=qq.top();
        qq.pop();
        if(dis[pp.second.first][pp.second.second]<pp.first)
            continue;

        for(int i=0;i<26;i++)
        {
            for(auto j:adj[i][pp.second.first])
            {
                for(auto k:adj[i][pp.second.second])
                {
                    if(dis[j.first][k.first]>pp.first+j.second+k.second)
                    {
                        dis[j.first][k.first]=pp.first+j.second+k.second;
                        qq.push({dis[j.first][k.first],{j.first,k.first}});
                    }
                }
            }
        }
    }
}

signed main()
{

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

    int t = 1;
    // cin>>t;
    while(t--)
    {
        int n,m,so,des;
        cin>>n>>m; so = 1; des = n;
        for(int i=0;i<26;i++)
        {
            adj[i].clear();
            adj[i].resize(n+1);
        }

        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                dis[i][j]=1e17;
        char w;
        int x,y,z;
        vector<pair<int,pair<int,int> > >edge;
        for(int i=0;i<m;i++)
        {
            cin>>x>>y>>w;
          z=1;
            edge.push_back({z,{x,y}});
            adj[w-'a'][x].push_back({y,z});
            adj[w-'a'][y].push_back({x,z});
        }
        dik(so,des);

        int mn=1e17;
        for(auto i:edge)
        {
            mn=min(mn,i.first+dis[i.second.first][i.second.second]);
            mn=min(mn,i.first+dis[i.second.second][i.second.first]);
        }

        for(int i=1;i<=n;i++)
            mn=min(mn,dis[i][i]);
        if(mn==1e17)
            cout<<-1<<'\n';
        else
            cout<<mn<<'\n';

    }

}

#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
double pi=acos(-1);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
// #define mp make_pair
#define pq priority_queue
const ll mod=1000000007;
#define f first
#define s second
#define pii pair< ll, ll >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)
using namespace std;
void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
const int MAX=200005;
vector<int> adj[MAX];
int n,l;
void add_edge(int u, int v){
    adj[u].push_back(v);
    adj[v].push_back(u);
}
int timer;
vector<int> tin, tout;
vector<vector<int>> up;
ll dp[MAX],ans[MAX];
void dfs(int v, int p){
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i-1]][i-1];
    for (int u : adj[v])  if (u != p)   dfs(u, v);
    tout[v] = ++timer;
}

bool is_ancestor(int u, int v){ return tin[u] <= tin[v] && tout[u] >= tout[v]; }
pair<int,int> lca(int u, int v){
    if (is_ancestor(u, v)) return {1,u};
    if (is_ancestor(v, u)) return {2,v};
    for (int i = l; i >= 0; --i)  if (!is_ancestor(up[u][i], v)) u = up[u][i];
    return {3,up[u][0]};
}
void preprocess(int root) {
    tin.resize(n+1);
    tout.resize(n+1);
    timer = 0;
    l = ceil(log2(n));
    up.assign(n+1, vector<int>(l + 1));
    dfs(root, root);
}
void dfs2(int v,int p,ll rem){
  rem+= dp[v];
  ans[v]=rem;
  for(auto i:adj[v])  if(i!=p) dfs2(i,v,rem);
}
void solve(){

  cin>>n;
  pii x[n];
  rep(i,1,n){
    cin>>x[i].f>>x[i].s;
    add_edge(x[i].f,x[i].s);
  }
  preprocess(1);
  int q;
  cin>>q;
  while(q--){
    int tq,eq,xq;
    cin>>tq>>eq>>xq;
    int from,notto;
    if(tq==1){
      from=x[eq].f;
      notto=x[eq].s;
    }
    else{
      from=x[eq].s;
      notto=x[eq].f;
    }

    pii z=lca(from,notto);
    if(z.f==3){
      dp[1]+= xq;
      dp[notto]-=xq;
    }
    else if(z.f==1){
      dp[1]+=xq;
      dp[notto]-=xq;
    }
    else{
      int anc=notto,u=from;
      while(1){
        int flag=0;
        for(int i=l;i>=0;i--){
          if(up[u][i]!=anc && is_ancestor(up[u][i],anc)==anc){
            flag=1;
            u=up[u][i];
            break;
          }
        }
        if(flag==0) break;
      }
      dp[u]+=xq;
    }
  }
  dfs2(1,-1,0);
  rep(i,1,n+1) cout<<ans[i]<<'\n';
}

int main(){
    optimizeIO();
    solve();
}


#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>

using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vpl = std::vector<pll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
template<class T>
using vv = std::vector<std::vector<T> >;
using std::cout; using std::string;
using std::endl; using std::cin;
using std::transform; using std::toupper;

#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)

#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)
void chmin(ll& a, ll b){ if(a > b) a = b; }
void chmax(ll& a, ll b){ if(a < b) a = b; }

int main(void)
{
    // たてx, 横y

    ll N, M;
    cin >> N >> M;
    vl As(N), Bs(M);

    REP(i, N)
    {
        cin >> As[i];
    }

    REP(i, M)
    {
        cin >> Bs[i];
    }

    ll i = 0, j = 0;
    while(i < N && j < M)
    {
        if(As[i] == Bs[j])
        {
            i ++;
            j ++;
        }
        else if(As[i] < Bs[j])
        {
            cout << As[i] << " ";
            i++;
        }
        else
        {
            cout << Bs[j] << " ";
            j ++;
        }
    }

    if(i < N)
    {
        while(i<N)
        {
            cout << As[i] << " ";
            i++;
        }
    }
    else
    {
        while(j < M)
        {
            cout << Bs[j] << " ";
            j ++;
        }
    }

    cout << endl;

    return 0;
}

// ＵＴＦ−８

#include<bits/stdc++.h>
/* #include<atcoder/all> */
/* using namespace atcoder; */

using namespace std;
using ll = long long int;
using lc = complex<double>;

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

int main(void) {
    constexpr ll MOD = 1 ? 1e9+7 : 998244353;
    const double PI = acos(-1);
    constexpr double eps = 1e-10;
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    if(1) {
        ll n;
        cin >> n;
        vector<ll> a(n), b(n), p(n), rp(n);
        for(auto &e: a) cin >> e;
        for(auto &e: b) cin >> e;
        for(auto &e: p) cin >> e, e--;

        for(ll i=0; i<n; i++)
            rp[p[i]] = i;

        vector<tuple<ll,ll>> aa(n);
        for(ll i=0; i<n; i++)
            aa[i] = {a[i], i};
        sort(begin(aa), end(aa));

        vector<tuple<ll,ll>> res;
        for(ll i=0; i<n; i++) {
            auto [_, j] = aa[i];
            if(p[j] == j) continue;
            ll k = rp[j];
            if(a[j] <= b[p[j]]) {
                cout << -1 << endl;
                return 0;
            }
            if(a[k] <= b[p[k]]) {
                cout << -1 << endl;
                return 0;
            }
            swap(rp[p[j]], rp[p[k]]);
            swap(p[j], p[k]);
            res.push_back({j, k});
        }

        cout << res.size() << endl;
        for(auto [x, y]: res)
            cout << x+1 << ' ' << y+1 << endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define nl "\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db long double
#define mod 1e9 + 7
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf INT_MAX
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test lli t; cin>>t; while(t--)
int modInverse(int a, int m)
{
    int m0 = m;
    int y = 0, x = 1;

    if (m == 1)
      return 0;

    while (a > 1)
    {
        int q = a / m;
        int t = m;

        m = a % m, a = t;
        t = y;

        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;

    return x;
}

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

	ios_base::sync_with_stdio(false);
	cin.tie(NULL;
	*/
	lli n;
	cin>>n;
	cout<<n-1<<nl;
}

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

int main() {
  int N;
  cin >> N;
  N--;
  cout << N << endl;
}


    #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    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; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    using P=pair<int,int>;
    const ll INF=1e18;
    const int mod=998244353;

    int main(){
      int n;
      cin>>n;
      vector<int>cnt(200005);
      int j=0;
      rep(i,n){
        int p;
        cin>>p;
        cnt[p]++;
        while(j<=200000&&cnt[j]){j++;}
        cout<<j<<endl;
      }
      return 0;
    }

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

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

  int ans = 0;
  map<int, bool> m;
  for (int i=0; i<N; i++) {
    int p;
    cin >> p;
    m[p] = true;
    for (int j=ans; j<=200000; j++) {
      if (!m.count(j)) {
        cout << j << endl;
        ans = j;
        break;
      }
    }
  }
}


#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             998244353
#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;

void solve()
{

		int n;
		cin >> n;

		int temp = n;
		int cnt = 0;

		vi s;

		while(temp)
			cnt++,s.pb(temp%10),temp/=10;

		int ans  = 0;

		for(int i = 2;i < cnt; i += 2)
		{
				int curr = 9;

				for(int j = 1;j < i/2; j++)
					curr*=10;

				ans += curr;

		}

		if(cnt&1)
		{
				db(ans)
				return;
		}

		reverse(all(s));

		for(int i = 0;i < (cnt/2); i++)
		{
				int curr = 0;

                if(i == 0)
				curr = max(s[i] - 1,0ll);
			    else
			    curr = s[i];

				for(int j = i + 1;j < (cnt/2) ;j++)
					curr*=10;

				ans += curr;
		}

        int f = 1;

		for(int i = 0;i < cnt/2 ;i++)
		{
				if(s[i] > s[cnt/2 + i])
					f = 0;
				else if(s[i] < s[cnt/2 + i])
					break;
		}

		db(ans + f)

}

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

// KEEP CALM and Enjoy the Problem B-)
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define mod 1000000007 // 1e9+7
#define inf 1e18

#define watch(x) cout<<(#x)<<" is "<<(x)<<"\n"
#define watch2(x,y) cout<<(#x)<<" is "<<(x)<<" and "<<(#y)<< " is " <<(y)<<"\n"

struct custom_hash {
    static uint64_t splitmix64(uint64_t x){
            // http://xorshift.di.unimi.it/splitmix64.c
            x += 0x9e3779b97f4a7c15;
            x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
            x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
            return x ^ (x >> 31);
    }

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

ll integer_power(ll x, ll y){
// integer_func() funcs in O(log y) time. Just WOW!
/* Take Care:
 MUST Remove modulo, if in Problem, dealing with integers >= 10^9*/

    ll res = 1;     // res -> result

    if (x == 0)
        return 0;

    while (y > 0){
        if (y & 1)
            res = (res*x);

        y = y>>1; // y = y/2
        x = (x*x);
    }
    return res;
}

ll count_no_of_digit(ll N){

    ll count;

    count = 0;
    while(N > 0){
        count += 1;
        N /= 10;
    }

    return count;
}

void run_case(){

    ll N,n,count_digit,i,ans,left,right;
    ll current_num;

    cin>>N;

    count_digit = count_no_of_digit(N);
    //watch(count_digit);

    ans = 0;

    n = 2;
    while(n < count_digit){
        ans += 9 * integer_power(10,(n/2 - 1));
        n += 2;
    }

    if(n == count_digit){
        // n is even

        left = N / (integer_power(10,n/2));
        right = N % (integer_power(10,n/2));

        i = integer_power(10,(n/2-1));
        while(1){

            current_num = i*integer_power(10,n/2) + i;
            //watch(current_num);

            if(current_num > N)
                break;

            ans += 1;
            i += 1;
        }
    }

    cout<<ans<<"\n";

}

int main(){

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

    ll T = 1;
    //cin>>T;
    while(T--)
        run_case();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
#define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin());
const ll mod=1e9+7;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=clock();

    vector<ll> a(4);
    for(ll i=0;i<4;i++)
        cin>>a[i];
    sort(a.begin(),a.end(),greater<ll>());
    if(a[0]+a[2]==a[1]+a[3] || a[0]+a[1]==a[2]+a[3] || a[0]+a[3]==a[1]+a[2] || a[0]==a[1]+a[2]+a[3])
        cout<<"Yes";
    else
        cout<<"No";

    cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
    T = clock();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mod 1000000007
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,start,end) for(int i=start; i<=end; i++)
#define repd(i,start,end) for(int i=start; i>=end; i--)
#define all(x) x.begin(),x.end()
#define debug(x) cout << "(" << #x << ": " << x << ")" << endl;
void solve() {
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    if(a==b+c+d || a+b==c+d || a+c==b+d || c==a+b+d || a+b+c==d || b+c==a+d || b==a+c+d)
    {
        cout<<"Yes"<<endl;
    }
    else
    {
        cout<<"No"<<endl;
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define even(x) (x % 2 == 0 ? true : false)
#define odd(x) (x % 2 ? true : false)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;

//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;
//const int mod = 1000000007;
//vector<vector<int>> dp(m, vector<int>(n));

void 	dfs(int p, vector<vector<int>>& paths, vector<bool>& visited)
{
	if (visited[p]) return ;

	visited[p] = true;
	for (int path : paths[p])
		dfs(path, paths, visited);
}

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

	vector<int> a(m), b(m);
	vector<vector<int>> paths(n);
	int ans = 0;

	while (m--) {
		int a, b;
		cin >> a >> b;
		paths[a - 1].push_back(b - 1);
	}

	for (int i = 0; i < n; i++) {
		vector<bool> visited(n);

		dfs(i, paths, visited);
		ans += accumulate(all(visited), 0);
	}

	cout << ans << endl;
}

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

	solve();
	return 0;
}

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

#define M 1000000007
#define FOR(i, s, n) for (ll i = (s); i < (n); ++i)
#define FORALL(i, n) for (ll i = 0; i < (n); ++i)
#define MOD 1000000007
#define ff first
#define ss second
#define pll pair<long long, long long>
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(false)
#define countbits(x) __builtin_popcount(x)
#define pb(x) push_back((x))
#define vll vector<ll>
#define all(x) x.begin(),x.end()
#define print(x) cout << x << '\n';
#define debug print("reaching\n")
#define mll map<ll,ll>
#define lsb(i) i&(-i)
#define printv(x) \
	FORALL(i, x.size()) \
	{ \
		cout << x[i] << ' '; \
	} \
	cout << endl \
;
#define printm(m) \
	for(auto i: m){\
		cout<<i.ff<<' '<<i.ss<<endl; \
	}\
;

template <typename T>
T extGcd(T a, T b, T &x, T &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	else
	{
		int g = extGcd(b, a % b, y, x);
		y -= a / b * x;
		return g;
	}
}

template <typename T>
T modInv(T a, T m)
{
	T x, y;
	extGcd(a, m, x, y);
	return (x % m + m) % m;
}

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

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

	int t=1;
	// cin>>t;
	ll tot=t;
	while(t--){
		ll n,k;
		cin>>n>>k;
		ll x;
		ll ans = 0;
		FOR(y, 2, 2*n+1){
			ll num = k + y;
			if(num < 2) continue;
			if(num > 2*n) continue;
			ll mid = n + 1;
			// has n ways
			ll dist = abs(num  - mid);
			ll dist2 = abs(y - mid);
			ans += (n - dist) * (n - dist2);
		}
		print(ans);
	}

	return 0;
}

/*

*/


/* A */
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using pii = pair<int, int>;

const int N = 3;
int n, m, a[N][N];

signed main(int argc, char** argv)
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
	//	freopen("output.txt", "w", stdout);
	#endif
	for (int k = 1; k <= 2; ++k) {
		for (int i = 1; i <= 2; ++i) cin >> a[k][i];
	}
	cout << a[1][1] * a[2][2] - a[1][2] * a[2][1] << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF=1e17;
const int N=1e5+3,M=1e5+3;
template <typename T>
inline void read(T &x){
  x=0;char ch=getchar();bool f=false;
  while(!isdigit(ch)) f|=ch=='-',ch=getchar();
  while(isdigit(ch))  x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
  x=f?-x:x;
}
template <typename T>
inline void print(T x){
  if(x<0) putchar('-'),x=-x;
  if(x>9) print(x/10);
  putchar(x%10^48);
}
int n,m,st,ed;
ll k[M<<1],t[M<<1];
int head[N],to[M<<1],Next[M<<1],tot;
ll dist[N];
priority_queue<pair<ll,int> > pq;
bool vis[N];
inline void Addedge(int u,int v,ll w1,ll w2){to[++tot]=v,Next[tot]=head[u],head[u]=tot,k[tot]=w2,t[tot]=w1;return;}
inline ll DJ(){
  for(register int i=1;i<=n;++i)  dist[i]=INF;
  dist[st]=0;
  pq.push(make_pair(0,st));
  while(!pq.empty()){
    int x=pq.top().second;pq.pop();
    if(vis[x])  continue;
    vis[x]=true;
    if(x==ed) return dist[ed];
    for(register int i=head[x];i;i=Next[i]){
      int y=to[i];
      if(dist[x]%k[i]==0){
        if(dist[y]>dist[x]+t[i]){
          dist[y]=dist[x]+t[i];
          pq.push(make_pair(-dist[y],y));
        }
      }
      else{
        ll tep=(dist[x]/k[i]+1)*k[i];
        if(dist[y]>tep+t[i]){
          dist[y]=tep+t[i];
          pq.push(make_pair(-dist[y],y));
        }
      }
    }
  }
  return -1;
}
int main(){
  read(n),read(m),read(st),read(ed);
  for(register int i=1;i<=m;++i){
    int u,v;ll w1,w2;
    read(u),read(v),read(w1),read(w2);
    Addedge(u,v,w1,w2);Addedge(v,u,w1,w2);
  }
  print(DJ());
  return 0;
}

#pragma warning(disable:4996)
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <queue>
#define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ln '\n'
#define rp ' '
typedef long long ll;
typedef long double lf;
using namespace std;
typedef struct Edge
{
	ll e, c, k;
};
typedef struct Node
{
	ll e, c;
	bool operator<(const Node tmp) const
	{
		return c > tmp.c;
	}
};
ll n, m, x, y;
ll dist[100100];
ll INF = 1e18;
priority_queue<Node> pq;
vector<Edge> map[100100];
void dijkstra()
{
	while (!pq.empty())
	{
		Node now = pq.top();
		pq.pop();
		if (now.c > dist[now.e]) continue;
		for (auto i : map[now.e])
		{
			if (dist[now.e] % i.k)
			{
				if (dist[i.e] > dist[now.e] + i.c + i.k - dist[now.e] % i.k)
				{
					dist[i.e] = dist[now.e] + i.c + i.k - dist[now.e] % i.k;
					pq.push({ i.e, dist[i.e] });
				}
			}
			else
			{
				if (dist[i.e] > dist[now.e] + i.c)
				{
					dist[i.e] = dist[now.e] + i.c;
					pq.push({ i.e, dist[i.e] });
				}
			}
		}
	}
}
int main()
{
	fastio;
	cin >> n >> m >> x >> y;
	for (ll i = 1; i <= m; i++)
	{
		ll a, b, t, k;
		cin >> a >> b >> t >> k;
		map[a].push_back({ b, t, k });
		map[b].push_back({ a, t, k });
	}
	fill(dist, dist + n + 1, INF);
	dist[x] = 0;
	pq.push({ x, 0 });
	dijkstra();
	if (dist[y] == INF) cout << -1;
	else cout << dist[y];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<vector<vector<long double> > > vvvld;
typedef vector<vector<long double> > vvld;
typedef vector<vector<vector<int> > > vvvi;
typedef vector<vector<int> > vvi;
#define rep(i,n) for (int  i = 0; i< (int)(n); i++)
int kazu(int x, string s){
  int ans =0;
  int now = 0;
  bool y = false;
  rep(i,s.size()){
    if(s.at(i)=='.') now++;
    if(s.at(i)=='#'){
      if(y){
        y = false;
        ans = now;
        break;
      }
      else{
        now = 0;
      }
    }
    if(i == x && s.at(i) != '#') y = true;
  }
  if(y) ans = now;
  return ans;
}
int main(){
  int h,w,x,y;
  cin>>h>>w>>x>>y;
  vector<string> s(h);
  rep(i,h) cin>>s.at(i);
  string tate;
  rep(i,h){
    tate += s.at(i).at(y-1);
  }
  int ans=kazu(y-1,s.at(x-1)) + kazu(x-1, tate);
  if(ans>0) ans--;
  cout<<ans<<endl;
  //cout << fixed;
  //cout << setprecision(10)<< << endl;
}


//1階から決めると色々決まるため、DPができる.
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n;
int a[100], b[100];
int idA[200];
int idB[200];
bool dp[201];

bool check(int i, int j) {
	int k;

	//[i, i + j) -- [i + j, i + 2j)
	for (k = 0; k < 2 * j; k++) {
		if (idA[i + k] != -1) {
			if (k >= j) { return false; }

			if (b[idA[i + k]] != -1) {
				if (idB[i + k + j] != idA[i + k]) {
					return false;
				}
			}
			else {
				if (idB[i + k + j] != -1) {
					return false;
				}
			}
		}
		if (idB[i + k] != -1) {
			if (k < j) { return false; }

			if (a[idB[i + k]] != -1) {
				if (idA[i + k - j] != idB[i + k]) {
					return false;
				}
			}
			else {
				if (idA[i + k - j] != -1) {
					return false;
				}
			}
		}
	}
	return true;
}

int main() {
	int i, j;

	cin >> n;

	vector<int> abSet;
	rep(i, n) {
		cin >> a[i] >> b[i];
		if (a[i] != -1) a[i]--;
		if (b[i] != -1) b[i]--;
		if (a[i] != -1 && b[i] != -1 && a[i] >= b[i]) {	//a[i] < b[i]
			cout << "No" << endl;
			return 0;
		}
		if (a[i] != -1) abSet.push_back(a[i]);
		if (b[i] != -1) abSet.push_back(b[i]);
	}
	sort(abSet.begin(), abSet.end());
	rep(i, (int)abSet.size() - 1) {
		if (abSet[i] == abSet[i + 1]) {	//distinct
			cout << "No" << endl;
			return 0;
		}
	}

	rep(i, 2 * n) idA[i] = idB[i] = -1;
	rep(i, n) if (a[i] != -1) idA[a[i]] = i;
	rep(i, n) if (b[i] != -1) idB[b[i]] = i;

	dp[0] = true;
	rep(i, 2 * n) {
		if (dp[i] == false) continue;
		for (j = 1; i + 2 * j <= 2 * n; j++) {
			if (check(i, j)) {
				dp[i + 2 * j] = true;
			}
		}
	}

	if (dp[2 * n]) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

// AUTHOR: Sumit Prajapati
#include <bits/stdc++.h>
using namespace std;

#define ull                 unsigned long long
#define ll                  long long
#define pii                 pair<int, int>
#define pll                 pair<ll, ll>
#define pb                  push_back
#define mk                  make_pair
#define ff                  first
#define ss                  second
#define all(a)              a.begin(),a.end()
#define trav(x,v)           for(auto &x:v)
#define debug(x)            cerr<<#x<<" = "<<x<<'\n'
#define llrand()            distribution(generator)
#define rep(i,n)            for(int i=0;i<n;i++)
#define repe(i,n)           for(int i=1;i<=n;i++)
#define FOR(i,a,b)          for(int i=a;i<=b;i++)
#define printar(a,s,e)      FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n'
#define endl                '\n'
#define curtime             chrono::high_resolution_clock::now()
#define timedif(start,end)  chrono::duration_cast<chrono::nanoseconds>(end - start).count()
#define MD                  1'000'000'007
#define MDL                 998244353
#define MX                  100'005

const ll INF=1e16;
auto time0 = curtime;
random_device rd;
default_random_engine generator(rd());
uniform_int_distribution<ull> distribution(0,0xFFFFFFFFFFFFFFFF);

//Is testcase present?
ll minimum_dif(vector<ll>& a,vector<ll>& b){
    ll res=INF;
    if(a.size()==0)
        return INF;
    if(b.size()==0)
        return INF;
    for(auto &el:b){
        ll l=0,r=a.size()-1;
        ll mid,ind=r;
        while(l<=r){
            mid=(l+r)>>1;
            if(a[mid]>=el){
                r=mid-1;
                ind=mid;
            }
            else
                l=mid+1;
        }
        res=min(res,abs(el-a[ind]));
        if(ind>0)
            res=min(res,abs(el-a[ind-1]));
        if(ind<(a.size()-1))
            res=min(res,abs(el-a[ind+1]));
    }
    return res;
}

void solve(){

    ll n;cin>>n;
    vector<ll>R,G,B;
    ll x;
    char ch;
    repe(i,2*n){
        cin>>x>>ch;
        if(ch=='R')
            R.pb(x);
        else if(ch=='G')
            G.pb(x);
        else
            B.pb(x);
    }
    sort(all(R));
    sort(all(G));
    sort(all(B));
    ll sz1=R.size(),sz2=G.size(),sz3=B.size();
    if(sz1%2==0 && sz2%2==0 && sz3%2==0){
        cout<<"0";
        return;
    }
    // cout<<sz1<<" "<<sz2<<" "<<sz3<<'\n';
    // return;
    assert(((sz1%2)+(sz2%2)+(sz3%2))==2);
    ll res=INF;
    ll GB=minimum_dif(B,G),RB=minimum_dif(R,B),RG=minimum_dif(R,G);
    if(sz1%2==0){
        res=min(GB,RB+RG);
    }
    else if(sz2%2==0){
        res=min(RB,RG+GB);
    }
    else{
        res=min(RG,RB+GB);
    }

    cout<<res;

}

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

    time0 = curtime;

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

    //cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n";
    return 0;

}


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

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define st first
#define nd second
#define endl '\n' //toggle off for interactive problems
#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << e << ' '; cerr << '\n'; //vector, set
#define what_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << x[i] << ' '; cerr <<  '\n'; //n first element of array
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x) cerr << #x << " is "; for(auto&e: (x)) cerr << cerr_pair(e) << ' '; cerr << '\n'; //vector, set
#define pwhat_is_a(x, n) cerr << #x << " is "; for(int i=0;i<n;i++) cerr << cerr_pair(x[i]) << ' '; cerr <<  '\n'; //n first element of array
// #define int long long
// #define pow mypow

typedef pair<int,int> pii;
const int N = 2e3+5;
const int INF = 1e9+5;
const int MOD = 1e9+7;

int n, m;
char a[N][N];
int d[N][N];
bool vis[N][N], visc[26];
vector<pii> g[26];
queue<pii> q;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
int gx, gy;

int32_t main()
{
	IOS
	// freopen("input.txt", "r", stdin);
	cin >> n >> m;
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			cin >> a[i][j];
			if (a[i][j] == 'S') {
				vis[i][j] = true;
				q.push({i, j});
			} else if (a[i][j] == 'G') {
				gx = i;
				gy = j;
			} else if (a[i][j] != '.' && a[i][j] != '#') {
				g[a[i][j]-'a'].push_back({i, j});
			}
		}
	}
	while(!q.empty()){
		auto [x, y] = q.front(); q.pop();
		for (int i=0; i<4; i++) {
			int nx = x + di[i];
			int ny = y + dj[i];
			if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
			if (vis[nx][ny] || a[nx][ny] == '#') continue;
			d[nx][ny] = d[x][y] + 1;
			vis[nx][ny] = true;
			q.push({nx, ny});
		}
		if (a[x][y] == 'G') break;
		if (a[x][y] != '.' && a[x][y] != '#' && !visc[a[x][y]-'a']) {
			visc[a[x][y]-'a'] = true;
			for(auto [nx, ny]:g[a[x][y]-'a']) {
				if (vis[nx][ny]) continue;
				d[nx][ny] = d[x][y] + 1;
				vis[nx][ny] = true;
				q.push({nx, ny});
			}
		}

	}
	int ans = d[gx][gy];
	cout << ((ans)?ans:-1) << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(c) c.begin(), c.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep3(i, a, b) for (int i = a; i >= b; --i)
#define endl '\n'
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void yes(bool t = 1) { cout << yesno[t] << endl; }

template <class T, class S>
inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S>
inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
#define INT(...)     \
    int __VA_ARGS__; \
    IN(__VA_ARGS__)
#define LL(...)     \
    ll __VA_ARGS__; \
    IN(__VA_ARGS__)
#define VEC(type, name, size) \
    vector<type> name(size);  \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                       \
    vector<vector<type>> name(h, vector<type>(w)); \
    IN(name)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S>
void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T>
void scan(vector<T> &);
template <class T>
void scan(vector<T> &a) {
    for (auto &i : a)
        scan(i);
}
template <class T>
void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail>
void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
template <class T>
vector<T> divisor(T x) {
    vector<T> ans;
    for (T i = 1; i * i <= x; i++)
        if (x % i == 0) {
            ans.pb(i);
            if (i * i != x)
                ans.pb(x / i);
        }
    return ans;
}
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// constexpr int inf = 1e9;
// constexpr i64 inf = 1e18;
// const int N = 500 * 1000 + 5; // use for N <= 5 * 10^5
// const int MX = 1e9 + 7; // For convenience, find the answer modulo 10^9+7
// cout << rng();
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    INT(h, w);
    vector<string> s(h);
    rep(i, h) {
        cin >> s[i];
    }
    int ans = 0;
    rep2(i, 1, h - 2) {
        rep2(j, 1, w - 2) {
            if (s[i][j] == '#') {
                // for R and L
                if (s[i][j - 1] == '.') {
                    ans += 1;
                    if (s[i - 1][j] == '#' and s[i - 1][j - 1] == '.') {
                        ans -= 1;
                    }
                }
                if (s[i][j + 1] == '.') {
                    ans += 1;
                    if (s[i - 1][j] == '#' and s[i - 1][j + 1] == '.') {
                        ans -= 1;
                    }
                }
                if (s[i - 1][j] == '.') {
                    ans += 1;
                    if (s[i][j - 1] == '#' and s[i - 1][j - 1] == '.') {
                        ans -= 1;
                    }
                }
                if (s[i + 1][j] == '.') {
                    ans += 1;
                    if (s[i][j - 1] == '#' and s[i + 1][j - 1] == '.') {
                        ans -= 1;
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int mod = 1e9 + 7;

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    for(int i = 0; i < v.size(); ++i) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

void solve() {

	ll H, W;
    cin >> H >> W;

    vector<string> grid(H);

    for(string &row: grid) {
        cin >> row;
    }

    vector<vector<char>> c = vector<vector<char>>(H, vector<char>(W, '.'));

    ll ans = 0;

    for(ll i = 0; i < H - 1; ++i) {
        for(ll j = 0; j < W - 1; ++j) {
            ll count = 0;
            if(grid[i][j] == grid[i+1][j]) count++;
            if(grid[i][j] == grid[i][j+1]) count++;
            if(grid[i][j] == grid[i+1][j+1]) count++;

            if(count == 0 || count == 2) {
                ans++;
                c[i][j] = 'C';
            }

        }
    }

    // for(auto r: c) {
    //     for(char x: r) cout << x << "";
    //     cout << "\n";
    // }

    cout << ans;
}

signed main() {

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

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

	while(t--)
		solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define s(x, n) memset(x, n, sizeof(x));
#define pb(x) push_back(x)
#define NIL -1
#define MAXN 11

void solve()
{
    ll n, a, b;
    cin >> n >> a >> b;
    if (a + b > n)
        cout << "0\n";
    else
    {
        ll temp = (n - a - b) % mod;
        ll ans = (temp + 1) % mod * (temp + 2) % mod * (2 * (n - a + 1) % mod * (n - b + 1) % mod - (temp + 1) % mod * (temp + 2) % mod + mod) % mod;
        cout << ans << "\n";
    }
}
int main()
{
#ifdef WIN32
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

#ifndef WIN32
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#endif
    int t;
    t = 1;
    cin >> t;
    for (int I = 1; I <= t; I++)
    {
        solve();
    }
    return 0;
}

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

int main() {
    ll t;
    const ll MOD = 1e9+7;
    std::cin >> t;
    for (int i = 0; i < t; i++) {
        ll n,a,b;
        std::cin >> n>>a>>b;
        if(a>b)swap(a,b);

        ll ma = min(a-1,n-b);
        ll mm = 0;

        mm += 2*ma*(1 - 2*b - ma + 2*n)%MOD*(1 - b + n)%MOD*(b-a+1);
        // std::cout << mm << std::endl;
        mm += ma*ma%MOD*(-2*b-ma+2*n+1)%MOD*(-2*b-ma+2*n+1)%MOD;
        mm += (b-a+1)%MOD*(b-a+1)%MOD*(n-b+1)%MOD*(n-b+1)%MOD;
        mm %= MOD;
        // std::cout << (n-a+1)*(n-a+1)*(n-b+1)*(n-b+1) << std::endl;
        // std::cout << mm << std::endl;

        std::cout << ((n-a+1)%MOD*(n-a+1)%MOD*(n-b+1)%MOD*(n-b+1)%MOD-mm+MOD)%MOD << std::endl;
    }
}


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

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

#define all(x) x.begin(), x.end()
const int N = 2e5 + 5;
vector<int> E[N];
int dep[N], ans[N], mx[N], t;
void dfs(int u, int pre){
	mx[u] = dep[u];
	for(int& v: E[u]){
		if(v == pre) continue;
		dep[v] = dep[u] + 1;
		dfs(v, u);
		mx[u] = max(mx[u], mx[v]);
	}
}
void dfs1(int u, int pre){
	sort(all(E[u]), [&](int x, int y){
		return mx[x] < mx[y];
	});
	ans[u] = ++t;
	for(int& v: E[u]){
		if(v != pre) dfs1(v, u);
	}
	++t;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	dfs(1, 0);
	int x = 1;
	for(int i = 1; i <= n; i++) if(dep[x] < dep[i]) x = i;
	dep[x] = 0;
	dfs(x, 0);
	dfs1(x, 0);
	for(int i = 1; i <= n; i++) cout << ans[i] << ' ';
	return 0;
}

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
int qmax(int a, int b){
	return (a > b) ? a : b;
}

int n;
vector<int> v[200200];
vector<int> ord;
int ans[200200];
int nowans = 1;
int pos[200200];
int dept[200200];
int mx_dept[200200];
bool cmp(int a, int b){
	return (mx_dept[a] < mx_dept[b]);
}
void get_dept(int now, int par){
	mx_dept[now] = dept[now];
	rep(i, (int)v[now].size()){
		if(v[now][i] != par){
			dept[v[now][i]] = dept[now] + 1;
			get_dept(v[now][i], now);
			mx_dept[now] = qmax(mx_dept[now], mx_dept[v[now][i]]);
		}
	}
}
int rt;

void dfs(int now, int par){
	ans[(int)ord.size()] = nowans;
	ord.push_back(now);
	sort(v[now].begin(), v[now].end(), cmp);
	rep(i, (int)v[now].size()){
		if(v[now][i] != par){
			nowans++;
			dfs(v[now][i], now);
		}
	}
	nowans++;
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	rep(i, n - 1){
		int a, b;
		cin >> a >> b;
		a--, b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	get_dept(0, -1);
	rt = 0;
	rep(i, n) if(dept[i] > dept[rt]) rt = i;
	memset(dept, 0, sizeof(dept)), memset(mx_dept, 0, sizeof(mx_dept));
	get_dept(rt, -1);
	dfs(rt, -1);
	rep(i, n) pos[ord[i]] = ans[i];
	rep(i, n) cout << pos[i] << " ";
	cout << endl;
	return 0;
}


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

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

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

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

const char newl = '\n';

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

#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define P pair<int,int>
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }

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

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int h,w; cin >> h >> w;
  vector<string> s(h); rep(i,h) cin >> s[i];
  ll ans = 0;
  rep(i,h){
    rep(j,w){
      rep(k,4){
        if(j+dx[k]<0 || w<=j+dx[k] || i+dy[k]<0 || h<=i+dy[k]) continue;
        if(s[i][j] == '.' && s[i+dy[k]][j+dx[k]] == '.') ans++;
      }
    }
  }
  cout << ans/2;
  cout << endl;
  return 0;
}