#include <math.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

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

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

const ll INF_ll = 1e17;
const int INF = 1e8;

ll sqrt_ll(ll n) {
  ll l = 0LL;
  ll r = 1e9 + 10;
  while (r - l > 1LL) {
    ll m = (l + r) / 2;
    if (m * m > n)
      r = m;
    else
      l = m;
  }
  return l;
}

int main() {
  long double X, Y, R;
  cin >> X >> Y >> R;
  const ll a = 10000;
  X *= (long double)a;
  Y *= (long double)a;
  R *= (long double)a;
  X = roundl(X);
  Y = roundl(Y);
  R = roundl(R);
  ll x = (ll)X;
  ll y = (ll)Y;
  ll r = (ll)R;

  ll s = (x - r) / a;
  if (s * 10000 < x - r) {
    s++;
  }
  ll ans = 0;
  for (ll i = s * a; i <= x + r; i += a) {
    ll p = r * r - (x - i) * (x - i);
    ll q = sqrtl(p);
    ll b = (y - q) / a;
    if (b * a < y - q) {
      b++;
    }
    ll c = (y + q) / a;
    if (c * a > y + q) {
      c--;
    }
    ans += (c * a - b * a) / a + 1;
  }

  cout << ans << endl;

  return 0;
}

#include<bits/stdc++.h> // 57 68 "Baklol, Take it easy"
using namespace std;

#define rep(x, k, n)        for(int x = (k); x < (n); ++x)
#define repr(x, k, n)       for(int x = (k); x > (n); --x)
#define tr(it, a)           for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll                  long long
#define siz(x)              ((int)(x).size())
#define pb                  push_back
#define mp                  make_pair
#define F                   first
#define S                   second
#define o2(x)               ( (x) * (x) )
#define all(x)              (x).begin(), (x).end()
#define clr(x, k)           memset(x, k, sizeof(x)) // 0, -1
#define sortall(x)          sort(all(x))
#define sortalld(x)         sort(all(x), greater<int>())
#define printv(v, x)        rep(i, 0, x) cout << v[i] << " \n"[i+1==x];
#define print2dv(V, y, x)   rep(j, 0, y) printv(V[j], x);
#define out(x)              cout << ((x) ? "Yes" : "No") << '\n'
#define setbits(x)          __builtin_popcountll(x)
#define inf                 INT_MAX
#define ninf                INT_MIN
#define int                 long long // "be carefull"

typedef pair<int, int>      pii;
typedef vector<int>         vi;
typedef vector<bool>        vb;
typedef vector<pii>         vpii;
typedef vector<vi>          vvi;
typedef vector<vb>          vvb;

template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
    cout << name << " = " << arg1  << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " = " << arg1 <<",";
    debug_out(comma+1, args...);
}

#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif

const int mod = 1e9 + 7; // 998244353;
const int N = 3e5 + 5, M = N;

//-----------------------------------------------------------------------------

void Solve_main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << min({a, b, c, d});
}

//-----------------------------------------------------------------------------

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

    // cout << setprecision(12) << fixed;
    int tc = 1;
    // cin >> tc;
    for(int i = 0; i < tc; i++) {
        Solve_main();
    }

    cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
    return 0;
}

/*
-> edge cases.
-> Submit - right file.
-> Move on - if completely_stuck > 30 minute.
-> entire input - multiple testcases.
-> time complexity - 1 sec : 4e8 will work but risky.
-> space complexity - 256 mb : 6e7(int), 3e7(ll), 2e8(bool, char),  will work.
-> mod.
-> builtin fuction - add ll.
-> Iterative > Recursive.
-> clear - global variables.
*/


#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (31 - __builtin_clz(x))
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 19, mod = 1000000007;

template <class T1, class T2> inline void addMod(T1& a, T2 b)	{   a = (a + b) % mod;  }
template <class T1, class T2> inline void multMod(T1& a, T2 b)	{   a = a * b % mod;    }

long long sum(long long a, long long b)
{	return (a+b) * (b-a+1) / 2 % mod;	}

void solve()
{
	long long n, a, b;
	scanf ("%lld%lld%lld", &n, &a, &b);
	if (a > b)
		swap(a, b);

	long long all = kw(n-a+1) % mod;
	multMod(all, kw(n-b+1) % mod);

	long long x = n+1-min(a+b-1, n);
	long long y = n+1-(b+1);
	long long bad = sum(x, y);
	multMod(bad, 2);
	addMod(bad, (n-b+1) * (b-a+1));
	multMod(bad, bad);
	multMod(bad, mod-1);
	long long res = all;
	addMod(res, bad);
	printf("%lld\n", res);
}

int main()
{
	int t = 1;
	scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}


/*
 * Author : Siddhant Khare
 */
#include <bits/stdc++.h>
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ll long long
#define ld long double
#define arr array
#define si(t) scanf("%d", &t)
#define sl(t) scanf("%ld", &t)
#define sll(t) scanf("%lld", &t)
#define sc(t) scanf("%c", &t)
#define ss(t) scanf("%s", &t)
#define sf(t) scanf("%f", &t)
#define slf(t) scanf("%lf", &t)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%ld\n", x)
#define pll(x) printf("%lld\n", x)
#define pf(x) printf("%f")
#define sz(x) (int)(x).size()
#define mem(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(x) x.begin(), x.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define sortall(x) sort(all(x))
#define mp make_pair
#define pb push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;

/**************************************/

/******** User-defined Function *******/
void solve()
{
    ll n;
    cin >> n;
    ll a[n], b[n];
    set<ll> st;
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i] >> b[i];
        st.insert(a[i]);
        st.insert(b[i]);
    }
    cout << st.size() << "\n";
    // Algorithm

    // Output
    // pll(ans) // long long print
}

/**************************************/

/********** Main()  function **********/
ll prt[400001];
ll find_set(ll v)
{
    if (v == prt[v])
        return v;
    return find_set(prt[v]);
}

void union_sets(ll a, ll b)
{
    a = find_set(a);
    b = find_set(b);
    if (a != b)
        prt[b] = a;
}

signed main()
{
    ll n, a, b;
    cin >> n;
    set<ll> res;
    for (ll i = 0; i <= 400000; i++)
        prt[i] = i;
    map<ll, vector<ll>> v;
    for (ll i = 0; i < n; i++)
    {
        cin >> a >> b;
        if (a > b)
            swap(a, b);
        v[a].push_back(b);
    }
    for (auto i : v)
    {
        for (auto j : i.second)
        {
            if (res.find(find_set(j)) == res.end())
                res.insert(find_set(j)), union_sets(i.first, j);
            else
                res.insert(find_set(i.first));
        }
    }
    cout << res.size() << endl;
}
/********  Main() Ends Here *************/

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)
//
////#define ATCODER
//#ifdef ATCODER
//#include<atcoder/all>
//using namespace atcoder;
//#endif

#include <algorithm>
#include <utility>
#include <vector>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <numeric>
#include <type_traits>

using namespace std;

#ifdef _MSC_VER
#include"stdafx.h"
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
void debug_out() { cerr << endl; }

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

#define ll long long
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define VSORT(v) sort(v.begin(), v.end());
#define VREVERSE(v) reverse(v.begin(), v.end())

#define print(x) cout << (x) << '\n'
#define spa <<" "<<
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
//#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline bool chmin(T1 & a, T2 b) { if (a > b) { a = b; return true; }return false; }
template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; }return false; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 400040;
const double pi = acos(-1);
constexpr long double EPS = 1e-14;
const int dx[4] = { 0,0,-1,1 }, dy[4] = { -1,1,0,0 };
using ld = long double;

bool G[20][20];
int INF = 1e9;
int dp[1 << 18];
int N;
bool possible[1 << 18];
int ans = INF;
int rec(int bit) {
	if (dp[bit] != INF)return dp[bit];
	int n = __builtin_popcountll(bit);

	if (n == 0)return dp[n] = 0;
	if (possible[bit])return dp[bit] = 1;
	vector<int>id(n);
	int idx = 0;
	REP(i, N) {
		if (bit >> i & 1)id[idx++] = i;
	}
	int ret = n;
	for (int nbit = bit;; nbit = (nbit - 1)&bit) {
		if (nbit == bit)continue;
		if (nbit == 0)break;
		ret = min(ret, rec(nbit) + rec(bit^nbit));

	}
	//for (int k = n-1; k >= 1; k--) {
	//	vector<int>p(n);
	//	FOR(i, n - k, n)p[i] = true;
	//	do {
	//		int bb = 0;
	//		REP(i, n) {
	//			if (p[i])bb |= 1 << id[i];
	//		}
	//		int bbb = bit ^ bb;
	//		ret = min(ret, rec(bb) + rec(bbb));
	//	} while (next_permutation(all(p)));
	//}
	//
	//DEBUG(b, ret);
	return dp[bit] = ret;
}

void solve() {
	cin >> N;
	int M; cin >> M;
	REP(i, 1 << N)dp[i] = INF;

	if (M == 0) {
		print(N);
		return;
	}
	REP(i, M) {
		int u, v; cin >> u >> v;
		u--, v--;
		G[u][v] = G[v][u] = true;
	}

	REP(i, N)G[i][i] = true;
	REP(bit, 1 << N) {
		int n = __builtin_popcount(bit);
		if (n <= 1)possible[bit] = true;
		bool ok = true;
		REP(i, N) {
			if (bit >> i & 1) {
				FOR(j, i + 1, N) {
					if (bit >> j & 1) {
						if (!G[i][j]) {
							ok = false;
							break;
						}
					}
				}
			}
		}
		if (ok)possible[bit] = true;
	}
	//print(1); return;
	ans = INF;
	int bit = 0;
	REP(i, N)bit |= 1 << i;
	print(rec(bit));
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//int q; cin >> q;
	//while (q--)
	solve();
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
constexpr int MX = 200005;
int p[MX];
map<int,int> mp[MX];
int find(int x){return (p[x] < 0 ? x : p[x] = find(p[x]));}
void uni(int x, int y){
	x = find(x); y = find(y);
	if(x != y){
		if(p[x] > p[y]) swap(x, y);
		p[x] += p[y];
		p[y] = x;
		for(auto [i,v]: mp[y]) mp[x][i] += v;
	}
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	int n, q; cin >> n >> q;
	fill(p, p+n, -1);
	rep(i,n){
		int c; cin >> c;
		mp[i][c-1] = 1;
	}
	rep(i,q){
		int t, a, b; cin >> t >> a >> b; a--; b--;
		if(t == 1) uni(a,b);
		else cout << mp[find(a)][b] << "\n";
	}
}

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

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 122) return char(x-32);
    if( 65 <= int(x) && int(x) <= 90) return x;
    return -1;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 122) return x;
    if( 65 <= int(x) && int(x) <= 90) return char(x+32);
    return -1;
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57) return int(x-'0');
    if(97 <= int(x) && int(x) <= 122) return int(x-96);
    if(65 <= int(x) && int(x) <= 90) return int(x-64);
    return -1;
}
bool isect (int l1, int r1, int l2, int r2){
    pii p1 = {l1,r1};
    pii p2 = {l2,r2};
    if(p1>p2)swap(p1,p2);
    return (p2.fi <= p1.se);
}
ll quickpow (ll num1, ll num2, ll MOD){
    if(num2==0)return 1%MOD;
    else if(num2==1)return num1%MOD;
    else{
        ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
int par[200010],n,c[200010];
map <int,int> m[200010];
int findrep(int x) {return par[x] == x ? x : par[x] = findrep(par[x]);}
void join(int a, int b){
    int repa = findrep(a);
    int repb = findrep(b);
    if(repa == repb) return;
    if(m[repa].size() < m[repb].size()) swap(repa,repb);
    for(auto i : m[repb]){
        int x = i.fi; int y = i.se;
        m[repa][x] += y;
    }
    m[repb].clear();
    par[repb] = repa;
}
int main(){

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

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

    int q; cin >> n >> q;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
        m[i][c[i]] = 1;
        par[i] = i;
    }
    while(q--){
        int t, a, b; cin >> t >> a >> b;
        if(t == 1) join(a,b);
        else{
            int rep = findrep(a);
            int ans = 0;
            if(m[rep].count(b)) ans = m[rep][b];
            else ans = 0;
            cout << ans << newl;
        }
    }

    return 0;
}


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

const ll mod = 1e9 + 7;

int run_test(){
  int n;
  cin >> n;
  int maxi = 0, mini = mod;
  for(int i = 0; i < n; i++){
    int k;
    cin >> k;
    maxi = max(maxi, k);
  }
  for(int i = 0; i < n; i++){
    int k;
    cin >> k;
    mini = min(mini, k);
  }
  cout << max(0, mini-maxi+1);
  return 0;
}

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int tt;
  tt = 1;
  while(tt--){
    run_test();
  }
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (ll i = 0; i < n; i++)
#define revf(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define rep1(i, b) for (ll i = 1; i <= b; i++)
#define inp(a, n)              \
	for (ll i = 0; i < n; i++) \
	cin >> a[i]
#define outp(a, n)             \
	for (ll i = 0; i < n; i++) \
	cout << a[i] << " "
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))
#define pb push_back
#define mp make_pair
#define BINSC binary_search
#define BITOUT __builtin_popcount
#define mem(x, n) memset(x, n, sizeof(x))
#define fast                          \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);
#define test  \
	ll t;     \
	cin >> t; \
	while (t--)
#define cn cout << "\n";
int main()
{
  fast
  ll n;
  cin>>n;
  ll ar[n],br[n];
  inp(ar,n);
  inp(br,n);
  set<ll>s;
  for(ll i=1;i<=1000;i++)
  s.insert(i);
  ll q=0;
  REP(i,1,1001)
  {
       forr(j,n)
       {
            if(ar[j]<=i&&i<=br[j])
            q=1;
            else
            s.erase(i);
       }
  }
  cout<<s.size();
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i,m,n) for(int i = m - 1; i >= n; i--)
#define ALL(v) v.begin(), v.end()
#define itn int
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define YES() cout << "YES" << endl
#define NO() cout << "NO" << endl
#define println(x) cout << x << endl
#define print(x) cout << x << " "
template<typename T, typename U>
inline bool CMAX(T &m, U x) { if (m < x) { m = x; return true; } return false; }
template<typename T, typename U>
inline bool CMIN(T &m, U x) { if (m > x) { m = x; return true; } return false; }

typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;
const int MOD = 1e9+7;
int main(){
    lint n;
    cin >> n;
    int digit = 0;
    lint ans = 0;
    lint temp = n;
    while(temp != 0){
        temp /= 10;
        digit++;
    }
    if(digit <= 3){
        println(0);
        return 0;
    }
    FOR(i,3,digit - 1){
        ans += (i / 3) * (powl(10,i + 1) - powl(10,i));
    }
    ans += ((digit - 1) / 3) * (n - lint(powl(10,digit - 1)) + 1 );
    println(ans);
    return 0;
}

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

int main(){
    double n, x0, y0, xh, yh;
    cin >> n >> x0 >> y0 >> xh >> yh;
    double xc = (x0 + xh)/2;
    double yc = (y0 + yh)/2;
    double arc = 3.1415926535897932384 * 2;
    cout << setprecision(10) << ((x0 - xc)* cos(arc/n)) - ((y0 - yc) * sin(arc/n)) + xc <<" " <<
     ((x0 - xc) * sin(arc/n)) + ((y0 - yc) * cos(arc/n)) + yc << endl;

}


#include<bits/stdc++.h>

using namespace std;

using ll = long long int;
using vi = vector<int>;
#define f(n) for(int i = 0; i < n; i++)
#define fj(n) for(int j = 0; j < n; j++)
#define fk(n) for(int k = 0; k < n; k++)
#define ci(n) cin >> n;
#define ic(n) int n; cin >> n;
#define lc(n) int n; cin >> n;
#define all(a) a.begin(), a.end()
#define pri(a, n) for(int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n";
#define scl(a, n) ll a[n] = {0}; for(int i = 0; i < n; i++) cin >> a[i];
#define pb push_back
#define eb emplace_back
#define sc second
#define ft first
#define int long long
void prip(int a[], int n){f(n) cout << a[i] << ' '; cout << '\n';}

const int inf = 2e9, mxp = 10, maxn = 2e6, mod = 1e9 + 7;
int bp(int a, int n)
{
    if(n == 0) return 1;
    if(n & 1ll) return (a * bp(a, n - 1)) % mod;
    int res = bp(a, n >> 1);
    return (res * res) % mod;
}
int fc[maxn + 1], rc[maxn + 1];
int cnk(int n, int k)
{
    if(k > n) return 0;
    int ans = (fc[n] * rc[k]) % mod;
    return (ans * rc[n - k]) % mod;
}

long long ans = 0;
void INVERSIONS(long long a[], int l, int r)
{
    if (r - l <= 1)
    {
        return;
    }
    int m = (l + r) / 2;
    INVERSIONS(a, l, m);
    INVERSIONS(a, m, r);
    long long b[r - l];
    int ll = l, rr = m;
    while (ll < m && rr < r)
    {
        if (a[ll] <= a[rr])
        {
            b[ll - l + rr - m] = a[ll];
            ll++;
        }
        else
        {
            b[ll - l + rr - m] = a[rr];
            rr++;
            ans += m - ll;
        }
    }
    while (ll < m)
    {
        b[ll - l + rr - m] = a[ll];
        ll++;
    }
    while (rr < r)
    {
        b[ll - l + rr - m] = a[rr];
        rr++;
    }
    for (int i = l; i < r; i++)
    {
        a[i] = b[i - l];
    }
}
main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ic(n)
    int a[2 * n], ta[n];
    f(n) cin >> a[i], a[i + n] = a[i], ta[i] = a[i];
    INVERSIONS(ta, 0, n);
    cout << ans << '\n';
    for(int i = 0; i < n - 1; i++) {
        ans += (n - a[i + n] - 1);
        ans -= a[i];
        cout << ans << '\n';
    }
}

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef long double ld;
#define f first
#define s second

const int N = 3e5 + 100;
const int sq = 100;
const ll mod = 95542721;
const int inf = 1e7;

ll seg[4*N], a[N], ind[N], n;

void add(int x, int b = 0, int e = n, int ind = 1)
{
	if(b + 1 == e)
	{
		seg[ind]++;
		return;
	}
	int mid = (b + e) / 2;
	if(x < mid)
		add(x, b, mid, ind*2);
	else
		add(x, mid, e, ind*2+1);

	seg[ind] = seg[ind*2] + seg[ind*2+1];
}

ll get(int l, int r, int b = 0, int e = n, int ind = 1)
{
	if(r <= b || e <= l)
		return 0;
	if(l <= b && e <= r)
		return seg[ind];

	int mid =(b + e) / 2;
	return get(l, r, b, mid, ind*2) + get(l, r, mid, e, ind*2+1);
}

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

	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		ind[a[i]] = i;
	}
	ll inv = 0;
	for(int i = n-1; i >= 0; i--)
	{
		add(ind[i]);
		inv += get(0, ind[i]);
		//cout << ind[i] << ' ' << get(0, ind[i]) << endl;
	}

	cout << inv << endl;
	for(int i = 0; i < n-1; i++)
	{
		inv = inv - a[i] + (n - a[i] - 1);
		cout << inv << endl;
	}

	return 0;
}


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

using namespace std;

void bmain()
{
    int n; cin >> n;
    long c[n][n];
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            cin >> c[i][j];
        }
    }
    long a0[n];
    for (int i=0;i<n;i++) a0[i]=c[0][i];
    long b0[n];
    b0[0]=0;
    for (int i=1;i<n;i++){
        b0[i]=c[i][0]-c[0][0];
        for (int j=1;j<n;j++){
            if (c[i][j]-a0[j]!=b0[i]){
                cout << "No" << endl;
                return;
            }
        }
    }
    long minb=b0[0];
    for (int i=1;i<n;i++) minb=min(b0[i],minb);
    if (minb<0){
        for (int i=0;i<n;i++){
            b0[i]-=minb;
            a0[i]+=minb;
            if (a0[i]<0){
                cout << "No" << endl;
                return;
            }
        }
    }
    cout << "Yes" << endl;
    for (int i=0;i<n;i++) cout << b0[i] << " ";
    cout << endl;
    for (int j=0;j<n;j++) cout << a0[j] << " ";
    cout << endl;
    return;
}

void amain()
{
    int n,m; cin >> n >> m;
    string s;
    long x[2];
    x[0]=0;
    x[1]=0;
    for (int i=0;i<n;i++) {
        cin>>s;
        int cpt=0;
        for (int j=0;j<m;j++){
            if (s[j]=='1') cpt++;
        }
        x[cpt%2]++;
    }
    cout << x[0]*x[1] << endl;
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    bmain();
    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;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
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;
  vi l(n, INF), r(n, -INF);
  rep(i, n) {
    int x, c;
    cin >> x >> c;
    --c;
    chmin(l[c], x);
    chmax(r[c], x);
  }
  vector<P> d;
  d.emplace_back(0,0);
  rep(i, n) if (l[i] != INF) d.emplace_back(l[i], r[i]);
  d.emplace_back(0,0);
  vl dp(2);
  for (int i = 1; i < d.size(); ++i) {
    vl p(2, LINF);
    swap(p, dp);
    int l = d[i].first, r = d[i].second;
    rep(j, 2) {
      int x = j ? d[i-1].second : d[i-1].first;
      chmin(dp[0], p[j] + abs(x-r) + r-l);
      chmin(dp[1], p[j] + abs(x-l) + r-l);
    }
  }
  cout << dp[0] << endl;
}


#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; ll x; cin >> n >> x;
    vector<ll> a(n);
    cin >> a;

    vector<ll> k(n);
    for(int i=0; i<n-1; i++){
        k[i] = a[i+1]/a[i];
    }
    k[n-1] = 1e18;

    vector<ll> xs(n);
    for(int i=n-1; i>=0; i--){
        xs[i] = x/a[i];
        x -= a[i] * xs[i];
    }

    ll dp[51][2];
    dp[0][0] = 1;
    dp[0][1] = 0;
    for(int i=0; i<n; i++){
        dp[i+1][0] = dp[i+1][1] = 0;
        dp[i+1][0] += dp[i][0];
        if(xs[i]>0){
            dp[i+1][1] += dp[i][0];
        }

        if(xs[i]<k[i]-1){
            dp[i+1][0] += dp[i][1];
        }

        dp[i+1][1] += dp[i][1];
    }

    cout << dp[n][0] << endl;

    return 0;
}

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << b - c << endl;
    return 0;
}


//IQ134高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB  998244353

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
    for (T& x: vec) { is >> x; }
    return is;
}

int main() {
	ll ans = 0;
	ll tmp = 0;
	int n; cin >> n;
	vector<int> a(n), b(n), p(n);
    cin >> a >> b >> p;
    bool ch = true;
    REP(i, n) p[i]--;
    REP(i, n){
        if(p[i] != i && a[i] <= b[p[i]]) ch = false;
    }
    if(ch == false){
        cout << -1 << endl;
    }
    else{
        priority_queue<pair<int, int>> q;
        vector<pair<int, int> > answer;
        REP(i, n){
            q.push(make_pair(a[i], i));
        }
        while(!q.empty()){
            int tai = q.top().first;
            int num = q.top().second;
            q.pop();
            //cout << "human" << num << "(weight:" << tai << ") has bag" << p[num] << endl;
            if(p[num] == num) continue;
            else{
                answer.push_back(make_pair(num, p[num]));
                swap(p[num], p[p[num]]);
                q.push(make_pair(tai, num));
            }
        }
        cout << answer.size() << endl;
        REP(i, answer.size()) cout << answer[i].first + 1 << " " << answer[i].second + 1 << endl;
    }
	//cout << ans << endl;
}

/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \
      |             |       |\          |    |       |       |\          |
   \  |             |       | \         |    |       |       | \         |
    \ |             |       |  \        |    |        \     /   \        |
     V              |       |   \        \__/|         -----     \       |
*/
#include <bits/stdc++.h>
using namespace std;

#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS  = 1e-8;
const int INF     = 0x3F3F3F3F;
const ll LINF     = 4611686018427387903;
const int MOD     = 1e9+7;
/*--------------------------------------------------------------------------------------*/

signed main() { EmiliaMyWife
	int n;
	cin >> n;
	vector<int> arr(n + 1), brr(n + 1), p(n + 1);
	for(int i = 1; i <= n; i++)
		cin >> arr[i];
	for(int i = 1; i <= n; i++)
		cin >> brr[i];
	for(int i = 1; i <= n; i++)
		cin >> p[i];
	vector<pair<int, int>> owo(n);
	for(int i = 0; i < n; i++)
		owo[i] = {arr[i + 1], i + 1};
	sort(owo.begin(), owo.end(), greater<pair<int, int>>());
	vector<pair<int, int>> ans;
	for(const auto &[a, id] : owo) {
		if(p[id] == id)
			continue;
		if(a <= brr[p[id]])
			return cout << "-1\n", 0;
		while(p[id] != id) {
			if(arr[p[id]] <= brr[p[p[id]]])
				return cout << "-1\n", 0;
			ans.push_back({id, p[id]});
			swap(p[id], p[p[id]]);
		}
	}
	cout << ans.size() << '\n';
	for(auto [a, b] : ans)
		cout << a << ' ' << b << '\n';
}


#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <iomanip>
#include <bitset>
using namespace std;
#define pi pair<int ,int>
long long n , m , p[5001][5001] , mod = 998244353 , kq = 0;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	srand (time(NULL));
	int test = 1;
	//cin >> test;
	while (test--)
	{
		cin >> n >> m;
		for (int i = 1 ; i <= n || i <= m ; i++)
		{
			p[i][0] = 1;
			for (int e = 1 ; e <= n || e <= m ; e++)
			{
				p[i][e] = 1ll * p[i][e-1] * i % mod;
			}
		}
		p[0][0] = 1;
		for (int i = 1 ; i <= n ; i++)
		{
			for (int e = 1 ; e <= m ; e++)
			{
				long long res = p[m - e][i - 1] * p[m][n - i - 1] % mod;
				res *= 1ll * (n-i);
				res %= mod;
				kq += res;
				kq %= mod;
			}
		}
		cout <<(1ll * n * p[m][n] % mod - kq + mod) % mod<<'\n';
	}
}

// In the name of Arceus

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

#define For(i, l, r) for (i = int(l); i <= int(r); i++)
#define Fo(i, n) For(i, 1, n)
#define Rof(i, r, l) for (i = int(r); i >= int(l); i--)
#define Ro(i, n) Rof(i, n, 1)
#define clr(a) memset(a, 0, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(b))
#define fc(a, ch) memset(a, ch, sizeof(a))

typedef unsigned long long uint64;
typedef long long int64;

#define T1 template <class A>
#define T2 template <class A, class B>
#define T3 template <class A, class B, class C>
#define T4 template <class A, class B, class C, class D>
inline void read(char &x) {
    do
        x = getchar();
    while (x <= ' ');
}
inline void read(char *s) {
    char ch;

    do
        ch = getchar();
    while (ch <= ' ');
    while (ch > ' ') {
        (*s) = ch;
        s++;
        ch = getchar();
    }
    (*s) = 0;
}
inline void read(std::string &s) {
    char ch;

    do
        ch = getchar();
    while (ch <= ' ');
    while (ch > ' ') {
        s.push_back(ch);
        ch = getchar();
    }
}
T1 inline void readint(A &x) {
    bool neg = false;
    char ch;

    do
        ch = getchar();
    while (ch <= ' ');
    if (ch == '-') {
        neg = true;
        ch = getchar();
    }
    x = 0;
    while (ch > ' ') {
        x = x * 10 + (ch & 15);
        ch = getchar();
    }
    if (neg)
        x = -x;
}
inline void read(int &x) { readint(x); }
inline void read(int64 &x) { readint(x); }

T2 inline void read(A &a, B &b) {
    read(a);
    read(b);
}
T3 inline void read(A &a, B &b, C &c) {
    read(a);
    read(b);
    read(c);
}
T4 inline void read(A &a, B &b, C &c, D &d) {
    read(a);
    read(b);
    read(c);
    read(d);
}
inline void writeln() { putchar('\n'); }
T1 inline void writeint(A x) {
    static char buf[20];
    int top = 0;

    if (!x) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    while (x) {
        buf[++top] = (x % 10) | 48;
        x /= 10;
    }
    while (top)
        putchar(buf[top--]);
}
inline void write(int x) { writeint(x); }
inline void write(unsigned x) { writeint(x); }
inline void write(int64 x) { writeint(x); }
inline void write(uint64 x) { writeint(x); }
inline void write(char ch) { putchar(ch); }
inline void write(char *s) {
	while(*s) {
		putchar(*s);
		s++;
	}
}
inline void write(const char *s) { printf(s); }
T1 inline void write_(A x) {
    write(x);
    putchar(' ');
}
T1 inline void writeln(A x) {
    write(x);
    putchar('\n');
}
T2 inline void write(A a, B b) {
    write_(a);
    write(b);
}
T2 inline void writeln(A a, B b) {
    write_(a);
    writeln(b);
}
T3 inline void writeln(A a, B b, C c) {
    write_(a);
    write_(b);
    writeln(c);
}
T4 inline void writeln(A a, B b, C c, D d) {
    write_(a);
    write_(b);
    write_(c);
    writeln(d);
}
#undef T1
#undef T2
#undef T3
#undef T4

// f[n][m]: 长度为 n，最大值不超过 m
// f[n][m] = \sum_{i = 1}^n
//			1)     (m - 1)^{n - 1} + m^{n - i} f[i - 1][m - 1] + (m - 1)^{i - 1} f[n - i][m]
//         = n(m - 1)^{n - 1} + m^n \sum_{i = 1}^n m^{-i} f[i - 1][m - 1] + (m - 1)^{-1} \sum_{i = 1}^n (m - 1)^i f[n - i][m]

// g[m][n]

const int N = 5000 + 10;
const int MOD = 998244353;

int n0, m0;
int g[N][N];

int main() {
	int n, m, mp;
	int fx, fy;
	int i;

	read(n0, m0);
	Fo(i, n0)
		g[1][i] = 1;

	For(m, 2, m0) {
		fx = fy = 0;
		mp = 1;
		Fo(n, n0) {
			fx = (1LL * fx * m + g[m - 1][n - 1]) % MOD;
			fy = (1LL * fy * (m - 1) + g[m][n - 1]) % MOD;
			g[m][n] = (g[m - 1][n] + 1LL * n * mp + fx + fy) % MOD;
			mp = mp * (m - 1LL) % MOD;
		}
	}
	writeln(g[m0][n0]);
	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

#include <algorithm>
#include <numeric>
#include <cmath>

using namespace std;
typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;

static const int  MAX_INT  = std::numeric_limits<int>::max(); // 2147483647 = 2^31-1
static const long MAX_LONG = std::numeric_limits<long>::max();
static const ll   MAX_LL   = std::numeric_limits<long long>::max();
static const int  INF      = (1 << 30);

#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)

///////////////////////////////////////////////////
// ------------------- utils ------------------- //
///////////////////////////////////////////////////
// change min/max
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; }

///////////////////////////////////////////////////
// ------------------- main -------------------- //
///////////////////////////////////////////////////

ll cnvtoN(ll ten, ll n) {
    ll res = 0;
    ll factor = 1;
    while (ten > 0) {
        res += (ten % n) * factor;
        ten /= n;
        factor *= 10;
    }
    return res;
}

//bool hasSeven(ll nbin) {
//    ll tgtNb = nbin;
//    while (tgtNb > 0) {
//        const ll res = tgtNb % 10;
//        if (res == 7) return true;
//        tgtNb /= 10;
//        if (tgtNb == 0) break;
//    }
//}

void Main() {
    ll N; cin >> N;

    ll cnt = 0;

    for (ll i = 1; i <= N; ++i) {
        // ten
        // if (hasSeven(i)) continue;
        bool hasS = false;
        ll tgtNb = i;
        while (tgtNb > 0) {
            const ll res = (tgtNb % 10);
            if (res == 7) {hasS = true; break;};
            tgtNb /= 10;
        }
        if (hasS) continue;

        // e
        ll ite = cnvtoN(i, 8);
        // if (hasSeven(e)) continue;
        tgtNb = ite;
        while (tgtNb > 0) {
            const ll res = (tgtNb % 10);
            if (res == 7) {hasS = true; break;};
            tgtNb /= 10;
        }
        if (hasS) continue;
        ++cnt;
    }
    cout << cnt << endl;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout << std::fixed << std::setprecision(15);
	Main();

	double tmp;
	cin >> tmp;

	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXN = 200000;
const int MOD = 998244353;
const int MAXPW = 300;
const int INV2 = (MOD + 1) / 2;
void inc(int& a, int b) { if ((a += b) >= MOD) a -= MOD; }

int n, mxpw;
int a[MAXN];
int ans[MAXPW + 1];

int sum[MAXPW + 1];
int choose[MAXPW + 1][MAXPW + 1];

void solve() {
	REPE(i, mxpw) { choose[i][0] = choose[i][i] = 1; FOR(j, 1, i) choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % MOD; }

	REPE(i, mxpw) sum[i] = 0;
	REP(i, n) {
		int x = 1;
		inc(sum[0], x);
		FORE(j, 1, mxpw) { x = (ll)x * a[i] % MOD; inc(sum[j], x); }
	}
	//printf("sum:"); REPE(i, mxpw) printf(" %d", sum[i]); puts("");

	FORE(x, 1, mxpw) {
		ans[x] = 0;
		REPE(i, x) {
			int num = ((ll)sum[i] * sum[x - i] - sum[x]) % MOD;
			//printf("(%d,%d) -> %d*%d/2\n", i, x, choose[x][i], num);
			ans[x] = (ans[x] + (ll)choose[x][i] * num % MOD * INV2) % MOD;
		}
	}
}

void run() {
	scanf("%d%d", &n, &mxpw);
	REP(i, n) scanf("%d", &a[i]);
	solve();
	FORE(i, 1, mxpw) printf("%d\n", ans[i]);
}

int main() {
	run();
	return 0;
}

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep_e(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 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;

int main() {
    long long n;
    string t;
    cin >> n >> t;
    long long ans = 0;
    string s = "110";
    int count = 1;
    long long max = pow(10, 10);
    if(t == "1"){
        ans = max * 2;
    }else if(t == "0"){
        ans = max;
    }else if(t == "00"){
        ans = 0;
    }else if(t == "01"){
        ans = max - 1;
    }else if(t == "10"){
        ans = max;
    }else if(t == "11"){
        ans = max;
    }else{ // 3文字以上
        while((int)s.size() - 3 < n){
            s = s + "110";
            count++;
        }

        rep(i, (int)s.size()){
            if(s.substr(i, n) == t){
                ans++;
            }
        }
        if(ans == 0){
            ans = 0;
        }else{
            if(t[0] == '0' and t[1] == '1' and t[2] == '1' and (int)t.size() % 3 == 2){
                ans = max - (int)t.size() / 3 - 1;
            }else if(t[0] == '0' and t[1] == '1' and t[2] == '1' and (int)t.size() % 3 != 2){
                ans = max - (int)t.size() / 3;
            }else if(t[0] == '1' and t[1] == '0' and t[2] == '1'){
                ans = max - (int)t.size() / 3;
            }else if(t[0] == '1' and t[1] == '1' and t[2] == '0' and (int)t.size() % 3 == 0){
                ans = max - (int)t.size() / 3 + 1;
            }else if(t[0] == '1' and t[1] == '1' and t[2] == '0' and (int)t.size() % 3 != 0){
                ans = max - (int)t.size() / 3;
            }
        }
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define matsuri pair<int,int>
#define iris 1000000007
using namespace std;

vector<int> G[111];
matsuri arr[11111];
int aoi[111][111],c[111],ans[11111];
bool v[111];

void dfs(int a,int f)
{
	v[a]=1;
	for(int b:G[a])
	{
		if(b==f)
			continue;
		int e=max(aoi[a][b], aoi[b][a]);
		if(ans[e]==0)
		{
			if(aoi[a][b])
				ans[e]=1;
			else
				ans[e]=2;
		}
		if(!v[b])
			dfs(b,a);
	}
}

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

	int n,m,i,a,b;

	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
		cin>>a>>b;
		arr[i]={a,b};
		aoi[a][b]=i;
	}
	for(i=1;i<=n;i++)
	{
		cin>>c[i];
	}
	for(i=1;i<=m;i++)
	{
		a=arr[i].first;
		b=arr[i].second;
		if(c[a]==c[b])
		{
			G[a].emplace_back(b);
			G[b].emplace_back(a);
		}
		else if(c[a]>c[b])
			ans[i]=1;
		else
			ans[i]=2;
	}
	for(i=1;i<=n;i++)
		if(!v[i])
			dfs(i,0);
	for(i=1;i<=m;i++)
	{
		if(ans[i]==1)
			cout<<"->\n";
		else
			cout<<"<-\n";
	}

	return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

int n,m,c[110],depth[110],ans[10010];
vector <pair <pii,int> > g[110];
vector <pii> e;
bool vis[110];

void dfs(int pos,int par,int d)
{
	vis[pos]=true;
	depth[pos]=d;
	bool bk=false;
	rep(i,g[pos].size())
	{
		if(g[pos][i].fi.fi==par&& !bk)
		{
			bk=true;
			continue;
		}
		if(vis[g[pos][i].fi.fi])
		{
			if(depth[g[pos][i].fi.fi]<d) ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
		}
		else
		{
			ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
			dfs(g[pos][i].fi.fi,pos,d+1);
		}
	}
}

int main()
{
	cin>>n>>m;
	int x,y;
	rep(i,m)
	{
		scanf("%d%d",&x,&y);
		e.pb(mpr(x,y));
	}
	repn(i,n) scanf("%d",&c[i]);
	rep(i,m)
	{
		if(c[e[i].fi]>c[e[i].se]) ans[i]=1;//->
		else if(c[e[i].fi]<c[e[i].se]) ans[i]=2;//<-
		else
		{
			g[e[i].fi].pb(mpr(mpr(e[i].se,i),0));
			g[e[i].se].pb(mpr(mpr(e[i].fi,i),1));
		}
	}
	repn(i,n)
	{
		if(vis[i]) continue;
		dfs(i,0,1);
	}
	rep(i,m) puts(ans[i]==1 ? "->":"<-");
	return 0;
}


#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;

template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
const double pi = 3.14159265358979;
const ll ju = 10000000000;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    ll n;
    string s;
    cin >> n >> s;

    if (n == 1){
        if (s == "0"){
            cout << ju << endl;
            return 0;
        }
        else{
            cout << 2*ju << endl;
            return 0;
        }
    }
    if (n == 2){
        if (s == "11"){
            cout << ju << endl;
            return 0;
        }
        else if (s == "10"){
            cout << ju << endl;
            return 0;
        }
        else if (s == "01"){
            cout << ju - 1 << endl;
            return 0;
        }
        else{
            cout << 0 << endl;
            return 0;
        }
    }

    ll type = -1;

    if (s[0] == '1' && s[1] == '1' && s[2] == '0') type = 1;
    else if (s[0] == '1' && s[1] == '0' && s[2] == '1') type = 2;
    else if (s[0] == '0' && s[1] == '1' && s[2] == '1') type = 3;

    if (type == -1){
        cout << 0 << endl;
        return 0;
    }

    bool pos = true;

    for (ll i = 3; i < n; i++){
        if (s[i] != s[i-3]){
            pos = false;
            break;
        }
    }

    if (!pos){
        cout << 0 << endl;
        return 0;
    }

    if (type == 1){
        if (n %3 == 0){
            cout << ju + 1 - n/3  << endl;
            return 0;
        }
        else{
            cout << ju - n/3 << endl;
            return 0;
        }
    }
    else if (type == 2){
        cout << ju - n/3 << endl;
        return 0;
    }
    else{
        if (n %3 == 0 || n %3 == 1){
            cout << ju - n/3  << endl;
            return 0;
        }
        else{
            cout << ju - 1 - n/3 << endl;
            return 0;
        }
    }

}

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

int main() {
    int N;
    string S = "110", T;
    cin >> N >> T;
    long long  all= 10000000000;
    long long res = 0;
    if (T == "0") res = all;
    else if (T == "1") res =  all * 2;
    else{
        //110のどこから始まるかで全探索
        for (int i = 0; i < 3; ++i) {
            //消費した110の数。例えば、0110はi=2のときのみだが、(4+2+2)/3=2で2個消費。
            //i=0なら2足しても結果は変わらない。i=1以上なら3がひとつ増えるように2がたされる。
            //2を足すのは、3個あるうちの1個でも消費していたら、それを消費したものと見なすため。
            long long a = (T.size() + i + 2)/3;
            //含まれていた場合のその個数
            long long add = all - a + 1;

            //含まれていたらadd そうでなければaddしない
            bool contain = true;
            for(int j=0; j < T.size();j++){
                if(T[j]!=S[(i+j)%3])contain=false;
            }
            if(contain) res+=add;
        }
   }
   cout << res << endl;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline int Asc(const void *a,const void *b){return strcmp((char*)a,(char*)b);}
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=(c==45),c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
int main(void)
{
  int N=IN(),check=0,len=0,sizeS=0,sizeT=0,idxS=0,idxT=0;char S[N][11],T[N][11],temp[11];
  rep(i,N)
  {
    if(!scanf("%s",temp)){exit(EXIT_FAILURE);}
    len=(int)strlen(temp);
    if(temp[0]=='!'){len--;rep(j,len){T[sizeT][j]=temp[j+1];}if(len<10){T[sizeT][len]=0;}sizeT++;}
    else{rep(j,len){S[sizeS][j]=temp[j];}if(len<10){S[sizeS][len]=0;}sizeS++;}
  }
  qsort(S,sizeS,11,Asc);qsort(T,sizeT,11,Asc);
  while(idxS<sizeS&&idxT<sizeT)
  {
    check=strcmp(S[idxS],T[idxT]);
    if(!check){return !puts(S[idxS]);}
    else if(check<0){idxS++;}else{idxT++;}
  }
  return !puts("satisfiable");
}

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

int main() {
  string S;
  cin >> S;
  int N = (int) S.size();
  for (int i = 0; i < N; i++) {
    if (S.at(i) == '6') {
      S.at(i) = '9';
    }
    else if (S.at(i) == '9') {
      S.at(i) = '6';
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    cout << S.at(i);
  }
  cout << endl;
  return 0;
}


#pragma GCC optimize(2)
#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define rush() int Case = 0; int T; cin >> T;  while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define sqr(x) x*x
#define lson rt<<1
#define rson rt<<1|1
#define ls lson, l, mid
#define rs rson, mid+1, r
#define y second
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-6;
const int N = 1e6+7;
string s, t;
bool judge()
{
    for(int i = 0; s[i]; i ++)  if(s[i] != 'a') return false;
    return true;
}
int main()
{
    int T;
    cin >> T;
    while(T --)
    {
        cin >> s;   t = s;
        if(judge())
        {
            cout << -1 <<endl;
            continue;
        }
        int res = mod;
        for(int i = 0; s[i]; i ++)
        {
            s = t;
            for(int j = i; s[j]; j ++)
            {
                swap(s[i], s[j]);
                if(s > "atcoder")
                {
                    res = min(res, j-i);
                    break;
                }
            }
        }
        cout << res <<endl;
    }
	return 0;
}

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORX(i, a, b, x) for (ll i = (a); i <= (b); i+=x)
#define debug(x) cout <<" || "<< #x << " is= " << x <<endl; //use for debug
#define read(a) ll a; cin >> a;
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long int
#define vi         vector<int>
#define vll        vector<ll>
#define vpii       vector<pair<int,int>>
#define pr         pair<long double,long double>
#define vpll       vector<pair<ll,ll>>
#define all(v)     v.begin(),v.end()
#define rall(v)    v.rbegin(),v.rend()  //reverse of above;
#define sz(container) int((container).size())
#define cut(x) {cout<<x<<"\n"; continue;}
#define setprec(x) cout << fixed << showpoint << setprecision(15)<<x<<"\n";
#define time cout << clock() / double(CLOCKS_PER_SEC) << endl;
#define endl '\n';
#define mod 1000000007
#define mod1 998244353
#define pi 3.141592653589793238
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

//*s.find_by_order() ->returns an iterator to the k-th largest element (counting from zero);
//s.order_of_key() ->number of elements strictly smaller then our item;
//st.rbegin()->second; last element in set
//st.erase(prev(st.end())); delete last element in set;
//sort(a+1,a+n+1,[](ll x,ll y){return abs(x)>abs(y);});
//sort(v.begin(), v.end(), greater<int>()); -> not sure work for first also if second are equal;
//q=lower_bound(all(v[a[i]]),i)-v[a[i]].begin()+1;
// s.substr(start_ind,length);
// vpll shift={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}} ->shift operator;
//bool comp(pr a,pr b){return a.second<b.second;} // use for differentiate order of comparision;
// cntbit(x) __builtin_popcount(x); -> no. of bits in given no.;

//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
const ll MAXN=2e7+5;
const ll inf=1e18+5;

int main()
{
  fast_io;
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  //“Don’t wish it were easier. Wish you were better.” – Jim Rohn;
  ll n; cin>>n;
  long double a[10];
  FOR(i,1,4) cin>>a[i-1];

  long double h,k,x,y,radius;
  h=(a[0]+a[2])/2.0;
  k=(a[1]+a[3])/2.0;

  // shift p0 wrt h,k ant. clkwise;

  long double theta=2*pi/(n*1.0);
  //debug(theta);
  a[0]-=h,a[1]-=k;

  x=a[0]*cos(theta)-a[1]*sin(theta);
  y=a[0]*sin(theta)+a[1]*cos(theta);

  x+=h,y+=k;
  setprec(x); setprec(y);

 }


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

int main(){
  int n; cin >> n;
  vector<int> a(n), b(n), c(n);
  for(int i = 0; i < n; ++i){cin >> a[i]; --a[i];}
  for(int i = 0; i < n; ++i){cin >> b[i]; --b[i];}
  for(int i = 0; i < n; ++i){cin >> c[i]; --c[i];}

  vector<long long> aa(n), bc(n);
  for(int i = 0; i < n; ++i){
    aa[a[i]] += 1;
    bc[b[c[i]]] += 1;
  }
  long long ans = 0;
  for(int i = 0; i < n; ++i){ans += aa[i]*bc[i];}
  cout << ans << endl;
}

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
typedef vector<int> vi;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define allrev(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())

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

    vi arr(n), b(n);
    forn(i, n)
    {
        cin >> arr[i];
    }
    forn(i, n)
    {
        cin >> b[i];
    }

    map<int, int> m;

    forn(i, n)
    {
        int x;
        cin >> x;
        --x;
        int val = b[x];
        m[val]++;
    }

    ll cnt = 0;

    forn(i, n)
    {
        if (m.find(arr[i]) != m.end())
        {
            cnt += m[arr[i]];
        }
    }

    cout << cnt << ln;
}
int main()
{
    fast_cin();
    ll t = 1;
    // cin >> t;
    for (int it = 0; it < t; it++)
    {
        // cout << "Case #" << it + 1 << ": ";
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
//ofstream cout("output.out");

#define fast() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define qii priority_que<int, vector<int>, greater<int>>
#define For(i, n) for(int i = 0; i < n; ++i)
#define For1(i, n) for(int i = 1; i <= n; ++i)
#define Forlr(i, l, r) for(int i = l; i <= r; ++i)
#define Forrl(i, r, l) for(int i = r; i >= l; --i)
#define o(a) { cout << #a << ":" << (a) <<" ";}
#define ov(a) {For(i, a.size()) cout << a[i] <<" ";}
#define ovv(a) {For(i, a.size()) {For(j, a[i].size()) cout << a[i][j] <<" ";cout <<"\n";}}
using ll = long long;
int n, m, k, q;
const int INF = 1e9 + 5;
const int nax = 1e5 + 5;
const ll mod = 1e9 + 7;
const int maxn = 3e5 + 7;

int main()
{
	fast();
	cin >> n;
	vi a(n);
	For(i, n) cin >> a[i];
	unordered_map<int, int> g;
	For(i, n){
		for(int j = 1; j * j <= a[i]; ++j){
			if(a[i] % j == 0){
				g[j] = __gcd(a[i], g[j]);
				g[a[i] / j] = __gcd(a[i], g[a[i] / j]);
			}
		}
	}
	int mi = *min_element(a.begin(), a.end());
	int rs = 1;
	for(auto i : g){
		if(i.first == i.second && i.first < mi) rs++;
	}
	cout << rs <<"\n";
}

#include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
	char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
	if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=105,mod=1000000007,inv2=(mod+1)/2;
typedef long long ll;
template<typename _Tp1,typename _Tp2>inline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);}
template<typename _Tp1,typename _Tp2>inline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);}
ll ksm(ll a,ll b=mod-2){ll res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod,b>>=1;}return res;}
int a[N],b[N];
int X[N*N],Y[N*N],n;
struct mat{
	int a[N][N];
	mat operator * (const mat &o)const{
		mat c;memset(c.a,0,sizeof(c.a));
		for(int i=0;i<n;++i)for(int k=0;k<n;++k)for(int j=0;j<n;++j)add(c.a[i][j],1ULL*a[i][k]*o.a[k][j]%mod);
		return c;
	}
}I,M,orig;
mat ksm(mat a,int b){
	mat res=I;
	while(b){
		if(b&1)res=res*a;
		a=a*a,b>>=1;
	}
	return res;
}
int main(){
	int m,k;read(n,m,k);
	for(int i=0;i<n;++i)I.a[i][i]=1;
	for(int i=0;i<n;++i)read(orig.a[i][0]);
	for(int i=1;i<=m;++i)read(X[i],Y[i]);
	ll inv=ksm(m);
	for(int i=0;i<n;++i)M.a[i][i]=1;
	for(int i=1;i<=m;++i){
		int x=X[i]-1,y=Y[i]-1;
		add(M.a[x][x],1LL*inv*(mod-inv2)%mod),add(M.a[x][y],1LL*inv*inv2%mod);
		add(M.a[y][y],1LL*inv*(mod-inv2)%mod),add(M.a[y][x],1LL*inv*inv2%mod);
	}
	mat o=ksm(M,k)*orig;
	for(int i=0;i<n;++i)printf("%d\n",o.a[i][0]);
	return 0;
}


#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int main(void){
    int n, m;
    cin >> n >> m;
    vector< int > a(m), b(m), c(m), d(m);
    vector< vector< int > > edg(n+1, vector< int >());
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        edg[a[i]].push_back(i);
        edg[b[i]].push_back(i);
    }

    vector< long long > dist(n+1, 1e18);
    using P = pair<long long, int>;
    priority_queue< P, vector<P>, greater<P> > q;
    dist[1] = 0;
    q.push(P(0, 1));
    while(!q.empty()){
        P p = q.top();
        q.pop();
        int v = p.second;
        if(dist[v] < p.first) continue;
        // cerr << v << " " << dist[v] << endl;
        for(int i: edg[v]){
            int to = b[i];
            if(to == v) to = a[i];
            long long cost = c[i] + (d[i] / (dist[v]+1));
            if(dist[v] + cost < dist[to]){
                dist[to] = dist[v] + cost;
                q.push(P(dist[to], to));
            }
            cost = c[i] + (d[i] / (dist[v]+2)) + 1;
            if(dist[v] + cost < dist[to]){
                dist[to] = dist[v] + cost;
                q.push(P(dist[to], to));
            }
            int rtd = sqrt(d[i]) - dist[v];
            for(int j = -10; j <= 10; j++){
                int w = rtd + j;
                if(w <= 0) continue;
                cost = c[i] + (d[i] / (dist[v]+1+w)) + w;
                if(dist[v] + cost < dist[to]){
                    dist[to] = dist[v] + cost;
                    q.push(P(dist[to], to));
                }
            }
        }
    }
    if(dist[n] == 1e18){
        cout << -1 << endl;
    }else{
        cout << dist[n] << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
vector<ll> topologycal_sort(vector<vector<ll>> L) {
    ll N = L.size();
    vector<ll> degree(N, 0), sorted_array;
    rep(i, N) for (auto v : L[i]) degree[v]++;

    queue<ll> que;
    rep(i, N) if (degree[i] == 0) que.emplace(i);

    while (!que.empty()) {
        ll u = que.front();
        que.pop();
        for (auto v : L[u])
            if (--degree[v] == 0) que.emplace(v);
        sorted_array.emplace_back(u);
    }
    // 各ノード番号をトポロジカル順にしたリスト
    return sorted_array;
}

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

    vvc<ll> G(N);
    rep(i, M){
        ll a, b;
        cin >> a >> b, a--, b--;
        G[a].eb(b);
    }

    vl T = topologycal_sort(G);

    ll ans = -INF;
    vl B(N, INF), S(N, -INF);
    rep(i, N){
        ll now = T[i];
        chmax(ans, A[now] - B[now]);
        repa(to, G[now]){
            chmin(B[to], A[now]);
            chmin(B[to], B[now]);
        }
    }
    cout << ans << endl;
}


#define    ll                long long
#define    dn                double
#define    mp                make_pair
#define    pb                push_back
#define    se                second
#define    fi                first
#define    mod               1000000007
#define    sob(v)            v.begin(),v.end()
#define    sobr(v)           v.rbegin(),v.rend()
#define    same(v)           v.erase(unique(v.begin(),v.end()),v.end())
#define    fr(i,a,b)         for(int i=a;i<=b;++i)
#define    frr(i,a,b)        for(int i=a;i>=b;--i)
#define    pi                acos(-1.00)
#define    inf               1e9
#define    EPS               1e-9
#define    sf(n)             scanf("%d",&n)
#define    sfl(n)            scanf("%lld",&n)
#define    check(n)          cout<<n<<"**CHECK"<<endl
#define    ffix(x)           cout<<fixed<<setprecision(x)
#define    fast              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define    ones(mask)        __builtin_popcount(mask)   // count set bit
#define    onesLL(mask)      __builtin_popcountll(mask) // for long long
#define    lzeros(mask)      __builtin_clz(mask)        // no of leading zeros
#define    tzeros(mask)      __builtin_ctz(mask)        // no of trailing zeros

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

typedef vector<int> vi;
typedef map<int,int> mii;
typedef pair<int,int> pii;
typedef pair<int,vi> pp;
typedef vector<pii> vpii;
typedef pair<pair<int,int>,int> pain;

int dr[8]= {-1,0,+1,0,-1,-1,+1,+1};
int dc[8]= {0,+1,0,-1,-1,+1,-1,+1};

int kx[8]= {-1,+1,+1,-1,+2,+2,-2,-2};
int ky[8]= {+2,+2,-2,-2,-1,+1,+1,-1};

const int N=100005;

int main()
{
    int n;
    cin>>n;
    ll a[n+1],b[n+1],pref[n+1];
    memset(pref,0,sizeof pref);
    memset(b,0,sizeof b);
    fr(i,1,n)
    {
        cin>>a[i];
        pref[i]=pref[i-1]+a[i];
    }
    fr(i,1,n)
    {
        b[i]=pref[i]+b[i-1];
    }
    ll mx=0;
    fr(i,1,n)
    {
        mx=max(mx,a[i]);
        ll ans=mx*i*1LL+b[i];
        cout<<ans<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string s[105];
ll a[25];
int f[25];
int n;
vector<int>v;
void make(int x)
{
    for(int i=n;i>1;i--)
    {
        f[i]=x%2;
        x=x>>1;
    }
}
int main()
{

     ios::sync_with_stdio(false);

     cin>>n;
     for(int i=1;i<=n;i++)
     {
         cin>>a[i];
     }
     ll pre=0;
     ll ans=1e18;
     int nm=pow(2,n-1);
     for(int cnt=0;cnt<=nm-1;cnt++)
     {
         make(cnt);
         f[1]=1;
         pre=0;
       //  cout<<cnt<<"cnt"<<endl;
         v.clear();
         for(int i=1;i<=n;i++)
         {
             if(f[i]==1)
             {
                 pre=pre|a[i];
             }
             else
             {
                 v.push_back(pre);
                 pre=a[i];
             }
         //    cout<<pre<<endl;
         }
         v.push_back(pre);
         ll x=v[0];
         for(int i=1;i<v.size();i++)
         {
             x=x^v[i];
         }
         ans=min(ans,x);
       //  cout<<x<<endl;

     }
     cout<<ans<<endl;

}
/*3
1 5 7
0cnt
1
5
7
3
1cnt
1
5
7
5
2cnt
1
5
7
7
3cnt
1
5
7
0
0
*/


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <tuple>
#include <set>
#include <map>
#include <cassert>

using namespace std;

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

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

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

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int m,n;
int num;
long long ans1,ans2;
string in;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		cin>>in;
		num=0;
		for(int j=0;j<m;j++)
			if(in[j]=='0')
				num++;
		if(num&1)
			ans1++;
		else
			ans2++;
	}
	printf("%lld",ans1*ans2);
	return 0;
}


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

void ans_add(string &t,vector<int>i){
  t+=to_string(i[0])+' '+to_string(i[1])+' '+to_string(i[2])+' '+to_string(i[3])+'\n';
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin>>n;
  vector<vector<int>>ad(n,vector<int>(4));
  for(int i=0;i<n;i++){
    cin>>ad[i][1]>>ad[i][2]>>ad[i][0];
    ad[i][3]=i;
  }
  sort(ad.begin(),ad.end(),[](vector<int> a,vector<int> b){
    return abs(a[0]-9999*50)<abs(b[0]-9999*50);
  });
  vector<vector<int>>ans(n,vector<int>(4));
  vector<bool>al(n);
  int base=(10000/n);
  for(int i=0;i<n;i++){
    if(al[(ad[i][1])/base]){
      ans[ad[i][3]]=(vector<int>){i,9998,i+1,9999};
    }else{
      if(ad[i][0]>=base*9999){

        ans[ad[i][3]]=(vector<int>){(ad[i][1])/base*base,0,min((ad[i][1])/base*base+base,9999),9998};

      }

      else{
        int b=min((ad[i][1])/base*base+base,9999)-(ad[i][1])/base*base;
        int u=max(0,ad[i][2]-ad[i][0]/base+1);
        int d=min(9998,ad[i][0]/b+u);
        if(abs((d-u)*b-ad[i][0])>abs((d-u+1)*b-ad[i][0])){
          d=min(9998,d+1);
        }
        ans[ad[i][3]]=(vector<int>){(ad[i][1])/base*base,u,min((ad[i][1])/base*base+base,9999),d};
      }

      al[(ad[i][1])/base]=true;
    }
  }
  string ret="";
  for(auto i:ans){
    ans_add(ret,i);
  }
  cout<<ret;
}

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();}

#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define random_shuffle(...) shuffle(__VA_ARGS__, rng)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 2e5+6969, INF = 2e9+6969;

int n,point[2][205],rec[205],ans[4][205];
bool br[10005][10005];
priority_queue<pair<ld,int>> q;

ld add(ll s, ll r, ll x)
{
	ld tmp = (ld)((r-s)*2 - x) / r;
	return tmp * x / r;
}

int random(int a, int b)
{
	return uniform_int_distribution<int>(a,b)(rng);
}

int main()
{
	scanf("%d", &n);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d%d", &point[0][i], &point[1][i], &rec[i]);
		if(br[point[0][i]][point[1][i]] == 0)
		{
			ans[0][i] = point[0][i];
			ans[1][i] = point[1][i];
			ans[2][i] = point[0][i]+1;
			ans[3][i] = point[1][i]+1;
			br[point[0][i]][point[1][i]] = 1;
			q.push(mp(add(1, rec[i], 1), i));
		}
	}
	int REPS = 1000'000;
	for(int i=0;;i++)
	{
		if(q.empty()) break;
		vi changes;
		for(int j=0;j<15;j++)
		{
			if(q.empty()) break;
			changes.pb(q.top().se);
			q.pop();
		}
		random_shuffle(all(changes));
		for(auto who: changes)
		{
		int X = ans[2][who] - ans[0][who];
		int Y = ans[3][who] - ans[1][who];
		bool lf,rg,up,dw;
		lf = rg = up = dw = 0;
		{
		if(ans[1][who] > 0)
		{
			up = 1;
			for(int i=ans[0][who];i<ans[2][who];i++)
			{
				if(br[i][ans[1][who]-1])
				{
					up = 0;
					break;
				}
			}
		}
		if(ans[3][who] < 10000)
		{
			dw = 1;
			for(int i=ans[0][who];i<ans[2][who];i++)
			{
				if(br[i][ans[3][who]])
				{
					dw = 0;
					break;
				}
			}
		}
		if(ans[0][who] > 0)
		{
			lf = 1;
			for(int i=ans[1][who];i<ans[3][who];i++)
			{
				if(br[ans[0][who]-1][i])
				{
					lf = 0;
					break;
				}
			}
		}
		if(ans[2][who] < 10000)
		{
			rg = 1;
			for(int i=ans[1][who];i<ans[3][who];i++)
			{
				if(br[ans[2][who]][i])
				{
					rg = 0;
					break;
				}
			}
		}
	}
		if(X*(Y+1) > rec[who]) up = dw = 0;
		if((X+1)*Y > rec[who]) lf = rg = 0;
		if((((lf|rg)|up)|dw) == 0) continue;
		vi options;
		if(up) options.pb(0);
		if(dw) options.pb(1);
		if(lf) options.pb(2);
		if(rg) options.pb(3);
		int choice = options[random(0,options.size()-1)];
				if(choice == 0)
				{
					ans[1][who]--;
					for(int i=ans[0][who];i<ans[2][who];i++) br[i][ans[1][who]] = 1;
					int R = X*(Y+1);
					if(R < rec[who])
					{
						q.push(mp(add(R, rec[who], (X+1+Y)/2), who));
					}
				}
				if(choice == 1)
				{
					for(int i=ans[0][who];i<ans[2][who];i++) br[i][ans[3][who]] = 1;
					ans[3][who]++;
					int R = X*(Y+1);
					if(R < rec[who])
					{
						q.push(mp(add(R, rec[who], (X+1+Y)/2), who));
					}
				}
			if(choice == 2)
			{
				ans[0][who]--;
				for(int i=ans[1][who];i<ans[3][who];i++) br[ans[0][who]][i] = 1;
				int R = (X+1)*Y;
				if(R < rec[who])
				{
					q.push(mp(add(R, rec[who], (X+1+Y)/2), who));
				}
			}
			if(choice == 3)
			{
				for(int i=ans[1][who];i<ans[3][who];i++) br[ans[2][who]][i] = 1;
				ans[2][who]++;
				int R = (X+1)*Y;
				if(R < rec[who])
				{
					q.push(mp(add(R, rec[who], (X+Y+1)/2), who));
				}
			}
		}
	}
	PII last = mp(0,0);
	for(int i=0;i<n;i++)
	{
		if(ans[2][i] == ans[3][i] && ans[2][i] == 0)
		{
			for(int x = last.fi; x <= 10000; x++)
			{
				for(int y = 0; y <= 10000; y++)
				{
					if(br[x][y] == 0)
					{
						ans[0][i] = x;
						ans[1][i] = y;
						ans[2][i] = x+1;
						ans[3][i] = y+1;
						last = mp(x,y);
					}
				}
			}
		}
		for(int j=0;j<4;j++) cout << ans[j][i] << " " ;
		puts("");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a,b;
int main(){
	scanf("%d%d",&a,&b);
	printf("%d",a==b?a:3-a-b);
	return 0;
}

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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rer(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);(i)++)
using ll = long long;
using P = pair<int,int>;
const int INF = 1001001001;

int main(){
    int A,B,C;
    cin >> A >> B;
    if(A==B) cout << A << endl;
    else{
        if(A+B==1) cout << 2 << endl;
        if(A+B==2) cout << 1 << endl;
        if(A+B==3) cout << 0 << endl;
    }
}


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

// Taken from: https://github.com/bqi343/USACO/blob/master/Implementations/content/contest/templateShort.cpp

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ull> vul;
typedef vector<ll> vl;
typedef vector<pi> vpi;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i,0,a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i,0,a)
#define trav(a, x) for (auto& a: x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize

#define mp make_pair
#define pb push_back
#define f first
#define s second

const int MOD = 1e9 + 7; // 998244353; // = (119<<23)+1
const int MX = 2e5 + 5;

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

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

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

//
// Created by aryssoncf on 28/10/2019.
//

#include <type_traits>

#include <iterator>
#include <utility>

template<typename T>
constexpr auto hasBegin(int) -> decltype(std::begin(std::declval<T>()), true) {
    return true;
}

constexpr bool hasBegin(...) {
    return false;
}

template<typename T>
using IsContainer = std::integral_constant<bool, hasBegin<T>(0)>;

#ifndef JHELPER_EXAMPLE_PROJECT_IO_HPP
#define JHELPER_EXAMPLE_PROJECT_IO_HPP

#endif //JHELPER_EXAMPLE_PROJECT_IO_HPP

template<typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
    for (T &element: vec) {
        is >> element;
    }
    return is;
}

template<typename T, typename U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    is >> p.first;
    is >> p.second;
    return is;
}

template<class X, class Y>
std::ostream &operator<<(std::ostream &os, std::pair<X, Y> const &p) {
    return os << p.first << " " << p.second;
}

template<class Ch, class Tr, class Container>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, Container const &x) {
    bool first = true;
    for (auto &y : x) {
        if (first) {
            first = false;
        } else {
            os << " ";
        }
        os << y;
    }
    return os;
}

using namespace std;

/*
PROG:
LANG: C++
*/

class BAchieveTheGoal {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n, k, m;
        in >> n >> k >> m;
        int tot = n * m;
        vi A(n - 1);
        in >> A;
        int sum = accumulate(all(A), 0), res = max(tot - sum, 0);
        if (res > k) {
            res = -1;
        }
        out << res << "\n";
    }

    void setup() {}
};

int main() {
    std::ios_base::sync_with_stdio(false);
    BAchieveTheGoal solver;
    solver.setup();
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    in.tie(nullptr);
    out << std::fixed;
    out.precision(20);
    solver.solve(in, out);
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int main(void)
{
    int T,t,x,y,n,m,k,s=0;
    int a[2000]={0};
    scanf("%d %d %d",&n,&k,&m);
    for(int i=0;i<n-1;i++)
    {
        scanf("%d",&a[i]);
        s+=a[i];
    }
    if(m*n-s<=k)
    {
        for(int i=0;i<=k;i++)
        {
            if(s+i>=m*n)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
    else
        printf("%d\n",-1);
    return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cinttypes>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;

#define PI 3.1415926535897932384626433832795

template<typename X>
bool max_u(X&m, X v)
{
	if(m<v)
	{
		m=v;
		return true;
	}
	return false;
}

template<typename X>
bool min_u(X&m, X v)
{
	if(m>v)
	{
		m=v;
		return true;
	}
	return false;
}

bool next1(string&s, int&i)
{
	int l=s.length();
	if(i>=l) return false;

	for(i++;i<l;i++)
	{
		if(s[i]=='1')
		{
			return true;
		}
	}
	return false;
}

struct solve
{
	solve(){}
	ll operator()(int n, string s, string t) {
		ll ans=0;
		int si=-1, ti=-1;

		for(int k=0;;k++)
		{
			if(si>=n && ti>=n)
			{
				return ans;
			}

//cout << si << " " << ti << "\n";
			if(si<ti)
			{
				int psi=si;
				if(!next1(s, si)) break;
				ans+=si-psi;
				next1(s, si);
			}
			else
			{
				if(si>=n) break;
				ans+=si-ti;
				next1(s, si);
				next1(t, ti);
			}
		}
		return -1;
	}
};

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

	for(;;)
	{
		int n;
		cin >> n;
		if(cin.fail()) break;
		string  s, t;
		cin >> s >> t;

		cout << solve()(n, s, t) << "\n";
	}
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int main(void){
    int n;
    long long ans = 0;
    string s, t;
    cin >> n >> s >> t;

    set< int > st;
    for(int i=0;i<n;i++) if(s[i] == '1') st.insert(i);

    for(int i=0;i<n;i++){
        if(s[i] == t[i]) continue;

        if(st.empty()){
            puts("-1");
            return 0;
        }

        auto itr = st.begin();
        int j = *itr;
        st.erase(itr);
        while(j <= i){
            if(st.empty()){
                puts("-1");
                return 0;
            }
            itr = st.begin();
            j = *itr;
            st.erase(itr);
        }
        s[j] = '0';
        ans += j-i;
    }
    cout << ans << endl;
}


#pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
template<class T>using uset = unordered_set<T>;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//using T = tuple<ll, ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion

#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}

#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}

void O() {cout << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);}
#pragma endregion

void Main();

int main(){
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

vl fibb(const vl& seed, ll N){
    vl fib(seed.size());
    copy(ALL(seed), fib.begin());
    for(ll i = seed.size();;i++){
        ll t = fib[i-1]+fib[i-2];
        if(t > N) break;
        fib.push_back(t);
    }
    return fib;
}

void Main(){
    I(ll, N);
    vl seed({0, 1});
    vl fib = fibb(seed, N);
    ll cnt = 1 + fib.size() - 2;
    ll i = 1;
    ll siz = fib.size();
    if(fib[fib.size()-1] == N){
        goto last;
    }
    while(1){
        seed[i]++;cnt++;
        fib = fibb(seed, N);
        if(fib[fib.size()-1] == N){
            goto last;
        }
        if(siz > fib.size()){
            seed[i]--;
            seed.push_back(seed[i-1] + seed[i]);
            i++;
        }
    }
last:
    //cout << fib << el;
    bool x = fib.size()&1;
    vl ans;
        // 逆かも
    REP(i, fib[1]){
        ans.push_back(x+1);
    }
    x = !x;
    ans.push_back(x+3);
    FOR(i, fib.size(), 2){
        REP(j, fib[i]-fib[i-1]-fib[i-2]){
            ans.push_back(x+1);
        }
        x = !x;
        ans.push_back(x+3);
    }
    ll s = min(130, (int)ans.size());
    cout << s << el;
    REP(i, s){
        cout << ans[i] << el;
    }
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>

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

using namespace std;

class CCalculator {
public:
    map<int, int> memo;

    int fib(int n) {
        if (n == 1 || n == 2) return 1;
        if (memo.count(n)) return memo[n];
        return memo[n] = fib(n - 1) + fib(n - 2);
    }

    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        set<int> s;
        REV(i, 90, 0) {
            int val = fib(i + 1);
            if (N >= val) {
                N -= val;
                s.insert(i + 1);
            }
        }

        int num = *s.rbegin();
        if (num % 2 == 0) num++;

        vector<int> res;
        FOR(i, 1, num) {
            if (i % 2 == 1) {
                res.push_back(3);
            } else {
                res.push_back(4);
            }
            if (s.count(num - i + 1)) {
                if (i % 2 == 1) {
                    res.push_back(1);
                } else {
                    res.push_back(2);
                }
            }
        }
        cout << res.size() << endl;
        REP(i, res.size()) cout << res[i] << endl;
    }
};

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

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


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
int a[N], b[N];
ll cnt;
int n;
void fun(int x, int y) {
	if (x == y)return;
	int mid = (x + y) / 2;
	fun(x, mid), fun(mid + 1, y);
	int p1 = x, p2 = mid + 1, p3 = x;
	while (p3 <= y) {
		if (p1 > mid) {
			b[p3++] = a[p2++];
		}
		else if (p2 > y) {
			b[p3++] = a[p1++];
		}
		else {
			if (a[p1] > a[p2]) {
				cnt += ll(y - p2 + 1);
				b[p3++] = a[p1++];
			}
			else {
				b[p3++] = a[p2++];
			}
		}
	}
	for (int i = x; i <= y; ++i)a[i] = b[i];
}
ll c[N];
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)c[i] = a[i];
	fun(0, n - 1);
	for (ll i = 0; i < n; ++i) {
		printf("%lld\n", cnt);
		cnt += ((ll)n - c[i] - 1ll) - c[i];
	}
	return 0;
}

/** @kutbilimone **/
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define rall(x) (x).rbegin(),(x).rend()
#define all(x) (x).begin(),(x).end()
#define arring(a, n) for(int i = 0; i < n; cin >> a[i++])
#define int long long

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int> a(1+n), sum(1+n), sqr(1+n);
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum[i] = a[i] + sum[i-1];
        sqr[i] = a[i]*a[i] + sqr[i-1];
    }

    int res = 0;
    for(int i = 2; i <= n; i++){
        int n = i-1, x = a[i];
        res += n*x*x - 2*x*sum[n] + sqr[n];
    }

    cout << res;

    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define per(i,a,b) for(ll i=(a);i>=(b);i--)
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define next Cry_For_theMoon
#define il inline
#define pb(x) push_back(x)
#define mapit map<int,int>::iterator
#define vit vector<int>::iterator
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double db;
using namespace std;
const int MAXN=1e5+10;
int n,size[MAXN],ans;
int color[MAXN];
vector<int>v[MAXN];
int main(){
	cin>>n;
	rep(i,1,n){
		sort(v[i].begin(),v[i].end());
		color[i]=1;int pre=-1;
		for(vit it=v[i].begin();it!=v[i].end();it++){
			if(*it==pre)continue;
			pre=*it;
			if(color[i]==*it)color[i]++;
			else break;
		}
		rep(j,2,n){
			if(j*i>n)break;
			v[j*i].pb(color[i]);
		}
	}
	rep(i,1,n)printf("%d ",color[i]);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<ll, ll> pt;

#define MOD 1000000007

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

	int N;
	cin >> N;

	vector<int> ans(N, 2);
	ans[0] = 1;

	for(int i = 2; i <= N; i++) {
		for(int j = 2; i * j <= N; j++) {
			ans[i * j - 1] = ans[i - 1] + 1;
		}
	}

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


/**
*    author:  shu8Cream
*    created: 26.04.2021 09:06:13
**/

#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using namespace std::chrono;
inline double get_time_sec(void){
    return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count())/1000000000;
}

int si,sj;
vvi t(50,vi(50));
vvi p(50,vi(50));

string finans = "";
int finscore = 0;

const vi di = {1,0,-1,0};
const vi dj = {0,1,0,-1};

void input(){
    cin >> si >> sj;
    rep(i,50)rep(j,50) cin >> t[i][j];
    rep(i,50)rep(j,50) cin >> p[i][j];
}

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

    input();

    set<int> allrem;
    int loop = 16;
    while(loop--){
        double start = get_time_sec();
        int score = 0;
        string ans = "";
        vector<P> remcell;

        while(get_time_sec() - start<0.124){

            int tscore = 0;
            string tans = "";
            set<int> rem;
            queue<P> pq;
            pq.push({si,sj});
            int i=0,j=0;
            while(!pq.empty()){
                int pi=i,pj=j;
                tie(i,j) = pq.front(); pq.pop();
                remcell.push_back({i,j});
                rem.insert(t[i][j]);
                tscore += p[i][j];
                queue<P> emp;
                swap(pq,emp);

                if(!(i == si && j == sj)){
                    if(i-pi==1) tans+="D";
                    else if(i-pi==-1) tans+="U";
                    else if(j-pj==1) tans+="R";
                    else if(j-pj==-1) tans+="L";
                }

                // cout << ans << endl;

                vi rand = {0,1,2,3};

                std::random_device seed_gen;
                std::mt19937 engine(seed_gen());
                shuffle(all(rand),engine);
                for(auto k : rand){
                    int ni = i + di[k];
                    int nj = j + dj[k];
                    if(ni<0 || ni>=50 || nj<0 || nj>=50) continue;
                    if(t[i][j]==t[ni][nj]) continue;
                    if(rem.count(t[ni][nj])) continue;
                    if(allrem.count(t[ni][nj])) continue;

                    int ngcnt = 0;
                    rep(kk,4){
                        int nni = ni + di[kk];
                        int nnj = nj + dj[kk];
                        if(nni<0 || nni>=50 || nnj<0 || nnj>=50) {ngcnt++; continue;}
                        if(t[nni][nnj]==t[ni][nj]) {ngcnt++; continue;}
                        if(rem.count(t[nni][nnj])) {ngcnt++; continue;}
                        if(allrem.count(t[nni][nnj])) {ngcnt++; continue;}
                    }
                    if(ngcnt==4) continue;

                    pq.push({ni,nj});
                }
            }

            if(tscore>score){
                score = tscore;
                ans = tans;
            }

        }

        int anslen = (ans.size()+1)/2;
        ans = ans.substr(0,anslen);

        finans += ans;

        allrem.insert(t[si][sj]);
        rep(k,anslen){
            if(ans[k]=='U') si--;
            if(ans[k]=='D') si++;
            if(ans[k]=='L') sj--;
            if(ans[k]=='R') sj++;
            allrem.insert(t[si][sj]);
        }
    }
    cout << finans << endl;
}

#include <bits/stdc++.h>

using namespace std;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

// types

int debug;

struct Input {
  int si, sj, num;
  vector<vector<int>> t, p;
  vector<int> sum_score;
  Input(istream &is) : t(50, vector<int>(50)), p(50, vector<int>(50)) {
    is >> si >> sj;
    for (int i = 0; i < 50; ++i)
      for (int j = 0; j < 50; ++j)
        is >> t[i][j];
    for (int i = 0; i < 50; ++i)
      for (int j = 0; j < 50; ++j)
        is >> p[i][j];

    num = 0;
    for (int i = 0; i < 50; ++i)
      for (int j = 0; j < 50; ++j)
        chmax(num, t[i][j] + 1);
    sum_score.resize(num);
    for (int i = 0; i < 50; ++i)
      for (int j = 0; j < 50; ++j)
        sum_score[t[i][j]] += p[i][j];
  }
};

struct Eval {
  int sx, sy;
  int self, other;
};

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const char dir_char[8] = "RDLU";

using Output = pair<string,int>;

Output sub_solve(const Input &input, Eval eval) {
  vector<int> visited(2500);
  int y = input.si, x = input.sj;
  const auto &t = input.t;
  const auto &p = input.p;
  const vector<int> &sum_score = input.sum_score;
  int max_score = 0;
  string max_res = "";

  vector<vector<pair<int,int>>> history;
  vector<int> dir_history;
  int turn = 0;
  int score = p[y][x];
  int max_turn = 0;

  string res = "";
  for (;;) {
    // cout << turn << " " << y << " " << x << endl;
    // if (turn > 10) break;

    int number = t[y][x];
    visited[number] = true;

    if (int(history.size()) <= turn) {
      vector<pair<int,int>> vec;
      for (int dir = 0; dir < 4; ++dir) {
        const int ny = y + dy[dir];
        const int nx = x + dx[dir];
        if (ny < 0 || nx < 0 || ny >= 50 || nx >= 50) continue;
        if (visited[t[ny][nx]]) continue;
        // int score = (nx + ny) * 10 - (sum_score[t[ny][nx]] - p[ny][nx]);
        int score = 0;
        score += nx * eval.sx;
        score += ny + eval.sy;
        score += p[ny][nx] * eval.self;
        score -= (sum_score[t[ny][nx]] - p[ny][nx]) * eval.self;

        vec.emplace_back(score, dir);
      }
      sort(vec.begin(), vec.end());
      history.push_back(vec);
    }
    else if (int(history.size()) > turn + 1) {
      history.pop_back();
    }

    auto &vec = history[turn];
    if (vec.empty()) {
      if (turn < max_turn - 30) break;
      --turn;
      history.pop_back();
      history.back().pop_back();
      const int dir = dir_history.back();
      if (score > max_score) {
        max_score = score;
        max_res = res;
      }
      score -= p[y][x];
      x -= dx[dir];
      y -= dy[dir];
      dir_history.pop_back();
      visited[number] = false;
      res.pop_back();
      continue;
    }

    ++turn;
    chmax(max_turn, turn);
    const int dir = vec.back().second;
    res += dir_char[dir];
    x += dx[dir];
    y += dy[dir];
    score += p[y][x];
    dir_history.push_back(dir);
  }
  return {max_res, max_score};
}

Output solve(const Input &input) {
  int max_score = 0;
  string max_res = "";
  mt19937 mt;
  for (int loop = 0; loop < 100; ++loop) {
    Eval eval;
    eval.sx = mt() % 1000;
    eval.sy = mt() % 1000;
    eval.self = mt() % 1000;
    eval.other = mt() % 1000;
    Output out = sub_solve(input, eval);
    if (out.second > max_score) {
      max_score = out.second;
      max_res = out.first;
    }
  }
  return {max_res, max_score};
}

int main(int argc, char**) {
  debug = (argc > 1);
  if (debug) {
    char filename[256];
    int sum = 0;
    for (int i = 0; i < 100; ++i) {
      sprintf(filename, "in/%.4d.txt", i);
      ifstream ifs(filename);
      Input input(ifs);
      int r = solve(input).second;
      cout << r << endl;
      sum += r;
    }
    cout << sum << endl;
  }
  else {
    const Input input(cin);
    auto res = solve(input);
    cout << res.first << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
char str[4][30],q[20];
int cnt;
int mymap[30];
bool st[30];
bool jdg[20];
long long temp[10];
bool dfs(int s){
    if(s>cnt){
        for(int i=1;i<=3;i++){
            if(mymap[str[i][1]-'a']==0) return false;
        }
        temp[1]=temp[2]=temp[3]=0;
        for(int i=1;i<=3;i++){
            for(int j=1;str[i][j];j++){
                temp[i]=temp[i]*10+mymap[str[i][j]-'a'];
            }
        }
        return temp[1]+temp[2]==temp[3];
    }
    for(int i=0;i<=9;i++){
        if(!jdg[i]){
            mymap[q[s]-'a']=i;
            jdg[i]=true;
            if(dfs(s+1)) return true;
            jdg[i]=false;
        }
    }
    return false;
}
int main(){
    for(int i=1;i<=3;i++) scanf("%s",str[i]+1);
    for(int i=1;i<=3;i++){
        for(int j=1;str[i][j];j++){
            if(!st[str[i][j]-'a']){
                st[str[i][j]-'a']=true;
                q[++cnt]=str[i][j];
            }
        }
    }
    if(cnt>10) puts("UNSOLVABLE");
    else{
        if(dfs(1)){
            for(int i=1;i<=3;i++) cout<<temp[i]<<endl;
        }
        else puts("UNSOLVABLE");
    }
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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 T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

int arr[] = {0,1,2,3,4,5,6,7,8,9};
map<char,int> mp;
LL getInt(string s){
    reverse(s.begin(), s.end());
    LL res = 0;
    LL base = 1;
    for(char c : s){
        res += base * arr[mp[c]];
        base *= 10;
    }

    return res;

}

bool check(string s){
    int idx = mp[s[0]];
    int x = arr[idx];
    if(x == 0) return false;
    else return true;
}

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);
    string A,B,C; cin >> A >> B >> C;
    set<char> se;
    for(char c : A) se.insert(c);
    for(char c : B) se.insert(c);
    for(char c : C) se.insert(c);
    int len = 0;
    for(char c : se) len++;
    if(len > 10){
        cout << "UNSOLVABLE" << endl;
        return 0;
    }
    int idx = 0;
    for(char c : se){
        mp[c] = idx;
        idx++;
    }

    do{
        if(!check(A) || !check(B) || !check(C)) continue;
        LL Ax = getInt(A);
        LL Bx = getInt(B);
        LL Cx = getInt(C);

        if(Ax + Bx == Cx){
            cout << Ax << endl;
            cout << Bx << endl;
            cout << Cx << endl;
            return 0;
        }

    }while(next_permutation(arr, arr+10));

    cout << "UNSOLVABLE" << endl;
}