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

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
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; }

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

ll n, m;

struct numinfo {
	int digit;
	int match;
	int min_match;
	int n;
};

vector<numinfo> nums;

bool dfs( int i, int remain_match, int remain_keta ){
	if ( i == SZ(nums) ){
		eprintf( "remain=%d %d   ", remain_match, remain_keta );
		rep(k,SZ(nums)){
			eprintf( "%d ", (int)nums[k].n );
		}
		eprintf( "\n" );
		if ( remain_match == 0 && remain_keta <= 0 )
			return true;
		return false;
	}
	for ( int k = remain_match/nums[i].match; k >= 0; k-- ){
		nums[i].n = k;
		int creatable_keta = (remain_match-k*nums[i].match)/nums[i].min_match;
		if ( remain_keta-k-creatable_keta > 0 )
			continue;
		if ( dfs( i+1, remain_match-k*nums[i].match, remain_keta-k ) )
			return true;
	}
	return false;
}

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

	vector<int> a(m);
	rep(i,m){
		cin >> a[i];
	}
	sort(a.rbegin(),a.rend());

	int digit_to_match[] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
	set<int> s;
	rep(i,m){
		int match = digit_to_match[a[i]];
		if ( s.count(match) == 0 )
			nums.push_back({a[i],match,100,0});
		s.insert(match);
	}
	m = nums.size();

	int min_match = 100;
	for ( int i = nums.size()-1; i >= 0; i-- ){
		nums[i].min_match = min_match;
		chmin(min_match, nums[i].match);
		eprintf( "digit,=%d match=%d min_match=%d\n", nums[i].digit, nums[i].match, nums[i].min_match );
	}

	for ( int keta = n / min_match; keta > 0; keta-- ){
		eprintf( "\n\nketa=%d\n", keta );
		if ( dfs( 0, n, keta ) ){
			sort(nums.begin(), nums.end(), [](numinfo &l, numinfo &r){
				return l.digit>r.digit;
			});
			rep(i,m){
				rep(j,nums[i].n){
					cout << nums[i].digit;
				}
			}
			cout << endl;
			return 0;
		}
	}

	return 0;
}

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

int cost[] = {0,2,5,5,4,5,6,3,7,6};
bool d[10] = {};
int main() {
    int n, m;
    cin >> n >> m;
    for (int i=0; i<m; i++) {
        int a;
        cin >> a;
        d[a] = 1;
    }

    vector<string> dp(n+1, "");
    for (int j=0; j<=n; j++) {
        for (int i=1; i<10; i++) {
            if ( !d[i] ) continue;
            if ( j+cost[i]>n ) continue;
            if ( j && dp[j]=="" ) continue;
            string s=dp[j]+(char)(i+'0');
            if ( s.length()==dp[j+cost[i]].length() ) {
                dp[j+cost[i]]=max(s, dp[j+cost[i]]);
            } else if ( s.length()>dp[j+cost[i]].length() ) {
                dp[j+cost[i]]=s;
            }
        }
    }

    cout << dp[n] << '\n';
    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout <<(#x)<<" is "<<(x)<<endl
#define debug cout <<"hi"<<endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD=1e9+7;
const int INF32=1<<30;
const ll INF64=1LL<<60;

void solve(){
  int n; cin >>n;
  ll a[n],cnt=0,sum=0,mn=MOD;
  for(int i=0; i<n; i++){
    ll x; cin >>x; a[i]=x;
    if(x<=0) cnt++;
    mn=min(mn,abs(x));
    sum+=abs(x);
  }
  if(cnt%2) sum-=2*mn;
  cout <<sum;
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  solve();
return 0;}

#include <iostream>
using namespace std;
typedef long long int LLI;
int main()
{
	int n;
	LLI a, b, x, diff, sum = 0 ;
	cin >> n >> x;
	cin >> a >> b;
	if( a + b > x )
	{
		diff = a + b - x;
		if( diff <= b )
	    	b -= diff;
	    else
	    	b = 0;
		sum += diff;
	}
	a = b;
	for(int i = 2; i < n; i++)
	{
	    cin >> b;
	    if( a + b > x )
		{
			diff = a + b - x;
			if( diff <= b )
		    	b -= diff;
		    else
		    	b = 0;
			sum += diff;
		}
		a = b;
	}
	cout << sum;
}


#include<iostream>
#include<cstdlib>
#include<queue>
#define MAX 22
#define BLOCK 1
#define START 2
#define GOAL 3
#define REP(i,a,e) for(int i=(a);i<=(e);++i)
#define REPD(i,a,e) for(int i=(a);i>=(e);--i)
using namespace std;
struct Map{
	Map(){
		REP(i,0,MAX-1)REP(j,0,MAX-1)map[i][j]=0;
	}
	int h,w;
	char map[MAX][MAX];
	int depth;
	pair<int, int> stone;
};
int BFS(Map &m){
	queue<Map> q;

	q.push( m );
	while( !q.empty() ){
		Map t = q.front();
		q.pop();

		/* up */
		REPD(i,t.stone.first,0){
			if( t.map[i][ t.stone.second ] == BLOCK ){
				if( abs(i-t.stone.first) > 1 ){
					Map tt = t;
					tt.map[ i ][ t.stone.second ] = 0;
					tt.stone = make_pair( i + 1, t.stone.second );
					if( ++tt.depth < 10 )
						q.push( tt );
				}
				break;
			}else if( t.map[i][ t.stone.second ] == GOAL ){
				return t.depth+1;
			}
		}
		/* down */
		REP(i,t.stone.first,t.h+1){
			if( t.map[i][ t.stone.second ] == BLOCK ){
				if( abs(i-t.stone.first) > 1 ){
					Map tt = t;
					tt.map[ i ][ t.stone.second ] = 0;
					tt.stone = make_pair( i - 1, t.stone.second );
					if( ++tt.depth < 10 )
						q.push( tt );
				}
				break;
			}else if( t.map[i][ t.stone.second ] == GOAL ){
				return t.depth+1;
			}
		}
		/* right */
		REP(j,t.stone.second,t.w+1){
			if( t.map[ t.stone.first ][ j ] == BLOCK ){
				if( abs(j-t.stone.second) > 1 ){
					Map tt = t;
					tt.map[ t.stone.first ][ j ] = 0;
					tt.stone = make_pair( t.stone.first, j - 1 );
					if( ++tt.depth < 10 )
						q.push( tt );
				}
				break;
			}else if( t.map[ t.stone.first ][ j ] == GOAL ){
				return t.depth+1;
			}
		}
		/* left */
		REPD(j,t.stone.second,0){
			if( t.map[ t.stone.first ][ j ] == BLOCK ){
				if( abs(j-t.stone.second) > 1 ){
					Map tt = t;
					tt.map[ t.stone.first ][ j ] = 0;
					tt.stone = make_pair( t.stone.first, j + 1 );
					if( ++tt.depth < 10 )
						q.push( tt );
				}
				break;
			}else if( t.map[ t.stone.first ][ j ] == GOAL ){
				return t.depth+1;
			}
		}
	}
	return -1;
}
int main(){
	while(true){
		Map m;
		scanf("%d%d", &m.w, &m.h);
		if( m.w==0 && m.h==0 )break;

		REP(i,1,m.h)REP(j,1,m.w){
			scanf("%d", &m.map[i][j]);
			if( m.map[i][j] == START ){
				m.stone=make_pair(i,j);
			}
		}
		m.depth = 0;

		printf("%d\n", BFS( m ));
	}
}

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

const int INF = 1 << 30;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

int w, h, field[20][20];

int solve(int x, int y, int t)
{
  if (t > 9)
    return INF;

  int ans = INF;
  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx < 0 || xx >= w || yy < 0 || yy >= h || field[yy][xx] == 1)
      continue;

    bool infield = true;
    while (field[yy][xx] == 0) {
      xx += dx[i];
      yy += dy[i];
      if (xx < 0 || xx >= w || yy < 0 || yy >= h) {
	infield = false;
	break;
      }
    }
    if (!infield)
      continue;

    if (field[yy][xx] == 3)
      return t+1;

    if (field[yy][xx] == 1) {
      field[yy][xx] = 0;
      ans = min(ans, solve(xx-dx[i], yy-dy[i], t+1));
      field[yy][xx] = 1;
    }
  }
  return ans;
}

int main()
{
  while (scanf("%d %d", &w, &h) != EOF) {
    if (w == 0 && h == 0)
      break;

    int x, y;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
	scanf("%d", &field[i][j]);
	if (field[i][j] == 2) {
	  x = j;
	  y = i;
	  field[i][j] = 0;
	}
      }
    }

    int ans = solve(x, y, 0);
    if (ans == INF)
      ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const int MAX = 2000000;
const int MOD = 998244353;
Int fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
Int COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main()
{
    COMinit();
    int N, M; cin >> N >> M;
    Int a = 0, b = 0, c = 0;
    for (int o = M % 2; o <= M; o += 2) {
        int t = (3 * M - o) / 2;
        a += COM(t + N - 1, N - 1) * COM(N, o);
        a %= MOD;
    }
    for (int o = M % 2; o <= M; o += 2) {
        int t = (3 * M - o) / 2;
        b += N * COM((t - M) + N - 1, N - 1) % MOD * COM(N - 1, o - 1);
        b %= MOD;
    }
    for (int o = M % 2; o <= M; o += 2) {
        int t = (3 * M - o) / 2;
        c += N * COM((t - M - 1) + N - 1, N - 1) % MOD * COM(N - 1, o);
        c %= MOD;
    }
    cout << (a + MOD - b + MOD - c) % MOD << '\n';
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
const int mod = 998244353;
typedef long long ll;
#define int long long
int fac[maxn], inv[maxn];
int n, m;
int pw(int a, int n) {
	int res = 1;
	while(n) {
		if(n & 1) res = (ll)res * a % mod;
		a = (ll)a * a % mod, n >>= 1;
	}
	return res;
}
int INV(int a) {
	return pw(a, mod - 2);
}
void pre() {
	fac[0] = 1;
	for(int i = 1; i < maxn; i++) fac[i] = (ll)fac[i - 1] * i % mod;
	inv[maxn - 1] = INV(fac[maxn - 1]);
	for(int i = maxn - 2; i >= 0; i--) inv[i] = (ll)inv[i + 1] * (i + 1) % mod;
}
int C(int n, int m) {
	return (ll)fac[n] * inv[m] % mod * (ll)inv[n - m] % mod;
}
int H(int n, int m) {
	return C(n + m - 1, m);
}
void add(int& a, int b) {
	a += b;
	if(a >= mod) a -= mod;
}
void sub(int& a, int b) {
	a -= b;
	if(a < 0) a += mod;
}
int32_t main() {
	pre();
	scanf("%lld%lld", &n, &m);
	int ans = 0;
	for(int i = 0; i <= min(n, m); i++) if((3 * m - i) % 2 == 0) {
		add(ans, C(n, i) * H(n, (3 * m - i) / 2) % mod);
	}
	int tmp = 0;
	for(int i = 0; i <= min(n, m); i++) if((m - i) % 2 == 0) {
		add(tmp, C(n, i) * H(n, (m - i) / 2) % mod);
	}
	for(int i = 0; i <= min(n - 1, m); i++) if((m - i) % 2 == 0) {
		sub(tmp, C(n - 1, i) * H(n - 1, (m - i) / 2) % mod);
	}
	tmp = (ll)tmp * n % mod;
	sub(ans, tmp);
	printf("%lld\n", ans);
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef tuple<int, int, int> TIII;
typedef vector<int> VI;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int MN = 1e9;

int main()
{
    fast_io();

    map<PII, int> M;

    int N;
    scanf("%d", &N);
    REP(i, N)
    {
        double A;
        scanf("%lf", &A);
        LL llA = llround(A * MN);

        // wylicz ile zawieraja 2 i 5
        int x = 0, y = 0;
        while (llA % 2 == 0)
        {
            x++;
            llA /= 2;
        }
        while (llA % 5 == 0)
        {
            y++;
            llA /= 5;
        }

        x = min(x, 18), y = min(y, 18);
        M[MP(x, y)]++;
    }

    LL res = 0;
    // zobacz ile jest par, ktore maja sume x >= 0 i sume y >= 0
    for (const auto &p : M)
    {
        if (p.first.first >= 9 && p.first.second >= 9)
            res += p.second*(LL)(p.second - 1) / 2;

        auto it = M.find(p.first); it++;
        while (it != M.end())
        {
            if (p.first.first + it->first.first >= 18 && p.first.second + it->first.second >= 18)
                res += p.second*(LL)it->second;

            it++;
        }
    }

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

    return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <iterator>
#include <stack>
#include <string.h>
#include <cstdlib>
#include <queue>
#include <list>
#include <string>

using namespace std;

int main()
{
    int a;
    int b[3];
    string s;

    cin >> s;
    a = s[0] - '0';
    for (int i = 1;i <= 3;i++){
        b[i-1] = s[i] - '0';
    }

    int f[3];
    int c[3];
    for (int i = 0;i < (1<<3);i++){
        for (int j = 0;j < 3;j++){
            if ((i>>j)%2){
                f[2-j] = 1;
                c[2-j] = b[2-j];
            }
            else {
                f[2-j] = 0;
                c[2-j] = -b[2-j];
            }
        }
        if (a+c[0]+c[1]+c[2] == 7){
            break;
        }
    }
    cout << a;
    for (int i = 0;i < 3;i++){
        if (f[i]){
            cout << "+" << b[i];
        }
        else {
            cout << "-" << b[i];
        }
    }
    cout << "=7" << endl;
}

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

using namespace std;

int main(int argc, char const* argv[])
{
	std::string s;
	std::cin >> s;
	std::reverse(s.begin(),s.end());
	std::cout << s << std::endl;
	return 0;
}


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

int main(){
  int64_t n,k;
  cin >> n >> k;
  if(k!=0){
    cout << "0\n";
    return 0;
  }
  vector<int64_t>a(n-1);
  for(int i=0;i<n-1;i++){
    cin >> k;
    if(k==0){
      cout << "0\n";
      return 0;
    }
    a.at(k-1)++;
  }
  int64_t ans=1;
  for(int i=0;i<n-2;i++){
    int64_t m=1;
    for(int j=0;j<a.at(i+1);j++){
      m*=a.at(i);
      m%=998244353;
    }
    ans*=m;
    ans%=998244353;
  }
  cout << ans << endl;
  return 0;
}

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

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cerr << #a << " = " << a << endl;

template<typename T> void setmax(T& x, T y) {x = max(x, y);}
template<typename T> void setmin(T& x, T y) {x = min(x, y);}

const double PI = acos(-1.0);
const LL INF = 1e9 + 47;
const LL LINF = INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

	int n;
	cin >> n;
	vector<int> d(n);
	FOR(i, 0, n) cin >> d[i];
	const int mod = 998244353;
	bool ok = d[0] == 0;
	FOR(i, 1, n) ok &= d[i] > 0;
	if (!ok){
		cout << 0 << endl;
		return 0;
	}

	vector<int> cnt(n, 0);
	for(auto i: d) cnt[i]++;
	while(cnt.back() == 0) cnt.pop_back();
	for(auto i: cnt) if (!i){
		cout << 0 << endl;
		return 0;
	}

	int ans = 1;
	FOR(i, 1, SZ(cnt)) FOR(j, 0, cnt[i]) ans = (ans * (LL) cnt[i - 1]) % mod;
	cout << ans << endl;

	cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
    return 0;
}


#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()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

void solve() {
	int h, w;
	cin >> h >> w;
	vector<string> v(h);
	rep(i,h) cin >> v[i];
	vector<vector<int>> d(h, vector<int>(w, INF));
	queue<pii> q;
	auto update = [&](int y, int x, int nd) {
		if (d[y][x] != INF) return;
		d[y][x] = nd;
		q.push(pii(y, x));
	};

	update(0, 0, 1);
	while (!q.empty()) {
		auto p = q.front(); q.pop();
		rep(i,4) {
			int ny = p.first + dy[i];
			int nx = p.second + dx[i];
			if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
			if (v[ny][nx] == '#') continue;
			update(ny, nx, d[p.first][p.second] + 1);
		}
	}
	if (d[h-1][w-1] == INF) {
		cout << -1 << endl;
	} else {
		int cs = 0;
		rep(i,h) rep(j,w) if (v[i][j] == '#') cs++;
		// cout << h * w << " " << cs << " " << d[h-1][w-1] << endl;
		cout << h * w - (cs + d[h-1][w-1]) << endl;
	}
}

int main() {
	solve();
	return 0;
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define debug(s, param) std::cerr << s << param << std::endl;

using namespace std;
using ll = long long;

const ll MOD = 1000000007;

int bfs(const vector<string>& grids, int h, int w) {
  vector<vector<int>> deltas{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  vector<vector<bool>> visit(h, vector<bool>(w, false));
  queue<pair<int, int>> q;
  auto isAvailable = [h, w](int row, int col) { return (row >= 0 && row < h && col >= 0 && col < w); };
  q.emplace(0, 0);
  visit[0][0] = true;
  int steps = 0;
  while (!q.empty()) {
    int size = (int)q.size();
    rep (i, size) {
      int row = q.front().first, col = q.front().second;
      if (row == h - 1 && col == w - 1) return steps;
      q.pop();
      for (const auto& delta : deltas) {
        int nextRow = row + delta[0], nextCol = col + delta[1];
        if (isAvailable(nextRow, nextCol) && grids[nextRow][nextCol] == '.' && !visit[nextRow][nextCol]) {
          visit[nextRow][nextCol] = true;
          q.emplace(nextRow, nextCol);
        }
      }
    }
    ++steps;
  }
  return -1;
}

int main() {
  int h, w;
  cin >> h >> w;
  int blackNums = 0;
  vector<string> grids(h, "");
  rep (i, h) {
    cin >> grids[i];
    rep (j, w) if (grids[i][j] == '#') ++blackNums;
  }
  int res = bfs(grids, h, w);
  if (res == -1) cout << -1 << endl;
  else  cout << h * w - (blackNums + res + 1) << endl;
  return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--)
#define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++)
#define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--)
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll INF = numeric_limits<ll>::max()/4;
const int n_max = 1e5+10;

int main(){
    ll n; cin >> n;
    string s;cin >> s;
    ll q; cin >> q;
    vector<ll> k(q);
    rep(i,q) cin >> k[i];
    auto f = [](auto a, auto b){
        return a + b;
    };

    rep(i,q){
        ll kt = k[i];
        ll cnt = 0;
        ll ans = 0;
        ll num = 0;
        ll d_num = 0, m_num = 0;
        rep(i,n){
            num++;
            if(s[i] == 'D')d_num++;
            if(s[i] == 'M'){
                m_num++;
                cnt += d_num;
            }
            if(num > kt){
                if(s[i-kt] == 'M')m_num--;
                if(s[i-kt] == 'D'){
                    d_num--;
                    cnt -= m_num;
                }
                num--;
            }
            if(s[i] == 'C')ans += cnt;
            // debug(i);debug(s[i]);debug(cnt);debug(ans);
        }
        cout << ans << endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll rm[2000005]={0},rc[2000005]={0},rmc[2000005]={0};
ll n;
string s;
ll solve(ll k){
  ll res=0;
  for(int i=1;i<=n;i++){
    if(s[i]=='D'){
      res+=rmc[i+k-1]-rmc[i];
      res-=rm[i]*(rc[i+k-1]-rc[i]);
    }
  }
  return res;
}
int main(){
  cin>>n;
  s.push_back('A');
  string ks;
  cin>>ks;
  s+=ks;

  for(int i=1;i<=n;i++){
    rm[i]=rm[i-1];
    rc[i]=rc[i-1];
    rmc[i]=rmc[i-1];
    if(s[i]=='M') rm[i]++;
    if(s[i]=='C') {
      rc[i]++;
      rmc[i]+=rm[i];
    }
  }
  for(int i=n+1;i<=n*2;i++){
    rm[i]=rm[i-1];
    rc[i]=rc[i-1];
    rmc[i]=rmc[i-1];
  }
  ll q;
  cin>>q;
  for(int i=0;i<q;i++){
    ll k;
    cin>>k;
    cout<<solve(k)<<endl;
  }
}


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

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	const int inf = 1e9 + 1e8;
	int n, k; cin >> n >> k;
	vector<int> x(n);
	int pos = 0;
	bool flag = false;
	for (int i = 0; i < n; i++) {
		cin >> x[i];
		if (x[i - 1] < 0 && x[i] > 0) pos = i;
		if (x[i] == 0) {
			pos = i;
			k--;
			flag = true;
		}
	}
	if (x[pos] < 0) pos = n;
	if (flag) {
		for (int i = pos; i + 1 < n; i++) x[i] = x[i + 1];
		n--;
	}
	int ans = k == 0 ? 0 : inf;
	for (int i = 0; i < n; i++) {
		if (i < pos) {
			int cnt = pos - i;
			if (cnt >= k) {
				ans = min(ans, -x[i]);
			} else {
				if (k + i - 1 < n) ans = min(ans, -2 * x[i] + x[k + i - 1]);
			}
		} else {
			int cnt = i - pos + 1;
			if (cnt >= k) {
				ans = min(ans, x[i]);
			} else {
				if (i - k + 1 >= 0) ans = min(ans, 2 * x[i] - x[i - k + 1]);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

#include <cstdio>
#include <cmath>
#define buf 10000
using namespace std;
int k=0;
int B[buf];
void Answer(int n);
int PrimeNumber(int n);

int main(void){
  int i,n;
  char str[buf];

  for(i=2;i<buf;i++)
    PrimeNumber(i);
  for( ;fgets(str,sizeof(str),stdin)!=NULL; ){
    sscanf(str,"%d",&n);
    if(n==0) break;
    Answer(n);
  }
  return 0;
}

void Answer(int n){
  int i,j;
  for(i=0;i<k;i++){
    if(B[i]>n)
        break;
  }
  for(j=i-1;j>=0;j--){
    if(B[j]-B[j-1]==2){
      printf("%d %d\n",B[j-1],B[j]);
      break;
    }
  }
}

int PrimeNumber(int n){
  int i,c;
  if(n==2){
    B[k]=n;
    k++; return 0;
  }
  if(n<2 || n%2==0)
    return 0;
  c=sqrt(n);
  for(i=3;i<=c;i+=2){
    if(n%i==0)
      return 0;
  }
  B[k]=n;
  k++; return 0;
}


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

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<int> a,q;
    int b;
    for(int i=0;i<4;i++){
        cin >> b;
        a.push_back(b);
    }
    for(int i=0;i<2;i++){
        cin >> b;
        q.push_back(b);
    }
    sort(a.begin(),a.end(),greater<int>());
    sort(q.begin(),q.end(),greater<int>());
    int ans=0;
    for(int i=0;i<3;i++){
        ans+=a[i];
    }
    ans+=q[0];
    cout << ans << endl;

    return 0;
}

#include <iostream>
using namespace std;

int main() {
  int A, B, C, D, E, F;
  cin >> A >> B >> C >> D >> E >> F;

  int sum = 0;

  if (A <= B && A <= C && A <= D) sum += B + C + D;
  else if (B <= A && B <= C && B <= D) sum += A + C + D;
  else if (C <= A && C <= B && C <= D) sum += A + B + D;
  else sum += A + B + C;

  if (E <= F) sum += F;
  else sum += E;

  cout << sum << endl;

  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

const int INF = 2147483647;

struct SegmentTree {
    int n;
    vector<int> heap;

    SegmentTree(int n): n(n) {
        heap.assign(4 * n, INF);
    }

    void update(int nodeId, int l, int r, int ql, int qr, int v) {
        if (qr < l || ql > r) {
            return;
        }
        else if (ql <= l && r <= qr) {
            heap[nodeId] = v;
        }
        else {
            pushdown(nodeId);
            int m = l + (r - l)/2;
            update(nodeId * 2, l, m, ql, qr, v);
            update(nodeId * 2 + 1, m + 1, r, ql, qr, v);
        }
    }

    int query(int nodeId, int l, int r, int q) {
        if (q < l || q > r) {
            return -1;
        }
        else if (heap[nodeId] != -1) {
            return heap[nodeId];
        }
        else {
            int m = l + (r - l)/2;
            return max(query(nodeId * 2, l, m, q), query(nodeId * 2 + 1, m + 1, r, q));
        }
    }

    void pushdown(int nodeId) {
        //cout << "push: " << nodeId << endl;
        if (heap[nodeId] != -1) {
            heap[nodeId * 2] = heap[nodeId];
            heap[nodeId * 2 + 1] = heap[nodeId];
            heap[nodeId] = -1;
        }
    }

};

int main() {
    int n, q;
    cin >> n >> q;
    SegmentTree st(n);
    for (int i = 0; i < q; i++) {
        int cmd;
        cin >> cmd;
        if (cmd == 0) {
            int ql, qr, v;
            cin >> ql >> qr >> v;
            st.update(1, 0, n -1, ql, qr, v);
//            for (int i = 0; i < 4 * n; i++) {
//                cout << i << ":" << st.heap[i] << endl;
//            }
        }
        else {
            int pos;
            cin >> pos;
            cout << st.query(1, 0, n-1, pos) << endl;
        }
    }

    return 0;
}

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

using namespace std;

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

const ll mod = 1000000007;

template <class T>
struct prop_segtree {
  int n;
  vector<T> data;
  vector<bool> propFlag;

  prop_segtree(const int s) {
    init(s);
  }

  prop_segtree(const int s, const T u) {
    init(s);
    for (int i = 0; i < s; i++) {
      data[i+n-1] = u;
    }
    for (int i = n-2; i >= 0; i--) {
      data[i] = 0;
    }
  }

  prop_segtree(const vector<T> &v) {
    int s = v.size();
    init(s);
    for (int i = 0; i < s; i++) {
      data[i+n-1] = v[i];
    }
    for (int i = n-2; i >= 0; i--) {
      data[i] = 0;
    }
  }

  void init(const int s) {
    n = 1;
    while (n < s) n <<= 1;
    data = vector<T>(2*n-1);
    propFlag = vector<bool>(2*n-1);
  }

  void propagate(int p, int a, int b) {
    if (propFlag[p]) {
      if (b - a > 1) {
        data[p*2+1] = data[p];
        data[p*2+2] = data[p];
        propFlag[p*2+1] = true;
        propFlag[p*2+2] = true;
        data[p] = 0;
      }
      propFlag[p] = false;
    }
  }

  void update(int l, int r, T v, int p = 0, int a = 0, int b = -1) {
    if (b < 0) b = n; // init

    // propagate value
    propagate(p, a, b);

    if (r <= a || b <= l) return; // out of range
    if (l <= a && b <= r) { // fully covered
      data[p] = v;
      propFlag[p] = true;
      propagate(p, a, b);
    }
    else {
      update(l, r, v, p*2+1, a, (a + b) / 2); // left
      update(l, r, v, p*2+2, (a + b) / 2, b); // right
    }
    return;
  }

  T query(int x, T p = 0, int a = 0, int b = -1) {
    if (b < 0) b = n; // init

    // propagate value
    propagate(p, a, b);

    if (b - a == 1) return data[p];// reached to bottom

    if (x < (a + b) / 2) return query(x, p*2+1, a, (a + b) / 2); // left
    else return query(x, p*2+2, (a + b) / 2, b); // right
  }
};

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

  int n, q;
  cin >> n >> q;
  vector<vector<int> > query(q, vector<int>(4));
  for (int i = 0; i < q; i++) {
    cin >> query[i][0] >> query[i][1];
    if (query[i][0] == 0) cin  >> query[i][2] >> query[i][3];
  }

  prop_segtree<int> pst(n, 0x7fffffff);
  vector<ll> ans;
  for (int i = 0; i < q; i++) {
    if (query[i][0] == 0) { // update
      pst.update(query[i][1], query[i][2]+1, query[i][3]);
    }
    else { // get
      ans.push_back(pst.query(query[i][1]));
    }
  }

  for (auto &x: ans) cout << x << endl;

  return 0;
}


#include <bits/stdc++.h>
#include <time.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P  = pair<ll,ll>;
using vi = vector<ll>;
using vv = vector<vi>;
const ll INF = 1001001001;
const ll NUM = 1<<10;

//(開始, 今)を管理
//例:
//(0,0), (1,1), (2,2), (3,3), (4,4), (5,5)
//壁(1,2)   .      .(該当領域)
//(0,0), (1,3), (2,3), (3,3), (4,4), (5,5) ->(1,3),(2,3)は消す
//(0,0),               (3,3), (4,4), (5,5)
//壁(2,3)                 .(該当領域)
//(0,0),               (3,4), (4,4), (5,5) ->(3,4)は消す
//(0,0),                      (4,4), (5,5)
//
//・順序集合(map)を使う: map[今]=開始の最大値
//・ans = min(今-開始), multisetで管理
//・該当領域+1の範囲で開始が一番右のものだけ残す
//・それ以外はansからもmapからも消す

//

int main(){
    int h,w;
    cin >> h >> w;
    map<int,int> s;
    multiset<int> m;
    rep(i,w)s[i] = i;
    rep(i,w)m.insert(0);

    rep(i,h){
        int l,r;
        cin>>l>>r;
        --l;

        auto itr = s.lower_bound(l);
        int maxstart= -1;
        while(itr!=s.end() && itr->first<=r){
            maxstart= max(maxstart, itr->second);
            int c = itr->first - itr->second;
            m.erase(m.find(c));
            s.erase(itr++);
            //itr=s.erase(itr);//これでもいい
            //s.erase(*itr); itr++;//これはだめ
        }
        if(r<w && maxstart != -1){
            s[r] = maxstart;
            m.insert(r-maxstart);
        }
        int ans = -1;
        if(!m.empty())
            ans = *m.begin() + (i+1);
        cout<<ans<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define hackcyborg shresth_walia
#define ll long long
#define ld double
#define pb push_back
#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int,null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
ll binpow(ll a,ll b,ll m=mod)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
        res=(a*res)%m;
        a=(a*a)%m;
        b/=2;
    }
    return res;
}
const int INF =1e9;
ll n;
vector<pair<ll,ll>> ar[200010];
void dijkstra(int s, vector<int> & d, vector<int> & p) {
    d.assign(n+1, INF);
    p.assign(n+1, -1);
    d[s] = 0;
    set<pair<int, int>> q;
    q.insert({0, s});
    while (!q.empty()) {
        int v = q.begin()->second;
        q.erase(q.begin());
        for (auto edge : ar[v]) {
            int to = edge.first;
            int len = edge.second;
            if (d[v] + len < d[to]) {
                q.erase({d[to], to});
                d[to] = d[v] + len;
                p[to] = v;
                q.insert({d[to], to});
            }
        }
    }
}
main()
{
    IO
    ll m;
    cin>>n>>m;
    ++n;
    ll a[n+1],b[n+1];
    for(int x=1;x<n;x++)
        {cin>>a[x]>>b[x];
        ++b[x];}
    b[n]=m;
    a[n]=1;
    set<pair<ll,ll>> ae;
    for(int x=1;x<=m;x++)
    {
         ae.insert({x,0ll});
    }
    for(int x=1;x<=n;x++)
    {
        auto it=ae.lower_bound(make_pair(a[x],-1ll));
        vector<pair<ll,ll>> as;
        if(it!=ae.end())
        while((it->first)<=(b[x]-1) && (it->first)>=a[x])
        {
            as.pb(*it);
            //cout<<it->second<<" "<<x<<"\n";
            ar[it->second].pb(make_pair(x,abs(it->first-b[x])+abs(it->second-x)));
            ++it;
            if(it==ae.end())break;
        }
        for(auto it : as)
            ae.erase(it);
        ae.insert(make_pair(b[x],x));
    }
    vector<ll> d,p;
    dijkstra(0,d,p);
    ll ans[n+1];
    for(int x=1;x<=n;x++)
        ans[x]=INF;
    for(int x=0;x<=n;x++)
    {   ans[x]=min(ans[x],d[x]);
        for(auto it : ar[x])
        {
            ans[it.first]=min(ans[it.first],d[x]+abs(x-it.first));
        }
    }
    ll mi=1e18;
    for(int x=n;x>=1;x--)
    {
        ans[x]=min(ans[x],mi+x);
        mi=min(mi,ans[x]-x);
    }
    for(int x=2;x<=n;x++)
         if(ans[x]<1e8)
        cout<<ans[x]-1<<"\n";
         else
            cout<<-1<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define VIEW(x) do {cerr << #x << ": "; for(auto i : x) cerr << i << " "; cerr << endl;} while(0)

struct CumulativeSum2D {
  vector<vector<long long>> csum;
  template<typename T>
  CumulativeSum2D(vector<vector<T>> input) : csum(input.size() + 1, vector<long long>(input[0].size() + 1, 0)) {
    for(int i = 0; i < input.size(); i++) for(int j = 0; j < input[i].size(); j++)
      csum[i+1][j+1] += csum[i+1][j] + csum[i][j+1] - csum[i][j] + input[i][j];
  }
  CumulativeSum2D(vector<string> input, char target) : csum(input.size() + 1, vector<long long>(input[0].size() + 1, 0)) {
    for(int i = 0; i < input.size(); i++) for(int j = 0; j < input[i].size(); j++)
      csum[i+1][j+1] += csum[i+1][j] + csum[i][j+1] - csum[i][j] + (input[i][j]==target);
  }
  long long query(int x_left, int y_top, int x_right, int y_bottom) { /* 0-indexed, inclusive */
    return csum[x_right+1][y_bottom+1] - csum[x_right+1][y_top] - csum[x_left][y_bottom+1] + csum[x_left][y_top];
  }
};

signed main() {
  int n,m,Q; cin >> n >> m >> Q;
  vector<vector<int>> grid(n, vector<int>(n, 0));
  rep(i,m) {
    int l,r; cin >> l >> r;
    grid[l-1][r-1]++;
  }
  CumulativeSum2D cs(grid);
  rep(i,Q) {
    int p,q; cin >> p >> q; --p,--q;
    cout << cs.query(p,p, q,q) << endl;
  }
}

#define _USE_MATH_DEFINES
#include <cmath>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cctype>
#define rep(i,start,n) for(int i=start;i<n;i++)
#define And &&
#define Or ||
#define Lb cout<<endl
using namespace std;

int main(void) {
	string str; cin >> str;
	int q; cin >> q;
	string meirei, x;
	int a, b;
	rep(i, 0, q) {
		cin >> meirei;
		cin >> a >> b;
		if (meirei == "replace") {
			cin >> x;
			rep(j, 0, x.length()) {
				str[a + j] = x[j];
			}
		}
		else if (meirei == "reverse") {
			rep(j, 0, (b - a + 1) / 2) {
				char temp;
				temp = str[a + j];
				str[a + j] = str[b - j];
				str[b - j] = temp;
			}
		}
		else {
			rep(j, 0, (b - a + 1)) {
				cout << str[a + j];
			}
			cout << endl;
		}
	}
}


#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <numeric>
#include <math.h>

using namespace std;

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

	int n, m, a, b, i, j;
	cin >> n >> m >> b >> a;
	for (i = 0; i < n; i++) {
		for (j = 0; j < m; j++) {
			if ((i < a) + (j < b) == 1) cout << "1";
			else cout << "0";
		}
		cout << "\n";
	}

	return 0;
}

#include<iostream>
#include<string>
#include<numeric>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define ceil(a,b) (((a)+(b)-1)/(b))
#define dist(a,b) max((a)-(b),(b)-(a))
#define yn(i) print((i)?"Yes":"No")
#define rep(i,n) for(int i=0;i<(n);i++)

#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}

int main(){
  int h,w; cin >>h >>w;
  int a,b; cin >>a >>b;
  int s=0;
  rep(i,h){
    if(i==b) s^=1;
    rep(j,w){
      if(j==a) s^=1;
      printf("%d",s);
    }
    printf("\n");
    s^=1;
  }
}


#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
 ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    set<int> s;
    for (int i = 0; i < k; i++) {
        int x;
        cin >> x;
        s.insert(x);
    }
    int a = n;
    while (1) {
        int c = a;
        bool ok = 1;
        while (c > 0) {
            int di = c % 10;
            if (s.count(di)) {
                ok = 0;
                break;
            }
            c /= 10;
        }
        if (ok) {
            cout << a << endl;
            break;
        }
        a++;
    }
	return 0;
}

#include <iostream>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    if(1<=(a&&b)<=100)
        {
            cout<<a*b;
        }
        return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 28)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int n;
int memo[32];
int dfs(int now){
	if(now > n)return false;
	if(now == n)return true;
	if(memo[now] != -1)return memo[now];
	int sum = dfs(now+1) + dfs(now+2) + dfs(now+3);
	return memo[now] = sum;
}

int main(void){
	while(cin >> n, n){
		fill_n(memo, 32, -1);
		cout << dfs(0)/3650+1 << endl;
	}

	return 0;
}

int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

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

#define int long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

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

template<typename T, typename U>
void print1(T Array, U size) {
  for (int i = 0; i < size; i++) {
    cout << Array[i] << " ";
  }
  cout << endl;
}

template<typename T>
void print(T Array, int height, int width) {
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      cout << " " << Array[i][j];
    }
    cout << endl;
  }
}

// ------------------------------------------------------------------------------------------

int n;
int dp[55];

int rec(int stage) {
  if (stage == 0) {
    return dp[stage] = 1;
  }

  if (dp[stage] != -1) {
    return dp[stage];
  }

  int ans = 0;

  for (int i = 1; i <= 3; i++) {
    if (stage - i < 0) continue;
    ans += rec(stage - i);
  }

  return dp[stage] = ans;
}

signed main() {
  fill(dp, dp+33, -1);
  rec(30);
  while (1) {
    cin >> n;
    if (n == 0) break;
    int t = (dp[n] + 3650 - 1) / 3650;
    cout << t << endl;
  }

  return 0;
}


#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <vector>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		std::vector<int> num;
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (; x; x /= 10) {
			num.push_back(x % 10);
		}
		while (!num.empty()) {
			putchar(num.back() ^ '0');
			num.pop_back();
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

void solve() {
	int h1, m1, h2, m2, k;
	read(h1), read(m1), read(h2), read(m2), read(k);
	int t1 = h1 * 60 + m1, t2 = h2 * 60 + m2;
	print(std::max(t2 - t1 - k, 0));
}

int main() {
	int T = 1;
	// read(T);
	while (T--) {
		solve();
	}
}

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

typedef long long LL;

int a,b,c,d,e;

int main(){
  scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
  printf("%d\n",max(c*60+d-a*60-b-e,0));
  return 0;
}


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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int A,B,M,p{100000},q{100000},mi{200000},x,y,c;
  cin >> A >> B >> M;
  vector<int> a(A), b(B);
  for (auto &e: a) {
    cin >> e;
    if(e<p) p = e;
  }
  for (auto &e: b) {
    cin >> e;
    if(e<q) q = e;
  }
  mi = p+q;
  while(cin >> x >> y >> c) {
    mi = min(mi,a[x-1]+b[y-1]-c);
  }
  cout << mi << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vb=vector<bool>;
using vvb=vector<vb>;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pll = pair<ll,ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for(int i=0;i<(int)n;i++)
#define rep2(i, s, n) for(int i=(s);i<(int)n;i++)
int INF=1e9+7;

int main() {
  int A,B,M;
  cin >> A >> B >> M;
  vi a(A),b(B),x(M),y(M),c(M);
  int mina=INF,minb=INF;
  rep(i,A) {
    cin >> a[i];
    mina=min(mina,a[i]);
  }
  rep(i,B) {
    cin >> b[i];
    minb=min(minb,b[i]);
  }
  rep(i,M) {
    cin >> x[i] >> y[i] >> c[i];
    x[i]--;y[i]--;
  }

  int ans=mina+minb;
  rep(i,M)
    ans=min(ans,a[x[i]]+b[y[i]]-c[i]);

  cout << ans << endl;
}


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

int main() {
  int64_t N, ans;
  cin >> N;
  for (int i=0; i<=sqrt(N); i++) {
    if (i*i<=N) {
      ans=i*i;
    }
  }
  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for(inti=a;i<=b;i++)

#define MOD 1000000007
typedef long long int ll;
typedef vector<int>vi;
typedef pair<int,int>pi;

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

int i = 0;
while(i*i<=n)
{
    i++;
}
cout<<(i-1)*(i-1)<<endl;
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int mod = 1e9 + 7;

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

ll inv (ll a) {
  return pwr (a, mod - 2);
}

ll nCr (ll n, ll r) {
  ll ans = 1;
  for (int i = 0; i < r; i++) ans = (ans * (n - i)) % mod;
  for (int i = 1; i <= r; i++) ans = (ans * inv (i)) % mod;

  return ans;
}

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

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

  ll ans = 0;
  for (int i = 1; i <= m; i++) {
    ans += (((ll)(m - i) * (m - i + 1)) / 2);
    ans %= mod;
  }
  ll a = (((ll)n * (n - 1)) + n) % mod;
  ans = (ans * a) % mod;
  ll tmp_ans = 0;
  for (int i = 1; i <= n; i++) {
    tmp_ans += (((ll)(n - i) * (n - i + 1)) / 2);
    tmp_ans %= mod;
  }
  ll b = (((ll)m * (m - 1)) + m) % mod;
  tmp_ans = (tmp_ans * b) % mod;

  ans = (ans + tmp_ans) % mod;

  ans = (ans * nCr ((ll)n * m - 2, k - 2)) % mod;

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

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

const int MAX = 1000000;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main()
{
    cout << fixed << setprecision(15);
    ll N, M, K;
    cin >> N >> M >> K;

    ll A = 1;
    A = (A*N)%MOD;
    A = (A*N)%MOD;
    A = (A*(M-1))%MOD;
    A = (A*M)%MOD;
    A = (A*(M+1))%MOD;

    ll B = 1;
    B = (B*M)%MOD;
    B = (B*M)%MOD;
    B = (B*(N-1))%MOD;
    B = (B*N)%MOD;
    B = (B*(N+1))%MOD;

    // 前処理
    COMinit();

    ll C = COM(N*M-2, K-2) % MOD;

    cout << ((((A+B)%MOD)* modinv(6, MOD) % MOD) * C) % MOD << endl;
    return 0;
}


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

int main() {
    string input;
    cin >> input;
    reverse(input.begin(), input.end());
    cout << input << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(15);
vector<int> forintinput(int n) {
	vector<int> x;
	for(int i = 0;i < n;i++){
		int ia;
		cin >> ia;
		x.push_back(ia);
	}
	return x;
}
int main(){
	int a,b,c;
	cin >> a >> b >> c;
	cout << min(a+b,min(b+c,c+a)) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
char field[12][12];
int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};

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

int main(void){
    char a;
    while(cin>>a){
        int ans=0;
        for(int i=0;i<12;i++){
            for(int j=0;j<12;j++){
                if(i==0&&j==0)field[0][0]=a;
                else cin>>field[i][j];
            }
        }
        for(int i=0;i<12;i++){
            for(int j=0;j<12;j++){
                if(field[i][j]=='1'){
                    dfs(i,j);ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define REP(var, a, b) for (int var = (a); var < (b); var++)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()
#define rALL(c) (c).rbegin(), (c).rend()
ll MOD = 1000000007;
const ll INF = 1LL << 62;
int main() {
  //
  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  ll s = 0;
  rep(i, n) s ^= a[i];
  ll ans = s;
  rep(i, n) { a[i] &= ~s; }
  int r = 0;
  for (int i = 60; i >= 0; i--) {
    int j = r;
    for (; j < n; j++) {
      if (a[j] >> i & 1) {
        swap(a[r], a[j]);
        break;
      }
    }
    if (j == n) continue;
    rep(k, n) {
      if (k == r) continue;
      if (a[k] >> i & 1) a[k] ^= a[r];
    }
    ++r;
  }

  ll x = 0;
  rep(j, r) {
    x = x ^ a[j];

  }
  ans += x * 2;
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   cin>>n;
    if(n<3)
        cout<<0;
    else if (n%3==0)
        cout<<n/3;
    else if(n%3!=0){
        while(true){
            n++;
            if(n%3==0){
                cout<<n/3-1;
             break;
            }
        }
    }
   return 0;
   }

#include<bits/stdc++.h>
//#define <- プログラムの文字列を置換する
//typedef <- 新しい型に名前を付けるためのキーワード

using namespace std;

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

const long long MOD = 1e9 + 7;
const long long INF = 1e15;
const int INF_INT = 1e9;

template<class TYPE>
struct edge{
    TYPE to,cost;
    edge(TYPE t,TYPE c){
        to = t;
        cost = c;
    }
};

int main(){
    int N;
    cin >> N;
    cout << N / 3 << endl;
    return 0;
}


//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n;
int arr[200005];

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

	cin>>n;
	rep(x,0,n) cin>>arr[x];

	int curr=0;
	ll ans=0;

	if (arr[0]!=0){
		cout<<"-1"<<endl;
		return 0;
	}

	rep(x,0,n){
		if (curr+1<arr[x]){
			cout<<"-1"<<endl;
			return 0;
		}
		else if (curr+1==arr[x]){
			ans++;
		}
		else{
			ans+=arr[x];
		}
		curr=arr[x];
	}

	cout<<ans<<endl;
}

#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
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; }

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

    ll n;
    cin>>n;
    vl as(n);
    cin>>as;

    if (n == 1) {
        if (as[0]) cout<<-1<<endl;
        else cout<<0<<endl;
        return 0;
    }

    ll ans = as.back();
    for (int i = n - 2; i >= 0; --i) {
        if (as[i] > i) {
            cout<<-1<<endl;
            return 0;
        }
        if (as[i] + 1 < as[i + 1]) {
            cout<<-1<<endl;
            return 0;
        }
        if (as[i] + 1 == as[i + 1]) continue;
        ans += as[i];
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define M ((ll)(1e9)+7)
#define F first
#define S 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;
ll power(ll x,ll y)
{
    if(y==0)
    return 1;
    ll res=power(x,y/2);
    res=(res*res)%M;
    if(y&1)
    res=(res*x)%M;
    return res;
}
void solve()
{
    ll h,w,a,b,va;
    cin>>h>>w>>a>>b;
    a=h-a+1;
    ll ta=a-1,tb=b+1;
    ll fact[200001];fact[0]=1;
    for(ll i=1;i<200001;i++)
    fact[i]=(fact[i-1]*i)%M;
    vector<ll> v,v1;
    for(ll i=tb;i<=w;i++)
    {
        va=fact[i+ta-2];
        va=(va*power(fact[i-1],M-2))%M;
        va=(va*power(fact[ta-1],M-2))%M;
        v.pb(va);
    }
    for(ll i=tb;i<=w;i++)
    {
        va=fact[h+w-a-i];
        va=(va*power(fact[h-a],M-2))%M;
        va=(va*power(fact[w-i],M-2))%M;
        v1.pb(va);
    }
    //cout<<v[0]<<" "<<v[1]<<" "<<v1[0]<<" "<<v1[1]<<endl;
    ll ans=0;
    for(int i=0;i<v.size();i++)
    {
        ans=(ans+(v[i]*v1[i])%M)%M;
    }
    cout<<ans<<endl;
}
int main()
{
    solve();
}

#include<cstdio>
typedef long long ll;
const ll mod=998244353;
inline ll rev(ll x)
{ll k=mod-2,r=1;for(;k;k>>=1,(x*=x)%=mod)if(k&1)(r*=x)%=mod;return r;}
ll fac[1111111],inv[1111111];
inline ll C(int n,int r)
{return((((fac[n]*inv[r])%mod)*inv[n-r])%mod);}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	if(n<m)n^=m,m^=n,n^=m;
	fac[0]=inv[0]=1;
	register int i;
	for(i=1;i<=n+m;i++)fac[i]=(fac[i-1]*i)%mod;
	inv[n+m]=rev(fac[n+m]);
	for(i=n+m-1;i;i--)inv[i]=inv[i+1]*(i+1)%mod;
	ll ans=0;
	for(i=1;i<=m;i++)
		(ans+=C(i*2,i)*C(n+m-2*i,n-i))%=mod;
	(((ans*=rev(2*C(n+m,n)))%=mod)+=n+mod)%=mod;
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
const int MAX_W=20;
const int DIRECTIONS=4;//0:up/1:right/2:down/3:left
const int MAX_DEPTH=10;
const int INF=1<<27;

struct tpl{
  int ary[MAX_W][MAX_W];
  int depth;
  int last;
  int x;
  int y;
};

int w,h;
int dfs(const tpl& t){
  int minDepth=INF;
  if(t.depth+1>MAX_DEPTH)return minDepth;
  REP(i,DIRECTIONS){
    tpl u;
    u.depth=t.depth+1;
    u.last=i;
    REP(j,h)REP(k,w)u.ary[j][k]=t.ary[j][k];
    u.x=t.x;
    u.y=t.y;
    if(i==0&&u.y>0&&u.ary[u.y-1][u.x]!=1){
      for(;u.y>=0;u.y--){
	if(u.ary[u.y][u.x]==3){
	  minDepth=min(minDepth,u.depth);
	  break;
	}else if(u.y>0&&u.ary[u.y-1][u.x]==1){
	  u.ary[u.y-1][u.x]=0;
	  minDepth=min(minDepth,dfs(u));
	  break;
	}
      }
    }else if(i==1&&u.x<w-1&&u.ary[u.y][u.x+1]!=1){
      for(;u.x<=w-1;u.x++){
	if(u.ary[u.y][u.x]==3){
	  minDepth=min(minDepth,u.depth);
	  break;
	}else if(u.x<w-1&&u.ary[u.y][u.x+1]==1){
	  u.ary[u.y][u.x+1]=0;
	  minDepth=min(minDepth,dfs(u));
	  break;
	}
      }
    }else if(i==2&&u.y<h-1&&u.ary[u.y+1][u.x]!=1){
      for(;u.y<=h-1;u.y++){
	if(u.ary[u.y][u.x]==3){
	  minDepth=min(minDepth,u.depth);
	  break;
	}else if(u.y<h-1&&u.ary[u.y+1][u.x]==1){
	  u.ary[u.y+1][u.x]=0;
	  minDepth=min(minDepth,dfs(u));
	  break;
	}
      }
    }else if(i==3&&u.x>0&&u.ary[u.y][u.x-1]!=1){
      for(;u.x>=0;u.x--){
	if(u.ary[u.y][u.x]==3){
	  minDepth=min(minDepth,u.depth);
	  break;
	}else if(u.x>0&&u.ary[u.y][u.x-1]==1){
	  u.ary[u.y][u.x-1]=0;
	  minDepth=min(minDepth,dfs(u));
	  break;
	}
      }
    }
  }
  return minDepth;
}

void solve(const tpl& t){
  int result=dfs(t);
  cout<<(result==INF?-1:result)<<endl;
}
int main(){ _;
  while(cin>>w>>h,(w|h)!=0){
    tpl t;
    t.depth=0;
    t.last=-1;
    REP(i,h)REP(j,w){
      cin>>t.ary[i][j];
      if(t.ary[i][j]==2){
	t.ary[i][j]=0;
	t.x=j;
	t.y=i;
      }
    }
    solve(t);
  }
}

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vi in(n+1);
		rep(i,n)cin>>in[i+1];
		vi cro;
		rep(i,n+1)rep(j,n+1)cro.pb(in[i]+in[j]);

		sort(all(cro));

		int out=0;
		rep(i,cro.size()){
			int d=m-cro[i];
			int h=cro.size();
			int l=0;
			while(1){
				if(h-l==1)break;
				int t=(h+l)/2;
				if(cro[t]>d)h=t;
				else l=t;
			}
			if(cro[i]+cro[l]<=m)out=max(out,cro[i]+cro[l]);
		}
		cout<<out<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
const int mod=998244353;
LL jc[2500005];
void add(LL &x,LL y){x=(x+y)%mod;}
LL sqrr(LL x){return x*x%mod;}
LL ksm(LL x,int y){
    if (y==0) return 1;if (y==1) return x;
    LL ans=sqrr(ksm(x,y>>1));
    if (y&1) ans=ans*x%mod;
    return ans;
}
LL ny(LL x){return ksm(x,mod-2);}
LL C(int n,int m){return jc[n]*ny(jc[m])%mod*ny(jc[n-m])%mod;}
LL calf(int n,int s){
    return C(n+s-1,s);
}
LL cal1(int n,int m,int k){
    if (k>n) return 0;
    if ((k^m)&1) return 0;
    return C(n,k)*calf(n,(m*3-k)/2)%mod;
}
LL cal2(int n,int m,int k){
    if (k>n) return 0;
    return C(n,k)*k%mod*calf(k,m-k)%mod;
}
void init(){
    const int n=2500000;
    jc[0]=1;
    for (int i=1;i<=n;i++) jc[i]=jc[i-1]*i%mod;
}
int main(){
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    init();
    int n,m,m3;cin>>n>>m;m3=m*3;
    LL ans=calf(n,m3);
    for (int i=m+1;i<=m3;i++) add(ans,mod-cal1(n,m,i));
    for (int i=1;i<=m;i++) add(ans,mod-cal2(n,m,i));
    cout<<ans<<endl;
    return 0;
}

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

int main(){
  int n,k;
  cin >> n >> k;
  vector<int>a(n);
  vector<double>kit(n);
  for(int i=0;i<n;i++){
    cin >> a.at(i);
    kit.at(i)=(a.at(i)+1.0)/2;
  }
  double ans=0,bf=0;
  for(int i=0;i<k;i++)
    bf+=kit.at(i);
  ans=bf;
  for(int i=k;i<n;i++){
    bf+=kit.at(i)-kit.at(i-k);
    ans=max(ans,bf);
  }
  cout << setprecision(9) << ans << endl;
  return 0;
}


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

int main() {
  int N,X,ans=0;
  cin >> N >> X;
  vector<int>vec(N);
  for(int i=0; i<N; i++){
    cin >> vec[i];
  }
  sort(vec.begin(),vec.end());
  for(int i=0; i<N; i++){
    if(X<vec[i]){
      break;
    }
    else{
    ans++;
    X -= vec[i];
    }
  }
  if(ans == N && X>0){
    ans--;
  }
  cout << ans << endl;
}

//
//  main.cpp
//  ITP1_6-A
//
//  Created by Saicj on 2015/07/14.
//  Copyright (c) 2015??´ saicj. All rights reserved.
//

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

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

    int n;
    int index = 0;
    cin >> n;

    int numList[n];

    while (true) {
        int temp;

        cin >> temp;
        numList[index] = temp;
        index++;

        if (index >= n){
            break;
        }
    }

    for (int i = index-1; i >= 0; i--) {

        if (i == 0){
            printf("%d",numList[0]);
            break;
        }

        printf("%d ",numList[i]);
    }

    printf("\n");

    return 0;
}


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

#define vi vector<int>

int solve(int k, vi& xs){
    sort(xs.begin(), xs.end()); //ordena o vector

    //xs.back() --> Returns a reference to the last element in the vector
    xs.back() *= (1<<k); // shiftar 1 vez pra esquerda eh multiplicar por 2

    return accumulate(xs.begin(), xs.end(), 0);
}

int main(){
    vi xs(3);

    for(int i=0;i<3;i++)
        cin>>xs[i];

    int k;

    cin>>k;

    auto ans = solve(k, xs);

    cout<<ans<<'\n'<<endl;

    return 0;
}

#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
#include <set>
#include <queue>
#include <utility>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    double a , b , c , k;
    cin >> a >> b >> c >> k;
    cout << a + b + c - max( a , max( b , c ) ) + max( a , max( b , c ) ) * pow( 2 , k );
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

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

char piece[10][10][10];

bool DONE = false;

typedef pair<int, int> pii;

vector<pair<pii, pii>> placement[10];

int done[10][10];

vector<pair<pii, pii>> doms;

class CDominoQuality {
public:
  bool okrow(int n, int row) {
    if (row < 0) return true;
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if (!done[row][j]) continue;
      if (j == 0 || (done[row][j] != done[row][j - 1])) cnt++;
    }
    return cnt == 3;
  }

  bool okcol(int n, int col) {
    if (col < 0) return true;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (!done[i][col]) continue;
      if (i == 0 || (done[i][col] != done[i - 1][col])) cnt++;
    }
    return cnt == 3;
  }

  void dfs(int n, int p) {
    if (DONE) return;
    if (p % n == 0) {
      int row = p / n - 1;
      if (!okrow(n, row - 1)) return;
      if (p == n * n) {
        for (int i = 0; i < n; i++) {
          if (!okcol(n, i)) {
            return;
          }
        }
        placement[n] = doms;
        DONE = true;
        return;
      }
    }
    dfs(n, p + 1);
    int r = p / n, c = p % n;
    if (c < n - 1 && !done[r][c + 1] && !done[r][c]) {
      doms.push_back({{r, c}, {r, c + 1}});
      done[r][c + 1] = done[r][c] = p + 1;
      dfs(n, p + 1);
      done[r][c + 1] = done[r][c] = 0;
      doms.pop_back();
    }
    if (r < n - 1 && !done[r + 1][c] && !done[r][c]) {
      doms.push_back({{r, c}, {r + 1, c}});
      done[r + 1][c] = done[r][c] = p + 1;
      dfs(n, p + 1);
      done[r + 1][c] = done[r][c] = 0;
      doms.pop_back();
    }
  }

  void solve(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    if (n == 2) {
      out << -1 << endl;
      return;
    }
    if (n == 3) {
      out << "aa.\n";
      out << "..b\n";
      out << "..b\n";
      return;
    }
    memset(piece, '.', sizeof piece);
    for (int i = 4; i <= 7; i++) {
      DONE = false;
      dfs(i, 0);
      for (int j = 0; j < placement[i].size(); j++) {
        auto p = placement[i][j];
        int r1 = p.first.first, c1 = p.first.second, r2 = p.second.first, c2 = p.second.second;
        piece[i][r1][c1] = piece[i][r2][c2] = j + 'a';
      }
    }
    int rep = n / 4 - 1, left = n % 4 + 4;
    vector<vector<char>> res(n, vector<char>(n, '.'));
    for (int k = 0; k < rep; k++) {
      for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
          res[k * 4 + i][k * 4 + j] = piece[4][i][j];
        }
      }
    }
    for (int i = 0; i < left; i++) {
      for (int j = 0; j < left; j++) {
        res[rep * 4 + i][rep * 4 + j] = piece[left][i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
      out << res[i][j];
      out << "\n";
    }
	}
};

int main() {
	CDominoQuality solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


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

using namespace std;

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

using vi = vector<int>;

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

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

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

#if 1

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

  int N, K;
  cin >> N >> K;
  queue<int> q;
  int rs = INT32_MAX;
  F0R(i, N) {
    int x;
    cin >> x;
    q.push(x);
    if (q.size() == K) {
      int l = q.front(), r = q.back();
      int score;
      if (l < 0) {
        if (r > 0) {
          score = min(abs(l), abs(r)) + r - l;
        }
        else {
          score = abs(l);
        }
      }
      else {
        score = abs(r);
      }
      chmin(rs, score);
      q.pop();
    }
  }
  cout << rs;
}
#endif

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple

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

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

typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
// ordered_set X;
// *X.find_by_order(i) = ith largest element (counting from zero)
// X.order_of_key(k) = number of items in X < k (strict lt)

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

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

    int N;
    ll x;

    cin >> N >> x;

    vll A(N);
    for (int i=0;i<N;i++) cin >> A[i];

    sort(all(A));

    int ans = 0;
    for (int i=0;i<N;i++) {
        if (x >= A[i]) {
            x-= A[i];
            ans++;
        }
    }
    if (x > 0 && ans == N) ans = N-1;
    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=1e9+7;
const int N = 2e5+5;

vector<ll>ans,veca,vecb;
vector<int>adj[N];
vector<double>aka;
vector<pair<int,double>>dp[N];
vector<bool>vis;

void init(int n){
    ans.clear();veca.clear();aka.clear();vis.clear();ans.resize(n);
    vecb.clear(),vecb.resize(n);veca.resize(n);vis.resize(n);
    for (int i = 0; i <n; i++) {
        vis[i]=0;
    }
    for (int i = 0; i<=n; i++) {
        adj[i].clear();
    }
    for (int i = 0; i <=n; i++) {
        dp[i].clear();
    }
    for (int i = 0; i < n; i++) {
        ans[i]=0;
    }
}

int check(int a,int b,int c){
    int res=0;
    if(a+b>c) res=1;
    return res;

}
bool valid(int a,int b,int c){
    if((a<b)&&(b<c)) return 0;
    return 1;
}
void solve(){
    int n,flag=0;
    cin>>n;
    init(n);
    for (int i = 0; i <n; i++) {
        cin>>veca[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                if(!valid(i,j,k)){
                    set<int>st;vector<int>vec;
                    st.insert(veca[i]);st.insert(veca[j]);st.insert(veca[k]);
                    vec.push_back(veca[i]);vec.push_back(veca[j]);vec.push_back(veca[k]);
                    if(st.size()==3){
                        sort(vec.begin(),vec.end());
                        flag+=check(vec[0],vec[1],vec[2]);
                    }
                }
            }
        }
    }
    cout<<flag<<"\n";

}

int main(){

    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }

    return 0;
}

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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

int main() {
  int n;
  cin >> n;
  vector<long> l(n);
  for (long &x : l) {
    cin >> x;
  }

  long ans = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        long sum = 0;
        long mx = 0;
        sum = l.at(i) + l.at(j) + l.at(k);
        mx = max(l.at(i), max(l.at(j), l.at(k)));
        if (sum <= 2 * mx) {
          continue;
        }

        if (l.at(i) != l.at(j) && l.at(j) != l.at(k) && l.at(k) != l.at(i)) {
          ans++;
        }
      }
    }
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main()
{
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<ll> A(N);
  int count = 0;
  ll m = P;
  rep(i,N)
  {
    cin >> A[i];
    if(A[i]<0)
    {
      count ++;
      A[i] = -1 * A[i];
    }
    m = min(m,A[i]);
  }
  ll sum = 0;
  rep(i,N)
  {
    sum += A[i];
  }
  if(count%2==0)
  {
    cout << sum << endl;
  }
  else
  {
    cout << sum - m * 2 << endl;
  }
  return 0;
}

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<vector<ll>> memo(N + 1, vector<ll>(2));
  memo[0][0] = 0;
  memo[0][1] = -INF;
  for (int i = 0; i < N; i++) {
    memo[i + 1][0] = max(memo[i][0] + A[i], memo[i][1] - A[i]);
    memo[i + 1][1] = max(memo[i][0] - A[i], memo[i][1] + A[i]);
  }
  cout << memo[N][0] << endl;
  return 0;
}


#include <iostream>
#include <functional>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <iomanip>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;++i)
#define repi(i,n) for(ll i=n-1;i>=0;--i)
#define sll(n) scanf("%lld", &n);
#define slf(n) scanf("%lf", &n);
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
typedef double lf;
typedef long long ll;
// const int mod = 1000000007;
const int mod = 998244353;

// vector<string> Qs(max_n);

bool check(vector<pll> ps){
    ll h = 0;
    for(auto p:ps){
        ll b = h + p.first;
        if(b < 0) return false;
        h = h + p.second;
    }

    return true;
}

void Main(){
    ll N;
    string s;
    sll(N);
    vector<pll> ls, rs;
    ll total = 0;
    rep(i, N){
        cin >> s;
        ll h = 0, b = 0;
        for(auto c:s){
            if(c=='(') h++; else h--;
            b = min(b, h);
        }
        if(h>=0) ls.emplace_back(b, h);
        else rs.emplace_back(b-h, -h);
        total += h;
    }
    sort(ls.rbegin(), ls.rend());
    sort(rs.rbegin(), rs.rend());

    if(check(ls) && check(rs) && total == 0){
        printf("Yes\n");
    }else{
        printf("No\n");
    }

}

int main(){
    Main();

    return 0;

}

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

const ll INF = 1LL << 60;

ll N, K;
vector<ll> v;

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

    ll ans = 0;
    for (int l = 0; l <= K; l++) {
        for (int r = 0; r <= K - l; r++) {
            if (l + r > N) continue;

            // 戻せる回数
            int d = K - (l + r);

            vector<ll> calc;
            ll tmp = 0;
            // 左からとる

            // cout << "pat1 l " << l << " r: " << r << " " << tmp << endl;
            // [0, l)
            for (int k = 0; k < l; k++) {
                calc.push_back(v[k]);
                tmp += v[k];
            }

            // 右からとる
            // cout << "pat2 l " << l << " r: " << r << " " << tmp << endl;
            // [N - r, N)
            for (int k = 0; k < r; k++) {
                calc.push_back(v[N - 1 - k]);
                tmp += v[N - 1 - k];
            }

            sort(calc.begin(), calc.end());

            // cout << "pat3 l " << l << " r: " << r << " " << tmp << endl;
            // 戻す
            for (int k = 0; k < min(d, (int)calc.size()); k++) {
                if (calc[k] > 0) break;
                tmp -= calc[k];
            }
            // cout << "l " << l << " r: " << r << " " << tmp << endl;
            ans = max(tmp, ans);
        }
    }

    cout << ans << endl;
}


// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n);      // 各桁の和を計算する。
int getDigit(int n);            // 数字の桁数を取得する。
string upper(string str);       // 英字を大文字に変換する。
string lower(string str);       // 英字を小文字に変換する。
vector<pair<long long,long long>> prime_factorize(long long p); // 素因数分解

// class
// ------------------------------------------------
class Combi {
  public:
    Combi();
    long long Combination(long long n, long long k);
    long long nPk_modp(long long n, long long k, long long p);
  private:
    map<long long, map<long long, long long>> memo;
    long long n_num;
    long long k_num;
};

// define
// ------------------------------------------------
#define all(a)        (a).begin(),(a).end()
#define rall(a)       (a).rbegin(),(a).rend()
#define sz(a)         int((a).size())
#define rep(i,n)      for(long long(i)=0;(i)<(n);(i)++)
#define repe(i,n)     for(long long(i)=0;(i)<=(n);(i)++)
#define vsort(v)      sort((v).begin(),(v).end())
#define rvsort(v)     sort(rall((v)))
#define vi            vector<int>
#define GCD(a,b)      __gcd((a),(b))
#define LCM(a,b)      (a)/GCD((a),(b))*(b)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

const int INF = 1e9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<long> vll;
// code
// ------------------------------------------------
int main() {

    ll n;cin >> n;
    vll a(n);
    rep(i,n) cin >> a[i];

    ll sum = 0;
    rep(i,n) sum += a[i];

    double ave = (double)sum / n;

    ll ans = 0;
    double dif = abs(ave - a[0]);
    rep(i,n){
        if(abs(ave - a[i]) < dif){
            dif = abs(ave - a[i]);
            ans = i;
        }
    }

    cout << ans << endl;

    return 0;
}
// funcの実体
// ------------------------------------------------
int getDigit(int n) {
  int i = 1;
  while(1) {
    n = n / 10;
    if(n == 1)
      break;
    i++;
  }
  return i;
}
int CalcSumOfDigit(int n) {
  int s = 0;
  while(n) {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str) {
  for(auto itr = str.begin();itr != str.end() ; itr++) {
    if(97 <= *itr && *itr <= 122) {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str) {
  for(auto itr = str.begin();itr != str.end() ; itr++) {
    if(65 <= *itr && *itr <= 90) {
      *itr = *itr + 32;
    }
  }
  return str;
}

Combi::Combi() {
  n_num = -1;
  k_num = -1;
};

ll Combi::Combination(ll n, ll k) {

  ll ret;
  if(memo[n][k] != 0) {
    ret = memo[n][k];
  } else if(n == k || k == 0) {
    memo[n][k] = 1;
    ret = 1;
  } else {
    ret = Combination(n - 1, k - 1) + Combination(n - 1, k);
    memo[n][k] = ret;
  }
  return ret;
}

long long Combi::nPk_modp(long long n, long long k, long long p) {
  ll ans = 1;
  for(long long i = k; i <= n; i++) {
    ans = (ans * i) % p;
  }

  return ans;
};

vector<pair<long long,long long>> prime_factorize(long long p) {
    vector<pair<long long,long long>> ret;

    for(long long x = 2; x * x <= p; ++x) {
        if(p % x != 0) continue;
        long long num = 0;
        while(p % x == 0) {
            num++;
            p /= x;
        }
        ret.push_back(make_pair(x,num));
    }

    if(p != 1) ret.push_back(make_pair(p, 1));

    return ret;
}

/*
"Won't stop until I'm phenomenal."
- Phenomenal, Eminem
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define ff first
#define Shazam ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL);
#define ss second
#define all(c) c.begin(),c.end()
#define endl "\n"
#define test() int t; cin>>t; while(t--)
#define fl(i,a,b) for(int i = a ; i <b ;i++)
#define get(a) fl(i,0,a.size())  cin>>a[i];
#define pra(a) fl(i,0,a.size()) cout<<a[i]<<" ";	cout<<endl;
#define pr(a,n) fl(i,0,n) cout<<a[i]<<" ";	cout<<endl;
const ll INF = 2e18;
const int inf = 2e9;
const int mod1 = 1e9 + 7;

int main(){
    Shazam;
    int n; cin>>n;
    vector<int> a(n+1);
    for(int i = 1; i <= n; i++) cin>>a[i];
    int res = 0;
    vector<int> ans(n+1);
    for(int i = n; i>0; i--){
        int curr = 0;
        for(int j = 2*i; j <=n; j+=i){
            curr+=ans[j];
        }
        if ((curr & 1) != (a[i] & 1)){
            ans[i] = 1;
            res++;
        }
    }
    cout<<res<<endl;
    for(int i = 1; i <= n; i++){
        if(ans[i])
            cout<<i<<" ";
    }
    cout<<endl;/*  */

    return 0;
}


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

int main(){
  int n;
  int a[5000];
  while(cin >> n && n>0){
    for(int i=0;i<n;i++)
      cin >> a[i];
    long long sum=0,tmp=0,ma=-100000;
    int m=-100000;
    bool flg=true;
    for(int i=0;i<n;i++){
      if(flg) m=max(m,a[i]);
      if(a[i]>=0){
	flg=false;
	sum+=a[i];
	continue;
      }
      ma=ma>sum?ma:sum;
      for(tmp=sum;i<n && a[i]<0;i++){
	tmp+=a[i];
      }i--;
      if(tmp>0)
	sum=tmp;
      else
	sum=0;
    }
    ma=ma>sum?ma:sum;

    cout << (flg?m:ma) << endl;
  }
}


#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 998244353;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
//typedef boost::multiprecision::cpp_int bigint;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	LL N;
	cin >> N;
	string S;
	cin >> S;
	VLL M(2 * N + 1, 0), C(2 * N + 1,0);
	VLL Ds;
	for (LL n = 0; n < N; n++) {
		if (S[n] == 'D')Ds.push_back(n + 1);
		if (S[n] == 'M')M[n + 1]++;
		if (S[n] == 'C')C[n + 1]++;
		M[n + 1] += M[n];
		C[n + 1] += C[n];
	}
	for (LL n = N + 1; n <= 2 * N; n++) {
		M[n] = M[n - 1];
		C[n] = C[n - 1];
	}
	VLL DP(2 * N + 1, 0);
	for (LL n = N; n >= 1; n--) {
		DP[n] = DP[n + 1];
		if (S[n - 1] == 'M')DP[n] += C.back() - C[n];
	}
	LL Q;
	cin >> Q;
	for (LL q = 0; q < Q; q++) {
		LL K;
		cin >> K;
		LL ans = 0;
		for (LL d : Ds) {
			LL temp = DP[d + 1] - DP[d + K] - (M[d + K - 1] - M[d]) * (C.back() - C[d + K-1]);
			ans += temp;
		}
		cout << ans << "\n";
	}
	return 0;
}

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

int main(void) {
  long long N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<int> k(Q);
  for (int i = 0; i < Q; i++) {
    cin >> k[i];
  }
  for (int j = 0; j < Q; j++) {
    long long d = 0, m = 0, dm = 0, ans = 0;
    for (int i = 0; i < N; i++) {
      if (i >= k[j]) {
        if (S[i - k[j]] == 'D') {
          d--;
          dm -= m;
        }
        if (S[i - k[j]] == 'M') m--;
      }

      if (S[i] == 'D') d++;
      if (S[i] == 'M') {
        m++;
        dm += d;
      }
      if (S[i] == 'C') {
        ans += dm;
      }
    }
    cout << ans << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
//#define endl "\n"
#define ciosup \
  cin.tie(0);  \
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  if (v.size() > 0) {
    os << v[v.size() - 1] << endl;
  }
  return os;
}

int main() {
  int n,t;
  cin >> n >> t;
  vint a(n), b(n);
  REP(i, n) { cin >> a[i] >> b[i]; }
  vector<vll> dp(n + 1, vll(6000 + 1, 0)), dp2(n + 1, vll(6000 + 1, 0));
  //REP(i, n + 1) { REP(j,6000) {dp[i][j] = 0;} }
  REP(i,n){
    REP(j,6000){
      if (dp[i][j] != -1) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if (j + a[i]<= 6000) dp[i + 1][j + a[i]] = max(dp[i + 1][j + a[i]], dp[i][j] + b[i]);
      }
      if (j > 0) dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - 1]);
    }
  }
  //REP(i, n + 1) { dp2[i][0] = 0; }
  IREP(i, n) {
    REP(j, 6000) {
      if (dp2[i][j] != -1) {
        dp2[i][j] = max(dp2[i][j], dp2[i + 1][j]);
        if (j + a[i] <= 6000)
          dp2[i][j + a[i]] = max(dp2[i][j + a[i]], dp2[i + 1][j] + b[i]);
      }
      if (j > 0) dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  ll ans = 0;
  REP(i,n){
    REP(j, t) { ans = max(ans, b[i] + dp[i][j] + dp2[i+1][t - 1 - j]); }
  }
  cout << ans << endl;
}

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

#define ll long long
#define ld long double
#define ar array

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
	cin >> x;
}
void read(double& d) {
	string t;
	read(t);
	d=stod(t);
}
void read(long double& d) {
	string t;
	read(t);
	d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
	read(h);
	read(t...);
}
template<class A> void read(vt<A>& x) {
	EACH(a, x)
		read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
	EACH(a, x)
		read(a);
}

string to_string(char c) {
	return string(1, c);
}
string to_string(bool b) {
	return b?"true":"false";
}
string to_string(const char* s) {
	return string(s);
}
string to_string(string s) {
	return s;
}
string to_string(vt<bool> v) {
	string res;
	FOR(sz(v))
		res+=char('0'+v[i]);
	return res;
}

template<size_t S> string to_string(bitset<S> b) {
	string res;
	FOR(S)
		res+=char('0'+b[i]);
	return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
		if(!f)
			res+=' ';
		f=0;
		res+=to_string(x);
	}
    return res;
}

template<class A> void write(A x) {
	cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
	write(h);
	write(t...);
}
void print() {
	write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
	write(h);
	if(sizeof...(t))
		write(' ');
	print(t...);
}

void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

template<class T> void offset(ll o, T& x) {
	x+=o;
}
template<class T> void offset(ll o, vt<T>& x) {
	EACH(a, x)
		offset(o, a);
}
template<class T, size_t S> void offset(ll o, ar<T, S>& x) {
	EACH(a, x)
		offset(o, a);
}

mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
	return uniform_int_distribution<ll>(a, b)(mt_rng);
}
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
ll mod=1e9+7;
const int N=1e6+5;
const ld PI=2*acos(0.0);

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

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

bool compare(array<int,2> a,array<int,2> b){
return a[0]<b[0];
}
void solve() {
int n,t; read(n,t);
vt<array<int,2> > td(n+1);
FOR(i,1,n+1) read(td[i][0],td[i][1]);
sort(++td.begin(),td.end(),compare);
int siz=(*--td.end())[0]+t+5;
vt<int> dp(siz,0);
vt<int> pre(siz,0);
FOR(i,1,n+1){
FOR(j,1,siz){
if(j-td[i][0]<0||j-td[i][0]>=t) { dp[j]=pre[j];}
else{ dp[j]=max(pre[j],pre[j-td[i][0]]+td[i][1]);}
}
FOR(j,1,siz){ pre[j]=dp[j]; dp[j]=0; }
//dbg(i,pre);
}
print(*max_element(all(pre)));
}

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


#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>;

constexpr ll INF = 1ll<<60;

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 A[2], B[2];

int main()
{
  rep( i, 2 )
    std::cin >> A[i];
  rep( i, 2 )
    std::cin >> B[i];

  std::cout << std::min(A[0], A[1])+std::min(B[0], B[1]) << std::endl;

  return 0;
}

#include <cstdio>
using ll = long long;
inline ll gcd(ll x, ll y)
{
	while (y)
	{
		auto r = x % y;
		x = y;
		y = r;
	}
	return x;
}
inline bool chk(int n)
{
	for (int i = 2; i * i <= n; i++)
	{
		if (!(n % i))
			return false;
	}
	return true;
}
inline ll lcm(ll x, ll y) { return x && y ? x / gcd(x, y) * y : x + y; }
ll pri[1005], arr[505][505];
int main()
{
	// freopen("AGC027-D.in", "r", stdin);
	int n, cur = 0, len = 0;
	scanf("%d", &n);
	bool even = n & 1 ^ 1;
	n += even;
	for (int i = 2; len <= n * 2; i++)
	{
		if (chk(i))
			pri[len++] = i;
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			arr[i][j] = 1;
	}
	for (int i = 0; i < n; i += 2, cur++)
	{
		int x = i, y = 0;
		while (x < n && y < n)
			arr[x++][y++] *= pri[cur];
	}
	for (int i = 2; i < n; i += 2, cur++)
	{
		int x = 0, y = i;
		while (x < n && y < n)
			arr[x++][y++] *= pri[cur];
	}
	cur = len - 1;
	for (int i = 0; i < n; i += 2, cur--)
	{
		int x = i, y = 0;
		while (x >= 0 && y < n)
			arr[x--][y++] *= pri[cur];
	}
	for (int i = 2; i < n; i += 2, cur--)
	{
		int x = n - 1, y = i;
		while (x >= 0 && y < n)
			arr[x--][y++] *= pri[cur];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = i & 1 ^ 1; j < n; j += 2)
			arr[i][j] = lcm(lcm(i ? arr[i - 1][j] : 0, j ? arr[i][j - 1] : 0),
							lcm(arr[i + 1][j], arr[i][j + 1])) + 1;
	}
	n -= even;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			printf("%lld%c", arr[i][j], " \n"[j + 1 == n]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  long long N,count=0;
  cin>>N;
  vector<long long> A(N);
  unordered_map<long long,long long> mp;
  for(int i=0;i<N;i++){
    cin>>A.at(i);
    mp[A.at(i)]++;
  }
  sort(A.begin(),A.end());
  reverse(A.begin(),A.end());
  for(int i=0;i<N;i++){
    if(mp[A.at(i)]>=4){
      if(count==0){
        cout<<A.at(i)*A.at(i)<<endl;
        return 0;
      }
      if(count>0){
        cout<<count*A.at(i)<<endl;
        return 0;
      }
    }
    if(mp[A.at(i)]>=2){
      if(count==0){
        count=A.at(i);
        mp[A.at(i)]=0;
      }
      else{
        cout<<count*A.at(i)<<endl;
        return 0;
      }
    }
  }
  cout<<0<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	ll l, r;
	cin >> l >> r;
	if (r - l + 1 >= 2019)
		cout << 0 << endl;
	else {
		ll ans = INF;
		repl(i, l, r + 1) repl(j, i + 1, r + 1) ans = min(ans, i * j % 2019);
		cout << ans << endl;
	}
	return 0;
}

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

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

    for (int i = A; i <= B; i++)
    {
        int c = i % 10, d = i / 10000 % 10;
        int e = i / 10 % 10, f = i / 1000 % 10;
        if (c == d && e == f)ans++;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> P;
typedef priority_queue<P,vector<P>,greater<P>> P_queue;

#define REP(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a,b) a=(a+b)%mod

const ll MOD=998244353;
const ll mod=1000000007;
const ll INF=1e15;
vec dx={1,0,-1,0};
vec dy={0,1,0,-1};

ll N,M;
ll d[301][301];
bool used[301];
ll Start[301];

ll Solve(ll x){
    if(!used[d[x][Start[x]]]) return d[x][Start[x]];
    Start[x]++;
    return Solve(x);
}

ll SOLVE(){
    vec kari(M+1,0);
    rep(i,N) kari[Solve(i)]++;
    ll MAX=-INF, basho=-1;
    rep(i,M) if(kari[i]>MAX){
        MAX=kari[i];
        basho=i;
    }
    //rep(i,M) cout<<kari[i]<<' ';
    //cout<<endl;
    //cout<<MAX<<' '<<basho+1<<endl;
    used[basho]=true;
    return MAX;
}
ll MAIN(){
    if(M==1) return N;
    ll ret=INF;
    rep(i,M-1) {
        //cout<<i<<':';
        ll K=SOLVE();
        ret=min(ret,K);
    }
    return ret;
}
int main(){
    cin>>N>>M;
    rep(i,N) rep(j,M) {cin>>d[i][j]; d[i][j]--;}
    cout<<MAIN()<<endl;

}

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector <int> vi;
typedef vector <string> vs;
#define N1 5
#define N2 13
int main(void){
	vi card(N2);
	string prize[7]={"null","one pair","two pair","three card","straight","full house","four card"};
	while (true){
		for (int i=0; i<N2; i++)
			card[i]=0;
		int a=0, b=0, d=N2, p=0,tmp;
		for (int i=0;i<N1;i++){
			char c;
			if (i!=0) cin>>c;
			cin>>tmp;
			if (cin.eof()) break;
			tmp--;
			if (tmp<d) d=tmp;
			if (card[tmp]==0) b++;
			card[tmp]++;
			if (card[tmp]>a) a=card[tmp];
		}
		if (b==0) break;
		switch (b){
		case 2:
			if (a==4) p=6;
			else p=5;
			break;
		case 3:
			if (a==3) p=3;
			else p=2;
			break;
		case 4:
			p=1;
			break;
		case 5:
			bool f=true;
			for (int i=d; i<d+N1; i++)
				if (card[i]!=1){
					f=false;
					break;
				}
			if (!f && card[0]==1){
				f=true;
				for (int i=9; i<N2; i++)
					if (card[i]!=1){
						f=false;
						break;
					}
			}
			if (f) p=4;
			else p=0;
		}
		cout<<prize[p]<<endl;
	}
}


#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;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main() {
    int n; cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    ll ans = 0;
    rep(i,60) {
        ll x = 0;
        rep(j,n) if (a[j]>>i & 1) x++;
        ll now = x*(n-x)%mod; //今考えてる桁から生じる和
        rep(k,i) now = now*2%mod;
        ans += now;
        ans %= mod;
    }
   cout << ans << endl;
}


#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<cstring>
#include<bitset>
#include<stack>
#include<time.h>

#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define ALL(x) x.begin(),x.end()
#define sz(a) ((int)a.size())
#define getmid ((l+r)>>1)
#define mst(var,val) memset(var,val,sizeof(var))
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define lowbit(x) x&(-x)
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
#ifdef local
#define dbg(args...) cout << #args << " -> ", err(args);
void err(){ cout << endl; }
template<typename T, typename... Args>
void err(T a, Args... args){ cout << a << ' '; err(args...); }
#else
#define dbg(args...)
#endif // local

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <double, double> pdd;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const long double eps=1e-8;
const int mod=1e9+7;
const int maxn=3e5+100;
const int N=1e6+1000;
const int M=500+10;
const ll mm=(1LL<<32);

template <class T>
inline void read(T &x)
{
    x = 0;
    char c = getchar();
    bool f = 0;
    for (; !isdigit(c); c = getchar())
        f ^= c == '-';
    for (; isdigit(c); c = getchar())
        x = x * 10 + (c ^ 48);
    x = f ? -x : x;
}

template <class T>
inline void write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    T y = 1;
    int len = 1;
    for (; y <= x / 10; y *= 10)
        ++len;
    for (; len; --len, x %= y, y /= 10)
        putchar(x / y + 48);
}
ll qpow(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}

int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
#endif // local
    IOS;
    ll n;cin>>n;
    ll len=1,tmp=26;
    while(n>tmp)
    {
        n-=tmp;
        tmp*=26;
        len++;
    }
    n-=1;
    string ans="";
    for(int i=1;i<=len;++i)
    {
        ans+='a'+n%26;
        n/=26;
    }
    reverse(ALL(ans));
    cout<<ans<<endl;
    return 0;
}

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    bool ans=true;
  	if(s.length()%2==1)
    {
      cout<<"No";
      return 0;
    }
    for(int i=0;i<s.length();i++)
    {
        if(i%2==0)
        {
            if(s[i]!='h')
            {
                ans=false;
                break;
            }
        }
        else
        {
            if(s[i]!='i')
            {
                ans=false;
                break;
            }
        }
    }
    if(ans)
        cout<<"Yes";
    else cout<<"No";
}