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

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;

inline int cmp(double x, double y = 0)
{
  if(fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b)
{
  ll ans = 1;
  while(b)
  {
    if(b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 85;
ll fat[maxn], inv[maxn];

ll C(int n, int k)
{
  if(n < k)
    return 0;
  if(n < 0 || k < 0)
    return 0;
  return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}

void initFactorials()
{
  fat[0] = 1;
  inv[0] = 1;
  for(int i = 1; i < maxn; i++)
  {
    fat[i] = (i * fat[i-1]) % mod;
    inv[i] = exp(fat[i], mod - 2);
  }
}

int dp[maxn][maxn][2 * maxn * maxn];

ll a[maxn][maxn];

ll b[maxn][maxn];

int n,m;

int main()
{
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for(int i = 0 ; i < n; i++)
    for(int j = 0; j < m; j++)
      cin >> a[i][j];

  for(int i = 0 ; i < n; i++)
    for(int j = 0; j < m; j++)
      cin >> b[i][j];

  dp[0][0][abs(a[0][0] - b[0][0])] = true;
  for(int i = 0; i < n; i++)
  {
    for(int j = 0 ; j < m; j++)
    {
      for(int k = 0; k < (n + m) * maxn; k++)
      {
        if(i > 0)
        {
          dp[i][j][k] = dp[i - 1][j][k + abs(a[i][j] - b[i][j])] |
                        dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))];
        }
        if(j > 0)
        {
          dp[i][j][k] = dp[i][j][k] |
                        dp[i][j - 1][k + abs(a[i][j] - b[i][j])] |
                        dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))];
        }
      }
    }
  }

  for(int i = 0; i < (n + m) * maxn; i++)
  {
    if(dp[n - 1][m - 1][i] == 1)
    {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

#include <iostream>
using namespace std;

const long long MOD = 1000000007;

int main() {
    long long n, m, result = 0;
    cin >> n >> m;
    long long x[n], y[m];
    for (long long i = 0; i < n; i++) {
        cin >> x[i];
    }
    for (long long i = 0; i < m; i++) {
        cin >> y[i];
    }
    long long left = 0, right = 0;
    for (long long i = 1, prefix = y[0]; i < m; i++) {
        left = (left + MOD - prefix + i * y[i]) % MOD;
        prefix = (prefix + y[i]) % MOD;
    }
    for (long long i = 1, prefix = x[0]; i < n; i++) {
        right = (right + MOD - prefix + i * x[i]) % MOD;
        prefix = (prefix + x[i]) % MOD;
    }
    cout << (left * right) % MOD;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
template <typename TYPE> inline void chkmax(TYPE &x,TYPE y){x<y?x=y:0;}
template <typename TYPE> inline void chkmin(TYPE &x,TYPE y){y<x?x=y:0;}
template <typename TYPE> void readint(TYPE &x)
{
    x=0;int f=1;char c;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    x*=f;
}
const int MAXN=605;

int n,m,dx[MAXN*4],dy[MAXN*4];
void work(int d)
{
	m=0;
	for(int i=-n;i<=n;++i)
		for(int j=-n;j<=n;++j)
			if(i*i+j*j==d)dx[++m]=i,dy[m]=j;
}
int c1[MAXN][MAXN],c2[MAXN][MAXN];
void dfs(int x,int y,int c[MAXN][MAXN])
{
	int tx,ty;
	for(int i=1;i<=m;++i)
	{
		tx=x+dx[i],ty=y+dy[i];
		if(tx<0 || ty<0 || tx>=n || ty>=n || c[tx][ty])continue;
		c[tx][ty]=3-c[x][y];
		dfs(tx,ty,c);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("code.in","r",stdin);
//	freopen("code.out","w",stdout);
	#endif
	int d;
	readint(n);n*=2;
	readint(d);work(d);
	for(int x=0;x<n;++x)
		for(int y=0;y<n;++y)
		{
			if(c1[x][y])continue;
			c1[x][y]=1;
			dfs(x,y,c1);
		}
	readint(d);work(d);
	for(int x=0;x<n;++x)
		for(int y=0;y<n;++y)
		{
			if(c2[x][y])continue;
			c2[x][y]=1;
			dfs(x,y,c2);
		}
	for(int i=1;i<=2;++i)
		for(int j=1;j<=2;++j)
		{
			int ans=0;
			for(int x=0;x<n;++x)
				for(int y=0;y<n;++y)
					if(c1[x][y]==i && c2[x][y]==j)++ans;
			if(ans*4>=n*n)
			{
				ans=n*n/4;
				for(int x=0;x<n;++x)
					for(int y=0;y<n;++y)
						if(c1[x][y]==i && c2[x][y]==j)
						{
							printf("%d %d\n",x,y);
							--ans;
							if(!ans)return 0;
						}
			}
		}
	return 0;
}

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

int main() {

    int n;

    while(cin >> n, n) {
        vector< pair< double, string > > ans;
        for(int i = 0; i < n; i++) {
            string str;
            int p, a, b, c, d, e, f, s, m;
            cin >> str >> p >> a >> b >> c >> d >> e >> f >> s >> m;
            int t = a + b + c + (d + e) * m;
            int cnt = f * m;
            ans.push_back(make_pair(-(cnt * s - p) / (double)t, str));
        }

        sort(ans.begin(), ans.end());
        for(int i = 0; i < n; i++) {
            cout << ans[i].second << endl;
        } cout << "#" << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;

void ins() {}
template<class T,class... Rest>void ins(T& v,Rest&... rest){cin>>v;ins(rest...);}
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) for(int i=0,_i=(n);i<_i;++i)
#define _rep3(i,a,b) for(int i=(a),_i=(b),_ii=(b)>(a)?1:-1;abs(_i-i)>0;i+=_ii)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2)(__VA_ARGS__)
#define all(f,c,...) (([&](decltype((c)) cccc) { return (f)(begin(cccc), end(cccc), ## __VA_ARGS__); })(c))

signed main() {
  ll N, M, V, P;
  ins(N, M, V, P);

  vector<ll> A(N);
  rep(i, N)
    cin >> A[i];
  all(sort, A); all(reverse, A);

  int ok = -1, ng = N;
  auto f = [&](int mid) {
    if (mid < P) return true;
    if (A[P-1] > A[mid]+M) return false;

    ll tmp = 1LL * ((N-mid)+P-1) * M;
    rep(i, P-1, mid)
      tmp += min(1LL*M, max(0LL, A[mid]+M-A[i]));
    return tmp >= M*V;
  };
  while (abs(ok - ng) > 1) {
    int mid = ok + (ng-ok) / 2;
    (f(mid) ? ok : ng) = mid;
  }
  cout << ok+1 << endl;
  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

// 二分探索
// 求める点以上の場合にtrueを返すよう実装する。
template <class V>
V binary_search(V _l, V _r, function<bool(V)> f) {
    V l = _l, r = _r;
    while(r>l) {
        ll mid = (l+r)/2;
        if (f(mid)) {
            r = mid;
        } else {
            l = mid;
        }
        if (l+1==r) {
            if (f(l)) {
                r = l;
            } else {
                l = r;
            }
        }
    }
    return l;
}

/* verified by atcoder agc 041 b
   https://atcoder.jp/contests/agc041/tasks/agc041_b */
vector<ll> a;
ll n,m,v,p;

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin>>n>>m>>v>>p;
    a.assign(n,0);
    rep(i,n) cin>>a[i];
    sort(begin(a), end(a));
    function<bool(ll)> chk = [&](ll idx) {
        if (a[idx]+m<a[n-p]) {
            return false;
        }
        ll cnt = 0;
        rep(i,n) {
            if (i>n-p||i<=idx) {
                cnt += m;
            }
            else {
                cnt += max(0LL, a[idx]+m-a[i]);
            }
        }
        return cnt >= m*v;
    };
    ll l = binary_search(0LL, n-1, chk);
    cout << n-l << endl;
    return 0;
}


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

int N,P;
int D[55];

bool check(int ind){
  for(int i=0;i<N;i++){
    if( i == ind ) continue;
    if( D[i] ) return false;
  }
  return true;
}

int main(){
  while( cin >> N >> P && (N||P) ) {
    memset(D,0,sizeof(D));
    int ind = 0;
    while( 1 ){
      if( P ){
	P--;
	D[ind]++;
	if( P == 0 ){
	  if( check(ind) ) break;
	}
      } else {
	P += D[ind];
	D[ind]=0;
      }
      ind++;
      ind%=N;
    }
    cout << ind << endl;
  }
}

#include <iostream>
#include <map>

using namespace std;

typedef pair<int, int> P;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int W, H;
int curl[22][22];
int ans;
P s;

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

void dfs(int x, int y, int n)
{
  if (n >= ans)
    return;

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (!inCurl(nx, ny))
      continue;
    if (curl[ny][nx] == 1)
      continue;

    while (true) {
      if (curl[ny][nx] == 3) {
	ans = n;
	return;
      }
      nx += dx[i];
      ny += dy[i];

      if (!inCurl(nx, ny))
	break;

      if (curl[ny][nx] == 1) {
	curl[ny][nx] = 0;
	dfs(nx - dx[i], ny - dy[i], n + 1);
	curl[ny][nx] = 1;
	break;
      }
    }
  }
}

int main()
{
  while (cin >> W >> H, W && H) {
    // Input
    for (int y = 0; y < H; y++)
      for (int x = 0; x < W; x++) {
	cin >> curl[y][x];
	if (curl[y][x] == 2) {
	  s.first = x;
	  s.second = y;
	  curl[y][x] = 0;
	}
      }

    ans = 11;
    dfs(s.first, s.second, 1);
    if (ans > 10)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}


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

int main(void){
	string str, instruction, temp;
	int a,b,q;

	cin >> str;
	cin >> q;

	for (int i = 0; i < q; i++){
		cin >> instruction >> a >> b;
		if (instruction == "replace"){
			cin >> temp;
			str.replace (a, (b-a)+1, temp);
		}
		else if (instruction == "reverse"){
			for (int i = a, j = b; i < ((a + b) / 2)+1; i++, j--){
				char temp = str[j];
				str[j] = str[i];
				str[i] = temp;
			}
		}
		else if (instruction == "print"){
			for (int i = a; i < b + 1; i++){
				cout << str[i];
			}
			cout << "\n";
		}
	}

	return 0;
}

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

int main() {
  int n, tmp;
  string s, res, station = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  while (cin >> n, n) {
    int key[n];
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      key[i] = tmp;
    }
    cin >> s;
    res = "";
    for (int i = 0; i < s.length(); i++) {
      res += station[(station.find(s[i])-key[i%n]+52)%52];
    }
    cout << res << el;
  }
}


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

int main() {
  int64_t M,a=0,b=0;
  cin>>M;
  for(int64_t i=0;i<M;i++){
    int64_t x,y;
    cin>>x>>y;
    a+=x*y;
    b+=y;
  }
  cout<<(a-1)/9+b-1<<endl;
}

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

#define int long long

int n,tot,sum;

signed main(){
	read(n);
	for(int i=1,x,y;i<=n;i++){
		read(x,y);
		tot+=y;sum+=x*y;
	}
	write(tot-1+(sum-1)/9);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long,long long>
#define ii pair<int,int>
#define mod 1000000007
#define endl '\n'
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll a,ll b){
	ll ans=1;
	while(b>0){
		if(b%2!=0){
			ans=(ans*a)%mod;
		}
		a=((a%mod)*(a%mod))%mod;
		b>>=1;
	}
	return ans;
}

void sieve(int n){
	bool prime[n+1];
	memset(prime,true,sizeof(prime));
	for(int p=2;p*p<=n;p++){
		if(prime[p]){
			for(int i=p*p;i<=n;i+=p)
				prime[i]=false;
		}
	}
	for(int p=2;p<=n;p++){
		if(prime[p])
			cout<<p<<" ";
	}
}

int ans(int index,int time,int t,vector<pair<int,int>>& v,vector<vector<int>>& value){
	if(index>=v.size())
		return 0;
	if(time>=t)
		return 0;
	if(value[index][time]!=-1)
		return value[index][time];
	int returnVal=0;
	returnVal = v[index].second + ans(index+1,time+v[index].first,t,v,value);
	returnVal = max(returnVal,ans(index+1,time,t,v,value));
	value[index][time]=returnVal;
	return returnVal;
}

int main() {
	IOS;
	int n,t;
	cin>>n>>t;
	vector<pair<int,int>> v;
	for(int i=0;i<n;i++){
		int p,q;
		cin>>p>>q;
		v.push_back({p,q});
	}
	sort(v.begin(),v.end());
	std::vector<vector<int>> va(n+1,vector<int> (t+1,-1));
	cout<<ans(0,0,t,v,va);

	return 0;
}

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

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

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

/* Macros reg. ends here */

const ll INF = 1LL << 50;
struct UnionFind {
  vector<ll> par, sizes;
  ll cnt;
  UnionFind(ll N) : par(N), sizes(N, 1), cnt(N) {
    for (ll i = 0; i < N; i++) par[i] = i;
  }

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

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

  ll size(ll x) { return sizes[find(x)]; }

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

  ll count() const { return cnt; }
};
int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n, t;
  cin >> n >> t;

  vll as(n), bs(n);
  rep(i, n) cin >> as[i] >> bs[i];

  vvll dp1(n + 1, vll(t, 0));
  vvll dp2(n + 2, vll(t, 0));

  rep(i, n) {
    rep(j, t - 1) {
      dp1.at(i + 1).at(j + 1) = dp1.at(i + 1).at(j);
      chmax(dp1.at(i + 1).at(j + 1), dp1.at(i).at(j + 1));
      if (j + 1 - as[i] >= 0)
        chmax(dp1.at(i + 1).at(j + 1), bs[i] + dp1.at(i).at(j + 1 - as[i]));
    }
  }

  rep(i, n) {
    rep(j, t - 1) {
      dp2.at(i + 1).at(j + 1) = dp2.at(i + 1).at(j);
      chmax(dp2.at(i + 1).at(j + 1), dp2.at(i).at(j + 1));
      if (j + 1 - as[n - 1 - i] >= 0)
        chmax(dp2.at(i + 1).at(j + 1),
              bs[n - 1 - i] + dp2.at(i).at(j + 1 - as[n - 1 - i]));
    }
  }

  ll juee = 0;
  rep(i, n) {
    rep(j, t){
      ll tmp = dp1[i][j] + bs[i] + dp2[n - 1 - i][t - 1 - j];
      chmax(juee, tmp);
    }
  }

  cout << juee << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int h1,m1,h2,m2,k;
    cin>>h1>>m1>>h2>>m2>>k;
    int a=h2-h1;
    int b=m2-m1;
    int c=(a*60+b)-k;
    cout<<c<<endl;
    exit(0);
}

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
using namespace std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;

int main(void){
    lint n,m,d;
    cin >> n >> m >> d;
    cout << (d?2:1)*(n-d)*(m-1)/pow(n,2) << endl;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=2e5+100;
const int M=4e5+100;
int head1[N],ver1[M],Next1[M];
int tot1,tot2;
int add1(int x,int y)
{
	ver1[++tot1]=y;
	Next1[tot1]=head1[x];
	head1[x]=tot1;
}

int head2[N],ver2[M],Next2[M];
int add2(int x,int y)
{
	ver2[++tot2]=y;
	Next2[tot2]=head2[x];
	head2[x]=tot2;
}
int cnt=0;
int c1[N],c2[N];
void dfs1(int x)
{
	c1[x]=cnt;
	for(int i=head1[x];i;i=Next1[i])
	{
		int y=ver1[i];
		if(c1[y])continue;
		dfs1(y);
	}
}
void dfs2(int x)
{

	c2[x]=cnt;
	for(int i=head2[x];i;i=Next2[i])
	{
		int y=ver2[i];

		if(c2[y])continue;
		dfs2(y);
	}
}

map<pair<int,int>,int>MM;
int main()
{
	int n,k,l;
	while(cin>>n>>k>>l)
	{
		MM.clear();
		memset(head1,0,sizeof(head1));
		memset(ver1,0,sizeof(ver1));
		memset(Next1,0,sizeof(Next1));
		memset(head2,0,sizeof(head2));
		memset(ver2,0,sizeof(ver2));
		memset(Next2,0,sizeof(Next2));
		memset(c1,0,sizeof(c1));
		memset(c2,0,sizeof(c2));
		tot1=0;
		tot2=0;
		for(int i=1;i<=k;i++)
		{
			int x,y;
			cin>>x>>y;
			add1(x,y);
			add1(y,x);
		}

		for(int i=1;i<=l;i++)
		{
			int x,y;
			cin>>x>>y;
			add2(x,y);
			add2(y,x);
		}
		for(int i=1;i<=n;i++)
		{
			if(c1[i]==0)
			{
				cnt++;
				dfs1(i);
			}
		}
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			if(c2[i]==0)
			{
				cnt++;
				dfs2(i);
			}
		}

		for(int i=1;i<=n;i++)
			MM[make_pair(c1[i],c2[i])]++;
		for(int i=1;i<=n;i++)
		{
			cout<<MM[make_pair(c1[i],c2[i])]<<" ";
			if(i==n)cout<<endl;
		}

	}
	return 0;
}

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
#define mp make_pair
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};

int main() {
    ll N,M,Q;
    cin >> N >> M >> Q;
    vvl cnt(N+1,vl(N+1,0));
    rep(i,M){
        ll L,R;
        cin >> L >> R;
        cnt[L][R]++;
    }
    FOR(i,0,N+1){
        FOR(j,1,N+1){
            cnt[i][j]+=cnt[i][j-1];
        }
    }
    FOR(i,1,N+1){
        FOR(j,0,N+1){
            cnt[i][j]+=cnt[i-1][j];
        }
    }
    rep(i,Q){
        ll p,q;
        cin >> p >> q;
        p--;
        cout << cnt[p][p]+cnt[q][q]-cnt[p][q]-cnt[q][p] << endl;
    }
}


#include <cstdio>
#include <vector>
using namespace std;
vector<int> a[1000];
bool v[1000];
int s, prevv[1000], n, m, x, y;
void dfs(int cur){
	v[cur] = true;
	for (auto e : a[cur]){
		if (e == s){
			prevv[e] = cur;
		}
		if (!v[e]){
			prevv[e] = cur;
			dfs(e);
		}
	}
}
vector<int> optimize(vector<int> p){
	bool b[n];
	fill(b, b + n, false);
	for (int i = 0; i < p.size(); i++) b[p[i]] = true;
	for (int i = 0; i < p.size(); i++){
		int temp = i - 1;
		if (i == 0) temp = p.size() - 1;
		for (auto e : a[p[i]]){
			if (b[e] && e != p[temp]){
				vector<int> np;
				np.emplace_back(p[i]);
				int cur = i;
				while (p[cur] != e){
					cur++;
					if (cur == p.size()) cur = 0;
					np.emplace_back(p[cur]);
				}
				return optimize(np);
			}
		}
	}
	return p;
}
int main(){
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++){
		scanf("%d %d", &x, &y);
		a[x - 1].emplace_back(y - 1);
	}
	for (s = 0; s < n; s++){
		fill(v, v + n, false);
		fill(prevv, prevv + n, -1);
		dfs(s);
		if (prevv[s] != -1){
			vector<int> p;
			p.emplace_back(s);
			int cur = prevv[s];
			while (cur != s){
				p.emplace_back(cur);
				cur = prevv[cur];
			}
			vector<int> bruh = optimize(p);
			printf("%d\n", bruh.size());
			for (int i = 0; i < bruh.size(); i++) printf("%d\n", bruh[i] + 1);
			return 0;
		}
	}
	printf("-1\n");
}

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#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 yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define no cout<<"No"<<endl
#define NO cout<<"NO"<<endl
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

ll pos=-1;
const ll MAX_N=1001;
vector<bool> visited,finished;
vvll edge(MAX_N);
vll hist;

void dfs(ll now){
  visited[now]=true;
  hist.push_back(now);
  for(auto e:edge[now]){
    if(finished[e])
      continue;

    if(visited[e]&&!finished[e]){
      pos=e;
      return;
    }

    dfs(e);
    if(pos!=-1)
      return;
  }

  hist.pop_back();
  finished[now]=true;
  return;
}

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

  ll n,m;
  cin>>n>>m;
  REP(i,m){
    ll a,b;
    cin>>a>>b;
    a--;
    b--;
    edge[a].push_back(b);
  }

  //find cycle
  visited.assign(n,false);
  finished.assign(n,false);
  pos=-1;
  REP(i,n){
    dfs(i);
    if(pos!=-1)
      break;
  }

  if(pos==-1){
    cout<<-1<<endl;
    return 0;
  }

  //restoration cycle
  vll cycle;
  while(true){
    ll t=hist.back();
    hist.pop_back();
    cycle.push_back(t);
    if(pos==t)
      break;
  }

  reverse(ALL(cycle));

  //minimize
  while(true){
    ll nc=cycle.size();
    vll table(n,-1);
    REP(i,nc)table[cycle[i]]=i;

    ll from=-1,to=-1;
    REP(i,nc){
      for(auto e:edge[cycle[i]]){
        if(table[e]!=(i+1)%nc && table[e]!=-1){
          from=i;
          to=table[e];
        }
      }
    }

    if(from==-1)
      break;

    vll ncycle;
    ll cur=to;
    while(true){
      ncycle.push_back(cycle[cur]);
      cur=(cur+1)%nc;
      if(cur==from)
        break;
    }
    ncycle.push_back(cycle[from]);
    cycle=ncycle;
  }

  cout<<cycle.size()<<endl;
  REP(i,cycle.size())cout<<cycle[i]+1<<endl;
}


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

int main(){

    int n;
    cin>>n;
    vector<int> arr(n+1);

    for(int i = 1; i <= n; i++) cin>>arr[i];

    vector<ll> pf(n+1);
    vector<ll> dp(n+1,LLONG_MIN);
    for(int i = 1; i <= n; i++) if(i&1) pf[i]+=arr[i]+pf[i-2 < 0 ? 0 : i-2]; // pref of odd sum

    dp[0] = 0;
    dp[1] = 0;
    for(int i = 2; i <= n; i++){
        if(i&1) dp[i] = max(dp[i-1],arr[i]+dp[i-2]); // for odd
        else dp[i] = max(pf[i-1],dp[i-2]+arr[i]); //for even
    }
    cout<<dp[n];
}


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

int main() {
  vector<int>vec(5);
  cin >> vec[0] >> vec[1] >> vec[2] >> vec[3] >> vec[4];
  int k;
  cin >> k;
  bool x = true;
  for(int i=0; i<4; i++){
    for(int j=i+1; j<5; j++){
      if(vec[j]-vec[i] > k){
        cout << ":(" << endl;
        return 0;
      }
    }
  }
  cout << "Yay!" << endl;
}

#include <bits/stdc++.h>
#define fast_io ios :: sync_with_stdio(false);
using namespace std;

int main() {
	cout << fixed << setprecision(3);
	fast_io;
	int a, b, c, d, e, k;
    cin >> a >> b >> c >> d >> e >> k;
    if(e-a<=k)cout<<"Yay!";
    else cout<<":(";

	return 0;
}


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

int main(){
    int n,k;
    cin >> n >> k;
    ve<int> x(n),y(n);
    rep(i,n) cin >> x[i];
    rep(i,n) y[i] = -x[n-1-i];
    int ans = 2e9;
    rep(i,n-k+1){
        int memo = abs(x[i]) + (x[i+k-1]-x[i]);
        ans = min(ans,memo);
        memo = abs(y[i]) + y[i+k-1]-y[i];
        ans = min(ans,memo);
    }
    cout << ans << endl;
    return 0;
}

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

const ll INF = 1LL << 60;

ll N, K;
vector<ll> v;
int main() {
    cin >> N >> K;
    v.resize(N);
    rep(i, N) cin >> v[i];

    bool zero_exist = false;
    rep(i, N) {
       if (v[i] == 0) {
           zero_exist = true;
       }
    }

    ll ans = INF;
    if (!zero_exist) {
        v.push_back(0);
        K++;
    }
    sort(v.begin(), v.end());

    ll zero_idx = lower_bound(v.begin(), v.end(), 0) - v.begin();
    // cout << zero_idx << endl;

    for (int i = 0; i <= N + 1 - K; i++) {
        ll r = i + K - 1;
        ll l = i;
        if (l <= zero_idx && zero_idx <= r) {
            ll tmp = INF;
            // 一直線
            if (v[r] < 0) {
                tmp = min(tmp, abs(v[l]));
            }
            // 一直線
            if (v[l] > 0) {
                tmp = min(tmp, abs(v[r]));
            }

            // 右行ってから左
            ll dist = abs(v[r]) * 2 + abs(v[l]);
            tmp = min(tmp, dist);

            // 左行ってから右
            ll dist2 = abs(v[l]) * 2 + abs(v[r]);
            tmp = min(tmp, dist2);

            // cout << i << " " << tmp << " " << dist << " " << dist2 << " " << abs(v[l]) << " " << abs(v[r]) << endl;
            ans = min(ans, tmp);
        }
    }
    if (ans == INF) {
        cout << 0 << endl;
    } else {
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll cnt[66][2];
int main(){
	int n;cin>>n;
	for(int i=0;i<n;i++){
		ll a;cin>>a;
		int bit=0;
		while(a){
			if(a&1){
				cnt[bit][1]++;
			}else cnt[bit][0]++;
			a/=2;
			bit++;
		}
		for(int j=bit;j<=64;j++)cnt[j][0]++;
	}
	ll ans=0;
	for(int i=0;i<=62;i++){
		ll p=(1LL<<i);p%=mod;
		ll hano=cnt[i][0]*cnt[i][1];
		hano%=mod;
		ans+=(p*hano);
		ans%=mod;
	}
	cout<<ans;
	return 0;
}

/*input
10 100000
50000 50001 50002 50003 50004 50005 50006 50007 50008 50009
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb push_back
#define MP(a,b) make_pair(a,b)
#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())
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}

//}}}
const ll maxn=100005;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;

		b>>=1;
	}
	return res;
}

ll inv[maxn];
ll dp[maxn];
int p[maxn];
int main(){
	IOS;
	int n,x;
	cin>>n>>x;
	for(int i=1;i<=n;i++) inv[i]=mypow(i,MOD-2);
	REP(i,n) cin>>p[i];
	sort(p,p+n);

	dp[x]=1;
	for(int i=1;i<=n;i++) dp[x]=dp[x]*i%MOD;
	for(int i=n-1;i>=0;i--){
		if(i==0){
			REP(j,x+1) if(j>=p[i]){
				dp[j%p[i]]=(dp[j%p[i]]+dp[j])%MOD;
				dp[j]=0;
			}
		}
		else{
			REP(j,x+1) dp[j]=dp[j]*i%MOD*inv[i+1]%MOD;
			REP(j,x+1) dp[j%p[i]]=(dp[j%p[i]]+dp[j]*inv[i])%MOD;
		}
		// for(int i=0;i<=x;i++) cout<<dp[i]<<" \n"[i==x];
	}
	ll ans=0;
	REP(i,x+1){
		ans+=dp[i]*i%MOD;
		if(ans>=MOD) ans-=MOD;
	}
	cout<<ans<<'\n';
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100000+10;
int n;
int a[maxn];
int partition(int a[],int p,int r)
{
   int x=a[r];
   int i=p-1;
   for(int j=p;j<r;j++)
   {
       if(a[j]<=x)
       {
           i++;
           swap(a[i],a[j]);
       }
   }
   swap(a[i+1],a[r]);
   return i+1;
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    int res=partition(a,0,n-1);
    for(int i=0;i<n;i++)
    {
        if(i) printf(" ");
        if(i==res) printf("[");
        printf("%d",a[i]);
        if(i==res) printf("]");
    }
    printf("\n");

    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 100010
using namespace std;
typedef long long ll;
ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m;
ll K;
int a[maxn],d[maxn],vis[maxn],id[maxn];
int p[maxn],top;
ll ans[maxn];
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i]=read();
    for(int i=1;i<=n;i++)  d[i]=a[i]-a[i-1];
    for(int i=1;i<=n;i++)  id[i]=i;
    m=read();K=read();
    for(int i=1;i<=m;i++)
    {
        int x=read();
        swap(id[x],id[x+1]);
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            top=0;
            for(int j=i;!vis[j];j=id[j])  vis[j]=1,p[++top]=j;
            for(int j=1;j<=top;j++)  ans[p[j]]=d[p[(K+j-1)%top+1]];
        }
    }
    for(int i=1;i<=n;i++)  ans[i]+=ans[i-1];
    for(int i=1;i<=n;i++)  printf("%lld\n",ans[i]);
    puts("");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <queue>

#define pb push_back
#define mp make_pair
#define x first
#define y second
#define FOR(i, a, b) for(int i=a, loop=b; i<=loop; ++i)
#define REV(i, a, b) for(int i=a, loop=b; i>=loop; --i)
#define mize(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define shl(k) ((k) << 1)
using namespace std;

const int N = 2000 + 5, mod = 998244353;
int n, k;
int64_t C[N][N], G[N][N], F[N], P[N], d[N], tmp, res[N][2];

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

    cin >> k >> n;

    int r = max(k, n);
    FOR(i, 0, r) C[i][0] = 1;
    FOR(i, 1, r) FOR(j, 1, i) C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;

    G[0][1] = 1;
    FOR(i, 1, n) FOR(j, 1, k) G[i][j] = (G[i][j-1] + G[i-1][j]) % mod;
    G[0][0] = 1; G[0][1] = 0;
    FOR(i, 0, n) FOR(j, 1, k) G[i][j] = (G[i][j] + G[i][j-1]) % mod;

    for(int m=1; m+m<=k; ++m) FOR(i, 2, n) F[m] = (F[m] + C[i-1][m+m-1] * G[n-i][k-m-m] % mod) % mod;
    for(int m=1; m+m+1<=k; ++m) FOR(i, 2, n) {
        tmp = G[n-i][k-m-m-1];
        if (n-i-1 >= 0) tmp = tmp + G[n-i-1][k-m-m-1];
        P[m] = (P[m] + C[i-1][m+m-1] * tmp % mod) % mod;
    }

    int64_t ans; int m;
    FOR(i, 0, k) res[i][0] = res[i][1] = -1;
    FOR(t, 2, k+k) {
        m = 0; FOR(i, 1, k) if (1<=t-i && t-i<=k) m += 1;
        if (res[m/=2][t&1] != -1) {
            ans = res[m][t&1];
            goto REP;
        }

        if (t&1) {
            ans = G[n][k];
            REV(i, m, 1) {
                d[i] = F[i];
                FOR(j, i+1, m) d[i] = (d[i] - d[j] * C[m-i][j-i] % mod + mod) % mod;
            }
        } else {
            ans = (G[n][k-1] + G[n-1][k-1]) % mod;
            REV(i, m, 1) {
                d[i] = P[i];
                FOR(j, i+1, m) d[i] = (d[i] - d[j] * C[m-i][j-i] % mod + mod) % mod;
            }
        }

        FOR(i, 1, m) ans = (ans - d[i] * C[m][i] % mod + mod) % mod;
        res[m][t&1] = ans;
        REP: cout << ans << '\n';
    }
}


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

bool check(int a,int b,int c)
{
    if((a+b)>c && (c+b)>a && (a+c)>b)
        return true;
    return false;
}

int main()
{
    // freopen("input01.txt", "r", stdin);
    // freopen("output01.txt", "w", stdout);
    int n;
    cin>>n;
    map<ll int,int> mp;
    ll int a;
    int l,ct=0;
    for(l=0;l<n;l++)
    {
        cin>>a;
        mp[a]++;
    }

    if(mp.size()<3)
    {
        cout<<0;
        return 0;
    }

    for(auto i=mp.begin();next(next(i))!=mp.end();i++)
    {
        for(auto j=next(i);next(j)!=mp.end();j++)
        {
            for(auto k=next(j);k!=mp.end();k++)
            {
                if(check(i->first,j->first,k->first))
                    ct+=(i->second*j->second*k->second);
            }
        }
    }

    cout<<ct;
    return 0;
}

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

char bd[12][12];

void del(int y, int x) {
    if (0 <= y && y < 12 && 0 <= x && x < 12 && bd[y][x] == '1') {
        bd[y][x] = '0';
        del(y-1, x);
        del(y+1, x);
        del(y, x-1);
        del(y, x+1);
    }
}

int main() {
    while (cin >> bd[0][0]) {
        for (int i=0; i<12; ++i) {
            for (int j=0; j<12; ++j) {
                if (i == 0 && j == 0) continue;
                cin >> bd[i][j];
            }
        }
        int cnt = 0;
        for (int i=0; i<12; ++i) {
            for (int j=0; j<12; ++j) {
                if (bd[i][j] == '1') {
                    del(i, j);
                    cnt ++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


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

int main(void){
    int N,K;
    cin >> N >> K;
    int l[N];
    for(int i = 0; i < N ;i++){
        cin >> l[i];
    }
    sort(l ,l+N ,greater<int>());
    int answer = 0;
    for(int i = 0; i < K ; i++){
        answer += l[i];
    }
    cout << answer << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define __lcm(a, b) a * b / __gcd(a, b)
// #define multicase
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 1e3 + 5;

int n, k, ans;

void solve() {
    int T = 1;
  #ifdef multicase
    cin >> T;
  #endif
    for(int tc = 1; tc <= T; tc++) {
        cin >> n >> k;
        ans = 1;
        while(n--) ans = min(ans * 2, ans + k);
        cout << ans << "\n";
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    solve();

}


// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
using namespace std;
// using namespace atcoder;
using ll = long long;
using P = pair<int, int>;
const int mod = 1e9 + 7;
// --------------------------------------------------------------------------------
void solve()
{
    int a, b;
    cin >> a >> b;
    for (int i = 0;; i++)
    {
        if (b == 1)
        {
            cout << 0 << endl;
            break;
        }
        if (b <= a * (i + 1) - i)
        {
            cout << i + 1 << endl;
            break;
        }
    }
}
// --------------------------------------------------------------------------------
int main()
{
    // srand((unsigned)time(NULL));
    // int bt = clock();
    solve();
    // double et = 1.0 * (clock() - bt) / CLOCKS_PER_SEC;
    // printf("Execution Time: %.4lf sec\n", et);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
 tree<int, null_type,less<int>,
 rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> void re(T& x) { cin >> x; }
template<typename T>
void remdup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "/home/o_o/MyCodes/pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007

void solve(){
	int a , b ;
	re(a,b);
	int cnt = 1 ;
	cout << (a + b - 3) / (a-1) ;
}

signed main() {
	unsyncIO();
	int tt =1 ;
	// cin >> tt;
	rep(i,0,tt) solve();

#ifdef np
	cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for(int i = a; i < b; i++)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef long long int lli;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

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

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

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <functional>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1000000000;
constexpr ll INF = 1000000000000000000;
template <typename T> bool chmin(T &x, const T &y) {if (x < y) {x = y; return true;} return false;}
template <typename T> bool chmax(T &x, const T &y) {if (x > y) {x = y; return true;} return false;}

int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];

    vector<ll> B(N, 0), C(N, 0);
    B[0] = abs(A[0]);
    for (int i = 1; i < N; ++i) B[i] = B[i - 1] + abs(A[i] - A[i - 1]);
    reverse(ALL(A));
    C[0] = abs(A[0]);
    for (int i = 1; i < N; ++i) C[i] = C[i - 1] + abs(A[i] - A[i - 1]);
    reverse(ALL(A));
    reverse(ALL(C));
    rep(i, N) {
        if (i == 0) cout << C[i + 1] + abs(A[i + 1]) << "\n";
        else if (i == N - 1) cout << B[i - 1] + abs(A[i - 1]) << "\n";
        else cout << B[i - 1] + abs(A[i + 1] - A[i - 1]) + C[i + 1] << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define MOD 1000000007
#define ar array
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL);

int32_t main() {
	fast_io;

	// int t; cin >> t;
	// while (t--) {

	// }
	string K; cin >> K;
	int D; cin >> D;
	int len = (int) K.size();
	vector<vector<int>> dp(D, vector<int> (2));

	for (int digit = 0; digit < K[0] - '0'; digit++) {
		dp[digit % D][true]++;
	}
	dp[(K[0] - '0') % D][false]++;

	for (int pos = 1; pos < len; pos++) {
		vector<vector<int>> new_dp(D, vector<int> (2));
		for (int sum = 0; sum < D; sum++) {
			for (bool choose_any_digit : {false, true}) {
				for (int digit = 0; digit < 10; digit++) {
					if (digit > K[pos] - '0' && !choose_any_digit) {
						break;
					}
					new_dp[(sum + digit) % D][choose_any_digit || digit < K[pos] - '0'] += dp[sum][choose_any_digit];
					new_dp[(sum + digit) % D][choose_any_digit || digit < K[pos] - '0'] %= MOD;
				}
			}
		}
		dp = new_dp;
	}
	int res = (dp[0][true] + dp[0][false]);
	res--;
	if (res < 0) {
		cout << res + MOD << endl;
	}
	else {
		cout << res << endl;
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define flash ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
ll dp[2][2][100];
ll mod=1e9+7;ll d;
ll mo(ll x)
{
    if(x>=0)
    return(x%d);
    else
    return(x%d+d)%d;
}
int main()
{
    flash
    string s;
    cin>>s;ll n=s.size();ll a[n];
    for(ll i=0;i<n;i++)
    {
        a[i]=s[i]-'0';
    }
cin>>d;
  dp[1][0][0] = 1;dp[1][1][0] = 1;

    for(ll i = n-1; i >=0 ; i--) {
        for(ll state = 0; state < 2 ; state++) {
            for(ll sum = 0; sum < d; sum++) {
                if(state) {
                    for(ll x = 0; x <= a[i] ; x++) {
                        dp[0][1][sum] += (x== a[i]) ? dp[1][1][mo(sum-x)]%mod:dp[1][0][mo(sum-x)]%mod;
                    }
                }
                else {
                    for(ll x = 0; x < 10 ; x++) {
                        dp[0][0][sum] += dp[1][0][mo(sum-x)]%mod;
                    }
                }
            }
          }
          if(i!=0)
          {
         for(ll sum=0;sum<d;sum++)
            {
                dp[1][1][sum]=dp[0][1][sum];
                dp[1][0][sum]=dp[0][0][sum];
                dp[0][1][sum]=0;
                dp[0][0][sum]=0;
            }
          }
    }
    dp[0][1][0]--;
    if(dp[0][1][0]==-1)
    dp[0][1][0]=mod-1;
    dp[0][1][0]=dp[0][1][0]%mod;
    cout<<dp[0][1][0]<<endl;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); }
using P = pair <int, int>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

ll MOD = 1000000007;
ll INF =1<<30;

int main(){
    int n,m;
    cin >> n;
    vector<string>s(n),t(m);
    rep(i,n)cin >>s[i];
    cin>>m;
    rep(i,m)cin >>t[i];
    map<string,int>mp;
    rep(i,n){
        mp[s[i]]++;
    }
    rep(i,m) if(mp[t[i]]>0)mp[t[i]]--;
    int ans = 0;
    for(auto x:mp){
        if(x.second>ans) ans=x.second;
    }
    cout << ans <<endl;
}

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define RG register
using namespace std;
const int N=1e6+10;
int n,ans;
char st[N],ed[N];
queue<int> pn;
inline int mi(int a,int b){return a<b ? a : b;}
inline int mx(int a,int b){return a>b ? a : b;}
int main()
{
//	freopen("Shik and Copying String.in","r",stdin);
	scanf("%d%s%s",&n,st+1,ed+1);
	if(!strcmp(st+1,ed+1)) return printf("0\n"), 0;
	for(int i=n,j=n;i;i--)
	{
		if(ed[i]==ed[i-1]) continue;
		j=mi(i,j);
		while(ed[i]!=st[j] && j) j--;
		if(!j) return printf("-1\n"), 0;
		while(!pn.empty())
		 if((int)pn.front()-(int)pn.size()>=i) pn.pop();
		 else break;
		pn.push(j);
		if(i!=j) ans=mx(ans,(int)pn.size());
	}
	printf("%d\n",ans+1);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll INF = 1000000000;
int main(int argc, char const *argv[]) {
  ll n;std::cin >> n;
  vvl dat = VV(n, 2, 0, ll);
  re(i, n) {
    scanf("%lld %lld", &dat[i][0], &dat[i][1]);
    dat[i][0] = INF - dat[i][0];
  }
  sort(all(dat), [](vll a, vll b){return a[0]-a[1]>b[0]-b[1];});

  vvl dp = VV(n+1, n+1, -INF*INF, ll);
  dp[0][0] = INF;

  for(int i=1;i<=n;i++){
    dp[i][0] = INF;
    for(int j=1;j<=i;j++){
      if(dp[i-1][j-1]<dat[i-1][0]) dp[i][j] = dp[i-1][j];
      else dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]-dat[i-1][1]);
    }
  }
  ll ans = 0;
  for(int i=0;i<=n;i++) if(dp[n][i]!=-INF*INF) ans = i;
  std::cout << ans << '\n';
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

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

#define INF 1000000000000000LL
#define N 5010
int n;
PII a[N];
ll f[N], g[N]; // min height

bool cmp(PII x, PII y) {
	return x.fi + x.se < y.fi + y.se || x.fi + x.se == y.fi + y.se && x.se < y.se;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i ++)
		cin >> a[i].fi >> a[i].se;
	sort(a, a+n, cmp);
	for (int i = 0; i <= n; i ++) f[i] = INF;
	f[0] = 0;
	for (int i = 0; i < n; i ++) {
		memcpy(g, f, sizeof g);
		for (int j = 0; j <= n; j ++) f[j] = INF;
		for (int j = 0; j <= n; j ++) if (g[j] < INF) {
			if (g[j] <= a[i].fi) f[j+1] = min(f[j+1], g[j] + a[i].se);
			f[j] = min(f[j], g[j]);
		}
	}
	int S = 0;
	for (int i = 0; i <= n; i ++)
		if (f[i] < INF) S = max(S, i);
	cout << S << endl;
	return 0;
}


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

long long MOD = pow(10, 9) + 7;

// O(sqrt(N))の素因数分解
int main(){
    int Q;
    cin >> Q;
    vector<int> bucket(Q+1, 1);

    if (Q == 1)
    {
        cout << 1 << endl;
        return 0;
    }
    for (int i = 2; i <= Q; i++)
    {
        int target = i;
        for (int j = 2; j <= i; j++)
        {
            if (target % j != 0)
            {
                continue;
            }

            while (target % j == 0)
            {
                bucket[j]++;
                target /= j;
            }
        }
    }
    long long ans = 1;
    for (int j = 0; j <= Q; j++)
    {
        ans *= bucket[j];
        ans = ans % MOD;
    }
    cout << ans << endl;
    return 0;
}


//#define USE_CPP11
// #define USE_PRINTF

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

#ifdef USE_CPP11
#include <unordered_map>
#include <unordered_set>
#include <functional>
#endif

#define PRINT(obj) cout << obj << endl
#define REP(i, n) for(int (i) = 0; (i) < (n); i++)
#define SPACE " "

typedef long long ll;

const int INF = 1e9 + 10;

using namespace std;

namespace Scanner {
#ifdef USE_PRINTF
  int    nextInt()    { int    d; scanf("%d" , &d); return d; }
  float  nextFloat()  { float  f; scanf("%f" , &f); return f; }
  double nextDouble() { double f; scanf("%lf", &f); return f; }
  string nextString() { string s; scanf("%s" , &s); return s; }
  ll     nextll()     { ll     l; scanf("%ll", &l); return l; }
#else
  int    nextInt()    { int    d; cin >> d; return d; }
  float  nextFloat()  { float  f; cin >> f; return f; }
  double nextDouble() { double f; cin >> f; return f; }
  string nextString() { string s; cin >> s; return s; }
  ll     nextll()     { ll     l; cin >> l; return l; }
#endif

  string getLine() {
    string input;
    getline(cin, input);
    return input;
  }
};

namespace myLibrary {
  vector<string> split(const string& str, char delim) {
    vector<string> ret;
    size_t current = 0, found;
    while ((found = str.find_first_of(delim, current)) != string::npos) {
      ret.push_back(string(str, current, found - current));
      current = found + 1;
    }
    ret.push_back(string(str, current, str.size() - current));
    return ret;
  }

#ifdef USE_CPP11
  ///
  /// higher-order functional
  /// This is something like map in haskell, python, etc.
  /// Sample usage:
  ///   vector<int> a = {1, 2, 3, 4, 5};
  ///   vector<int> b = myLibrary::_Map<int, int>(a, [](int n){
  ///     return n * n;
  ///   });
  /// Result:
  ///   b = 1, 4, 9, 16, 25
  ///
  template<typename T_IN, typename T_OUT, class Functor>
  vector<T_OUT> _Map(vector<T_IN> src, Functor func) {
    vector<T_OUT> ret;
    for (int i = 0; i < src.size(); i++) {
      ret.push_back(func(src[i]));
    }
    return ret;
  }
#endif

};

/// BEGIN //////////////////////////////////////////////////////////

int main() {
  while (true) {
    int m    = Scanner::nextInt();
    int nmin = Scanner::nextInt();
    int nmax = Scanner::nextInt();

    if (m + nmin + nmax == 0) {
      break;
    }

    vector<int> Ps(m);
    REP(i, m) {
      int p = Scanner::nextInt();
      Ps[i] = p;
    }

    int ans, ans_diff = -1;
    for (int i = nmin - 1; i <= nmax - 1; i++) {
      int diff = Ps[i] - Ps[i + 1];
      if (diff >= ans_diff) {
        ans_diff = diff;
        ans = i;
      }
    }
    PRINT(ans + 1);
  }

  return 0;
}
/// END   //////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

int main() {
	int m,min,max,ten[200],gap=10001,sa;

	cin>>m>>min>>max;

	while(1){
		if(m==0 && min==0 && max==0){
			break;
		}
	    for(int i=1;i<=m;i++){
    		cin>>ten[i];
    	}
    	for(int i=min;i<=max;i++){
    		if(gap>=ten[i+1]-ten[i]){
	    		gap=ten[i+1]-ten[i];
    			sa=i;
	    	}
	    }
    	cout<<sa<<endl;
    	gap=10001;
    	sa=0;
	    cin>>m>>min>>max;
	}

	return 0;
}


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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e5 + 5, MOD = 998244353;
int a[N];

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

    int n;
    cin >> n;

    int mx = 0;

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

        if (i == 0 && x != 0) {
            cout << "0\n";
            return 0;
        }

        a[x]++;
        mx = max(mx, x);
    }

    if (a[0] != 1) {
        cout << "0\n";
        return 0;
    }

    ll ans = 1;

    for (int i = 1; i <= mx; i++) {
        for (int j = 0; j < a[i]; j++)
            ans = ans * a[i - 1] % MOD;
    }

    cout << ans << "\n";

    return 0;
}

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

int main() {
    map<int, bool> check;

    int n; cin >> n;
    while (n--) {
        int a; cin >> a;
        if (check[a]) {
            cout << "NO" << endl;
            return 0;
        }
        check[a] = true;
    }

    cout << "YES" << endl;
}


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

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 998244353;

////////////////////////////////////////////////////////////////////

const int N = 3e2 + 5;

int A[N], DP[N][N][N];

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

int main() {

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	mt19937 Rnd(time(0));

	string S; cin >> S;
	int m = 1, o = 0;
	for (char c : S) if (c == '0') m++; else A[m - 1]++, o++;
	reverse(A, A + m);
	DP[0][0][0] = 1;
	for (int i = 0; i < m; i++) for (int j = 0; j <= o; j++) for (int k = 0; k <= o; k++) {
		if (!DP[i][j][k]) continue;
		for (int l = 0; l <= A[i] + k; l++) Add(DP[i + 1][j + max(A[i] - l, 0)][k + A[i] - l], DP[i][j][k]);
	}
	int k; cin >> k;
	k = min(k, o);
	int ans = 0;
	for (int i = 0; i <= k; i++) Add(ans, DP[m][i][0]);
	cout << ans << endl;

	return 0;
}

#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

using namespace std;

#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
typedef long long Long;
typedef double Double;

template <class U, class V>
istream& operator>>(istream& is, pair<U, V>& p) {
  is >> p.first >> p.second;
  return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& x : v) {
    is >> x;
  }
  return is;
}

template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& x : v) {
    os << x << " ";
  }
  return os;
}

const int N = 303;
const int MOD = 998244353;
int memo[N][N][N];

vector<int> v;
int count(int ind, int ones, int k) {
  if (k < 0) return 0;
  if (ones < 0) return 0;
  if (ind == 0) return 1;
  int& res = memo[ind][ones][k];
  if (res != -1) return res;
  res = 0;
  for (int i = 0; i <= v[ind] + ones; ++i) {
    res =
        (res + count(ind - 1, v[ind] + ones - i, k - max(0, v[ind] - i))) % MOD;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  string s;
  int k;
  cin >> s >> k;

  memset(memo, -1, sizeof memo);
  v.emplace_back(0);
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '1') {
      ++v.back();
    } else {
      v.emplace_back(0);
    }
  }

  cout << count(sz(v) - 1, 0, min(N - 1, k)) << endl;

  return 0;
}


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

int main(void)
{
	priority_queue <int> w, k;
	int tmp;

	for (int i = 0; i < 10; i++){
		scanf("%d", &tmp);
		w.push(tmp);
	}
	for (int i = 0; i < 10; i++){
		scanf("%d", &tmp);
		k.push(tmp);
	}

	int sumw = 0, sumk = 0;
	for (int i = 0; i < 3; i++){
		sumw += w.top();
		w.pop();
		sumk += k.top();
		k.pop();
	}

	printf("%d %d\n", sumw, sumk);

	return (0);
}


#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define ld long double
#define ff first
#define ss second
const ll mod =   1e9 +7  ;
ll multiply(ll a, ll b ){ return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) {     return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a%mod) - (b % mod)+ mod) % mod ; }
////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
void solve(){
    ll n , m , v ,  p ;
    cin >> n >> m >> v >> p ;
    vector < ll > a(n) ;
    for( int i = 0 ; i < n ; i++) cin >> a[i]  ;
    sort(a.begin(),a.end());
    vector < ll > pre(n) ;
    pre[0] = a[0] ;
    for( int i = 1 ; i < n ; i++) pre[i] = pre[i-1] + a[i] ;
//    for( int i = 0 ; i < n ; i++) cout << a[i] << " "  ;
//    cerr << "\n" ;
    int ans = 0 ;
    for( int i = n-1 ; i >= 0  ; i--){
        ll r = n-p ;
        if(i>=r){
            ans++;
            continue ;
        }
        if(a[i]+m>=a[r]){
            int vote = v-p-i ;
            if(vote>0){
                ll sum = pre[r]-pre[i] ;
               // cout << i << " " << vote << " " << sum << " " << (a[i]+m)*(r-i)-sum<< "\n";
                if(m*vote<=(a[i]+m)*(r-i)-sum){
                    ans++;
                }
            }
            else{
                ans++;
            }
        }
        else{
            break ;
        }
    }
    cout << ans ;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1 ;
    //cin >> t ;
    while(t--){
        solve( );
    }
}

/*program by mangoyang*/
#pragma GCC optimize("Ofast", "inline")
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
    int ch = 0, f = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    if(f) x = -x;
}
const int N = 1000005;
int a[N], n, m, p, v;
inline int check(int mid){
    int num = 0; ll tot = 0;
    vector<int> vec;
    for(int i = 1; i <= n; i++)
        if(a[i] <= mid){
            tot += min(mid - a[i], m);
            if(m > mid - a[i])
                vec.push_back(m - mid + a[i]);
        }
        else num++, tot += m;
    if(num >= p) return 0;
    ll now = 1ll * v * m - tot;
    if(now <= 0) return 1;
    sort(vec.begin(), vec.end(), greater<int>());
    for(int i = 0; i < (int) vec.size(); i++){
        now -= vec[i], num++;
        if(now <= 0) break;
    }
    if(num >= p) return 0;
    return 1;
}
int main(){
    read(n), read(m), read(v), read(p);
    for(int i = 1; i <= n; i++) read(a[i]);
    int l = 0, r = 2e9, res = 2e9;
    while(l <= r){
        int mid = (1ll * l + r) >> 1ll;
        if(check(mid)) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
        if(a[i] + m >= res) ans++;
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
#define N 6005
#define INF 0x3f3f3f3f
#define LL long long
int n,MOD,f[4][N*2];
int rd()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return f*x;
}
int main()
{
	n=rd(),MOD=rd();
	f[0][N]=1;
	for(int i=1;i<=3*n;i++)
	{
		int l=N-(i-1),r=N+(i-1);
		for(int j=l;j<=r;j++)
		{
			for(int k=3;k>=1;k--)
				f[k][j]=f[k-1][j];
			f[0][j]=0;
		}
		for(int j=l;j<=r;j++)
		{
			f[0][j+1]=(f[0][j+1]+f[1][j])%MOD;
			int del=i-1;
			f[0][j-1]=(f[0][j-1]+1ll*f[2][j]*del%MOD)%MOD;
			del=1ll*(i-1)*(i-2)%MOD;
			f[0][j]=(f[0][j]+1ll*f[3][j]*del%MOD)%MOD;
		}
	}
	int ans=0;
	for(int i=N;i<=N+3*n;i++)
		ans=(ans+f[0][i])%MOD;
	printf("%d\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAX_FACT = 1e5 * 2 + 1;

ll fact[MAX_FACT], invFact[MAX_FACT], inv[MAX_FACT];

void init() {
  fact[0] = fact[1] = 1;
  invFact[0] = invFact[1] = 1;
  inv[1] = 1;
  for (auto i = 2; i < MAX_FACT; ++i)
  {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    invFact[i] = invFact[i - 1] * inv[i] % MOD;
  }
}

ll calcCombination(ll n, ll k) {
  if (n < k) {
    return 0;
  }
  if (n < 0 || k < 0) {
    return 0;
  }
  return fact[n] * (invFact[k] * invFact[n - k] % MOD) % MOD;
}

int main()
{
  ll N, M, K;
  cin >> N >> M >> K;

  init();

  ll result = 0;
  for (int i = 1; i < N; i++) {
    result = (result + i * (N - i) * M % MOD * M % MOD) % MOD;
  }
  for (int i = 1; i < M; i++) {
    result = (result + i * (M - i) * N % MOD * N % MOD) % MOD;
  }
  result = result * calcCombination(N * M - 2, K - 2) % MOD;
  cout << result << endl;
  return 0;
}


#include <bits/stdc++.h>

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

using namespace std;

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  rep(i, n) cin >> a.at(i + 1);
  vector<int> b(n + 1, 0);
  for (int i = n; i >= 1; i--) {
    int sum = 0;
    for (int j = i + i; j <= n; j += i) {
      sum += b.at(j);
    }
    if (a.at(i) == 1) {
      if (sum % 2 == 1) {
        b.at(i) = 0;
      } else {
        b.at(i) = 1;
      }
    } else {
      if (sum % 2 == 1) {
        b.at(i) = 1;
      } else {
        b.at(i) = 0;
      }
    }
  }
  vector<int> res;
  for (int i = 1; i <= n; i++) {
    if (b.at(i) == 1) {
      res.push_back(i);
    }
  }
  cout << res.size() << endl;
  rep(i, res.size()) {
    if (i != 0) {
      cout << " ";
    }
    cout << res.at(i);
  }
  cout << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 1000000007
void solve()
{
	ll n;
	cin>>n;
	vector<ll> x(n);
	for(ll i=0;i<n;i++)
		cin>>x[i];
	sort(x.begin(),x.end());
	ll a=0,b=0,c=0;
	for(ll i=n-1;i>=0;i--)
	{
		if(x[i]==x[i-1])
		{
			// cout<<x[i];
			a=x[i];
			c=i-1;
			break;
		}
	}
	// cout<<c<<a;
	for(ll i=c-1;i>=0;i--)
	{
		if(x[i]==x[i-1])
		{
			b=x[i];
			break;
		}
	}
	cout<<a*b;
	return;
}

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


#include <bits/stdc++.h>

using namespace std;

#define loop(i, n) for(int i = 0;i < int(n);i++)
#define rloop(i, n) for(int i = int(n);i >= 0;i--)
#define range(i, a, b) for(int i = int(a);i <= int(b);i++)
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1, v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1, v2, v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1, v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1, v2, v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define endl '\n'

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

const int N = 1e5 + 5;
const int LG = 18;
int up[N][LG];
int arr[N];
int n, l, q;

void run_case() {
    cin >> n;
    loop(i, n)cin >> arr[i];
    cin >> l >> q;

    rloop(i, n - 1) {
        int par = i == n - 1 ? n - 1 : (upper_bound(arr + i, arr + n, arr[i] + l) - arr - 1);
        up[i][0] = par;
        for (int k = 1; k < LG; ++k)
            up[i][k] = up[up[i][k - 1]][k - 1];
    }

    while (q--) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        if (a > b)swap(a, b);
        int ans = 1;
        for (int i = LG - 1; i >= 0; i--) {
            if (up[a][i] < b) {
                a = up[a][i];
                ans += 1 << i;
            }
        }
        cout << ans << endl;
    }
}

int main() {

#ifndef ONLINE_JUDGE
//    freopen("in.in", "r", stdin);
//    freopen("out.in", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T = 1;
//    cin >> T;
    while (T--)run_case();

    return 0;
}

#include <bits/stdc++.h>
const int N=100005,M=20;
using namespace std;
int n,L,q,ar[N],nx[M][N];
int calc(int i){
    int l=i,r=n,ans=-1;
    while(l<=r){
        int m=(l+r)/2;
        if(ar[m]-ar[i]<=L){
            ans=m;
            l=m+1;
        }else{
            r=m-1;
        }
    }
    return ans;
}
int kth(int cur,int k){
    for(int i=M-1;i>=0;i--){
        if(k>=(1<<i)){
            k-=(1<<i);
            cur=nx[i][cur];
        }
    }
    return cur;
}
int main()
{ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>ar[i];
    cin>>L>>q;
    for(int i=1;i<=n;i++)nx[0][i]=calc(i);
    for(int i=1;i<M;i++){
        for(int j=1;j<=n;j++){
            nx[i][j]=nx[i-1][nx[i-1][j]];
        }
    }
    while(q--){
        int a,b;cin>>a>>b;
        if(a>=b)swap(a,b);
        int l=1,r=n,ans=-1;
        while(l<=r){
            int m=(l+r)/2;
            //cout<<a<<' '<<m<<' '<<kth(a,m)<<'\n';
            if(kth(a,m)>=b){
                ans=m;
                 r=m-1;
            }else{

                l=m+1;
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	string s,t;
	cin>>s>>t;
	cout<<t<<s;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define PI acos(-1)
#define fast_io ios_base::sync_with_stdio(false) ; cin.tie(0); cout.tie(0);
ll mod=1e9+7;

ll rep_pow(ll a, ll b){//繰り返し2乗法
    if(b==0) return 1;
    else if(b%2==0){
        ll memo = rep_pow(a, b/2);
        return memo*memo%mod;
    }else{
        return rep_pow(a, b-1)*a%mod;
    }
}

ll nCr(ll n, ll r){//mod逆元使ったnCr
    ll ret=1;
    rep(i,r){
        ret *= n-i;//分子
        ret %= mod;
        ret *=rep_pow(r-i, mod-2);//mod逆元によりr!で割ると同値
        ret %= mod;
    }
    return ret;
}
ll gcd(ll a, ll b){ if(b==0) return a; return gcd(b, a%b);}

int counter(int x){
    if(x==0) return 0;
    return counter(x>>1)+(x&1);
}

int main(){
    fast_io

    int n,k;
    cin>>n>>k;
    int ans=n-k+1;

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


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

int main(){
  int nin;
  while(1){
    scanf("%d",&nin);
    if(!nin)break;

    long long int human[4002]={0};
    long long int no[4002],tanka,kazu,outflag=0;

    for(int i=0;i<nin;i++){
      scanf("%d %d %d",&no[i],&tanka,&kazu);
      human[no[i]]+=tanka*kazu;
    }

    for(int i=0;i<nin;i++){
      if(human[no[i]]>=1000000 && human[no[i]]!=-1){
	printf("%d\n",no[i]);
	human[no[i]]=-1;
	outflag=1;
      }
    }

    if(outflag==0)printf("NA\n");

  }
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n,m,ans=0;
  cin >> n >> m;
  while(n!=0||m!=0){
    ans = 0;
    n++;
    vector<int>a(m,0),b(m,0),hito(n,0),one(n,0);
    rep(i,m){
      cin>>a[i]>>b[i];
      if(a[i]==1){
	hito[b[i]]++;
	one[b[i]]++;
      }
      if(b[i]==1){
	hito[a[i]]++;
	one[a[i]]++;
      }
    }
    rep(i,n){
      if(i==0||i==1)continue;
      rep(j,m){
	if(one[i]!=0){
	  if(i==a[j])hito[b[j]]++;
	  if(i==b[j])hito[a[j]]++;
	}
      }
    }
    rep(i,n){
      if(i==0||i==1)continue;
      if(hito[i]!=0){
	ans++;
      }
    }
    cout<<ans<<endl;
    cin >> n >> m;
  }
  return (0);
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
int main() {
  string A;
  cin >> A;
  char B, C;
  if(A.size() == 2) {
    cout << A << endl;
  }
  else {
    B = A.at(0);
    C = A.at(2);
    A.at(0) = C;
    A.at(2) = B;
    cout << A << endl;
  }
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int) (x).size()

ll sum(int n);

void go(){
	string s;cin>>s;
	if(sz(s)==3) reverse(all(s));
	cout << s;
}
int main(){

   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);

   go();

   return 0;
}

ll sum(int n){
   if(n == 0){
      return n;
   }else{
      return sum(n/10) + n%10;
   }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=n-1;i>=(0);i--)
#define reps(i,f,n) for(ll i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int n;
string m = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

int findidx(char c) {
	rep(i, m.size())if(c == m[i]) return i;
}

void solve(void) {
	int k[110];
	string s;
	rep(i, n) cin >> k[i];
	cin >> s;
	string ans;
	rep(i, s.size()) {
		int idx = findidx(s[i]);
		idx = ((idx + 52) - k[i % n]) % 52;
		ans += m[idx];
	}
	cout << ans << endl;
}

int main(void) {
	// cout << m.size() << endl;
	while(1) {
		cin >> n;
		if(n == 0) break;
		solve();
	}
	return 0;
}

// King's Inspection

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

using namespace std;

int main() {
    int n;
    int now=0;
    int k[102];
    int ansNum;
    char station[102];
    char ans[102];
    string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //vector<char> ans;

    while(1) {
      // Input
        cin>>n;
        if(n == 0) {
            break;
        }
        for(int i=0; i<n; i++) {
            cin>>k[i];
        }
        cin.ignore();
        ansNum = 0;

	// Output
	now = 0;
        for(int i=0; i<102; i++) {
            scanf("%c", &station[i]);
            if(station[i] == '\n') {
                break;
            }
            ansNum++;

	    int tmp;
	    tmp = alpha.find(station[i]);
	    ans[i] = alpha[(tmp-k[now]+52)%52];
	    now++;
            if(now == n) {
                now = 0;
            }
        }

        for(int i=0; i<ansNum; i++) {
            cout<<ans[i];
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
#define ll long long
#define INF 100000000000000000LL
using namespace std;
signed main() {
  int n, m, cun, ans = 0;
  string s[10000], t[10000];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }

  for (int i = 0; i < n; i++) {
    cun = 0;
    for (int j = 0; j < n; j++) {
      if (s[i] == s[j]) {
        cun++;
      }
    }

    for (int j = 0; j <m; j++) {
      if (s[i] == t[j]) {
        cun--;
      }
    }
    ans = max(ans, cun);
  }

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

#include <stdlib.h>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
using namespace std;
using ll = long long;

int main(){
  int N,M;
  cin >> N;
  map<string,int> blue;
  string s;
  for(int i=0;i<N;++i){
    cin >> s;
    if(blue.count(s)) blue.at(s)++;
    else blue[s]=1;
  }
  cin >> M;
  map<string,int> red;
  for(int i=0;i<M;++i){
    cin >> s;
    if(red.count(s)) red.at(s)++;
    else red[s]=1;
  }

  int ans=-200;
  for(auto p: blue){
    auto word=p.first;
    auto value=p.second;
    if(red.count(word)){
      ans=max(ans,value-red.at(word));
    }
    else ans=max(ans,value);
  }

  if(ans<0) cout << 0 << endl;
  else cout << ans << endl;

  return 0;
}


#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

int main(){
    string s;
    cin>>s;
    vector<ll> Mnext(26,0);
    vector<vector<ll>> back(s.size()+1,Mnext);
    for(int i=0;i<s.size();i++){
        back[i+1]=Mnext;
        Mnext[s[i]-'a']=i+1;
    }
    for(int i=0;i<26;i++){Mnext[i]=s.size();}
    vector<vector<ll>> next(s.size()+1,Mnext);
    for(ll i=s.size()-1;i>=0;i--){
        next[i]=Mnext;
        Mnext[s[i]-'a']=i;
    }
    ll k;
    cin>>k;
    ll ans=0;
    vector<vector<vector<ll>>> dp(s.size()+1,vector<vector<ll>>(s.size()+1,vector<ll>(k+1,0)));
    for(int i=1;i<=s.size();i++){
        for(int t=1;t<=s.size();t++){
            for(int h=0;h<=k;h++){
                dp[i][t][h]=max(dp[i-1][t][h],dp[i][t-1][h]);
                if(s[i-1]==s[s.size()-t]){
                    dp[i][t][h]=max(dp[i][t][h],dp[i-1][t-1][h]+1);
                    for(int w=0;w<26;w++){
                        dp[i][t][h]=max(dp[i][t][h],dp[back[i][w]][s.size()-next[s.size()-t][w]][h]+1);
                    }
                }
                if(h!=0){
                    dp[i][t][h]=max(dp[i][t][h],dp[i-1][t-1][h-1]+1);
                }
            }
        }
    }
    for(int i=0;i<=s.size();i++){
        for(int t=0;t<=s.size();t++){
            for(int h=0;h<=k;h++){
                if(i+t<=s.size()){ans=max(ans,dp[i][t][h]*2);}
                if(i+t==s.size()+1){ans=max(ans,dp[i][t][h]*2-1);}
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define inf 998244353
#define LINF 1000000000000000007LL
#define ll long long
using namespace std;
int dp[310][310][310];
string s;
int calc( int lef, int rig, int k){
	if( dp[lef][rig][k] != 0){
		return dp[lef][rig][k];
	}
	if( lef == rig ){
		return 1;
	}
	if( lef > rig ){
		return 0;
	}
	else{
		if( s[lef] == s[rig] ){
			dp[lef][rig][k] = calc( lef+1, rig -1, k ) + 2;
			return dp[lef][rig][k];
		}
		else{
			int c1 = -1;
			if( k > 0 ) c1 = calc( lef+1, rig-1, k-1) + 2;
			int c2 = calc( lef+1, rig, k);
			int c3 = calc( lef, rig-1, k);
			dp[lef][rig][k] = max( c1, max(c2,c3) );
			return dp[lef][rig][k];
		}
	}
}
signed main(){
	int k;
	cin >> s;
	cin >> k;
	int n = s.size();
	int ans = calc( 0, n-1, k );
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

template<class V> class segtree {
private:
    int n;
    vector<V> node, lazy;
    vector<bool> lazyFlag;

public:
    segtree(vector<V> v) {
        int sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1);
        lazy.resize(2*n-1, 0);
        lazyFlag.resize(2*n-1,false);
        rep(i,sz){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = min(node[i*2+1],node[i*2+2]);
        }
    }
    void eval(int k, int l, int r) {
        if(lazyFlag[k]) {
            node[k] = lazy[k];
            if(r - l > 1) {
                lazy[k*2+1] = lazy[k*2+2] = lazy[k];
                lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
            }
            lazyFlag[k] = false;
        }
    }
    void range(int a, int b, V x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return;
        }
        if(a <= l && r <= b) {
            lazy[k] = x;
            lazyFlag[k] = true;
            eval(k, l, r);
        }
        else {
            range(a, b, x, 2*k+1, l, (l+r)/2);
            range(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = min(node[2*k+1],node[2*k+2]);
        }
    }
    V query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return INT_MAX;
        }
        if(a <= l && r <= b){
            return node[k];
        }
        V vl = query(a, b, 2*k+1, l, (l+r)/2);
        V vr = query(a, b, 2*k+2, (l+r)/2, r);
        return min(vl,vr);
    }
};

int main()
{
    int n,q;
    cin >> n >> q;
    segtree<int> sg(vector<int>(n,INT_MAX));
    rep(i,q){
        int k;
        cin >> k;
        if(k){
            int x;
            cin >> x;
            cout << sg.query(x,x+1) << "\n";
        }else{
            int x,y,z;
            cin >> x >> y >> z;
            sg.range(x,y+1,z);
        }
    }
}

#include<cstdio>
#include<algorithm>

using int64=long long;
using uchar=unsigned char;

constexpr int maxn(200000);
constexpr int64 inf(1e17);

template<class _Tp>
	inline bool bchkMin(_Tp&x,const _Tp&y)
		{return(x>y)&&(x=y,1);}

namespace IOManager{
	constexpr int FILESZ(131072);
	char buf[FILESZ];
	const char*ibuf=buf,*tbuf=buf;

	struct IOManager{
		inline char gc()
			{return(ibuf==tbuf)&&(tbuf=(ibuf=buf)+fread(buf,1,FILESZ,stdin),ibuf==tbuf)?EOF:*ibuf++;}

		template<class _Tp>
			inline operator _Tp(){
				_Tp s=0u;char c=gc();
				for(;c<48;c=gc());
				for(;c>47;c=gc())
					s=(_Tp)(s*10u+c-48u);
				return s;
			}
	};
}IOManager::IOManager io;

int sz[maxn+1];
int64 szl[maxn+1],szr[maxn+1];

struct Edge{
	int s,t;int64 w;

	inline void init(const int&is,const int&it,const int64&iw)
		{s=is;t=it;w=iw;}

	inline bool operator<(const Edge&rhs)
		const{return w<rhs.w;}
}e[maxn*30],*cur=e;

void calc(const int&l,const int&r){
	if(l==r)
		return;
	const int m=(l+r)>>1;

	int lmp,rmp;
	int64 lmv=inf,rmv=inf;

	for(int i=l;i<=m;++i)
		if(bchkMin(lmv,szl[i]))
			lmp=i;
	for(int i=m+1;i<=r;++i)
		if(bchkMin(rmv,szr[i]))
			rmp=i;

	for(int i=l;i<=m;++i)
		(++cur)->init(i,rmp,szl[i]+rmv);
	for(int i=m+1;i<=r;++i)
		(++cur)->init(lmp,i,lmv+szr[i]);

	calc(l,m);calc(m+1,r);
}

int ufs[maxn+1];

inline int anc(const int&v)
	{return v==ufs[v]?v:ufs[v]=anc(ufs[v]);}

int main(){
	const int n=io,D=io;
	int64 t=0;
	for(int i=1;i<=n;++i,t+=D)
		ufs[i]=i,
		sz[i]=io,
		szl[i]=sz[i]-t,
		szr[i]=sz[i]+t;

	calc(1,n);
	std::sort(e+1,cur+1);
	int64 ans=0;
	for(Edge*i=e+1;i<=cur;++i)
		if(anc(i->s)!=anc(i->t))
			ans+=i->w,ufs[ufs[i->s]]=ufs[i->t];
	printf("%lld",ans);

	return 0;
}


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

#define int long long
struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;
//const int MOD = 1e9 + 7;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b) - 1)/(int)(b))

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

    vector<string> board(8,string(8,'.'));
    vector<intpair> queen(k);

    rep(i,k){
        cin >> queen[i].first >> queen[i].second;
    }

    int a[8];
    rep(i,8) a[i] = i;

    do{
        bool ok = true;
        for(auto p : queen){
            ok = false;
            rep(i,8){
                if(p.first == i && p.second == a[i]) ok = true;
            }
            if(!ok) break;
        }

        rep(i,8)rep(j,8){
            if(i==j) continue;
            if(i+a[i] == j+a[j]) ok = false;
            if(i-a[i] == j-a[j]) ok = false;
        }

        if(ok){
            rep(i,8){
                board[i][a[i]] = 'Q';
            }
            break;
        }
    }while(next_permutation(a, a+8));

    for(string s : board){
        cout << s << endl;
    }
}

#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repr(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repr(i,0,n)
#define reprrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) reprrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFL 9e18
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<Pi> > vvPi;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi> > pqlP;
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
template <class T>
using vec = vector<T>;
template<class T>
using pql = priority_queue<T, vector<T>, greater<T>>;
string debug_show(Pi a) {
	return "(" + to_string(a.first) + "," + to_string(a.second) + ")";
}

template<class T>
struct augEdge {
	T from, to;
	int cost;
	bool operator<(augEdge e) { return cost < e.cost; }
	bool operator>(augEdge e) { return cost > e.cost; }
};
template<class T>
using augGraph = vector<augEdge<T>>;

class Lowlink {
public:
	vi ord, low;
	vb vis;
	vi articulation;
	vPi bridge;

	Lowlink(const Graph &G) {
		ord = vi(G.size());
		low = vi(G.size());
		vis = vb(G.size(), false);
		int k = 0;
		for (int i = 0; i < G.size(); i++) {
			if (!vis[i]) dfs(G, i, -1, k);
		}
	}

	void dfs(const Graph &G, int v, int p, int &k)
	{
		vis[v] = true;

		ord[v] = k++;
		low[v] = ord[v];

		bool isArticulation = false;
		int ct = 0;

		for (int i = 0; i < G[v].size(); i++) {
			if (!vis[G[v][i].to]) {
				ct++;
				dfs(G, G[v][i].to, v, k);
				low[v] = min(low[v], low[G[v][i].to]);
				if (p != -1 && ord[v] <= low[G[v][i].to]) isArticulation = true;
				if (ord[v] < low[G[v][i].to]) bridge.push_back(make_pair(min(v, G[v][i].to), max(v, G[v][i].to)));
			}
			else if (G[v][i].to != p) {
				low[v] = min(low[v], ord[G[v][i].to]);
			}
		}

		if (p == -1 && ct > 1) isArticulation = true;
		if (isArticulation) articulation.push_back(v);
	}
};

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

	int N, M;
	cin >> N >> M;
	Graph G(N);
	rep(i, M) {
		int a, b;
		cin >> a >> b;
		//a--; b--;
		G[a].push_back(Edge{ a,b,1 });
		G[b].push_back(Edge{ b,a,1 });
	}

	Lowlink lowlink(G);
	sort(all(lowlink.articulation));

	for (auto a : lowlink.articulation)cout << a << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    std::vector<long long> trips;
    int ans=0;
    cin>>n;
    trips.resize(n);
    for(auto &x:trips)
        cin>>x;
    n = trips.size();
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n;k++){
                if(trips[i]!=trips[j] && trips[i]!=trips[k] && trips[j]!=trips[k])
                    if(trips[i]+trips[j]>trips[k] && trips[i]+trips[k]>trips[j] && trips[j]+trips[k]>trips[i])
                        ans++;
            }
        }
    }
    cout << ans;
    return 0;
}

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define  fi0 for(ll i=0;i<n;i++)
string s;
string strd(int n){
    string x;
    while(n){
        int d=n%10;
        x+=(d+'0');
        n/=10;
    }
    reverse(x.begin(),x.end());
    return x;
}
void digit()
{
    cin>>s;
    ll sum=0;
    for(int i=0;i<s.size();i++){
        char a=(char)s[i];
        int d=(int) a-'0';
        sum+=d;
    }
    if(sum%9==0)cout<<"Yes\n";
    else cout<<"No\n";
}
int main()
{
   ll n;
   cin>>n;
   ll cnt=0;
   ll a[n];
  fi0{
    cin>>a[i];
   }

   sort(a,a+n);
   ///fi0 cout<<a[i]<<' ';
   for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
        for(int k=j+1;k<n;k++){
             if(a[i]!=a[j]&&a[j]!=a[k]){
                vector<ll>xxx;
                xxx.push_back(a[i]);
                xxx.push_back(a[j]);
                xxx.push_back(a[k]);
                sort(xxx.begin(),xxx.end());
                if(xxx[0]+xxx[1]>xxx[2])cnt++;
             }
        }
    }
   }
   cout<<cnt<<endl;
}


#include <iostream>
#include <map>
#include <set>

#define HW 160
using namespace std;

/*struct rect
{
	int h;
	int w;
	int tai;
};*/

int main(int argc, char const *argv[])
{
	int h,w;

	map<int,set<pair<int,int> > > mp;

	for(int i1=1;i1<HW;i1++){
		for(int i2=i1+1;i2<HW;i2++){
			mp[i1*i1+i2*i2].insert(make_pair(i1,i2));
		}
	}

	while(1){
		cin>>h>>w;
		if(h+w==0) break;

		map<int,set<pair<int,int> > >::iterator it=mp.lower_bound(h*h+w*w);
		if(it!=mp.end()) {
			//cout<<(*it).first<<"a";
			set<pair<int,int> >::iterator is=(*it).second.upper_bound(make_pair(h,w));

			if(is!=(*it).second.end()) {
				cout<<(*is).first<<" "<<(*is).second<<endl;
				//cout<<(*is).second<<"b";
				/*if((is++)==(*it).second.end()){
					//cout<<"c";
					it++;
					is=(*it).second.begin();
					cout<<(*is).first<<" "<<(*is).second<<endl;
				}
				else */
			}
			else {
				it++;
				is=(*it).second.begin();
				cout<<(*is).first<<" "<<(*is).second<<endl;
			}
		}

	}
	return 0;
}

#include <cstdio>

using namespace std;

#define min(a,b) (((a) > (b))?(b):(a))
#define INF 1000001

int dp[100][100];
int V;

int warshall_floyd(int k) {
    int i;

    for (i=0;i<V;i++) {
        int j;
        for (j=0;j<V;j++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
    }
}

int main() {
    while (1) {
        int i;
        int n, k;

        scanf("%d %d", &n, &k);
        if (n == 0 && k == 0) return 0;

        V = n;
        for (i=0;i<n;i++) {
            int j;
            for (j=0;j<n;j++) {
                if (i == j) {
                    dp[i][j] = 0;
                } else {
                    dp[i][j] = INF;
                }
            }
        }

        for (i=0;i<k;i++) {
            int c, d;
            int query;

            scanf("%d", &query);
            if (query) {
                int e;

                scanf("%d%d%d", &c, &d, &e);
                c--;
                d--;
                if (dp[c][d] > e) dp[c][d] = e;
                if (dp[d][c] > e) dp[d][c] = e;
                warshall_floyd(c);
                warshall_floyd(d);
            } else {
                int c, d;

                scanf("%d%d", &c, &d);
                c--;
                d--;
                if (dp[c][d] == INF) printf("%d\n", -1);
                else printf("%d\n", dp[c][d]);
            }
        }
    }
}


#include<bits/stdc++.h>
#define dbug printf("I am here\n");
#define Fast ios_base::sync_with_stdio(false); cin.tie(0);
#define vs              v.size()
#define sot(v)        sort(v.begin(),v.end())
#define rev(v)        reverse(v.begin(),v.end())
#define ii                pair<int,int>
#define ll                long long
#define ull              unsigned long long
#define pb              push_back
#define mpp          make_pair
#define Okay         0
#define pi               3.14159

static ll int inf = 1e9;
const int cont = 10e5+10;
using namespace std;
bool ar[10];
int n,m;

bool wrong(int n){
    while(n>0){
        if(ar[n%10])return true;
        n/=10;
    }
    return false;
}

void solve(){
    int m,n;
    cin>>n>>m;
    for(int i=0; i<m;i++){
        int a;
        cin>>a;
        ar[a] = 1;
    }

   while(wrong(n)){
    n++;
   }
   cout<<n<<endl;
}

int main(){

solve();

	return Okay;
}

#include <bits/stdc++.h>
//#define DEBUG
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <class T>
void print_vect(std::vector<T> v){
    for(int i = 0 ; i < v .size(); i++) cout << v[i] << " ";
    cout << endl;
}

void solve(){
    int n, k;
    cin >> n >> k;
    set<int> d;
    for(int i = 0 ; i < 10; i++){
        d.insert(i);
    }
    for(int i = 0 ; i < k; i++){
        int x;
        cin >>  x;
        d.erase(x);
    }
    int p[5];
    for(int i = 0 ; i < 5 ; i++){
        p[i] = 0;
    }
    int j;
    for(j = 4 ; j >= 0; j--){
        p[j] = n % 10;
        n = n / 10;
        if( n == 0 ) break;
    }
    int i;
    for(i = j ; i < 5 ; i++){
        if( d.find(p[i]) == d.end()){
            break;
        }else{
            continue;
        }
    }
    int smallest_digit = *d.lower_bound(0);
    auto it = d.lower_bound(p[i]);
    if ( it == d.end()){
        int update_index = i - 1;
        while ( d.lower_bound(p[update_index] + 1) == d.end()){
            update_index--;
        }
        p[update_index] = *d.lower_bound(p[update_index] + 1);
        int ans = 0;
        for(int k = 0 ; k < 5 ; k++){
            if( k <= update_index){
                ans = ans * 10 + p[k];
            }else{
                ans = ans * 10 + smallest_digit;
            }
        }
        cout << ans << endl;
        return;
    }

    p[i] = *it;

    int ans = 0;
    for( int k = 0 ; k < 5 ; k++){
        if( k <= i ){
            ans = ans * 10 + p[k];
        }else{
            ans = ans * 10 + smallest_digit;
        }
    }

    cout << ans << endl;
}

int main()
{
    //making data IO Fast
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    /****************************/

    #ifdef DEBUG
    int TEST_CASES = 2;
    while(TEST_CASES--){
    #endif // DEBBUG

    solve();

    #ifdef DEBUG
    }
    #endif // DEBUG

    return 0;
}


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

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

using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using vi=vector<int>;
using vii=vector<vector<int>>;

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

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

int main(){
    int N, M; cin >> N >> M;
    vi L(M), R(M);
    for(int i=0; i<M; i++){
        cin >> L[i] >> R[i];
    }

    int l=L[0], r=R[0];
    bool flag=0;
    for(int i=1; i<M; i++){
        if(max(l, L[i]) <= min(r, R[i])){
            l=max(l, L[i]);
            r=min(r, R[i]);
        }
        else flag=true;
    }
    if(flag) cout << 0 << endl;
    else cout << r-l+1 << endl;
    return 0;
}

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

int main() {
  int N, M;
  cin>>N>>M;
  int arr[N+1];
  for(int i=0;i<=N;++i) arr[i] = 0;
  int L, R;
  for(int i=0;i<M;++i) {
    cin>>L>>R;
    L--;
    arr[L]++;
    arr[R]--;
  }
  for(int i=1;i<=N;++i) arr[i] += arr[i-1];
  int ans = 0;
  for(int i=0;i<=N;++i) {
    if(arr[i]==M) ans++;
  }
  cout<<ans<<endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

int main(){

    int t; cin>>t;
    REP(kai,t){
        int out=0;
        int point=0;
        bool rui[3]={false,false,false};
        while(true){
            string s; cin>>s;
            if(s=="OUT"){
                out++;
                if(out==3)goto nextf;
            }else if(s=="HOMERUN"){
                point++;
                REP(j,3)if(rui[j])point++;
                REP(j,3)rui[j]=false;
            }else{
                if(rui[2])point++;
                rui[2]=rui[1];
                rui[1]=rui[0];
                rui[0]=true;
            }
        }
    nextf:;
        cout<<point<<endl;
    }

    return 0;
}


#include <iostream>
#define INF (int)1e09

int main(void) {
    int m;
    int n_min;
    int n_max;
    int gap;
    int pass_num;

    int pass;
    int fail;

    while (1) {
        std::cin >> m >> n_min >> n_max;
        pass = INF;
        fail = INF;
        gap = 0;

        if (m == 0) {
           break;
        }

        for (int i = 0; i < n_min; i++) {
            std::cin >> pass;
        }
        for (int i = n_min; i <= n_max; i++) {
            std::cin >> fail;
            if (pass - fail >= gap) {
                gap =  pass -fail;
                pass_num = i;
            }
            pass = fail;
        }
        for (int i = n_max + 1; i < m; i++) {
            std::cin >> pass;
        }
        std::cout << pass_num << std::endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, a, b;
    while (cin >> n >> a >> b, n || a || b) {
        vector<ll> s(n); cin >> s;
        P ans(-linf, -linf);
        rep(i, n-1) {
            if (a <= i+1 && i+1 <= b) {
                chmax(ans, P(s[i] - s[i+1], i+1));
            }
        }
        cout << ans.second << endl;
    }
}


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

using namespace std;

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

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

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

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

  return 0;
}

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
struct point{int x;int y;};
int i,j,k,count1=0,count2=0;

int main(){
    int a,b,c;
    cin>>a>>b>>c;
    if(c>min(a,b)&&c<max(a,b))cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}


// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <stdio.h>
#include <string.h>

int main()
{
int n;
int runner;
int score;
int out;
char nyuuryoku[114514];
    scanf("%d",&n);
        for(int i=0;i<n;i++){
            runner=0;
            score=0;
            out=0;
            while(out<3){
                scanf("%s",nyuuryoku);
                    if(strcmp(nyuuryoku,"HIT")==0){
                        runner=runner+1;
                    }
                    else if(strcmp(nyuuryoku,"HOMERUN")==0){
                        score=score+runner+1;
                        runner=0;
                    }
                    else if(strcmp(nyuuryoku,"OUT")==0){
                        if(out==2&&runner>3){
                            score=score+runner-3;
                        }

                           out=out+1;
                    }
            }
            printf("%d\n",score);
        }

}
// GCC reference:
//   https://gcc.gnu.org/

// C++ language references:
//   https://msdn.microsoft.com/library/3bstk3k5.aspx
//   http://www.cplusplus.com/
//   https://isocpp.org/
//   http://www.open-std.org/jtc1/sc22/wg21/

// Boost libraries references:
//   http://www.boost.org/doc/

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(){
  int n;
  string event;
  int base=0;
  int inning=0;
  cin>>n;
  vector<int> point(n);
  int outnum=0;
  while(inning<n){
    cin>>event;
    if(event=="HIT"){
      base<<=1;
      base|=1;
      if(base&8){
        point[inning]++;
        base^=8;
      }
    }else if(event=="HOMERUN"){
      point[inning]+=1;
      for(int i=0;i<3;i++)
        point[inning]+=(base>>i)&1;
      base=0;
    }else if(event=="OUT"){
      outnum++;
      if(outnum>2){
        outnum=0;
        inning++;
        base=0;
      }
    }
  }
  for(int p:point)
    cout<<p<<endl;
}