#include <bits/stdc++.h>
#define ri register
#define int long long
#define E (n+1)
using namespace std; const int N=200010;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int n,a[N],b[N],c[N];
signed main()
{
    n=read();
    for(ri int i=1;i<=n;i++) a[i]=read();
    for(ri int i=1;i<=n;i++) b[i]=read();
    for(ri int i=1;i<=n;i++) c[i]=read();
    sort(a+1,a+1+n), sort(b+1,b+1+n), sort(c+1,c+1+n);
    int res=0;
    for(ri int i=1;i<=n;i++)
    {
        int p=lower_bound(a+1,a+1+n,b[i])-a;
        int q=upper_bound(c+1,c+1+n,b[i])-c;
        res+=(n-q+1)*(p-1);
    }
    printf("%lld\n",res);
    return 0;
}

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

int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n);
    for (auto &x: a) cin >> x;
    for (auto &x: b) cin >> x;
    for (auto &x: c) cin >> x;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());
    vector<long long> l(n);
    int j = 0;
    for (int i = 0; i < n; i++) {
        while (j < n && c[j] <= b[i]) j++;
        l[i] = n - j;
    }
    for (int i = n - 1; i > 0; i--) l[i-1] += l[i];
    long long ans = 0;
    j = 0;
    for (int i = 0; i < n; i++) {
        while (j < n && b[j] <= a[i]) j++;
        if (j == n) break;
        ans += l[j];
    }
    cout << ans << endl;
    return 0;
}


#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <complex>

int main(){

  // read problem
  long long int M, tmp1, tmp2;
  std::vector<long long int> d, c;

  scanf("%lld", &M);

  for(long long int i = 0; i < M; i++){
    scanf("%lld%lld", &tmp1, &tmp2);
    d.push_back(tmp1);
    c.push_back(tmp2);
  }

  /*std::cerr << "M: " << M << std::endl;
  std::cerr << "d: " << std::endl;
  for(long long int i = 0; i < M; i++){
    std::cerr << d[i] << " ";
  }std::cerr << std::endl;
  std::cerr << "c: " << std::endl;
  for(long long int i = 0; i < M; i++){
    std::cerr << c[i] << " ";
  }std::cerr << std::endl;*/

  // solve problem
  long long int carryover = 0;
  long long int topdigit, period, tmp_pos;
  long long int ans = 0;
  std::vector<long long int> tmp_digits(10);

  for(long long int i = 0; i < M; i++){
    //std::cerr << "i: " << i << " carryover: " << carryover << std::endl;
    // calculate carryover
    topdigit = d[i];
    while(carryover>0){
      ans++;
      topdigit = topdigit+carryover;
      carryover = topdigit/10;
      topdigit = (topdigit+10)%10;
    }

    //std::cerr << "carryover is done" << std::endl;

    // calculate one period
    tmp_digits[0] = topdigit;
    for(long long int j = 1; j < 10; j++){
      tmp_digits[j] = d[i];
    }
    tmp_pos = 0;
    period = 0;
    for(long long int tmp_pos = 0; tmp_pos < 9; tmp_pos++){
      while(tmp_digits[tmp_pos] > 0){
        period++;
        tmp_digits[tmp_pos+1] = tmp_digits[tmp_pos] + tmp_digits[tmp_pos+1];
        tmp_digits[tmp_pos] = tmp_digits[tmp_pos+1]/10;
        tmp_digits[tmp_pos+1] = (tmp_digits[tmp_pos+1]+10)%10;
      }
      /*for(long long int k = 0; k < 10; k++){
        std::cerr << tmp_digits[k];
      }std::cerr << std::endl;*/
    }

    ans += period*((c[i]-1)/9);
    //std::cerr << "one period calculated" << std::endl;

    // calculate amari
    tmp_pos = 9 - ((c[i]+8)%9);
    if(tmp_pos == 0){
      continue;
    }
    tmp_digits[tmp_pos] = topdigit;
    for(long long int j = tmp_pos+1; j < 10; j++){
      tmp_digits[j] = d[i];
    }
    for(long long int tmp_pos = 0; tmp_pos < 9; tmp_pos++){
      while(tmp_digits[tmp_pos] > 0){
        ans++;
        tmp_digits[tmp_pos+1] = tmp_digits[tmp_pos] + tmp_digits[tmp_pos+1];
        tmp_digits[tmp_pos] = tmp_digits[tmp_pos+1]/10;
        tmp_digits[tmp_pos+1] = (tmp_digits[tmp_pos+1]+10)%10;
      }
    }

    //std::cerr << "amari calculated" << std::endl;

    carryover = tmp_digits[9];
  }

  std::cerr << "Answer: " << std::endl;
  std::cout << ans << std::endl;

  return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define inf 2e9
#define linf 9000000000000000000ll
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int m;
ll d[200001], c[200001];
ll digitsum(ll n) {
    ll res = 0;
    while (n != 0) {
        res += n % 10;
        n /= 10;
    }
    return res;
}
int main() {
    cin >> m;
    rep(i, m) { cin >> d[i] >> c[i]; }
    ll sum = 0;
    ll dignum = 0;
    rep(i, m) {
        dignum += c[i];
        sum += d[i] * c[i];
    }
    cout << dignum - 1 + (sum - 1) / 9 << endl;
    return 0;
}


#include<iostream>
using namespace std;
int main (void)
{
  int r;
  cin>>r;
  r=r*2*3141592;
  cout<<r/1000000<<"."<<r%1000000;
}

#include<bits/stdc++.h>

using namespace std;

#define pi acos(-1)
int main(){
    int r;
    cin >> r;
    cout << fixed << setprecision(20) << (2*r*pi) << endl;
    return 0;
}


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

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

const int maxn = 505;
ll a[maxn][maxn];
int n;

const int maxm = 1e4 + 5;
bool vis[maxm];

map<int, int> zhu, fu;

ll gcd(ll x, ll y){
    return y == 0 ? x : gcd(y, x % y);
}

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

void solve(){
    memset(vis, 0, sizeof(vis));
    vector<int> prime;
    vis[1] = 1;
    for(int i = 2;i < maxm;i++){
        if(!vis[i]){
            prime.push_back(i);
            for(int j = 2 * i;j < maxm;j += i){
                vis[j] = 1;
            }
        }
    }
    int front = 0, rear = n - 1;
    for(int i = 2;i <= 2 * n;i += 2){
        int k = i / 2;
        if(k & 1){
            zhu[i] = prime[front++];
        }else{
            zhu[i] = prime[rear--];
        }
    }
    int k;
    if(n & 1)
        k = n;
    else
        k = n - 1;
    front = n, rear = n + k - 1;
    for(int i = 1 - k, odd = 1;i <= k - 1;i += 2, odd++){
        if(odd & 1){
            fu[i] = prime[front++];
        }else{
            fu[i] = prime[rear--];
        }
    }
    for(int i = 1;i <= n;i++){
        int st;
        if(i & 1){
            st = 1;
        }else{
            st = 2;
        }
        for(int j = st;j <= n;j += 2){
            int id1 = zhu[i + j];
            int id2 = fu[i - j];
            a[i][j] = 1LL * id1 * id2;
        }
    }
    for(int i = 1;i <= n;i++){
        int st;
        if(i & 1){
            st = 2;
        }else{
            st = 1;
        }
        for(int j = st;j <= n;j += 2){
            a[i][j] = 1;
            if(j > 1){
                a[i][j] = lcm(a[i][j], a[i][j - 1]);
            }
            if(j < n){
                a[i][j] = lcm(a[i][j], a[i][j + 1]);
            }
            if(i > 1){
                a[i][j] = lcm(a[i][j], a[i - 1][j]);
            }
            if(i < n){
                a[i][j] = lcm(a[i][j], a[i + 1][j]);
            }
            a[i][j]++;
        }
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            cout << a[i][j] << ((j == n) ? '\n' : ' ');
        }
    }
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    if(n == 2){
        cout << 4 << " " << 7 << endl;
        cout << 23 << " " << 10 << endl;
        return 0;
    }
    solve();
    return 0;
}

#include<bits/stdc++.h>
const int N=503;
using namespace std;
typedef long long ll;
ll a[N][N];
int n,p[N*2],tot;
bool np[N*30];
inline int F(int x){
	return p[n+x/2];
}
inline int G(int x){
	return p[(x+n/2*2)/2];
}
int main(){
	scanf("%d",&n);
	for(int i=2;tot<2*n;i++){
		if(!np[i])p[tot++]=i;
		for(int j=i*i;j<N*30;j+=i)np[j]=1;
	}
	for(int i=0;i<n;i++,puts(""))
	for(int j=0;j<n;j++)
	if(i+j&1){
		printf("%lld ",1ll*F(i+j-1)*F(i+j+1)
		*G(i-j-1)*G(i-j+1)+1);
	}else printf("%d ",F(i+j)*G(i-j));
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.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 FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#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
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
    R(N,X);
    LL need=0;
    priority_queue<PLL>h0,h1,h2;
    REP(i,N){
        R(b[i],l[i],u[i]);
        need+=b[i]*l[i];
        h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
    }
    LL an=0;
    while(need>=h0.top().F){
        an+=X;
        used[h0.top().S]=1;
        need-=h0.top().F;
        h0.pop();
    }
    if(need<=0){
        W(an);
        return 0;
    }
    int add_id=h0.top().S;
    LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
    LL mi=1e18;
    REP(i,N){
        if(used[i]){
            LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);

            if(l[i]*b[i]>=need2){
                mi=min(mi,(need2+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
        else{
            if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
            if(l[i]*b[i]>=need){
                mi=min(mi,(need+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
    }
    an+=mi;
    W(an);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
struct sj{
	int a,l,r;ll x;
	inline sj(){}
	inline sj(int a,int l,int r,ll x):a(a),l(l),r(r),x(x){}
	inline bool operator<(const sj &t)const{return x>t.x;}
}d[N];
inline void cmax(ll &a,ll b){if(a<b)a=b;}
inline ll Y(int i,ll k){
	ll a=d[i].a;
	if(k<=a)return k*d[i].l;
	else    return k*d[i].r+a*(d[i].l-d[i].r);
}
int main(){
	int n,i,a,l,r,x;
	ll L=0ll,R,M,S,g=0ll,t1,t2;
	scanf("%d%d",&n,&x);R=(S=(t1=((ll)x))*n)-1ll;
	for(i=1;i<=n;++i){
		scanf("%d%d%d",&a,&l,&r);t2=a;
		d[i]=sj(a,l,r,t1*r+t2*(l-r));
		g+=l*t2;
	}
	sort(d+1,d+n+1);
	while(L<=R){
		M=(L+R)>>1;
		l=M/x;
		r=M%x;
//		printf("M=%lld l=%d r=%d",M,l,r);
//		for(i=1;i<=n;++i)printf(" (%d,%d)",d[i].x,Y(i,r));
//		printf("\n");
		t1=-1000000000000ll;t2=0ll;
		for(i=1;i<=l;++i){
			t2+=d[i].x;
			cmax(t1,Y(i,r)-d[i].x);
		}
		t1+=d[i].x;
		for(   ;i<=n;++i)cmax(t1,Y(i,r));
		if(t1+t2>=g)R=(S=M)-1ll;else L=M+1ll;
	}
	printf("%lld",S);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct _IO{_IO(){ios::sync_with_stdio(0);cin.tie(0);}}_io;

typedef long long ll;
typedef long double db;
typedef unsigned long long ull;
typedef vector<int> vi; typedef vector<vi> vii;
typedef pair<int, int> pi;
typedef tuple<int, int, int> t3;
typedef map<int, int> mi;
#define fi first
#define se second
#define vt vector
#define be begin()
#define ed end()
#define sz size()
#define cl clear()
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define lb(v, x) lower_bound(v.begin(), v.end(), x)
#define lbi(v, x) (lb(v, x) - v.begin())
#define tp(t, x, y) t x, y; tie(x, y)
#define tp3(t, x, y, z) t x, y, z; tie(x, y, z)
#define mp make_pair
#define mt make_tuple
#define co const
#define fc(x) for(int x = 1, _end = ri(); x <= _end; x++)
void uni(vi &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); }

db rd() { db x; cin >> x; return x; }
ll ri() { ll x; cin >> x; return x; }
string rs() { string s; cin >> s; return s; }
pi rpi() { pi x; cin >> x.fi >> x.se; return x; }
vi rvi(int n, int f = 0) { vi v(n + f); for (int i = f; i < v.size(); i++) cin >> v[i]; return v; }
vii rvii(int n, int m, int f = 0) { vii v(n + f, vi(n + f)); for (int i = f; i < v.size(); i++) for (int j = f; j < v[0].size(); j++) cin >> v[i][j]; return v; }
void pr() {}; template <class T, class ...U> void pr(T x, U ...y) { cout << x << ' ', pr(y...); }
void prl() { cout << '\n'; }; template <class T, class ...U> void prl(T x, U ...y) { cout << x << ' ', prl(y...); }
void pr(const vi &v, int f = 0) { for (int i = f; i < v.size(); i++) cout << v[i] << ' '; }
void prl(const vi &v, int f = 0) { pr(v, f); cout << '\n'; }
void pr(const pi &x) { cout << x.fi << ' ' << x.se; };
void prl(const pi &x) { pr(x); cout << '\n'; }

#define rep(i, l, r) for (int i = l; i < r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)

const int N = 2e5 + 5, M = 1e9 + 7;

int fa[N];
int fi(int x) {
  return !fa[x] || fa[x] == x ? x : fa[x] = fi(fa[x]);
}

vi g[N];
map<int, vi> cnt;
bool vis[N];
void dfs(int u) {
  vis[u] = 1;
  cnt[fi(u)].pb(u);
  for (auto &v : g[u]) {
    if (!vis[v]) dfs(v);
  }
}

int main() {
  int n = ri(), a = ri(), b = ri();
  rep(i, 0, a) {
    int x = ri(), y = ri();
    fa[fi(x)] = fi(y);
  }
  rep(i, 0, b) {
    int u = ri(), v = ri();
    g[u].pb(v);
    g[v].pb(u);
  }
  vi ans(n + 1);
  rep(i, 1, n + 1) {
    if (!vis[i]) {
      cnt.clear();
      dfs(i);
      for (auto &p : cnt) {
        for (auto &v : p.se) {
          ans[v] = p.se.sz;
        }
      }
    }
  }
  pr(ans, 1);
}

#include <bits/stdc++.h>
// statics
using namespace std;
using int64 = int_fast64_t;
using PAIR = pair<int, int>;
constexpr int INF = 1 << 30;
constexpr int64 LINF = 1LL << 60;
constexpr int MOD = 1e9 + 7;
constexpr int MAX_N = 1e5 + 1;

// init/input
#define int int64
#define INIT ios::sync_with_stdio(false);cin.tie(0);
#define VAR(type, ...) type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T &t) {cin>>t;}
template<typename First, typename...Rest> void MACRO_VAR_Scan(First &first, Rest&...rest) {cin>>first;MACRO_VAR_Scan(rest...);}
#define VEC(type, c, n) vector<type> c(n);for(auto &&i:c)cin>>i;

// out
#define OUT(dist) cout<<(dist);
#define FOUT(n, dist) cout<<fixed<<setprecision(n)<<(dist);
#define SP cout<<" ";
#define BR cout<<"\n";
#define debug(x) cerr<<#x<<":"<< (x);BR;

// utility
#define ALL(a) (a).begin(), (a).end()
#define EACH(i, a) for(auto &&i:(a))
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=0;--i)
#define REP(i, n) for(int i=0;i<(n);++i)
#define RREP(i, n) for(int i=(n)-1;i>=0;--i)

struct UnionFind {
  vector< int > par;
  UnionFind(int n = 1) {
    init(n);
  }

  void init(int n = 1) {
    par.resize(n);
    for(int i = 0; i < n; i++)
      par[i] = -1;
  }

  int root(int n) {
    if (par[n] < 0) return n;
    return par[n] = root(par[n]);
  }

  bool merge(int x, int y) {
    x = root(x); y = root(y);
    if (x == y) return false;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;

    return true;
  }

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

signed main() {
  INIT;
	VAR(int, n, k, l);
	UnionFind road(n + 1);
	REP(i, k) {
		VAR(int, p, q);
		road.merge(--p, --q);
	}
	UnionFind train(n + 1);
	REP(i, l) {
		VAR(int, s, t);
		train.merge(--s, --t);
	}

	map< PAIR, int > mp;
	REP(i, n) {
		mp[make_pair(road.root(i), train.root(i))]++;
	}

	REP(i, n) {
		OUT(mp[make_pair(road.root(i), train.root(i))]);
		if (i < n - 1) SP
		else BR
	}

  return 0;
}


#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
#define show(obj) {for(auto x:obj)cout<<x<<' ';cout<<endl;}
#define line "----------"
typedef long long ll;
typedef pair<int,int> P;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const ll MOD = 1e9 + 7;

int main(){
	string s;
	cin>>s;
	ll ans = s.size()*(s.size()-1)/2+1;
	map<char,ll> cha;
	rep(i,s.size()){
		cha[s[i]]++;
	}
	for(auto x: cha){
		ll t = x.second;
		ans -= t*(t-1)/2;
	}
	cout<<ans<<endl;
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <numeric>
#include <cmath>
#include <iomanip> //cout << fixed << setprecision(15) << << endl;
#include <cassert>
//#include "atcoder/all"

using namespace std;
//using namespace atcoder;

typedef long long ll;

#define pb push_back
#define rep(i, n) for(int i=0;i<(n);i++)
#define REP(i, n) for(int i=1;i<=(n);i++)
#define P pair<int,int>
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int mx8[] = {0, 0, 1, -1, -1, 1, -1, 1};
int my8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int mx4[] = {1, -1, 0, 0};
int my4[] = {0, 0, -1, 1};

const int MOD = 1000000007;

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

    string s;
    cin >> s;
    int m=0;

    int n = s.size();
    int a = 0;
    rep(i, n) {
        a += int(s[i])-48;
        m += pow(10,n-i-1) * (int(s[i])-48);
    }

    if(m % a == 0) cout << "Yes" << endl;
    else cout << "No" << endl;
}


#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define rrep(i,n) for (int i = n-1; i >= 0; i--)
#define MOD (1000000007)
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pb push_back

#define mp make_pair
#define all(a) (a).begin(),(a).end()

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

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

ll keta(ll n){
  string s = to_string(n);
  ll num = s.size();
  return num;
}

const ll INF = 1LL << 60;

const int dh[4] = {1,0,-1,0};
const int dw[4] = {0,1,0,-1};

struct Edge{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w){}
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, int>;

class UnionFind{
public:
  vi Parent;
  UnionFind(int n){
    Parent = vi(n,-1);
  }

  int root(int a){
    if(Parent[a] < 0) return a;
    return Parent[a] = root(Parent[a]);
  }

  bool issame(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return -Parent[root(a)];
  }

  bool merge(int a, int b){
    a = root(a);
    b = root(b);

    if(a == b) return false;

    if(size(a) < size(b)) swap(a,b);
    Parent[a] += Parent[b];
    Parent[b] = a;

    return true;
  }
};

vi MP(string s){
  vi A(s.size()+1);
  A[0] = -1;
  int j = -1;
  rep(i,s.size()) {
    while(j >= 0 && s[i] != s[j]) j = A[j];
    j++;
    A[i+1] = j;
  }
  return A;
}

vi Manacher(string s){
  vi R(s.size());
  int i = 0, j = 0;
  while(i < s.size()){
    while(i-j >= 0 && i+j < s.size() && s[i-j] == s[i+j]) ++j;
    R[i] = j;
    int k = 1;
    while(i-k >= 0 && i+k < s.size() && k+R[i-k] < j) R[i+k] = R[i-k], k++;
    i += k; j -= k;
  }
  return R;
}

vi Z_algorithm(string &s){
  vi Z(s.size());
  Z[0] = s.size();
  int i = 1, j = 0;
  while(i < s.size()){
    while(i+j < s.size() && s[j] == s[i+j]) j++;
    Z[i] = j;
    if(j == 0){++i; continue;}
    int k = 1;
    while(i+k < s.size() && k+Z[k] < j) Z[i+k] = Z[k], ++k;
    i += k; j -= k;
  }
  return Z;
}

const int MAX = 1e6+1;

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

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(ll n, ll k){
  if(n >= MAX){
    ll tmp = 1;
    rep(i,k){
      tmp *= (n-i);
      tmp %= MOD;
    }
    return tmp*finv[k]%MOD;
  }

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

ll POW(ll x, ll n){
  ll ret = 1;
  if(n < 0){// n < 0 にも対応
    n *= -1;
    x = inv[x];
  }
  while(0 < n){
    if(n%2 == 0){
      x = x*x%MOD;
      n /= 2;
    }
    else{
      ret = ret*x%MOD;
      n--;
    }
  }
  return ret;
}

int main(){
  int A, B; cin >> A >> B;
  A--; B--;
  int H, W; H = W = 100;
  char a[H][W];
  rep(h,H) rep(w,W) {
    if(h < H/2) a[h][w] = '#';
    else a[h][w] = '.';
  }

  for(int h = 0; h < H; h += 2) for(int w = 0; w < W; w += 2) {
    if(h < H/2 && A > 0){
      a[h][w] = '.';
      A--;
    }
    if(h > H/2 && B > 0){
      a[h][w] = '#';
      B--;
    }
  }
  printf("%d %d\n", H, W);
  rep(h,H) rep(w,W) {
    cout << a[h][w];
    if(w == W-1) cout << endl;
  }
}

#pragma region Macros
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;
#pragma endregion

void solve() {
    int a, b;
    cin >> a >> b;
    vector<string> res(100);
    for (int i=0; i<100; i++) {
        for (int j=0; j<100; j++) {
            if (i < 50) res.at(i).push_back('.');
            else res.at(i).push_back('#');
        }
    }
    int x = 0, y = 0;
    for (int i=0; i<b-1; i++) {
        res.at(x).at(y) = '#';
        y += 2;
        if (y >= 100) {
            x += 2;
            y = 0;
        }
    }
    x = 51, y = 0;
    for (int i=0; i<a-1; i++) {
        res.at(x).at(y) = '.';
        y += 2;
        if (y >= 100) {
            x += 2;
            y = 0;
        }
    }
    cout << 100 << ' ' << 100 << '\n';
    for (string s : res) cout << s << '\n';
}

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

    solve();

    return 0;
}


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

int main(){
    vector<queue<char>> s(3);
    for(int ix=0;ix<3;ix++){
        string tmp;
        cin >> tmp;
        for(int ixx=0;ixx<tmp.size();ixx++) s.at(ix).push(tmp.at(ixx));
    }
    char card;
    int turnplayer = 0;
    while(!s.at(turnplayer).empty()){
        card = s.at(turnplayer).front();
        s.at(turnplayer).pop();
        turnplayer = card - 'a';
    }
    if(turnplayer == 0) cout << "A" << endl;
    if(turnplayer == 1) cout << "B" << endl;
    if(turnplayer == 2) cout << "C" << endl;
}

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

int main(){
  string sa,sb,sc;
  cin>>sa>>sb>>sc;
  reverse(sa.begin(),sa.end());
  reverse(sb.begin(),sb.end());
  reverse(sc.begin(),sc.end());
  queue<char> card;
  card.push(sa[sa.size()-1]);
  sa.pop_back();
  char ans;
  while(true){
    char now=card.front();
    card.pop();
    if(now=='a'){
      if(sa.empty()) {ans='A'; break;}
      card.push(sa[sa.size()-1]);
      sa.pop_back();
    }
    if(now=='b'){
      if(sb.empty()) {ans='B'; break;}
      card.push(sb[sb.size()-1]);
      sb.pop_back();
    }
    if(now=='c'){
      if(sc.empty()) {ans='C'; break;}
      card.push(sc[sc.size()-1]);
      sc.pop_back();
    }
  }
  cout<<ans<<endl;
}


#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve()
{
  int n, v1, v2;
  cin >> n;
  map<int, int> hist1;
  map<int, int> hist2;
  for(int i = 0; i < n / 2; ++i)
  {
    cin >> v1 >> v2;
    hist1[v1]++;
    hist2[v2]++;
  }

  vector<P> h1{ P(0, -1) };
  vector<P> h2{ P(0, -1) };
  for(const auto& h : hist1)
  {
    h1.emplace_back(P(h.second,  h.first));
  }
  for(const auto& h : hist2)
  {
    h2.emplace_back(P(h.second,  h.first));
  }
  sort(h1.rbegin(), h1.rend());
  sort(h2.rbegin(), h2.rend());

  ll ans = 0;
  int i1 = 0, i2 = 0;
  if (h1[0].second == h2[0].second)
  {
    if (h1[0].first == h2[0].first)
    {
      h1[1] > h2[1] ? i1++ : i2++;
    }
    else
    {
      h1[0] < h2[0] ? i1++ : i2++;
    }
  }
  ans = n - h1[i1].first - h2[i2].first;
  cout << ans;
}

int main()
{
  fastio;
  solve();

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  int max_v = 100000;
  vector<int> va(n), cnt1(max_v + 1, 0), cnt2(max_v + 1, 0);
  for (int i = 0; i < n; ++i) {
    cin >> va[i];
    if (i % 2 == 0) {
      cnt1[va[i]]++;
    } else {
      cnt2[va[i]]++;
    }
  }

  auto max2 = [](vector<int> &cnt) {
    array<int, 2> res{0, 0};
    for (int i = 0; i < cnt.size(); i++) {
      if (cnt[res[0]] < cnt[i]) {
        res[1] = res[0];
        res[0] = i;
      } else if (cnt[res[1]] < cnt[i]) {
        res[1] = i;
      }
    }

    return res;
  };

  auto max2_even = max2(cnt1), max2_odd = max2(cnt2);
  int ans;
  if (max2_even[0] != max2_odd[0]) {
    ans = n - cnt1[max2_even[0]] - cnt2[max2_odd[0]];
  } else {
    ans = n - max(cnt1[max2_even[0]] + cnt2[max2_odd[1]],
                  cnt1[max2_even[1]] + cnt2[max2_odd[0]]);
  }

  cout << ans << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

ii mer(int a, int b) {
    int op = 0;
    while (1) {
        ++op;
        if (a + b < 10)
            return mp(a+b,op);
        else
            tie(a,b) = mp((a+b)/10, (a+b)%10);
    }
}

ii mr[10][10];
ii mem[10][60];
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    for (int i = 0; i < 10; ++i)
        for (int j = 0; j < 10; ++j)
            mr[i][j] = mer(i, j);

    for (int c = 0; c < 10; ++c) {
        mem[c][0] = mp(c, 0);
        for (int i = 1; i < 60; ++i) {
            auto t = mr[mem[c][i - 1].f][mem[c][i - 1].f];
            mem[c][i] = mp(t.f, t.s + mem[c][i - 1].s * 2);
        }
    }

    int n;
    cin >> n;
    int ans = 0;
    vector <int> ar;
    for (int i = 0; i < n; ++i) {
        int d, k;
        cin >> d >> k;
        int c = d;
        --k;
        for (int j = 0; j < 60; ++j) {
            if ((k >> j) & 1) {
                ii add = mem[d][j];
                auto t = mr[c][add.f];
                ans += t.s;
                ans += add.s;
                c = t.f;
            }
        }
        ar.app(c);
    }
    int cur = ar[0];
    for (int i = 1; i < ar.size(); ++i) {
        ii t = mr[cur][ar[i]];
        cur = t.f;
        ans += t.s;
    }
    cout << ans << endl;
}


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

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

  int ans = 0;
  for ( int n = a; n <= b; n++ ) {
    string num = to_string(n);
    if ( num[0] == num[4] && num[1] == num[3] ) { ans++; }
  }

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


#include <iostream>
int main(){
    int n;
    std::cin>>n;
    std::cout<<(n-2)*180;
}

#include<iostream>
#include<stdio.h>//printf("%.*f\n",*********);
#include<vector>//s.erase(n),vector
#include<algorithm>//next_permutation
#include<set>
#include<string>//char('a'=32+'A')
                //to_string,string,substr
#include<sstream>
#include<complex>
#include<time.h>
#include<random>
#include<cmath>//min,max
#include<math.h>
#include<queue>
#include<time.h>
#include<iomanip>
#include<locale>
#include<utility>//swap
#define rt "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define rop(i,n) for(int i=1;i<=n;i++)//start==1
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define drop(i,n) for(int i=n;0<i;i--)//end==1
#define yes(ans) if(ans)cout<<"yes"<<rt;else cout<<"no"<<rt;
#define Yes(ans) if(ans)cout<<"Yes"<<rt;else cout<<"No"<<rt;
#define YES(ans) if(ans)cout<<"YES"<<rt;else cout<<"NO"<<rt;
#define Yay(ans) if(ans)cout<<"Yay!"<<rt;else cout<<":("<<rt;
#define sec(a,b,ans) if(ans)cout<<a<<rt;else cout<<b<<rt;
#define vcin(a,n) rep(i,n)cin>>a[i];//vcin(配列名),(繰り返し回数)
#define sort(s) sort(s.begin(),s.end())//昇順
#define reve(s) reverse(s.begin(),s.end())//反転
#define please return
#define AC 0
#define rapid_pleaseAC_fast cin.tie(0);ios::sync_with_stdio(false)
#define pi 3.1415926535897932384626//pi
#define nine 1000000000//10^9

using namespace std;
typedef vector<int> vint;
typedef long double ldouble;
typedef vector<string> vstr;
typedef vector<char> vchar;
typedef vector<double> vdou;
typedef vector<double> vdouble;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<char>> vvchar;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<string>> vvstr;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;

long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}

long long LCM(long long a, long long b) {
	return a * b / GCD(a, b);
}

unsigned GetDigit(unsigned num) {
	return std::to_string(num).length();
}

int tow(int n) {//2のn乗
	if (n == 0)return 1;
	int x = tow(n / 2);
	x *= x;
	if (n % 2 == 1)x *= 2;
	return x;//@domino
}

int KETA(int n) { //Sum of Each Digit
	int sum = 0;
	while (n > 0) {
		sum += n % 10; n /= 10;
	}
	return sum;
}

unsigned ND(unsigned num) {//Number of Digits
	return std::to_string(num).length();//outmax=10
}

bool KIBN(string s) {
	rep(i, s.size() / 2) {
		if (s[i] != s[s.size() - 1 - i])return false;
	}
	return true;
}

/*

 (char)toupper(a[n])=文字列のn文字目を大文字で出力

  pow(a,b)=aのb乗

  */

int main(void) {
	rapid_pleaseAC_fast;

	int n;
	cin >> n;
	cout << (n - 2) * 180 << rt;

	please AC;
}
//参考：---


#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<iterator>//集合演算(積集合,和集合,差集合など)
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int,int>;
using ll = long long;
const int INF = 100005;
const int MAX_N = 100005;

bool comp(P a, P b){
    if(a.second != b.second){
        return a.second < b.second;
    }else{
        return a.first < b.first;
    }
}

int n;
int main(){
    cin >> n;
    vector<P> a,b;
    int x,y;
    rep(i,n){
        cin >> x >> y;
        a.push_back(make_pair(x,y));
    }

    rep(i,n){
        cin >> x >> y;
        b.push_back(make_pair(x,y));
    }

    sort(a.rbegin(),a.rend());
    sort(b.begin(),b.end(),comp);

    int count =0;

    rep(i,n){
        for(int j=0; j<b.size(); j++){
            if(a[i].first < b[j].first && a[i].second < b[j].second ){
                count ++;
                b.erase(b.begin()+j);
                break;
            }
        }
    }

    cout << count << endl;

}

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

struct EdgeMF
{
	int to;
	ll capacity;
	int reverse;
};

void addEdgeForFlow(vector<vector<EdgeMF>>& G, int from, int to, ll capacity, ll reverseCapacity)
{
	G[from].push_back({ to, capacity, (int)G[to].size() });
	G[to].push_back({ from, reverseCapacity, (int)G[from].size() - 1 });
}

ll dfsForFlow(vector<vector<EdgeMF>>& G, vector<bool>& used, int u, int t, ll f)
{
	if (u == t)
	{
		return f;
	}

	used[u] = true;
	for (auto& e : G[u])
	{
		if (used[e.to] || e.capacity <= 0)
		{
			continue;
		}

		auto d = dfsForFlow(G, used, e.to, t, min(f, e.capacity));
		if (d > 0)
		{
			e.capacity -= d;
			G[e.to][e.reverse].capacity += d;
			return d;
		}
	}

	return 0;
}

ll calcMaxFlow(vector<vector<EdgeMF>>& G, int s, int t)
{
	auto f = 0ll;
	while (true)
	{
		vector<bool> used((int)G.size());
		auto d = dfsForFlow(G, used, s, t, LLONG_MAX);
		if (d <= 0)
		{
			break;
		}
		f += d;
	}

	return f;
}

int main()
{
	int N;
	cin >> N;
	vector<int> x(2 * N);
	vector<int> y(2 * N);
	for (auto i = 0; i < 2 * N; ++i)
	{
		cin >> x[i] >> y[i];
	}

	vector<vector<EdgeMF>> G(2 * N + 2);
	for (auto i = 0; i < N; ++i)
	{
		for (auto j = N; j < 2 * N; ++j)
		{
			if (x[i] < x[j] && y[i] < y[j])
			{
				addEdgeForFlow(G, i, j, 1, 0);
			}
		}
	}
	int s = 2 * N;
	int t = s + 1;
	for (auto i = 0; i < N; ++i)
	{
		addEdgeForFlow(G, s, i, 1, 0);
		addEdgeForFlow(G, i + N, t, 1, 0);
	}

	cout << calcMaxFlow(G, s, t) << endl;

	return 0;
}


/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

#define MOD 1000000007

void solve(int test_case) {//239///F.Sugoroku
	int n,k;cin>>n>>k;
	if(n==k)cout<<2*n;
	else cout<<2*max(n,k)-1;
}

int main() {

	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////

	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}

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

int main() {
  int A,B;
  cin >> A >> B;
  if(A>B){
    cout << A+max(A-1,B) << endl;
    return 0;
  }
  cout << B+max(B-1,A) << endl;
}


#include <iostream>
#include <iomanip>
#include<math.h>
#include<list>
#include <algorithm>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<string>
#include <numeric>
#include <queue>
#include <sstream>
#include <bitset>
#include<stack>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;
const long long ll_max = 9223372036854775807;
const long long ll_min = -9223372036854775807;
const int int_max = 2147483647;

template<typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T>
map<T, T> getPrimeFactor(T n) {
	map<T, T> res;
	for (T i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			res[i] ++;
			n /= i;
		}
	}
	if (n != 1) res[n] = 1;
	return res;
}

template<typename T>
bool IsPrimeNumber(T num)
{
	if (num <= 2) return true;
	else if (num % 2 == 0) return false;

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

	return true;
}

long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
	if (p == 0 || n == 1) {
		return 1;
	}
	else {
		ll ret = pow2(n * n % mod, p / 2, mod);
		if (p % 2 == 1) {
			ret *= n;
		}
		return ret % mod;
	}
}

#define rep(i,s,e) for(ll i=s;i<e;i++)
#define repeq(i,s,e) for(ll i=s;i<=e;i++)

int main() {
	int N;
	cin >> N;
	vll num(4);
	ll d = 10;
	rep(i, 0, 4) {
		num[i] = N % d;
		N -= num[i];
		num[i] *= 10;
		num[i] /= d;
		d *= 10;
	}

	rep(i, 0, (1LL << 3)) {
		ll tmp = num[3];
		rep(j, 0, 3) {
			if (i & (1LL << j)) {
				tmp += num[j];
			}
			else {
				tmp -= num[j];
			}
		}
		if (tmp == 7) {
			cout << num[3];
			for (ll j = 2; j >= 0; j--) {
				if (i & (1LL << j)) {
					cout << '+';
				}
				else {
					cout << '-';
				}
				cout << num[j];
			}
			cout << "=7" << endl;
			return 0;
		}
	}

	return 0;

}

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int R;
	scanf("%d",&R);
	printf("%f",2*R*acos(-1.0));
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;

string res;

int main()
{
    ll a, b;
    cin >> a >> b;

    println(2*b - a);
}

#include<stdio.h>

int main(){

	char word1[105];
	char word2[105];

	scanf ("%s %s", &word1, &word2);
	printf("%s%s\n",word2,word1);

	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define all(i,n) (i),(i+n)

int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
int dx8[8]={1,0,-1,1,-1,1,0,-1};
int dy8[8]={1,1,1,0,0,-1,-1,-1};
int dx9[9]={0,1,0,-1,1,-1,1,0,-1};
int dy9[9]={0,1,1,1,0,0,-1,-1,-1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;
// << fixed << setprecision
// --------------------------------------
int n;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  ll ans = 1;
  int c[3];
  REP(i, 3) c[i] = 0;
  REP(i, n) {
    int a;
    cin >> a;
    int t = 0, p = 0;
    REP(j, 3) if(c[j] == a) {
      t++; p = j;
    }

    ans = (ans * t) % mod;
    c[p]++;
  }

  cout << ans << endl;
}

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <deque>
#include <array>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define range(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
#define all(x) x.begin(), x.end()
#define PI 3.1415926536

using namespace std;

const int MAX_N = 5000;
array<int, MAX_N + 1> bit;
int sets, n;

int sum(int i){
  int ret = 0;
  while(i > 0){
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

void add(int i, int x){
  while(i <= n){
    bit[i] += x;
    i += i & -i;
  }
}

void solve(){
  cin >> n;
  if(n == 0) exit(0);
  bit.fill(0);
  for (int i = 1; i <= n; ++i) {
    int t; cin >> t;
    add(i, t);
  }

  int ans = -1e9;
  for (int i = 0; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      ans = max(ans, sum(j) - sum(i));
    }
  }
  cout << ans << endl;
}

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

  for(;;) solve();

  return 0;
}


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

using ll = long long;
#define REP(i,n) for(int i = 0;i < n;i++)

/*
*	verified : http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=4713284#1
*	last revised : 29.July.2020
 */
template<class T> class BIT {
private:
	int n;
	vector<T> dat;//[1,n]
public:
	BIT(int maxn, int _n) {
		dat.assign(maxn + 1, 0);
		n = _n;
	}
	T sum(int i) {
		T res = 0;
		while (i > 0) {
			res += dat[i];
			i -= i & -i;
		}
		return res;
	}
	void add(int i, T x) {
		while (i <= n) {
			dat[i] += x;
			i += i & -i;
		}
	}
};
#define maxn 500050
#define F first
#define S second
struct range{
  int l,r,id;
  range(){}
  range(int l,int r,int id):l(l),r(r),id(id){}
};
bool comp(range& a,range& b){
  return a.r < b.r;
}
int main(){
  cin.tie(0);ios::sync_with_stdio(false);
  int N,Q;cin >> N >> Q;
  vector<int> c(N+1);
  for(int i=1;i<=N;i++)cin>>c[i];
  vector<range> a(Q);
  for(int i=0;i<Q;i++){
    int l,r;cin >> l >> r;
    a[i] = range(l,r,i);
  }
  sort(a.begin(),a.end(),comp);
  BIT<int> bit(maxn,maxn);
  vector<int> good(maxn,0);
  vector<int> ans(Q,0);
  int i = 0,j = 1;
  for(;i < Q;i++){
    for(;j <= a[i].r;j++){
      if(good[c[j]] == 0){
        bit.add(j,1);
      }else{
        bit.add(good[c[j]],-1);
        bit.add(j,1);
      }
      good[c[j]] = j;
    }
    if(a[i].l == 1) ans[a[i].id] = bit.sum(a[i].r);
    else ans[a[i].id] = bit.sum(a[i].r) - bit.sum(a[i].l-1);
  }
  for(int i=0;i<Q;i++)cout << ans[i] << '\n';
}

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
double pi = 3.1415926535897932384646433;
typedef long long ll;
typedef pair<ll, ll>pii;
ll ccw(pii a, pii b, pii c)
{
  return (b.first - a.first)*(c.second - b.second) - (b.second - a.second)*(c.first - b.first);
}
double dist(pii a, pii b)
{
  return sqrt(double((b.first - a.first)*(b.first - a.first) + (b.second - a.second)*(b.second - a.second)));
}
double dot(pii a, pii b)
{
  return double(a.first*b.first + a.second*b.second);
}
double ans[111];
int main()
{
  fill(ans, ans + 111, 0);
  int num;
  scanf("%d", &num);
  vector<pii>v;
  map<pii, int>ma;
  for (int i = 0; i < num; i++)
  {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    v.push_back(make_pair(a, b));
    ma[make_pair(a, b)] = i;
  }
  sort(v.begin(), v.end());
  vector<pii>v1, v2;
  for (int i = 0; i < num; i++)
  {
    for (;;)
    {
      if (v1.size() <= 1)break;
      pii a = v1[v1.size() - 2], b = v1[v1.size() - 1];
      if (ccw(a, b, v[i])<0)v1.pop_back();
      else break;
    }
    v1.push_back(v[i]);
  }
  for (int i = 0; i < num; i++)
  {
    for (;;)
    {
      if (v2.size() <= 1)break;
      pii a = v2[v2.size() - 2], b = v2[v2.size() - 1];
      if (ccw(a, b, v[i])>0)v2.pop_back();
      else break;
    }
    v2.push_back(v[i]);
  }
  for (int i = int(v2.size()) - 2; i >= 1; i--)v1.push_back(v2[i]);
  for (int i = 0; i < v1.size(); i++)
  {
    pii s = v1[(i + v1.size() - 1) % v1.size()];
    pii t = v1[i];
    pii u = v1[(i + 1) % v1.size()];
    //printf("%lld %lld  %lld %lld  %lld %lld\n", s.first, s.second, t.first, t.second, u.first, u.second);
    double x = acos(dot(make_pair(u.first - t.first, u.second - t.second), make_pair(s.first - t.first, s.second - t.second)) / dist(s, t) / dist(t, u));
    ans[ma[v1[i]]] = (pi - x) / (2 * pi);
  }
  for (int i = 0; i < num; i++)printf("%.6lf\n", ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
    return '"' + s + '"';
}

string to_string(const char* s) {
    return to_string((string) s);
}

string to_string(bool b) {
    return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(v[i]);
    }
    res += "}";
    return res;
}

template <size_t N>
string to_string(bitset<N> v) {
    string res = "";
    for (size_t i = 0; i < N; i++) {
        res += static_cast<char>('0' + v[i]);
    }
    return res;
}

template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
    long long int n, k; cin >> n >> k;
    long long ans = k;
    long long t; cin >> t;
    for (int i = 1; i < n; i++) {
        long long x; cin >> x;
        ans += min(k, (x - t));
        t = x;
    }
    cout << ans;
}

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

int ts[200010];

int main(){
	int n, t;
	scanf("%d%d", &n, &t);
	for(int i = 0; i < n; i++)
		scanf("%d", &ts[i]);
	long ans = t;
	for(int i = 0; i + 1 < n; i++) {
		int time = ts[i+1] - ts[i];
		ans += (time > t) ? t : time;
	}
	printf("%ld\n", ans);
}


#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
#include<bitset>
#include<cstdlib>
#include<set>
#include<unordered_map>
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define low(x) (x&(-x))
#define IL inline
#define reg register
#define LL long long
#define N 500010
#define MOD 998244353
#define MO 1000000007
#define INF 0x3f3f3f3f
using namespace std;

int T,n,m,i,j,k,y,z,pos,l,r;
LL Ans,x;
int a[N],b[N];
char c[N];

IL LL Min(LL a,LL b){return (a<b)?a:b;}
IL LL Max(LL a,LL b){return (a>b)?a:b;}

IL int read(){
	int p=0,f=1;	char	c=getchar();
	while (c<48||c>57)	{if (c=='-')	f=-1;	c=getchar();}
	while (c>=48&&c<=57)	p=(p<<1)+(p<<3)+c-48,c=getchar();
	return p*f;
}

struct Segtree{
	struct Tree{
		int l,r;
		LL d,lz;
	}t[N*4];

	IL void Maketree(int x,int low,int high){
		int mid=(low+high)>>1;
		t[x].l=low;	t[x].r=high;
		if (low==high){t[x].d=a[low];	return;}
		Maketree(l(x),low,mid);	Maketree(r(x),mid+1,high);
		t[x].d=Min(t[l(x)].d,t[r(x)].d);
	}

	IL void Push(int x){
		LL p=t[x].lz;
		t[l(x)].d+=p;	t[r(x)].d+=p;
		t[l(x)].lz+=p;	t[r(x)].lz+=p;
		t[x].lz=0;
	}

	IL void Add(int x,int low,int high,LL val){
		reg int mid=(t[x].l+t[x].r)>>1;
		if (t[x].l==low && t[x].r==high){
			t[x].d+=val;	t[x].lz+=val;
			return;
		}
		if (t[x].lz) Push(x);
		if (high<=mid)	Add(l(x),low,high,val);
		else if (low>mid)	Add(r(x),low,high,val);
		else Add(l(x),low,mid,val),Add(r(x),mid+1,high,val);
		t[x].d=Min(t[l(x)].d,t[r(x)].d);
	}

	IL LL Query(int x,int low,int high){
		reg int mid=(t[x].l+t[x].r)>>1;
		if (t[x].l==low && t[x].r==high)	return t[x].d;
		if (t[x].lz) Push(x);
		if (high<=mid)	return Query(l(x),low,high);
		else if (low>mid)	return Query(r(x),low,high);
		else return Min(Query(l(x),low,mid),Query(r(x),mid+1,high));
	}
}T1,T2;

struct Tree{
	int l,r;
	LL d,lz;
}t[N*4];

int main(){
	#ifdef __Marvolo
	freopen("zht.in","r",stdin);
	freopen("zht.out","w",stdout);
	#endif
	n=read();	m=read();
	T1.Maketree(1,1,m);
	for (i=1;i<=m;i++)	a[i]=-i;
	T2.Maketree(1,1,m);
	for (i=1;i<=n;i++){
		l=read();	r=read();

		if (r<m){
			Ans=T2.Query(1,1,r);	x=T1.Query(1,r+1,r+1);
			if (Ans+r+1<x)	T1.Add(1,r+1,r+1,Ans+r+1-x),T2.Add(1,r+1,r+1,Ans+r+1-x);
		}

		T1.Add(1,l,r,INF);	T2.Add(1,l,r,INF);
		T1.Add(1,1,m,1);	T2.Add(1,1,m,1);

		Ans=T1.Query(1,1,m);
		if (Ans>=INF)	puts("-1");
		else printf("%lld\n",Ans);
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int vis[1010][1010];
inline vector<pair<int,int> > getdistant(int n,int D1,bool hasret=true)
{
	int cnt[2]={0,0};
	vector<pair<int,int> > ret,trans;
	for(int i=0;i<n;i++)
	{
		int v=round(sqrt(D1-i*i));
		if(v*v+i*i==D1)trans.push_back(make_pair(i,v)),trans.push_back(make_pair(i,-v)),trans.push_back(make_pair(-i,v)),trans.push_back(make_pair(-i,-v));
	}
	memset(vis,255,sizeof(vis));
	queue<pair<int,int> > q;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(vis[i][j]==-1)
			{
				// puts("----");
				q.push(make_pair(i,j));
				vis[i][j]=0;
				while(!q.empty())
				{
					pair<int,int> fr=q.front();q.pop();
					// printf("%d %d\n",fr.first,fr.second);
					for(auto &x:trans)
					{
						int nx=fr.first+x.first,ny=fr.second+x.second;
						if(nx>=0&&nx<n&&ny>=0&&ny<n&&vis[nx][ny]==-1)
						{
							vis[nx][ny]=vis[fr.first][fr.second]^1;
							q.push(make_pair(nx,ny));
						}
					}
				}
			}
		}
	}
	if(!hasret)return ret;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cnt[vis[i][j]]++;
		}
	}
	if(cnt[0]>cnt[1])
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(vis[i][j]==0)ret.push_back(make_pair(i,j));
			}
		}
	}
	else
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(vis[i][j]==1)ret.push_back(make_pair(i,j));
			}
		}
	}
	return ret;
}
int vis2[1010][1010];
inline vector<pair<int,int> > solve(int n,int d1,int d2)
{
	vector<pair<int,int> > ret;
	if(n==1)
	{
		ret.push_back(make_pair(0,0));
		return ret;
	}
	if(d1%4&&d2%4)
	{
		for(int i=0;i<n;i+=2)
		{
			for(int j=0;j<n;j+=2)
			{
				ret.push_back(make_pair(i,j));
			}
		}
		return ret;
	}
	if(d2%4==0)swap(d1,d2);
	if(d2%4)
	{
		auto p=getdistant(n/2,d1/4);
		if(d2%4==2)
		{
			for(auto &x:p)
			{
				ret.push_back(make_pair(x.first*2,x.second*2));
				ret.push_back(make_pair(x.first*2,x.second*2+1));
			}
		}
		else
		{
			for(auto &x:p)
			{
				ret.push_back(make_pair(x.first*2,x.second*2));
				ret.push_back(make_pair(x.first*2+1,x.second*2+1));
			}
		}
		return ret;
	}
	else
	{
		getdistant(n,d2,0);
		memcpy(vis2,vis,sizeof(vis));
		getdistant(n,d1,0);
		int cnt[4]={0,0,0,0};
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cnt[vis[i][j]*2+vis2[i][j]]++;
			}
		}
		int mx=0;
		for(int i=1;i<=3;i++)if(cnt[i]>cnt[mx])mx=i;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(vis[i][j]*2+vis2[i][j]==mx)ret.push_back(make_pair(i,j));
			}
		}
		return ret;
	}
}
int n,D1,D2;
int main()
{
	scanf("%d%d%d",&n,&D1,&D2);
	auto ans=solve(n<<1,D1,D2);
	ans.resize(n*n);
	for(auto &p:ans)printf("%d %d\n",p.first,p.second);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

template<class T>
map<T,T> fact(T n){
    map<T,T> Map;
    for(T i = 2; i * i <= n; i++){
        while(n % i == 0){
            Map[i]++;
            n /= i;
        }
    }
    if(n == 1)return Map;
    Map[n]++;
    return Map;
}

int main()
{
    ll a, b;
    cin >> a >> b;
    ll gc = gcd(a, b);
    cout << fact(gc).size() + 1 << endl;
}


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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int H, W;

PII solve() {
    PII res;
    PII min_dh(INF, INF);

    PII this_dh(H * H + W * W, H);

    // x > y
    for (int y = 1; y <= 150; y++) {
        for (int x = y + 1; x <= 150; x++) {
            PII dh(x * x + y * y, y);
            if (this_dh < dh && dh < min_dh) {
                min_dh = dh;
                res = MP(y, x);
            }
        }
    }

    return res;
}

int main(void) {
    while (cin >> H >> W, H) {
        PII ans = solve();
        cout << ans.fst << " " << ans.snd << endl;
    }

	return 0;
}

#include <vector>
#include <algorithm>
#include <iostream>
#include <climits>
using namespace std;
typedef long long int int64;

vector<int64> P,Q;
int64 N,M,T;

int main(void)
{
	while(true)
	{
		N = 0;
		M = 0;
		P.clear();
		Q.clear();
		P.shrink_to_fit();
		Q.shrink_to_fit();
		cin >> N >> M;
		if(N == 0 && M == 0)
		{
			exit(0);
		}
		for(int i = 0;i < N;i++)
		{
			cin >> T;
			P.push_back(T);
		}
		P.push_back(0);
		for(auto itr = P.begin();itr < P.end();itr++)
		{
			for(auto itr2 = P.begin();itr2 < P.end();itr2++)
			{
				Q.push_back(*itr + *itr2);
			}
		}
		Q.push_back(LLONG_MAX / 2);
		Q.push_back(LLONG_MIN / 2);
		sort(Q.begin(),Q.end());
		int64 ms = 0;
		for(auto itr = Q.begin();itr < Q.end()+1;itr++)
		{
			ms = max(ms,*itr + *(lower_bound(Q.begin(),Q.end() - 1,M-*itr+1) - 1));
		}
		cout << ms << endl;
	}
}


#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 200200;
typedef long long ll;
const int mod = 998244353;
inline ll pow(ll a,int b,int ans = 1) {
	for(;b;b >>= 1,a = a * a % mod) if(b & 1)
		ans = ans * a % mod;
	return ans;
}
inline ll inverse(int x){ return pow(x, mod - 2); }
int n, a[maxn];
inline bool get() {
	int s = 0, one = 0;
	rep(i, 1, n) s += a[i] % 2, one = one || a[i] == 1;
	if(s != 1 || one) return n - s & 1;
	int gcd = 0;
	for(int i = 1;i <= n;++i) gcd = std::__gcd(gcd, a[i] -= a[i] % 2);
	for(int i = 1;i <= n;++i) a[i] /= gcd;
	return get() ^ 1;
}
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	rep(i, 1, n) cin >> a[i];
	cout << (get() ? "First" : "Second") << '\n';
}

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

const ll INF = 1e18;
ll N;
ll X[200000], Y[200000];
char U[200000];

ll get_min(char first, char second, ll sign) {
    map<ll, set<ll>> m;
    REP(i, N) {
        if (U[i] != first) continue;
        ll line = Y[i] + sign * X[i];
        if (m.count(line) == 0) {
            set<ll> set {Y[i]};
            m[line] = set;
        } else {
            m[line].insert(Y[i]);
        }
    }

    ll ret = INF;
    REP(i, N) {
        if (U[i] != second) continue;
        ll line = Y[i] + sign * X[i];
        if (m.count(line) == 0) continue;
        auto it = m[line].lower_bound(Y[i]);
        if (it != m[line].begin()) {
            it--;
            ret = min(ret, (Y[i] - *it) * 10);
        }
    }
    return ret;
}

ll get_min_same_line() {
    ll ret = INF;
    map<ll, set<ll>> m;
    REP(i, N) {
        if (U[i] != 'U') continue;
        if (m.count(X[i]) == 0) {
            set<ll> set {Y[i]};
            m[X[i]] = set;
        } else {
            m[X[i]].insert(Y[i]);
        }
    }
    REP(i, N) {
        if (U[i] != 'D' || m.count(X[i]) == 0) continue;
        auto it = m[X[i]].lower_bound(Y[i]);
        if (it != m[X[i]].begin()) {
            it--;
            ret = min(ret, (Y[i] - *it) * 5);
        }
    }

    m.clear();
    REP(i, N) {
        if (U[i] != 'R') continue;
        if (m.count(Y[i]) == 0) {
            set<ll> set {X[i]};
            m[Y[i]] = set;
        } else {
            m[Y[i]].insert(X[i]);
        }
    }
    REP(i, N) {
        if (U[i] != 'L' || m.count(Y[i]) == 0) continue;
        auto it = m[Y[i]].lower_bound(X[i]);
        if (it != m[Y[i]].begin()) {
            it--;
            ret = min(ret, (X[i] - *it) * 5);
        }
    }
    return ret;
}

int main() {
    cin >> N;
    REP(i, N) cin >> X[i] >> Y[i] >> U[i];
    ll ans = INF;
    ans = min(ans, get_min('R', 'D', -1));
    ans = min(ans, get_min('U', 'L', -1));
    ans = min(ans, get_min('L', 'D', 1));
    ans = min(ans, get_min('U', 'R', 1));
    ans = min(ans, get_min_same_line());
    if (ans == INF) cout << "SAFE" << endl;
    else cout << ans << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    long long N;
    cin >> N;
    vector<long long> a(5);
    for(int i = 0; i < 5; i++) cin >> a[i];
    long long b = 1e17;
    for(int i = 0; i < 5; i++) b = min(b, a[i]);
    long long t = (N + b - 1) / b;
    long long ans = 5 + t - 1;
    cout << ans << endl;
}

#include <bits/stdc++.h>

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

using namespace std;

int main() {
  in(ll, n);
  vector<ll> transportation(5);
  rep(i, 5) cin >> transportation[i];
  vector<ll> population(6);
  population[0] = n;
  ll ans = 0;
  ll last_change = 0;
  while (population[5] < n) {
    ++ans;
    vector<ll> change(6);
    rep(i, 5) {
      ll c = min(transportation[i], population[i]);
      change[i] -= c;
      change[i + 1] += c;
    }
    rep(i, 6) population[i] += change[i];
    if (last_change > 0 && change[5] == last_change) {
      out(ans + (n - population[5] + last_change - 1) / last_change);
      return 0;
    }
    last_change = change[5];
  }
  out(ans);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define REPL(i,m,n) for(ll i=(ll)(m); i<(ll)(n); i++)
#define repl(i,n) REPL(i,0,n)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  rep(q, Q) {
    int k;
    cin >> k;
    ll cntD = 0, cntM = 0;
    ll cnt = 0;
    ll ans = 0;
    rep(i, N) {
      if(S[i] == 'D') {
        cntD++;
      } else if(S[i] == 'M') {
        cntM++;
        cnt += cntD;
      } else if(S[i] == 'C') {
        ans += cnt;
      }

      if(i >= k-1) {
        if(S[i-k+1] == 'D') {
          cntD--;
          cnt -= cntM;
        } else if(S[i-k+1] == 'M') {
          cntM--;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < n ; i++)

int main(){
    int n,K;

    while(cin >> n >> K , n + K){
        map<string,int> m;
        string str[11],in;
        rep(i,n){
            cin >> str[i];
        }
        int cnt = 0;
        if(K == 2){
            rep(i,n){
                rep(j,n){
                    if(i == j) continue;
                    in = str[i]+str[j];
                    if(!m[in]){
                        m[in]++;
                        cnt++;
                    }
                }
            }
        }
        else if(K == 3){
            rep(i,n){
                rep(j,n){
                    if(i == j) continue;
                    rep(k,n){
                        if(i == k ||j == k) continue;
                        in = str[i]+str[j]+str[k];
                        if(!m[in]){
                            m[in]++;
                            cnt++;
                        }
                    }
                }
            }
        }
        else{
            rep(i,n){
               rep(j,n){
                   if(i == j) continue;
                    rep(k,n){
                        if(i == k || j == k) continue;
                        rep(l,n){
                            if(i == l || j == l || k == l) continue;
                            in = str[i]+str[j]+str[k]+str[l];
                            if(!m[in]){
                                m[in]++;
                                cnt++;
                            }
                        }
                    }
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}


// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef long long ll;

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

int main() {
	ll k=read(); int n=50;
	ll c=k/n,r=k%n;
	printf("%d\n",n);
	for (int i=1;i<=n;++i) {
		if (i<=r) printf("%lld ",n-1+c+n-(r-1));
		else printf("%lld ",n-1+c-r);
	}
	return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main(){
    long long K;
    while(cin >> K){
        vector<long long> res(50, 49+(K+49)/50);
        long long n = (K+49)/50*50;
        while(n > K){
            int mIdx = 0;
            for(int i=1;i<50;i++) if(res[mIdx] < res[i]) mIdx = i;
            for(int i=0;i<50;i++){
                if(i == mIdx) res[i] -= 50;
                else res[i]++;
            }
            --n;
        }
        cout << 50 << endl;
        cout << res[0];
        for(int i=1;i<50;i++) cout << " " << res[i];
        cout << endl;
    }
}


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

const int N = 5005;
const long long inf = 1e18;

int n, H[N], P[N];
int a[N];
long long f[N][N];

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> H[i] >> P[i], a[i] = i;
	sort(a + 1, a + n + 1, [&](int x, int y) {
		return H[x] + P[x] < H[y] + P[y];
	});

	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) f[i][j] = inf;
	}

	f[0][0] = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j <= i; ++j) {
			f[i][j] = f[i-1][j];
			if (j > 0 && H[a[i]] >= f[i-1][j-1]) {
				f[i][j] = min(f[i][j], f[i-1][j-1] + P[a[i]]);
			}
		}
	}

	for (int i = n; i >= 0; --i) if (f[n][i] != inf) {
		cout << i << endl; return 0;
	}
}

#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=mod*mod*3LL;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int N = 100001;
vector<vector<int> > fat(N,vector<int> (20,-1));

int par(int now,int n){
	for(int i=18;i>=0;i--){
		if(now == -1) continue;
		if(n>>i&1) now = fat[now][i];
	}
	return now;
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n;
    int x[n];
    for(int i=0;i<n;i++) cin >> x[i];

    int L;
    cin >> L;
    for(int i=0;i<n;i++) fat[i][0] = upper_bound(x,x+n,x[i]+L) - x - 1;
    for(int i=0;i<18;i++)for(int j=0;j<n;j++) fat[j][i+1] = fat[fat[j][i]][i];

    cin >> q;
    vector<int> ans;
    while(q--){
    	int a, b;
    	cin >> a >> b;
    	a--;b--;
    	if(a > b)swap(a,b);
    	int le = 0, ri = n, mid;
    	while(ri - le > 1){
    		mid = (ri + le) / 2;
    		int p = par(a, mid);
    		if(p >= b) ri = mid;
    		else le = mid;
    	}
    	ans.pb(ri);
    }
    for(auto& i:ans){
    	cout << i << endl;
    }
    return 0;
}


#include <iostream>
#include <string>

//Hoshino search and replace Hoshina
void replace_word(std::string s){
	const std::string find_word("Hoshino");
	//Hoshino search
	std::string::size_type pos = s.find(find_word);
	while(pos != std::string::npos){
		//Hoshino replace Hoshina
		s.replace(pos, find_word.length(), "Hoshina");
		pos = s.find(find_word, pos + find_word.length());
	}
	//output
	std::cout << s << std::endl;
	return;
}

int main(){
	std::string s;
	int num;
	std::cin >> num; //data set
	std::getline(std::cin, s); //read a new line
	for(int i=0; i<num; ++i){
		//read text
		std::getline(std::cin, s);
		//replace
		replace_word(s);
	}
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define MOD 998244353
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

inline int fastpow(int bas, int ex){
	register int res = 1;
	for(; ex; ex >>= 1, bas = bas * (ll)bas % MOD) if(ex & 1) res = res * (ll)bas % MOD;
	return res;
}

inline int kazu(int a, const int &b) {return (a += b) >= MOD ? a - MOD : a;}
inline int hiku(int a, const int &b) {return (a -= b) < 0 ? a + MOD : a;}

int N, sum = 0, f[90005], g[90005];

int main(){
	getint(N), f[0] = g[0] = 1;
	for(register int i = 1, a; i <= N; i++){
		getint(a), sum += a;
		for(register int j = sum; j >= 0; j--){
			f[j] = f[j] * 2LL % MOD;
			if(j >= a) f[j] = kazu(f[j], f[j - a]), g[j] = kazu(g[j], g[j - a]);
		}
	}
	int ans = fastpow(3, N);
	for(register int i = sum + 1 >> 1; i <= sum; i++) ans = hiku(ans, f[i] * 3LL % MOD);
	if(!(sum & 1)) ans = kazu(ans, g[sum >> 1] * 3LL % MOD);
	return printf("%d\n", ans), 0;
}


/*  /$$                                     /$$
      | $$                                    | $$
  /$$$$$$$  /$$$$$$   /$$$$$$$  /$$$$$$   /$$$$$$$  /$$$$$$   /$$$$$$
 /$$__  $$ /$$__  $$ /$$_____/ /$$__  $$ /$$__  $$ /$$__  $$ /$$__  $$
| $$  | $$| $$$$$$$$| $$      | $$  \ $$| $$  | $$| $$$$$$$$| $$  \__/
| $$  | $$| $$_____/| $$      | $$  | $$| $$  | $$| $$_____/| $$
|  $$$$$$$|  $$$$$$$|  $$$$$$$|  $$$$$$/|  $$$$$$$|  $$$$$$$| $$
 \_______/ \_______/ \_______/ \______/  \_______/ \_______/|__//$$$$$$ /$$$$$$
                                                               |______/|______/

*/
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <unordered_map>
#define fastio        ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ln            cout << endl;
#define vi            vector<int>
#define vll           vector<long long>
#define sortl(vec)    sort(vec.begin(), vec.end());
#define sortr(vec)    sort(vec.rbegin(), vec.rend());
#define forn(i, x, n) for(long long i = x; i < n; i++)
#define revn(i, n, x) for(long long i = n; i >= x; i--)
#define in(vec)       for(auto &it : vec) cin >> it;
#define loop(vec)     for(auto &it : vec)
#define	out(vec)      for(auto &it : vec) cout << it << " "; ln
#define ll            long long
#define mod           1000000007
#define pb            emplace_back
#define mp            make_pair
#define um            unordered_map
#define pii           pair<int, int>
#define pll           pair<ll, ll>
#define f             first
#define s             second
#define pi            3.14159265359
#define o(x)		  cout << x << endl;
#define CLOCK         cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
using namespace std;

ll bS(ll l, ll r, ll x, vll arr) {
	while (l <= r) {
		ll m = l + (r - l) / 2;

		if (arr[m] == x)
			return m;

		if (arr[m] < x)
			l = m + 1;

		else
			r = m - 1;
	}

	return -1;
}

void primeFactors(ll n) {
	while (n % 2 == 0) {
		//cout << 2 << " ";
		n = n / 2;
	}

	for (ll i = 3; i <= sqrt(n); i += 2) {
		while (n % i == 0) {
			//cout << i << " ";
			n = n / i;
		}
	}

	if (n > 2)
		cout << n << " ";
}

ll power(ll x, ll y, ll p) {
	ll res = 1;
	x = x % p;

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

		y = y >> 1;
		x = (x * x) % p;
	}
	return res;
}

ll modulo(ll a, ll b) {
	ll c = a % b;
	return (c < 0) ? c + b : c;
}

int main() {
// in(vec) works for size-defined type vectors

#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input1.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output1.txt", "w", stdout);
#endif

	fastio
	ll t = 1;
	while (t--) {
		ll n;
		cin >> n;
		vll vec(n);
		in(vec);
		vll count(60, 0);

		forn(i, 0, n) {
			forn(j, 0, 60) {
				if (vec[i] & 1LL << j) {
					count[j]++;
				}

			}
		}

		ll sum = 0, zero, prod = 1;
		forn(i, 0, 60) {
			zero = n - count[i];
			prod = ((zero % mod) * (count[i] % mod)) % mod;
			sum += ((prod % mod) * ((1LL << i) % mod)) % mod;
			sum %= mod;
		}
		o(sum)

	}
	CLOCK
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll cnt[66][2];
int main(){
	int n;cin>>n;
	for(int i=0;i<n;i++){
		ll a;cin>>a;
		int bit=0;
		while(a){
			if(a&1){
				cnt[bit][1]++;
			}else cnt[bit][0]++;
			a/=2;
			bit++;
		}
		for(int j=bit;j<=64;j++)cnt[j][0]++;
	}
	ll ans=0;
	for(int i=0;i<=62;i++){
		ll p=(1LL<<i);p%=mod;
		ll hano=cnt[i][0]*cnt[i][1];
		hano%=mod;
		ans+=(p*hano);
		ans%=mod;
	}
	cout<<ans;
	return 0;
}


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

int main(){
  int n,ans;
  ans=0;
  cin >> n;
  vector<string> s(3);

  for(int i=0;i<3;i++){
	cin >> s[i];
  }
  for(int i=0;i<n;i++){
    if(s[0][i]==s[1][i] && s[0][i]==s[2][i]);
    else if(s[0][i]==s[1][i] || s[0][i]==s[2][i] || s[1][i]==s[2][i])ans++;
    else ans+=2;
  }
  cout << ans;
  return 0;
}

#include<iostream>
#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main()
{
   int n,l;
   cin>>n;
   vector<int>v;
   int a[n];
   for(int i=0;i<n;i++){
    cin>>a[i];
   }
   int d=a[0];
   int c=0;
   for(int i=0;i<n;i++){
      if(a[i]<=d){
        c++;
        v.push_back(a[i]);
        d=a[i];
      }
    }
     cout<<c<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld long double
#define f first
#define s second
#define matrix vector<vector<char>>
#define zero(n,m) matrix(n , vector<char>(m,'0'))
#define one(n,m) matrix(n , vector<ll>(m,1))
#define pii pair<int,int>
#define point complex<ld>
#define angle(a) (atan2((a).imag() , (a).real() ))
#define pi acos(-1)
#define X real()
#define Y imag()
const int N = 2e5+5;

ll n , a[N] , b[N] , seg[4*N] , lazy[4*N];
ll st[N] , en[N] , st2[N] , en2[N];
set<ll> stt;

void probagate(int node , int st , int en)
{
    if (lazy[node] != 0)
    {
        seg[node] += lazy[node];
        if (st != en)
        {
            lazy[2*node] += lazy[node];
            lazy[2*node+1] += lazy[node];
        }
        lazy[node] = 0;
    }
}

void update(int node , int st , int en , int l , int r , int val)
{
    probagate(node , st , en);

    if (st > en || st > r || en < l)
        return;

    if (st >= l && en <= r)
    {
        lazy[node] += val;
        return probagate(node , st , en);
    }

    int mid = (st+en)/2;
    update(2*node,st,mid,l,r,val);
    update(2*node+1,mid+1,en,l,r,val);
    seg[node] = max(seg[2*node] , seg[2*node+1]);
}

ll query(int node , int st , int en , int l , int r)
{
    if (st > en || st > r || en < l) return 0;

    probagate(node , st , en);

    if (st >= l && en <= r)
    {
        return seg[node];
    }

    int mid = (st+en)/2;

    return max(query(2*node , st , mid , l , r) , query(2*node+1 , mid+1 , en , l , r));
}

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

    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        if (!st[a[i]]) st[a[i]] = i;
        en[a[i]] = i;
    }

    for(int i=1; i<=n; i++)
    {
        cin >> b[i];
        if (!st2[b[i]]) st2[b[i]] = i;
        en2[b[i]] = i;
        stt.insert(b[i]);
    }

    for(ll i=1; i<=n; i++)
    {
        ll l = st[b[i]] , r = en[b[i]];
        if (l > i)
        {
            update(1,0,n-1,0,l-i-1,1);
        }
        l = max(en[b[i]]+1,i) , r = n;
        if (l<=r)
        {
            ll l2 = l-i , r2 = r-i;
            if (l2 <= r2)
            {
                update(1,0,n-1,l2,r2,1);
            }
        }

        //from one to st[b[i]]-1
        l = 1 , r = min(st[b[i]]-1 , i-1);
        if (l<=r)
        {
            ll l2 = n-i+l , r2 = n-i+r;
            if (l2 <= r2)
            {
                update(1,0,n-1,l2,r2,1);
            }
        }

        if (i > en[b[i]])
        {
            l = en[b[i]]+1 , r = i-1;
            if (l<=r)
            {
                ll l2 = n-i+l , r2 = n-i+r;
                if (l2 <= r2)
                {
                    update(1,0,n-1,l2,r2,1);
                }
            }
        }
    }

    for(int i=0; i<n; i++)
    {
        if (query(1,0,n-1,i,i) == n)
        {
            cout << "Yes\n";
            for(int j=1; j<=n; j++)
            {
                int newIdx = j + i;
                if (newIdx > n) newIdx -= n;
                a[newIdx] = b[j];
            }
            for(int j=1; j<=n; j++)
                cout << a[j] << ' ';
            cout << '\n';
            return 0;
        }
    }
    cout << "No\n";

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i, N) cin >> A[i];

    ll ans = 0;
    int ct = 0;
    bool flag = false;
    repd(i, 1, N) {
        if (A[i] > ct + 1) {
            ans = -1;
            ct = 0;
            break;
        }
        else if (A[i] == ct + 1) {
            ct = A[i];
        }
        else {
            ans += ct;
            ct = A[i];
        }
    }

    ans += ct;
    if (A[0] >= 1) ans = -1;
    cout << ans << endl;
    return 0;
}


// luogu-judger-enable-o2
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
#define R register
using namespace std;
char s[351];int len,k;
int f[351][351][351],ans;
inline void read(R int &x)
{
    x=0;R int f=1;
	R char c=getchar();
	while(c<'0'||c>'9')
	{
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>=48&&c<=57)
	{
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
	x=x*f;
}
inline void input_data()
{
	char ch=getchar();
	while(ch>='a'&&ch<='z')
		s[++len]=ch,ch=getchar();
	read(k);return;
}
int dfs(R int k,R int l,R int r)
{
	if(l>r) return false;
	if(l==r) return s[l]==s[r];
	if(f[k][l][r]) return f[k][l][r];
	f[k][l][r]=max(dfs(k,l+1,r),dfs(k,l,r-1));
	if(s[l]==s[r]) f[k][l][r]=max(f[k][l][r],dfs(k,l+1,r-1)+2);
	if(k>=1) f[k][l][r]=max(f[k][l][r],dfs(k-1,l+1,r-1)+2);
	return f[k][l][r];
}
int main()
{
	input_data();
	for(R int i=0;i<=k;++i)
		ans=max(ans,dfs(i,1,len));
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
using namespace std;

int main(){
	int n=1;
	int a = 0;
	int f = 0;
	int quest[10000] = { 1 };
	double ans[10000];
	int checkc = 0;
	int i = 4;
	double check[3] = { 1, 2, 4 };

	do{
		cin >> quest[a];
		a++;
	} while (quest[a-1] != 0);
	while (a > f+1){
		switch (quest[f]){
		case 1:
			ans[f] = 1;
			break;
		case 2:
			ans[f] = 2;
			break;
		case 3:
			ans[f] = 4;
			break;
		default:
			while (quest[f] >= i){
				ans[f] = check[0] + check[1] + check[2];
				i++;
				check[0] = check[1];
				check[1] = check[2];
				check[2] = ans[f];
			}
		}

		ans[f] = ans[f] / 3650;
		checkc = (int)(ans[f]);

		if (ans[f] > (double)(checkc)){
			checkc++;
		}
		if (ans[f] != 0){
			cout << checkc << endl;
		}
		else{
			cout << endl;
		}
		f++;
	}
	return 0;
}

#include <iostream>

#define reep(i,n,m) for(int i=(n);i<(m);i++)
#define rep(i,n) reep(i,0,n)

using namespace std;

int main(){
    int n;
    while(cin >> n,n){
        int dp[35]={0};
        dp[0]=1;
        rep(i,n){
            dp[i+1] += dp[i];
            dp[i+2] += dp[i];
            dp[i+3] += dp[i];
        }

        cout << dp[n]/(365*10)+1 << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define PI acos(-1)

int main(){
    long long x,y,d,ans;
    bool minus_x = false,minus_y = false;
    cin >> x >> y;
    if(x < 0)minus_x = true;
    if(y < 0)minus_y = true;

    d = abs(abs(x) - abs(y));
    if(x == 0 || y == 0){
        if(x == 0 && y < 0 || y == 0 && x > 0)ans = d + 1;
        else ans = d;

    }else{
        if(abs(x) > abs(y)){
        if(minus_x && minus_y)ans = d;
        else if(minus_x || minus_y)ans = d + 1;
        else ans = d + 2;
    }
    else if(abs(x) == abs(y)){
        if(minus_x && minus_y)ans = d;
        else if(minus_x || minus_y)ans = d + 1;
        else ans = d;
    }
    else{
        if(minus_x && minus_y)ans = d + 2;
        else if(minus_x || minus_y)ans = d + 1;
        else ans = d;
        }
    }
    cout << ans << endl;
}

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

int main(){
    int x,y; cin >> x >> y;
    int ans = 0;
    if(x>0){
        if(y>0){
            if(x>y){
                ans++; // x -> -x
                ans += abs(x-y); // x -> -y
                ans++; // x-> y
            }else if(x < y){
                ans+= y-x; // x->y
            }
        }else if(y<0){
            if(abs(x) > abs(y)){
                ans++; // x->-x
                ans+= abs(x) - abs(y); // x->-y
            }else if(abs(x) < abs(y)){
                ans+= abs(y) - abs(x); // x->y
                ans++;   // x -> -y
            }else{
                ans++;
            }
        }else{
            ans++;
            ans+= x;
        }
    }else if(x<0){
        if(y>0){
            if(abs(x)>abs(y)){
                ans += abs(x) -abs(y);
                ans++;
            }else if(abs(x)<abs(y)){
                ans++;
                ans += abs(y) - abs(x);
            }else{
                ans++;
            }
        }else if(y<0){
            if(abs(x)>abs(y)){
                ans += abs(x) -abs(y);
            }else if(abs(x)<abs(y)){
                ans++;
                ans += abs(y) - abs(x);
                ans++;
            }
        }else{
            ans+=abs(x);
        }
    }else{
        if(y>0) ans+=y;
        else if(y<0) ans+= abs(y) + 1;
    }
    cout << ans << endl;
    return 0;
}


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

int main() {
    int a, b, k; cin >> a >> b >> k;
    for (int i=0; i<k; ++i) {
        int na, nb;
        if (i%2 == 0) {
            na = a/2;
            nb = b + a/2;
        } else {
            nb = b/2;
            na = a + b/2;
        }
        a = na;
        b = nb;
    }
    cout << a << " " << b << endl;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
	int n, m, f[10001] = { 0 }, s[10001] = { 0 }, ss[501] = {0}, f_, s_, cnt = 0;
	int up = 0, down = 0;
	cin >> n >> m;
	ss[1] = 3;
	while (n != 0 && m != 0)
	{
		memset(ss, 0, sizeof(ss));
		for (int i = 0; i < m; i++)
		{
			cin >> f[i] >> s[i];
			if (f[i] == 1)
			{
				ss[s[i]] = 2;
			}
		}
		for (int i = 0; i < m; i++)
		{
			if (ss[s[i]] == 2 && ss[f[i]] == 0)
				ss[f[i]] = 1;
			if (ss[f[i]] == 2 && ss[s[i]] == 0)
				ss[s[i]] = 1;
		}
		for (int i = 2; i <= n; i++) {
			if (ss[i] != 0)cnt++;
		}
		cout << cnt << endl;
		cnt = 0;
		cin >> n >> m;
	}
	return 0;
}

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

const int INF = 1e+9;
typedef pair<int, int> P;
typedef pair<P, int> pi;

int main() {
	vector<pi> data;
	for(int y = 1; y <= 150; ++y){
		for(int x = y + 1; x <= 150; ++x){
			data.push_back(pi(P(x * x + y * y, y), x));
		}
	}
	sort(data.begin(), data.end());
	int h, w;
	while(cin >> h >> w && (h || w)){
		int base = h * h + w * w;
		auto it = lower_bound(data.begin(), data.end(), pi(P(base, h), w));
		while((*it).first.first == base && (*it).first.second == h){
			++it;
		}
		cout << (*it).first.second << " " << (*it).second << endl;
	}
	return 0;
}


#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 vtpl(x,y,z) vector<tuple<x,y,z>>
#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<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    string s;cin >> s;
    if("zyxwvutsrqponmlkjihgfedcba"==s)cout << -1 <<endl,exit(0);
    if(s.size()==26){
        set<char> st;
        char x;
        per(i,25){
            st.ins(s[i+1]);
            if(s[i]>s[i+1])s.erase(i+1);
            else{
                x=s[i];
                s.erase(i);
                break;
            }
        }
        x=*st.upper_bound(x);
        cout << s<<x <<endl;
        return 0;
    }
    set<char> st;
    rep(i,s.size())st.ins(s[i]);
    rep(i,26){
        if(!st.count('a'+i)){
            //cout << i <<endl;
            char x='a'+i;
            cout << s <<x<<endl;
            return 0;
        }
    }
}

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repSE(i,s,n) for (int i = (s); i < (n); i++)
#define rrepSE(i,s,e) for (int i = (s); i > (e); i--)
#define ssort(v) sort(v.begin(), v.end())
#define gsort(v) sort(v.rbegin(), v.rend())
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
#define DEBUG_PRINT(var) std::cout << #var << " = " << var << "\n";

int main()
{
    string st;
    cin >> st;
    int s = 0, t = 0;
    rep(i,st.size()) {
        if (st[i] == 'S') s++;
        if (st[i] == 'T') {
            if (s > 0) s--;
            else t++;
        }
    }
    cout << s + t << "\n";
    return 0;
}


#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Modulo{
const int mod=998244353;
il int ad(int x,int y){return x+y>=mod?x+y-mod:x+y;}
il int sub(int x,int y){return ad(x,mod-y);}
il int mul(int x,int y){return (ll)x*y%mod;}
il void inc(int &x,int y){x=ad(x,y);}
il void inc2(int &x,int y){x=mul(x,y);}
il int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
template<class ...Args>il int ad(const int a,const int b,const Args &...args) {return ad(ad(a,b),args...);}
template<class ...Args>il int mul(const int a,const int b,const Args &...args) {return mul(mul(a,b),args...);}
}
//using namespace Modulo;
namespace Miracle{
const int N=2e5+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
int n,q,A,B;
ll f[N],s[N];
ll x[N];
struct SegmentTree{
ll mi[4*N];
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
void pushup(int x){
	mi[x]=min(mi[ls],mi[rs]);
}
void build(int x,int l,int r){
	if(l==r){
		mi[x]=inf;return;
	}
	mi[x]=inf;
	build(ls,l,mid);build(rs,mid+1,r);
}
void upda(int x,int l,int r,int p,ll c){
	if(l==r){
		mi[x]=min(mi[x],c);return;
	}
	if(p<=mid) upda(ls,l,mid,p,c);
	else upda(rs,mid+1,r,p,c);
	pushup(x);
}
ll query(int x,int l,int r,int L,int R){
	if(L<=l&&r<=R){
		return mi[x];
	}
	ll ret=inf;
	if(L<=mid) ret=min(ret,query(ls,l,mid,L,R));
	if(mid<R) ret=min(ret,query(rs,mid+1,r,L,R));
	return ret;
}

}sg[2];
ll dp(){
	memset(f,0x3f,sizeof f);
	memset(s,0,sizeof s);
	sg[0].build(1,1,n);sg[1].build(1,1,n);
	s[0]=0;s[1]=abs(x[1]-B);
	for(reg i=2;i<=q;++i) s[i]=s[i-1]+abs(x[i]-x[i-1]);

	sg[0].upda(1,1,n,A,-A);
	sg[1].upda(1,1,n,A,A);

	ll mi=0;
	for(reg i=1;i<=q;++i){
		if(i==q){
			return s[i]+mi;
		}else{
			f[i]=min(sg[0].query(1,1,n,1,x[i+1])+x[i+1]+s[i],sg[1].query(1,1,n,x[i+1],n)-x[i+1]+s[i]);
			sg[0].upda(1,1,n,x[i],f[i]-s[i+1]-x[i]);
			sg[1].upda(1,1,n,x[i],f[i]-s[i+1]+x[i]);

			mi=min(mi,f[i]-s[i+1]);
		}
	}
	return 23333;
}
int main(){
	rd(n);rd(q);rd(A);rd(B);
	for(reg i=1;i<=q;++i) rd(x[i]);
	ll ans=dp();swap(A,B);ans=min(ans,dp());
	printf("%lld\n",ans);
	return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
*/

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

//N点連結グラフの二部グラフ判定
//Gはグラフの隣接リスト表現
//colorは大きさNの配列。二部グラフだったとき、独立集合がここに返ってくる
//vは探索を始める点。cは探索を始める点に塗る色
//vと同じ独立集合に含まれる点はcに塗られ、そうでない点は-cに塗られる
//colorは先に0で埋めておく
bool is_bipartite_graph (vector<vector<int>> &G, vector<int> &color, int v, int c){
    color[v] = c;
    for (int next_v : G[v]){
        if(color[next_v] == c){
            return false;
        }
        if(color[next_v] == 0 && !is_bipartite_graph(G, color, next_v, -c)){
            return false;
        }
    }
    return true;
}

int main(){
    ll N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    int x, y;
    for (int i = 0; i < M; i++) {
        cin >> x >> y; x--; y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    vector<int> color(N, 0);
    if(is_bipartite_graph(G, color, 0, 1)){
        ll a, b;
        a = 0; b = 0;
        for (int i = 0; i < N; i++) {
            if(color[i] == 1){
                a++;
            }
            else{
                b++;
            }
        }
        printf("%lld\n", a*b - M);
    }
    else{
        printf("%lld\n", (N-1)*N/2 - M);
    }

}


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

const int MN = 20123, INF = 5012345, MOD = 1000 * 1000 * 1000 + 7;
int inv[INF], fict[INF], invf[INF], dp[MN][MN];

void fact() {
	fict[0] = inv[0] = invf[0] = 1;
	fict[1] = inv[1] = invf[1] = 1;
	for (int i = 2; i < INF; i++) {
		inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD;
		fict[i] = 1ll * fict[i - 1] * i % MOD;
		invf[i] = 1ll * invf[i - 1] * inv[i] % MOD;
	}
}

int chose(int a, int b) {
	return 1ll * fict[b] * invf[a] % MOD * invf[b - a] % MOD;
}

int main() {
	fact();
	int n, k;
	cin >> n >> k;
  	if (k == 1)
      return cout << 1, 0;
	dp[0][0] = 1;
	for (int i = 1; i <= n; i++)
		for (int j = 0; j <= n; j++) {
			if (j > i)
    	          continue;
			dp[i][j] = dp[i - 1][j];
			if (j)
				dp[i][j] = (dp[i][j] + 1ll * dp[i][j - 1] * chose(k - 2, n * k - (j - 1) * (k - 1) - i - 1) % MOD) % MOD;
        }
	cout << 1ll * dp[n][n] * fict[n] % MOD;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 +c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 1000000007;
int fac[10000005],invfac[10000005];
int dp[2005][2005],N,K,sum[2005];
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
void update(int &x,int y) {
    x = inc(x,y);
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int Query(int n,int m) {
    return C(n + m - 1,m - 1);
}
int fpow(int x,int c) {
    int res = 1,t = x;
    while(c) {
	if(c & 1) res = mul(res,t);
	t = mul(t,t);
	c >>= 1;
    }
    return res;
}
void Solve() {
    read(N);read(K);
    if(K == 1) {puts("1");return;}
    fac[0] = 1;
    for(int i = 1 ; i <= 10000000 ; ++i) fac[i] = mul(fac[i - 1],i);
    invfac[10000000] = fpow(fac[10000000],MOD - 2);
    for(int i = 9999999 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
    dp[1][0] = 1;
    for(int i = 2 ; i <= N ; ++i) {
	int t = (i - 1) * K;
	for(int j = N ; j >= 0 ; --j) sum[j] = inc(sum[j + 1],dp[i - 1][j]);
	for(int j = 0 ; j <= i ; ++j) {
	    update(dp[i][j],mul(sum[max(0,j - 1)],Query(K - 2,t - j + 1)));
	}
    }
    int ans = 0;
    for(int j = 0 ; j <= N ; ++j) update(ans,mul(dp[N][j],fac[N]));
    out(ans);enter;
}

int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}


#include <cstdio>

int main() {
	int a, b, c, d, e, tot = 0;
	scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
	if (c < a) c += 24;
	tot += (c - a) * 60 + (d - b);
	printf("%d", tot - e);
	return 0;
}

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

int main() {
  int a, b;
  cin >> a >> b;
  int ans;
  if (a == b) {
    ans = a + b;
  } else {
    int mx = max(a, b);
    ans = mx + mx - 1;
  }

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