// arc120_c
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#include "../../debug_util/timer.hpp"
#endif
using namespace std;

using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

#define REP(i, n) for (int i = 0 ; i < (int)(n) ; ++i)
#define REPN(i, m, n) for (int i = m ; i < (int)(n) ; ++i)
#define REP_REV(i, n) for (int i = (int)(n) - 1 ; i >= 0 ; --i)
#define REPN_REV(i, m, n) for (int i = (int)(n) - 1 ; i >= m ; --i)
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e15)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

// 区間add, 区間sum
template<typename T>
struct LazySegmentTree{
    int n;
    vector<T> val;
    vector<T> lazy;
    vector<int> width;

    explicit LazySegmentTree(int n_){
        n = 1;
        while (n < n_) n <<= 1;

        val = vector<T>(n<<1, 0);
        lazy = vector<T>(n<<1, 0);
        width = vector<int>(n<<1, 1);
        for (int i = n-2; i >= 0; i--) {
            width[i] = width[i*2+1] + width[i*2+2];
        }
    }
    void lazy_propagate(int k) {
        val[k] += lazy[k] * width[k];
        if (k < n-1) {
            lazy[k*2+1] += lazy[k];
            lazy[k*2+2] += lazy[k];
        }
        lazy[k] = 0;
    }
    void add(int a, int b, int k, int l, int r, int x) {
        lazy_propagate(k);

        if (b <= l || r <= a) {
            return;
        }
        if (a <= l && r <= b) {
            lazy[k] += x;
            lazy_propagate(k);
            return;
        }
        int mid = (l+r)/2;
        add(a, b, k*2+1, l, mid, x);
        add(a, b, k*2+2, mid, r, x);
        val[k] = val[k*2+1] + val[k*2+2];
    }

    void add(int a, int b, int x) {
        add(a, b, 0, 0, n, x);
    }

    T get_sum(int a, int b, int k, int l, int r) {
        lazy_propagate(k);

        if (b <= l || r <= a) {
            return 0;
        }
        if (a <= l && r <= b) {
            return val[k];
        }
        int mid = (l+r)/2;
        T sl = get_sum(a, b, k*2+1, l, mid);
        T sr = get_sum(a, b, k*2+2, mid, r);
        return sl + sr;
    }

    T get_sum(int a, int b) {
        return get_sum(a, b, 0, 0, n);
    }
};

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    int N;
    cin >> N;
    vector<int> A(N), B(N);
    REP(i, N) cin >> A[i];
    REP(i, N) cin >> B[i];

    map<int, vector<int>> mp;
    REP_REV(i, N){
        mp[A[i] + i].emplace_back(i);
    }

    LazySegmentTree<ll> st(N);
    REP(i, N){
        st.add(i, i+1, i);
    }

    ll ans = 0;
    REP(i, N){
        int x = B[i] + i;
        if (mp[x].empty()){
            print(-1);
            return 0;
        }

        int idx = mp[x].back();
        ans += st.get_sum(idx, idx+1);

        mp[x].pop_back();
        st.add(idx+1, N, -1);

//        print(i, idx);
    }

    print(ans);

    return 0;
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//#include <atcoder/convolution>
//#include <atcoder/modint>
//using Modint = atcoder::modint998244353;

using namespace std;
typedef long long ll;

void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    ex_gcd(b, a % b, d, y, x);
    y -= a / b * x;
  }
}
ll inv(ll a, ll n) {
  ll d, x, y;
  ex_gcd(a, n, d, x, y);
  return d == 1 ? (x % n + n) % (n / d) : -1;
}
ll gcd(ll x, ll y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}

const int maxn = 200005;
const int mod = 998244353;

int idx[maxn];

struct BIT {
  int c[maxn];
  void add(int pos){
    while(pos<maxn){
      c[pos]++;
      pos+=(pos&-pos);
    }
  }
  int query(int pos){
    int ret = 0;
    while(pos>0){
      ret += c[pos];
      pos-=(pos&-pos);
    }
    return ret;
  }
}bit;

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n;
  cin>>n;
  unordered_map<int,vector<int>>mpa, mpb;
  for(int i=0;i<n;i++){
    int x;
    cin>>x;
    mpa[x+i].push_back(i);
  }
  for(int i=0;i<n;i++){
    int x;
    cin>>x;
    mpb[x+i].push_back(i);
  }
  ll ret= 0;
  for(auto&pr: mpa){
    if(mpb.find(pr.first)==mpb.end()||mpb[pr.first].size()!=pr.second.size()){
      cout<<-1<<endl;
      return 0;
    }
    auto&a = pr.second;
    auto&b = mpb[pr.first];
    for(int i=0;i<a.size();i++){
      idx[b[i]] = a[i];
    }
  }
  for(int i=0;i<n;i++){
    int tt = idx[i];
    int tmp = bit.query(maxn-1)-bit.query(tt+1);
    int tmp2 = tt + tmp;
    ret += abs(tmp2-i);
    bit.add(tt+1);
  }
  cout<<ret<<endl;
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

namespace WHH
{
	typedef long long ll;

	int N;
	ll ans;

	inline ll gcd(ll a,ll b)
	{return ((b==0)?a:gcd(b,a%b));}

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

	int work()
	{
		scanf("%d",&N);
		ans = 1;
		for(int i=2;i<=N;i++) ans = lcm(ans,i);
		++ans;
		printf("%lld",ans);
		return 0;
	}
}

int main() {return WHH::work();}

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<bitset>
#include<stack>
#define fio cin.tie(0);ios_base::sync_with_stdio(false)
#define ll long long
using namespace std;
int main(){
    ll int k;
    cin>>k;
    ll int a = 1;
    for(ll int i=2;i<=k;i++){
        a=a*i/__gcd(a,i);
    }
    cout<<a+1<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
typedef long long LL;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 4e5 + 5;

int n, m, a[N], b[N];
LL ans = 4e17, suf[N], pre[N];

int main() {
	cin>>n>>m;
	rep (i, 1, n) cin>>a[i];
	sort(a + 1, a + n + 1);
	rep (i, 1, m) cin>>b[i];
	sort(b + 1, b + m + 1);
	per (i, n, 1) suf[i] = suf[i + 1] + a[i]*(i&1 ? 1 : -1);
	rep (i, 1, n) pre[i] = pre[i - 1] + a[i]*(i&1 ? -1 : 1);
	rep (i, 1, n) {
		int pos = lower_bound(b + 1, b + m + 1, a[i]) - b;
		if (pos != m + 1) ans = min(ans, pre[i - 1] + suf[i + 1] + b[pos] - a[i]);
		if (pos != 1) ans = min(ans, pre[i - 1] + suf[i + 1] + a[i] - b[pos - 1]);
	}
	printf("%lld\n", ans);
}

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10,mod=998244353;
typedef long long LL;
LL qpow(LL x,LL k){
	LL res=1;
	while(k){
		if(k&1)res=res*x%mod;
		x=x*x%mod;
		k>>=1;
	}
	return res;
}
int main(){
	long long n,m,k;
	scanf("%lld%lld%lld",&n,&m,&k);
	if(n==1){
		printf("%lld",qpow(k,m));
	}
	else if(m==1){
		printf("%lld",qpow(k,n));
	}
	else{
		LL res=0;
		for(int i=1;i<=k;i++){//最小值的最大值是i时
			res=(res+((qpow(i,n)-qpow(i-1,n)+mod)*qpow(k-i+1,m)%mod)+mod)%mod;
		}
		cout<<res;
	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
//#pragma GCC optimize("O3")
using namespace std;
using namespace __gnu_pbds;
auto random_address = [] { char *p = new char; delete p; return uint64_t(p); };
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n;
string s;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    cin>>s;
    s=" "+s;
    if(s[1]!=s[n])
    {
        cout<<1<<'\n';
        return 0;
    }
    int nrlit=0;
    for(int i=1;i<n;i++)
        if(s[i]!=s[1]&&s[i+1]!=s[1])
        {
            cout<<2;
            return 0;
        }
    cout<<-1;

    return 0;
}

/*
========= Plozia =========
	Author:Plozia
	Problem:T2
	Date:2021/3/21
========= Plozia =========
*/

#include <bits/stdc++.h>

typedef long long LL;
const int MAXN = 500 + 10;
const LL P = 998244353;
int n, m, a[MAXN][MAXN];
LL sum;

int read()
{
	int sum = 0, fh = 1; char ch = getchar();
	for (; ch < '0' || ch > '9'; ch = getchar()) fh -= (ch == '-') << 1;
	for (; ch >= '0' && ch <= '9'; ch = getchar()) sum = (sum << 3) + (sum << 1) + (ch ^ 48);
	return (fh == 1) ? sum : -sum;
}

LL ksm(LL a, LL b, LL p)
{
	LL ans = 1 % p;
	for (; b; b >>= 1, a = a * a % p)
		if (b & 1) ans = ans * a % p;
	return ans;
}

int main()
{
	n = read(), m = read();
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
		{
			char ch = getchar();
			while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
			if (ch == 'R') a[i][j] = 1;
			if (ch == 'B') a[i][j] = 2;
		}
	for (int i = 1; i <= n; ++i)
	{
		int Tag = a[i][1];
		int x = i, y = 1;
		bool flag = 0;
		for (;;)
		{
			--x; ++y;
			if (x < 1 || y > m) break ;
			if (Tag == 0 && a[x][y] != 0) {	Tag = a[x][y]; }
			else if (a[x][y] != 0)
			{
				flag = 1;
				if (a[x][y] != Tag) { printf("0\n"); return 0; }
			}
		}
		if (Tag == 0 && flag == 0) ++sum;
	}
	for (int i = 2; i <= m; ++i)
	{
		int Tag = a[n][i];
		int x = n, y = i;
		bool flag = 0;
		for (;;)
		{
			--x; ++y;
			if (x < 1 || y > m) break ;
			if (Tag == 0 && a[x][y] != 0) {	Tag = a[x][y]; }
			else if (a[x][y] != 0)
			{
				flag = 1;
				if (a[x][y] != Tag) { printf("0\n"); return 0; }
			}
		}
		if (Tag == 0 && flag == 0) ++sum;
	}
	printf("%lld\n", ksm(2, sum, P)); return 0;
}


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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define all(a) (a).begin(),(a).end()
#define ODD(x) ( ((x)&1)==0?0:1 )
#define SIGN(x) ( ((x) > 0) - ((x) < 0)  )
#define dbg(x) cerr << #x"= " << x << endl

std::mt19937_64  generator(std::chrono::system_clock::now().time_since_epoch().count());

inline ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a, ll b) { return a ? gcd(b%a, a): b; }
ll lcm(ll a, ll b) { return a / gcd(a,b) * b; }

void READ(bool _local){
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
    if (_local)
        freopen ("in.txt", "r", stdin);
#endif
}

int main() {
    READ(0);
    int n;cin>>n;
    vi a(n);
    FOR(i,n) cin>>a[i];
    sort(all(a));
    ll ret=a[0]+1;
    for(int i=1;i<n;++i){
        ret = ret * (a[i]-a[i-1]+1) % MOD;
    }
    cout << ret;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back

using ll = long long;
using db = double;
using pii = pair<int, int>;
using vi = vector<int>;
mt19937 mrand(time(0));
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }

int n, x[20], y[20], z[20], f[1 << 17][17], ans = 1e9;
int dis(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]); }
signed main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
    memset(f, 0x3f, sizeof(f)), f[1][0] = 0;
    for(int S = 2; S < 1 << n; S++)
        for(int i = 0; i < n; i++) if(S >> i & 1)
            for(int j = 0; j < n; j++) if((S >> j & 1) && j != i)
                f[S][i] = min(f[S][i], f[S ^ (1 << i)][j] + dis(j, i));
    for(int i = 0; i < n; i++) ans = min(ans, f[(1 << n) - 1][i] + dis(i, 0));
    printf("%d\n", ans);
    fprintf(stderr, "time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rrep(i, n) for (int i = 1; i < (int)(n+1); ++i)
const long long INF = 1LL<<60;
int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    cin>>N;
    ll C[N][N];
    ll A[N];
    ll B[N];
    rep(i,N){
        A[i]=INF;
        B[i]=INF;
    }
    rep(i,N){
        rep(j,N){
        cin>>C[i][j];
            A[i]=min(A[i],C[i][j]);
            B[j]=min(B[j],C[i][j]);
        }
    }
    bool an=true;
    rep(i,N){
        rep(j,N){
            if(A[i]+B[j]<C[i][j]){
                an=false;
            }
        }
    }
    rep(i,N){
        rep(j,N){
            A[i]=min(A[i],C[i][j]-B[j]);
            B[j]=C[i][j]-A[i];
        }
    }
    rep(i,N){
        rep(j,N){
            if(A[i]+B[j]!=C[i][j]){
                an=false;
            }
        }
    }
    if(an==false){
        cout<<"No"<<endl;
    }
    else{
        cout<<"Yes"<<endl;
        rep(i,N){
            cout<<A[i];
            if(i!=N-1){
                cout<<" ";
            }
            else{
                cout<<""<<endl;
            }
        }
        rep(i,N){
            cout<<B[i];
            if(i!=N-1){
                cout<<" ";
            }
            else{
                cout<<""<<endl;
            }
        }
    }
}


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

int main(){
    int n, x;
    cin >> n >> x;
    int v[n], p[n];
    for (int i=0; i<n; i++){
        cin >> v[i] >> p[i];
    }
    int alcohol;
    for (int i=0; i<n; i++){
        alcohol += v[i]*p[i];
        if (alcohol > x*100){
            cout << i + 1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}

/*/ Author: _Math.man /*/

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

/*/---------------------------Defines-----------------------------------------/*/

#pragma GCC optimize("Ofast")
#define int long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for(int i =0 ;i <n;i++)
#define fn1 for( int i =1;i<=n; i++)
#define fm for(int j =0 ;j <m;j++)
#define fm1 for(int j =1;j<=m;j++)
#define fi first
#define se second
#define endl '\n'
#define PI  3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
const int N = 2e6+5;
const int INF = 1e18L;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*/-----------------------------Debug begins----------------------------------/*/
vector<string> split(const string& s, char c) {
    vector<string> v; stringstream ss(s); string x;
    while (getline(ss, x, c)) v.emplace_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
    stringstream s; s << "[";
    for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
    if(n>0)
    s << arr[n - 1] ;
    s<< "]";
    return s.str();
}
#define trace(args...) {__trace_begin(__LINE__); __trace(split(#args, ',').begin(), args);}
inline void __trace_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __trace_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __trace_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __trace_out_var(T val) { cerr << val; }
inline void __trace(vector<string>::iterator it) { cerr << endl; }

template<typename T, typename... Args>
inline void __trace(vector<string>::iterator it, T a, Args... args) {
    cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
    __trace_out_var(a);
    cerr << "; ";
    __trace(++it, args...);
}
/*/-----------------------------Code begins----------------------------------/*/
int ar[N];
int dp[N];
int go(int n){
	return (n*(n+1))/2;
}
signed main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    IOS;
    int T=1;
    // cin >> T;
    while(T--){
    	int n;
    	cin >> n ;
    	int ans=0;
		fn{
			int x,y;
			cin >> x>>y;
			ans+=go(y)-go(x-1);
		}
		cout <<ans<<'\n';
    }
    return 0;
}


/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")*/
// only when really needed

/* GNU G++17 7.3.0: No long long for faster code
   GNU G++17 9.2.0 (64 bit, msys 2): Long long only for faster code */

#include <bits/stdc++.h>

#define for1(i,a,b) for (int i = a; i <= b; i++)
#define for2(i,a,b) for (int i = a; i >= b; i--)
#define int long long

#define sz(a) (int)a.size()
#define pii pair<int,int>

/*
__builtin_popcountll(x) : Number of 1-bit
__builtin_ctzll(x) : Number of trailing 0
*/

#define PI 3.1415926535897932384626433832795
#define INF 1000000000000000000
#define MOD 1000000007
#define MOD2 1000000009
#define EPS 1e-6

using namespace std;

signed main() {

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

    // freopen("cf.inp", "r", stdin);
    // freopen("cf.out", "w", stdout);

    int n, k, cur = 0;
    vector<pii> v;
    cin >> n >> k;
    for1(i,1,n) {
        int a, b;
        cin >> a >> b;
        v.push_back({a, b});
    }

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

    for (auto p : v) {
        int a = p.first, b = p.second;

        if (a - cur > k) {
            cout << cur + k;
            return 0;
        }

        k -= a - cur;
        k += b;
        cur = a;
    }

    cout << cur + k;

}

#define MOD_TYPE 2

#pragma region Macros

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

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 0
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-7;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

void solve()
{
  int n;
  ll x;
  cin >> n >> x;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  mp[x] = 1;
  rep(i, n - 1)
  {
    map<ll, ll> nxt;
    for (auto [r, v] : mp)
    {
      ll m = a[i + 1] / a[i];
      ll t = r / a[i];
      ll b[3];
      b[0] = m - t % m;
      b[1] = b[0] - m, b[2] = b[0] + m;
      rep(j, 3)
      {
        if (abs(b[j]) < m)
          nxt[r + b[j] * a[i]] += v;
      }
    }
    mp = move(nxt);
  }
  ll sum = 0;
  for (auto [k, v] : mp)
  {
    if (k % a[n - 1] == 0)
      sum += v;
  }
  cout << sum << "\n";
}

int main()
{
  solve();
}


#include<bits/stdc++.h>
using namespace std;
long long x, y, a[200005], b[200005], ans, n;
priority_queue <int> pq;
main(){
	cin>>n;
	for (int i=1; i<=n; i++)cin>>a[n-i+1],ans+=a[n-i+1];
	for (int i=1; i<=n; i++)cin>>b[i],ans+=b[i];
	for (int i=1; i<=n; i++){
		pq.push(-a[i]);
		pq.push(-b[i]);
		ans+=pq.top();
		pq.pop();
	}
	cout<<ans;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

int main(){
  ll n;cin >> n;
  vl a(n),b(n);rep(i,n)cin >> a[i];rep(i,n)cin >> b[i];rev(all(a));
  priority_queue<ll,vl,greater<ll>> que;
  ll sum=accumulate(all(a),0LL)+accumulate(all(b),0LL);
  ll ans=0;
  rep(i,n){
    que.push(a[i]);que.push(b[i]);
    ans+=que.top();que.pop();
  }
  cout << sum-ans <<endl;
}


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

#define int long long
#define FOR(i,a,b,c) for(int i=a;i<b;i+=c)
#define REV(i,a,b,c) for(int i=a;i>=b;i-=c)
#define all(x) x.begin(),x.end()
#define Dhruv main

const int pinf=LLONG_MAX;
const int ninf=LLONG_MIN;
const int mod=1000000007;

using namespace std;
using namespace __gnu_pbds;

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

typedef tree
<int,null_type,less_equal <int>,rb_tree_tag,tree_order_statistics_node_update>
indexed_multiset;

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

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

int32_t Dhruv(){

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

    int T=1;

	while(T--){

		 int a,b;
		 cin>>a>>b;
		 if(min(a,b)+3>max(a,b)) cout<< "Yes\n";
		 else cout<< "No\n";
	}

	#ifdef LOCAL
    	cout<< "Time Elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<< "s\n";
    #endif

    return 0;

}

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <math.h>
#include <chrono>
#include <random>
#include <assert.h>
#include <functional>
using namespace std ;
using ll = long long ;
using ld = long double ;
template<class T> using V = vector<T> ;
template<class T> using VV = V<V<T>> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
ll mod = 998244353 ;
long double pie = acos(-1) ;
ll INF = 1000000000000 ;

void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
void fix_cout(){cout << fixed << setprecision(20) ;}
template<class T> void chmax(T &a,T b){	if(a<b) a = b ;}
template<class T> void chmin(T &a,T b){	if(a>b) a = b ;}

int main(){
    int n ; cin >> n ;
    V<int> a(n) ;
    for(auto &i:a) cin >> i ;
    int mx = -1 ;
    int ans = -1 ;
    for(int i=2;i<=1000;i++){
        int  cnt = 0 ;
        for(int j=0;j<n;j++) cnt += (a[j]%i==0) ;
        if(cnt>mx){
            mx = cnt ;
            ans = i ;
        }
    }
    cout << ans << endl ;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()

int get_smaller_half(ll n) {
    string s = to_string(n);
    int nsize = s.length();
    if (nsize % 2 == 1) {
        exit(9);
    }
    int M = nsize / 2;
    string lhss, rhss;
    for (int i = 0; i < s.size(); ++i) {
        if (i < M) {
            lhss += s.at(i);
        } else {
            rhss += s.at(i);
        }
    }
    int lhs = stoi(lhss), rhs = stoi(rhss);
    if (lhs > rhs) {
        return lhs - 1;
    } else {
        return lhs;
    }
}

void solve(long long N) {
    for (ll i = 1; i < 2e12; i++) {
        if (stoll(to_string(i) + to_string(i)) > N) {
            cout << i - 1 << endl;
            return;
        }
    }
}

int main() {
    long long N;
    scanf("%lld", &N);
    solve(N);
    return 0;
}

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

int main() {
    ll n;
    cin >> n;
    int ans = 0;
    for(int i = 1; i<pow(10, log10(n)); i++){
        string s = to_string(i);
        s += s;
        if(stol(s) > n){
            break;
        }
        else{
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(40)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct Vertex {
    ll p;
    vector<ll> c;
    ll num_edge = 0;
    ll num_vertex = 0;
    ll count = 0;
    ll get = 1;
};

Vertex V[100005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    V[0].p = -1;
    REP(i, N - 1) {
        ll p;
        cin >> p;
        p--;
        V[p].c.push_back(i + 1);
        V[i + 1].p = p;
    }

    queue<ll> q;
    REP(i, N) {
        if (V[i].c.size() == 0)
            q.push(i);
    }
    while (!q.empty()) {
        // 頂点tに来たときに、いくつ取れるのか？を調べる
        ll t = q.front();
        q.pop();
        V[t].num_edge = 0;
        V[t].num_vertex = 1;
        vector<ll> odd, even, even_good;
        for (auto &child:V[t].c) {
            V[t].num_edge += V[child].num_edge + 1;
            V[t].num_vertex += V[child].num_vertex;
            ll x = V[child].num_edge * 2LL + V[child].num_vertex;
            if (x % 2 == 0) {
                ll y = V[child].num_vertex - 2LL * V[child].get;
                if (y > 0)
                    even_good.push_back(child);
                else
                    even.push_back(child);
            }
            else
                odd.push_back(child);
        }

        V[t].get = 1;
        //! oddの頂点を相手にとられる数が少ない順に選んでいく選ぶ
        sort(odd.begin(), odd.end(), [](ll a, ll b) {
            ll x = V[a].num_vertex - 2LL * V[a].get;
            ll y = V[b].num_vertex - 2LL * V[b].get;
            return x > y;
        });
        {
            REP(i, even_good.size()) {
                V[t].get += V[even_good[i]].get;
            }
            REP(i, odd.size()) {
                if (i % 2 == 0)
                    V[t].get += V[odd[i]].get;
                else
                    V[t].get += V[odd[i]].num_vertex - V[odd[i]].get;
            }
            if ((odd.size() % 2LL) == 0) {
                REP(i, even.size()) {
                    V[t].get += V[even[i]].get;
                }
            }
            else {
                REP(i, even.size()) {
                    V[t].get += V[even[i]].num_vertex - V[even[i]].get;
                }
            }
        }

        if (V[t].p >= 0) {
            ll p = V[t].p;
            V[p].count++;
            if (V[p].count == V[p].c.size()) {
                q.push(p);
            }
        }
    }
    cout << V[0].get << endl;
#if false
    REP(i, N) {
        cout << V[i].get << " ";
    }
    cout << endl;
    REP(i, N) {
        cout << V[i].num_vertex << " ";
    }
    cout << endl;
#endif
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
vector<int> c[100005];
int n, x, ans1[100005], ans2[100005];
bool msf(pair<int, int> x, pair<int, int> y){
	if ((x.first+x.second)%2!=(y.first+y.second)%2){
		if (x.first-x.second < 0 and (x.first-x.second)%2==0)
			return 1;
		if (y.first-y.second < 0 and (y.first-y.second)%2==0)
			return 0;
		return (x.first+x.second)%2;
	}
	return (x.first-x.second)<(y.first-y.second);
}
void dfs(int idx){
	vector<pair<int, int> > dj;
	ans1[idx]++;
	for (int i=0; i<c[idx].size(); i++)
		dfs(c[idx][i]);
	for (int i=0; i<c[idx].size(); i++)
		dj.push_back(make_pair(ans1[c[idx][i]], ans2[c[idx][i]]));
	sort(dj.begin(), dj.end(), msf);
	int cur = 1;
	for (int i=0; i<dj.size(); i++){
		if (cur==1){
			ans1[idx]+=dj[i].first;
			ans2[idx]+=dj[i].second;
			if ((dj[i].first+dj[i].second)%2)
				cur = 0;
		}
		else {
			ans2[idx]+=dj[i].first;
			ans1[idx]+=dj[i].second;
			if ((dj[i].first+dj[i].second)%2)
				cur = 1;
		}
	}
//	cout << idx << " : " << ans1[idx] << "/" << ans2[idx] << endl;
}
int main (){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i=2; i<=n; i++){
	cin >> x;
	c[x].push_back(i);
}
dfs(1);
cout << ans1[1];
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) SORT(var); REVERSE(var)
#define OPTIMIZE_STDIO ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed
#define endl '\n'

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

using P = pair<ll, ll>;

void solve(istream& cin, ostream& cout) {
  ll n;
  cin >> n;
  vector<P> pa;
  REP(i, 5) {
    pa.push_back(P(5 - i, 15 - 3 * i));
  }
  ll ans = 0;
  ll prev = LINF;
  EACH(el, pa) {
    ll now = pow(ll(10), el.second);
    if (n / now >= 1) {
      ans += el.first * (min(n, prev) - now + 1);
    }
    prev = now - 1;
  }
  cout << ans << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif

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

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

    int n;
    string s;
    cin >> n >> s;
    ll ans=0;
    for(int i=1;i<=n/2;++i){
        int x=2*i;
        vector<int> t(4,0);
        rep(j,x){
            if(s[j]=='A') ++t[0];
            else if(s[j]=='T') ++t[1];
            else if(s[j]=='G') ++t[2];
            else ++t[3];
        }
        if(t[0]==t[1] && t[2]==t[3]) ++ans;
        rep(j,n-x){
            if(s[j]=='A') --t[0];
            else if(s[j]=='T') --t[1];
            else if(s[j]=='G') --t[2];
            else --t[3];
            if(s[j+x]=='A') ++t[0];
            else if(s[j+x]=='T') ++t[1];
            else if(s[j+x]=='G') ++t[2];
            else ++t[3];
            if(t[0]==t[1] && t[2]==t[3]) ++ans;
        }
    }
    cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
//const lint MOD =1000000007;
const lint MOD=998244353;
const lint MAX = 1000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
typedef map<lint,lint> M;
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)

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

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

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

lint primary(lint num)
{
    if (num < 2) return 0;
    else if (num == 2) return 1;
    else if (num % 2 == 0) return 0;

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            return 0;
        }
    }

    return 1;
}
   long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
    lint lcm(lint a,lint b){
        return a/__gcd(a,b)*b;
    }
     lint gcd(lint a,lint b){
        return __gcd(a,b);
    }

    int main(){
        lint a,b,c;
        cin>>a>>b>>c;
        a=a*(a+1)/2;
        a%=MOD;
        b=b*(b+1)/2;
        b%=MOD;
        c=c*(c+1)/2;
        c%=MOD;
        lint ans=a;
        ans*=b;
        ans%=MOD;
        ans*=c;
        ans%=MOD;
        cout<<ans<<endl;
    }

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

template <typename T, typename F>
class SegTree{
  int n; T uni; F f;
  vector<T> dat;
public:
  SegTree(int _n, F f, T uni) : f(f), uni(uni) {
    n = 1;
    while(n < _n) n *= 2;
    dat = vector<T>(2*n,uni);
  }
  void set(int k, const T x) {
    dat[k+n] = x;
  }
  void build() {
    for(int i = n-1; i >= 0; i--) {
      dat[i] = f(dat[2*i],dat[2*i+1]);
    }
  }
  void update(int k, T x) {
    k += n;
    dat[k] += x;
    while(k >>= 1){
      dat[k] = f(dat[2*k],dat[2*k+1]);
    }
  }
  T query(int a, int b) {
    T l = uni, r = uni;
    for(a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if(a & 1) l = f(l,dat[a++]);
      if(b & 1) r = f(dat[--b],r);
    }
    return f(l,r);
  }
};

int main(){
  int n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(n), b(m);
  auto f = [](ll a,ll b){return a + b;};
  SegTree<ll,decltype(f)> sa(MAX,f,0), sb(MAX,f,0), na(MAX,f,0), nb(MAX,f,0);
  na.set(0,n); nb.set(0,m);
  na.build(); nb.build();
  ll l[q], r[q], d[q];
  rep(i,q) cin >> l[i] >> r[i] >> d[i];
  vector<ll> v;
  v.push_back(0);
  rep(i,q) v.push_back(d[i]);
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  int vs = v.size();
  map<ll,ll> mp;
  rep(i,vs) mp[v[i]] = i;
  ll sum = 0;
  rep(i,q) {
    ll t, x, y;
    t = l[i], x = r[i], y = d[i];
    x--;
    if(t == 1) {
      sum -= nb.query(0,mp[a[x]]+1)*a[x];
      sum += nb.query(0,mp[y]+1)*y;
      if(a[x] > y) sum += sb.query(mp[y]+1,mp[a[x]]+1);
      else sum -= sb.query(mp[a[x]]+1,mp[y]+1);
      na.update(mp[a[x]],-1);
      na.update(mp[y],1);
      sa.update(mp[a[x]],-a[x]);
      sa.update(mp[y],y);
      a[x] = y;
    } else {
      sum -= na.query(0,mp[b[x]])*b[x];
      sum += na.query(0,mp[y])*y;
      if(b[x] > y) sum += sa.query(mp[y],mp[b[x]]);
      else sum -= sa.query(mp[b[x]],mp[y]);
      nb.update(mp[b[x]],-1);
      nb.update(mp[y],1);
      sb.update(mp[b[x]],-b[x]);
      sb.update(mp[y],y);
      b[x] = y;
    }
    cout << sum << endl;
  }
  return 0;
}


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

typedef long long ll;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
//

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ll n;
    cin>>n;
    int val=sqrt(n);
    ll cnt=0;
    unordered_map<ll,bool> t;
    for(ll i=2;i<=val;i++){
        for(ll j=2;j<=40;j++){
            ll v=pow(i,j);
            if(v>n){
                break;
            }
            if(t[v]){
                continue;
            }
            t[v]=true;
            cnt++;
        }
    }
    ll res=n-cnt;
    cout<<res<<endl;
}

// Problem: C - Unexpressed
// Contest: AtCoder - Caddi Programming Contest 2021(AtCoder Beginner Contest 193)
// URL: https://atcoder.jp/contests/abc193/tasks/abc193_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// Disclaimer: Don't copy my template, it may lead to plagiarism.
/* Author: Soumy Jain
   Handle: soumy_jain || soumyjain
   "Beautiful flowers too, eventually wither and fall. That's the fate of all living beings."
   "I hate perfection. To be perfect is to be unable to improve any furthur." - Mayuri Kurotsuchi | Bleach
   "I smile to show the pressure of heroes and to trick the fear inside of me."
   "Gravel may be gravel, but me? I'm the gravel that shatters diamonds."
   "If you were to write a story with me in the lead role, it would certainly be...a TRAGEDY." - Kaneki Ken | Tokyo
   Ghoul
*/
/******************************************************************************/
// clang-format off
#include <bits/stdc++.h>
#define ll        long long
#define ull       unsigned long long
#define SPEEDHACK ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Tc        ll tc; cin>>tc; while(tc--)
#define Qu        ll qu; cin>>qu; while(qu--)
#define ff        first
#define ss        second
#define sz(v)     (ll)(v).size()
#define file      freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MOD       1000000007      // 998244353
using namespace std;
/******************************************************************************/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(ll x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(ull x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template<typename T, typename V>
void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; }
template<typename T>
void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; }
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
// clang-format on

/***********************************MAIN***************************************/
// Are you ready to face the wrath of test cases? Good luck noob Soumy!

int main()
{
    SPEEDHACK
    // file
    ll n;
    cin >> n;
    set<ll> S;
    for (ll i = 2; i <= 100000; i++)
    {
        for (ll j = 2; j <= 100000; j++)
        {
            ll x = pow(i, j);
            if (x <= n)
                S.insert(x);
            else
                break;
        }
    }
    cout << n - S.size();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+10;
int main()
{
    long long N;
    cin>>N;
    N*=2;
    long long ans=0;
    for(long long i=1;i*i<=N;i++)
    {
        if(N%i!=0)continue;
        long long temp=i;
        if((N/temp+1-temp)%2==0)ans++;
        temp=N/i;
        if((N/temp+1-temp)%2==0)ans++;
    }
    cout<<ans<<endl;
}

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

int main(){
  string s,S;
  cin >> s;
  S=s;
  s[0]=S[1];
  s[1]=S[2];
  s[2]=S[0];
  cout << s << endl;
  return 0;
}


#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <utility>
#include <climits>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

#define mp make_pair
#define fr first
#define sc second

#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#define rep(s, i, n) for (int(i) = (s); (i) < (n); (i)++)
#define repe(s, i, n) for (int(i) = (s); (i) <= (n); (i)++)
#define rrep(s, i, n) for (int(i) = (s); (i) > (n); (i)--)
#define rrepe(s, i, n) for (int(i) = (s); (i) >= (n); (i)--)
#define allof(a) (a).begin(), (a).end()

int main(int argc, char const *argv[])
{
    ll a, b;
    cin >> a >> b;

    ll primes[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
    int lenp = sizeof(primes) / sizeof(ll);

    vector<ll> dp(1 << lenp, 0);
    dp[0] = 1;

    for (ll num = a; num <= b; num++)
    {
        int bit = 0;
        for (int j = 0; j < lenp; j++)
        {
            if (num % primes[j] == 0)
            {
                bit |= 1 << j;
            }
        }
        for (int st = 0; st < (1 << lenp); st++)
        {
            if ((st & bit) == 0)
            {
                dp[st | bit] += dp[st];
            }
        }
    }
    ll ans = 0;
    for (ll nst : dp)
    {
        ans += nst;
    }
    cout << ans << endl;
    return 0;
}

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

int main()
{
   double a,b;
   cin>>a>>b;
   cout<<(a*b)/100<<endl;
    return 0;
}


#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef long double ld;
typedef pair<long double, long double> pld;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll i, j;

    ll L;
    cin >> L;
    ll ans;
    ans = L - 1;
    ans = ans * (L - 2);
    ans = ans * (L - 3);
    ans = ans * (L - 4) / 8 / 3;
    ans = ans * (L - 5) / 5;
    ans = ans * (L - 6) / 6;
    ans = ans * (L - 7) / 7;
    ans = ans * (L - 8) / 4;
    ans = ans * (L - 9) / 9;
    ans = ans * (L - 10) / 10;
    ans = ans * (L - 11) / 11;
    ans /= 2;

    cout << ans << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=10000;
int pcnt;
int primes[N];
bool st[N];
ll ans[2550];
void init()
{
	for(int i=2;i<=N;i++)
	{
		if(!st[i]) primes[pcnt++]=i;
		for(int j=0;i*primes[j]<=N;j++)
		{
			st[i*primes[j]]=true;
			if(i%primes[j]==0) break;
		}
	}
}
int main()
{

	int n;
	cin>>n;
	ans[1]=6;
	ans[2]=15;
	ans[3]=10;
	int i=4;
	for(int j=12;j<=10000;j+=6)
	{
		ans[i++]=j;
		if(i>n) break;
	}

	for(int j=30;j<=10000;j+=15)
	{
		if(j%6==0) continue;
		ans[i++]=j;
		if(i>n) break;
	}

for(int j=20;j<=10000;j+=10)
	{
		if(j%6==0||j%15==0) continue;
		ans[i++]=j;
		if(i>n) break;
	}

	for(int i=1;i<=n;i++)
	cout<<ans[i]<<" ";

}


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

int main()
{  cin.tie(NULL);
   ios_base::sync_with_stdio(false);
   cout.tie(NULL);
   int a,b,w;
   cin>>a>>b>>w;
   w=w*1000;
   int l=INT_MAX;//min
   int r=INT_MIN;//max;
   for(int i=1;i<=w;i++)
   {
     if(a*i<=w&&w<=b*i)
     {
         l=min(i,l);
         r=max(i,r);
     }
   }
   if(l!=INT_MAX&&r!=INT_MIN)
   {
   cout<<l<<" "<<r<<endl;
   }
   else
   {
       cout<<"UNSATISFIABLE"<<endl;
   }
   return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];}
#define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";};cout << endl;
#define ld long double
#define PI 3.141592653589793238
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define int ll

void solve(){
    int a, b, w; cin >> a >> b >> w;
    int mx = w / (a / 1000.0);
    int mn = ceil(w / (b / 1000.0));
    if(mn > mx){
        cout << "UNSATISFIABLE" << endl;
        return;
    }
    cout << mn << " " << mx << endl;
}

int32_t main(){
    FAST
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 50;
const int inf = 1e9;
inline int lowbit(const int & x) { return x & (-x);}
int n, rt;
int ls[maxn], rs[maxn];
int a[maxn][maxn];
void check()
{
	for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) a[i][j] = (i == j) ? 0 : inf;
	for(int i = 1; i <= n; ++i) a[i][ls[i]] = a[i][rs[i]] = 1;
	for(int k = 1; k <= n; ++k)
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				a[i][j] = min(a[i][k] + a[k][j], a[i][j]);
	for(int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j)
			printf("%d ", a[i][j]);
		puts("");
	}
}
int main()
{
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
	{
		ls[i] = (i << 1) % n + 1;
		rs[i] = (i << 1 | 1) % n + 1;
		printf("%d %d\n", ls[i], rs[i]);
	}
	//check();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		printf("%d %d\n",(i*2-1)%n+1,(i*2)%n+1);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
int adj[2001][2001];
set<int> nb[20001];
int n;
int walk(int root) {
	vector<int> d(n+1, INF);
	set<pair<int, int> > q;
	for(set<int>::iterator i = nb[root].begin(); i!=nb[root].end(); i++){
		q.insert(make_pair(adj[root][*i], *i));
		d[*i]=adj[root][*i];
	}
	while (!q.empty()) {
		int v = q.begin()->second;
		q.erase(q.begin());
        for (set<int>::iterator is=nb[v].begin(); is!=nb[v].end(); is++) {
            int to = *is;
            int len = adj[v][*is];
			if (d[v] + len < d[to]) {
                q.erase(make_pair(d[to], to));
                d[to] = d[v] + len;
                q.insert(make_pair(d[to], to));
            }
        }
	}
	if(d[root]==INF) return -1;
	return d[root];
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int m;
	cin >> n >> m;
	for(int i = 1; i<=n; i++) for(int j = 1; j<=n; j++) adj[i][j] = INF;
	for(int i = 0; i<m; i++){
		int a, b, c;
		cin >> a >> b >> c;
		adj[a][b] = min(adj[a][b], c);
		nb[a].insert(b);
	}
	for(int i = 1; i<=n; i++){
		cout << walk(i) << '\n';
	}
	return 0;
}

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
const int INF = 10000000000000;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void dijkstra(vector<vector<pair<int, int>>> &G, int s,
              vector<int> &dist) {  //スタート地点を指定
    //コスト、向かう頂点でpair(コストが小さい順に並ぶ)
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        que;
    que.push(make_pair(0, s));
    while (!que.empty()) {
        pair<int, int> P = que.top();
        que.pop();
        int v = P.second;
        if (dist[v] < P.first)
            continue;
        dist[v] = P.first;
        for (auto &next : G.at(v)) {
            que.push({dist[v] + next.second, next.first});
        }
    }
}

signed main() {
    int N, M;
    cin >> N >> M;

    vector<vector<pair<int, int>>> G(N);
    vector<int> ans(N, INF);
    rep(i, M) {
        int A, B, C;
        cin >> A >> B >> C;
        A--;
        B--;
        if (A == B)
            ans[A] = min(ans[A], C);
        else
            G[A].emplace_back(make_pair(B, C));
    }

    rep(i, N) {
        for (auto nx : G[i]) {
            int cost = nx.second;
            int nv = nx.first;
            vector<int> dist(N, INF);
            dijkstra(G, nv, dist);
            ans[i] = min(dist[i] + cost, ans[i]);
        }
    }

    rep(i, N) {
        if (ans[i] == INF)
            cout << -1 << "\n";
        else
            cout << ans[i] << "\n";
    }
}


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

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long int
#define nl "\n"
#define forf(i,a,n) for(ll i=a;i<=n;i++)
#define forb(i,b,n) for(ll i=b;i>=n;i--)
#define pb push_back
#define vll vector<ll>
#define all(qw) qw.begin(),qw.end()
#define read(vec) ll qwe;cin>>qwe;vec.pb(qwe)
#define pll pair<ll,ll>
#define vpll vector<pll>
#define ff first
#define ss second
#define mp make_pair
#define sll set<ll>
#define mll map<ll,ll>
#define go(x) {cout<<x<<nl;return;}
#define mod 1000000007
#define ina(v,n)  forf(i,0,n-1)  cin>>v[i];
#define inao(v,n) forf(i,1,n)    cin>>v[i];
#define inv(v,n) forf(i,1,n)    {read(v);}
#define trav(arr,var) for(auto& var:arr)

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

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

bool isprime(ll n) {
    if(n==1) return false;
    for (ll i=2;i*i<=n;++i){if(n%i==0) return false;}
    return true;
}

                //SOLVE

void solve(){
   long double n,d,h;
   cin>>n>>d>>h;
   vector<long double> a(n),b(n);
   forf(i,0,n-1){
      cin>>a[i];
      cin>>b[i];
   }
   long double ans=0;
   forf(i,0,n-1){
      long double temp=((d*b[i]-h*a[i])/(d-a[i]));
      ans=max(temp,ans);
   }
   cout<<setprecision(16)<<ans;
}

void onlinejudge(){
  #ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
  #endif // ONLINE_JUDGE
}

int main(){  // Change the approach
  onlinejudge();
  //fastio
  ll ttt;ttt=1;
 // cin>>ttt;
  while(ttt--) solve();
return 0;
}

// CHECK FOR EXTREME CASES AND ZEROS;

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

int main(){
int n;double x,y,a,b,c = 0; cin >> n >> x >> y;
for(int i=0;i<n;i++){
   cin >> a >> b;
   c = max(c,y-(y-b)/(x-a)*x);
}
cout << c << endl;
}


#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long>
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << " " << y << endl
#define co3(x, y, z) cout << x << " " << y << " " << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;

const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9;
int h, w;
char s[2000][2001];
int ans[2000][2000], sum[2000][2000][3];

int main() {
    ci2(h, w);
    rep(i, h) ci(s[i]);
    ans[0][0] = 1;
    rep(i, h) {
        rep(j, w) {
            if (s[i][j] == '#') continue;
            if (j > 0) {
                ans[i][j] = (ans[i][j] + sum[i][j - 1][0]) % MOD;
                sum[i][j][0] = sum[i][j - 1][0];
            }
            if (i > 0) {
                ans[i][j] = (ans[i][j] + sum[i - 1][j][1]) % MOD;
                sum[i][j][1] = sum[i - 1][j][1];
            }
            if (i > 0 && j > 0) {
                ans[i][j] = (ans[i][j] + sum[i - 1][j - 1][2]) % MOD;
                sum[i][j][2] = sum[i - 1][j - 1][2];
            }
            rep(k, 3) sum[i][j][k] = (sum[i][j][k] + ans[i][j]) % MOD;
        }
    }
    co(ans[h - 1][w - 1]);
    return 0;
}

#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
#include <cfloat>
#include <iomanip>
using namespace std;
string S[2000];
long long dp[2000][2000];
long long dp_aux[2000][2000][3];
const long long MOD = 1000000007;
int main(){
  int H;
  int W;
  cin >> H >> W;

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

  dp[0][0] = 1;
  for(int i = 1; i < W; i++){
    if(S[0][i] != '#'){
      dp_aux[0][i][2] = (dp[0][i - 1] + dp_aux[0][i - 1][2]) % MOD;
      dp[0][i] = dp_aux[0][i][2];
    } else {
      break;
    }
  }

  for(int i = 1; i < H; i++){
    if(S[i][0] != '#'){
      dp_aux[i][0][0] = (dp[i - 1][0] + dp_aux[i - 1][0][0]) % MOD;
      dp[i][0] = dp_aux[i][0][0];
    }
    for(int j = 1; j < W; j++){
      if(S[i][j] != '#'){
        dp_aux[i][j][0] = (dp[i - 1][j] + dp_aux[i - 1][j][0]) % MOD;
        dp_aux[i][j][1] = (dp[i - 1][j - 1] + dp_aux[i - 1][j - 1][1]) % MOD;
        dp_aux[i][j][2] = (dp[i][j - 1] + dp_aux[i][j - 1][2]) % MOD;
        dp[i][j] = ((dp_aux[i][j][0] + dp_aux[i][j][1]) % MOD + dp_aux[i][j][2]) % MOD;
      }
    }
  }
  cout << dp[H - 1][W - 1] << endl;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define md 1000000007
using namespace std;
int main(){
ll n,H,D,d,h,i;
double mx=0;

cin>>n>>D>>H;

for(i=0;i<n;i++){
	cin>>d>>h;
	if(H*d>=h*D) continue;
	double numr = h*D - H*d;
	double den = D-d;
	double val = numr/den;
	mx = max(mx,val);
}
cout.precision(10);
cout<<fixed<<mx;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define F first
#define S second
ll MOD = 1000000007;

ll power(ll base, ll n){
	if (n == 0) return 1;
	if (n == 1) return base;
	ll halfn = power(base, n/2);
	if (n % 2 == 0) return (halfn * halfn) % MOD;
	return (((halfn * halfn) % MOD) * base) % MOD;
}

ll inverse(ll n){
	return power(n, MOD-2);
}

ll add(ll a, ll b){
	return (a+b) % MOD;
}

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

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

const int N = (1<<18) + 1;
vi p(N), r(N);
int get(int a){
	return p[a] = (p[a] == a ? p[a] : get(p[a]));
}
void Union(int a, int b){
	int c = get(a); int d = get(b);
	if (c == d) return;
	if (r[c] == r[d]) r[c]++;
	if (r[c] > r[d]){
		p[d] = c;
	}else{
		p[c] = d;
	}
}
int main(){
    ios::sync_with_stdio(false);
    int n; ld d, h; cin >> n >> d >> h;
    ld grad = h / d;
	FOR(i, 0, n){
		ld h1, d1; cin >> d1 >> h1;
		grad = min( (h-h1)/(d-d1), grad);
	}
	ld ans = h - grad * d;
	cout << setprecision(18) << ans << '\n';
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<functional>
#include<iomanip>
#define rep(i,n,m) for(int i=(n);i<(int)(m);i++)
#define reps(i,n,m) for(int i=(n);i<=(int)(m);i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a,x) lb(all(a), x) - a.begin()
#define UB(a,x) ub(all(a), x) - a.begin()
#define chartoint(c) (int)((c) - '0')
#define chartoll(c) (long long)((c) - '0')
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define fps(n) fixed << setprecision(n)
#define MOD 1000000007
#define itn int
#define enld endl
#define ednl endl
#define icn cin
#define cotu cout
#define Endl endl
#define stirng string
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
using Graph = vector<vector<int>>;
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 A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> A(n);
    vector<ll> B(n);
    rep(i,0,n) cin >> A[i];
    rep(i,0,n) cin >> B[i];
    ll sum = 0LL;
    rep(i,0,n) sum += A[i]*B[i];
    if(sum == 0) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

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

int main(){
    std::fstream myfile("D:\\CodeBlocks\\c++\\input.txt.c", std::ios_base::in);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n;
    cin>>n;

    vector<ll> vec(n);

    for(ll i=0;i<n;i++){
        cin>>vec[i];
    }
    ll sum=0;
    for(ll i=0;i<n;i++){
        ll temp;
        cin>>temp;
        sum+=(vec[i]*temp);
    }
    if(sum==0)
        cout<<"Yes\n";
    else
        cout<<"No\n";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
void randinit(){srand((unsigned)time(NULL));}
int main(){
    ll N;
    cin >> N;
    N *= 2;
    vector<ll> A,B,C;
    for(ll i = 0;i < N;i++){
        ll a;
        char c;
        cin >> a >> c;
        if(c == 'R') A.emplace_back(a);
        if(c == 'G') B.emplace_back(a);
        if(c == 'B') C.emplace_back(a);
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    sort(C.begin(),C.end());
    if(A.size() % 2 == 0 && B.size() % 2 == 0 && C.size() % 2 == 0){
        cout << 0 << endl;
        return 0;
    }
    ll a,b,c;
    if(A.size() == 0) a = 0;
    else a = 2 - (A.size() % 2);
    if(B.size() == 0) b = 0;
    else b = 2 - (B.size() % 2);
    if(C.size() == 0) c = 0;
    else c = 2 - (C.size() % 2);
    if(a > b){
        swap(a,b);
        swap(A,B);
    }
    if(b > c){
        swap(b,c);
        swap(B,C);
    }
    if(a > b){
        swap(a,b);
        swap(A,B);
    }
    if(b > c){
        swap(b,c);
        swap(B,C);
    }
    if(a > b){
        swap(a,b);
        swap(A,B);
    }
    if(b > c){
        swap(b,c);
        swap(B,C);
    }
    ll ans = MAX;
    if(a == 0){
        swap(a,c);
        swap(A,C);
    }
    vector<ll> AA = A,BB = B;
    BB.emplace_back(-MAX);
    BB.emplace_back(MAX);
    sort(BB.begin(),BB.end());
    for(ll i = 0;i < A.size();i++){
        ans = min(ans,abs(A[i] - *(lower_bound(BB.begin(),BB.end(),A[i]))));
        ans = min(ans,abs(A[i] - *(upper_bound(BB.begin(),BB.end(),A[i]) - 1)));
    }
    if(c != 0){
        vector<ll> AAA = A,BBB = B,CCC = C;
        AAA.emplace_back(-MAX);
        AAA.emplace_back(MAX);
        sort(AAA.begin(),AAA.end());
        BBB.emplace_back(-MAX);
        BBB.emplace_back(MAX);
        sort(BBB.begin(),BBB.end());
        vector<ll> P;
        for(ll i = 0;i < C.size();i++){
            ll p = min(
                abs(C[i] - *(lower_bound(AAA.begin(),AAA.end(),C[i]))),
                abs(C[i] - *(upper_bound(AAA.begin(),AAA.end(),C[i]) - 1))
            );
            P.emplace_back(p);
        }
        vector<ll> PP = P;
        sort(PP.begin(),PP.end());
        for(ll i = 0;i < C.size();i++){
            ll p = min(
                abs(C[i] - *(lower_bound(BBB.begin(),BBB.end(),C[i]))),
                abs(C[i] - *(upper_bound(BBB.begin(),BBB.end(),C[i]) - 1))
            );
            if(PP[0] == PP[1] || PP[0] != P[i]){
                ans = min(ans,p + PP[0]);
            }
            else{
                ans = min(ans,p + PP[1]);
            }
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < (t); ++i)
#define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define bn(x) ((1<<(x))-1)
#define newline puts("")
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using P = pair<ll,int>;
using T3 = tuple<int,int,int>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vp = vc<P>;
using vt = vc<T3>;
int getInt(){int x;scanf("%d",&x);return x;}
vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;}
template<typename T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();}
template<typename T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
vc<string> split(const string& s,char d=' '){vc<string> r(1);for(char c:s)if(c==d)r.pb("");else r.back()+=c;return r;}
string operator*(const string& s,int t){return join(vc<string>(t,s));}
template<typename T1,typename T2>bool mins(T1& x,const T2&y){if(x>y){x=y;return true;}else return false;}
template<typename T1,typename T2>bool maxs(T1& x,const T2&y){if(x<y){x=y;return true;}else return false;}
template<typename T>T dup(T x, T y){return (x+y-1)/y;}
template<typename T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
template<typename T>void uni(T& a){sort(rng(a));a.erase(unique(rng(a)),a.end());}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
using vvl = vc<vl>;

struct Solver {
  void solve() {
    int n;
    scanf("%d",&n);
    vvl a(3);
    vp p;
    rep(i,n*2) {
      char c;
      ll x;
      scanf("%lld %c",&x,&c);
      c %= 3;
      a[c].pb(x);
      p.eb(x,c);
    }
    sort(rng(p));
    vl dp(16, LINF);
    dp[0] = 0;
    for (auto x : p) {
      vl pd(16,LINF); swap(dp,pd);
      rep(i,16) {
        mins(dp[i^1<<x.se], pd[i]);
        ll now = -x.fi;
        if (i&8) now = -now;
        mins(dp[i^8], pd[i]+now);
      }
    }
    ll ans = dp[0];

    // vi os;
    // rep(i,3) {
    //   sort(rng(a[i]));
    //   if (sz(a[i])&1) os.pb(i);
    // }
    // ll ans = 0;
    // if (sz(os)) {
    //   ans = LINF;
    //   vl x = a[os[0]];
    //   vl y = a[os[1]];
    //   int xi = 0, yi = 0;
    //   while (xi < sz(x) && yi < sz(y)) {
    //     mins(ans, abs(x[xi]-y[yi]));
    //     if (x[xi] < y[yi]) ++xi; else ++yi;
    //   }
    // }
    cout<<ans<<endl;
  }
};

int main() {
  int ts = 1;
  // scanf("%d",&ts);
  rrep(ti,ts) {
    Solver solver;
    solver.solve();
  }
  return 0;
}


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

int main() {
    int n;
    map<int,int> b;
    cin >> n;
    ll ans = 0;
    vector<int> a(n);
    rep(i,n){
        cin >> a[i];
        b[a[i]] = 0;
    }
    for(int i = 0; i < n; ++i){
        int k=i;
        k -= b[a[i]];
        ans += k;
        b[a[i]]++;
    }
    cout << ans << endl;
    return 0;
}

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

int main(){
    ll n;
    cin >> n;
    vector<ll> s(n);
    for(ll i=0; i<n; i++){
        string S;
        cin >> S;
        if(S=="AND") s.at(i)=1;
        else s.at(i)=0;
    }
    vector<ll> t(n+1,0);
    vector<ll> f(n+1,0);
    t.at(0)=1;
    f.at(0)=1;
    for(int i=0; i<n; i++){
        if(s.at(i)==0){
            t.at(i+1) = t.at(i)*2+f.at(i);
            f.at(i+1) = f.at(i);
        }
        else{
            t.at(i+1) = t.at(i);
            f.at(i+1) = t.at(i)+f.at(i)*2;
        }
    }
    /*for(int i=0; i<=n; i++){
        cout << t.at(i) << " ";
    }
    cout << endl;
    for(int i=0; i<=n; i++){
        cout << f.at(i) << " ";
    }*/
    cout << t.at(n) << endl;
}


#include <bits/stdc++.h>

#define N 105
#define Mo 998244353

#define pb push_back
#define mk make_pair
#define fi first
#define se second

using namespace std;

int dp[N][N][N * N], n, a[N], fac[N];

int main()
{
	scanf("%d", &n); fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % Mo;
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[0] += a[i];
	dp[0][0][0] = 1;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
			for (int k = 0; k <= 100 * n; ++k)
				(dp[i + 1][j][k] += dp[i][j][k]) %= Mo,
				(dp[i + 1][j + 1][k + a[i + 1]] += dp[i][j][k]) %= Mo;
	int ans = 0;
	if (a[0] % 2 == 0)
	for (int j = 1; j <= n; ++j)
		(ans += 1ll * fac[j] * fac[n - j] % Mo * dp[n][j][a[0] / 2] % Mo) %= Mo;
	printf("%d\n", ans);
}

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

const int MAXN = 100005;
int f[MAXN], sz[MAXN], n;
vector<int> son[MAXN];
struct Data {
	int a, b;
	bool operator<(const Data &d) const {
		return a - b < d.a - d.b;
	}
};

void dfs(int u) {
	sz[u] = 1;
	vector<Data> v0, v1;
	for (int v : son[u]) {
		dfs(v);
		sz[u] += sz[v];
		if (sz[v] & 1) v1.push_back(Data { f[v], sz[v] - f[v] });
		else v0.push_back(Data { f[v], sz[v] - f[v] });
	}
	for (const Data &d : v0) {
		if (d.a <= d.b) f[u] += d.a;
	}
	sort(v1.begin(), v1.end());
	for (int i = 0; i < (int)v1.size(); i++) {
		if (i & 1) f[u] += v1[i].b;
		else f[u] += v1[i].a;
	}
	if (v1.size() & 1) {
		for (const Data &d : v0) {
			if (d.a > d.b) f[u] += d.b;
		}
	} else {
		for (const Data &d : v0) {
			if (d.a > d.b) f[u] += d.a;
		}
	}
	++f[u];
}

int main() {
	scanf("%d", &n);
	for (int i = 2; i <= n; i++) {
		int p; scanf("%d", &p);
		son[p].push_back(i);
	}
	dfs(1);
	printf("%d\n", f[1]);
	return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
VII dist(600,vector<ll>(600,INF));
int main()
{
	owo
	int r,c;
	cin>>r>>c;
	VII a(r,vector<ll>(c)),b(r,vector<ll>(c));
	for(int i=0;i<r;i++)
		for(int j=0;j<c-1;j++)cin>>a[i][j];

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

	dist[0][0] = 0;
	priority_queue<pair<ll,pii>,vector<pair<ll,pii>>,greater<pair<ll,pii>>>pq;
	pq.push({0,{0,0}});
	while(!pq.empty()){
		ll d = pq.top().fi;
		pii v = pq.top().se;
		pq.pop();
		if(d != dist[v.fi][v.se])continue;
		int rn = v.fi;
		int cn = v.se;
		if(cn < c-1 && d+a[rn][cn] < dist[rn][cn+1]){
			dist[rn][cn+1] = d+a[rn][cn];
			pq.push({dist[rn][cn+1],{rn,cn+1}});
		}
		if(cn && d+a[rn][cn-1] < dist[rn][cn-1]){
			dist[rn][cn-1] = d+a[rn][cn-1];
			pq.push({dist[rn][cn-1],{rn,cn-1}});
		}
		if(rn < r-1 && d+b[rn][cn] < dist[rn+1][cn]){
			dist[rn+1][cn] = d+b[rn][cn];
			pq.push({dist[rn+1][cn],{rn+1,cn}});
		}
		for(int i=rn-1;i>=0;i--){
			if(dist[i][cn] > d+rn-i+1){
				dist[i][cn] = d+rn-i+1;
				pq.push({dist[i][cn],{i,cn}});

			}
		}
	}
	cout<<dist[r-1][c-1];
}

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

int main() {
  int M;
  cin >> M >> M;
  vector<string> s(M);
  map<string, int> m;
  set<string> S;
  vector<vector<char>> a(20, vector<char>(20, 'A'));
  deque<tuple<int, int, string>> t;
  for (int i = 0; i < M; i++) {
    cin >> s.at(i);
    if (m.count(s.at(i))) {
      m.at(s.at(i))++;
    }
    else {
      m[s.at(i)] = 1;
    }
  }
  for (int i = 0; i < M; i++) {
    if (!(S.count(s.at(i)))) {
      S.insert(s.at(i));
      t.push_back(make_tuple(m.at(s.at(i)), (int)s.at(i).size(), s.at(i)));
    }
  }
  sort(t.begin(), t.end());
  for (int i = 0; i < 20; i++) {
    M = 0;
    for (int j = 0; j < 10; j++) {
      if (M + get<1>(t.front()) > 19) {
        break;
      }
      for (int k = 0; k < get<1>(t.front()); k++) {
        a.at(i).at(M + k) = get<2>(t.front()).at(k);
      }
      M += get<1>(t.front());
      t.pop_front();
    }
  }
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 20; j++) {
      cout << a.at(i).at(j);
      if (j > 18) {
        cout << endl;
      }
    }
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200000 + 10;
const int MOD = 998244353;

int pow(int x, int k) {
    if (k == 0) return 1;
    long long t = pow(x, k / 2);
    t = (t * t) % MOD;
    if (k % 2 == 1) t = (t * x) % MOD;
    return (int)(t);
}

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    if (min(n, m) == 1) {
        cout << pow(k, max(m, n)) << "\n";
        return 0;
    }

    long long res = 0;
    for(int maxa = 1; maxa <= k; ++maxa) {
        int na = (pow(maxa, n) - pow(maxa - 1, n) + MOD) % MOD, nb = pow(k - maxa + 1, m);
        res = (res + ((1LL * na * nb) % MOD) ) % MOD;
    }
    cout << res << "\n";
}

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 101

using namespace std;
double memo[MAXN][MAXN][MAXN]{};

double dfs(vector<int> &coins) {
  if (coins[1] == 0)
    return 100.0 - coins[0];
  if (coins[0] == 100 || coins[1] == 100 || coins[2] == 100)
    return 0.0;
  if (memo[coins[0]][coins[1]][coins[2]] >= 0)
    return memo[coins[0]][coins[1]][coins[2]];
  int s = 0;
  for (int coin : coins)
    s += coin;
  double ans = 0;
  for (int i = 0; i < 3; ++i) {
    if (coins[i] > 0) {
      vector<int> nxt(coins);
      nxt[i]++;
      ans += (double)coins[i] / s * (1 + dfs(nxt));
    }
  }
  return memo[coins[0]][coins[1]][coins[2]] = ans;
}

int main() {
  vector<int> coins(3);
  for (int i = 0; i < 3; ++i)
    cin >> coins[i];
  sort(coins.rbegin(), coins.rend());
  memset(memo, -1.0, sizeof(memo));
  printf("%.12f", dfs(coins));
}


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

#define endl "\n"
#define ll long long

int main()
{
  string s;
  cin>>s;
  reverse(s.begin(),s.end());
  int l = s.size();
  for(int i=0;i<l;i++)
  {
    if(s[i]=='9')s[i] = '6';
    else if(s[i]=='6')s[i] = '9';
  }
  cout<<s<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1e9+7;

vector<vector<int>> graph;
vector<vector<bool>> isReach;
int n,m;
void init() {
  for (int i = 0; i < n; ++i)
  {
    isReach[i][i] = true;
    queue<int> nxt;
    nxt.push(i);
    vector<bool> seen(n, false);
    while (!nxt.empty()) {
      auto now = nxt.front();
      nxt.pop();
      seen[now] = true;
      isReach[i][now] = true;
      for (const auto& e: graph[now]) {
        if (seen[e]) continue;
        nxt.push(e);
      }
    }
  }
}

int main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin>>n>>m;
  graph.resize(n);
  for (int i = 0; i < m; ++i)
  {
    int a,b;
    cin>>a>>b;
    a--; b--;
    graph[a].push_back(b);
  }
  int ans = 0;
  isReach = vector<vector<bool>>(n, vector<bool>(n, false));
  init();
  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (isReach[i][j]) ans++;
    }
  }
  cout << ans << "\n";
}

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

int main(){
    int n, k;
    cin >> n >> k;
    vvec<int> t(n, vec<int>(n, 0));
    rep(i, n) rep(j, n) cin >> t[i][j];
    int array[n-1];
    rep(i, n-1) array[i] = i + 1;
    int prev, cost, ans;
    ans = 0;
    do{
        prev = 0;
        cost = 0;
        // for(int i=0; i<n-1; i++){
        //     cout<<array[i];
        //     if(i!=3)cout<<" ";
        // }
        // cout<<endl;
        for(int i=0; i<n-1; i++){
            cost += t[prev][array[i]];
            prev = array[i];
        }
        cost += t[prev][0];
        if (cost == k) ans++;
    } while (next_permutation(array,array+n-1));
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<ll, ll>;
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
// struct Edge { ll to; ll cost; Edge(ll t, ll c) { to = t; cost = c;} };
// using Graph = vector<vector<Edge>>;
// using Graph = vector<vector<ll>>;
// priority_queue<P, vector<P>, greater<P>> q;

int main() {
    ll n; cin >> n;
    ll ans = 10010010010;
    vector<ll> a(n), b(n);
    rep(i, n) cin >> a[i] >> b[i];
    for (ll i = 0; i < n; i++) {
        for (ll j = 0; j < n; j++) {
            if (i == j) ans = min(ans, a[i] + b[i]);
            else {
                ans = min(ans, max(a[i], b[j]));
            }
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
	int a,b; cin >> a >> b;
	if(b>a)
	{
	    int sum=0;
	    int val=-1;
	    for(int i=0;i<b;i++)
	    {
	        cout << val << " ";
	        sum += val;
	        val -= 1;
	    }
	    int val2=1;
	    for(int i=0;i<a-1;i++)
	    {
	        cout << val2 << " ";
	        sum += val2;
	        val2 += 1;
	    }
	    cout << abs(sum);
	}
	else
	{
	    int sum=0;
	    int val=1;
	    for(int i=0;i<a;i++)
	    {
	        cout << val << " ";
	        sum += val;
	        val += 1;
	    }
	    int val2=-1;
	    for(int i=0;i<b-1;i++)
	    {
	        cout << val2 << " ";
	        sum += val2;
	        val2 -= 1;
	    }
	    cout << -1*sum;
	}
	return 0;
}


#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x,"r",stdin)
#define WF(x) freopen(x,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
const LL MOD = (LL)998244353;
const int SIZE = 2e5+5;
const LL INF = 1LL<<60;
const double eps = 1e-4;
const double PI=3.1415926535897932;

int a[100009];
int b[100009];
int c[100009];
map<int,vector<int> > m;

int main(){
    DRI(n);
    RI(a[n+1]);a[n+1]++;
    REPP(i,1,n+1)RI(a[i]);
    REPP(i,1,n+1)RI(b[i]);
    b[n+1]=a[n+1];
    REP(i,n+2){
        m[a[i]-i].PB(i);
    }
    LL ans = 0;
    REPP(i,1,n+1){
        if(!m.count(b[i]-i)){
            puts("-1");return 0;
        }
        const vector<int> &v = m[b[i]-i];
        auto it = lower_bound(ALL(v),i);
        if(it==v.end()){
            //ans += abs(i-*prev(it));
            c[i] = *prev(it);
        }
        else if(it==v.begin()){
            //ans += abs(*it - i);
            c[i] = *it;
        }
        else{
            //ans += min(abs(*it - i),abs(i-*prev(it)));
            if(abs(*it - i)<abs(i-*prev(it)))c[i] = *it;
            else c[i] = *prev(it);
        }
    }
    c[n+1]=n+1;
    REPP(i,1,n+1){
        //printf("%d ",c[i]);
        if(a[i]==b[i])continue;
        bool same = (c[i]>i && c[i+1] == c[i])||(c[i]<i && c[i-1] == c[i]);
        if (same) ans++;
        else {
            ans += abs(i-c[i]);
        }
    }
    printf("%lld",ans);
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1.1e5;
int n,m,ta,tb;
struct qj{
	int l,r;
}a[N],b[N];
int main()
{
	cin>>n>>m;int la=0;
	for(int i=1,x;i<=n;i++){
	scanf("%d",&x);if(la+1<=x-1)a[++ta]=(qj){la+1,x-1};la=x;}
	int x=m;if(la+1<=x)a[++ta]=(qj){la+1,x};la=0;
	for(int i=1,x;i<=n;i++){
	scanf("%d",&x);if(la+1<=x-1)b[++tb]=(qj){la+1,x-1};la=x;}
	x=m;if(la+1<=x)b[++tb]=(qj){la+1,x};la=0;
	ll ans=0;int j=1;
	for(int i=1;i<=tb;i++){//cout<<i<<" "<<ans<<endl;
		if(a[j].l<b[i].l)ans+=b[i].l-a[j].l;
		while(b[i].l<=b[i].r){
			if(a[j].r-a[j].l>b[i].r-b[i].l)return puts("-1"),0;
			b[i].l+=(a[j].r-a[j].l+1);if(b[i].l>b[i].r){
				if(a[j].r>b[i].r)ans+=a[j].r-b[i].r;j++;break;
			}j++;
		}
	}cout<<ans;
}


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

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

  int cnt = 0;
  for (int i = 0; i < s.size(); ++i) {
    string str = s.substr(i, 4);
    if (str == "ZONe") cnt ++;
  }

  cout << cnt << endl;

  return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i,j) for(int i=0;i<j;i++)
#define REPA(i,j) for(int i=1;i<=j;i++)
#define FORN(i,j,k) for(int i=j;i<k;i++)
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define vpii vector<pii >
#define all(a) a.begin(),a.end()
using namespace std;
const int INF=1<<30;
inline void read(int &x){
//	short neg=1;
	x=0;
	char c=getchar();
	/*while(c<'0'||c>'9'){
		if(c=='-')neg=-1;
		c=getchar();
	}*/
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c^48),c=getchar();
	}
//	x*=neg;
}
const int N=3e5+10;
int n,k;
int a[N];
int cnt[N];
pii minn[N];// without itself
int main(void){
	read(n);read(k);
	REP(i,n)read(a[i]);
	memset(cnt,0,sizeof(cnt));
	REP(i,n)cnt[a[i]]++;
	minn[1]=mp(cnt[0],0);
	for(int i=2;i<n;i++){
		if(cnt[i-1]<minn[i-1].fi){
			minn[i]=mp(cnt[i-1],i-1);
		}
		else{
			minn[i]=minn[i-1];
		}
	}
	ll ans=0;
	int m=0,it=0;//cnt[it]==0
	while(cnt[it]!=0)it++;//find the first i->0
	while(k>0&&cnt[0]-m>0&&it>0){
//		cout<<it<<' '<<m<<' '<<ans<<' '<<minn[it].fi<<endl;
		ans+=it*(min(k,minn[it].fi-m));
		k-=minn[it].fi-m;
		m=minn[it].fi;
		it=minn[it].se;
	}
	printf("%lld",ans);
	return 0;
}


#include<iostream>
#include<string>
using namespace std;
typedef long long ll;

int main() {
	ll n,x;
	ll a[100001];
	ll b = 0,c=0;
	cin >> n>>x;

	for (ll i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i] == x) {
			b += 1;
		}
		else {
			c = i;
		}
	}
	if (b == n) {
		cout << " " << endl;
	}else {
		for (ll i = 0; i < n; i++) {
			if (i == c) {
				cout << a[i] << endl;
			}
			else {
				if (a[i] != x) {
					cout << a[i] << " ";
				}

			}
		}
	}

	return 0;
}

/**
 * @author: adityasonani
 * */
#include <bits/stdc++.h>

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

#define     ll          long long
#define     ld        	long double
#define     ln          "\n"
#define     fastio      ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define     MOD         (int) 1000000007
#define     rep(i, n)   for (int i = 0; i < (int)n; i++)
#define     all(x)      (x).begin(), (x).end()
#define     debug(x)    cerr << #x << " = " << x << endl;
#define     precise(x)  cout << fixed << setprecision(x)
#define     MAX         (int) 10000007 // 10^7

using namespace std;

/*
*/

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

    rep(i, n)  {
        int a;
        cin>>a;
        if(a!=k) cout << a << " ";
    }
}

signed main()
{
    fastio;

    auto start = std::chrono::high_resolution_clock::now();

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

    while(t--)
        solve();

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);

    // cerr << "\nTime taken : " << ((long double)duration.count())/((long double) 1e9) <<" s "<< endl;

    return 0;
}


# include <bits/stdc++.h>
using namespace std;
# define rep(i,a,n) for (int i=a; i<=n; ++i)
# define per(i,a,n) for (int i=a; i>=n; --i)
# define bug puts("H");
# define pb push_back
# define mp make_pair
# define all(x) (x).begin(), (x).end()
# define SZ(x) (int)x.size()
# define fi first
# define se second
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mem(a,b) memset(a,b,sizeof(a))
# define INF 1000000001
# define MOD 1000000007
typedef vector<int> Vi;
typedef long long i64;
typedef pair<int, int> Pi;
mt19937 mrand(random_device{}());
int rnd(int x) {return mrand()%x;}
i64 powmod(i64 a, i64 b){i64 res=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
i64 Inv(i64 b){return powmod(b,(i64)MOD-2);}
typedef long long ll;

const int N = 100005;
const double eps = 1e-9;
const double inf = 1e18;

int a[N], b[N];
Vi odd, even;

bool comp(int aa, int bb) {return aa>bb;}
int main ()
{
    int n;
    i64 sum = 0;
    scanf("%d", &n);
    rep(i,1,n) scanf("%d", a+i), sum += a[i];
    rep(i,1,n) {
        scanf("%d", b+i), b[i] -= a[i];
        if (i&1) odd.pb(b[i]);
        else even.pb(b[i]);
    }
    sort(all(odd), comp);
    sort(all(even), comp);
    rep(i,0,n/2-1) {
        if (odd[i]+even[i] > 0) sum += odd[i]+even[i];
    }
    printf("%lld\n", sum);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
#define fst first
#define snd second

const int N = 1e5 + 5;
int a[N], b[N];

//ll dp[N][2][2];
//const ll INF = 1e18;

VI diff[2];

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  int n;
  cin >> n;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans += a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    b[i] -= a[i];
  }

  //  dp[0][0][1] = dp[0][1][1] = -INF;
  //  for (int i = 1; i <= n; i++) {
  //    for (int p : {0, 1}) {
  //      for (int nb : {0, 1}) {
  //        dp[i][p][nb] = dp[i - 1][p][nb];
  //      }
  //    }
  //    int p = i & 1;
  //    for (int nb : {0, 1}) {
  //      dp[i][p][nb] = max(dp[i][p][nb], dp[i - 1][1 - p][1 - nb] + b[i]);
  //    }
  //  }
  //  ans += max(dp[n][0][0], dp[n][1][0]);

  for (int i = 1; i <= n; i++) {
    diff[i & 1].push_back(b[i]);
  }
  sort(diff[0].begin(), diff[0].end());
  sort(diff[1].begin(), diff[1].end());

  while (!diff[0].empty() && !diff[1].empty()) {
    ll cur = diff[0].back() + diff[1].back();
    diff[0].pop_back();
    diff[1].pop_back();
    if (cur > 0) ans += cur;
  }

  cout << ans << endl;

  return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long int
#define inf 1000000000000
#define mod 998244353
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define print(a) printf("%d\n",a)
#define printl(a) printf("%lld\n",a)
#define prints(a) printf("%d ",a)
#define printsl(a) printf("%lld ",a)
#define ps(x,y)   fixed<<setprecision(y)<<x
using namespace std;
const int N=303;
ll C[N][N];
ll a[N],ans[N];

ll add(ll x,ll y){
    ll res=x+y;
    if(res>=mod)res-=mod;
    return res;
}
ll sub(ll x,ll y){
    ll res=x-y;
    if(res<0)res+=mod;
    return res;
}
ll mult(ll x,ll y){
  return (x*y)%mod;
}

void solve()
{
    for(int i=0;i<N;i++){
        C[i][0]=C[i][i]=1;
    }
    for(int i=1;i<N;i++){
        for(int j=1;j<i;j++){
            C[i][j]=add(C[i-1][j-1],C[i-1][j]);
        }
    }

    int n,k;
    scan(n),scan(k);
    while(n--){
        ll x;
        scanl(x);
        ll p=1,q=1;
        a[0]=add(a[0],p);
        for(int i=1;i<=k;i++){
            p=mult(x,p);
            q=mult(2*x,q);
            a[i]=add(a[i],p);
            ans[i]=sub(ans[i],q);
        }
    }
    for(int x=1;x<=k;x++){
        for(int i=0;i<=x;i++){
            ans[x]=add(ans[x],mult(C[x][i],mult(a[x-i],a[i])));
        }
    }

    for(int i=1;i<=k;i++){
        ll x=ans[i];
        if(x&1)x+=mod;
        x/=2;
        printl(x);
    }

}
int main()
{
// #ifndef ONLINE_JUDGE
//     freopen("in.txt", "r", stdin);
//     freopen("out.txt", "w", stdout);
// #endif
    int t = 1;
    //scan(t);
    while (t--) {
        solve();
    }

}

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;
//struct edge{lint to,num;};
//vector<int> bucket[MAX_N/1000];

lint powmod(lint a,lint b){
  return b?powmod(a*a%MOD,b/2)*(b%2?a:1)%MOD:1;
}

lint fac[510000],finv[510000],inv[510000];
void COMinit(){
  fac[0]=fac[1]=1;
  finv[0]=finv[1]=1;
  inv[1]=1;
  for(int i=2;i<510000;i++){
    fac[i]=fac[i-1]*i%MOD;
    inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
    finv[i]=finv[i-1]*inv[i]%MOD;
  }
}
lint COM(lint n,lint k){
  if (n<k) return 0;
  if (n<0 || k<0) return 0;
  return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
lint facdiv(lint n,lint k){
  if (n<k) return 0;
  if (n<0 || k<0) return 0;
  return (fac[n]*finv[k])%MOD;
}

int main(void){
    COMinit();
    lint div2=powmod(2,MOD-2);
    lint N,K;
    cin >> N >> K;
    lint A[N];
    rep(i,N) cin >> A[i];
    //lint sum=accumulate(A,A+N,0LL);
    //sum%=MOD;
    lint powsumk[301];
    rep(i,301){
        lint suma=0;
        rep(j,N) suma+=powmod(A[j],i);
        powsumk[i]=suma%MOD;
    }
    for(int i=1;i<=K;i++){
        lint ans=0;
        rep(j,i+1){
            lint keisu=COM(i,j);
            lint res=(powsumk[j]*powsumk[i-j]-powsumk[i]+MOD)%MOD;
            //cout << powsumk[j] << " " << powsumk[j-i] << endl;
            res=(res*div2)%MOD;
            ans=(ans+res*keisu)%MOD;
        }
        cout << ans << endl;
    }
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<complex>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l,r) l+(r-l)/2
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
int st[205],en[205];
int dp[205][205];
int rec(int l,int r){
	if(dp[l][r]!=-1)return dp[l][r];
	int l1=l,r1=(l+r)/2,d=(r-l)/2+1;
	int f=0;
	for(;l1<=r1;l1++){
		int s=st[l1];
		int e=en[l1+d];
		//cout<<s<<" "<<l1<<" "<<e<<" "<<l1+d<<"\n";
		if(s>0&&s!=l1+d){f=1;break;}
		if(e>0&&e!=l1){f=1;break;}
		if(s==-1&&e==-1){f=1;break;}
	}
	//cout<<l<<" "<<r<<" "<<f<<"\n";
	if(!f)return dp[l][r]=1;
	dp[l][r]=0;
	for(int i=l+1;i+1<r;i+=2){
		dp[l][r]|=(rec(l,i)&rec(i+1,r));
	}
	return dp[l][r];
}
void solve(){
	int n;
	cin>>n;
	int f=0;
	for(int i=0;i<n;i++){
		int s,e;
		cin>>s>>e;
		if(s!=-1){
			if(st[s])f=1;
			st[s]=e;
		}
		if(e!=-1){
			if(en[e])f=1;
			en[e]=s;
		}
	}
	if(f){cout<<"No";return;}
	for(int i=1;i<=2*n;i++){
		if(st[i]>0&&st[i]<=i){cout<<"No";return;}
		if(en[i]>0&&en[i]>=i){cout<<"No";return;}
		if(st[i]>0&&en[i]>0){cout<<"No";return;}
		if(st[i]!=0)en[i]=i;
		else if(en[i]!=0)st[i]=i;
	}
	for(int i=0;i<=2*n;i++)for(int j=0;j<=2*n;j++)dp[i][j]=-1;
	if(rec(1,2*n))cout<<"Yes";
	else cout<<"No";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string.h>
#include <assert.h>
#include <iomanip>
#include <bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define ull unsigned long long
#define F first
#define S second
#define nl "\n"
#define ld long double
#define sortv(v) sort(v.begin(),v.end())
#define rsortv(v) sort(v.rbegin(),v.rend())
#define div 1000000007
#define div2 998244353
#define PI 3.141592653589793
#define inf 1000000000000000000
#define blk 320
#define pb push_back

void solve()
{
    int n;cin>>n;
    vector<pair<int,int>> v;
    set<int> s;
    for(int i=0;i<n;i++)
    {
        int x,col;cin>>x>>col;
        --col;
        v.pb({x,col});
        s.insert(col);
    }
    int m=s.size();
    map<int,int> mp;
    int idx=0;
    for(int x:s)
        mp[x]=idx++;
    int c[m][2];
    for(int i=0;i<m;i++)
    {
        c[i][0]=inf;
        c[i][1]=-inf;
    }
    for(int i=0;i<n;i++)
    {
        int x=v[i].F,col=v[i].S;
        col=mp[col];
        c[col][0]=min(c[col][0],x);
        c[col][1]=max(c[col][1],x);
    }
    int dp[m][2];
    for(int i=0;i<m;i++)
        dp[i][0]=dp[i][1]=inf;
    if(c[0][0]<0 && c[0][1]>0)
    {
        dp[0][0]=abs(c[0][1])+abs(c[0][1]-c[0][0]);
        dp[0][1]=abs(c[0][0])+abs(c[0][0]-c[0][1]);
    }
    else
    {
        if(c[0][1]<0)
        {
            dp[0][0]=abs(c[0][0]);
            dp[0][1]=abs(c[0][0])+abs(c[0][0]-c[0][1]);
        }
        else
        {
            dp[0][1]=c[0][1];
            dp[0][0]=c[0][1]+(c[0][1]-c[0][0]);
        }
    }
    for(int i=1;i<m;++i)
    {
        int l=c[i][0],r=c[i][1];
        if(r<=c[i-1][0])
            dp[i][0]=dp[i-1][0]+abs(c[i-1][0]-l);
        else
            dp[i][0]=dp[i-1][0]+abs((r-l))+abs(r-c[i-1][0]);

        if(r<=c[i-1][1])
            dp[i][0]=min(dp[i][0],dp[i-1][1]+abs(c[i-1][1]-l));
        else
            dp[i][0]=min(dp[i][0],dp[i-1][1]+(r-l)+abs(r-c[i-1][1]));

        if(c[i-1][0]<l)
            dp[i][1]=min(dp[i][1],dp[i-1][0]+abs(c[i-1][0]-r));
        else
            dp[i][1]=min(dp[i][1],dp[i-1][0]+abs(c[i-1][0]-l)+abs(r-l));

        if(c[i-1][1]<l)
            dp[i][1]=min(dp[i][1],dp[i-1][1]+abs(c[i-1][1]-r));
        else
            dp[i][1]=min(dp[i][1],dp[i-1][1]+abs(c[i-1][1]-l)+abs(r-l));
    }
    int ans=min(dp[m-1][0]+abs(c[m-1][0]),dp[m-1][1]+abs(c[m-1][1]));
    cout<<ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
        int t=1;
        //cin>>t;
        while(t--)
        {
            solve();
        }
}


#include<bits/stdc++.h>
typedef long long ll;
ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
#define all(x) (x).begin(),(x).end()
template<class T>void cxk(T&a,T b){a=a>b?a:b;}
template<class T>void cnk(T&a,T b){a=a<b?a:b;}
int a[100010],b[100010];
std::vector<ll>A,B;
int main(){
#ifdef LOCAL
	freopen("in.in","r",stdin);
	//freopen("out.out","w",stdout);
#endif
	int n=gi();
	ll sa=0;
	for(int i=1;i<=n;++i)sa+=a[i]=gi();
	for(int i=1;i<=n;++i)b[i]=gi()-a[i];
	A.resize(n/2+1),B.resize(n/2+1);
	for(int i=1,j=1;i<=n;i+=2,++j)A[j]=b[i],B[j]=b[i+1];
	std::sort(A.begin()+1,A.end(),std::greater<ll>());
	std::sort(B.begin()+1,B.end(),std::greater<ll>());
	for(int i=1;i<=n/2;++i)A[i]+=A[i-1],B[i]+=B[i-1];
	ll ans=0;for(int i=0;i<=n/2;++i)cxk(ans,A[i]+B[i]);
	printf("%lld\n",ans+sa);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#define ll long long
#define pb push_back
#define forn(i,x,n) for(int i=x;i<=n;++i)
#define forr(i,x,n) for(int i=n;i>=x;--i)
#define lson (rt<< 1)
#define rson (rt<< 1 | 1)
#define gmid ((l+r)>> 1 )
using namespace std;

const int maxn=3050;
const int p=1e9+7;
ll a[maxn];
ll f[maxn][maxn],s[maxn][maxn],b[maxn][maxn];
int main()
{
	int n;
	cin>>n;
	forn(i,1,n)
	  cin>>a[i];
	forn(i,1,n)
	  {
	  	  s[i][0]=s[i-1][0]+a[i];
	  	  forn(j,1,n)
	  	    s[i][j]=(s[i][0]%j);
	  }
	b[0][0]=1;
	forn(i,1,n)
	  forr(j,1,i)
	    {
	    	f[i][j]=b[s[i][j]][j-1];
			b[s[i][j+1]][j]=(b[s[i][j+1]][j]+f[i][j])%p;
	    }
	ll ans=0;
	forn(i,1,n)
	  ans=(ans+f[n][i])%p;
	cout<<ans<<endl;

}

#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N=3e3+5,mod=1e9+7;
int t,a[N],pref[N],dp[N][N],n;
vector<int>c[N];
 main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		pref[i] = pref[i-1] + a[i] ;
	}
	dp[0][0] = 1;
	int ans = 0;
	for(int i=1;i<=n;i++) {
		for(int j=0;j<=n;j++) {
			c[pref[j]%i].push_back(j);
		}
		for(int j=0;j<i;j++) {
			int p = 0;
			for(int k=0;k<c[j].size();k++) {
				dp[c[j][k]][i] = p;
				p += dp[c[j][k]][i-1];
				if(p>=mod) p-=mod;
			}
			c[j].clear();
		}
		ans += dp[n][i];
		if(ans>=mod) ans -= mod;
	}
	cout<<ans;
}