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

short N;
ll ans = 0;
string L, R, b, r;
map<pair<string, string>, int> M;

void aff(vector<bool>&vec, short i) {
	if (i == N) {
		for (short j = 0;j < N;j++) {
			if (vec[j])b.push_back(L[j]);
			else r.push_back(L[j]);
		}
		b += 'B';
		r += 'R';
		if (M.find({ b,r }) != M.end()) {
			M.at({ b,r })++;
		}
		else {
			M.insert({ {b,r},1 });
		}
		b = "";
		r = "";
		return;
	}
	aff(vec, i + 1);
	vec[i] = 1;
	aff(vec, i + 1);
	vec[i] = 0;
	return;
}

void aff2(vector<bool>&vec, short i) {
	if (i == N) {
		for (short j = 0;j < N;j++) {
			if (vec[j])b.push_back(R[j]);
			else r.push_back(R[j]);
		}
		b += 'B';
		r += 'R';
		if (M.find({ b, r }) != M.end()) {
			ans += (ll)M.at({ b,r });
			//cout << b << " " << r << endl;
			//cout << ans << endl;
		}
		b = "";
		r = "";
		return;
	}
	aff2(vec, i + 1);
	vec[i] = 1;
	aff2(vec, i + 1);
	vec[i] = 0;
	return;
}

int main(){
	cin >> N;
	string S;
	cin >> S;
	for (short i = 0;i < N;i++) {
		L += S[i];
	}
	for (short i = 0;i < N;i++) {
		R += S[i + N];
	}
	reverse(R.begin(), R.end());
	vector<bool> vec(N, false);
	aff(vec, 0);
	aff2(vec, 0);
	cout << ans << endl;
}

#include "bits/stdc++.h"
using namespace std;
//template <typename... T> void read(T& ... t){ ((cin >> t), ...); }
//template <typename... T> void write(T ... t){ ((cout << t), ...); }
//#define int long long
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define size(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define ln '\n'
#define space ' '
using LL = long long;
using pii = pair<int,int>;
const int INF = 2e9;
const int NN = 2e3;
const int MOD = 998244353;
namespace Mm{
	inline int mul(const LL &x, const LL &y) { return (x*y)%MOD; }
	inline int add(const LL &x, const LL &y) { return (x+y)%MOD; }
	inline int power(const LL &x, const LL &n){ if(n == 0) return 1; int y=power(x, n>>1); y=mul(y,y); return (n&1)?mul(y,x):y; }
	inline int inv(const LL &x){ return power(x, MOD-2); }
	// int _fact[2*NN] = {0};
	// inline int fact(const int &x){ return (x==0 ? 1 : (_fact[x]!=0 ? _fact[x] : _fact[x]=mul(x, fact(x-1)))); }
	// inline int cnk(const int &n, const int &k){ return (n<k)?0:mul(fact(n),inv(mul(fact(k),fact(n-k)))); }
	int _cnk[2*NN+1][2*NN+1] = {0};
	inline int cnk(const int &n, const int &k){ return (n<k)?0:(k==0)?1:(_cnk[n][k]!=0?_cnk[n][k]:_cnk[n][k]=add(cnk(n-1,k),cnk(n-1,k-1))); }
} using namespace Mm;
int n,k;
int dp[NN+1][NN+1];
int nbPairs[2*NN+1];

#define read(a,b) cin>>a>>b
#define write(a,b) cout<<a<<b

int32_t main(){ FAST;
	read(k,n);
	dp[0][0] = 1;
	for(int i=1; i<=NN; ++i){ // #pairs
		int prefSum = 0;
		for(int j=0; j<=NN; ++j){ // #items to distribute on pairs
			dp[i][j] = add(dp[i-1][j], add(prefSum, prefSum)); // k € [1;j]
			prefSum = add(prefSum, dp[i-1][j]);
		}
	}

	for(int i=1; i<=k; ++i) for(int j=i+1; j<=k; ++j) ++nbPairs[i+j];

	for(int i=2; i<=2*k; ++i){
		int ans = 0;
		int others = k - 2*nbPairs[i];

		if(i&1){
			if(others == 0){
				ans = dp[nbPairs[i]][n];
				write(ans, ln);
				continue;
			}

			for(int giveOthers=0; giveOthers<=n; ++giveOthers){
				int givePairs = n-giveOthers;
				ans = add(ans, mul(dp[nbPairs[i]][givePairs], cnk(others+giveOthers-1, giveOthers)));
			}
		}
		else{
			--others;
			if(others == 0){
				ans = add(dp[nbPairs[i]][n], dp[nbPairs[i]][n-1]);
				write(ans, ln);
				continue;
			}

			for(int giveOthers=0; giveOthers<=n; ++giveOthers){
				int givePairs = n-giveOthers;
				ans = add(ans, mul(dp[nbPairs[i]][givePairs], cnk(others+giveOthers-1, giveOthers)));
			}
			for(int giveOthers=0; giveOthers<=n-1; ++giveOthers){
				int givePairs = n-1-giveOthers;
				ans = add(ans, mul(dp[nbPairs[i]][givePairs], cnk(others+giveOthers-1, giveOthers)));
			}
		}

		write(ans, ln);
	}
}


#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = (n-1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<62;
const int IINF = 1000000000;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using Graph = vector<vector<int>>;

typedef pair<int, int> P;

string ABCD;

bool isEnd = false;

void dfs(int total, string signs, int index){
    if (isEnd) return;
    if(index == 4){
        if (total == 7){
            string ans = "";
            ans += ABCD[0];
            for (int i = 0; i < 3; i++){
                ans += signs[i];
                ans += ABCD[i+1];
            }
            cout << ans+"=7" << endl;
            isEnd = true;
        }
        return;
    }
    dfs(total+(ABCD[index]-'0'), signs+"+", index+1);
    dfs(total-(ABCD[index]-'0'), signs+"-", index+1);

}

int main(){
    cin >> ABCD;

    dfs(ABCD[0]-'0', "", 1);
}

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>
#include <assert.h>
#include <cstring>
#include <set>

using namespace std;

typedef long long ll;
int arr[200001];
int main()
{
    int N;
    cin >> N;
    int temp;
    int i;
    int m = 0;
    ll sum = 0;
    for(i = 0; i < N; i++)
    {
        cin >> temp;
        arr[i] = temp;
        m = max(m,arr[i]);
        sum += m-arr[i];
    }

    for(i = N-1; i >= 0; i--)
    {

    }

    cout << sum;
}


#include<iostream>
using namespace std;

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

  if (a > 8 || b > 8) cout << ":(";
  else cout << "Yay!";

  return 0;
}

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

int main(){
  string S;
  int W;
  cin >> S >> W;
  for(int i=0;i<S.size();i+=W){
    cout << S.at(i);
  }
  cout << endl;
}


#include<iostream>
#define INF 2 << 23
using namespace std;
int main(){
  int n,m;
  while(cin >> n >> m){
    if(n==0&&m==0) break;
    int cost[m+1][m+1];
    int time[m+1][m+1];

    for(int i=0;i<=m;i++)
      for(int j=0;j<=m;j++){
        if(i==j)
          cost[i][j] = time[i][j] = 0;
        else
          cost[i][j] = time[i][j] = INF;
      }

    for(int i=0;i<n;i++){
      int a,b;
      cin >> a >> b;
      cin >> cost[a][b] >> time[a][b];
      cost[b][a] = cost[a][b]; time[b][a] = time[a][b];
    }

    for (int i = 1; i <= m; i++)      // ?????±????????????
      for (int j = 1; j <= m; j++)    // ????§???????
        for (int k = 1; k <= m; k++){  // ??????
          cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
          time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
        }

    int k;
    cin >> k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin >> p >> q >> r;
      switch(r){
      case 0: cout << cost[p][q] << endl; break;
      case 1: cout << time[p][q] << endl; break;
      default: break;
      }
    }
  }
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-9

const int MAX_N = 10;
int n;
int a[MAX_N+1][MAX_N+1];

int main()
{
  while((cin >> n) && n){
    rep(i, n+1) rep(j, n+1) a[i][j] = 0;
    rep(i, n)   rep(j, n)   cin >> a[i][j];

    rep(i, n)   rep(j, n)   a[n][j] += a[i][j];
    rep(i, n+1) rep(j, n)   a[i][n] += a[i][j];

    rep(i, n+1) rep(j, n+1) printf("%5d%s", a[i][j], (j==n ? "\n" : ""));
  }

  return 0;
}


#include <bits/stdc++.h>
#define simplify ios_base::sync_with_stdio(0);cin.tie(0);
#define int long long
#define endl "\n"
using namespace std;

const int mod=998244353;
const int N=2e5+5;

int fact[N];
int invFact[N];

int inverseMod(int x,int mod);
void precompute(){
    fact[0]=fact[1]=1;

    for(int i=2;i<N;i++){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
    }
    invFact[N-1]=inverseMod(fact[N-1],mod);

    for(int i=N-2;i>=0;i--){
        invFact[i]=invFact[i+1]*(i+1);
        invFact[i]%=mod;
    }
}
int NCR(int n,int r){

    if(r>n)return 0;
    int num=fact[n];
    num*=invFact[r];
    num%=mod;
    num*=invFact[n-r];
    num%=mod;
    return num;
}

int power(int x,int n,int mod){
//(x^n)%mod
    int result=1;
    while(n){
        if(n%2)
            result=(result*x)%mod;
        x=(x*x)%mod;
        n/=2;
    }
    return result;
}

int inverseMod(int x,int mod){

return power(x,mod-2,mod);
}

int32_t main(){

    simplify
    int n,m,k;
    cin>>n>>m>>k;
    precompute();
    int ans=0;
    int other;
    for(int i=0;i<=k;i++){
        other=m*power(m-1,n-1-i,mod);
        other%=mod;
        other=other*NCR(n-1,i);
        other%=mod;
        ans+=other;
        ans%=mod;
    }
    cout<<ans;

    return 0;
}

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

ll modpow(ll a, ll n, ll mod){
    ll res=1;
    while(n>0){
        if(n&1) res = res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}

ll modinv(ll a, ll mod){
    return modpow(a, mod-2, mod);
}

int main() {
    ll N,M,K,ans;
    cin>>N>>M>>K;
    ll mod = 998244353;
    ll kakuk = M*modpow(M-1,N-1,mod)%mod;
    ans = kakuk;
    ll gyakuM = modinv(M-1,mod);
    rep(i,K){
        kakuk = (kakuk * (N-i-1))%mod;
        kakuk = (kakuk * modinv(i+1,mod))%mod;
        kakuk = (kakuk * gyakuM)%mod;
        ans += kakuk;
        ans = ans%mod;
    }
    if(M==1&&N-1==K) cout << 1 << endl;
    else cout << ans << endl;
}


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

int main() {
    ll N, x;
    cin >> N >> x;
    vector<ll> a(N);
    rep(i,N) cin >> a.at(i);
    ll ans = 0;
    rep(i,N) {
        if (i == 0) continue;
        if(a.at(i) + a.at(i-1) > x) {
            ll diff = a.at(i) + a.at(i-1) - x;
            ans += diff;
            if (diff <= a.at(i)) a.at(i) -= diff;
            else a.at(i) = 0;
        }
    }
    cout << ans << endl;
}

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

typedef long long ll;
const int mod=1e9+7;

int main(){
    int n,x,a;
    while(~scanf("%d%d%d",&n,&x,&a)){
        ll ans=0;
        if(a>x)
            ans+=a-x,a=x;
        while(--n){
            int b;
            scanf("%d",&b);
            if(a+b>x)
                ans+=a+b-x,a=x-a;
            else
                a=b;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<utility>
#include<cmath>
#include<assert.h>
#include<set>
#include<map>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

map<pii, vector<pair<pii, int>>> G;
map<pii, int> d;
set<int> company[100100];//company[i]:=駅iを通る会社
int N, M;

void dijkstra() {
    priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> que;
    d[pii(0, -1)] = 0;
    que.push(make_pair(0, pii(0, -1)));
    while (!que.empty()) {
        pair<int, pii> p = que.top();
        que.pop();
        pii v = p.second;
        if (d.count(v) && d[v] < p.first)continue;

        rep(i, G[v].size()) {
            pair<pii, int> edge = G[v][i];
            if (!d.count(edge.first) || d[edge.first] > d[v] + edge.second) {
                d[edge.first] = d[v] + edge.second;
                que.push(make_pair(d[edge.first], edge.first));
            }
        }
    }
}

signed main() {
    cin >> N >> M;
    rep(i, M) {
        int p, q, c;
        cin >> p >> q >> c;
        p--;
        q--;
        G[pii(p, c)].push_back(make_pair(pii(q, c), 0));
        G[pii(q, c)].push_back(make_pair(pii(p, c), 0));
        company[p].insert(c);
        company[q].insert(c);
    }
    rep(i, N) {
        for (int c:company[i]) {
            G[pii(i, c)].push_back(make_pair(pii(i, -1), 0));
            G[pii(i, -1)].push_back(make_pair(pii(i, c), 1));
        }
    }

    dijkstra();
    if (d.count(pii(N - 1, -1)))
        cout << d[pii(N - 1, -1)] << endl;
    else cout << -1 << endl;
    return 0;
}

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

#define INF 1<<30-1

using namespace std;

int n;
int c[101][101];
int t[101][101];
int flg;

int washa(int st,int en,int va){
	if(flg==1){
		for(int k=1;k<=n;k++){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(c[i][j]>c[i][k]+c[k][j]){
						c[i][j]=c[i][k]+c[k][j];
					}
					if(t[i][j]>t[i][k]+t[k][j]){
						t[i][j]=t[i][k]+t[k][j];
					}
				}
			}
		}
		flg=0;
	}
	if(va==0){
		return c[st][en];
	}else{
		return t[st][en];
	}
}

int main(){
	while(1){
		int m;
		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				c[i][j]=t[i][j]=INF;
			}
		}
		flg=1;

		scanf("%d%d",&m,&n);
		if(m==0 && n==0){
			break;
		}
		for(int i=0;i<m;i++){
			int a,z,s,x;
			scanf("%d%d%d%d",&a,&z,&s,&x);
			c[a][z]=c[z][a]=s;
			t[a][z]=t[z][a]=x;
		}

		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int a,z,s;
			scanf("%d%d%d",&a,&z,&s);

			printf("%d\n",washa(a,z,s));
		}
	}
}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++i)
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; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
#define fs first
#define sc second
// 多倍長
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using boost::rational;
using boost::multiprecision::cpp_int;

const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}

// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;

vector<string> split(const string &s, char delim) {
    vector<string> elems;
    string item;
    for (char ch: s) {
        if (ch == delim) {
            if (!item.empty())
                elems.push_back(item);
            item.clear();
        }
        else {
            item += ch;
        }
    }
    if (!item.empty())
        elems.push_back(item);
    return elems;
}

int ng[500];
int dp[500];

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    ll n,m;
    cin >> n >> m;
    vector<ll> a(n);
    vector<ll> s(n + 1);
    map<ll, ll> mp;
    rep(i, n) {
        cin >> a[i];
        s[i + 1] = s[i] + a[i];
    }
    rep(i, n + 1) mp[s[i] % m]++;
    ll ans = 0;
    for(auto num : mp){
        ans += num.sc * (num.sc - 1) / 2;
    }
    print(ans);

}

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

int f(int x,int y){
	if(y<10)return x*10+y;
	return x*100+y;
}

int main(){
while(1){
	int n,k,c[100],ans=1;
	vector<int> sum;
	scanf("%d%d",&n,&k);
	if(n==0&&k==0)break;
	for(int i=0;i<n;i++)scanf("%d",&c[i]);
	int a=0;
	for(int i=0;i<n;i++){
		int a0=c[i];
		for(int j=0;j<n;j++){
			if(j!=i){
				int a1=f(a0,c[j]);
				if(k==2)sum.push_back(a1);
				else {
					for(int l=0;l<n;l++){
						if(l!=j&&l!=i){
							int a2=f(a1,c[l]);
							if(k==3)sum.push_back(a2);
							else{
								for(int m=0;m<n;m++){
									if(m!=l&&m!=j&&m!=i){
										int a3=f(a2,c[m]);
										sum.push_back(a3);
									}
								}
							}
						}
					}
				}
			}
		}
	}
	sort(sum.begin(),sum.end());
	int s=sum.size();
	for(int i=1;i<s;i++){
		if(sum[i]!=sum[i-1])ans++;
	}
	printf("%d\n",ans);
}
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

int N, M, A[1005], B[1005];
ll ans = 1;

int main(){
	getint(N), getint(M);
	for(register int i = 1; i <= N; i++) getint(A[i]);
	for(register int i = 1; i <= M; i++) getint(B[i]);
	sort(A + 1, A + N + 1), sort(B + 1, B + M + 1), A[N + 1] = B[M + 1] = inf;
	for(register int i = 1; i < N; i++) if(A[i] == A[i + 1]) return puts("0"), 0;
	for(register int i = 1; i < M; i++) if(B[i] == B[i + 1]) return puts("0"), 0;
	for(register int i = 1, j = 1, k = 1; i <= N * M; i++){
		while(j <= N && A[j] < i) j++;
		while(k <= M && B[k] < i) k++;
		if(A[j] == i && B[k] != i) ans = ans * (M - k + 1) % MOD;
		else if(A[j] != i && B[k] == i) ans = ans * (N - j + 1) % MOD;
		else if(A[j] != i && B[k] != i) ans = ans * ((M - k + 1) * (N - j + 1) - (N * M - i)) % MOD;
	}
	return printf("%lld\n", ans), 0;
}

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

int main(){
    int n,m;cin >> n >> m;
    vector<int> a(n),b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(RALL(a));
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }
    sort(RALL(b));
    LL h=0,w=0;
    LL ans = 1, res = 0;
    for (int i = n*m; i >= 1; i--) {
        if(h < n && i == a[h] && w < m && i == b[w]){
            h++;
            w++;
            res += h + w - 2;
        }
        else if(h < n && i == a[h]){
            if(w==0) ans = 0;
            h++;
            res += w - 1;
            ans = ans * w % mod;
        }
        else if(w < m && i == b[w]){
            if(h==0) ans = 0;
            w++;
            res += h - 1;
            ans = ans * h % mod;
        }
        else{
            if(res <= 0) ans = 0;
            ans = ans * res % mod;
            res--;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int p[1000005];

int getSum(int* BIT,int idx){
	int sum=0;
	while(idx>0){
		sum+=BIT[idx];
		idx-=(idx&-idx);
	}
	return sum;
}

void update(int* BIT,int N,int idx,int val){
	while(idx<=N){
		BIT[idx]+=val;
		idx+=(idx&-idx);
	}
	return;
}

int main() {
	memset(p,0,sizeof(p));

	int N,i;
	scanf("%d",&N);
	int Q;
	scanf("%d",&Q);

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

	int *BIT=new int[N+1];
	for(i=0;i<=N;i++)
	BIT[i]=0;

	vector<pair<int,pair<int,int> > > queries(Q);
	for(i=0;i<Q;i++){
		scanf("%d %d",&queries[i].second.first,&queries[i].first);
		queries[i].second.second=i;
	}
	vector<int> ans(Q,0);
	sort(queries.begin(),queries.end());
	int j=0;
	for(i=1;i<=N;i++){
		if(p[A[i]]>0)
		update(BIT,N,p[A[i]],-1);
		p[A[i]]=i;
		update(BIT,N,i,1);
		while(j<Q&&queries[j].first==i){
			ans[queries[j].second.second]=getSum(BIT,i)-getSum(BIT,queries[j].second.first-1);
			++j;
		}
		if(j==Q)
		break;
	}

	for(int i=0;i<Q;i++)
	printf("%d\n",ans[i]);
	// your code goes here
	return 0;
}

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

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

int main(){
	while(1){
		llong n, m;
		scanf("%lld%lld", &n, &m);
		if(n==0) return 0;

		vector<llong> v(n);
		REP(i, n){
			scanf("%lld", &v[i]);
		}
		v.push_back(0);
		n++;

		vector<llong> a;
		REP(i, n){
			REP(j, n){
				a.push_back(v[i]+v[j]);
			}
		}
		VSORT(a);

		llong ans=0;
		REP(i, n){
			REP(j, n){
				llong tmp=v[i]+v[j];
				llong k=upper_bound(a.begin(), a.end(), m-tmp)-a.begin();
				if(k==a.size()) continue;
				k--;

				if(k>=0) ans=max(ans, tmp+a[k]);
			}
		}

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


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

int N, M;
vector<vector<int>> G;
vector<int> indeg;  // 入次数
vector<bool> visited;
vector<int> ans;  // トポロジカルソートの結果

void bfs(int s) {
  queue<int> q;
  q.push(s);
  visited.at(s) = true;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int nx : G.at(cur)) {
      indeg.at(nx)--;
      if (indeg.at(nx) == 0 && !visited.at(nx)) {
        q.push(nx);
        ans.at(nx) = cur + 1;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  G.resize(N);
  for (int i = 0; i < N-1+M; i++) {
    int a, b; cin >> a >> b;
    a--; b--;
    G.at(a).push_back(b);
  }

  indeg.resize(N);
  visited.resize(N);
  ans.resize(N);
  for (int i = 0; i < N; i++) {
    for (int e : G.at(i)) {
      indeg.at(e)++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (indeg.at(i) == 0 && !visited.at(i)) bfs(i);
  }
  for (int v : ans) cout << v << endl;
}

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

void dfs(vector<vector<int>> &g, vector<bool> &used, int v, vector<int> &ans) {
    used[v] = true;
    for(int nv: g[v]) {
        if(!used[nv])  {
            dfs(g, used, nv, ans);
        }
    }
    ans.push_back(v);
}
int main(){
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vector<vector<int>> to(n);
    vector<vector<int>> from(n);
    rep(i,n-1+m) {
        int a,b;
        cin >> a >> b;
        a--;b--;
        to[a].push_back(b);
        from[b].push_back(a);
    }
    vector<bool> used(n, false);
    vector<int> topo_sort(0);
    rep(v,n) {
        if(!used[v]) dfs(to, used, v, topo_sort);
    }
    vector<int> inv_topo(n);
    rep(i,n) {
        inv_topo[topo_sort[i]] = i;
    }
    rep(i,n) {
        if(from[i].size() == 0) {
            cout << 0 << endl;
            continue;
        }
        int ans = from[i][0];
        for(int v :from[i]) {
            if(inv_topo[v] < inv_topo[ans]) {
                ans = v;
            }
        }
        cout << ans+1 << endl;
    }
}


#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
    const int MOD = 1000000007;
    inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
    inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
    inline int mul(int x, int y) { return 1LL * x * y % MOD; }
    inline int qpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = mul(x, x))
            if (y & 1) ans = mul(ans, x);
        return ans;
    }
};  // namespace modular

namespace Base {
    template <typename Tp>
    inline Tp input() {
        Tp x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return 0;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x *= y;
    }
    template <typename Tp>
    inline void read(Tp &x) {
        x = input<Tp>();
    }
    template <typename Tp>
    inline void chmax(Tp &x, Tp y) {
        x < y ? x = y : 0;
    }
    template <typename Tp>
    inline void chmin(Tp &x, Tp y) {
        x > y ? x = y : 0;
    }
};  // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 607

int N, D1, D2;

struct Graph {
    std::vector<pr> dir;
    std::queue<pr> q;
    int col[MAX_N][MAX_N];
    Graph() { memset(col, -1, sizeof(col)); }

    void build(int D) {
        lep(i, 0, N * 2) {
            if (i * i > D) break;
            lep(j, 0, N * 2) {
                if (j * j > D) break;
                if (i * i + j * j == D) {
                    dir.push_back(pr(i, j));
                    dir.push_back(pr(i, -j));
                    dir.push_back(pr(-i, j));
                    dir.push_back(pr(-i, -j));
                }
            }
        }
    }
    bool in(int x, int y) { return 0 <= x && x < N * 2 && 0 <= y && y < N * 2; }
    void bfs(int x, int y) {
        q.push(pr(x, y));
        col[x][y] = 0;
        while (!q.empty()) {
            pr k = q.front();
            q.pop();
            for (pr t : dir) {
                pr nk = pr(k.fi + t.fi, k.se + t.se);
                if (in(nk.fi, nk.se) && col[nk.fi][nk.se] == -1) {
                    col[nk.fi][nk.se] = col[k.fi][k.se] ^ 1, q.push(nk);
                }
            }
        }
    }
    void color() {
        // for (auto t : dir) printf("%d %d\n", t.fi, t.se);
        // puts("--------");
        // return;
        lep(i, 0, N * 2) lep(j, 0, N * 2) if (col[i][j] == -1) bfs(i, j);
    }
} G1, G2;

void init() {
    G1.build(D1), G2.build(D2);
    G1.color(), G2.color();
}

std::vector<pr> ans[2][2];
void solve() {
    lep(i, 0, N * 2) {
        lep(j, 0, N * 2) {
            ans[G1.col[i][j]][G2.col[i][j]].push_back(pr(i, j));
        }
    }
    lep(i, 0, 2) {
        lep(j, 0, 2) if (ans[i][j].size() >= N * N) {
            while (ans[i][j].size() > N * N) ans[i][j].pop_back();
            for (pr k : ans[i][j]) printf("%d %d\n", k.fi, k.se);
            return;
        }
    }
}

int main() {
    read(N), read(D1), read(D2);
    init();
    solve();
    return 0;
}


#include <bits/stdc++.h>
using i64 = long long;

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

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

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

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

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

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

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

    return 0;
}

#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<queue>
using namespace std;
long long int inf = 10000000000;

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

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

  vector< vector<long long int> > dist(n, vector<long long int>(n, inf));

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

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      if( dist[j][i] == inf ) continue;
      for(int k=0; k<n; k++){
        if( dist[i][k] == inf ) continue;
        if( dist[j][k] > dist[j][i] + dist[i][k] ) dist[j][k] = dist[j][i] + dist[i][k];
      }
    }
  }

  for(int i=0; i<n; i++){
    if( dist[i][i] < 0 ){
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      if( dist[i][j] == inf ) cout << "INF";
      else cout << dist[i][j];
      if( j == n-1 ) cout << endl;
      else cout << " ";
    }
  }

  return 0;
}

// EOF


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef map<ll,ll>::iterator itll;
typedef long double ld;
typedef map<ll,ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(),vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp,it) for(it = mp.begin();it!=mp.end();it++)
#define FOR(i,a,n) for(ll i=a;i<n;i++)
#define FORE(i,a,n) FOR(i,a,n+1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cerr.tie(0);
	ll a,b;
	cin>>a>>b;
	cout<<b*2-a<<Endl;
	return 0;
}

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <string>
#include <cmath>
#include <complex>
#include <numeric>
#include <cassert>

#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i) cout << fixed << setprecision(i)		//coutの浮動小数出力の桁数設定
using namespace std;

const int64 mod = 998244353;

int64 mpow(int64 mm, int64 xx)
{
	if(xx == 0)
	{
		return 1;
	}
	int64 tmp = mpow(mm, xx / 2);
	tmp *= tmp;
	tmp %= mod;
	if(xx % 2 == 1)
	{
		tmp *= mm;
		tmp %= mod;
	}
	return tmp;
}

int main()
{
	int64 kk, xx;
	cin >> kk >> xx;
	for(int64 ii = xx - kk + 1;ii <= xx + kk - 2;ii++)
	{
		cout << ii << ' ';
	}
	cout << (xx + kk - 1) << endl;
	return 0;
}


#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

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

  int mi=1000;
  while(s>=100){
    mi=min(mi,abs(753-int(s%1000)));
    s/=10;
  }

  cout<<mi<<endl;

  return 0;
}

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

int main() {
    string s;
    cin>>s;
    int minv=1000000005;
    size_t l=s.length();
    rep(i,l-2){
        string ss=s.substr(i,3);
        int num=stoi(ss);
        minv=min(minv,abs(num-753));
    }
    cout<<minv<<"\n";
    return 0;
}


#include <stdio.h>

int main() {
	char S[30];
	scanf("%[^\n]", S);
	S[3] = '8';
	printf("%s", S);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  char s[6];
  cin >> s;
  for(int i=0;i<5;i++){
    if(s[i]=='A'&&s[i+1]=='C'){
      cout << "Yes";
      return 0;
    }
  }
  cout << "No";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=17;
const int INF=0x3f3f3f3f;
ll n,m,conn[MAX][MAX],dist[MAX][MAX],dp[MAX][1<<MAX],visit[MAX][1<<MAX],calc[1<<MAX],trk[MAX],ans;
void calc_build(ll x,ll v,ll idx){
    //printf("%lld %lld %lld\n",x,v,idx);
    if(idx>=n) {
        calc[x]=v;
        return;
    }
    calc_build(x,v,idx+1);
    trk[idx]=1;
    for(ll i=0;i<idx;i++) if(trk[i]) v+=dist[i][idx];
    calc_build(x+(1<<idx),v,idx+1);
    trk[idx]=0;
}
ll dp_build(ll x,ll bit){
    if(x==n-1) return dp[x][bit]=calc[bit];
    if(visit[x][bit]==0){
        visit[x][bit]=1;
        dp[x][bit]=-INF;
        for(ll i=0;i<n;i++) if(((1<<i)&bit)&&conn[i][x]){
            ll cmp[MAX],cmp_cnt=0;
            for(ll j=0;j<n-1;j++) if(((1<<j)&bit)&&i!=j&&x!=j) cmp[cmp_cnt++]=j;
            for(ll j=0;j<(1<<(cmp_cnt));j++){
                ll g1=1<<x,g2;
                for(ll k=0;k<cmp_cnt;k++) if((1<<k)&j) g1+=(1<<cmp[k]);
                g2=bit-g1;
                dp[x][bit]=max(dp[x][bit],calc[g1]+dp_build(i,g2)+dist[x][i]);
            }
        }
    }
    return dp[x][bit];
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(ll i=0;i<m;i++){
        ll t1,t2,t;
        scanf("%lld%lld%lld",&t1,&t2,&t);
        t1--,t2--;
        conn[t1][t2]=1,dist[t1][t2]=t;
        conn[t2][t1]=1,dist[t2][t1]=t;
        ans+=t;
    }
    calc_build(0,0,0);
    printf("%lld\n",ans-dp_build(0,(1<<n)-1));
}

#include <bits/stdc++.h>
#define N 20
#define M (1 << 15 | 5)
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int mp[N][N], inc[M], cot[M][N], dp[M][N];
int main(int argc, char const *argv[]) {
	int n = read(), m = read();
	for (int i = 1; i <= m; i++) {
		int x = read() - 1, y = read() - 1;
		mp[x][y] = mp[y][x] = read();
	}
	int tot = 1 << n;
	for (int i = 0; i < tot; i++)
		for (int a = 0; a < n; a++) if (i >> a & 1)
			for (int b = 0; b < a; b++) if (i >> b & 1)
				if (mp[a][b]) inc[i] += mp[a][b];
	for (int i = 0; i < tot; i++)
		for (int a = 0; a < n; a++) if (~ i >> a & 1)
			for (int b = 0; b < n; b++) if (i >> b & 1)
				if (mp[a][b]) cot[i][a] += mp[a][b];
	memset(dp, -1, sizeof dp);
	dp[1][0] = 0;
	for (int i = 0; i < tot; i++) {
		for (int a = 0; a < n; a++) {
			if ((i >> a & 1) && ~ dp[i][a]) {
				for (int b = 0; b < n; b++) if (~ i >> b & 1)
					if (mp[a][b]) dp[i | (1 << b)][b] = max(dp[i | (1 << b)][b], dp[i][a] + mp[a][b]);
				int lst = tot - 1 - i;
				for (int j = lst; j; j = (j - 1) & lst) {
					int now = inc[j] + cot[j][a];
					dp[i | j][a] = max(dp[i | j][a], dp[i][a] + now);
				}
			}
		}
	}
	printf("%d\n", inc[tot - 1] - dp[tot - 1][n - 1]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD=1e9+7;
const long double Pi=acos(-1);
using  P=pair<ll,ll>;
int main(){
  ll counte=0,countw=0;
  string str;
  cin>>str>>str;
  vector<ll> w((ll)str.size());
  vector<ll> e((ll)str.size());
  for(ll i=0;i<(ll)str.size();i++){
    if(i==0){
      if(str.at(i)=='E'){e.at(i)=1;w.at(i)=0;counte++;}
      else{e.at(i)=0;w.at(i)=1;countw++;}
    }
    else{
      if(str.at(i)=='E'){e.at(i)=e.at(i-1)+1;w.at(i)=w.at(i-1);counte++;}
      else{e.at(i)=e.at(i-1);w.at(i)=w.at(i-1)+1;countw++;}
    }
  }
  ll ans;
  for(ll i=0;i<(ll)str.size();i++){
    if(i==0){ans=counte-e.at(0);}
    else if(i==(ll)str.size()-1){ans=min(ans,w.at(i));}
    else{ans=min(ans,w.at(i-1)+counte-e.at(i));}
  }
  cout<<ans<<endl;
}

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

    int sum = 0;
    int best = 1e8;
    rep (i, n) {
        int x = l + i;
        sum += x;
        if (abs(best) > abs(x)) {
            best = x;
        }
    }

    sum -= best;
    println(sum)

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll,ll>>>;
#define rep(i,n) for (int i=0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N){
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    return A;
}
void OutputVec(vector<ll> A){
    for (int i = 0; i < A.size(); ++i) {
        cout << A[i] << ",";
    }
    cout << endl;
}
vector<vector<ll>> InputVec2d(ll H,ll W){
    vector<vector<ll>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<ll>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        for (int xi = 0; xi < W; ++xi) {
            cin >> A[yi][xi];
        }
    }
    return A;
}
vector<vector<char>> InputVec2dStr(ll H,ll W){
    vector<vector<char>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<char>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        string S;
        cin >> S;
        for (int xi = 0; xi < W; ++xi) {
            A[yi][xi] = S[xi];
        }
    }
    return A;
}
void OutputVec2d(vector<vector<ll>> A){
    for (int yi = 0; yi < A.size(); ++yi) {
        for (int xi = 0; xi < A[yi].size(); ++xi) {
            cout << A[yi][xi] << ",";
        }
        cout << endl;
    }
}
graph InputGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    return G;
}
graph InputDGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    return G;
}
void OutputGraph(graph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j]<< ",";
        }
        cout << endl;
    }
}
void OutputAnswerBool(bool b,bool isUpperCase = false){
    if(b){
        if(isUpperCase){
            cout << "YES" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }else{
        if(isUpperCase){
            cout << "NO" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}

int main() {
    ll N,M;
    cin >> N >> M;
    auto A = InputVec2d(N,M);
    vector<bool> b(M);
    for (int i = 0; i < M; ++i) {
        b[i] = true;
    }
    ll m = N;
    for (int i = 0; i < M; ++i) {
        vector<ll> bucket(M);
        for (int ni = 0; ni < N; ++ni) {
            for (int mi = 0; mi < M; ++mi) {
                if(b[A[ni][mi]-1]){
                    bucket[A[ni][mi]-1]++;
                    break;
                }
            }
        }
        ll del = -1;
        for (int j = 0; j < M; ++j) {
            if(b[j]){
                if(del == -1){
                    del = j;
                }
                if(bucket[j] >= bucket[del]){
                    del = j;
                }
            }
        }
        m = min(m,bucket[del]);
        b[del] = false;
    }
    cout << m << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int main(){
    int n,m,A[301][301],B[301][301];
    cin>>n>>m;
    rep(i,n)rep(j,m) cin>>A[i][j],B[i][A[i][j]]=j+1;
    int num[301],ans=n;
    rep(i,m) num[i+1]=1;
    rep(i,m){
        int C[301]={0};
        rep(j,n){
            int a=m,b;
            rep(k,m) if(num[k+1]){
                if(a>=B[j][k+1]) a=B[j][k+1],b=k+1;
            }
            C[b]++;
        }
        int a=0,b;
        rep(j,m) if(a<=C[j+1]) a=C[j+1],b=j+1;
        ans=min(ans,a),num[b]--;
    }
    cout<<ans<<"\n";
}


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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<ll> a(n);
    set<ll> st;
    map<ll, ll> mp;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        mp[a[i]]++;
        st.emplace(a[i]);
    }
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    int m = a.size();
    vector<ll> c(m);
    for (int i = 0; i < m; i++) {
        c[i] = mp[a[i]];
    }
    vector<vector<int>> g(m);
    for (int i = 0; i < m; i++) {
        for (ll x = 2; x < 2e9; x <<= 1) {
            int j = lower_bound(a.begin(), a.end(), x - a[i]) - a.begin();
            if (j < i && a[i] + a[j] == x) {
                g[i].emplace_back(j);
                g[j].emplace_back(i);
            }
        }
    }
    debug(a);
    debug(c);
    debug(g);
    ll ans = 0;
    vector<bool> was(m);
    function<void(int)> dfs = [&](int v) {
        ll t = 0;
        for (int to : g[v]) {
            if (was[to]) {
                continue;
            }
            was[to] = true;
            dfs(to);
            t += c[to];
        }
        if (t >= c[v]) {
            ans += c[v];
            c[v] = 0;
        } else {
            ans += t;
            c[v] -= t;
        }
    };
    for (int i = 0; i < m; i++) {
        if (!was[i] && __builtin_popcount(a[i]) == 1) {
            was[i] = true;
            dfs(i);
            ans += c[i] / 2;
        }
    }
    for (int i = 0; i < m; i++) {
        if (!was[i]) {
            was[i] = true;
            dfs(i);
        }
    }
    cout << ans << '\n';
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int max_n = 2e5 + 5;

int tab[max_n];

map<int, int> zliczanie;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, wynik = 0;
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> tab[i];
        zliczanie[tab[i]]++;
    }
    sort(tab, tab + n);
    for (int i = n - 1; i >= 0; i--){
        if (!zliczanie[tab[i]])
            continue;
        zliczanie[tab[i]]--;
        if (zliczanie[(2 << __lg(tab[i])) - tab[i]]){
            zliczanie[(2 << __lg(tab[i])) - tab[i]]--;
            wynik++;
        }
    }
    cout << wynik << "\n";
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mem(a,b) memset(a, b, sizeof(a) )
#define all(x) (x).begin(),(x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

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

int main(){
    ll n,m,v,p;cin>>n>>m>>v>>p;
    ll t=m*v;
    vl a(n);
    rep(i,n)cin>>a[i];
    sort(all(a));
    int r=n-1,l=0;
    while(r-l>1){
        int mid=(r+l)/2;
        ll sum=(mid+1)*m,now=a[mid]+m;
        if(now<a[n-p]){
            l=mid;
            continue;
        }
        for(int i=mid+1;i<=n-p;++i){
            sum+=now-a[i];
        }
        if(sum+(p-1)*m<t){
            l=mid;
        }
        else{
            r=mid;
        }
    }
    ll ans=r;
    ll sum=(l+1)*m,now=a[l]+m;
    if(now>=a[n-p]){
    for(int i=l+1;i<=n-p;++i){
        sum+=now-a[i];
    }
    if(sum+(p-1)*m>=t){
        ans=l;
    }
    }
    cout<<n-ans<<endl;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

signed main(void)
{
    ll n;
    cin >>n;
    ll x;
    cin >>x;
    vll a(n);
    ll r = 0;
    rep(i, n) {
        cin >>a[i];
    }
    Sort(a);

    rep(i, n) {
        x -= a[i];
        if (x < 0) break;
        r++;
    }
    if (x > 0) r--;

    mes(r);

}


#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int, int >
#define F first
#define S second
#define int long long int
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define endl '\n'
using namespace std;
const int N=2e5+100;
ll seg[N*4][2];
ll lazy=0;
void upd(ll nod,ll b,ll l,ll r,ll id,ll val){
    if (r-l==1){
        seg[nod][b]=min(seg[nod][b],val);
        return;
    }
    ll mid=(r+l)/2;
    if (mid>id) upd(nod*2,b,l,mid,id,val);
    else upd(nod*2+1,b,mid,r,id,val);
    seg[nod][b]=min(seg[nod*2][b],seg[nod*2+1][b]);
    return ;
}
ll get(ll nod,ll b,ll l,ll r,ll L,ll R){
    if (l>=R || L>=r) return (ll)1e15;
    if (l>=L && r<=R) return seg[nod][b];
    ll mid=(r+l)/2;
    return min (get(nod*2,b,l,mid,L,R),get(nod*2+1,b,mid,r,L,R));
}
ll n,a[N],x,y,q;
int32_t main(){
    cin >> n >> q >> x >> y;
    for (int i=1;i<=q;i++){
        cin >> a[i];
    }
    memset(seg,31,sizeof seg);
    a[0]=x;
    upd(1,0,1,N,y,-y);
    upd(1,1,1,N,y,y);
    for (int i=1;i<=q;i++){
        ll z=min(get(1,0,1,N,1,a[i])+a[i],get(1,1,1,N,a[i],N)-a[i]);
        ll t=abs(a[i]-a[i-1]);
     //   cout << z << " " << lazy << endl;
        z+=lazy;
        lazy+=t;
        z-=lazy;
        upd(1,0,1,N,a[i-1],z-a[i-1]);
        upd(1,1,1,N,a[i-1],z+a[i-1]);
    }
    ll ans=(ll)1e15;
    for (int i=1;i<=n;i++){
        ll z=get(1,0,1,N,i,i+1);
        z+=i;
        ll t=get(1,1,1,N,i,i+1);
        t-=i;
        ans=min(ans,min(z,t));
    }
    cout << ans+lazy << endl;
}

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

int main(){
  int n, K; scanf("%d %d", &n, &K);
  vector<int> a(n);
  for(int i = 0; i < n; i++) scanf("%d", &a[i]);

  int ans = 0;
  for(int left = 0; left <= K; left++){
    for(int right = 0; right <= K; right++){
      if(left + right > min(n, K)) continue;
      vector<int> data;
      int ans1 = 0;
      for(int i = 0; i < left; i++) data.push_back(a[i]), ans1 += a[i];
      for(int i = n - 1; i > n - 1 - right; i--) data.push_back(a[i]), ans1 += a[i];
      sort(data.begin(), data.end());
      for(int i = 0; i < min(K - left - right, left + right); i++) if(data[i] < 0) ans1 -= data[i];
      ans = max(ans, ans1);
    }
  }
  printf("%d\n", ans);
}


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

using namespace std;

namespace {

	int cnt;
	void InsertionSort(vector<int>& A, int g)
	{
		for (int i = g; i < A.size(); ++i)
		{
			int v = A[i];
			int j = i - g;

			while (j >= 0 && A[j] > v) {
				A[j + g] = A[j];
				j -= g;
				cnt++;
			}

			A[j + g] = v;
		}
	}

	void ShellSort(vector<int>& A)
	{
		int m;
		cnt = 0;
		vector<int> G;
		G.push_back(1);
		for (int i = 1; i <= A.size(); ++i)
		{
			int g = pow(4, i) + 3 * pow(2, i - 1) + 1;
			if (g <= A.size()) G.push_back(g);
			else { m = i; break; }
		}

		for (int i = m - 1; i >= 0; --i)
			InsertionSort(A, G[i]);

		cout << m << endl;
		for (int i = m - 1; i >= 0; --i) {
			cout << G[i];
			i != 0 ? cout << " " : cout << endl;
		}
		cout << cnt << endl;
		for (int i = 0; i < A.size(); ++i)
			cout << A[i] << endl;
	}
}

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

	int n;
	cin >> n;

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

	ShellSort(A);

	return 0;
}

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

#define ll long long

using namespace std;

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

	ll n;
	cin >> n;
	vector<ll> a(n);

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

	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i != 0) {
			if (a[i] < a[i - 1]) {
				sum += a[i - 1] - a[i];
				a[i] += a[i - 1] - a[i];

			}
		}
	}

	cout << sum;

	return 0;
}


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

string Caesar(string a) {
	for (int b = 0; b < a.length(); b++) {
		if (a[b] == 'z')a[b] = 'a';
		else if (a[b] >= 'a'&&a[b] < 'z')a[b] = a[b] + 1;
	}
	return a;
}
int main()
{
	string a;
	while (getline(cin, a)) {
		for (int b = 0; b < 26; b++) {
		    a=Caesar(a);
			for (int c = 0; c < a.length() - 4; c++) {
				if (a[c] == 't'&&a[c + 1] == 'h') {
					if (a[c + 2] == 'e') { cout << a << endl; goto l; }
					else	if (a[c + 2] == 'a'&&a[c + 3] == 't') { cout << a << endl; goto l; }
					else	if (a[c + 2] == 'i'&&a[c + 3] == 's') { cout << a << endl; goto l; }
				}
			}
			if(a[a.length()-1]=='t'&&a[a.length() - 2] == 'h'&&a[a.length() - 3] == 'e') { cout << a << endl; break; }
		l:;
		}
	}
}

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s,t;
	while(getline(cin,s)){t=s;
	for(int i=0;i<26;i++){
		s=t;
		for(int j=0;j<s.size();j++){
			if(s[j]>='a'&&s[j]<='z')for(int k=0;k<i;k++){
				s[j]++;
				if(s[j]>'z')s[j]-=26;
			}
		}
		if((s.find("the")!=s.npos||s.find("this")!=s.npos||s.find("that")!=s.npos))break;
	}
	cout<<s<<endl;}
}


#include <bits/stdc++.h>
#define DEBUG
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll read(){
	char c=getchar();bool flag=0;ll x=0;
	while(c<'0'||c>'9'){if(c=='-')flag=1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return flag?-x:x;
}
#define N 2010
const ll p=1e9+7;
ll dp[N][N],fac[N*N],inv[N*N];
ll fpow(ll a,ll x){
	ll ans=1;
	while(x){
		if(x&1) ans=ans*a%p;
		a=a*a%p,x=x/2;
	}
	return ans;
}
ll Cnt(ll n,ll m){
	return fac[m]*inv[n]%p*inv[m-n]%p;
}
int main(){
	ll n=read(),k=read();
	if(k==1){
		printf("1");
		return 0;
	}
	fac[0]=1;
	for(ll i=1;i<=n*k;i++) fac[i]=fac[i-1]*i%p;
	inv[n*k]=fpow(fac[n*k],p-2);
	for(ll i=n*k-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%p;
	for(ll i=1;i<=n;i++) dp[i][0]=1;
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=i;j++){
			dp[i][j]=dp[i-1][j]+dp[i][j-1]*(n-(j-1))%p*Cnt(k-2,n*k-i-(j-1)*(k-1)-1)%p;
			dp[i][j]%=p;
		}
	}
	printf("%lld",dp[n][n]);
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef pair<int,int> pii;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define per(i,a,b) for(int i=(b)-1; i>=(a); --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

const ll MOD = 1e9+7;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);
	int n, m;
	cin >> n >> m;
	map<int, int> y;
	map<int, int> x;
	rep(i,0,n) {
		int a;
		cin >> a;
		y[a] = i;
	}
	rep(i,0,m) {
		int b;
		cin >> b;
		x[b] = i;
	}
	ll ret = 1;
	ll A = 0;
	ll B = 0;
	per(i,1,n*m+1) {
		bool flagy = (y.find(i) != y.end());
		bool flagx = (x.find(i) != x.end());
		if(flagy && flagx) {
			A++; B++;
		} else if(flagy) {
			ret = ret * B % MOD;
			A++;
		} else if(flagx) {
			ret = ret * A % MOD;
			B++;
		} else {
			ll loc = A * B - (ll)(n*m - i);
			ret = ret * loc % MOD;
		}
	}
	cout << ret << '\n';
}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++i)
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; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
#define fs first
#define sc second
// 多倍長
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using boost::rational;
using boost::multiprecision::cpp_int;

const double PI = acos(-1);
const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}

// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;

vector<string> split(const string &s, char delim) {
    vector<string> elems;
    string item;
    for (char ch: s) {
        if (ch == delim) {
            if (!item.empty())
                elems.push_back(item);
            item.clear();
        }
        else {
            item += ch;
        }
    }
    if (!item.empty())
        elems.push_back(item);
    return elems;
}

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int a,b;
    cin >> a >> b;
    string s;
    cin >> s;
    if((int)s.size() != a + b + 1){
      cout << "No" << endl;
      return 0;
    }else if(s[a] != '-'){
       cout << "No" << endl;
       return 0;
    } else if(count(s.begin(), s.end(), '-') != 1) {
      cout << "No" << endl;
       return 0;
    } else {
      cout << "Yes" << endl;
      return 0;
    }

}

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

int main() {

  int N;
  cin >> N;

  vector<int> A(N);
  for(int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  sort(A.begin(), A.end());
  for(int i = 0; i < N; i++) {
//    cout << A.at(i) << endl;
  }

  for(int i = 0; i < N-1; i++) {
    if(A.at(i) == A.at(i+1)) {
      cout << "NO" << endl;
      break;
    }
    else if(i == N-2) cout << "YES" << endl;
  }

}


#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
//debug
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

int main(){
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;

  //左端がリーダの時，向きを変える必要がある人をcount
  REP(i,n){
    if(i == 0) continue;
    if(s[i] == 'E') cnt++;
  }
  int ans = cnt;

  //リーダを左から順にずれしていく
  REP(i,n){
    if(i == 0) continue;
    if(s[i-1] == 'W') cnt++;
    if(s[i] == 'E') cnt--;
    ans = min(ans,cnt);
  }

  cout << ans << endl;

  return 0;
}

//pragma
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)

#define pb push_back
#define F first
#define S second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo,int hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

const ll lrand(ll lo,ll hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

#define getc getchar
template<typename T>
T getnum(){
	int sign = 1;
	T ret = 0;
	char c;

	do{
		c = getc();
	}while(c == ' ' || c == '\n');
	if(c == '-')sign = -1;
	else ret = c-'0';
	while(1){
		c = getc();
		if(c < '0' || c > '9')break;
		ret = 10*ret + c-'0';
	}
	return sign * ret;
}

inline void ini(int& x){
	x = getnum<int>();
}

inline void scani(int& x){
	scanf("%d",&x);
}

//end of macro

const ll MOD = 1e9 + 7;
inline ll addmod(ll a, ll b){
	if(a+b<MOD)return a+b;
	return a+b-MOD;
}
inline ll mulmod(ll a, ll b){
	return (a*b)%MOD;
}
inline ll expmod(ll a, ll e){
	ll ret = 1;
	while(e){
		if(e&1)ret = mulmod(ret, a);
		a = mulmod(a, a);
		e >>= 1;
	}
	return ret;
}
inline ll divmod(ll a, ll b){
	return mulmod(a, expmod(b, MOD-2));
}

const int N = 2e5 + 5;

int fac[N];
inline int C(int n, int r){
	return divmod(fac[n], mulmod(fac[r], fac[n-r]));
}

int h,w,a,b;

inline int ways(int x1, int y1, int x2, int y2){
	int x = x2-x1;
	int y = y2-y1;
	return C(x+y, x);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #define endl '\n'
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(10);

    fac[0] = 1;
    REP(i,1,N-1)fac[i] = mulmod(fac[i-1], i);

    cin>>h>>w>>a>>b;

    int ans = 0;
    int i = h - a;
    REP(j,b+1,w){
    	ans = addmod(ans, mulmod(ways(1, 1, i, j), ways(i+1, j, h, w)));
    }

    cout << ans << endl;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
int main() {
	int N;
	std::cin >> N;
	std::vector<int>sum(N), sort(N);
	for (int i = 0; i < (N - 1)*N / 2; i += 1) {
		int a, b, c, d;
		std::cin >> a >> b >> c >> d;
		if (c > d) {
			sum[a - 1] += 3;
		}
		else if (d > c) {
			sum[b - 1] += 3;
		}
		else {
			sum[a - 1] += 1;
			sum[b - 1] += 1;
		}
	}
	for (int i = 0; i < N; i += 1)
		sort[i] = sum[i];
	std::sort(sort.begin(), sort.end());
	for (int i = 0; i < N; i += 1)
		for (int j = N-1; j >= 0; j -= 1) {
			if (sum[i] == sort[j]) {
				std::cout << N-j << std::endl;
				break;
			}
		}
	return 0;
}


#include "bits/stdc++.h"

using namespace std;

int main() {
  using ull = unsigned long long;
  ull ans[50];
  ull k;
  cin >> k;
  cout << 50 << endl;
  ull j = k / 50;
  int i = k % 50;
  for (auto &x : ans) {
    x = 49 + j;
  }
  for (int a = 0; a < i; ++a) {
    ans[a] += 51;
  }
  for (auto &x : ans) {
    x -= i;
    cout << x << " ";
  }
}

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

ll k;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> k;
    int n = 50;
    vector <ll> a(n);
    for(int i = 0 ; i < n ; i++){
        a[i] = i;
    }
    ll f = k / n;
    for(auto &i : a) i += f * n;
    for(auto &i : a) i -= f * (n - 1);
    for(int itt = 0 ; itt < k % n ; itt++){
        int mn = 0;
        for(int j = 0 ; j < n ; j++){
            if(a[j] < a[mn]) mn = j;
        }
        for(int j = 0 ; j < n ; j++){
            if(j == mn) a[j] += n;
            else a[j]--;
        }
    }
    cout << n << endl;
    for(auto &i : a) cout << i << " ";
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll=long long;
using P = pair<int,int>;
using LP=pair<ll,int>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
const ll MOD=1000000007,MOD2=998244353;

int main() {
    int A,B,C;
    cin>>A>>B>>C;
    if((A-C)*(B-C)<=0)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main(){cout<<fixed<<setprecision(10);
		   int a,b,c;
           cin>>a>>b>>c;
           Yes((a-c)*(b-c)<0);
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Zalgorithm */

// A[i] := S と S[i:|S|-1] の最長共通接頭辞の長さ

void Zalgorithm(const char *S, int *A) {
  int i = 1, j = 0, k, n = strlen(S);
  A[0] = n;
  while (i < n) {
    while (i + j < n && S[j] == S[i + j]) j++;
    A[i] = j;
    if (j == 0) {
      i++;
      continue;
    }
    for (k = 1; i + k < n && k + A[k] < j; k++) A[i + k] = A[k];
    i += k;
    j -= k;
  }
}

int N, A[SIZE], B[SIZE];
int z[30][SIZE*5];
char S[30][SIZE*5];

int main() {

  scanf("%d", &N);

  for (int i=0; i<N; i++) scanf("%d", A+i);
  for (int i=0; i<N; i++) scanf("%d", B+i);

  for (int i=0; i<N*5; i++) {
    int tmp;
    if (i < N)
      tmp = B[i];
    else if (i < N*3)
      tmp = A[i%N];
    else
      tmp = A[i%N] ^ ((1 << 30) - 1);

    for (int j=0; j<30; j++) {
      S[j][i] = tmp % 2 + 1;
      tmp /= 2;
    }
  }

  for (int i=0; i<30; i++)
    Zalgorithm(S[i], z[i]);

  for (int i=0; i<N; i++) {
    int b = A[i] ^ B[0];
    bool ok = true;

    for (int j=0; j<30; j++) {
      if (b & (1 << j)) {
        ok &= z[j][i+N*3] >= N;
      } else {
        ok &= z[j][i+N] >= N;
      }
    }

    if (ok) {
      printf("%d %d\n", i, b);
    }
  }

  return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main(){
  int n;
  cin >>n;
  cin.ignore();
  while(n--){
    string word;
    getline(cin,word);
    string::size_type index;
    for(;;){
      index = word.find("Hoshino");
      if(index == string::npos){break;}
      word[index+6] = 'a';
    }
    cout <<word<<endl;
  }
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int MAXN = 305;

int n,k,ans;
int f[2][MAXN][MAXN];

char s[MAXN];

int main()
{
	scanf("%s",s + 1);
	n = strlen(s + 1);
	scanf("%d",&k);
	for (int i = 0;i <= k;i++)
	{
		memcpy(f[i & 1],f[~i & 1],sizeof(f[i & 1]));
		for (int j = 1;j <= n;j++)
			for (int k = n;k >= j + 1;k--)
			{
				if (s[j] == s[k])
					f[i & 1][j][k] = max(f[i & 1][j][k],f[i & 1][j - 1][k + 1] + 1);
				else if (i)
					f[i & 1][j][k] = max(f[i & 1][j][k],f[~i & 1][j - 1][k + 1] + 1);
				f[i & 1][j][k] = max(f[i & 1][j][k],f[i & 1][j - 1][k]);
				f[i & 1][j][k] = max(f[i & 1][j][k],f[i & 1][j][k + 1]);
			}
	}
	for (int i = 0;i <= n;i++)
		ans = max(ans,f[k & 1][i][i + 1] * 2);
	for (int i = 1;i <= n;i++)
		ans = max(ans,f[k & 1][i - 1][i + 1] * 2 + 1);
	printf("%d\n",ans);
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;

const ll INF=(1LL<<50);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
  if(a < b) return gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
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;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
  os << "[ ";
  REP(a.size()){
    os<< a[i] << " ";
  }
  os<< " ]";
  return os;
}

const long long MOD = 1000000007;

const long long max_cache=1000000;
long long cache_fact[max_cache],cache_inv_fact[max_cache];
long long combination_cached(long long n,long long r){
  ll m=MOD;
  if(r<0||r>n)return 0;
  if(r==0||r==n)return 1;
  if(n>m){
    std::cerr<<"Combination Error: n is greater than m"<<std::endl;
    exit(1);
  }
  static bool cached=false;
  if(cached==false){
    cached=true;
    cache_inv_fact[1]=1;
    for (long long i = 2; i < max_cache; i++) {
      cache_inv_fact[i]=(m-m/i)*cache_inv_fact[m%i]%m;
    }
    for (long long i = 2; i < max_cache; i++){
      cache_inv_fact[i]=cache_inv_fact[i]*cache_inv_fact[i-1]%m;
    }
    cache_fact[0]=1;
    for (long long i = 1; i < max_cache; i++) {
      cache_fact[i]=cache_fact[i-1]*i%m;
    }
  }
  r=min(r,n-r);
  return cache_fact[n]*cache_inv_fact[r]%m*cache_inv_fact[n-r]%m;
}
void solve(long long N, long long M, long long K){
  ll ans=0;
  For(i,N){
    For(j,M){
      ans=(ans+(i+j)*combination_cached(N*M-2,K-2)%MOD*(N-i)%MOD*(M-j)%MOD)%MOD;
      if(i>0&&j>0){
        ans=(ans+(i+j)*combination_cached(N*M-2,K-2)%MOD*(N-i)%MOD*(M-j)%MOD)%MOD;
      }
    }
  }
  cout<<ans<<endl;
}

int main(){
  cout<<setprecision(1000);
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    long long K;
    scanf("%lld",&K);
    solve(N, M, K);
    return 0;
}


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

const int64_t MOD = 1000000007;
typedef int64_t ll;
typedef uint64_t ull;

#define FOR(i, start, end) for(uint64_t i=start; i<end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0){
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) {
  return (m*n)/gcd(m,n);
}

// vector<vector<uint64_t> > v(n+1, vector<uint64_t>(n+1, 0))
// v[n][k]に組み合わせ数が入る。
void comb(vector<vector <uint64_t> > &v){
  for(uint64_t i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(uint64_t k = 1;k <v.size();k++){
    for(uint64_t j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

// 掛け算オーバーフロー判定
bool is_product_overflow(uint64_t a, uint64_t b) {

	uint64_t prod = a * b;

	return (prod / b != a);
}

//素因数分解
void primeFactorization(uint64_t a, list<uint64_t> &factors){ //素因数分解を出力するプログラム
  long i,sq;
  if(a%2==0){ //偶数の場合
    factors.push_back(2);
    primeFactorization(a/2,factors); //2で割った値で再帰
    return;
  }
  sq = sqrt(a);
  for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合
	if(a%i==0){
	  factors.push_back(i);
      primeFactorization(a/i,factors); //割れた値で再帰
      return;
	}
  }
  //偶数でも3以上√a以下の奇数の場合でも割り切れない場合
  if(a!=1){ //aが1でないなら、a自身は素数
    factors.push_back(a);
  }
}

// フェルマーの小定理
// mod. m での a の逆元 a^{-1} を計算する
// (a/b)%m = a*movinv(b,m)%m
int64_t modinv(int64_t a, int64_t m) {
    int64_t b = m, u = 1, v = 0;
    while (b) {
        int64_t t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

/*
  //エラトステネスのふるい
  int maxNum = 100000;
  char isNotPrime[maxNum+1] = {};
  isNotPrime[0]=isNotPrime[1]=1;
  for(int i=2;i<maxNum;i++){
    if(isNotPrime[i]==1) continue;
    int num = i+i;
    while(num<=maxNum){
      isNotPrime[num]=1;
      num += i;
    }
  }
*/
// 円周率
// M_PI

// #include <iomanip> // setprecisionを使用するのに必要
// cout << std::fixed << std::setprecision(15) << y << endl;

// 昇順
// priority_queue<int, vector<int>, greater<int> > queue;

// 降順ソート vector<int> v;
// sort(v.begin(),v.end(),greater<int>());

signed main() {
  ull n;
  cin >> n;

  ull a = 0;
  for(ull i=sqrt(n);i>=1;i--){
    if(n%i==0){
      a = i;
      break;
    }
  }
  if(a==1){
    a = n;
  }
  if(a<n/a){
    a = n/a;
  }
  //cout << a << ":" << n/a << endl;
  int count = 0;
  while(a>0){
    a /= 10;
    count++;
  }

  cout << count << endl;
  return 0;
}

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

int f(ll a,ll b)
{
	return (int)max(log10(a),log10(b));
}

int main()
{
	ll N;
	cin>>N;
	int ans=10;
	for(int i=1;i<sqrt(N)+1;i++)
	{
		if(N%i!=0) continue;
		ll k=N/i;
		int v=f(i,k);
		ans=min(ans,v);
	}
	cout<<ans+1<<endl;
	return 0;
}


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

typedef pair<int,int> P;
#define ll long long
#define ld long double
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(x) (x).begin(),(x).end()

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

bool isprime(int num)
{
    for(int i = 2; i*i <=num; i++)
    {
        if(num%i==0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int n;cin >> n;
    vector<int> a(n);
    int counter = 0;
    int kouho = 6;
    while(counter < n)
    {
        if(isprime(kouho))
        {
            a[counter] = kouho;
            counter++;
        }
        kouho += 5;

    }
    rep(i,n)
    {
        cout << a[i] << " ";
    }
    cout  << endl;
}

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

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

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

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

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

//dpTable
int dp[100050];

int main(){
    ll N;
    cin >> N;
    int sqrtN = sqrt(N);
    int A,B;
    for (int i = sqrtN; i >= 1; i--) {
        if (N % i == 0) {
            A = i;
            B = N / i;
            break;
        }
    }
    int cntA = 0,cntB = 0;
    while (A != 0) {
        A /= 10;
        cntA++;
    }
    while (B != 0) {
        B /= 10;
        cntB++;
    }
    cout << max(cntA,cntB) << endl;
    return 0;
}


#include<iostream>
#include<string>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	string s;
	int n;
	cin>>n;
	cin.ignore();
	rep(i,n){
		getline(cin,s);
		int size=s.size();
		rep(j,size-6){
			string point=s.substr(j,7);
			if(point=="Hoshino"){
				point="Hoshina";
				s.replace(j,7,point);
			}
		}
		cout<<s<<endl;
	}
	return 0;
}

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

int main(){
	int n,pos;
	string s;
	while(cin>>n&&n){
		getline(cin,s);
		while(n--){
			getline(cin,s);
			while((pos=s.find("Hoshino",0))!=string::npos)
				s.replace(pos,7,"Hoshina");
			cout<<s<<endl;
		}
	}
	return 0;
}


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

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

    map<string, int> mp;
    rep(i,n) {
        string s; cin >> s;
        mp[s]++;
    }
    int m; cin >> m;
    rep(i,m) {
        string t; cin >> t;
        mp[t]--;
    }

    int ans = -10000;
    for (auto x : mp) {
        chmax(ans, x.second);
    }
    if (ans >= 0) cout << ans << endl;
    else cout << 0 << endl;
}

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

int main(){
    int N,M;
    int count = 0;
    int score = 0;
    cin >> N;
    vector<string> s(N);
    for(int i=0;i<N;i++){
        cin >> s.at(i);
    }
    cin >> M;
    vector<string> t(M);
    for(int i=0;i<M;i++){
        cin >> t.at(i);
    }
    for(int i=0;i<N;i++){
        count = 0;
        for(int j=0;j<N;j++){
            if(s.at(i) == s.at(j)) count++;
        }
        for(int j=0;j<M;j++){
            if(s.at(i) == t.at(j)) count--;
        }
        score = max(score,count);
    }
    cout << score << endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=305;
const int MOD=998244353;
int n,m;
int a[N];
long long f[N][N*N],g[N][N*N];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),m+=a[i];
	f[0][0]=1;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=m;j++)
		{
			f[i][j]=f[i-1][j]*2%MOD;
			if(j>=a[i]) f[i][j]=(f[i][j]+f[i-1][j-a[i]])%MOD;
		}
	long long ans=1;
	for(int i=1;i<=n;i++)
		ans=ans*3%MOD;
	for(int j=(m+1)/2;j<=m;j++)
		ans=(ans-f[n][j]*3%MOD+MOD)%MOD;
	if(m%2==0)
	{
		g[0][0]=1;
		for(int i=1;i<=n;i++)
			for(int j=0;j<=m;j++)
			{
				g[i][j]=g[i-1][j];
				if(j>=a[i]) g[i][j]=(g[i][j]+g[i-1][j-a[i]])%MOD;
			}
		ans=(ans+g[n][m/2]*3%MOD)%MOD;
	}
	printf("%lld",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for(int i = 0; i < n; i++)
#define REP(i, a, b) for(int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr <<__LINE__<< ": " << #x << " = " << x << endl
#define debug_vec(v) cerr<<__LINE__<<": "<<#v<<" = ";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<60) - 1;

void solve() {
    int n;
    cin >> n;
    vector<int> A(n);
    for(auto &a : A) cin >> a;
    ll ans = -LINF;
    if (n % 2 == 0) {
        ll sum = 0;
        for (int i = 1; i < n; i+=2) sum += A[i];
        chmax(ans, sum);
        for (int i = 0; i < n; i+=2){
            sum += A[i] - A[i + 1];
            chmax(ans, sum);
        }
        cout << ans << endl;
        return;
    } else {
        n /= 2;
        ll dp[n + 1][3];
        rep(i, n + 1) rep(j, 3) dp[i][j] = -LINF;
        dp[0][0] = dp[0][1] = dp[0][2] = 0LL;
        rep(i, n) {
            chmax(dp[i + 1][0], dp[i][0] + A[2 * i]);
            chmax(dp[i + 1][1], max(dp[i][0], dp[i][1]) + A[2 * i + 1]);
            chmax(dp[i + 1][2], max({dp[i][0], dp[i][1], dp[i][2]}) + A[2 * i + 2]);
        }
        cout << *max_element(dp[n], dp[n] + 3) << endl;
        return;
    }
}

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


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1001001001
#define LINF 1001001001001001001
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define bit(n) (1LL<<(n))
using namespace std;

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

bool check(int N, int M, vector<vector<int>> a, int x) {
    vector<deque<int>> list(M);
    vector<bool> playable(M, true);
    vector<deque<int>> A(N);
    rep(i,N) rep(j,M) A[i].push_back(a[i][j]);
    rep(i,N) {
        int k = A[i].front(); A[i].pop_front();
        list[k].push_back(i);
    }
    while (true) {
        int flag = -1;
        int max_count = x;
        rep(i,M) {
            if (list[i].size() > max_count) {
                flag = i;
                max_count = list[i].size();
            }
        }
        if (flag < 0) {
            return true;
        }
        playable[flag] = false;

        while (!list[flag].empty()) {
            int i = list[flag].front(); list[flag].pop_front();
            while (true) {
                if (A[i].empty()) return false;
                int k = A[i].front(); A[i].pop_front();
                if (playable[k]) {
                    list[k].push_back(i);
                    break;
                }
            }
        }
    }
}

int main() {

    int N, M;
    cin >> N >> M;
    vector<vector<int>> A(N, vector<int>(M));
    rep(i,N) rep(j,M) {
        cin >> A[i][j];
        A[i][j]--;
    }

    for (int i = 1; i <= N; i++) {
        if (check(N, M, A, i)) {
            cout << i << endl;
            return 0;
        }
    }

    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define INF (2e9)
const int maxv=10000+10;
const int maxe=100000+10;
int V,E;
int d[maxv];
struct Edge
{
    int from,to,cost;
};
Edge es[maxe];

int p[maxv];
int rank[maxv];

bool cmp(const Edge& a,const Edge& b)
{
    return a.cost<b.cost;
}

void init(int n)
{
    for(int i=0; i<n; i++)
    {
        p[i]=i;
        rank[i]=0;
    }
}
int fnd(int x)
{
    if(x==p[x]) return x;
    return fnd(p[x]);

}
bool is_same(int x,int y)
{
    return fnd(x)==fnd(y);

}
void unite(int x,int y)
{
    x=fnd(x);
    y=fnd(y);
    if(x==y) return;
    if(rank[x]>rank[y])
    {
        p[y]=x;
    }
    else
    {
        p[x]=y;
        if(rank[x]==rank[y]) rank[y]++;
    }
}
void kruskal()
{
    sort(es,es+E,cmp);
    init(V);
    int res=0;
    for(int i=0; i<E; i++)
    {
        Edge &e=es[i];
        if(!is_same(e.from,e.to))
        {
            unite(e.from,e.to);
            res+=e.cost;
        }
    }
    printf("%d\n",res);
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&V,&E);
    int u,v,cost;
    for(int i=0; i<E; i++)
    {
        scanf("%d%d%d",&u,&v,&cost);
        es[i].from=u;
        es[i].to=v;
        es[i].cost=cost;
    }
    kruskal();
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int N,n,ans=0;
  cin>>N>>n;
  if(n){
    puts("-1");
    return 0;
  }
  for(int i=1;i<N;i++){
    int a;
    cin>>a;
    if(n>=a)ans+=a;
    else if(n+1==a)ans++;
    else{
      puts("-1");
      return 0;
    }
    n=a;
  }
  cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end() // 昇順ソート
#define rall(v)(v).rbegin(), (v).rend() // 降順ソート
#define INF 1LL << 60
typedef long long int LL;
typedef long long int ll;
#define pll pair < ll, ll >
#define F first
#define S second
const int MOD = 1000000007;
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; }
// 任意のlogは　対数の底の変換を使う    log(N) / log(任意の底)

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

    vector<LL> num(N);
    rep(i,N){
        num[i] = i - A[i];
    }
    bool ok = true;
    if(num[0] != 0)ok = false;
    rep(i,N-1){
        if(A[i+1] - A[i] > 1)ok = false;
    }

    if(ok){
        LL ans = 0;
        rep(i,N){
            if(binary_search(all(num),i)){
                LL k = upper_bound(all(num),i) - num.begin() - 1;
                ans += (k-i);
            }
        }
        cout << ans << endl;
    }
    else{
        cout << -1 << endl;
    }
}


#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;

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

	vector<int> v(3);
	v.at(0) = A;
	v.at(1) = B;
	v.at(2) = C;
	sort(v.begin(), v.end());

	int n = v.back();
	while (K) {
		n *= 2;
		K--;
	}

	cout << v.at(0) + v.at(1) + n << endl;
}

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

long int inf=100000*100000+100;
int main()
{
    int v,e,r;
    int s[500000];
    int t[500000];
    int d[500000];
    long int dis[100000];
    cin>>v>>e>>r;
    for(int i=0;i<e;i++)
    {
        cin>>s[i]>>t[i]>>d[i];
    }

    for(int i=0;i<v;i++)
    {
        dis[i]=inf;
    }
    dis[r]=0;

    for(int i=0;i<v-1;i++)
    {
        int end=1;
        for(int j=0;j<e;j++)
        {
            if(dis[t[j]]>dis[s[j]]+d[j])
            {
                dis[t[j]]=dis[s[j]]+d[j];
                end=0;
            }
        }

        if(end==1)
            break;
    }

    for(int i=0;i<v;i++)
    {
        if(dis[i]==inf)
            cout<<"INF"<<endl;
        else
            cout<<dis[i]<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main()
{
  int n = 0;
  fscanf(stdin, "%d", &n);
  for (int i = 1; i <= n; ++i) {
    if (i % 3 == 0) {
      fprintf(stdout, " %d", i);
    }
    else {
      for (int x = i; x > 0; x /= 10) {
        if (x % 10 == 3) {
          fprintf(stdout, " %d", i);
          break;
        }
      }
    }
  }
  fprintf(stdout, "\n");
  return 0;
}

#include <iostream>
#include <math.h>

using namespace std;

int main() {

	int n;
	cin >> n;

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

		if (i % 3 == 0) {
			cout << " " << i;
		}
		else {

			for (int j = 0; j < 4;j++) {

				int n;
				int m;

				n = pow(10, j + 1);
				m = n / 10;

				if (j == 0 && i % n == 3) {
					cout << " " << i;
					break;
				}else if (j != 0 && (i % n) / m == 3) {
					cout << " " << i;
					break;
				}
			}

		}
	}

	cout << endl;

	//cin >> /*変数他*/;
	//cout << /*文字列*/ << endl;
	return 0;
}


//khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

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

const int N = 1000000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

int n, Ans;
char S[N], T[N];
int bias, que[N], head, tail;

int main() {
	cin >> n;
	for (int i = 1; i <= n;i++) cin >> S[i];
	for (int i = 1; i <= n; i++) cin >> T[i];
	int ok = 1;
	for (int i = 1; i <= n; ++i) if (S[i] != T[i]) ok = 0;
	if (ok) return cout << 0, 0;
	head = 1, tail = 0;
	for (int i = n, j = n; i >= 1; --i) {
		if (j > i) j = i;
		while (head <= tail && que[head] + bias > i) ++head;
		Ans = std::max(Ans, tail - head + 2);
		if (i == 1 || T[i - 1] != T[i]) {
			while (j && S[j] != T[i]) --j;
			if (!j) return cout << -1, 0;
			--bias;
			que[++tail] = j - bias;
			--j;
		}
	}
	cout << Ans;
	return 0;
}

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>

#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
// #define int ll

using namespace std;
typedef long long ll;
typedef long double ld;

int get(vector<pair<int, int>> have) {
	if(len(have) == 0)
		return 0;
	if(len(have) == 1 && have[0].ft == have[0].sc)
		return 0;
	int ans = 1;
	vector<pair<int, int>> t(len(have));

	for (int i = len(have) - 1; i >= 0; i--) {
		int l = 0, r = len(have) - i;
		while(r - l > 1) {
			int m = (l + r) / 2;
			if(have[i + m].ft - m >= have[i].sc)
				r = m;
			else
				l = m;
		}
		ans = max(ans, r);
	}
	return max(1, ans);
}

signed main() {
	#ifdef PC
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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

    map<char, int> was;
    vector<int> r(n + 1, -1);
    vector<pair<int, int>> have;
    for (char c = 'a'; c <= 'z'; c++)
    	was[c] = -1;
    int uk = n;
    for (int i = n - 1; i >= 0; i--) {
    	if(uk > i)
    		uk--;
    	while(uk >= 0 && s[uk] != t[i])
    		uk--;
    	if(uk == -1) {
    		cout << -1;
    		return 0;
    	}
    	r[uk] = max(r[uk], i);
    }
    // for (int i = 0; i < n; i++) {
    // 	while(uk <= i && s[uk] != t[i])
    // 		uk++;
    // 	if(uk > i) {
    // 		cout << -1;
    // 		return -1;
    // 	}
    // 	r[uk] = i;
    // }
    // for (int i = 0; i < n; i++) {
    // 	was[s[i]] = i;
    // 	if(was[t[i]] == -1) {
    // 		cout << -1;
    // 		return 0;
    // 	}
    // 	r[was[t[i]]] = i;
    // }
    for (int i = 0; i < n; i++) {
    	if(r[i] == -1)
    		continue;
    	have.pb({i, r[i]});
    }
    int lr = -1, rr = -1;
   	vector<pair<int, int>> now;
    int ans = 0;
    for (int i = 0; i < len(have); i++) {
    	if(rr < have[i].ft) {
    		ans = max(ans, get(now));
    		now.clear();
    		now.pb(have[i]);
    		lr = have[i].ft, rr = have[i].sc;
    		continue;
    	}
    	now.pb(have[i]);
    	lr = have[i].ft, rr = have[i].sc;
    }
    ans = max(ans, get(now));
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
struct SegmentTree {
  int sz;
  vector<int> d, m, dm;
  SegmentTree(string s) {
    int n = (int)(s.size());
    sz = 1;
    while(sz < n) sz *= 2;
    d.clear();
    m.clear();
    dm.clear();
    d.resize(2*sz+1, 0);
    m.resize(2*sz+1, 0);
    dm.resize(2*sz+1, 0);
    for(int i=0;i<n;++i) {
      if(s[i] == 'D') {
        d[i+sz-1] = 1;
      } else if(s[i] == 'M') {
        m[i+sz-1] = 1;
      }
    }
    for(int i=sz-2;i>=0;--i) {
      d[i] = d[2*i+1] + d[2*i+2];
      m[i] = m[2*i+1] + m[2*i+2];
      dm[i] = dm[2*i+1] + dm[2*i+2] + d[2*i+1] * m[2*i+2];
    }
  }
  tuple<int, int, int> query(int a, int b, int l, int r, int k) {
    if(b <= l || r <= a) return make_tuple(0, 0, 0);
    if(a <= l && r <= b) return make_tuple(d[k], m[k], dm[k]);
    tuple<int, int, int> v1 = query(a, b, l, (l+r)/2, 2*k+1);
    tuple<int, int, int> v2 = query(a, b, (l+r)/2, r, 2*k+2);
    int resd = get<0>(v1) + get<0>(v2);
    int resm = get<1>(v1) + get<1>(v2);
    int resdm = get<2>(v1) + get<2>(v2) + get<0>(v1) * get<1>(v2);
    return make_tuple(resd, resm, resdm);
  }
  tuple<int, int, int> query(int a, int b) {
    return query(a, b, 0, sz, 0);
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n; cin >> n;
  string s; cin >> s;
  vector<int> su(n);
  su[0] == s[0] == 'M';
  for(int i=1;i<n;++i) {
    su[i] = su[i-1] + (s[i] == 'M');
  }
  int q; cin >> q;
  for(int i=0;i<q;++i) {
    int k; cin >> k;
    ll cntd = 0, cntm = 0, now = 0, ans = 0;
    for(int j=0;j<k;++j) {
      if(s[j] == 'D') {
        cntd++;
      } else if(s[j] == 'M') {
        now += cntd;
      } else if(s[j] == 'C') {
        ans += now;
      }
    }
    for(int j=k;j<n;++j) {
      if(s[j-k] == 'D') {
        cntd--;
        now -= su[j-1] - su[j-k];
      }
      if(s[j] == 'D') {
        cntd++;
      } else if(s[j] == 'M') {
        now += cntd;
      } else if(s[j] == 'C') {
        ans += now;
      }
    }
    cout << ans << endl;
  }
}

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;
using PP = std::pair<P, P>;

constexpr ll INF = 1ll<<60;

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

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

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

ll H, W;
std::vector<P> ps;
ll a[510][510];
bool used[510][510];
std::vector<PP> ans;

int main()
{
  std::cin >> H >> W;

  std::vector<P> ps;

  rep( i, H ) rep( j, W )
    std::cin >> a[i][j];

  ll dir = 1;
  ll ci = 0, cj = 0;

  while( true ) {
    ll ni, nj;

    if( (dir==1 && cj == W-1) || (dir==-1 && !cj) ) {
      dir *= -1;
      ni = ci+1, nj = cj;
    } else {
      ni = ci, nj = cj+dir;
    }

    if( used[ni][nj] || ci >= H )
      break;

    if( (a[ci][cj]&1) && ni < H ) {
      --a[ci][cj];
      ++a[ni][nj];

      ans.emplace_back( P( ci+1, cj+1 ), P( ni+1, nj+1 ) );
    }

    ci = ni;
    cj = nj;
  }

  std::cout << ans.size() << std::endl;

  for( auto pp : ans )
    printf( "%lld %lld %lld %lld\n", pp.first.first, pp.first.second, pp.second.first, pp.second.second );

  return 0;
}


#include<bits/stdc++.h>
using ll=long long;
#define rep(i,j,n) for(ll i=(j);i<(n);i++)
#define per(i,n,j) for(ll i=(n);i>=(j);i--)
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF=1LL<<60;

ll l,r;

int main(){
    cin>>l>>r;
    ll ans=INF;
    rep(i,l,min(l+2019,r)){
        rep(j,l+1,min(l+2020,r+1)){
            ll num=(i*j)%2019;
            chmin(ans,num);
        }
    }
    /*if(l%2019==0 ||l%2019>=r%2019 || r-l>=2019) ans=0;
    else{
        ll num;
        num=l%2019*(l+1)%2019;
        if(num<2019) ans=num;
        else ans=num%2019;
    }*/
    cout<<ans<<"\n";
    return 0;
}

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

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

int main(){
    int l, r;
    cin >> l >> r;

    if (r - l + 1 >= 2019) {
        cout << 0 << endl;
        return 0;
    }
    ll res = INF;
    for (ll i = l; i < r; ++i) {
        for (ll j = l+1; j <= r; ++j) {
            chmin(res, (i*j) % 2019);
        }
    }
    cout << res << endl;
}