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

int main(int argc, char const *argv[])
{
	int n,p;
	while(cin>>n>>p,n+p)
	{
		int a[n];
		for (int i = 0; i < n; ++i) a[i]=0;
		int turn=0,s=p;
		while(1)
		{
			if(s>0)
			{
				++a[turn],--s;
				if(a[turn]==p) break;
				++turn;
				if(turn==n) turn=0;
			}
			else
			{
				if(a[turn]>0) s=a[turn],a[turn]=0;
				++turn;
				if(turn==n) turn=0;
			}
		}
		cout<<turn<<endl;
	}
	return 0;
}

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
typedef pair<int,int> P;
int w,h;
int grid[21][21];
P start,goal;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool isvalid(int x,int y){
  return (x>=0&&x<w&&y>=0&&y<h);
}
int dfs(P p, int t){
  if(p == goal) return t;
  if(t > 10) return t;
  int ret = 100;
  int x = p.first;
  int y = p.second;
  REP(r,4){
    int nx = x + dx[r];
    int ny = y + dy[r];
    if(isvalid(nx,ny) && grid[ny][nx] != 1){
      while(isvalid(nx,ny)){
        if(grid[ny][nx] == 3 ||
            (isvalid(nx+dx[r],ny+dy[r]) && grid[ny+dy[r]][nx+dx[r]] == 1)){
          if(grid[ny][nx] != 3) grid[ny+dy[r]][nx+dx[r]] = 0;
          ret = min(ret,dfs(P(nx,ny),t+1));
          if(grid[ny][nx] != 3) grid[ny+dy[r]][nx+dx[r]] = 1;
          break;
        }
        nx += dx[r];
        ny += dy[r];
      }
    }
  }
  return ret;
}
int main(){
  while(cin>>w>>h,w){
    REP(y,h)REP(x,w){
      cin>>grid[y][x];
      if(grid[y][x]==2){
        start = P(x,y);
      }
      if(grid[y][x]==3){
        goal = P(x,y);
      }
    }
    int ans = dfs(start,0);
    if(ans > 10) ans = -1;
    cout<<ans<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    set<pair<int, int>> st;
    multiset<int> mst;
    for (int i = 0; i < w; i++) {
        st.emplace(i, i);
        mst.emplace(0);
    }
    st.emplace(w, w);
    int inf = 1e9;
    mst.emplace(inf);
    for (int i = 0; i < h; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        vector<pair<int, int>> v;
        int c = -1;
        for (auto it = st.lower_bound({a, -1}); it != st.end(); it++) {
            pair<int, int> p = *it;
            if (a <= p.first && p.first <= b) {
                c = max(c, p.second);
                v.emplace_back(p);
            } else {
                break;
            }
        }
        for (auto p : v) {
            st.erase(p);
            mst.erase(mst.find((p.first == w ? inf : p.first - p.second)));
        }
        if (c != -1) {
            st.emplace(b, c);
            if (b == w) {
                b = inf;
                c = 0;
            }
            mst.emplace(b - c);
        }
        int res = *mst.begin();
        if (res >= 1e8) {
            cout << -1 << '\n';
        } else {
            cout << res + i + 1 << '\n';
        }
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll linf = 1e18;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

int H, W;
bitset<160 * 160 + 1> dp[81][81];
int cost[81][81];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> H >> W;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            cin >> cost[i][j];
        }
    }
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            int tmp;
            cin >> tmp;
            cost[i][j] -= tmp;
        }
    }

    int s = H + W - 1;
    dp[0][0][80 + cost[0][0]] = true;
    dp[0][0][80 - cost[0][0]] = true;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            dp[i + 1][j] |= dp[i][j] << (80 + cost[i + 1][j]);
            dp[i + 1][j] |= dp[i][j] << (80 - cost[i + 1][j]);
            dp[i][j + 1] |= dp[i][j] << (80 + cost[i][j + 1]);
            dp[i][j + 1] |= dp[i][j] << (80 - cost[i][j + 1]);
        }
    }

    int ans = inf;
    for(int i = 0; i <= s * 160; i++){
        if(!dp[H - 1][W - 1][i]) continue;
        chmin(ans, abs(i - s * 80));
    }
    cout << ans << endl;
    return 0;
}


#include <stdio.h>

typedef long long var;

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

int h, w, a, b;
var inv[N + 1], fac[N + 1], invfac[N + 1];

var f(int n, int m);
var c(int n, int m);

int main() {
  inv[0] = fac[0] = invfac[0] = inv[1] = 1;
  for (int i = 2; i <= N; ++i)
    inv[i] = ((MOD - MOD / i) * inv[MOD % i]) % MOD;
  for (int i = 1; i <= N; ++i) {
    fac[i] = (fac[i - 1] * i) % MOD;
    invfac[i] = (invfac[i - 1] * inv[i]) % MOD;
  }
  scanf("%d%d%d%d", &h, &w, &a, &b);
  var res = f(h, w);
  for (int i = 1; i <= a; ++i)
    (res -= (f(h - i + 1, b) * f(i, w - b)) % MOD) %= MOD;
  printf("%lld\n", (res + MOD) % MOD);
  return 0;
}

var f(int n, int m) {
  return c((n - 1) + (m - 1), m - 1);
}

var c(int n, int m) {
  if (n < 0 || m < 0 || n < m) return 0;
  return (((fac[n] * invfac[m]) % MOD) * invfac[n - m]) % MOD;
}

/* Author: Dhruv Rastogi */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define all(sdk) sdk.begin(),sdk.end()
#define mll map<int, int>
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;++i)
#define repd(i,a,b) for(int i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define endl '\n'
#define vvll vector<vector<int> >
#define vll vector<int>
#define mint map<int,int>
#define sz(x) (int)x.size()
#define sll set<int>
#define pll pair<int,int>
#define F first
#define S second
const long double PI = acos(-1.0);

int dp[45][2][1<<18];

void solve(){
	dp[0][0][1]=1;
	int n,x,y,z;
	cin>>n>>x>>y>>z;
	int reqd_mask = 1|(1<<z)|(1<<(y+z))|(1<<(x+y+z));
	rep(i,0,n){
		rep(j,0,2){
			rep(k,0,1<<18){
				rep(d,1,11){
					int new_mask = ((k<<d)|1)&((1<<18)-1);
					int new_j = j;
					if((new_mask&reqd_mask)==reqd_mask) new_j=1;
					dp[i+1][new_j][new_mask]+=dp[i][j][k];
					dp[i+1][new_j][new_mask]%=hell;
				}
			}
		}
	}
	int ans=0;
	rep(i,0,1<<18){
		ans+=dp[n][1][i];
		ans%=hell;
	}
	cout<<ans<<endl;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
#define int long long
using namespace std;
using Graph = vector<vector<int>>;
// Californium:Paken
vector<bool> seen;
void dfs(const Graph& G, int v) {
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) {
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v); // 再帰的に探索
    }
}
signed main() {
    int L, R; cin >> L >> R;
    int M = 2019;
    if (L / 2019 == R / 2019 && L % 2019 != 0) {
        for (int X = L; X < min(L+2019,R); X++) {
            for (int Y = X+1; Y <= min(L+2019,R); Y++) {
                M = min(M,X*Y%2019);
            }
        }
        cout << M << endl;
    }
    else {
        cout << 0 << endl;
    }
}

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

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
const int INF_N = 1e+9;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

//繰り返し二乗法
ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

//逆元(Eucledean algorithm)
ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

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

// 素因数が何乗かをmapで返却する
map< ll, int > prime_factor(ll n) {
  map< ll, int > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

void solve() {
    ll N, M; cin >> N >> M;
    auto pf = prime_factor(M);

    ll res = 1;
    for(auto a: pf){
        res = modint(modint(res) * comb(a.second + N - 1, a.second));
    }

    cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //cout << fixed << setprecision(10);
  init_f();
  //init();
  //int t; cin >> t; rep(i, t)solve();
  solve();
//   stop
    return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int,int>;

struct edge{
  ll to, cost;
};

int main()
{
  ll N;
  cin >> N;
  vector<vector<ll>> A(N-1,vector<ll>(3));
  vector<vector<edge>> vec(N,vector<edge> ());
  REP(i, N-1){
    REP(j, 3){
      cin >> A.at(i).at(j);
    }
    A.at(i).at(0)--;
    A.at(i).at(1)--;
    edge e1 = {A.at(i).at(1),A.at(i).at(2)};
    edge e2 = {A.at(i).at(0),A.at(i).at(2)};
    vec.at(A.at(i).at(0)).push_back(e1);
    vec.at(A.at(i).at(1)).push_back(e2);
  }

  ll Q,K;
  cin >> Q >> K;
  K--;

  queue<ll> que;
  vector<ll> dist(N,-1);
  que.push(K);
  dist[K]=0;
  while(!que.empty()){
    ll v = que.front();
    que.pop();
    for(auto e:vec.at(v)){
      ll nv = e.to;
      ll ncost = e.cost;
      if(dist[nv]!=-1) continue;

      dist[nv]=dist[v]+ncost;
      que.push(nv);
    }
  }

  vector<ll> ans(Q);
  REP(i,Q){
    ll x,y;
    cin >> x >> y;
    x--;
    y--;
    ans.at(i)=dist[x]+dist[y];
  }

  REP(i,Q){
    cout << ans.at(i) << endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll LINF = 1LL << 60;

struct edge {
  ll to, cost;
};

vector<vector<edge>> g;
vector<bool> visited;
vector<ll> dist;

void dfs(int s) {
  visited[s] = true;
  rep(i, g[s].size()) {
    if (visited[g[s][i].to]) continue;
    int to = g[s][i].to, cost = g[s][i].cost;
    dist[to] = dist[s] + cost;
    dfs(to);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //////////////////////////////////////
  ll n;
  cin >> n;
  g.resize(n);
  visited.resize(n);
  dist.resize(n);
  rep(i, n) {
    visited[i] = false;
    dist[i] = LINF;
  }
  rep(i, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    g[a].push_back(edge{b, c});
    g[b].push_back(edge{a, c});
  }
  ll q, k;
  cin >> q >> k;
  --k;
  dist[k] = 0;
  dfs(k);
  rep(i, q) {
    ll x, y, ans = 0;
    cin >> x >> y;
    --x;
    --y;
    cout << dist[x] + dist[y] << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

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

#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)

#define EPS 1e-10
//ここから編集

struct Mo
{
  vector<int> left, right, order;
  vector<bool> v;
  int width;
  int nl, nr, ptr;

  Mo(int n, int q) : width(sqrt(n)), nl(0), nr(0), ptr(0), v(n), left(q), right(q){}

  void insert(int l, int r)
  {
    left[ptr] = l;
    right[ptr] = r;
    ptr++;
  }

  void build()
  {
    ptr =  0;
    order.resize(left.size());
    iota(all(order), 0);
    sort(all(order), [&](int a, int b)
    {
      if(left[a] / width != left[b] / width) return left[a] < left[b];
      else{
        if ((left[a] / width) % 2 == 0) return right[a] < right[b];
        else return right[a] > right[b];
      }
    });
  }

  inline int process()
  {
    const auto id = order[ptr];
    while(nl > left[id]) distribute(--nl);
    while(nr < right[id]) distribute(nr++);
    while(nl < left[id]) distribute(nl++);
    while(nr > right[id]) distribute(--nr);
    return order[ptr++];
  }

  inline void distribute(int idx)
  {
    v[idx].flip();
    if(v[idx])add(idx);
    else del(idx);
  }

  inline void add(int idx);

  inline void del(int idx);
};

int N, A[501010], Q;
int ans[501010];
int cnt[1010101], sum;

inline void Mo::add(int idx)
{
  if(cnt[A[idx]]++ == 0) ++ sum;
}

inline void Mo::del(int idx)
{
  if(--cnt[A[idx]] == 0) --sum;
}

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

  scanf("%d %d", &N, &Q);
  for(int i=0; i<N; i++) scanf("%d", &A[i]);

  Mo mo(N,Q);
  for(int i=0; i<Q; i++){
    int a, b; scanf("%d %d", &a, &b);
    mo.insert(--a, b);
  }

  mo.build();
  for(int i=0; i<Q; i++){
    int idx = mo.process();
    ans[idx] = sum;
  }
  for(int i=0; i<Q; i++){
    printf("%d\n", ans[i]);
  }
  return 0;
}

#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int yuzu=5e5;
typedef int fuko[yuzu|10];
struct node {
  int l,r,id;
  void rd(int x) {
    read(l),read(r),id=x;
  }
}qr[yuzu|10];
fuko c,cnt,lxy;
int main() {
  int i,n,q,bk;
  read(n),read(q),bk=sqrt(n);
  for (i=1;i<=n;++i) c[i]=read();
  for (i=1;i<=q;++i) qr[i].rd(i);
  sort(qr+1,qr+q+1,[&](node a,node b){return a.l/bk^b.l/bk?a.l<b.l:a.r<b.r;});
  int nl=1,nr=0,ans=0;
  for (i=1;i<=q;++i) {
    for (;nr<qr[i].r;) !cnt[c[++nr]]++?++ans:0;
    for (;nr>qr[i].r;) !--cnt[c[nr--]]?--ans:0;
    for (;nl<qr[i].l;) !--cnt[c[nl++]]?--ans:0;
    for (;nl>qr[i].l;) !cnt[c[--nl]]++?++ans:0;
    lxy[qr[i].id]=ans;
  }
  for (i=1;i<=q;++i) printf("%d\n",lxy[i]);
}


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

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

ll gcd(ll x, ll y){
	if(x%y == 0) return y;
	else return gcd(y, x%y);
}

int main(){
	ll a, b;
	cin >> a >> b;
	ll c = gcd(a, b);
	ll sqc = static_cast<ll>(sqrt(c));
	ll ans = 0;
	for(ll i = 2; i <= sqc; i++){
		ll k = 0;
		while (c% i == 0) {
			c/=i;
			k++;
		}
		if(k != 0) ans++;
	}
	if(c != 1) ans++;
	cout << ans+1 << endl;
}

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

ll pow(ll x,ll y,ll mod){
    if(y==0) return 1;
    if(y&1) return x*pow(x,y-1,mod)%mod;
    ll p=pow(x,y/2,mod);
    return p*p%mod;
}

ll com(ll x,ll y,ll mod){
    ll res=1;
    rp(i,y) {
        res *= x-i;
        res %= mod;
    }
    rp(i,y){
        res *= pow(i+1,mod-2,mod);
        res %= mod;
    }
    return res;
}
int main(){
    int n,m;
    cin >> n >> m;
    vi p_count;
    for(int i=2; i*i<=m; i++){
        int ctr=0;
        while(m%i==0){
            m /= i;
            ctr++;
        }
        if(ctr) p_count.pb(ctr);
    }
    if(m!=1) p_count.pb(1);
    int ps=p_count.size();
    ll ans=1;
    for(auto p:p_count){
        ans *= com((ll)p+(ll)n-1ll,(ll)(n-1),Mod);
        ans %= Mod;
    }
    print(ans);
    return 0;
}


#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
// please, read the question correctly (do you need set or multiset)???
const int N=200010; //check the limits, dummy
int x[N], y[N];
int n, m;
char s[N][11];
int main(){
	int t;
	// while(){
	scanf("%d",&n);
	vector<pair<int,int>>v1,v2, v3, v4, v5, v6;
	for(int i=0; i<n; ++i){
		scanf("%d%d%s",x+i, y+i, s[i]);
		if(s[i][0]=='U'){
			v1.push_back(mk(x[i], i));
			v5.push_back(mk(y[i], i));
		}
		else if(s[i][0]=='D'){
			v2.push_back(mk(x[i], i));
			v6.push_back(mk(y[i], i));
		}
		else if(s[i][0]=='R'){
			v3.push_back(mk(x[i], i));
		}
		else{
			v4.push_back(mk(x[i], i));
		}
	}
	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	sort(v3.begin(), v3.end());
	sort(v4.begin(), v4.end());

	sort(v5.begin(), v5.end());
	sort(v6.begin(), v6.end());

	int ans=1e9;
	int j=0, j1=0, j2=0;
	map<int, int> mp, mp1, mp2;
	for(int i=0; i<v4.size(); ++i){
		for(j;j<v1.size(); ++j){
			if(v1[j].fs>v4[i].fs)break;
			auto tmp=v1[j].sc;
			mp[x[tmp]-y[tmp]]=x[tmp];
		}
		auto tmp=v4[i].sc;
		if(mp.count(x[tmp]-y[tmp])){
			ans=min(ans, (x[tmp]-mp[x[tmp]-y[tmp]])*10);
		}

		for(j1;j1<v3.size(); ++j1){
			if(v3[j1].fs>v4[i].fs)break;
			auto tmp=v3[j1].sc;
			mp1[y[tmp]]=x[tmp];
		}
		 tmp=v4[i].sc;
		if(mp1.count(y[tmp])){
			ans=min(ans, (x[tmp]-mp1[y[tmp]])*5);
		}

		for(j2; j2<v2.size(); ++j2){
			if(v2[j2].fs>v4[i].fs)break;
			auto tmp=v2[j2].sc;
			mp2[x[tmp]+y[tmp]]=x[tmp];
		}
		 tmp=v4[i].sc;
		if(mp2.count(x[tmp]+y[tmp])){
			ans=min(ans, (x[tmp]-mp2[x[tmp]+y[tmp]])*10);
		}
	}
	mp.clear();
	mp1.clear();
	j=(int)v1.size()-1;
	j1=(int)v2.size()-1;
	for(int i=(int)(v3.size())-1; i>=0; --i){
		for(j; j>=0; --j){
			if(v1[j].fs<v3[i].fs)break;
			auto tmp=v1[j].sc;
			mp[x[tmp]+y[tmp]]=x[tmp];
		}
		auto tmp=v3[i].sc;
		if(mp.count(x[tmp]+y[tmp])){
			ans=min(ans, (mp[x[tmp]+y[tmp]]-x[tmp])*10);
		}

		for(j1; j1>=0; --j1){
			if(v2[j1].fs<v3[i].fs)break;
			auto tmp=v2[j1].sc;
			mp1[x[tmp]-y[tmp]]=x[tmp];
		}
		 tmp=v3[i].sc;
		if(mp1.count(x[tmp]-y[tmp])){
			ans=min(ans, (mp1[x[tmp]-y[tmp]]-x[tmp])*10);
		}
	}

	j=0;
	mp.clear();
	for(int i=0; i<v6.size(); ++i){
		for(j; j<v5.size(); ++j){
			if(v5[j].fs>v6[i].fs)break;
			auto tmp=v5[j].sc;
			mp[x[tmp]]=y[tmp];
		}
		auto tmp=v6[i].sc;
		if(mp.count(x[tmp])){
			ans=min(ans, (y[tmp]-mp[x[tmp]])*5);
		}
	}
	if(ans==1e9)
		printf("SAFE\n");
	else
		cout<<ans<<endl;
	// }
}

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T>
bool chmax(T& a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for(int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T>
vector<T> make_v(size_t a) {
    return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
    for(auto& e : t) {
        fill_v(e, v);
    }
};
struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) {
        par.resize(n);
        siz.resize(n, 1);
        for(int i = 0; i < n; i++)
            par[i] = i;
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    bool unite(int u, int v) {
        u = find(u), v = find(v);
        if(u == v)
            return false;
        if(siz[u] < siz[v])
            swap(u, v);
        par[v] = u;
        siz[u] += siz[v];
        return true;
    }
    bool same(int u, int v) { return find(u) == find(v); }
};
struct edge {
    ll u, v, cost;
    bool operator<(const edge& e) { return cost < e.cost; }
};
ll d;
vector<ll> a;
vector<edge> es;
void calc(int l, int r) {
    if(r - l <= 1) {
        return;
    }
    int m = (l + r) / 2;
    ll mi = LINF, s, t;
    for(ll i = l; i < m; i++) {
        ll k = -i * d + a[i];
        if(mi > k) {
            mi = k;
            s = i;
        }
    }
    mi = LINF;
    for(ll i = m; i < r; i++) {
        ll k = i * d + a[i];
        if(mi > k) {
            mi = k;
            t = i;
        }
    }
    for(ll i = l; i < m; i++) {
        es.push_back({i, t, (t - i) * d + a[t] + a[i]});
    }
    for(ll i = m; i < r; i++) {
        es.push_back({s, i, (i - s) * d + a[i] + a[s]});
    }
    calc(l, m);
    calc(m, r);
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n >> d;
    a.resize(n + 1);
    for(ll i = 1; i <= n; i++) {
        cin >> a[i];
    }
    calc(1, n + 1);
    UnionFind uf(n + 1);
    sort(all(es));
    ll ans = 0;
    for(auto e : es) {
        if(uf.unite(e.u, e.v)) {
            ans += e.cost;
        }
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define uu unsigned
#define io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define MOD 1000000007
#define pl pair<ll,ll>
#define mkp make_pair
#define ss second
#define ff first
#define mit(a,b) map<a,b>::iterator
#define sit(a) set<a>::iterator
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define printcas cout<<"Case "<<cas<<": "
#define _ continue;
#define endl '\n'
#define __ cout<<endl;continue;
#define dd (double)
const ll oo=(1ll<<60);
int gi(){
	int x=0,w=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')w=0,ch=getchar();
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
double dp[305][305][305]={0};
int main()
{
     //io;
    //string hudai(1000000,'a');
    int T=1;
    //T=gi();
     for(int cas=1;cas<=T;cas++)
    {
        int n,x,cnt[4]={0},i,j,k;
        n=gi();
        for(i=0;i<n;i++)
        {
            x=gi();
            cnt[x]++;
        }
        dp[0][0][0]=0.0;
        for(i=0;i<=cnt[3];i++)
        {
            for(j=0;j<=cnt[2]+cnt[3];j++)
            {
                for(k=0;k<=cnt[1]+cnt[2]+cnt[3];k++)
                {
                    if(i+j+k==0) _
                    dp[i][j][k]=(1.0/(i+j+k+0.0))*(n+0.0+((!i)?0.0:i*1.0*dp[i-1][j+1][k])+((!j)?0.0:j*1.0*dp[i][j-1][k+1])+((!k)?0.0:k*1.0*dp[i][j][k-1]));
                    //if(dp[i][j][k]-n==0.0) cout<<i<<' '<<j<<' '<<k<<endl;
                }
            }
        }
        printf("%.15lf",dp[cnt[3]][cnt[2]][cnt[1]]);

    }
    return 0;
}


	#include<bits/stdc++.h>
    using namespace std;
    #define 	ll 			long long
    #define 	w(t)		int t; cin>>t; while(t--)
	#define 	AsmShuvo()  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int main()
    {
    AsmShuvo()

    	ll N, sum=0, i;
		cin>>N;

		for(i=1; i<=N; i++){
		if(i%3 != 0 && i%5 != 0){
			sum = sum+i;
		    }
		}
		cout<<sum<<endl;

	}

#if __has_include("debug.h")
    #include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define d(...) 2;
#endif

#define int long long int

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"

#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ff first
#define ss second
#define eps 1e-10

#define gcd(a,b)            __gcd((a),(b))
#define lcm(a,b)            ((a*b)/gcd(a,b))

void solve();

int32_t main()
{
    #ifdef DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
    #endif

    fast

    int t = 1;
	// cin>>t;

	while(t--)
	    solve();
	return 0;
}

void solve()
{
	int i,j;
	int a;
	cin>>a;
	cout<<a+a*a+a*a*a<<endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=1;i<=n;i++)
const ll MOD = 1000000007;
long long modinv(long long a, long long m=MOD) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
    /*MODの割り算の際に
    a/＝b;
    を
    a *= modinv(b,MOD);
    とする。
        */
}
int main(){
    string N;cin >> N;
    rep(i,N.length()){
        if(N[i]=='7'){
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;
    return 0;
}

#include<iostream>

using namespace std;

int main() {
	string str;
	cin >> str;
	//0から4文字分
	str.replace(0, 4, "2018");
	printf("%s", str.c_str());

	return 0;
}


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

int n, k;

int main(){
	while(1){
		scanf("%d %d", &n, &k);
		if(n == 0 && k == 0) break;

		vector<string> v;
		set<string> num;
		for(int i=0; i<n; i++){
			char tmp[3];
			scanf("%s", tmp);
			v.push_back(tmp);
		}
		sort(v.begin(), v.end());

		do{

			string tmp = "";
			for(int i=0; i<k; i++)
				tmp += v[i];
			num.insert(tmp);

		}while(next_permutation(v.begin(), v.end()));

		printf("%lu\n", num.size());
	}
	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

int card[10],n,k,sen[10];
vector<int> vec;

void dfs(int h,int num){

    if(h==k){
        for(int i=0;i<vec.size();i++)
            if(vec[i]==num) return;
        vec.push_back(num);
        return;
    }

    for(int i=0;i<n;i++){
        if(!sen[i]){
            sen[i]=true;
            dfs(h+1,num*pow(10,((int)log10(card[i])+1))+card[i]);
            sen[i]=false;
        }
    }

    return;
}

void solve(){

    for(int i=0;i<n;i++)
        scanf("%d",&card[i]);

    dfs(0,0);

    printf("%d\n",(int)vec.size());

    vec.clear();

    return;
}

int main(){

    while(1){
        scanf("%d%d",&n,&k);
        if(n+k==0) break;
        solve();
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ar array
#define endl '\n'
#define ll long long
#define in insert
#define pb push_back
#define vt vector
#define P_Q(x) priority_queue<x>
#define p_q(x) priority_queue<x, vector<x>, greater<x>>
#define Rep(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(ll i=a;i>=b;i--)
#define FOR(m) Rep(i,1,m)
#define For(m) Rep(i,0,m-1)
#define Rbl(x,a) for(auto &x : a)
#define FIO ios::sync_with_stdio(0);   cin.tie(0);
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define vpii vector<pii>
#define vpll vector<pll>
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define mod 1000000007

const int mxN = 2e5 + 1;
//ll a[mxN];
ll d;
string k;

void add(ll &a, ll b)
{
    a = (a + b) % mod;
}

int main()
{
FIO

    cin >> k >> d;
    vt<vt<ll>> dp(d, vt<ll>(2, 0));
    dp[0][0] = 1;
    int len = k.length();
    for(int i = 0; i < len; i++)
    {
        vt<vt<ll>> new_dp(d, vt<ll>(2, 0));
        for(int sum = 0; sum < d; sum++)
        {
            for(bool small : {0, 1})
            {
                for(int digit = 0; digit < 10; digit++)
                {
                    if(digit > k[i] - '0' && !small)
                        break;
                    add(new_dp[(sum + digit) % d][small || (digit < k[i] - '0')], dp[sum][small]);
                }
            }
        }
        dp = new_dp;
    }

    ll ans = mod - 1;
    add(ans, dp[0][0] + dp[0][1]);
    cout << ans << endl;

    return 0;
}

#include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 10000
#define MAXD 100

int dp[MAXN+1][MAXD+1][2];

int main(){
  string s;cin>>s;
  int n = s.size();
//  reverse(s.begin(), s.end());
  int d;cin>>d;

  dp[0][0][1]=1;

  rep(i, n)rep(j, d)rep(k, 10){
    int cd = s[i]-'0';

    int nj = (j+k)%d;

//cout<<cd<<' '<<j<<'>'<<nj<<' '<<k<<endl;

    if(k==cd){
      dp[i+1][nj][1] += dp[i][j][1];
      dp[i+1][nj][0] += dp[i][j][0];
    }else if(k<cd){
      dp[i+1][nj][0] += dp[i][j][1] + dp[i][j][0];
    }else{
      dp[i+1][nj][0] += dp[i][j][0];
    }
    dp[i+1][nj][1] %= MOD;
    dp[i+1][nj][0] %= MOD;
  }

  ll result = (dp[n][0][1]+dp[n][0][0])%MOD;
  cout << (result-1+MOD)%MOD << endl;

  return 0;

cout<<dp[n][0][1]<<' '<<dp[n][0][0]<<endl;
cout<<dp[n][0][1]<<' '<<dp[n][0][0]<<endl;

rep(i, n+1){
  rep(j, d+1)cout<<dp[i][j][0]+dp[i][j][1]<<' ';
  cout<<endl;
}

  return 0;
}


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

typedef long long int ll;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
const ll mod = 1000000007;
const ll INF = 10010010010010010;

int main() {
    int a,b; cin >> a >> b;
    cout << (b+a-3) / (a-1) << endl;
    return 0;
}

#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
#include <assert.h>
#include <deque>
using namespace std;
typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA cerr << "AAAAA" << endl
#define debug_ cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                   \
    cout << #v << endl;                \
    for (int i = 0; i < v.size(); i++) \
    {                                  \
        cout << v[i] << " ";           \
    }                                  \
    cout << endl;
#define debug_vec2(v)                         \
    cout << #v << endl;                       \
    for (int i = 0; i < v.size(); i++)        \
    {                                         \
        for (int j = 0; j < v[i].size(); j++) \
        {                                     \
            cout << v[i][j] << " ";           \
        }                                     \
        cout << endl;                         \
    }

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

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

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<int, pair<int, int>>; // クラスカル法とかで、{cost, {from, to}}的に使う。
const ll INF = 1001001001001001001;
const double pi = acos(-1);

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i];
    }
    //左からi個取り出す
    //右からj個取り出す
    ll ans = 0;
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i + j > k || i + j > n)
            {
                break;
            }
            ll tmp = 0;
            priority_queue<int, vector<int>, greater<int>> que;
            for (int l = 0; l < i; l++)
            {
                tmp += v[l];
                que.push(v[l]);
            }
            for (int l = 0; l < j; l++)
            {
                tmp += v[n - 1 - l];
                que.push(v[n - 1 - l]);
            }
            for (int l = 0; l < k - i - j; l++)
            {
                if (que.empty())
                {
                    break;
                }
                int x = que.top();
                if (x >= 0)
                {
                    break;
                }
                que.pop();
                tmp -= x;
            }
            ans = max(ans, tmp);
        }
    }
    cout << ans << endl;

    return 0;
}


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

int main() {
  int H,W; cin>>H>>W;
  vector<vector<int>> A(H,vector<int>(W));
  rep(i,H){
    rep(j,W){
      cin>>A.at(i).at(j);
    }
  }
  rep(i,H) if(i%2==1) reverse(A.at(i).begin(),A.at(i).end());

  vector<int> S;
  vector<bool> F(H*W,false);
  rep(i,H){
    rep(j,W){
      S.push_back(A.at(i).at(j)%2);
    }
  }
  int count=0,temp=0;
  rep(i,H*W-1){
    if(temp==0&&S.at(i)==1)temp++;
    else if(temp==1&&S.at(i)==1)temp--;

    if(temp==1){
      F.at(i)=true;
      count++;
    }
  }
  cout<<count<<endl;

  rep(i,H*W){
    if(F.at(i)){
      int x=i%W,y=i/W,xd,yd;
      if(y%2==0){
        if(x==W-1) xd=x,yd=y+1;
        else xd=x+1,yd=y;
      }
      else{
        x=W-x-1;
        if(x==0) xd=x,yd=y+1;
        else xd=x-1,yd=y;
      }
      cout<<y+1<<' '<<x+1<<' '<<yd+1<<' '<<xd+1<<endl;
    }
  }
}

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

int H, W, A[509][509];
vector<tuple<int, int, int, int>> L;

int main() {
	cin >> H >> W;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) cin >> A[i][j];
	}
	for (int i = 1; i <= H; i++) {
		for (int j = W - 1; j >= 1; j--) {
			if (A[i][j + 1] % 2 == 0) continue;
			L.push_back(make_tuple(i, j + 1, i, j));
			A[i][j + 1]--;
			A[i][j]++;
		}
	}
	for (int i = H - 1; i >= 1; i--) {
		if (A[i + 1][1] % 2 == 0) continue;
		L.push_back(make_tuple(i + 1, 1, i, 1));
		A[i + 1][1]--;
		A[i][1]++;
	}

	cout << L.size() << endl;
	for (int i = 0; i < L.size(); i++) cout << get<0>(L[i]) << " " << get<1>(L[i]) << " " << get<2>(L[i]) << " " << get<3>(L[i]) << endl;
	return 0;
}


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

inline char dif(char sp) {
  if (sp == 'S') {
    return 'W';
  } else {
    return 'S';
  }
}

inline char next(char prevsp, char prevsay, char nowsp, char nowsay) {
  char res;
  if (nowsp == 'S') {
    if (nowsay == 'o') {
      res = prevsp;
    } else {
      res = dif(prevsp);
    }
  } else {
    if (nowsay == 'o') {
      res = dif(prevsp);
    } else {
      res = prevsp;
    }
  }
  return res;
}

string process(char ze, char one, string say, int n) {
  string ani(n + 2, '0');
  ani[0] = ze;
  ani[1] = one;
  for (int i = 2; i <= n + 1; i++) {
    ani[i] = next(ani[i - 2], say[i - 2], ani[i - 1], say[i - 1]);
  }
  return ani;
}

bool check(string &result, int n) {
  bool res;
  if (result[0] == result[n] && result[1] == result[n + 1]) {
    res = true;
  } else {
    res = false;
  }
  result.pop_back();
  result.pop_back();
  return res;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  s.push_back(s[0]);
  string result;
  char sp[2] = {'S', 'W'};
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++) {
      result = process(sp[i], sp[j], s, n);
      if (check(result, n)) {
        cout << result << endl;
        return 0;
      }
    }
  cout << -1 << endl;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(int i = m, i##_len = (n); i < i##_len; ++i)
#define reprev(i, n) for(ll i = (n-1LL); i >= 0; --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; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

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

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    string s;
    cin >> n >> s;
    bool sw[4][100001] = {}; // s:true w:false
    rep (i, 4) rep (j, 2) sw[i][j] = !!(i & (1 << j));
    rep (i, 4) {
        rep (j, n - 2) sw[i][j + 2] = sw[i][j] ^ sw[i][j + 1] ^ (s[(j + 1) % n] == 'o');
        if (sw[i][n - 2] ^ sw[i][n - 1] ^ (s[n - 1] == 'o') == sw[i][0]
            && sw[i][n - 1] ^ sw[i][0] ^ (s[0] == 'o') == sw[i][1]) {
                rep (k, n) cout << (sw[i][k] ? 'S' : 'W');
                cout << "\n";
                return 0;
        }
    }
    cout << "-1\n";
    return 0;
}


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

int main() {
    int ans = 0;
    int N, T;
    cin >> N >> T;
    vector<int> A(N), B(N);
    for(int i = 0; i < N; i++) {
        cin >> A.at(i) >> B.at(i);
    }
    vector<vector<int>> DP1(N, vector<int>(T, 0)), DP2(N, vector<int>(T, 0));
    for(int i = 0; i < N; i++) {
        for(int j = 1; j < T; j++) {
            if(i == 0) {
                if(0 <= j - A.at(i))
                    DP1.at(i).at(j) = B.at(i);
            } else {
                DP1.at(i).at(j) = max(DP1.at(i - 1).at(j), DP1.at(i).at(j - 1));
                if(0 <= j - A.at(i)) {
                    DP1.at(i).at(j) =
                        max(DP1.at(i).at(j),
                            DP1.at(i - 1).at(j - A.at(i)) + B.at(i));
                }
            }
        }
    }

    for(int i = N - 1; i >= 0; i--) {
        for(int j = 1; j < T; j++) {
            if(i == N - 1) {
                if(0 <= j - A.at(i))
                    DP2.at(i).at(j) = B.at(i);
            } else {
                DP2.at(i).at(j) = max(DP2.at(i + 1).at(j), DP2.at(i).at(j - 1));
                if(0 <= j - A.at(i)) {
                    DP2.at(i).at(j) =
                        max(DP2.at(i).at(j),
                            DP2.at(i + 1).at(j - A.at(i)) + B.at(i));
                }
            }
        }
    }

    ans = max(B.at(0) + DP2.at(1).at(T - 1),
              B.at(N - 1) + DP1.at(N - 2).at(T - 1));
    for(int i = 1; i < N - 1; i++) {
        for(int j = 0; j < T; j++) {
            ans = max(ans, B.at(i) + DP1.at(i - 1).at(j) +
                               DP2.at(i + 1).at(T - j - 1));
        }
    }

    cout << ans << endl;
}

#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int dp[2][6005];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, t;
	cin >> n >> t;
	vector<P> v(n);
	rep(i,n) cin >> v[i].first >> v[i].second;
	sort(v.begin(), v.end());
	rep(i,n){
		int a = v[i].first;
		int b = v[i].second;
		rep(j,6001){
			chmax(dp[(i+1)%2][j], dp[i%2][j]);
			if(j < t) chmax(dp[(i+1)%2][j+a], dp[i%2][j] + b);
		}
	}
	int ans = 0;
	rep(j,6001) chmax(ans, dp[n%2][j]);
	cout << ans << endl;
	return 0;
}


//https://onlinejudge.u-aizu.ac.jp/challenges/sources/JOI/Prelim/0566?year=2012

#include <bits/stdc++.h>

using namespace std;

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

    vector<int> pt(static_cast<unsigned long>(n + 1));//各チームの得点
    for (int i = 0; i < n * (n - 1) / 2; ++i) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;

        if (c > d) {
            pt[a] += 3;
        } else if (c < d) {
            pt[b] += 3;
        } else if (c == d) {
            pt[a]++;
            pt[b]++;
        }
    }

    vector<int> vt;//順位計算用
    for (int i = 1; i <= n; ++i) {
        vt.emplace_back(pt[i]);
    }
    vt.emplace_back(100000);
    sort(vt.begin(), vt.end());

    for (int i = 1; i <= n; ++i) {
        auto tmp = upper_bound(vt.begin(), vt.end(), pt[i]);
        cout << n + 1 - (tmp - vt.begin()) << endl;
    }
    return 0;
}

/*
 *
 */

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

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

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

struct koji {
    int t, x;
    int flag;

    bool operator<(const koji& other) const {
        if (t < other.t) return true;
        else return false;
    }
};

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<int> S(N), T(N), X(N), D(Q);
    vector<koji> kojis(2*N);
    REP(i, N) {
        cin >> S[i] >> T[i] >> X[i];
        S[i] -= X[i];
        T[i] -= X[i];
        kojis[2*i].t = S[i];
        kojis[2*i+1].t = T[i];
        kojis[2*i].x = X[i];
        kojis[2*i+1].x = X[i];
        kojis[2*i].flag = 1;
        kojis[2*i+1].flag = -1;
    }
    sort(ALL(kojis));
    REP(i, Q) cin >> D[i];

    int k = 0;
    multiset<int> events;
    REP(i, Q) {

        while(k < kojis.size() && kojis[k].t <= D[i]) {
            if(kojis[k].flag == 1) events.insert(kojis[k].x);
            else events.erase(events.find(kojis[k].x));
            k++;
        }

        if(events.empty()) {
            cout << -1 << endl;
        } else {
            cout << *events.begin() << endl;
        }
    }

    return 0;
}


#include<iostream>
using namespace std ;

int table[10010] = {} ;

void f(){
	table[0] = 1 ;
	table[1] = 1 ;
	for( int i=2 ; i<=10000 ; i++ ){
		if( !table[i] ){
			for( int j=2 ; i*j<=10000 ; j++ ) table[i*j] = 1 ;
		}
	}
}

main(){
	f() ;

	int n ;
	while( cin >> n , n ){
		int p = 0 ;
		for( int i=5 ; i<=n ; i++ ){
			if( !table[i] && !table[i-2] ) p = i ;
		}

		cout << p-2 << " " << p << endl ;
	}
}

/* All men have limits. They learn what they are and learn
   not to exceed them, I ignore mine. -Batman           */

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

int main(){

	int i,j;
	long long sum;
	int n;
	while(scanf("%d", &n) != EOF){
		i = 0;
		sum = 0;
		while( i != 600){
			sum += n*i*i;
			i += n;
		}
		printf("%lld\n", sum);
	}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod ll(1e9+7)
ll exp(ll a,ll b)
{
  if(b==0)
    return 1;
  if(b%2==0)
    return exp(a*a%mod,b/2);
  return a*exp(a*a%mod,(b-1)/2)%mod;
}
int main()
{
  #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
  #endif
   int n,k;
   cin>>n>>k;
   vector<int>arr(n+1);
   for(int i=1; i<=n; i++){
    cin>>arr[i];
   }
   int mx=0;
   for(int i=0; i<=k; i++){
    for(int j=0; j<=(k-i); j++){
      if(i+j>=n){
        vector<int>ans;
        int sum=0;
        for(int x=1; x<=n; x++){
          sum+=arr[x];
          ans.push_back(arr[x]);
        }
        sort(ans.begin(),ans.end());
        int num=min(n,k-n);
        for(int x=0; x<num; x++){
          if(ans[x]>=0)
            break;
          sum-=ans[x];
        }
        mx=max(sum,mx);
      }
      else{
        vector<int>ans;
        int sum=0;
        for(int x=1; x<=i; x++){
          ans.push_back(arr[x]);
          sum+=arr[x];
        }
        for(int x=n; x>=(n-j+1); x--){
          ans.push_back(arr[x]);
          sum+=arr[x];
        }
        int num=min(i+j,k-(i+j));
        sort(ans.begin(),ans.end());
        for(int x=0; x<num; x++){
          if(ans[x]>=0)
            break;
          sum-=ans[x];
        }
        mx=max(sum,mx);
      }
    }
   }
   cout<<mx;
   return 0;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;
ll m;
template <typename T>
T zero_sum_ranges(vector<T> A) {
    T n = A.size();
    T sum[n + 1] = {};
    sum[1] = A[0];
    map<T, T> mp;
    for(size_t i = 0; i < n; i++) {
        sum[i + 1] = (sum[i] + A[i]) % m;
    }
    T ret = 0;
    for(size_t i = 0; i <= n; i++) {
        if(mp.count(sum[i])) ret += mp[sum[i]];
        mp[sum[i]]++;
    }
    cout << ret << endl;
}

int main() {
    ll n, i, j, k;
    cin >> n >> m;
    vector<ll> v(n);
    for(auto &x:v)cin >> x;
    zero_sum_ranges(v);
    return 0;
}


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

int main(){
    string s;
    cin>>s;
    int n=s.size();
    ll ans=1;
    map<char,int>mp;
    rep(i,n){
        mp[s[i]]++;
        ans+=i+1-mp[s[i]];
    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long tint;
typedef long double ld;

#define forsn(i, s, n) for(int i=s;i<int(n);i++)
#define forn(i, n) forsn(i, 0, n)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define NACHO ios::sync_with_stdio(0); cin.tie(NULL);
#define dbg cout << "hu" << endl;

const int INF = 50000001;
const int MOD = 1000000007;

//impossible pairs: (a^3, 1) (a^2, a^1)

int main(){
	string s; cin >> s;
	int n = s.size();
	tint quan = tint(n)*tint(n-1)/2;
	vector<int> cont (26, 0);
	forn(i, n){
		cont[s[i]-'a']++;
	}
	tint ret = 0;
	forn(i, 26){
		if(cont[i]) ret+=(tint(cont[i])*tint(cont[i]-1))/2;
	}
	cout << quan-ret+1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define fir first
#define sec second
#define mod 998244353
#define int long long
#define INF (int)(1e18)
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
typedef pair<int,int> pii;
int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
int mul(int x,int y){return 1LL*x*y%mod;}
#define N 5005
int f[N][N];
struct Node
{
	int x,y;
};
Node a[N];
bool cmp(Node x,Node y)
{
	return x.x+x.y<y.x+y.y;
}
signed main()
{
#ifdef __LOCAL__
	freopen("in.txt","r",stdin);
#endif
	int n=read();
	for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
	sort(a+1,a+n+1,cmp);
	for(int i=0;i<=n;i++)
	{
		for(int j=1;j<=n;j++) f[i][j]=INF;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		{
			f[i][j]=f[i-1][j];
			if(f[i-1][j-1]<=a[i].x)
			{
				f[i][j]=min(f[i][j],f[i-1][j-1]+a[i].y);
			}
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		if(f[n][i]<INF) ans=i;
	}
	cout<<ans<<endl;
#ifdef __LOCAL__
	cout<<"Time Used : "<<clock()<<endl;
#endif
	return 0;
}

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
/* #include <regex> */

using namespace std;

/* g++ -g -std=c++11 */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

const LL inf = 1e16;
VI H,P, sorted;

bool cmp(const int i, const int j)
{ return H[i]+P[i] < H[j]+P[j]; }

int main()
{
#ifdef foo_
    freopen("foo", "rt", stdin);
#endif
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n) {
        H = P = sorted = VI(n);
        FOR(i,0,n) {
            cin >> H[i] >> P[i];
            sorted[i] = i;
        }
        sort(ALL(sorted),cmp);
        vector<LL> dp = vector<LL>(n+1,inf);
        dp[0] = 0; int ans = 0;
        FOR(i,0,n) {
            const int j = sorted[i];
            FORR(x,n-1,-1) if (dp[x] != inf && dp[x] <= H[j]) {
                dp[x+1] = min(dp[x+1], dp[x]+P[j]);
                ans = max(ans, x+1);
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 105;

int n, m, ex, ey;
char s[N][N], a[N][N];
short ans, dp[N][N][N][N], sum[N][N];

int Sum(int U, int L, int D, int R) {
	if (U > D) swap(U, D);
	if (L > R) swap(L, R);
	return sum[D][R] + sum[U - 1][L - 1] - sum[D][L - 1] - sum[U - 1][R];
}

void Upd(short &x, short y) {
	x = max(x, y);
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		scanf("%s", s[i] + 1);
		for (int j = 1; j <= m; ++j) {
			if (s[i][j] == 'E') {
				ex = i;
				ey = j;
			}
			if (s[i][j] == 'o') sum[i][j] = 1;
			sum[i][j] -= sum[i - 1][j - 1];
			sum[i][j] += sum[i][j - 1] + sum[i - 1][j];
		}
	}
	for (short a = ex; a >= 1; --a) {
		for (short c = ey; c >= 1; --c) {
			for (short b = ex; b <= n; ++b) {
				for (short d = ey; d <= m; ++d) {
					short U = b - ex + 1, D = n - ex + a;
					short L = d - ey + 1, R = m - ey + c;
					short now = dp[a][b][c][d];
					Upd(ans, now);
					if (a > U)
						Upd(dp[a - 1][b][c][d], now + Sum(a - 1, max(L, c), a - 1, min(R, d)));
					if (b < D)
						Upd(dp[a][b + 1][c][d], now + Sum(b + 1, max(L, c), b + 1, min(R, d)));
					if (c > L)
						Upd(dp[a][b][c - 1][d], now + Sum(max(U, a), c - 1, min(D, b), c - 1));
					if (d < R)
						Upd(dp[a][b][c][d + 1], now + Sum(max(U, a), d + 1, min(D, b), d + 1));
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int n, m;
int x, y;

int a[110][110];

char buf[110];
void in(){
	read(n, m);
	for(int i=1; i<=n; ++i){
		scanf("%s", buf+1);
		for(int j=1; j<=m; ++j){
			a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
			if(buf[j] == 'o'){
				++a[i][j];
			} else if(buf[j] == 'E'){
				x=i; y=j;
			}
		}
	}
}

int dp[102][102][102][102];

int rect(int L1, int R1, int D1, int U1, int L2, int R2, int D2, int U2){
	int L=max(L1, L2);
	int R=min(R1, R2);
	int D=max(D1, D2);
	int U=min(U1, U2);
	if(L>R || D>U) return 0;
	if(0) printf("%d %d %d %d\n", L, R, D, U);
	return a[R][U]-a[L-1][U]-a[R][D-1]+a[L-1][D-1];
}

int main()
{
	in();
	for(int w=1; w<=n; ++w){
		for(int h=1; h<=m; ++h){
			for(int l=1; l+w-1<=n; ++l){
				int r=l+w-1;
				if(x<l || r<x) continue;
				for(int d=1; d+h-1<=m; ++d){
					int u=d+h-1;
					if(y<d || u<y) continue;
					int me=dp[l][r][d][u];
					auto f = [&](int A, int B, int C, int D){
						int I=l+A;
						int J=r+B;
						int K=d+C;
						int L=u+D;
						int nv = rect(1+(r-x), n-(x-l), 1+(u-y), m-(y-d),
							(A||B)?(A?I:J):(I),
							(A||B)?(A?I:J):(J),
							(C||D)?(C?K:L):(K),
							(C||D)?(C?K:L):(L)
						);
						int& T=dp[I][J][K][L];
						if(T < me+nv){
							if(0) printf("upd from %d-%d, %d-%d to df %d %d %d %d : %d+%d = %d\n",
								l,r,d,u, A,B,C,D, me, nv, me+nv);
						}
						T=max(T, me+nv);
					};
					if(1 < l) f(-1, 0, 0, 0);
					if(r < n) f(0, 1, 0, 0);
					if(1 < d) f(0, 0, -1, 0);
					if(u < m) f(0, 0, 0, 1);
				}
			}
		}
	}
	printf("%d\n", dp[1][n][1][m]);
	return 0;
}


#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int maxn = 3e5 + 11;
const int mod=998244353;

double eps = 1e-9;

unordered_map<int,int>mpp2[1000],mpp5[1000];
int mx;
LL cal2(int a,int b){
    LL res = 0;
    fo(i,0,a) {
        for(auto to:mpp2[i]){
            if(i+b+to.first>=0) res+=to.second;
        }
    }
    fo(i,a+1,mx){
        for(auto to:mpp2[i]){
            if(a+b+to.first>=0) res+=to.second;
        }
    }
    return res;
}
LL cal5(int a,int b){
    LL res = 0;
    fo(i,0,a) {
        for(auto to:mpp5[i]){
            if(i+b+to.first>=0) res+=to.second;
        }
    }
    fo(i,a+1,mx){
        for(auto to:mpp5[i]){
            if(a+b+to.first>=0) res+=to.second;
        }
    }
    return res;
}

char s[maxn];
int main(){
//    op;
    int n;
    read(n);
    LL ans = 0;
    fo(i,1,n){
        scanf("%s",s);

        int len = strlen(s);
        LL nw = 0;
        int d=0;

        fo(i,0,len-1){
            if(s[i]=='.') d = i-len+1;
            else{
                nw = nw*10+s[i]-'0';
            }
        }
        int n2=0,n5=0;
        while(nw%2==0) n2++,nw/=2;
        while(nw%5==0) n5++,nw/=5;

        int mi = min(n2,n5);
        n2-=mi;n5-=mi;
        mx=max(mx,n2);
        mx=max(mx,n5);
        d+=mi;

        if(n2){
            ans+=cal5(n2,d);
        }
        else{
            ans+=cal2(n5,d);
        }
        mpp2[n2][d]++;
        mpp5[n5][d]++;
    }
    printf("%lld\n",ans);
    return 0;
}

#include <iostream>
using namespace std;
bool board[8][8];
char grid[8][8];
int upperlim = (1 << 8) - 1;

void solve(int row, int ld, int rd, int d)
{
    // cout<<d<<endl;
    if (d == 8)
    {
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (board[i][j] && grid[i][j]!='Q') return;
            }
        }

        for (int i = 0; i < 8; ++i)
         {
             for (int j = 0; j < 8; ++j)
             {
                 cout<<grid[i][j];
             }
             cout<<endl;
         }
         return;
    }

    if (row != upperlim)
    {
        int pos = upperlim & ~(row | ld | rd);
        while (pos)
        {
            // cout<<"pos:"<<pos<<endl;
            int p = pos & (~pos + 1);
            // cout<<"p:"<<p<<endl;
            int num = p;
            int col = 0;
            while(num != 1) {
                num = num>>1;
                col++;
            }
            grid[d][col] = 'Q';
            pos = pos - p;
            solve(row + p, (ld + p) << 1, (rd + p) >> 1, d+1);
            grid[d][col] = '.';
        }
    }

    return;
}

int main()
{
    int k;
    cin>>k;
    for (int i = 0; i < 8; ++i)
    {
        for (int j = 0; j < 8; ++j)
        {
            board[i][j] = false;
            grid[i][j] = '.';
        }
    }
    for (int i = 0; i < k; i++)
    {
        int r,c;
        cin>>r>>c;
        board[r][c] = true;
    }

    solve(0, 0 , 0, 0);

    return 0;
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 610;
int used[SIZE][SIZE];
int N;
int res[SIZE][SIZE],D;
int dx[4]={1,1,-1,-1};
int dy[4]={1,-1,1,-1};
int v;
void dfs(int x,int y,int b){
    if(b)res[x][y]|=v;
    used[x][y]=1;
    for(int k=0;k*k<=D;k++){
        int tmp=sqrt(D-k*k);
        if(tmp*tmp+k*k==D){
            REP(j,4){
                int nx=x+dx[j]*k;
                int ny=y+dy[j]*tmp;
                if(nx<0||ny<0||nx>=2*N||ny>=2*N) continue;
                if(used[nx][ny])continue;
                dfs(nx,ny,1^b);
            }
        }
    }
}
void fil(int _D,int _v){
    D=_D;
    v=_v;
    MS0(used);
    REP(i,2*N){
        REP(j,2*N){
            if(!used[i][j]){
                dfs(i,j,0);
            }
        }
    }
}
VPII ker[4];
int main(){
    int D1,D2;
    R(N,D1,D2);
    fil(D1,1);
    fil(D2,2);
    REP(i,2*N)REP(j,2*N){
        ker[res[i][j]].PB({i,j});
    }
    REP(i,4){
        if(SZ(ker[i])>=N*N){
            REP(j,N*N){
                W(ker[i][j]);
            }
            return 0;
        }
    }
    return 0;
}


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

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

int main(){
	while(1){
		int n, m;
		cin>>n>>m;
		if(n==0) break;
		int a[n];
		REP(i, n) cin>>a[i];
		sort(a, a+n, greater<int>());
		int sum = 0;
		REP(i, n) sum+=a[i];
		for(int i=m-1; i<n; i+=m){
			sum-=a[i];
		}
		printf("%d\n", sum);
	}
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

vector<char> cmap;
char getrev(char c,int kagi){
	int rst;
	if(c>='a') rst = c -'a' ;
	else rst = c - 'A'+ ('Z'-'A'+1);
	if(rst-kagi<0) return cmap[rst-kagi+cmap.size()];
	else return cmap[rst-kagi];
}
int main(){
	for(int i='a';i<'z'+1;i++) cmap.pb(i);
	for(int i='A';i<'Z'+1;i++) cmap.pb(i);

	while(1){
		int n;
		cin >> n;
		if(n==0) break;
		int klist[100];
		REP(i,n){
			cin >> klist[i];
		}
		string ango;
		cin >> ango;
		int nowkagi = 0;
		REP(i,ango.size()){
			cout << getrev(ango[i],klist[nowkagi]);
			if(nowkagi==n-1) nowkagi = 0;
			else nowkagi++;
		}
		cout << endl;
	}
}


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

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

  map<int, int> m;
  int x;
  rep (i, n){
    cin >> x;
    if (m[x] == 0) m[x] = 1;
    else m[x] = 0;
  }
  int count = 0;
  for (P p : m){
    if (p.second == 1) ++count;
  }
  cout << count << endl;
  return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	rep(i, n) cin >> a[i];
	sort(a.begin(), a.end());
	int count = 1;
	int out = 0;
	rep(i, n - 1){
		if(a[i] == a[i + 1]) count++;
		else{
			if(count % 2 == 1) out++;
			count = 1;
		}
	}
	if(count % 2 == 1) out++;
	cout << out << endl;
}


#include <bits/stdc++.h>

#define F first
#define S second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define in(x) insert(x)
#define forr(i, a, b) for(int i = a;i <= b;i++)
#define sz(s) (int)(s.size())
#define all(x) x.begin(), x.end()
#define did_you_calis_enough ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define int long long

using namespace std;

const int INF =  4294967295;
const int  N = 2e6 + 667;
const int mod = 1e9 + 7;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double PI = acos(-1.0);

main() {
	did_you_calis_enough
	int d, t, s;
	cin >> d >> t >> s;
	cout << ((t * s >= d) ? "Yes" : "No");
}

//Coded by LoliThief

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <queue>
#define rep(i,n) for (int i=0;i<(n);i++)

using namespace std;

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

	if (a != b && b != c && c != a) {
		cout << "No" << endl;
	}
	else if (a == b && b == c) {
		cout << "No" << endl;
	}
	else cout << "Yes" << endl;

	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI=acos(-1);

template<typename T>
istream& operator>> (istream& is, vector<T> &vec){
    for(T& x: vec) is >> x;
    return is;
}

int main(){
    ll n, k; cin >> n >> k;
    vector<ll> x(n); cin >> x;
    ll inf = 1000000000, ans = inf;
    rep(i, n-k+1){
        ll a = x[i], b = x[i+k-1];
        if(a < 0 && b < 0) ans = min(ans, abs(a));
        else if(a > 0 && b > 0) ans = min(ans, b);
        else{
            if(abs(a) < abs(b)) ans = min(ans, abs(a)*2 + abs(b));
            else ans = min(ans, abs(a) + abs(b)*2);
        }
    }
    cout << ans << endl;

    return 0;
}

#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<cfloat>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(int i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<=n;i++)
#define RIP(i,a,b) for(int i=a;i<b;i++)
#define all(V) V.begin(),V.end()
#define MOD 1000000007
#define mod 998244353
#define inf MOD
#define sec setprecision
#define Pair pair<ll,ll>
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
using namespace std;
using ll = long long;
using ld = long double;
ll gcd(ll a, ll b)
{
	if (a % b == 0)	return(b);
	else return(gcd(b, a % b));
}
ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
ll pow(ll x, ll n) {
	x %= mod;
	if (n == 0)
		return 1;
	if (n % 2 == 0)
		return (pow((x * x) % mod, n / 2) % mod);
	else
		return (x * pow(x, n - 1)) % mod;
}
void solve() {

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


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

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

#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);

// map of result of factorization
void factorization(ll n, map<ll, ll> &a) {
    while((n%2) == 0) {
        n /= 2;
        if(a.count(2)) a.at(2)++;
        else a[2] = 1;
    }
    for(ll i = 3; i*i <= n; i+=2) {
        while((n%i) == 0) {
            n /= i;
            if(a.count(i)) a.at(i)++;
            else a[i] = 1;
        }
    }
    if(n != 1) a[n] = 1;
}

void factorial_map(vector<ll> &n, vector<ll> &ninv);

int main() {
    ll n, m;
    cin >> n >> m;
    map<ll, ll> a;
    vector<ll> nfac(101010), ninv(101010);
    factorial_map(nfac, ninv);
    factorization(m, a);
    ll ans = 1;

    for(auto p: a) {
        auto v = p.second;
        ll comb = mtime(nfac[v+n-1], ninv[v], ninv[n-1]);
        ans = mtime(ans, comb, 1);
    }

    cout << ans << endl;

    return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) {
    if(n == 0) return 1;

    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        ret %= MOD;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count))%MOD;
}

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

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}

void factorial_map(vector<ll> &n, vector<ll> &ninv) {
    n[0] = 1;
    for(ll i = 1; i < n.size(); i++) n[i] = (n[i-1] * i) % MOD;
    ninv[n.size()-1] = inv(n[n.size()-1]);
    for(ll i = n.size() - 2; i > -1; i--) ninv[i] = (ninv[i+1] * (i+1)) % MOD;
}

#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

bool dfs(const vector<vector<int64_t>>& adjs, vector<int64_t>& colors,
         int64_t v, int64_t c) {
    colors[v] = c;

    for (auto u : adjs[v]) {
        if (colors[u] >= 0) {
            if (colors[u] + c != 1) {
                return false;
            }

            continue;
        }

        if (!dfs(adjs, colors, u, (c + 1) % 2)) {
            return false;
        }
    }

    return true;
}

int main() {
    int64_t n, m;
    cin >> n >> m;

    vector<vector<int64_t>> adjs(n);
    for (auto _ : irange(0L, m)) {
        int64_t a, b;
        cin >> a >> b;
        --a;
        --b;
        adjs[a].push_back(b);
        adjs[b].push_back(a);
    }

    vector<int64_t> colors(n, -1L);
    if (dfs(adjs, colors, 0L, 0L)) {
        // two-party
        array<int64_t, 2> nn{};
        nn[0] = count(colors.begin(), colors.end(), 0L);
        nn[1] = n - nn[0];

        // cerr << nn[0] << " " << nn[1] << endl;

        int64_t ans = 0;
        for (auto i : irange(0L, n)) {
            ans += nn[(colors[i] + 1) % 2] - adjs[i].size();
        }
        ans /= 2;

        cout << ans << endl;
    } else {
        cout << n * (n - 1) / 2 - m << endl;
    }
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3f
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define N 305

using namespace std;
typedef pair<int,int> Pair;
const int mod = 1e9 + 7;
inline int add(int x, int y) { return (x += y) - (x >= mod ? mod : 0); }
inline void inc(int &x, int y) { (x += y) -= (x >= mod ? mod : 0); }
inline int mul(int x, int y) { return 1ll * x * y % mod; }

template <class T> inline T	input() {
	int x; char ch; while (!isdigit(ch = getchar()));
	for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48));
	return x;
}

template <class T> inline void chkmin(T &x, T &y) { x = x < y ? x : y; }
template <class T> inline void chkmax(T &x, T &y) { x = x > y ? x : y; }

struct {
	inline operator int () { return input<int>(); }
	inline operator long long () { return input<long long>(); }
	template <class T> inline void operator () (T &x) { x = *this; }
	template<class T, class ...A> inline void operator () (T &x, A &...a)
	{ x = *this; this -> operator ()(a...); }
} read;

int n, m, ans = inf;
int a[N][N];
int cnt[N];
bool vis[N];

void calc() {
	memset(cnt, 0, sizeof cnt);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			if (vis[a[i][j]]) {
				++cnt[a[i][j]];
				break;
			}
}

int main() {
	read(n, m);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			read(a[i][j]);

	for (int i = 1; i <= m; ++i) vis[i] = true;
	calc();

	for (int i = 1; i <= m; ++i) {
		int pos = max_element(cnt + 1, cnt + m + 1) - cnt;
		ans = min(ans, cnt[pos]);
		vis[pos] = false;
		calc();
	}

	printf("%d\n", ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int main(){
    int N, M;
    cin >> N >> M;
    int A[N][M];
    rep(i, N) rep(j, M) {cin >> A[i][j]; A[i][j]--;}
    //(l,r]
    int l = 0, r = N;
    while(r-l > 1){
        int n = (l+r)/2;
        vector<int> cnt(M, 0), now(N, 0);
        vector<bool> used(M, false);
        bool able = true;
        while(true){
            rep(i, N){
                while(now[i] < M && used[A[i][now[i]]]) now[i]++;
                if(now[i] == M) able = false;
                else cnt[A[i][now[i]]]++;
            }
            bool update = false;
            rep(i, M){
                if(cnt[i] > n) update = true, used[i] = true;
            }
            fill(all(cnt), 0);
            if(!update) break;
        }
        if(able) r = n;
        else l = n;
    }
    cout << r << endl;
}


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

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

int dz[4] = {1,-1,-1,1};
int dw[4] = {1,1,-1,-1};
int main() {
    int n;cin >> n;
    vector<pair<int,int> > x(n);
    rep(i,n) cin >> x[i].first >> x[i].second;
    vector<int> a(8);
    rep(i,8) a[i]=i;
    do{
	bool board[8][8] = {false};
	for(int i=0;i<8;i++){
	    board[i][a[i]] = true;
	}
	bool ok = true;
	rep(i,n){
	   if(board[x[i].first][x[i].second] == false) ok = false;
	}
	if(ok == false) continue;
	for(int i=0;i<8;i++){
	    for(int j=0;j<4;j++){
		int t = 1;
		while(1){
		    int s = i+dz[j]*t;
		    int r = a[i]+dw[j]*t;
		    if(s < 0 || 8 <= s || r < 0 || 8 <= r)break;
		    if(board[s][r]==true) ok = false;
		    t++;
		}
	    }
	}
	if(ok){
	    rep(i,8){
		rep(j,8){
		    if(board[i][j]) cout <<"Q";
		    else cout <<".";
		}
		cout << endl;
	    }
	    break;
	}
    }while(next_permutation(a.begin(),a.end()));
    return 0;
}

#include <iostream>
#define SIZE 8

void threaten(int board[][SIZE], int x, int y)
{
	for (int i=0; i<SIZE; i++) {
		board[i][y]++; board[x][i]++;
	}
	for (int i=-x; i<SIZE-x; i++) {
		if (0 <= y+i && y+i < SIZE) board[x+i][y+i]++;
		if (0 <= y-i && y-i < SIZE) board[x+i][y-i]++;
	}
	board[x][y]+=100;
}

void unthreaten(int board[][SIZE], int x, int y)
{
	for (int i=0; i<SIZE; i++) {
		board[i][y]--; board[x][i]--;
	}
	for (int i=-x; i<SIZE-x; i++) {
		if (0 <= y+i && y+i < SIZE) board[x+i][y+i]--;
		if (0 <= y-i && y-i < SIZE) board[x+i][y-i]--;
	}
	board[x][y]-=100;
}

bool solve(int board[][SIZE], int rest)
{
	if (rest==0) return true;

	for (int i=0; i<SIZE; i++) {
		for (int j=0; j<SIZE; j++) {
			if (board[i][j]>0) continue;
			threaten(board, i, j);
			if (solve(board, rest-1)) return true;
			unthreaten(board, i, j);
		}
	}
	return false;
}

int main()
{
	int board[SIZE][SIZE];
	for (int i=0; i<SIZE; i++) {
		for (int j=0; j<SIZE; j++) board[i][j] = 0;
	}

	int k; std::cin >> k;
	for (int i=0; i<k; i++) {
		int x, y;
		std::cin >> x >> y;
		threaten(board, x, y);
	}

	solve(board, SIZE-k);

	for (int i=0; i<SIZE; i++) {
		for (int j=0; j<SIZE; j++) {
			if (board[i][j] < 100) std::cout << ".";
			else std::cout << "Q";
		}
		std::cout << std::endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int w[10];
  int k[10];
  for(int i=0;i<10;i++){
    cin >>w[i];
  }
  for(int i=0;i<10;i++){
    cin >>k[i];
  }
  sort(w,w+10,greater<int>());
  sort(k,k+10,greater<int>());
  cout<<w[0]+w[1]+w[2]<<' '<<k[0]+k[1]+k[2]<<endl;
}

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string stro, str;
  while(getline(cin, stro)){
    for(int i = 0; i < 26; i++){
      str = stro;
      for(int j = 0; j < stro.length(); j++){
	if(str[j] >= 'a' && str[j] <= 'z')
	  if(str[j] + i > 'z')
	    str[j] += i - 26;
	  else
	    str[j] += i;
      }
      if(str.find("the") != string::npos || str.find("this") != string::npos || str.find("that") != string::npos)
	cout << str << endl;
    }
  }
}


#include<cstdio>
#include<iostream>
#include<cstring>
#define RG register
#define si short int
using namespace std;
const int N=101;
si n,m,x,y,ans,dp[N][N][N][N],sum[N][N];
char g[N][N];
template<typename I> inline void read(I &ot){
	I ch=getchar(), x=0, f=1;
	while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();	}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();	}
	ot=x*f;}
template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}
template<typename I>inline I mx(const I&a,const I&b){return a>b ? a : b;}
template<typename I>inline I mi(const I&a,const I&b){return a<b ? a : b;}
template<typename I>inline void swp(I&a,I&b){I t=a; a=b; b=t;}
inline void cmx(si &a,si b){if(b>a) a=b;}
inline si s(int a,int b,int c,int d){
	if(a>c) swp(a,c); if(b>d) swp(b,d);
	return sum[c][d]-sum[c][b-1]-sum[a-1][d]+sum[a-1][b-1];
}
int main()
{
//	freopen("Salvage Robots.in","r",stdin);
	read(n,m);
	for(RG int i=1;i<=n;i++)
	{
		scanf("%s",g[i]+1);
		for(RG int j=1;j<=m;j++)
		{
			if(g[i][j]=='o') sum[i][j]=1;
			else if(g[i][j]=='E') x=i, y=j;
			sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
		}
	}
	for(int i=x;i;i--)
	 for(int j=y;j;j--)
	  for(int k=x;k<=n;k++)
	   for(int l=y;l<=m;l++)
	   {
			ans=mx(ans,dp[i][j][k][l]);
			int li=l-y+1, ri=m-y+j, us=k-x+1, ds=n-x+i;
			if(i-1>=us) cmx(dp[i-1][j][k][l],dp[i][j][k][l]+s(i-1,mx(li,j),i-1,mi(ri,l)));
			if(j-1>=li) cmx(dp[i][j-1][k][l],dp[i][j][k][l]+s(mx(i,us),j-1,mi(k,ds),j-1));
			if(k+1<=ds) cmx(dp[i][j][k+1][l],dp[i][j][k][l]+s(k+1,mx(li,j),k+1,mi(ri,l)));
			if(l+1<=ri) cmx(dp[i][j][k][l+1],dp[i][j][k][l]+s(mx(i,us),l+1,mi(k,ds),l+1));
	   }
	cout<<ans<<endl;
	return 0;
}

#include <algorithm>
#include <cstdio>
#define upd(x, y) (x) = std::max(x, y)
char str[105];
int dp[105][105][105][105], row_pre[105][105], col_pre[105][105];
int main()
{
	// freopen("AGC004-E.in", "r", stdin);
	int n, m, x, y;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", str);
		for (int j = 0; j < m; j++)
		{
			row_pre[i][j] = (j ? row_pre[i][j - 1] : 0) + (str[j] == 'o');
			col_pre[j][i] = (i ? col_pre[j][i - 1] : 0) + (str[j] == 'o');
			if (str[j] == 'E')
			{
				x = i;
				y = j;
			}
		}
	}
	auto calc_row = [&] (int row, int l, int r)
	{
		int L = std::max(l, r - y), R = std::min(r, l + m - y - 1);
		return L <= R ? row_pre[row][R] - (L ? row_pre[row][L - 1] : 0) : 0;
	};
	auto calc_col = [&] (int col, int l, int r)
	{
		int L = std::max(l, r - x), R = std::min(r, l + n - x - 1);
		return L <= R ? col_pre[col][R] - (L ? col_pre[col][L - 1] : 0) : 0;
	};
	for (int i = 0; i <= x; i++)
	{
		for (int j = n - 1; j >= x; j--)
		{
			for (int k = 0; k <= y; k++)
			{
				for (int t = m - 1; t >= y; t--)
				{
					if (j - i < x)
						upd(dp[i][j][k][t], dp[i - 1][j][k][t] + calc_row(i - 1, k, t));
					if (j - i < n - x - 1)
						upd(dp[i][j][k][t], dp[i][j + 1][k][t] + calc_row(j + 1, k, t));
					if (t - k < y)
						upd(dp[i][j][k][t], dp[i][j][k - 1][t] + calc_col(k - 1, i, j));
					if (t - k < m - y - 1)
						upd(dp[i][j][k][t], dp[i][j][k][t + 1] + calc_col(t + 1, i, j));
				}
			}
		}
	}
	printf("%d\n", dp[x][x][y][y]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,N) for(int i = 0; i < (N); i++)
#define erep(i,N) for(int i = N - 1; i >= 0; i--)
const ll INF = 1000000000;
//const ll MOD = 1000000007;
const ld PI = (acos(-1));
using Graph = vector<vector<int>>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true;} return false;}
typedef pair<int, int> P;
double rad(double a) {return a * 180 / PI;}

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[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の根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

//dpTable
int dp[100050];

int main(){
    int N,M = 0;
    cin >> N;
    vector<int> a(200050,0);
    for (int i = 1; i <= N; i++) cin >> a.at(i);
    vector<int> acnt(200050,0);
    for (int i = N; i > 0; i--) {
        int cnt = 0;
        for (int j = i; j <= N; j += i) {
            cnt += acnt.at(j);
        }
        if (!(cnt % 2 == a.at(i))) {
            acnt.at(i) = 1;
            M++;
        }
    }
    cout << M << endl;
    for (int i = 1; i <= N; i++) {
        if (acnt.at(i) == 1) {
            cout << i << ' ';
        }
    }
    cout << endl;
    return 0;
}

#include<bits/stdc++.h>
#define rep(i,n)  for(ll i=0; i<n; i++)
#define vi vector<int>
#define all(x) (x).begin(),(x).end()
#define INF 1e9
using ll = long long;
using namespace std;
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 main(){
  int n; cin >> n;
  string s,t; cin >> s >> t;
  int ans = s.size()+t.size();
  for(int i = n; 0 <= i; i--)
  if(s.find(t.substr(0,i)) != string::npos){
    ans -= i;
    break;
  }
  cout << ans;
  cout << "\n";
  return 0;
}


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

int main() {
	long long N;
	cin >> N;
	long long total = 0;
	for (long long i = 1; i * i <= N; i++) {
		if (N % i == 0) {
			total = i;
		}
	}
	long long result = N / total;
	long long count = 0;
	while (result != 0) {
		result = result / 10;
		count++;
	}
	cout << count << endl;
}

#include <stdio.h>

int main(void){
  int i;
  scanf("%d", &i);
  i = i*i*i;
  printf("%d\n", i);
}


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

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
const int INF_N = 1e+9;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

//繰り返し二乗法
ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

//逆元(Eucledean algorithm)
ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

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

void solve() {
    string s; cin >> s;
    int n = s.size();
    int sc=0, res=0;
    rep(i, n){
        if(s[i] == 'S'){
            sc++;
        }else{
            if(sc > 0){
                sc--;
                res++;
            }
        }
    }
    cout << n-2*res << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //cout << fixed << setprecision(10);
  //init_f();
  //init();
  //int t; cin >> t; rep(i, t)solve();
  solve();
//   stop
    return 0;
}

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

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

  int n;
  cin >> n;

  int l = 1, r = n - 1;
  string ls, rs;

  cout << 1 << endl;
  cin >> ls;
  if (ls == "Vacant") return 0;

  cout << n - 1 << endl;
  cin >> rs;
  if (rs == "Vacant") return 0;

  if (ls != rs) {
    cout << 0 << endl;
    string s;
    cin >> s;
    return 0;
  }

  while (true) {
    int mid = (l + r) / 2;
    cout << mid << endl;
    string ms;
    cin >> ms;
    if (ms == "Vacant") return 0;
    if ((mid - l) % 2 == 0 && ms != ls) {
      r = mid;
      rs = ms;
    }
    else if ((mid - l) % 2 == 1 && ms == ls) {
      r = mid;
      rs = ms;
    }
    else if ((r - mid) % 2 == 0 && ms != rs) {
      l = mid;
      ls = ms;
    }
    else if ((r - mid) % 2 == 1 && ms == rs) {
      l = mid;
      ls = ms;
    }
  }
}


#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;
const int MOD = 1000000007;

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

int main() {
    int n;
    string s, t;
    cin >> n >> s >> t;
    int ans = s.size() + t.size();
    int tot = 0;
    for(int i = n - 1; i >= 0; i--) {
        bool check = true;
        for(int j = i; j < n; j++) {
            if(s[j] != t[j - i]) check = false;
        }
        if(check) tot = n - i;
    }
    cout << ans - tot << endl;

    return 0;
}

#include <bits/stdc++.h>

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

using namespace std;

int main() {
    string a, b, c;
    cin >> a >> b >> c;
    int ai = 0;
    int bi = 0;
    int ci = 0;
    char x = 'a';
    char ans = '\0';
    while (1) {
        switch (x) {
            case 'a':
                if (ai == a.size()) {
                    cout << 'A';
                    return 0;
                }
                x = a[ai];
                ai++;
                break;
            case 'b':
                if (bi == b.size()) {
                    cout << 'B';
                    return 0;
                }
                x = b[bi];
                bi++;
                break;
            case 'c':
                if (ci == c.size()) {
                    cout << 'C';
                    return 0;
                }
                x = c[ci];
                ci++;
                break;
        }
    }
}


#include <bits/stdc++.h>
#define ll  long long
#define ull unsigned long long
using namespace std;
#define  inf 1e6+18
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define mod 1000000007
#define  f1(i,n) for(i=1;i<=n;i++)
#define f0(i,n)  for(i=0;i<n;i++)
#define w(x)   while(x--)
const int maxn=500001;
#define  pq priority_queue
#define  ff first
#define  ss second
#define pb  push_back
#define lb lower_bound

ll k1=0;
bool comp(pair<ll,ll>a,pair<ll,ll>b)
{
	k1=max(k1,abs(a.ff-b.ff)+abs(a.ss-b.ss));
	if(a.ff>b.ff)
		return 1;

	if(a.ff==b.ff&&a.ss>b.ss)
		return 1;
	return 0;
}
int dx[]={1,-1};
int dy[]={1,-1};
int main()
{
	fastio;
	int t;
	ll i,j,k,n,m,l;
	cin>>n>>k>>l;
	if(k*l>=n)
		cout<<"Yes";
	else
		cout<<"No";

}
//Lordvishnu@almighty

#include<bits/stdc++.h>
using namespace std;
int main()
{
     ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int d,t,s;
cin>>d>>t>>s;
if(s*t>=d) cout <<"Yes\n";
else cout <<"No\n";
}


#include <iostream>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e18;
const ll MOD = 1000000007;
typedef pair<ll,ll> P;
const ll MAX = 100010;
constexpr ll nx[8] = {1,0,-1,0,-1,-1,1,1};
constexpr ll ny[8] = {0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    vector<ll> a(n);
    cins(all(a));
    bool ok = 1;
    if (a[0] != 0) ok = 0;
    ll now = 0,cnt = 0;
    rep(i,1,n){
        if (a[i] == 0 || a[i] == 1){
            now = a[i];
            continue;
        }
        if (a[i] > now+1) ok = 0;
        now = a[i];
    }
    ll stock = 0;
    now = 0;
    if (ok){
        rep(i,n){
            if (a[i] < now){
                cnt += stock;
                stock = a[i];
            }
            else if (a[i] == now){
                cnt += a[i];
            }
            else{
                stock = a[i];
            }
            now = a[i];
        }
        PRINT(cnt+now);
    }
    else PRINT(-1);
}

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

int main(){
  string s;
  cin >> s;
  int ans =0;
  if(s == "RRR"){ans = 3;}
  if(s == "RRS"){ans = 2;}
  if(s == "RSR"){ans = 1;}
  if(s == "SRR"){ans = 2;}
  if(s == "RSS"){ans = 1;}
  if(s == "SRS"){ans = 1;}
  if(s == "SSR"){ans = 1;}
  if(s == "SSS"){ans = 0;}
  cout << ans << endl;
}


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

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int N=1e5+5;

int n,m,a[N],k,p,b[N];

signed main()
{
	n=read();
	m=read();
	k=read();
	p=read();
	for ( int i=1;i<=n;i++ )
		a[i]=read();
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	for ( int i=1;i<=n;i++ ) b[i]=b[i-1]+a[i];
	int ans=p;
	int i;
	for ( i=p+1;i<=n;i++ )
	{
		if(a[i]+m<a[p]) continue;
		int j=i;
//		while(a[j]==a[i]&&j<=n) j++;
//		j--;
		int sum=(n-i+1)+(p-1);
		if(sum>=k) ans+=(j-i+1);
		else
		{
			int alb=b[i-1]-b[p-1];
			int need=(k-((n-i+1)+(p-1)))*m;
			if(((a[i]+m)*(i-p)-alb)>=need) ans+=(j-i+1);
		}
		i=j;
	}
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>
#include<tuple>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
	int N, M; cin >> N >> M;
	vector<pair<int, bool>> num(10);
	for (int a = 1; a <= 9; a++) {
		int p = 0;
		if (a == 1)p = 2;
		if (a == 7)p = 3;
		if (a == 4)p = 4;
		if (a == 2 || a == 3 || a == 5)p = 5;
		if (a == 6 || a == 9)p = 6;
		if (a == 8)p = 7;
		num[a].first = p;
	}

	for (int i = 0; i < M; i++) {
		int a; cin >> a;
		num[a].second = true;
	}

	vector<int> dp(N + 1, -1000);
	dp[0] = 0;
	int po = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 1; j <= 9; j++) {
			if (num[j].second && i + num[j].first <= N) {
				dp[i + num[j].first] = max(dp[i + num[j].first], dp[i] + 1);
				if (dp[i + num[j].first] > 0) po = max(po, i + num[j].first);
			}
		}
	}

	//cout << "after dp" << endl;
	vector<int> ans;
	while (po != 0) {
		for (int i = 9; i >= 1; i--) {
			if (po - num[i].first >= 0 && num[i].second && dp[po] - 1 == dp[po - num[i].first]) {
				ans.push_back(i);
				po -= num[i].first;
				break;
			}
		}
		//cout << "po=" << po << endl;
	}

	//reverse(ans.begin(), ans.end());
	for (int i = 0; i < ans.size() ; i++) {
		cout << ans[i];
	}
	cout << endl;
}