#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

bool solve() {
	int n = ri();
	int id[2 * n];
	for (auto &i : id) i = -1;
	int paired[2 * n];
	for (auto &i : paired) i = -1;
	for (int i = 0; i < n; i++) {
		int a = ri() - 1;
		int b = ri() - 1;
		if (a != -2 && b != -2 && a >= b) return false;
		if (a != -2) {
			if (id[a] != -1) return false;
			id[a] = i;
		}
		if (b != -2) {
			if (id[b] != -1) return false;
			id[b] = i;
		}
		if (a != -2 && b != -2) {
			paired[a] = b;
			paired[b] = a;
		}
	}
	bool ok[2 * n + 1];
	memset(ok, 0, sizeof(ok));
	ok[0] = 1;
	for (int i = 0; i < 2 * n; i++) {
		if (!ok[i]) continue;
		for (int j = 1; i + j * 2 <= 2 * n; j++) {
			bool t = true;
			for (int k = i; k < i + j; k++) if (id[k] != -1 && id[k + j] != -1 && id[k] != id[k + j]) { t = false; break; }
			for (int k = i; k < i + j; k++) if (id[k] != -1 && paired[k] != -1 && paired[k] != k + j) { t = false; break; }
			for (int k = i + j; k < i + j + j; k++) if (id[k] != -1 && paired[k] != -1 && paired[k] != k - j) { t = false; break; }
			if (t) ok[i + j * 2] = true;
		}
	}
	return ok[2 * n];
}

int main() {
	puts(solve() ? "Yes" : "No");
	return 0;
}

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

#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()

typedef long long ll;

#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)

#define fi first
#define se second

const int N = 100;
int mat[N][N];
int n, m, a, b;
int ans = 0;

bool valid(int i, int j){
	return i>=0 and i<n and j>=0 and j<m;
}

bool poe(int cor, int io, int jo, int nn, int mm){
	fr(di,nn) fr(dj,mm){
		int i = io+di, j = jo+dj;
		if(!valid(i,j) or mat[i][j]==cor) return 0;
	}
	fr(di,nn) fr(dj,mm){
		int i = io+di, j = jo+dj;
		mat[i][j] = cor;
	}
	return 1;
}

void prinmat(){
	fr(i,n){
		fr(j,m) cout << mat[i][j];
		cout << endl;
	}
	cout << endl;
}

void rec(int id = 0){
	int achou = 0;
	//prin(id);
	fr(i,n) fr(j,m){
		if(!mat[i][j]){
			if(a and poe(1,i,j,2,1)){
				//prinmat();
				a--;
				rec(id+1);
				poe(0,i,j,2,1);
				a++;
				//prinmat();
			}
			if(a and poe(1,i,j,1,2)){
				a--;
				rec(id+1);
				poe(0,i,j,1,2);
				a++;
			}
			if(b and poe(1,i,j,1,1)){
				b--;
				rec(id+1);
				poe(0,i,j,1,1);
				b++;
			}
			achou = 1;
			goto sai;
		}
	}
	sai:
	if(!achou){
		ans++;
		//prin(id);
	}
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m >> a >> b;
	rec();
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

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

typedef vector<vi> vvi;
typedef vector<vll> vvll;

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

typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second

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

namespace debug {
    void __print(int x) {cerr << x;}
    void __print(long long x) {cerr << x;}
    void __print(double x) {cerr << x;}
    void __print(long double x) {cerr << x;}
    void __print(char x) {cerr << '\'' << x << '\'';}
    void __print(const string &x) {cerr << '\"' << x << '\"';}
    void __print(bool x) {cerr << (x ? "true" : "false");}

    template<typename T, typename V>
    void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
    template<typename T>
    void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
    void _print() {cerr << "]\n";}
    template <typename T, typename... V>
    void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}

#ifdef ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
}

using namespace debug;

const char nl = '\n';

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

vi adj[30];
bool marked[30];
vi adj2[30];
int boje[30];

bool moze = 1;

void dfs(int s, int tren = 0) {
    if(!moze) return;
    boje[s] = tren;
    for(auto e : adj2[s]) {
        if(boje[e] == boje[s]) {
            moze = 0;
            return;
        }
        if(boje[e] != -1) continue;
        dfs(e, tren^1);
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    ll ans = 0;

    for(int i = 0; i < n; ++i) boje[i] = -1;

    for(int mask = 0; mask < (1<<n); ++mask) {
        for(int i = 0; i < n; ++i) {
            marked[i] = (mask>>i)&1;
        }

        bool isti = 0;

        for(int i = 0; i < n; ++i) {
            for(auto e : adj[i]) {
                if(marked[e] && marked[i]) {
                     isti = 1;
                     break;
                }
            }
            if(isti) break;
        }

        if(isti) continue;

        for(int i = 0; i < n; ++i) {
            if(marked[i]) continue;
            for(auto e : adj[i]) {
                if(!marked[e]) adj2[i].pb(e);
            }
        }

        int komp = 0;
        moze = 1;

        for(int i = 0; i < n; ++i) {
            if(marked[i]) continue;
            if(boje[i] == -1) {
                ++komp;
                dfs(i);
                if(!moze) {
                    break;
                }
            }
        }

        if(moze) {
            ll tren = 1;
            for(int i = 0; i < komp; ++i) tren *= 2;
            ans += tren;
            //dbg(mask, komp, tren);
        }

        for(int i = 0; i < n; ++i) {
            adj2[i].clear();
            boje[i] = -1;
        }
    }

    cout << ans << nl;
}

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

    int testCases = 1;
    //cin >> testCases;
    while(testCases--)
        solve();

}

#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>
ostream &operator<<(ostream &os, const vector<T> &x){
    os << "{";
    for(size_t i = 0; i < x.size(); i++){
        if(i < x.size()-1) os << x[i] << ", ";
        else os << x[i];
    }
    os << "}";
    return os;
}

int main(void){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    rep(j, m){
        int s, t;
        cin >> s >> t;
        s--, t--;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    vector<int> idx(n, -1);
    auto dfs = [&](auto dfs, int now, array<int, 20> &v)->lli{
        for(auto to : g[now]){
            if(v[to] == v[now]){
                return 0;
            }
        }
        lli res = 1;
        for(auto to : g[now]){
            if(idx[to] == -1) idx[to] = idx[now]+1;
            if(idx[to] == idx[now]+1){
                lli tmp = 0;
                v[to] = (v[now]+1)%3;
                tmp += dfs(dfs, to, v);
                v[to] = (v[now]+2)%3;
                tmp += dfs(dfs, to, v);
                v[to] = -1;
                res *= tmp;
                if(tmp == 0) break;
            }
        }
        return res;
    };
    lli ans = 1;
    array<int, 20> v;
    v.fill(-1);
    rep(i, n){
        if(idx[i] != -1) continue;
        idx[i] = 0;
        v[i] = 0;
        ans *= dfs(dfs, i, v)*3;
    }
    cout << ans << endl;
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;

int n;
int g[21];
bool memo2[1<<18];
bool visited[1<<18];
bool isOK(int pat) {
  bool &res = memo2[pat];
  if(!visited[pat]) {
    visited[pat] = true;
    res = true;
    REP(i, n) {
      if(pat & (1 << i)) {
        if((g[i] & pat) != pat) {
          res = false;
          break;
        }
      }
    }
  }
  return res;
}

int memo[1 << 18];

int solve(int rest) {
  int &res = memo[rest];
  if(res < 0) {
    if(rest == 0) {
      res = 0;
    } else if(((rest-1) & rest) == 0) {
      res = 1;
    } else {
      int first = rest & -rest;
      int rest2 = rest ^ first;
      res = 1 + solve(rest2);
      for(int s = rest2; s; s = (s-1) & rest2) {
        if(isOK(s | first)) {
          res = min(res, 1 + solve(rest ^ first ^ s));
        }
      }
    }
  }
  return res;
}

int main(void) {
  int nEdge;
  scanf("%d%d", &n, &nEdge);
  REP(i, n) {
    g[i] |= 1 << i;
  }
  REP(i, nEdge) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a] |= 1 << b;
    g[b] |= 1 << a;
  }
  // if(nEdge == n*(n-1) / 2) {
  //   cout << 1 << endl;
  //   return 0;
  // }
  memset(memo, -1, sizeof memo);
  int res = solve((1 << n)-1);
  cout << res << endl;

  return 0;
}

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <bitset>

using namespace std;

clock_t timeStart, timeFinish;

void timeBegin() {
    timeStart = clock();
}

void timeEnd() {
    timeFinish = clock();
}

void timeDuration() {
    double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
    cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
    cout << " sec " << endl;
}

class InputReader {
public:
    InputReader() {
        input_file = stdin;
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    InputReader(const char *file_name) {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    inline InputReader &operator >>(int &n) {
        while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }
    inline InputReader &operator >>(long long &n) {
        while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance() {
        ++ cursor;
        if(cursor == SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};

const int MAXN = 500000;

char s[1 + MAXN], t[1 + MAXN];
vector<int> ones;

int main() {
    //timeBegin();
    //ifstream cin("input.txt");
    //ofstream cout("papagali.out");
    int n;
    cin >> n >> (s + 1) >> (t + 1);
    int s1 = 0, t1 = 0;
    for (int i = 1; i <= n; i++) {
        if (s[i] == '1') {
            s1++;
            ones.push_back(i);
        }
        if (t[i] == '1') {
            t1++;
        }
    }
    reverse(ones.begin(), ones.end());
    if (t1 % 2 != s1 % 2 || t1 > s1) {
        cout << "-1\n";
        return 0;
    }
    long long answer = 0;
    for (int i = 1; i <= n; i++) {
        if (t[i] == '0') {
            if (!ones.empty() && ones.back() <= i) {
                int x = ones.back();
                ones.pop_back();
                if (ones.empty()) {
                    cout << "-1\n";
                    return 0;
                }
                int y = ones.back();
                ones.pop_back();
                answer += y - x;
            }
        } else {
            if (ones.empty()) {
                cout << "-1\n";
                return 0;
            }
            int x = ones.back();
            ones.pop_back();
            answer += x - i;
        }
    }
    cout << answer << "\n";
    return 0;
}


/*
  author: Kamal Kant Bhardwaj
  date:
*/
#include<bits/stdc++.h>
using namespace std;

#define N               3005
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(newx)      __builtin_popcountll(newx)
#define zrobits(newx)      __builtin_ctzll(newx)
#define mod             1000000007
#define inf             1e18
#define ps(newx,y)         finewxed<<setprecision(y)<<newx
#define mk(arr,n,type)  type *arr=new type[n];
#define w(newx)            int newx; cin>>newx; while(newx--)

void c_p_c()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

}
int mat[10005][103];
int32_t main()
{
  c_p_c();
  int n; cin >> n; string s; cin >> s;
  int a[103];
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  int maxi = mod;
  for (int i = 1; i <= n; i++)
  {
    maxi = min(maxi, abs(a[i] - a[i - 1]));
  }

  int sum = 0;
  for (int i = 0; i < 10005; i++)
    for (int j = 0; j < 103; j++)
      mat[i][j] = 0;
  int div = a[0] / maxi;
  int rem = a[0] % maxi;
  for (int j = 0; j < maxi; j++)
  {
    mat[j][0] = div ;
    if (rem > 0)
    {
      mat[j][0] += 1;
    }
    rem--;
    sum += mat[j][0];
  }
  for (int i = 1; i <= n; i++)
  {

    // int newsum = 0;

    //  int diff = abs(a[i] - sum);
    int div = a[i] / maxi;
    int rem = a[i] % maxi;
    // cout << div << "  " << rem << endl;
    for (int j = 0; j < maxi; j++)
    {
      mat[j][i] = div;
      if (rem > 0)
        mat[j][i] += 1;
      rem--;
    }
  }
  cout << maxi << endl;
  for (int j = 0; j < maxi; j++)
  {
    for (int i = 0; i <= n; i++)
    {
      cout << mat[j][i] << " ";
    }
    cout << endl;

  }

  return 0;

}

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept {
            return itr != other.itr;
        }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept
        : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T, T Div = 2>
constexpr T INFTY = std::numeric_limits<T>::max() / Div;

template <class T> bool setmin(T& lhs, const T& rhs) {
    if (lhs > rhs) {
        lhs = rhs;
        return true;
    }
    return false;
}

template <class T> using Vec = std::vector<T>;

void A_main() {
    usize N;
    std::cin >> N;
    Vec<char> S(N);
    for (auto& x : S) {
        std::cin >> x;
    }
    Vec<u32> A(N + 1);
    for (auto& x : A) {
        std::cin >> x;
    }
    u32 min = INFTY<u32>;
    for (const auto i : rep(0, N)) {
        setmin(min, (A[i] < A[i + 1] ? A[i + 1] - A[i] : A[i] - A[i + 1]));
    }
    std::cout << min << '\n';
    for (const auto k : rep(0, min)) {
        for (const auto i : rep(0, N + 1)) {
            std::cout << A[i] / min + (k < (A[i] % min)) << " \n"[i == N];
        }
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    A_main();
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int,int> Pii;
#define reg register
#define mp make_pair
#define pb push_back
#define Mod1(x) ((x>=P)&&(x-=P))
#define Mod2(x) ((x<0)&&(x+=P))
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); }
template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }

char IO;
template <class T=int> T rd(){
	T s=0; int f=0;
	while(!isdigit(IO=getchar())) f|=IO=='-';
	do s=(s<<1)+(s<<3)+(IO^'0');
	while(isdigit(IO=getchar()));
	return f?-s:s;
}

const int N=1e6+10,INF=1e9+10;

int n;
char s[N];
char ch[]="BWR";

int F[N],cnt[N];
int C(int n,int m){
	if(cnt[n]-cnt[m]-cnt[n-m]) return 0;
	return F[n]*F[m]*F[n-m]%3;
}

int main(){
	rep(i,F[0]=1,N-1) {
		cnt[i]=cnt[i-1],F[i]=F[i-1];
		int x=i;
		while(x%3==0) x/=3,cnt[i]++;
		F[i]=F[i]*x%3;
	}
	n=rd(),scanf("%s",s+1);
	int sum=0;
	rep(i,1,n) {
		int t=0;
		if(s[i]=='W') t=1;
		if(s[i]=='R') t=2;
		if(~n&1) t=3-t;
		sum=(sum+C(n-1,i-1)*t)%3;
	}
	//if(~n&1) sum=n*3-sum;
	sum%=3,putchar(ch[sum]);
}


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

ll solve() {
    int n; cin >> n;
    vector<vector<ll>> r(3);
    rep(i, 2 * n) {
        ll a; char c;
        cin >> a >> c;
        r[string("RGB").find(c)].push_back(a);
    }

    rep(i, 3) {
        r[i].push_back(-inf);
        r[i].push_back(inf);
        sort(r[i].begin(), r[i].end());
    }

    auto f = [&](int i, int j) -> ll {
        ll res = inf;
        for(auto &x : r[i]) {
            if(abs(x) == inf) continue;
            auto it = upper_bound(r[j].begin(), r[j].end(), x);
            res = min(res, min(*it - x, x - *prev(it)));
        }
        return res;
    };

    rep(i, 3) {
        int j = (i + 1) % 3;
        int k = (i + 2) % 3;
        if(r[j].size() % 2 && r[k].size() % 2) return min(f(j, k), f(i, j) + f(i, k));
    }

    return 0;
}

int main() {
    cout << solve() << '\n';
    return 0;
}

#include<cstdio>
#include<algorithm>
#define N 300010
#define db double
#define ll long long
#define fo(x,a,b) for(int x=(a);x<=(b);x++)
#define fd(x,a,b) for(int x=(a);x>=(b);x--)
using namespace std;
struct node{ll x; int id;}a[N];
int n,t[4],tot1=0,tot2=0,tot3=0,tot4=0;
ll b[N],c[N],d[N],e[N];
ll ans=(1ll<<50),ans1=(1ll<<50),ans2=(1ll<<50);

inline ll read() {
	ll x=0;int f=0;char c=getchar();
	while (c<'0'||c>'9') f=(c=='-')?1:f,c=getchar();
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return f?-x:x;
}

int gc() {
	char c=getchar(); while (c!='R'&&c!='G'&&c!='B') c=getchar();
	if (c=='R') return 1; else if (c=='G') return 2; else return 3;
}

inline bool cmp(node x,node y) {return x.x<y.x;}

void calc1() {
	ans1=ans2=(1ll<<50);
	int pos=-1;
	fo(i,1,tot1) {
		int l=1,r=tot2,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i]));
			if (c[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans1) ans1=res,pos=i;
	}
	fo(i,1,tot1) {
		int l=1,r=tot3,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i]));
			if (d[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans2&&i!=pos) ans2=res;
	}
	ans=min(ans,ans1+ans2);
}

void calc2() {
	ans1=ans2=(1ll<<50);
	int pos=-1;
	fo(i,1,tot1) {
		int l=1,r=tot3,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i]));
			if (d[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans2) ans2=res,pos=i;
	}
	fo(i,1,tot1) {
		int l=1,r=tot2,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i]));
			if (c[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans1&&i!=pos) ans1=res;
	}
	ans=min(ans,ans1+ans2);
}

int main() {
	n=read();n=2*n;
	fo(i,1,n) {
		a[i].x=read(),a[i].id=gc(),t[a[i].id]++;
		if (a[i].id==1) b[++tot1]=a[i].x;
		else if (a[i].id==2) c[++tot2]=a[i].x;
		else d[++tot3]=a[i].x;
	}
	sort(a+1,a+n+1,cmp);
	if (t[1]%2==0&&t[2]%2==0&&t[3]%2==0)
		return 0&printf("0\n");
	sort(b+1,b+tot1+1);
	sort(c+1,c+tot2+1);
	sort(d+1,d+tot3+1);
	if (tot2%2==0) {
		swap(b,c),swap(tot1,tot2);
	}
	else if (tot3%2==0) {
		swap(b,d),swap(tot1,tot3);
	}
	fo(i,1,tot2) {
		int l=1,r=tot3,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(d[mid]-c[i]));
			if (d[mid]>=c[i]) r=mid-1; else l=mid+1;
		}
		ans=min(ans,res);
	}
	calc1();
	calc2();
	printf("%lld\n",ans);
	return 0;
}


#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
#include<algorithm>
constexpr unsigned long long MOD = 998244353;
constexpr unsigned long long IM = 18479187003;
constexpr unsigned long long MOD32 = 665496236;

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

    unsigned long long y = H * W - K;
    while(--K>=0){
        int h=0, w=0;
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();}
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();}
        f[h-1][w-1] = getchar_unlocked();
        getchar_unlocked();
    }
    --H; --W;
    dp[0] = 1;
    for(int i = 0; i < H; ++i){
        for (int j = 0; j < W; ++j){
            switch(f[i][j]) {
                case 'R':
                    dp[j + 1] += dp[j];
                    dp[j] = 0;
                    break;
                case '\0':{
        dp[j] *= MOD32;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(dp[j], IM, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
        dp[j] = dp[j] - x * MOD+MOD;
             }
                case 'X':
                    dp[j + 1] += dp[j];
            }
        }
            switch(f[i][W]) {
                case 'R':
                    dp[W] = 0;
                    break;
                case '\0':{
        dp[W] *= MOD32;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(dp[W], IM, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64);
#endif
       dp[W]= dp[W] - x * MOD+MOD;
             }
            }
    }
    for (int j = 0; j < W; ++j){
        switch(f[H][j]) {
            case '\0':{
        dp[j] *= MOD32;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(dp[j], IM, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
        dp[j] = dp[j] - x * MOD+MOD;
               }
            case 'R':
            case 'X':
                dp[j + 1] += dp[j];
        }
    }

    unsigned long long x = 3;
    while(y) {
      if(y & 1) {
        dp[W] *= x;
#ifdef _MSC_VER
        unsigned long long x2;
        _umul128(dp[W], IM, &x2);
#else
        unsigned long long x2 =
            (unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64);
#endif
        dp[W] = dp[W] - x2 * MOD+MOD;
               }
      {
        x *= x;
#ifdef _MSC_VER
        unsigned long long x2;
        _umul128(x, IM, &x2);
#else
        unsigned long long x2 =
            (unsigned long long)(((unsigned __int128)(x)*IM) >> 64);
#endif
        x = x- x2 * MOD + MOD;}
      y >>= 1;
    }
    printf("%d", dp[W] % MOD);

    return 0;
}

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

#define INF 1020304050607080901

long long steps(long long left, long long t) {
  map<long long,long long> m;
  m[left] = 0;
  queue<long long> q;
  long long td = 1LL<<t;
  q.push(left);
  while (!q.empty())
  {
    long long now = q.front();
    q.pop();
    for (long long i=0;i<=t;i++) {
      long long d = 1LL<<i;
      if (now&d) {
        long long to;
        to = now-d;
        if (to >= 0) {
          if (m.find(to) == m.end()) {
            m[to] = m[now]+1;
            q.push(to);
          }
        }
        to = now+d;
        if (to <= td) {
          if (m.find(to) == m.end()) {
            m[to] = m[now]+1;
            q.push(to);
          }
        }
        break;
      }
    }
  }
  long long ret = INF;
  if (m.find(0) != m.end()) ret = min(ret,m[0]);
  // if (m.find(td) != m.end()) ret = min(ret,m[td]);
  return ret;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  long long x,y;
  cin >> x >> y;
  long long ans = abs(x-y);
  long long beg = x;
  long long t = 0;
  while (true) {
    long long kans = t;
    long long left = abs(beg-y);
    long long d = 1LL<<t;
    kans += left/d;
    kans += steps(left%d,t);
    //cout << beg << ' ' << kans << endl;
    ans = min(ans,kans);
    if (beg > y) break;
    beg *= 2;
    t++;
  }
  /* for (int i=0;i<7;i++) {
    for (int j=0;j<(1<<i);j++) {
      cout << j << ' ' << i << ' ' << steps(j,i) << endl;
    }
  }*/
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define bolt                 \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)
#define test   \
    int tt;    \
    cin >> tt; \
    while (tt--)
#define ll long long
#define ld long double
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
int main(){
    bolt;
    ll a,b,c,d;cin>>a>>b>>c>>d;
    if(d>=a*b && d<=a*c){
        cout<<"No"<<endl;
    }else{
        cout<<"Yes"<<endl;
    }
}

#include <iostream>
using namespace std;
int main(void)
{
    double v, t, s, d;
    cin >> v >> t >> s >> d;
    if(d/v-s > 1e-7 || 1e-7 < t-d/v) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}


//tt89 ;)
#include<bits/stdc++.h>
using namespace std;
//typedef long long ll;
#define int long long
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define clock cerr<<"\n"<<(float)clock()/CLOCKS_PER_SEC*1000<<" ms"<<endl;
#define ff first
#define ss second
void testcase(int t) { cout<<"Case #"<<t<<": "; }
void online() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
}
const int mod= 1e9+7;

int n,m,k,x;
vector<int> a,b,c,d,arr;

int getAns() {
    int ans=0;
    for(int i=0;i<m;i++) {
        if(arr[a[i]]>0 && arr[b[i]]>0) ans++;
    }
    return ans;
}

void getRec(int i) {
    if(i==k) {
        int y=getAns(); x= max(x,y);
        return;
    }
    arr[c[i]]++; getRec(i+1); arr[c[i]]--;
    arr[d[i]]++; getRec(i+1); arr[d[i]]--;
}

int solve() {
    x=0;
    getRec(0);
    return x;
}

signed main() {
    fastIO;
    int T=1;
//    cin>>T;
    for(int t=1;t<=T;t++) {
        cin>>n>>m;
        arr.resize(n+1,0);
        a.resize(m); b.resize(m);
        for(int i=0;i<m;i++) cin>>a[i]>>b[i];
        cin>>k;
        c.resize(k); d.resize(k);
        for(int i=0;i<k;i++) cin>>c[i]>>d[i];
        cout<<solve()<<"\n";
    }
    return 0;
}

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define FAST ios_base::sync_with_stdio(false);cin.tie(0);
    const double EPS = 1e-9;
    const double PI = acos(-1);
    const int knightDir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,-1},{2,1},{-1,-2},{1,-2}};
    //const int dx[]={0,0,0,1,1,1,-1,-1,-1};
    //const int dy[]={-1,0,1,-1,0,1,-1,0,1};
    const int dx[] = {0 , 1 , 0 , -1};
    const int dy[] = {1 , 0 , -1 , 0};
    //#define MOD         1000000007LL
    #define MAX         200015
    const ll MOD = 998244353;
    inline ll mod(ll a){return ((a % MOD + MOD) % MOD);}
    inline ll add(ll a, ll b){return mod(mod(a) + mod(b));}
    inline ll sub(ll a, ll b){return mod(mod(a) - mod(b));}

    int l[MAX],r[MAX];
    vector<int> graph[MAX];
    int depth[MAX] , vis[MAX];
    ll c[MAX];
    void depthDFS(int now , int d)
    {
        depth[now] = d;

        for(int a : graph[now])
        {
            if(!vis[a])
            {
                vis[a] = 1;
                depthDFS(a , d + 1);
            }
        }
    }

    void sumDFS(int now , ll point)
    {
        c[now] += point;

        for(int a : graph[now])
        {
            if(!vis[a])
            {
                vis[a] = 1;
                sumDFS(a , c[now]);
            }
        }

    }

    int main()
    {
        FAST
        int t;
        t = 1;

        while(t--)
        {
            int n;
            cin >> n;

            for(int i = 1 ; i < n ; i++)
            {
                int a , b;
                cin >> a >> b;
                a-- , b--;

                l[i] = a , r[i] = b;
                graph[a].push_back(b);
                graph[b].push_back(a);
            }

            vis[0] = 1;
            depthDFS(0 , 0);

            int q;
            cin >> q;

            for(int i = 0 ; i < q ; i++)
            {
                int t , e , x;
                cin >> t >> e >> x;

                int yes = l[e], no = r[e];
                if(t == 2)
                {
                    swap(yes , no);
                }

                if(depth[yes] < depth[no])
                {
                    c[0] += x;
                    c[no] -= x;
                }
                else c[yes] += x;
            }

            memset(vis , 0 , sizeof(vis));
            vis[0] = 1;
            sumDFS(0 , 0);

            for(int i = 0 ; i < n ; i++)cout << c[i] << endl;

        }
    }


#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << "  ";
#define pb push_back
using vi = vector<lint>;
using vvi = vector<vector<lint>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr lint LINF = LLONG_MAX/2;

int N, M, TIME=clock();
vector<pair<int, string>> V;
vi C(8);
int sum = 0;
vector<string> S(20);

int total() {
	string s;
	int p = 0;
	set<string> t, u;
	rep(i, M) t.insert(V[i].second);
	vector<string> v(20);
	rep(i, N) v[i] = S[i];
	rep(q, 2) {
		rep(i, N) {
			s = v[i] + v[i];
			for (auto a : t) {
				bool f = false;
				rep(j, N) {
					if (a == s.substr(j, a.size())) {
						f = true;
						break;
					}
				}
				if (f) p += a.size();
				else u.insert(a);
			}
			t.clear();
			swap(t, u);
		}
		rep(i, N) { v[i].clear(); rep(j, N) v[i] += S[j][i]; }
	}
	return p;
}

int delta(int a, int b, int x, int y) {
	string s;
	int p = 0;
	set<string> t, u;
	rep(i, M) t.insert(V[i].second);
	rep(q, 2) {
		s = S[a] + S[a];
		for (auto a : t) {
			bool f = false;
			int c = (b+N-a.size()+1)%N;
			rep(j, a.size()) {
				if (a == s.substr(c+j, a.size())) {
					f = true;
					break;
				}
			}
			if (f) p += a.size();
			else u.insert(a);
		}
		t.clear();
		swap(t, u);
		swap(a, x);
	}
	rep(q, 2) {
		s = "";
		rep(i, N) s += S[i][b];
		rep(i, N) s += S[i][b];
		for (auto a : t) {
			bool f = false;
			int c = (b+N-a.size()+1)%N;
			rep(j, a.size()) {
				if (a == s.substr(c+j, a.size())) {
					f = true;
					break;
				}
			}
			if (f) p += a.size();
			else u.insert(a);
		}
		t.clear();
		swap(t, u);
		swap(b, y);
	}
	return p;
}

int delta_1(int a, int b) {
	string s;
	int p = 0;
	set<string> t, u;
	rep(i, M) t.insert(V[i].second);
	s = S[a] + S[a];
	for (auto a : t) {
		bool f = false;
		int c = (b+N-a.size()+1)%N;
		rep(j, a.size()) {
			if (a == s.substr(c+j, a.size())) {
				f = true;
				break;
			}
		}
		if (f) p += a.size();
		else u.insert(a);
	}
	t.clear();
	swap(t, u);

	s = "";
	rep(i, N) s += S[i][b];
	rep(i, N) s += S[i][b];
	for (auto a : t) {
		bool f = false;
		int c = (b+N-a.size()+1)%N;
		rep(j, a.size()) {
			if (a == s.substr(c+j, a.size())) {
				f = true;
				break;
			}
		}
		if (f) p += a.size();
		else u.insert(a);
	}
	t.clear();
	swap(t, u);
	return p;
}

int main() {
	cin >> N >> M;
	string s;
	rep(i, M) {
		cin >> s;
		V.pb({s.size(), s});
		rep(j, s.size()) C[s[j]-'A']++;
	}
	rep(i, 8) sum += C[i];
	rep(i, 7) C[i+1] += C[i];
	sort(all(V));

	int a=0, b=0, c=0, x, y, z;
	rep(i, N) {
		rep(j, N) {
			x = rand()%sum;
			rep(k, 8) {
				if (x < C[k]) {
					S[i] += 'A'+k;
					break;
				}
			}
		}
	}
	// show(total())
	int count = 0;
	while (true) {
		count++;
		a = rand()%N;
		b = rand()%N;
		if (count%5 == 0) {
			y = S[a][b]-'A';
			x = y;
			while (x == y) x = rand()%8;
			c = delta_1(a, b);
			S[a][b] = 'A'+x;
			z = delta_1(a, b);
			if (c > z) S[a][b] = 'A'+y;
		} else {
			x = rand()%N;
			y = rand()%N;
			c = delta(a, b, x, y);
			swap(S[a][b], S[x][y]);
			z = delta(a, b, x, y);
			if (c > z) swap(S[a][b], S[x][y]);
		}
		// list(c)list(z) show(total())
		if (clock()-TIME > 2.95 * CLOCKS_PER_SEC) break;
	}
	// show(count)
	rep(i, N) std::cout << S[i] << '\n';
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

void rotate(string &s , int x) {
    string t = s;
    for (int i = 0; i < s.size(); i++) {
        t[(i + x) % s.size()] = s[i];
    }
    s = t;
    return;
}

int main() {

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

    int n , m;
    cin >> n >> m;
    vector<string> sb(m);
    for (int i = 0; i < m; i++) {
        cin >> sb[i];
    }

    for (int i = 0; i < m - 1; i++) {
        for (int j = i + 1; j < m; j++) {
            if (sb[i].find(sb[j]) != -1) {
                sb[j] = "nothing";
            }
        }
    }

    vector<pair<int,string> > s;

    for (int i = 0; i < m; i++) {
        if (sb[i] == "nothing") continue;
        s.push_back(make_pair(sb[i].size() , sb[i]));
    }

    sort(s.begin() , s.end());
    s.erase(unique(s.begin() , s.end()),s.end());
    reverse(s.begin() , s.end());
    m = s.size();

    vector<string> ans(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ans[i] += '.';
        }
    }

    const int sz = m/ n;
    vector<vector<string> >  s2(sz);

    for (int i = 0; i < sz; i++) {
        for (int j = i * n; j < (i + 1) * n; j++) {
            s2[i].push_back(s[j].second);
        }
    }

    vector<vector<bool> > used(sz , vector<bool>(n , false));

    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < s2[i].size(); j++) {
            string t = s2[i][j];
            bool ok = false;
            string t2 = t;
            int id = 0;
            for (int k = 0; k < ans[j].size(); k++) {
                if (ans[j][k] != '.' && id < t2.size()) {
                    if (ans[j][k] == t2[id]) {
                        id++;
                        if (id == t2.size()) {
                            ok = true;
                        }
                    }
                }
                if (ans[j][k] == '.') {
                    for (int l = 0; l < t.size(); l++) {
                        if (ok) {
                            used[i][j] = true;
                            break;
                        }
                        if (n - k + 1 < t.size()) {
                            break;
                        } else {
                            ans[j][k + l] = t[l];
                            used[i][j] = true;
                        }
                    }
                    break;
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (ans[i][j] == '.') {
                ans[i][j] = 'A';
            }
        }
    }

    for (int i = 0; i < n; i++) {
        rotate(ans[i] , i);
    }

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

    return 0;
}


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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
//#define int long long
#define INF 1e18
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; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;

Graph G;
int n, d[250010];

// <最短距離, 頂点の番号>
using P = pair<int, int>;

void dijkstra(int s)
{
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d + n, INF);
    d[s] = 0;
    que.push(P(0, s));

    while (!que.empty())
    {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (d[v] < p.first)
            continue;

        for (int i = 0; i < G[v].size(); ++i)
        {
            Edge e = G[v][i];
            if (d[e.first] > d[v] + e.second)
            {
                d[e.first] = d[v] + e.second;
                que.push(P(d[e.first], e.first));
            }
        }
    }
}

int a[510][510];
int b[510][510];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int r,c;
    cin>>r>>c;
    n=r*c;
    G.assign(r*c , vector<Edge>());
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c-1; j++)
        {
            cin>>a[i][j];
        }

    }
    for (int i = 0; i < r-1; i++)
    {
        for (int j = 0; j < c; j++)
        {
            cin>>b[i][j];

        }

    }

    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {

            if(j<c-1) G[c*i + j].push_back({c*i+j+1, a[i][j]});
            if(j>=1) G[c*i + j].push_back({c*i+j-1, a[i][j-1]});
            if(i<r-1) G[c*i + j].push_back({c*(i+1)+j, b[i][j]});
        }

    }
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            for (int k = 1; k < r; k++)
            {
                if(i - k < 0 ) continue;
                G[c*i+j].push_back({c*i+j - c*k , 1 + k});
            }

        }

    }
    dijkstra(0);
    cout<<d[r*c-1]<<endl;

    return 0;
}

#include <bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
const double maxv = 1e100;
ull n, k, money, ans, ai, bi;
vector <vector <long long> > ab;
int main() {
	cin >> n >> k;
	money = k;
	for(int i = 0; i < n; i++) {
		cin >> ai >> bi;
		vector <long long> abi;
		abi.push_back(ai);
		abi.push_back(bi);
		ab.push_back(abi);
	}
	sort(ab.begin(), ab.end());
	for(int i = 0; i < n && money > 0 && ans <= maxv; i++) {
		if((i > 0 && money < ab[i][0] - ab[i - 1][0]) || (i == 0 && money < ab[i][0])) {
			ans += money;
			money = 0;
			break;
		}
		if(i == 0) {
			money -= ab[i][0];
			money += ab[i][1];
			ans += ab[i][0];
		}
		else {
			money -= ab[i][0] - ab[i - 1][0];
			money += ab[i][1];
			ans += ab[i][0] - ab[i - 1][0];
		}
	}
	if(money > 0) ans += money;
	if(money < 0) ans -= 0 - money;
	cout << ans;
	return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <queue>
#include <fstream>
#include <stdio.h>
#include <bitset>
#include <set>
#include <iomanip>
#include <random>
#include <iterator>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned long long unll;
#define fi first
#define se second

void dfs(int v, vector<vector<int>>& adj, vector<bool>& used)
{
    used[v] = 1;
    for (int i = 0; i < adj[v].size(); ++i)
    {
        if (used[adj[v][i]]) continue;
        dfs(adj[v][i], adj, used);
    }
}

void solve()
{
    int n, res = 0;
    cin >> n;
    vector<int> a(n);
    for (auto& i : a) cin >> i;
    vector<vector<int>> adj(200000);
    for (int i = 0; i < n; ++i)
    {
        adj[a[i] - 1].push_back(a[n - 1 - i] - 1);
    }
    vector<bool> used(200000, 0);
    for (int i = 0; i < n; ++i)
    {
        if (used[a[i] - 1]) continue;
        dfs(a[i] - 1, adj, used);
        --res;
    }
    sort(a.begin(), a.end());
    for (int i = 1; i < n; ++i) if (a[i - 1] != a[i]) ++res;
    ++res;
    cout << res;

}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

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

typedef long long ll;

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr << '\n'; }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); }

#define        optimize           ios::sync_with_stdio(0); cin.tie(0);
#define PI    acos(-1.0)
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define RESET(a, b) memset(a, b, sizeof(a))
#define pii pair <int, int>
#define min3(a, b, c) min(c, min(a, b))
#define max3(a, b, c) max(c, max(a, b))

const ll MX = 2e5 + 105;

vector <int> adj[MX];
int ara[MX];
bool vis[MX];
int c = 0;

void dfs(int u)
{
    c++;
    vis[u] = 1;
    for(auto v: adj[u]){
        if(!vis[v]){
            vis[v] = 1;
            dfs(v);
        }
    }
}

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

    for(int i = 0; i < n; i++){
        cin>>ara[i];
    }
    for(int i = 0; i < n/2; i++){
        adj[ara[i]].push_back(ara[n-1-i]);
        adj[ara[n-1-i]].push_back(ara[i]);
    }

    int ans = 0;
    for(int i = 1; i <= 2e5; i++){
        if(!vis[i]){
            c = 0;
            dfs(i);
            ans += (c-1);
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i,n) for(int i = 0; i < (int) n; i++)
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

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

  if (a == b) {
    cout << x << "\n";
    return 0;
  }

  int stair = min(y, 2 * x);
  if (a < b) cout << (b - a) * stair + x << "\n";
  else cout << (a - b - 1) * stair + x << "\n";
}

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

using ll = long long;
using pii = pair<int,int>;
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてから使う
#define INF 2147483647
#define LLINF 9223372036854775807LL

int main(){
	int n; cin>>n;
	ll ans = 0;
	rep(i,n){
		ll a,b; cin>>a>>b;
		ans += (b-a+1) * (b+a)/2;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#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 Clear(a) a = decltype(a)()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define vec vector
typedef long long ll;
typedef pair<ll,ll> P;
typedef string::const_iterator state; //構文解析
const double PI=acos(-1);
//const ll big=998244353;
const ll big=1000000007LL;
const ll INF=1e18;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
ll max(ll x,ll y){
    if(x>y)return x;
    else return y;
}
ll min(ll x,ll y){
    if(x<y)return x;
    else return y;
}
ll expm(ll x,ll y,ll mod=big){
    if(y==0)return 1;//0^0=1
    if(x==1||x==0)return x;
    if(y%2==1)return (expm(x,y-1,mod)*x)%mod;
    ll t=expm(x,y/2,mod);
    return (t*t)%mod;
}
ll exp(ll x,ll y){
    if(y==0)return 1;//0^0=1
    if(x==1||y==0)return x;
    if(y%2==1)return exp(x,y-1)*x;
    ll t=exp(x,y/2);
    return t*t;
}
struct unionfind{
    vector<int> par;
    vector<int> high;
    vector<int> size;
    vector<map<int,int>> num;
    unionfind(int n):par(n,-1), high(n,1), size(n,1),num(n){
    }
    int find(const int x){
        if(par[x]==-1)return x;
        return par[x]=find(par[x]);
    }
    void unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y)return;

        if(high[x]>high[y])swap(x,y);
        if(high[x]==high[y])high[y]++;
        for(auto i:num[x]){//xのグループに属すクラス全部 i.first=class i.secnod=人数
            num[y][i.first]+=i.second;
        }
        par[x]=y;
        size[y]+=size[x];
        return;
    }
    int getsize(const int i){
        return size[find(i)];
    }
    void get(const int m){
        rep(i,m){
            int a,b;cin >> a >> b;
            unite(a-1,b-1);
        }
        return;
    }
};

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(10);
    /*--------------------------------*/

    int n;cin >> n;
    int q;cin >> q;
    unionfind uf2(n);
    rep(i,n){
        int cl;cin >> cl;
        uf2.num[i][cl-1]=1;
    }
    //各タイミングで,両方で連結しているのを求めないとだめ.
    rep(i,q){
        int com,a,b;cin >> com >> a >> b;
        if(com==1){
            uf2.unite(a-1,b-1);
            //このuniteで,同じクラスかつ連結になった個数を求めたい
        }else{
            a=uf2.find(a-1);
            cout << uf2.num[a][b-1] << '\n';
        }
    }
}
//・printデバッグ消した？・オーバーフローしない？
//・気づいた条件分岐書き忘れてない？.....よし出せ！

#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
using ll = long long;

#define N 1005
int dp[N][N];
vector<int> a, b;
int n, m;

int solve(int i, int j) {
    if (i >= n) return abs(m-j);
    if (j >= m) return abs(n-i);

    int& curr = dp[i][j];
    if (curr+1) return curr;
    curr = 1e6;

    curr = min(curr, 1 + solve(i+1, j));
    curr = min(curr, 1 + solve(i, j+1));
    curr = min(curr, 2 + solve(i+1, j+1));

    if (a[i] == b[j]) {
        curr = min(curr, solve(i+1, j+1));
    } else {
        curr = min(curr, 1 + solve(i+1, j+1));
    }

    return curr;
}

int main() {
    FAST;

    memset(dp, -1, sizeof(dp));

    cin >> n >> m;

    a = vector<int>(n);
    b = vector<int>(m);

    int i, j, k, x, y, sz;
    for (i = 0; i < n; ++i) cin >> a[i];
    for (i = 0; i < m; ++i) cin >> b[i];

    if (n < m) {
        swap(n, m);
        swap(a, b);
    }

    cout << solve(0,0);

    return 0;
}


#include<map>
#include<ctime>
#include<cstdio>
#include<cctype>
#include<string>
#define ll long long
const ll N=1e3+10;
const ll HgS=1e9+7;
using namespace std;
ll read() {
	char c;
	ll x=0,f=1;
	while(!isdigit(c=getchar()))
		f-=2*(c=='-');
	while (isdigit(c)){
		x=x*10+(c-48)*f;
		c=getchar();
	}
	return x;
}
ll n,f,res;
ll fib(ll n){
	ll a=0,b=1,c=0;
	for(ll i=1;i<=n;++i){
		c=(a+b)%HgS;
		a=b;
		b=c;
	}
	return a;
}
int main() {
	#ifndef ONLINE_JUDGE
	freopen("D.in","r",stdin);
	freopen("D.out","w",stdout);
	#endif
	clock_t t1=clock();
	//--------
	n=read();
	for(int i=0;i<4;++i){
		char c;
		while(!isalpha(c=getchar()));
		f<<=1;
		f|=(c=='B');
	}
	switch(f){
		case 0:
		case 1:
		case 2:
		case 3:
		case 5:
		case 7:
		case 13:
		case 15:
			res=1;
			break;
		case 4:
		case 10:
		case 11:
		case 12:
			res=1;
			for(int i=4;i<=n;++i){
				res<<=1;
				res%=HgS;
			}
			break;
		default:
			res=fib(n-1);
			break;
	}
	printf("%lld",res);
	//--------
	clock_t t2=clock();
	fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int main(){
    ll N;
    cin >> N;
    char aa, ab, ba, bb;
    cin >> aa >> ab >> ba >> bb;
    string s;
    s = aa;
    s = s + ab;
    s = s + ba;
    s = s + bb;
    if(s == "ABBA" || s == "BAAA" || s == "BAAB" || s == "BBBA"){
        vector<ll> a(N+5);
        a[2] = 1;
        a[3] = 1;
        for (int i = 4; i < N+3; i++) {
            a[i] = (a[i-1] + a[i-2]) % MOD;
        }
        printf("%lld\n", a[N]);
    }
    else if(s == "ABAA" || s == "BABA" || s == "BABB" || s == "BBAA"){
        vector<ll> a(N+5);
        a[2] = 1;
        a[3] = 1;
        for (int i = 4; i < N+3; i++) {
            a[i] = a[i-1] * 2 % MOD;
        }
        printf("%lld\n", a[N]);
    }
    else{
        printf("%d\n", 1);
    }

}


#include <bits/stdc++.h>
#include <random>
using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define endk '\n'
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; }
lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); }
lint ceil(lint a, lint b) { return (a + b - 1) / b; }
lint digit(lint a) { return (lint)log10(a); }
const lint MOD = 1e9 + 7, INF = 1e9;
lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 };
void YN(bool flag) { cout << (flag ? "Yes" : "No") << endk; }
typedef pair<lint, string> Pa;
typedef pair<lint, plint> tlint;

lint N;
lint dist[100][100];
int main() {
	cin >> N;
	REP(i, N) {
		REP(j, N) {
			dist[i][j] = INF;
			char c;
			cin >> c;
			if (c == '1') dist[i][j] = 1;
		}
	}
	REP(k, N) {
		REP(i, N) {
			REP(j, N) {
				chmin(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	long double ans = 0;
	REP(i, N) {
		long double sum = 1;
		REP(j, N) {
			if (i == j) continue;
			if (dist[j][i] != INF) sum++;
		}
		ans += (long double)1 / sum;
	}
	cout << setprecision(16) << ans << endk;
}

//wtrl,everybody hangbeat me
#include<bits/stdc++.h>
/*#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>*/
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<ll,ll> pll;
/*=====================================================================*/
#define pb push_back
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(s) (s).begin(),(s).end()
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define forn(i,p,n) for(int i=p;i<=n;i++)
#define ford(i,p,n) for(int i=n;i>=p;i--)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();++i)
#define INF 1e9
#define PI acos(-1)
/*=====================================================================*/
string int_to_string(ll n)
{
	string s="";
	while(n)
	{
		ll now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
ll string_to_int(string s)
{
	ll n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
/*======================================================================*/
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
const int month[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
void solve()
{
	int n;
	cin>>n;
	int now=0;
	for(int i=1;now<=1e9;i++)
	{
		now+=i;
		if(now>=n)
		{
			cout<<i<<endl;
			return;
		}
	}
}
/*======================================================================*/
signed main()
{
    std::ios::sync_with_stdio(false);
  /*
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
  */
  /*====================================================================*/
  int t=1;
  //cin>>t;
  while(t--)
  {
  	solve();
  }
  return 0;
}
/*
注意数组越界，vector长度为0的时候循环会RE
注意输入顺序
注意循环时int*int爆ll
注意答案存爆ll
*/


#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#define M 100001
#define inf 2147483647
typedef long long ll;

int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }

inline void read(int &T) {
    int x = 0; bool f = 0; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = !f; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    T = f ? -x : x;
}

int a, b, x, y, ans = inf;

int main() {
    read(a), read(b), read(x), read(y);
    y = min(y ,2 * x);
    if (a > b) std::cout << x + abs((a - 1) - b) * y;
    if (a <= b) std::cout << x + abs(a - b) * y;
    //std::cout << ans << '\n';
    return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>

//#define ordered_set tree<int, null_type, less<int >,rb_tree_tag, tree_order_statistics_node_update>
//using namespace __gnu_pbds;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define ll long long
#define ld long double
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define vii vector<int>
#define vll vector<ll>
#define clr(v, d) memset(v,d,sizeof(v))
using namespace std;
const int N = 2e5 + 9;

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

    ll a,b,c;
    cin>>a>>b>>c;
    if(c&1){
        if(a>b)cout<<">\n";
        else if(b>a)cout<<"<\n";
        else cout<<"=\n";
    }
    else{
        a=abs(a);
        b=abs(b);
        if(a>b)cout<<">\n";
        else if(b>a)cout<<"<\n";
        else cout<<"=\n";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

struct Union_Find_Tree{
    vector<int> data;
    const int n;

    Union_Find_Tree(int n) : n(n){
        data.assign(n, -1);
    }

    int root(int x){
        if(data[x] < 0) return x;
        return data[x] = root(data[x]);
    }

    int operator [] (int i) {return root(i);}

    bool unite(int x, int y){
        x = root(x), y = root(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y], data[y] = x;
        return true;
    }

    int size(int x) {return -data[root(x)];}

    bool same(int x, int y) {return root(x) == root(y);}

    void clear() {fill(begin(data), end(data), -1);}
};

int main(){
    int H, W; cin >> H >> W;

    vector<string> S(H);
    rep(i, H) cin >> S[i];

    vector<vector<int>> xs(H), ys(W);
    rep(i, H){
        xs[i].eb(0);
        rep2(j, 1, W-2){
            if(S[i][j] == '#') xs[i].eb(j);
        }
        xs[i].eb(W-1);
    }

    rep(j, W){
        ys[j].eb(0);
        rep2(i, 1, H-2){
            if(S[i][j] == '#') ys[j].eb(i);
        }
        ys[j].eb(H-1);
    }

    vector<vector<bool>> used(H, vector<bool>(W, false));
    vector<bool> ux(H, false), uy(W, false);
    queue<pii> que;

    used[0][0] = true;

    que.emplace(0, 0);
    while(!que.empty()){
        auto [x, y] = que.front(); que.pop();
        ux[x] = true, uy[y] = true;

        if(y > 0){
            auto it = lower_bound(all(xs[x]), y);
            --it;
            int ny = *it;
            if(!used[x][ny]){
                used[x][ny] = true;
                que.emplace(x, ny);
            }
        }
        if(y < W-1){
            auto it = upper_bound(all(xs[x]), y);
            int ny = *it;
            if(!used[x][ny]){
                used[x][ny] = true;
                que.emplace(x, ny);
            }
        }

        if(x > 0){
            auto it = lower_bound(all(ys[y]), x);
            --it;
            int nx = *it;
            if(!used[nx][y]){
                used[nx][y] = true;
                que.emplace(nx, y);
            }
        }
        if(x < H-1){
            auto it = upper_bound(all(ys[y]), x);
            int nx = *it;
            if(!used[nx][y]){
                used[nx][y] = true;
                que.emplace(nx, y);
            }
        }
    }

    vector<int> X, Y;
    rep(i, H) if(!ux[i]) X.eb(i);
    rep(i, W) if(!uy[i]) Y.eb(i);

    int A = sz(X), B = sz(Y);

    if(A == 0 || B == 0) {cout << "0\n"; return 0;}

    Union_Find_Tree uf(A+B);
    rep(i, A){
        rep(j, B){
            if(S[X[i]][Y[j]] == '#') uf.unite(i, A+j);
        }
    }

    int a = 0, b = 0;
    rep(i, A){
        bool flag = true;
        rep(j, i){
            if(uf.same(i, j)) flag = false;
        }
        a += flag;
    }
    rep(i, B){
        bool flag = true;
        rep(j, i){
            if(uf.same(A+i, A+j)) flag = false;
        }
        b += flag;
    }

    cout << min(a, b) << '\n';
}

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

std::pair<int,int> next[1010][1010];
std::pair<int,int> find(std::pair<int,int> P)
{
	if(next[P.first][P.second]==P) return P;
	else return next[P.first][P.second] = find(next[P.first][P.second]);
}

char x[1010][1010];
int check[1010][1010];
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	for(int i=1;i<=a;i++) scanf("%s",x[i]+1);
	x[1][1] = '#';
	x[1][b] = '#';
	x[a][1] = '#';
	x[a][b] = '#';
	for(int i=1;i<=a;i++) for(int j=1;j<=b;j++) next[i][j] = std::make_pair(i,j);

	int count1 = 0, count2 = 0;
	for(int i=1;i<=a;i++)
	{
		int last = -1;
		for(int j=1;j<=b;j++)
		{
			if(x[i][j]=='#')
			{
				if(last!=-1)
				{
					std::pair<int,int> s = std::make_pair(i,last);
					std::pair<int,int> t = std::make_pair(i,j);
					s = find(s);
					t = find(t);
					if(s.first != t.first || s.second != t.second) next[s.first][s.second] = t;
				}
				last = j;
			}
		}
		if(last==-1) count1++;
	}

	for(int j=1;j<=b;j++)
	{
		int last = -1;
		for(int i=1;i<=a;i++)
		{
			if(x[i][j]=='#')
			{
				if(last!=-1)
				{
					std::pair<int,int> s = std::make_pair(last,j);
					std::pair<int,int> t = std::make_pair(i,j);
					s = find(s);
					t = find(t);
					if(s.first != t.first || s.second != t.second) next[s.first][s.second] = t;
				}
				last = i;
			}
		}
		if(last==-1) count2++;
	}

	int ans = 0;
	for(int i=1;i<=a;i++)
	{
		for(int j=1;j<=b;j++)
		{
			if(x[i][j]=='#')
			{
				std::pair<int,int> s = std::make_pair(i,j);
				s = find(s);
				if(s==std::make_pair(i,j) && check[i][j]==0)
				{
					//printf("%d %d??\n",i,j);
					ans++;
					check[i][j] = 1;
				}
			}
		}
	}
	int ans2 = count1<count2?count1:count2;
	if(ans2>0) printf("%d",ans+ans2-1);
	else printf("%d",ans-1);
}


#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) { cout << (val ? yes : no) << "\n"; }

void say(bool val, string yes = "Yes", string no = "No") { say<string>(val, yes, no); }

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

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

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 2 "main.cpp"

const std::string FIRST = "First";
const std::string SECOND = "Second";
void solve(int n, const std::vector<ll> &a) {

  if (a.size() % 2 == 0) {
    map<int, int> mp;
    REP(i, n) { mp[a[i]]++; }
    for (const auto [key, val] : mp) {
      if (val % 2 != 0) {
        cout << FIRST << endl;
        return;
      }
    }
    cout << SECOND << endl;
  } else {
    cout << SECOND << endl;
  }
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  int t;
  cin >> t;
  REP(i, t) {
    int n;
    cin >> n;
    vl a(n);
    cin >> a;
    solve(n, a);
  }

  return 0;
}

// Artur Kraska, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007LL

using namespace std;

long long a;

int solve()
{
    cin >> a;
    if(a%4 == 0)
        printf("Even\n");
    else if(a%2 == 0)
        printf("Same\n");
    else
        printf("Odd\n");
    return 0;
}

int main()
{
    testy()
        solve();

    return 0;
}


/****************************************************************************************************************************/
/*************************************                 My Template Start                *************************************/
/****************************************************************************************************************************/
#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include<bits/stdc++.h>

#define all(x)          (x).begin(),(x).end()
#define sortall(x)      sort(all(x))
#define reverseall(x)   reverse(all(x))
#define siz(x)          (int)(x).size()
#define rep(i, n)       for(int i=0;i<(n);i++)
#define repi(i, l, r)   for(int i=l;i<=(r);i++)
#define V1(t)           vector<t>
#define V2(t)           vector<vector<t>>>
#define V3(t)           vector<vector<vector<t>>>
#define VV1(t, a)       vector<t>(a)
#define VV2(t, a, b)    vector<vector<t>>(a, VV1(t, b))
#define VV3(t, a, b, c) vector<vector<vector<t>>>(a, VV2(t, b, c))

#define pb push_back
#define eb emplace_back

#define fi first
#define se second

using namespace std;
using ull = unsigned long long;
using ll  = long long;
using ld  = long double;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using pil = pair<int, long long>;
using pli = pair<long long, int>;
using vi  = vector<int>;
using vd  = vector<double>;
using vl  = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;

#ifdef LOCAL
template<typename Type> void _DEBUG(Type arg) { cerr << arg; }
template<typename Type, typename... Types> void _DEBUG(Type arg, Types ...args) { cerr << arg << ", "; _DEBUG(args...); }
#define debug(...) cerr << "[ " << #__VA_ARGS__ << " ] : ", _DEBUG(__VA_ARGS__)
void _DEBUG_START() { cerr << "\n<===== DEBUG START ======>\n"; }
void _DEBUG_END()   { cerr << "\n<====== DEBUG END =======>\n"; }

#define debug2(...) _DEBUG_START(), debug(__VA_ARGS__), _DEBUG_END()
#else
#define debug(...)
#define debug2(...)
#endif

constexpr char ln  = '\n';
constexpr char sp  = ' ';
constexpr int dy[] = {-1,1,0,0,-1,-1,1,1};
constexpr int dx[] = {0,0,-1,1,-1,1,-1,1};
const ld PI        = acos((ld)-1);

template<int fp> struct fastio { fastio() { ios::sync_with_stdio(false); cin.tie(0); if(fp)cout<<fixed<<setprecision(fp); } };
template<typename Type> void input(Type &arg){cin >> arg;}
template<typename Type, typename... Types> void input(Type &arg, Types &...args){cin>>arg;input(args...);}
template<typename Type> void output(Type arg){cout<<arg;}
template<typename Type, typename... Types> void output(Type arg, Types ...args){cout<<arg;output(args...);}
template<typename Type> inline ostream& operator<<(ostream &out, const vector<Type> &v) { for(auto &i: v) out<<i<<' '; return out; }
template<typename Type> inline istream& operator>>(istream &in, vector<Type> &v)       { for(auto &i: v) in>>i; return in; }

template<typename T>
T power(T a, T b, T mod=numeric_limits<T>::max()){
    if(b == 0)return 1;
    if(b % 2)return a * power(a, b-1, mod) % mod;
    return power(a * a % mod, b >> 1, mod);
}

/****************************************************************************************************************************/
/*************************************                   My Template End                *************************************/
/****************************************************************************************************************************/

bool slope(pii a, pii b){
    int x = abs(a.fi - b.fi);
    int y = abs(a.se - b.se);
    // -1 ≤ y / x ≤ 1
    return -x <= y && y <= x;
}

int main(){
    fastio<0>();

    int n; cin >> n;
    vii v(n);
    rep(i, n){
        int x, y; cin >> x >> y;
        v[i] = pii(x, y);
    }

    int cnt = 0;
    rep(i, n){
        repi(j, i + 1, n - 1){
            cnt += slope(v[i], v[j]);
        }
    }

    cout << cnt << ln;
    return 0;
}

#include<bits/stdc++.h>
//Everything is theoretically impossible untill it is done
#define ll long long
#define vecto vector<int>
#define set set<int>
#define stack stack<int>
#define io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pp pair<int,int>
#define mk make_pair
#define mod 10000007
#define MAX INT_MAX
#define MIN INT_MIN
#define w(x) int x; cin>>x; while(x--)
#define set_built(x) __builtin_popcountll(x)
#define zero_built(x) __bultin_ctzll(x)
using namespace std;
int sum(int n){
    int p=0;
    while(n!=0){
        int k;
        k=n%10;
        p=p+k;
        n=n/10;
    }
    return p;
}

// Asli function
void solve(){
    vector<pair<double,double>>v;
   int t;
   cin>>t;
   while(t--){
       double x,y;
       cin>>x>>y;
       v.push_back(mk(x,y));
   }
   int count=0;
   for(int i=0; i<v.size()-1; i++){
       for(int j=i+1; j<v.size(); j++){
           double t;
           t=(v[j].second-v[i].second)/(v[j].first-v[i].first);
           if(t>=-1&&t<=1)
           count++;
       }
   }
   cout<<count<<endl;

}

//naam ka function
int main(){
        solve();
    return 0;
}


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

#define ub              upper_bound
#define lb              lower_bound
#define isrt            insert
#define clr             clear
#define rsz             resize
#define ff              first
#define ss              second
#define int             long long int
#define pb              push_back
#define pf              push_front
#define mkp             make_pair
#define mkt             make_tuple
#define pii             pair<int,int>
#define tii             tuple<int,int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int>>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define INF             1e9
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define all(v)          v.begin(),v.end()
#define rall(v)         v.rbegin(),v.rend()
#define endl            "\n"
using cd = complex<double>;
const double PI = acos(-1);
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int maxm(int a, int b)
{
    return (a >= b ? a : b);
}

int minm(int a, int b)
{
    return (a <= b ? a : b);
}

int power(int x, int y, int p)
{
    int res = 1;
    x = x % p;
    if (x == 0)
        return 0;

    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

#define FMAX 2000005
vi fact(FMAX, 1);
vi fact_inv(FMAX, 1);
vi lp(FMAX);
vi pr;

int nCrmodp(int n, int r, int p)
{
    if (r > n)
        return 0;
    int pro = (fact[n] * fact_inv[r]) % p;
    pro = (pro * fact_inv[n - r]) % p;
    return pro;
}
void precompute()
{
    int x = 1;
    for (int i = 1; i < FMAX; ++i)
    {
        x *= i;
        x %= mod;
        fact[i] = x;
        fact_inv[i] = power(x, mod - 2, mod);
    }
}

void spf_calc()
{
    for (int i = 2; i < FMAX; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
            pr.push_back (i);
        }
        for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] < FMAX; ++j)
            lp[i * pr[j]] = pr[j];
    }
}

int next_smaller(vector<int>v, int val)
{
    int l = 0, r = v.size() - 1, ans = -1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (v[mid] <= val)
        {
            ans = mid;
            l = mid + 1;
        }
        else
            r = mid - 1;
    }
    return ans;
}

void c_p_c()
{
    precompute();
    //spf_calc();
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

signed main()
{
    c_p_c();

    int n, k, m;
    cin >> n >> m >> k;

    if (n > (m + k))
        cout << "0\n";
    else
    {
        int tot = nCrmodp(n + m, n, mod);
        tot -= nCrmodp(n + m, m + k + 1, mod);
        tot = (tot + mod) % mod;
        cout << tot << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define REP(a,b) for(int a=0;a<(b);++a)
#define REP1(i,n) for(int i=1;i<=(n);++i)
#define debug(x) cerr<<#x<<": "<<x<<'\n'
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isPossible(x) printf("%s\n",(x) ? "Possible" : "Impossible")
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
// #define INF (1<<9+9)
const int INF = 0x3fffffff;
// const long long INF = 1LL<<50;

#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define pie 3.14159265358979323846

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
template<class T=int>
T in(){T x;cin>>x;return (x);}
template<class T>
void print(T& x){cout<<x<<'\n';}

const int MOD =(int)998244353;
// const int mod =(int)998244353;
const int mod =(int)1e9+7;

const int MAX =510000;
ll fac[MAX],finv[MAX],inv[MAX];
void COMint(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(int i=2;i<MAX;i++){
        fac[i]=fac[i-1]*i%MOD;
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}
ll COM(int n,int k){
    if(n<k) return 0;
    if(n<0||k<0)return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
ll gcd(ll a,ll b){
    if(a<0)a=-a;
    if(b<0)b=-b;
    if(b==0)return a;
    if(a>b){
        swap(a,b);
    }
    return gcd(a,b%a);
}
ll lcm(ll a,ll b){
  if(a<0)a=-a;
  if(b<0)b=-b;
    ll g;g=gcd(a,b);
    return b/g*a;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
    if(a.second != b.second){
        return a.second < b.second;
    }else{
        return a.first < b.first;
    }
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
    if(a.first != b.first){
        return a.first < b.first;
    }else{
        return a.second < b.second;
    }
}
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
//int N, M, K, T, H, W, L, R;
// long long int N, M, K, T, H, W, L, R
ll RS(ll N,ll P){
    if(P==0){
        return 1;
    }else{
        if(P%2==0){
            ll t=RS(N,P/2);
            return t*t;
        }else{
            return N*RS(N,P-1);
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    int N;
    cin>>N;
    double x0,y0,xn2,yn2;
    cin>>x0>>y0>>xn2>>yn2;
    double ox,oy;
    ox=(x0+xn2)/2;
    oy=(y0+yn2)/2;
    debug(ox);debug(oy);
    double c,s;
    c=cos(2*pie/N);
    s=sin(2*pie/N);
    double ansx,ansy;
    ansx=(x0-ox)*c-(y0-oy)*s;
    ansy=(x0-ox)*s+(y0-oy)*c;
    ansx+=ox;
    ansy+=oy;
    cout<<setprecision(25)<<ansx<<" "<<ansy<<endl;
    return 0;

}


#include <iostream>
#include <vector>
#include <map>
#include <tuple>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
const double eps = 1e-10;

int main() {
    ll n, x, v, p;
    scanf("%lld", &n);
    scanf("%lld", &x);

    ll sum = 0;
    int result = -1;
    for(int i=0; i<n; i++) {
        scanf("%lld", &v);
        scanf("%lld", &p);
        sum += v * p;
        if(float(sum) / float(100) > x) {
            result = i+1;
            break;
        }
    }

    cout << result << '\n';
    return 0;

}

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

#define endl '\n'
#define FastIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
typedef long long ll;
int main(){
  FastIO;

  ll n,x;
  cin>>n;
  int c=0;
  string s,s1,sf;
  for(ll i=1;i<=1000000;i++){
    s=to_string(i);
    s1=to_string(i);
    sf=s+s1;
    stringstream str(sf);
    str>>x;
    if(x<=n){
      c++;
    }
  }
  std::cout << c << '\n';
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
//bool vis[1000];
int x[]={1,-1,0,0};
int y[]={0,0,1,-1};
//int f=0;
//int t[500000];
//int ll[500000];
//"'';

int main(){
int r,c,n,m;
cin>>r>>c>>n>>m;
int dp[r][c][4];
memset(dp,0,sizeof(dp));
vector<vector<int> > v(r,vector<int>(c,0));
queue<vector<int> > q;
int ans=0;
for(int i=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    v[a][b]=1;
    for(int j=0;j<4;j++){
        q.push({a,b,j});
        dp[a][b][j]=1;
    }
}
for(int i=0;i<m;i++){
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    v[a][b]=-1;
}
while(!q.empty()){
    vector<int> u=q.front();
    q.pop();
    int na=u[0]+x[u[2]];
    int nb=u[1]+y[u[2]];
    int nd=u[2];
    if(na<r&&na>=0&&nb<c&&nb>=0&&dp[na][nb][nd]==0&&v[na][nb]!=-1){
        dp[na][nb][nd]=1;
        q.push({na,nb,nd});
    }
}
for(int i=0;i<r;i++){
    for(int j=0;j<c;j++){
        int f=0;
        for(int k=0;k<4;k++){
            f=max(f,dp[i][j][k]);
        }
        ans+=f;
    }
}
cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i = k ;i<=n;i++)
#define FOR(i,n,k) for(int i = n;i>=k;i--)
#define ll long long
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define mems(a,x) memset(a,x,sizeof(a))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>  pii;
typedef pair<ll, ll>    pl;
typedef vector<int>     vi;
typedef vector<ll>      vl;
typedef vector<pii>     vpii;
typedef vector<pl>      vpl;
typedef vector<vi>      vvi;
typedef vector<vl>      vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
    uniform_int_distribution<int> uid(0, lim - 1);
    return uid(rang);
}
const int mod = 1000000007;
const int N = 3e5, M = N;

//vi g[N];
//int a[N];

void solve()
{
    int h, w, n, m, i, j;
    cin >> h >> w >> n >> m;

    int a[h][w] = {0};
    int dp[h][w] = {0};

    clr(a);
    clr(dp);

    fo(i, n)
    {
        int x, y;
        cin >> x >> y;
        x--; y--;
        a[x][y] = 1;
    }
    fo(i, m)
    {
        int x, y;
        cin >> x >> y;
        x--; y--;
        a[x][y] = -1;

    }

    fo(i, h)
    {
        fo(j, w)
        {
            if (j != 0 && (a[i][j - 1] == 1 || a[i][j - 1] == 2) && a[i][j] == 0)
            {    a[i][j] = 2;}
        }
        FOR(j, w - 2, 0)
        {
            if ((a[i][j + 1] == 1 || a[i][j + 1] == 2) && a[i][j] == 0)
            {a[i][j] = 2;}
        }
    }

    /* fo(i, h)
     {
         fo(j, w)
         cout << a[i][j] << " ";
         cout << endl;
     }*/
    fo(i, w)
    {
        fo(j, h)
        {
            if (j != 0 && (a[j - 1][i] == 1 || a[j - 1][i] == 3) && (a[j][i] == 0 || a[j][i] == 2))
            {
                //cout << j << " " << i << endl;
                a[j][i] = 3;
            }
        }
        FOR(j, h - 2, 0)
        {
            if ((a[j + 1][i] == 1 || a[j + 1][i] == 3) && (a[j][i] == 0 || a[j][i] == 2))
            {   //cout << j << " " << i << endl;
                a[j][i] = 3;
            }
        }
    }

    int ans = 0;
    fo(i, h)
    {
        fo(j, w)
        {
            //cout << a[i][j] << " ";
            ans += (a[i][j] == 1 || a[i][j] == 2 || a[i][j] == 3);
        }
        //cout << endl;
    }
    cout << ans << endl;

}

int main() {
    //ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

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

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define cnts(x,c) count(all(x),c)
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) {a = b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) {a = b; return 1;} return 0;}
template<class T> void print(const T &t) {cout<<t<<"\n";}
template<class T> void PRINT(const T &t) {rep(i,sz(t)) cout<<t[i]<<" \n"[i==sz(t)-1];}
const ll INF = 1LL << 62;
const int iINF = 1 << 30;

using Graph = vector<vector<int>>;
int n,m;

int BFS(const Graph &G, int s) {
    int n = sz(G);
    vector<int> dist(n,-1);
    queue<int> que;

    dist[s]=0;
    que.push(s);

    while(!que.empty()) {
        int v=que.front();
        que.pop();
        fore(nv,G[v]) {
            if (dist[nv]!=-1) continue;
            dist[nv]=dist[v]+1;
            que.push(nv);
        }
    }
    int cnt=0;
    fore(x,dist) if(x!=-1) cnt++;
    return cnt;
}

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

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

#define mp make_pair
#define fi first
#define se second
const int maxn=100000;
typedef pair<int,int> P;
vector<P>g[maxn+5];
int vis[maxn+5];
int pr[maxn+5];
int col[maxn+5];
void dfs(int x,int pre,int c)
{
    vis[x]=1;
    pr[c]=1;
    if(col[pre]!=c)
        col[x]=c;
    for(int i=0; i<g[x].size(); i++)
    {
        P tmp=g[x][i];
        if(vis[tmp.fi])
            continue;
        dfs(tmp.fi,x,tmp.se);
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        g[u].push_back(mp(v,c));
        g[v].push_back(mp(u,c));
    }
    dfs(1,0,0);
    int tmp;
    for(int i=1;i<=n;i++)
    {
        if(!pr[i])
        {
            tmp=i;
            break;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!col[i])
            printf("%d\n",tmp);
        else
            printf("%d\n",col[i]);
    }
}


#include <bits/stdc++.h>

using namespace std;

// a + (a+1) + ... + b
long sum(const long &a, const long &b) { return (a + b) * (b - a + 1) / 2; }

int main() {
  int T;
  cin >> T;
  while (T--) {
    long L, R;
    cin >> L >> R;
    const long minC = L;
    const long maxC = R - L;
    if (minC > maxC) {
      cout << 0 << endl;
    } else {
      cout << sum(R - maxC - L + 1, R - minC - L + 1) << endl;
    }
  }
  return 0;
}

//コンパイラ最適化
#pragma GCC optimize("Ofast")

//インクルードなど
#include<bits/stdc++.h>

using namespace std;

//エイリアス
using ll = long long;
using ld = long double;
using Graph = vector<vector<ll>>;

//イテレーション
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007 //10^9+7:合同式の法 問題による
#define MAXR 100000 //10^5:配列の最大のrange
#define DDD fixed << setprecision(20)
//略記
//#define F first
//#define S second

//グリッドの各方向に対する差分配列
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

//myceil : a/b 以上の最小整数(負の数対応)
//myfloor: a/b 以下の最大整数(負の数対応)
ll myceil(ll a,ll b){if(a%b==0)return a/b;ll cnt=0;if(a<0){a=-a;cnt++;}if(b<0){b=-b;cnt++;}if(cnt%2==0)return(a+(b-1))/b;return-(a/b);}
ll myfloor(ll a,ll b){if(a%b==0)return a/b;ll cnt=0;if(a<0){a=-a;cnt++;}if(b<0){b=-b;cnt++;}if(cnt%2==0)return a/b;return-(a+(b-1))/b;}
// myupper: nで割り切れる a以上の最小整数(n: 負の数未対応)
// mylower: nで割り切れる a以下の最大整数(n: 負の数未対応)
ll myupper(ll a, ll n){if(a%n == 0) return a;if(a > 0) return a+n-a%n;if(a < 0) return a-a%n;return a;}
ll mylower(ll a, ll n){if(a%n == 0) return a;if(a < 0) return a-n-a%n;if(a > 0) return a-a%n;return a;}

/*
#include<boost/multiprecision/cpp_dec_float.hpp>
// 精度を落とすキャストは明示 : static_cast<型>(変数)
using namespace boost::multiprecision;
using f50 = cpp_dec_float_50;
using f100 = cpp_dec_float_100;
*/

signed main(){
  ll t; cin >> t;
  REP(i, t){
    ll l, r; cin >> l >> r;
    if(l <= r-l && r-l <= r) cout << (r-2*l+1)*(r-2*l+2)/2 << endl;
    else cout << 0 << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define ff(i,n) for(i=0;i<n;++i)
#define fff(i,a,b) for(i=a;i<b;++i)
#define pb push_back
#define mp make_pair
#define N 1000050

ll lpf[N], mobius[N];
ll fre[N];

// Function to calculate least
// prime factor of each number
void least_prime_factor()
{
    for (ll i = 2; i < N; i++)

        // If it is a prime number
        if (!lpf[i])

            for (ll j = i; j < N; j += i)

                // For all multiples which are not
                // visited yet.
                if (!lpf[j])
                    lpf[j] = i;
}

// Function to find the value of Mobius function
// for all the numbers from 1 to n
void Mobius()
{
    for (ll i = 1; i < N; i++) {

        // If number is one
        if (i == 1)
            mobius[i] = 1;
        else {

            // If number has a squared prime factor
            if (lpf[i / lpf[i]] == lpf[i])
                mobius[i] = 0;

            // Multiply -1 with the previous number
            else
                mobius[i] = -1 * mobius[i / lpf[i]];
        }
    }
}

// Function to find the number of pairs
// such that gcd equals to 1
ll gcd_pairs(vector<ll> a, ll n)
{
    // To store maximum number
    ll maxi = 0;

    // Find frequency and maximum number
    for (ll i = 0; i < n; i++) {
        fre[a[i]]++;
        maxi = max(a[i], maxi);
    }

    least_prime_factor();
    Mobius();

    // To store number of pairs with gcd equals to 1
    ll ans = 0;

    // Traverse through the all possible elements
    for (ll i = 1; i <= maxi; i++) {
        if (!mobius[i])
            continue;

        ll temp = 0;
        for (ll j = i; j <= maxi; j += i)
            temp += fre[j];

        ans += temp * (temp - 1) / 2 * mobius[i];
    }

    // Return the number of pairs
    return ans;
}

// Driver code
vector<ll> arr;
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll l,r;
    cin>>l>>r;
    if(l==1)
    	l++;
    if(r==1)
		r++;
    ll n = r-l+1,i;
    for(i=l;i<=r;i++){
		arr.pb(i);
	}
	ll div=0,gcd1 = gcd_pairs(arr,n);
	ll j;
	for(i=l;i<=r;i++){
		ll f = i,mult=1;
		while((f*mult)<=r){
			if(mult>1)
				div++;
			mult++;
		}
	}
	n = r-l+1;
	ll ans = (n*(n-1))/2 - gcd1-div;
	cout<<(2*ans);
	return 0;
}

//GIVE ME AC!!!!!!!!!!!!!!!!!
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll int
#define ld long double
#define floatset() fixed<<setprecision(15)
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define pb push_back
#define eb emplace_back
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using vl=vector<ll>;
using vi=vector<int>;
using vs=vector<string>;
using vc=vector<char>;
using vvl=vector<vl>;
using P=pair<ll,ll>;
using vvc=vector<vc>;
using vd=vector<double>;
using vp=vector<P>;
using vb=vector<bool>;
using P=pair<ll,ll>;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
template<typename T1,typename T2 >
ostream &operator<<(ostream&os,const pair<T1,T2>&p) {
  os<<p.first<<" "<<p.second;
  return os;
}
template<typename T1,typename T2>
istream &operator>>(istream&is,pair<T1,T2>&p) {
  is>>p.first>>p.second;
  return is;
}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&v) {
  for(int i=0;i<(int)v.size();i++) {
    os<<v[i]<<(i+1!=v.size()?" ":"");
  }
  return os;
}
template<typename T>
istream &operator>>(istream&is,vector<T>&v) {
  for(T &in:v)is>>in;
  return is;
}
void scan(){}
template<class Head,class... Tail>
void scan(Head&head,Tail&... tail) {
    cin>>head;
    scan(tail...);
}
template<class T>
void print(const T &t) { cout << t << '\n'; }
template<class Head, class... Tail>
void print(const Head &head, const Tail &... tail) {
    cout << head << ' ';
    print(tail...);
}
template<class... T>
void fin(const T &... a) {
    print(a...);
    exit(0);
}
template<typename T1,typename T2>
inline bool chmax(T1&a,T2 b){return a<b&&(a=b,true);}
template<typename T1,typename T2>
inline bool chmin(T1&a,T2 b){return a>b&&(a=b,true);}
vector<ll>prime_factorize(ll N) {
  vector<ll>res;
    for (ll a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        while (N % a == 0) {
            N /= a;
        }
        res.push_back(a);
      if(N==1)break;
    }
    if (N != 1) res.push_back(N);
    return res;
}
int main(){
  LL(l,r);
  long long ans=0;
  rep(i,max(2,l),r+1){
    vl p=prime_factorize(i);
    ll cnt=0;
    ll s=p.size();
    rep(bit,1,(1<<s)){
      ll now=1;
      rep(j,0,p.size()){
        if((bit>>j)&1){
          now*=p[j];
        }
      }
      ll res=(r/now)-((i-1)/now);
      if(__builtin_popcount(bit)%2==1){
        cnt+=res;
      }
      else{
        cnt-=res;
      }
    }
    ans+=cnt;
  }
  rep(i,max(2,l),r+1){
    ans-=r/i;
  }
  fin(ans*2);
}


// #include <bits/stdc++.h>
// #include <atcoder/all>

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#include <cctype>
#include <map>
#include <queue>
#include <limits>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <locale>

using namespace std;
// using namespace atcoder;

const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define rep(i,n) for (int i = 0; i < (n); i++)
#define ll long long
#define P pair<int, int>

// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif

const double PI = acos(-1);

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

	int total = pow(2, n);
	vector<ll> a(total);
	vector<ll> save;

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

	copy(a.begin(), a.end(), back_inserter(save));

	int result = 0;

	while (true)
	{
		vector<int> removeIndexs;
		for (int i = 0; i < a.size() - 1; i+=2)
		{
			if (a[i] - a[i+1] > 0)
			{
				removeIndexs.push_back(i+1);
			}
			else
			{
				removeIndexs.push_back(i);
			}
		}

		if (a.size() == 2)
		{
			result = a[removeIndexs.back()];
			break;
		}

		for (int i = removeIndexs.size() - 1; i != static_cast<decltype(i)>(-1); i--)
		{
			a.erase(a.begin() + removeIndexs[i]);
		}
	}

	for (int i = 0; i < save.size() ; i++)
	{
		if (save[i] == result)
		{
			cout << i+1 << endl;
			break;
		}
	}

	return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int n;
    cin >> n;
    vector<int> v(1 << (n+1));
    int p = (1 << n);
    vector<int> a(p);
    map<int, int> aa;
    for (int i = 0; i < p; ++i) {
        scanf("%d", &v[i+p]);
        aa[v[i+p]] = i;
    }
    for (int i = p-1; i > 0; --i) {
        v[i] = max(v[i*2],v[i*2+1]);
    }
    cout << aa[min(v[2], v[3])]+1 << "\n";
    return 0;
}