#include <bits/stdc++.h>

typedef long long ll;

const int maxn = 1e5 + 1e2;
int bipart[maxn];
int color[maxn];
std::vector<int> adj[maxn];

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

	int v, e;
	std::cin >> v >> e;

	for(int i = 0; i < e; i++){
		int f, s;
		std::cin >> f >> s;
		f--, s--;

		adj[f].push_back(s);
		adj[s].push_back(f);
	}

	memset(bipart, -1, sizeof(bipart));
	memset(color, -1, sizeof(color));

	ll single = 0;
	ll nonbipartite = 0;
	ll bipartite = 0;

	for(int i = 0; i < v; i++){
		if(bipart[i] == -1){
			std::queue<int> q;

			bipart[i] = 1;
			int size = 0;
			q.push(i);
			color[i] = 0;

			while(!q.empty()){
				int fr = q.front();
				size++;
				q.pop();

				for(int next : adj[fr]){
					if(color[next] == -1){
						bipart[next] = 0;
						color[next] = !color[fr];
						q.push(next);
					} else if(color[next] == color[fr]){
						bipart[i] = 0;
					}
				}
			}

			if(size == 1){
				single++;
			} else if(bipart[i]){
				bipartite++;
			} else {
				nonbipartite++;
			}
		}
	}

	ll sum = 0;
	sum += (ll) 2 * v * single;
	sum -= single * single;

	sum += 2 * bipartite * bipartite;
	sum += 2 * bipartite * nonbipartite;
	sum += nonbipartite * nonbipartite;

	std::cout << sum << '\n';
}

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}

//わけわかめ
int P[100001];
bool dame[100001];
int Find(int A) {
	if (P[A] < 0) return A << 1;
	return P[A] = Find(P[A] >> 1) ^ (P[A] & 1);
}

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

	int N = getint(), M = getint();
	rep1(i, N) P[i] = -1;
	rep(i, M) {
		int a = Find(getint());
		int b = Find(getint());

		//なんてセンスのない
		int a1 = a >> 1;
		int b1 = b >> 1;
		if (a1 == b1) {
			if (!((a ^ b) & 1)) dame[a1] = true;
			continue;
		}
		if (P[a1] > P[b1]) {
			P[b1] += P[a1];
			dame[b1] |= dame[a1];
			P[a1] = b ^ !(a & 1);
		}
		else {
			P[a1] += P[b1];
			dame[a1] |= dame[b1];
			P[b1] = a ^ !(b & 1);
		}
	}

	ll x = 0, y = 0, z = 0;
	rep1(i, N) {
		if (P[i] < 0) {
			if (P[i] == -1) z++;
			else if (dame[i]) y++;
			else x++;
		}
	}
	printf("%lld\n", x * x + (x + y) * (x + y) + 2 * N * z - z * z);

	Would you please return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<complex>
#define rep(i, a, n) for(int i=a;i<n;i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define R long double
#define P complex<R>
using namespace std;
const int mod = 1000000007;
const int INF = 1e12;
template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}
const R PI = acos(-1.0L);
const R inf = 1e13;
int x[110], y[110];
R ans[110];
inline R dist(int i, P q) {
   P p = P(x[i], y[i]);
   return norm(p - q);
}
signed main(){
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
	{
		cin>>x[i]>>y[i];
    }
    int limit = 200000;
    rep(i, 0, limit){
        R cur = 2.0 * PI / limit * i;
        P point = polar(inf, cur);
        R d = 1e130;
        int idx = -1;
        rep(j, 0, n) if(chmin(d, dist(j, point))) idx = j;
        ans[idx] += 1.0;
    }
    cout.precision(20);
	rep(i, 0, n) cout << fixed << 1.0 * ans[i] / limit << endl;
	return 0;
}


//Author: Fuadul Hasan(fuadul202@gmail.com)
#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int N   = 2e5 + 5;

int main(){

	int a,b;
	cin>>a>>b;
	cout<<max(a+(a-1),max(b+(b-1),a+b))<<endl;

	return 0;
}

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

int main() {
    int a,b,ans;
    cin >> a >> b;
    ans = (a==b ? 2*a : 2*max(a,b)-1);
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int,int>;
/* ちゃんと考えてわかって実装 */

ll amari(ll a){
    if(abs(a) % 2 == 0) return 0;
    else return 1;
}

ll sho(ll a){
    if(a<0 && amari(a)==1) return (a / (-2)) + 1;
    else return a / (-2);
}

stack<char> base_min2(ll a){
    stack<char> res;
    if(a == 0){
        res.push('0');
        return res;
    }
    while(true){
        if(a == 1){
            res.push('1');
            break;
        }
        else{
            if(amari(a) == 1){
                res.push('1');
                a = sho(a);
            }
            else if(amari(a) == 0){
                res.push('0');
                a = sho(a);
            }
        }
    }
    return res;
}

int main(void){
    ll n;
    cin >> n;
    stack<char> ans = base_min2(n);
    while(!ans.empty()){
        cout << ans.top();
        ans.pop();
    }
    return 0;
}

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

int main() {
  string S;
  cin>>S;
  for(int i=0;i<8;i++){
    string ans;
    ans+=S.at(0);
    int sum=S.at(0)-'0';
    for(int j=0;j<3;j++){
      if(i>>j&1){
        sum+=S.at(j+1)-'0';
        ans+="+";
        ans+=S.at(j+1);
      }
      else{
        sum-=S.at(j+1)-'0';
        ans+="-";
        ans+=S.at(j+1);
      }
    }
    if(sum==7){
        cout<<ans<<"=7"<< endl;
        return 0;

    }
  }
}


#include <cstdio>
#include <algorithm>
using namespace std;

int N, M, p[1001], s[1001 * 1001];

int main(){
    while(scanf("%d%d", &N, &M) != EOF && (N|M)){
        for(int x = 0; x < N; x++){
            scanf("%d", p+x);
        }
        p[N++] = 0;
        for(int x = 0; x < N; x++){
            for(int y = 0; y < N; y++){
                s[N*x + y] = p[x] + p[y];
            }
        }
        sort(s, s+N*N);
        int ans = 0;
        for(int x = 0; x < N*N; x++){
            int* lb = lower_bound(s, s + N * N, M - s[x]);
            if(lb != s) --lb;
            if(s[x] + *lb > M) continue;
            ans = max(ans, s[x] + *lb);
        }
        printf("%d\n", ans);
    }
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y);
#define chmin(x,y) x = min(x,y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

int main() {
    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    rep(i,h)rep(j,w) cin >> a[i][j];
    vector<P> now, next;
    rep(i,h)rep(j,w) {
        if (a[i][j]%2 == 0) continue;
        if (i == h-1 && j == w-1) continue;
        if (i == h-1) {
            now.emplace_back(i+1,j+1);
            next.emplace_back(i+1,j+2);
            a[i][j]--;
            a[i][j+1]++;
            continue;
        }
        if (j == w-1) {
            now.emplace_back(i+1,j+1);
            next.emplace_back(i+2,j+1);
            a[i][j]--;
            a[i+1][j]++;
            continue;
        }
        now.emplace_back(i+1,j+1);
        next.emplace_back(i+2,j+1);
        a[i][j]--;
        a[i+1][j]++;
    }
    int n = now.size();
    cout << n << endl;
    rep(i,n) {
        cout << now[i].first << " " << now[i].second << " " << next[i].first << " " << next[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k,i,n) for(ll i=k;i<n;++i)
ll dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int main(void){
    ll h,w;
    cin>>h>>w;
    string s[h];
    rep(0,i,h)cin>>s[i];
    ll ans=0;
    vector<vector<bool>>d(h,vector<bool>(w,false));
    rep(0,i,h){
        rep(0,j,w){
            if(d[i][j]==false){
                ll cw=0,cb=0;
                queue<pair<ll,ll>>q;
                d[i][j]=true;
                q.push({i,j});
                while(!q.empty()){
                    ll x=q.front().first,y=q.front().second;q.pop();
                    if(s[x][y]=='#')++cb;
                    else ++cw;
                    rep(0,k,4){
                        ll nx=x+dx[k],ny=y+dy[k];
                        if(!(nx>=0&&nx<h&&ny>=0&&ny<w))continue;
                        if(s[x][y]!=s[nx][ny]&&(!d[nx][ny])){
                            d[nx][ny]=true;
                            q.push({nx,ny});
                        }
                    }
                }
                //cout<<cb<<" "<<cw<<endl;
                ans+=cw*cb;
            }
        }
    }
    cout<<ans;
}

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

ll dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
ll counterb=0,counterw=0;
void dfs(ll x,ll y,ll H, ll W,vector<vector<bool>> &field,vector<vector<bool>> &check){
    check[x][y]=1;
    if(field[x][y]==1){counterb++;}
    if(field[x][y]==0){counterw++;}
    bool meter=(x+y+field[x][y])%2;
    for(int i=0;i<4;i++){if(x+dx[i]>=0&&x+dx[i]<H&&y+dy[i]>=0&&y+dy[i]<W){
        if(check[x+dx[i]][y+dy[i]]==0&&(x+y+dx[i]+dy[i]+field[x+dx[i]][y+dy[i]])%2==meter){
            dfs(x+dx[i],y+dy[i],H,W,field,check);
        };}
    ;}

}

int main(){
ll H;
cin >> H;
ll W;
cin >> W;
vector<vector<bool>> field(H, vector<bool>(W, 0));
vector<vector<bool>> check(H, vector<bool>(W, 0));
for(int i=0;i<H;i++){
 for(int j=0;j<W;j++){
     char k;
     cin>>k;
     if(k=='#'){field[i][j]=1;}
 ;}
;}
    ll ans=0;
for(ll i=0;i<H;i++){
 for(ll j=0;j<W;j++){

     if(check[i][j]==0){
         dfs(i,j,H,W,field,check);ans+=counterb*counterw;
         counterb=0;counterw=0;
     }

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

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template<class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main(){
    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> a(n, vector<ll>(m));
    REP(i, n){
        REP(j, m){
            cin >> a.at(i).at(j);
            a.at(i).at(j)--;
        }
        reverse(ALL(a.at(i)));
    }

    ll ans = n;
    vector<bool> use(m, true);
    REP(_, m){
        vector<ll> count(m, 0);
        REP(i, n){
            while(!use.at(a.at(i).back())){
                a.at(i).pop_back();
            }
            count.at(a.at(i).back())++;
        }
        const ll max_idx = max_element(ALL(count)) - count.begin();
        ans = min(ans, count.at(max_idx));
        use.at(max_idx) = false;
    }
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second
#define int long long

const int maxn = 310;
const ll inf = 1e18+10;
//const int mod = 998244353;
//const int mod = 1e9+7;

int n, m;
int a[maxn][maxn], ptr[maxn], cnt[maxn];
bool mark[maxn];

bool ok(int q)
{
   // cout<< q <<" ";
    set<pair<int,int>> se;
    for(int i = 1; i <= m; i++) cnt[i] = 0;
    for(int i = 1; i <= n; i++)
    {
        ptr[i] = 1;
        cnt[a[i][ptr[i]]]++;
    }
    for(int i = 1; i <= m; i++)
    {
        se.insert({cnt[i],i});
        mark[i] = 0;
    }

    while(se.size())
    {
        // cout<< se.size() <<" ";
        auto it = se.end(); it--;
        int X = (*it).S;
        //cout<< X <<" "<< cnt[X] <<" \n";
        if(cnt[X] <= q) return 1;

        se.erase(it);
        if(se.empty()) return 0;

        mark[X] = 1;
        for(int i = 1; i <= n; i++)
        {
            if(mark[a[i][ptr[i]]] == 0) continue;
            while(mark[a[i][ptr[i]]])
                ptr[i]++;
          //  if(X == 2)
          //      cout<< i <<" "<< ptr[i] <<"\n";
            int Y = a[i][ptr[i]];
            se.erase({cnt[Y],Y});
            cnt[Y]++;

          //  if(X == 2)
         //       cout<< Y <<" "<< cnt[Y] <<"\n";

            se.insert({cnt[Y],Y});
        }
    }
    return 0;
}

signed main()
{
    //ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    cin>> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin>> a[i][j];

    int hi = n+1, lo = 0;
    while(hi-lo > 1)
    {
        int tm = (hi+lo) >> 1;
        if(ok(tm)) hi = tm;
        else lo = tm;
    }
    //cout<< ok(2) <<" ";
    cout<< hi;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define repp(i,a,b) for(int i = (int)(a) ; i < (int)(b) ; ++i)
#define repm(i,a,b) for(int i = (int)(a) ; i > (int)(b) ; --i)

int main(){
	LL D; cin >> D;
	if(D%9 != 0) return cout << 0 << endl, 0;
	D /= 9;
	LL ans = 0;
	repp(i,1,19){
		LL z = 1;
		repp(k,0,i) z *= 10;
		LL x = D;
		LL a = 1;
		LL b = 1;
		while(z > 0){
			a *= 10-x%10-b;
			if(z == 1) break;
			x -= x%10*(z-1)/9;
			if(x < 0) x = -x;
			x /= 10;
			z /= 100;
			b = 0;
		}
		if(x == 0) ans += a;
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 +c - '0';
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
        out(x / 10);
    }
    putchar('0' + x % 10);
}
int64 D;

int Len(int64 D) {
    int res = 0;
    while(D) {
        res++;
        D /= 10;
    }
    return res;
}
int64 pw[20];
int64 v[20],d[20];
int up;
int64 dfs(int64 rem,int dep) {
    if(dep == up) return !rem;
    int64 t = rem / v[dep];
    int64 res = 0;
    if(abs(t - 1) <= 9 && abs(rem - (t - 1) * v[dep]) < v[dep]) {
        int c = 0;
        if(t - 1 >= 0 && !dep) c = 1;
        res += (d[t - 1 + 9] - c) * dfs(rem - (t - 1) * v[dep],dep + 1);
    }
    if(abs(t) <= 9 && abs(rem - t * v[dep]) < v[dep]) {
        int c = 0;
        if(t >= 0 && !dep) c = 1;
        res += (d[t + 9] - c) * dfs(rem - t * v[dep],dep + 1);
    }
    if(abs(t + 1) <= 9 && abs(rem - (t + 1) * v[dep]) < v[dep]) {
        int c = 0;
        if(t + 1 >= 0 && !dep) c = 1;
        res += (d[t + 1 + 9] - c) * dfs(rem - (t + 1) * v[dep],dep + 1);
    }
    return res;
}
void Solve() {
    read(D);
    int Ld = Len(D);
    int64 ans = 0;
    pw[0] = 1;
    for(int i = 1 ; i <= 18 ; ++i) pw[i] = pw[i - 1] * 10;
    for(int i = 0 ; i <= 9 ; ++i) {
        for(int j = 0 ; j <= 9 ; ++j) {
            d[i - j + 9]++;
        }
    }
    for(int i = Ld ; i <= 2 * Ld ; ++i) {
        for(int j = 0 ; j <= i / 2 ; ++j) v[j] = pw[i - j - 1] - pw[j];
        up = i / 2;
        int64 tmp = dfs(D,0);
        if(i & 1) tmp *= d[0 + 9];
        ans += tmp;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}


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

int main(){
    int x, y;
    cin >> x >> y;

    long long int ans = 0;
    if(x == y) ans = 0;
    else if(x == 0){
        ans = abs(y);
        if(y < 0) ans += 1;
    }
    else if(y == 0){
        ans = abs(x);
        if(x > 0) ans += 1;
    }
    // different sign
    else if(x == -y) ans = 1;
    else if(x > 0 && y < 0) ans = 1 + abs(x-(-y));
    else if(x < 0 && y > 0) ans = min(y-x, 1+abs(y-(-x)));
    // same sign
    else if(x > y) ans = 2 + (x-y);
    else if(x < y) ans = y-x;

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

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
typedef unsigned long long ull;
#define vvi std::vector<std::vector<int> >
#define vvs std::vector<std::vector<string> >
#define MODs 1000000007;
typedef long long int ll;
using namespace std;
int main(int argc, char const *argv[]) {
  int N;
  ll ans = 0;
  string s, S, t, T="";
  std::cin >> N;
  std::cin >> s;
  S = s.substr(0, N);
  t = s.substr(N);
  for(int i=0;i<N;i++) T+=t[N-1-i];
  std::vector<int> num(26, 0);
  for(int i=0;i<N;i++) num[S[i]-'a']++, num[T[i]-'a']--;
  bool flag = true;
  for(int i=0;i<26;i++) if(num[i]!=0) flag = false;
  vvs str = vv(0, 2, "", string);
  for(int i=0;i<(1<<N);i++){
    int n = i+(1<<20), count=0;
    string ah = "", bh = "";
    while(count<N){
      if(n%2==1) ah += T[count];
      if(n%2==0) bh += T[count];
      count++;
      n/=2;
    }
    str.push_back(std::vector<string> {ah, bh});
  }
  sort(str.begin(), str.end());
  for(int i=0;i<(1<<N);i++){
    int n = i + (1<<20), count=0;
    string ah="", bh="";
    while(count<N){
      if(n%2==1) ah += S[count];
      if(n%2==0) bh += S[count];
      count++;
      n/=2;
    }
    int k = upper_bound(str.begin(), str.end(), std::vector<string> {ah,bh}) - lower_bound(str.begin(), str.end(), std::vector<string> {ah, bh});
    ans += k;
  }
  std::cout << (!flag?0:ans) << '\n';
  return 0;
}


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

typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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

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

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MX = 1000001;

ll N;

struct Comp {
	bool operator()(const l_l &a, const l_l &b) {
		return a.first > b.first;
	}
};

void solve() {
    cin >> N;
    vector<string> a(N);
    vector<l_l> adds, subs;

	int tlc = 0, trc = 0;

    for (int i = 0; i < N; i++) {
		int lc = 0, rc = 0;
		cin >> a[i];
		for (auto& c : a[i]) {
			if (c == '(') {
				lc++;
			} else {
				if (lc) lc--;
				else rc++;
			}
		}
		if (lc && !rc) tlc += lc;
		else if (!lc && rc) trc += rc;
		else if (lc && rc) {
			if (lc >= rc) adds.push_back({rc, lc});
			else subs.push_back({rc, lc});
		}
	}
	sort(adds.begin(), adds.end());
    sort(subs.begin(), subs.end(), Comp());

    bool pos = true;
    for (auto& i : adds) {
		if (i.first > tlc) {
			pos = false;
			break;
		} else {
			tlc -= i.first;
			tlc += i.second;
		}
	}

	for (auto& i : subs) {
		if (i.first > tlc) {
			pos = false;
			break;
		} else {
			tlc -= i.first;
			tlc += i.second;
		}
	}

    if (pos && tlc == trc) cout << "Yes";
    else cout << "No";
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
    ll T = 1; // cin >> T;
    while (T--) solve();
    return 0;
    // You should actually read the stuff at the bottom
}

/* Stuff to Look For
 * -----------------
 * Int overflow, array bounds
 * Initializing all variables, avoid weird behavior
 * Edge cases(n = 0, n = 1)
 * Just return 0 after result
 */

// #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=998244353;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-11;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	inline long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
char in[1100000];
pair<int,int>p[1100000];
vector<pair<int,int> > L;
vector<pair<int,int> > R;

int main(){
	int a;scanf("%d",&a);
	int sum=0;
	for(int i=0;i<a;i++){
		scanf("%s",in);
		int t=0;
		int m=0;
		for(int j=0;in[j];j++){
			if(in[j]=='(')t++;
			else t--;
			m=min(m,t);
		}
		p[i]=make_pair(t,m);
		sum+=t;
	}
	if(sum){
		printf("No\n");return 0;
	}
	for(int i=0;i<a;i++){
		if(p[i].first>=0){
			L.push_back(make_pair(-p[i].second,i));
		}else{
			R.push_back(make_pair(p[i].second-p[i].first,i));
		}
	}
	std::sort(L.begin(),L.end());
	std::sort(R.begin(),R.end());
	int cur=0;
	for(int i=0;i<L.size();i++){
		int I=L[i].second;
		if(cur+p[I].second<0){
			printf("No\n");return 0;
		}
		cur+=p[I].first;
	}
	for(int i=0;i<R.size();i++){
		int I=R[i].second;
		if(cur+p[I].second<0){
			printf("No\n");return 0;
		}
		cur+=p[I].first;
	}
	printf("Yes\n");
}


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

using namespace std;

int main(void) {
	int m,nmin,nmax;
	while (cin >> m >> nmin >> nmax, m) {
		vector<int> p(m);
		for (int i = 0; i < m; ++i) {
			cin >> p[i];
		}
		int res = 0;
		int gap = 0;
		for (int i = nmin; i <= nmax; ++i) {
			if (p[i-1] - p[i] >= gap) {
				res = i;
				gap = p[i-1] - p[i];
			}
		}
		cout << res << endl;
	}
	return 0;
}

#include <iostream>
#include <string>

using namespace std;

const int MOD = 1000000007;

int solve(const string& s){
    const int N = s.size()/2;
    long long res = 1;
    int numS = 0, sumS = 0, inc = 0;
    for(auto& c : s){
        if(c == 'B'){
            if(inc%2 == 0){
                ++inc;
                ++numS;
                ++sumS;
            } else {
                if(numS == 0) return 0;
                --inc;
                res = (res * numS) % MOD;
                --numS;
            }
        } else {
            if(inc%2 == 0){
                if(numS == 0) return 0;
                --inc;
                res = (res * numS) % MOD;
                --numS;
            } else {
                ++inc;
                ++numS;
                ++sumS;
            }
        }
    }
    if(sumS != N) return 0;
    for(int i=1;i<=N;i++) res = (res*i)%MOD;
    return res;
}

int main(){
    int N;
    while(cin >> N){
        string s; cin >> s;
        cout << solve(s) << endl;
    }
}


#include <iostream>
#include <algorithm>

int main()
{
    int n;
    int64_t a, b;
    std::cin >> n >> a >> b;
    int x;
    std::cin >> x;

    int64_t sum = 0;
    for (int i = 1; i < n; ++i)
    {
        int y;
        std::cin >> y;
        sum += std::min((y - x) * a, b);

        x = y;
    }

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

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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}
struct edge {
  ll to, cost;
};

int main() {
  int N, M, X, Y; cin >> N >> M >> X >> Y;
  vector<int> x(N); REP(i, N) cin >> x[i];
  vector<int> y(M); REP(i, M) cin >> y[i];
  sort(all(x)); sort(all(y));
  string ans = "War";
  for(int i = X + 1; i <= Y; ++i) {
    if(x[N - 1] < i && i <= y[0]) ans = "No War";
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define prec(y,x) fixed<<setprecision(y)<<x
#define inf 1e18
#define pi 3.1415926535
typedef tree<int, null_type, less<int>, rb_tree_tag,                    //if using ll change int to ll
        tree_order_statistics_node_update> pbset;
#define fob(x) find_by_order(x)
#define ook(x) order_of_key(x)
int main()

{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	string s;  ll l, r; l = 0; r = 0; vector<pair<int, pair<int, int>>> v1, v2;
	vector<int> ans, ans1;
	for (int i = 1; i <= n; i++)
	{
		cin >> s; int a = 0; int b = 0; //cout << s << endl;
		for (int j = 0; j < s.size(); j++)
		{

			if (s[j] == '(')
				a++;
			else
			{
				if (a == 0)
					b++;
				else
					a--;
			}
		}
		if (a == 0 && b == 0)
		{
			ans.pb(i);
		}
		else if (a == 0)
		{
			r += b;
			ans1.pb(i);
		}
		else if (b == 0)
		{
			l += a; ans.pb(i);
		}
		else
		{
			if (a >= b)
			{
				v1.pb(mk(b, mk(a, i)));
			}
			else
			{
				v2.pb(mk(a, mk(b, i)));
			}
		}
	}

	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	int flag = 0;
	for (int i = 0; i < v1.size(); i++)
	{
		int b = v1[i].ff;
		int a = v1[i].ss.ff;
		ans.pb(v1[i].ss.ss);
		l -= b;
		if (l < 0)
		{
			flag = 1; break;
		}
		l += a;
	}
	if (flag == 1)
	{
		cout << "No" << endl; return 0;
	}
	for (int i = v2.size() - 1; i >= 0; i--)
	{
		int a = v2[i].ff;
		int b = v2[i].ss.ff;
		ans.pb(v2[i].ss.ss);
		l -= b;
		if (l < 0)
		{
			flag = 1; break;
		}
		l += a;
	}

	if (flag == 1 || l != r)
	{
		cout << "No" << endl;
	}
	else
	{
		cout << "Yes" << endl;

	}
	return 0;
}

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

const int S=(1<<20)+5;
char buf[S],*H,*T;
inline char Get()
{
    if(H==T) T=(H=buf)+fread(buf,1,S,stdin);
    if(H==T) return -1;return *H++;
}
inline int read()
{
    int x=0;char c=Get();
    while(!isdigit(c)) c=Get();
    while(isdigit(c)) x=x*10+c-'0',c=Get();
    return x;
}

typedef long long LL;
const int N=200010;
int n,q,a,b,X[N];
int delta[N];
LL sumd[N],f[N];

struct SegmentTree
{
    LL mnv[N<<2];int leaf;
    void build()
    {
        for(leaf=1;leaf<=(n+2);leaf<<=1);
        for(int i=1;i<=leaf+n;i++) mnv[i]=INT64_MAX/3;
    }
    void modify(int k,LL x)
    {
        mnv[leaf+k]=min(mnv[leaf+k],x);
        for(int i=(leaf+k)>>1;i;i>>=1)
            mnv[i]=min(mnv[i<<1],mnv[i<<1|1]);
    }
    LL qmin(int l,int r)
    {
        LL res=INT64_MAX/3;
        for(l=leaf+l-1,r=leaf+r+1;l^r^1;l>>=1,r>>=1)
        {
            if(~l&1) res=min(res,mnv[l^1]);
            if(r&1) res=min(res,mnv[r^1]);
        }
        return res;
    }
}t1,t2;

LL solve(int a,int b)
{
    X[0]=a;
    for(int i=1;i<=q;i++)
    {
        delta[i]=abs(X[i]-X[i-1]);
        sumd[i]=sumd[i-1]+delta[i];
    }
    f[1]=abs(b-X[1]);
    t1.build();t2.build();
    t1.modify(X[0],f[1]-sumd[1]-X[0]);
    t2.modify(X[0],f[1]-sumd[1]+X[0]);
    for(int i=2;i<=q;i++)
    {
        f[i]=t1.qmin(1,X[i])+X[i]+sumd[i-1];
        f[i]=min(f[i],t2.qmin(X[i],n)-X[i]+sumd[i-1]);
        t1.modify(X[i-1],f[i]-sumd[i]-X[i-1]);
        t2.modify(X[i-1],f[i]-sumd[i]+X[i-1]);
    }
    LL ans=INT64_MAX/3;
    for(int i=0;i<=q;i++) ans=min(ans,f[i]+sumd[q]-sumd[i]);
    return ans;
}

int main()
{
    n=read();q=read();a=read();b=read();
    for(int i=1;i<=q;i++) X[i]=read();
    LL ans=min(solve(a,b),solve(b,a));
    printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
const int MOD=1e9+7;
//const int MOD=998244353;
const long long LINF=1e18;
#define int long long
//template
/*
問題の言い換え
//0の行き先が0じゃなかったら0にしてans++;
0を根とする根付き木を考える
各部分根付き木の深さが(根の深さを0として）K以下になるように分解するとき最小でいくつに分解しないといけないか
これは葉から決めていけばよさそう
*/
//main
signed main(){
  int N,K,ans=0;cin>>N>>K;
  std::vector<int> v(N),in(N,0),depth(N,1);;
  for(int i=0;i<N;i++)cin>>v[i],v[i]--;
  if(v[0])ans++;v[0]=0;
  for(int i=0;i<N;i++)in[v[i]]++;
  queue<int> que;
  for(int i=0;i<N;i++)if(!in[i])que.push(i);
  while(que.size()){
    int p=que.front();que.pop();if(p==0)continue;
    if(depth[p]==K&&v[p])depth[p]=0,ans++;
    depth[v[p]]=max(depth[v[p]],depth[p]+1);
    if(!--in[v[p]])que.push(v[p]);
  }
  cout<<ans<<endl;
}

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

int A[110], ans[110];

int main() {
	int N, M;
	cin >> N >> M;
	int cnt=0;
	for(int i=0; i<M; ++i){
		cin >> A[i];
		cnt += A[i]%2;
	}
	if(cnt>2){
		cout << "Impossible" << endl;
		return 0;
	}
	if(cnt>0){
		bool flag=false;
		for(int i=0; i<M; ++i){
			if(A[i]%2==1){
				if(flag){
					swap(A[i], A[M-1]);
				}else{
					swap(A[i], A[0]);
					flag=true;
				}
			}
		}
	}
	cout << A[0];
	for(int i=1; i<M; ++i) cout << ' ' << A[i];
	cout << endl;
	int bs=0;
	if(A[0]>1){
		ans[bs]=A[0]-1;
		++bs;
	}
	A[0]=2;
	for(int i=0; i<M-1; ++i){
		ans[bs]=A[i];
		++bs;
	}
	if(A[M-1]>1){
		ans[bs]=A[M-1]-1;
		++bs;
	}
	cout << bs << endl;
	cout << ans[0];
	for(int i=1; i<bs; ++i) cout << ' ' << ans[i];
	cout << endl;
	return 0;
}


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

int main(){
    ll n,m,x,y;
    cin>>n>>m>>x>>y;
    ll a_max=-101;
    ll b_min=101;
    ll tmp;
    rep(i,n){
        cin>>tmp;
        a_max=std::max(a_max, tmp);
    }
    rep(i,m){
        cin>>tmp;
        b_min=std::min(b_min, tmp);
    }

    if(a_max<b_min && x<b_min && a_max<y){
        cout<<"No War"<<endl;
    }else{
        cout<<"War"<<endl;
    }

}

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main(){
  ll n,a,b,c,d,e;
  cin>>n>>a>>b>>c>>d>>e;

  ll mi=min({a,b,c,d,e});
  ll x=(n+mi-1)/mi-1;

  cout<<x+5<<endl;

  return 0;
}


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

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

void game(int n, int& ans, int ni, int nj, int gi, int gj, vector<vector<int> >& table){

    if(ni == gi && nj == gj){
        if(ans == -1) ans = n;
        else ans = min(ans, n);
        return;
    }else if(n == 10){
        return;
    }else{

        for(int h = 0; h < 4; h++){
            int nexti = ni;
            int nextj = nj;
            if(table[ni + di[h]][nj + dj[h]] == 1) continue;
            while(1){
                nexti += di[h];
                nextj += dj[h];
                if(table[nexti][nextj] == 1){
                    table[nexti][nextj] = 0;
                    game(n + 1, ans, nexti - di[h], nextj - dj[h], gi, gj, table);
                    table[nexti][nextj] = 1;
                    break;
                }else if(table[nexti][nextj] == 3){
                    game(n + 1, ans, nexti, nextj, gi, gj, table);
                    break;
                }else if(table[nexti][nextj] == -1){
                    break;
                }
            }
        }
    }
    return;
}

int main(){

    while(1){
        int w, h; cin >> w >> h;
        if(!w) break;

        int si, sj, gi, gj;
        vector<vector<int> > table(h + 2, vector<int> (w + 2, -1));
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> table[i][j];
                if(table[i][j] == 2){
                    si = i;
                    sj = j;
                    table[i][j] = 0;
                }
                if(table[i][j] == 3){
                    gi = i;
                    gj = j;
                }
            }
        }

        int ans = -1;
        game(0, ans, si, sj, gi, gj, table);

        cout << ans << endl;
    }

    return 0;
}

#include <cstdio>
#include <vector>

using namespace std;

class Edge{
public:
	int from,to,cost;
	Edge(int f,int t,int c){
		from = f;
		to = t;
		cost = c;
	}
	void swap(){
		int a = from;
		from = to;
		to = a;
	}
};

vector<Edge> edge;

int n,m;
const int INF = 999999999;

int solve(int s){
	int* d = new int[n + 1];
	for(int i=0;i<=n;i++){
		d[i] = INF;
	}
	d[s] = 0;

	while(1){
		bool f = true;
		vector<Edge>::iterator it;
		for(it = edge.begin();it<edge.end();it++){
			if(d[it->from] != INF){
				if(d[it->to] > d[it->from] + it->cost){
					d[it->to] = d[it->from] + it->cost;
					f = false;
				}
			}
		}
		if(f){
			break;
		}
	}
	int ans = 0;
	for(int i=2;i<=n;i++){
		if(d[i] <= 2){
			ans += 1;
		}
	}
	delete[] d;
	return ans;
}

int main(){
	int a,b;
	while(scanf("%d %d",&n,&m) && (n || m)){
		edge.clear();
		for(int i=0;i<m;i++){
			scanf("%d %d",&a,&b);
			edge.push_back( Edge(a,b,1) );
			edge.push_back( Edge(b,a,1) );
		}
		printf("%d\n",solve(1));
	}
}


#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "complex"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 1e-8;

long long int N, M, K, H, W, L, R;

list<int> Prime(int num) {
	list<int>P;
	for (int i = 5; i <= num; i += 6) {
		bool flag = true;
		for (auto j : P) {
			if (j*j > i) {
				break;
			}
			if (i%j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i);
		flag = true;
		for (auto j : P) {
			if (j*j > i + 2) {
				break;
			}
			if ((i + 2) % j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i + 2);
	}
	P.push_front(3);
	P.push_front(2);
	return P;
}

inline long long int gcd(long long int a, long long int b) {
	if (a < b)a ^= b ^= a ^= b;
	while (b) {
		a %= b;
		a ^= b ^= a ^= b;
	}
	return a;
}

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

	cin >> N;
	auto P = Prime(10000);
	list<int>p;
	P.pop_front();
	while (p.size() < N + 2) {
		p.push_back(P.front());
		P.pop_front();
	}
	vector<int>tate((N+2)/2);
	vector<int>yoko((N+2)/2);
	for (int i = 0; i < (N + 2) / 2; i++) {
		if (i % 2) {
			tate[i] = p.front();
			p.pop_front();
		}
		else {
			tate[i] = p.back();
			p.pop_back();
		}
	}
	for (int i = 0; i < N/2+1; i++) {
		if (i % 2) {
			yoko[i] = p.front();
			p.pop_front();
		}
		else {
			yoko[i] = p.back();
			p.pop_back();
		}
	}
	vector<vector<long long int>>v(N, vector<long long int>(N));
	map<long long int, int>m;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if ((i^j) & 1) {
				v[i][j] = tate[i / 2] * yoko[j / 2] * 2;
				if (m[v[i][j]])v[i][j] *= 2;
				m[v[i][j]]++;
			}
		}
	}
	int dir[] = { 0,1,0,-1,0 };
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (!v[i][j]) {
				v[i][j] = 1;
				for (int k = 0; k < 4; k++) {
					int ny = i + dir[k];
					int nx = j + dir[k + 1];
					if (ny < 0 || nx < 0 || ny >= N || nx >= N)continue;
					v[i][j] = v[i][j] / gcd(v[i][j], v[ny][nx])*v[ny][nx];
				}
				v[i][j]++;
				if (m[v[i][j]])v[i][j] = v[i][j] * 2 - 1;
				m[v[i][j]]++;
			}
		}
	}
	long long int mx = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (j)cout << " ";
			cout << v[i][j];
			mx = max(mx, v[i][j]);
		}
		cout << endl;
	}
	return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

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

const int MAXN = 2e5 + 10;
int pc;
int pr[MAXN] , f[MAXN];
LL A[MAXN] , B[MAXN];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline void G(int n) {
		for (int i = 2; i <= n; ++i) {
				if (!f[i]) f[i] = pr[++pc] = i;
				for (int j = 1; j <= pc && i * pr[j] <= n; ++j) {
						f[i * pr[j]] = pr[j];
						if (i % pr[j] == 0) break;
				}
		}
}
int main() {

		int n;
		scanf("%d" , &n); G(10000);
		for (int i = 1; i <= n; ++i) A[i] = pr[i & 1 ? i / 2 + 1 : n + n - i / 2 + 1];
		for (int i = 1; i <= n; ++i) B[i] = pr[(i & 1 ? n - i / 2 : n + i / 2) + (n & 1)];
		A[0] = A[n + 1] = B[0] = B[n + 1] = 1;
		for (int i = 1; i <= n; ++i , printf("\n"))
		for (int j = 1; j <= n; ++j)
				if ((i + j) & 1) printf("%lld " , (LL) A[(i + j) / 2] * A[(i + j) / 2 + 1] * B[(n + i - j + (n & 1)) / 2] * B[(n + i - j + (n & 1)) / 2 + 1] + 1);
				else printf("%lld " , A[(i + j) / 2] * B[(n + i - j + (n & 1)) / 2]);
	  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<numeric>
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)
int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    int sum = accumulate(a.begin(), a.end(), (int)0);
    int frame_num = 0;
    int frame_min_dif = 1e8;
    rep(i, n) {
        int tmp_num = a[i] * n;
        int dif = abs(tmp_num - sum);
        if ( dif < frame_min_dif ) {
            frame_num = i;
            frame_min_dif = dif;
        }
    }
    cout << frame_num << endl;
    return 0;
}

#include <iostream>

using namespace std;

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

	double ans = 1.0;
	if (d == 0) {
		ans *= m - 1;
		ans /= n;
	}else{
		ans = 2.0 * (n - d);
		ans *= m - 1;
		ans /= n * n;
	}
	printf("%.10f\n", ans);

	return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
char str[110];
int b[110];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<a;i++)scanf("%d",b+i);
		scanf("%s",str);
		for(int i=0;str[i];i++){
			int p=0;
			if('a'<=str[i]&&str[i]<='z')p=str[i]-'a';
			else p=str[i]-'A'+26;
			p-=b[i%a];
			if(p<0)p+=52;
			if(p<26)str[i]='a'+p;
			else str[i]='A'+p-26;
		}
		printf("%s\n",str);
	}
}

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

char byte_minus(char ch, int num)
{
  if ( 97 <= ch && ch <= 122 )
  {
    if ( ch - 97 >= num )
    {
      return ch - num;
    }
    else
    {
      return byte_minus('Z', num - (ch - 96) );
    }
  }
  else
  {
    if ( ch - 65 >= num )
    {
      return ch - num;
    }
    else
    {
      return byte_minus('z', num - (ch - 64) );
    }
  }
}

int main()
{
  while (true)
  {
    int n;
    cin >> n;
    if ( n == 0 ) break;
    int* keys = new int[n];
    for (int i = 0; i < n; i++)
    {
      cin >> keys[i];
    }
    string cipher;
    //getline(cin, cipher);
    cin >> cipher;
    for (int i = 0, size = cipher.length(); i < size; i++)
    {
      int key_index = i % n;
      char result = byte_minus(cipher[i], keys[key_index]);
      cout << result;
    }
    cout << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
//const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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; }

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

  int n = s.length();
  cout << s.substr(0, n - 8) << endl;

  return 0;
}

#include<iostream>
#include<algorithm>
#include <string>
using namespace std;
int main() {
	int a, b, c, d, n = 0, ans = 0, ans2 = 0;
	string s;
	cin >>s;
	s.pop_back();
	s.pop_back();
	s.pop_back();
	s.pop_back();
	s.pop_back();
	s.pop_back();
	s.pop_back();
	s.pop_back();
	cout << s << endl;
}


#include <stdio.h>
int main()
{
  int a[101],n,cou;
  scanf("%d",&n);
  for(cou=1;cou<=n;cou++){
    scanf("%d",&a[cou]);
  }
  for(cou=n;cou>=1;cou--){
    printf("%d",a[cou]);
    if(cou==1){
      break;
    }
      printf(" ");
  }
  printf("\n");
  return 0;
}

#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using ll = long long;
using vll = vector<long long>;
using vvll = vector<vll>;
const double EPS = 1e-9;
const long long MOD=1e9+7;
ll dx4[4]={1,0,-1,0};
ll dy4[4]={0,-1,0,1};
ll dx8[8]={1,0,-1,1,-1,1,0,-1};
ll dy8[8]={1,1,1,0,0,-1,-1,-1};
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define SZ(x) ((int)(x).size())
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; }

using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
ll n;
double dfs(vvvd &dp, ll n1, ll n2, ll n3){
  if(n1==0 && n2==0 && n3==0) return 0.0;
  if(dp[n1][n2][n3] >= 0.0){
    return dp[n1][n2][n3];
  }
  double expect = (double)n / (n1+n2+n3);
  if(n1 > 0){
    expect += dfs(dp, n1-1, n2, n3) * (double)n1/(n1+n2+n3);
  }
  if(n2 > 0){
    expect += dfs(dp, n1+1, n2-1, n3) * (double)n2/(n1+n2+n3);
  }
  if(n3 > 0){
    expect += dfs(dp, n1, n2+1, n3-1) * (double)n3/(n1+n2+n3);
  }
  return dp[n1][n2][n3] = expect;
}
int main(){
  cin>>n;
  vll a(n); rep(i,n) cin>>a[i];
  double p=1.0/n;
  ll n1,n2,n3; n1=n2=n3=0;
  rep(i,n){
    if(a[i]==1) n1++;
    else if(a[i]==2) n2++;
    else if(a[i]==3) n3++;
  }
  vvvd dp(n+1,vvd(n+1,vd(n+1,-1)));
  double ans = dfs(dp,n1,n2,n3);
  cout<< fixed << setprecision(10)<< ans<<endl;
}


// AOJ ITP 1_5_D
#include<cstdio>

void call( int n )
{
	int i = 1;
	do
	{
		int x = i;

		if( x % 3 == 0 )
			printf( " %d", i );
		else
		{
			do
			{
				if( x % 10 == 3 )
				{
					printf( " %d", i );
					break;
				}
				x /= 10;
			} while( x );
		}
	}while( ++i <= n );

	puts("");

	return;
}

int main()
{
	int n;
	scanf( "%d", &n );
	call( n );

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF=1001001001;
const int mod=1e9+7;

int main(){
  int N,K;
  cin>>N>>K;
  vector<char>D(K);
  for(int i=0;i<K;i++){
    cin>>D[i];
  }
  for(int i=N;;i++){
    string t=to_string(i);
    bool use=true;
    for(int k=0;k<t.size();k++){
      bool ok=true;
      for(int j=0;j<K;j++){
        if(t[k]==D[j]){
          ok=false;
        }
      }
      if(!ok){use=false;}
    }
    if(use){cout<<t<<endl;return 0;}
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod=1000000007;
const int INF=1001001001;

vector<pair<int64_t,int64_t>>prime_factorize(int64_t x){
  vector<pair<int64_t,int64_t>>p;
  for(int64_t i=2;i*i<=x;i++){
    int cnt=0;
    if(x%i==0){
    while(x%i==0){cnt++;x/=i;}
    p.push_back(make_pair(i,cnt));
    }
  }
  if(x!=1){p.push_back(make_pair(x,1));}
  return p;
}

int main() {
  string s;
  cin>>s;
  int n=s.size();
  stack<char>k;
  for(int i=0;i<n;i++){
    if(k.empty()){k.push(s[i]);}
    else if(s[i]=='T'&&k.top()=='S'){k.pop();}
    else{k.push(s[i]);}
  }
  cout<<k.size()<<endl;
  return 0;
}


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

ll x = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  if ((n-7) % 10 == 0) {
    cout << "Yes" << endl;
    return 0;
  }
  n /= 10;
  if ((n-7) % 10 == 0) {
    cout << "Yes" << endl;
    return 0;
  }
  n /= 10;
  if ((n-7) % 10 == 0) {
    cout << "Yes" << endl;
    return 0;
  }
  cout << "No" << endl;
  return 0;
}

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

using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  bool ok = false;
  if (s[0] == '7')
    ok = true;
  if (s[1] == '7')
    ok = true;
  if (s[2] == '7')
    ok = true;
  if (ok) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


#include <iostream>
using namespace std;
int n;
int A[100000];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)cin>>A[i];
	int x=A[n-1];
	int j=A[0]<x?1:0;
	for(int i=1;i<n-1;i++)
	{
		if (A[i]<=x)
		{
			int aj=A[j];
			A[j]=A[i];
			A[i]=aj;
			j++;
		}
	}
	A[n-1]=A[j];
	A[j]=x;
	for (int i=0;i<j;i++)
	{
		if (i) cout <<' ';
		cout<<A[i];
	}
	if(j)cout<<' ';
	cout<<'['<<A[j]<<']';
	for(int i=j+1;i<n;i++)//bug here i=j+1
	{
		cout <<' '<<A[i];
	}
	cout<<endl;//bug here
}

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<cmath>
#include<list>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int maxn = 100000 + 8;

int n,a[maxn];
int Partition(int a[],int p,int r)
{
    int last_element = a[r];
    int i = p-1;
    for(int j = p; j < r; ++j)
    {
        if(a[j] <= last_element)
        {
            i++;
            swap(a[i],a[j]);
        }
    }
    swap(a[i+1],a[r]);
    return i+1;
}
int main()
{
    scanf("%d",&n);
    fill(a,a+n,0);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d",&a[i]);
    }
    int pos = Partition(a,0,n-1);
    for(int i = 0;i < n; ++i){
        if(i == pos) printf("");
        printf("%s%s%d%s", i==0?"":" ",i == pos?"[":"",a[i],i == pos?"]":"");
    }
    printf("\n");
    return 0;
}

///*
//const int maxn = 1e5 + 8;
//
//int n,k,w[maxn];
//
//int check(ll P)
//{
//    for(int i = 0; i < n; ++i){
//
//    }
//}
//ll BinarySearch()
//{
//    ll left = 0;
//    ll right = maxn * maxn;
//    while(left <= right)
//    {
//        ll mid = (left + right) / 2;
//        int r = check(mid);
//        if(r >= n)  ;
//        else ;
//    }
//    return right;
//}
//int main()
//{
//    scanf("%d%d",&n,&k);
//    fill(w,w+n,0);
//    for(int i = 0; i < n; ++i) scanf("%d",&w[i]);
//    ll ans = BinarySearch();
//}
//
/*
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int tile = 0;
int Board[8][8];

void ChessBoard(int tr,int tc,int dr,int dc,int size){
    if(size == 1)return ;
    int t = tile++;
    int s = size / 2;
    if(dr < tr + s && dc < tc + s){
        ChessBoard(tr,tc,dr,dc,s);
    }
    else{
        Board[tr + s - 1][tc + s - 1] = t;
        ChessBoard(tr,tc,tr + s - 1, tc + s - 1,s);
    }

    if(dr < tr + s && dc >= tc + s){
        ChessBoard(tr,tc + s,dr,dc,s);
    }
    else{
        Board[tr + s - 1][tc + s] = t;
        ChessBoard(tr,tc+s,tr + s - 1, tc + s,s);
    }

    if(dr >= tr + s && dc < tc + s){
        ChessBoard(tr+s,tc,dr,dc,s);
    }
    else{
        Board[tr + s][tc + s - 1] = t;
        ChessBoard(tr+s,tc,tr + s, tc + s - 1,s);
    }

    if(dr >= tr + s && dc >= tc + s){
        ChessBoard(tr+s,tc+s,dr,dc,s);
    }
    else{
        Board[tr + s][tc + s] = t;
        ChessBoard(tr+s,tc+s,tr + s, tc + s,s);
    }

}

void Print(){
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            cout << setw(3) << Board[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

int main(){
    memset(Board,0,sizeof Board);
    ChessBoard(0,0,0,1,8);
    Print();
}
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mn=2e6+10;
const ll mod=998244353;
ll inv(ll x){
    ll ans=1;
    for(ll i=mod-2;i;i>>=1,x=x*x%mod)if(i&1)ans=ans*x%mod;
    return ans;
}
ll fact[mn];
ll ch(ll n,ll k){
    if(k<0||k>n)return 0;
    return fact[n]*inv(fact[k])%mod*inv(fact[n-k])%mod;
}
ll hail(ll k,ll n,ll m){
    m++;
    return ch(k+n-1,n-1)-n*ch(k+n-1-m,n-1)%mod;
}
int main()
{
    ll n,m,i;
    fact[0]=1;
    for(i=1;i<mn;i++)fact[i]=fact[i-1]*i%mod;
    scanf("%lld%lld",&n,&m);
    ll ans=0;
    for(i=m%2;i<=min(n,m);i+=2){
        ans+=ch(n,i)*hail((3*m-i)/2,n,m-1)+(hail((3*m-i)/2,n,m)-hail((3*m-i)/2,n,m-1))*ch(n-1,i);
        ans%=mod;
    }
    if(ans<0)ans+=mod;
    printf("%lld",ans);
}

#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
const long long MOD = 1e9+7;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ll> pdl;
typedef pair<ld, ld> pdd;
//typedef boost::multiprecision::cpp_int bigint;

ll A, B;

int main() {
	cin >> A >> B;
	vector<vector<ll>> MAP;   //1->黒 0->白
	MAP.resize(100, vector<ll>(100, 0));
	if (A > 1) {
		ll x = 0;
		for (ll c = 0; c < floor((ld)(A - 1) / 50); c++, x += 2) {
			for (ll y = 0; y < 100; y += 2) {
				MAP[x][y + 1] = 1;
				MAP[x + 1][y] = 1;
				MAP[x + 1][y + 1] = 1;
			}
		}
		for (ll y = 0; y < (A - 1) % 50; y++) {
			MAP[x + 1][2 * y + 1] = 1;
			MAP[x + 1][2 * y] = 1;
			MAP[x][2 * y + 1] = 1;
		}
		x += 3;
		for (ll b = 2; b <= B; x += 2) {
			for (ll y = 0; y < 100 && b <= B; b++, y += 2) {
				MAP[x][y] = 1;
			}
		}
		cout << "100 100" << endl;
		for (ll y = 0; y < 100; y++) {
			for (ll x = 0; x < 100; x++) {
				if (MAP[x][y])cout << "#";
				else cout << ".";
			}
			cout << endl;
		}
	}
	else {
		for (ll b = 1,x = 1; b <= B; x += 2) {
			for (ll y = 0; y < 100 && b <= B; b++, y += 2) {
				MAP[x][y] = 1;
			}
		}
		cout << "100 100" << endl;
		for (ll y = 0; y < 100; y++) {
			for (ll x = 0; x < 100; x++) {
				if (MAP[x][y])cout << "#";
				else cout << ".";
			}
			cout << endl;
		}
	}
	return 0;
}


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

int main(){
  double n,m,d,ans;
  cin >> n >> m >> d;
  ans=0;
  if(d != 0){
    for(int i=0;i<m-1;i++){
      ans+=(2*(n-d))/(n*n);
    }
  }
  else{
   for(int i=0;i<m-1;i++){
      ans+=(n-d)/(n*n);
    }
  }
  cout<<fixed<<setprecision(20)<<ans<<endl;

  return 0;
}

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
#define mp make_pair
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};

int main() {
    ll n,m,d;
    cin >> n >> m >> d;
    cout << fixed << setprecision(20);
    if(d!=0){
        cout << (ld)(n-d)*2*(m-1)/(n*n) << endl;
    }
    else{
        cout << (ld)(n-d)*(m-1)/(n*n) << endl;
    }
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){

  int N,M;
  cin>>N;
  vector<string>S(N);
  for(int i=0;i<=N-1;i++){

    cin>>S[i];
  }
  cin>>M;
  vector<string>T(M);
  for(int i=0;i<=M-1;i++){

    cin>>T[i];
  }
  int countplus=0;
  int countminus=0;
  int ans=0;
  for(int i=0;i<=N-1;i++){

    countplus=0;
    for(int j=0;j<=N-1;j++){

      if(S[i]==S[j])countplus++;
    }
    countminus=0;
    for(int j=0;j<=M-1;j++){

      if(S[i]==T[j])countminus++;
    }
    ans=max(ans,countplus-countminus);
  }
  cout<<ans;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<double,double> pdd;
#define SQ(i) ((i)*(i))
#define MEM(a, b) memset(a, (b), sizeof(a))
#define SZ(i) int(i.size())
#define FOR(i, j, k, in) for (int i=j ; i<(k) ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=(k) ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define REP1(i,j) FOR(i, 1, j+1, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define ALL(_a) _a.begin(),_a.end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define X first
#define Y second
#ifdef jayinnn
#define TIME(i) Timer i(#i)
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,deque<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
class Timer {
private:
    string scope_name;
    chrono::high_resolution_clock::time_point start_time;
public:
    Timer (string name) : scope_name(name) {
        start_time = chrono::high_resolution_clock::now();
    }
    ~Timer () {
        auto stop_time = chrono::high_resolution_clock::now();
        auto length = chrono::duration_cast<chrono::microseconds>(stop_time - start_time).count();
        double mlength = double(length) * 0.001;
        debug(scope_name, mlength);
    }
};
#else
#define TIME(i)
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
#endif

const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int iNF = 0x3f3f3f3f;
const ll MAXN = 100005;

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

/********** Good Luck :) **********/
int main () {
    TIME(main);
    IOS();
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin >> a[i];
    ll ans = 0;
    for(int i=1;i<n;i++){
        if(a[i] < a[i-1]){
            ans += a[i-1] - a[i];
            a[i] = a[i-1];
        }
    }

    cout << ans << endl;

    return 0;
}


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

using ll = long long;

class Stream{
public:
    Stream(){

    }
    //配列以外の入力を受け取る
    void read(){

    }
    template<typename First, typename... Rest>
    void read(First& first, Rest&... rest){
        cin >> first;
        read(rest...);
    }
    //配列を区切って出力する
    template<typename T>
    void write_vec(vector<T>& v, char divider){
        for(size_t i = 0; i < v.size(); i++){
            cout << v[i] << divider;
        }
    }
};

int main(){
    Stream io;

    int R, G;
    io.read(R, G);
    cout << G + G - R;
    return 0;
}

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

int main()
{
long r,g,b; cin>>r>>g;
b=(g*2)-r;
cout<<b;
}


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

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

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll N, M;
    cin >> N;
    map<string, ll> n, m;
    set<string> S;
    ll maxi = 0;
    rep(i, N) {
        string temp;
        cin >> temp;
        n[temp]++;
        S.insert(temp);
    }
    cin >> M;
    rep(i, M) {
        string temp;
        cin >> temp;
        m[temp]++;
        S.insert(temp);
    }
    iter(s, S) maxi = max(maxi, n[s] - m[s]);
    cout << maxi << endl;
    return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<tuple>
#include<map>
#include<bitset>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;

int main(){

  int N, M;
  cin >> N;
  vector<string> vec1(N);
  for (int i=0; i<N; i++){
    cin >> vec1.at(i);
  }
  cin >> M;
  vector<string> vec2(M);
  for (int i=0; i<M; i++){
    cin >> vec2.at(i);
  }

  int max=0;
  for (int i=0; i<N; i++){
    int sum=0;
    for (int j=0; j<N; j++){
      if (vec1.at(i)==vec1.at(j)){
        sum++;
      }
    }
    for (int k=0; k<M; k++){
      if (vec1.at(i)==vec2.at(k)){
        sum--;
      }
    }
    if (sum>max){
      swap(sum, max);
    }
  }

  cout << max << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long LL;
typedef long double ld;
typedef vector<ll>VI;
typedef pair<int,int>P;
#define VV(T) vector<vector<T>>
#define sz(x) int(x.size())
#define PI 3.1415926535897932384626433832795
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define reprev(i,n) for (ll i=(ll)n-1;i>=0;i--)
#define ALL(a)  (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define c_max(a, b) (((ll)a)>((ll)b)?(a):(b))
#define c_min(a,b) (((ll)a)<((ll)b)?(a):(b))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define $(x) {cout<<#x<<" = " <<(x)<<endl;}
#define fi first
#define se second
#define MAX 100100//5
#define MAX2 200100
#define MAX6 1001001//6
#define MAX7 10010010//7
#define SENTINEL 2000000000//9
#define INF 1<<30
#define INFTY 1LL<<60
#define MAX_INT INT_MAX
#define Endl '\n'
#define CLR(mat) memset(mat, 0, sizeof(mat))
inline ll GCD(ll a,ll b){return b?GCD(b,a%b):a;}//最大公約数
inline ll lcm(ll a,ll b){return a*b/GCD(a,b);}//最小公倍数
template<class T> bool contain(const std::string& s, const T& v) {
   return s.find(v) != std::string::npos;
}
const int MOD = 1000000007;

inline ll mod_add(ll a,ll b){return (a+b)%MOD;}
inline ll mod_sub(ll a,ll b){return (a+MOD-b)%MOD;}
inline ll mod_mul(ll a,ll b){return ((a%MOD)*(b%MOD))%MOD;}
inline ll mod_pow(ll x,ll n,ll mod=(ll)MOD){ll res=1;while(n>0){
if(n&1)res=res*x%mod;
x=x*x%mod;n>>=1;}return res;}
inline ll mod_inv(ll x){return mod_pow(x,MOD-2);}
inline ll mod_div(ll a, ll b){return mod_mul(a,mod_inv(b));}
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
// vector型から重複を削除 list.erase(unique(ALL(list)),list.end());
// g++ -o a a.cpp -Wall -lm -std=c++17 -I .
//push_back -> emplace_back

string solve(ll n){
   string str="";
   while(n!=0){
      int x=n/(-2);
      int y=n%(-2);
      if(y<0){
         y+=2;
         x++;
      }
      str+=char(y+'0');
      n=x;
   }
   if(str=="")str="0";
   reverse(ALL(str));
   return str;

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

#include <cstdio>
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#define rep(i,n) for (int i=0;i<n;i++)
using ll = long long;
using namespace std;

void solve(){

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

    ll n;
    cin >> n;

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

    vector<vector<ll>> a(33,vector<ll> (2));
    a[0][0] = 1;
    a[0][1] = 0;

    ll m = 1;

    for(int i=1;i<33;i++){
        m*=-2;
        if(m>0){
            a[i][0] = a[i-1][0] + m;
            a[i][1] = a[i-1][1];
        }else{
            a[i][1] = a[i-1][1] + m;
            a[i][0] = a[i-1][0];
        }
    }
    ll tmp = n;
    vector<int> ans(33,0);
    for(int i=32;i>0;i--){
        if(tmp>0){
            if(a[i][0]>=tmp&&tmp>a[i-1][0]){
                ans[i] = 1;
                tmp = tmp - m;
            }else{
                ans[i] = 0;
            }
        }else{
            if(a[i][1]<=tmp&&tmp<a[i-1][1]){
                ans[i] = 1;
                tmp = tmp - m;
            }else{
                ans[i] = 0;
            }
        }
        m /= (-2);
    }
    if(tmp == 1){
        ans[0] = 1;
    }else{
        ans[0] = 0;
    }

    bool flag = false;

    for(int i=32; i >= 0; i--){
        if(!flag){
            if(ans[i]==1){
                cout << ans[i];
                flag = true;
            }
        }else{
            cout << ans[i];
        }
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <iomanip>
#include <set>
#include <sstream>
#include <queue>
using namespace std;
typedef long long ll;

int main() {
  ll n, a, b, c, d, e;
  cin >> n >> a >> b >> c >> d >> e;
  ll min_move = min({a,b,c,d,e});
  cout << (n + min_move - 1) / min_move + 4 << endl;
  return 0;
}

#include<iostream>

using namespace std;

void swap(int *a,int *b){
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

int main(void){
  int a,b,c;
  cin >> a >> b >> c;
  swap(&a,&b);
  swap(&a,&c);
  cout << a <<" "<< b << " " << c << endl;
  return 0;
}


/**
*	created: 24.08.2020 21:32:00
**/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
using ll = long long;
using P = pair<int,int>;
// #define endl '\n'
#define int long long
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rep2(i,s,n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
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 long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

signed main() {

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

    int n; cin >> n;
    vector<int> v;
    v.push_back(2);
    rep2(i,3,55556) {
        bool ok = true;
        rep2(j,2,sqrt(i)+1) if (i % j == 0) ok = false;
        if (ok) v.push_back(i);
    }
    vector<int> v1;
    for (auto p : v) if (p % 5 == 1) v1.push_back(p);
    rep(i,n) {
        if (i == 0) cout << v1[i];
        else cout << " " << v1[i];
    }
    cout << endl;
    return 0;
}

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

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

  cout << a + pow( a, 2 ) + pow( a, 3 ) << endl;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>
#include <deque>
#include <set>
#include <climits>
#include <memory>
#include <numeric>
#include <utility>

#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define fore(i,a) for(auto &i:a)
#define INF INT_MAX/2;
#define PI 3.14159265358979323846264338327950L;
const int INF2 = 1001001001;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
//ll llmax = 10000000000000000000ll;
const ll LINF = 1e18;
using namespace std;
using Graph = vector<vector<int>>;

int cnt_digit(ll N)
{
	int digit = 0;

	while (N > 0)
	{
		N /= 10;
		digit++;
	}
	return digit;
}

 // 最大公約数計算

ll gcd(ll a, ll b)
{
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
// 最小公倍数の計算

ll lcm(ll a, ll b)
{
	ll g = gcd(a, b);

	return a / g * b;
}

struct union_find
{
	vector<int> par, r;
	union_find(int n)
	{
		par.resize(n);
		r.resize(n);
		init(n);
	}

	void init(int n)
	{
		for (int i = 0; i < n; i++) par[i] = i;
		for (int i = 0; i < n; i++) r[i] = 0;
	}

	int find(int x)
	{
		if (par[x] == x)return x;
		else return find(par[x]);
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (r[x] < r[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (r[x] == r[y])
			{
				r[x]++;
			}
		}
	}
};

const int MAX_N = 110000;

int memo[MAX_N];

int rec(int n) {
	if (n == 0)return 0;
	if (memo[n] != -1)return memo[n];

	int res = n;

	for (int pow6 = 1; pow6 <= n; pow6 *= 6)res = min(res, rec(n - pow6) + 1);

	for (int pow9 = 1; pow9 <= n; pow9 *= 9)res = min(res, rec(n - pow9) + 1);

	return memo[n] = res;
}

int calc(int x) {
	int ret = 0;
	while (x % 2 == 0) {
		x /= 2;
		ret++;
	}

	return ret;
}

int a[12][12];
int D;
ll G;
vector<ll> P, C;

//aのp乗を求めるアルゴリズム
long modpow(long a, int p) {
	if (p == 0)return 1;
	if (p % 2 == 0) {
		//pが偶数のとき
		int halfP = p / 2;
		long half = modpow(a, halfP);
		return half * half % mod;
	}
	else {
		//pが奇数の時は、
		//pを偶数にするために1減らす
		return a * modpow(a, p - 1) % mod;
	}

}

//(10*9*8)/(3*2*1);
//10*9*8 -> ansMul
//3*2*1 -> ansDiv;
long calcComb(int a, int b) {
	if (b > a - b) return calcComb(a, a - b);
	long ansMul = 1;
	long ansDiv = 1;
	rep(i, 0, b) {
		ansMul *= (a - i);
		ansDiv *= (i + 1);
		ansMul %= mod;
		ansDiv %= mod;
	}
	//ansMul /ansDivをやりたい
	//ansDivの逆元を使って求めよう。

	long ans = ansMul * modpow(ansDiv, mod - 2) % mod;
	return ans;

}

bool is_war(const vector<int>& x, const vector<int>& y) {
	for (int Z = -100; Z <= 100; Z++) {
		bool is_ok = true;
		rep(i, 0, x.size()) {
			if (x[i] >= Z) {
				is_ok = false;
			}
			rep(i, 0, y.size()) {
				if (y[i] < Z) {
					is_ok = false;
				}
			}

		}
		if (is_ok)return false;

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

	int n, m;
	cin >> n >> m;
	vector<int> x(n + 1),y(m+1);
	cin >> x[0] >> y[0];

	rep(i, 0, n) {
		cin >> x[i + 1];
	}
	rep(i, 0, m) {
		cin >> y[i + 1];
	}
	if (is_war(x, y)) {
		cout << "War" << endl;
	}
	else {
		cout << "No War" << endl;
	}

}

#include<bits/stdc++.h>
#define INF 10000000000ll
#define ll long long
using namespace std;
signed main(){
  ll n,m,x,y,a,b;
  cin>>n>>m>>x>>y;
  for(int i=0;i<n;i++){
    cin>>a;
    x = max(a,x);
  }
  for(int i=0;i<m;i++){
    cin>>b;
    y = min(y,b);
  }
  if(y > x){
    cout<<"No War\n";
  }else{
    cout<<"War\n";
  }
  return 0;
}


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

using namespace std;

long long inf=1000000007;

int main(){
	int x,y;
	cin>>x>>y;

	vector<long long> score={300000,200000,100000,0};

	long long ans=0;

	ans+=score.at(min(3,x-1));
	ans+=score.at(min(3,y-1));

	if(x==1 && y==1) ans+=400000;

	cout<<ans<<endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;
int num[maxn], x[maxn];

int main()
{
    int n, s;
    scanf("%d%d", &n, &s);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d%d",x+i,num+i);
    }
    int l=1,r=n;
    long long ans=0;
    long long sum=0;
    int nows;
    bool flag;
    if(num[1]>=num[n]){
        nows=x[n];sum+=num[r];
        flag=1;
    }
    else{
        nows=x[1];sum+=num[l];
        flag=0;
    }
    while(s<x[r]&&s>x[l]){
        //printf("%d %d %lld %lld\n",l,r,ans,sum);
        if(flag){
            if(num[l]>=sum){
                ans+=abs(nows-x[l]);
                flag=0;
                nows=x[l];
                sum+=num[l];
                r--;
            }
            else
                sum+=num[l++];
        }
        else{
            if(num[r]>sum){
                ans+=abs(x[r]-nows);
                flag=1;
                nows=x[r];
                sum+=num[r];
                l++;
            }
            else
                sum+=num[r--];
        }
    }
    //printf("%d %d %lld %lld\n",l,r,ans,sum);
    if(s>x[r]){
        ans+=abs(nows-x[l]) + abs(nows-s);
    }
    else ans+=abs(nows-s) + abs(nows-x[r]);
    printf("%lld\n",ans);
    return 0;
}


#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class T>
vector<int> kmp(const T &sentence, const T &word) {
    int lenS = (int)sentence.size(), lenW = (int)word.size(), idx = -1, head = 0;
    vector<int> table(lenW + 1, -1), res;
    for (int i = 0; i < lenW; ++i) {
        while (idx >= 0 && word[i] != word[idx])idx = table[idx];
        table[i + 1] = ++idx;
    }

    idx = 0;
    while (head + idx < lenS) {
        if (word[idx] == sentence[head + idx]) {
            ++idx;
            if (idx == lenW) {
                res.push_back(head);
                head = head + idx - table[idx];
                idx = table[idx];
            }
        } else {
            head = head + idx - table[idx];
            if (idx > 0)idx = table[idx];
        }
    }
    return res;
}

void solve() {
    int N;
    cin >> N;
    vi A(N), B(N), AA(N), BB(2 * N);
    rep(i, N) {
        cin >> A[i];
    }
    rep(i, N) {
        cin >> B[i];
    }
    rep(i, N) {
        AA[i] = A[i] ^ A[(i + 1) % N];
        BB[i] = B[i] ^ B[(i + 1) % N];
        BB[i + N] = BB[i];
    }
    BB.pop_back();

    vi found = kmp(BB, AA);
    each(i, found) {
        i = (i == 0 ? i : N - i);
    }
    sort(all(found));

    each(i, found) {
        cout << i << ' ' << (A[i] ^ B[0]) << endl;
    }
}

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

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

#define MOD 1000000007

using namespace std;

typedef long long ll;

typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<string> vs;

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

typedef vector<pll> vpl;
typedef vector<pi> vpi;
typedef vector<vs> vvs;

typedef vector<pll> vvpl;
typedef vector<vpi> vvpi;

typedef queue<ll> ql;
typedef queue<int> qi;
typedef queue<pll> qpl;
typedef queue<pi> qpi;

template <typename T> void printv(const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    printf("[%d] %d\n", i, v[i]);
  }
}

template <typename T> void printvv(const vector<T> &vv) {
  for (int i = 0; i < vv.size(); ++i) {
    for (int j = 0; j < vv[i].size(); ++j) {
      printf("[%d][%d] %d\n", i, j, vv[i][j]);
    }
  }
}

template <typename T, typename U> void printp(const pair<T, U> &p) {
  cout << p.first << " " << p.second << endl;
}

template <typename T> void printvp(const vector<pair<T, T>> &vp) {
  for (int i = 0; i < vp.size(); ++i) {
    printf("[%d] %d, %d\n", vp[i].first, vp[i].second);
  }
}

template <typename T> void printvvp(const vector<vector<pair<T, T>>> &vvp) {
  for (int i = 0; i < vvp.size(); ++i) {
    for (int j = 0; j < vvp[i].size(); ++j) {
      printf("[%d][%d] %d, %d\n", i, j, vvp[i][j].first, vvp[i][j].second);
    }
  }
}

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

  ll tmp = 0;
  for (int bit = 0; bit < (1 << (S.length() - 1)); ++bit) {
    tmp = S[0] - '0';
    for (int i = 1; i < S.length(); ++i) {
      if (bit & 1 << (i - 1)) {
        tmp += S[i] - '0';
      } else {
        tmp -= S[i] - '0';
      }
    }
    if (tmp == 7) {
      for (int i = 0; i < S.length(); ++i) {
        cout << S[i];
        if (i < S.length() - 1) {
          char op = (bit & (1 << i)) >= 1 ? '+' : '-';
          cout << op;
        }
      }
      cout << "=7" << endl;
      break;
    }
  }
  return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdlib>
#include<set>
#include<vector>
#include<sstream>
#include<queue>
#include <iomanip>
#include<unordered_set>
#include <stack>
#include <unordered_map>
#include <map>
#include<numeric>
#include<chrono>
#include <fstream>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i+=1)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i-=1)
#define endl '\n'
#define N 1000000007 // prime modulo value
#define M 998244353
#define all(x) x.begin(),x.end()
#define mkp(a,b) make_pair(a,b)
using namespace std;
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);
	}
};
inline int mex(unordered_set<int> st)
{
	int m = 0;
	while (st.find(m) != st.end())
	{
		m += 1;
	}
	return m;
}
inline int calculateGrundy(int n)
{
	if (n == 1)
		return 0;
	unordered_set<int> mexi;
	for (int i = 1; i <= sqrt(n); i += 1)
	{
		if (n % i == 0)
		{
			if (n / i != n)
				mexi.insert(calculateGrundy(n / i));
			mexi.insert(calculateGrundy(i));
		}
	}
	return mex(mexi);
}
inline ll gcd(ll a, ll b)
{
	// This is code taken from geeksforgeek
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
/*ll power(ll x, ll y)
{
	// This is code taken from geeksforgeek
	ll res = 1;

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

	x = x;

	while (y > 0)
	{

		if (y & 1)
			res = (res * x);

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

/*ll modInverse(ll n, ll p)
{
	return power(n, p - 2, p);
}*/

ll sum(ll n)
{
	return (n * (n + 1)) / 2;
}
ll sum_digit(ll n)
{
	ll ans = 0;
	while (n)
	{
		ans += n % 10;
		n /= 10;
	}
	return ans;
}
ll gp(ll r, ll n)
{
	return (pow(r,n)-1)/(r-1);
}
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll d,t,s;
	cin>>d>>t>>s;
	ll time=(d-s*t);
	if(time<=0)
	{
		cout<<"Yes";
	}
	else
	{
		cout<<"No";
	}

	return 0;
}

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    int n,d,v;
    cin >> n >> d >> v;
    double time = (double)n/v;
    if(time <= d) cout << "Yes" << endl;
    else cout << "No" << endl;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <iomanip>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll,ll>>>;
#define rep(i,n) for (int i=0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N){
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    return A;
}
void OutputVec(vector<ll> A){
    for (int i = 0; i < A.size(); ++i) {
        cout << A[i] << ",";
    }
    cout << endl;
}
vector<vector<ll>> InputVec2d(ll H,ll W){
    vector<vector<ll>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<ll>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        for (int xi = 0; xi < W; ++xi) {
            cin >> A[yi][xi];
        }
    }
    return A;
}
vector<vector<char>> InputVec2dStr(ll H,ll W){
    vector<vector<char>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<char>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        string S;
        cin >> S;
        for (int xi = 0; xi < W; ++xi) {
            A[yi][xi] = S[xi];
        }
    }
    return A;
}
void OutputVec2d(vector<vector<ll>> A){
    for (int yi = 0; yi < A.size(); ++yi) {
        for (int xi = 0; xi < A[yi].size(); ++xi) {
            cout << A[yi][xi] << ",";
        }
        cout << endl;
    }
}
graph InputGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    return G;
}
graph InputDGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    return G;
}
void OutputGraph(graph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j]<< ",";
        }
        cout << endl;
    }
}
void OutputAnswerBool(bool b,bool isUpperCase = false){
    if(b){
        if(isUpperCase){
            cout << "YES" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }else{
        if(isUpperCase){
            cout << "NO" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}

int main() {
    string S;
    cin >> S;
    ll w;
    cin >> w;
    for (int i = 0; i < S.size(); i += w) {
        cout << S[i];
    }
    cout << endl;
}

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

int main() {
string S;
int w;
cin>>S>>w;

int i=0;
while(i<S.size()){
  cout<<S[i];
  i+=w;
}
cout<<endl;
}


#include <iostream>
#include <string>

using namespace std;

long long solve(const string& S, int k){
    int dst = 0;
    long long res = 0;
    long long cnt = 0;
    int m = 0;
    int c = 0;
    for(int i=0;i<S.size();i++){
        while(dst < S.size() && dst - i < k){
            if(S[dst] == 'M') ++m;
            if(S[dst] == 'C'){ ++c; cnt += m; }
            ++dst;
        }
        if(S[i] == 'D') res += cnt;
        if(S[i] == 'M') { cnt -= c; --m; }
        if(S[i] == 'C') --c;
    }
    return res;
}

int main(){
    int N; cin >> N;
    string S; cin >> S;
    int Q; cin >> Q;
    for(int i=0;i<Q;i++){
        int k; cin >> k;
        cout << solve(S, k) << endl;
    }
}

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

int main() {
    int N, Q; cin >> N >> Q;
    atcoder::fenwick_tree<ll> bit(N+1);
    for (int i=1;i<=N;i++) {
        int a; cin >> a;
        bit.add(i, a);
    }
    for (int q=0; q<Q; q++) {
        int d, l, r;
		cin >> d >> l >> r;
        if (d == 0) {
            bit.add(l+1, r);
        } else {
            cout << bit.sum(l+1, r+1) << "\n";
        }
    }
}


#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <map>
#define INF 100000000
#define rep(i,a) for(int i=0;i<(a);i++)

using namespace std;
typedef long long ll;

int main(){
    int hosei[]={0,4,0,1,4,6,2,4,0,3,5,1,3};
    string day[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int m,n;
    while(1){
        cin>>m>>n;
        if(!(m||n))break;
        cout<<day[(hosei[m]+n%7-1)%7]<<endl;
    }

    return 0;
}

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main(int argc, char const* argv[])
{
	int n,m;
	int place[1000];

	while( cin >> n >> m && n != 0 ){
		for( int i = 0;i < 1000;i++ ) place[i] = 0;

		for( int i = 0;i < n;i++ ) cin >> place[i];
		sort( place, place + n, greater<int>() );

		for( int i = 1;i * m - 1 < n;i++ ) place[i * m - 1] = 0;

		int answer = 0;
		for( int i = 0;i < n;i++ ) answer += place[i];

		cout << answer << endl;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main(){
	int h,w,s;
	while(true){
	cin>>h>>w;
	if(h==0&&w==0)
		break;
	bool t=true;
	s=h*h+w*w;
	h++;
	while(true){
		if(h*h>=s-h*h)
			break;
		if(s==h*h+(int)sqrt((double)s-h*h+0.0)*(int)sqrt((double)s-h*h+0.0)){
			t=false;
			break;
		}
		h++;
	}
	s++;
	if(t==true){
	while(true){
		h=1;
		while(true){
		if(h*h>=s-h*h)
			break;
		if(s==h*h+((int)sqrt((double)s-h*h+0.0)*(int)sqrt((double)s-h*h+0.0))){
			t=false;
			break;
		}
		h++;
		}
		if(t==false)
			break;
		s++;
	}
	}
	cout<<h<<" "<<(int)sqrt((double)s-h*h+0.0)<<endl;
	}
	return 0;
}

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
struct rect{
  int w,h,v;
  bool operator<(const rect& a) const{
    return v==a.v ? h < a.h : v < a.v;
  }
};
int main(){
  int i,j,k,w,h;
  vector <rect> rs;
  rect r;
  for(i=1;i<151;i++){
    for(j=1;j<i;j++){
      r.w=i;
      r.h=j;
      r.v=i*i+j*j;
      rs.push_back(r);
    }
  }
  sort(rs.begin(),rs.end());
  cin >> h >> w;
  while(h!=0){
    for(i=0;i<rs.size();i++){
      if(rs[i].w==w&&rs[i].h==h) cout << rs[i+1].h << " " << rs[i+1].w << endl;
    }
    cin >> h >> w;
  }
  return 0;
}


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

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

# define int long long

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

# define S second
# define F first
# define kill(x) return(cout << x << '\n', 0LL)
# define debug(x) cerr<< #x << " = " << (x) << endl
# define ddebug(x, y) cerr<< #x << " = " << (x)  << ", " << #y << " = " << (y) << endl
# define tdebug(x, y, z) cerr<< #x << " = " << (x)  << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << endl
# define bin(x) cerr<< #x << " : " << bitset<4>(x) << endl
# define sz(s) (int)s.size()
# define sq(x) (x) * (x)
# define PB(x) push_back(x)
# define smax(x, y) (x) = max((x), (y))
# define smin(x, y) (x) = min((x), (y))
# define all(x) x.begin(), x.end()
# define SP fixed << setprecision(10)
# define uop(x, y) pii(min(x, y), max(x, y))
# define MP(x, y) make_pair(x, y)

const int MAXN = 300 + 10;
int dp[MAXN][MAXN][MAXN];

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

	string s;
	int k;
	cin >> s >> k;

	for (int t = 1; t <= sz(s); t ++)
	{
		for (int i = 0; i + t - 1 < sz(s); i ++)
		{
			int j = i + t - 1;
			for (int l = 0; l <= k; l ++)
			{
				if (t == 1)
				{
					dp[i][j][l] = 1;
					continue;
				}

				dp[i][j][l] = max(dp[i + 1][j][l], dp[i][j - 1][l]);
				if (l || s[i] == s[j])
					smax(dp[i][j][l], dp[i + 1][j - 1][l - (s[i] != s[j])] + 2);
			}
		}
	}
	cout << dp[0][sz(s) - 1][k] << '\n';

	return 0;
}

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

int main() {
  int a,b,ans;
  cin >> a >> b;
  if(a > b){
    ans = a;
    a--;
  }else{
	ans = b;
    b--;
  }
  if(a > b){
    ans += a;
    a--;
  }else{
	ans += b;
    b--;
  }
  cout <<ans;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll a[114514],b[114514];
int main(){
	ll n,inf=1e15;
	cin>>n;
	b[n+1]=inf;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=-inf;
	}
	ll ans=1;
	b[1]=a[1];
	b[0]=inf;
	b[n+1]=-inf;
	for(int i=2;i<=n;i++){
		if(b[ans]>=a[i]){
			ans++;
			b[ans]=a[i];
		}else{
			ll ng=0,ok=n+1;
			while(ok-ng>1){
				ll mid=(ok+ng)/2;
				if(b[mid]>=a[i])ng=mid;
				else ok=mid;
			}
			b[ok]=a[i];
		}
	}
	//for(int i=0;i<=5;i++)cout<<b[i]<<' ';
	cout<<ans;
	return 0;
}

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

#define vec              vector<int>
#define vecll            vector<ll>
#define vecd             vector<double>
#define vecst            vector<string>
#define vecb             vector<bool>
#define v2(v,n,m,init)   vector<vector<int>>  v(n, vector<int>(m, init))
#define vb2(v,n,m,init)  vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v,n,m,init) vector<vector<ll>>   v(n, vector<ll>(m, init))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2)     cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
template <typename T>
bool chmax(T &a, const T& b){if(a<b){a=b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T& b){if(a>b){a=b; return true;} return false;}
//----------------------------------------------------------------

int main(void) {
  int n;
  cin >> n;
  arrll(a,n);
  rep(i,n){
    a[i] = 1000000000-a[i];
  }
  ll INF = 1e18;
  vecll dp(n, INF);

  rep(i,n){
    auto tmp = upper_bound(all(dp), a[i]);
    *tmp = a[i];
  }

  auto ans = lower_bound(all(dp), ll(1e18)-1);
  prt(ans-dp.begin());
}


#include<bits/stdc++.h>
#define ll long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
int a[55];

int main()
{
    fastio();
    int n,k;
    cin>>n>>k;
    rep(i,1,n) cin>>a[i];
    int ans = 0;
    rep(i,0,min(k,n)){
        rep(L,0,i){
            int R = i - L;
            multiset<int> st;
            rep(j,1,L) st.insert(a[j]);
            rep(j,1,R) st.insert(a[n-j+1]);
            rep(j,1,k-i){
                if(st.size()==0) break;
                auto it = st.begin();
                int v = *it;
                if(v >= 0) break;
                st.erase(it);
            }
            int tmp = 0;
            for(int x:st) tmp+=x;
            ans = max(ans,tmp);
        }
    }
    cout<<ans<<endl;
    return 0;
}

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

const int N = 50 + 5, mod = 1e9 + 7;
int n, k;
int a[N], dp[N][N][102];

int solve(int l, int r, int left)
{
    if (l > r)
        return 0;
    if (dp[l][r][left] != -1)
        return dp[l][r][left];
    int ans = 0;
    if (a[l] < 0)
    {
        if (left > 0)
            ans = max(ans, solve(l + 1, r, left - 1) + a[l]);
        if (left > 1)
            ans = max(ans, solve(l + 1, r, left - 2));
    }
    if (a[l] >= 0 && left > 0)
        ans = max(ans, solve(l + 1, r, left - 1) + a[l]);
    if (a[r] < 0)
    {
        if (left > 0)
            ans = max(ans, solve(l, r - 1, left - 1) + a[r]);
        if (left > 1)
            ans = max(ans, solve(l, r - 1, left - 2));
    }
    if (a[r] >= 0 && left > 0)
        ans = max(ans, solve(l, r - 1, left - 1) + a[r]);
    return dp[l][r][left] = ans;
}

int main()
{
    //ios::sync_with_stdio(false);cin.tie(nullptr);ios_base::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    memset(dp, -1, sizeof dp);
    cout << solve(0, n - 1, k);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define sp(x) fixed << setprecision(x)
#define ordered_set tree<int , null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

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

const int inf = int(1e9);
const int mod = inf + 7;
const int N = 1e6 + 555; // TL MB
const double PI = acos(-1.0);
void mul(ll &x, ll y){
	x *= y;
	if(x >= mod) x %= mod;
}

void add(ll &x, ll y){
	x += y;
	if(x >= mod) x -= mod;
	if(x < 0) x += mod;
}
ll bp(ll a, ll n){
	ll r = 1;
	while(n){
		if(n & 1) mul(r, a);
		mul(a, a);
		n >>= 1;
	}

}

void solve()
{
	int n;
	cin >> n;
	ll a[n];
	vector<ll> p;
	rep(i, n) cin >> a[i];
	sort(a, a + n);
	rep(i, n){
		if(i && a[i] == a[i - 1]) p.pb(a[i++]);
	}
	sort(p.rbegin(), p.rend());
	if(sz(p) <= 1) cout << 0;
	else cout << p[0] * p[1];
}

int main()
{
	// freopen("crosses.in", "r", stdin);
	// freopen("crosses.out", "w", stdout);
    Red;
  	int T = 1;
  	// cin >> T;
  	for(int i = 1; i <= T; ++i){
  		solve();
  	}
  	return 0;

}

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

int main(){

    int N;
    string A,B,C;
    cin >> N;
    cin >> A >> B >> C;
    int ans = 0;
    for(int i = 0; i < N;i++){

        if(A[i] == B[i] && B[i] != C[i] && C[i] != A[i])ans++;
        else if(A[i] != B[i] && B[i] == C[i] && C[i] != A[i])ans++;
        else if(A[i] != B[i] && B[i] != C[i] && C[i] == A[i])ans++;
        else if(A[i] != B[i] && B[i] != C[i] && C[i] != A[i])ans+=2;
    }
    cout << ans << endl;

}


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

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

    vector<vector<char>> ans;
    for (int i = 0; i < 50; i++) ans.push_back(vector<char>(100, '#'));
    for (int i = 0; i < 50; i++) ans.push_back(vector<char>(100, '.'));

    a--, b--;
    for (int i = 0; a > 0; i += 2) {
        for (int j = 0; j < 100 && a > 0; j += 2, a--) {
            ans[i][j] = '.';
        }
    }
    for (int i = 51; b > 0; i += 2) {
        for (int j = 0; j < 100 && b > 0; j += 2, b--) {
            ans[i][j] = '#';
        }
    }

    cout << 100 << " " << 100 << endl;
    for (auto e : ans) cout << string(begin(e), end(e)) << endl;
}


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

using namespace __gnu_pbds;
using namespace std;

#ifdef LOCAL
	#define debug(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
	#define debug(...) 42
#endif

#define sf scanf
#define pf printf
#define nl printf("\n")
#define ps(s) printf("%s",s.c_str())
#define mem(x,val) memset(x,val,sizeof x)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) (int)(x).size()
#define test_case int ___t;scanf("%d", &___t);for(int cs=1;cs<=___t;cs++)

inline string getString(int n){char ch[n+5];sf(" %s",ch);return string(ch);}
inline string getLine(int n)
{char ch[n+5];fgets(ch,n+5,stdin);string s=string(ch);s.pop_back();return s;}

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;
typedef long long ll;

const int S=100100;
int main()
{
	ll a,b;
	while(~sf("%lld %lld",&a,&b))
	{
		ll g=__gcd(a,b);

		int cnt=0;
		if(g%2==0)
		{
			cnt++;
			while(g%2==0)g/=2;
		}

		for(int i=3;i<=sqrt(g);i++)
		{
			if(g%i==0)
			{
				cnt++;
				while(g%i==0)g/=i;
			}
		}
		if(g>1)cnt++;
		pf("%d\n",cnt+1);
	}
    return 0;
}

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

long long mod = 1000000007;

class UnionFind
{
public:
   vector<int> parent;
   UnionFind(int N)
   {
      for (int i = 0; i < N; i++)
      {
         parent.push_back(-1);
      }
   }
   int root(int A)
   {
      if (parent[A] < 0)
         return A;
      return parent[A] = root(parent[A]);
   }
   int size(int A)
   {
      return -parent[root(A)];
   }

   bool unite(int A, int B)
   {
      A = root(A);
      B = root(B);
      if (A == B)
         return false;
      if (size(A) < size(B))
         swap(A, B);
      parent[A] += parent[B];
      parent[B] = A;
      return true;
   }
};
vector<pair<long long, long long>> prime(long long N)
{
   vector<pair<long long, long long>> res;
   for (long long i = 2; i * i <= N; i++)
   {
      if (N % i != 0)
         continue;
      long long ex = 0;
      while (N % i == 0)
      {
         ex++;
         N = N / i;
      }
      res.push_back({i, ex});
   }
   if (N != 1)
      res.push_back({N, 1});
   return res;
}
int main(void)
{
   long long A, B;
   cin >> A >> B;
   vector<pair<long long, long long>> PA = prime(A);
   vector<pair<long long, long long>> PB = prime(B);
   map<long long, long long> result;
   for (int i = 0; i < PA.size(); i++)
   {
      result[PA[i].first]++;
   }
   for (int i = 0; i < PB.size(); i++)
   {
      result[PB[i].first]++;
   }
   long long count = 1;
   for (auto ans : result)
   {
      if (ans.second == 2)
         count++;
   }
   cout << count << endl;
   return 0;
}