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

void solve(){
    int n;
    cin >> n;
    set<ll> st;
    vector<vector<ll>> a(n, vector<ll>(5));
    rep(i,n){
        rep(j,5){
            cin >> a[i][j];
            st.insert(a[i][j]);
        }
    }
    ll ans = 0;
    vector<ll> S;
    for(auto p : st) S.push_back(p);
    int ok = 0, ng = S.size();
    while(ng - ok > 1){
        int mid = (ok + ng) / 2;
        vector<vector<bool>> v(4, vector<bool>(1<<5, false));
        v[0][0] = true;
        rep(i,n){
            for(int j = 2; j >= 0; --j){
                rep(k,(1<<5)){
                    if(!v[j][k]) continue;
                    int nj = j+1;
                    int nk = k;
                    rep(l,5){
                        if(a[i][l] >= S[mid]){
                            nk |= (1<<l);
                        }
                    }
                    v[nj][nk] = true;
                }
            }
        }
        if(v[3][(1<<5)-1]){
            ok = mid;
        }
        else{
            ng = mid;
        }
    }
    cout << S[ok] << endl;
}

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

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned int
#define test int t;cin>>t; while(t--)
#define en '\n'
typedef pair<int, int>     pii;
typedef pair<char, int>    pci;
typedef pair<char, char>   pcc;
typedef vector<pii>       vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(x) (int)x.size()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007

void __print(int x) {cerr << x;}
void __print(float 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 char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
// You should only debug a pair of simple data types. For example,
// the debug won't work if one of pair's elements is collection type
// (std::vector, std::map, std::set...).
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 &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

// bool check_prime(int n)  return whether a number is prime or not O(sqrt(N));
// int BE(int x,int n,int m)  return x^n%m; O(logN);
// void sieve()  from number 1-1000001 saare prime store kar lega  in bool is_prime[1000001] array
// vector<int> z_array(string s)  return vector which is Z-array of string s;
// vector<int> lps(string s)      return vector which is lps array of string s;
// int power(int x,int n) return x^n; O(logN);
// using_ordered_set (template for using ordered set , replace first 2 lines of this page with this code..);
const int gf = 1e6 + 9;
int a[gf][5];
int power(int x, int n) //function to calculate x raise to power n
{
	int result = 1;
	while (n > 0)
	{
		if (n % 2 == 1)
			result = result * x;
		x = x * x;
		n = n / 2;
	}
	return result;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	if (fopen("input.txt", "r"))
	{
		freopen ("input.txt" , "r" , stdin);
		freopen ("output.txt" , "w" , stdout);
	}
#endif
	//_______________________________-code starts-_______________________________________________

	int n;
	cin >> n;
	//int a[n], b[n], c[n], d[n], e[n];
	zip(i, 0, n)
	{
		cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4];
	}
	int l = 1, r = 1e9;
	int ans = 0;
	while (l <= r)
	{
		int m = (l + r) / 2;
		//debug(l, r);
		set<int>s;
		zip(i, 0, n)
		{
			int var = 0;
			zip(k, 0, 5)
			{
				if (a[i][k] >= m)
					var += power(2, 4 - k);
			}
			s.insert(var);
		}
		//	debug(s);
		int flag = 0;
		for (int p : s)for (int q : s)for (int r : s)
				{
					int lol = p | q | r;
					if (lol == 31)flag = 1;
				}
		debug(s, flag);

		if (flag)
		{
			ans = m;
			l = m + 1;
		}
		else
		{
			r = m - 1;
		}
	}
	cout << ans << en;

	return 0;

}


#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

//using namespace harudake;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 n,k;
  cin>>n>>k;
  vector<tuple<i64,i64>> vt;
  rep(i,n) {
    i64 a,b;
    cin>>a>>b;
    vt.emplace_back(a,b);
  }
  vt.emplace_back(0,k);
  vt.emplace_back(1'000'000'000'000'000,0);
  sort(all(vt));
  i64 sum = 0;
  i64 pos = 0;
  rep(i,n+1) {
    auto[a0,b0] = vt[i];
    auto[a1,b1] = vt[i+1];
    sum += b0;
    i64 d = a1 - a0;
    if (sum < d) break;
    pos = a1;
    sum -= d;
  }
  pos += sum;
  cout<<pos<<endl;
  return 0;
}

#include<bits/stdc++.h>
#define ulli unsigned long long int
using namespace std;
int main()
{
    ulli n,k;
    cin>>n>>k;
    vector<pair<ulli,ulli>>v;
    for(int i = 0;i<n;i++)
    {
        ulli a,b;
        cin>>a>>b;
        v.push_back({a,b});
    }
    sort(v.begin(),v.end());
    for(int i = 0;i<n;i++)
    {
        if(v[i].first > k)
            break;
        k += v[i].second;
    }
    cout<<k;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int MAXN = 510;

int A[MAXN];

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    while(T--)
    {
    	int N;
    	cin >> N;

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

    	bool flag = true;

    	vector<int> res;

    	auto op = [&](int x)
    	{
    		res.push_back(x);
    		swap(A[x],A[x+1]);
    		flag = !flag;
    	};

    	for(int x = N ; x >= 5 ; x--)
    	{
    		int pos = -1;
    		for(int i = 1 ; i <= N ; i++)
    		{
    			if(A[i] == x)
    			{
    				pos = i;
    			}
    		}

    		if(pos == 1)
    		{
    			if(!flag)op(2);
    		}
    		if(pos == 2)
    		{
    			if(flag)op(3);
    		}
    		if(pos == 3)
    		{
    			if(!flag)op(4);
    		}
    		if(pos >= 4)
    		{
    			if(pos%2 == 1)
    			{
    				if(!flag)op(2);
    			}
    			else
    			{
    				if(flag)op(1);
    			}
    		}

    		for(int i = pos ; i < x ; i++)
    		{
    			op(i);
    		}
    	}

    	if(N >= 4)
    	{
	    	int pos4 = -1;

	    	for(int i = 1 ; i <= 4 ; i++)
	    	{
	    		if(A[i] == 4)
	    		{
	    			pos4 = i;
	    		}
	    	}

	    	if(pos4 == 1)
	    	{
	    		if(!flag)op(2);
	    	}
	    	else if(pos4 == 2)
	    	{
	    		if(flag)op(3);
	    	}
	    	else if(pos4 == 3)
	    	{
	    		if(!flag)
	    		{
	    			op(2);
	    			op(3);
	    			pos4 = 2;
	    		}
	    	}
	    	else
	    	{

	    	}

	    	for(int i = pos4 ; i < 4 ; i++)
	    	{
	    		op(i);
	    	}
    	}

    	if(N >= 3)
    	{
	    	while(A[1] != 1 || A[2] != 2 || A[3] != 3)
	    	{
	    		if(flag)op(1);
	    		else op(2);
	    	}
    	}
    	else
    	{
    		while(A[1] != 1 || A[2] != 2)
	    	{
	    		if(flag)op(1);
	    	}
    	}

    	// for(int i = 1 ; i <= N ; i++)
    	// {
    	// 	cout << A[i] << " ";
    	// }
    	// cout << '\n';

    	cout << res.size() << '\n';

    	for(auto x : res)
    	{
    		cout << x << " ";
    	}
    	cout << '\n';
    }

    return 0;
}

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <numeric>

/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";}
#define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);}
#define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7
#define dbg1(x1) DBG_PRINT(#x1,x1,std::endl)
#define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2)
#define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3)
#define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4)
#define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5)
#define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6)
#define dbg(...) DBG_LINE()\
SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__)
#else
#define dbg(...) {}
#endif

/* utility functions */
struct read_item{read_item(){};
template<class T>operator T(){T t;std::cin>>t;return t;}};
char splf(int i,int n){return(i+1<n)?' ':'\n';};
template<bool up>
struct _RI{int i;_RI(int a):i(a){}
int operator*(){return i;}void operator++(){i+=(up?1:-1);}
bool operator!=(const _RI& r){return up?i<r.i:i>r.i;}};
template<bool up>
struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;}
_RX(int a,int b):x(up?a:(b-1)),y(up?b:(a-1)){}_RX(int a):_RX(0,a){}};
typedef _RX<true> range;
typedef _RX<false> revrange;

/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;

using namespace std;

bool iseven(int x) {
	return (x & 1) == 0;
}

void oesort3(vector<int>& xs, int pos, bool even, vector<int>& ops) {

	int retry = 0;

	auto check = [&]() {
		for(auto i : range(3)) {
			if(xs[pos + i] != pos + i) {
				return false;
			}
		}
		return true;
	};

	while(!check()) {

		int p = (iseven(pos) == even) ? pos : pos + 1;
		ops.push_back(p);
		swap(xs[p], xs[p + 1]);
		even = !even;

		retry += 1;
		assert(retry < 100);
	}
}

void oesort(vector<int>& xs, int pos, bool even, vector<int>& ops) {

	const int N = xs.size();

	if(N - pos == 3) {
		return oesort3(xs, pos, even, ops);
	}

	assert(N - pos >= 4);

	int target = pos;
	while(xs[target] != pos) {
		target += 1;
	}

	if(target == pos) {
		oesort(xs, pos + 1, even, ops);
		return;
	}

	int retry = 0;

	while(iseven(target - 1) != even) {
		if(target - 2 >= pos) {
			ops.push_back(target - 2);
			swap(xs[target - 2], xs[target - 1]);
			even = !even;
		} else {
			ops.push_back(target);
			swap(xs[target], xs[target + 1]);
			target += 1;
			even = !even;
		}
		retry += 1;
		assert(retry < 100);
	}

	for(auto i : revrange(pos, target)) {
		assert(iseven(i) == even);
		ops.push_back(i);
		swap(xs[i], xs[i + 1]);
		even = !even;
	}

	oesort(xs, pos + 1, even, ops);
}

void solve() {

	const int N = read_item();
	vector<int> As(N);
	for(auto i : range(N)) {
		As[i] = (int)read_item() - 1;
	}

	if(N == 2) {
		if(As[0] == 0) {
			printf("0\n");
			return;
		} else {
			printf("1\n1\n");
			return;
		}
	}

	static vector<int> ops;
	ops.resize(0);

	oesort(As, 0, true, ops);
	int c = ops.size();

	assert(c <= N * N);
	printf("%d\n", c);
	for(auto i : range(c)) {
		printf("%d%c", ops[i] + 1, splf(i, c));
	}

}

int main() {

	const int T = read_item();
	for(auto _ : range(T)) {
		solve();
	}

	return 0;
}

/* wafdayo~~~ */


#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll N, X, A[200];
ll dp[200][200][200];

signed main(){
    cin >> N >> X;
    REP(i, N) cin >> A[i];
    ll ans = LONG_LONG_MAX;
    FOR(k, 1, N + 1){
        REP(i, N + 1) REP(j, N + 1) REP(m, k) dp[i][j][m] = LONG_LONG_MAX;
        dp[0][0][X % k] = X;
        REP(i, N){
            REP(j, k + 1){
                REP(m, k){
                    if(dp[i][j][m] == LONG_LONG_MAX) continue;
                    dp[i + 1][j][m] = min(dp[i + 1][j][m], dp[i][j][m]);
                    ll d = (k + (m - A[i]) % k) % k;
                    if(j < k && dp[i][j][m] - A[i] >= 0) dp[i + 1][j + 1][d] = min(dp[i + 1][j + 1][d], dp[i][j][m] - A[i]);
                }
            }
        }
        if(dp[N][k][0] != LONG_LONG_MAX) ans = min(ans, dp[N][k][0] / k);
    }
    PRINT(ans);
    return 0;
}

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

#define forn(i,x,n) for(int i = x; i < n; ++i)

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
	int n,m,s,g;

	cin>>n>>m;
	vector<int> dist(n*m);
	vector<bool> done(n*m,false);
	vector<vector<int>> adj(n*m);
	unordered_map<char,vector<int>> teleport;
	vector<string> a(n);
	forn(i,0,n)
		cin>>a[i];
	forn(i,0,n)
		forn(j,0,m){
			dist[i*m+j]=INT_MAX;
			if(a[i][j]!='#' && a[i][j]!='.')
				teleport[a[i][j]].push_back(i*m+j);
			if(a[i][j]=='S')
				s=i*m+j;
			if(a[i][j]=='G')
				g=i*m+j;
			if(i && a[i-1][j]!='#')
				adj[i*m+j].push_back((i-1)*m+j);
			if(j && a[i][j-1]!='#')
				adj[i*m+j].push_back(i*m+j-1);
			if(i<n-1 && a[i+1][j]!='#')
				adj[i*m+j].push_back((i+1)*m+j);
			if(j<m-1 && a[i][j+1]!='#')
				adj[i*m+j].push_back(i*m+j+1);
		}

	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
    dist[s]=0;
    q.push({dist[s],s});

    while(q.size()){
        int f = q.top().second;
        q.pop();
        // if(done[f])
        // 	continue;
        done[f]=true;
        for(auto v: adj[f])
            if(dist[v]>dist[f]+1)
                dist[v]=dist[f]+1, q.push({dist[v],v});
        if(teleport.find(a[f/m][f%m])!=teleport.end()){
        	for(auto v:teleport[a[f/m][f%m]])
	        	if(dist[v]>dist[f]+1)
	                dist[v]=dist[f]+1, q.push({dist[v],v});
        	teleport.erase(a[f/m][f%m]);
        }
    }
	cout<<(dist[g]==INT_MAX?-1:dist[g]);
}


#include <bits/stdc++.h>
using namespace std;
long long i,j,p,l,t;
long long c[234567],a[213167],b[211167],d,e[234567],tt=0,k=0,y,z,n=0,x;
char m;
string q,w,s;
int main(){
    cin >>t>>l>>p;
    cout<<t+l+p-min(t,min(l,p));
}

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

int main(void)
{
    long long x,y;
    cin >> x >> y;
    long long a[3]={0};

    if(x==y) cout<<x<<endl;
    else
    {
        a[x]++;
        a[y]++;

        for(long long i=0;i<3;i++)
        {
            if(a[i]==0) cout<<i<<endl;
        }
    }

    return 0;
}


/** Fucc :/
 - doanphuc : /
	** author: doanphuc. **
**/
#include <bits/stdc++.h>
#define TASK ""
#define inpf freopen(TASK".inp", "r", stdin)
#define outf freopen(TASK".out", "w", stdout)
#define FOR(i,x,y) for(int i = x; i <= y ; i ++)
#define FORT(i,x,y) for(int i = x; i < y ; i ++)
#define FORD(i,x,y) for(int i = x; i >= y ; i --)
#define clean(x) memset(x, 0, sizeof(x))
#define F first
#define S second
#define maxn 3000005
#define pii pair<ll,ll>
#define ll long long
#define pb push_back
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define all(x) (x).begin(), (x).end()
#define pia pair<pii, int>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std; const ll MOD = 1e9 + 7 ;
ll n, a[maxn], b[maxn], gt[maxn], d[maxn] ;
int main()
{
    cin >> n;
    FOR(i, 1, n) cin >> a[i] ; FOR(i, 1, n) cin >> b[i] ;
    ll res = 0 ;
    FOR(i, 1, n)
    {
        res = max(res, a[i]) ; gt[i] = res ;
    }
    res = 0 ;
    FOR(i, 1, n)
    {
        ll x = b[i] * gt[i] ;
        if(x > res) res = x ;
        d[i] = res ;
    }
    FOR(i, 1, n) cout << d[i] << endl ;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std; using ll = long long;
using ull = unsigned long long; using P = pair<int, int>;
const int INF = 1e9; const int MOD = 1000000007;
const int dx[] = {-1,0,1,0}; const int dy[] = {0,-1,0,1};
// const int dx[] = {-1,-1,-1, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1,-1, 1,-1, 0, 1};
#define PI 3.14159265358979323846264338327950L
// static const double e = 2.718281828459045;
//cout << fixed << setprecision(10) << ans << endl;
vector<ll> A, B;
struct UnionFind {
    vector<int> par, rnk, mem;
    vector<ll> sum_a, sum_b;
    UnionFind(int n) {
        par.resize(n); fill(par.begin(), par.end(), -1);
        rnk.resize(n); fill(rnk.begin(), rnk.end(),  0);
        mem.resize(n); fill(mem.begin(), mem.end(),  1);
        sum_a.resize(n);
        rep(i, n) sum_a[i] = A[i];
        sum_b.resize(n);
        rep(i, n) sum_b[i] = B[i];
    }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    void merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return;
        if (rnk[x] < rnk[y]) {
            par[x] = y; mem[y] += mem[x];
            sum_a[y] += sum_a[x];
            sum_b[y] += sum_b[x];
        } else {
            par[y] = x; mem[x] += mem[y];
            sum_a[x] += sum_a[y];
            sum_b[x] += sum_b[y];
            if (rnk[x] == rnk[y]) rnk[x]++;
        }
    }
    int size(int x) {
        return mem[root(x)];
    }
    ll size_a(int x) {
        return sum_a[root(x)];
    }
    ll size_b(int x) {
        return sum_b[root(x)];
    }
};

int main() {
    int N, M; cin >> N >> M;
    A.resize(N); B.resize(N);
    rep(i, N) cin >> A[i];
    rep(i, N) cin >> B[i];

    UnionFind uf(N);
    rep(i, M) {
        int c, d; cin >> c >> d; c--; d--;
        uf.merge(c, d);
    }
    int flg = 1;
    rep(i, N) {
        if (uf.size_a(i) != uf.size_b(i)) flg = 0;
    }
    if (flg == 1) cout << "Yes" << endl;
    else cout << "No" << endl;
}


#include<bits/stdc++.h>
using namespace std;
int h,w,a,b;
int ans;

void dfs(int i,int bit,int a,int b){
	if(i==h*w){
		ans++;
		return;
	}
	if(bit&(1<<i)){
		dfs(i+1,bit,a,b);
		return;
	}
	if(b){
		dfs(i+1,bit|1<<i,a,b-1);
	}
	if(a){
		if(i%w!=w-1&&(~bit&1<<(i+1))){
			dfs(i+1,bit|1<<i|1<<(i+1),a-1,b);
		}
		if(i+w<h*w){
			dfs(i+1,bit|1<<i|1<<(i+w),a-1,b);
		}
	}
}

int main(){
	cin>>h>>w>>a>>b;
	dfs(0,0,a,b);
	cout<<ans<<'\n';
}

// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder
#include<bits/stdc++.h>
//#include<atcoder/all>
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

//ordered_set 重複不可
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// use set_function + find_by_order(select itr-num)

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

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

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

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

//cout << std::fixed << std::setprecision(15) << y << endl; //小数表示

int h, w, a, b;

vector<vector<vector<Pii>>> gridmemo;
vector<vector<vector<Pii>>> gridmemo2;

void solve(vector<vector<Pii>> grid, int x, int  y, int count){
    if(x+1 == h && y+1 == w){
        if(count == a){
            gridmemo.pb(grid);
        }else{
            return;
        }
    }else if(x+1 == h){
        solve(grid, 0, y+1, count);
    }else{
        solve(grid, x+1, y, count);
    }
    REP(i,0,2){
        int nx = x + dx[i], ny = y+dy[i];
        if(nx >= 0 && nx < h && ny >= 0 && ny < w && grid[nx][ny] == Pii(-1, -1) && grid[x][y] == Pii(-1, -1)){
            grid[x][y] = Pii(nx, ny);
            grid[nx][ny] = Pii(x, y);
            if(x+1 == h && y+1 == w){
                if(count+1 == a){
                    gridmemo.pb(grid);
                }else{
                    return;
                }
            }else if(x+1 == h){
                solve(grid, 0, y+1, count+1);
            }else{
                solve(grid, x+1, y, count+1);
            }
            grid[x][y] = Pii(-1, -1);
            grid[nx][ny] = Pii(-1, -1);
        }
    }
}

int main(){
    cin >> h >> w >> a >> b;
    vector<vector<Pii> > grid(h, vector<Pii>(w, Pii(-1, -1)));
    int ans = 0;
    solve(grid, 0, 0, 0);
    /*
    REP(i, 0, gridmemo.size()){
        REP(x, 0,h){
            REP(y, 0, w){
                cout << gridmemo[i][x][y] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    */
    REP(i,0,gridmemo.size()){
        int flag = 1;
        REP(l, 0, gridmemo2.size()){
            int f = 1;
            REP(x, 0, h){
                REP(y, 0, w){
                    if(gridmemo[i][x][y] != gridmemo2[l][x][y]){
                        f = 0; //OK
                    }
                }
            }
            if(f){
                flag = 0;
            }
        }
        if(flag){
            gridmemo2.pb(gridmemo[i]);
        }
        ans += flag;
    }
    cout << ans << endl;
}


//---Vaidik Patel---//
//-------------------Proud to be Indian--------------------------------//
#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp> // include the associative containers or group of templates such as set,multi multimap, map etc.
#include<ext/pb_ds/tree_policy.hpp> // include the tree_order_statistics_node update
using namespace __gnu_pbds;
#define int long long int
#define ld long double
#define pii pair<int,int>
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; // find_by_order()
#define deb1(x) cout<<#x<<" : "<<x<<"\n";
#define deb2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<"\n";
#define deb3(x,y,z) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<" "<<#z<<": "<<z<<"\n";
#define deb4(x,y,z,w) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<"\t"<<#w<<" : "<<w<<endl;
#define deb5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl;
#define deb6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rFOR(i,a,b) for(int i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vvi vector<vector<int>>
#define M 998244353
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int powermodm(int x,int n,int m)
{int ans=1;   while(n){if(n&1){ans=(ans*x)%m;}x=(x*x)%m;n=n>>1;}return ans;}

void solve()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    stack<char>st;

    FOR(i,0,n)
    {
        if(i<2)
        {
            st.push(s[i]);
        }
        else
        {
            if(s[i]=='x' && st.size()>=2)
            {
                char y=st.top();
                st.pop();
                char x=st.top();
                st.pop();
                if(x=='f' && y=='o')
                {

                }
                else
                {
                    st.push(x);
                    st.push(y);
                    st.push(s[i]);
                }

            }
            else
            {
                st.push(s[i]);
            }
        }
    }
    cout<<st.size()<<"\n";
}
signed main()
{

    FIO

    int t;
    t=1;
    //cin>>t;
    FOR(i,0,t)
    {
        //cout<<"Case #"<<i+1<<": ";
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define u unsigned
#define ull unsigned long long
#define ld long double
#define pb push_back
#define fasterInOut ios::sync_with_stdio(false); cin.tie(0);
#define pi acos(-1)
#define f(i,a,b) for(int i=(a);i<=(b);i++)
#define fr(i,a,b) for(int i=(a);i>=(b);i--)
#define MAX 1000000

void function(int x);

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("C:/Users/ASUS/Desktop/inout/input.txt", "r", stdin);
    freopen("C:/Users/ASUS/Desktop/inout/output.txt", "w", stdout);
    #endif

    int a,b,c;
    cin>>a>>b>>c;
    if((a+b)>=(b+c) && (a+b)>=(a+c))
    	cout<<a+b<<endl;
    else if((b+c)>=(a+b) && (b+c)>=(a+c))
    	cout<<b+c<<endl;
    else if((a+c)>(a+b) && (a+c)>(b+c))
    	cout<<a+c<<endl;

    return 0;
}


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

#define rep(i, j, n) for(int i = (j); i < (int)(n); i++)
#define print(s) cout << s << endl
#define SIZE(s) long long(s.size())

using Graph = vector<vector<int>>;
using ll = long long;

int main(){
  int a, b, c; cin >> a >> b >> c;
  if(a == b) print(c);
  else if(b == c) print(a);
  else if(c == a) print(b);
  else print(0);
}

/*
　　　　　／＞　 フ
　　　　　| 　_　 _|
　 　　　／`ミ _x 彡
　　 　 /　　　 　 |
　　　 /　  ヽ　　 ?
　／￣|　　 |　|　|
　| (￣ヽ＿_ヽ_)_)
　＼二つ
 */
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pf push_front
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x)*(x))
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define CC(x) cout << (x) << endl
#define AL(x) x.begin(),x.end()
#define pw(x) (1ull<<(x))
#define NMSL cout << "NMSL" << endl;
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define fio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define in128 __int128_t
using namespace std;
const int N = 2e5+10;
const int INF = 0x7f7f7f7f;
const double EPS = 1e-3;
const long double PI = 3.14159265358979323846;
int ksm(int a,int b,int mod)
{
    int ans=1;
    while(b)
    {
        if(b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
signed main()
{
    fio
    int a,b,c;
    cin>>a>>b>>c;
    b=ksm(b,c,4)+4;
    a%=10;
    cout<<ksm(a,b,10);
    return 0;
}


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

#define forn(i,x,n) for(int i = x; i < n; ++i)

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
	int n,m,s,g;

	cin>>n>>m;
	vector<int> dist(n*m);
	vector<bool> done(n*m,false);
	vector<vector<int>> adj(n*m);
	unordered_map<char,vector<int>> teleport;
	vector<string> a(n);
	forn(i,0,n)
		cin>>a[i];
	forn(i,0,n)
		forn(j,0,m){
			dist[i*m+j]=INT_MAX;
			if(a[i][j]!='#' && a[i][j]!='.')
				teleport[a[i][j]].push_back(i*m+j);
			if(a[i][j]=='S')
				s=i*m+j;
			if(a[i][j]=='G')
				g=i*m+j;
			if(i && a[i-1][j]!='#')
				adj[i*m+j].push_back((i-1)*m+j);
			if(j && a[i][j-1]!='#')
				adj[i*m+j].push_back(i*m+j-1);
			if(i<n-1 && a[i+1][j]!='#')
				adj[i*m+j].push_back((i+1)*m+j);
			if(j<m-1 && a[i][j+1]!='#')
				adj[i*m+j].push_back(i*m+j+1);
		}

	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
    dist[s]=0;
    q.push({dist[s],s});

    while(q.size()){
        int f = q.top().second;
        q.pop();
        if(done[f])
        	continue;
        done[f]=true;
        for(auto v: adj[f])
            if(dist[v]>dist[f]+1)
                dist[v]=dist[f]+1, q.push({dist[v],v});
        if(teleport.find(a[f/m][f%m])!=teleport.end()){
        	for(auto v:teleport[a[f/m][f%m]])
	        	if(dist[v]>dist[f]+1)
	                dist[v]=dist[f]+1, q.push({dist[v],v});
        	teleport.erase(a[f/m][f%m]);
        }
    }
	cout<<(dist[g]==INT_MAX?-1:dist[g]);
}

#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define ifr(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m,n,r) m.reserve(n);m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd(){return 0ll;} template<typename T,typename... Args> T gcd(T a,Args... args) { return __gcd(a,(__typeof(a))gcd(args...)); }
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef unordered_map<ll,ll> umap;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007

const int N = 1e5 + 2;

void Solve() {
    long long t, n;
    cin>>t>>n;
    long long ans = ceil((100.0*n)/t);
    cout<<ans+n-1<<endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt","r",stdin);
    // #endif

    // int T;cin>>T;while(T--)
    {
        Solve();
    }
    return 0;
}


#pragma GCC optimize("O3")

#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// using cint = cpp_int;

#define fastio \
std::cin.tie(nullptr); \
std::ios::sync_with_stdio(false);

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

#define For(i, m, n)    for(int i = (m); (i) < (n); ++(i))
#define Rep(i, n)       For(i, 0, n)
#define Each(e, con)    for(auto e: con)
#define rEach(e, con)   for(auto &e: con)
#define crEach(e, con)  for(const auto &e: con)

#define All(con)        (con).begin(), (con).end()
#define Unique(con)     std::sort(All(con)); (con).erase(std::unique(All(con)), (con).end())
#define Reverse(con)    std::reverse(All(con))
#define Middle(con)     (con).begin() + ((con).end() - (con).begin()) / 2

template <class Ty1, class Ty2>
std::istream &operator>>(std::istream &is, std::pair<Ty1, Ty2> &arg)
{ is >> arg.first >> arg.second; return is; }
template <class Ty>
std::istream &operator>>(std::istream &is, std::vector<Ty> &arg)
{ rEach(e, arg) is >> e; return is; }
template <class Ty>
std::istream &operator>>(std::istream &is, std::vector<std::vector<Ty>> &arg)
{ rEach(sub, arg) { rEach(e, sub) { is >> e; }} return is; }

template <class Ty1, class Ty2>
std::istream &vecinput(std::vector<Ty1> &arg1, std::vector<Ty2> &arg2)
{ assert(arg1.size() == arg2.size());
auto sz = arg1.size(); Rep(i, (int)sz) std::cin >> arg1[i] >> arg2[i]; return std::cin; }
template <class Ty1, class Ty2, class Ty3>
std::istream &vecinput(std::vector<Ty1> &arg1, std::vector<Ty2> &arg2, std::vector<Ty3> &arg3)
{ assert(arg1.size() == arg2.size()); assert(arg2.size() == arg3.size());
auto sz = arg1.size(); Rep(i, (int)sz) std::cin >> arg1[i] >> arg2[i] >> arg3[i]; return std::cin; }
template <class Ty1, class Ty2, class Ty3, class Ty4>
std::istream &vecinput(std::vector<Ty1> &arg1, std::vector<Ty2> &arg2, std::vector<Ty3> &arg3, std::vector<Ty4> &arg4)
{ assert(arg1.size() == arg2.size()); assert(arg2.size() == arg3.size()); assert(arg3.size() == arg4.size());
auto sz = arg1.size(); Rep(i, (int)sz) std::cin >> arg1[i] >> arg2[i] >> arg3[i] >> arg4[i]; return std::cin; }

int main() {
fastio

    int r, x, y;
    cin >> r >> x >> y;

    double dist = hypot(x, y);

    int ans = 0;
    while(dist > 2 * r) {
        dist -= r;
        ++ans;
    }

    if(abs(dist - r) < 1e-9) cout << ans + 1 << endl;
    else cout << ans + 2 << endl;

    return 0;
}

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

#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define pb push_back
#define ff first
#define ss second
#define max(a,b) (a>b ? a:b)
#define min(a,b) (a<b ? a:b)

void solve(){
	int r,x,y;
	cin>>r>>x>>y;
	int ans=1;
	int k=x*x+y*y;
	if(r*r>k){
		cout<<2;
		return;
	}
	while(k>(ans*r*ans*r)){
		ans++;
	}
	cout<<ceil(ans);
}

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// int t;cin>>t;while(t--)
	{
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define ll long long
#define ding cerr<<endl<<"Time: "<<(clock()*1000./CLOCKS_PER_SEC)<<" ms"<<endl;
const int maxn=2e5+10;
int siz[maxn],dfn[maxn],rdfn[maxn],tot,totd,n;
vector<int> yuan[maxn],cun[maxn];
void dfs(int now,int fa,int dep){
    dfn[now]=++tot;rdfn[tot]=now;
    siz[now]=1;
    totd=max(totd,dep);
    cun[dep].push_back(dfn[now]);
    for(int i=0;i<yuan[now].size();i++){
        int v=yuan[now][i];
        if(v==fa) continue;
        dfs(v,now,dep+1);
        siz[now]+=siz[v];
    }
}
ll req(int now1,int now2,int dep){
    return upper_bound(cun[dep].begin(),cun[dep].end(),now2)-lower_bound(cun[dep].begin(),cun[dep].end(),now1);
}
signed main(){
    ios;cin>>n;
    for(int i=2;i<=n;i++){
        int a1;cin>>a1;
        yuan[i].push_back(a1);yuan[a1].push_back(i);
    }
    dfs(1,0,1);
    int q;cin>>q;
    while(q--){
        int a1,a2;cin>>a1>>a2;a2++;
        cout<<req(dfn[a1],dfn[a1]+siz[a1]-1,a2)<<endl;
    }
    ding;return 0;
}

//بسم الله الرحمن الرحيم

// ----->  Author :    N A Y E M
// ----->           [ CSE-19 , CU ]

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
typedef unsigned long long ull ;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;
//using u128 = __uint128_t;
#define el << '\n'
#define PI 3.141592653589793238
#define _test_ int _ ; cin >> _ ; while( _-- )
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define file() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
ll gcd ( ll  a, ll b ) { return __gcd ( a, b ) ; }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ) ; }
ll big_mod(ll base , ll power , ll mod){
    ll ret=1;base%=mod;while(power){if(power&1)ret=ret*base%mod;base=base*base%mod;power>>=1;}return ret;}
ll mod_inverse(ll a , ll mod ){ return big_mod(a,mod-2,mod);}
ull POW(ll a , ll b){if(b==0) return 1;ull ret=1;while(b--)ret*=a;return ret;}
int dx[]={0,0,+1,-1,+1,-1,-1,+1} ;
int dy[]={+1,-1,0,0,+1,-1,+1,-1} ;
// ------------------>
const ll infLL = 1e15;
const int inf = 1e8+123;
const ll mod = 1e9+7;
const ll mx = 3e5+123;
// ------------------>
pdd p[mx];

void _case_()
{
    ll n , ans = 0 ;
    cin >> n ;
    for(int i=0;i<n;i++)
    {
        cin >> p[i].first >> p[i].second ;
    }
    for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
        {
            double m = (abs(p[i].second-p[j].second)/abs(p[i].first-p[j].first));
            if(m>=-1 and m<=1) ans ++ ;
        }
    cout << ans el;
}

int main( )
{
    optimize() ;
    //_test_
       _case_() ;
    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
using ll = long long;
const int inf = 1e9+5;
const ll INF = 1e18+5;
const int nax = 2e5+500;
void out(vector<int> &a) {
	cout << a.size();
	for(int z: a) {
		cout <<" "<< z;
	}
	cout <<"\n";
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) cin >> a[i];
	int base = (1 << min(n,8)) - 1;
	vector<vector<int>> f(200, vector<int> (0));
	for(int i=1;i<=base;i++) {
		int ga = 0;
		vector<int> ans;
		for(int j=0;j<min(n,8);j++) {
			if(i & (1 << j)) {
				ga += a[j];
				ga %= 200;
				ans.eb(j+1);
			}
		}
		if(f[ga].size() != 0) {
			cout << "Yes\n";
			out(f[ga]);
			out(ans);
			return 0;
		} else {
			f[ga] = ans;
		}

	}
	cout <<"No\n";

}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<list>
#include<bitset>
#include<sstream>

using namespace std;
using ll = long long;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define REP(i, n) for(int i = 0; i < n; i++)
#define endl "\n"
using Graph = vector<vector<int>>;

int main(){
    string s;
    cin>>s;
    string ans;
    for(int i=0;i<3;i++){
        ans += s[(i+1)%3];
    };
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define ll long long
using namespace std;

const int MAX = 5e5+50;
char s[MAX], t[MAX];
int n;
vector<int> a, b;

int main() {
	scanf("%d%s%s", &n, s + 1, t + 1);
	for (int i = 1; i <= n; i++) {
		if (s[i] == '0') a.emplace_back(i);
		if (t[i] == '0') b.emplace_back(i);
	}
	if (SZ(a) != SZ(b)) {
		puts("-1");
		return 0;
	}
	int sum = 0;
	for (int i = 0; i < SZ(a); i++) sum += a[i] != b[i];
	printf("%d\n", sum);
	return 0;
}

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

#define ll long long
#define deb(x) cerr << #x << ":" << x << "\n"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;

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

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int ll

int get(string s,string t){
    int n=s.size();
    set<int> zero;
    set<int> one;
    for(int i=0;i<n;i++){
        if(s[i]=='0'){
            zero.insert(i);
        }else{
            one.insert(i);
        }
    }
    int ans=0;
    set<int> st;
    for(int i=0;i<n;i++){
        if(s[i]!=t[i])st.insert(i);
    }
    for(int i=0;i<n;i++){
        deb(i);
        if(s[i]!=t[i]){
            if(t[i]=='0'){
                ++ans;
                assert(zero.size()>0);
                int pos=*zero.begin();
                zero.erase(zero.begin());
                s[pos]='1';
                one.insert(pos);
            }else{
                deb("else");
                int pos=*one.begin();
                one.erase(one.begin());
                int x=*zero.begin();
                int y=n-1;
                auto zit=zero.lower_bound(pos);
                if(zit!=zero.end()){
                    y=*zit;--y;
                }
                auto eit=st.lower_bound(pos);
                int rpos=pos;
                if(eit!=st.end()){
                    rpos=*eit;
                }
                int z=n;
                auto sit=zero.lower_bound(x+1);
                if(sit!=zero.end()){
                    z=*sit;
                    // zero.erase(zero.begin());
                }
                if(z<pos){
                    ans+=pos-i;
                    s[rpos]='0';
                    zero.insert(rpos);
                }else{
                    ++ans;
                    s[rpos]='0';
                    zero.insert(rpos);
                }

            }
            st.erase(i);
        }
    }
    return ans;
}

int get_ans(string &s,string &t){
    int ans=min(get(s,t),get(t,s));
    reverse(all(s));reverse(all(t));
    return min(ans,min(get(s,t),get(t,s)));
}

int optiaml(string s,string t){
    int n=s.size();
    if(count(all(s),'0')!=count(all(t),'0'))return -1;
    vector<int> v;
    for(int i=0;i<n;i++){
        if(t[i]=='0')v.emplace_back(i);
    }
    int ans=0,j=0;
    for(int i=0;i<n;i++){
        if(s[i]=='0'){
            ans+=(i!=v[j++]);
        }
    }
    return ans;
}

void solve()
{
    int n;
    cin>>n;
    string s,t;
    cin>>s>>t;
    cout<<optiaml(s,t)<<"\n";
}

int32_t main()
{
#ifndef ONLINE_JUDGE
    freopen("../text_files/input.txt", "r", stdin);
    freopen("../text_files/output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int N, vis[10005];
int main () {
#ifndef ONLINE_JUDGE
    freopen("cpp.in", "r", stdin);
#endif
	cin >> N;
	if (N == 3) return puts("6 10 15") & 0;
	for (int i = 6; i <= 10000; i += 6) vis[i] = 1;
	for (int i = 10; i <= 10000; i += 10) vis[i] = 1;
	for (int i = 15; i <= 10000; i += 15) vis[i] = 1;
	for (int i = 1; i <= 10000 && N; i++)
		if (vis[i]) printf("%d ", i), N--;
	putchar('\n');
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 10009;

int n, a[N], tot;

int main() {
    cin >> n;
    for(int i = 6; i < 10000; i ++) {
        if(i == 12) continue;
        if(i % 6 == 0 || i % 10 == 0 || i % 15 == 0)
            a[tot ++] = i;
    }
    for(int i = 0; i < n; i ++) cout << a[i] << ' ';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;

int main() {
    int n,m;
    cin >> n >> m;
    set<int> sa, sb;
    rep(i,n){
        int ai ;
        cin >> ai;
        sa.insert(ai);
    }
    rep(i,m){
        int bi ;
        cin >> bi;
        sb.insert(bi);
    }
    vector <int> v(1001,0);
    for(int a : sa) if(sb.count(a)==0) v[a] = 1;
    for(int b : sb) if(sa.count(b)==0) v[b] = 1;
    rep(i,1001) if(v[i] == 1) cout << i << ' ';
}

#include <iostream>
using namespace std;
int main() {
	int a, b;
  cin >> a >> b;
  cout << (a+b) / 2 << ' ' << (a-b) / 2 << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long
#define ld long double
#define test ll t=0;cin>>t; while(t--)
#define loop(i,a,b) for(i=a;i<=b;i++)
#define loopr(i,a,b) for(i=a;i>=b;i--)
#define lb lower_bound
#define ub upper_bound
#define mid(l,r) mid=l+(r-l)/2
#define endl "\n"
#define yess cout<<"YES\n"
#define noo cout<<"NO\n"

int main()
{
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   fastio;
   ll n,k;cin>>n>>k;
   vector<ll>a(n);
   ll i,y=0;
   ll s[1000000]={0};
   ll ans=0;
   for(i=0;i<n;i++)
   {
    cin>>a[i];
    s[a[i]]++;
   }
   for(i=0;i<1e6;)
   {
    if(s[i]==0){
      ans=ans+i;y++;
      if(y==k)break;
     if(i==0)break;
      i=0;continue;

    }
    s[i]--;i++;
   }
   cout<<ans;

}

#include<bits/stdc++.h>
using namespace std;
#define stop exit(0)
#define pb push_back
#define skip continue
#define ll long long
ll a,b,c,d=0,e=0,i,f=0,g=0,j,k=0,l=0,r,z,m,h,p[200001],mod=1000000007,mod1=998244353;
string t,x,q,s;
long long df[101];
//double ;
vector<ll>v,u,w;
vector<ll>dv[101];
//vector<string>;
set<ll>y;
map<ll,ll>n;
void dfs(ll x){
    df[x]=1;
    for(ll i=0;i<dv[x].size();i++){
        if(df[dv[x][i]]==0){
            df[dv[x][i]]=1;
            dfs(dv[x][i]);
        }
    }
}
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>a>>b;
    for(i=1;i<=b;i++){
        cin>>p[i];
    }
    d=0;
    e=a+1;
    sort(p+1,p+b+1);
    if(b==0){
        cout<<1;
    }
    else if(b==a){
        cout<<0;
    }
    else{
        if(p[1]>1){
            v.pb(p[1]-1);
        }
        for(i=2;i<=b;i++){
            if(p[i]-1!=p[i-1]){
                v.pb(p[i]-p[i-1]-1);
            }
        }
        if(p[b]<a){
            v.pb(e-p[b]-1);
        }
        sort(v.begin(),v.end());
        d=v[0];
        e=mod;
        for(i=1;i<=d;i++){
            f=0;
            for(j=0;j<v.size();j++){
                f=f+((v[j]+v[0]-1)/v[0]);
            }
            e=min(e,f);
        }
        cout<<e;
    }
}


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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
const int N = 1e5 + 10, M = 3e5 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
#define x first
#define y second
const int mod = 1e9 + 7;
#define gcd __gcd

void solve() {
	LL n;
	scanf("%lld", &n);
	LL res = 2e18;
	for(LL i = 0; (1LL << i) <= n; i++) {
		LL a = n / (1LL << i);
		LL c = n % (1LL << i);
		res = min(res, a + i + c);
	}
	printf("%lld\n", res);
}

int main() {
	// freopen("in.txt", "r", stdin);
    // int t; cin >> t; while(t--)
    solve();
    return 0;
}

#include <bits/stdc++.h>
#include <string>

typedef long long ll;
typedef long double ldb;
typedef double db;
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define len(s) s.length()
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define on cout << endl
#define o2(a, b) cout << a << " " << b
#define os cout << " "
#define all(v) v.begin(), v.end()
#define mem(n, m) memset(n, m, sizeof(n))
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define mll map<ll, ll>
#define lb lower_bound
#define ub upper_bound
#define ret return 0
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define ford(container, it) for (__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define fors(container, it, a, b) for (__typeof(container.begin()) it = a; it != b; it++)
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define d4(a, b, c, d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define itoa(x) to_string(x);
#define atoi(x) stoll(x);

#define boost ios_base::sync_with_stdio(0)
#define MOD 1000000007
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define inf 999999999999999999
#define siz 100001
#define SIZ 1000001
#define SIZE 200001

#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 w(x)  \
    int x;    \
    cin >> x; \
    while (x--)

ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
ll pwr(ll x, ll y)
{
    ll res = 1;
    x = x;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (b * a) / gcd(a, b);
}
ll fact(ll n)
{
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}
ll nPr(ll n, ll r)
{
    return fact(n) / fact(n - r);
}
void c_p_c()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll f(ll n)
{
    ll m = 0;
    while(n>0)
    {
        m = max(m, n%10);
        n/=10;
    }
    return m;
}
// int dp[1005][100005]; //#books vs pricelimit
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    c_p_c();
    ll tt;
    tt = 1;
    // cin >> tt;
    while (tt-- > 0)
    {
        ll n;
        cin>>n;
        ll b = 0, a = n, c = 0, q = a*pwr(2, b);
        ll m = a+b+c;
        while(pwr(2, b)<=n)
        {
            b++;
            a = n/pwr(2, b);
            c = n%pwr(2, b);
            m = min(a+b+c, m);
        }
        d1(m);
    }

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define all(c) c.begin(),c.end()
#define sz(x) int(x.size())
#define ar array
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
ll exp(ll x,ll y){
  x%=mod;
  ll res=1;
  while(y){
    if(y&1)
    res=res*x%mod;
    x=x*x%mod;
    y>>=1;
  }
  return res;
}
ll fact(ll n){
  ll res=1;
  for(ll i=2;i<=n;++i)
  (res*=i)%=mod;
  return res;
}
bool isprime(ll n){
  for(ll i=2;i*i<=n;++i)
  if(n%i==0) return 0;
  return 1;
}
map<ll,ll>mp1,mp2;
ll f1(ll x,ll y){
  if(y<=x) {
    return abs(x-y);
  }
  if(mp1.count(y)) return mp1[y];
  ll ans1=0,ans2=0;
  if(y%2) {
    ans1+=2+f1(x,(y-1)/2);
    ans2+=2+f1(x,(y+1)/2);
    return mp1[y]=min(ans1,ans2);
  }
  else return mp1[y]=1+f1(x,y/2);
}
ll f2(ll x,ll y){
  if(y<=2*x) {
    return abs(x-y);
  }
  if(mp2.count(y)) return mp2[y];
  ll ans1=0,ans2=0;
  if(y%2) {
    ans1+=2+f2(x,(y-1)/2);
    ans2+=2+f2(x,(y+1)/2);
    return mp2[y]=min(ans1,ans2);
  }
  else return mp2[y]=1+f2(x,y/2);
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll x,y;
cin>>x>>y;
cout<<min(f1(x,y),f2(x,y));

   return 0;
}

#include <bits/stdc++.h>

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

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 60;

int mod = 1000000007;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N; cin >> N;
    vector<ll> A(N); rep(i, N) cin >> A[i];
    vector<ll> B(N, 0); rep(i, N-1) B[i+1] = B[i] + A[i+1]*(i+1);
    ll mx = -1, sum = 0;
    for(ll k = 1; k <= N; k++){
        mx = max(mx, A[k-1]);
        sum += A[k-1];
        cout << k*(mx + sum) - B[k-1] << endl;
    }
    return 0;
}


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

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

#define rep(i, n) for (int64_t i = 0; i < n; ++i)
#define rep2(i, n) for (int64_t i = 1; i <= n; ++i)
#define repb(i, l, n) for (int64_t i = l; i < n; ++i)
#define repb2(i, l, n) for (int64_t i = l; i <= n; ++i)
#define repc(i, l, n, d) for (int64_t i = l; i < n; i+=d)
#define repc2(i, l, n, d) for (int64_t i = l; i <= n; i+=d)
#define repe(a, b) for (auto&(a) : (b))
#define ALL(v) (v).begin(), (v).end()
#define Sort(x) sort(ALL(x))
#define Sort_rev(x) Sort(x);reverse(ALL(x))
#define mp(a, b) make_pair((a), (b))
#define Push_back(a, b) push_back( mp( (a), (b) ) )
#define ctoi(c) (c)-'0'

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1;  } return 0;  }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1;  } return 0;  }
template<typename V,typename T> bool find_num(V v, T num) { if ( find(ALL(v), num) == v.end() ) { return false; } return true; }

const int inf = 0x3fffffff;
const int64_t INF = 0x3fffffffffffffff;
const int64_t MOD = 1e9+7;

int main() {
    int64_t n;
    cin >> n;
    std::vector<int64_t> x(n), y(n);
    rep(i, n) {
        cin >> x[i] >> y[i];
    }
    int64_t ans = 0;
    rep(i, n-1) {
        repb(j, i+1, n) {
            // if ( x[i] <= x[j] ) {
                if ( y[i]-abs(x[j]-x[i]) <= y[j]
                    && y[j] <= y[i]+abs(x[j]-x[i]) ) {
                    ++ans;
                }
            // } else {

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

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
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; }

const int mod = 1000000007;
// const int mod = 998244353;

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), z(n);
  rep(i, n) cin >> x[i] >> y[i] >> z[i];
  int n2 = 1<<n;
  vvi dp(n2, vi(n, INF));
  vvi dist(n, vi(n));
  rep(i, n) rep(j, n) {
    int now = abs(x[j] - x[i]);
    now += abs(y[j] - y[i]);
    now += max(0, z[j] - z[i]);
    dist[i][j] = now;
  }
  rep(i, n) {
    if (i == 0) continue;
    dp[1<<i][i] = dist[0][i];
  }
  rep(i, n2) rep(j, n) {
    if (~i>>j&1) continue;
    rep(k, n) {
      if (i>>k&1) continue;
      chmin(dp[i|1<<k][k], dp[i][j] + dist[j][k]);
    }
  }
  cout << dp[n2-1][0] << endl;
}


/*
    @uthor: Amit Kumar
    user -->GitHub: drviruses ; CodeChef: dr_virus_ ; Codeforces,AtCoder,Hackerearth,Hakerrank: dr_virus;
*/
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//#define ln mp::cpp_int;
#define int      long long
#define double   long double
#define uint     unsigned long long
#define all(vec) vec.begin(),vec.end()
#define endl "\n"
int google_itr = 1;
#define google(x) cout<<"Case #"<<x<<": "
#define pi 3.14159265358979323846264338327950L

vector<string> vec_splitter(string s) {
        s += ',';
        vector<string> res;
        while(!s.empty()) {
                res.push_back(s.substr(0, s.find(',')));
                s = s.substr(s.find(',') + 1);
        }
        return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
        if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
        stringstream ss; ss << H;
        cerr << args[idx] << " = " << ss.str();
        debug_out(args, idx + 1, LINE_NUM, T...);
}

#define XOX
#ifdef XOX
    #define deb(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
    #define deb(...) 42
#endif

const int mod = 1e9+7;
const int inf = 1e18;

unordered_set<string> seen;
int res = INT_MAX;
void minimumTimeRequired(vector<int>& jobs, int k, int start, vector<int>& assigned, int prefer) {
        // make a copy of the assignment, and use it to calculate the cache key and the workload of the worker with heaviest workload
        vector<int> a = assigned;
        sort(a.begin(), a.end());
        string key = "";
        int heaviest = 0;
        for (int num: a) {
            heaviest = max(heaviest, num);
            key += to_string(num) + "_";
        }
        // pruning trick 2
        if (heaviest > res) return;
        // pruning trick 1
        if(seen.find(key)!=seen.end()) return;
        seen.insert(key);
        if (start >=(int) jobs.size()) {
            // Found a possible assignment, check to see if it's better than what we have seen
            int sln = 0;
            for (int i=0; i<assigned.size(); i++) {
                sln = max(sln, assigned[i]);
            }
            res = min(res, sln);
            return;
        }

        unordered_set<int> tried;
        for (auto j=0; j<assigned.size(); j++) {
            int i = (j + prefer) % k;
            if (tried.find(assigned[i]) != tried.end()) continue;
            tried.insert(assigned[i]);
            assigned[i] += jobs[start];
            // pruning trick 3: force the next iteration to try to do assignments from the next worker
            minimumTimeRequired(jobs, k, start+1, assigned, prefer+1);
            assigned[i] -= jobs[start];
        }
    }

void virus(){
        int n, ans = 0;
        cin >> n;

        vector <int> arr(n);
        for(auto &i:arr) cin >> i;
        vector<int> assigned(2);
        sort(all(arr), greater <int>());
        minimumTimeRequired(arr, 2, 0, assigned, 0);
        cout << res << endl;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

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

    int t = 1;
    //cin >> t;
    while(t--){
           auto start = system_clock::now();
           virus();
           auto stop = system_clock::now();
           duration<double> duration = (stop - start);
           //cerr << "\nTime: "<< fixed << setprecision(10) << (double)duration.count() <<"sec"<< endl;
           //your code goes here
    }
    return 0;
}


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

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

template<typename T>
istream& operator>>(istream &is, vector<T> &v){
    for(int i=0; i<v.size(); i++) is >> v[i];
    return is;
}

#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
    cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cout << setprecision(20) << fixed;

    int n; cin >> n;
    vector<int> a(n), b(n);
    cin >> a >> b;

    cout << max(0, *min_element(b.begin(), b.end()) - *max_element(a.begin(), a.end()) + 1) << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
template <typename T>
void print(T a, string message = "debug: ");
template <typename T>
void printvec(vector<T> a, string message = "debug: ");
int n;
vector<int> a, b;

int main()
{
  cin >> n;
  a.resize(n);
  b.resize(n);
  int amin = 0, bmax = 1000;
  for (int i = 0; i < n; i++)
  {
    cin >> a[i];
    if (amin < a[i])
    {
      amin = a[i];
    }
  }
  for (int i = 0; i < n; i++)
  {
    cin >> b[i];
    if (bmax > b[i])
    {
      bmax = b[i];
    }
  }

  if (bmax - amin + 1 > 0){
    cout << bmax - amin + 1 << endl;
  }else{
    cout << 0 << endl;
  }

  return 0;
}

template <typename T>
void print(T a, string message)
{
  cout << message << a << endl;
}
template <typename T>
void printvec(vector<T> a, string message)
{
  for (ll i = 0; i < a.size(); i++)
  {
    cout << message << "[" << i << "]: " << a[i] << endl;
  }
}


//#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define CT printf("\n-------------------------\n");
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll Inf=1ll<<60ll;
typedef pair<int,int> pii;
const int N=100005;
priority_queue<pii,vector<pii>,greater<pii> > q[26];
int n,dp[N];
char s[N];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);

	scanf("%d",&n);
	scanf("%s",s+1);
	memset(dp,0x3f,sizeof(dp));
	dp[0]=0;
	for(int i=0;i<=n;++i){
		for(int j=0;j<26;++j){
			if(q[j].empty()||j==s[i]-'a')continue;
			pii tmp=q[j].top();
			dp[i]=min(dp[i],dp[tmp.second]+1);
		}
		if(i==n)break;
		q[s[i+1]-'a'].push(make_pair(dp[i],i));
	}
	if(dp[n]==inf){puts("-1");return 0;}
	printf("%d\n",dp[n]);

//	fclose(stdin);fclose(stdout);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;

constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
//constexpr long long MOD = (ll) 1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;

using Array = vector<ll>;
using Matrix = vector<Array>;

/*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
*/

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;
}

void solve() {
    int n;
    cin >> n;
    vec<ll> a(n);
    REP(i, n) cin >> a[i];

    vec<ll> used(n, 0);
    set<int> b;
    REP(i, n - 1) {
        if (a[i] > i + 1 and a[i + 1] < i + 2) {
            used[i] = 1;
            b.insert(i);
        }
    }

    vec<int> ans;
    while (b.size()) {
        auto it = b.begin();
        b.erase(it);
        int i = *it;
        //cout<<i+1<<en;
        used[i] = -1;
        ans.push_back(i + 1);
        swap(a[i], a[i + 1]);
        if (i - 1 >= 0) {
            if (used[i - 1] != -1) {
                if (a[i - 1] > i and a[i] < i + 1) {
                    used[i-1] = 1;
                    b.insert(i - 1);
                } else {
                    used[i-1] = 0;
                    b.erase(i - 1);
                }
            }
        }

        if (i + 2 < n) {
            if (used[i + 1] != -1) {
                if (a[i + 1] > i+2 and a[i + 2] < i + 3) {
                    used[i+1] = 1;
                    b.insert(i + 1);
                } else {
                    used[i+1] = 0;
                    b.erase(i + 1);
                }
            }
        }
    }

    if (ans.size() != n-1) {
        cout << -1 << en;
        return;
    }
    REP(i, n) {
        if (a[i] != i + 1) {
            cout << -1 << en;
            return;
        }
    }
    for (auto i:ans) cout << i << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
/*
    ll t;
    cin >> t;
    REP(i, t - 1) {
        solve();
    }*/

    solve();

    return 0;
}


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

typedef long long ll;

#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(a) a.begin(), a.end()
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0);

const int maxn = 200010;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll LINF = 1e18;
int n, m, a[maxn], b[maxn];
map<int, int> M, N;
string s, ss;
set<int> S;

int main()
{
    int n, ans=0, ans2=0;
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 2; i <= 1000; i++)
    {
        int cnt = 0;
        for(int j = 0; j < n; j++)
        {
            if(a[j]%i==0) cnt++;
        }
        if(ans<cnt) ans=cnt, ans2=i;
    }
    cout << ans2;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <ctype.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi >;
using vl = vector<ll>;
using vvl = vector<vl >;
using pairi = pair<int, int>;
using pairl = pair<ll, ll>;

#define TR ","
#define TS " "
#define rep(i,N) for(ll i=0;i<(ll)N;++i)
#define rpt(i,a,b) for(ll i=a;i<(ll)b;++i)
#define all(v) v.begin(), v.end()
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

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

ll lcm(ll x, ll y) {
    return x / gcd(x, y) * y;
}

void printVector(const vector<ll>& vec) {
    for (ll value : vec) {
        cout << value << " ";
    }
    cout << endl;
}

vector<int> IntegerToVector(int bit, int N) {
    vector<int> S;
    for (int i = 0; i < N; ++i) {
        if (bit & (1 << i)) {
            S.push_back(i);
        }
    }
    return S;
}

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++) {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll COM(ll n, ll k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main()
{
    ll M = 0, N, ans = 0;;
    cin >> N;
    vl A(N);
    rep(i, N) cin >> A[i];
    rep(i, N) M = max(A[i], M);

    vl B(M+1,0);

    rep(i, N) {
        rpt(j, 2, A[i]+1) {
            if (A[i] % j == 0) {
                ++B[j];
            }
        }
    }

    rep(i, M+1) {
        ans = max(ans, B[i]);
    }

    rep(i, M+1) {
        if (B[i] == ans) {
            cout << i << endl;
            return 0;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll,ll> P;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define EFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,I) for(const auto &(i):(I))
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ll(x.size())
#define INF32 2147483647
#define INF64 9223372036854775807
#define MOD 1000000007
#define PI acos(-1)
#define endl '\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; }

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    string s; cin >> s;
    reverse(ALL(s));
    REP(i,SIZE(s)){
        if(s[i] == '6') s[i] = '9';
        else if(s[i] == '9') s[i] = '6';
    }
    cout << s << endl;
    return 0;
}

#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<cstdio>
#include<functional>
#include <bitset>
#include <iomanip>
#include <cctype>
#include <list>
#include <cassert>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define ll long long
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
using vvv = vector<vector<vector<ll>>>;
using vv = vector<vector<ll>>;
using vec = vector<ll>;
using P = pair<ll, ll>;
int main(void){
   ll n; cin >> n;
   vector<int> digits;

   while(n) digits.push_back(n % 10), n /= 10;

   ll ans = 20;

   rep(i, pow(2, digits.size())){

       ll cnt = 0;
       ll sum = 0;

       rep(j, digits.size()){
           if (i & (1 << j)) {
           //if(i >> j & 1){
               ++cnt;
               sum += digits[j];
           }
       }

       if(sum % 3 == 0 && cnt != 0)
         ans = min(ans, (ll)digits.size() - cnt);
   }

   cout << (ans == 20 ? -1 : ans);
}


#include <bits/stdc++.h>
#define faster ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ff first
#define ss second
using namespace std;

void file()
{
    freopen ( "CHUYEN.inp", "r", stdin );
    freopen ( "CHUYEN.out", "w", stdout );
}

typedef pair<int, int> ii;

const int maxn = 1e2 + 7;

int n, m;
vector<ii> ds;
vector<vector<int>> dsk;

int mau[maxn];
int c[maxn];
int e[maxn][maxn];

void dfs ( int u )
{
    mau[u] = 1;
    for ( int v : dsk[u] )
    {
        if ( ( c[u] > c[v] || c[u] == c[v] ) && !e[u][v] && !e[v][u] )
            e[u][v] = 1, dfs ( v );
    }

}

void solve()
{
    for ( int i = 1; i <= n; ++i )
        if ( mau[i] == 0 )
            dfs ( i );
    for ( int i = 0; i < ds.size(); ++i )
    {
        int u, v;
        u = ds[i].ff;
        v = ds[i].ss;
        if ( e[u][v] )
            cout << "->";
        else
            cout << "<-";

        cout << '\n';
    }

}

void input()
{
    cin >> n >> m;
    dsk.assign ( n + 1, vector<int>() );
    for ( int i = 1; i <= m; ++i )
    {
        int u, v;
        cin >> u >> v;
        ds.push_back ( {u, v} );
        dsk[u].push_back ( v );
        dsk[v].push_back ( u );
    }
    for ( int i = 1; i <= n; ++i )
        cin >> c[i];
}

signed main()
{
    faster;
    //file();
    input();
    solve();
}

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { if (a < b) { a = b; return true; } return false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { if (a > b) { a = b; return true; } return false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void _co() { cout << '\n'; }
template <class Head, class... Tail>
void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t LINF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);

Vec ans;
Vec a, b;

template <class T>
struct Graph {
    struct edge {
        int64_t from, to;
        T dist;

        bool operator<(const edge &rhs) const { return dist < rhs.dist; }

        bool operator>(const edge &rhs) const { return dist > rhs.dist; }

        edge &operator+=(const edge &rhs) {
            to = rhs.to;
            dist += rhs.dist;
            return *this;
        }
        edge operator+(const edge &rhs) { return edge(*this) += rhs; }
    };

    int64_t V;
    vector<T> dist;
    vector<vector<edge>> edges;
    bitset<100000> is_visited;

    Graph(int64_t v) : V(v) {
        edges = vector<vector<edge>>(V);
        dist = vector<T>(V, numeric_limits<T>::max());
    }

    void add_edge(int64_t a, int64_t b, T d = 1, bool is_dual = false) {
        edges[a].push_back(edge{a, b, d});
        if (is_dual) edges[b].push_back(edge{b, a, d});
    }

    void dfs(ll v, ll p, T c) {
        // ce(v, p, c);
        if (c != -1) {
            if (is_visited[c])
                return;
            is_visited[c] = true;
            if (v == b[c])
                ans[c] = 1;
            else
                ans[c] = 2;
        }
        // ce("ok", v, p, c);
        for (auto e : edges[v]) {
            if (e.to == p)
                continue;
            dfs(e.to, v, e.dist);
        }
    }
};

int main(void) {
    ll n, m;
    cin >> n >> m;
    a.resize(m);
    b.resize(m);
    rep(i, m) cin >> a[i] >> b[i];
    Vec c(n);
    cin >> c;
    Graph<ll> g(n);

    ans.resize(m);
    rep(i, m) a[i]--, b[i]--;
    rep(i, m) {
        if(c[a[i]] > c[b[i]])
            ans[i] = 1;
        else if (c[a[i]] < c[b[i]])
            ans[i] = 2;
        else
            g.add_edge(a[i], b[i], i, true);
    }

    rep(i, n) g.dfs(i, i, -1);

    rep(i, m) {
        if (ans[i] == 1)
            co("->");
        else
            co("<-");
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
char s[N],t[N],zhan[N];
int top;
int main()
{
	int n;
	scanf("%s",s+1);
	n=strlen(s+1);
	int l=n+1,r=n,rev=0;
	for(int i=1;i<=n;i++)
	if(s[i]=='R') {
		rev^=1;
	} else {
		if(rev) l--,t[l]=s[i];
		else r++,t[r]=s[i];
	}
	for(int i=l;i<=r;i++)
	{
		zhan[++top]=t[i];
		if(zhan[top]==zhan[top-1]) top-=2;
	}
	if(!rev) for(int i=1;i<=top;i++) printf("%c",zhan[i]);
	else for(int i=top;i>=1;i--) printf("%c",zhan[i]);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i=0; i<(n); ++i)
#define all(x) (x).begin(),(x).end()
const int INF = 1e9+1;
using ll = long long;
using p = pair<int, int>;
using v = vector<int>;

int main() {
  string S;
  cin >> S;
  string T;
  int n = 0;
  bool R = false;
  for (auto x : S) {
    if (x == 'R') {
      R = !R;
    } else {
      int j = 0;
      if (R) {
        j = 0;
        if (x == T[j]) {
          n--;
          T = T.substr(1);
          continue;
        }
        T = x + T;
      } else {
        j = n-1;
        if (x == T[j]) {
          n--;
          T = T.substr(0, n);
          continue;
        }
        T += x;
      }
      n++;
    }
  }
  if (R) reverse(all(T));
  cout << T << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPR(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define FORR(i, m, n) for (int i = (int)n - 1; i >= (int)m; i--)
#define REPE(i, v) for (auto i : v)
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
using Graph = vector<vector<int> >;
using WeightedGraph = vector<vector<pair<int, int> > >;
using LD = long double;
using VI = vector<int>;
using VVI = vector<vector<int> >;
using PII = pair<int, int>;
using MII = map<int, int>;
using SI = set<int>;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T> >;
template <typename T> using VVV = vector<vector<vector<T> > >;
void print() {cout << endl;}
template <typename Head, typename... Tail> void print(Head&& head, Tail&&... tail) {cout << head; if (sizeof...(tail) != 0) cout << " ";print(forward<Tail>(tail)...);}
template <typename T> void print(vector<T> &vec) {for (auto& a : vec) {cout << a; if (&a != &vec.back()) cout << " ";} cout << endl;}
template <typename T> void print(vector<vector<T> > &df) { for (auto& vec : df) { print(vec);}}
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template <typename T> void SORT (T &v) {sort(v.begin(), v.end());}
template <typename T> void SORTR (T &v) {sort(v.begin(), v.end(), greater<>());}
template <typename T> T GCD(T x,T y) {return __gcd(x,y);}
template <typename T> T LCM(T x,T y) {return x/__gcd(x,y)*y;}
template <typename T> int SZ(T v) {return v.size();}
template <typename T> T PCNT(T x) {return __builtin_popcountll(x);}
template <typename T> T BIT(T x) {return 1LL<<(x);}
int MOD = 1000000007;
int INF = 2000000000;
long double PI = acosl(-1);

int32_t main() {
    chrono::system_clock::time_point start_time, end_time;
    start_time = chrono::system_clock::now();
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    int ans = 0;

    int N, M;
    cin >> N;
    vector<int> A(N);
    cin >> M;
    vector<int> B(M);
    MII m;
    REP (i, N) {
        cin >> A[i];
        m[A[i]]++;
    }
    REP (i, M) {
        cin >> B[i];
        m[B[i]]++;
    }
    set<int> s;
    REP (i, N) {
        if (m[A[i]] == 1) {
            s.insert(A[i]);
        }
    }
    REP (i, M) {
        if (m[B[i]] == 1) {
            s.insert(B[i]);
        }
    }
    for (auto i : s) {
        cout << i << ' ';
    }
    cout << endl;

    end_time = chrono::system_clock::now();
    assert(chrono::duration_cast<chrono::milliseconds>(end_time-start_time).count() < 6000);
    return 0;
}

/*
cd ~/Desktop/Programming/AtCoder/contest/jsc2021/jsc2021_b && sh shellscripts/test_cpp.sh
cd ~/Desktop/Programming/AtCoder/contest/jsc2021/jsc2021_b && sh shellscripts/etest_cpp.sh
cd ~/Desktop/Programming/AtCoder/contest/jsc2021/jsc2021_b && sh shellscripts/submit_cpp.sh
*/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ALL(c) (c).begin(),(c).end()
#define RALL(c) (c).rbegin(),(c).rend()
#define ZERO(c) memset(c, 0, sizeof(c))
using ll = long long;
using P = pair<int,int>;

const int INF = 1e9 + 5;
const ll INFL = 1e18 + 5;

int N, M, a,b,c;
set<int> A, B, C;
vector<int> v(3000);

int main() {
    cin >> N >> M;
    rep(i,N) {
        cin >> a;
        A.insert(a);
    }

    rep(i,M) {
        cin >> b;
        B.insert(b);
    }

    for (auto elem : A) {
        if (B.count(elem) == 0) C.insert(elem);
    }

    for (auto elem : B) {
        if (A.count(elem) == 0) C.insert(elem);
    }

    for (auto elem : C)
        cout << elem << endl;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int lli;

#define all(arr) arr.begin(),arr.end()
#define f first
#define s second
#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define nl cout<<"\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cout<<"abcd\n";
#define pi pair<int,int>
#define pii pair<int,pi>
#define pb push_back
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#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 clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define MOD 1000000007
#define space ' '

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;

template <typename T>
void input(vector<T> &arr,lli n) {
  T temp;
  for(lli i=0;i<n;i++) cin>>temp, arr.push_back(temp);
}

template <typename T>
void output(vector<T> arr) {
  for(auto x:arr) cout<<x<<" ";
  cout<<endl;
}

template <typename T>
void input_set(set<T> &arr,lli n) {
  T temp;
  for(lli i=0;i<n;i++) cin>>temp, arr.insert(temp);
}

lli mul(lli a, lli b) {
  return (a%MOD*b%MOD)%MOD;
}

lli power(lli a,lli b) {
  lli ans = 1;
  while(b > 0) {
    if(b&1)
      ans = mul(ans, a);
    a = mul(a,a);;
    b >>= 1;
  }
  return ans;
}

lli sumnatural(lli n){
    lli sum = (n* (n+1)) / 2;
    return sum;
}

lli suminrange(lli l,lli r){
    return sumnatural(r) - sumnatural(l-1);
}

void solve() {
  lli n,sum=0;
  cin >> n;

  while(n--){
      lli a,b;
      cin >> a >> b;
      sum = sum + suminrange(a,b);
      //cout << a << b << endl;
      //cout << suminrange(a,b) << endl;;
  }

  cout << sum;
}

int main() {

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

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long int ULLI;

int main()
{
	int n;
	ULLI a, b;
	ULLI sum = 0;

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> a >> b;

		sum += (a + b) * (b - a + 1) / 2;
	}

	cout << sum << endl;

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int read()
{
	char c;
	int w=1;
	while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
	int ans=c-'0';
	while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
	return ans*w;
}
char s[61];
ll m;
int n;
ll base[600];
bool check(ll x)
{
	base[1]=1;
	ll nowans=s[1]-'0';
	for(int i=2;i<=n;i++)
	{
		if(1.0*x*base[i-1]>m)return false;
		base[i]=base[i-1]*x;
		if(1.0*(s[i]-'0')*base[i]+nowans>m)return false;
		nowans+=base[i]*(s[i]-'0');
	}
	return true;
}
int main(){
	scanf("%s",s+1);
	cin>>m;
	n=strlen(s+1);
	if(n==1)
	{
		if(m<s[1]-'0')
		{
			puts("0");
			return 0;
		}
		else
		{
			puts("1");
			return 0;
		}
	}
	reverse(s+1,s+n+1);
	int num=0;
	for(int i=1;i<=n;i++)num=max(num,s[i]-'0');
	ll l=num+1;
	ll r=m;
	ll ans=0;
	if(!check(num+1))
	{
		puts("0");
		return 0;
	}
	while(l<=r)
	{
		ll mid=l+r>>1;
//		cout<<mid<<endl;
		if(check(mid))ans=mid,l=mid+1;
		else r=mid-1;
	}
	cout<<ans-num<<endl;
	return 0;
}

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
/*
struct Edge
{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
*/
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

bool check(ll x, string X, ll M)
{
  // x進数
  ll sum = 0;
  for (int i = 0; i < X.size(); i++)
  {
    if (sum > M / x)
      return false;
    else
      sum = sum * x + (ll)(X[i] - '0');
  }
  if (sum <= M)
    return true;
  else
    return false;
}
// sum + c, (sum + c) * x + c,... を繰り返す

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string X;
  cin >> X;
  ll M;
  cin >> M;
  int d = 0;
  for (int i = 0; i < X.size(); i++)
  {
    int now = X[i] - '0';
    d = max(d, now);
  }
  if (X.size() == 1)
  {
    if (M >= d)
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }

  ll left = d, right = M + 10;
  while (left + 1 < right)
  {
    ll mid = (left + right) / 2;
    if (check(mid, X, M))
      left = mid;
    else
      right = mid;
    cerr << "left: " << left << "  right: " << right << endl;
  }
  cout << left - d << endl;
  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;

int main(){
    int A, B; cin >> A >> B; A--;

    rep3(i, B, 1){
        if((B/i)-(A/i) >= 2) {cout << i << '\n'; return 0;}
    }
}

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

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

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

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

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

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

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

int main() {
    ll a, b; cin >> a >> b;
    ll ans = 1;
    for(ll i = 2;i <= b / 2;++i) {
        ll j = 1;
        if(i * j < a) {
            if(a % i == 0) j = a / i;
            else j = a / i + 1;
        }
        ll val = i * j;

        if(a <= val && val <= b && a <= val + i && val + i <= b) {
            chmax(ans, i);
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, n) for (int i = 0; i < (int)(n);)
#define rep0(i, n) for (int i = 0; i <= (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;

int main(void) {
  int N;
  cin >> N;
  int A;
  cin >> A;
  int B;
  cin >> B;

  cout << N - A + B << endl;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
typedef std::vector<long long> vi;
#define F first
#define S second
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
const ll INF = LLONG_MAX / 2;
const ll N = 2e5 + 1;
using namespace std;
int main()
{
    fast;
    ll t = 1;
    // std::cin >> t;
    while (t--)
    {
        ll n, i, j = 0, s, d;
        std::cin >> n >> s >> d;
        fr(i, 0, n)
        {
            ll x, y;
            cin >> x >> y;
            if (x < s && y > d)
                j = 1;
        }
        if (j == 1)
            cout << "Yes\n";
        else
        {
            cout << "No\n";
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=25;
int color[maxn];
int n,m,a,b;
vector<int>v[maxn];
vector<int>node;
int vis[maxn];
void dfs(int s){
	vis[s]=1;
	node.push_back(s);
	for(int i=0;i<v[s].size();i++)
	if(!vis[v[s][i]]) dfs(v[s][i]);
}
ll cnt(int cur){
	if(cur==node.size()) return 1;         //相乘 则return值为1不为0
	ll ans=0;
	for(int i=0;i<3;i++){
		int ve=node[cur],sign=0;
		for(int j=0;j<v[ve].size();j++){
			if(color[v[ve][j]]==i){
				sign=1;
				break;
			}
		}
		if(!sign){
			color[ve]=i;
			ans+=cnt(cur+1);
			color[ve]=-1;
		}
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m;
	memset(color,-1,sizeof color);
	memset(vis,0,sizeof vis);
	for(int i=0;i<m;i++){
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	ll res=1,k;
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			node.clear();           //存当前可以到达的所有节点
			dfs(i);
			k=cnt(0);
			res*=k;
		}
	}
	cout<<res<<endl;
	return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>
#include <random>
#include <complex>
#include <functional>
#include <tuple>
#include <new>

using namespace std;

#define EPS (1e-9)
#define INF (1e9)
#define MOD (ll)(1e9 + 7)
#define PI (acos(-1))
#define REP(i,a,n) for(int i=a;i<n;i++)
#define rep(i, n) REP(i,0,n)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" ; "IMPOSSIBLE")

using ll = long long int;

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

ll beki(ll a, ll b) {
    ll tmp = 1;
    rep(i, b) tmp *= a;
    return tmp;
}

ll modPow(ll x, ll a) {
    if (a == 1) return x;
    if (a % 2) return (x * modPow(x, a - 1)) % MOD;
    ll t = modPow(x, a / 2);
    return ((t % MOD) * (t % MOD)) % MOD;
}

ll modInv(ll x) {
    return modPow(x, MOD - 2);
}

ll modComb(ll n, ll k) {
    ll tmp = 1;
    for (int i = 0; i < k; i++) {
        tmp *= n - i;
        tmp %= MOD;
    }
    for (int i = 1; i <= k; i++) {
        tmp *= modInv(i);
        tmp %= MOD;
    }
    return tmp;
}

ll N;
vector<ll> A(30);
ll ans;

int main() {
    cin >> N;
    rep(i, N) cin >> A[i];

    rep(i, 1 << (N - 1)) {
        //iのnビット目がたってたらが区切り(たかだか100万回)
        ll ortmp = A[0], xortmp = 0;
        rep(j, N - 1) {
            if (!((i >> j) & 1)) {
                ortmp |= A[j + 1];
            }
            else {
                xortmp ^= ortmp;
                ortmp = A[j + 1];
            }
        }

        xortmp ^= ortmp;

        if (i == 0) ans = xortmp;
        else ans = min(ans, xortmp);
    }

    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
template <class T> ostream &operator << (ostream &os, const vector<T> &p) { os << "["; for (auto&it : p) os << it << " "; return os << "]";}
template <class S, class T> ostream &operator << (ostream &os, const pair<S, T> &p) { return os << "(" << p.first << "," << p.second << ")";}
#ifndef ONLINE_JUDGE
#define deb(...) dbs(#__VA_ARGS__,__VA_ARGS__)
template <class T> void dbs(string str, T t) { cerr << str << ":" << t << "\n";}
template<class T, class...S> void dbs(string str, T t, S... s) { int idx = str.find(','); cerr << str.substr(0, idx) << ":" << t << ","; dbs(str.substr(idx + 1), s...);}
#else
#define deb(...){}
#endif
#define int long long
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
#define pr(x) {cout << x << '\n'; return;}
#define nl cout<< '\n'
#define rep(i,n) for(int i = 0; i < n; i++)
#define re(i,n) for(int i = 1; i <= n; i++)
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
template<class T> inline T Bit(T x, int i) { return (x >> i) & 1;}
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int N = 1e5 + 3; // check the limits

void solve() {
	int n, m; cin >> n >> m;
	vector<pair<int, pii>> g[n + 1];
	rep(i, m) {
		int x, y, c, d;
		cin >> x >> y >> c >> d;
		g[x].pb({y, {c, d}});
		g[y].pb({x, {c, d}});
	}
	set<pii> s;
	s.insert({0, 1});
	vi dis(n + 1, 1e18);
	dis[1] = 0;
	while (sz(s)) {
		pii p = *s.begin();
		s.erase(s.begin());
		int v = p.se, w = p.fi;
		for (auto u : g[v]) {
			int x = u.fi, c = u.se.fi, d = u.se.se;
			vi cur{w};
			for (int i = sqrt(d) - 1; i <= sqrt(d + 1); i++) {
				if (w >= i) continue;
				cur.pb(i);
			}
			if (d >= w) cur.pb(d);
			if (d - 1 >= w) cur.pb(d - 1);
			for (auto i : cur) {
				int time = i + (d / (i + 1)) + c;
				if (dis[x] > time) {
					s.erase({dis[x], x});
					dis[x] = time;
					s.insert({dis[x], x});
				}
			}
		}
	}
	int ans = dis[n];
	if (ans >= 1e14) ans = -1;
	cout << ans << '\n';
}

signed main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//int t; cin >> t;
	//while (t--)
	solve();
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
constexpr long long MOD2 = 998244353;
constexpr long long INF = 1LL << 60;
const long double PI = acosl(-1.0);
constexpr long double EPS = 1e-11;
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;
}
ll c[100000 + 2], d[100000 + 2];
struct edge {
    long long to, cost, strict;
};
struct graph{
    long long v;
    vector<vector<edge>> adj;
    vector<long long> dist, used;

    graph(long long n) {
        init(n);
    }
    void init(long long n){
        v = n;
        adj.resize(v);
        dist.assign(v, INF);
        used.assign(v, 0LL);
    }
    void add_edge(long long stt,long long gol,long long cst,long long ks){
        edge e;
        e.to = gol;
        e.cost = cst;
        e.strict = ks;
        adj[stt].emplace_back(e);
    }
	void dijkstra(long long stt){
        for (long long i = 0; i < v;i++){
            dist[i] = INF;
        }
        dist[stt] = 0;
		//cost,to
        priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;
        pq.push({0LL, stt});
		while(!pq.empty()){
            auto p = pq.top();
            pq.pop();
            long long vertex = p.second;
            if (dist[vertex] < p.first) continue;
            for (auto e : adj[vertex]) {
                ll time = dist[vertex];
                if (time < sqrtl(e.strict) - 1) time = sqrtl(e.strict);
                time += e.strict / (time + 1);
                if (chmin(dist[e.to], time + e.cost)) pq.push({dist[e.to], e.to});
            }
        }
    }
    long long prim(){
        priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;
        long long res = 0;
        pq.push({0LL, 0LL});
        while(!pq.empty()){
            long long cst = pq.top().first, vertex = pq.top().second;
            pq.pop();
            if (used[vertex]) continue;
            used[vertex] = 1;
            res += cst;
            for(auto e:adj[vertex]){
                if (!used[e.to]) pq.push({e.cost, e.to});
            }
        }
        return res;
    }
};
int main(){
    ll n, m;
    cin >> n >> m;
    graph g(n);
    for (ll i = 0; i < m; i++) {
        ll a,b;
        cin >> a >> b >> c[i] >> d[i];
        a--;
        b--;
        g.add_edge(a, b, c[i], d[i]);
        g.add_edge(b, a, c[i], d[i]);
    }
    g.dijkstra(0);
    if(g.dist[n-1]==INF){
        cout << "-1" << endl;
    }
    else
        cout << g.dist[n - 1] << endl;
}