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

int main() {
  int N;
  cin >> N;
  for (int i = N; i > 0; i--) {
    double a = sqrt(i);
    if (ceil(a) == floor(a)) {
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    ll n,m;
    cin>>n;
    m=sqrt(n);
    cout<<m*m<<endl;

    return 0;
}


#include <iostream>

using namespace std;

int main(){
  int a,b;
  cin >> a >> b;
  if(a>8)cout << ":(";
  else if(b>8)cout << ":(";
  else cout << "Yay!";
}

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
using ld = long double;
const int MOD = 1e9+7;

int main() {
    int a, b; cin >> a >> b;
    cout << max({a+b, a*b, a-b}) << endl;

    return 0;
}


#include <iostream>
using namespace std;

#include <algorithm>

int main() {
	int n;
	while (1) {
		cin >> n;
		if (n==0) break;
		int p1=0, p2=0;
		int c1, c2;
		for (int i=0; i<n; i++) {
			cin >> c1 >> c2;
			if (c1==c2) p1+=c1, p2+=c2;
			if (c1>c2) p1+=c1+c2;
			if (c1<c2) p2+=c1+c2;
		}
		cout << p1 << " " << p2 << endl;
	}
	return 0;
}

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

  int n;
  string str;
  cin >> n;

  for(int i=0;i<n;i++){

    int out = 0;
    int a[4] = {};

    while(out != 3){

      cin >> str;

      if(str == "OUT") out++;
      else if(str == "HOMERUN"){
	a[3] += a[0]+a[1]+a[2]+1;
	a[0] = a[1] = a[2] = 0;
      }
      else if(str == "HIT"){
	a[3] += a[2];
	a[2] = 0;
	a[2] = a[1];
	a[1] = 0;
	a[1] = a[0];
	a[0] = 1;
      }

    }
    cout << a[3] << endl;
  }
  return (0);
}


#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define ar array<int,2>
#define inf 1000000000000000000
int mod=1e9+7;
int min(int a,int b){return (a<b)?a:b;}
int max(int a,int b){return (a>b)?a:b;}
int fp(int a,int b){if(b==0) return 1;int x=fp(a,b/2);x=(x*x)%mod;if(b&1) x=(x*a)%mod;return x;}

const int N=1e4+5;
int d;
string s,tmp;
int sz;

int dp[N][2][101];
int pw[N];

void solve(int pos,int lm)
{

  if(dp[pos][lm][0]!=-1)
        return;

    if(pos==s.size())
    {
    //    cout<<tmp<<endl;
        for(int i=0;i<d;i++)
        dp[pos][lm][i]=0;
        dp[pos][lm][0]=1;
        return;
    }

    for(int i=0;i<d;i++)
        dp[pos][lm][i]=0;

    int l=0;
    int r=9;

    if(lm)
        r=s[pos]-'0';

    for(int i=l;i<=r;i++)
    {
       // tmp.push_back(i+'0');
        int nlm=0;
        if(lm&&i==r)
            nlm=1;
        solve(pos+1,nlm);
        for(int j=0;j<d;j++)
        {
            int x=(i+j)%d;
            dp[pos][lm][x]+=dp[pos+1][nlm][j];
            if(dp[pos][lm][x]>=mod)
                dp[pos][lm][x]-=mod;
        }
    }
}

signed main() {
 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 memset(dp,-1,sizeof(dp));
 int n;
 cin>>s>>d;
 sz=s.size();
 pw[0]=1;
 for(int i=1;i<=sz;i++)
    pw[i]=(pw[i-1]*10)%d;
 solve(0,1);
 int ans=(dp[0][1][0]-1+mod)%mod;
 cout<<ans<<endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
using namespace std;
const long long INF = 1LL<<60; // 仮想的な無限大の値;
using ll = long long;
using P  = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main()
{
    int N;
    string S;
    cin >> N >> S;
    ll cnt[3] {0, 0,0};

    rep(i, N){
        if(S[i] == 'R') {cnt[0]++;}
        if(S[i] == 'G') {cnt[1]++;}
        if(S[i] == 'B') {cnt[2]++;}
    }

    ll tot = cnt[0]*cnt[1]*cnt[2];
    rep(i, N)rep2(j, i+1, N){
        int k = j+j-i;
        if(k < N)
        {
            if(S[i] == S[j]) continue;
            if(S[k] == S[j]) continue;
            if(S[i] == S[k]) continue;
            --tot;
        }
    }
    cout << tot << endl;
    return 0;
}


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

vector<int>adj[100003],leaf;
int par[100003],depth[100005],sparse[100005][20],root=0,lz=0;

int lca(int p,int q)
{

    int a,b,c,d,lp,i,j;

    if(depth[p]<depth[q])
    {
        c=p; p=q; q=c;
    }

    for(j=19;j>=0;j--)
    {
         if((depth[p]-(1 << j))>=depth[q])
         {
             p=sparse[p][j];  if(depth[p]==depth[q])break;
         }
    }

    if(p==q){ return p; }
    else
    {
         for(j=19;j>=0;j--)
         {
             if(sparse[p][j]!=0 && sparse[p][j]!=sparse[q][j])
             {

                 p=sparse[p][j]; q=sparse[q][j];

                 if(par[p]==par[q])break;
             }

         }

         return par[p];
    }

}

void dfs(int src)
{
    int sz=adj[src].size();

    if(sz==1 && src!=root){   leaf.push_back(src);  lz++;   }

    for(int lp=0;lp<sz;lp++)
    {
        int u=adj[src][lp];

        if(u!=root && par[u]==0)
        {
            par[u]=src; sparse[u][0]=src; depth[u]=depth[src]+1;
            dfs(u);

        }

    }

}

int main()
{

    int n,m,a,b,i,j,u,v,ans,z,x,y,fr,sc,tr,w;
    scanf("%d %d %d",&n,&u,&v);

    for(i=1;i<=n-1;i++)
    {
        scanf("%d %d",&a,&b);
        adj[a].push_back(b); if(root==0){  if(adj[a].size()==2)root=a;  }
        adj[b].push_back(a); if(root==0){  if(adj[b].size()==2)root=b;  }

    }

    if(n==2)printf("0\n");
    else
    {
       dfs(root);

       for(j=1;j<=19;j++)
       {
           for(i=1;i<=n;i++)
           {
               if(sparse[i][j-1]!=0)
               {
                   sparse[i][j]=sparse[sparse[i][j-1]][j-1];
               }
           }
       }

       m=lca(u,v);

       ans=(depth[v]-depth[m])+(depth[u]-depth[m]); ans=ans-1;

       for(i=0;i<lz;i++)
       {
           y=leaf[i]; m=lca(u,y); w=lca(v,y);

           fr=(depth[u]-depth[m])+(depth[y]-depth[m]);
           sc=(depth[v]-depth[w])+(depth[y]-depth[w]);

           if(fr<sc)
           {
               if(sc-1>ans)ans=sc-1;
           }

       }

       printf("%d\n",ans);

    }

    return 0;
}

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

int main(){
  int h, w;
  cin >> h >> w;

  map<int,int> ns; //（現在地、スタート地点の最大値）
  multiset<int> dist; //現在地ースタート地点の最大値

  rep(i,w) ns[i] = i; //キーiにiを代入
  rep(i,w) dist.insert(0); //はじめは距離0

  rep(i,h){
    int a, b;
    cin >> a >> b;
    --a; --b;

    auto it = ns.lower_bound(a);
    int ms = -1; //最も右にある開始点を保持
    while(1){
      //壁を1だけ超えるスタート地点を取得（b+1）
      if(it == ns.end() || it -> first > b+1) break;
      ms = max(ms, it -> second);
      dist.erase(dist.find(it->first - it->second));
      ns.erase(it++);
    }
    //要素が見つからない、wを超えてしまう時を除く。
    if(ms != -1 && b+1 != w){
      dist.insert(b+1 - ms);
      ns[b+1] = ms;
    }

    int ans = -1;
    if(dist.size() > 0){
      ans = *dist.begin();
      ans += i+1;
    }
    cout << ans << endl;
  }

  return 0;
}


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

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

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

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

char Decryption(int k, char s) {
  char station[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
                    'n','o','p','q','r','s','t','u','v','w','x','y','z',
                    'A','B','C','D','E','F','G','H','I','J','K','L','M',
                    'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  int num;

  for(int i = 0; i < 52; i++) {
    if(s >= 'a' && s <= 'z')
      num = s - 'a';
    else
      num = s - 39;
  }
  return station[(52 + num - k) % 52];
}

int main() {
  vector<int> K;
  int n;
  string s_in;
  string s_out;

  while(1) {
    cin >> n;
    if(n == 0)
      break;

    for(int i = 0; i < n; i++) {
      int k;
      cin >> k;
      K.push_back(k);
    }

    cin >> s_in;

    for(int i = 0; i < s_in.size(); i++) {
      s_out += Decryption(K[i%n], s_in[i]);
    }

    cout << s_out << endl;

    s_out.erase();
    K.clear();
  }
  return 0;
}


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

int main() {

  int N,Y;
  cin >> N >> Y;

  int ansi=-1, ansj=-1, ansk=-1;

  for(int i=0; i<=N; i++){
    for(int j=0; j<=N-i; j++){
      int k = N-i-j;
      int total = i*10000 + j*5000 + k*1000;
      if(total == Y){
        ansi = i;
        ansj = j;
        ansk = k;
      }
    }
  }

  cout << ansi << " " << ansj << " " << ansk << endl;
}

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

using namespace std;

int main() {
  int64_t N, M;
  cin >> N >> M;
  int64_t res = 1;
  for (int64_t m = 1; m <= sqrt(M); ++m) {
    if (M % m) continue;
    int64_t m2 = M / m;
    if (m >= N) res = max(res, m2);
    if (m2 >= N) res = max(res, m);
  }
  cout << res << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long i,j,k,l,m,n,o,p,q;
    while(cin>>m>>n>>o>>p)
    {
        if(m+n>o+p)
            cout<<"Left"<<endl;
        else if(m+n<o+p)
            cout<<"Right"<<endl;
        else
            cout<<"Balanced"<<endl;
    }
    return 0;
}

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

#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second

using ll = long long;
using P = pair<ll, ll>;

/*追加部分*/

/*main関数*/

int main() {
FASTIO;
    int a, b, c, d; cin >> a >> b >> c >> d;

    cout << ((a + b) > (c + d) ? "Left" : (a + b) == (c + d) ? "Balanced" :  "Right") << endl;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define dbg(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

vector<int> g[111111];
int p[111111];
ll ans;
ll n,k;

int dfs(int n){
  int ma=0;
  for(int i=0;i<g[n].size();i++){
    ma=max(ma,dfs(g[n][i]));
  }
  if(n==0)return 0;
  if(ma==k-1&&p[n]!=0){
    ans++;
    return 0;
  }
  return ma+1;
}

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

  for(int i=0;i<n;i++){
    cin>>p[i];
    p[i]--;
    if(i==0&&p[i]!=0){
	 ans++;
	 p[i]=0;
    }
    if(i){
	 g[p[i]].pb(i);
    }
  }

  dfs(0);

  cout<<ans<<endl;

  return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
int n,m;
ll x,sum,ans;
map<ll,int>mmp;
int main()
{
    scanf("%d%d",&n,&m);
    sum=0;
    ans=0;
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&x);
        sum+=x;
        ans+=mmp[sum%m];
        if(sum%m==0) ans++;
        mmp[sum%m]++;
    }
    printf("%lld\n",ans);
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<cmath>
#include<iomanip>
using namespace std;

int main(void){
	int N,A[1000],c,hokan,flag,j,i,kaisuu;
	cin>>N;
	for(c=0;c<N;c++){
		cin>>A[c];
	}
	flag=1;
	i=0;
	kaisuu=0;
	while(flag){
		flag=0;
		for(j=N-1;i<j;j--){
			if(A[j]<A[j-1]){
				hokan=A[j];
				A[j]=A[j-1];
				A[j-1]=hokan;
				flag=1;
				kaisuu++;
			}
		}i++;
	}	for(c=0;c<N;c++){
		cout<<A[c];
		if(c!=N-1){
			cout<<" ";
		}else{
			cout<<endl;
		}
	}cout<<kaisuu<<endl;
	return 0;
}

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
const int MAX=100000;
const int geta=100;
struct BIT
{
	int bit[MAX+1];
	void add(int i,int x)
	{
		while(i<=MAX)
		{
			bit[i]+=x;
			i+=i&-i;
		}
	}
	int sum(int i)
	{
		int res=0;
		while(i>0)
		{
			res+=bit[i];
			i-=i&-i;
		}
		return res;
	}
};
BIT bit;
int N,a[105];
int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		scanf("%d",&a[i]);
		a[i]+=geta;
	}
	int ans=0;
	for(int i=0;i<N;i++)
	{
		ans+=bit.sum(a[i]);
		bit.add(a[i],1);
	}
	sort(a,a+N);
	for(int i=0;i<N;i++)printf("%d%c",a[i]-geta,(i==N-1)?'\n':' ');
	printf("%d\n",N*(N-1)/2-ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

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

  int eated_apple = 10000000;
  int sum_taste = 0;
  for(int i=1; i<=N; i++){
    sum_taste += L+i-1;
    if(abs(eated_apple)>abs(L+i-1)) eated_apple = L+i-1;
  }

  cout << sum_taste-eated_apple << endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
const long long int sMod = 998244353;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";};
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

void solve(){
    ll n;
    ll l;
    cin >> n >> l;
    if(l>=0) print((l+l+n-1)*n/2-l);
    else if(l+n-1>=0) print((l+l+n-1)*n/2);
    else print((l+l+n-1)*n/2-(l+n-1));
    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,t,f,ans;
double a[200001];
map<pair<int,int>,int> m;
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lf",&a[i]);
		int b=round(a[i]*1000000000);
		t=f=-9;
		while(b%2==0)
		{
			b/=2;
			t++;
		}
		while(b%5==0)
		{
			b/=5;
			f++;
		}
		m[make_pair(t,f)]++;
	}
	for(auto i:m)
		for(auto l:m)
			if(i.first.first+l.first.first>=0&&i.first.second+l.first.second>=0)
			{
				if(i==l)
					ans+=i.second*(i.second-1)/2;
				if(i<l)
					ans+=i.second*l.second;
			}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int cnt[80][50];
int cnt2[200100], cnt5[200100];

char str[50];

long long inp()
{
    memset(str, 0, sizeof(str));
    scanf("%s", str);
    long long ret = 0;
    int point = 0;
    int pointflag = 0;
    for(int i = 0; str[i] ; ++i)
    {
        if(pointflag)
        {
            point++;
        }
        if(str[i] == '.'){
            pointflag = 1;
        }else{
            ret = ret*10+str[i]-'0';
        }
    }
    while(point < 9) ret *= 10, point++;
    return ret;
}

int main()
{
#ifdef CitricAcid
    freopen ("input.txt", "rt", stdin);
#endif
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        long long xint = inp();
        while(xint%2 == 0) cnt2[i]++, xint /= 2;
        while(xint%5 == 0) cnt5[i]++, xint /= 5;
        cnt[cnt2[i]][cnt5[i]]++;
    }
    for(int i = 64; i >= 0; --i)
        for(int j = 25; j >= 0; --j)
            cnt[i][j] = cnt[i][j] + cnt[i+1][j] + cnt[i][j+1] - cnt[i+1][j+1];
    long long ans = 0;
    for(int i = 1; i <= n; ++i)
    {
        int tmp2 = max(0, 18-cnt2[i]);
        int tmp5 = max(0, 18-cnt5[i]);
        ans += cnt[tmp2][tmp5];
        if(cnt2[i] >= tmp2 && cnt5[i] >= tmp5) ans--;
    }
    printf("%lld\n", ans/2);
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define pb push_back

int main(){
    int x,y,z;
    while(1){
        cin>>x>>y>>z;

        if(x==0&&y==0&&z==0)break;

        int a[114514];
        int b[114514];

        rep(i,x){
            cin>>a[i];
        }

        rep(i,z-y+1){
            b[i]=a[i+y-1]-a[i+y];
        }

        rep(i,z-y+1){
            // cout<<i<<" "<<b[i]<<endl;
        }

        int count=0;
        int tmp=0;
        int ans=0;

        for(int i=0;i<z-y+1;i++){
            if(tmp<=b[i]){
                tmp=b[i];
                ans=i+y;
            }
        }
        cout<<ans<<endl;

    //     repi(i,y-1,z){
    //         // cout<<a[i]<<endl;
    //         // cout<<a[i+1]<<endl;

    //         if(tmp<=a[i]-a[i+1]){
    //             tmp=max(tmp,a[i]-a[i+1]);
    //             ans=i-1+y;
    //         }

    //         // cout<<"tmp:"<<tmp<<endl;
    //         // cout<<"ans:"<<ans<<endl;

    //         count++;
    //     }
    //     // cout<<count<<endl;
    //     // cout<<"tmp:"<<tmp<<endl;
    //     // cout<<"ans:"<<ans<<endl;
    //     if(y==1){
    //         cout<<ans+1<<endl;
    //     }else{
    //         cout<<ans<<endl;
    //     }

    }

}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

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

  int e;
  while(cin >>e,e){
    int ans=1000000;
    for(int y=0;y*y<=e;y++){
      for(int z=0;z*z*z<=e-y*y;z++){
        int x=e-z*z*z-y*y;
        ans = min(ans,x+y+z);
      }
    }
    cout << ans << 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 i64 = long long;

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

    std::vector<std::tuple<int, int, int>> es;
    std::vector<std::pair<int, int>> w;
    for (int i = 0; i < m; i++) {
        int p, q, c;
      	scanf("%d%d%d", &p, &q, &c);
        --p; --q;
        es.emplace_back(p, q, c);
        w.emplace_back(p, c);
        w.emplace_back(q, c);
    }

    std::sort(w.begin(), w.end());
    w.erase(std::unique(w.begin(), w.end()), w.end());

    auto get = [&](int v, int c) {
        return n + std::lower_bound(w.begin(), w.end(), std::make_pair(v, c)) - w.begin();
    };

    std::vector<std::vector<std::pair<int, int>>> g(w.size() + n);
    for (const auto &e : w) {
        int v = get(e.first, e.second);
        g[e.first].emplace_back(v, 1);
        g[v].emplace_back(e.first, 0);
    }
    for (const auto &e : es) {
        auto p = std::get<0>(e), q = std::get<1>(e), c = std::get<2>(e);
        int pv = get(p, c), qv = get(q, c);
        g[pv].emplace_back(qv, 0);
        g[qv].emplace_back(pv, 0);
    }

    constexpr int inf = std::numeric_limits<int>::max() / 2;
    std::vector<int> dist(g.size(), inf);
    std::deque<int> q { 0 };
    dist[0] = 0;
    while (!q.empty()) {
        int v = q.front();
        q.pop_front();
        for (auto to : g[v]) {
            if (dist[to.first] <= dist[v] + to.second) continue;
            dist[to.first] = dist[v] + to.second;
            if (to.second) q.push_back(to.first);
            else q.push_front(to.first);
        }
    }

	printf("%d\n", dist[n - 1] == inf ? -1 : dist[n - 1]);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define f1(a,b,c) for(int c=a;c<=b;c++)
#define f2(a,b,c) for(int c=a;c>=b;c--)
#define f3(a,b,c) for(int c=a;c;c=b)
#define so1(a,n) sort(a+1,a+n+1,mycmp);
#define so2(a,n) sort(a+1,a+n+1);
#define ll long long
#define itn int
#define ubt int
#define pii pair<int,int>
#define mp make_pair
const int twx=1e6+100;
const int inf=0x7fffffff;
ll read()
{
    ll sum=0;
    ll flag=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
        {
            flag=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        sum=((sum*10)+c-'0');
        c=getchar();
    }
    return sum*flag;
}
int n,m;
struct LV
{
    int Next;
    int y;
    int v;
}a[twx<<1];
int Link[twx];
int len;
map<pii,int> p;
int cnt=0;
bool v[twx];
int d[twx];
void Insert(int x,int y,int z)
{
    a[++len].y=y;
    a[len].Next=Link[x];
    a[len].v=z;
    Link[x]=len;
}
int get(int x,int y)
{
    if(p.find(mp(x,y))!=p.end())
    {
        return p[mp(x,y)];
    }
    else
    {
        return p[mp(x,y)]=++cnt;
    }
}
void spfa()
{
    memset(d,0x3f,sizeof d);
    queue<int> q;
    q.push(1);
    v[1]=1;
    d[1]=0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        f3(Link[x],a[i].Next,i)
        {
            int y=a[i].y;
            int vs=a[i].v;
            if(d[y]>d[x]+vs)
            {
                d[y]=d[x]+vs;
                if(!v[y])
                {
                    v[y]=1;
                    q.push(y);
                }
            }
        }
        v[x]=0;
    }
}
void init()
{
	n=read();
    m=read();
    cnt=n;
    f1(1,m,i)
    {
        int x=read();
        int y=read();
        int z=read();
        int xz=get(x,z);
        int yz=get(y,z);
        Insert(xz,yz,0);
        Insert(yz,xz,0);
        Insert(x,xz,1);
        Insert(xz,x,1);
        Insert(y,yz,1);
        Insert(yz,y,1);
    }
    spfa();
    printf("%d\n",d[n]==0x3f3f3f3f?-1:d[n]/2);
}
void work()
{

}
void print()
{

}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
    init();
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0;

#define MAX 505

int n,m;

int main(){
  while(1){
    cin>>n>>m;
    vector<int> G[MAX];
    int num[n+5];
    int used[n+5];
    int ans=0;
    if(n==0&&m==0) break;
    for(int i=0; i<n+5; i++) for(int j=0; j<n+5; j++) {num[i]=INF; used[i]=0;}
    for(int i=0; i<m; i++){
      int a,b;
      cin>>a>>b;
      G[a].push_back(b);
      G[b].push_back(a);
    }

    queue<int> que;
    que.push(1);
    num[1]=0;
    used[1]=true;
    while(!que.empty()){
      int v=que.front(); que.pop();
      for(int i=0; i<G[v].size(); i++){
        int e=G[v][i];
        if(used[e]) continue;
        num[e]=num[v]+1;
        used[e]=true;
        que.push(e);
      }
    }

    for(int i=1; i<=n; i++){
      if(num[i]<=2) {
        ans++;
      }
    }

    cout<<ans-1<<en
  }
}

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

int main() {

	//ifstream cin("INPUT.txt");
	//ofstream cout("OUTPUT.txt");

	int n, m, sum, i;
	int a, b;

	while(cin >> n >> m, n != 0 && m != 0) {

	int friends[500] = {0}; //1=友達 2=友達の友達
	int m1[10000] = {0};
	int m2[10000] = {0};
	for(i = 0; i < m; i++) {
		cin >> m1[i] >> m2[i];
	}
	for(i = 0; i < m; i++) {
		a = m1[i];
		b = m2[i];
		if(friends[a-1] == 1 && friends[b-1] == 1) {
		} else if(a == 1) {
			friends[b-1] = 1;
		} else if(b == 1) {
			friends[a-1] = 1;
		} else if(friends[a-1] == 1) {
			friends[b-1] = 2;
		} else if(friends[b-1] == 1) {
			friends[a-1] = 2;
		}
	}
	for(i = 0; i < m; i++) {
		a = m1[i];
		b = m2[i];
		if(friends[a-1] == 1 && friends[b-1] == 1) {
		} else if(a == 1) {
			friends[b-1] = 1;
		} else if(b == 1) {
			friends[a-1] = 1;
		} else if(friends[a-1] == 1) {
			friends[b-1] = 2;
		} else if(friends[b-1] == 1) {
			friends[a-1] = 2;
		}
	}
	sum = 0;
	for(i = 0; i < n; i++) {
		if(friends[i] > 0)
			sum++;
	}

	cout << sum << endl;

	}

	return 0;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ld long double
#define al(a) (a).begin(),(a).end()
#define mk make_pair
#define check cout<<"?"<<endl;

ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;

int main(){
    string s; cin>>s;
    vector<int> v(26,0);
    for(auto ch:s) v[ch-'a']=1;
    rep(i,26){
        if(v[i]==0){
            cout<<(char)('a'+i)<<endl;
            return 0;
        }
    }
    cout<<"None"<<endl;
}

#include <iostream>
#include <vector>

template<typename T>
void Dump(const std::vector<T>& xs)
{
  for (int i = 0; i < xs.size()-1; ++i)
  {
    std::cout << xs[i] << ' ';
  }
  std::cout << xs.back() << std::endl;
}

template<typename T>
int BubbleSort(std::vector<T>* src)
{
  std::vector<T>& xs = *src;
  bool change {true};
  int count = {0};
  while (change) {
    change = false;
    for (int i = xs.size()-1; i > 0; --i)
    {
      if (xs[i] < xs[i-1]) {
        T buf = xs[i];
        xs[i] = xs[i-1];
        xs[i-1] = buf;

        change = true;
        ++count;
      }
    }
  }
  return count;
}

int main()
{
  int n {};
  std::vector<int> xs {};
  std::cin >> n;
  xs.resize(n);
  for (int i = 0; i < n; ++i)
  {
    std::cin >> xs[i];
  }

  int cnt{BubbleSort(&xs)};

  Dump(xs);
  std::cout << cnt << std::endl;

  return 0;
}


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

int main(){
int n;
int k;
cin>>n>>k;
if(n%k==0){
    cout<<0<<endl;
}
else{
    cout<<1<<endl;
}
return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD (int)1e9+7
#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 vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()

ll powM(ll a,ll b,ll m )
{
  a%=m;
  ll ans=1;
  while(b>0)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }

return ans;
}

ll c(ll a,ll b)
{
  if(a==0)return b==0?1:0;

if(b>a)return 0;

  ll val=1;
ll fac=1;
for(int i=1;i<=b;i++)fac*=i;
for(int i=a;i>=a-b+1;i--)val*=i;

  return val/fac;

}

ll f(ll n)
{
  ll ans=1LL;
  while(ans<=n)ans<<=1;

return ans;
}

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

ll n,k;
cin>>n>>k;
ll ans=0;
if(n%k)ans=1;

cout<<ans<<"\n";

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}
vp seg;
ll N=1;
void init(vi v){
    while(N<v.size())N*=2;
    seg=vp(N*2-1,P(-inf,-1));
    rep(i,v.size())seg[i+N-1]=P(v[i],i);
    for(int i=N-2;i>=0;i--)seg[i]=max(seg[i*2+1],seg[i*2+2]);
}
void add(ll i,ll x){
    i=N+i-1;
    seg[i].first+=x;
    while(i>0){
        i=(i-1)/2;
        seg[i]=max(seg[i*2+1],seg[i*2+2]);
    }
}
P getma(ll a,ll b,ll k,ll l,ll r){
    if(r<=a||b<=l)return P(-inf,-1);
    if(a<=l&&r<=b)return seg[k];
    P c1=getma(a,b,k*2+1,l,(l+r)/2);
    P c2=getma(a,b,k*2+2,(l+r)/2,r);
    return max(c1,c2);
}
int main(){
    ll n;cin>>n;
    vi a(n),b(n);
    rep(i,n)cin>>a[i]>>b[i];
    vp v(n);
    rep(i,n)v[i]=P(a[i]+b[i],i);
    sort(all(v));
    vi dp(n+1,inf);
    dp[0]=0;
    rep(i,n){
        for(int j=i;j>=0;j--){
            if(dp[j]<=a[v[i].se])chmin(dp[j+1],dp[j]+b[v[i].se]);
        }
    }
    for(int i=n;i>=0;i--)if(dp[i]!=inf)dame(i);
}

#include <bits/stdc++.h>

using namespace std;

const long long inf = (long long) 1e18;

const int N = 5010;

long long f[N][N];
 bool cmp(pair<int,int> a,pair<int,int> b){
return a.first < b.first;
}
int main() {
  int n;
  scanf("%d", &n);
  vector< pair<int,int> > a(n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i].first, &a[i].second);
    a[i].first += a[i].second;
  }
  sort(a.begin(), a.end(),cmp);
  for (int i = 0; i < n; i++) {
    a[i].first -= a[i].second;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      f[i][j] = inf;
    }
  }
  f[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      long long ft = f[i][j];
      if (ft == inf) {
        continue;
      }
      f[i + 1][j] = min(f[i + 1][j], ft);
      if (ft <= a[i].first) {
        f[i + 1][j + 1] = min(f[i + 1][j + 1], ft + a[i].second);
      }
    }
  }
  int ans = 0;
  for (int j = 0; j <= n; j++) {
    if (f[n][j] != inf) {
      ans = j;
    }
  }
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define INF 2e9
#define LINF 1e18
#define mod 1000000007
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main() {
    int n; cin >> n;
    vector<int> a(n),b(n),c(n);
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> b[i];
    rep(i,n) cin >> c[i];
    sort(all(a)); sort(all(c));
    ll ans = 0;
    rep(i,n) {
        ll anibu = lower_bound(all(a),b[i]) - a.begin(); //b[i]より小となる個数
        ll cnibu = c.end() - upper_bound(all(c),b[i]); //b[i]より大となる個数
        ans += anibu*cnibu;
    }
    cout << ans << endl;
}

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

int N;
int A[3][100001];
LL dp[3][100001] = {};

int main() {
    scanf("%d", &N);
    rep(j, 3) { rep(i, N) scanf("%d", &A[j][i]); A[j][N] = 1000000001; }
    rep(j, 3) sort(A[j], A[j] + N);
    rep(i, N + 1) dp[0][i] = i;
    rep(j, 2) {
        int p = 0;
        rep(i, N) {
            while (A[j][p] < A[j + 1][i]) p++;
            dp[j + 1][i + 1] += dp[j + 1][i] + dp[j][p];
        }
    }
    printf("%lld\n", dp[2][N]);
    return 0;
}


#include<cstdio>
#include<utility>
#include<algorithm>
#include<vector>
#include<queue>
#define MAX_N 262144
#define INF 1000000000000000
typedef long long ll;
using namespace std;

int used[MAX_N];
ll n,d,a[MAX_N],ans=0,two[20];
pair<ll,ll> p[MAX_N],t[2*MAX_N][2];
vector<pair<ll,ll> > g[MAX_N];
void init(){
    for(ll i=0;i<2*MAX_N;i++){
        t[i][0]=t[i][1]=make_pair(INF,i);
    }
    two[0]=1;
    for(int i=1;i<20;i++){
        two[i]=two[i-1]*2;
    }
}
void change(ll i){
    ll y=p[i].second+MAX_N;
    t[y][0].first=d*(y-MAX_N)+p[i].first;
    t[y][1].first=d*(n-1-y+MAX_N)+p[i].first;
    y/=2;
    while(y>0){
        t[y][0]=min(t[2*y][0],t[2*y+1][0]);
        t[y][1]=min(t[2*y][1],t[2*y+1][1]);
        y/=2;
    }
}
pair<ll,ll> smq(ll l,ll r,int y){
    ll cnt=1,x=l;
    if(l==r){
        return t[l][y];
    }
    while(x>0){
        if(x/2*two[cnt]==l&&(x/2+1LL)*two[cnt]-(1LL)==r){
            return t[x/2][y];
        }
        else if(x/2*two[cnt]<l||(x/2+1LL)*two[cnt]-(1LL)>r){
            return min(t[x][y],smq((x+1LL)*two[cnt-1],r,y));
        }
        x/=2;
        cnt++;
    }
    return t[x][y];
}
int main(){
    pair<ll,ll> x;
    priority_queue<pair<ll,ll> ,vector<pair<ll,ll>>, greater<pair<ll,ll>>> q;
    scanf("%lld %lld",&n,&d);
    for(ll i=0;i<n;i++){
        scanf("%lld",&a[i]);
        p[i]=make_pair(a[i],i);
    }
    init();
    sort(p,p+n);
    change(0);
    for(ll i=1;i<n;i++){
        x=smq(p[i].second+MAX_N,n-1+MAX_N,0);
        if(x.first<INF){
            g[p[i].second].push_back(make_pair(x.first-d*p[i].second+p[i].first,x.second-MAX_N));
            g[x.second-MAX_N].push_back(make_pair(x.first-d*p[i].second+p[i].first,p[i].second));
        }
        x=smq(MAX_N,p[i].second+MAX_N,1);
        if(x.first<INF){
            g[p[i].second].push_back(make_pair(x.first-d*(n-1-p[i].second)+p[i].first,x.second-MAX_N));
            g[x.second-MAX_N].push_back(make_pair(x.first-d*(n-1-p[i].second)+p[i].first,p[i].second));
        }
        change(i);
    }
    used[p[n-1].second]=1;
    for(int i=0;i<g[p[n-1].second].size();i++){
        q.push(g[p[n-1].second][i]);
    }
    while(!q.empty()){
        x=q.top();
        q.pop();
        if(used[x.second]==0){
            ans+=x.first;
            for(int i=0;i<g[x.second].size();i++){
                q.push(g[x.second][i]);
            }
            used[x.second]=1;
        }
    }
    printf("%lld\n",ans);
}

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli;
#define fir first
#define sec second
#define Mp make_pair
const int MAXN = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;

template<typename T>
void chk_min(T &a,T b){ if(a>b) a=b;}

ll a[MAXN];

int main(void)
{
	int n;
	ll d;
	scanf("%d%lld",&n,&d);
	for(int i=1; i<=n; ++i) scanf("%lld",&a[i]);

	ll ans=0;
	for(int i=1; i<=n; ++i) ans += a[i];

	for(int i=2; i<=n; ++i) chk_min(a[i], a[i-1]+d);
	for(int i=n-1; i>=1; --i) chk_min(a[i], a[i+1]+d);

	for(int i=1; i<=n; ++i) ans += a[i];
	printf("%lld",ans + d*(n-1) - a[1] - a[n]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//name[i][j]-> vvl name(i,vl(j))
//map<int,int>mp
//余りを切り上げる =>  (a + b - 1) / b
vi vx={-1,0,1,0};
vi vy={0,1,0,-1};
using pi = pair<int,int>;

signed main(){
	int a,b; cin>>a>>b;
	vvi v(a,vi(b));
	queue<pi> q;
	rep(i,0,a){
		rep(j,0,b){
			char c; cin>>c;
			if(c=='#'){
				v[i][j]=1;
				q.push(make_pair(i,j));
			}
			else v[i][j]=0;
		}
	}
	while(!q.empty()){
		pi p=q.front(); q.pop();
		int k,l; tie(k,l)=p;
		bool ok=false;
		rep(i,0,4){
			k+=vx[i]; l+=vy[i];
			if(k>=0 && k<a){
				if(l>=0 && l<b){
					if(v[k][l]==1)ok=true;
				}
			}
			k-=vx[i]; l-=vy[i];
		}
		if(!ok){
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;

	return 0;
}

#include <cstdio>
#include <iostream>
#include <vector>

#define lson (idx << 1)
#define rson (idx << 1 | 1)
#define INF 1000000000000LL
#define MAXN 200010

using namespace std;
typedef long long ll;
// `lo` is the current minimum of the segment.
// `lh` is the current value of the left endpoint.
// `lazy` is the lazy tag for addition.
struct Node {
	int l, r;
	ll lo;
	bool flag = false;
} s[MAXN << 2];
int h, w;
// Push up
void calc(int idx) {
	s[idx].lo = min(s[lson].lo, s[rson].lo);
}
void build(int idx, int l, int r) {
	s[idx].l = l, s[idx].r = r;
	if (l == r)	return;
	int mid = (l + r) >> 1;
	build(lson, l, mid);
	build(rson, mid + 1, r);
}
// Lazy propagation
void pushdown(int idx) {
	if (s[idx].flag) {
		ll L = s[idx].lo;
		int l = s[idx].l;
		for (int i = lson; i <= rson; ++i) {
			s[i].lo = L + s[i].l - l;
			s[i].flag = true;
		}
	}
	s[idx].flag = false;
}
// Assign segment [l, r] according to f[l-1] = L
void update(int idx, int l, int r, ll L) {
  if (s[idx].l >= l && s[idx].r <= r) {
    s[idx].lo = L + s[idx].l - l + 1;
    s[idx].flag = true;
    return;
  }
  pushdown(idx);
  int mid = (s[idx].l + s[idx].r) >> 1;
  if (l <= mid)
    update(lson, l, r, L);
  if (mid + 1 <= r)
    update(rson, l, r, L);
  calc(idx);
}
// Range minimum query
ll query(int idx, int l, int r) {
	if (r < 1 || l > w)
		return INF;
	if (s[idx].l >= l && s[idx].r <= r)
		return s[idx].lo;
	pushdown(idx);
	ll ans = INF;
	int mid = (s[idx].l + s[idx].r) >> 1;
	if (l <= mid)
		ans = min(ans, query(lson, l, r));
	if (mid + 1 <= r)
		ans = min(ans, query(rson, l, r));
	return ans;
}
void solve() {
	scanf("%d%d",&h,&w);
	vector<int> l(h), r(h);
	for (int i = 0; i < h; ++i)
		scanf("%d%d",&l[i],&r[i]);
	vector<ll> ans(h, INF);
	build(1, 1, w);
	for (int i = 0; i < h; ++i) {
		ll L = query(1, l[i] - 1, l[i] - 1);
		update(1, l[i], r[i], L);
		ans[i] = min(ans[i], query(1, 1, w))+i+1;
		if (ans[i] == INF)	break;
	}
	for (ll i : ans)	cout << (i >= INF ? -1 : i) << endl;
}
int main() {
	ios::sync_with_stdio(false);cin.tie(0);
	solve();
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
using namespace std;
long long n,d,a[200005],r[200005],l[200005];
int main()
{
	cin>>n>>d;
	long long ret=0;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		ret+=a[i];
	}
	ret+=(n-1)*d;
	//cout<<ret<<endl;
	r[0]=a[0];
	for(int i=1;i<n;i++)
	    r[i]=min(a[i],r[i-1]+d);
	l[n-1]=a[n-1];
	for(int i=n-2;i>=0;i--)
	    l[i]=min(a[i],l[i+1]+d);
	for(int i=1;i<n-1;i++)
	    ret+=min(l[i],r[i]);
	cout<<ret;
	return 0;
}

#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;

int main()
{
    int n;
    ll d;
    cin >> n >> d;
    ll a[200005];
    priority_queue<P, vector<P>, greater<P>> que;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        que.push(P(a[i], i));
    }
    bool used[200005]{0};
    ll ans = 0;
    while(que.size()){
        P p = que.top();
        que.pop();
        int u = p.second;
        if(u > 0 && !used[u]){
            ans += d + a[u] + a[u - 1];
            if(d + a[u] < a[u - 1]){
                a[u - 1] = d + a[u];
                que.push(P(a[u - 1], u - 1));
            }
            used[u] = true;
        }
        if(u < n - 1 && !used[u + 1]){
            ans += d + a[u] + a[u + 1];
            if(d + a[u] < a[u + 1]){
                a[u + 1] = d + a[u];
                que.push(P(a[u + 1], u + 1));
            }
            used[u + 1] = true;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
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;}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << endl;}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

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

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

int main(){
	ll n,m,k; cin >> n >> m >> k;
	comInit();
	ll sum = 0;
	REP(i,1,m){
		sum += n*n%mod*i%mod*(m-i)%mod;
		sum %= mod;
	}
	REP(i,1,n){
		sum += m*m%mod*i%mod*(n-i)%mod;
		sum %= mod;
	}
	cout << sum*com(n*m-2,k-2)%mod << endl;
}

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

using ll = long long;
using namespace std;
constexpr ll inf = 1e15;

#define MAX_N 1000

constexpr int MAX = 10000;
constexpr int MOD = 1e9+7;

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

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

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// dp[i][j] := 最大のグループ人数がj人であるとき、i人をグループ分けする場合の数
ll dp[MAX_N+1][MAX_N+1] = {0};

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

    int N, A, B, C, D;
    cin>>N>>A>>B>>C>>D;

    for (int i = 0; i <= B; i++) dp[0][i] = 1;

    COMinit();

    for (int n = 1; n <= N; n++) {
        for (int i = A; i <= B; i++) {
            ll comb = 1;
            dp[n][i] = dp[n][i-1];
            for (int j = 1; j < C; j++) comb = (((comb * COM(n - i * (j - 1), i)) % MOD) * inv[j]) % MOD;
            for (int j = C; j <= min(D, (n/i)); j++) {
                 ll F = i * j;
                 comb = (((comb * COM(n - i * (j - 1), i)) % MOD) * inv[j]) % MOD;
                 dp[n][i] = (dp[n][i] + (dp[n - F][i - 1] * comb % MOD)) % MOD;
            }
        }
    }

    cout<<dp[N][B]<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef double D;
typedef long long int LL;

#define st first
#define nd second
#define pb push_back
#define PLL pair <LL, LL>
#define PII pair <int, int>

const int N = 507;
const int MX = 1e9 + 7;
const LL INF = 1e18 + 9LL;

int n;
LL tab[N][N];
vector <int> primes;

LL nwd(LL a, LL b){
	if(b == 0)
		return a;
	return nwd(b, a%b);
}

LL get(LL a, LL b, LL c, LL d){
	LL ret = a / nwd(a, b) * b;
	ret = ret / nwd(ret, c) * c;
	ret = ret / nwd(ret, d) * d;
	return ret;
}

bool ok(int a){
	for(int i = 2; i * i <= a; ++i)
		if(a%i == 0)
			return false;
	return true;
}

int main(){
	scanf("%d", &n);
	if(n == 2){
		puts("2 3\n5 4");
		return 0;
	}

	for(int i = 2; primes.size() < n + n; ++i)
		if(ok(i))
			primes.push_back(i);

	for(int i = 0; i <= n + 1; ++i)
		for(int j = 0; j <= n + 1; ++j)
			tab[i][j] = 1;

	int sx = n - 1 + (n & 1), sy = 1;
	for(int i = 1; i <= n - 1 + (n & 1); ++i){
		int cx = sx, cy = sy;
		while(cx <= n && cy <= n){
			tab[cx][cy] *= primes.back();
			++cx, ++cy;
		}

		if(sx > 2)
			sx -= 2;
		else{
			if(sy == 1 && sx == 2)
				sx = 1, sy++;
			else
				sy += 2;
		}

		primes.pop_back();
	}

	sx = 1, sy = 1;
	for(int i = 1; i <= n; ++i){
		int cx = sx, cy = sy;
		while(cx <= n && cy >= 1){
			tab[cx][cy] *= primes.back();
			++cx, --cy;
		}

		if(sy + 1 < n)
			sy += 2;
		else{
			if(sx == 1 && sy + 1 == n)
				sy = n, ++sx;
			else
				sx += 2;
		}

		primes.pop_back();
	}

	for(int i = 1; i <= n; ++i){
		for(int j = 1; j <= n; ++j){
			if((i + j) & 1)
				tab[i][j] = get(tab[i - 1][j], tab[i][j - 1], tab[i + 1][j], tab[i][j + 1]) + 1;
			printf("%lld ", tab[i][j]);
		}

		puts("");
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define lor(a,b,c) for(register int a=b;a<=c;++a)
#define ror(a,b,c) for(register int a=c;a>=b;--a)
typedef long long ll;

const int MAXN=505,MAXD=7919;
const int movx[4]={-1,0,1,0},movy[4]={0,1,0,-1};

int n; ll a[MAXN][MAXN];
bool vis[MAXD]; ll prime[MAXN<<1];

inline ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}

inline ll lcm(ll a,ll b){
	if(!a||!b) return a|b;
	return a/gcd(a,b)*b;
}

inline void judge(){
	int x,a[505][505];
	scanf("%d",&x); lor(i,1,x) lor(j,1,x) scanf("%d",&a[i][j]);

}

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

	if(n==2) return printf("4 7\n23 10\n"),0;

	lor(i,2,MAXD){
		if(!vis[i]) prime[++prime[0]]=i;
		for(register int j=1;j<=prime[0]&&i*prime[j]<=MAXD;++j) vis[i*prime[j]]=true;
	}

	lor(i,1,n) lor(j,1,n) if(!((i+j)&1)){
		a[i][j]=prime[(i+j)>>1]*prime[n+(n+1)/2+(i-j)/2];
	}
	lor(i,1,n) lor(j,1,n) if((i+j)&1){
		a[i][j]=lcm(lcm(a[i-1][j],a[i][j-1]),lcm(a[i+1][j],a[i][j+1]))+1;
	}
	lor(i,1,n){
		lor(j,1,n) printf("%lld ",a[i][j]); printf("\n");
	}

	return 0;
}


#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
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; }
template<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;

int main()
{
        ll N, A, B;
        cin >> N >> A >> B;
        vll X(N+1);
        rep(i, 1, N+1) {
                cin >> X[i];
        }

        vll dp(N+1);

        rep(i, 2, N+1) {
                dp[i] += dp[i-1] + min(A * (X[i]-X[i-1]), B);
        }
        cout << dp.back() << endl;
}

#include <iostream>
using namespace std;
typedef long long int LLI;
int main()
{
	LLI n, a, b, save;
	cin >> n >> a >> b;

	LLI first, second, ans = 0;
	cin >> first >> second;
	save = ( second - first ) * a;
	if( save > b )
	    ans += b;
	else
	    ans += save;
	first = second;

	for(int i = 2; i < n; i++)
	{
		cin >> second;
	    save = ( second - first ) * a;
		if( save > b )
	    	ans += b;
	 	else
	    	ans += save;
		first = second;
	}
	cout << ans;
}


#include <iostream>
#include <string>

using namespace std;

int main(){
  string text;
  while(getline(cin,text,'\n')){
    for(int i=0;i<26;i++){
      for(int w=0;w<text.size();w++){
	if ('a'<=text[w]&&text[w]<'z')
	  text[w]++;
	else if(text[w]=='z')
	  text[w] = 'a';
      }
      if (text.find("this") != string::npos||text.find("that") != string::npos||text.find("the") != string::npos){
	cout << text << endl;
	break;
      }
    }
  }
}

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

const int ALPHA = 26;

int main(){
	string input;
	while(cin && getline(cin, input)){
		string cand[ALPHA];
		for(int i=0; i<ALPHA; i++){
			cand[i] = "";
			for(int n=0; n<input.size(); n++){
				if('a'<=input[n]&&input[n]<='z'){
					cand[i] += (input[n]-'a'+i)%ALPHA + 'a';
				} else {
					cand[i] += input[n];
				}
			}
			if(cand[i].find("the") != string::npos
				|| cand[i].find("this") != string::npos
				|| cand[i].find("that") != string::npos){
				cout<<cand[i]<<endl;
			}
		}
	}
	return 0;
}


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

struct edge{
  int to, cost;
};

int main(int argc, char *argv[]){
  int n, m, r;
  cin >> n >> m >> r;
  vector<vector<edge> > g(n);
  int from;
  for (int i = 0; i < m; i++) {
    edge e;
    cin >> from >> e.to >> e.cost;
    g[from].push_back(e);
  }
  int dp[n];
  for (int i = 0; i < n; i++) {
    dp[i] = 1e9;
  }
  dp[r] = 0;
  //???????????§?????????
  //????????????????????????????????????
  bool update = true;
  int cnt = 0;
  while(update){
    if(cnt > n)break;
    cnt++;
    update = false;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < g[i].size(); j++) {
        edge e = g[i][j];
        if(dp[e.to] > dp[i] + e.cost){
          dp[e.to] = dp[i] + e.cost;
          update = true;
        }
      }
    }
  }
  if(cnt > n){
    std::cout << "NEGATIVE CYCLE" << std::endl;
  }else{
    for (int i = 0; i < n; i++) {
      if(dp[i] == 1e9)std::cout << "INF" << std::endl;
      else std::cout << dp[i] << std::endl;
    }
  }
  return 0;
}

#include <iostream>
using namespace std;

#define full (m == 100 || e == 100 || j == 100)
#define avg(n) (m + e + j >= n)
#define me80 (m >= 80 || e >= 80)

int main() {
	int n, m, e, j;
	while (1) {
		cin >> n; if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> m >> e >> j;
			if (full || m + e >= 180 || avg(240)) cout << "A" << endl;
			else if (avg(210) || (avg(150) && me80)) cout << "B" << endl;
			else cout << "C" << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REV(i,a,n) for(int i=a;i>=n;i--)
#define all(a) a.begin(),a.end()
const int NUM = 2e5 + 5;

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int num_tests=1;
    //cin >> num_tests;
    while(num_tests-->0){
        int n; cin >> n;
        int ans = 0;
        while(n >= 500)
        {
            int x = n/500;
            ans += x*1000;
            n -= (x)*500;
        }
        while(n >= 5)
        {
            int y = n/5;
            ans += y*5;
            n -= y*5;
        }
        cout << ans;
    }
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < n; i++)
static const long long INF = 1000000;
using p = pair<int,int>;
int main(){
    int64_t x,ans;
    cin >> x;
    int a,b,c;
    a = x/500;
    b = x - 500*a;
    c = b/5;
    ans = 1000*a + c*5;
    cout << ans << endl;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
  char S;
  int mix=0;
  int a=0;
  for(int i=0; i<3;i++){
    cin >> S;
    if (S=='R'){
      a++;
      mix=max(a,mix);
    } else {
      a=0;
    }
  }
  cout << mix << endl;
}

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

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

    if (S.find("RRR") != string::npos) {
        cout << 3 << endl;
    } else {
        if (S.find("RR") != string::npos) {
            cout << 2 << endl;
        } else {
            if (S.find("R") != string::npos) {
                cout << 1 << endl;
            } else {
                cout << 0 << endl;
            }
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 998244353
#define N 1000009
#define cbn(x,y) ((ll)fac[x]*inv[y]%mod*inv[(x)-(y)]%mod)
using namespace std;

int m,n,fac[N],inv[N];
int ksm(int x,int y){
	int z=1; for (; y; y>>=1,x=(ll)x*x%mod) if (y&1) z=(ll)z*x%mod;
	return z;
}
int main(){
	scanf("%d%d",&m,&n); if (m<n) swap(m,n);
	int i,ans=0;
	fac[0]=inv[0]=inv[1]=1;
	for (i=1; i<N; i++) fac[i]=(ll)fac[i-1]*i%mod;
	for (i=2; i<N; i++) inv[i]=mod-(ll)inv[mod%i]*(mod/i)%mod;
	for (i=2; i<N; i++) inv[i]=(ll)inv[i-1]*inv[i]%mod;
	for (i=1; i<=n; i++) ans=(ans+(ll)cbn(i<<1,i)*cbn(m+n-(i<<1),m-i))%mod;
	printf("%lld\n",((ll)ans*ksm(cbn(m+n,m)<<1,mod-2)+m)%mod);
	return 0;
}

#include <algorithm>
#include <cmath>
#include <vector>
#include <functional>
#include <cstdlib>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <ctype.h>
#include <climits>
#include <queue>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)(n); ++i)
template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;}

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

	vector<int> w(n+1), e(n+1);
	w[0] = 0;
	e[0] = 0;
	REP(i,n){
		w[i+1] = w[i];
		e[i+1] = e[i];
		if(s[i]=='W') w[i+1]++;
		if(s[n-1-i]=='E') e[i+1]++;
	}

	int ans=1000000;
	REP(i,n){
		chmin(ans, w[i+1]+e[n-i]-1);
	}

	cout << ans << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,q;
  	cin>>n>>q;
  	vector<int>a(n);
  	for(int i=0;i<n;i++)cin>>a[i];
  	vector<vector<int> >d(n);
  	vector<int>f(n);
  	vector<int>ans(q);
	vector<vector<int> >id(n);
  	for(int i=0;i<q;i++)
    {
      int l,r;
      cin>>l>>r;
      l--;
      r--;
      d[r].push_back(l);
      id[r].push_back(i);
    }
  	vector<int>p(n+1,-1);
  	for(int i=0;i<n;i++)
    {
      if(p[a[i]]!=-1)
      {
        for(int j=p[a[i]];j<n;j=(j|(j+1)))f[j]-=1;
      }
      p[a[i]]=i;
      for(int j=p[a[i]];j<n;j=(j|(j+1)))f[j]+=1;
  	  for(int j=0;j<d[i].size();j++)
      {
        int lsum=0;
        int rsum=0;
        for(int t=i;t>=0;t=(t&(t+1))-1)rsum+=f[t];
        if(d[i][j]>0)
        {
          for(int t=d[i][j]-1;t>=0;t=(t&(t+1))-1)lsum+=f[t];
        }
        ans[id[i][j]]=rsum-lsum;
      }
    }
  for(auto x:ans)cout<<x<<endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int ff;
int n,m,col[500100],blo,tot,le,ri,ans[500100],cnt[500100];
pair<pair<int,int>,int> que[500100];
bool pp(pair<pair<int,int>,int> x,pair<pair<int,int>,int> y){
	return x.first.second<y.first.second;
}
inline void inser(int val){cnt[val]++;if(cnt[val]==1) ff++;}
inline void eras(int val){cnt[val]--;if(cnt[val]==0) ff--;}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&col[i]);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&que[i].first.first,&que[i].first.second);
		que[i].second=i;
	}
	sort(que+1,que+m+1);
	blo=max(1,(int)(sqrt(m)));
	tot=(m+blo-1)/blo;
	for(int i=1;i<=tot;i++){
		le=blo*(i-1)+1;
		ri=min(blo*i,m);
		sort(que+le,que+ri+1,pp);
		for(int j=1;j<=n;j++) cnt[j]=0;
		ff=0;
		for(int j=que[le].first.first;j<=que[le].first.second;j++)
			inser(col[j]);
		ans[que[le].second]=ff;
		for(int j=le+1;j<=ri;j++){
			for(int kk=que[j-1].first.first;kk<que[j].first.first;kk++)
				eras(col[kk]);
			for(int kk=que[j].first.first;kk<que[j-1].first.first;kk++)
				inser(col[kk]);
			for(int kk=que[j-1].first.second+1;kk<=que[j].first.second;kk++)
				inser(col[kk]);
			ans[que[j].second]=ff;
		}
	}
	for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

string _str[ 7 ] = { "null", "one pair", "two pair", "three card", "straight", "full house", "four card" };
int ans = 0;
int _hand[ 5 ];

int main() {
	while ( ~scanf( "%d,%d,%d,%d,%d", &_hand[ 0 ], &_hand[ 1 ], &_hand[ 2 ], &_hand[ 3 ], &_hand[ 4 ] ) ) {
		ans = 0;
		sort( _hand, _hand + 5 );
		bool is_straight = true;
		for ( int i = 0; i < 4; i++ ) {
			if ( _hand[ i + 1 ] - _hand[ i ] != 1 ) {
				is_straight = false;
				break;
			}
		}
		if ( is_straight ) {
			ans = 4;
		}
		for ( int i = 0; i < 5; i++ ) {
			if ( _hand[ i ] == 1 ) {
				_hand[ i ] += 13;
			}
		}
		sort( _hand, _hand + 5 );
		is_straight = true;
		for ( int i = 0; i < 4; i++ ) {
			if ( _hand[ i + 1 ] - _hand[ i ] != 1 ) {
				is_straight = false;
				break;
			}
		}
		if ( is_straight ) {
			ans = 4;
		}

		bool is_three_card = false;
		bool is_one_pair = false;
		for ( int i = 1; i <= 20; i++ ) {
			int many = upper_bound( _hand, _hand + 5, i ) - lower_bound( _hand, _hand + 5, i );
			if ( many == 4 ) {
				ans = 6;
				break;
			}
			if ( many == 3 ) {
				is_three_card = true;
				ans = 3;
				if ( is_one_pair ) {
					ans = 5;
					break;
				}
			}
			if ( many == 2 ) {
				if ( is_one_pair ) {
					ans = 2;
					break;
				}
				if ( is_three_card ) {
					ans = 5;
					break;
				}
				is_one_pair = true;
				ans = 1;
			}
		}
		printf( "%s\n", _str[ ans ].c_str() );
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
			f=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
int main()
{
	int d=read(),t=read(),s=read();
	if(t*s>=d)
		puts("Yes");
	else
		puts("No");
	return 0;
}

#include <iostream>
using namespace std;
#define fin(ans) cout << (ans) << '\n'
int main()
{
    int A[3];
    cin >> A[0] >> A[1] >>A[2];
    sort(A,A+3);
    if(A[0] == A[1] && A[1] == A[2]){
        fin("No");
    }else if ((A[0] != A[1] && A[1] != A[2]) ){
        fin("No");
    }else{
        fin("Yes");
    }

        return 0;
}


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

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

  if (s.size() != 2)
    reverse(s.begin(), s.end());
  cout << s << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[1500];
    int i,n;
    cin>>s;
    n=strlen(s);
    if(n==2){
        cout<<s;
    }
    else{
        for(i=n-1;i>=0;i--){
            cout<<s[i];
        }
    }
return 0;

    }


#include<iostream>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<typeinfo>
#include<sstream>
#include<list>
#include<set>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
int in()
{
	int x;cin>>x;
	return x;
}
int stringInt(string a)
{
	return atoi(a.c_str());
}
string intString(int a)
{
	stringstream ss;
	ss<<a;
	return ss.str();
}
vector<int> SieveOfEratosthenes(int x)
{
	vector<int> a;
	vector<int> b(x+1,0);
	for(int i=2;i<=x;i++)
	{
		if(b[i]==0)
		{
			a.push_back(i);
			for(int l=i;l<=x;l+=i)
			{
				b[l]=1;
			}
		}
	}
	return a;
}

int main ()
{
	int n,m;
	while(cin>>n>>m&&n&&m)
	{
		vector<long long > a,b;
		b.push_back(0);
		rep(i,n)
		{
			b.push_back(in());
		}
		rep(i,n+1)
		{
			rep(l,n+1)
			{
				a.push_back(b[i]+b[l]);
			}
		}
		long long ans = 0;
		sort(a.begin(),a.end());
		rep(i,a.size())
		{
			vector<long long>::iterator t=upper_bound(a.begin(),a.end(),m-a[i]);
			if(t==a.begin())continue;
			t--;
			ans=max(ans,*t+a[i]);
		}
		cout<<ans<<endl;

	}

}

//  0529.cpp
//  2012/12/14.

#if 1

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
    for(int N, M; cin >> N >> M && (N || M); )
	{
		vint points(N);

		rep(i, N) cin >> points[i];
		points.push_back(0);

		vint pair;
		rep(i, N+1) REP(j, i, N+1) pair.push_back(points[i] + points[j]);

		sort(ALL(pair));

		int ans = 0;
		rep(i, pair.size())
		{
			int s = M - pair[i];

			int idx = lower_bound(ALL(pair), s) - pair.begin() - 1;

			if(0 <= idx && pair[i] + pair[idx] <= M) chmax(ans, pair[i] + pair[idx]);
		}

		cout << ans << endl;
	}
}

#endif


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
//#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 998244353LL;

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    if (v.size() > 0) {
        os << v[v.size() - 1] << endl;
    }
    return os;
}

int main() {
    ll n;
    cin >> n;
    priority_queue<pint> pque_plus;
    priority_queue<pint> pque_nplus;
    ll max_memo = 0;
    REP(i, n) {
        string s;
        cin >> s;
        pint cur{0, 0};
        ll now = 0, minval = 0, maxval = 0;
        REP(j, s.size()) {
            now += (s[j] == '(' ? 1 : -1);
            minval = min(minval, now);
            maxval = max(maxval, now);
        }
        cur.SE = now;
        if (cur.SE > 0){
            cur.FI = minval;
            pque_plus.push(cur);
        } else if (cur.SE < 0) {
            now = minval = maxval = 0;
            IREP(j, s.size()) {
                now += (s[j] == ')' ? 1 : -1);
                minval = min(minval, now);
            }
            cur.FI = minval;
            cur.SE = now;
            pque_nplus.push(cur);
        } else {
            max_memo = max(max_memo, abs(minval));
            now = minval = maxval = 0;
            IREP(j, s.size()) {
                now += (s[j] == ')' ? 1 : -1);
                minval = min(minval, now);
            }
            max_memo = max(max_memo, abs(minval));
        }
    }

    ll now = 0;
    while (pque_plus.size()){
        pint cur_val = pque_plus.top();
        pque_plus.pop();
        if (now + cur_val.FI < 0){
            cout << "No" << endl;
            return 0;
        }
        now += cur_val.SE;
    }

    ll now2 = 0;
    while (pque_nplus.size()) {
        pint cur_val = pque_nplus.top();
        pque_nplus.pop();
        if (now2 + cur_val.FI < 0) {
            cout << "No" << endl;
            return 0;
        }
        now2 += cur_val.SE;
    }

    if (now != now2) {
        cout << "No" << endl;
        return 0;
    }
    if (now < abs(max_memo)) {
        cout << "No" << endl;
        return 0;
    }
    cout << "Yes" << endl;
}

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

int main() {
	int N;
	cin >> N;
	vector<int>A(N + 2);
	A[0] = 0;
	A[N + 1] = 0;
	for (int i = 0; i < N; ++i) {
		int a;
		cin >> a;
		A[i + 1] = a;
	}
	int ans = 0;
	for (int i = 0; i < N + 1; ++i) {
		ans += abs(A[i + 1] - A[i]);
	}
	for (int i = 0; i < N; ++i) {
		cout << ans + abs(A[i + 2] - A[i]) - abs(A[i + 1] - A[i]) - abs(A[i + 2] - A[i + 1]) << endl;
	}
	return 0;
}


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

int main() {
  int N;
  cin >> N;
  int ans = 111;
  for (int i = 1; i < 10; i++) {
    if (N <= ans * i) {
      cout << ans * i << endl;
      break;
    }
    else if (i == 9) {
      cout << 1111 << endl;
    }
  }
}

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int score=0;
		int runner=0;
		for(int j=0;j<3;){
			string s;
			cin>>s;
			if(s=="HIT"){
				if(runner==3)
					score++;
				else
					runner++;
			}
			else if(s=="HOMERUN"){
				score+=runner+1;
				runner=0;
			}
			else
				j++;
		}
		cout<<score<<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 N, A[int(2e5+5)], now[int(2e5+5)];
vector <int> ans;

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

  for (int i = N; i >= 1; i--){
    if (now[i] % 2 == A[i]) continue;
    ans.push_back(i);
    for (int j = 1; j * j <= i; j++){
      if (i % j != 0) continue;
      now[j]++;
      if (j * j != i) now[i/j]++;
    }
  }

  if (now[1] % 2 != A[1]){
    cout << -1 << endl;
    return 0;
  }

  int M = ans.size();
  cout << M << endl;
  if (M > 0){
    cout << ans[0];
    for (int i = 1; i < M; i++) cout << " " << ans[i];
    cout << endl;
  }

  return 0;
}

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

#include <iostream>
#include <vector>

class DPreparingBoxes {
public:
    void solve(std::istream& in, std::ostream& out) {
        long long n;
        std::vector<int> ans, a(200020), b(200020);
        in >> n;
        for(int i = 0; i < n; i++){
            in >> a[i];
        }
        for(int i = n-1; i >= 0; i--){
            int count = 0;
            for(int j = 2*(i+1); j <= n; j += (i+1)){
                count += b[j-1];
            }
            if((count % 2) != a[i]){
                b[i] = 1;
                ans.push_back(i+1);
            }
        }
        out << ans.size() << std::endl;
        for(auto i : ans){
            out << i << " ";
        }
        out << std::endl;
    }
};

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


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

	while (1)
	{
		int tmp;

		cin >> tmp;

		if (!tmp)
		{
			break;
		}

		input.push_back(tmp);

		(maxsize < tmp) ? (maxsize = tmp) : (0);
	}

	vector<int> prime, ans;

	for (int i(2); i <= maxsize; ++i)
	{
		for (vector<int>::iterator i2(prime.begin()), n2(prime.end()); i2 != n2; ++i2)
		{
			if ((i % *i2) == 0)
			{
				goto nx;
			}
		}

		prime.push_back(i);

		if (i != *prime.begin() && binary_search(prime.begin(), prime.end(), i - 2))
		{
			ans.push_back(i);
		}
		nx:;
	}

	for (vector<int>::iterator i(input.begin()), n(input.end()); i != n; ++i)
	{
		int out(*(--upper_bound(ans.begin(), ans.end(), *i)));

		cout << out - 2 << ' ' << out << endl;
	}

	return 0;
}

#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
int card[20000],i,j,a=0,b=0,c=1,n;
cin>>n;
while(n!=0){
for(i=0;i<2*n;i++){
cin>>card[i];
}
for(i=0;i<2*n;i=i+2){
if(card[i]>card[c]){
a=a+card[i]+card[c];
}
else if (card[i]==card[c]){
a=a+card[i];
b=b+card[c];
}
else{
b=b+card[i]+card[c];
}
c=c+2;
}
cout<<a<<" "<<b<<endl;
c=1;
a=0;
b=0;
cin>>n;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll MOD=1e9+7;

void solve()
{

	ll a[5];
	for(ll i=0;i<5;i++)
		cin>>a[i];
	ll k;
	cin>>k;
	for(ll i=0;i<5;i++)
		for(ll j=0;j<5;j++)
			if(a[j]-a[i]>k)
			{
				cout<<":(";
				return;
			}
	cout<<"Yay!";
	return;
}

int main()
{
        fast;
        ll q;
        q=1;
        // cin>>q;
        while(q--)
        	solve();

        return 0;
}

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<"\n"
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define mp make_pair
#define sz(x) (ll)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using vc = vector<char>;
using vvc = vector<vc>;
template<class T> inline bool chmax(T& a, T b) {
 if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
 if (a > b) { a = b; return true; } return false;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
int main(){
  ll h,w;
  cin>>h>>w;
  vvll a(h,vll(w));
  vvll b(h,vll(w));
  rep(i,h)rep(j,w) cin>>a[i][j];
  rep(i,h)rep(j,w) cin>>b[i][j];
  vector<vvb> dp(h,vvb(w,vb(160*80,false)));
  dp[0][0][abs(a[0][0]-b[0][0])]=true;
  rep(i,h)rep(j,w)rep(z,160*80){
    if(dp[i][j][z]){
      if(i+1<h){
      dp[i+1][j][z+abs(a[i+1][j]-b[i+1][j])]=true;
      dp[i+1][j][abs(z-abs(a[i+1][j]-b[i+1][j]))]=true;
      }
      if(j+1<w){
      dp[i][j+1][z+abs(a[i][j+1]-b[i][j+1])]=true;
      dp[i][j+1][abs(z-abs(a[i][j+1]-b[i][j+1]))]=true;
      }
    }
  }
  rep(i,160*80){
    if(dp[h-1][w-1][i]){
      COUT(i);
      return 0;
    }
  }
}


#include<iostream>
using namespace std;
int main(){
	int sx, sy, tx,ty;
	cin >> sx >> sy >> tx >> ty;
	for(int i = 0; i<(ty- sy); i++){
		cout << 'U';
	}
	for(int i = 0; i<(tx- sx); i++){
		cout << 'R';
	}
	for(int i = 0; i<(ty- sy); i++){
		cout << 'D';
	}
	for(int i = 0; i<(tx- sx); i++){
		cout << 'L';
	}

	int csx = sx, csy =sy, ctx = tx,cty = ty;
	csx -= 1;
	cty += 1;
	cout << 'L';
	for(int i = 0; i<(cty- csy); i++){
		cout << 'U';
	}
	for(int i = 0; i<(ctx- csx); i++){
		cout << 'R';
	}
	 cout << 'D';

	ctx = tx +1;
	csy = sy -1;
	csx = sx, cty = ty;
	cout << 'R';
	for(int i = 0; i<(cty- csy); i++){
		cout << 'D';
	}
	for(int i = 0; i<(ctx- csx); i++){
		cout << 'L';
	}
	cout << 'U';

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int mi=(-1000000);

int main() {
	int k,x; cin >> k >> x;
	if(x-k+1<mi) {
		for(int i=mi;i<mi+k;i++) cout << i;
		cout << endl;
		return 0;
	}
	for(int i=x-k+1;i<x+k;i++) cout << i << ' ';
	cout << endl;
}


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

#define fastio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

#define ll                      long long
#define read1(x)                 cin>>x
#define read2(x,y)               cin>>x>>y
#define print(x)                cout<<x<<endl
#define pii                     pair<int, int>
#define pli                     pair<ll, int>
#define pil                     pair<int, ll>
#define pll                     pair<ll, ll>
#define vi                      vector<int>
#define vll                     vector<ll>
#define vb                      vector<bool>
#define vd                      vector<double>
#define vs                      vector<string>
#define ff                      first
#define ss                      second
#define pb                      push_back
#define ppb                     pop_back
#define pf                      push_front
#define ppf                     pop_front
#define vpii                    vector<pii>
#define umap                    unordered_map
#define uset                    unordered_set
#define clr(a,b)                memset(a,b,sizeof a)
#define all(ds)                 ds.begin(), ds.end()
#define rev(ds)                 ds.rbegin(), ds.rend()
#define asc(A)                  sort(A.begin(), A.end())
#define dsc(A)                  sort(A.begin(), A.end(), greater<int>())
#define gcd(x,y)                __gcd(x,y)
#define clr(a,b)                memset(a,b,sizeof a)
#define fr(i, n)                for(int i=0; i<n;++i)
#define fr1(i, n)               for(int i=1; i<=n; ++i)
#define rfr(i, n)               for(int i=n-1; i>=0; --i)
#define printVector(v)          for(int i= 0;i<v.size();i++) {cout<<v[i]<<" ";} cout<<endl;
#define precise(x)              cout<<fixed<<setprecision(x)
#define inf                     10000000000000

ll lcm(ll a, ll b) {return (a*b)/__gcd(a, b); }
ll power(ll x, ll y, ll m) { if(!y) return 1; ll p=power(x, y>>1, m)%m; p=p*p%m; return (y&1)?(x*p)%m:p;}
ll modInverse(ll a, ll m){ return power(a, m-2, m); }
bool isPrime(ll num){bool flag=true;for(ll i =2;i<=num/2;i++){if(num%i==0) {flag=false; break;} }return flag;}
ll sumofdigits(ll n){ll c=0;while(n>0){c++;n/=10;}return c;}
bool sortinrev(const pair<int,int>&a,const pair<int,int>&b) {return a.first > b.first; }
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) {return a.second < b.second; }

//const double PI= 2*acos(0.0);
const long long MOD= 1000000007;

void solve(){
    int n; cin>>n;
    vi v;
    fr(i, n){
        int x; cin>>x;
        v.push_back(x);
    }
    sort(all(v));
    if(v.size() <= 2){
        print(0);
        return;
    }
    int count = 0;
    for(int i = 0; i < n-2; ++i){
        for(int j = i+1; j < n-1; ++j){
            if(v[i] == v[j])continue;
            for(int k = j+1; k < n; ++k){
                if(v[i] == v[k] || v[j] == v[k])continue;
                if(v[i] + v[j] > v[k])count++;
            }
        }
    }
    cout<<count<<endl;

}
int main()
{
#ifndef ONLINE_JUDGE
    input_output
#else
    fastio
#endif

    int t =1 ;
    // cin>>t;
    while(t--){
        solve();
    }

    return 0;
}

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

using namespace std;

int swap_count = 0; // number of swaps

void insertion_sort(vector<int> &sort_vec, int n, int interval) {
  for (int i = interval; i < n; ++i) {
    int pivot = sort_vec[i];
    int j = i - interval;
    while (j >= 0 && sort_vec[j] > pivot) { //
      sort_vec[j + interval] = sort_vec[j]; // perform swap
      j = j - interval; // drop by interval
      swap_count++;
    }
    sort_vec[j + interval] = pivot; // put pivot in proper place
  }
}

int main() {
  int n; //number of entries
  cin >> n;

  vector<int> sort_vec;
  //initialize unsorted vector
  for (int i = 0; i < n; ++i) {
    int entry;
    cin >> entry;
    sort_vec.push_back(entry);
  }

  vector<int> intervals;
  //initialize array according to formula k_next = 3*k_prev + 1 (Knuth)
  int next = 1;
  while (true) {
    intervals.push_back(next);
    next = next * 3 + 1;
    if (next >= n) break;
  }

  int intervals_size = intervals.size();
  //shell sort
  for (int i = intervals_size - 1; i >= 0; --i) {
    insertion_sort(sort_vec, n, intervals[i]);
  }

  //print results
  cout << intervals_size << endl; // number of intervals
  for (int i = intervals_size - 1; i >= 0; --i) {
    cout << intervals[i];
    if (i <= n - 1) cout << " ";
  }
  cout << endl;
  cout << swap_count << endl; // number of swaps
  for (int i = 0; i < n; ++i) { // sorted vector
    cout << sort_vec[i] << endl;
  }

}


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

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

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

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

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

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

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

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

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

template <typename A>
string to_string(A v) {
    if(v.size() >= 200) {
        return "Too big container";
    }

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

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

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

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

void debug_out() { cerr << endl; }

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

#define rep(i,n) for (int i = 0; i < n; ++i)
#define per(i,n) for (int i = n - 1; i >= 0; --i)
#define rep1(i,n) for (int i = 1; i <= n; ++i)
#define reps(i,s,n) for (int i = s; i < n; ++i)
#define all(c) begin(c),end(c)
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double ld;
typedef pair<int,int> PINT;
typedef pair<ll,ll> PLL;
const long long MOD = 1e9+7;
#define precout() cout << std::fixed << std::setprecision(20);
#define print(a) cout << a << endl;
const int INFI = (1 << 30) - 1;
const long long INFLL = (1LL << 62) - 1;
template<typename T> void printv(vector<T>&v) {
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << " ";
    cout << v.back() << endl;
}
const int dy[4] = { 0, 1, 0, -1 };
const int dx[4] = { 1, 0, -1, 0 };
double deg2rad(double deg) { return deg * M_PI / 180; }
double rad2deg(double rad) { return rad * 180 / M_PI; }

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

    ll N, X; cin >> N >> X;
    ll ans = 0;
    ll left; cin >> left;

    rep(i, N - 1) {
        ll a; cin >> a;
        ll over = (left + a) - X;
        if(over > 0) {
            ans += over;
            if(over > a) {
                left = 0;
            } else {
                left = a - over;
            }
        } else {
            left = a;
        }
    }

    print(ans);
}

#include<bits/stdc++.h>

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

using namespace __gnu_pbds;
using namespace std;

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

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

void solve()
{
    ll n,x;
    cin>>n>>x;
    ll ct=0;
    VL arr(n);
    fr(i,0,n)
        cin>>arr[i];
    fr(i,1,n)
    {
        if((arr[i]+arr[i-1])>x)
        {
            ll rem=(arr[i]+arr[i-1])-x;
            ll f=min(rem,arr[i]);
            rem-=f;
            arr[i]-=f;
            ct+=f;
            if((arr[i]+arr[i-1])>x)
            {
                ct+=rem;
            }
        }
    }
    cout<<ct<<endl;
    return;
}
int main()
{
    IOS;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        //cout<<"Case #"<<pp-t<<":\n";
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
*/
/*
recursion - matrix exponential

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


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

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

	if( abs(A-B) < 2 ){
		cout << "Yay!" << endl;
	}
	else{
		cout << ":(" << endl;
	}

}

#include <stdio.h>

int main () {
    int N, K, A = 1;
    scanf("%d %d", &N, &K);

    for (int i = 1; i <= N; i++) {
        if (A * 2 < A + K) {
            A *= 2;
        }
        else {
            A += K;
        }
    }
    printf("%d", A);
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)
int main() {
    string s;
    cin >> s;
    string tmp_s;
    rep(i, s.length()) {
        tmp_s += s[i];
        if ( tmp_s + "FESTIVAL" == s ) break;
    }
    cout << tmp_s << endl;
    return 0;
}

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

int main(void) {
  int ans = 0, n, min = 101;
  rep(i, 4) {
    scanf("%d", &n);
    ans += n;
    if(n < min) min = n;
  }
  ans -= min;
  min = 101;
  rep(i, 2) {
    scanf("%d", &n);
    ans += n;
    if(n < min) min = n;
  }
  ans -= min;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll nature(ll a){//絶対値を返す
  if(a>=0){return(a);}
  else{return(-1*a);}
}
int main(){
    long long N,count=0,count0=0;
    cin>>N;
    long long ans=0,m=10000000007;
    vector<long long> vec(N);
    for(int i=0;i<N;i++){
        cin>>vec.at(i);
        if(vec.at(i)==0){
            count0++;
        }
        if(vec.at(i)<0){
            count++;
        }
        long long M=nature(vec.at(i));
        ans+=M;
        m=min(m,M);
    }
    for(int i=0;i<N;i++){
    }
    if(count%2==0){
        cout<<ans<<endl;
    }
    else{
        cout<<ans-2*m<<endl;
    }
}

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

int main() {
  int N;
  cin>>N;
  int64_t Z=0,MIN=167167167167617,A=1;
  for(int i=0;i<N;i++){
    int64_t a;
    cin>>a;
    if(a<0){
      A*=-1;
      a*=-1;
    }
    Z+=a;
    MIN=min(MIN,a);
  }
  cout<<Z-MIN+A*MIN<<endl;
}


#include <bits/stdc++.h>

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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    policy_set;

#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define ALL(c) c.begin(),c.end()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;

void preprocess(){

}
const ll N=3e5+7;
const ll INF=1e18+1;
const ll MOD=1e9+7;
//Have to ramp up more on implementation skillz to do it fast,
// and debug skillz, it is not as easy as you think with two pointers
ll n,a[N],pref[N];
ll f(ll l1,ll r1,ll l2, ll r2){
    return abs((pref[r1]-pref[l1-1])-(pref[r2]-pref[l2-1]));
}
// So we will implement the two pointer solution to this also
// very well
// So in ternary search we always need to check 4 values near, the start,end
// like in that cf problem, no need to do this all over again
ll find_left(ll i){
    ll start=1,end=i-1;
    while(start<=end){
        ll mid1=(2*start+end)/3,mid2=(start+2*end)/3;
        // cout<<start<<" "<<mid1<<" "<<mid2<<" "<<end<<"\n";
        if(f(1,mid1,mid1+1,i)>f(1,mid2,mid2+1,i))
            start=mid1+1;
        else
            end=mid2-1;
    }
        // cout<<start<<" "<<end<<"\n";
    ll arr[]={start,end},ans=1e18,ansidx=start;
    for(ll j=0;j<2;j++)
        for(ll jj=-1;jj<=1;jj++){
            ll idx=arr[j]+jj;
            if(idx<1 || idx>i-1)continue;
            if(ans>f(1,idx,idx+1,i))ans=f(1,idx,idx+1,i),ansidx=idx;
        }

    return ansidx;
}
// I remember facing this problem before
ll find_right(ll i){
    ll start=i+1,end=n-1;
    while(start<=end){
        ll mid1=(2*start+end)/3,mid2=(start+2*end)/3;
        if(f(i+1,mid1,mid1+1,n)>f(i+1,mid2,mid2+1,n))
            start=mid1+1;
        else
            end=mid2-1;
    }

    ll arr[]={start,end},ans=1e18,ansidx=start;
    for(ll j=0;j<2;j++)
        for(ll jj=-1;jj<=1;jj++){
            ll idx=arr[j]+jj;
            if(idx<i+1 || idx>n)continue;
            if(ans>f(i+1,idx,idx+1,n))ans=f(i+1,idx,idx+1,n),ansidx=idx;
        }
    return ansidx;
}
void solve(){
    cin>>n;
    for(ll i=1;i<=n;i++)cin>>a[i],pref[i]=pref[i-1]+a[i];
    ll ans=INT64_MAX;
    // cout<<find_left(4)<<" "<<f(1,1,2,4)<<" "<<f(1,2,3,4)<<" "<<f(1,3,4,4)<<"\n";
    // return;
    for(ll i=1;i<=n;i++){
        //i ins the middle split, in this case
        // 1..left left+1..i i+1..right right+1..n
        ll left=find_left(i),right=find_right(i);
        // cout<<left<<" "<<right<<"\n";
        vector<ll> vecs({pref[left],pref[i]-pref[left],pref[right]-pref[i],pref[n]-pref[right]});
        sort(all(vecs));
        ans=min(ans,vecs.back()-vecs[0]);
    }
    cout<<ans<<"\n";
}
int main() {
	// #ifndef ONLINE_JUDGE
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	// #endif
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	preprocess();
	int t=1;
	//cin>>t;
	//cout<<t<<endl;
	for(int i=0;i<t;i++)
		solve();
	return 0;
}

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

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 1000100
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void solve(void)
{
  int n;cin>>n;
  vector<int> vec (n,0);
  for (int i = 0; i < n; i++)
  {
    cin>>vec[i];
    if(i != 0)
    vec[i] += vec[i-1];
  }
  int i1 = 0;
  int i2 = 1;
  int i3 = 2;
  int res = INF;
  while(i2 < n-2)
  {
    while(i1 < i2 - 1)
    {
      if(abs(vec[i1] - (vec[i2] - vec[i1])) > abs(vec[i1+1] - (vec[i2] - vec[i1+1])) )
      i1 ++;
      else
      {
        break;
      }
    }

    while(i3 < n - 2)
    {
      if(abs(vec[n-1] - vec[i3] - (vec[i3] - vec[i2])) >abs(vec[n-1] - vec[i3+1] - (vec[i3+1] - vec[i2])) )
      i3 ++;
      else
      {
        break;
      }
    }
    int p1 = vec[i1];
    int p2 = vec[i2] - vec[i1];
    int p3 = vec[i3] - vec[i2];
    int p4 = vec[n-1] - vec[i3];
    int m1 = max(max(p1,p2),max(p3,p4));
    int m2 = min(min(p1,p2),min(p3,p4));
    chmin(res,m1 - m2);
    i2++;
  }
  cout<<res<<endl;

  return;
}

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

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<int,int> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=4000000000000000010LL;
const int MAX=310;
const double EPS=1e-6;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
    int dd[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    vector<string>v;
    v.push_back("Wednesday");
    v.push_back("Thursday");
    v.push_back("Friday");
    v.push_back("Saturday");
    v.push_back("Sunday");
    v.push_back("Monday");
    v.push_back("Tuesday");
    int mo,da;
    while(cin>>mo>>da,mo){
        int d=da;
        for(int i=0;i<mo-1;i++){
            d+=dd[i];
        }
        cout<<v[d%7]<<endl;
    }
}