#include <stdio.h>

int main()
{
    long long int n;

    scanf ("%lld",&n);
    char out[100]={0};
    int i=0;

    n--;
    while(n>=0){
        out[i]= n%26 +'a';
        n/=26;
        i++;
        n--;
    }
    for(int j=i-1; j>=0; j--){
        printf("%c",out[j]);
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define F(i,j,k,in) for(int i=j;i<k;i+=in)
#define DF(i,j,k,in) for(int i=j;i>=k;i-=in)
#define feach(it,l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(),a.end()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef vector<vector<ll>> vvll;
typedef vector<vector<int>> vvin;
typedef vector<pair<ll,ll>> vpll;
typedef vector<pair<int,int>> vpin;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef queue<int> qin;
typedef priority_queue<int> pqin;
typedef map<ll,ll> mpll;
typedef map<int,int> mpin;
typedef pair<ll,ll> pll;
typedef pair<int,int> pin;
const ll MOD=1000000007;
const long double PI=3.1415926535897932384626433832795;
ll pwr(ll b,ll p){ll res=1;while(p){if(p&1) {res*=b; p--;}else{b*=b; p>>=1;}}return res;}
int main()
{
ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL);
ll x; cin>>x;
string ans;
while (x) {
  if (x%26) ans+=('a'+(x%26)-1);
  else {--x; ans+='z';}
  x/=26;
}
reverse (fall(ans));
cout<<ans;
return 0;
}


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

typedef long long ll;

int main() {
    int A, B, C, K;
    cin >> A >> B >> C >> K;
    int X = max({A, B, C});
    int Y = 1;
    rep(i, K) Y *= 2;
    cout << A + B + C + X * (Y - 1) << "\n";
}

#include <bits/stdc++.h>
using namespace std;
int main() {
    int k, sum=0;
	vector<int> v1(3);
	for(int i=0;i<3;i++)
	{
	    cin>>v1[i];
	}
		cin>>k;
	sort(v1.begin(),v1.end());
	for(int i=1;i<=k;i++){
	v1[2]=v1[2]*2;}
	for(int i=0;i<3;i++)
	{
	    sum+=v1[i];
	}
    cout<<sum<<"\n";
	return 0;
}


//### In The Name Of GOD ###

//%% Remember; (((He))) is The (((Accepter))) %%

// GOD IS GREATER ...

#include <bits/stdc++.h>

using namespace std;

typedef long long ll ;
typedef long double ld ;

//#define int ll
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define X first
#define Y second
#define sz(a) (int)(a.size())
#define sg(a, b) (a + b - 1) / b

const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000 + 10 ;
const int MAXN = 10000 + 10;
const int MLOG = 17;
const int NMAX = 20;

signed main() {
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int m, n;
	cin >> n >> m;
	int A[MAXN], B[MAXN];
	for(int i = 0; i < n; i++) cin >> A[i];
	for(int i = 0; i < m; i++) cin >> B[i];
	sort(A, A + n, greater<int>());
	sort(B, B + m, greater<int>());
	int x = 0, y = 0, ans = 1;
	for(int i = n * m; i >= 1; i--) {
		if(A[x] == i && B[y] == i) {
			x++;
		   	y++;
		}else if(A[x] == i) {
			x++;
			ans = (ll)ans * y % MOD;
		}else if(B[y] == i) {
			y++;
			ans = (ll)ans * x % MOD;
		}else {
			ans = (ll)ans * max(0, x * y - n * m + i) % MOD;
		}
	}
	ans *= (x == n);
	ans *= (y == m);
	cout << ans;
}

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

signed main () {
	int n, m, a;
    cin >> n >> m;
    map < int, int > pref;
    int cur = 0;
    ++pref[cur];
    int64_t ans = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a;
        (cur += a) %= m;
        ans += pref[cur];
        ++pref[cur];
    }
    cout << ans << '\n';
}


#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

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

const ll INF = 1LL << 50;

struct UnionFind {
  vll par;
  vll wcnt;
  vll bcnt;
  ll cnt;
  UnionFind(ll N, vector<string>& mp) : par(N), wcnt(N, 0), bcnt(N, 0), cnt(N) {
    for (ll i = 0; i < N; i++) {
      par[i] = i;
      ll w = (ll)mp[0].size();
      if (mp[i / w][i % w] == '.')
        wcnt[i] = 1;
      else
        bcnt[i] = 1;
    }
  }

  ll find(ll x) { return par[x] == x ? x : par[x] = find(par[x]); }

  bool same(ll x, ll y) { return find(x) == find(y); }

  ll get_wcnt(ll x) { return wcnt[find(x)]; }
  ll get_bcnt(ll x) { return bcnt[find(x)]; }

  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    ll sizex = wcnt[x] + bcnt[x];
    ll sizey = wcnt[y] + bcnt[y];
    if (sizex < sizey) swap(x, y);
    par[y] = x;
    wcnt[x] += wcnt[y];
    bcnt[x] += bcnt[y];
    cnt--;
  }

  ll count() const { return cnt; }
};

int main() {
  ll h, w;
  cin >> h >> w;
  vector<string> mp(h);
  rep(i, h) { cin >> mp[i]; }

  UnionFind uf(h * w, mp);
  rep(i, h) rep(j, w) rep(k, 4) {
    ll y = i + dy[k];
    ll x = j + dx[k];
    bool cond = 0 <= x && x < w && 0 <= y && y < h;
    if (!cond) continue;
    if (mp[y][x] != mp[i][j]) {
      uf.unite(x + y * w, j + i * w);
    }
  }
  ll ans = 0;
  rep(i, h * w) {
    if(i == uf.find(i)){
      ans += uf.get_bcnt(i) * uf.get_wcnt(i);
    }
  }

  cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
template <class T> T input() {T t; cin>>t; return t;}
#define FOR(i,a,b) for (ll i = a; i < b; i++)
#define REP(i,b) FOR(i,0,b)
#define RFOR(i,a,b) for (ll i = a-1; i >= b; i--)
#define RREP(i,a) RFOR(i,a,0)
#define REPALL(i,x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i,x) for (ll i = x.size()-1; i >= 0; i--)
#define REPITR(itr,x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x,num) count(ALL(x), num)
#define MEMSET(x,val) memset(x, val, sizeof(x))
#define CHMAX(a,b) a = max(a,b)
#define CHMIN(a,b) a = min(a,b)
#define debug(x) cerr<<__LINE__<<": "<<(#x)<<" = "<<(x)<<endl;
void YES(bool flag) {cout<<(flag ? "YES" : "NO")<<endl;}
void Yes(bool flag) {cout<<(flag ? "Yes" : "No")<<endl;}
void yes(bool flag) {cout<<(flag ? "yes" : "no")<<endl;}
#define PRINT_ARRAY_H(i,a) REPALL(i,a) {cout<<a[i]<<(i != a.size()-1 ? " " : "");} newline;
#define e1 first
#define e2 second
#define newline putchar('\n')
#define cspace ' '
#define sspace " "
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};

int H, W;
vector<string> s;
ll ans = 0ll;
VVI visited(444, VI(444));

ll bfs(int y, int x)
{
    int num_b = 1, num_w = 0;

    deque<pair<int, int>> frends;
    frends.push_back(make_pair(y, x));
    visited[y][x] = 1;

    while (!frends.empty()) {
        auto p = frends.front();
        frends.pop_front();

        if (s[p.e1][p.e2] == '#') {
            REP(i,4) {
                if (0 <= p.e2 + dx[i] && p.e2 + dx[i] < W && 0 <= p.e1 + dy[i] && p.e1 + dy[i] < H
                    && visited[p.e1 + dy[i]][p.e2 + dx[i]] == 0 && s[p.e1 + dy[i]][p.e2 + dx[i]] == '.') {

                    frends.push_back(make_pair(p.e1 + dy[i], p.e2 + dx[i]));
                    visited[p.e1 + dy[i]][p.e2 + dx[i]] = 1;
                    num_w++;
                }
            }
        } else if (s[p.e1][p.e2] == '.') {
            REP(i,4) {
                if (0 <= p.e2 + dx[i] && p.e2 + dx[i] < W && 0 <= p.e1 + dy[i] && p.e1 + dy[i] < H
                    && visited[p.e1 + dy[i]][p.e2 + dx[i]] == 0 && s[p.e1 + dy[i]][p.e2 + dx[i]] == '#') {

                    frends.push_back(make_pair(p.e1 + dy[i], p.e2 + dx[i]));
                    visited[p.e1 + dy[i]][p.e2 + dx[i]] = 1;
                    num_b++;
                }
            }
        }
        //cout<<p.e1<<sspace<<p.e2<<sspace<<visited[p.e1][p.e2]<<endl;
    }

    //cout<<num_b<<sspace<<num_w<<endl;
    return (ll)num_b * num_w;
}

int main()
{
    cin>>H>>W;
    REP(i,H) s.push_back(input<string>());

    REP(i,H) {
        REP(j,W) {
            if (s[i][j] == '#' && visited[i][j] == 0) {
                ans += bfs(i, j);
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++ i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; -- i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++ i)
#define ri rd<int>
using namespace std;
const int mod = 998244353;
const int maxN = 1e6 + 7;

inline int pls(int x, int y) {return (x + y) % mod;}
inline int mns(int x, int y) {return pls(x, mod - y);}
inline int mul(int x, int y) {return 1LL * x * y % mod;}
inline void Add(int &x, int y) {x = pls(x, y);}
inline void Mul(int &x, int y) {x = mul(x, y);}

template<class T> inline T rd() {
	bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
	T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x;
}

int n, m;
int iv[maxN];
int fac[maxN], ifac[maxN];

inline int C(int x, int y) {return (y <= x) ? mul(mul(fac[x], ifac[y]), ifac[x - y]) : 0;}
inline int ways(int x, int y) {return C(x + y, y);}

int main() {

	n = ri(), m = ri();
	if (n < m) swap(n, m);
	iv[1] = 1; rep (i, 2, n + m) iv[i] = mul(iv[mod % i], mod - mod / i);
	fac[0] = 1; rep (i, 1, n + m) fac[i] = mul(fac[i - 1], i);
	ifac[0] = 1; rep (i, 1, n + m) ifac[i] = mul(ifac[i - 1], iv[i]);

	int res = 0;
	Add(res, mul(ways(n, m), n));
	rep (i, 1, m) Add(res, mul(iv[2], mul(ways(i, i), ways(n-i, m-i))));

	printf("%d\n", mul(res, mul(mul(ifac[n+m], fac[m]), fac[n])));

	return 0;
}

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

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

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

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

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

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

template<typename T> struct binary_indexed_tree {
    int S;
    vector<T> table;

    binary_indexed_tree<T>(int _S = 0) : S(_S) {
        table.resize(S+1);
    }

    // Adds v to the element at index i
    void add(int i, T v) {
        for (i++; i <= S; i += i&-i)
            table[i] = table[i] + v;
    }

    // Returns the sum of the elements at indices [0, i)
    T sum(int i) const {
        T res = T();
        for (; i; i -= i&-i)
            res = res + table[i];
        return res;
    }

    // Returns the sum of the elements at indices [l, r)
    T sum(int l, int r) const {
        return sum(r) - sum(l);
    }

    /*
     * Returns in O(logS) the first i in [0, S] such that comp(sum(i)) is true.
     * Returns -1 if no such i exists.
     * Requires that comp(sum(i)) is non-decreasing in i.
     * The empty prefix is considered to have sum equal to T's default value.
     */
    int lower_bound(const auto& comp) const {
        int inx = 0;
        T cur = T();
        if (comp(cur)) return 0;

        for (int i = 31 - __builtin_clz(S); i >= 0; i--) {
            int nxt = inx + (1 << i);
            if (nxt <= S && !comp(cur + table[nxt])) {
                inx = nxt;
                cur = cur + table[nxt];
            }
        }
        return inx < S ? inx + 1 : -1;
    }
};

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

    int N, X;
    cin >> N >> X;

    ll cur = 0;

    vector<tuple<ll, int, int, int>> exams(N);
    for (int i = 0, b, l, u; i < N; i++) {
        cin >> b >> l >> u;
        cur += ll(b) * l;
        exams[i] = make_tuple(ll(b) * l + ll(X - b) * u, b, l, u);
    }
    sort(all(exams), [](auto a, auto b) { return get<0>(a) > get<0>(b); });

    binary_indexed_tree<ll> full(N);
    for (int i = 0; i < N; i++) {
        full.add(i, get<0>(exams[i]));
    }

    ll ans = ll(X) * full.lower_bound([&](ll tot) { return tot >= cur; });

    for (int part = 0; part < N; part++) {
        ll mv; int b, l, u; tie(mv, b, l, u) = exams[part];

        full.add(part, -mv);

        int ftake = full.lower_bound([&](ll tot){ return tot + mv >= cur; });
        ll need = max(0ll, cur - full.sum(ftake));

        full.add(part, mv);

        ll use = min(ll(b), (need + l - 1) / l);
        need = max(0ll, need - use * l);
        use += (need + u - 1) / u;

        ans = min(ans, use + ll(X) * (ftake - (ftake > part)));
    }

    cout << ans << endl;

    return 0;
}


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

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

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

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

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

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

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

	Would you please return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using Graph=vector<vector<ll>>;
ll N,M;
int main(){
  cin>>N>>M;
  Graph G(N);
  for(int i=0;i<M;i++){
  ll u,v;cin>>u>>v;u--;v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }vector<ll>dist(N,-1);
  ll a=0;ll b=0;ll c=0;
  for(int i=0;i<N;i++){
  if(dist[i]==-1){
  bool x=true;
    dist[i]=0;
    queue<ll>que;que.push(i);ll sum=1;
    while(!que.empty()){
    ll u=que.front();
      que.pop();
      for(auto v:G[u]){
      if(dist[v]!=-1 && dist[v]!=dist[u])
        continue;
        else if(dist[v]==dist[u] && x)
          x=false;
        else{
        dist[v]=dist[u]+1;sum++;que.push(v);
        }
      }
     }if(sum==1)b+=1;
    else{
    a++;
      if(x)c++;

    }

  }
  }ll ans=2*b*(N-b)+(b*b)+(a*a)+(c*c);
  cout<<ans<<endl;
  return 0;
}


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

int main(){

  int n;

  while(cin>>n,n!=0){
    int sa=0,sb=0,a,b;
    for(int i=0;i<n;i++){
      cin>>a>>b;
      if(a==b){
	sa+=a;
	sb+=b;
      } else if(a>b){
	sa+=a+b;
      } else {
	sb+=a+b;
      }
    }

    cout<<sa<<" "<<sb<<endl;
  }

}


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

int main() {
	int N, Q;
	cin >> N >> Q;
	vector<tuple<int, int, int>>stx(N);
	for (int n = 0;n<N;++n) {
		cin >> get<1>(stx[n]) >> get<2>(stx[n]) >> get<0>(stx[n]);
	}
	sort(stx.begin(), stx.end());

	set<pair<int, int>> d;
	for (int i = 0, x; i < Q; ++i) {
		cin >> x;
		d.emplace(x, i);
	}

	vector<long long>tm(Q, -1);
	for (int i = 0;i<N;++i) {
		auto first = d.lower_bound(make_pair(get<1>(stx[i]) - get<0>(stx[i]), 0));
		auto end = d.upper_bound(make_pair(get<2>(stx[i]) - get<0>(stx[i]) - 1, Q));

		for (auto it = first; it != end; ++it) {
			tm[it->second] = get<0>(stx[i]);
		}
		d.erase(first, end);
	}
	for (int i = 0; i < Q; ++i) cout << tm[i] << '\n';
	return 0;
}

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

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

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

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

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

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

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

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

#pragma region イベントソート
using type = pair<int, ll>; // 1: query, 0; delete, -1: insert
using event = pair<ll, type>;
#pragma endregion

int main()
{
	int n, q; cin >> n >> q;
	vector<event> v;
	rep(i, n)
	{
		ll s, t, x; cin >> s >> t >> x;
		v.push_back({ s - x,{-1,x} });
		v.push_back({ t - x,{0,x} });
	}
	rep(i, q)
	{
		ll d; cin >> d;
		v.push_back({ d,{1,0} });
	}
	sort(all(v));
	multiset<ll> s;
	for (auto p : v)
	{
		ll e = p.second.first;
		ll pos = p.second.second;
		if (e == -1) s.insert(pos);
		else if (e == 0)s.erase(s.lower_bound(pos));
		else
		{
			if (s.empty())cout << -1 << endl;
			else cout << *s.begin() << endl;
		}
	}
}


#include <iostream>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

#define rep(i,n) for(int i=0; i < n; i++)
using ll =  long long;
using namespace std;
#define pi 3.14159265358979

int main()
{
	int n, minNum = 1010101, ans = 0;
	cin >> n;

	rep(i, n)
	{
		int x;
		cin >> x;
		if (x <= minNum) ans++;		// 入力値より前の値以下であれば条件を満たす
		minNum = min(x, minNum);	// その時点での最大値更新
	}
	cout << ans << endl;
}

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

#define int long long

#define ii pair<int,int>
#define fi first
#define sc second

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

void solve() {
  int n;
  cin >> n;
  set<int> s;
  for (int i = 0; i < n; i++) {
    int t; cin >> t;
    if (s.count(t)) s.erase(t);
    else s.insert(t);
  }
  cout << s.size() << '\n';
}

signed main() {
#ifdef _DEBUG
  // freopen("in" , "r", stdin );
  // freopen("out", "w", stdout);
#endif
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) solve();
}


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

signed main(){
  int N;
  cin>>N;
  int ans=1;
  while(ans*ans<=N)ans+=1;
  ans-=1;
  cout<<ans*ans<<endl;
  return 0;
}

#include <bits/stdc++.h>
#define fastIO (cin.tie(0), cout.tie(0), ios::sync_with_stdio(false))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define precise(i) fixed << setprecision(i)
using ll = unsigned long long int;
using namespace std;

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

    do {
        double t = (int)sqrt(n);
        if (t * t == n) {
            cout << n << endl;
            return 0;
        }
    } while (n--);
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,i,j,ans;
  string s;
  cin>>n>>s;
  vector<vector<int>> z(2,vector<int>(n+1));
  for(i=1;i<=n;i++){
    for(j=0;j<2;j++) z.at(j).at(i)+=z.at(j).at(i-1);
    if(s.at(i-1)=='E') z.at(0).at(i)++;
    else if(s.at(i-1)=='W') z.at(1).at(i)++;
  }
  ans=n;
  for(i=1;i<=n;i++) ans=min(ans,z.at(1).at(i-1)+z.at(0).at(n)-z.at(0).at(i));
  cout<<ans<<endl;
}

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

using namespace std;

constexpr long long mod = 1000000007;
constexpr double eps = 0.00001;

int main() {
    int n;
    cin >> n;
    long long *a = new long long[n];
    multiset<long long> b;
    for(int i = 0;i < n;i++){
        cin >> a[i];
        b.insert(a[i]);
    }
    sort(a,a+n,greater<>());
    int c = 0;
    for(int i = 0;i < n;i++) {
        if(b.find(a[i]) == b.end()) continue;
        b.erase(b.find(a[i]));
        long long t = 1;
        while(t <= a[i]*2) {
            if(b.find(t-a[i]) != b.end()) {
                c++;
                b.erase(b.find(t-a[i]));
                break;
            }
            t = t << 1;
        }
    }
    cout << c << endl;
}


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

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

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

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

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

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

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

#include<bits/stdc++.h>
using namespace std;
#define int  long long
#define tc(t)  int t; cin>>t; while(t--)
#define f(n)    for(int i=0;i<n;i++)
#define endl   "\n"

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     int n,m;
     cin>>n>>m;
     vector<pair<int,string>>v(m);
     vector<pair<int,int>>dp(n,{0,0});
     for(int i=0;i<m;i++){
         int q;
         string verdict;
         cin>>q>>verdict;
         v[i]={q,verdict};
     }

     int correct=0,penality=0;
     for(int i=0;i<m;i++)
     {
       int qn=v[i].first;
       string verdict=v[i].second;
       if(verdict=="WA")
       {
           if(dp[qn-1].second==0)
           {dp[qn-1].first++;}

       } else{
           if(dp[qn-1].second==0) {
               dp[qn - 1].second = 1;
               correct++;
               penality+=dp[qn-1].first;
           }
       }

     }
     cout<<correct<<" "<<penality<<endl;

}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<double, string> pds;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

bool cmp(pds a, pds b) {
	return (a.first == b.first) ? (a.second < b.second) : (a.first > b.first);
}

int main() {
	for (int N; cin >> N&&N;) {
		vector<pds> p;
		for (int i = 0; i < N; i++) {
			string L; int P, A, B, C, D, E, F, S, M;
			cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M;
			int Q = (S*F)*M;
			double cp = (double)(Q - P) / (A + B + C + (D + E)*M);
			p.push_back(pds(cp, L));
		}
		sort(p.begin(), p.end(), cmp);
		for (int i = 0; i < p.size(); i++) {
			cout << p[i].second /*<< " " << p[i].first*/ << endl;
		}
		cout << "#" << endl;
	}
	return 0;
}

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

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

    double sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != j) {
                int xx = x[i] - x[j];
                int yy = y[i] - y[j];
                double d = sqrt(xx*xx + yy*yy);
                sum += d;
            }
        }
    }
    double ans = sum / (double)n;
    printf ("%.7lf", ans);
}


#include <iostream>
#include <stack>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <functional>
#include <string>
#include <cstdlib>
#include <numeric>
#include <cstdbool>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <iomanip>

typedef long long ll;

#define rep(i,n) for(ll i=0;i<n;i++)
using namespace std;

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

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h,n, w,m,t; string s;
ll R, C;
ll sy, sx, gy, gx;
char maze[510][510];
ll dis[20][20];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
ll gcd(ll x, ll y) {
	if (x % y == 0) return y;
	return gcd(y, x % y);
}
ll lcm(ll a,ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}
void comb(vector<vector <ll> >& v) {
	for (ll i = 0; i < v.size(); i++) {
		v[i][0] = 1;
		v[i][i] = 1;
	}
	for (ll k = 1; k < v.size(); k++) {
		for (int j = 1; j < k; j++) {
			v[k][j] = (v[k - 1][j - 1] + v[k - 1][j])%ma;
		}
	}
}

ll GetDigit(ll num) {
	return log10(num) + 1;
}
ll Combination(int n, int r)
{
	if (n - r < r) r = n - r;
	if (r == 0) return 1;
	if (r == 1) return n;

	vector<ll> numerator(r);
	vector<ll> denominator(r);

	for (int k = 0; k < r; k++)
	{
		numerator[k] = n - r + k + 1;
		denominator[k] = k + 1;
	}

	for (int p = 2; p <= r; p++)
	{
		int pivot = denominator[p - 1];
		if (pivot > 1)
		{
			int offset = (n - r) % p;
			for (int k = p - 1; k < r; k += p)
			{
				numerator[k - offset] /= pivot;
				denominator[k] /= pivot;
			}
		}
	}

	ll result = 1;
	for (int k = 0; k < r; k++)
	{
		if (numerator[k] > 1) result *= numerator[k];
		result %= ma;
	}

	return result;
}

ll bfs() {
	queue<P> que;
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) dis[i][j] = INF;
	}
	que.push(P(sy - 1, sx - 1));
	dis[sy - 1][sx - 1] = 0;

	while (que.size() > 0) {
		P p = que.front();
		que.pop();
		if (p.first == gy - 1 && p.second == gx - 1) break;
		for (ll i = 0; i < 4; i++) {
			ll ny = p.first + dy[i];
			ll nx = p.second + dx[i];
			if (0 <= ny && ny < R && 0 <= nx && nx < C && maze[ny][nx] == '.' && dis[ny][nx] == INF) {
				que.push(P(ny, nx));
				dis[ny][nx] = dis[p.first][p.second] + 1;
			}
		}
	}
	return dis[gy - 1][gx - 1];
}

int main() {
	ll k, t;
	cin >> k >> t;
	vector<ll> a(t);
	rep(i, t) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	ll p = a[t - 1];
	cout << max(p - 1 - (k - p), (ll)0) << endl;
}

#include <iostream> // cout, endl, cin
#include <cmath> //sqrt pow
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower

#define rep(i,n) for(int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main(){
  int k,t;
  cin >> k >> t;
  vi a(t);
  int maxnum = 0, pos;
  rep(i, t){
    cin >> a[i];
    if(a[i] > maxnum){
      maxnum = a[i];
      pos = t;
    }
  }
  int ans = maxnum - 1;
  ans = max(ans - k + maxnum, 0);
  cout << ans << endl;
}


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

void print(int a, int b, char str[]);
void rev(int a, int b, char str[]);
void rep(int a, int b, char str[], char repstr[]);

int main()
{
	char str[1000];
	int orderlen;
	cin >> str;
	cin >> orderlen;
	cin.clear();
	cin.ignore();
	for(int i=0; i<orderlen; i++)
	{
		char orderstr[1000], buf[1100],
			pripar[3][8]={
				"print",
				"reverse",
				"replace"
		};
		int a, b, j=0;
		enum __ORDER{PRI, REV, REP, NONE} ORDER;
		cin.getline(buf, sizeof(buf));
		while(true){
			if(buf[j]==' '){
				bool print=true, replace=true, reverse=true;
				for(int k=0; k<j; k++){
					if(print) if(pripar[0][k] != buf[k]) print = false;
					if(reverse) if(pripar[1][k] != buf[k]) reverse = false;
					if(replace) if(pripar[2][k] != buf[k]) replace = false;
					if(!print && !reverse && !replace) break;
				}
				if(print){
					ORDER = PRI;
				} else if(reverse){
					ORDER = REV;
				} else if(replace){
					ORDER = REP;
				} else {
					ORDER = NONE;
				}
				j++;
				{
					int num=0;
					char numbuf[4]="   ";
					while(true){
						numbuf[num] = buf[j];
						num++;
						j++;
						if(buf[j]== ' ' || buf[j]=='\0') break;
					}
					a = atoi(numbuf);
					j++;
					num = 0;
					while(true){
						numbuf[num] = buf[j];
						num++;
						j++;
						if(buf[j]== ' ' || buf[j]=='\0') break;
					}
					b = atoi(numbuf);
					j++;
				}
				break;
			}
			j++;
		}
		if(ORDER == REP){
			int num = 0;
			while(true){
				if(buf[j]=='\0') break;
				orderstr[num] = buf[j];
				num++;
				j++;
			}
		}
		switch(ORDER){
		case PRI:
			print(a, b, str);
			break;
		case REV:
			rev(a, b, str);
			break;
		case REP:
			rep(a, b, str, orderstr);
			break;
		default:
			break;
		}
	}
	return 0;
}

void print(int a, int b, char str[])
{
	for(int i=a; i<=b; i++){
		cout << str[i];
	}
	cout << endl;
}
void rev(int a, int b, char str[])
{
	char tmp;
	for(int i=a, j=b; i<j; i++){
		tmp = str[i];
		str[i] =str[j];
		str[j] = tmp;
		j--;
	}
}
void rep(int a, int b, char str[], char repstr[])
{
	int i=0;
	for(int j=a; j<=b; j++){
		str[j] = repstr[i];
		i++;
	}
}

#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod=1e9+7;

signed main(){
  string s;
  cin>>s;
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    string t;
    cin>>t;
    int a,b;
    cin>>a>>b;
    if(t=="reverse")reverse(s.begin()+a,s.begin()+b+1);
    if(t=="replace"){
      string p;
      cin>>p;
      for(int j=a;j<=b;j++)s[j]=p[j-a];
    }
    if(t=="print"){
      for(int j=a;j<=b;j++)cout<<s[j];
      cout<<endl;
    }
  }
}


#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a,ll b){
  if(b == 0) return a;
  return gcd(b , a % b);
}

const ll MOD = 998244353;
const ll MAX = 2000001;

ll mod(ll a){
  return a % MOD;
}

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

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

void nCrprep() {
    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 nCr(ll n, ll r){
    if (n < r) return 0;
    if (n < 0 || r < 0) return 0;
    return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

ll nCrcheep(ll n,ll r){
  if(r == 0) return 1;
  else if(r == 1) return n;
  else return nCrcheep(n-1,r-1)*n/r;
}

int main(){
  nCrprep();

  ll n,m,k; cin >> n >> m >> k;
  vl mpow(n+1); mpow[0] = 1;

  rep(i,n){
    mpow[i+1] = mod(mpow[i]*(m-1));
  }

  ll sum = 0;

  rep(i,k+1){
    sum += mod(mod(m*mpow[n-i-1])*nCr(n-1,i));
    sum = mod(sum);
  }

  cout << sum << endl;

}

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

// #define int ll
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using si = set<int>;
using ii = pair<int, int>;
using vii = vector<ii>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define dotc()    int tcs; if (cin >> tcs) rep(tc, tcs)
#define sz(x)     (int)x.size()
#define eb        emplace_back
#define pb        push_back
#define mp        make_pair
#define all(x)    x.begin(), x.end()
#define rall(x)   x.rbegin(), x.rend()
#define fi        first
#define se        second

int inf(0x3fffffff);
int mod(998244353);
double pi(acos(-1.0));

ll iv(ll a, ll b) {
  return a > 1 ? b - iv(b % a, a) * b / a : 1;
}

ll modExp(ll b, ll e) {
  ll r = 1;
  while (e) {
    if (e & 1) r = r * b % mod;
    b = b * b % mod;
    e /= 2;
  }
  return r;
}

vector<ll> dpc;

ll C(int n, int r) {
  if (dpc.empty()) {
    dpc.pb(1);
    for (int i = 1; i <= n; i++) {
      dpc.pb(dpc[i - 1] * (n - i + 1) % mod * iv(i, mod) % mod);
    }
  }
  return dpc[r];
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("io\\in.txt", "r", stdin);
  freopen("io\\out.txt", "w", stdout);
#endif

  int n, m, k;
  cin >> n >> m >> k;

  ll cnt = 0;
  rep(i, k + 1) {
    cnt = (cnt + m * modExp(m - 1, n - i - 1) % mod *
           C(n - 1, i)) % mod;
  }

  cout << cnt;

  return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cassert>
#define ll long long
using namespace std;
inline ll read(){
	ll re=0,flag=1;char ch=getchar();
	while(ch>'9'||ch<'0'){
		if(ch=='-') flag=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') re=(re<<1)+(re<<3)+ch-'0',ch=getchar();
	return re*flag;
}
ll d,len,ans=0,mi[20],me[20],cur[20];
void dfs(ll n,ll val,ll tmp){
//	cout<<"dfs "<<n<<' '<<val<<' '<<tmp<<' '<<d<<' '<<mi[len-n-1]<<' '<<mi[n-1]<<'\n';
	if(n>(len/2-1)){

		if(len%2) tmp*=10;
		if(val==d) ans+=tmp;
		return;
	}
	ll i=-9;
	while(i<=9&&i*(mi[len-n-1]-mi[n])+val<=d) i++;
	i--;cur[n]=i;
	dfs(n+1,val+i*(mi[len-n-1]-mi[n]),tmp*(me[i+10]-(n==0&&i>=0)));
	if(i==9) return;
	i++;cur[n]=i;
	dfs(n+1,val+i*(mi[len-n-1]-mi[n]),tmp*(me[i+10]-(n==0&&i>=0)));
}
int main(){
	d=read();ll i,j;
	mi[0]=1;
	for(i=1;i<=18;i++) mi[i]=mi[i-1]*10;
	for(i=0;i<=9;i++) for(j=0;j<=9;j++) me[i-j+10]++;
	for(len=0;len<=19;len++) dfs(0,0,1);
	printf("%lld\n",ans);
//	system("pause");
}


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

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

int main()
{
    int a,b,c; cin >> a >> b >> c;
    int cnt = 0;
    if(a == b) cnt++;
    if(a == c) cnt++;
    if(b == c) cnt++;
    if(cnt == 1) cout << "Yes" << endl;
    else cout << "No" << endl;
}

#include<iostream>
using namespace std;

int main() {
	int D, T, S;
	cin >> D >> T >> S;
	int A = S * T;

	if (A >= D) {
		cout << "Yes\n";
	}
	else {
		cout << "No\n";
	}

	return 0;
}


#include <bits/stdc++.h>

#define F(i, a, b) for (ll i = a; i <= b; i ++)
#define G(i, a, b) for (ll i = a; i >= b; i --)
#define out4(x, y, z, w) { if (debug) printf(#x" = "),W(x),printf(" "#y" = "),W(y),printf(" "#z" = "),W(z),printf(" "#w" = "),W(w),putc('\n'); }
#define out3(x, y, z) { if (debug) printf(#x" = "), W(x), printf(" "#y" = "), W(y), printf(" "#z" = "), W(z), putc('\n'); }
#define out2(x, y) { if (debug) printf(#x" = "), W(x), printf(" "#y" = "), W(y), putc('\n'); }
#define out1(x) { if (debug) printf(#x" = "), W(x), putc('\n'); }
#define outline(x) { if (debug) puts("-------------------------------------------"#x"----------------------------------------"); }
#define outarr(a, L, R) { if (debug) { printf(#a"[%d..%d] = ", L, R); F(i, L, R) W(a[i]), putc(' '); putc('\n'); } }
#define Assert(x) { if ((debug) && (!(x))) { puts("Wrong Answer"); exit(0); } }
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mem(a, b) memset(a, b, sizeof a)
#define mec(a, b) memcpy(a, b, sizeof a)
#define mx(a, b) ((a) = max(a, b))
#define mn(a, b) ((a) = min(a, b))
#define lowbit(x) ((x) & (-(x)))
#define abs(x) ((max(x, - (x))))
#define get getchar()
#define pb push_back
#define putc putchar
#define inf 2e9

template <typename Int>
void R(Int &x) {
	char c = get; x = 0; Int t = 1;
	for (; !isdigit(c); c = get) t = (c == '-' ? - 1 : t);
	for (; isdigit(c); x = (x << 3) + (x << 1) + c - '0', c = get); x *= t;
}
template <typename Int>
void W(Int x) {
	if (x < 0) { putc('-'); x = - x ; }
	if (x > 9) W(x / 10); putc(x % 10 + '0');
}

#define debug (0)

using namespace std;

#define ll long long
const ll N = 1e5 + 10;
ll n, s, x[N], p[N];
struct node { ll s, v; };

node Dfs(ll l, ll r) {
	out2(l, r);
	outarr(p, 1, n);
	if (s <= x[l]) return {x[r], x[r] - s};
	if (s >= x[r]) return {x[l], s - x[l]};
	if (p[l] < p[r]) {
		p[r] += p[l];
		node a = Dfs(l + 1, r);
		return {x[l], a.v + a.s - x[l]};
	}
	else {
		p[l] += p[r];
		node a = Dfs(l, r - 1);
		return {x[r], a.v + x[r] - a.s};
	}
}

int main() {
//	freopen("data.in","r",stdin);

	R(n), R(s);
	F(i, 1, n) R(x[i]), R(p[i]);

	W(Dfs(1, n).v);
}

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

inline int gi()
{
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	int sum = 0;
	while('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
	return sum;
}

typedef long long ll;
const int maxn = 100005;

int n, s, x[maxn];
ll ans, p[maxn];

int main()
{
	n = gi(); s = gi();
	for (int i = 1; i <= n; ++i) x[i] = gi(), p[i] = gi();

	int l = 1, r = n, d = p[1] >= p[n];
	while (1) {
		if (x[l] >= s) {ans += x[r] - s; break;}
		if (x[r] <= s) {ans += s - x[l]; break;}
		if (p[l] >= p[r]) {
			if (d != 0) {ans += x[r] - x[l]; d ^= 1;}
			p[l] += p[r--];
		} else {
			if (d != 1) {ans += x[r] - x[l]; d ^= 1;}
			p[r] += p[l++];
		}
	}

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

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

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

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

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

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

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

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

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

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios:: sync_with_stdio(false);
  cin.tie();
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> sum(n + 5, 0);
  for (int i = 0; i + 1 < n; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      sum[i + 1]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    sum[i] += sum[i - 1];
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    int ans = sum[r - 1] - sum[l - 1];
    cout << ans << '\n';
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
ll factorial(int i) {       //階乗
    if (i == 0) return 1;
    return (factorial(i - 1)) * i;
}

int main() {
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<int> ac(n);
    vector<int> sum(n + 1, 0);
    rep(i, n) {
        if(i != n) {
            if(s[i] == 'A' && s[i + 1] == 'C') {
                ac[i]++;
                i++;
            }
        }
    }
    rep(i, n) {
        sum[i + 1] += sum[i] + ac[i];
    }
    /*cout << endl;
    rep(i, n) cout << ac[i] << ' ';
    cout << endl;
    rep(i, n + 1) cout << sum[i] << ' ';
    cout << endl;
    //確認済*/
    vector<vector<int>> l(q, vector<int>(2));
    rep(i, q) {
        rep(j, 2) cin >> l[i][j];
    }
    rep(i, q) {
        cout << sum[l[i][1] - 1] - sum[l[i][0] - 1] << endl;
    }

}


#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
typedef long long ll;

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

string s[12];
bool used[12][12];

void dfs(int y, int x) {
    used[y][x] = true;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < 12 && 0 <= ny && ny < 12 && s[ny][nx] == '1' && used[ny][nx] == false) {
            dfs(ny, nx);
        }
    }
}

int main(void) {
    while (1) {
        bool flag = false;
        for (int i = 0; i < 12; i++) {
            if (!(cin >> s[i])) flag = true;
        }
        if (flag) break;
        for (int i = 0; i < 12; i++)
            for (int j = 0; j < 12; j++)
                used[i][j] = false;
        int ans = 0;
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++) {
                if (s[i][j] == '1' && !used[i][j]) {
                    ans++;
                    dfs(i, j);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

#include<cstdio>
int sima(int x,int y);
char SIMA[14][14];
int main ()
{
    while(scanf("%s",SIMA[1]+1)!=EOF){
        int i,j,ans=0;
        for(i=2;i<13;i++){
            scanf("%s",SIMA[i]+1);
        }
        for(i=1;i<13;i++){
            for(j=1;j<13;j++){
                if(SIMA[j][i]=='1'){
                    ans+=sima(j,i);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
int sima(int x,int y)
{
    if(SIMA[x][y]=='0' || SIMA[x][y]==0) return 1;
    else SIMA[x][y]='0';
    return sima(x+1,y)*sima(x,y+1)*sima(x-1,y)*sima(x,y-1);
}


#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<time.h>
#include<sstream>
#define MINN -1e8
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f3f3f;
const int P=1e9+7;
const int MAXN=32000;
const double pi=3.1415926;
const ll mod=1000000007;
int n;
ll ans=1;
char c[200005];
ll f(ll len)
{
	ll cur=1;
	for(ll i=1;i<=len;i++)
	{
		cur=cur*i%mod;
	}
	return cur;
}
int main()
{
	cin>>n;getchar();
	ll tmp=1,len=1;
	for(int i=0;i<2*n;i++)c[i]=getchar();
	if(c[0]!='B'||c[n*2-1]!='B')
	{
		cout<<0;return 0;
	}
	for(int i=1;i<2*n-1;i++)
	{
		if(c[i]=='B')
		{
			if(tmp%2==1)
			{
				ans=ans*tmp%mod;
				tmp--;len++;
			}
			else tmp++;
		}
		else
		{
			if(tmp%2==1) tmp++;
			else
			{
				ans=ans*tmp%mod;
				tmp--;len++;
			}
		}
	}
	if(tmp!=1)
	{
		cout<<0;return 0;
	}
	else
	{
		ans=ans*f(len)%mod;
		cout<<ans;
	}
	return 0;
}

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <queue>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <complex>

long long int MOD = 1e9 + 7;

int main(){

  // read problem
  long long int N;
  std::string Stmp;
  scanf("%lld", &N);
  std::cin >> Stmp;
  std::string S = "W" + Stmp;

  std::cerr << "S: " << std::endl << S << std::endl;

  // solve problem
  long long int tmp_left = 0;
  long long int tmp_state = 0; // left
  long long int ans = 1;
  long long int possible = 1;

  for(long long int i = 1; i <= 2*N; i++){
    std::cerr << i << " " << tmp_left << " " << tmp_state << std::endl;
    if(S[i] != S[i-1]){
      // keep tmp_state
      if(tmp_state == 0){ // left
        tmp_left++;
      }
      else{// right
        ans *= tmp_left;
        ans = (ans+MOD)%MOD;
        tmp_left--;
      }
    }
    else{
      // change tmp_state
      if(tmp_state == 0){
        tmp_state = 1; // right
        ans *= tmp_left;
        ans = (ans+MOD)%MOD;
        tmp_left--;
      }
      else{
        tmp_state = 0; // left
        tmp_left++;
      }
    }
    // check answer
    if(tmp_left < 0){
      ans = 0;
      break;
    }
  }

  // left has not been used up
  if(tmp_left != 0){
    ans = 0;
  }

  // order of choosing pairs
  for(long long int i = 1; i <= N; i++){
    ans *= i;
    ans = (ans+MOD)%MOD;
  }

  std::cerr << "Answer: " << std::endl;
  std::cout << ans << std::endl;

  return 0;
}


#include <iostream>
using namespace std;

int main() {
	long long N, A, B;
	cin>>N>>A>>B;
	long long arr[N];
	for (int i=0; i<N; i++){
		cin>>arr[i];
	}
	long long sum=0;
	for (int i=0; i<N-1; i++){
		if (A*(arr[i+1]-arr[i])>B){
			sum += B;
		}
		else sum += A*(arr[i+1]-arr[i]);
	}
	cout << sum << endl;
}

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0;i < (n);i++)
using ll = long long;
const ll MOD=1000000007;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);

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

int main(){
  ll A,B,M; cin>>A>>B>>M;
  vector<ll> a(A),b(B);
  ll mina=INF,minb=INF;
  rep(i,A) {cin>>a.at(i); chmin(mina,a.at(i));}
  rep(i,B) {cin>>b.at(i); chmin(minb,b.at(i));}

  ll ans=mina+minb;
  rep(i,M){
    ll x,y,c; cin>>x>>y>>c; x--; y--;
    ll sum=a.at(x)+b.at(y)-c;
    chmin(ans,sum);
  }

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


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <numeric>
#define ll long long
#define wolfe ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define elif else if
using namespace std;
int main () {
    wolfe;
    int n;
    cin >> n;
    int sum = 0;
    while(n>=500) {
        n -= 500;
        sum += 1000;
    }
    while(n>=5) {
        n -=5;
        sum += 5;
    }
    cout << sum << endl;
    return 0;
}

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

int main(){
    ll n;
    vector <ll> a(5);
    cin >> n >> a[0] >> a[1] >> a[2] >> a[3] >> a[4];
    vector <ll> z(6,0);
    z[0]=n;
    rep(i,4){
        a[i+1]=min(a[i],a[i+1]);
    }

    ll ans;
    if(n%a[4]==0){
    ans=n/a[4];
    }
    else{
        ans=n/a[4]+1;
    }
    ans+=4;

    cout << ans << endl;

    return 0;
}


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

int main(){
    map<string,int>s;
    map<string,int>t;
    int n,m;
    cin>>n;
    string str;
    for(int i=0;i<n;i++){
        cin>>str;
        s[str]++;
    }
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>str;
        t[str]++;
    }
    int ans=0;
    for(auto i=s.begin();i!=s.end();i++){
        ans=max(ans,s[i->first]-t[i->first]);
    }
    cout<<ans<<endl;

    return 0;
}

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define INF 1000000000000
#define MOD 1000000007
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) \
    cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                 \
    cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
         << x3 << endl
#define debug_4(x1, x2, x3, x4)                                             \
    cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
         << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                         \
    cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
         << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                        \
    CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
    (__VA_ARGS__)
#else
#define debug(...)
#endif

using namespace std;
using ll = long long;

int main() {
    ll n, cnt = 0;
    cin >> n;
    vector<ll> 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(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());

    for (auto v_b : b) {
        // 未満を求める
        auto iter_a = lower_bound(a.begin(), a.end(), v_b);
        auto idx_a = distance(a.begin(), iter_a);
        // 上を求める
        auto iter_c = upper_bound(c.begin(), c.end(), v_b);
        auto idx_c = distance(c.begin(), iter_c);
        debug(v_b, idx_a, idx_a, c.size() - idx_c);
        cnt += (c.size() - idx_c) * (idx_a);
    }
    cout << cnt << endl;
    return 0;
}


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

int main() {
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  const int dx = tx - sx, dy = ty - sy;

  // print string(how many, what)
  cout << string(dy, 'U') << string(dx, 'R');  // path 1 (s->t)
  cout << string(dy, 'D') << string(dx, 'L');  // path 2 (t->s)
  cout << 'L' << string(dy+1, 'U') << string(dx+1, 'R') << 'D';  // path 3 (s->t)
  cout << 'R' << string(dy+1, 'D') << string(dx+1, 'L') << 'U';  // path 4 (t->s)
  cout << endl;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  v(string) cmd(3);
  rep(i,3) {
    cin >> cmd[i];
  }
  vi pos(3,0);
  int now=0;
  while(1) {
    if(pos[now]>=sz(cmd[now])) {
      cout << char(now+'A') << endl;
      return 0;
    }
    int next = cmd[now][pos[now]]-'a';
    pos[now]++;
    now=next;
  }

  return 0;
}


#include <iostream>
#include <climits>
#include <vector>
#include <cmath>
using namespace std;

vector<int> A;
vector<int> g;

int n, m;
int cnt;

auto prn=[=](){
    for(auto a : A)
        cout<<a<<"\n";
};

void _insSort(int g){
    for(int i=g-1;i<n;i++){
        int maxi=i;
        for(int j=i+g;j<n;j+=g){
            if(A[j] < A[maxi]){
                maxi=j;
            }
        }
        if(i!=maxi){
            swap(A[i],A[maxi]);
            cnt++;
        }
    }
 //   cout<<"----"<<g<<"-----\n";
 //   prn();
}

void insSort(int g){
    for(int i=g;i<n;i++){
        int v = A[i];
        int j = i - g;
        while(j>=0 && A[j] > v){
            A[j+g] = A[j];
            j -= g;
            cnt++;
        }
        A[j+g] = v;
    }
}

void  ShellSort(){
    int i;
    cnt=0;
    m = n;
    if(m>100)
	m=100;
    g.push_back(1);
    for(i=1;pow(4,i)+3*pow(2,i-1)+1<n&&i<=m;i++){
        g.push_back(pow(4,i)+3*pow(2,i-1)+1);
    }
    if(i!=m)
	m=i;
    for(i=g.size()-1;i>=0;i--)
        insSort(g[i]);
}

int main(){
    ios::sync_with_stdio(false);
    int  t;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>t;
        A.push_back(t);
    }
    ShellSort();
    cout<<m<<"\n";
    for(int i=g.size()-1;i>0;i--)
        cout<<g[i]<<" ";
    cout<<g[0]<<"\n";
    cout<<cnt<<endl;
    prn();
    return 0;
}

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

int insertionSort(int a[], int n, int g){
	int cnt = 0;
	for(int i=g;i<n;++i){
		int v = a[i];
		int j = i - g;
		while(j>=0 && a[j]>v){
			a[j+g] = a[j];
			j = j - g;
			++cnt;
		}
		a[j+g] = v;
	}
	return cnt;
}

int* interleavedlist(int n, int *m){
	int i = 1, j = 0;
	*m = log10(2*n+1) / log10(3);
	int *g = new int[*m];
	while(i<=n){
		g[j] = i;
		++j;
		i = 3 * i + 1;
	}
	return g;
}

void shellSort(int a[], int n){
	int cnt = 0;
	int m;
	int *g = interleavedlist(n,&m);

	cout<<m<<endl;
	for(int i=m-1;i>=0;--i)cout<<g[i]<<(i?" ":"");
	cout<<endl;

	for(int i=m-1;i>=0;--i){
		cnt += insertionSort(a,n,g[i]);
	}
	cout<<cnt<<endl;
}

int main() {
	int n, *data;

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

	shellSort(data,n);

	for(int i=0;i<n;++i){
		cout<<data[i]<<endl;
	}
	return 0;
}


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

long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}

bool is_prime(long long N) {
    if (N == 1) return false;
    for (long long i = 2; i * i <= N; ++i) {
        if (N % i == 0) return false;
    }
    return true;
}

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0;
        while (N % a == 0) {
            ++ex;
            N /= a;
        }
        res.push_back({a, ex});
    }
    if (N != 1) res.push_back({N, 1});
    return res;
}

int main() {
  int K,T,max=0,ans=0,sum=0;
  cin >> K >> T;
  vector<int> a(T);
  rep(i,T) {
    cin >> a[i];
    sum+=a[i];
    if(a[i]>max) max=a[i];
  }
  sum-=max;
  //sort(T.begin(),T.end(),greater<int>());
  if(sum>max-2) ans=0;
  else ans=max-sum-1;
  cout << ans << endl;

  return 0;
}

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

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

int main()
{
    int k,t;
    cin >> k >> t;
    vector<int> a(t);
    for (int i = 0; i < t; i++)
    {
        cin >> a[i];
    }

    sort(all(a));
    reverse(all(a));

    int ans = max(a[0] - 1 - (k - a[0]) ,0);

    cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define fi  first
#define se  second

typedef pair<int,int>   ii;

priority_queue<int> q;
vector<ii>  v;
int n, sum = 0;

signed main()  {
    ios_base::sync_with_stdio(false);   cin.tie(0);
    cin >> n;
    for(int i = 0 ; i < n ; ++i)    {
        int D, L;   cin >> D >> L;
        v.push_back(ii(D + L,L));
    }
    sort(v.begin(),v.end());
    for(int i = 0 ; i < n ; ++i)    {
        q.push(v[i].se);
        sum += v[i].se;
        if(sum > v[i].fi)   {
            sum -= q.top();
            q.pop();
        }
    }

    cout << q.size() << endl;
}

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
    int N; cin >> N;
    vector<tuple<int, int, int>> A(N);
    for (int i = 0; i < N; i++) {
        int h, p; cin >> h >> p;
        A[i] = make_tuple(h + p, h, p);
    }
    sort(begin(A), end(A));
    const Int INF = 1e18;
    vector<Int> dp(N+1, INF), dp2;
    dp[0] = 0;
    for (int i = 0; i < N; i++) {
        dp2 = dp;
        int h = get<1>(A[i]), p = get<2>(A[i]);
        for (int j = 0; j < N; j++) {
            if (dp2[j] == INF || dp2[j] > h) {
                break;
            }
            dp[j+1] = min(dp[j+1], dp2[j] + p);
        }
    }
    int ans = 0;
    for (int i = 0; i <= N; i++) {
        if (dp[i] != INF) {
            ans = i;
        }
    }
    cout << ans << '\n';
    return 0;
}


#include <stdio.h>

typedef struct _Gem{
	int x;
	int y;
	bool hit;
} Gem;

static void
pick_gem(Gem* g, int n, int x, int y)
{
	for (int i = 0; i < n; i++) {
		if (!g[i].hit && g[i].x == x && g[i].y == y) {
			g[i].hit = true;
			return;
		}
	}
}

static bool
check_gem(Gem* g, int n)
{
	for (int i = 0; i < n; i++) {
		if (g[i].hit == false) {
			return false;
		}
	}

	return true;
}

int
main(void)
{

	char line[80];
	int n;
	int m;

	Gem g[20];

	char d[30];
	int l[30];

	while (true) {
		if (fgets(line, sizeof line, stdin) == NULL) {
			return 1;
		}
		if (sscanf(line, "%d", &n) != 1) {
			return 1;
		}

		if (n == 0) {
			break;
		}

		for (int i = 0; i < n; i++) {
			if (fgets(line, sizeof line, stdin) == NULL) {
				return 1;
			}
			if (sscanf(line, "%d %d", &(g[i].x), &(g[i].y)) != 2) {
				return 1;
			}
			g[i].hit = false;
		}

		if (fgets(line, sizeof line, stdin) == NULL) {
			return 1;
		}
		if (sscanf(line, "%d", &m) != 1) {
			return 1;
		}

		for (int i = 0; i < m; i++) {
			if (fgets(line, sizeof line, stdin) == NULL) {
				return 1;
			}
			if (sscanf(line, "%c %d", &(d[i]), &(l[i])) != 2) {
				return 1;
			}
		}

		int x = 10;
		int y = 10;
		for (int i = 0; i < m; i++) {
			if (d[i] == 'N') {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, x, ++y);
				}
			} else if (d[i] == 'S') {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, x, --y);
				}
			} else if (d[i] == 'E') {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, ++x, y);
				}
			} else {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, --x, y);
				}
			}
		}
		printf("%s\n", (check_gem(g, n) ? "Yes" : "No"));
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
bool field[32][32];
const string dic="NESW";
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
int main(){
	while(true){
		int n;	cin>>n;
		if(n==0)	return 0;
		for(int i=0;i<32;i++)	for(int j=0;j<32;j++)	field[i][j]=false;
		for(int i=0;i<n;i++){
			int x,y;	cin>>x>>y;
			field[y][x]=true;
		}
		int m;	cin>>m;
		int cx=10,cy=10;
		for(int i=0;i<m;i++){
			char d;	int l;	cin>>d>>l;
			for(int j=0;j<4;j++){
				if(dic[j]==d){
					for(int k=0;k<l;k++){
						cx+=dx[j];
						cy+=dy[j];
						if(field[cy][cx]){
							n--;
							field[cy][cx]=false;
						}
					}
					break;
				}
			}
		}
		if(n==0)	cout<<"Yes"<<endl;
		else 	cout<<"No"<<endl;
	}
}


#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int main() {
	int n, p; // テ、ツクツ甘ゥツ卍?50 50
	int kouho[50];

	//10^6
	while(cin >> n >> p && n && p)
	{
		//cout << n << " " << p << endl;
		rep(i,50){kouho[i]=0;}
		int box = p;
		int index=0;
		while(true)
		{
			if(index >= n){ index=0; }
			if(box>0){
				box--; kouho[index]++;
			}else{
				box=kouho[index]; kouho[index]=0;
				bool flag=false;
				/*
				rep(i,n){if(kouho[i]!=0){flag=false;}}
				if(flag){cout << index << endl; break;}
				*/
				rep(i,n){
					if(kouho[i]==p){flag=true;}
				}
				if(flag){
					if(index==0){index=n;}
					cout << (index-1)%n << endl; break;
				}
			}
			index++;
		}
	}
	return 0;
}

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

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	if(n&1)for(int i=1;i<=m;i++){
		printf("%d %d\n",i,n-i);
	}
	else{
		int cnt = 0;
		for(int i=1;cnt<m&&i<=n/4;cnt++,i++){
			printf("%d %d\n",i,n/2-i+1);
		}
		int cur = 1;
		for(int i=n/2+1;cnt<m;i++,cnt++){
			printf("%d %d\n",i,n-cur);
			cur++;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i=0; i<(n); i++)
#define rep2(i, k, n) for (ll i=k; i<(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; }

const ll mod = 2019;

int main() {
    ll l, r;
    cin >> l >> r;
    map<ll, ll> m;
    for (ll i = l; i <= r && i <= l+5000; i++){
        m[i%mod]++;
    }
    ll ans = 2020;
    vector<ll> a;
    for (auto x: m){
        rep1(i, x.second) a.push_back(x.first);
    }
    ll n = a.size();
    for (ll i=0; i<(n); i++){
        for (ll j=i+1; j<(n); j++){
            ans = min(ans, a[i]*a[j]%2019);
        }
    }
    cout << ans << endl;
    //printf("%.12f", ans);
}

#include<bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
s.find_by_order(x) xth element in set
x.order_of_key(x) number of elements <x*/
#define ll long long
#define vi vector<int>
#define si set<int>
#define mii map<int,int>
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define extract_word(s)  stringstream str(s); while(str>>word)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define SET(s) cout<<fixed<<setprecision(s)
#define chotu 1000000007
#define set0(a) memset(a,0,sizeof(a))
#define endl "\n"
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define lower_string(s) transform(all(s),s.begin(),::tolower())
#define upper_string(s) transform(all(s),s.begin(),::toupper())
#define size(s) s.size()
template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }
template<typename T,typename U>
bool compare(T x,U y){return(abs(x-y)<=1e-9);}
void solve()
{
fastio
int l,r;
cin>>l>>r;
if(l==0)
cout<<0;
else
{
	if((r-l)>=2019)
	cout<<0;
	else
	{
		int ans=10000000;
		for(int i=l;i<=r;i++)
		{
			for(int j=i+1;j<=r;j++)
			ans=min(ans,((i%2019)*(j%2019))%2019);
		}
		cout<<ans;
	}
}
}
int main()
{
//code
fastio
int t;
t=1;
while(t--)
{
solve();
cout<<endl;
}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == b && b != c) {
        cout << "Yes\n";
    } else if (b == c && a != b) {
        cout << "Yes\n";
    } else if (a == c && a != b) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int a,b,c,d;
  cin>>a>>b>>c>>d;
  cout<<min(a,b)+min(c,d)<<endl;
}


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

int main(){
    int n;
    cin >> n;
    int k = 111;
    for(int i = 1; i < 10; i++){
        if(k >= n){
            cout << k << endl;
            return 0;
        }
        k += 111;
    }
}

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    int n;cin>>n;
    for(int i = n;i<10000;i++){
       string str = std::to_string(i);
       if(str[0] == str[1]&&str[1] == str[2]){
         cout<<i;
         return 0;
         break;
       }
    }

    return 0;
}


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

int main(){
  int n;
  cin >> n;
  vi a(n+1);
  rep(i,n){
    int x;
    cin >> x;
    a[i+1]=x;
  }

  int  m=0;
  vi b(n+1);
  for(int i=n;i>0;i--){
    for(int j=2*i;j<=n;j+=i){
      a[i]^=b[j];
    }
    if(a[i]){
      m++;
      b[i]++;
    }
  }

  cout<<m << "\n";
  rep(i,n+1){
    if(b[i]) cout << i << ' ';
  }

  return 0;
}

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Modulo{
const int mod=998244353;
il int ad(int x,int y){return x+y>=mod?x+y-mod:x+y;}
il int sub(int x,int y){return ad(x,mod-y);}
il int mul(int x,int y){return (ll)x*y%mod;}
il void inc(int &x,int y){x=ad(x,y);}
il void inc2(int &x,int y){x=mul(x,y);}
il int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
template<class ...Args>il int ad(const int a,const int b,const Args &...args) {return ad(ad(a,b),args...);}
template<class ...Args>il int mul(const int a,const int b,const Args &...args) {return mul(mul(a,b),args...);}
}
using namespace Modulo;
namespace Miracle{
const int N=1e6+5;
int n,m;
int jie[N],inv[N];
int C(int n,int m){
	return mul(jie[n],inv[m],inv[n-m]);
}
int main(){
    rd(n);rd(m);
    jie[0]=1;
    int lim=2*max(n,m);
    for(reg i=1;i<=lim;++i) jie[i]=mul(jie[i-1],i);
	inv[lim]=qm(jie[lim]);
	for(reg i=lim-1;i>=0;--i) inv[i]=mul(inv[i+1],i+1);
	int d=min(n,m);
	int sum=0;
	for(reg i=1;i<=d;++i){
		inc(sum,mul(C(2*i,i),C(n+m-2*i,n-i)));
	}
	inc2(sum,qm(2));
	inc(sum,mul(max(n,m),C(n+m,n)));
	int ans=mul(sum,qm(C(n+m,n)));
	cout<<ans;
	return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
*/


#include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

string change(string s,int n){
	string ret(s);
	rep(i,0,ret.size()){
		if(ret[i]>='a' && ret[i]<='z'){
			if(ret[i]+n>'z') ret[i]+=n-26;
			else ret[i]+=n;
		}
	}
	return ret;
}

int main(){
	string s;
	string str[3]={"this","that","the"};
	string stu[2]={" ","."};
	while(getline(cin,s)){
		bool f=0;
		rep(i,0,26){
			string t;
			t=change(s,i);
			rep(j,0,3){
				rep(k,0,2){
					if(t.find(str[j]+stu[k])!=string::npos){
						o(t); f=1; break;
					}
				}
				if(f) break;
			}if(f) break;
		}
	}
}

#include <iostream>
using namespace std;

int e, m;

int main(void){
  while(cin >> e && e != 0){
    m = 1000000;
    for(int y = 0; y <= 1000; y++){
      for(int z = 0; z <= 100; z++){
        int x = e - y * y - z * z * z;
        if(x < 0) continue;
        if(x + y + z < m) m = x + y + z;
      }
    }
    cout << m << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#define sz(arr) (int)arr.size()
#define all(arr) arr.begin(), arr.end()

using namespace std;

void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...) \
  T __VA_ARGS__;    \
  _cin(__VA_ARGS__)

int main() {
  Cin(int, n, h, w);

  cout << (n - h + 1) * (n - w + 1) << endl;

  return 0;
}

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

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

   for(int i=0;i<s.size();i++){
       if(s[i]=='h'){
           if(s[i+1]=='i'){
               i++;
           }else{
               cout << "No" << endl;
           return 0;
           }
       }else{
           cout << "No" << endl;
           return 0;
       }
   }
   cout << "Yes" << endl;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
	string buf;

	while (cin >> buf) {
		int amount = 0;
		while ( !(buf.empty()) ) {
			switch ( buf.at(0) ) {
			case 'M':
				buf.erase( buf.begin() ); amount += 1000; break;
			case 'D':
				buf.erase( buf.begin() ); amount += 500; break;
			case 'C':
				if ( buf.size() >= 2 ) {
					if ( buf.at(1) == 'M' ) { buf.erase(0,2); amount += 900; break; }
					else if ( buf.at(1) == 'D' ) { buf.erase(0,2); amount += 400; break; }
				}
				buf.erase( buf.begin() ); amount += 100; break;
			case 'L':
				buf.erase( buf.begin() ); amount += 50; break;
			case 'X':
				if ( buf.size() >= 2 ) {
					if ( buf.at(1) == 'C' ) { buf.erase(0,2); amount += 90; break; }
					else if ( buf.at(1) == 'L' ) { buf.erase(0,2); amount += 40; break; }
				}
				buf.erase( buf.begin() ); amount += 10; break;
			case 'V':
				buf.erase( buf.begin() ); amount += 5; break;
			case 'I':
				if ( buf.size() > 1 ) {
					if ( buf.at(1) == 'X' ) { buf.erase(0,2); amount += 9; break; }
					else if ( buf.at(1) == 'V' ) { buf.erase(0,2); amount += 4; break; }
				}
				buf.erase( buf.begin() ); amount += 1; break;
			}
			// cout << buf << endl;
		}
		cout << amount << endl;
	}

	return 0;
}


#include<bits/stdc++.h>

typedef long long ll;
using namespace std;
int main()
{
//ll SUN, MON, TUE, WED, THU, FRI, SAT;
string s;
cin>>s;
if(s=="SUN") cout<<7<<endl;
if(s=="MON") cout<<6<<endl;
if(s=="TUE") cout<<5<<endl;
if(s=="WED") cout<<4<<endl;
if(s=="THU") cout<<3<<endl;
if(s=="FRI") cout<<2<<endl;
if(s=="SAT") cout<<1<<endl;
}

/*
    @uthor: Varun Mishra
    "varun21999" ->  Codechef, Codeforce, Hackerrank, Hackerearth;
*/
//#include<boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int;
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int            long long int
#define ull            unsigned int
#define ld             long double
#define pb             push_back
#define pa             pair<int,int>
#define ppa            pair<pair<int,int>,int>
#define pap            pair<int,pair<int,int>>
#define v1d            vector<int>
#define v2d            vector<vector<int> >
#define vpa            vector<pair<int,int>>
#define M              1000000007
#define MN             LLONG_MIN
#define MX             LLONG_MAX
#define ff             first
#define ss             second
#define endl           "\n"
#define v1s            vector<string>
#define all(v)         v.begin(),v.end()
#define S(v)           sort(v.begin(),v.end())
#define RS(v)          sort(v.rbegin(),v.rend())
#define R(v)           reverse(v.begin(),v.end())
#define mxpq(T)        priority_queue <T>
#define mnpq(T)        priority_queue<T,vector<T>,greater<T>>
#define T              int tc;cin>>tc;while(tc--)
#define p2d(v)         for(auto a:v){for(auto b:a)cout<<b<<" ";cout<<endl;}
#define p1d(v)         for(auto a:v)cout<<a<<" ";cout<<endl;
#define ppd(v)         for(auto a:v)cout<<a.ff<<" "<<a.ss<<endl;
#define forIloop(i,n)  for(auto i=0;i<n;i++);
#define forDloop(i,n)  for(auto i=n-1;i>=0;i--);
#define qqq 400005
#define isEven(n)  (!n & 1);
const int MOD = 1e9 + 7;
// bool isEven(int n){
//     return (!(n & 1));
// }
// int popcount(int num){
//     num = num - ((num >> 1) & 0x55555555);
//     num = (num & 0x33333333) + ((num >> 2) & 0x33333333);
//     return ((num + (num >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
// }
// int powerOfTwo(int n){
//     return 1 << n;
// }

// int arr[1000001];
// bool temet = false;

void codeforce2(){
    string s; cin>>s;
    vector<string> temp{"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    int index = find(temp.begin(), temp.end(), s) - temp.begin();
    cout << 7 - index << endl;
    return;

}

int32_t main(){

    cout<<fixed<<setprecision(16);
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    /*#ifndef _DEBUG
     freopen("input.txt",  "r",  stdin);
     freopen("output.txt", "w", stdout);
    #endif*/
    //auto start = high_resolution_clock::now();

    // Lucario();
    //codeforceContest();
    //hackerEarth();
    //codeChef();
    codeforce2();

   /* auto stop = high_resolution_clock::now();
    auto duration = duration_cast<seconds>(stop - start);
    cout << "Time: "<<duration.count()<<endl;*/
    return 0;
}


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

int main()
{
    int N, x;
    vector<int> A;
    int ans = 0;
    cin >> N >> x;
    for (int i = 0; i < N; ++i)
    {
        int a;
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    for (int i = 0; i < N; ++i)
    {
        if (A[i] > x){
            break;
        }
        x -= A[i];
        ++ans;
    }
    if (x > 0 && ans == N){
        ans -= 1;
    }
    cout << ans << endl;
}

#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 rall(x) (x).rbegin(), (x).rend()
#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 vc = vector<char>;
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> ll rup(T a, T b) { return a % b ? a / b + 1 : a / b; }
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; }
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;
ll intpow(ll a, ll n, ll _mod=numeric_limits<ll>::max()) { ll p=1; while (n) { if (n&1) p=p*a%_mod; a=a*a%_mod; n>>=1; } return p; }
ll modc(ll a, char op, ll b, ll _mod=mod)
{
        a %= _mod; b %= _mod; ll res = 1;
        switch (op) {
        case '+': res = (a + b) % _mod; break;
        case '-': res = (a - b) % _mod; break;
        case '*': res = a * b % _mod; break;
        case '/': res = modc(a, '*', modc(b, '^', _mod-2, _mod), _mod); break;
        case '^': res = intpow(a, b, _mod); break;
        case 'P': rep(i, a-b+1, a+1) res = modc(res, '*', i, _mod); break;
        case 'C': res = modc(modc(a, 'P', b, _mod), '/', modc(b, 'P', b, _mod)); break;
        }
        if (res < 0) { res += _mod; } return res;
}

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

        vll a(N);
        rep(i, 0, N) cin >> a[i];

        ll ans = 0;
        sort(all(a));
        rep(i, 0, N) {
                if (i != N-1) {
                        if (x >= a[i]) {
                                x -= a[i];
                                ++ans;
                        }
                } else {
                        if (a.back() == x) ++ans;
                }
        }

        cout << ans << endl;
}


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

using ll = long long;

#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define len(x) int((x).size())

ll dp_real[2][301][301];
ll dp_suff[2][301][301];
ll dp_diag[2][301][301];

const ll p = 998244353LL;

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

	#ifdef ARTHUR_LOCAL
		ifstream cin("input.txt");
	#endif

	string s;
	int k;

	cin>>s>>k;

	k=min(k,300);

	int n = len(s);
	int zi=0;

	int initial_ones = 0;
	int ind=n-1;

	while(ind>=0 && s[ind]=='1')
	{
		ind--;
		initial_ones++;
	}

	// initialise the dp

	for(int i=0; i<=min(k,initial_ones); i++)
	{
		dp_real[0][i][i]=1;
		//cout << 0 << " " << i << " " << i << " " << 1 << endl;
	}

	// diag dudes is pretty easy to do

	for(int i=0; i<=k; i++)
	{
		for(int j=0; j<=k; j++)
		{
			dp_diag[0][i][j]=dp_real[0][i][j];

			if(i>0&&j>0)
			{
				dp_diag[0][i][j] += dp_diag[0][i-1][j-1];
				dp_diag[0][i][j] %= p;
			}
		}
	}
	// suff dudes ... not so bad either ???

	for(int i=0; i<=k; i++)
	{
		for(int j=k; j>=0; j--)
		{
			dp_suff[0][i][j]=dp_real[0][i][j];

			if(j<k)
			{
				dp_suff[0][i][j]+=dp_suff[0][i][j+1];
				dp_suff[0][i][j]%=p;
			}
		}
	}

	for(int ii=n-1; ii>=0; ii--)
	{
		if(s[ii]=='1') continue;

		// if(!skip_first)
		// {
		// 	skip_first=1;
		// 	continue;
		// }

		zi=1-zi;

		int a = 0;
		int copy_i = ii-1;

		while(copy_i>=0 && s[copy_i]=='1')
		{
			a++;
			copy_i--;
		}

		//cout << a << endl;

		for(int i=0; i<=k; i++)
		{
			for(int j=0; j<=k; j++)
			{
				// compute new real_dp[zi][i][j] value

				dp_real[zi][i][j] = dp_suff[1-zi][i][j];

				dp_real[zi][i][j] += dp_diag[1-zi][i][j];
				dp_real[zi][i][j] %= p;

				if(i-a > 0 && j-a > 0) // maybe add conditions on j < i or whatever
				{
					dp_real[zi][i][j] += p - dp_diag[1-zi][i-a-1][j-a-1];
					dp_real[zi][i][j] %= p;
				}

				dp_real[zi][i][j] += p-dp_real[1-zi][i][j];
				dp_real[zi][i][j] %= p;

				//cout << ii << " " << i << " " << j << " " << dp_real[zi][i][j] << endl;

				dp_diag[zi][i][j] = dp_real[zi][i][j];

				if(i>0&&j>0)
				{
					dp_diag[zi][i][j] += dp_diag[zi][i-1][j-1];
					dp_diag[zi][i][j] %= p;
				}
			}

			dp_suff[zi][i][k]=dp_real[zi][i][k];

			for(int j=k-1; j>=0; j--)
			{
				dp_suff[zi][i][j] = dp_real[zi][i][j];
				dp_suff[zi][i][j] += dp_suff[zi][i][j+1];
				dp_suff[zi][i][j] %= p;
			}
		}
	}

	ll ans=0LL;

	for(int i=0; i<=k; i++)
	{
		ans += dp_real[zi][i][0];
		ans %= p;
	}

	cout << ans << endl;
}

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

const int N = 18, mod = 1e9 + 7;

int dp[44][1 << N], x, y, z;
int yo(int n, int mask) {
	mask &= (1 << 18) - 1;
	if (mask >> x & 1 && mask >> (x + y) & 1 && mask >> (x + y + z) & 1) return 0;
	if (n == 0) return 1;
	int &ret = dp[n][mask];
	if (ret != -1) return ret;
	int ans = 0;
	for (int i = 1; i <= 10; i++) {
		ans += yo(n - 1, (mask << i) | (1 << i));
		if (ans >= mod) ans -= mod;
	}
	return ret = ans;
}
int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n >> x >> y >> z;
	memset(dp, -1, sizeof dp);
	int ans = 1;
	for (int i = 1; i <= n; i++) ans = 1LL * ans * 10 % mod;
	ans = (ans - yo(n, 0) + mod) % mod;
	cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define chmax(a,b) a=max(a,b)
int n,Q,bl;
int c[500005];
int ans=0,cnt[500005];
struct query{
	int l,r,id;
}q[500005];
int qans[500005];
bool cmp(query a,query b){
	return (a.r/bl)==(b.r/bl)?a.l<b.l:a.r<b.r;
}
void add(int x){
	if(!cnt[c[x]]){
		ans++;
	}
	cnt[c[x]]++;
}
void del(int x){
	cnt[c[x]]--;
	if(!cnt[c[x]]){
		ans--;
	}
}
int main(){
	cin>>n>>Q;
	bl=n/sqrt(Q*2/3);
	for(int i=1;i<=n;++i){
		cin>>c[i];
	}
	for(int i=1;i<=Q;++i){
		cin>>q[i].l>>q[i].r;
		q[i].id=i;
	}
	sort(q+1,q+1+Q,cmp);
	int l=0,r=0;
	for(int i=1;i<=Q;++i){
		int ql=q[i].l,qr=q[i].r;
//		cout<<ql<<' '<<qr<<'\n';
		while(l<ql){
			del(l);
			l++;
		}
		while(l>ql){
			l--;
			add(l);
		}
		while(r>qr){
			del(r);
			r--;
		}
		while(r<qr){
			r++;
			add(r);
		}
		qans[q[i].id]=ans;
	}
	for(int i=1;i<=Q;++i){
		cout<<qans[i]<<'\n';
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Field {
  Field(int w, int h) : w(w), h(h) {}
  bool invalid(int x, int y){ return x < 0 || w <= x || y < 0 || h <= y; }
  const int w, h;
};

struct Solver {
  Solver(){}
  int solve(){
  }
};

int main()
{
  int n;
  while (n = in()){
    int robo_x, robo_y;
    robo_x = robo_y = 10;
    set<pair<int, int> > gems;
    for (int i = 0; i < n; i++){
      int x, y;
      x = in();
      y = in();
      gems.insert(make_pair(x, y));
    }
    set<pair<int, int> > robos;
    int m = in();
    for (int i = 0; i < m; i++){
      string s;
      int v;
      int d;
      cin >> s >> v;

      if (s == "N") d = 0;
      else if (s == "W") d = 3;
      else if (s == "E") d = 2;
      else d = 1;
      for (int j = 0; j < v; j++){
        robos.insert(make_pair(robo_x, robo_y));
        robo_x += dx[d];
        robo_y += dy[d];
        robos.insert(make_pair(robo_x, robo_y));
      }
    }
    vector<pair<int, int> > mult;
    set_intersection(gems.begin(), gems.end(),
        robos.begin(), robos.end(),
        back_inserter(mult)
        );

    if (mult.size() == gems.size()){
      puts("Yes");
    }
    else puts("No");
  }

  return (0);
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Mod = 998244353;
int fpow(int x, int y) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % Mod;
		x = 1ll * x * x % Mod;
		y >>= 1;
	}
	return res;
}
const int MAX_N = 1e6 + 5;
int N = 1e6, M, fac[MAX_N], ifc[MAX_N];
int C(int n, int m) {
	if (n < m || n < 0 || m < 0) return 0;
	else return 1ll * fac[n] * ifc[m] % Mod * ifc[n - m] % Mod;
}
int main () {
	for (int i = fac[0] = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
	ifc[N] = fpow(fac[N], Mod - 2);
	for (int i = N - 1; ~i; i--) ifc[i] = 1ll * ifc[i + 1] * (i + 1) % Mod;
	cin >> N >> M;
	if (N > M) swap(N, M);
	int ans = 0;
	for (int i = 1; i <= N; i++) ans = (ans + 1ll * C(i << 1, i) * C(N - i + M - i, N - i)) % Mod;
	ans = 1ll * ans * fpow(2 * C(N + M, M) % Mod, Mod - 2) % Mod;
	printf("%d\n", (ans + M) % Mod);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const bool DBG = 1;

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

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

#define sort_by(x, y) sort(all(x), [&](const auto& a, const auto& b) { return y; })

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

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

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

void solve() {
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << fixed << setprecision(15);

    int n,m; cin >> n >> m;
    if(n % 2) {
        F0R(i,m) {
            cout << i+1 << " " << 2*m-i << "\n";
        }
    } else {
        int max_odd_m = m, max_even_m = m;
        if(m % 2 == 0) max_odd_m--;
        else max_even_m--;
        for(int i = 0; 2*i+1 <= m; i++) {
            cout << i+1 << " " << max_odd_m+1-i << "\n";
        }
        for(int i = 0; 2*i+2 <= m; i++) {
            cout << max_odd_m+2+i << " " << max_odd_m+2+max_even_m-i << "\n";
        }
    }

	return 0;
}


#include<bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed<<setprecision(x)
#define pii pair<ll,ll>
#define vint vector<ll>
#define pb push_back
using namespace std;
void Freopen(){
	freopen(title".in","r",stdin);
	freopen(title".out","w",stdout);
}
ll read(){
	ll g=0,f=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();}
	return g*f;
}
const ll N=1e5+5;
ll n,st,x[N],p[N];
ll calc(ll l,ll r,ll t){
	if(st<x[l])return x[r]-st;if(x[r]<st)return st-x[l];
	if(p[l]>=p[r])return p[l]+=p[r],calc(l,r-1,l)+(t==r?x[r]-x[l]:0);
	else return p[r]+=p[l],calc(l+1,r,r)+(t==l?x[r]-x[l]:0);
}
signed main(){
	n=read(),st=read();
	for(ll i=1;i<=n;i++)x[i]=read(),p[i]=read();
	return cout<<calc(1,n,p[1]<p[n]?1:n),signed();
}

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

using namespace std;

using ll = long long;

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

    ll X[N], P[N];
    for (int i = 0; i < N; ++i) {
        cin >> X[i] >> P[i];
    }

    queue<ll> neg, pos;
    // Sより前、後にある座標をそれぞれ両端から入れていく
    for (int i = 0; i < N && X[i] < S; ++i) {
        neg.push(P[i]);
    }
    for (int i = N - 1; i >= 0 && X[i] > S; --i) {
        pos.push(P[i]);
    }

    vector<ll> route;
    // 最適解における探索順を後ろから

    int l = 0, r = N - 1;
    while (true) {
        // もう一方しか残っていない場合
        if (neg.empty()) {
            for (; l <= r; --r) {
                route.push_back(r);
            }
            break;
        }
        if (pos.empty()) {
            for (; l <= r; ++l) {
                route.push_back(l);
            }
            break;
        }

        // 末端の人口を比較、少ない方が選ばれる
        if (neg.front() >= pos.front()) {
            route.push_back(r);
            --r;
            neg.front() += pos.front();
            pos.pop();
        } else {
            route.push_back(l);
            ++l;
            pos.front() += neg.front();
            neg.pop();
        }
    }

    ll ans = 0, now = S;
    while (!route.empty()) {
        ll id = route.back();
        route.pop_back();
        ans += abs(X[id] - now);
        now = X[id];
    }

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


//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
ll pow(ll x, ll ntimes) {
    ll ret = 1;
    while (ntimes > 0) {
        if ((ntimes & 1) == 1) {
            ret = ret * x;
        }
        x = x * x;
        ntimes >>= 1;
    }
    return ret;
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    ll ans = 1;
    vector<bool> prime(n+1,true);
    for(ll i=2;i<=n;++i){
        if(prime[i]==true){
            ll cnt = 0;
            for(ll j=2*i;j<=n;j+=i){
                prime[j]=false;
            }
            ll t = 1;
            while(pow(i,t)<=n){
                ll x = pow(i,t);
                cnt += (n/x);
                t++;
            }
            ans *= (cnt+1);
            ans %= mod;
        }
    }
    cout << ans << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define int long long
#define mod 1000000007
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
#define N 200005
int a[N];
char s[N];
int n;
signed main()
{
#ifdef __LOCAL__
	freopen("in.txt","r",stdin);
#endif
	n=read();
	scanf("%s",s+1);
	for(int i=1;i<=n*2;i++)
	{
		a[i]=(s[i]=='B');
	}
	int cur=0,ans=1;
	for(int i=1;i<=n*2;i++)
	{
		if((cur&1)==a[i])
		{
			ans=ans*cur%mod;
			cur--;
		}
		else cur++;
	}
	if(cur!=0) return cout<<"0\n",0;
	for(int i=1;i<=n;i++)
	{
		ans=ans*i%mod;
	}
	cout<<ans<<endl;
#ifdef __LOCAL__
	printf("Time Used : %d\n",clock());
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
typedef long long ll;
typedef long double ld;
const int MAX = 100000;
int above[MAX + 2];
ll dp[MAX + 1];
const int MAXN = 205;
ll fac[MAXN], invfac[MAXN];
ll inv[MAXN];
const ll MOD = 1e9 + 7;
int binom[MAXN][MAXN];
ll modex(ll a, ll p, ll m = MOD) {
	if (!p) return 1;
	if (p & 1) return a * modex(a, p - 1, m) % m;
	ll v = modex(a, p >> 1, m);
	return v * v % m;
}
int main() {
	fac[0] = 1;
	for (int i = 1; i < MAXN; ++i)
		fac[i] = fac[i - 1] * i % MOD;
	invfac[MAXN - 1] = modex(fac[MAXN - 1], MOD - 2);
	for (int i = MAXN - 2; i >= 0; --i)
		invfac[i] = invfac[i + 1] * (i + 1) % MOD;
	for (int i = 1; i < MAXN; ++i)
		inv[i] = invfac[i] * fac[i - 1] % MOD;
	for (int i = 0; i < MAXN; ++i)
		for (int j = 0; j <= i; ++j)
			if (!j || j == i) binom[i][j] = 1;
			else binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MOD;
	int n, X;
	scanf("%d%d", &n, &X);
	vector<int> s(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &s[i]);
		above[s[i] - 1] = 1;
	}
	sort(s.begin(), s.end());
	for (int i = MAX; i >= 0; --i) above[i] += above[i + 1];
	for (int i = 0; i <= X; ++i) {
		for (int k = 0; k < n; ++k) {
			if (s[k] > i) break;
			int v = i % s[k];
			dp[i] += dp[v] * binom[n - above[i] - 1][n - above[v]] % MOD * fac[above[v] - above[i] - 1] % MOD;
			dp[i] %= MOD;
		}
		if (s[0] > i) dp[i] = i;
	}
	printf("%lld\n", dp[X] * binom[n][above[X]] % MOD * fac[above[X]] % MOD);
}

//g++ -std=c++14 test.cpp -o test.out

 //全部開催すると仮定した後、1つずつ減らして調べていく

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;

#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N,M;
LL A[300][300];

LL ans = INF;

void solve(){
	//i番目の人が何番目のスポーツを選んでいるかをselected_index[i]とおく
	vector<int> selected_index(N,0);
	vector<int> hold(M,1);//M番目のスポーツを開催するのかどうか
	auto max_count = [&](){
		//スポーツiを選んでいる人間の数をcounter[i]とおく
		vector<int> counter(M,0);
		for(int i=0;i<N;i++){
			if(selected_index[i]<M) counter[A[i][selected_index[i]]]++;
		}

		int max_counter = 0;//選んでる人が一番多いスポーツの人数
		int max_sports = 0;//選んでる人が一番多いスポーツの番号
		for(int i=0;i<M;i++){
			if(max_counter<counter[i]){
				max_counter = counter[i];
				max_sports = i;
			}
		}
		return pair<LL,LL>(max_counter,max_sports);
	};
	ans = max_count().first;
	for(int _=0;_<M-1;_++){
		pair<LL,LL> maxs = max_count();
		LL max_counter = maxs.first;
		LL max_sports = maxs.second;

		hold[max_sports] = 0;//選んでる人が一番多いスポーツを中止
		for(int i=0;i<N;i++){
			while(1){
				if(!hold[A[i][selected_index[i]]]) selected_index[i]++;
				else break;
			}
		}

		// cout << endl;
		// for(int i=0;i<N;i++){
		// 	for(int j=0;j<M;j++){
		// 		if(hold[A[i][j]]==1) cout << A[i][j] << " ";
		// 		else cout << "  ";
		// 	}
		// 	cout << endl;
		// }

		maxs = max_count();
		ans = min(ans,maxs.first);
	}
}

int main(){
    cin >> N >> M;
    for(int i=0;i<N;i++){
    	for(int j=0;j<M;j++){
    		cin >> A[i][j];
    		A[i][j]--;
    	}
    }

    solve();

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