#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

using vi = vector<int>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define fi first
#define se second
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

#if 1

template <typename U>
map<U, int> primeFactorization(U n) {

  map<U, int> rs;

  while (!(n & 1)) {
    n >>= 1;
    rs[2]++;
  }

  for (U i = 3; i * i <= n; /*NOP*/) {
    auto q = n / i;
    auto r = n - i * q;
    if (r == 0) {
      rs[i]++;
      n = q;
    }
    else {
      i += 2;
    }
  }

  if (n > 1) {
    rs[n]++;
  }

  return rs;
}

template<typename T>
vector<T> getCompositeNums(map<T, int> primeNums) {

  vector<T> results;

  if (primeNums.empty()) {
    return results;
  }

  // どの素数を何個ずつ掛け合わせるか
  auto cur(primeNums);
  for (auto& c : cur) {
    c.second = 0;
  }

  auto it = cur.begin();
  while (true) {
    if (++it == cur.end()) {
      u64 com = 1;
      for (auto& c : cur) {
        com *= pow<u64>(c.first, c.second);
#if 0
        cout << c.first << "," << c.second << '\n';
#endif
      }
      if (com > 1) {
        results.push_back(com);
#if 0
        cout << com << '\n';
#endif
      }

      bool end = false;
      while (true) {
        if (it == cur.begin()) {
          end = true;
          break;
        }
        it--;
        if (++(it->second) > primeNums[it->first]) {
          it->second = 0;
          continue;
        }
        break;
      }
      if (end) {
        break;
      }
    }
  }

  sort(results.begin(), results.end());
  return results;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;

  auto cn = getCompositeNums(primeFactorization(M));
  cn.insert(cn.begin(), 1);
  //cout << cn;

  M /= *lower_bound(rng(cn), N);
  cout << M;
}
#endif

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

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

int main() {
  int N,M;
  cin>>N>>M;
  int a=M/N;
  for(int i=a;i>=1;i--){
    if(M%i==0){cout<<i<<endl;return 0;}
  }

  return 0;
}


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

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

const ll MAX_N = 6e5 + 25, MAX_M = 1500000, MOD = 1e9 + 7;
ll n, ans;
ll Arr[MAX_N];
ll dp[MAX_N];
ll DP[MAX_N];
ll ind;
vector <ll> vec[MAX_N];
vector <pii> P[MAX_M];
unordered_map <ll, ll> Map;

ll _sum (ll a, ll b) {
	ll c = a + b;
	if (MOD <= c)
		c -= MOD;

	return c;
}

ll _mul (ll a, ll b) {
	ll c = 1LL * a * b % MOD;
	return c;
}

ll _pow (ll a, ll b) {
	if (!b)
		return 1;

	ll res = _pow(a, b >> 1);
	res = _mul(res, res);

	if (b & 1)
		res = _mul(res, a);

	return res;
}

void solve1(ll tmp) {
	ll a = 1, b = 0, x = 0;
	for (ll i = 0; i < n; i++) {
		x ^= Arr[i];

		if (x != 0 && x != tmp)
			continue;

		if (x == 0) {
			dp[i] = b;
			a = _sum(a, dp[i]);
		}
		else {
			dp[i] = a;
			b = _sum(b, dp[i]);
		}
	}

	cout << dp[n - 1] << "\n";
}

ll calc1() {
	ll x = 0, cnt = 0;
	for (ll i = 0; i < n; i++) {
		x ^= Arr[i];
		if (!x)
			cnt++;
	}

	return _pow(2, cnt - 1);
}

ll calc2(ll tmp) {
	ll res = P[tmp][0].second;
	DP[0] = 0;
	dp[0] = P[tmp][0].second;

	for (ll i = 1; i < P[tmp].size(); i++) {
		dp[i] = _mul(_mul(dp[i - 1], P[tmp][i].second), P[tmp][i].first - P[tmp][i - 1].first);
		ll f = _mul(dp[i - 1], _pow(P[tmp][i - 1].second, MOD - 2));
		f = _sum(f, _mul(P[tmp][i].first - P[tmp][i - 1].first, DP[i - 1]));
		dp[i] = _sum(dp[i], _mul(f, P[tmp][i].second));

		DP[i] = _sum(DP[i - 1], dp[i - 1]);
		res = _sum(res, dp[i]);
	}

	return res;
}

void process1() {
	ll x = 0;
	for (ll i = 0; i < n; i++) {
		vec[ind].push_back(Arr[i]);
		x ^= Arr[i];
		if (!x)
			ind++;
	}
}

void process2() {
	for (ll i = 0; i < ind; i++) {
		Map.clear();
		ll x = 0;
		for (ll j = 0; j + 1 < vec[i].size(); j++) {
			x ^= vec[i][j];
			Map[x]++;
		}

		for (auto t: Map)
			P[t.first].push_back({i, t.second});
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (ll i = 0; i < n; i++)
		cin >> Arr[i];

	ll tmp = 0;
	for (ll i = 0; i < n; i++)
		tmp ^= Arr[i];

	if (tmp) {
		solve1(tmp);
		return 0;
	}

	ans = calc1();
	process1();
	process2();

	for (ll i = 0; i < MAX_M; i++)
		if (P[i].size())
			ans = _sum(ans, calc2(i));

	cout << ans << "\n";
	return 0;
}

#include<bits/stdc++.h>

int main(){
    using namespace std;
    constexpr unsigned long MOD = 1000000007;
    unsigned long N;
    cin >> N;
    vector<unsigned long> a(N + 1);
    for(unsigned long i = 1, A; i <= N; ++i){
        cin >> A;
        a[i] = a[i - 1] ^ A;
    }
    auto f = [&MOD](auto& a, auto k){(a.second += (a.first += a.second * k) %= MOD) %= MOD; return a.first;};
    if(a.back()){
        unsigned long k = 0;
        pair<unsigned long, unsigned long> p{1, 0};
        for(const auto i : a)
            if(i == a.back())f(p, k), k = 0;
            else if(!i) ++k;
        cout << p.first << endl;
    }else{
        vector<pair<pair<unsigned long, unsigned long>, unsigned long>> tapi(1048576, {{1, 0}, 0});
        vector<unsigned long> zeros(N + 2);
        unsigned long ans{0}, t{250000002};
        for(unsigned long i = 0; i <= N; ++i){
            zeros[i + 1] = zeros[i] + !a[i];
            if(a[i]){
                ans += f(tapi[a[i]].first, zeros[i + 1] - zeros[tapi[a[i]].second]);
                tapi[a[i]].second = i + 1;
            }else (t *= 2) %= MOD;
        }
        cout << (ans + t) % MOD << endl;
    }
    return 0;
}


#include <iostream> // cout, endl, cin
#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
using namespace std;

int main() {
	int P, Q, R;
	cin >> P >> Q >> R;

	int A = max(P, Q);
	int B = max(A, R);

	cout << P + Q + R - B << endl;

}

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

int main(){
  vi v(3);
  rep(i,3) cin >> v[i];
  sort(v.begin(),v.end());
  cout << v[0]+v[1] << endl;
}


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

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

  if(B == 1){
      cout << "0" << endl;
      return 0;
  }

  ans = ((B-A) + (A-2))/(A-1) + 1;
  cout << ans << endl;
}

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

int main(){
    int a, b;
    cin >> a >> b;
    int sum = 1;
    rep(i,22){
        if(sum >= b){
            cout << i << endl;
            return 0;
        }
        sum--;
        sum += a;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=2020,MOD=998244353;

template <class T>
inline bool read(T &ret)
{
    char c;
    int sgn;
    if (c = getchar(), c == EOF)
    {
        return 0;
    }
    while (c != '-' && (c < '0' || c > '9'))
    {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return 1;
}

template <class T>
inline void write(T x)
{
    if (x > 9)
    {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

int C[MAXN<<1][MAXN<<1],pw[MAXN<<1],n,k;

inline int add(int x,int y)
{
    return (1LL*x+y)%MOD;
}

inline int mul(int x,int y)
{
    return 1LL*x*y%MOD;
}

inline void init(int n)
{
    C[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        C[i][0]=1,C[i][i]=1;
        for(int j=1;j<i;j++)
            C[i][j]=add(C[i-1][j-1],C[i-1][j]);
    }
    pw[0]=1;
    for(int i=1;i<=n;i++) pw[i]=mul(pw[i-1],2);
}

inline int f(int x,int y,int z)
{
    if(y<0) return 0;
    int ret=0;
    for(int q=0;q<=z&&q<=x;++q)
    {
        if (q+y-1<=z+y-1&&q+y-1>=0)
        {
            ret=add(ret,mul(C[x][q],mul(pw[q],C[z+y-1][q+y-1])));
        }
    }
    return ret;
}

int cnt,ans;

int main()
{
    read(k),read(n);
    init(n+k);
    for(int i=2;i<=2*k;i++)
    {
        cnt=0;
        for (int j=1;j<=k;++j)
        {
            if (1<=i-j&&i-j<=k) ++cnt;
        }
        if(i&1)
            ans=f(cnt/2,k-cnt,n);
        else
        {
            ans=f((cnt-1)/2,k-cnt,n);
            ans=add(ans,f((cnt-1)/2,k-cnt,n-1));
        }
        cout<<ans<<endl;
    }
    return 0;
}

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for(int i = 0; i < n; ++i)
using namespace std;
using LLONG = long long;
const LLONG MOD = 998244353;

class ModCalc
{
	LLONG m_mod = MOD;
	vector<LLONG> m_fac;
	vector<LLONG> m_ifac;

public:
	ModCalc(const LLONG mod)
	{
		m_mod = mod;
	}

	// Combinationを計算したいときの前処理：O(max)
	ModCalc(const int max, const LLONG mod)
	{
		m_mod = mod;
		m_fac = vector<LLONG>(max + 1);
		m_ifac = vector<LLONG>(max + 1);
		m_fac[0] = m_ifac[0] = 1;
		for (int i = 0; i < max; ++i)
		{
			m_fac[i + 1] = m_fac[i] * (i + 1) % mod;    // n! (mod M)
			m_ifac[i + 1] = m_ifac[i] * ModPow(i + 1, mod - 2) % mod;   // k!^(M-2) (mod M)
		}
	}

	const LLONG ModAdd(LLONG a, LLONG b) const
	{
		return (a + b) % m_mod;
	}

	const LLONG ModSubtract(LLONG a, LLONG b) const
	{
		LLONG diff = a - b;
		return diff >= 0 ? diff : diff + m_mod;
	}

	const LLONG ModMulti(LLONG a, LLONG b) const
	{
		LLONG prod = a * b % m_mod;
		return prod >= 0 ? prod : prod + m_mod;
	}

	const LLONG ModDiv(LLONG a, LLONG b) const
	{
		LLONG c = m_mod, u = 1, v = 0;
		while (c)
		{
			LLONG t = b / c;
			b -= t * c; swap(b, c);
			u -= t * v; swap(u, v);
		}
		u %= m_mod;
		if (u < 0) u += m_mod;
		return (a % m_mod) * u % m_mod;
	}

	const LLONG ModPow(LLONG x, LLONG n) const
	{
		LLONG ret = 1;
		while (n != 0)
		{
			if (n & 1) ret = ret * x % m_mod;
			x = x * x % m_mod;
			n = n >> 1;
		}
		return ret;
	}

	const LLONG ModComb(LLONG n, LLONG r) const
	{
		if (n == 0 && r == 0) return 1;
		if (n < r || n < 0) return 0;
		LLONG tmp = m_ifac[n - r] * m_ifac[r] % m_mod;
		return tmp * m_fac[n] % m_mod;
	}
};

int main()
{
	int N; cin >> N;
	vector<int> ds(N); REP(i, N) cin >> ds[i];
	map<int, int> d2NumMap;
	REP(i, N)
	{
		++d2NumMap[ds[i]];
	}
	if (ds.front() != 0 || d2NumMap[0] > 1)
	{
		cout << 0 << endl;
	}
	else
	{
		int maxD = d2NumMap.rbegin()->first;
		LLONG ans = 1;
		ModCalc mod(MOD);
		for (int d = 1; d <= maxD; ++d)
		{
			if (d2NumMap.count(d) == 0)
			{
				ans = 0;
				break;
			}

			ans = mod.ModMulti(ans, mod.ModPow(d2NumMap[d - 1], d2NumMap[d]));
		}
		cout << ans << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>
#include <stack>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = (int)(n-1); i >= 0; i--)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(x) (x).begin(),(x).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; }

typedef long long ll;

using namespace std;

template <class Monoid>
class SegmentTree
{
public:
  using Func = function<Monoid(Monoid, Monoid)>;
  const Func F;
  const Monoid M1;
  int sz;
  vector<Monoid> seg;

  SegmentTree(int n,const Func fu, const Monoid &m1): F(fu) , M1(m1) {init(n);}
  void init(int n)
  {
      sz= 1;
      while (sz < n) sz *= 2;
      seg.assign(sz * 2, M1);
  }
  void update(int k,const Monoid &x)
  {
    k+=sz;
    seg[k]=x;
    while(k>>=1)
    {
      seg[k]=F(seg[k*2],seg[k*2+1]);
    }
  }
  Monoid query(int l,int r)
  {
    Monoid L=M1,R=M1;
    l+=sz;r+=sz;
    for(;l<r;l>>=1,r>>=1)
    {
      if(l&1)L=F(L,seg[l++]);
      if(r&1)R=F(seg[--r],R);
    }
    return F(L,R);
  }
  Monoid &operator[](int n){
    return seg[n+sz];
  }
};

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;cin>>n;
  ll m;cin>>m;

  SegmentTree<ll>st(n+1,[](ll a,ll b){return a+b;},0);
  vector<vector<ll>>g(n+1);
  rep(i,n)g[0].push_back(n-i);
  n++;
  rep(i,m){
    ll a,b;cin>>a>>b;
    g[a].push_back(b);
  }
  vector<ll>visited(n),ls(n);
  ll id=0,cn=0;
  ll mi=1e9,tcnt=0;
  function<void(ll,ll&,ll&)>dfs=[&](ll v,ll& idx,ll&cnt){
    ls[v]=idx++;
    st.update(ls[v],1);
    visited[v]=1;
    for(auto nv:g[v]){
      cnt++;
      if(ls[nv]!=0&&st[ls[nv]]){
        //cout<<"test"<<nv<<" "<<v<<" "<<ls[nv]<<" "<<ls[v]<<endl;
        if(chmin(mi,st.query(ls[nv],ls[v]+1))){
          tcnt=cnt;
        }
      }
      if(visited[nv])continue;
      dfs(nv,idx,cnt);
      cnt++;
    }
    st.update(ls[v],0);
  };
  vector<ll>et;
  function<void(ll,ll&)>dfs2=[&](ll v,ll&cnt){
  //  cout<<"cnt"<<cnt<<endl;
    st.update(ls[v],v);
    visited[v]=1;
    et.push_back(v);
    for(auto nv:g[v]){
      cnt++;

      if(ls[nv]!=0&&st[ls[nv]]){
        if(cnt==tcnt){
          cout<<mi<<"\n";
          //cout<<ls[nv]<<" "<<ls[v]<<endl;
          repi(i,ls[nv],ls[v]+1){
            if(st[i]>0)cout<<st[i]<<"\n";
          }
          cout<<flush;
        }
      }
      if(visited[nv])continue;
      dfs2(nv,cnt);
      cnt++;
    //  et.push_back(v);
    }
    st.update(ls[v],0);
  };
  dfs(0,id,cn);
  cn=0;
  visited.assign(n,0);
  dfs2(0,cn);
  cout<<endl;
  if(mi==1e9){
    cout<<-1<<endl;
  }
  return 0;

}

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

int h, w, dx[] = { -1,0,1,0 }, dy[] = { 0,-1,0,1 };
vector<string>m;

int main() {
	cin >> h >> w;
	m.resize(h);
	int sum = 0;
	for (int i = 0; i < h; i++) {
		cin >> m[i];
		for (int j = 0; j < w; j++) {
			if (m[i][j] == '.')sum++;
		}
	}

	vector<vector<int>>d(h, vector<int>(w, 0));
	queue<pair<int, int>>q;
	q.push({ 0,0 });
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second; q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i], ny = y + dy[i];
			if (nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
			if (m[nx][ny] == '#'||d[nx][ny])continue;
			q.push({ nx,ny });
			d[nx][ny] = d[x][y] + 1;
		}
	}
	if (d[h - 1][w - 1]) {
		cout << sum - (d[h - 1][w - 1] + 1) << endl;
	}
	else cout << -1 << endl;

	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int N;
  cin>>N;
  vector<int> A(1010);
  for(int i=1;i<=N;i++){
    int n=i;
    for(int j=2;j<=n/j;j++)
      for(;n%j==0;n/=j)
        A[j]++;
    if(n!=1)
      A[n]++;
  }
  int ans=1;
  for(int i:A)
    ans=ans*(i+1)%1000000007;
  cout<<ans<<endl;
}

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

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

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

char toLowerCase(char c){
  if(isLowerCase(c)){
    return c;
  }else{
    return c+'a'-'A';
  }
}
char toUpperCase(char c){
  if(isLowerCase(c)){
    return c-('a'-'A');
  }else{
    return c;
  }
}
ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
int main() {
  string n;cin>>n;
  if(n.size()==3){
    char c=n[0];
    n[0]=n[2];
    n[2]=c;
  }
  cout<<n<<endl;
  return 0;
}


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

int main(){
  int a,b,c;
  cin >> a >> b >> c;
  int A = min(a,b);
  int B = max(a,b);
  for(int i=A;i<=B;i++){
    if(i == c){
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}

#include <bits/stdc++.h>

typedef long long ll;

#define rep(i, n) for (int i = 0; i < (ll)n; ++i)
#define INF 10e7
#define MOD 1000000000 + 7
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
using namespace std;

void solve(void)
{
    int a, b, c;
    cin >> a >> b >> c;
    if ((a <= c && c <= b) || (b < c && c <= a))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

int main(void)
{
    solve();
}


#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<vector<ll> > Graph;
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 INF = 1e18;

//エラトステネスの篩
std::vector<bool> IsPrime;
void sieve(size_t max){
    if(max+1 > IsPrime.size()){     // resizeで要素数が減らないように
        IsPrime.resize(max+1,true); // IsPrimeに必要な要素数を確保
    }
    IsPrime[0] = false; // 0は素数ではない
    IsPrime[1] = false; // 1は素数ではない

    for(size_t i=2; i*i<=max; ++i) // 0からsqrt(max)まで調べる
        if(IsPrime[i]) // iが素数ならば
            for(size_t j=2; i*j<=max; ++j) // (max以下の)iの倍数は
                IsPrime[i*j] = false;      // 素数ではない
}

int main(){
    int n;
    cin >> n;
    vector<int>ans;
    sieve(56000);
    int now = 11;
    while(ans.size()!=n){
        if(IsPrime[now]){
            ans.push_back(now);
        }
        now += 10;
    }
    rep(i,n){
        cout << ans[i] << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a)  (a).begin(),(a).end()
const ll INF = 1e18+18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
static const double pi = 3.141592653589793;

int main(){
    ll N;
    cin>>N;
    if(N==0){
        cout<<0<<endl;
        return 0;
    }
    if(N==1){
        cout<<1<<endl;
        return 0;
    }
    if(N>0){
        ll Num=0;
        ll Key=1;
      	ll Keta=-2;
        while(Num<N){
            Num+=Key;
            Key*=4;
          	Keta+=2;
        }
        ll Dif=Num-N;
        ll J=1;
        cout<<1;
        vector<ll> W(Keta, 0);
      	ll i=0;
        while(Dif>0){
            W[i]=Dif%2;
          	i++;
            Dif/=2;
        }

        rep(i, Keta){
            if(J+1){
				cout<<W[Keta-1-i];
            }else{
              	cout<<(1-W[Keta-1-i]);
            }

          	J*=-1;
        }
    }
  	if(N==-1){
      	cout<<11<<endl;
      return 0;
    }
  	if(N<0){
      	ll Num=0;
        ll Key=2;
      	ll Keta=-1;
        while(Num<N*-1){
            Num+=Key;
          	Keta+=2;
            Key*=4;
        }
        ll Dif=Num-N*-1;
      	cout<<1;
        vector<ll> W(Keta, 0);
      	ll i=0;
        while(Dif>0){
            W[i]=Dif%2;
          	i++;
            Dif/=2;
        }
      	ll J=1;
        rep(i, Keta){
            if(J+1){
				cout<<W[Keta-1-i];
            }else{
              	cout<<(1-W[Keta-1-i]);
            }

          	J*=-1;
        }
    }
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e18
#define mod 1000000007

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

  ll N;
  cin>>N;
  vector<ll> X(N),Y(N),K(N);
  rep(i,N){
    char c;
    cin>>X[i]>>Y[i]>>c;
    if(c=='R')K[i]=0;
    if(c=='U')K[i]=1;
    if(c=='L')K[i]=2;
    if(c=='D')K[i]=3;
  }

  ll res=INF;

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(Y[i]-X[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][Y[i]-X[i]].push_back(X[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[1].size()){
        ll x=nids[1][i];
        auto it=lower_bound(all(nids[2]),x);
        if(it==nids[2].end())continue;
        minch(res,(*it-x)*10);
      }
      rep(i,nids[0].size()){
        ll x=nids[0][i];
        auto it=lower_bound(all(nids[3]),x);
        if(it==nids[3].end())continue;
        minch(res,(*it-x)*10);
      }
    }
  }

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(Y[i]+X[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][Y[i]+X[i]].push_back(X[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[0].size()){
        ll x=nids[0][i];
        auto it=lower_bound(all(nids[1]),x);
        if(it==nids[1].end())continue;
        minch(res,(*it-x)*10);
      }
      rep(i,nids[3].size()){
        ll x=nids[3][i];
        auto it=lower_bound(all(nids[2]),x);
        if(it==nids[2].end())continue;
        minch(res,(*it-x)*10);
      }
    }
  }

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(X[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][X[i]].push_back(Y[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[1].size()){
        ll x=nids[1][i];
        auto it=lower_bound(all(nids[3]),x);
        if(it==nids[3].end())continue;
        minch(res,(*it-x)*5);
      }
    }
  }

  {
    vector<ll> keys;
    rep(i,N){
      keys.push_back(Y[i]);
    }
    sort(all(keys)); uni(keys);

    map<ll,vector<ll>> xs[4];
    rep(i,N){
      xs[K[i]][Y[i]].push_back(X[i]);
    }

    for(ll k : keys){
      vector<ll> nids[4];
      rep(dir,4){
        vector<ll> tmp=xs[dir][k];
        sort(all(tmp));
        nids[dir]=tmp;
      }

      rep(i,nids[0].size()){
        ll x=nids[0][i];
        auto it=lower_bound(all(nids[2]),x);
        if(it==nids[2].end())continue;
        minch(res,(*it-x)*5);
      }
    }
  }

  if(res==INF)cout<<"SAFE"<<endl;
  else cout<<res<<endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int MAX=100000+5;

int n;
pair<int,int> coord[MAX];
vector<int> par(MAX,0);
vector<pair<int,pair<int,int> > > edges;
int root(int x)
{
	while(par[x]!=x)
	{
		par[x]=par[par[x]];
		x=par[x];
	}
	return x;
}

void union1(int x,int y)
{
	int u=root(x);
	int v=root(y);
	par[u]=v;
}
int main()
{
	cin>>n;
	vector<pair<int,int> > xc;
	vector<pair<int,int> > yc;

	for(int i=1;i<=n;i++)
	{
		int x,y;
		cin>>x>>y;
		coord[i]={x,y};
		xc.push_back({x,i});
		yc.push_back({y,i});
	}
	for(int i=1;i<=n;i++)
	{
		par[i]=i;
	}
	sort(xc.begin(),xc.end());
	sort(yc.begin(),yc.end());
	for(int i=0;i<n-1;i++)
	{
		edges.push_back({abs(xc[i].first-xc[i+1].first),{xc[i].second,xc[i+1].second}});
	}
	for(int i=0;i<n-1;i++)
	{
		edges.push_back({abs(yc[i].first-yc[i+1].first),{yc[i].second,yc[i+1].second}});
	}
	long long int sum=0;
	//kruskal
	for(int i=1;i<=n;i++)
		par[i]=i;
	sort(edges.begin(),edges.end());
	for(int i=0;i<edges.size();i++)
	{
		int w=edges[i].first;
		int u=edges[i].second.first;
		int v=edges[i].second.second;
		if(root(u)!=root(v))
		{
			union1(u,v);
			sum+=w;
		}
	}
	cout<<sum<<endl;
}


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

int main()
{
    int h,w,n;
    cin >> h >> w >>n;
    int a=max(h,w);
 cout<<(a + n - 1) / a<<endl;
return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <cstdio>
#include <cmath>
#include <iterator>
#include <map>
//#include <list>
#include <iomanip>

using namespace std;

#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i,a,n) for(Int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(Int (i)=(a); (i)<=(n); (i)++)
const int MAX_N = 1000000;
const Int MAX_N_Int = 1000000000000;

template <typename T>
void printV(const std::vector<T>& v, const char * const separator = " ")
{
    if(!v.empty())
    {
        std::copy(v.begin(),
                  --v.end(),
                  std::ostream_iterator<T>(std::cout, separator));
        std::cout << v.back() << "\n";
    }
}

bool isPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}
Int gcd(Int a, Int b) {
    return b != 0 ? gcd(b, a % b) : a;
}

Int lcm(Int a, Int b) {
    return a / gcd(a, b) * b;
    //a*bは64bit integer overflow
}

int Max(int a, int b, int c) {
    int temp = max(a, b);
    return max(temp, c);
}

int Min(int a, int b, int c) {
    int temp = min(a, b);
    return min(temp, c);
}

bool integer(double num) {
    return floor(num) == num;
}

Int fact(int num) {
    if (num == 0)
        return 1;
    else
        return num * fact(num - 1);
}

Int yakusu(int n) {
    int cnt = 0;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            cnt++;
            if (i * i != n) cnt++;
        }
    }
    return cnt;
}

Int fact_mod(Int n, Int mod) {
    Int f = 1; repq(i,2,n) f = f * (i % MOD) % MOD;
    return f;
}

// 繰り返し二乗法 (modの世界での累乗)
Int mod_pow(Int x, Int n, Int mod) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

// 組み合わせ nCr を求める
Int combination_mod(Int n, Int r, Int mod) {
    if(r > n-r) r = n-r;
    if(r == 0) return 1;
    Int a = 1;
    rep(i, 0, r) a = a * ((n-i) % mod) % mod;
    Int b = mod_pow(fact_mod(r, mod), mod-2, mod);
    return (a % mod) * (b % mod) % mod;
}

Int cntZero(string s){
    int cnt=0;
    reverse(s.begin(),s.end());
    for (int i = 0; i < s.length(); ++i) {
        if(s[i]=='0')cnt++;
        else break;
    }
    return cnt;
}

int main() {
    //COMinit();
    IOS;
    prec;
    Int a,b,c,d,n,m,k,x,y=0,ans=0,ans1=0;
    string str,s="",t,u;
    Int h,w;
    cin>>h>>w>>n;
    Int cnt=0;
    rep(i,0,min(h,w)){
        ans+=max(h,w);
        cnt++;
        if(ans>=n){
            cout<<cnt<<endl;
            return 0;
        }
    }
    rep(i,0,max(h,w)){
        ans+=min(h,w);
        cnt++;
        if(ans>=n){
            cout<<cnt<<endl;
            return 0;
        }
    }

    cout<<endl;
    return 0;
}


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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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 INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

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

  ll n,k; cin>>n>>k;
  ll ans=n-k+1;
  if(ans<0) ans=0;
  cout<<ans<<endl;

}

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

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

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

    int n, k; cin >> n >> k;
    cout << n - k + 1 << endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if (ch == '-') w = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int N(2e5 + 10), Mod(1e9 + 7);
int fac[N], inv[N], f[N], n, m, A, B;
int C(int n, int m) { return 1ll * fac[n + m] * inv[m] % Mod * inv[n] % Mod; }
int fastpow(int x, int y)
{
	int ans = 1;
	for (; y; y >>= 1, x = 1ll * x * x % Mod)
		if (y & 1) ans = 1ll * ans * x % Mod;
	return ans;
}

int main()
{
	n = read() - 1, m = read() - 1, A = read() - 1, B = read() - 1, fac[0] = inv[0] = 1;
	if (A == n || B == m) return puts("0"), 0;
	for (int i = 1; i <= n + m; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
	inv[n + m] = fastpow(fac[n + m], Mod - 2);
	for (int i = n + m - 1; i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % Mod;
	int ans = C(n, m);
	for (int i = 0; i <= A; i++)
		ans = (ans - 1ll * C(i, m - B - 1) * C(n - i, B) % Mod + Mod) % Mod;
	printf("%d\n", ans);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define int long long
int pref[250005], suff[250005], ans, prefix[250005], h , w, a, b;

const int mod = 1e9 + 7;

int BinPow(int x,int n){
    if(n == 0)
        return 1;
    if(n == 1)
        return x;
    else{
        int sum = BinPow(x, n / 2) % mod;
        sum*=sum; sum%=mod;
        if(n % 2 != 0)
            sum *= x, sum%=mod;
        return sum;
    }
}

int C(int x,int n){
    if(x < 0 || x > n || n < 0) return 0;
    int sum = pref[n] * suff[x]; sum %= mod;
    sum*=suff[n - x]; sum %= mod;
    return sum;
}

signed main(){
    cin.tie(0), ios::sync_with_stdio(0);
    cin >> h >> w >> a >> b;
    pref[0] = 1;
    for(int i = 1; i <= 250000; i++){
        pref[i] = pref[i - 1]*i;
        pref[i] %= mod;
    }
    suff[0] = 1;
    for(int i = 1; i <= 250000; i++)
        suff[i] = BinPow(pref[i], mod - 2);
    for(int i = 1; i <= 100000; i++){
        if(i >= h - a + 1){
            prefix[i] += C(b - 1, i + b - 2);
            pref[i] %= mod;
            prefix[i] += prefix[i - 1];
        }
    }
    ans = C(h - 1,h + w - 2);
    for(int i = 1; i <= h; i++){
        if(i >= h - a + 1){
            int dist = w - b;
            int dist1 = h - i + 1;
            ans -= C(b - 1, i + b - 2)*C(dist1 - 1,dist + dist1 -2 );
            ans %= mod;
            if(ans < 0) ans += mod;
        }
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define pb push_back
#define mk make_pair
#define db double long
const int siz = 2 * 1e5 + 7;
int parent1[siz];
int nodesize1[siz];
int parent2[siz];
int nodesize2[siz];
int get2(int x) {
	if (parent2[x] == x)return x;
	return parent2[x] = get2(parent2[x]);
}
void dsu2(int u, int v) {
	u = get2(u);
	v = get2(v);
	if (v != u) {
		if (rand() & 1)swap(u, v);
		parent2[v] = u;
		nodesize2[u] += nodesize2[v];
		nodesize2[v] = 0;
	}
}
int get1(int x) {
	if (parent1[x] == x)return x;
	return parent1[x] = get1(parent1[x]);
}
void dsu1(int u, int v) {
	u = get1(u);
	v = get1(v);
	if (v != u) {
		if (rand() & 1)swap(u, v);
		parent1[v] = u;
		nodesize1[u] += nodesize1[v];
		nodesize1[v] = 0;
	}
}
void clear() {
	for (int i = 0; i < siz; i++) {
		parent1[i] = i;
		nodesize1[i] = 1;
		parent2[i] = i;
		nodesize2[i] = 1;
	}
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, k, l;
	cin >> n >> k >> l;
	clear();
	for (int i = 0; i < k; i++) {
		int a, b;
		cin >> a >> b;
		if (get1(a) != get1(b)) {
			dsu1(a, b);
		}
	}
	for (int i = 0; i < l; i++) {
		int a, b;
		cin >> a >> b;
		if (get2(a) != get2(b)) {
			dsu2(a, b);
		}
	}
	map<pi, int>m;
	for (int i = 1; i <= n; i++) {
		m[mk(get1(i), get2(i))]++;
	}
	for (int i = 1; i <= n; i++) {
		cout << m[mk(get1(i), get2(i))] << " ";
	}
	cout << endl;
}

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int K, T;
  cin>>K>>T;
  int tmp;
  priority_queue<pair<int, int> > que;
  for(int i=0;i<T;++i) {
    cin>>tmp;
    que.push(mp(tmp, i));
  }
  pair<int, int> tmp1, tmp2;
  int bef = -1;
  int ans = 0;
  for(int i=0;i<K;++i) {
    if(que.size()==1) {
      tmp1 = que.top();que.pop();
      if(bef==tmp1.se) ans++;
      tmp1.fi--;
      bef = tmp1.se;
      if(tmp1.fi>0) que.push(tmp1);
    }
    else {
      tmp1 = que.top();que.pop();
      tmp2 = que.top();que.pop();
      if(bef==tmp1.se) {
        tmp2.fi--;
        bef = tmp2.se;
      }
      else {
        tmp1.fi--;
        bef = tmp1.se;
      }
      if(tmp1.fi>0) que.push(tmp1);
      if(tmp2.fi>0) que.push(tmp2);
    }
  }
  cout<<ans<<endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

long long cnt;

void print(vector<int> &data){
	for (int i = 0; i < data.size(); ++i){
		cout << data[i] << endl;
	}
}

void insertSort(vector<int> &data, int d){
	int n = data.size();
	for (int i = d; i < n; ++i) {
		int v = data[i];
		int j = i - d;
		while (j >= 0 && data[j] > v) {
			data[j + d] = data[j];
			j -= d;
			cnt++;
		}
		data[j + d] = v;
	}
}

void insertSort2(vector<int> &data, int d){
	int n = data.size();
	for (int i = d; i < 2 * d; ++i) {
		int v = data[i];
		int j = i - d;
		while (data[j] > v) {
			data[j + d] = data[j];
			j -= d;
			cnt++;
		}
		data[j + d] = v;
	}
}

void shellSort(vector<int> &data){
	vector<int> dist;
	for (int i = 1; i <= data.size(); i = 3 * i + 1)
		dist.push_back(i);

	int n = dist.size();
	cout << n << endl;

	for (int i = n - 1; i >= 0; --i)
	{
		insertSort(data, dist[i]);
		cout << dist[i];
		if (i != 0) cout << " ";
		else cout << endl;
	}
	cout << cnt << endl;
	print(data);
}

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

	shellSort(data);

	return 0;
}


//こんなプログラムを組んではいけない(戒め
//マージの仕様が複雑だし、空白マスを引く処理忘れやすいし、ひどいコードだ。
#include<iostream>
#include<queue>
using namespace std;

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

char data[h][w+2];

//データ構造をマージする一般的なテクなし。
class UnionFind {
public:
	int oya[2521], size;
	void ini(int size) { this->size = size; for( int i = 0;i < size; i++ ) oya[i] = i; }
	int root(int x) { if( oya[x] == x ) return x; return (oya[x] = root(oya[x]) ); }
	void marge(int x, int y) { x = root(x); y = root(y); if ( x != y ) { size--; oya[x] = y; } }
}uf;

void bfs(int sy, int sx) {
	queue<int> que;
	que.push(sy*w + sx);

	while( !que.empty() ) {
		int now = que.front();
		que.pop();

		if ( data[now/w][now%w] == '0' )
			continue;
		if ( uf.root(now) != now ) 	//すでに調べている(マージされている)ならcontinue
			continue;

		uf.marge(now, sy*w + sx);	//nowをsy*w + sxにくっつける(逆にしたら無限ループするかも)

		for(int i = 0; i < 4; i++ ) {
			int ny = now/w + dy[i];
			int nx = now%w + dx[i];
			if ( 0 <= ny && ny < h && 0 <= nx && nx < w )
				que.push(ny * w + nx);
		}
	}
}

int main() {
	int i, j;
	while( cin >> data[0] ) {
		for( i = 1; i < h; i++ )
			cin >> data[i];

		uf.ini(h*w);

		int minus = 0;
		for( i = 0; i < h; i++ ) {
			for( j = 0; j < w; j++ ) {
				minus += (data[i][j] == '0');
				bfs(i, j);
			}
		}
		cout << uf.size - minus << endl;
	}
	return 0;
}


#include <iostream>

using namespace std;

int main(){

	int x;
	int ans =0;

	cin >> x;
	ans = x * x * x;

	cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define loop(k) rep(author_is_masa,k)
#define chmin(a,b) if((a)>(b)) (a)=(b);
#define chmax(a,b) if((a)<(b)) (a)=(b);
#define vi vector<int>
#define pii pair<int,int>
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define pb push_back
#define pf push_front
int gcd(int a,int b){/*a>=0,b>=0,￢(a=b=0)*/
  while(min(a,b)>0){if(a<b)swap(a,b);a%=b;}return max(a,b);
}
int dx[]={1,0,-1,0,1,-1,-1,1};
int dy[]={0,1,0,-1,1,1,-1,-1};
const int MOD = 1e9+7;
const long long INF = 1e18+10;

/*--------------------------------------------------------------------*/

signed main(){
	int a,b;cin>>a>>b;
	cout<<a*b<<endl;

	return 0;
}


#include <bits/stdc++.h>

#define PB emplace_back

using namespace std;

const int N = 17;

long long int a[N][N], dp[1<<N];

int main () {
    ios::sync_with_stdio(0);
    int n;
    cin >> n;
    for(int i = 0 ; i < n ; i++ ) {
        for(int j = 0 ; j < n ; j++ ) {
            cin >> a[i][j];
        }
    }
    long long int ans = -1e18;
    for(int i = 1 ; i < (1 << n ) ; i++ ) {
        long long int mas = 0;
        vector<int>v;
        for(int j = 0 ; j < n ; j++ ) {
            if((1<<j)&i) v.PB(j);
        }
        for(int u: v) {
            for(int uu: v) {
                if(u <= uu)continue;
                mas += a[u][uu];
            }
        }
        for(int j = i ; j > 0 ; j = (j - 1) & i) {
            mas = max(mas, dp[j] + dp[i^j]);
        }
        if(ans < mas)ans = mas;
        dp[i] = mas;
    }
    cout << ans;
}

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

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

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

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

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

ll N;

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

	int tlc = 0, trc = 0;

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

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

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

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

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

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


#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 100000000;

int cost[101][101];
int d[101];
bool used[101];
int n,k;

int dijkstra(int s, int g) {
  fill(d,d+n,INF);
  fill(used,used+n,false);
  d[s] = 0;

  while(true) {
    int v = -1;

    for (int u = 0; u < n; u++ ) {
      if (!used[u] && ( v == -1 || d[u] < d[v])) v = u;
    }

    if (v == -1) break;
    used[v] = true;

    for (int u = 0; u < n; u++ ) {
      d[u] = min(d[u],d[v] + cost[v][u]);
    }
  }
  if ( d[g] == INF ) return INF;
  else return d[g];

}

int main() {
  while (cin >> n >> k, n||k) {
    for ( int i = 0; i < n; i++ ) {
      for ( int j = 0; j < n; j++ ) {
	cost[i][j] = INF;
      }
    }
    for ( int i = 0; i < k; i++ ) {
      int order; cin >> order;
      if ( order == 0 ) {
	int a,b; cin >> a >> b;
	a--; b--;
	int ans = dijkstra(a,b);
	if ( ans == INF ) cout << -1 << endl;
	else cout << ans << endl;
      } else if ( order == 1 ) {
	int c,d,e; cin >> c >> d >> e;
	c--; d--;
	cost[c][d] = min(e,cost[c][d]);
	cost[d][c] = min(e,cost[d][c]);
      }

    }
  }
  return 0;
}

#include<iostream>
using namespace std;

class meb{
public:
	int num;
	unsigned long long tanka;
	unsigned long long count;
	unsigned long long sum;
};

int main(){
	int n;
	while(1){
		int cc=0;
		cin>>n;
		meb st[n];
		if(n==0)break;
		for(int i=0;i<n;i++){
			cin>>st[i].num>>st[i].tanka>>st[i].count;
		}
		for(int k=0;k<n;k++){
			st[k].sum=st[k].tanka*st[k].count;
		}
		for(int j=0;j<n;j++){
			for(int l=j+1;l<n;l++){
				if(st[j].num==st[l].num){
					(st[j].sum)=(st[j].sum)+(st[l].sum);
					st[l].sum=0;
				}
			}
		}
		for(int a=0;a<n;a++){
			if(st[a].sum>=1000000)cout<<st[a].num<<endl;
			else cc++;
		}
		if(cc==n)cout<<"NA"<<endl;
	}
	return 0;
}


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

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(lol i=0;i<lol(n);i++)
#define REPD(i,n) for(lol i=n-1;i>=0;i--)
#define FOR(i,a,b) for(lol i=a;i<=lol(b);i++)
#define FORD(i,a,b) for(lol i=a;i>=lol(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい
#define SIZE(x) lol(x.size()) //sizeをsize_tからllに直しておく
//定数
#define PI 3.1415926535897932385 //pi
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//最大値最小値
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main()
{
    lol result{0};
    string n; cin >> n;
    //lol k; cin >> k;
    /*
    vector<lol> a(n);
    REP(i, n)
    {
        cin >> a[i];
    }
    */
    REP(i, 3)
    {
        if(n[i] == '7')
            result = 1;
    }

    if(result)
        cout << "Yes" << endl;
    else
    {
        cout << "No" << endl;
    }

    return 0;
}

#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <queue>
#include <queue>
#include <stack>
#include <sstream>
#include <set>

typedef  long long ll;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = { 0, 1, 0,-1};
const int INF = 1e9;
#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(),a.end())
#define REVERSE(a) reverse(a.begin(),a.end())
int guki (int a){
    if(a%2==0) return 0;
    else return 1;
}
int gcd(int a, int b){
    if(a%b==0){
        return b;
    }else{
        return (gcd(b,a%b));
    }
}
int lcm(int a, int b){
    int x = gcd(a,b);
    return (a*b/x);
}

using namespace std;
// now

struct UnionFind {
    vector<int> d;
    UnionFind(int n=0) : d(n,-1){}
    int Find (int x){//根の番号を探す
        if(d[x] < 0) return x;
        return d[x] = Find(d[x]);
    }
    bool unite (int x, int y){
        x = Find(x); y = Find(y);
        if(x == y ) return false;
        if(d[x] < d[y]){
            swap(x,y);
        }
        d[x] += d[y];
        d[y] = x;
        return true;
    }

    bool same(int x, int y){
        return Find(x) == Find(y);
    }
    int size(int x) {
        return (-d[Find(x)]);
    }
};

ll nCr (ll n, ll r){
    ll ans = 1;
    for(ll i = n ; i > n-r ; i --){
        ans *= i;
    }
    for(ll i = 1 ; i<= r; i ++){
        ans /= i;
    }
    return ans ;
}

int nPr (int n,int r){
    int ans = 1;
    for(int i = n ; i > n-r ; i --){
        ans *= i;
    }
    return ans ;
}
 ll modd = 1e9+ 7;
ll wa (ll n,ll mi, ll ma){
    ll ans  = n * (mi+ma) /2;
    return ans;
}

int main(){

    ll n;
    cin >> n;
    string s = "";

    while(n){
        n--;
        s += ((n%26) + 'a');
        n /= 26;
    }

    reverse(s.begin(), s.end());
    cout << s <<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<double,string> P;
int main(){
  int n;
  while(cin>>n,n){
    P pr[55];
    for(int i=0;i<n;i++){
      string l;
      cin>>l;
      int p,a,b,c,d,e,f,s,m;
      cin>>p>>a>>b>>c>>d>>e>>f>>s>>m;
      double sumi=f*s*m-p;
      double sumt=a+b+c+(d+e)*m;
      pr[i]=P(-(sumi/sumt),l);
    }
    sort(pr,pr+n);
    for(int i=0;i<n;i++)cout<<pr[i].second<<endl;
    cout<<"#"<<endl;
  }
  return 0;
}

#include <stdio.h>
#include <string.h>

int main(void){
	int p, sum, d, e, f, s, m, n, i, j, x, max_i;
	double tmp1, eff[50];
	char str[50][21], C, tmp2[21];
	while(1){
		scanf("%d%*c",&n);
		if(n == 0)
			break;
		for(i = 0;i < n;i++){
			j = 0;
			while((C = getchar()) != ' ')
				str[i][j++] = C;
			str[i][j] = '\0';
			scanf("%d",&p);
			sum = 0;
			for(j = 0;j < 3;j++){
				scanf("%d",&x);
				sum += x;
			}
			scanf("%d%d%d%d%d%*c",&d,&e,&f,&s,&m);
			for(j = 0;j < m;j++)
				sum += d + e;
			eff[i] = (double)(m * f * s - p) / sum; // 期作 * 1つの種からできる実の数 * 実の価格 - 種の値段
		}
		for(i = 0;i < n - 1;i++){
			max_i = i;
			for(j = i + 1;j < n;j++){
				if(eff[max_i] < eff[j])
					max_i = j;
				else if(eff[max_i] == eff[j] && strcmp(str[max_i],str[j]) > 0)
					max_i = j;
			}
			if(i != max_i){
				tmp1 = eff[i];
				strcpy(tmp2,str[i]);
				eff[i] = eff[max_i];
				strcpy(str[i],str[max_i]);
				eff[max_i] = tmp1;
				strcpy(str[max_i],tmp2);
			}
		}
		for(i = 0;i < n;i++)
			printf("%s\n",str[i]);
		printf("#\n");
	}

	return 0;
}


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

int main(){
  int n;
  cin>>n;
  cout<<n/3<<endl;
}

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

int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    string s;
    if(n==0)
        s="0";
    while(n!=0){
        int cur=n%(-2);
        n/=(-2);
        if(cur<0){
            cur=abs(cur);
            n++;
        }
        s+=cur+48;
    }
    reverse(all(s));
    cout<<s<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main (void)
{
  int a,b;
  cin>>a>>b;
  int start=1;
  b=b-1;
  int ans;
  ans=b/(a-1);
    if(b%(a-1)!=0)ans++;
  cout<<ans<<endl;
}

#include <stdio.h>
typedef long long ll;

int main(void) {
  ll h, w, n, m;
  scanf("%lld%lld%lld", &h, &w, &n);
  m = h > w ? h : w;
  printf("%lld", (n + m - 1) / m);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
const long long INF = 1<<29;

const int MOD = 1000000007;
long long ruizyou(long long m,long long n){
  if(m == 0)return 0;
  if(m == 1)return 1;
  long long ans = 1;
  long long tmp = m;
  for(int i=0;i<=30;i++){
    if(n & (1<<i)){
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x){
  if(x == 0)return 1;
  return x * kaizyou(x-1) % MOD;
}
long long comb(long long x,long long y){
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x-y) * kaizyou(y) % MOD;
  return bunsi * ruizyou((int)bunbo,MOD-2) % MOD;
}
struct unionfind{
  vector<int> par;
  vector<int> hukasa;
  unionfind(int n){
    par = vector<int>(n);
    hukasa = vector<int>(n,0);
    for(int i=0;i<n;i++){
      par.at(i) = i;
    }
  }
  int root(int x){
    if(par.at(x) == x)return x;
    return root(par.at(x));
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry)return;
    if(hukasa.at(rx) >= hukasa.at(ry)){
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1,hukasa.at(rx));
    }
    else{
      par.at(rx) = ry;
    }
  }
  bool same(int x,int y){
    return root(x) == root(y);
  }
};
int ctoi(char a){
  return a - '0';
}
long long gcd(long long a,long long b){
  long long c = max(a,b);
  long long d = min(a,b);
  if(d % c == 0)return c;
  return gcd(c,d%c);
}
long long lcm(long long a,long long b){
  return a * b / gcd(a,b);
}
int main(){
  int a;
  cin >> a;
  cout << a + a*a + a*a*a << endl;
}

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

int n, a, b;
int main() {
	cin >> n;
	if(n==1) cout << "Hello World";
	else {
		cin >> a >> b;
		cout << a+b;
	}
	return 0;
}


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

const int N_MAX = 100;
const int INF = 1000000;

struct edge{
    int to, cap, rev;
    edge(int t, int c, int r): to(t), cap(c), rev(r){}
};

vector<edge> G[N_MAX * 2];
bool used[N_MAX * 2];

void add_edge(int from, int to, int cap){
    G[from].emplace_back(to, cap, G[to].size());
    G[to].emplace_back(from, 0, G[from].size()-1);
}

int dfs(int v, int t, int f){
    if (v == t) return f;
    used[v] = true;
    for(int i = 0; i < G[v].size(); i++){
        edge &e = G[v][i];
        if (!used[e.to] && e.cap > 0){
            int d = dfs(e.to, t, min(f, e.cap));
            if (d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t){
    int flow = 0;
    for(;;){
        memset(used, 0, sizeof(used));
        int f = dfs(s, t, INF);
        if (f == 0) return flow;
        flow += f;
    }
}

int main(){
    int n;
    cin >> n;
    vector<pair<int, int>> RED;
    vector<pair<int, int>> BLUE;
    for(int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;
        RED.emplace_back(a, b);
    }
    for(int i = 0; i < n; i++){
        int c, d;
        cin >> c >> d;
        BLUE.emplace_back(c, d);
    }
    for(int i = 1; i <= n; i++){
        int a = RED[i-1].first, b = RED[i-1].second;
        for(int j = 1; j <= n; j++){
            int c = BLUE[j-1].first, d = BLUE[j-1].second;
            if (a < c && b < d){
                add_edge(i, n+j, 1);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        add_edge(0, i, 1);
        add_edge(n+i, 2 * n + 1, 1);
    }
    cout << max_flow(0, 2 * n + 1) << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define fi first
#define se second
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

int main(){
    ll N;
    cin >> N;
    vector<ll> a(N), b(N), c(N), d(N);
    vector<Pll> R(N), B(N);
    rep(i,N){
        cin >> a[i] >> b[i];
        //a[i];
        //b[i];
        R[i] = Pll(a[i], b[i]);
    }
    vector<vector<ll>> note(2*N+100,vector<ll>(2*N+100));
    rep(i,N){
        cin >> c[i] >> d[i];
        //c[i]++;
        //d[i]++;
        B[i] = Pll(c[i], d[i]);
        /*rep(j,c[i]){
            rep(k,d[i]){
                note[j][k]++;
            }
        }*/
    }
    sort(R.begin(), R.end());
    sort(B.begin(), B.end());
    vector<ll> memo(N*2+100);
    ll ans = 0;
    rep(i,N){
        ll cc = B[i].fi;
        ll dd = B[i].se;
        ll id = -1;
        ll score = -1e18;
        rep(j,N){
            if(memo[j]) continue;
            ll aa = R[j].fi;
            ll bb = R[j].se;
            if(aa < cc && bb < dd){
                if(bb >= score){
                //if(score > note[aa][bb]){
                //    score = note[aa][bb];
                    id = j;
                    score = bb;
                }
                //}
            }
        }
        if(id != -1){
            memo[id] = 1;
            ans++;
        }
        /*
        rep(j,cc){
            rep(k,dd){
                note[j][k]--;
            }
        }*/
    }
    cout << ans << endl;
}


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

//ライブラリ始まり

//定数
//円周率
const double pi = 3.1415926535897932384;
//天井
const int INF = 1000000000; // = 10^9
const ll LINF = 1000000000000000; // = 10^15
//ABC文字列
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZABC";
const string abc = "abcdefghijklmnopqrstuvwxyzabc";
//よくあるmodくん
const ll MOD = 1000000007; // = 10^9 + 7

//データ構造
//Union_Find木
struct UnionFind {
  vector<int> UF; // UF.at(i) : iの親の番号
  vector<int> SIZE; // SIZE.at(root(i)) : iと連結されてる要素の数
  UnionFind(int N) : UF(N), SIZE(N, 1) { // 最初は全てが根であるとして初期化
    for(int i = 0; i < N; i++) {
      UF.at(i) = i;
    }
  }
  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (UF.at(x) == x) {
      return x;
    }
    return UF.at(x) = root(UF.at(x));
  }
  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x); // xの根をrx
    int ry = root(y); // yの根をry
    if (rx == ry) {
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    }
    // xとyの根が同じでない(=同じ木にない)時：小さい方の根を大きい方の根につける
    if (SIZE.at(rx) < SIZE.at(ry)) {
      UF.at(rx) = ry;
      SIZE.at(ry) += SIZE.at(rx);
      SIZE.at(rx) = 0;
    }
    else {
      UF.at(ry) = rx;
      SIZE.at(rx) += SIZE.at(ry);
      SIZE.at(ry) = 0;
    }
  }
  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
  int size(int x) { // xと連結されてる要素の数を返す
    return SIZE.at(root(x));
  }
};

//関数 (計算量)
//ctoi (O(1))
int ctoi(char c){
  if (c == '0') return 0;
  if (c == '1') return 1;
  if (c == '2') return 2;
  if (c == '3') return 3;
  if (c == '4') return 4;
  if (c == '5') return 5;
  if (c == '6') return 6;
  if (c == '7') return 7;
  if (c == '8') return 8;
  if (c == '9') return 9;
  return -1;
}
//to_char (O(1))
char to_char(int i){
  if (i == 0) return '0';
  if (i == 1) return '1';
  if (i == 2) return '2';
  if (i == 3) return '3';
  if (i == 4) return '4';
  if (i == 5) return '5';
  if (i == 6) return '6';
  if (i == 7) return '7';
  if (i == 8) return '8';
  if (i == 9) return '9';
  return ' ';
}
//素数判定
//一つ一つの判定ではこっちの方が速い(一つ当たりO(√N))
//一方100までに何個あるかなどはエストネスの篩の方が良い（O(NlogN))
bool PN(int x) {
  if (x <= 1) return false; // 1や0,-1は素数ではない
  if (x == 2) return true; // √2 + 1 > 2 なので下でやると 2 ％ 2 = 0 となり判定できない
  for (int i = 2; i < sqrt(x) + 1; i++) {
    if (x % i == 0) return false; // 割れたら素数じゃない
  }
  return true;
}
// A^N mod M を計算する。modしたくなかったらM = LINFとかにすればよい。(O(√N))
ll modpow(ll A, ll N, ll M) {
  long long ans = 1;
  while (N > 0) {
    if (N & 1) ans = ans * A % M;
      A = A * A % M;
      N >>= 1;
   }
  return ans;
}

//ライブラリ終わり

//メイン処理
int main() {
  cout << fixed << setprecision(16); //精度向上
  //ここまでテンプレ
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<vector<int>> T(N + 2, vector<int>(N + 1, 0));
  for (int i = 0; i < M; i++) {
    int L, R;
    cin >> L >> R;
    T.at(L).at(R)++;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= N - i; j++) {
      T.at(j).at(j + i) += (T.at(j + 1).at(j + i) + T.at(j).at(j + i - 1));
      T.at(j).at(j + i) -= T.at(j + 1).at(j + i - 1);
    }
  }
  for (int i = 0; i < Q; i++) {
    int S, G;
    cin >> S >> G;
    cout << T.at(S).at(G) << endl;
  }
}

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

using ll = long long;

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

  int n, q;
  string s;
  cin >> n >> s >> q;

  while (q--) {
    int k;
    cin >> k;

    vector<ll> d(n), m(n), dm(n);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        if (s.at(i) == 'D') d.at(i)++;
        if (s.at(i) == 'M') m.at(i)++;
        continue;
      }

      d.at(i) = d.at(i - 1);
      m.at(i) = m.at(i - 1);
      dm.at(i) = dm.at(i - 1);

      if (i >= k) {
        if (s.at(i - k) == 'D') {
          d.at(i)--;
          dm.at(i) -= m.at(i - 1);
        }
        if (s.at(i - k) == 'M') {
          m.at(i)--;
        }
      }

      if (s.at(i) == 'D') {
        d.at(i)++;
      }
      if (s.at(i) == 'M') {
        m.at(i)++;
        dm.at(i) += d.at(i);
      }
      if (s.at(i) == 'C') {
        ans += dm.at(i);
      }
    }

    cout << ans << '\n';
  }
}


#include <algorithm>
#include <iostream>

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

using namespace std;

int n, m;
int p[1003];
void solve() {
    sort(p, p + n);
    reverse(p, p+n);
    int ans = 0;

    rep(i, n) ans += p[i];
    for (int i = 0; i + m - 1 < n; i += m) {
        ans -= p[i + m - 1];
    }
    cout << ans << endl;
}
int main() {
    while (cin >> n >> m) {
        rep(i, n) cin >> p[i];
        if (n == 0 && m == 0) break;
        solve();
    }
    return 0;
}

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

void insert_sort(long int *num,long int n,long int g,long int *cnt);
int main(){
        long int cnt[1];
        long int num[1000000];
        long int n;
        long int i;
        long int m[1000000];
        long int count=0;
        long int k;
        int yn=0;
        cnt[0]=0;

        cin>>n;
        for(i=0;i<n;i++){
                cin>>num[i];
        }
        m[0]=n-1;
        while(yn==0){
                yn=1;
                insert_sort(num,n,m[count],cnt);
                count++;
                m[count]=m[count-1]/2;

                for(i=0;i<n-1;i++){
                        if(num[i]>num[i+1]){
                                yn=0;
                                break;
                        }
                }

        }
        cout<<count<<endl;
        for(i=0;i<count;i++){
                cout<<m[i];
                if(i<count-1){
                        cout<<" ";
                }else{
                        cout<<endl;
                }
        }
        cout<<cnt[0]<<endl;
        for(i=0;i<n;i++){
                cout<<num[i]<<endl;
        }
        return 0;
}

void insert_sort(long int *num,long int n,long int g,long int *cnt){
        long int i;
        long int j;
        long int k;
        long int tmp;
        long a;
        a=cnt[0];
        for(i=g;i<n;i++){
                tmp=num[i];
                j=i-g;
                while((j>=0)&&(tmp<num[j])){
                        num[j+g]=num[j];
                        j-=g;
                        cnt[0]++;
                }
                num[j+g]=tmp;
        }
}


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

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

  int tasu = A + B;
  int hiku = A - B;
  int kake = A * B;

  if (tasu >= hiku && tasu >= kake) {
    cout << tasu << endl;
  }
  else if (hiku >= kake && hiku >= tasu) {
    cout << hiku << endl;
  }
  else {
    cout << kake << endl;
  }
}

#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

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


#include <algorithm>
#include <boost/optional.hpp>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for(int i = 0, i##_MACRO = (n); i < i##_MACRO; i++)
#define RANGE(i, a, b) for(int i = (a), i##_MACRO = (b); i < i##_MACRO; i++)
#define EACH(e, a) for(auto&& e : a)
#define ALL(a) std::begin(a), std::end(a)
#define RALL(a) std::rbegin(a), std::rend(a)
#define FILL(a, n) memset((a), n, sizeof(a))
#define FILLZ(a) FILL(a, 0)
#define INT(x) (static_cast<int>(x))
#define PRECISION(x) std::fixed << std::setprecision(x)

using namespace std;

using ll = long long;
using VI = std::vector<int>;
using VI2D = std::vector<vector<int>>;
using VLL = std::vector<long long>;
using VLL2D = std::vector<vector<long long>>;

constexpr int INF = 2e9;
constexpr double EPS = 1e-10;
constexpr double PI = acos(-1.0);

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

template <typename T>
constexpr int sign(T x) {
	return x < 0 ? -1 : x > 0 ? 1 : 0;
}

template <>
constexpr int sign(double x) {
	return x < -EPS ? -1 : x > EPS ? 1 : 0;
}

template <typename T, typename U>
constexpr void chmax(T& m, U x) {
	m = max(m, x);
}

template <typename T, typename U>
constexpr void chmin(T& m, U x) {
	m = min(m, x);
}

template <typename T>
constexpr T square(T x) {
	return x * x;
}

int main() {
	int n, k;
	cin >> n >> k;
	VI v(n);
	REP(i, n) { cin >> v[i]; }

	int res_max = 0;
	REP(i, n) { res_max += max(v[i], 0); }
	VI left(k + 1, 0);
	REP(ope, k + 1) {
		REP(bound, ope + 1) {
			int tmp = 0;
			priority_queue<int, VI, greater<int>> pq;
			REP(index, ope) {
				if(index < min(n, bound)) {
					tmp += v[index];
					pq.push(v[index]);
				} else if(!pq.empty() && pq.top() < 0) {
					tmp -= pq.top();
					pq.pop();
				}
			}
			chmax(left[ope], tmp);
		}
	}
	reverse(ALL(v));
	VI right(k + 1, 0);
	REP(ope, k + 1) {
		REP(bound, ope + 1) {
			int tmp = 0;
			priority_queue<int, VI, greater<int>> pq;
			REP(index, ope) {
				if(index < min(n, bound)) {
					tmp += v[index];
					pq.push(v[index]);
				} else if(!pq.empty() && pq.top() < 0) {
					tmp -= pq.top();
					pq.pop();
				}
			}
			chmax(right[ope], tmp);
		}
	}

	int result = 0;
	REP(i, k + 1) { chmax(result, left[i] + right[k - i]); }

	cout << min(result, res_max) << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

const ll INF = 1LL << 60;

int n;
ll D;
ll a[200005];
vector< E > edges;

void solve(int L, int R) {
    if (R - L <= 1) return;
    int mid = (R + L) / 2;

    ll lval = INF;
    int lidx = -1;
    for (int i = L; i < mid; i++) {
        ll tmp = a[i] - D * i;
        if (lval > tmp) {
            lidx = i;
            lval = tmp;
        }
    }

    ll rval = INF;
    int ridx = -1;
    for (int i = mid; i < R; i++) {
        ll tmp = a[i] + D * i;
        if (rval > tmp) {
            ridx = i;
            rval = tmp;
        }
    }

    for (int i = L; i < mid; i++) {
        edges.emplace_back(a[i] + a[ridx] + D * (ridx - i), P(i, ridx));
    }

    for (int i = mid; i < R; i++) {
        edges.emplace_back(a[i] + a[lidx] + D * (i - lidx), P(lidx, i));
    }

    solve(L, mid);
    solve(mid, R);
}

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

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

    int size(int x) {
        return -data[find(x)];
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> D;
    for (int i = 0; i < n; i++) cin >> a[i];
    solve(0, n);

    sort(edges.begin(), edges.end());
    UnionFind uf(n);
    ll ans = 0;
    for (E& e : edges) {
        ll cost = e.first;
        int u = e.second.first, v = e.second.second;
        if (!uf.same(u, v)) {
            uf.unite(u, v);
            ans += cost;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
#define MAX 10000000

int b[110];
bool used[110];
struct edge{int from, to, cost;};

int solve(int a[110][110], int n, int s, int e){
	fill(b, b+110, MAX);
	fill(used, used+110, false);
	b[s] = 0;
	while(1){
		int v = -1;
		for(int i = 1;i <= n;i++){
			if(!used[i] && (v == -1 || b[v] > b[i]))v = i;
		}
		if(v == -1)break;
		used[v] = true;
		for(int i = 1;i <= n;i++){
			if(a[v][i] != -1)b[i] = min(b[i], b[v] + a[v][i]);
		}
	}
	return 0;
}

int main(){
	int a[110][110], n, k;

	while(cin >> n >> k, k){
		fill((int*)a, (int*)(a+110), -1);
		int c, d, e, f;
		for(int i = 0;i < k;i++){
			cin >> c;
			if(c == 0){
				cin >> d >> e;
				solve(a, n, d, e);
				if(b[e] != MAX)cout << b[e] << endl;
				else cout << -1 << endl;
			}else {
				cin >> d >> e >> f;
				if(a[d][e] > f || a[d][e] == -1)
				a[d][e] = a[e][d] = f;
			}
		}

	}

	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vll vector<long long>
#define pll pair<long long, long long>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> o_tree;

const int N=1e5+5;
vll v[N];
ll vis[N];
vll path;
ll maxh[N];
ll f=0;

void dfs(ll x,ll s,ll dest)
{
  if(f)return;
  if(x==dest)
  {
    f=1;
    path.pb(x);
    return;
  }
  path.pb(x);
  for(auto it:v[x])
  {
    if(it==s)continue;
    dfs(it,x,dest);
  }
  if(f)return;
  path.pop_back();
}

void dfs2(ll x,ll s)
{
  ll f=0;
  // cout<<x+1<<"\n";
  for(auto it:v[x])
  {
    if(it==s)continue;
    f=1;
    dfs2(it,x);
    maxh[x]=max(maxh[x],maxh[it]+1ll);
  }
  if(!f)
  {
    maxh[x]=1;
  }
}

void solve()
{
  ll n;
  cin>>n;
  ll aa,bb;
  cin>>aa>>bb;
  aa--,bb--;
  ll i,j;
  for(i=1;i<n;i++)
  {
    ll x,y;
    cin>>x>>y;
    x--,y--;
    v[x].pb(y);
    v[y].pb(x);
  };
  dfs(aa,-1,bb);
  // for(auto it:path)
  // {
  //   cout<<it+1<<" ";
  // }
  // all i need is max height of subtree
  dfs2(bb,-1);
  ll maxi=0;
  for(i=1;i<sz(path);i++)
  {
    // distance travelled to reach node path[i] = i;
    ll dist_1 = i;
    ll dist_from_bb = sz(path)-1-i;
    if(dist_1>dist_from_bb)continue;
    ll maxheight = maxh[path[i]]-1;
    ll dif = dist_from_bb-dist_1;
    ll tans = dist_1;
    tans+=(dif+(maxheight-1));
    // ll tans = (maxheight)+dist_from_bb;
    // cout<<dif<<" "<<maxheight<<" "<<dist_from_bb<<" ";
    // cout<<path[i]+1<<"<-- "<<tans<<"\n";
    maxi=max(maxi,tans);
  }
  cout<<maxi<<"\n";

}

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

if (fopen("input.txt","r" ))
  {
     freopen("input.txt", "r", stdin);
     freopen("output.txt", "w", stdout);
  }
cout<<setprecision(20);

ll t=1;

// cin>>t;

while(t--)
{
solve();
}

return 0;
}


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

int main() {

    int n, m;
    cin >> n;
    unordered_map<string, int> um;
    int res = 0;
    string s;
    for (int i = 0; i < n; i++) {
        cin >> s;
        um[s]++;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> s;
        um[s]--;
    }
    for (auto p : um) {
        res = max(res, p.second);
    }
    cout << res << endl;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

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

const int N = 200200;
const int MAXN = 600600;

int ans = 59408656;
int n;

vector<int> on0[MAXN][4];
vector<int> on1[MAXN][4];
vector<int> on2[MAXN][4];
vector<int> on3[MAXN][4];

int Upd(vector<int> a, vector<int> b)
{
    int ans = 59408656;
    int j = 0;
    for (int i = 0; i < a.size(); i++)
    {
        while (j < b.size() && a[i] > b[j]) j++;
        if (j < b.size())
        {
            ans = min(ans, b[j] - a[i]);
        }
    }
    return ans;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int x, y;
        char dir;
        cin >> x >> y >> dir;
        x += N;
        int z;
        if (dir == 'U') z = 0;
        if (dir == 'R') z = 1;
        if (dir == 'D') z = 2;
        if (dir == 'L') z = 3;
        on0[x][z].push_back(y);
        on1[y][z].push_back(x);
        on2[x + y][z].push_back(x);
        on3[x - y][z].push_back(x);
    }
    for (int i = 0; i < MAXN; i++)
    {
        for (int z = 0; z < 4; z++)
        {
            sort(all(on0[i][z]));
            sort(all(on1[i][z]));
            sort(all(on2[i][z]));
            sort(all(on3[i][z]));
        }
        ans = min(ans, Upd(on0[i][0], on0[i][2]));
        ans = min(ans, Upd(on1[i][1], on1[i][3]));
        ans = min(ans, 2 * Upd(on3[i][0], on3[i][3]));
        ans = min(ans, 2 * Upd(on3[i][1], on3[i][2]));
        ans = min(ans, 2 * Upd(on2[i][1], on2[i][0]));
        ans = min(ans, 2 * Upd(on2[i][2], on2[i][3]));
    }
    if (ans == 59408656) cout << "SAFE";
    else cout << ans * 5;
}


#include <bits/stdc++.h>
#include <ctime>
#define rep(i,a,b) for(int i =a;i<b;i++)
using namespace std;
typedef unsigned long long ll;
inline int cine() {int a; return scanf("%d", &a) ? a : 1e9;}

void solve() {
    int train, bus, mine = 1e9, sum = 0 ;
    rep(i, 0, 2) {
        cin >> train;
        mine = min(mine, train);
    }
    sum += mine;
    mine = 1e9;
    rep(i, 0, 2) {
        cin >> bus;
        mine = min(mine, bus);
    }
    sum += mine;
    cout << sum;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("i.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    solve();
#ifndef ONLINE_JUDGE
    cout << "\nTime Elapsed :" << 0.1 * clock() / CLOCKS_PER_SEC << "s\n";
#endif
}

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

int main() {
  int q,w,e,r; cin >>q>>w>>e>>r;
  cout << min(q,w) + min(e,r);
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <numeric>
#include <ctime>
#include <complex>
#include <bitset>
#include <random>
#include <stack>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define double ld
#define loop(i, n) for(int i = 0; i < (int)n; ++i)
#define loop1(i, n) for(int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair <int, int>
#define all(x) begin(x), end(x)
#define ti tuple <int, int, int>
#define Point Vect
#define mkt make_tuple
#define no {cout << -1; return;}

void solve() {
    int L = 0, R = 1000000;
    int n, m;
    cin >> n >> m;
    loop(i, m) {
        int l, r;
        cin >> l >> r;
        L = max(l, L);
        R = min(R, r);
    }
    if (R < L)
        cout << 0;
    else
        cout << R - L + 1;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //int t; cin >> t; loop(i, t)
    solve();
    return 0;
}

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

using namespace std;

#define rep(i,N) for(int i=0;i<N;i++) //0から
#define REP(i,N) for(int j=i;j<N;j++) //iから

typedef long long ll;

ll binary_search(vector<ll> a, ll n, ll key){
    ll right = n, left = -1;
    ll md = (right + left) / 2;

    while(right - left > 1){
        if(a[md] <= key){
            right = md;
        }else{
            left = md;
        }
        md = (right + left) / 2;
    }
    if(left == -1) return -1; //無い場合
    return right;
}

vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
    vector<ll> p(n,0);
    p[0]=1;
    p[1]=1;
    for(ll i=2;i<n;i++){
        if(p[i]==0){
            prime.push_back(i);
            p[i]=i;
        }
        ll k=prime.size();
        for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
            p[i*prime[j]]=prime[j];
        }
    }
}

#define MOD ((ll)1e+9 + 7)

//cout<<fixed<<setprecision(10);
int main(){
    int n,m;
    cin>>n>>m;
    vector<int> L(m),R(m+1);
    rep(i,m){
        cin>>L[i]>>R[i];
        L[i]--;
    }
    vector<int> cnt(n+1,0);
    rep(i,m){
        cnt[L[i]]++;
        cnt[R[i]]--;
    }
    rep(i,n){
        cnt[i+1]+=cnt[i];
    }
    int ans=0;
    rep(i,n){
        if(cnt[i]==m){
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}


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

using namespace std;

int main(){
	double n;
	cin >> n;
	cout << n*(180-(360/n)) << endl;
	return 0;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#define pi 3.14159265358979323846
#define ll long long
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
int main(){
  string s;
  cin >> s;
  if(s == "RRR"){
    cout << "3\n";
    return 0;
  }
  if(s == "RRS" || s == "SRR"){
    cout << "2\n";
    return 0;
  }
  if(s == "SSS"){
    cout << "0\n";
    return 0;
  }
  cout << "1\n";
  return 0;
}


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

int main()
{
  int n; cin >> n;
  int a;
  string s = to_string(n);
  for (char c : s)
  {
    a += (c - '0');
  }
  if (!(n % a))
  {
    cout << "Yes" << endl;
  }
  else
  {
    cout << "No" << endl;
  }

}


#include <iostream>
#include <string>
using namespace std;
int main(){
    string s; cin >> s;
    int cnt = 0;
    for(int i = 2; i < s.size()-1; i++){
        if(s[i] == 'C') {
            cnt++;
            if(cnt == 1) s[i] = 'c';
        }
    }
    if(cnt != 1){
        puts("WA");
        return 0;
    }
    for(int i = 0; i < s.size(); i++){
        if(i == 0) {
            if(s[i] != 'A'){
                puts("WA");
                return 0;
            }
        }
        else{
            if(s[i] - 'a' < 0){
                puts("WA");
                return 0;
            }
        }
    }
    puts("AC");
    return 0;
}


#include <stdio.h>
	int main() {
		char x[102];
		char g[102];
		scanf ("%s %s", x, g);
		printf("%s%s", g ,x);

	  return 0;
	}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<60) - 1;

int main(){
    string s,t;
    cin >> s >> t;
    cout << t + s << endl;
}


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

int main(void)
{
    int n, cnt=0;
    string a,b,c;
    cin >> n >> a >> b >> c;
    rep(i,n) cnt += set<char>({a[i], b[i], c[i]}).size() - 1;
    cout << cnt << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
#define Priq priority_queue<int,vi,greater<int>>
#define pb push_back
#define mp make_pair
signed main(){
  int N; cin>>N;
  vector<string> S(3);
  rep(i,0,3) cin>>S[i];
  int ans=0;
  rep(i,0,N){
    set<char> T;
    rep(j,0,3)
      T.insert(S[j][i]);
    ans+=T.size()-1;
  }
  cout<<ans<<endl;
  }


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

/** author: __nishad__ **/
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll ;
typedef vector<ll> vll ;

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(v) (v).begin(),(v).end()
#define precision(k) cout<<setprecision(k)<<fixed;
#define loop(i,a,b) for(ll i=(a);i<=(b);++i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define getpos(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define unq(v) sort(all(v)),(v).erase(unique((v).begin(),(v).end()),(v).end())
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323
#define endl "\n"

int main()
{
    IOS;

    ll n, max=0;
    cin>>n;
    int i;

    for(i=n; i>0; i--){
        if(sqrt(i) == int(sqrt(i))){
            max = i;
            cout<<max;
            return 0;
        }
    }

}


#include <cstdio>
#include <algorithm>
#define mod 998244353
#define maxn 200010
#define LL long long
using namespace std;
LL fact[maxn],inv[maxn],ans,t[maxn],p;
int n,m,k,nm;
LL quick_pow(LL a,LL b){//a^b
	LL ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans%mod;
}
void init(){
	int i;
	scanf("%d%d%d",&n,&m,&k);
	nm=n,nm=max(nm,m),fact[0]=1;
	for(i=1;i<=nm;i++)fact[i]=fact[i-1]*i%mod;//fact[i]代表i!
	inv[nm]=quick_pow(fact[nm],mod-2);
	for(i=nm-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;//inv[i]代表i!的乘法逆元
}
LL C(LL a,LL b){//组合数计算
	return fact[a]*inv[a-b]%mod*inv[b]%mod;
}
int main(){
	int i;
	init();
	ans=0,p=quick_pow(m-1,n-k-1);
	for(i=n-k;i<=n;i++){
		ans=(ans+m*p%mod*C(n-1,i-1)%mod)%mod;//C(n-1,i-1)隔板法
		p=p*(m-1)%mod;
	}
	printf("%lld\n",ans);
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s)    (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
int mod = 998244353;
ll fpow(ll x, ll p) {
	if (!p)
		return 1;
	ll sq = fpow(x, p / 2);
	sq *= sq;
	sq %= mod;
	if (p & 1)
		sq *= x;
	return sq % mod;
}
const int N = 2e5 + 9;
ll fact[N];
void preProc() {
	fact[0] = fact[1] = 1;
	for (int i = 2; i < N; i++)
		fact[i] = (fact[i - 1] * i) % mod;
}
ll nCrMod(ll n, ll r) {
	// you must call preProc before using nCr
	// you must modify mod first
	// you must modify N
	if (n < r) return 0;
	return (fact[n] * fpow((fact[n - r] * fact[r]) % mod, mod - 2LL)) % mod;
}
int main() {
	file();
	fast();
	preProc();
	ll n, m, k; cin >> n >> m >> k;
	if (k == n - 1)
		RT(fpow(m, n) << endl);
	ll ans = 0;
	for (int i = 0; i <= k; i++) {
		ll num = m * nCrMod(n - 1, i);
		num %= mod;
		num *= fpow(m - 1, n - i - 1);
		num %= mod;
		ans += num;
		ans %= mod;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned ui;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef pair<bool, bool> pbb;
typedef vector<int> vi;
#define pb push_back
#define fi first
#define se second
#define mid ((l + r) >> 1)
#define ls (i << 1)
#define rs (i << 1 | 1)
#define enum(i, j, k) for(int i = j; i <= (k); i ++)
#define open(i, j, k) for(int i = j; i <  (k); i ++)
#define dec(i, j, k)  for(int i = j; i >= (k); i --)
#define ae(x) for(node *p = h[x]; p; p = p->nxt)
#define fill(x, k) memset(x, k, sizeof x)
#define copy(x, y) memcpy(x, y, sizeof x)
#define fio(x) freopen(x".in", "r", stdin); freopen(x".out", "w", stdout);
template <class T> bool chkmin(T &x, T y)
{ return y < x ? (x = y , true) : false; }
template <class T> bool chkmax(T &x, T y)
{ return y > x ? (x = y , true) : false; }
void __init()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(16);
    srand(time(0) ^ ui(ul(new char)));
}
/* default code ends here */
const int maxn = 2e3 + 10;
int n, m, q;
int a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];
int main()
{
	__init();
	cin >> n >> m >> q;
	string foo;
	enum(i, 1, n)
	{
		cin >> foo;
		enum(j, 1, m)
			a[i][j] = foo[j - 1] - '0';
	}
	/*enum(i, 1, n)
	{
		enum(j, 1, m)
			cerr << a[i][j] << ' ';
		cerr << endl;
	}*/
	enum(i, 1, n)
		enum(j, 2, m)
			b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + bool(a[i][j] && a[i][j - 1]);
	/*enum(i, 1, n)
	{
		enum(j, 1, m)
			cerr << b[i][j] << ' ';
		cerr << endl;
	}*/
	enum(i, 2, n)
		enum(j, 1, m)
			c[i][j] = c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1] + bool(a[i][j] && a[i - 1][j]);
	enum(i, 1, n)
		enum(j, 1, m)
			a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
	int x1, y1, x2, y2;
	while(q --)
	{
		cin >> x1 >> y1 >> x2 >> y2;
		//cerr << (a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]) << ' ' << (b[x2][y2] - b[x2][y1] - b[x1 - 1][y2] + b[x1 - 1][y1]) << ' ' << (c[x2][y2] - c[x2][y1 - 1] - c[x1][y2] + c[x1][y1 - 1]) << endl;
		cout << (a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]) - (b[x2][y2] - b[x2][y1] - b[x1 - 1][y2] + b[x1 - 1][y1]) - (c[x2][y2] - c[x2][y1 - 1] - c[x1][y2] + c[x1][y1 - 1]) << '\n';
	}
	return 0;
}

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

const int inf=1e9+10;
const int maxn=16;

int dp[1<<maxn][maxn],dp2[1<<maxn],n,m,cost[maxn][maxn];
vector <int> g[1<<maxn];

int main(){
	for(int i=0;i<maxn;i++) for(int j=0;j<maxn;j++)
		cost[i][j]=0;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		int u,v,c;
		scanf("%d%d%d",&u,&v,&c);
		u--; v--;
		cost[u][v]=c;
		cost[v][u]=c;
	}
	for(int mask=0;mask<(1<<n);mask++)
		for(int i=0;i<n;i++) for(int j=0;j<n;j++)
			if((mask&(1<<i))&&(mask&(1<<j)))
				dp2[mask]=dp2[mask]+cost[i][j];
	for(int mask=0;mask<(1<<n);mask++) dp2[mask]=dp2[mask]/2;
	for(int mask=0;mask<(1<<n);mask++) for(int i=0;i<n;i++) dp[mask][i]=-inf;
	for(int mask=0;mask<(1<<n);mask++) if(mask&1) dp[mask][0]=dp2[mask];
	for(int mask=0;mask<(1<<n);mask++)
		for(int i=0;i<n;i++){
			if((dp[mask][i]==-inf)||!(mask&(1<<i))) continue;
			for(int mask2=(1<<n)-1-mask;mask2>0;mask2=(mask2-1)&((1<<n)-1-mask)){
				for(int j=0;j<n;j++){
					if(mask2&(1<<j)&&cost[i][j]>0){
						dp[mask|mask2][j]=max(dp[mask|mask2][j],dp[mask][i]+cost[i][j]+dp2[mask2]);
					}
				}
			}
		}
	printf("%d\n",dp2[(1<<n)-1]-dp[(1<<n)-1][n-1]);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>

using namespace std;

int main(){
  string str;
  while(cin >> str){
    //    cout << str << endl;
    string strtmp;
    for(int i=0;i<str.size();i++){
      strtmp.push_back(str[str.size()-1-i]);
    }
    cout << strtmp << endl;
  }

}

#include <bits/stdc++.h>
using namespace std;
//型名省略
typedef long long ll;
typedef vector<long long> vl;
typedef vector<vector<long long>> vll;
typedef vector<char> vc;
typedef vector<vector<char>> vcc;
typedef vector<string> vs;
//マクロ
#define rep(i,n) for(ll i = 0; i < (ll) n; i++)
#define rep2(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define all(A) A.begin(), A.end()

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


#include<cstdio>
#include<queue>
#include<utility>

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 400;

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

bool color[MAX_N][MAX_N];
int H, W;

char ch[MAX_N + 1];

void input(){
	scanf("%d%d", &H, &W);
	for(int i = 0; i < H; ++i){
		scanf("%s", ch);
		for(int j = 0; j < W; ++j){
			color[i][j] = (ch[j] == '#');
		}
	}
}

bool isin_(int i, int j){
	return i >= 0 && i < H && j >= 0 && j < W;
}

bool check_(int i, int j, int d){
	int ni = i + di[d];
	int nj = j + dj[d];
	if(!isin_(ni, nj)) return false;
	if(color[i][j] == color[ni][nj]) return false;
	return true;
}

bool used[MAX_N][MAX_N];

long long solve(){
	long long ans = 0;
	long long b = 0, w = 0;
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			if(used[i][j]) continue;
			b = 0;
			w = 0;
			queue<P> que;
			used[i][j] = true;
			que.push(P(i, j));
			while(!que.empty()){
				P p = que.front();
				que.pop();
				int ci = p.first, cj = p.second;
				if(color[ci][cj]) b++;
				else w++;
				for(int d = 0; d < 4; ++d){
					if(!check_(ci, cj, d)) continue;
					int ni = ci + di[d], nj = cj + dj[d];
					if(used[ni][nj]) continue;
					used[ni][nj] = true;
					que.push(P(ni, nj));
				}
			}
			ans += b * w;
		}
	}
	return ans;
}

int main(){
	input();
	long long ans = solve();
	printf("%lld\n", ans);
	return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
	int INFTY = 1000000000;

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

	int p[n + 1];
	for (int i = 0; i < n; i++) {
		scanf("%d %d", p + i, p + i + 1);
	}

	int m[n + 1][n + 1];

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			m[i][j] = 0;
		}
	}

	for (int l = 2; l <= n; ++l) {
		for (int i = 1; i <= n - l + 1; ++i) {
			int j = i + l - 1;
			m[i][j] = INFTY;
			for (int k = i; k < j; ++k) {
				m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
			}
		}
	}
	cout << m[1][n] << endl;

	return 0;
}


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

int main() {
  double H, W, N;
  cin >> H >> W >> N;

  if(H >= W) cout << ceil(N/H) << endl;
  else cout << ceil(N/W) << endl;
}

#include <iostream>
#include <string>

namespace ABC169
{

  class A
  {
  public:
    A() = default;
    ~A() = default;

    void Run()
    {

      int A = 0;
      int B = 0;
      std::cin >> A;
      std::cin >> B;

      std::cout << A*B << std::endl;
    }
  };

}
int main()
{

  ABC169::A prob;
  prob.Run();

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

//constructor: LCA(graph);
template<class T>
class LCA_weight {
private:
    int n;
    int D = 30;                        //hight
    vector<int> depth;                 //depth from root
    vector<vector<pair<int,T>>> hen;   //tree
    vector<vector<int>> par;           //doubling array
    vector<T> dista;                    //distance form root

    void dfs(int v, int p, int dep, T dep_dist) {
        par[v][0] = p;
        depth[v] = dep;
        dista[v] = dep_dist;
        for(auto i : hen[v]) {
            if(i.first == p)continue;
            dfs(i.first, v, dep + 1, dep_dist + i.second);
        }
    }

public:

    LCA_weight(const vector<vector<pair<int,T>>>& hen):hen(hen) {
        n = hen.size();
        par.resize(n, vector<int>(D + 1));
        depth.resize(n);
        dista.resize(n);

        dfs(0, -1, 0, 0);
        for(int i = 0; i < D; i++) {
            for(int j = 0; j < n; j++) {
                if(par[j][i] == -1)par[j][i + 1] = -1;
                else par[j][i + 1] = par[par[j][i]][i];
            }
        }
    }

    //go back to root
    int back(int v, int dist) {
        for(int i = 0; i < D; i++)if((1 << i) & dist) {
            if(v == -1)return v;
            v = par[v][i];
        }
        return v;
    }

    //get LCA
    int get(int u, int v) {
        int ret = 0;
        if(depth[u] > depth[v])swap(u, v);
        ret = depth[v] - depth[u];
        v = back(v, ret);
        if(u == v)return v;

        for(int i = D; i >= 0; i--) {
            if(par[u][i] != par[v][i]) {
                u = par[u][i];
                v = par[v][i];
            }
        }
        return par[u][0];
    }

    //u-v path distance
    T dist(int u, int v) {
        int lca = get(u, v);
        return (dista[u] + dista[v]) - dista[lca] * 2;
    }

    //print parents
    void print(int i) {
        cerr << "[";
        for(int j = 0; j < n; j++)cerr << par[j][i] << (j == n - 1 ? "" : ",");
        cerr << "]";
    }

};

int main()
{
    int n; cin >> n;
    vector<vector<pair<int,ll>>> hen(n);
    for(int i = 0; i < n - 1; i++) {
        int a, b, c; cin >> a >> b >> c;
        a--, b--;
        hen[a].emplace_back(b, c);
        hen[b].emplace_back(a, c);
    }
    LCA_weight<ll> lca(hen);
    int q, k; cin >> q >> k;
    k--;
    while(q--) {
        int s, t; cin >> s >> t; s--, t--;
        cout << lca.dist(s, k) + lca.dist(t, k) << endl;
    }
}

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
// using namespace atcoder;

struct Edge {
    ll to;
    ll cost;
    Edge(ll t, ll c) {
        to = t;
        cost = c;
    }
};

typedef vector<vector<Edge>> Graph;

Graph G;
vector<int> seen;
vector<ll> d;

void dfs(ll node, ll value) {
    seen[node] = 1;
    d[node] = value;
    for (Edge e: G[node]) {
        if (seen[e.to] == 0) {
            dfs(e.to, value + e.cost);
        }
    }
}

void Main()
{
    ll n;
    cin >> n;

    vector<ll> a(n-1), b(n-1), c(n-1);
    rep(i, n-1) cin >> a[i] >> b[i] >> c[i];
    ll Q, K;
    cin >> Q >> K;
    vector<ll> x(Q), y(Q);
    rep(i, Q) cin >> x[i] >> y[i];

    G.assign(n, vector<Edge>());
    seen.assign(n, 0);
    d.assign(n, 0);
    rep(i, n-1) {
        G[b[i]-1].push_back(Edge{a[i]-1, c[i]});
        G[a[i]-1].push_back(Edge{b[i]-1, c[i]});
    }

    dfs(K - 1, 0);

    rep(i, Q) {
        ll res = d[x[i] -1] + d[y[i] - 1];
        put(res);
    }

}
signed main(){ Main();return 0;}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<vc> vcc;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim)
{
    uniform_int_distribution<int> uid(0, lim - 1);
    return uid(rang);
}

int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);

const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];

void solve()
{
    ll i, j, n, m;
    cin >> n >> m;
    vi a(10);
    fo(i, m)
    {
        cin >> j;
        a[j] = 1;
    }
    for (i = n; i <= 100000; i++)
    {
        string s = to_string(i);
        bool k = true;
        fo(j, s.size())
        {
            if (a[s[j] - '0'])
            {
                k = false;
                break;
            }
            else
            {
                continue;
            }
        }
        if (k)
        {
            cout << i << endl;
            return;
        }
        else
        {
            continue;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

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

    return 0;
}

int mpow(int base, int exp)
{
    base %= mod;
    int result = 1;
    while (exp > 0)
    {
        if (exp & 1)
            result = ((ll)result * base) % mod;
        base = ((ll)base * base) % mod;
        exp >>= 1;
    }
    return result;
}

void ipgraph(int n, int m)
{
    int i, u, v;
    while (m--)
    {
        cin >> u >> v;
        u--, v--;
        g[u].pb(v);
        g[v].pb(u);
    }
}

void dfs(int u, int par)
{
    for (int v : g[u])
    {
        if (v == par)
            continue;
        dfs(v, u);
    }
}

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

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;

const int INT_INF = 2147483646;
const ll LL_INF = 9223372036854775807;
const double PI=3.1415926535897932;

bool SecondCompare(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second<b.second;
}

bool SecondCompareDes(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
}

ll minll(ll a, ll b){
    if(a >= b)  return b;
    else    return a;
}

ll maxll(ll a, ll b){
    if(a >= b)  return a;
    else    return b;
}

ll n,k;

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

}