#pragma GCC optimize "trapv"
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
// #define mp make_pair
#define pii pair<int,int>
// #define mod 998244353
#define rep(i,n) for(int i=0;i<n;i++)
#define repp(i,a,n) for(int i=a;i<n;i++)
#define all(v) v.begin(),v.end()
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)
vector<string> split(const string& s, char c) {
    vector<string> v; stringstream ss(s); string x;
    while (getline(ss, x, c)) v.push_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
    stringstream s; s << "[";
    for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
    s << arr[n - 1] << "]";
    return s.str();
}
#define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __evars_out_var(T val) { cerr << val; }
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template<typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
    cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
    __evars_out_var(a);
    cerr << "; ";
    __evars(++it, args...);
}
template<typename T_vector>
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
    if (start < 0) start = 0;
    if (end < 0) end = int(v.size());

    for (int i = start; i < end; i++)
        cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
template<typename T> istream& operator >>(istream &in,vector<T> &v){
    for(auto &x:v) in>>x; return in;
}
template<typename T> ostream& operator <<(ostream &out,vector<T> &v){
    for(auto &x:v) out<<x<<' '; return out;
}
template<typename T1,typename T2> istream& operator >>(istream &in,pair<T1,T2> &p){
    in>>p.first>>p.second; return in;
}
template<typename T1,typename T2> ostream& operator <<(ostream &out,pair<T1,T2> &p){
    out<<p.first<<' '<<p.second; return out;
}
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
template<class T>using uset=unordered_set<T,custom_hash>;
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fio;
    clock_t clk = clock();
    int t = 1;
    // cin >> t;
    rep(tc, t)
    {
        // cout << "Case #" << tc + 1 << ": ";
        vll a(3);
        cin >> a;
        sort(all(a));
        if(a[1] - a[0] == a[2] - a[1])
        cout << "Yes" << endl;
        else
        cout << "No" << endl;
    }
    cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
}

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
  int64_t n, q;
  cin >> n >> q;
  vector<int64_t> a(n);

  vector<int64_t> dsum(n);
  for (int i = 0; i < n; i++)
  {
    cin >> a[i];
    if (i > 0)
    {
      dsum[i] = dsum[i - 1] + a[i] - a[i - 1] - 1;
    }
    else
    {
      dsum[i] = a[i] - 1;
    }
  }

  for (int i = 0; i < q; i++)
  {
    int64_t k;
    cin >> k;

    int l = 0;
    int r = n - 1;
    int c = (l + r) / 2;

    int64_t ans = 0;
    if (k <= dsum[l])
    {
      ans = k;
    }
    else if (dsum[r] < k)
    {
      ans = a[r] + k - dsum[r];
    }
    else
    {
      // 二分探索
      while (c != r && c != l)
      {
        if (dsum[c] < k)
        {
          l = c;
          c = (l + r) / 2.0;
        }
        else
        {
          r = c;
          c = floor((l + r) / 2.0);
        }
      }

      ans = a[c] + k - dsum[c];
    }
    cout << ans << endl;
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fi                 first
#define sc                second
#define pii              pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define fl fflush(stdout)
//#define mod 1000000007
#define mod2 998244353
#define pi 3.14159265358979323846
//bool comp(const pair<int,double> &a,const pair<int,double> &b){  return (a.second > b.second);  }
/*
bool prime[111111];
bool isPrime(int n)
{
    for(int i=2;i*i<=n;++i)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
bool prime[100005];
void sieve(int n)
{
    memset(prime,true,sizeof(prime));
    prime[1]=false;
    for (int p=2;p*p<=n;p++)
    {
        if (prime[p]==true)
        {
            for (int i=p*p;i<=n;i+=p)prime[i]=false;
        }
    }
}
void dfs(int p){
    used[p]=true;
    rep(i,n){
        if(used[i]==true) continue;
        if(X[i]==X[p] || Y[i]==Y[p])dfs(i);
    }
}
void bfs(int s) {
    d = vector<int>(n, INF);  d[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (auto to : g[v]) {  if (d[to] == INF) {  d[to] = d[v] + 1;  q.push(to); } }
    }
}
void update(int x, ll val){while(x <= n)tree[x] += val,x += (x & -x);}
ll read(int x){ ll sum=0; while (x>0)sum+=tree[x],x -= (x & -x); return sum;}
void update(int l, int r, int nod, int lvl)
{
      if(l==r)tree[nod] = x;
      else
        {
            int mid = (l+r)/2; //left tree
            if(i <= mid) update(l,mid,nod*2,lvl-1);//right tree
            else update(mid+1,r,nod*2+1,lvl-1);
            if(lvl%2 == 0) tree[nod] = (tree[2*nod]^tree[nod*2 + 1]);
            else tree[nod] = (tree[2*nod]|tree[nod*2 + 1]);
        }
}
bool sortbysec(const pair<int,int> &a , const pair<int,int> &b) {
    if(a.first==b.first) {return(a.second < b.second);}
    else return a.first<b.first;
    }
while(n>0)
    {
        if(n&1)ans=(ans*x)%mo;
         n/=2;
          x =(x*x)%mo;
    }

bool sortbysec(const pair<int,int> &a , const pair<int,int> &b) {
     return a.second<b.second;//
    }
#define max 60
ll dp[25][25];
ll nCr(ll n, ll r)
{
       if(n==r) return dp[n][r] = 1;
       if(r==0) return dp[n][r] = 1;
       if(r==1) return dp[n][r] = n;
       if(dp[n][r]) return dp[n][r];
       return dp[n][r] = (nCr(n-1,r) + nCr(n-1,r-1));
}
*/
void solve(){
    //points to remember:
    //1.overflow 2.max/min values 3.array bound 4.special case(n==1) 5.infinite loop
    int n; cin>>n;
    vector<int> v(n);
    rep(i,0,n)cin>>v[i];
    int mex=0;
    set<int> s,p;
    rep(i,0,200001)p.insert(i);
    rep(i,0,n){
        p.erase(v[i]);
        if(v[i]==mex)mex=*p.begin();
        cout<<mex<<"\n";
    }
}
int main(){
    //ios_base::sync_with_stdio(false);  cin.tie(0);cout.tie(0);
    //sieve(100000);
    //int t; cin>>t; while(t--)
    solve();
}

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
ll f(ll x){
	return (x+1)*x/2;
}
int main(){
	ll l=1,r=100000;
	ll n;
	cin >> n;
	while(l<r){
		ll mid=(l+r)/2;
		if(f(mid)<n)
			l=mid+1;
		else
			r=mid;
	}
	cout << l << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <stdio.h>
#include <unistd.h>
using namespace std;
typedef long long int ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define drep(i, n) for(int i = n - 1; i >= 0; i--)
#define itrep(itr, base) for (auto itr = base.begin(); itr != base.end(); itr++)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl
#define PI 3.14159265359

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

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

int main() {
  string a, b;
  cin >> a >> b;
  int ans = 0;
  rep (i, a.size()) {
    ans += a[i] - '0';
  }
  int tmp = 0;
  rep (i, b.size()) {
    tmp += b[i] - '0';
  }
  if (ans > tmp) P(ans);
  else P(tmp);
  return 0;
}

#include <iostream>
using namespace std;
int main() {
    int a,b;
    cin>>a>>b;
    int m,sum=0,n,summ=0;
    while(a!=0)
    {
        m=a%10;
        sum=sum+m;
        a=a/10;
    }
     while(b!=0)
    {
        n=b%10;
        summ=summ+n;
        b=b/10;
    }
    if(sum>summ)
    cout<<sum;
    else
    cout<<summ;
    return 0;
}


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

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout<< #x << '=' << x <<endl
#define MOD 1000000007

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

    ll n , k;
    cin>>n>>k;
    ll a[n];
    map<ll , ll> mp;
    for(int i = 0; i < n; i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    ll ans = 0 , cur = 0;
    for(ll i = 0; i <= n; i++){
        ans += ((k - min(k , mp[i]))*i);
        k -= (k - min(k , mp[i]));
    }
    for(ll i = 0; i <= n; i++){
        if(mp[i] == 0){
            cur = i;
            break;
        }
    }
    ans += (k * (cur));
    cout<<ans;
    return 0;
}

#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba(a) emplace_back((a))
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
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 (a>b) { a=b; return 1; } return 0; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
template<typename TypeInt>
std::string Itoa(const TypeInt v, int base)
{
    static const char table[] = "0123456789";
    string ret;
    static numeric_limits<TypeInt> t;
    TypeInt n = v;
    if (t.is_signed) {
        if (v < 0) n *= -1;
    }

    while (n >= base) {
        ret += table[n%base];
        n /= base;
    }
    ret += table[n];
    if (t.is_signed) {
        if (v < 0 && base == 10) ret += '-';
    }
    // 文字列の順番を逆にする
    std::reverse(ret.begin(), ret.end());

    return ret;
}
int main(){
	int N;
	scan(N);
	int ans=0;
	rep1(i,N){
		string s1=Itoa(i,10);
		string s2=Itoa(i,8);
		bool is=true;
		for(const auto& c:s1){
			if(c=='7'){
				is=false;
				break;
			}
		}
		for(const auto& c:s2){
			if(c=='7'){
				is=false;
				break;
			}
		}
		if(is) ans++;
	}
	prin(ans);
	return 0;
}


// A - kcal
#include<bits/stdc++.h>
#define repi(i, s, n) for(int i=(int)(s); i<(int)(n); ++i)
#define rep(i, n) repi(i, 0, n)
#define rrepi(i, n, s) for(int i=n-1; i>=s; --i)
using namespace std;
using ll = long long;

int main(){
    int a, b;
    cin >> a >> b;
    double v = (double)b/100;
    double ans = v*a;
    printf("%.15f\n", ans);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int ar[n][m];
    int min=1000;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>ar[i][j];
            if(ar[i][j]<min)
                min=ar[i][j];
        }
    }
    int ctr=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(ar[i][j]>min){
                ctr=ctr+(ar[i][j]-min);
            }
        }
    }
    cout<<ctr<<endl;
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static int IN(void)
{
  int x=0,f=1,c;while(c=getchar(),c<48||c>57){if(c==45)f=-f;}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f*x;
}
static void OUT(long x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x-x/10*10+48);}
typedef struct List{int adj,v,c,d;}edge;
int hsz=0;long minHeap[200001][2],output[2];
static void PUSH(int id,long key)
{
  int i=++hsz,j=i>>1;long sw[2];
  minHeap[i][0]=id,minHeap[i][1]=key;
  while(j)
  {
    if(minHeap[i][1]<minHeap[j][1])
    {
      sw[0]=minHeap[j][0],sw[1]=minHeap[j][1];
      minHeap[j][0]=minHeap[i][0],minHeap[j][1]=minHeap[i][1];
      minHeap[i][0]=sw[0],minHeap[i][1]=sw[1];
      i=j;j>>=1;
    }else break;
  }
}
static void POP(void)
{
  int i=1,j=i+1;long sw[2];
  output[0]=minHeap[i][0],output[1]=minHeap[i][1];
  minHeap[i][0]=minHeap[hsz][0],minHeap[i][1]=minHeap[hsz--][1];
  while(j<=hsz)
  {
    if(j<hsz&&minHeap[j^1][1]<minHeap[j][1]){j^=1;}
    if(minHeap[j][1]<minHeap[i][1])
    {
      sw[0]=minHeap[j][0],sw[1]=minHeap[j][1];
      minHeap[j][0]=minHeap[i][0],minHeap[j][1]=minHeap[i][1];
      minHeap[i][0]=sw[0],minHeap[i][1]=sw[1];
      i=j;j=i<<1;
    }else break;
  }
  return;
}
int main(void)
{
  int N=IN(),M=IN(),vis[100000],esz=0,head[100000];
  long dist[100000];edge e[200001];
  rep(i,N){dist[i]=9e18;}
  while(M--)
  {
    int A=IN(),B=IN(),C=IN(),D=IN();
    e[++esz].adj=head[A-1];
    head[A-1]=esz;
    e[esz].v=B;
    e[esz].c=C;
    e[esz].d=D;
    e[++esz].adj=head[B-1];
    head[B-1]=esz;
    e[esz].v=A;
    e[esz].c=C;
    e[esz].d=D;
  }
  dist[0]=0l;
  PUSH(1,0l);
  while(hsz)
  {
    POP();
    long v=output[1];
    if(vis[output[0]-1]){continue;}
    for(int i=head[output[0]-1];i;i=e[i].adj)
    {
      int to=e[i].v;long len=0l;
      if(e[i].d)
      {
        int x=sqrt(e[i].d)-1;
        if(e[i].d/(x+1)>1+e[i].d/(x+2)){x++;}
        if(e[i].d/(x+1)>1+e[i].d/(x+2)){x++;}
        if(v>1l*x){len=1l*e[i].c+1l*e[i].d/(v+1l);}
        else{len=1l*e[i].c+1l*x-v+1l*e[i].d/(1l*x+1l);}
      }else{len=1l*e[i].c;}
      if(dist[to-1]>v+len)
      {
        dist[to-1]=v+len;
        PUSH(to,v+len);
      }
    }
    vis[output[0]-1]=1;
  }
  if(dist[N-1]==9e18){puts("-1");}else OUT(dist[N-1]);
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(ar) ar.begin(),ar.end()
#define endl '\n'

void swap(string &y,string &z,int a,int b) {
    char k;
    k=y[a];
    y[a]=z[b];
    z[b]=k;
}
void swapin(string &y,int a,int b) {
    char k;
    k=y[a];
    y[a]=y[b];
    y[b]=k;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string y=s.substr(0,n);
    string z=s.substr(n,n);
    int q;
    cin>>q;
    int kk=0;
    while(q--) {
        int t,a,b;
        cin>>t>>a>>b;
        a--;
        b--;

        if(t==1) {
            if(kk%2) {
                if(a<n) {
                    if(b>=n) {
                        b=b-n;
                        swap(z,y,a,b);
                    }
                    else {
                        swapin(z,a,b);
                    }
                }
                else {
                    a=a-n;
                    b=b-n;
                    swapin(y,a,b);
                }
            }
            else {
                if(a<n) {
                    if(b>=n) {
                        b=b-n;
                        swap(y,z,a,b);
                    }
                    else {
                        swapin(y,a,b);
                    }
                }
                else {
                    a=a-n;
                    b=b-n;
                    swapin(z,a,b);
                }
            }

        }
        else{
            kk++;
        }
    }
    if(kk%2) {
        cout<<z<<y<<endl;
    }
    else{
        cout<<y<<z<<endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

#define nline "\n"
#define ff first
#define ss second
#define ll long long
#define pb push_back

void solve() {
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
        sum += i;
        if (sum >= n) {
            cout << i;
            break;
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // int t;
    // cin >> t;
    // while (t--)
    solve();
}

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <cstdint>
#include <vector>
using namespace std;

int main(){
    long long n;
    cin>>n;
    long long ans=0.5*(-1+sqrt(1+8*n));
    if(ans*(ans+1)<2*n){
        ans+=1;
    }
    cout<<ans;

}


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

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

const ll INF = 10e16;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> s(H);
    rep(i, H) cin >> s[i];

    vector<vector<P>> G(26);
    vector<bool> reached(26, false);
    rep(i, H) rep(j, W) {
        if (s[i][j] >= 'a' && s[i][j] <= 'z') {
            G[s[i][j] - 'a'].push_back({i, j});
        }
    }

    rep(i, H) rep(j, W) {
        if (s[i][j] != 'S') continue;

        vector<vector<ll>> dist(H, vector<ll>(W, INF));
        queue<P> que;

        que.push({i, j});
        dist[i][j] = 0;

        while (!que.empty()) {
            int h = que.front().first;
            int w = que.front().second;
            que.pop();

            if (s[h][w] >= 'a' && s[h][w] <= 'z' &&
                reached[s[h][w] - 'a'] == false) {
                for (auto p : G[s[h][w] - 'a']) {
                    if (p.first == h && p.second == w) continue;
                    if (dist[p.first][p.second] < INF) continue;
                    dist[p.first][p.second] = dist[h][w] + 1;
                    que.push({p.first, p.second});
                    reached[s[h][w] - 'a'] = true;
                }
            }

            for (int direction = 0; direction < 4; direction++) {
                int nh = h + dx[direction];
                int nw = w + dy[direction];
                if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
                if (dist[nh][nw] < INF) continue;
                if (s[nh][nw] == '#') continue;
                dist[nh][nw] = dist[h][w] + 1;
                que.push({nh, nw});

                if (s[nh][nw] == 'G') {
                    cout << dist[h][w] + 1 << endl;
                    return 0;
                }
            }
        }
        cout << -1 << endl;
    }
}

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

typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
typedef array<ll,4> allll;

#define pb push_back
#define ff first
#define ss second
#define MAX 300005
#define MOD 1000000007
#define INF 1e9+100
vector<int>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };

string s[2000];
int dist[2000][2000],h,w;
vector<aii> vec[26];
int bfs(int x, int y, int dx, int dy)
{
    queue<aii> q;
    q.push({x,y});
    memset(dist,-1,sizeof dist);
    dist[x][y]=0;
    int already_pushed[26]= {};
    while(!q.empty())
    {
        auto [cx,cy]=q.front();
        q.pop();

        for(int i=0; i<4; i++)
        {
            int nx=cx+xMove[i],ny=cy+yMove[i];
            if(nx>=0 and nx<h and ny>=0 and ny<w and s[nx][ny]!='#' and dist[nx][ny]==-1)
            {
                dist[nx][ny]=dist[cx][cy]+1;
                q.push({nx,ny});
            }
        }

        if(s[cx][cy]>=97 and s[cx][cy]<=122 and !already_pushed[s[cx][cy]-97])
        {
            already_pushed[s[cx][cy]-97]=1;
            for(auto [nx,ny]: vec[s[cx][cy]-97])
            {
                if(dist[nx][ny]==-1)
                {
                    dist[nx][ny]=dist[cx][cy]+1;
                    q.push({nx,ny});
                }
            }
        }
    }

    return dist[dx][dy];
}

int main()
{
    //freopen("input.in","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0),cout.tie(0);
    ios_base::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);
    int TC=1;
    int n,m,q,k;

    //cin>>TC;
    for(int t1=1; t1<=TC; t1++)
    {
        cin>>h>>w;
        for(int i=0; i<h; i++)
            cin>>s[i];
        int sx,sy,dx,dy;
        for(int i=0; i<h; i++)
        {
            for(int j=0; j<w; j++)
            {
                if(s[i][j]=='S')
                    sx=i,sy=j;
                else if(s[i][j]=='G')
                    dx=i,dy=j;
                else if(s[i][j]!='#' and s[i][j]!='.')
                    vec[s[i][j]-97].pb({i,j});
            }
        }

        cout<<bfs(sx,sy,dx,dy)<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
typedef unsigned long long ull;
typedef long long ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define nep(i,r,l) for(int i=r;i>=l;i--)
void sc(int &x){scanf("%d",&x);}
void sc(int &x,int &y){scanf("%d%d",&x,&y);}
void sc(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void sc(ll &x){scanf("%lld",&x);}
void sc(ll &x,ll &y){scanf("%lld%lld",&x,&y);}
void sc(ll &x,ll &y,ll &z){scanf("%lld%lld%lld",&x,&y,&z);}
void sc(char *x){scanf("%s",x);}
void sc(char *x,char *y){scanf("%s%s",x,y);}
void sc(char *x,char *y,char *z){scanf("%s%s%s",x,y,z);}
void out(int x){printf("%d\n",x);}
void out(ll x){printf("%lld\n",x);}
void out(int x,int y){printf("%d %d\n",x,y);}
void out(ll x,ll y){printf("%lld %lld\n",x,y);}
void out(int x,int y,int z){printf("%d %d %d\n",x,y,z);}
void out(ll x,ll y,ll z){printf("%lld %lld %lld\n",x,y,z);}
using namespace std;
const int N=5e5+5,mod=3;
int n,rt,dep[N],son[N];
vector<int>e[N];
void dfs(int u,int p)
{
    dep[u]=dep[p]+1;
    if(dep[u]>dep[rt]) rt=u;
    for(int v:e[u])
        if(v!=p)
        dfs(v,u);
}
void dfs2(int u,int p)
{
    dep[u]=1;
    for(int v:e[u])
        if(v!=p)
        {
            dfs2(v,u);
            if(dep[v]+1>dep[u])
                dep[u]=dep[v]+1,son[u]=v;
        }
}
int id,ans[N];
void dfs3(int u,int p)
{
    ans[u]=++id;
    for(int v:e[u])
        if(v!=son[u]&&v!=p)
        dfs3(v,u);
    if(son[u]) dfs3(son[u],u);
    ++id;
}
int main()
{
    //freopen("1.in","r",stdin);freopen("1.out","w",stdout);
    sc(n);
    rep(i,1,n-1)
    {
        int u,v;sc(u,v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs(1,0);
    dfs2(rt,0);
    dfs3(rt,0);
    rep(i,1,n)
    printf(i==n?"%d\n":"%d ",ans[i]);
}

#include<bits/stdc++.h>
// #pragma optimize("-O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,avx,mmx,tune=native")
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define INF 1e9
//#define BIG_INF 1e18
#define vi vector<ll>
#define sz(a) ll((a).size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1)
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
// typedef tree<
// ll,
// null_type,
// less<ll>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;
typedef long long ll;
const ll BIG_INF = 4e18;
const ll mod = 1e9 + 7;
 ll fast_exp(ll a, ll b)
{
    if(b <= 0)
       return 1;
    else
    {
       ll res = 1;
       res = fast_exp(a,b/2);
       res = (res*res)%mod;
       if(b%2 == 1)
          res = (res*a)%mod;
       return res;

    }
}

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

long long true_rand(long long n) {
    // Returns a random number between 0 and n - 1 inclusive using mt19937.
    uniform_int_distribution<long long> uid(0, n - 1);
    return uid(rng);
}
typedef double ld;
const long long N = 2e5+1000;
const long double pi = acos(-1.0);
using cd = complex<double>;
const double PI = acos(-1);
ld eps = 1e-12;
int vis[N] = {};
vector <int> edge[N];
int farthest = 1, dist = 0;
int dia_2;
void dfs(int v, int cur_dist)
{
    vis[v] = 1;
    if(cur_dist > dist)
    {
        farthest = v;
        dist = cur_dist;
    }
    for(auto e: edge[v])
    {
        if(vis[e] == 0)
        {
            dfs(e,cur_dist+1);
        }
    }
}
int col[N] = {};
void color(int v)
{
    vis[v] = 1;
    if(v == dia_2)
        col[v] =1;
    for(auto e: edge[v])
    {
        if(vis[e] == 0)
        {
            color(e);
            col[v] = max(col[v],col[e]);
        }
    }
}

int global_num = 1;
int final_num[N] = {};
void final_dfs(int v)
{
    vis[v] = 1;
    final_num[v] = global_num;
    for(auto e: edge[v])
    {
        if(vis[e] == 0 && col[e] == 0)
        {
            global_num++;
            final_dfs(e);
            global_num++;
        }
    }
    for(auto e: edge[v])
    {
        if(vis[e] == 0 && col[e] == 1)
        {
            global_num++;
            final_dfs(e);
            global_num++;
        }
    }

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen("c.txt","r",stdin);
    // freopen("input.txt", "w", stdout);
    int n;
    cin >> n;
    int u, v;
    for(int i = 0; i < n-1; i++)
    {
        cin >> u >> v;
        edge[u].pb(v);
        edge[v].pb(u);
    }
    dfs(1,0);
    int dia_1 = farthest;
    int dia_1_dist = dist;
    farthest = dia_1;
    dist = 0;
    for(int i = 1; i<=n; i++)
        vis[i] = 0;
    dfs(dia_1,0);
    dia_2 = farthest;

    for(int i = 1; i<=n; i++)
        vis[i] = 0;
    color(dia_1);
    for(int i = 1; i<=n; i++)
        vis[i] = 0;

    final_dfs(dia_1);
    for(int i = 1; i <= n; i++)
        cout << final_num[i] << " ";
    cout << '\n';

    // cout << "Case #" << cs++ << ": " << ans << '\n';

    return 0;

}


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

int main() {
  double X,Y,Z,A,G;
  cin >> X >> Y >> Z;
  G=Y/X;
  double i=0;
  while(i/Z<G){
      A=i;
      i++;
    }
  cout << A <<endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define BIG 2000005

ll M = 998244353;

int main() {
    ll A, B, C;
    cin >> A >> B >> C;
    ll ans = A * (A+1) / 2 % M;
    ans = (ans * (B * (B+1) / 2 % M)) % M;
    ans = (ans * (C * (C+1) / 2 % M)) % M;
    cout << ans << endl;
    return 0;
}


//#define _GLIBCXX_DEBUG
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1000000000;

//参照: http://satanic0258.hatenablog.com/entry/2016/04/29/004730
ll Pow(ll N, ll P){
    ll re = 1;
    for(ll i=0; i<P; ++i){
        re *= N;
    }
    return re;
}
int ans = INF;

void dfs(vector<int>& A, vector<ll> digits){
    int ret =0;
    if (A.size() == digits.size()){
        for (int i=0; i<digits.size(); i++){
            if (A[i] == 1){
                digits[i] = 0;
                ret++;
            }
        }
        int digsum =0;
        for (int i=0; i<digits.size(); i++){
            digsum += digits[i];
            digsum %= 3;
        }

        if (digsum%3 == 0 and A != vector<int>(digits.size(),1))chmin(ans,ret);
        return;
    }

    for (int v=0; v<2; v++){
        A.push_back(v);
        dfs(A, digits);
        A.pop_back();
    }
}

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

    ll digit_len = 1;
    ll nd = N;
    while (nd > 10){
        nd /= 10;
        digit_len++;
    }

    vector<ll>digits(digit_len);

    for (int i=0; i<digit_len; i++){
       if (i==0)digits[i] = (N % Pow(10,i+1));
       else digits[i] = ((N % Pow(10,i+1)) - digits[i-1]) / Pow(10,i);
    }

    ll digit_sum = 0;

    for (int i=0; i<digit_len; i++){
        digits[i] %= 3;
        digit_sum += digits[i];
        digit_sum %= 3;
    }
    if (digit_sum == 0){
        cout << 0 << endl;
        return 0;
    }
    vector<int>Hoge;
    dfs(Hoge, digits);

    if (ans != INF) cout << ans << endl;
    else cout << -1 << endl;
}

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

// Unionfind
struct uf{
  vi d;
  uf() {};
  uf(int mx) : d(mx,-1) {};
  int root(int x){
    if(d[x]<0) return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y){
    x = root(x); y = root(y);
    if(x == y) return false;
    if(d[x] > d[y]) swap(x,y);
    d[x] += d[y]; d[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y);}
  int size(int x){ return -d[root(x)];}
  int operator[] (int x) {return root(x);}
  int operator() (int x) {return size(x);}
};

int main() {
  int n, q; cin >> n >> q;
  vi c(n);
  rep(i,n) cin >> c[i], --c[i];

  vector<set<int> > s(n); // 1次元目が根の２次元目にその根がどのような種類のクラスを保持しているか
  vector<map<int,int> > m(n); // 1次元目が根の２次元目で各クラスに何人いるかを保持
  rep(i,n){
    s[i].insert(c[i]);
    m[i][c[i]] = 1;
  }

  uf dis(n);

  vi ans;
  rep(_,q){
    int qi; cin >> qi;
    if(qi == 1){
      int a,b; cin >> a >> b; --a,--b;
      if(dis.same(a,b)) continue;
      if(dis.size(a) < dis.size(b)) swap(a,b);
      int ra = dis.root(a), rb = dis.root(b);
      for(int clsb : s[rb]){
        if(s[ra].count(clsb)) m[ra][clsb] += m[rb][clsb];
        else s[ra].insert(clsb), m[ra][clsb] = m[rb][clsb];
      }
      // printf("ra: %d m[ra][1]: %d\n", ra, m[ra][1]);
      dis.unite(a,b);

    }else{
      int x,y; cin >> x >> y; --x,--y;
      ans.push_back(m[dis.root(x)][y]);
    }
  }
  for(int res: ans) cout << res << endl;
  return 0;
}


/*
   Author   : MatsuTaku
   Date     : 01/30/21
*/

#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#ifndef ONLINE_JUDGE
#define Assert(cond)      assert(cond)
#else
#define Assert(cond)
#endif
#define rep(i, n)         for (size_t i = 0, i##_len = (n); i < i##_len; i++)
using lint = long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
void chmax(T& dst, T x) { dst = max(dst, x); }
template<typename T>
void chmin(T& dst, T x) { dst = min(dst, x); }

class Solver {
 public:
  Solver();
  void solve();
};

Solver::Solver() {}

void Solver::solve() {
  int n,m; cin>>n>>m;
  vector<pair<int,int>> AB(m);
  rep(i, m) {
    int a,b; cin>>a>>b; a--; b--;
    AB[i] = {a,b};
  }
  int k; cin>>k;
  vector<pair<int,int>> CD(k);
  rep(i, k) {
    int c,d; cin>>c>>d; c--; d--;
    CD[i] = {c,d};
  }
  int ans = 0;
  rep(mask, 1<<k) {
    vector<int> C(n);
    rep(i, k) {
      if (mask&(1<<i)) {
        C[CD[i].first]++;
      } else {
        C[CD[i].second]++;
      }
    }
    int sat = 0;
    for (auto [a,b] : AB) {
      if (C[a] > 0 and C[b] > 0)
        sat++;
    }
    chmax(ans, sat);
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(10);
  Solver solver;
  int t = 1;
//  cin>>t;
  while (t--) {
    solver.solve();
  }
  return 0;
}

#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds;
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
//const int mod=998244353;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
VL dx={1,0,-1,0};
VL dy={0,1,0,-1};
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
vector<VL> adj;
VL visit2,lefti,righti;
int comp;
ll a,b;
void dfs(int v)
{
    a+=lefti[v];
    b+=righti[v];
    visit2[v]=1;
    for(auto to:adj[v])
    {
        if(visit2[to]==0)
        {
            dfs(to);
        }
    }
    return;
}
void solve()
{
    ll n,m;
    cin>>n>>m;

    lefti.assign(n,0);
    righti.assign(n,0);
    fr(i,0,n)
    {
        cin>>lefti[i];
    }
    fr(j,0,n)
    {
        cin>>righti[j];
    }
    comp=0;
    bool ans=true;
    adj.assign(n,VL());
    visit2.assign(n,0);
    fr(i,0,m)
    {
        ll x,y;
        cin>>x>>y;
        x--,y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    fr(i,0,n)
    {
        if(visit2[i]==0)
        {
            a=0;b=0;
            dfs(i);
            if(a!=b)
            {
                ans=false;
            }
        }
    }
   // ll ans=true;
    if(a!=b)
    {
        ans=false;
    }
    fr(i,0,n)
    {
        if(visit2[i]==0)
        {
            ans=false;
        }
    }
    if(ans)
        cout<<"Yes\n";
    else
        cout<<"No\n";
    return;
}
int main()
{
    IOS;
    ll t=1,pp;
    //cin>>t;
    pp=t;
    while(t--)
    {
        //cout<<"Case #"<<pp-t<<": ";
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * BE CAREFUL REGARDING THE DEFAULT VALUES IN segement trees etc
        * BE very careful in int vs long long vs unsigned long long
*/
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithmQ


/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e9+7;
const ll MAXN=100200;
ll i,j,mask;
ll mod(ll x)
{
    return (x+M)%M;
}
ll add(ll a,ll b)
{
    return mod(mod(a)+mod(b));
}
ll mul(ll a,ll b)
{
    return mod(mod(a)*mod(b));
}
ll power(ll p,ll x)
{
    ll res=1;
    while(x>0) {
        if(x%2==1) {
            res=mul(res,p);
        }
        x/=2;
        p=mul(p,p);
    }
    return res;
}
void solve()
{
    ll n;
    cin>>n;
    ll sumMinus=0, sumPlus=0;
    vector<ll> v(n);
    F(i,0,n) {
        cin>>v[i];
    }
    if(n==1) {
        cout<<v[0];
    } else {
        sumMinus=add(v[0], -v[1]);
        sumPlus=add(v[0], v[1]);
        ll cntPlus=1;
        ll cntMinus=1;
        F(i,2,n) {
            ll mn=add(sumPlus, mul(-1,mul(v[i], cntPlus)) );
            ll mx=add(sumPlus, mul(cntPlus, v[i]) );
            mx=add(mx, add(sumMinus, mul(cntMinus, v[i]) ));
            sumMinus=mn;
            sumPlus=mx;
            ll tmp=cntPlus;
            cntPlus=add(cntPlus, cntMinus);
            cntMinus=tmp;
            //cout<<sumMinus<<" "<<sumPlus;nl
        }
//        nl
//        cout<<sumMinus<<" "<<sumPlus;nl
        ll ans=add(sumMinus, sumPlus);
        cout<<ans;
    }
}
int main()
{
    IOS
    /*#ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE*/
    ll t,test=1;
    //cin>>test;
    F(t,1,test+1) {
        //cout<<"Case #"<<t<<": ";
        solve();
        nl
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#define N 110
#define mo 998244353

using namespace std;

int f[N][5010];
int n, w[N];
int sum;
int jc[N];
int ans;

int main(){
//	freopen("funny.in", "r", stdin);
	scanf("%d", &n);
	for (int i=1; i<=n; i++) scanf("%d", &w[i]), sum+=w[i];
	jc[0]=1;
	for (int i=1; i<=n; i++) jc[i]=1ll*jc[i-1]*i%mo;
	if (sum%2==1){
		printf("%d\n", 0);
		return 0;
	}
	int sx=sum/2;
	f[0][0]=1;
	for (int i=1; i<=n; i++){
		for (int j=n; j; j--){
			for (int k=w[i]; k<=sx; k++){
				(f[j][k]+=f[j-1][k-w[i]])%=mo;
			}
		}
	}
	for (int i=1; i<=n; i++){
		(ans+=1ll*jc[i]*jc[n-i]%mo*f[i][sx]%mo)%=mo;
	}
	printf("%d\n", ans);

	return 0;
}

#include<map>
#include<set>
#include<list>
#include<ctime>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
#define _rep(i,s,t) for(register int i=s;i>=t;--i)
#define Rep(i,s,t) for(register int i=s;i<t;++i)
#define go(x) for(register int e=las[x];e;e=nxt[e])
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define is insert
#define es erase
#define pii pair<int,int>
#define ms(f,x) memset(f,x,sizeof f)
#define mc(f,x) memcpy(f,x,sizeof f)
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define gi(x) read(x)
#define gii(x,y) read(x),read(y)
#define giii(x,y,z) read(x),read(y),read(z)
namespace IO{
    #define gc getchar()
    #define pc(x) putchar(x)
    template<typename T>inline void read(T &x){
        x=0;int f=1;char ch=gc;while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=gc;}
        while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=gc;x*=f;return;
    }
    template<typename T>inline void write(T x=0){
        T wr[51];wr[0]=0;if(x<0)pc('-'),x=-x;if(!x)pc(48);
        while(x)wr[++wr[0]]=x%10,x/=10;while(wr[0])pc(48+wr[wr[0]--]);
		putchar('\n');return;
    }
}
using IO::read;
using IO::write;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const int N=111,mod=998244353;
int f[N][N*N];
int n,x,sum,fac[N];
void inc(int &x,int y){
	x+=y;
	if(x>=mod)
		x-=mod;
}
int main(){
	gi(n);
	f[0][0]=1;
	fac[0]=1;
	rep(i,1,n)
		fac[i]=1ll*fac[i-1]*i%mod;
	rep(i,1,n){
		gi(x);
		sum+=x;
		_rep(k,i,1)
			_rep(j,sum,x)
				inc(f[k][j],f[k-1][j-x]);
	}
	if(sum&1){
		puts("0");
	}
	else{
		int ans=0;
		rep(i,0,n)
			ans=(ans+1ll*f[i][sum/2]*fac[i]%mod*fac[n-i])%mod;
		cout<<ans<<endl;
	}
	return 0;
}


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

int n,val[100005][10],init[10];
bool life[35][6];

bool ok(int x)
{
	for(int i=1;i<init[5];i++)
		for(int j=1;j<=3;j++)
			life[i][j]=0;
	int now;
	for(int i=1;i<=n;i++)
	{
		now=0;
		for(int j=0;j<5;j++)
		{
			if(val[i][j]>=x)
				now+=init[j];
		}
		life[now][1]=1;
		for(int j=0;j<init[5];j++)
			for(int k=1;k<=2;k++)
				life[j|now][k+1]|=life[j][k];
	}
	return life[init[5]-1][3];
}

int main()
{
	init[0]=1;
	for(int i=1;i<=5;i++)
		init[i]=init[i-1]*2;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=0;j<5;j++)
			scanf("%d",&val[i][j]);
	int l=1,r=1e9,mid;
	life[0][1]=life[0][2]=life[0][3]=1;
	while(l!=r)
	{
		mid=(l+r+1)/2;
		if(ok(mid))
			l=mid;
		else
			r=mid-1;
	}
	printf("%d\n",l);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return false;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return false;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;

int n;
vector<vl> X(3030,vl(5,0));

bool check(int t){
    set<int> s;
    rep(i,n) {
        int bit=0;
        fore(j,X[i]){
            bit<<=1; bit|=(j>=t);
        }
        s.insert(bit);
    }
    fore(x,s) fore(y,s) fore(z,s){
        if((x|y|z)==(1<<5)-1) return true;
    }
    return false;
}

int main() {
    fio(); cin>>n;
    rep(i,n) rep(j,5) cin>>X[i][j];
    ll ok=0,ng=1LL<<31;
    while(ng-ok>1){
        ll mid=(ok+ng)/2;
        if(check(mid)) ok=mid;
        else ng=mid;
    }
    print(ok);
}


#include <bits/stdc++.h>
#define lc (o<<1)
#define rc ((o<<1)|1)
#define PB push_back
#define MK make_pair
using namespace std;
#define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl

const int maxn = 5000 + 5;
const int modu = 998244353; // 1e9 + 7
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

typedef long long LL;

void read(LL &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9')  {f|=(ch=='-');ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return;
}

void read(int &x) { LL y; read(y); x = (int)y; }
void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; }
void read(char *s) { scanf("%s", s); }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }

int n, m;
int a[maxn];
LL inv[maxn], c[maxn], f[20][maxn];
int b[20];

int main() {
    // freopen("my.txt", "w", stdout);
    read(n, m);
    inv[1] = 1;
    for (int i = 2; i <= n; ++i)
        inv[i] = (modu - (modu/i))*inv[modu%i]%modu;
    c[0] = 1;
    for (int i = 1; i <= n; ++i)
        c[i] = (n-i+1)*inv[i]%modu*c[i-1]%modu;
    int nb = 0;
    while (m) {
        b[nb++] = m%2;
        m /= 2;
    }
    memset(f, 0, sizeof(f));
    f[nb-1][0] = 1;
    int tot = b[nb-1];
    for (int i = nb-2; i >= 0; --i) {
        for (int j = 0; j <= tot*2 + b[i]; ++j) if (j%2 == 0) {
            for (int k = 0; k <= j && k <= n; k += 2)
                f[i][j] = (f[i][j] + f[i+1][(j-k)/2]*c[k]) % modu;
        }
        tot = tot*2 + b[i];
    }
    printf("%lld\n", f[0][tot]);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  ll c = 1;
  while (c * 1000 <= n) {
    c *= 1000;
    ans += n - c + 1;
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
using namespace std;
const int mxN = 2e5 + 5;
const int INF = 1e18 + 7;

int N;

int stringtointeger(string x)
{
    int ans = 0, j = 1;

    for (int i = x.size() - 1; i >= 0; i--)
    {
        ans += (x[i] - '0') * j;
        j *= 10;
    }

    return ans;
}

string integertostring(int x)
{
    string ans = "";
    while (x > 0)
    {
        ans += (x % 10) + '0';
        x /= 10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

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

#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
    freopen("Error.txt", "w", stderr);
#endif

    cin >> N;
    if (N <= 10)
        cout << 0;
    else
    {
        int ans = 0;
        if (N >= 100000000000)
        {
            ans = 99999;
            int start = 100000;
            string s = integertostring(start);
            s += s;
            while (stringtointeger(s) <= N)
            {
                start++;
                ans++;
                s = integertostring(start);
                s += s;
            }
            cout << ans;
        } else if (N >= 1000000000)
        {
            ans = 9999;
            int start = 10000;
            string s = integertostring(start);
            s += s;
            while (stringtointeger(s) <= N)
            {
                start++;
                ans++;
                s = integertostring(start);
                s += s;
            }
            cout << ans;
        } else if (N >= 10000000)
        {
            ans = 999;
            int start = 1000;
            string s = integertostring(start);
            s += s;
            while (stringtointeger(s) <= N)
            {
                start++;
                ans++;
                s = integertostring(start);
                s += s;
            }
            cout << ans;
        } else if (N >= 100000)
        {
            ans = 99;
            int start = 100;
            string s = integertostring(start);
            s += s;
            while (stringtointeger(s) <= N)
            {
                start++;
                ans++;
                s = integertostring(start);
                s += s;
            }
            cout << ans;
        } else if (N >= 1000)
        {
            ans = 9;
            int start = 10;
            string s = integertostring(start);
            s += s;
            while (stringtointeger(s) <= N)
            {
                start++;
                ans++;
                s = integertostring(start);
                s += s;
            }
            cout << ans;
        } else if (N >= 10)
        {
            ans = 0;
            int start = 1;
            string s = integertostring(start);
            s += s;
            while (stringtointeger(s) <= N)
            {
                start++;
                ans++;
                s = integertostring(start);
                s += s;
            }
            cout << ans;
        }
    }

}

//20-03-2021  18:10:04

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;

int main() {
    ll n; cin >> n;
    int ans = 0;
    for (int x = 1; ; x++) {
        if (stoll(to_string(x) + to_string(x)) > n) {
            cout << x - 1 << endl;
            return 0;
        }
    }
}


#include<bits/stdc++.h>
#define For(i,j,k)  for(int i=j;i<=k;++i)
#define Dow(i,j,k)  for(int i=k;i>=j;--i)
#define ll long long
#define pb push_back
#define fir first
#define sec second
#define pb push_back
#define pa pair<int,int>
#define mk make_pair

using namespace std;

inline ll read()
{
    ll t=0,dp=1;char c=getchar();
    while(!isdigit(c))  {if(c=='-') dp=-1;c=getchar();}
    while(isdigit(c))   t=t*10+c-48,c=getchar();
    return t*dp;
}
inline void write(ll x){if(x<0)  {putchar('-');x=-x;}    if(x>=10)    write(x/10);putchar(x%10+48);}
inline void writeln(ll x){write(x);puts("");}
inline void write_p(ll x){write(x);putchar(' ');}
int main()
{
	ll N=read();
	ll f[233];
	ll x=0,y=0;
	f[0]=1;f[1]=2;
	int tot=1;
	For(i,2,100)
	{
		f[i]=f[i-1]+f[i-2];
		if(f[i]>N)	break;
		tot=i;
	}
	int top=0;
	int q[233];
	Dow(i,0,tot)
	{
		if(f[i]<=N)	q[++top]=i,N-=f[i];
	}
	reverse(q+1,q+top+1);
	int now=3;
	int tep=1;
	int ans[233],top_ans=0;
	For(i,1,tot+1)
	{
		ans[++top_ans]=now;
		if(now==3)	x=x+y;
		if(now==4)	y=x+y;
		if(q[tep]==i-1)
		{
			if(now==4)	ans[++top_ans]=1;
			else	ans[++top_ans]=2;
			++tep;
		}
		now=7-now;
	}
	writeln(top_ans);
	x=y=0;
	Dow(i,1,top_ans)
	{
		writeln(ans[i]);
		if(ans[i]==1)	x++;
		if(ans[i]==2)	y++;
		if(ans[i]==3)	x=x+y;
		if(ans[i]==4)	y=x+y;
	}
	// cout<<tot<<' '<<f[tot]<<endl;
	// cout<<x<<"=="<<y<<endl;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
	-- Benq
*/

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

int main() {
    int n;
    cin >> n;
    vector<int> t(n);
    vector<ll> l(n);
    vector<ll> r(n);
    rep(i,n) cin >> t[i] >> l[i] >> r[i];

    vector<pair<double, double>> h(n);
    rep(i,n) {
        if(t[i]==1 || t[i]==2) {
            h[i].first = l[i];
        } else {
            h[i].first = l[i] + 0.1;
        }
        if(t[i]==1 || t[i]==3) {
            h[i].second = r[i];
        } else {
            h[i].second = r[i] - 0.1;
        }
    }
    //debug(h[0].first);
    //debug(h[0].second);
    int ans=0;

    rep(i,n-1) {
        for(int j=i+1; j<n; j++) {
            if(h[i].first <= h[j].first && h[j].first <= h[i].second) {
                ans++; //debug(i); debug(j);
            }
            else if(h[i].first <= h[j].second && h[j].second <= h[i].second) {
                ans++; //debug(i); debug(j);
            }
            else if(h[j].first <= h[i].first && h[i].first <= h[j].second) {
                ans++; //debug(i); debug(j);
            }
            else if(h[j].first <= h[i].second && h[i].second <= h[j].second) {
                ans++; //debug(i); debug(j);
            }
        }
    }

    cout << ans << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

struct edge{
	long long v, t, k;
};

const long long N = 1e5 + 10;
const long long INF = 1e18;
vector<edge> node[N];
long long n, m, x, y,  st[N];
long long d[N];

long long dijkstra() {
	for (long long i = 1; i <= n; i++) d[i] = INF;
	d[x] = 0;
	priority_queue<pair<long long, long long>> q;
	q.push({0, x});

	while (!q.empty()) {
		long long u = q.top().second; q.pop();
		if (st[u]) continue;
		st[u] = 1;

		for (auto e : node[u]) {
			long long v = e.v, t = e.t, k = e.k;

			if (d[u] % k == 0) {
				if (d[u] + t < d[v]) {
					d[v] = d[u] + t;
					q.push({-d[v], v});
				}
			} else {
				if (d[u] + k - (d[u] % k) + t < d[v]) {
					d[v] = d[u] + (k - d[u] % k) + t;

					q.push({-d[v], v});
				}
			}
		}
	}

	if (d[y] == 1e18) return -1;
	return d[y];
}

int main() {

	cin >> n >> m >> x >> y;
	while (m--) {
		long long u, v, t, k;
		cin >> u >> v >> t >> k;
		node[u].push_back({v, t, k});
		node[v].push_back({u, t, k});
	}
	cout << dijkstra() << '\n';

	return 0;
}

/**
 * author  : UshanGhosh
 * created : 2021-06-27 12:02:50
**/

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

#define    ll      long long
#define    pii     pair<int,int>
#define    vii     vector<int>
#define    vcc     vector<char>
#define    pll     pair<long long, long long>
#define    mem     memset
#define    sof     sizeof
#define    co1     __builtin_popcountll
#define    PB      push_back
#define    UB      upper_bound
#define    LB      lower_bound
#define    MP      make_pair
#define    TS      to_string
#define    F       first
#define    S       second
#define    pi      acos(-1)
#define    mod     (long long)1000000007
#define    inf     (long long)1e18
#define    endl    '\n'

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    while(t--){
        long long a,b,c,d,e,f,i,j,k,m,n,o,x,y;
        cin >> a >> b >> c >> d;
        ll p1 = a, p2 = 0;
        for(i=1; i<=100005; i++) {
            p1 += b;
            p2 += c;
            ll div = (p1 - 1) / p2 + 1;
            if(div <= d) {
                cout << i << "\n";
                return 0;
            }
        }
        cout << -1 << "\n";
    }
}


#include <bits/stdc++.h>
#include <ctime>
#include <cstdlib>

#define int long long
#define ii pair<int, int>
#define pb push_back
#define mp make_pair
#define MOD 1000000009
#define E13 10000000000000
#define DUMP(a) \
    do { std::cout << "[" << #a " = " << (a) << "]" <<  std::endl; } while(false)
#define d0(x) cout<<(x)<<" "
#define d1(x) cout<<(x)<<endl
#define edge pair<int, pair<int, int> >
#define REP0(i, n) for (int i=0; i<n; ++i)
#define REP1(i, n) for (int i=1; i<=n; ++i)
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl
#define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl
using namespace std;

template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ ";
    for(auto v : V) os << v << " ";
    return os << "]";
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ ";
    for(auto s:S) os<<s<<" ";
    return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")";
}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ ";
    for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") ";
    return os<<"}";
}

signed main(){
    int n; cin >> n;
    vector<int> value(n+1, 1);
    for (int i=1; i<=n; ++i){
        for (int j=2*i; j<=n; j+=i){
            value[j] = max(value[j], value[i]+1);
        }
    }
    for (int i=1; i<=n; ++i){
        cout << value[i] << " ";

    }
    cout << endl;
    return 0;
}

#include<bits/stdc++.h>
#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL)

using namespace std;
#define F first
#define S second

const int N = 100001;
vector<int> primes;
int isprime[N] = {0};
void sieve()
{
    isprime[0] = 1;
    isprime[1] = 1;
    primes.push_back(2);
    for(int i=4 ; i<N ; i+=2)
        isprime[i] = 1;

    for(int i=3 ; i*i<N ; i+=2)
    {
        if(isprime[i]==0)
        {
            primes.push_back(i);
            for(int j=i*i ; j<N ; j+=i)
                isprime[j] = 1;
        }
    }
}
vector<pair<int , int> > retpf(int x)
{
    vector<pair<int , int> > ans;
    int cnt = 0;
    for(int i=0 ; i<primes.size() && (primes[i]*primes[i]<=x) ; i++)
    {
        if(x%primes[i]==0)
        {
            cnt = 0;
            while(x%primes[i]==0)
            {
                cnt++;
                x/=primes[i];
            }
            ans.push_back(make_pair(primes[i] , cnt));
        }
        if(isprime[x]==0)
        {
            ans.push_back(make_pair(x , 1));
            break;
        }
    }
    return ans;
}

int main()
{
fastIO;
sieve();

int n;
cin>>n;
vector<int> dp(n+1 , 1);
for(int i=1 ; i<=n ; i++)
{
    if(isprime[i]==0)
        dp[i] = 2;
    else
    {
        int ctr = 0;
        vector<pair<int , int> > pf = retpf(i);
        for(int j=0 ; j<pf.size() ; j++)
        {
            if(pf[j].F!=1 && pf[j].F!=i)
            {
                ctr += pf[j].S;
            }
        }
        dp[i] = ctr+1;
    }
}
for(int i=1 ; i<=n ; i++)
    cout<<dp[i]<<" ";
return 0;
}


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

#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast  ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 1922337203685912
#define endl "\n"
ll fast_expo(ll x,ll p){
  if(p==0) return 1;
  else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;}
  else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll invMod(ll num){
  return fast_expo(num, MOD - 2)%MOD;
}

const ll N = 1e2 + 5;
ll dp[N][N][N];

int main(){
  fast
  #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif

  ll t_c; t_c = 1;
//   cin >> t_c;

  for(ll t_i = 0; t_i < t_c; ++t_i){
    ll n, x; cin >> n >> x;
    ll arr[n + 1]{};
    memset(dp, -1, sizeof(dp));
    for(ll i = 1; i <= n; ++i)
        cin >> arr[i];

    for(ll i = 1; i <= n; ++i){ // final number of elements
        dp[i][0][0] = 0;
        for(ll j = 1; j <= n; ++j){  // current index of element
            for(ll k = j; k >= 0; --k){   // current size of elements
                for(ll m = 0; m < i; ++m){     //  current mod value
                    if(dp[i][k][m] == -1)
                        continue;
                    ll val = dp[i][k][m] + arr[j];
                    dp[i][k + 1][val%i] = max(dp[i][k + 1][val%i], val);
                }
            }

        }
    }

    ll ans = LLONG_MAX;
    for(ll i = 1; i <= n; ++i){
        ll val = x%i;

        if(dp[i][i][val] == -1)
            continue;

        ll diff = (x - dp[i][i][val])/i;
        ans = min(ans, diff);
    }

    cout << ans;
  }
  return 0;
}

#include<queue>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<limits.h>
#include<utility>
#include<vector>
#include<tuple>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for(ll i=0;i<n;i++)
#define ALL(A) A.begin(),A.end()

struct UnionFind{
    vector<ll> par;
    UnionFind(ll N):par(N){
        rep(i,N){
            par[i]=i;
        }
    }
    ll root(ll x){
        if(par[x]==x)return x;
        return par[x]=root(par[x]);
    }
    void unite(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        if(rx==ry){
            return;
        }
        par[rx]=ry;
    }
    bool same(ll x, ll y){
        ll rx=root(x);
        ll ry=root(y);
        return rx==ry;
    }

};
long long GCD(long long a, long long b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
int main(){
    ll n,x;
    cin>>n>>x;
    vector<ll> a(n);
    rep(i,n)cin>>a[i];
    ll ans=LLONG_MAX;
    rep(i,n+1){
        if(i==0)continue;
        //                              i                        n            mod
        vector<vector<vector<ll> > > dp(i+1,vector<vector<ll> > (n+1,vector<ll>(i,-1)));
        dp[0][0][0]=0;
        rep(j,i){
            rep(k,n){
                //cout<<i<<j<<k<<endl;
                rep(l,i){

                    //if(i==3&&j==3&&k==3)cout<<"  "<<dp[j][k][l]<<endl;
                    dp[j][k+1][l]=max(dp[j][k][l],dp[j][k+1][l]);
                    if(dp[j][k][l]!=-1){
                        //cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<dp[j][k][l]<<endl;
                        dp[j+1][k+1][((l+a[k])%i)]=max(dp[j][k][l]+a[k],dp[j+1][k+1][((l+a[k])%i)]);
                        //cout<<dp[j+1][k+1][((l+a[k])%i)]<<endl;
                        //cout<<((l-a[m]+i)%i+i)%i<<" "<<dp[j][k][l]<<" "<<dp[j+1][m+1][((l-a[m]+i)%i+i)%i]<<endl;
                    }

                }
            }
        }
        rep(k,n){
                //cout<<i<<j<<k<<endl;
                rep(l,i){

                    //if(i==3&&j==3&&k==3)cout<<"  "<<dp[j][k][l]<<endl;
                    dp[i][k+1][l]=max(dp[i][k][l],dp[i][k+1][l]);

                }
            }
        //rep(j,n+1)cout<<j<<dp[i][j][x%i]<<endl;
        //rep(j,n+1){
        if(dp[i][n][x%i]==-1)continue;
            //cout<<i<<" "<<dp[i][j][0]<<endl;
        ans=min(ans,(x-dp[i][n][x%i])/i);
        //cout<<i<<"  "<<ans<<"  "<<dp[i][n][x%i] <<endl;
    }
    cout<<ans<<endl;
}


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

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h, w;
    cin >> h >> w;
    vector<string> f(h);
    rep(i,h) cin >> f[i];
    vector<vector<int>> sum(h, vector<int>(w));
    rep(i,h){
        int tmp, l;
        l = -1; tmp = 0;
        rep(j,w){
            if(f[i][j] == '.'){
                if(l == -1) l = j;
                tmp++;
            }
            else{
                if(l == -1) continue;
                for(int k = l; k < j; k++){
                    sum[i][k] += tmp;
                }
                tmp = 0;
                l = -1;
            }
        }
        if(l != -1){
            for(int k = l; k <= w-1; k++) sum[i][k] = tmp;
        }
    }

    /*rep(i,h){
        rep(j,w) cout << sum[i][j] << ' ';
        cout << endl;
    }*/

    rep(j,w){
        int tmp, l;
        l = -1, tmp = 0;
        rep(i,h){
            if(f[i][j] == '.'){
                if(l == -1) l = i;
                tmp++;
            }
            else{
                if(l == -1) continue;
                for(int k = l; k < i; k++){
                    sum[k][j] += tmp;
                }
                //if(j == 0) cout << tmp << ' ' << l  << ' ' << endl;
                tmp = 0;
                l = -1;
            }
        }
        if(l != -1){
            for(int k = l; k < h; k++){
                sum[k][j] += tmp;
            }
        }
    }
/*
    rep(i,h){
        rep(j,w) cout << sum[i][j] << ' ';
        cout << endl;
    }
*/
    vector<mint> p(4000001);
    p[0] = 1;
    rep(i,4000000){
        p[i+1] = p[i];
        p[i+1] *= 2;
    }

    int t = 0;
    rep(i,h) rep(j,w) if(f[i][j] == '.') t++;
    mint ans = 0;
    rep(i,h) rep(j,w) if(sum[i][j] > 0) sum[i][j]--;
/*
    rep(i,h){
        rep(j,w) cout << sum[i][j] << ' ';
        cout << endl;
    }
*/
    rep(i,h) rep(j,w){
        if(sum[i][j] > 0){
            mint tmp = 0;
            tmp += p[sum[i][j]];
            tmp -= 1;
            tmp *= p[t-sum[i][j]];
            ans += tmp;
        }
    }
    cout << ans << endl;
}

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

int main() {
    //入力
    int n,m;
    cin >> n >> m;
    vector<int> a(m), b(m);
    rep(i,m) cin >> a[i] >> b[i];
    int k;
    cin >> k;
    vector<int> c(k), d(k);
    rep(i,k) cin >> c[i] >> d[i];

    //出力
    int ans = 0;
    //2^kパターんを2進数で表現
    int k2 = 1<<k;
    rep(s,k2){ //条件全パターん網羅
        //入れる皿を用意
        vector<int> dish(n+1);
        //該当する皿にボールを追加
        rep(i,k){
            //右シフトして入れる場所にインクリメント
            if(s>>i&1) dish[d[i]]++;
            else dish[c[i]]++;
        }
        //条件を満たす皿の個数を数える
        //満たす個数
        int now = 0;
        rep(i,m){
            //条件の皿はa[i]とb[i]
            if (dish[a[i]] == 0) continue;
            if (dish[b[i]] == 0) continue;
            //上記以外なら増やす
            now++;
        }
        //現在の入れる皿と今までの最大値
        ans = max(ans, now);
    }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>

using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll modq=998244353;
const ll INF=1e18;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}

ll modpow(ll a,ll n) {
    ll res = 1;
    while (n>0) {
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}

int main(void){
    vector<ll> Fib(1050);
    Fib.at(0)=0,Fib.at(1)=1;
    rep(i,1010) Fib.at(i+2)=(Fib.at(i+1)+Fib.at(i))%mod;

    ll N;
    cin>>N;
    char caa,cab,cba,cbb;
    cin>>caa>>cab>>cba>>cbb;
    if(N<4){
        cout<<1<<endl;
        return 0;
    }
    if(caa=='A' && cab=='A'){
        cout<<1<<endl;
        return 0;
    }
    if(cbb=='B' && cab=='B'){
        cout<<1<<endl;
        return 0;
    }
    if(cab=='B' && cbb=='A'){
        if(cba=='A'){
            cout<<modpow(2,N-3)<<endl;
            return 0;
        }
        cout<<Fib.at(N-1)<<endl;
        return 0;
    }
    if(cba=='B'){
        cout<<modpow(2,N-3)<<endl;
        return 0;
    }
    cout<<Fib.at(N-1)<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int N;
  cin>>N;
  vector<int> A(N);
  vector<int> B(N);
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  for(int i=0;i<N;i++){
    cin>>B[i];
  }
  sort(A.begin(),A.end());
  sort(B.begin(),B.end());
  int x=A[N-1],y=B[0];
  //cout<<x<<","<<y<<endl;
  if(y-x+1<0){
    cout<<"0"<<endl;
  }
  else cout<<y-x+1<<endl;
}


// Problem C

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <cassert>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <stack>

#define MAXN 1000
#define pb push_back
#define mp make_pair

using namespace std;

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

int N;
vector<pair<pair< ll, bool>, string > > v;

int main() {
  cin >> N;
  ll P1 = 1000000007;
  ll P2 = 1000000009;
  string s1;
  for (int i=0; i<N; i++) {
    cin >> s1;
    ll x1 = 0;
    ll x2 = 0;
    if (s1[0] == '!') {
      for (int j=1; j<(int)s1.length(); j++) {
	x1 = ((x1 * 229) % P1) + s1[j] - 'a' + 1;
	x2 = ((x2 * 1187) % P2) + s1[j] - 'a' + 1;
      }
      v.pb(mp(mp(x1 * x2, false), s1));
    }
    else {
      for (int j=0; j<(int)s1.length(); j++) {
	x1 = ((x1 * 229) % P1) + s1[j] - 'a' + 1;
	x2 = ((x2 * 1187) % P2) + s1[j] - 'a' + 1;
      }
      v.pb(mp(mp(x1 * x2, true), s1));
    }
  }

  sort(v.begin(), v.end());
  for (int i=0; i<N-1; i++)
    if (v[i].first.first == v[i+1].first.first) {
      if (v[i].first.second && !v[i+1].first.second) {
	cout << v[i].second << endl;
	return 0;
      }
      else if (!v[i].first.second && v[i+1].first.second) {
	cout << v[i+1].second << endl;
	return 0;
      }
    }

  cout << "satisfiable" << endl;

  return 0;
}

#include<bits/stdc++.h>

int main(){
    using namespace std;
    unsigned long N;
    cin >> N;
    vector<unsigned long> A(N), B(N);
    for(auto&& i : A)cin >> i;
    for(auto&& i : B)cin >> i;
    priority_queue<unsigned long> pq;
    reverse(begin(A), end(A));
    unsigned long ans{0};
    for(unsigned long i{0}; i < N; ++i){
        pq.emplace(A.back());
        pq.emplace(B.back());
        A.pop_back();
        B.pop_back();
        ans += pq.top();
        pq.pop();
    }
    cout << ans << endl;
    return 0;
}


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

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

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int a, b, i, x, y;
    cin >> a >> b;
    for (i = b - a; i > 0; i--) {
        x = a / i;
        if (a % i)
            x++;
        y = b / i;
        if (x == y)
            continue;
        if (gcd(i * x, i * y) == i) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define DB double
#define LL long long

#define MST(a,b) memset((a),(b),sizeof(a))

#ifdef _DEBUG_
#define MRK() cout<<"Mark"<<endl;
#define WRT(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define MRK() ;
#define WRT(x) ;
#endif

#define MAXN 1010000
#define MAXM 2010000
#define MOD 998244353  //1000000007
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-5

#define _ 0
using namespace std;

string s;
int n;
void init()
{

}

void work()
{
	cin>>n;
	cin>>s;
	if (s.front()!=s.back())
	{
		cout<<1<<endl;
		return ;
	}
	for (int i=1;i<n-2;i++)
		if (s[i]!=s.back() && s[i+1]!=s.back())
		{
			cout<<2<<endl;
			return ;
		}
	cout<<-1<<endl;

}

int main()
{
	#ifdef _DEBUG_
	freopen("C:/Users/DELL/Documents/Dev-C++/sample.in","r",stdin);
	#endif
	init();
	int casenum=1;
	//scanf("%d",&casenum);
	for (int testcase=1;testcase<=casenum;testcase++)
	{
		#ifdef _DEBUG_
		printf("Case #%d:\n",testcase);
		#endif
		work();
	}
	return ~~(0^_^0);
}


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

string S1,S2,S3;
vector<char> chars;
char used[10];

bool check() {
    map<char, char> mp;
    rep(i,10) if (used[i] != 0) mp[used[i]] = char('0' + i);

    string s1,s2,s3;
    for(auto &c: S1) s1 += mp[c];
    for(auto &c: S2) s2 += mp[c];
    for(auto &c: S3) s3 += mp[c];

    if(to_string(stoll(s1)) != s1) return false;
    if(to_string(stoll(s2)) != s2) return false;
    if(to_string(stoll(s3)) != s3) return false;
    if(stoll(s1) + stoll(s2) != stoll(s3)) return false;
    if(stoll(s1) == 0) return false;
    if(stoll(s2) == 0) return false;
    if(stoll(s3) == 0) return false;

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    return true;
}

bool dfs(int di) {
    if (di == chars.size()) return check();
    rep(i,10) {
        if (used[i] == 0) {
            used[i] = chars[di];
            if (dfs(di + 1)) return true;
            used[i] = 0;
        }
    }
    return false;
}

int main() {
    cin >> S1 >> S2 >> S3;
    rep(i,S1.size()) chars.push_back(S1[i]);
    rep(i,S2.size()) chars.push_back(S2[i]);
    rep(i,S3.size()) chars.push_back(S3[i]);
    sort(chars.begin(),chars.end());
    chars.erase(unique(chars.begin(),chars.end()), chars.end());

    if (chars.size() > 10) {
        cout << "UNSOLVABLE" << endl;
        return 0;
    }

    bool ans = dfs(0);
    if(ans == false) cout << "UNSOLVABLE" << endl;
}

// give up
#include <bitset>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>

// alias, using declaration
using llint = long long; using ldouble = long double;
template <class T = llint> using vector = std::vector<T>;
template <class T = llint> using deque = std::deque<T>;
template <class T = llint, class U = llint, class... V> using map = std::map<T, U, V...>;
template <class T = llint, class... U> using set = std::set<T, U...>;
template <class T = llint, class U = llint> using umap = std::unordered_map<T, U>;
template <class T = llint> using uset = std::unordered_set<T>;
template <class T = llint, class U = T> using pair = std::pair<T, U>;
template <class T = llint> using varray = std::valarray<T>;
template <class T = llint, class... U> using pqueue = std::priority_queue<T, U...>;
using std::array; using std::bitset; using std::string; using std::tuple;

// constant
constexpr llint INF = 0x3FFFFFFFFFFFFFFF;
constexpr llint INVALID = 0x8000000000000000;

namespace io {
  // in
  template <class... T, size_t... S> auto in(std::index_sequence<S...>) {tuple<T...> t; (..., (std::cin >> std::get<S>(t))); return t;}
  template <class... T, size_t... S> auto in(std::index_sequence<S...>, size_t n) {tuple<vector<T>...> t{vector<T>(n)...}; for (size_t i = 0; i < n; i++) (..., (std::cin >> std::get<S>(t)[i])); return t;}
  template <size_t N, class T = llint, class... U> auto in() {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>(std::index_sequence_for<T, U...>());}
  template <size_t N, class T = llint, class... U> auto in(size_t n) {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(std::index_sequence_for<T, U...>(), n);}
  template <size_t N, class T = llint> auto in(size_t n, size_t m) {static_assert(N == 1); vector<vector<T>> v(n, vector<T>(m)); for (auto&& vi : v) for (auto&& vij : vi) std::cin >> vij; return std::make_tuple(v);}
  // out
  template <class T, class... U> void out(const T& a, const U&... b) {std::cout << a << (sizeof...(b) ? " " : "\n"); if constexpr (sizeof...(b)) out(b...);}
  template <class T, class U = const char*> void out(const vector<T>& a, U d = " ") {for (auto&& b : a) std::cout << b << (&b != &a.back() ? d : "\n");}
}

int main() {
  // input
  auto [N, M] = io::in<2>();
  auto [A, B] = io::in<2>(M);

  // solve
  vector<vector<>> graph(N);
  for (llint i = 0; i < M; i++) {
    graph[A[i] - 1].emplace_back(B[i] - 1);
    graph[B[i] - 1].emplace_back(A[i] - 1);
  }
  vector<bool> visited(N);
  vector<vector<>> s;
  auto dfs_s = [&](auto f, llint n) -> void {
    visited[n] = true;
    s.back().emplace_back(n);
    for (auto g : graph[n]) {
      if (!visited[g]) {
        f(f, g);
      }
    }
  };
  for (llint i = 0; i < N; i++) {
    if (!visited[i]) {
      s.emplace_back();
      dfs_s(dfs_s, i);
    }
  }
  vector<> color(N, INVALID);
  auto result = 1LL;
  auto dfs_p = [&](auto f, auto b, auto e, llint c) -> llint {
    for (auto g : graph[*b]) {
      if (color[g] == c) return 0;
    }
    auto bn = std::next(b);
    if (bn == e) return 1;
    color[*b] = c;
    llint r = f(f, bn, e, 0) + f(f, bn, e, 1) + f(f, bn, e, 2);
    color[*b] = INVALID;
    return r;
  };
  for (auto t : s) {
    result *= dfs_p(dfs_p, t.begin(), t.end(), 0) * 3;
  }

  // output
  io::out(result);
}


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

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

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

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

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

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

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

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

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

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

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

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

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

void debug_out() { cerr << endl; }

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

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
	int res = 1;
	while(b){
		if(b&1)res = mul(res,a);
		b >>= 1;
		a = mul(a,a);
	}
	return res;
}
const int maxn = 10;
int N,P,A[maxn];
//int res;
//void dfs(int pos){
	//if(pos >= N){
		//int act = 0;
		//bool can = true;
		//forn(i,0,N){
			//act += A[i];
			//if(act%P == 0){
				//can = false;
				//break;
			//}
		//}
		//res += (can == true);
		//return;
	//}
	//forn(i,1,P){
		//A[pos] = i;
		//dfs(pos+1);
	//}
//}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> P;
	//dfs(0);
	//cout << res << '\n';
	int res = mul(P-1,power(P-2,N-1));
	cout << res << '\n';

	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using prique = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }

struct fastio {
	fastio() {
		cin.tie(0); cout.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(20);
		cerr << fixed << setprecision(20);
	}
}fastio_;

int main() {
	ll n;
	cin >> n;
	string s, t;
	cin >> s >> t;
	set<int> st;
	rep(i, n) if (s[i] == '1') st.insert(i);
	st.insert(inf);
	ll ans = 0;
	rep(i, n) {
		if (t[i] == '1') {
			int x = *st.lower_bound(i);
			if (x == inf) fail();
			ans += x - i;
			st.erase(x);
		}
	}
	st.erase(inf);
	if (st.size() % 2 == 1) fail();
	while (!st.empty()) {
		int x = *st.begin();
		st.erase(x);
		int y = *st.begin();
		st.erase(y);
		ans += y - x;
	}
	cout << ans << '\n';
}


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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<P> a(n);
  rep(i, n) cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  ll now = 0;
  rep(i, n) {
    if (now + k >= a[i].first) {
      k = k - (a[i].first - now) + a[i].second;
      now = a[i].first;
    } else {
      cout << now + k << endl;
      return 0;
    }
  }
  cout << now + k << endl;
  return 0;
}

#include <bits/stdc++.h>
#define setIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265358979
#define f first
#define s second
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<long long, long long> pll;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
typedef pair<double, double> pdd;
typedef pair<int, string> pis;
int main() { // Ladder: Do 43
    setIO;
    int a, b, c;
    cin>>a>>b>>c;
    if (pow(a,2)+pow(b,2)<pow(c,2)) cout << "Yes\n";
    else cout << "No";
}


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

int main()
{
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, 0, n) cin >> a[i] >> b[i];
    ll sum = 0;
    rep(i, 0, n) sum += b[i]*(b[i] + 1)/2 - a[i]*(a[i] - 1)/2;
    cout << sum << endl;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	int n;
	cin>>n;
	ll a,b;ll sum=0LL;
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		sum+=(a+b)*(b-a+1)/2;
	}
	cout<<sum;
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define rint regester int
const int maxn = 500 + 10;
const int mod = 998244353;
using std::ios;
using std::cin;
using std::cout;
using std::max;
using std::min;
using std::sort;
using std::unique;
using std::lower_bound;
using std::swap;
using std::abs;
using std::acos;
using std::queue;
using std::map;
using std::string;
int read(){int x = 0,f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f;}
int h, w, p[maxn][maxn], ans = 1;
string s[maxn];
signed main(){
	h = read(), w = read();
	for(int i = 1; i <= h; i++)cin >> s[i];
	for(int i = 1; i <= h; i++){
		for(int j = 0; j < w; j++){
			if(s[i][j] == 'R')p[i][j + 1] = 1;else
			if(s[i][j] == 'B')p[i][j + 1] = 2;else p[i][j + 1] = 0;
		}
	}
	for(int i = 2; i <= h + w; i++){
		int c = 0, pp = 0;
		for(int j = max(1ll, i - w); j < i && j <= h; j++){
			++pp;
			int k = i - j;
			if(p[j][k] == 1){if(c == 2){cout << 0; return 0;} c = 1;}else
			if(p[j][k] == 2){if(c == 1){cout << 0; return 0;} c = 2;}
		}
		if(c == 0){
			ans *= 2;
			ans %= mod;
		}
	}
	cout << ans;
}

/*
ID: gurban1
LANG: C++
TASK:
*/
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 24;
const int maxn=2e5+5;
const int mod=998244353;
int n,m;
ll dp[maxn][N];
ll f[maxn],ans;

ll binmod(ll a,ll b) {if(b==0)return 1;ll x=binmod(a*a%mod,b>>1);return b&1?x*a%mod:x;}

ll inv(ll a){
	return binmod(a,mod-2);
}

ll choose(int a,int b){
	if(a < b) return 0;
	ll x = (f[a] * inv(f[b])) % mod;
	x = (x * inv(f[a-b]))%mod;
	return x;
}

int main(){
	// freopen("","r",stdin);
	// freopen("","w",stdout);

	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> m;

	f[0] = 1;
	for(int i = 1;i <= n;i++) f[i] = (f[i-1] * i) % mod;

	for(int i = m;i >= 1;i--){
		dp[i][1] = 1;
		for(int j = i+i;j <= m;j += i)
			for(int k = 1;k < N;k++) dp[i][k+1] += dp[j][k];
		for(int j = 1;j < N;j++){
			if(j > n) break;
			ll nw = (dp[i][j] * choose(n-1,j-1)) % mod;
			ans = (ans + nw) % mod;
		}
	}
	cout<<ans;
}


#include <bits/stdc++.h>
//#include <cmath>
typedef long long ll;
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007
//#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define bs(yup,x) binary_search(yup.begin(),yup.end(),x) //it will return bollean value
#define lb(yup,x) lower_bound(yup.begin(),yup.end(),x)  //it will return the adress of the number which is equal to or just greater than x ,and if it is equal to yup.end() than their in no such number exist
#define ub(yup,x) upper_bound(yup.begin(),yup.end(),x)
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
using namespace std;
const long long N=1000001,INF=2000000000000000000;
const long double EPS= 0.000000000001;

// calculate a^b%p
ll power(ll a, ll b, ll p)
    {
        if(a==0)
        return 0;
        ll res=1;
        //a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }

//sieve to calculate an array of prime and non prime integers till N
vi prime;
bool isprime[N];
void pre()
{
    for(int i=2;i<N;i++)
    {
        if(isprime[i])
        {
            for(int j=i*i;j<N;j+=i)
            isprime[j]=false;
            prime.pb(i);
        }
    }
    return;
}

double fn_ternary(double b,double c,double x){
	double ans = (x*x + b*x + c)/(sin(x));
	return ans;
}

double ternary_search(double l,double r,double b,double c){
	//for finding minimum
	//to find max interchange r and l
	double eps = 1e-10;
	while(r-l>eps){
		double m1 = l+(r-l)/3;
		double m2 = r-(r-l)/3;
		double f1 = fn_ternary(b,c,m1);
		double f2 = fn_ternary(b,c,m2);
		if(f1<f2){
			r = m2;
		}else{
			l = m1;
		}
	}
	return fn_ternary(b,c,l);
}

ll binary_search(ll a[],int n,ll k){
	int l = 0,r = n-1;
	while(l<=r){
		int m = l + (r-l)/2;
		if(a[m]==k){
			return m;
		}
		else if(a[m]>=k){
			r = m - 1;
		}
		else if(a[m]<k){
			l = m + 1;
		}
	}
	return -1;
}

// if more values to calculate use pascals triangle (C(n,k) = C(n-1,k-1)+C(n-1,k))
/*
const ll maxn = 1000000;
ll C[maxn+1][maxn+1];
C[0][0] = 1;
void nCr(ll p){
	for(ll i=1;i<=maxn;i++){
		C[i][0] = C[i][i] = 1;
		for(int j = 1;j<=i;j++){
			C[i][j] = (C[i-1][j-1]%p + C[i-1][j]%p)%p;
		}
	}
	return ;
}
*/
//calculating ncr modulo using fermats little theorem
//useful while calculating less discrete values of ncr

ll modinverse(ll n,ll p){
	return power(n,p-2,p);
}

ll nCr(ll n,ll r,ll p){
	ll f[n+1];
	f[0] = 1;
	for(ll i=1;i<=n;i++){
		f[i] = f[i-1]*i%p;
	}

	return (f[n]%p * modinverse(f[r],p)%p * modinverse(f[n-r],p)%p)%p;
}

bool comp(vector<int> v1,vector<int> v2){
	return v2[0]>v1[0];
}
int main() {
    IOS;
    //fill(isprime, true);
    //pre();
    int t;
    //cin>>t;
    t=1;
    while(t--){
		int n;
		cin>>n;
		vector<int> v[n];
		for(int i=0;i<n;i++){
			int id,x,y;
			cin>>id>>x>>y;
			v[i].push_back(x);
			v[i].push_back(y);
			v[i].push_back(id);
		}
		int cnt = 0;
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				vector<int> a = v[i],b = v[j];
				if(a[0]<=b[0]){
					if(a[1]==b[0]&&(a[2]==1||a[2]==3)&&(b[2]==1||b[2]==2)){
						cnt++;
					}
					else if(a[1]>b[0]){
						cnt++;
					}
				}
				else{
					if(b[1]==a[0]&&(b[2]==1||b[2]==3)&&(a[2]==1||a[2]==2)){
						cnt++;
					}
					else if(b[1]>a[0]){
						cnt++;
					}
				}

			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}

#include <iostream>
#include <vector>
#pragma GCC optimize ("-O3")
using namespace std;

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    vector<double> l(n), r(n);
    for (int i = 0; i < n; i++) {
        int t; cin >> t >> l[i] >> r[i];
        t--;
        if (t & 1) r[i] -= 0.5;
        if (t & 2) l[i] += 0.5;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            ans += (max(l[i], l[j]) <= min(r[i], r[j]));
        }
    }
    cout << ans << endl;
}


#include <iostream>
using namespace std;

int main() {

	long long a, b;
	cin >> a >> b;
	bool flag = false;
	for (long long i = 1; i <= b / i; i ++) {
		if (b % i == 0) {
			if (b / i + i == a) {
				flag = true;
				break;
			}
		}
	}
	if (flag) cout << "Yes";
	else cout << "No";
	return 0;

}

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

#define MAX( a , b )    ( a > b ) ? a : b
#define MIN( a , b )    ( a < b ) ? a : b

using ull = unsigned long long;
using ll = long long;
using ul = unsigned long;

int main(){
    ull A,B;
    ull result = 0;

    cin >> A >> B;
    ull C = B - A;

    for(ull i = C; i > 0; i--){
        ull j = B % i;
        ull k = B - j;
        if((k - i) >= A){
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define test int t;cin>>t;while(t--)
#define REP(i,n) for (int i = 1; i <= n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define ll long long
#define ld long double
#define int ll
#define pb push_back
#define ii pair<int,int>
#define llll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<ii>
#define vllll vector<llll>
#define ff first
#define ss second
#define INF 1000000000
#define HINF 1000000000000000
#define mem(a,b) memset(a,b,sizeof(a))
#define arrin(x,n) int x[n]; for(int o=0;o<n;o++) cin>>x[o]
#define arrout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define vecin(x,n); vi x;int hool; for(int o=0;o<n;o++) {cin>>hool; x.pb(hool);}
#define vecout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define all(x) x.begin(),x.end()
#define deb(x) cout<<#x<<'='<<x<<endl
#define deb2(x,y) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<endl
#define deb3(x,y,z) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<" "<<#z<<'='<<z<<endl
#define debarr(arr,n) for(int o=0;o<n;o++) deb2(o,arr[o])
#define debarrall(arr) debarr(arr,arr.size())
#define IO freopen("input.txt", "r", stdin); freopen("output.txt", "w+", stdout)
const double pi = 3.14159265358979323846;

int powersimple(int a, int b){//a^b
    int res=1;
    while(b>0){
        if(b&1)
		{res=(res*a);
		b--;}
        a=(a*a);
        b>>=1;
    }
    return res;
}

int ncr(int n,int k)
{
	int ans=1;
	if(k>n-k)
	k=n-k;
	for(int i=1;i<=k;i++)
	ans*=(n-i+1),ans/=i;
	return ans;
}

int power(int x,int y,int p)
{
    int res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

int modInverse(int n, int p)
{
    return power(n, p - 2, p);
}

int ncrModPFermat(int n,int r, int p)
{

    if (r == 0)
		return 1;

    int fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}

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

///////////////END OF TEMPLATE//////////////////

void solve()
{

	int i=0,j=0,k=0,m,n,p,sum=0,cnt=0;
	int x,y,a,b;
	long double r;
	cin>>x>>y>>a>>b;
	m=b/(a-1);
	if(y<=m)
	{
	    cnt=((y-1));
	    r=log(cnt/x)/log(a);
	    while((r-1)>=0)
	    {
	        sum++;
	        x*=a;
	        r-=1;
	    }
	    cout<<sum;
	    return;
	}
	//sum=((m)/x)/a;
	cnt=(m);
	r=log(cnt/x)/log(a);
	    while((r-1)>=0)
	    {
	        sum++;
	        x*=a;
	        r-=1;
	    }
	//x=sum*a*x;
	//if(x<m and x*a<y)
	{
	//    sum++;
	 //   x*=a;
	}
	sum+=(y-1-x)/b;
	cout<<sum;
}
int32_t main() {
    fast
    //IO;
	//cout<<fixed<<setprecision(15);
    //test
    {

        solve();

    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
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; }
const ll MOD = 1e9+7;

int main(){
    ll X, Y, A, B;
    cin >> X >> Y >> A >> B;

    ll L = B / (A-1);
    //if(B % (A-1) == 0)
    //    L--;

    ll ans = 0;

    while(L >= X){
        if(X*A >= Y)
            break;
        X *= A;
        ans++;
    }

    ll d = Y-1 - X;
    ll n = d / B;
    ans += n;
    X = X + n*B;

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