#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec){
    ll n = vec.size();
    rep(i,n) cout << "i" << " " << vec.at(i) << "\n";
}

void pr2d(vector<vector<ll>> vvec){
    ll h = vvec.size();
    ll w = vvec.at(0).size();
    rep(i,h){
        rep(j,w){
            cout << vvec.at(i).at(j) << " ";
        }
        cout << "\n";
    }
}

int main(){
    ll n ; cin >> n;
    ll prev ; cin >> prev;
    bool NG = false;
    if(prev!=0) NG = true;
    ll ans = 0;
    rep(i,n-1){
        ll x ; cin >> x;
        if(x == prev + 1){
            ans++;
        }else if(x>prev+1){
            NG = true;
        }else{
            ans+=x;
        }
        prev = x;
    }
    if(NG){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } }return a; }
ll ac(ll n, ll k) { ll a = 1; rep(i, 1, k) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }
typedef struct { ll n, m; } p2;

int main()
{
	int N;
	cin >> N;
	vector<int> A(N);
	ll ans = 0;
	rep(i, 0, N) {
		cin >> A[i];
		if (A[i] > i || i && A[i] - A[i - 1] > 1) {
			puts("-1");
			return 0;
		}
		if (i && A[i] != A[i - 1] + 1) {
			ans += (ll)A[i - 1];
		}
	}
	ans += (ll)A[N-1];
	printf("%lld\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <int, int>
#define sz(x) (int)((x).size())
#define int long long

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

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 20;
const int NMAX = 1e4 + 11;
const ll INF64 = 3e18 + 1;
const double lil = 0.0000000000001;

//ifstream cin ("test.in");
//ofstream cout ("test.out");

int n, a[N][N], dp[(1 << N)];

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

    cin >> n;

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

    //create groups
    for(int mask = 0; mask < (1 << n); mask++){

        int ans = 0;

        for(int i = 0; i < n; i++){
            if(!(mask & (1 << i)))continue;
            for(int j = 0; j < i; j++){
                if(!(mask & (1 << j)))continue;
                ans += a[i][j];
            }
        }

        dp[mask] = ans;
    }

    for(int mask = 1; mask < (1 << n); mask++){
        for(int i = mask; i; i = (mask & (i - 1))){
            if(dp[mask] < dp[mask - i] + dp[i])dp[mask] = dp[mask - i] + dp[i];
        }
    }

    rc(dp[(1 << n) - 1]);
}

//Author:xht37
#include <bits/stdc++.h>

#define ui unsigned int
#define ll long long
#define ul unsigned ll
#define ld long double

#define pi pair <int, int>
#define fi first
#define se second
#define mp make_pair

#define ls (p << 1)
#define rs (ls | 1)
#define md ((t[p].l + t[p].r) >> 1)

#define vi vector <int>
#define pb push_back
#define pq priority_queue

#define dbg(x) cerr << #x" = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)

#define fl(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)

using namespace std;

namespace io {
	const int SI = 1 << 21 | 1;
	char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
	int f, t;
	#define gc() (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)
	inline void flush() {
		fwrite(OB, 1, OS - OB, stdout), OS = OB;
	}
	inline void pc(char x) {
		*OS++ = x;
		if (OS == OT) flush();
	}

	template <class I>
	inline void rd(I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15), c = gc());
		x *= f;
	}
	template <class I>
	inline void rd(I &x, I &y) {
		rd(x), rd(y);
	}
	template <class I>
	inline void rd(I &x, I &y, I &z) {
		rd(x), rd(y), rd(z);
	}
	template <class I>
	inline void rda(I *a, int n) {
		for (int i = 1; i <= n; i++) rd(a[i]);
	}
	inline void rdc(char &c) {
		for (c = gc(); c < 33 || c > 126; c = gc());
	}
	inline void rds(char *s, int &n) {
		for (c = gc(); c < 33 || c > 126; c = gc());
		for (n = 0; c >= 33 && c <= 126; s[++n] = c, c = gc());
		s[n+1] = '\0';
	}
	inline void rds(string &s) {
		for (c = gc(); c < 33 || c > 126; c = gc());
		for (s.clear(); c >= 33 && c <= 126; s.pb(c), c = gc());
	}

	template <class I>
	inline void print(I x, char k = '\n') {
		if (!x) pc('0');
		if (x < 0) pc('-'), x = -x;
		while (x) ch[++t] = x % 10 + '0', x /= 10;
		while (t) pc(ch[t--]);
		pc(k);
	}
	template <class I>
	inline void print(I x, I y) {
		print(x, ' '), print(y);
	}
	template <class I>
	inline void print(I x, I y, I z) {
		print(x, ' '), print(y, ' '), print(z);
	}
	template <class I>
	inline void printa(I *a, int n) {
		for (int i = 1; i <= n; i++) print(a[i], " \n"[i==n]);
	}
	inline void printc(char c) {
		pc(c);
	}
	inline void prints(char *s, int n) {
		for (int i = 1; i <= n; i++) pc(s[i]);
		pc('\n');
	}
	inline void prints(string s) {
		int n = s.length();
		while (t < n) pc(s[t++]);
		pc('\n'), t = 0;
	}
	struct Flush {
		~Flush() {
			flush();
		}
	} flusher;
}
using io::rd;
using io::rda;
using io::rdc;
using io::rds;
using io::print;
using io::printa;
using io::printc;
using io::prints;

const int N = 1e5 + 7;
int n, m, v, p, a[N];
ll s[N];

int main() {
	rd(n, m), rd(v, p), rda(a, n);
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) s[i] = a[i] + s[i-1];
//	for (int i = 1; i <= n; i++) debug("%d%c", a[i], " \n"[i==n]);
	for (int i = 1; i <= n; i++) {
		int c = n + 1 - (upper_bound(a + 1, a + n + 1, a[i] + m) - a);
		if (c + 1 > p) continue;
		int d = (upper_bound(a + 1, a + n + 1, a[i]) - a) - 2;
//		debug("%d %d %d\n", i, c, d);
		ll o = 1ll * m * (v - 1 - c - d) - (1ll * (a[i] + m) * (n - 1 - c - d) - (s[n-c] - s[d+1]));
//		dbg(o);
		if (o <= (s[n-c] - s[n-p+1]) - 1ll * (p - 1 - c) * a[i]) {
			print(n - i + 1);
			break;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

template<typename T>
auto print(std::vector<T> array,bool reverse = false)
 -> void{
 	if(reverse){
		auto iterator = array.rbegin();
		std::cout<<*iterator;
		iterator++;
		for(;iterator != array.rend();++iterator){
			std::cout<<" "<< *iterator;
		}

	}else{
		auto iterator = array.begin();
		std::cout<<*iterator;
		iterator++;
		for(;iterator != array.end();++iterator){
			std::cout<<" "<< *iterator;
		}
	}
	std::cout<<"\n";
}

template<typename T>
auto println(std::vector<T> array)
 -> void{
	for(auto v : array){
		std::cout<< v <<"\n";
	}
}

int cnt = 0;

template<typename T>
auto insertionSort(std::vector<T> *A,int n,int g)
 -> void{
 	for(int i = g;i < n;i++){
 		auto v = (*A)[i];
 		auto j = i - g;
 		while(j >= 0 && (*A)[j] > v){
 			(*A)[j+g] = (*A)[j];
 			j = j - g;
 			cnt++;
 		}
 		(*A)[j+g] = v;
 	}
 }

template<typename T>
auto shellSort(std::vector<T> *A,int n)
 -> void{
 	cnt = 0;

 	int h = 0;
 	std::vector<int> G;
    for(int k = 1;;k++){
    	h = static_cast<int>((pow(3,k)-1)/2);
    	if(h > n)
    		break;
    	G.push_back(h);
    }

 	std::cout<< G.size() <<std::endl;
 	print(G, true);

 	for(auto it = G.rbegin();it != G.rend();++it){
 		insertionSort<T>( A, n, (*it));
 	}

 }

auto main()
 -> int{

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

	int n;
	std::cin>>n;

	std::vector<int> array(n);
	for(int i=0;i < n;i++)
		std::cin>>array[i];

	shellSort( &array, n);

 	std::cout<< cnt <<"\n";
	println(array);

	return 0;
}

#include<stdio.h>

int main(){
    int n;
    scanf("%d",&n);
    int array[n];
    for(int i = 0;i < n;i++)    scanf("%d",&array[i]);
    for(int i = n;i > 0;i--){
        if(i != 1){
            printf("%d ",array[i-1]);
        } else{
            printf("%d\n",array[i-1]);
        }
    }
}


#include <stdio.h>
const int N=110;
const int INF=1<<30;
int min(int a,int b){
    return a>b?b:a;
}
int main(){
    int n,dp[N][N],a[N];
    scanf("%d%d%d",&n,&a[0],&a[1]);
    for(int i=1;i<n;i++)scanf("%d%d",&a[i],&a[i+1]);
    for(int k=1;k<n;k++){
        for(int i=0;i<n-k;i++){
            dp[i][i+k]=INF;
            for(int j=i;j<i+k;j++)dp[i][i+k]=min(dp[i][i+k],dp[i][j]+dp[j+1][i+k]+a[i]*a[j+1]*a[i+k+1]);
        }
    }
    printf("%d\n",dp[0][n-1]);
}


#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size())cout << a[i] << " ";cout << "\n"
#define vecvec(a) vector<vector<(a)>>
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const ld pi = 3.14159265358;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
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;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

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

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);
struct edge{ll to,cost;};
ll N;const ll NMAX = 100005LL;
vector<edge> G[NMAX];
vector<ll> dist(NMAX,INF);
void dfs(ll from,ll par,ll d){
    dist[from] = d;
    for(auto x : G[from]){
        if(x.to != par){
            dfs(x.to,from,d + x.cost);
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll a,b,c;
    cin >> N;
    rep(i,N - 1){
        cin >> a >> b >> c;
        a--;b--;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
    ll Q,K;
    cin >> Q >> K;
    dist[K - 1] = 0;
    dfs(K - 1,-1,0);
    ll A,B;
    rep(i,Q){
        cin >> A >> B;
        cout << dist[A - 1] + dist[B - 1] << "\n";
    }
}

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define all(x) (x).begin() , (x).end()
#define f first
#define s second
#define pr(x) cout<<x<<endl;
#define pr2(x,y) cout<<x<<" "<<y<<endl;
#define pr3(x,y,z) cout<<x<<" "<<y<<endl;
#define prv(v) for(auto x:v) cout<<x<<" ";
using namespace std;

const ll N =(2e5+5);
const ll MOD = 1e9+7;
const ll INF = 1e16;
const ll LOG = 29;

long long binpow(long long a, long long b) {
     a %= MOD;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%MOD ;
        a = (a * a)%MOD ;

        b >>= 1;
    }
    res%=MOD;
    return res;
}
vll adj[N];
vll adj2[N];
ll col[N];
ll col2[N];
bool vis[N];
bool vis2[N];
map<pll,ll> gg;
    void dfs(ll x,ll c)
        {
            vis[x]=true;
            col[x]=c;
            for(auto i : adj[x])
            {
                if(vis[i]==false)
                {
                    dfs(i,c);
                }
            }
        }

    void dfs2(ll x,ll c)
        {
            vis2[x]=true;
            col2[x]=c;
            for(auto i : adj2[x])
            {
                if(vis2[i]==false)
                {
                    dfs2(i,c);
                }
            }
        }

void solve(){
    ll n,k,l;
    cin>>n>>k>>l;
    for(int i =1;i<=k;i++){
        ll u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(int i = 1;i<=l;i++){
        ll u,v;
        cin>>u>>v;
        adj2[u].pb(v);
        adj2[v].pb(u);
    }
    ll cur = 1;
    for(int i =1;i<=n;i++){
        if(!vis[i]){
            dfs(i,cur);
            cur++;
        }
    }
    cur =1;
    for(int i =1;i<=n;i++){
        if(!vis2[i]){
            dfs2(i,cur);
            cur++;
        }
    }

    for(int i =1;i<=n;i++){
        gg[mp(col[i],col2[i])]++;
    }
    for(int i =1;i<=n;i++){
        cout<<gg[mp(col[i],col2[i])]<<" ";
    }
    cout<<endl;

}

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

    ll t=1;
    while(t--){
        solve();
    }

    return 0;

}


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

int main(){
	int k;
  	string s, r;
  	cin >> k >> s;
  	if(s.size()<=k) cout << s << endl;
  	else{
    	for(int i=0;i<k;i++){
        	r += s.at(i);
        }
      	cout << r << "..." << endl;
    }

}

#include <bits/stdc++.h>
#define INF INT_MAX
#define ll  long long
#define ull unsigned long long
#define rep(i,n) for(ll i=0; i<n; ++i)
#define FOR(i, s, e) for(ll i=s; i<e; ++i)
#define MOD 1000000007
using namespace std;
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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int K; cin >> K;
  string S; cin >> S;
  if(S.size() > K) {
    S = S.substr(0,K) + "...";
  }
  cout << S << endl;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define int ll
vector<ll> node[100005];
ll group[100005]={0},group_num[100005]={0},col[100005]={0};
ll nibu[100005]={0};
bool used[100005]={0};
bool dfs(ll pos,ll k,ll cl){
  used[pos]=true;
  group[pos]=k;
  col[pos]=cl;
  ll nc=cl%2;
  nc++;
  bool res=true;
  for(int i=0;i<node[pos].size();i++){
    ll to=node[pos][i];
    if(!used[to]){
      used[to]=true;
      dfs(to,k,nc);
    }
    if(col[to]!=nc){
      res=false;
    }
  }
  return res;
}
signed main(){
  ll n,m;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    ll s,t;
    cin>>s>>t;
    node[s].push_back(t);
    node[t].push_back(s);
  }
  for(int i=1;i<=n;i++) used[i]=false;
  ll cnct_num=0,a=0,b=0,c=0;
  for(int i=1;i<=n;i++){
    if(!used[i]){
      used[i]=true;
      group[i]=cnct_num;
      if(node[i].size()==0) {
        c++;
        continue;
      }
      bool f=dfs(i,cnct_num,1);
      b++;
      cnct_num++;
    }
  }
 for(int i=1;i<=n;i++){
    for(int j=0;j<node[i].size();j++){
      ll to=node[i][j];
      if(col[i]==col[to]){
        nibu[group[i]]=1;
      }
    }
  }
  for(int i=0;i<cnct_num;i++){
    if(nibu[i]==1){
      b--;
      a++;
    }
  }
  cout<<a*a+2*a*b+2*b*b+c*(n*2-c)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
void computeLPSArray(string pattern, int m, int lps[]){
    int len=0;
    lps[0]=0;   //lps[0] is always 0
    int i=1;
    while(i<m){
        if(pattern[i]==pattern[len]){
            len++;
            lps[i]=len;
            i++;
        }else{
            if(len!=0)
                len=lps[len-1];
            else{
                lps[i]=0;
                i++;
            }
        }
    }
}
void kmpSearch(string str,string pattern){
    int m=pattern.length();
    int n=str.length();
    int lps[m];  //longest prefix suffix array
    computeLPSArray(pattern,m,lps);
    int i=0; //text
    int j=0; //pattern
    bool found=0;
    while(i<n){
        if(pattern[j]==str[i]){
            i++;
            j++;
        }
        if(j==m){
            j=lps[j-1];
            found=1;
            break;
        }else if(i<n && pattern[j]!=str[i]){
            if(j!=0) j=lps[j-1];
            else i=i+1;
        }
    }
    if(!found)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
}
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    string str,pattern;
    while(cin>>str){
        pattern="AC";
        kmpSearch(str,pattern);
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define MOD 1000000007
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define ALL(v) v.begin(), v.end()
typedef long long ll;

int main(){
    ll x;cin >> x;

    int t=x/500;
    int f=(x-t*500)/5;

    cout << 1000*t+5*f << endl;

    return 0;
}


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

vector<int> G[1010];
bool H[1010][1010];
bool visited[1010];

vector<int> path;
vector<int> cycle;

void dfs(int v) {
  visited[v] = true;
  path.push_back(v);
  for (int u : G[v]) {
    if (!cycle.empty()) continue;
    if (find(path.begin(), path.end(), u) != path.end()) {
      cycle = path;
      return;
    } else {
      dfs(u);
    }
  }
  path.pop_back();
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    H[a][b] = true;
  }

  for (int i = 0; i < N; i++) {
    if (visited[i]) continue;
    path.clear();
    dfs(i);
    if (cycle.size() > 0) {
      while (!H[cycle[cycle.size() - 1]][cycle[0]]) cycle.erase(cycle.begin());
      bool f = true;
      while (f) {
        f = false;
        bool used[1010] = {};
        for (int u : cycle) used[u] = true;

        int from, to;
        for (int i = 0; i < cycle.size(); i++) {
          int v = cycle[i];
          for (int u = 0; u < N; u++) {
            if (!used[u]) continue;
            if (u == v) continue;
            if (u == cycle[(i + 1) % cycle.size()]) continue;
            if (H[v][u]) {
              f = true;
              from = v, to = u;
              break;
            }
          }
        }

        if (f) {
          vector<int> tmp;
          tmp.push_back(from);
          while (to != tmp[0]) {
            tmp.push_back(to);
            for (int u : G[to]) {
              if (used[u] && H[to][u]) {
                to = u;
                break;
              }
            }
          }
          cycle = tmp;
        }
      }
      cout << cycle.size() << endl;
      for (int v : cycle) {
        cout << v + 1 << endl;
      }
      return 0;
    }
  }

  cout << -1 << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N = 1003;
int n, m, dis[N], par[N];
vector<int> adj[N], ans;
int main(int argc, char const *argv[])
{
	scanf("%d %d\n", &n, &m);
	for (int i = 0; i < m; ++i)
	{
		int u, v;
		scanf("%d %d\n", &u, &v);
		adj[u].push_back(v);
	}
	for (int s = 1; s <= n; ++s)
	{
		queue<int> q;
		for (int i = 1; i <= n; ++i)
		{
			par[i] = dis[i] = -1;
		}
		q.push(s);
		dis[s] = 0;
		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			for (int v: adj[u])
			{
				if (dis[v] == -1)
				{
					par[v] = u;
					dis[v] = 1 + dis[u];
					q.push(v);
				}
				else if (v == s)
				{
					int cur = u;
					vector<int> temp;
					while (cur != -1)
					{
						temp.push_back(cur);
						cur = par[cur];
					}
					if (temp.size() < ans.size() || ans.empty())
					{
						ans.clear();
						for (int node: temp)
							ans.push_back(node);
					}
				}
			}
		}
	}
	if (ans.empty())
		printf("%d\n", -1);
	else
	{
		printf("%d\n", (int)ans.size());
		for (int node: ans)
			printf("%d\n", node);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
T prim(WeightedGraph< T > &g) {
  using Pi = pair< T, int >;

  T total = 0;
  vector< bool > used(g.size(), false);
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  que.emplace(0, 0);
  while(!que.empty()) {
    auto p = que.top();
    que.pop();
    if(used[p.second]) continue;
    used[p.second] = true;
    total += p.first;
    for(auto &e : g[p.second]) {
      que.emplace(e.cost, e.to);
    }
  }
  return total;
}

int main() {
  int V, E;
  scanf("%d %d", &V, &E);
  WeightedGraph< int > g(V);
  for(int i = 0; i < E; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  printf("%d\n", prim(g));
}

//BINARY_BOY!!
//THINK TWICE CODE ONCE { TRUE }
// #####   ###  ##   #   ###   #####   #   #  #####	#####
// #    #   #   # #  #  #   #  #    #   # #   #	#	#	#
// #####    #   #  # #  #####  #####     #    #####	#	#
// #    #   #   #   ##  #   #  #   #     #    #	#	#	#
// #####   ###  #    #  #   #  #    #    #    #####	#####
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 10000 + 5;
vector < pair < int  , pair < int , int > > > v ;
vector< int > v2 [ MAXN ];
int n , m, sum = 0, num [ MAXN ];
bool cmd(pair < int , pair<int , int> > p1 , pair < int , pair<int , int> > p2 )
{
	return p1.first < p2.first;
}
int main() {
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin >> n >> m;
	for(int i = 0 ; i < m ; i++)
	{
		int a , b, c;
		cin >> a >> b >> c;
		v.push_back(make_pair(c , make_pair(a , b)));
	}
	sort(v.begin() , v.end() , cmd);
	for(int i = 0 ; i <= n ; i++)
	{
		v2[ i ].push_back(i);
		num [ i ] = i;
	}
	for(int i = 0 , j = 0 ; i < v.size() && j < n - 1; i++)
	{
		if(num [ v [ i ].second.first ] != num [ v [ i ].second.second ] )
		{
			sum += v [ i ].first;
			j++;
			if(v2 [ num [v [ i ].second.first ]].size() <  v2 [num[ v [ i ].second.second ]].size())
			{
				for(int f = 0 , l = v2 [ num [v [ i ].second.first ] ].size() , k = num [v [ i ].second.first]; f < l ; f++)
				{
					v2 [ num [v [ i ].second.second ] ].push_back(v2 [ k ][ f ]);
					num [v2 [ k ][ f ]] = num [v [ i ].second.second];
				}
			}
			else {
				for(int f = 0 , l = v2 [ num [v [ i ].second.second ] ].size() , k = num [v [ i ].second.second] ; f < l ; f++)
				{
					v2 [ num [v [ i ].second.first] ].push_back(v2 [ k ][ f ]);
					num [v2 [ k ][ f ]] = num [v [ i ].second.first];
				}
			}
		}
	}
	cout << sum << endl;
}


#include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
int main(){
int n;
cin>>n;
vector<int>in(n);
rep(i,n)cin>>in[i];
rep(i,n-1)cout<<in[n-i-1]<<" ";
cout<<in[0]<<endl;
}

// class point	の練習（クラスの高度な実装）
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>

#define INF 1000001
typedef long long llong;
using namespace std;

int main() {
	int n;
	int a_i;
	vector<int> vt;
	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> a_i;
		vt.push_back(a_i);
	}
	reverse(vt.begin(), vt.end());
	for (vector<int>::iterator it = vt.begin(); it != vt.end(); it++) {
		if (it == vt.begin())cout << *it;
		else cout << " " << *it;
	}
	cout << endl;
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;
using namespace std;
int main()
{
  int A,B,M;
  cin>>A>>B>>M;
  int a[100010],b[100010],x[100010],y[100010],z[100010];
  rep(i,A)cin>>a[i];
  rep(i,B)cin>>b[i];
  rep(i,M)cin>>x[i]>>y[i]>>z[i];

  int min=200100;
  rep(i,M){
    int sum=0;
    sum+=a[x[i]-1]+b[y[i]-1]-z[i];
    // cout<<sum<<endl;
    if(sum<min)min=sum;
  }
  if(*min_element(a,a+A)+*min_element(b,b+B)<min)min=*min_element(a,a+A)+*min_element(b,b+B);
  cout<<min<<endl;
  return 0;
}

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

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

	vector<int> ac(A);
	vector<int> bc(B);

	int minA = 1<<30, minB = 1<<30;
	rep(i, A) {
		cin >> ac.at(i);
		minA = min(minA, ac.at(i));
	}
	rep(j, B) {
		cin >> bc.at(j);
		minB = min(minB, bc.at(j));
	}

	int ans = minA + minB;

	rep(i, C) {
		int m;
		int tmp=0;
		cin >> m;
		tmp += ac.at(m-1);
		cin >> m;
		tmp += bc.at(m - 1);
		cin >> m;
		tmp -= m;
		ans = min(ans, tmp);
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;

int main() {
    ll n; cin >> n;
    vector<int> x(n);
    vector<int> y(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> y[i];
    }
    vector<int> perm(n);
    for (int i = 0; i < n; ++i) perm[i] = i;

    double dist = 0;
    do {
        for (int i = 0; i < n - 1; ++i) {
            int xi = x[perm[i]];
            int nextxi = x[perm[i + 1]];
            int yi = y[perm[i]];
            int nextyi = y[perm[i + 1]];
            dist += sqrt(pow((nextxi - xi), 2) + pow((nextyi - yi), 2));
        }
    } while (next_permutation(perm.begin(), perm.end()));
    for (int i = 2; i <= n; ++i) {
        dist /= i;
    }
    cout << setprecision(20);
    cout << dist << endl;
}

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

UL buf[9][8][8]={};
UL mask[9]={};
UL ans[8][8];

void put(int x, int y, int k){
 buf[k][x][y]=2;
 for(int xx=0; xx<8; xx++) for(int yy=0; yy<8; yy++){
  if(buf[k][xx][yy]) continue;
  if(xx==x || yy==y || (x+y)==(xx+yy) || (x-y)==(xx-yy))
   buf[k][xx][yy]=1;
 }
}

bool proc(UL k){
 if(k==8){ rep(x,8)rep(y,8) ans[x][y]=buf[8][x][y]; return true; }
 UL y=0; rep(i,8) if(~mask[k] & (1<<i)) y=i;
 mask[k+1] = mask[k] | (1<<y);
 rep(x,8){
  rep(xx,8)rep(yy,8) buf[k+1][xx][yy]=buf[k][xx][yy];
  if(buf[k+1][x][y]) continue;
  put(x,y,k+1);
  if(proc(k+1)) return true;
 }
 return false;
}

int main() {
 mask[0]=0;
 UL K; scanf("%u",&K);
 rep(i,K){
  UL x,y; scanf("%u%u",&y,&x);
  mask[K] |= (1<<y);
  put(x,y,K);
 }
 proc(K);
 rep(y,8){
  rep(x,8) printf((ans[x][y]==2)?"Q":".");
  printf("\n");
 }
 return 0;
}


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

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

  vector<vector<int>> ans(2);

  if(n%2 == 1){
    for(int l = 1, r = n-1; l < r; l++, r--){
        ans.at(0).push_back(l);
        ans.at(1).push_back(r);
    }
  }
  else{
    bool flag = false;
    for(int l = 1, r = n-1; l < r; l++, r--){
        if(!flag && r-l <= n/2){
          --r;
          flag = true;
        }
        ans.at(0).push_back(l);
        ans.at(1).push_back(r);
    }
  }

  for(int i = 0; i < m; i++){
    cout << ans.at(0).at(i) << " " << ans.at(1).at(i) << endl;
  }
}

#include <iostream>

int main()
{
    int sx, sy, tx, ty;
    std::cin >> sx >> sy >> tx >> ty;

    for (int i = 0; i < ty - sy; ++i)
    {
        std::cout << 'U';
    }
    for (int i = 0; i < tx - sx; ++i)
    {
        std::cout << 'R';
    }
    // tに到達した

    for (int i = 0; i < ty - sy; ++i)
    {
        std::cout << 'D';
    }
    for (int i = 0; i < tx - sx; ++i)
    {
        std::cout << 'L';
    }
    // sに戻った

    std::cout << 'L';
    for (int i = 0; i < ty - sy + 1; ++i)
    {
        std::cout << 'U';
    }
    for (int i = 0; i < tx - sx + 1; ++i)
    {
        std::cout << 'R';
    }
    std::cout << 'D';
    // 2回目のt

    std::cout << 'R';
    for (int i = 0; i < ty - sy + 1; ++i)
    {
        std::cout << 'D';
    }
    for (int i = 0; i < tx - sx + 1; ++i)
    {
        std::cout << 'L';
    }
    std::cout << 'U';

    std::cout << std::endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, q;

    while (scanf("%d %d", &n, &q) && n){
        int a[128] = {0};

        for (int i = 0; i < n; i++){
            int m;
            scanf("%d", &m);
            for (int j = 0; j < m; j++){
                int x;
                scanf("%d", &x);
                a[x]++;
            }
        }

        int mpos = 1;

        for (int i = 1; i < 128; i++){
            if (a[i] > a[mpos]) mpos = i;
        }

        printf("%d\n", mpos * (a[mpos] >= q));
    }

    return (0);
}

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int n, q, m, mx, t, s;
	bool a[51][101], f;

	while(1){
		cin >> n >> q;
		if(n == 0 && q == 0){
			break;
		}
		mx = 0;
		fill(&a[0][0], &a[50][100], false);

		for(int i = 0; i < n; i++){
			cin >> m;
			for(int j = 0; j < m; j++){
				cin >> t;
				a[i][t] = true;
			}
			if(t > mx){
				mx = t;
			}
		}
		m = 0;
		f = true;
		for(int i = 1; i <= mx; i++){
			t = 0;
			for(int j = 0; j < n; j++){
				if(a[j][i]){
					t++;
				}
			}
			if(m < t){
				m = t;
				s = i;
			}
		}

		if(m >= q){
			cout << s << endl;
		} else {
			cout << 0 << endl;
		}
	}

	return 0;
}


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

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

  int FB, F, B;
  FB = N / 15;
  F = N / 3;
  B = N / 5;

  long long ans = 0;

  for(int i = 1; i <= N; i++) {
    ans += i;
  }

  for(int i =1 ; i <= F; i++) {
    ans -= i*3;
  }

  for(int i =1 ; i <= B; i++) {
    ans -= i*5;
  }

  for(int i =1 ; i <= FB; i++) {
    ans += i*15;
  }

  cout << ans << endl;
}

#include<bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define sci(x) int x; scanf("%d", &x);
#define TC(x) sci(x); while(x--)
typedef long long ll;
using namespace std;
int main()
{
    int n;
    cin>>n;
    ll sum = 0;
    for(int i=1;i<=n;i++){
        if(i%5!=0 && i%3!=0) sum+=i;
    }
    cout<<sum<<endl;
    return 0;
}


#if __has_include("debug.h")
    #include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define d(...) 2;
#endif

#define int long long int
#define ld long double

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"

#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ff first
#define ss second
#define eps 1e-10
#define PI acos(-1LL)

#define gcd(a,b)            __gcd((a),(b))
#define lcm(a,b)            ((a*b)/gcd(a,b))

void solve();

int32_t main()
{
    #ifdef DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
    #endif

    fast

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

	while(t--)
	    solve();
	return 0;
}

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

	vector<int>a(n);

	multiset<int>s;

	for(i=0;i<n;i++)
	{
		cin>>a[i];
		// s.insert(a[i]);
	}

	int cnt=0;

	for(i=0;i<n;i++)
	{
		auto it=s.lower_bound(a[i]);
		if(it==s.begin())
		{
			cnt++;
		}
		s.insert(a[i]);
	}
	cout<<cnt<<endl;

}

#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define vec(s) vector<s>
#define vvec(s) vector<vector<s>>
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli,lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes(x) cout<<(x?"Yes":"No")<<endl;
#define YES(x) cout<<(x?"YES":"NO")<<endl;
#define out(s) cout<<s<<endl;
#define Reverse(x) reverse(all(x));
#define Sort(x) sort(all(x));
#define pb(s) push_back(s);
#define sp " "
#define INF 10000000000
#define LINF 9000000000000000000
#define all(s) s.begin(),s.end()
void vout(vi v){
    for(lli i=0;i<v.size();i++)
    cout<<v.at(i)<<endl;
}
int main(){
    cout << std::fixed << std::setprecision(10);
    string s;
    cin >> s;
    if(s.size()==3)
    {
        Reverse(s);
    }
    out(s);
}


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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint& arg1, const pint& arg2) { return arg1.first > arg2.first; }
template<class T> void chmax(T& a, T b) { if (a < b) { a = b; } }
template<class T> void chmin(T& a, T b) { if (a > b) { a = b; } }

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

    cout << n / 3 << endl;
    return 0;
}

#include<bits/stdc++.h>
#define lg long long int
#define loop(i,s,e) for(lg i=s;i<=e;i++)
#define iloop(i,s,e) for(lg i=e;i>=s;i--)
#define pb push_back
#define mp make_pair
#define mod 1000000007

using namespace std;

lg min(lg a,lg b)
{return a<b?a:b;}
lg max(lg a,lg b)
{return a>b?a:b;}
lg lcm(lg a ,lg b)
{
    return (a*b)/__gcd(a,b);
}

int main()
{

    lg n;
    cin>>n;

    cout<<n+(n*n)+(n*n*n)<<endl;

}


#include<bits/stdc++.h>
#include<cctype>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define all(v) (v).begin(),(v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int,int>

int main(){
  ll N; cin >> N;
  ll a, b, c, d, e; cin >> a >> b >> c >> d >> e;
  ll m = min({a,b,c,d,e});
  ll retval = (N+m-1ll)/m + 4ll;
  cout << retval << endl;

  // cout << fixed << setprecision(10);

  return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <bitset>
#define ll long long
using namespace std;
const int MOD = 1000000007;
const long long INF = 1LL << 60;
int main() {
   cin.tie(0);
   ios::sync_with_stdio(false);
   ll k, t;
   cin >> k >> t;
   vector<ll> a(t);
   for(ll i = 0;i < t;i++) {
      cin >> a[i];
   }
   ll cnt = 0;
   ll before_idx = -1;
   for(ll i = 0;i < k;i++) {
      ll m = 0;
      ll idx = -1;
      for(ll j = 0;j < t;j++) {
         if (before_idx != j) {
            if (m < a[j]) {
               m = a[j];
               idx = j;
            }
         }
      }
      if (m==0) {
         a[before_idx]--;
         cnt++;
      } else {
         a[idx]--;
         before_idx = idx;
      }
   }
   cout << cnt << endl;
}


#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
	int a, b, c, k;
	cin >> a >> b >> c >> k;

	vector<int> x = {a, b, c};
	int ans = 0;
	for(int i = 0; i < k; ++i) {
		sort(x.begin(), x.end());
		x[2] *= 2;
	}

	cout << accumulate(x.begin(), x.end(), 0) << endl;

	return 0;
}

#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << endl;
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

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

    vi v(3);
    REP(i, 3) cin >> v[i];

    sort(ALL(v), greater<>());

    int k;
    cin >> k;

    print(v[0] * (1 << k) + v[1] + v[2]);

}


#include<iostream>

int main() {
  int a; std::cin >> a;
  std::cout << a + a*a + a*a*a << std::endl;
}

// Example program
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{

    ll n;
    cin >> n;
    ll s1 = pow (n,2);
    ll s2 = pow (n,3);
    cout << n + s1 + s2 << endl;

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};

int main(){
    ll x;
    cin >> x;
    ll ans = (x/11)*2;
    x %= 11;
    if (x==0) {;}
    else if (x<=6) ans++;
    else ans += 2;
    cout << ans << endl;
    return 0;
}

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(rng(s));
  bool ok = false;
  int cnt = 0, nt = 0;
  rep(i,n) {
    if(s[i]=='T') nt++;
    if(!ok&&s[i]=='T') ok = true;
    else if(ok&&s[i]=='S'&&nt>0) {
      cnt++;
      nt--;
    }
  }
  int ans = n-cnt*2;
  if(ans<0) ans = 0;
  cout << ans << endl;
  return 0;
}


//#define _USE_MATH_DEFINES
#include <iostream>
//#include <stdio.h>
//#include <iomanip>
//#include <vector>
//#include <string>
//#include <algorithm>
//#include <functional>
//#include <cmath>

using namespace std;

class Dice
{
public:
	Dice(int num[6]);
	int Play(char c);
	int T;
private:
	int NS[4],EW[4];
	int NS_p,EW_p;
};

//?????????????????????
Dice::Dice(int num[6])
{
	NS_p = 0;
	EW_p = 0;
	NS[0] = num[0];
	NS[1] = num[1];
	NS[2] = num[5];
	NS[3] = num[4];
	EW[0] = num[0];
	EW[1] = num[3];
	EW[2] = num[5];
	EW[3] = num[2];
	T = num[0];
}

int Dice::Play(char c){

	if (c == 'N' || c == 'S'){
		if(c == 'N') NS_p = (NS_p + 1) % 4;
		else {
			if (NS_p == 0) NS_p = 3;
			else NS_p--;
		}
		EW[EW_p] = NS[NS_p];
		EW[(EW_p+2)%4] = NS[(NS_p+2)%4];
	}
	else if (c == 'E' || c == 'W'){
		if (c == 'E') EW_p = (EW_p + 1) % 4;
		else {
			if (EW_p == 0) EW_p = 3;
			else EW_p--;
		}
		NS[NS_p] = EW[EW_p];
		NS[(NS_p + 2) % 4] = EW[(EW_p + 2) % 4];
	}
	T = NS[NS_p];

	return 1;
}

int main(){

	int num[6];
	char c;

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

	Dice d1(num);

	while (cin >> c){
		d1.Play(c);
	}

	cout << d1.T << endl;

	return 0;
}

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n);
  rep(i, n) cin >> x[i];

  int ans = 2e9;
  for (int l = 0; l + k - 1 < n; ++l) {
    int r = l + k - 1;
    if (x[l] >= 0) ans = min(ans, x[r]);
    else if (x[r] <= 0) ans = min(ans, -x[l]);
    else if (x[r] + x[l] >= 0) ans = min(ans, -2 * x[l] + x[r]);
    else ans = min(ans, 2 * x[r] - x[l]);
  }
  cout << ans << endl;
  return 0;
}


#include<stdio.h>

int main(){
	char date[10], month[10], year[10];
	scanf("%[^/]/%[^/]/%s", year, month, date);
	printf("2018/01/%s", date);
	return 0;
}

#include<stdio.h>

struct SI{int top;int west;int south;int east;int north;int back;};

struct SI E(struct SI P);
struct SI W(struct SI);
struct SI S(struct SI P);
struct SI N(struct SI P);

int main(){

struct SI P;
char q;

scanf("%d%d%d%d%d%d",&P.top, &P.south, &P.east, &P.west, &P.north, &P.back);
while(scanf("%c",&q)!=EOF){
if(q=='E')P=E(P);
if(q=='N')P=N(P);
if(q=='W')P=W(P);
if(q=='S')P=S(P);

}
printf("%d\n",P.top);

return 0;
}

struct SI N(struct SI P){
int t=P.top;
P.top=P.south;P.south=P.back;P.back=P.north;P.north=t;
return P;
}

struct SI S(struct SI P){
int t=P.top;
P.top=P.north;P.north=P.back;P.back=P.south;P.south=t;
return P;
}
struct SI E(struct SI P){
int t=P.top;
P.top=P.west;P.west=P.back;P.back=P.east;P.east=t;
return P;
}
struct SI W(struct SI P){
int t=P.top;
P.top=P.east;P.east=P.back;P.back=P.west;P.west=t;
return P;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

ll sp[100010],sm[100010];

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

    int z=0;
    vector<ll> x(n);

    vector<ll> p,m;

    rep(i,n){
        cin>>x[i];
        if(x[i]==0) z++;
        else if(x[i]>0){
            p.push_back(x[i]);
        }
        else{
            m.push_back(-x[i]);
        }
    }

    reverse(all(m));
    ll ans=1LL<<60;
    if(z==1){
        k--;
        if(k==0){
            cout<<0<<endl;
            return 0;
        }
    }
    if(p.size()>=k){
        ans=min(ans,p[k-1]);
    }
    if(m.size()>=k){
        ans=min(ans,m[k-1]);
    }

    for(int i=1;i<k;i++){
        if(m.size()<i) break;
        if(p.size()<k-i) continue;
        ans=min(ans,2*m[i-1]+p[k-i-1]);
    }

    for(int i=1;i<k;i++){
        if(p.size()<i) break;
        if(m.size()<k-i) continue;
        ans=min(ans,2*p[i-1]+m[k-i-1]);
    }

    cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
main(){
  while(1){
    long long n,m;
    long long sum=0;
    cin>>n>>m;
    if(n==0&&m==0) break;
    long long p[1005];
    for(int i=0;i<n;i++){
      cin>>p[i];
      sum+=p[i];
    }
    sort(p,p+n,greater<long long>());
    for(int i=1;i<=n;i++){
      if(i%m==0){
	sum-=p[i-1];
      }
    }
    cout<<sum<<endl;
  }
}


#include <bits/stdc++.h>

using namespace std;

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

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T    In(T& x){ cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Rect {
  int h, w, d;
  Rect(int h, int w) : h(h), w(w), d(sq(h) + sq(w)) {}
  bool operator<(const Rect& x) const {
    return d == x.d ? h < x.h : d < x.d;
  }
};

int main()
{
  vector<Rect> v;
  int h, w;
  for (int x = 1; x <= 150; x++){
    for (int y = 1; y < x; y++){
      v.push_back(Rect(y, x));
    }
  }
  sort(v);

  while (h = in(), w = in(), h || w){
    Rect r = Rect(h, w);
    int index = ubnd(v, r);
    printf("%d %d\n", v[index].h, v[index].w);
  }
  return (0);
}

#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
int main() {
	int h, w;
	while (cin >> h >> w&&h != 0 && w != 0) {
		int t = h*h + w*w;
		int h2 = h+1;
		while (true) {
			for (int i = h2; i*i < t; i++) {
				long double b = sqrt(t - i*i);
				if (b > (long double)i) {
					int c = b;
					if ((long double)c == b) {
						cout << i << " " << b << endl;
						goto stop;
					}
				}
			}
			t++;
			h2 = 1;
		}
	stop:;
	}
}


#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
using namespace std;
/// khodaya komak kon
/// ya navid navid
const int N=2e5+100;
ll seg[4*N];
void shift(ll nod){
    if (seg[nod]==1){
        seg[nod*2]=1;
        seg[nod*2+1]=1;
    }
}
void upd(ll nod,ll l,ll r,ll L,ll R){
    if (l>=R || L>=r){
        return ;
    }
    if (l>=L && r<=R){
        seg[nod]=1;
        return ;
    }
    shift(nod);
    ll mid=(r+l)/2;
    upd(nod*2,l,mid,L,R);
    upd(nod*2+1,mid,r,L,R);
}
ll get(ll nod,ll l,ll r,ll id){
    if (r-l==1){
        return seg[nod];
    }
    shift(nod);
    ll mid=(r+l)/2;
    if (mid>id){
        return get(nod*2,l,mid,id);
    }
    else{
        return get(nod*2+1,mid,r,id);
    }
}
ll k;
vector <pii> h;
ll st[N];
ll fn[N];
vector <int> g[N];
ll cnt=0;
ll par[N];
vector <int> t;
ll par1[N];
ll dfs(ll v,ll p,ll hi){
    par1[v]=p;
    t.pb(v);
    ll z=t.size();
    z-=k;
    z=max((ll)0,z);
    par[v]=t[z];
    h.pb({hi,v});
    st[v]=cnt;
    for (int i=0;i<g[v].size();i++){
        ll u=g[v][i];
        if (u==p){
            continue;
        }
        cnt++;
        dfs(u,v,hi+1);
    }
    t.pop_back();
    fn[v]=cnt;
}
int32_t main(){
    sync;
    ll n;
    cin >> n >> k;
    ll ans=0;
    ll x;
    cin >> x;
    if (x!=1){
        ans++;
    }
    for (int i=2;i<=n;i++){
        cin >> x;
        g[x].pb(i);
        g[i].pb(x);
    }
    upd(1,0,N,0,1);
    dfs(1,1,0);
    sort(h.begin(),h.end());
    reverse(h.begin(),h.end());
    for (int i=0;i<n-1;i++){
        ll v=h[i].S;
        ll u=par[v];
        //cout << v << " " << u << endl;
        ll z=get(1,0,N,st[v]);
        if (z==1 || par1[u]==1){
            continue;
        }
        ans++;
        upd(1,0,N,st[u],fn[u]+1);
    }
    cout << ans;
}

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

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

#define all(x) (x).begin(), (x).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

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

int cuts = 0;

int go(auto& g, int K, int loc) {
    int ht = 0;
    for (int ch : g[loc]) {
        int bl = go(g, K, ch);

        if (bl + 1 > K - !!loc)
            cuts++;
        else
            ht = max(ht, bl + 1);
    }
    return ht;
}

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

    int N, K;
    cin >> N >> K;

    vvi g(N);

    vi par(N);
    for (int i = 0; i < N; i++) {
        cin >> par[i];
        --par[i];
        if (i) {
            g[par[i]].push_back(i);
        }
    }

    go(g, K, 0);
    cout << !!par[0] + cuts << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const double pi=acos(-1.0);
int n,x[105],y[105];
double ang[105];
int main()
{
 	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);
	for(int i=1;i<=n;i++)
	{
		int c=0;
		for(int j=1;j<=n;j++)if(i^j)ang[c++]=atan2(y[j]-y[i],x[j]-x[i]);
		sort(ang,ang+c);
		double mx=0;
		ang[c]=ang[0]+2*pi;
		for(int j=0;j<c;j++)mx=max(mx,ang[j+1]-ang[j]);
		mx-=pi;
		if(mx<0)mx=0;
		printf("%.10lf\n",mx/(2*pi));
	}
 	return 0;
}

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

int main(){
    int n;
    cin >> n;
    vector<int> a;
    ll sum = 0;
    a.push_back(0);
    rep(i,n){
        int c;
        cin >> c;
        a.push_back(c);
    }
    a.push_back(0);
    n += 2;
    rep(i,n-1){
        sum += abs(a[i+1] - a[i]);
    }
    for(int i = 1;i < n-1;i++){
        cout << sum - abs(a[i+1] - a[i]) -abs(a[i] - a[i-1]) + abs(a[i+1] - a[i-1]) << endl;
    }
}


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

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

    vector<int> a(n+1);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    vector<int> b, all(n+1);
    for(int i = n; i >= 1; i--) {
        int cnt = 0;
        for(int j = i; j <= n; j+=i) {
            if(all[j] == 1)
                cnt++;
        }

        if((cnt % 2 == 0 && a[i] == 1) ||
           (cnt % 2 == 1 && a[i] == 0)) {
            all[i] = 1;
            b.push_back(i);
        }
    }

    int bs = (int)b.size();

    cout << bs << endl;
    rep(i,bs) cout << b[i] << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,cnt=0;
  cin >> n;
  int p[n],m=(1<<30);
  for(int i=0;i<n;i++){
    cin >> p[i];
    if(m>p[i]){
      cnt++;
      m=min(m,p[i]);
    }
  }
  cout << cnt << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read() {
	int x = 0, fh = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') fh = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		x = (x << 1) + (x << 3) + c - '0';
		c = getchar();
	}
	return x * fh;
}
const int N = 1010;
int n, ans, i, a[N];
int main() {
    n = read();
    for (i = 1; i <= n * 2; i++) a[i] = read();
    sort (a + 1, a + 1 + n * 2);
    for (i = 1; i <= n * 2; i += 2) ans += a[i];
    cout << ans << '\n';
	return 0;
}

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

int main(){
    int n;
    cin >> n;
    vector<int>vec = {};

    if(n == 0){
        cout << "0" << endl;
        return 0;
    }

    while(n != 1){
        if(n%2 == 1 || n%2 == -1){
            vec.push_back(1);
            n--;
            n /= (-2);
        }
        else {
            vec.push_back(0);
            n /= (-2);
        }
    }vec.push_back(1);

    for(int i = (int)vec.size() - 1 ;i>=0;i--){
        cout << vec[i];
    }
    cout << endl;
}


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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define ll long long int
#define MOD ((int)(1e9) + 7)

using namespace std;

class BNiceShopping {
public:
    void solve(istream& cin, ostream& cout) {
        int a, b, m;
        cin >> a >> b >> m;
        int minA = INT_MAX, minB = INT_MAX;
        vector<int> sa, sb;
        for(int i=0; i<a; i++) {
            int tp;
            cin >> tp;
            minA = min(minA, tp);
            sa.push_back(tp);
        }
        for(int i=0; i<b; i++) {
            int tp;
            cin >> tp;
            minB = min(minB, tp);
            sb.push_back(tp);
        }
        int mincost = minA+minB;
        for(int i=0; i<m; i++) {
            int x, y, c;
            cin >> x >> y >> c;
            mincost = min(mincost, sa[x-1] + sb[y-1] - c);
        }
        cout << mincost << '\n';
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    BNiceShopping solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}

//----------------------------------------------------------------------
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
//----------------------------------------------------------------------

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

int main(void) {
    int n; cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> c(n);
    rep(i, n)
        cin >> a[i];
    rep(i, n)
        cin >> b[i];
    rep(i, n)
        cin >> c[i];

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());

    ll ans = 0;
    for(int i = 0; i < n; ++i) {
        ll key = b[i];
        ll numc = c.end() - upper_bound(c.begin(), c.end(), key);
        ll numa = lower_bound(a.begin(), a.end(), key) - a.begin();
        ans += numa * numc;
    }
    cout << ans << endl;
    return 0;
}


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

int main(){
	int N, K;
	cin >> N >> K;
	int ans = 0;
	for(int i = 1; i <= N - K + 1; ++i) {
		ans++;
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr ll inf = LLONG_MAX;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ----------------------end----------------------- */

int main() {
    int n,k;cin >>  n >> k;
    vi a(n);
    rep(i, n) cin >> a[i];
    resort(a);
    int ans = 0;
    rep(i, k) ans += a[i];
    cout << ans << endl;
    return 0;
}


const int LG = 21;
const int FN = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(FN);
    __ufact.resize(FN);
    __rev.resize(FN);
    __rev[1] = 1;
    for (int i = 2; i < FN; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < FN; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (k < 0 || k > n) return 0;
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int Root(int x, vector<int> &root)
{
    if (x == root[x]) return x;
    return root[x] = Root(root[x], root);
}

void Merge(int v, int u, vector<int> &root, vector<int> &sz)
{
    v = Root(v, root), u = Root(u, root);
    if (v == u) return;
    if (sz[v] < sz[u])
    {
        sz[u] += sz[v];
        root[v] = u;
    }
    else
    {
        sz[v] += sz[u];
        root[u] = v;
    }
}

int ok(int x, int n)
{
    return 0 <= x && x < n;
}

void bfs(int v, vi &dist, vector<vi> &graph)
{
    fill(all(dist), -1);
    dist[v] = 0;
    vi q = {v};
    for (int i = 0; i < q.size(); i++)
    {
        for (auto u : graph[q[i]])
        {
            if (dist[u] == -1)
            {
                dist[u] = dist[q[i]] + 1;
                q.push_back(u);
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    if (s == "zyxwvutsrqponmlkjihgfedcba")
    {
        cout << "-1";
        return 0;
    }
    vector<int> la(26);
    forn(i, s.size()) la[s[i] - 'a']++;
    for (int i = 0; i < 26; i++) if (la[i] == 0)
    {
        cout << s;
        cout << (char)(i + 'a');
        return 0;
    }
    string t = s;
    next_permutation(all(t));
    for (int i = 0; i < t.size(); i++)
    {
        if (s[i] == t[i]) cout << s[i];
        else
        {
            cout << t[i];
            return 0;
        }
    }
}

/* Note:
Check constants at the beginning of the code.
    N is set to 4e5 but be careful in problems with large constant factor.
    Setting N in every problem is more effective.
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/


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

typedef long long ll;

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

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

const char nl = '\n';

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
    int h, w; cin >> h >> w;
    int canvas[h + 2][w + 2];
    FOR(i, 1, h + 1) {
        string s; cin >> s;
        FOR(j, 1, w + 1) canvas[i][j] = s[j];
    }
    bool ok = true;
    F0R(i, h) {
        F0R(j, w) {
            if (canvas[i][j] == '#') {
                if (canvas[i+1][j] == '.' && canvas[i - 1][j] == '.' && canvas[i][j + 1] == '.' && canvas[i][j - 1] == '.') {
                    ok = false;
                    break;
                }
            }
        }
    }
    cout << (ok ? "Yes\n": "No\n");
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
// #define INF __INT32_MAX__
#define INF 1e9
#define LINF __LONG_LONG_MAX__

int main() {
    int K, T; cin >> K >> T;
    vi A(T); rep(i, T) cin >> A[i];

    sort(A.begin(), A.end());
    reverse(A.begin(), A.end());

    int x = A[0];
    for (int i = 1; i < T; i++) {
        x -= A[i];
    }

    if (x > 0) {
        cout << (x - 1) << endl;
    } else {
        cout << 0 << endl;
    }
}


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

/*
提出前に確認!
- setとmultisetを間違えない
*/

int mod = 1e9 + 7;

int	main(void)
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	long long ans = 0;
	long long all = 0;
	for (int i = 0; i < n; i++)
	{
		ans += all * a[i] % mod;
		ans %= mod;
		all += a[i];
		all %= mod;
	}
	cout << ans << endl;
	return (0);
}

#include <bits/stdc++.h>
#define ll  long long
#define ull unsigned long long
using namespace std;
#define  inf 1e6+18
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define mod 1000000007
#define  f1(i,n) for(i=1;i<=n;i++)
#define f0(i,n)  for(i=0;i<n;i++)
#define w(x)   while(x--)
const int maxn=5001;
#define  pq priority_queue
#define  ff first
#define  ss second
#define pb  push_back
#define lb lower_bound

ll k1=0;
void solve(string s,string r)
{
   int z[maxn]={};
   string t=r;
   t+'#';
   t+=s;
   //for(i=0;i)
   z[0]=0;
   int left=0;
   int right=0;
   int i,j,k,l,n;
   n=t.size();
   for(int k=1;k<n;k++)
   {
   	if(k>right)
   	{
   		right=k;
   		left=k;
   		while(k<n&&t[right-left]==t[right])
   			right++;
   		z[k]=right-left;
   		right--;

   	}
   	else
   	{
   		int k1=k-left;
   		if(z[k1]<right-k+1)
   			z[k]=z[k1];
   		else
   		{
   			left=k;
   			while(k<n&&t[right-left]==t[right])
   			right++;
   		     z[k]=right-left;
   		   right--;

   		}
   	}
   }
   k=r.size();
   k1=k;
   for(i=r.size();i<=t.size()-r.size();i++)
   {
   	k1=min((int)k1,k-z[i]);
   }
}
int main()
{
	fastio;
	int t;
	ll i,j,k,l,n;
	cin>>n;
	ll a[n+1]={},b[n+1]={};
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		a[i]=a[i]%mod;
		b[i]=b[i-1]+a[i];
		b[i]=b[i]%mod;

	}
	l=0;
	for(i=1;i<n;i++)
	{
		l=(l+((b[n]-b[i]+mod)*a[i]%mod))%mod;
	}
	cout<<l;

}
//Lordvishnu@almighty


#include<iostream>
#include<cmath>

using namespace std;

int main() {
    while (1) {
        int h, w;
        cin >> h >> w;
        if (h == 0 && w == 0) break;
        int m = h * h +  w * w;
        bool end = false;
        for (int m = h * h + w * w; m <= 45000; ++m) {
            //cout << m << endl;
            int bh = m == h * h + w * w ? h + 1 : 1;
            for (; bh <= 150; ++bh) {

                if (m > bh * bh) {
                    int r = m - bh * bh;
                    int bw = int(floor(sqrt(r)));
                    //cout << bh << " " << bw << endl;
                    if (bw * bw == r) {
                        if (bw <= bh) break;
                        cout << bh << " " << bw << endl;
                        end = true;
                        break;
                    }
                }
            }
            if(end)break;
        }

    }
}

//#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"

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

#define MOD 1000000007LL
#define INF (1LL<<60LL)
#define int long long
#define MAX_N (100001)
typedef long long ll;

using namespace std;

vector<vector<bool>> v(100, vector<bool>(100,1));

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int a, b; cin >> a >> b;
	bool rev = false;
	if (a > b) {
		swap(a, b);
		rev = true;
	}
	int i = 0, j = 0;
	if (b > 1)a--;
	rep(_, a) {
		v[i][j] = 0;
		j += 2;
		if (j >= 100) {
			i++;
			j = i % 2;
		}
	}
	b -= max(0LL, a - 50);
	i += 3;
	j = 1;
	rep(_, b-1) {
		rep(i2, 3)rep(j2, 3) {
			v[i + i2 - 1][j + j2 - 1] = 0;
		}
		v[i][j] = 1;
		j += 2;
		if (j >= 98) {
			i+=2;
			j = 1;
		}
	}
	char m[2];
	if (!rev) {
		m[0] = '.';
		m[1] = '#';
	}else  {
		m[1] = '.';
		m[0] = '#';
	}
	cout << 100 << " " << 100 << "\n";
	rep(i2, 100)rep(j2, 100) {
		cout << m[v[i2][j2]];
		if (j2 == 99) cout << "\n";
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 100 + 10;

int total, N;
int a[MAXN]= {0};
int st[MAXN]= {0}, ed[MAXN]= {0};
int odd[MAXN]= {0}, ocnt = 0;

int ans[MAXN]= {0}, acnt = 0;

int getnum () {
	int num = 0;
	char ch = getchar ();

	while (! isdigit (ch))
		ch = getchar ();
	while (isdigit (ch))
		num = (num << 3) + (num << 1) + ch - '0', ch = getchar ();

	return num;
}

int main () {
	total = getnum (), N = getnum ();
	for (int i = 1; i <= N; i ++) {
		a[i] = getnum ();
		if (a[i] & 1) odd[++ ocnt] = i;
	}
	if (N == 1) {
		printf ("%d\n", a[1]);
		if (total == 1) puts ("1"), puts ("1");
		else {
			puts ("2");
			printf ("1 %d\n", total - 1);
		}
		return 0;
	}
	if (ocnt > 2) {
		puts ("Impossible");
		return 0;
	}
	if (ocnt >= 1) swap (a[1], a[odd[1]]);
	if (ocnt >= 2) swap (a[N], a[odd[2]]);
	ans[++ acnt] = a[1] + 1;
	for (int i = 2; i < N; i ++) ans[++ acnt] = a[i];
	if (a[N] - 1 > 0) ans[++ acnt] = a[N] - 1;
	for (int i = 1; i <= N; i ++) {
		if (i > 1) putchar (' ');
		printf ("%d", a[i]);
	}
	printf ("\n%d\n", acnt);
	for (int i = 1; i <= acnt; i ++) {
		if (i > 1) putchar (' ');
		printf ("%d", ans[i]);
	}
	puts ("");

	return 0;
}

/*
3 2
2 1
*/

/*
6 1
6
*/

/*
55 10
1 2 3 4 5 6 7 8 9 10
*/


#include<bits/stdc++.h>
#define int long long

using namespace std;

const int maxn = 200010;
int n, ans;

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

inline void write(int x)
{
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top) putchar(sta[-- top] + 48);
}

signed main()
{
    n = read();
    ans = (n - 2) * 180;
    write(ans);
    return 0;
}

/*
M-SOLUTIONS プロコンオープン
3以上の整数Nが与えられる。
正N角形の内角の和を求める。
制約
・3<=N<=100
方針
N角形の内角の和は、(N-2)*180
で求めらえる。
*/

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

const ll INF = 1LL<<62;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

using Graph = vector<vector<int>>;

typedef pair<int, int> P;
const int MOD = 1000000007;

int main(){
    int N; cin >> N;
    cout << (N-2)*180 << endl;
}


#include <iostream>
#include <iomanip> //setprecision
#include <cstdlib>
#include <cmath>

double abs(double coodinate1, double coodinate2);

int main(void) {
    double x1;
    double y1;
    double x2;
    double y2;
    std::cin >> x1 >> y1 >> x2 >> y2;
    double dif_x = abs(x1, x2);
    double dif_y = abs(y1, y2);
    double target = dif_x * dif_x + dif_y * dif_y;

    std::cout << std::setprecision(12);
    std::cout << sqrt(target) << std::endl;

    return EXIT_SUCCESS;
}

double abs(double coodinate1, double coodinate2) {
    double dif = coodinate1 - coodinate2;
    if (dif < 0) {
        dif *= (-1.0);
    }
    return dif;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  ll N,A,B;
  cin >> N >> A >> B;
  ll now,next;
  cin >> now;

  ll ans = 0;
  for(int i = 0; i < N-1; i++){
    cin >> next;
    ans += min(abs(now - next)*A, B);
    now = next;
  }

  cout << ans << endl;
}


#include<cstring>
#include<string>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<list>
#include<set>
#include<map>
#include<complex>
#include<sstream>
#include<climits>
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MAX_N = 100000;
int N, L, Q;
int x[MAX_N];
int r[20][MAX_N];

int can_reach(int i, int d) {
  if (d == 0) return i;
  int k = 0;
  while (d >= (1<<k)) ++k;
  --k;
  return can_reach(r[k][i], d-(1<<k));
}

int main() {
  cin >> N;
  rep(i,N) cin >> x[i];
  cin >> L >> Q;

  for (int i = 0; i < N; ++i) {
    r[0][i] = upper_bound(x, x+N, x[i]+L) - x - 1;
  }

  int K = 0;
  while (N >= (1<<K)) ++K;
  for (int k = 1; k < K; ++k) {
    for (int i = 0; i < N; ++i) {
      r[k][i] = r[k-1][r[k-1][i]];
    }
  }

  while (Q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a; --b;
    if (a > b) swap(a,b);

    int lb = 0, ub = N, d = 0;
    while (ub-lb > 1) {
      d = (ub+lb)/2;
      if (can_reach(a, d) >= b) ub = d;
      else lb = d;
    }
    printf("%d\n", ub);
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repb(i, n) for(ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for(ll i = (a); i < (b); i++)
#define reprb(i, a, b) for(ll i = (b)-1; i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b){ return b?GCD(b, a % b):a; }
inline ll LCM(ll a, ll b){ return a/GCD(a, b)*b; }
inline ll powmod(ll x, ll y, ll m = MOD){ ll r=1; while(y){ if(y&1) r*=x; x*=x; r%=m; x%=m; y>>=1; } return r; }
template<class S, class T>bool chmax(S &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class S, class T>bool chmin(S &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

const int M = 100010;
ll n;
vll x;
int d[18][M];

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cin >> n;
    x.resize(n);
    rep(i, n){
        cin >> x[i];
    }
    ll l;
    cin >> l;

    rep(j, n){
        d[0][j] = upper_bound(ALL(x), x[j] + l) - x.begin() - 1;
    }
    repr(i, 1, 18)rep(j, n){
        d[i][j] = d[i-1][d[i-1][j]];
    }
    /*
    rep(i, 18)rep(j, n){
        cout << d[i][j] << " \n"[j+1==n];
    }
    cout << endl;
    */
    ll q;
    cin >> q;
    ll a, b;
    rep(qq, q){
        cin >> a >> b;
        a--;b--;
        if(a > b) swap(a, b);
        ll ans = 0;
        ll p = a;
        while(1){
            rep(i, 18){
                //cout << i << " " << p << endl;
                if(d[i][p] >= b){
                    if(i == 0){
                        ans++;
                        goto enendd;
                    }else{
                        ans += powmod(2, i-1);
                        p = d[i-1][p];
                        break;
                    }
                }
            }
        }
        enendd:
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//(a+b-1)/b
//priority_queue<ll, vector<ll>, greater<ll>> q;

signed main(){
	ll n,a,b; cin>>n>>a>>b;
	cout<<(n-a+1)*(n-b+1)<<endl;

	return 0;
}

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

int main() {
  int n,h,w;
  cin >> n >> h >> w;
  int a = 0;
  int b = 0;
  for(int i = 1; i <= n; i++){
    if(i+w-1 <= n) a++;
    if(i+h-1 <= n) b++;
  }
  cout << a*b << endl;
  return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <numeric>
#include <functional>

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

using namespace std;
typedef long long ll;

const ll Inf = 1L << 61;

int main(int argc, char *argv[])
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int V, E;
  cin >> V >> E;
  ll d[V][V];
  REP(i,V) REP(j,V) {
    if (i == j) d[i][j] = 0;
    else d[i][j] = Inf;
  }
  REP(i,E) {
    ll s, t, c;
    cin >> s >> t >> c;
    d[s][t] = c;
  }
  REP(k,V) REP(i,V) REP(j,V) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  REP(i,V) if (d[i][i] < 0) {
    cout << "NEGATIVE CYCLE" << endl;
    return 0;
  }
  REP(i,V) REP(j,V) {
    if (d[i][j] >= Inf >> 10) cout << "INF";
    else cout << d[i][j];
    if (j < V-1) cout << ' ';
    else cout << '\n';
  }
  cout << flush;

  return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
    char s[4];
    cin >> ("%s",s);
    if(s[0]=='7'||s[1]=='7'||s[2]=='7')
    cout << ("Yes");
    else
    cout << ("No");
}

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

#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<double> > vvd;
typedef vector<vector<string> > vvs;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

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;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
//以降 cin の入力元が 'input.txt' になる
//std::ifstream in("input.txt");
//std::cin.rdbuf(in.rdbuf());

signed main()
{
    gearup;
    string a,b,c; cin >> a >> b >> c;
    int A = a.size();
    int B = b.size();
    int C = c.size();
    if(a[A-1] == b[0] && b[B-1] == c[0])out("YES");
    else out("NO");
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100005;
int n,L,Q,x[maxn],f[maxn][18],g[maxn][18];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&x[i]);
	scanf("%d",&L);
	for(int i=1;i<=n;i++)
		f[i][0]=upper_bound(x+1,x+n+1,x[i]+L)-x-1;
	for(int j=1;j<=17;j++)
		for(int i=n;i;i--)
			f[i][j]=f[f[i][j-1]][j-1];
	for(int i=1;i<=n;i++)
		g[i][0]=lower_bound(x+1,x+n+1,x[i]-L)-x;
	for(int j=1;j<=17;j++)
		for(int i=1;i<=n;i++)
			g[i][j]=g[g[i][j-1]][j-1];
	int a,b;
	scanf("%d",&Q);
	while(Q--)
	{
		scanf("%d%d",&a,&b);
		int ans=0;
		if(a<b)
		{
			for(int i=17;i>=0;i--)
				if(f[a][i]<b) a=f[a][i],ans+=(1<<i);
		}
		else
		{
			for(int i=17;i>=0;i--)
				if(g[a][i]>b) a=g[a][i],ans+=(1<<i);
		}
		printf("%d\n",ans+1);
	}
	return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <stdio.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <list>
#include <math.h>
#include <map>
#include <iomanip>
#include <functional>
#include <string>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os;
}
template<class T> istream& operator>>(istream& is, vector<T>& v) {
	for (auto i = begin(v); i != end(v); i++) is >> *i; return is;
}

#define MOD 1000000007

struct Q {
	int b_id;
	int steps;
	int q_id;

	Q(int b_id, int q_id) : b_id(b_id), q_id(q_id), steps(0) {}

	bool operator<(const Q& other) const
	{
		return std::tie(b_id, q_id) < std::tie(other.b_id, other.q_id);
	}
};

int ofs[1000000];
int ans[1000000];
vector<set<Q>> qs;

void merge_into(int from, int to)
{
	if (qs[from].size() > qs[to].size())
	{
		swap(qs[from], qs[to]);
		swap(ofs[from], ofs[to]);
	}
	for (auto x : qs[from])
	{
		x.steps += ofs[from] - ofs[to];
		qs[to].insert(x);
	}
	qs[from].clear();
}

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

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

	int n;
	cin >> n;

	vector<int> x(n);
	for (int i = 0; i < n; i++)
		cin >> x[i];

	qs.resize(n);

	int L;
	cin >> L;

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

		if (a > b)
			swap(a, b);
		qs[a].insert(Q(b, i));
	}

	for (int i = 0; i < n; i++)
	{
		while (qs[i].size() > 0 && qs[i].begin()->b_id <= i)
		{
			auto f = *qs[i].begin();
			qs[i].erase(qs[i].begin());
			ans[f.q_id] = f.steps + ofs[i];
		}
		ofs[i]++;
		int to = lower_bound(x.begin(), x.end(), x[i] + L + 1) - x.begin() - 1;
		merge_into(i, to);
	}

	for (int i = 0; i < q; i++)
		cout << ans[i] << "\n";

	return 0;
}


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

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

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

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

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

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

    return 1;
}
   long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
    lint lcm(lint a,lint b){
        return a/__gcd(a,b)*b;
    }
     lint gcd(lint a,lint b){
        return __gcd(a,b);
    }
  int main(){
      lint n;
      cin>>n;
      tuple<lint,lint,lint> a[n];
      tuple<lint,lint,lint>b[n];
      rep(i,n){
          lint a1,b1;
          cin>>a1>>b1;
          a[i]=make_tuple(b1+a1,a1,i);
          b[i]=make_tuple(a1+b1,b1,i);
      }
      bool iseat[n];
      fill(iseat,iseat+n,false);
      sort(a,a+n,greater<tuple<lint,lint,lint>>());
      sort(b,b+n,greater<tuple<lint,lint,lint>>());
      lint ans=0;
      lint i=0,j=0,k=0;
      while(1){
          lint s1,v1,c1;
          tie(s1,v1,c1)=a[i];
           while(iseat[c1]){
               i++;
                tie(s1,v1,c1)=a[i];
           }
           iseat[c1]=true;
           ans+=v1;
           k++;
           if(k==n)break;
            tie(s1,v1,c1)=b[j];
           while(iseat[c1]){
               j++;
                tie(s1,v1,c1)=b[j];
           }
           iseat[c1]=true;
           ans-=v1;
           k++;
           if(k==n)break;
           i++;j++;
      }
      cout<<ans<<endl;
  }

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <numeric>
#include <sstream>
#include <iomanip>
using lint = long long;

int main()
{

    lint n;
    scanf("%lld", &n);

    std::vector<lint> a(n);
    std::vector<lint> b(n);

    std::vector<std::pair<lint, lint>> ab(n);

    for (int i = 0; i < n; ++i)
    {
        lint a_e, b_e;
        scanf("%lld", &a_e);
        scanf("%lld", &b_e);

        a[i] = a_e;
        b[i] = b_e;
        ab[i] = std::make_pair(-a_e - b_e, i);
    }

    std::sort(ab.begin(), ab.end());

    lint ans_a = 0;
    lint ans_b = 0;
    for (int i = 0; i < n; ++i)
    {
        lint idx = ab[i].second;
        if (i % 2 == 0)
        {
            ans_a += a[idx];
        }
        else
        {
            ans_b += b[idx];
        }
    }

    printf("%lld", ans_a - ans_b);

    return 0;
}


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

#define vt          vector
#define sz(x)       int((x).size())
#define all(x)      (x).begin(), (x).end()
#define rall(x)     (x).rbegin(), (x).rend()
#define fi          first
#define se          second

using ll = long long;
using pii = pair<int, int>;

void solve() {
    int n, m;
    cin >> n >> m;
    vt<int> p(n + 1), ac(n + 1);
    while (m--) {
        int x;
        string s;
        cin >> x >> s;
        if (s == "AC") ac[x] = 1;
        else if (!ac[x]) p[x]++;
    }

    int pen = 0;
    for (int i = 1; i <= n; i++) pen += p[i] * ac[i];
    cout << accumulate(all(ac), 0) << ' ' << pen;
}

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

    int tcs = 1;
    for (int tc = 1; tc <= tcs; tc++) {
        // cout << "Case " << tc << ": ";
        solve();
    }
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <climits>
#include <cassert>
#include <algorithm>
#include <utility>

using namespace std;
using Int = long long int;
using UInt = unsigned long long int;
using VI = vector<long long int>;
using VVI = vector<vector<long long int> >;

int main(void) {
    // 問題番号, 正解したかどうか, WAの個数
    map<Int, pair<Int, Int> > mp;
    Int n, m;
    cin >> n >> m;

    for (Int i = 0; i < m; ++i) {
        Int p;
        string s;
        cin >> p >> s;
        --p;
        if (mp[p].first == 0) {
            if (s == "AC") {
                mp[p].first = 1;
            } else {
                ++mp[p].second;
            }
        }
    }

    Int ac_num = 0;
    Int penalty_num = 0;
    for (Int i = 0; i < n; ++i) {
        if (mp[i].first != 0) {
            ac_num += mp[i].first;
            penalty_num += mp[i].second;
        }
    }
    cout << ac_num << " " << penalty_num << endl;

    return 0;
}


//
//  abc167_Bracket_Sequencing.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
ll mod = 998244353;
ll INF = 1000000007;
#define PI 3.14159265

std::map<int, int> factorization (int n) {
  std::map<int, int> f;
  for (int i = 2; i*i <= n; i++) {
    while (n%i == 0) {
      f[i] += 1;
      n /= i;
    }
  }
  if (n > 1) {
    f[n] += 1;
  }
  return f;
}

ll modpow (int x, int n) {
  if (n==0) return 1%mod;
  ll u = modpow(x, n/2);
  u = (u*u)%mod;
  if (n%2==1) u = (u*x)%mod;
  return u;
}

void dfs(vector<int> A, int n, int m)  {
  if  (A.size() > n) {
    for (int i = 1; i <= n; i++) {
      std::cout << A[i];
    }
    std::cout << std::endl;
    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A, n, m);
    A.back()++;
  }
}

const int MAX = 510000;

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

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

ll HCOM(int n, int k) {
    return (n == 0 && k == 0) ? 1 : COM(n+k-1, k);
}

int main() {
  ll n;
  std::cin >> n;
  std::vector<string> s;

  priority_queue<pair<ll, ll> > pos_q;
  priority_queue<pair<ll, ll> > neg_q;

  ll final_balance = 0;
  for (int i = 0; i < n; i++) {
    std::string t;
    std::cin >> t;
    s.push_back(t);

    ll balance = 0;
    ll min_prefix = 0;
    for (int j = 0; j < t.size(); j++) {
      if (t[j] == '(') {
        balance++;
      }
      else {
        balance--;
      }
      min_prefix = min(min_prefix, balance);
    }
    final_balance += balance;
    if (balance != 0 || min_prefix != 0) {
      if (balance >= 0) {
        pos_q.push(make_pair(min_prefix, balance));
      }
      else {
        neg_q.push(make_pair(min_prefix - balance, -balance)); // reverse sequence
      }
    }
  }

  bool check_pos = true;
  int b_pos = 0;

  while (!pos_q.empty()) {
    pair<int, int> pos = pos_q.top();
    pos_q.pop();
    int prefix = b_pos + pos.first;
    if (prefix < 0) { check_pos = false; break; }
    b_pos += pos.second;
  }

  bool check_neg = true;
  int b_neg = 0;

  while (!neg_q.empty()) {
    pair<int, int> neg = neg_q.top();
    neg_q.pop();
    int prefix = b_neg + neg.first;
    if (prefix < 0) { check_neg = false; break; }
    b_neg += neg.second;
  }

  if (final_balance == 0 && check_neg && check_pos) {
    std::cout << "Yes" << std::endl;
  }
  else {
    std::cout << "No" << std::endl;
  }

  // std::cout << " balance: " << final_balance << " prefix: " << final_prefix << std::endl;

  return 0;
}

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

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

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  __MAKE_TIME__  Problem: __PROBLEM__ / Link: __CONTEST_URL__  ----- */
/* ------問題------

-----問題ここまで----- */
/* -----解説等-----

----解説ここまで---- */

struct UnionFind {
	vector<int> data;
	UnionFind() {}
	UnionFind(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};

auto f(const vector<PII>&edge) {

	int id = 0;
	map<int, int>M; {
		for (const auto& it : edge) {
			if (M.find(it.first) == M.end()) {
				M[it.first] = id++;
			}
			if (M.find(it.second) == M.end()) {
				M[it.second] = id++;
			}
		}
	}
	const int sz = id;
	UnionFind uf(sz);
	map<int, int>roots;
	for (const auto& it : edge) {
		uf.unionSet(M[it.first], M[it.second]);
	}
	int rootid = 0;
	for (const auto& it : edge) {
		if (roots.find(uf.root(M[it.first])) == roots.end()) {
			roots[uf.root(M[it.first])] = rootid++;
		}
		//if (roots.find(uf.root(M[it.second])) == roots.end()) {
		//	// これはあってはならない
		//	assert(0);
		//	roots[uf.root(M[it.second])] = rootid++;
		// }
	}

	vector<vector<PII>>res(rootid);
	for (const auto& it : edge) {
		res[roots[uf.root(M[it.first])]].push_back(it);
	}
	return res;

}

VL Dijkstra(vector<vector<PLL>>& G, int s) {
	VL dist(SZ(G), LINF);
	dist[s] = 0;
	priority_queue<PLL, vector<PLL>, greater<PLL>> que;
	que.push(PLL(0LL, s));

	while (!que.empty()) {
		PLL p = que.top(); que.pop();
		int v; /* 頂点*/ long long d; /* 頂点vまでの距離 */
		tie(d, v) = p;

		if (d > dist[v]) continue;

		FOR(i, 0, (int)G[v].size()) {
			int nv = G[v][i].first;
			if (dist[nv] > dist[v] + G[v][i].second) {
				dist[nv] = dist[v] + G[v][i].second;
				que.push(PLL(dist[nv], nv));
			}
		}
	}
	return dist;
}
int read() { int in; scanf("%d", &in); return in; }

int main() {
	LL N = read(), M = read();

	vector<vector<PII>>Cedge(1e6);
	FOR(i, 0, M) {
		int p = read() - 1, q = read() - 1, c = read() - 1;
		Cedge[c].push_back(PII(p, q));
	}
	vector<vector<PLL>>G(N + 1e6); // 2Nでいい?
	int cid = N;

	FOR(c, 0, 1e6) {
		auto res = f(Cedge[c]);
		for (auto subedges : res) {
			set<int>used;
			for (auto e : subedges) {
				if (used.find(e.first) == used.end()) {
					G[cid].push_back(PLL(e.first, 1));
					G[e.first].push_back(PLL(cid, 1));
					used.insert(e.first);
				}
				if (used.find(e.second) == used.end()) {
					G[cid].push_back(PLL(e.second, 1));
					G[e.second].push_back(PLL(cid, 1));
					used.insert(e.second);
				}

			}
			cid++;
		}
	}
	auto res = Dijkstra(G, 0);
	LL ans = res[N - 1] / 2;
	if (ans == LINF / 2)ans = -1;
	cout << ans << "\n";

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

const int n = 50;

int main() {
    ll k;
    cin >> k;
    cout << n << endl;

    vector<ll> ret(n, n - 1);
    for (int i = 0; i < k % n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j)ret[j] += n;
            else ret[j]--;
        }
    }
    for (int i = 0; i < n; i++) ret[i] += k / n;

    for (int i = 0; i < n; i++) {
        if (i > 0)cout << " ";
        cout << ret[i];
    }
    cout << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
	while(b){
		a %= b;
		swap(a, b);
	}
	return a;
}
int who_wins(vector<int> a){
	int q[2] = {};
	for(int v : a) q[v % 2]++;
	if(q[0]%2){
		return 1;
	}
	if(q[1] > 1){
		return 2;
	}
	for(int &v : a) if(v%2){
		if(v == 1) return 2;
		v--;
	}
	int g = 0;
	for(int v : a) g = gcd(g, v);
	for(int & v : a) v /= g;
	return 3 - who_wins(a);
}
int main(){
	int n; cin >> n;
	vector<int> a(n);
	for(int i = 0; i < n; i++) cin >> a[i];
	if(who_wins(a) == 1) cout << "First" << endl;
	else cout << "Second" << endl;
	return 0;
}


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

int main(){
  int n;
  cin >> n;
  int s=0,i=0;
  while(s<=n){
    i++;
    s=i*i;
  }
  i--;
  cout << i*i << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 1e9+7;
signed main(){
    string N;
    cin >> N;
    int ans = 0;
    bool kuriage = false;
    for(int i = N.size()-1; i >= 0; i--) {
        int keta = (int)(N[i]-'0');
        if (kuriage) {
            keta++;
        }
        kuriage = false;
        if (keta > 5 || (keta == 5 && i > 0 && (int)(N[i-1]-'0') > 4) ) {
            ans+=10-keta;
            kuriage = true;
        }
        else {
            ans+=keta;
        }
    }
    if (kuriage) {
        ans++;
    }
    cout << ans << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

void b_accepted(void) {
	string s;
	cin >> s;
	bool judge_a = false, judge_b = false, judge_c = false;

	int cnt_c = 0;
	for (int i = 0; i < s.size(); i++) {
		if (i == 0) {
			if (s[i] == 'A') judge_a = true;
			else             break;
		}
		else if (s[i] == 'C'){
			if (i >= 2 && i < s.size() - 1) {
				cnt_c++;
			}
		}
		else {
			if (s[i] < 'a') {
				judge_c = false;
				break;
			}
			else judge_c = true;
		}
	}

	if (cnt_c == 1) judge_b = true;

	if (judge_a & judge_b & judge_c) {
		cout << "AC" << endl;
	}
	else {
		cout << "WA" << endl;
	}
}

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

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

int main()
{
  string S;cin>>S;
  if(S[0]!='A'||S.size()<3){printf("WA");return 0;}
  int countc=0;
  for(int i=2;i<S.size()-1;i++)if(S[i]=='C')countc++;
  if(countc!=1){printf("WA");return 0;}
  for(int i=1;i<S.size();i++)if(S[i]!='A'&&S[i]!='C'&&S[i]-'A'>=0&&S[i]-'A'<=26){printf("WA");return 0;}
  printf("AC");
}


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

using namespace std;

int main() {
  long N;
  vector<long> t;

  cin >> N;
  for (int i = 0; i < 5; ++i) {
    long k;
    cin >> k;
    t.push_back(k);
  }
  long min = *min_element(begin(t), end(t));
  cout << ((N - 1) / min) + 5 << endl;
  return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
ll mm=1000000000;ll MM=mm+7;
#define rep(i, n) for(int i=0;i<n;i++)
#define PI 3.141592653589793

int main(){
   int n;
   cin >> n;
   VI a(n+2);
   a[0]=0;
   a[n+1]=0;
   for(int i=1;i<=n;i++){
      cin >> a.at(i);
   }
   ll ro=0;
   rep(i,n+2){
      ro+=abs(a[i]-a[i+1]);
   }
   for(int i=1;i<=n;i++){
      if((a[i]-a[i-1])*(a[i+1]-a[i])>=0){
         cout << ro << endl;
      }
      else{
         int z=min(abs(a[i]-a[i-1]),abs(a[i+1]-a[i]));
         cout << ro-2*z << endl;
      }
   }
}


#include <iostream>
using namespace std;

int main() {
	int X, Y;
    cin >> X >> Y;
	if (X < 9 && Y <9){
      cout << "Yay!" << endl;
      return 0;
    }
	cout << ":(" << endl;
}

#include <iostream>
#include <stdio.h>
using namespace std;
int main(void){
    int l, i=0, a[10];
    while(2 > i){
        cin >> a[i];
        i++;
    }
    if (a[0] > 8 || a[1] > 8) {
        cout << ":(" << "\n";
    } else {
        cout << "Yay!" << "\n";
    }
}