#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

const int MOD=1e9+7;
int D[1<<20], Dsum[1<<20], Dminus[1<<20], PS[500001];

long long mod(long long a)
{
	if(a>=0) return a%MOD;
	return ((-a/MOD+1)*MOD+a)%MOD;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, PX=0, A;
	cin>>N; PS[0]=1;
	for(int i=1;i<=N;i++) {
		cin>>A;
		if(PX^=A) {
			D[PX]=mod(1+1LL*Dsum[PX]*PS[i-1]-1LL*Dminus[PX]);
			Dsum[PX]=(Dsum[PX]+D[PX])%MOD;
			Dminus[PX]=(Dminus[PX]+1LL*D[PX]*PS[i-1])%MOD;
		}
		else PS[i]=1;
		PS[i]+=PS[i-1];
	}
	if(PX) cout<<D[PX]<<'\n';
	else {
		int ans=1;
		for(int i=2;i<PS[N];i++) ans=(2*ans)%MOD;
		for(int i=(1<<20)-1;i;i--) ans=(ans+Dsum[i])%MOD;
		cout<<ans<<'\n';
	}
	return 0;
}

    /**   Created by: Marcos Portales
          Codeforces User: marcosportales
          Country: Cuba
          Copyright                    */
#include<bits/stdc++.h>
using namespace std;
/// Pragmas:
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
#define vi vector<int>
#define sz(a) int((a).size())
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,n) for(int i=(1);i<=(n);i++)
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 200005;
const int mod = 1000000007;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 2000000000000000000ll;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Test.insert(x)late ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
    if( !e ) return 1;
    if( e & 1 ) return qpow(b,e-1) * b % mod;
    int pwur = qpow(b,e>>1);
    return (pwur * pwur) % mod;
}
int modinv(int x){
    return qpow(x,mod-2);
}

/// My Code -------------------------------------------------

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);
    srand(time(NULL));
    //freopen("a.in","r",stdin);
    //freopen("a.in","w",stdout);
    int n;
    cin>>n;
    vi v(n);
    int ans=0;
    rep(i,n){
        cin>>v[i];
    }
    bool fl=1;
    while(1){
        rep(i,n){
            if(v[i]%2)fl=0;
        }
        if(!fl)break;
        rep(i,n)v[i]/=2;
        ans++;
    }
    cout<<ans<<enl;

    return 0;
}


#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl "\n"
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define bitcnt(v) __builtin_popcount(v)
#define all(v) v.begin(), v.end()

long double dp[301][301][301];
long double N;

long double solve(int ones, int twos, int threes){
	if(dp[ones][twos][threes] >= 0) return dp[ones][twos][threes];
	long double cur = 1;
	if(threes > 0) cur += (threes/N) * (solve(ones,twos+1,threes-1));
	if(twos > 0) cur += (twos/N) * (solve(ones+1,twos-1,threes));
	if(ones > 0) cur += (ones/N) * (solve(ones-1,twos,threes));
	cur *= N / (ones+twos+threes);
	return dp[ones][twos][threes] = cur;
}

void solution(){
	// expected value dp -- states are messed up, use top down to simplify implementation
	int n; cin >> n;
	N = (long double)n;
	vector<int> a(n); for(auto& x : a) cin >> x;
	int ones = 0, twos = 0, threes = 0;
	for(int x : a){
		if(x == 1) ones++;
		else if(x == 2) twos++;
		else threes++;
	}
	memset(dp,-1,sizeof(dp));
	dp[0][0][0] = 0;
	printf("%0.14Lf\n", solve(ones,twos,threes));
}

int main(){

	fio; // fast input output
	int t;
	t = 1;
	// cin >> t;
	for(int i = 1; i <= t; i++){
		// GOOGLE //
		// cout << "Case #" << i << ": ";
		solution();
	}
	return 0;
}

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

const int maxn=1e5+10;
int n,id[maxn],omove,orev,emove,erev,bit[maxn],rnk[maxn];
bool rev[maxn],vis[maxn];

inline void wrong(){
	puts("No");
	exit(0);
}
inline void add(int x,int val){
	while(x<maxn){
		bit[x]+=val;
		x+=x&-x;
	}
}
inline int sum(int x){
	int res=0;
	while(x){
		res+=bit[x];
		x-=x&-x;
	}
	return res;
}

int main(){
	cin>>n;
	for(int i=1,a;i<=n;++i){
		cin>>a;
		id[i]=(a+2)/3;
		rnk[id[i]]=i;
		if(vis[id[i]]||a==3*id[i]-1)
			wrong();
		vis[id[i]]=true;
		if(a==3*id[i])
			rev[i]=true;
	}
	for(int i=1,a;i<=n;++i){
		cin>>a;
		if(a!=3*id[i]-1)
			wrong();
	}
	for(int i=1,a;i<=n;++i){
		cin>>a;
		if(rev[i]&&a!=3*id[i]-2||!rev[i]&&a!=3*id[i])
			wrong();
	}
	for(int i=1;i<=n;++i)
		if((i&1)!=(id[i]&1))
			wrong();
	for(int i=1;i<=n;i+=2)
		add(i,1);
	for(int i=1;i<=n;i+=2){
		add(rnk[i],-1);
		omove+=sum(rnk[i]);
		orev+=rev[i];
	}
	memset(bit,0,sizeof(bit));
	for(int i=2;i<=n;i+=2)
		add(i,1);
	for(int i=2;i<=n;i+=2){
		add(rnk[i],-1);
		emove+=sum(rnk[i]);
		erev+=rev[i];
	}
	if((omove&1)!=(erev&1)||(emove&1)!=(orev&1))
		wrong();
	puts("Yes");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

int main(){
	int n,h,v;
	cin >> n >> h >> v;
	cout << (n - h + 1) * (n - v + 1) ;
}

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

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

  int ans = 0;
  ans += x / 500 * 1000;
  x %= 500;
  ans += x / 5 * 5;

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


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

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

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

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

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

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

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

using namespace std;

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

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

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

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

ll f(int l, int r, int o) {
	if (s < x[l] || x[r] < s) return abs(s - x[o]);
	if (p[l] >= p[r]) return p[l] += p[r], f(l, r - 1, l) + abs(x[l] - x[o]);
	return p[r] += p[l], f(l + 1, r, r) + abs(x[r] - x[o]);
}

int main() {
	rd(n), rd(s);
	for (int i = 1; i <= n; i++) rd(x[i], p[i]);
	print(f(1, n, s < x[1] ? n : x[n] < s ? 1 : p[1] >= p[n] ? n : 1));
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define maxn 100005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
ll ksm(ll a, ll b) {
   if (!b) return 1;
   ll ns = ksm(a, b >> 1);
   ns = ns * ns % mod;
   if (b & 1) ns = ns * a % mod;
   return ns;
}
int n, s;
ll ans;
int x[maxn];
ll p[maxn];
ll ttm[maxn];
int ls[maxn], pp[maxn];
void caltm(int a) {
	if (!ls[a]) return ;
	else if (ttm[a]) return ;
	else caltm(ls[a]), ttm[a] = ttm[ls[a]] + pp[a];
}
int main() {
	cin >> n >> s;
	for (int i = 1; i <= n; i++)
		scanf("%d%d", &x[i], &p[i]);
	int l = 1, r = n;
	while (1) {
		if (s <= x[l] || s >= x[r]) {
			for (int i = l; i <= r; i++)
				ttm[i] = abs(s - x[i]);
			break;
		}
		if (p[l] >= p[r]) {
			ls[r] = l, pp[r] = x[r] - x[l];
			p[l] += p[r], r--;
		}
		else {
			ls[l] = r, pp[l] = x[r] - x[l];
			p[r] += p[l], l++;
		}
	}
	for (int i = 1; i <= n; i++)
		caltm(i), ans = max(ans, ttm[i]);//, cout << ttm[i] << ' ' << ls[i] << ' ' << pp[i] << endl;
	cout << ans << endl;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
  	int a,b;
  	cin>>a>>b;
  	if(a<=8&&b<=8)
      cout<<"Yay!\n";
    else
      cout<<":(\n";

    }

#include <bits/stdc++.h>

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define  endl 			"\n"
#define  FOR(i, a, n)   for (ll i = a; i < n; i++)
#define  ROF(i, a, n)   for (ll i = a; i >= n; i--)
#define  all(x)         (x).begin(), (x).end()
#define  mset(x, val)   memset(x,val,sizeof(x))
#define  dline          cout<<"///REACHED///\n";

typedef long long ll;

const ll   MOD   =  1e+9+7;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   MAXN  =  4e+5+7;

// 6

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll a,b;
	cin>>a>>b;
	if(((16-(2*min(a,b))+1)/2+min(a,b))>= max(a,b)){
		cout<<"Yay!";
	}
	else{
		cout<<":(";
	}

	return 0;
}


#include <bits/stdc++.h>
#define lop(i, n) for (int i = 0; i < (n); ++i)
#define tc(t) int t; cin>>t; while(t--)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);
    int a,b,c;
    cin>>a>>b>>c;
    int a1 = a + b;
    int a2 = a + c;
    int a3 = b + c;
    if (a1 <= a2 && a1 <= a3) {
        cout<<(a1);
    } else if (a2 <= a1 && a2 <= a3) {
        cout<<(a2);
    } else if (a3 <= a1 && a3 <= a2) {
        cout<<(a3);
    }

}

#include<bits/stdc++.h>
#include<random>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define pb push_back
#define mod 1000000007
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "Yes"<<endl
#define no  cout<< "No"<<endl
#define all(v) (v).begin(),(v).end()
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
void fastio()
{
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout);
  #endif
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL) ;
  cout.tie(NULL);
}
signed main()
{
    fastio();
    int t=1;
   // cin>>t;
    while(t--)
    {

      int a[3];
      for(int i=0;i<3;i++)
      cin>>a[i];
      sort(a,a+3);
      cout<<a[0]+a[1];

    }
}

// 🍪 🍪 🍪


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    int n;
    while(cin>>n,n){
        long long int syain,kakaku,kosuu,syains[5000]={0};
        long long int bango[5000]={0},flag[5000]={0};
        bool flag2=0;
        for(int i=0;i<n;i++){
            cin>>syain>>kakaku>>kosuu;
            bango[i]=syain;
            syains[syain]+=kakaku*kosuu;
        }
        for(int i=0;i<5000;i++){
            if(syains[bango[i]]>=1000000){
                if(flag[bango[i]]==0){
                        cout<<bango[i]<<endl;
                        flag[bango[i]]=1;
                }
            }
        }
        for(int i=0;i<5000;i++){
            if(flag[i]==1)flag2=1;
        }
    if(flag2==0)cout<<"NA"<<endl;
    }
    return 0;
}

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

struct EmployeeSale {
	long employee_number;
	long long sales;
};

int main()
{
	int data_count = 0;
	while (std::cin >> data_count) {
		if (data_count == 0) {
			break;
		}
		std::vector<EmployeeSale> sales_per_employee;
		for (int i = 0; i < data_count; ++i) {
			long employee_number = 0;
			long unit_price = 0;
			long quantity = 0;
			if (std::cin >> employee_number >> unit_price >> quantity) {
				long long each_sales = static_cast<long long>(unit_price) * quantity;
				auto it = std::find_if(
					sales_per_employee.begin(),
					sales_per_employee.end(),
					[=](EmployeeSale const& sale){ return sale.employee_number == employee_number; });
				if (it != sales_per_employee.end()) {
					it->sales += each_sales;
				} else {
					EmployeeSale sales = { employee_number, each_sales };
					sales_per_employee.push_back(sales);
				}
			}
		}

		std::vector<EmployeeSale> sales_achieved;
		std::copy_if(
			sales_per_employee.begin(),
			sales_per_employee.end(),
			std::back_inserter(sales_achieved),
			[](EmployeeSale const& sale){ return sale.sales >= 1000000; });
		if (!sales_achieved.empty()) {
			std::for_each(
				sales_achieved.begin(),
				sales_achieved.end(),
				[](EmployeeSale const& sale){ std::cout << sale.employee_number << std::endl; });
		} else {
			std::cout << "NA\n";
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

typedef pair<int, int> ii;

int n;

string ans, newans;

int main() {
    cin.tie(0), ios::sync_with_stdio(0);
    cin >> n;
    cout << "0\n";
    cout.flush();
    cin >> ans;
    if(ans == "Vacant"){
        cout << "0\n";
        cout.flush();
        cin >> ans;
        exit(0);
    }
    int pos = 0;
    for(int i = 17; i >= 0; i--){
        if(pos + (1 << i) < n){
            cout << pos + (1 << i) << "\n";
            cout.flush();
            cin >> newans;
            if(newans == "Vacant"){
                cout << pos + (1 << i) << "\n";
                cout.flush();
                cin >> newans;
                exit(0);
            }
            if(ans == newans)
                pos += (1 << i);
        }
    }
    cout << pos + 1 << "\n";
    cout.flush();
    cin >> newans;
    exit(0);
}

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

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;//998244353;

template<class _T> static void get(_T& a) {
	std::cin >> a;
}
template<class _T> static void get(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void get(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}
template<class _T> static void getint(_T& a) {
	if (sizeof(_T) > sizeof(int)) {
		scanf("%lld", &a);
	}
	else {
		scanf("%d", &a);
	}
}

static void exec();

int main()
{
	exec();
	fflush(stdout);
	return 0;
}

static void exec()
{
	int N;
	get(N);

	int now = 0, start = 0;
	int end = N;
	std::string ans;

	printf("%d\n", now);
	fflush(stdout);
	get(ans);
	std::string even = ans;
	std::string odd = "Female";
	if (even == odd) odd = "Male";

	while (ans != "Vacant") {
		now = (start + end) / 2;

		printf("%d\n", now);
		fflush(stdout);
		get(ans);

		if (((now % 2) && (ans == odd)) || ((now % 2 == 0) && (ans == even))) {
			start = now;
		}
		else {
			end = now;
		}
	}
}


#include <iostream>
using namespace std;
int main() {
    int N, K; cin >> N >> K;
    int ans = 0;
    for (int i = 1; i <= N; i++) if (i + (K - 1) <= N) ans++;
    printf("%d\n", ans);
}

#include <bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef pair<int, int> Pint;
typedef pair<int64_t, int64_t> Pll;

int main() {
    int N, K;
    cin >> N >> K;
    cout << N - K + 1 << endl;
}


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

int main(){
string A,B,C;
  cin >> A >> B >> C;
  int D = A.size() - 1;
  int E = B.size() - 1;
  int F = C.size() - 1;
  if(A[D] == B[0] && B[E] == C[0]){
cout << "YES" << endl;
  }
else{
cout << "NO" << endl;
}
}

#include <bits/stdc++.h>
#define TOP 0
#define FRONT 1
#define LEFT 2
#define RIGHT 3
#define BACK 4
#define BOTTOM 5
using namespace std;

//TOP,FRONT,LEFT,RIGHT,BACK,BOTTOM
int dice[6]={1,2,4,3,5,6};

//?\\?????¢??????
void rotN(){
  swap(dice[TOP],dice[FRONT]);
  swap(dice[FRONT],dice[BOTTOM]);
  swap(dice[BOTTOM],dice[BACK]);
}

//???????????¢??????
void rotS(){
  //rotN(),rotN(),rotN();
  swap(dice[TOP],dice[BACK]);
  swap(dice[BACK],dice[BOTTOM]);
  swap(dice[BOTTOM],dice[FRONT]);
}

//????????¢??????
void rotE(){
  swap(dice[TOP],dice[LEFT]);
  swap(dice[LEFT],dice[BOTTOM]);
  swap(dice[BOTTOM],dice[RIGHT]);
}

//????????¢??????
void rotW(){
  //rotE(),rotE(),rotE();
  swap(dice[TOP],dice[RIGHT]);
  swap(dice[RIGHT],dice[BOTTOM]);
  swap(dice[BOTTOM],dice[LEFT]);
}

//???????????????????¨????????????????
void rotCW(){
  swap(dice[FRONT],dice[RIGHT]);
  swap(dice[RIGHT],dice[BACK]);
  swap(dice[BACK],dice[LEFT]);
}

//??????????????????????¨???????????????????
void rotCCW(){rotCW(),rotCW(),rotCW();}

int main(){
  cin>>dice[TOP]>>dice[FRONT]>>dice[RIGHT];
  cin>>dice[LEFT]>>dice[BACK]>>dice[BOTTOM];

  string str;
  cin>>str;
  for(int i=0;i<str.size();i++){
    if(str[i]=='N') rotN();
    if(str[i]=='S') rotS();
    if(str[i]=='E') rotE();
    if(str[i]=='W') rotW();
  }

  cout << dice[TOP]<<endl;

}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

#define FOR(i,n) for(int i = 0; i < n; i++)
#define FORa(i,a,b) for(int i = a; i < b; i++)
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define SORT(a,n) sort(begin(a), begin(a) + n)
#define VSORT(v) sort(v.begin(), v.end())
#define MAX 1000000
#define ll long long
#define LOW(s) transform(s.begin(),s.end(),s.begin(),::tolower)
#define UP(s) transform(s.begin(),s.end(),s.begin(),::toupper)

//-------------------------------------------------------//

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

	if((int)s.size() > k) {
		s = s.substr(0,k);
		s += "...";
	}
	cout << s << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long k, n;
	string s;

	cin >> k >> s;

	n = (long long)(s.length());

	if (k >= n){
		cout << s << endl;
	}
	else{
		cout << s.erase(k) << "..." << endl;
	}

	return 0;
}


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

const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

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

    ll x,y;
    cin >> x >> y;

    ll ans = 0;

    if (x * y > 0){
        if (x <= y){
            ans = y-x;
        }
        else{
            ans = x-y+2;
        }
    }
    else if (x * y == 0){
        if (x == 0){
            if (y >= 0){
                ans = y;
            }
            else{
                ans = abs(y)+1;
            }
        }
        else{
            if (x < 0){
                ans = abs(x);
            }
            else{
                ans = x+1;
            }
        }
    }
    else{
        if (abs(x) >= abs(y)){
            ans = abs(x)-abs(y)+1;
        }
        else{
            ans = abs(y)-abs(x)+1;
        }
    }

    cout << ans << endl;
}

#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<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2000+5,base=2001*3;
int f[N*3][N*6],mod;
void add(int &x,int y){
	x=(x+y)%mod;
}
signed main(){
	int n;
	cin>>n>>mod;
	n*=3;
	f[0][base]=1;
	for(int i=0;i<n;++i)
		for(int j=-i;j<=i;++j)
			if(f[i][j+base]){
				add(f[i+1][j+1+base],f[i][j+base]);
				add(f[i+2][j-1+base],1ll*f[i][j+base]*(i+1)%mod);
				add(f[i+3][j+base],1ll*f[i][j+base]*(i+1)%mod*(i+2)%mod);
			}
	int ans=0;
	for(int j=0;j<=n;++j)
		add(ans,f[n][j+base]);
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

// Morris-Pratt
// https://youtu.be/9MphwmIsO7Q?t=7283
template<typename T>
struct MP {
    int n;
    T t;
    vector<int> a;
    MP() {}
    MP(const T& t): t(t) {
        n = t.size();
        a = vector<int>(n+1);
        a[0] = -1;
        int j = -1;
        for (int i = 0; i < n; ++i) {
            while (j != -1 && t[j] != t[i]) j = a[j];
            j++;
            a[i+1] = j;
        }
    }
    int operator[](int i) { return a[i];}
    vector<int> findAll(const T& s) {
        vector<int> res;
        int j = 0;
        for (int i = 0; i < s.size(); ++i) {
            while (j != -1 && t[j] != s[i]) j = a[j];
            j++;
            if (j == n) {
                res.push_back(i-j+1);
                j = a[j];
            }
        }
        return res;
    }
};

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

    vector<ll> ax(n);
    vector<ll> bx(n);
    rep(i, n) {
        ax[i] = a[i]^a[(i+1)%n];
        bx[i] = b[i]^b[(i+1)%n];
    }
    ax.insert(ax.end(), ax.begin(), ax.end());

    MP<vector<ll>> mp(bx);
    vector<int> match = mp.findAll(ax);
    for (int e:match) {
        if (e>=n) continue;
        ll x = a[e] ^ b[0];
        cout << e << " " << x << endl;
    }

    //cout << ans << 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 ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;

bool f(P l,P r){
    int a = l.second - l.first;
    int b = r.second - r.first;
    if(a > 0 && b <= 0){
        return true;
    }
    if(a <= 0 && b > 0){
        return false;
    }
    if(a > 0 && b > 0){
        return l.first < r.first;
    }if(a < 0 && b < 0){
        return l.second > r.second;
    }
}

int main(){
    int n;
    cin >> n;
    vector<string> s(n);
    rep(i,n) cin >> s[i];
    vector<P> lr(n);
    rep(i,n){
        int l = 0;
        int r = 0;
        rep(j,s[i].size()){
            if(s[i][j] == '('){
                ++r;
            }else{
                if(r > 0) --r;
                else ++l;
            }
        }
        lr[i].first = l;
        lr[i].second = r;
    }

    sort(lr.begin(),lr.end(),f);
    if(lr[0].first!=0){
        cout << "No" << endl;
        return 0;
    }
    int l = 0;
    int r = 0;
    rep(i,n){
        if(r < lr[i].first){
            cout << "No" << endl;
            return 0;
        }
        r -= lr[i].first;
        r += lr[i].second;
    }
    cout << (r==0?"Yes":"No") << endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#define ll long long
using namespace std;
ll _11[18];
int main()
{
	ll d;
	scanf("%lld",&d);
	if(d%9){puts("0");return 0;}
	d/=9;
	ll ans=0;
	for(int i=1;i<=17;i++) _11[i]=_11[i-1]*10+1;
	ll len=1;
	for(int i=1;i<=17;i++)
	{
		ll res=1,del=d,L=len,R=1,b=_11[i];
		for(int j=0;j<=(i>>1);j++)
		{
			res*=10-abs(del%(R*10)/R)-(!j);
            del-=b*(del%(R*10)/R);
            b-=L+R;
            L/=10;
            R*=10;
        }
        if(!del) ans+=res;
        len*=10;
	}
	printf("%lld",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int e;
    cin >>e;

    while(e)
    {
        int x=0;
        int y=0;
        int z=0;
        int m=0;
     for(z=0;z*z*z<=e;z++)
        for(y=0;z*z*z+y*y<=e;y++)
        {
            x = e-z*z*z-y*y;
            if(x<0)
                continue;

                int t = x+y+z;
                if(m ==0||m>t)
                    m=t;
            }

        cout <<m<<endl;

        cin >>e;
    }
    return 0;
}


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

int main() {
    int n;
    int k;
    int numShow = 1;
    int minNumber = 9999999;
    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        // 操作Aの処理
        for (int a = i; a < n; a++) {
            numShow *= 2;
            if (numShow > minNumber) {
                break;
            }
        }

        // 操作Bの処理
        for (int b = n - i; b < n; b++) {
            numShow += k;
            if (numShow > minNumber) {
                break;
            }
        }

        if (numShow < minNumber) {
            minNumber = numShow;
        }

        numShow = 1;
    }

    cout << minNumber << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 * 2;

int main() {
	int n, k;
	cin >> n >> k;
	int num = 1;
	rep(i, n){
		num += min(num, k);
	}
	cout << num << endl;
}


#include <array>
#include <cstdlib>
#include <iostream>
#include <string>
#include <queue>
#include <vector>
class SSF {
private:
	void ScanLine(std::vector<std::string> *map, const char lx, const char rx, const char y, const char oy, std::array<std::queue<char>, 4> *buffer) {
		unsigned char i = lx;
		while (i <= rx) {
			while (i < rx) {
				if (map->at(y).at(i) != '0') {
					break;
				}
				++i;
			}
			if (map->at(y).at(i) == '0') {
				break;
			}
			buffer->at(0).push(i);
			while (i <= rx) {
				if (map->at(y).at(i) == '0') {
					break;
				}
				++i;
			}
			buffer->at(1).push(i - 1);
			buffer->at(2).push(y);
			buffer->at(3).push(oy);
		}
		return;
	}
public:
	unsigned char SeedFill(std::vector<std::string> *map, const unsigned char x, const unsigned char y) {
		std::array<std::queue<char>, 4> buffer;
		unsigned char lx, rx, ly, oy;
		if (map->at(y).at(x) == '0' || map->empty() == true) {
			return 0;
		}
		buffer.at(0).push(x);
		buffer.at(1).push(x);
		buffer.at(2).push(y);
		buffer.at(3).push(y);
		while (buffer.front().empty() == false) {
			char lxsav = buffer.at(0).front() - 1, rxsav = buffer.at(1).front() + 1;
			lx = buffer.at(0).front();
			buffer.at(0).pop();
			rx = buffer.at(1).front();
			buffer.at(1).pop();
			ly = buffer.at(2).front();
			buffer.at(2).pop();
			oy = buffer.at(3).front();
			buffer.at(3).pop();
			if (map->at(ly).at(lx) == '0') {
				continue;
			}
			while (rx < map->at(ly).size() - 1) {
				if (map->at(ly).at(rx + 1) == '0') {
					break;
				}
				++rx;
			}
			while (lx > 0) {
				if (map->at(ly).at(lx - 1) == '0') {
					break;
				}
				--lx;
			}
			for (unsigned char i = lx; i <= rx; ++i) {
				map->at(ly).at(i) = '0';
			}
			if (ly - 1 >= 0) {
				if (ly - 1 == oy) {
					ScanLine(map, lx, lxsav, ly - 1, ly, &buffer);
					ScanLine(map, rxsav, rx, ly - 1, ly, &buffer);
				}
				else {
					ScanLine(map, lx, rx, ly - 1, ly, &buffer);
				}
			}
			if (ly + 1 <= map->size() - 1) {
				if (ly + 1 == oy) {
					ScanLine(map, lx, lxsav, ly + 1, ly, &buffer);
					ScanLine(map, rxsav, rx, ly + 1, ly, &buffer);
				}
				else {
					ScanLine(map, lx, rx, ly + 1, ly, &buffer);
				}
			}
		}
		return 1;
	}
};
int main() {
	while (1) {
		SSF ssf;
		std::vector<std::string> map;
		unsigned short count = 0;
		do {
			map.resize(count + 1);
			std::getline(std::cin, map.at(count));
			if (map.at(count).empty() == true) {
				std::getline(std::cin, map.at(count));
				if (map.at(count).empty() == true) {
					return EXIT_SUCCESS;
				}
			}
			map.at(count).resize(map.front().size(), '0');
			map.at(count).shrink_to_fit();
		} while (++count < map.front().size());
		map.shrink_to_fit();
		count = 0;
		for (unsigned char j = 0; j < map.size(); ++j) {
			for (unsigned char i = 0; i < map.at(j).size(); ++i) {
				count += ssf.SeedFill(&map, i, j);
			}
		}
		std::cout << count << std::endl;
	}
	return EXIT_SUCCESS;
}

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PA;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
queue<PA> que;
int main(){
  int n,ans;
  char m[13][13];

  while(cin >> m[0]){

    for(int j = 0;j < 12;j++){
      if(j==0) continue;
      cin >> m[j];
    }
    ans = 0;
    for(int j = 0;j < 12;j++){
      for(int k = 0;k < 12;k++){
	if(m[j][k] == '1'){
	  que.push(PA(j,k));
	  while(!que.empty()){
	    PA p=que.front();que.pop();
	    m[p.first][p.second] = '0';
	    for(int l = 0;l < 4;l++){
	      int y = p.first+dy[l];
	      int x = p.second+dx[l];
	      if(x<0||y<0||x>=12||y>=12||m[y][x]=='0') continue;
	      que.push( PA(y,x) );
	    }
	  }
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }
  return (0);
}


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

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main() {
  int n; cin >> n;
  string s;
  getline(cin, s);
  while(n--) {
    getline(cin, s);
    rep(i, s.size()) {
      if(s.substr(i, 7) == "Hoshino") s[i+6] = 'a';
    }
    cout << s << endl;
  }
}

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

int main() {
string az="abcdefghijklmnopqrstuvwxyz";
    string s;
    cin >> s;
    sort(s.begin(),s.end());
    int i=0,j=0;
    for(int j=0;j<s.length();j++) {
        if(az[i]==s[j]) {
		i++;
		}
        if(i==26) {
		cout << "None" << endl;
		return 0;
		}
    }
    cout << az[i];
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define mod 1000000007
int main() {
    int n;
    cin >> n;
    int k = 0, l = 0, m = 0;
    ll ret = 1;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        if (k == a) {
            int count = 1;
            k++;
            if (l == a) count++;
            if (m == a) count++;
            ret = ret * count % mod;
        }
        else if (l == a) {
            int count = 1;
            l++;
            if (m == a) count++;
            ret = ret * count % mod;
        }
        else if (m == a) {
            m++;
        }
        else {
            ret = 0;
            break;
        }
    }
    cout << ret << endl;
    return 0;
}

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

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

int main()
{
	int n;
	long long ans = 0;
	cin>> n;
	vector<int> a(n);
	vector<int> cnt(n);
	for(int i=0;i<n;i++){
		cin>> a[i];
	}

	int cnt0 = 3;
	if(a[0] == 0){
		ans = 3;
		cnt[0]++;
		cnt0--;
	}
	for(int i=1;i<n;i++){
		if(a[i] == 0){
			ans *= cnt0;
			cnt0--;
			cnt[0]++;
			if(cnt0<0) ans = 0;
		}else{
			ans *= (cnt[a[i]-1] - cnt[a[i]]);
			cnt[a[i]]++;
			if(3<cnt[a[i]]) ans = 0;
		}
		if(1E9+7<=ans) ans %= (long long)(1E9+7);
	}

	cout<<ans<<endl;
}


#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int N;
        cin>>N;
        if(!N)break;
        int rui[10000]={0};
        for(int i=1;i<=N;i++){
            int a;
            cin>>a;
            rui[i]=rui[i-1]+a;
        }
        int ans=-2000000000;
        //for(int i=0;i<=N;i++)printf("[%d]",rui[i]);
        for(int i=0;i<=N;i++){
            for(int j=i+1;j<=N;j++)ans=max(ans,rui[j]-rui[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define ALL(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	for (int n; cin >> n&&n;) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		vector<int> M(n);
		for (int i = 0; i < n; i++) {
			M[i] = a[i];
			int temp = a[i];
			for (int j = i + 1; j < n; j++) {
				temp += a[j];
				M[i] = max(M[i], temp);
			}
		}
		cout << *max_element(ALL(M)) << endl;
	}
	return 0;
}


#include <iostream>
using namespace std;

int main(){
  int a,s;
  while(cin>>a){//EOF?????§
    s=0;
    for(int i=a;i<600;i+=a){
      s+=i*i*a;
    }
    cout<<s<<endl;
  }
  return 0;
}

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::cout.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/

int main() {
	std::string s;
	std::cin >> s;
	std::map<char, lnt> map;
	lnt n=s.size();
	lnt ans=n*n;
	for(auto c:s) map[c]++;
	for(auto e:map) {
		ans-=e.second*e.second;
	}
	print(ans/2+1);
}


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

const int INF = 1 << 30;
const int V = 100000;
const int E = 500000;
struct Edge{ int to, cost; };
vector<Edge> list[E];
typedef pair<int, int> P;
int d[V];

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

	int v, e, r;
	cin >> v >> e >> r;

	int a, b, c;
	for(int i = 0; i < e; ++i){
		cin >> a >> b >> c;
		list[a].push_back(Edge{b, c});
	}

	priority_queue<P, vector<P>, greater<P>> pque;
	pque.push(P(0, r));
	fill(d, d + v, INF);
	d[r] = 0;

	while(!pque.empty()){
		P p = pque.top();
		pque.pop();
		int v1 = p.second;
		if(d[v1] < p.first) continue;

		for(int i = 0; i < list[v1].size(); ++i){
			Edge e1 = list[v1][i];
			if(d[e1.to] > d[v1] + e1.cost){
				d[e1.to] = d[v1] + e1.cost;
				pque.push(P(d[e1.to], e1.to));
			}
		}
	}

	for(int i = 0; i < v; ++i){
		if(d[i] != INF)
			cout << d[i] << endl;
		else
			cout << "INF" << endl;
	}

	return 0;
}

#include<functional>
#include<algorithm>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF=1000000007;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const int N=101;

int cost[N][N];
int tim[N][N];

int bfs(int n,int p,int q,int graph[N][N]){
  vector<int> v(n);
  queue<int> que;
  rep(i,n)v[i]=INF;
  v[p]=0;
  que.push(p);
  int u;
  while(!que.empty()){
    u=que.front(),que.pop();

    rep(i,n){
      if(v[i]>v[u]+graph[u][i]){
        v[i]=v[u]+graph[u][i];
        que.push(i);
      }
    }
  }
  return v[q];
}

signed main(){
  int n,m;
  while(cin>>n>>m,n!=0){
    rep(i,m)rep(j,m){
      cost[i][j]=(i==j?0:INF);
      tim[i][j]=(i==j?0:INF);
    }
    rep(i,n){
      int a,b,c,t;
      cin>>a>>b>>c>>t;
      a--;b--;
      cost[a][b]=cost[b][a]=c;
      tim[a][b]=tim[b][a]=t;
    }
    int k;
    cin>>k;
    rep(i,k){
      int p,q,r;
      cin>>p>>q>>r;
      p--;q--;
      int ans;
      if(r==0){
        ans=bfs(m,p,q,cost);
      }else{
        ans=bfs(m,p,q,tim);
      }
      cout<<ans<<endl;
    }
  }
  return 0;
}


#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    int temp=n;
    int lastdigit;
    int digits=0;
    while(n!=0){
        lastdigit=n%10;
        n/=10;
        digits++;
    }
    int result=0;
    while(digits){
        result*=10;
        digits--;
        result+=lastdigit;
    }
    if(result>=temp){
        cout<<result;
    }
    else
    {
        cout<<(result+111);
    }
}

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {
	int n;
	cin >> n;
	int next = n / 100 * 111;
	if (next < n) {
		next += 111;
	}
	cout << next << endl;
}


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

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector< vector<ll> > vvint;
typedef vector< vector< vector<ll> > > vvvint;
typedef vector<string> vstring;
typedef vector< vector<string> > vvstring;
typedef vector<char> vchar;
typedef vector< vector<char> > vvchar;
typedef vector<long double> vdouble;
typedef vector< vector<long double> > vvdouble;
typedef vector< vector< vector<long double> > > vvvdouble;
typedef pair<ll,ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i,n) for(ll i=0;i<n;i++)
#define repf(i,f,n) for(ll i=f;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define MOD 1000000007
// #define MOD 998244353
// #define MOD LLONG_MAX
const double PI=3.14159265358979323846;
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; }

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

void printv(vint &v){
    for(auto e:v) cout<<e<<" ";
        cout<<endl;
    }

// 繰り返し二乗法
ll power(ll a, ll b){
    if(a==1)return a;
    // if(a==0)re
    ll res=1;
    while(b>0){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}

const int MAX=2100000;

ll fact[MAX], fact_inv[MAX];

void init_fact(ll n){
    fact[0]=1;
    // 階乗の計算
    for(ll i=0; i<n; i++) fact[i+1]=fact[i]*(i+1)%MOD;
    fact_inv[n]=power(fact[n], MOD-2);
    // 逆元の計算
    for(ll i=n-1;i>=0;i--) fact_inv[i]=fact_inv[i+1]*(i+1)%MOD;
}

ll comb(ll n, ll r){
    return (fact[n]*fact_inv[r])%MOD*fact_inv[n-r]%MOD;
}

ll perm(ll n, ll r){
    return (fact[n]*fact_inv[n-r])%MOD;
}

int main() {
    cout<<fixed<<setprecision(10);

    ll k; cin>>k;
    ll n = 50;

    ll x,y;
    x = k/n;
    y = k%n;
    // ll a_base = (x+1)*n - 1 - y;
    ll a_base = ( n - 1 ) + x - y;
    vint ans(n, a_base);
    rep(i,y){
        ans[i] += n - (i+1) + 2;
    }

    cout << n <<endl;
    for(auto e:ans) cout<<e<<" ";
    cout<<endl;

    return 0;

}

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    long k;
    cin >> k;
    long q = k / 50, r = k % 50;
    cout << 50 << '\n';
    for (long i=0; i<50; i++)
        cout << q + i - r + (i < r ? 51 : 0) << (i < 49 ? ' ' : '\n');
    return 0;
}


#include<stdio.h>
#include<string.h>
void haichi(void);
void Block(int,int);
int c,d,x,y,xg,yg,M,Flag;
int MAP[101][101],MF[101][101];
int main(void)
{
	int w,h,xs,ys,n;
	while(1){
		Flag=0;
		memset(MAP,0,sizeof(MAP));
		memset(MF,0,sizeof(MF));
		scanf("%d %d",&w,&h);
		if(w == 0 && h == 0) break;
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			haichi();
		}
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				printf("%d",MAP[i][j]);
			}
			printf("\n");
		}*/
		M = MAP[ys][xs];//
		if(MAP[ys][xs] == MAP[yg][xg])
			Block(xs,ys);
		if(Flag==1)
			printf("OK\n");
		else
			printf("NG\n");
	}
	return 0;
}

void haichi(void)
{
	if(d==0){
		MAP[y][x]=c;MAP[y][x+1]=c;MAP[y][x+2]=c;MAP[y][x+3]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;MAP[y+1][x+2]=c;MAP[y+1][x+3]=c;
	}
	else if(d==1){
		MAP[y][x]=c;MAP[y][x+1]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;
		MAP[y+2][x]=c;MAP[y+2][x+1]=c;
		MAP[y+3][x]=c;MAP[y+3][x+1]=c;
	}
}

void Block(int X,int Y)
{
	MF[Y][X] = 1;
	//printf("%d",M);
	if(X == xg && Y == yg)
		Flag = 1;
	if(MAP[Y-1][X] == M && MF[Y-1][X] == 0) Block(X,Y-1); //???
	if(MAP[Y+1][X] == M && MF[Y+1][X] == 0) Block(X,Y+1); //???
	if(MAP[Y][X-1] == M && MF[Y][X-1] == 0) Block(X-1,Y); //???
	if(MAP[Y][X+1] == M && MF[Y][X+1] == 0) Block(X+1,Y); //???
}

#include<iostream>
using namespace std;
#define MAX 100
#define BLANK 0
#define LONG 4
#define SHORT 2
#define HORIZONTAL 0
#define VERTICAL 1
#define VISITED 9

int field[MAX+2][MAX+2];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
void rec(int x,int y,int c)
{
  int i;
  if(field[x][y]!=c)return;
  field[x][y]=VISITED;
  for(i=0;i<4;i++)rec(x+dx[i],y+dy[i],c);
  return;
}

int main()
{
  int w,h,xs,ys,xg,yg,n,c,d,x,y;
  int i,j;
  while(cin>>w>>h>>xs>>ys>>xg>>yg>>n,w|h){
    for(j=1-1;j<=h+1;j++)for(i=1-1;i<=w+1;i++)field[i][j]=BLANK;
    while(n-->0){
      cin>>c>>d>>x>>y;
      if(d==HORIZONTAL)
	for(i=0;i<LONG;i++)for(j=0;j<SHORT;j++)field[x+i][y+j]=c;
      else
	for(i=0;i<SHORT;i++)for(j=0;j<LONG;j++)field[x+i][y+j]=c;
    }
    if(field[xs][ys]!=BLANK)rec(xs,ys,field[xs][ys]);
    puts(field[xg][yg]==VISITED?"OK":"NG");
  }
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
  string a;
  cin>>a;
  if(a.at(0)!='A'){
    cout<<"WA"<<endl;
    return 0;
  }
  for(ll i=2;i<a.size()-1;i++){
    if(a.at(i)=='C'){
      a.at(i)='z';
      break;
    }
    if(i==a.size()-2){
      cout<<"WA"<<endl;
      return 0;
    }
  }
  for(ll i=1;i<a.size();i++){
    if(a.at(i)<'a'||a.at(i)>'z'){
      cout<<"WA"<<endl;
      return 0;
    }
  }
  cout<<"AC"<<endl;

  return 0;
}

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

int main(){
    string str;
    cin >> str;
    for(auto it=str.rbegin();it!=str.rend();++it)cout << *it;
    cout << endl;
    return 0;
}


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

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)

int n,k;
int card[16];
int check[16];
set<string> ss;

string tos(int a){
	char str[100];
	int i=0;
	while( a>0 ){
		str[i++] = a%10 + '0';
		a /= 10;
	}
	str[i] = 0;
	string s = string(str);
	return string(s.rbegin(), s.rend());
}
void solve(int cnt, string str){
	//printf("%d , %s\n",cnt,str.c_str());
	if( cnt==0 ){
		ss.insert(str);
		return;
	}
	for(int i=0; i<n; i++)if( !check[i] ){
		check[i] = 1;
		solve(cnt-1, str + tos(card[i]));
		check[i] = 0;
	}
	return;
}
int main(){
	while(scanf("%d%d",&n,&k),n|k){
		ss.clear();
		rep(i,n)scanf("%d",&card[i]);
		solve(k,"");
		cout << ss.size() << endl;
	}

	return 0;
}

#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#pragma warning (disable: 4996)
using namespace std;

int dat[10] = { 0,2,5,5,4,5,6,3,7,6 };

int N, M, A[1 << 18];
int dp[1 << 18];

int main() {
	cin >> N >> M;
	for (int i = 1; i <= M; i++) cin >> A[i];

	for (int i = 0; i <= N; i++) dp[i] = -(1 << 30);
	dp[0] = 0;
	for (int i = 0; i <= N; i++) {
		for (int k = 1; k <= M; k++) dp[i + dat[A[k]]] = max(dp[i + dat[A[k]]], dp[i] + 1);
	}

	int val = N; string S = "";
	while (val > 0) {
		int r = 0;
		for (int i = 1; i <= M; i++) {
			if (val >= dat[A[i]] && dp[val] - 1 == dp[val - dat[A[i]]]) r = max(r, A[i]);
		}
		val -= dat[r]; S += ('0' + r);
	}
	//reverse(S.begin(), S.end());
	cout << S << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	bool A=0;
	char ch,ch_;
	cin>>ch;
	ch_=ch;
	while((cin>>ch)!=0) {
		if(ch_=='A'&&ch=='C') {
			cout<<"Yes";
			return 0;
		}
		ch_=ch;
	}
	cout<<"No";
	return 0;
}

#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>
#include <fstream>

using namespace std;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()

#pragma region UnionFind
struct UnionFind
{
	vector<int> par;

	UnionFind(int n) : par(n, -1) {}
	void init(int n) { par.assign(n, -1); }

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

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

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

	int size(int x)
	{
		return -par[root(x)];
	}
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b)
{
	if (a%b == 0)return b;
	return gcd(b, a%b);
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma endregion

int main()
{
	string s; cin >> s;
	rep(i, s.size() - 1)if (s.substr(i, 2) == "AC")
	{
		cout << "Yes" << endl;
		return 0;
	}
	cout << "No" << endl;
}


#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; }
template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; }
const int MAXN = (int)3e5 + 17;
const int mod = (int)1e9 + 7;
const int OFFSET = 5;

int n, suff[MAXN];
string s;

void read()
{
	cin >> s;
	n = s.size();
}

int dp[MAXN][10][4];

int rec(int pos, int len, int bd)
{
	if(bd > 2)
	{
		if(bd % 2 == 0) len += (bd - 2), bd = 2;
		if(bd % 2 == 1) len += (bd - 1), bd = 1;
	}

	if(bd < 0) bd = 0;
	if(len > 4) len = 4;

	if(pos == n)
	{
		if(len > 0) return 1;
		return 0;
	}

	int &memo = dp[pos][len + OFFSET][bd];
	if(memo != -1) return memo;
	memo = 0;

	if(s[pos] == '1' || s[pos] == '?') memo = (memo + rec(pos + 1, len + 1, bd - 1)) % mod;
	if(s[pos] == '0' || s[pos] == '?') memo = (memo + rec(pos + 1, len - 1, bd + 1)) % mod;
	return memo;
}

void solve()
{
	suff[n] = 1;
	for(int i = n - 1; i >= 0; i--)
	{
		suff[i] = suff[i + 1];
		if(s[i] == '?') suff[i] = (2ll * suff[i + 1]) % mod;
	}

	memset(dp, -1, sizeof(dp));
	cout << rec(0, 0, 0) << endl;
}

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

	read();
	solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define MAXN	1000005
#define P	998244353
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
long long fac[MAXN], inv[MAXN];
void update(long long &x, long long y) {
	x = (x + y) % P;
}
long long power(long long x, long long y) {
	if (y == 0) return 1;
	long long tmp = power(x, y / 2);
	if (y % 2 == 0) return tmp * tmp % P;
	else return tmp * tmp % P * x % P;
}
long long getc(long long x, long long y) {
	return fac[x] * inv[y] % P * inv[x - y] % P;
}
int main() {
	int n, m;
	read(n), read(m);
	if (n < m) swap(n, m);
	fac[0] = 1;
	for (int i = 1; i <= n + m; i++)
		fac[i] = fac[i - 1] * i % P;
	inv[n + m] = power(fac[n + m], P - 2);
	for (int i = n + m - 1; i >= 0; i--)
		inv[i] = inv[i + 1] * (i + 1) % P;
	long long ans = 0;
	for (int i = 1; i <= m; i++)
		update(ans, getc(2 * i, i) * getc(n + m - 2 * i, m - i));
	ans = ans * power(getc(n + m, m) * 2 % P, P - 2) % P;
	update(ans, n);
	cout << ans << endl;
	return 0;
}


#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

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

int main(){
    int N, a; std::cin >> N;
    //dp[i][j] := i-4(0-order)文字まで見て j=(取らない)-(取った)+1
    std::vector<std::vector<long long>> dp(N+4, std::vector<long long>(3, 0));
    for(int i(4), i_len(N+4); i < i_len; ++i){
        std::cin >> a;
        if(i & 1)
            dp[i][1] = std::max(dp[i-1][0], std::max(dp[i-2][1], dp[i-3][0]) + a);
        else{
            dp[i][0] = dp[i-2][0] + a;
            if(i > 4)
                dp[i][2] = std::max(dp[i-1][1], std::max({dp[i-2][2], dp[i-3][1], dp[i-4][0]}) + a);
        }
    }
    std::cout << dp[N+3][(N & 1) + 1] << std::endl;
    return 0;
}

//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define bged(v) (v).begin(),(v).end()
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
typedef long long ll;
const int Imx=2147483647;
const ll Lbig=2e18;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it,len;
	fastio(){it=len=0;}
	inline char get()
	{
		if(it<len)return s[it++];it=0;
		len=fread(s,1,100000,stdin);
		if(len==0)return EOF;else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while(c==' '||c=='\n')c=get();
		if(it>0)it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0;bool ng=0;char c;c=_buff.get();
	while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
	if(c=='-')ng=1,c=_buff.get();
	while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
	return ng?-r:r;
}
template<class T> inline void putnum(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=_buff.get();while(c<=32)c=_buff.get();return c;}
ll qpow(ll x,ll k){return k==0?1:1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const double eps=1e-7;
const double pi=acos(-1);
bool is0(double x){return x>-eps&&x<eps;}
struct point
{
	double x,y;
	point(double _x=0,double _y=0){x=_x;y=_y;}
	point operator+(const point &t)const{return point(x+t.x,y+t.y);}
	point operator-(const point &t)const{return point(x-t.x,y-t.y);}
	point operator*(const point &t)const{return point(x*t.x-y*t.y,x*t.y+y*t.x);}
	point operator*(const double &t)const{return point(x*t,y*t);}
	point operator/(const double &t)const{return point(x/t,y/t);}
	point operator-()const{return point(-x,-y);}
	double len()const{return sqrt(x*x+y*y);}
	point operator/(const point &t)const{return point(x*t.x+y*t.y,-x*t.y+y*t.x)/t.len()/t.len();}
	double det(const point &t)const{return x*t.y-y*t.x;}
	double dot(const point &t)const{return x*t.x+y*t.y;}
	double len2()const{return x*x+y*y;}
	point unit()const{return (*this)/len();}
	bool operator<(const point &t)const{return is0(x-t.x)?y<t.y:x<t.x;}
	bool operator==(const point &t)const{return is0(x-t.x)&&is0(y-t.y);}
	void out(){cerr<<x<<","<<y<<endl;}
	double rad()const{return atan2(y,x);}
	point conj()const{return point(x,-y);}
};
point a[111];
int n;
int main()
{
	geti(n);
	for(int i=1;i<=n;i++)
	{
		int x,y;
		getii(x,y);
		a[i].x=x;a[i].y=y;
	}
	for(int i=1;i<=n;i++)
	{
		vector<pair<double,int> > v;
		int cur=0;
		for(int j=1;j<=n;j++)
		{
			if(i==j)continue;
			point p=(a[j]-a[i])*point(0,-1);
			point q=(a[j]-a[i])*point(0,1);
			double l=p.rad();
			double r=q.rad();
			v.PB(MP(l,1));
			v.PB(MP(r,-1));
			if(l>r)cur++;
		}
		v.PB(MP(-acos(-1),0));
		v.PB(MP(acos(-1),0));
		sort(v.begin(),v.end());
		double sum=0;
		for(int j=0;j+1<v.size();j++)
		{
			cur+=v[j].SS;
			if(cur==0)
			{
				sum+=v[j+1].FF-v[j].FF;
			}
		}
		cout<<fixed<<setprecision(10)<<sum/acos(-1)/2<<endl;
	}

	return 0;
}


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

using i64 = long long;
using u64 = unsigned long long;

const int mod = (int) 1e9+7;

void __print() {cerr << "]\n";}

template<typename T, typename... V>
void __print(T t, V... v)
{ cerr << t; if(sizeof...(v)) cerr << ", "; __print(v...);}

#define dbg(x...) cerr << "[" << #x << "] = [", __print(x)
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(NULL)

int n;
int a[51];

int f(int k)
{
    int ans = 0;
    for(int A = 0; A <= n; A++) {
        for(int B = 0; B <= n; B++) {
            if(A+B > k || A+B > n) continue;
            int sum = 0;
            vector<int> v;
            for(int i = 0; i < A; i++) {
                v.push_back(a[i]);
                sum += a[i];
            }
            for(int i = n-1; i >= n - B; i--) {
                v.push_back(a[i]);
                sum += a[i];
            }
            sort(v.rbegin(), v.rend());
            ans = max(ans, sum);
            for(int i = 0; i < k-A-B && v.size(); i++) {
                sum -= v.back();
                ans = max(ans, sum);
                v.pop_back();
            }
        }
    }
    return ans;
}

int main()
{
    int k;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    int ans = 0;
    for(int i = 0; i <= k; i++) {
        ans = max(ans, f(i));
    }
    printf("%d\n", ans);
}

#include<bits/stdc++.h>
using namespace std;
#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 erep(i,a,n) for(int i = a;i<=n;i++)
typedef long long ll;
#define int long long
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstring vector<string>
#define vdouble vector<double>
#define vll vector<ll>:
#define vbool vector<bool>
#define INF 1101010101010101010
#define MOD 1000000007
#define P pair<int,int>
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

const int MAXN = 1010;
const int MAXV = MAXN*(MAXN-1)/2;

vint to[MAXV];
int id[MAXN][MAXN];

bool visited[MAXV];
int dp[MAXV];
bool calculated[MAXV];

int dfs(int a){
  if(visited[a]){
    if(!calculated[a]){
      return -1;
    }
    return dp[a];
  }
  visited[a] = true;
  dp[a] = 1;
  for(int p:to[a]){
    int u = dfs(p);
    if(u == -1){
      return -1;
    }
    dp[a] = max(dp[a],u+1);
  }
  calculated[a] = true;
  return dp[a];
}

int toID(int a,int b){
  if(a < b) swap(a,b);
  return id[a][b];
}

signed main(){
  int n;
  cin >> n;
  vvint A(n,vint(n-1));
  rep(i,0,n){
    rep(j,0,n-1){
      cin >> A[i][j];
      A[i][j]--;
    }
  }
  int v = 0;
  rep(i,0,n){
    rep(j,0,n){
      if(i > j){
        id[i][j] = v;
        v++;
      }
    }
  }
  rep(i,0,n){
    rep(j,0,n-1){
      A[i][j] = toID(i,A[i][j]);
    }
  }
  rep(i,0,n){
    rep(j,0,n-2){
      to[A[i][j+1]].push_back(A[i][j]);
    }
  }
  int ans = 0;
  rep(i,0,v){
    int res = dfs(i);
    if(res == -1){
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans,res);
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int M = 1e9+7 ;

int main(){

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

    int n ;
    cin >> n ;

    vector<pair<ll,ll>> A(n);
    for(int i = 0 ; i < n ; ++i){
        cin >> A[i].first >> A[i].second ;
    }

    auto comp = [&](pair<ll,ll>& a,  pair<ll,ll>& b){
        return a.first + a.second < b.first + b.second ;
    };

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

    vector<vector<ll>> dp (n+1 , vector<ll> (n + 1, 1e14));

    dp[0][0] = 0 ;

    for(int i = 1 ; i <= n ; ++i){
        for(int j = 0 ; j <= n ; ++j){
            if (j == 0){
                dp[i][j] = min(dp[i][j] , dp[i-1][j]) ;
                continue ;
            }

            dp[i][j] = dp[i-1][j];

            // if(i == 1 && j == 1){
            //     cout <<"&&&&&&&&&&&&&&&77"<<"\n";
            //     cout << dp[i-1][j] << "\n";
            //     cout << dp[i-1][j-1] << "\n";
            //     cout <<A[i-1].first <<" , "<< A[i-1].second << "\n";
            //     cout <<"%%%%%%%%%%%%%%%%%%"<<"\n";
            // }

            if(A[i-1].first >=  dp[i-1][j-1]){
                dp[i][j] = min (dp[i][j], A[i-1].second + dp[i-1][j-1] );
            }

        }

    }

    // cout <<"dp array :: "<< "\n";

    // for(int i = 1 ; i <= n ;++i){
    //     for(int j = 1 ; j <= n ;++j){
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << "\n";
    // }
    for(int j = n ; j >= 0 ;--j){
        if (dp[n][j] != 1e14){
            cout <<  j << "\n";
            break;
        }
    }

    return 0;
}

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD (1000000007)
//#define MOD (998244353)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

vii v;
int n,dp[5000][5001];

int f(int i,int k){// f(i,j) min price when j items taken and i items included
    if(k<0)return INF;
    if(i==-1){
        if(k>0)return INF;
        return 0;
    }

    int &ans=dp[i][k];
    if(ans!=-1)return ans;

    ans=INF;
    int picheka=f(i-1,k-1);
    if(picheka<=v[i].first)ans=min(ans,picheka+v[i].second);
    ans=min(ans,f(i-1,k));

    return ans;
}

signed main(){
    FASTER;
    memset(dp,-1,sizeof dp);
    cin>>n;
    v=vii(n);cin>>v;
    sort(all(v),[](const pii &a,const pii &b){return a.first+a.second<b.first+b.second;});

    for(int i=n;i>=0;i--){
        if(f(n-1,i)!=INF){
            cout<<i;
            return 0;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	ll l, r;
	cin >> l >> r;
	if (r - l + 1 >= 2019)
		cout << 0 << endl;
	else {
		ll ans = INF;
		repl(i, l, r + 1) repl(j, i + 1, r + 1) ans = min(ans, i * j % 2019);
		cout << ans << endl;
	}
	return 0;
}

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007

//ここから編集する
class Solve {
public:

    Int modmul(Int a, Int b) {
        return a * b % MOD;
    }

    void solve() {
        Int N;
        string S;

        cin >> N >> S;
        N *= 2;

        vector<int> LR(N);
        if (S[0] == 'W' || S.back() == 'W') {
            cout << 0 << endl;
            return;
        }

        LR[0] = 0;
        for (int i = 1; i < N; ++i) {
            if (S[i - 1] == S[i]) {
                LR[i] = 1 - LR[i - 1];
            } else {
                LR[i] = LR[i - 1];
            }
        }

        Int ans = 1;
        Int tapi = 0;
        for (int i = 0; i < N; ++i) {
            if (LR[i] == 0) {
                tapi++;
            } else {
                ans = modmul(ans, tapi);
                tapi--;
            }
        }

        if (tapi != 0) {
            cout << 0 << endl;
            return;
        }

        for (int i = 0; i < N / 2; ++i) {
            ans = modmul(ans, i + 1);
        }

        cout << ans << endl;

    }
};

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

    Solve().solve();

    return 0;
}


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

int main() {
  string s;
  cin >> s;
  bool AC = true;
  if(s[0] != 'A') AC = false;
  int count = 0;
  for(int i=2; i<s.size()-1; i++) {
    if(s[i]=='C') count++;
    else if (isupper(s[i])) AC = false;
    if(i==s.size()-2 && count != 1) AC = false;
  }
  for(int i=1; i<s.size(); i++) {
    if(i==1 || i==s.size()-1) {
      if(isupper(s[i])) AC = false;
    }
  }
  if(AC) cout << "AC" << endl;
  else cout << "WA" << endl;
  return 0;
 }

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
  string input;
  cin >> input;
  regex pattern("^A[a-z]+C[a-z]+$");
  if (regex_match(input, pattern))
  {
    cout << "AC" << endl;
  }
  else
  {
    cout << "WA" << endl;
  }
  return 0;
}


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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

const int INF = 1e9;

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

int H, W;
int a[20][20];
int ans;

bool inside(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

void dfs(int x, int y, int c) {
    if (c > 10) return;

    rep(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if (!inside(nx, ny)) continue;
        if (a[nx][ny] == 1) continue;
        while (true) {
            if (!inside(nx, ny)) break;
            if (a[nx][ny] == 1) {
                a[nx][ny] = 0;
                dfs(nx - dx[k], ny - dy[k], c + 1);
                a[nx][ny] = 1;
                break;
            }
            else if (a[nx][ny] == 3) {
                ans = min(ans, c);
                break;
            }

            nx += dx[k], ny += dy[k];
        }
    }
}

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

    while (cin >> W >> H, H) {
        int sx, sy;
        rep(i, H) rep(j, W) {
            cin >> a[i][j];
            if (a[i][j] == 2) {
                sx = i, sy = j;
            }
        }

        ans = INF;
        dfs(sx, sy, 1);

        cout << (ans == INF ? -1 : ans) << endl;
    }
}

// ALDS1_13_A
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i,n) for(int i=0;i<int(n);i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
namespace you {
    std::string to_string(char val) {
        return std::string(1, val);
        // return std::string{val};
        // you::to_string(char)
    }
}

vector<int> x;
vector<int> y;
vector<int> vx={-1,1,1,-1};
vector<int> vy={-1,1,-1,1};

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

    int k;
    cin>>k;
    rep(i,k){
        int xx,yy;
        cin>>xx>>yy;
        x.push_back(xx);
        y.push_back(yy);
    }
    vector<int> kind(8);
    rep(i,8) kind[i]=i;
    do{
        vector<vector<bool>> ban(8,vector<bool>(8,false)); //falseが. trueがQ
        rep(i,k){
            ban[x[i]][y[i]]=true;
        }
        int numq=k;
        rep(i,8){
            if(ban[i][kind[i]]==false){
                ban[i][kind[i]]=true;
                numq+=1;
            }
        }
        int okq=0;
        if(numq!=8) continue; //Qを8個以上おいてるときは無視
        else{
            //cout<<kind<<endl;
            rep(i,8){ //8個のQについてみる。
                int ny=i,nx=kind[i];
                bool flag=true;
                rep(j,4){ //左右上下斜め方向の確認
                    int newx=nx+vx[j],newy=ny+vy[j];
                    while(0<=newx&&newx<=7&&0<=newy&&newy<=7){
                        if(ban[newy][newx]==true){
                            flag=false;
                            break;
                        }
                        newx+=vx[j];
                        newy+=vy[j];
                    }
                    if(!(flag)) break;
                }
                if(flag) okq+=1;
            }
        }
        //cout<<okq<<endl;
        if(okq==8) break;
    }while(next_permutation(all(kind)));
    //cout<<kind<<endl;

    vector<vector<bool>> ans(8,vector<bool>(8,false));
    rep(i,8) ans[i][kind[i]]=true;
    rep(i,8){
        rep(j,8){
            if(ans[i][j]) cout<<"Q";
            else cout<<".";
        }
        cout<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cmath>
#define buf 10000
using namespace std;
int k=0;
int B[buf];
void Answer(int n);
int PrimeNumber(int n);

int main(void){
  int i,n;
  char str[buf];

  for(i=2;i<buf;i++)
    PrimeNumber(i);
  for( ;fgets(str,sizeof(str),stdin)!=NULL; ){
    sscanf(str,"%d",&n);
    if(n==0) break;
    Answer(n);
  }
  return 0;
}

void Answer(int n){
  int i,j;
  for(i=0;i<k;i++){
    if(B[i]>n)
        break;
  }
  for(j=i-1;j>=0;j--){
    if(B[j]-B[j-1]==2){
      printf("%d %d\n",B[j-1],B[j]);
      break;
    }
  }
}

int PrimeNumber(int n){
  int i,c;
  if(n==2){
    B[k]=n;
    k++; return 0;
  }
  if(n<2 || n%2==0)
    return 0;
  c=sqrt(n);
  for(i=3;i<=c;i+=2){
    if(n%i==0)
      return 0;
  }
  B[k]=n;
  k++; return 0;
}

#include<iostream>
using namespace std;
bool isp[10000];
main()
{
    isp[0]=isp[1]=1;
    for(int i=2;i<=100;i++)
    {
        if(isp[i])continue;
        for(int j=i*i;j<=10000;j+=i)isp[j]=1;
    }
    int n;
    while(cin>>n,n)
    {
        while(isp[n]||isp[n-2])n--;
        cout<<n-2<<" "<<n<<endl;
    }
}


#include <iostream>
using namespace std;

int main(void){
   int n,a[100];
   cin >> n;
   for(int i = 1; i <= n; i++){
       cin >> a[i];
   }

   cout << a[n];
   for(int i = n-1; i >= 1; i--){
       cout << " " << a[i];
   }
   cout << endl;
   return 0;
}

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

#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, n) for(ll i = 1; i < n + 1; i++)
#define PI 3.14159265359
#define EPS 0.0000000001
#define MOD 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;

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

    multiset<string> mp;
    string s;
    string S[N];
    rep(i, N){
        cin >> s;
        S[i] = s;
        mp.insert(s);
    }

    ll M;
    cin >> M;
    multiset<string> mn;
    rep(i, M){
        cin >> s;
        mn.insert(s);
    }

    ll ans = 0;
    ll count;
    rep(i, N){
        count = mp.count(S[i]) - mn.count(S[i]);
        ans = max(ans, count);
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector< pair< ll,ll> >
#define plli pair< ll,ll >
#define vps vector< pair< string, string> >
#define vpi vector< pair< int, int> >
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define forn(i,a,n) for(ll i=a;i<n;i++)
#define forr(i,n,a) for(ll i=n-1;i>=a;i--)
#define scan(arr,a,n) for(ll i=(a);i<(n);i++)cin>>(arr)[i];
#define print(arr,a,n) for(ll i=(a);i<(n);i++)cout<<(arr)[i]<<" ";

const ll inf = 1e18;

ll add(ll x, ll y) {ll res = x + y; return (res >= mod ? res - mod : res);}
ll mul(ll x, ll y) {ll res = x * y; return (res >= mod ? res % mod : res);}
ll sub(ll x, ll y) {ll res = x - y; return (res < 0 ? res + mod : res);}
ll power(ll x, ll y) {ll res = 1; x %= mod; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, mod - 2);}

int main(){
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    #endif
    ll n, x;
    cin>>n>>x;
    ll a[n];
    scan(a, 0, n);
    ll ans = 0;
    forn(i, 1, n){
        if(a[i]+a[i-1]>x){
            ll z = a[i-1]+a[i]-x;
            ans+=z;
            if(z>=a[i]){
                a[i] = 0;
                z-=a[i];
                a[i-1]-=z;
            } else {
                a[i]-=z;
            }

        }
    }
    cout<<ans<<endl;
}

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

int main() {
  int N, Q;
  string S;
  cin >> N >> Q >> S;
  vector<int> V(N,0);
  for (int i=1; i<N; i++) {
    if (S[i-1]=='A' && S[i]=='C') {
      V[i]=V[i-1]+1;
    }
    else {
      V[i]=V[i-1];
    }
    //cout << V[i]<< endl;
  }
  int l, r;
  for (int i=0; i<Q; i++) {
    cin >> l >> r;
    l--;
    r--;
    cout << V[r]-V[l] << endl;
  }
}


// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで
// -fsanitize=undefinedでオーバーフロー検出,オーバーフローする入力を投げるとよい
#include <bits/stdc++.h>
#define mod 1000000007
#define INF LLONG_MAX
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};
bool comp(pair<ll, ll> a, pair<ll , ll> b) {
    // 基本はfirstで比較
    if(a.first != b.first){
        // return a.first < b.first; // 昇順
        return a.first > b.first; // 降順
    }

    // それ以外はsecondで比較
    if(a.second != b.second){
        return a.second > b.second;
    }else{
        // どちらも同じ
        return true;
    }
}

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

    ll a,b,d,m,n,maxi=0,f=0,mini=INF,sum=0;
    string str,stra="CODEFESTIVAL2016";
    ll k;
    ll w,x,y;
    ll h;
    cin>>n;
    vector<ll> v(n);
    rep(i,n) cin>>v[i];
    map<ll,ll> m1;
    map<ll,ll> m2;
    set<ll> s1;
    set<ll> s2;
    rep(i,n){
        if(i%2) {
            s1.insert(v[i]);
            m1[v[i]]++;
        }else{
            s2.insert(v[i]);
            m2[v[i]]++;
        }
    }
    vector<pair<ll,ll>> p1(s1.size()+1);
    vector<pair<ll,ll>> p2(s2.size()+1);
    p1[s1.size()]=make_pair(0,-1);
    p2[s2.size()]=make_pair(0,-2);
    ll i=0;
    for(auto itr=s1.begin();itr!=s1.end();itr++,i++){
        p1[i]=make_pair(m1[*itr],*itr);
    }
    i=0;
    for(auto itr=s2.begin();itr!=s2.end();itr++,i++){
        p2[i]=make_pair(m2[*itr],*itr);
    }
    sort(all(p1),comp);
    sort(all(p2),comp);
    if(p1[0].second==p2[0].second){
        cout<<n-max(p1[1].first+p2[0].first,p1[0].first+p2[1].first)<<endl;
    }else{
        cout<<n-(p1[0].first+p2[0].first)<<endl;
    }

    return 0;
}

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

int main() {

	int n; cin >> n;
	if (n == 2) {
		int o, e; cin >> o >> e;
		if (o == e) cout << 1 << endl;
		else cout << 0 << endl;
		return 0;
	}
	const int p = n / 2;
	const int z = 100001;
	vector<int> o_num(z, 0), e_num(z, 0);
	for (int i = 0; i < n; i++) {
		int v; cin >> v;
		if (i % 2 == 0) o_num[v]++;
		else e_num[v]++;
	}
	int o_firstCnt = 0;
	int	o_secondCnt = 0;
	int o_firstCntValue = 0;
	int e_firstCnt = 0;
	int	e_secondCnt = 0;
	int e_firstCntValue = 0;
	for (int i = 0; i < z; i++) {
		if (o_firstCnt <= o_num[i]) {
			o_secondCnt = o_firstCnt;
			o_firstCnt = o_num[i];
			o_firstCntValue = i;
		}
		else {
			o_secondCnt = max(o_secondCnt, o_num[i]);
		}
		if (e_firstCnt <= e_num[i]) {
			e_secondCnt = e_firstCnt;
			e_firstCnt = e_num[i];
			e_firstCntValue = i;
		}
		else {
			e_secondCnt = max(e_secondCnt, e_num[i]);
		}
	}
	int ans;
	if (o_firstCntValue != e_firstCntValue) {
		ans = n - o_firstCnt - e_firstCnt;
	}
	else { // o_firstCntValue == e_firstCntValue
		ans = min(n - o_secondCnt - e_firstCnt, n - o_firstCnt - e_secondCnt);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 1e9 + 7;

int N, K;
vector<int> D;

void dfs(int n) {
    bool flag = false;
    int tmp = n;
    while (tmp > 0) {
        for (int i = 0; i < K; i++) {
            if (tmp % 10 == D[i]) {
                flag = true;
                break;
            }
        }
        if (flag == true) break;
        tmp /= 10;
    }
    if (flag == true) dfs(n + 1);
    if (flag == false) cout << n << endl;
}

int main() {
    cin >> N >> K;
    for (int i = 0; i < K; i++) {
        int d;
        cin >> d;
        D.push_back(d);
    }
    dfs(N);
    return 0;
}

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(a)  (a).begin(),(a).end()
#define endl "\n"

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

void solve() {
    int N,K;
    cin >> N >> K;
    set<int> D;
    REP(_,K) {
        int d;
        cin >> d;
        D.insert(d);
    }

    FOR(val,N,100000) {
        string s = to_string(val);
        bool ok = true;
        for(const auto &it: s) if(D.count(((int)it-'0'))) ok = false;
        if(!ok) continue;
        cout << val << endl;
        return;
    }
}

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


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

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

  vector<ll> ay;
  vector<ll> by;

  for(ll i = 1; i <= sqrt(a);i++){
    if(a%i == 0){
      ay.push_back(i);
      if(i != a/i){
        ay.push_back(a/i);
      }
    }
  }
  for(ll i = 1; i <= sqrt(b);i++){
    if(b%i == 0){
      by.push_back(i);
      if(i != b/i){
        by.push_back(b/i);
      }
    }
  }
  //cout << ay.size() << " " << by.size() << endl;
  vector<ll> kys;
  for(int i = 0;i<ay.size();i++){
    for(int j = 0;j<by.size();j++){

      if(ay[i]==by[j]){
        kys.push_back(ay[i]);
        //cout << ay[i] << " " << by[j]<< endl;
      }
    }
  }

  int res = 0;
  int flg = 0;
  sort(kys.begin(),kys.end(),greater<ll>());
  for(int i = 0;i<kys.size();i++){
    flg = 0;
    for(int j = i+1;j<kys.size();j++){
      if(__gcd(kys[i],kys[j]) != 1){
        flg = 1;
        break;
      }
    }
    if(flg == 0){res++;}
  }

  cout << res;
}

#include <stdio.h>
int main(void){
int A,B;
scanf ("%d %d",&A,&B);
if (B+2<=A) printf ("%d",A+(A-1));
if (A+2<=B) printf ("%d",B+(B-1));
else {
    if (A==B+1||B==A+1||A==B) printf ("%d",A+B);
}
}


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

int main(){
  int N;
  cin >> N;
  int cpN = N;
  int sum = 0;

  while(cpN > 0){
    sum += cpN % 10;
    cpN /= 10;
  }

  if(N % sum == 0)cout << "Yes" << endl;
  else cout << "No" << endl;

}

#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f),(t)-(f)))
#define ALL(a) (a).begin(),(a).end()
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;cin>>s;
  for(int i=0;i<s.size()-1;i++){
    if(s[i] == 'A' && s[i+1] == 'C'){
      cout<<"Yes"<<endl;
      return 0;
    }
  }
  cout<<"No"<<endl;
  return 0;
}


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

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

template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }

ll a[16][16];
ll v[1 << 16]; // value of that submask
ll dp[1 << 16];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

	int n; read(n);
	rep(i, 0, n) rep(j, 0, n) read(a[i][j]);

	for (int b = 0; b < (1 << n); b++) {
		for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if ((b >> i & 1) && (b >> j & 1))
			v[b] += a[i][j];
	}

	dp[0] = 0;
	for (int i = 1; i <= n; i++) for (int j = 0; j < (1 << n); j++) if (__builtin_popcount(j) == i) {
		dp[j] = v[j];
		for (int k = j; k > 0; k = (k - 1) & j) {
			chmax(dp[j], dp[j ^ k] + v[k]);
		}
	}

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

#include<bits/stdc++.h>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
typedef long long ll;
using namespace std;
const int maxn=2e3+5;
char S[maxn];
int s[maxn][maxn],a[maxn][maxn];
int i,j,H,W,ans,h[maxn][maxn],t,q[maxn][2],cnt,len;
int main(){
	scanf("%d%d",&H,&W);
	fo(i,1,H) {
		scanf("%s",S+1);
		fo(j,1,W) s[i][j]=S[j]=='#';
	}
	ans=max(H,W);
	if (H==1||W==1) return printf("%d\n",ans),0;
	H--,W--;
	fo(i,1,H){
		cnt=0;
		fo(j,1,W){
			t=s[i][j]^s[i][j+1]^s[i+1][j]^s[i+1][j+1]^1;
			len=h[i][j]=(t)?h[i-1][j]+1:0;
			for(;cnt&&q[cnt][0]>len;cnt--) ans=max(ans,(q[cnt][0]+1)*(j-q[cnt-1][1]));
			q[++cnt][0]=len,q[cnt][1]=j;
		}
		for(;cnt;cnt--) ans=max(ans,(q[cnt][0]+1)*(W-q[cnt-1][1]+1));
	}
	printf("%d\n",ans);
}


#include<iostream>
#include<algorithm>
using namespace std;
int p[200000];

int main() {

	//C
	int n;
	cin >> n;
	int i;
	for (i = 0; i < n; i++) {
		cin >> p[i];
	}
	sort(p, p + n);
	for (i = 0; i < n-1; i++) {
		if (p[i] == p[i + 1]) {
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
	//D
	/*int n, k;
	cin >> n >> k;
	int i;
	for (i = 0; i < n; i++) {
		cin >> p[i];
	}
	int max = 0;
	int sum = 0;
	for (i = 0; i < k; i++) {
		sum = sum + p[i];
	}
	max = sum;
	for (i = k; i < n; i++) {
		sum = sum + p[i] - p[i - k];
		if (sum > max)max = sum;
	}
	//cout << max + k << endl;
	double f = (double)(((double)max +(double) k) / 2);
	printf("%.10f\n", f);
	//cout << (max + k) / 2 << endl;
	return 0;
	*/
}


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

typedef long long ll;

int main()
{
    string str;
    int A, B, C, D;
    int N[15];

    cin >> str;

    transform(begin(str), end(str), N, [](char c) {return c - '0'; });
    A = N[0];
    B = N[1];
    C = N[2];
    D = N[3];

    for (int i=0;i<2;++i) {
        for (int j=0;j<2;++j) {
            for (int k=0;k<2;++k) {
                int ans =0;
                string op1 = (i%2==0) ? "+" :"-";
                string op2 = (j%2==0) ? "+" :"-";
                string op3 = (k%2==0) ? "+" :"-";
                if (op1=="+") {
                    ans =A+B;
                }
                else {
                    ans = A-B;
                }
                if (op2=="+") {
                    ans +=C;
                }
                else {
                    ans -= C;
                }
                if (op3=="+") {
                    ans +=D;
                }
                else {
                    ans -= D;
                }
                if (ans==7) {
                    cout << A<<op1<<B<<op2<<C<<op3<<D<<"="<<7<<endl;
                    return 0;
                }

            }

        }
    }

}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int now[4];
char C[4], op[2] = {'+', '-'};

int main(){
  for (int i = 0; i < 4; i++){
    cin >> C[i];
    now[i] = C[i] - '0';
  }

  for (int i = 0; i < 8; i++){
    int val = now[0], num = i;

    for (int j = 1; j < 4; j++){
      val += pow(-1, num % 2) * now[j];
      num /= 2;
    }

    if (val == 7){
      num = i;
      cout << now[0];
      for (int j = 1; j < 4; j++){
        cout << op[num%2] << now[j];
        num /= 2;
      }

      cout << "=7" << endl;

      return 0;
    }

  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int n;
    cin>>s>>n;
    for(int i=0;i<s.size();i+=n)cout<<s[i];
    puts("");
}

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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 9e18
#define ceill(a, b) (a+b-1)/b
#define ok cout << "ok" << endl;
#define sp << " " <<
ll mod = 1e9+7;
void ans(bool b, ostream& out=cout){ out << (b ? "Yes" : "No") << endl; }
void ans2(bool b, ostream& out=cout){ out << (b ? "YES" : "NO") << endl; }
template<typename T> inline bool chmax(T &a, T b){ if(a<b){a=b; return true;} return false; }
template<typename T> inline bool chmin(T &a, T b){ if(b<a){a=b; return true;} return false; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){fill((T*)array, (T*)(array+N), val);}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

class debug
{
public:
    template<typename T> void vdeb(T vec);
    template<typename T1, typename T2> void adeb(T1 *arr, T2 n);
    template<typename T> void mdeb(T mp);
};

#endif //INCLUDE_DEBUG_HPP

template<typename T> void debug::vdeb(T vec)
{
    cout << "#vector set debug" << endl;
    for(auto vv : vec) cout << vv << " ";
    cout << endl;
}

template<typename T1, typename T2> void debug::adeb(T1 *arr, T2 n)
{
    cout << "#adebug" << endl;
    for(int i=0; i<=n; i++) cout << arr[i] << " ";
    cout << endl;
}

template<typename T> void debug::mdeb(T mp)
{
    cout << "#map pair debug" << endl;
    for(auto const& m : mp) cout << m.first sp m.second << endl;
}
debug dbg;

class BAcrostic {
public:
	void solve(std::istream& in, std::ostream& out)
	{
	    string s;
	    int w;
	    in >> s >> w;

	    string res = "";
	    for(int i=0; i<s.length(); i++){
            if(i%w == 0) res += s[i];
	    }

	    out << res << endl;
	}
};

int main() {
	BAcrostic solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i) res.push_back(n / i);
    }
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  auto divs = divisor(n);
  sort(divs.begin(), divs.end());
  ll left = -1;
  ll right = divs.size();
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    ll v1 = divs[mid];
    ll v2 = n / divs[mid];
    if (to_string(v1).size() >= to_string(v2).size())
      right = mid;
    else
      left = mid;
  }
  ll v1 = divs[right];
  ll v2 = n / divs[right];
  cout << max(to_string(v1).size(), to_string(v2).size()) << endl;
  return 0;
}

#include<stdio.h>

int main(){
    char str[15];
    scanf("%s", str);
    str[3] = '8';
    printf("%s\n", str);

    return 0;

}


#include <bits/stdc++.h>
 using namespace std;
  int main(){
      int n;
      cin >> n;
      int ans=0;
      for(int i=n;i!=0;i--){
           int a=sqrt(i);
          if(a*a==i){
              ans=i;
              break;
          }
      }
      cout << ans << endl;

  }

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(Array) sort(Array.begin(), Array.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define out(ans) cout << ans << endl;
const int MOD = 1000000007;
const int INF = 2147483647;
const ld PI = 3.14159265358979323846;

//------------↓------------- M -------------- E ---------------- M --------------- O ---------------↓--------------//
// コンパイル
// g++ -std=c++1z
//
// -------型変換--------
// int を string に変換
// string s = to_string(n);
// string を int に変換
// int n = stoi(s);
//
// -------二分探索---------
// k以上の値が最初に現れる時のイテレータ
// lower_bound(data.begin(), data.end(), k)
// kより大きい値が最初の現れる時のイテレータ O(logN)
// upper_bound(data.begin(), data.end(), k)
// kがdataに存在するかをtrue or falseで返す O(logN)
// binary_search(data.begin(), data.end(), k)
//
//
//
//
//
//
//
//------------↑------------- M -------------- E ---------------- M --------------- O ---------------↑--------------//

int main() {

    ll N; cin >> N;
    ll ans = 0;
    for (ll i = 1; i*i <= N; ++i) {
        ans = max(ans, i*i);
    }

    out(ans);

    return 0;
}


#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,ans=0x3f3f3f3f;
int a[100000+5];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n-m+1;i++){
		int j=m+i-1;
		ans=min(ans,min(abs(a[i])+abs(a[j]-a[i]),abs(a[j])+abs(a[j]-a[i])));
	}
	if(ans==0x3f3f3f3f) cout<<0<<endl;
	cout<<ans<<endl;
}

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

using namespace std;

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

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back
#define REP(i,a,b) for(int i=a; i<=b; i++)
#define MOD 1000000007

void solve(int test_case) {//209
	int n,k;
	cin>>n>>k;
	k--;
	vi arr(n);
	for(int &i:arr)cin>>i;
	int mi = INT_MAX;
	REP(i,0,n-k-1) {
		if(arr[i+k]<0){
			mi=min(mi, -arr[i]);
		} else if(arr[i]<0) {
			mi = min(mi, min(-2*arr[i]+arr[i+k],2*arr[i+k]-arr[i]));
		} else mi=min(mi, arr[i+k]);
	}
	cout<<mi;
}

int main() {

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

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


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

using namespace std;

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

typedef long long ll;

#define pb push_back

const int MOD = 1e9 + 7;
const int N = 1e5;

int dp[N][100];

void add(int& x, int y) {
	x += y;
	if (x > MOD) x -= MOD;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	string a;
	cin >> a;
	int b;
	cin >> b;
	int k = a.size();
	dp[0][0] = 1;
	for (int i = 1; i < k; i++)
		for (int j = 0; j < b; j++)
			for(int x = 0; x < 10; ++x)
				add(dp[i][j], dp[i - 1][((j - x) % b + b) % b]);
	int ans = 0;
	int sum = 0;
	for (int i = 0; i < k; i++) {
		for (int j = 0; j < (a[i] - '0'); j++)
			add(ans, dp[k - i - 1][((b - sum - j) % b + b) % b]);
		sum += (a[i] - '0');
		sum %= b;
	}
	if(sum == 0)
		ans++;
	cout << ans - 1;
	return 0;
}
// ez

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T> using v2 = vector<vector<T>>;
template<typename T> inline v2<T> fill(int r, int c, const T& t){ return v2<T>(r, vector<T>(c, t)); }
#define F first
#define S second

void solve(){
    int n;
    string s;
    cin >> n >> s;
    vector<int> r(n), g(n), b(n);
    r[0] = (s[0]=='R');
    for(int i = 1; i < n; ++i) r[i] = r[i-1]+(s[i]=='R');
    g[0] = (s[0]=='G');
    for(int i = 1; i < n; ++i) g[i] = g[i-1]+(s[i]=='G');
    b[0] = (s[0]=='B');
    for(int i = 1; i < n; ++i) b[i] = b[i-1]+(s[i]=='B');

    ll ans = 0;
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if((s[i]=='R' && s[j]=='G') || (s[i]=='G' && s[j]=='R')){
                ans += b[n-1]-b[j];
                if(2*j-i<n && s[2*j-i]=='B') --ans;
            }
            else if((s[i]=='R' && s[j]=='B') || (s[i]=='B' && s[j]=='R')){
                ans += g[n-1]-g[j];
                if(2*j-i<n && s[2*j-i]=='G') --ans;
            }
            else if((s[i]=='G' && s[j]=='B') || (s[i]=='B' && s[j]=='G')){
                ans += r[n-1]-r[j];
                if(2*j-i<n && s[2*j-i]=='R') --ans;
            }
        }
    }
    cout << ans << '\n';
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#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 n, m;
    cin >> n >> m;
    vector<ll> s(n+1);
    rep(i,n) {
        ll a;
        cin >> a;
        s[i+1] = (s[i] + a)%m;
    }
    ll ans = 0;
    map<int,ll> mp;
    for (int i = 0; i <= n; i++) {
        ans += mp[s[i]];
        mp[s[i]]++;
    }
    cout << ans << endl;
    return 0;
}

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

int main(){
    ll N,M;
    cin >> N >> M;
    vector<ll> vec(N + 1);
    vec[0] = 0;
    rep(i,N){
        cin >> vec[i + 1];
    }
    rep(i,N){
        vec[i + 1] += vec[i];
    }
    rep(i,N + 1){
        vec[i] %= M;
    }
    sort(all(vec));
    ll ans = 0;
    ll count = 1;
    vec.push_back(-1);
    rep(i,N + 1){
        if (vec[i + 1] == vec[i]) count++;
        else{
            ans += (count - 1) * count / 2;
            count = 1;
        }
    }
    cout << ans << endl;
}