#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 6010;
int MOD;

int dp[N][2*N];

int add(int a, int b){
    return (a + b) % MOD;
}

int mul(ll a, int b){
    return (a * b) % MOD;
}

void sadd(int &a, int b){
    a = add(a, b);
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int n;cin >> n >> MOD;
    n *= 3;
    dp[0][n] = 1;

    for(int i=1 ; i<=n ; i++)
        for(int j=0 ; j<=2*n ; j++){
            if(j)
                sadd(dp[i][j], dp[i-1][j-1]);
            if(i>1)
                sadd(dp[i][j], mul(i-1, dp[i-2][j+1]));
            if(i>2)
                sadd(dp[i][j], mul((i-1)*(i-2), dp[i-3][j]));
        }

    int ans=0;

    for(int i=n ; i<=2*n ; i++)
        sadd(ans, dp[n][i]);
    cout << ans << "\n";
}

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

#define FOR(i,s,e) for(int i=(s);(i)<(int)(e);++(i))
#define REP(i,e) FOR(i,0,e)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define ppb(x) pop_back(x)
#define mp make_pair
#define mt make_tuple
#define t0(t) get<0>((t))
#define t1(t) get<1>((t))
#define t2(t) get<2>((t))

typedef long long ll;

int main() {
  int h, w;
  fscanf(stdin, "%d%d ", &h, &w);
  multiset<int> hd; // horizontal distance between from start to current position
  map<int, int> hdmap; // (current column, start column) to obtain the answer in log(n)
  REP(i,w) {
    hdmap[i] = i;
    hd.insert(0);
  }
  REP(i,h) {
    int l, r;
    fscanf(stdin, "%d%d ", &l, &r);
    --l; // semiopen interval [l-1, r) based on 0-index
    auto it = hdmap.lower_bound(l);
    int re = -1;
    while (it != hdmap.end() && it->first <= r) {
      re = max(re, it->second);
      hd.erase(hd.find(it->first - it->second));
      hdmap.erase(it++);
    }
    // the only optimal start point in [l-1, r) remains
    if (re != -1 && r < w) {
      hd.insert(r-re);
      hdmap[r] = re;
    }
    int res = -1;
    if (hd.size()) res = *hd.begin() + (i+1);
    fprintf(stdout, "%d\n", res);
  }
  return 0;
}


#include <cstdio>
#include <cassert>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <iostream>
using namespace std;

void erase1(multiset<int> &s, int x) {
    auto it = s.find(x);
    if (it != s.end()) s.erase(it);
}

const int INF = 1<<30;

vector<int> solve(int H, int W, const int *L, const int *R) {
    map<int, int> m;
    multiset<int> distances;
    vector<int> ans(H);
    for (int i = 0; i < W; i++) {
        m[i] = 0;
        distances.insert(0);
    }
    for (int h = 0; h < H; h++) {
        int l = L[h], r = R[h];
        auto it = m.lower_bound(l);
        int d = INF;
        while (it != m.end() && it->first <= r) {
            d = min(d, it->second + r + 1 - it->first);
            erase1(distances, it->second);
            it = m.erase(it);
        }
        if (d < INF && r < W - 1) {
            if (it == m.end() || it->first > r + 1) {
                m[r+1] = d;
                distances.insert(d);
            } else if (d < it->second) {
                erase1(distances, it->second);
                distances.insert(d);
                m[r+1] = d;
            }
        }
        ans[h] = distances.empty() ? -1 : *distances.begin() + h + 1;
    }
    return ans;
}

vector<int> check(int H, int W, const int *L, const int *R) {
    int dp[H+1][W];
    fill(dp[0], dp[0]+W, 0);
    for (int i = 1; i <= H; i++) {
        dp[i][0] = L[i-1] == 0 ? INF : dp[i-1][0];
        for (int j = 1; j < W; j++) {
            if (j < L[i-1]) dp[i][j] = min(dp[i-1][j], dp[i][j-1] + 1);
            else if (j <= R[i-1]) dp[i][j] = dp[i][j-1] + 1;
            else dp[i][j] = min(dp[i-1][j], dp[i][j-1] + 1);
        }
    }

    vector<int> ans(H);
    for (int h = 0; h < H; h++) {
        ans[h] = h + 1 + *min_element(dp[h+1], dp[h+1] + W);
        if (ans[h] >= INF) ans[h] = -1;
    }
    return ans;
}

int main() {
    int H, W; scanf("%d %d", &H, &W);
    int L[H], R[H];
    for (int i = 0; i < H; i++) {
        scanf(" %d %d ", &L[i], &R[i]);
        L[i]--; R[i]--;
    }
    vector<int> ans = solve(H, W, L, R);
    for (int x : ans) printf("%d\n", x);

    // vector<int> expect = check(H, W, L, R);
    // for (int x : expect) cerr << x << endl;
    // for (int i = 0; i < H; i++) assert(ans[i] == expect[i]);
}

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

int main() {
  int H, W;
  cin >> H >> W;
  map<int, int> MA;
  multiset<int> MS;
  for (int i = 0; i < W; i++) MA[i] = i, MS.insert(0);
  for (int h = 1, A, B; cin >> A >> B; h++) {
    auto it = MA.lower_bound(--A);
    int tmp = -1;
    while (it != MA.end() && it -> first <= B) {
      tmp = max(tmp, it -> second);
      MS.erase(MS.find(it -> first - it -> second));
      MA.erase(it++);
    }
    if (tmp != -1 && B < W) MA[B] = tmp, MS.insert(B - tmp);
    cout << ((MS.size()) ? *MS.begin() + h : -1) << "\n";
  }
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=2005,Mod=1e9+7;
int dp[N][N],fac[N*N],inv[N*N];
inline int C(int n,int m){
	return 1ll*fac[n]*inv[m]%Mod*inv[n-m]%Mod;
}
inline int qpow(int a,int b){
	int ans=1;
	while (b){
		if (b&1) ans=1ll*ans*a%Mod;
		a=1ll*a*a%Mod,b>>=1;
	}
	return ans;
}
int main (){
	int n,k;scanf ("%d%d",&n,&k);
	if (k==1) {puts("1");return 0;}
	dp[0][0]=1;fac[0]=1;
	for (int i=1;i<=n*k;i++) fac[i]=1ll*fac[i-1]*i%Mod;
	inv[n*k]=qpow(fac[n*k],Mod-2);for (int i=n*k-1;i>=0;i--) inv[i]=1ll*inv[i+1]*(i+1)%Mod;
    for (int i=0;i<=n;i++) dp[i][0]=1;
	for (int i=1;i<=n;i++)
        for (int j=0;j<=i;j++)
            dp[i][j]=dp[i-1][j]+1ll*dp[i][j-1]*(n-j+1)%Mod*C(n*k-(i+(j-1)*(k-1))-1,k-2)%Mod,dp[i][j]%=Mod;
    printf ("%d",dp[n][n]);
	return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <climits>
#include <algorithm>
using namespace std;
string a;
int d,n,mod=1e9+7;

int digitSum(int idx,int sum,bool tight,vector<vector<int> > &dp){
	if(idx==n)
		return sum==0?1:0;
	if(!tight&&dp[n-idx-1][sum]!=-1)
		return dp[n-idx-1][sum];
	int ans =0;
	int k = tight?a[idx]-48:10;
	for(int i=0;i<k;i++)
		ans = (digitSum(idx+1,(sum+i)%d,0,dp)+ans)%mod;
	if(tight){
		ans = (digitSum(idx+1,(sum+k)%d,1,dp)+ans)%mod;
	} else {
		dp[n-idx-1][sum] = ans;
	}
	return ans;
}

int main()
{
	cin >> a >> d;
	n = a.size();
	vector<vector<int> > dp(n,vector<int>(d,-1));
	cout << (digitSum(0,0,1,dp)-1+mod)%mod;
    return 0;
}


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

int main() {
  vector<int> num(3);
  rep(i, 3) cin >> num.at(i);
  for(int i = 1; i < 3; i++) {
    int tmp = 0;
    tmp = num.at(0);
    num.at(0) = num.at(i);
    num.at(i) = tmp;
  }
  rep(i, 3) cout << num.at(i) << " ";
  cout << endl;
  return 0;
}

#include <iostream>
#include <vector>

using namespace std;

void abc_160a();

void abc_161a();

int main()
{
  //abc_160a();
  abc_161a();

}

void abc_160a()
{
  string S;
  cin >> S;

  if (S[2] == S[3] && S[4] == S[5]) cout << "Yes\n";
  else cout << "No\n";
}

void swap(int& a, int& b)
{
  int tmp;
  tmp = a;
  a = b;
  b = tmp;
}

void abc_161a()
{
  int x, y, z;
  cin >> x >> y >> z;

  swap(x, y);
  swap(x, z);

  cout << x << " " << y << " " << z << "\n";
}


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

int insertionSort(vector<int> &A, int N, int g, int &cnt){
  for(int i = g; i < N; i++){
    int v = A.at(i);
    int j = i - g;
    while(j >= 0 && A.at(j) > v){
      A.at(j+g) = A.at(j);
      j = j - g;
      cnt++;
    }
    A.at(j+g) = v;
  }
}

void shellSort(vector<int> &A, int N){

  int cnt = 0;
  int m;
  vector<int> G(100);

  for(int i = 0; i < 100; i++){
    if(i == 0){
      G.at(i) = N / 2 + 1;
    }
    else{
      G.at(i) = G.at(i-1) / 2 - 1;
    }
    if(G.at(i) < 0){
      G.at(i-1) = 1;
      G.at(i) = 0;
      m = i;
      break;
    }
  }
  for(int i = 0; i < m; i++){
    insertionSort(A, N, G.at(i), cnt);
  }

  cout << m << endl;
  for(int i = 0; i < m; i++){
    if(i) cout << " ";
    cout << G.at(i);
  }
  cout << endl << cnt << endl;
  for(int i = 0; i < N; i++){
    cout << A.at(i) << endl;
  }
}

int main(){
  int n;
  cin >> n;
  vector<int> R(n);

  for(int i = 0; i < n; i++){
    cin >> R.at(i);
  }

  shellSort(R, n);

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

void insertion_sort(vector<int>& A, int n, int g, int& cnt)
{
  for (int i = g; i < n; ++i) {
    int key = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > key) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = key;
  }
}

void shell_sort(vector<int>& A, int n, int& cnt)
{
  vector<int> G;
  int h = 1;
  G.push_back(h);
  int upper = n / 2;
  if (n >= 1000) { upper = n / 9; }
  else if (n >= 100) { upper = n / 7; }
  for (int i = 3 * h + 1; i < upper; i = 3 * i + 1) {
    G.push_back(i);
  }
  reverse(G.begin(), G.end());
  int m = G.size();
  fprintf(stdout, "%d\n", m);
  for (int i = 0; i < m; ++i) {
    if (i) { fprintf(stdout, " "); }
    fprintf(stdout, "%d", G[i]);
  }
  fprintf(stdout, "\n");
  for (int i = 0; i < m; ++i) {
    insertion_sort(A, n, G[i], cnt);
  }
}

int main()
{
  int n = 0;
  fscanf(stdin, "%d", &n);
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%d", &A[i]);
  }
  int cnt = 0;
  shell_sort(A, n, cnt);
  fprintf(stdout, "%d\n", cnt);
  for (int i = 0; i < n; ++i) {
    fprintf(stdout, "%d\n", A[i]);
  }
  return 0;
}


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

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

int main() {
  ll s;
  cin >> s;
  int cnt = 0;
  ll S = s;
  while(S > 0){
    S /= 10;
    cnt++;
  }
  const int f = 753;
  int ans = 1000;
  for(int i = 3; i <= cnt; i++){
    int x = s/pow(10,cnt -i);
    ans = min(abs(x-f), ans);
    ll p = pow(10,cnt+2-i);
    s %= p;
  }
  cout << ans << endl;
}

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

int main(){
    string s;
    cin >> s;
    vector<int> cnts;
    bool isS = true;
    int cnt = 0;
    rep(i,s.size()){
        if (isS && s[i] == 'T'){
            cnts.push_back(cnt);
            isS = !isS;
            cnt = 0;
        }else if (!isS && s[i] == 'S'){
            cnts.push_back(cnt);
            isS = !isS;
            cnt = 0;
        }
        cnt++;
    }
    cnts.push_back(cnt);
    if (s[s.size()-1] != 'S') cnts.push_back(0);

    int tot = cnts[1] - cnts[0];
    int ans = 0, sz = cnts.size();
    for(int i = 3; i < sz; i+=2){
        tot += cnts[i] - cnts[i-1];
        ans = max(ans, tot);
    }
    tot = cnts[sz-1];
    ans = max(ans, tot);
    for(int i = sz-3; i >= 0; i -= 2){
        tot += cnts[i] - cnts[i+1];
        ans = max(ans, tot);
    }
    cout << ans * 2 << endl;
    return 0;
}


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

int main(){
  long long n,q;
  cin >> n >> q;
  long long s[n],t[n],x[n],d[q];
  pair<long long, int> p[n];
  for(int i=0;i<n;i++){
    cin >> s[i] >> t[i] >> x[i];
    p[i]=make_pair(x[i], i);
  }
  sort(p, p+n);
  vector<pair<long long, int> > vec;
  bool active[n];
  for(int i=0;i<n;i++){
    vec.push_back(make_pair(s[p[i].second]-x[p[i].second]-1, i));
    vec.push_back(make_pair(t[p[i].second]-x[p[i].second]-1, i));
    active[i]=false;
  }
  for(int i=0;i<q;i++){
    cin >> d[i];
    vec.push_back(make_pair(d[i], -1));
  }
  sort(vec.begin(), vec.end());
  priority_queue<int, vector<int>, greater<int> > que;
  for(size_t i=0;i<vec.size();i++){
    if(vec[i].second == -1){
      if(!que.empty()){
        cout << p[que.top()].first << endl;
      }else{
        cout << -1 << endl;
      }
    }else{
      long long idx = vec[i].second;
      if(active[idx]){ //工事終了
        active[idx] = false;
        if(idx == que.top()){
          que.pop();
          while(!que.empty()){
            if(active[que.top()]){
              break;
            }else{
              que.pop();
            }
          }
        }
      }else{ //工事開始
        active[idx] = true;
        que.push(idx);
      }
    }
  }
  return 0;
}

#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<bitset>
#include<iomanip>
#include<list>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define RE return 0
//ios::sync_with_stdio(false);
//std::cin.tie(0);
//<< setprecision(20)
const int mod=1e9+7;
const int big=1e9+100;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
template <class T> void soun(T& ar)
{sort(ar.begin(),ar.end());ar.erase(unique(ar.begin(),ar.end()),ar.end());}
int main(void){
	int bi,i,j,n,m,all=0;cin>>n>>m;
	int hen[15][15];
	for(i=0;i<15;i++){
		for(j=0;j<15;j++){
			hen[i][j]=-big;
		}
	}
	for(i=0;i<m;i++){
		int a,b,c;cin>>a>>b>>c;a--;b--;
		all+=c;
		hen[a][b]=c;
		hen[b][a]=c;
	}
	int waka[32768]={0};
	//bitが立っているところでの残す道のア
	for(bi=0;bi<(1<<n);bi++){
		for(i=0;i<n;i++){
			if((bi&(1<<i))==0){continue;}
			for(j=0;j<i;j++){
				if((bi&(1<<j))==0){continue;}
				waka[bi]+=max(0,hen[i][j]);
			}
		}
	}
	static int dp[32768][15]={0};
	for(bi=0;bi<(1<<n);bi++){for(j=0;j<15;j++){dp[bi][j]=-big;}}
	for(bi=0;bi<(1<<(n-1));bi++){dp[bi][0]=waka[bi];}
	for(bi=0;bi<(1<<n);bi++){
		for(int bjj=0;bjj<(1<<n);){
			if((bi&bjj)!=0){
				bjj+=(bi&bjj);

				continue;
			}
			for(i=0;i<n;i++){
				if((bi&(1<<i))==0){continue;}
				for(j=0;j<n;j++){
					if((bjj&(1<<j))==0){continue;}
					maxeq(dp[bjj+bi][j],dp[bi][i]+waka[bjj]+hen[i][j]);
				}
			}
			bjj++;
		}
	}
	cout<<all-dp[(1<<n)-1][n-1]<<endl;
	RE;
}


#include <algorithm>
#include <cmath>
#include <cstdio>

#define int long long
#define dotimes(i, n) for (int i : range(n))

using namespace std;

class range {
  class iterator {
    int i, s;
  public:
    iterator(int i, int s): i(i), s(s) {}
    int& operator*() {
      return i;
    }
    void operator++() {
      i += s;
    }
    bool operator!=(iterator const& that) {
      return i < that.i;
    }
  };
  iterator b, e;
public:
  range(int e): b({0, 1}), e({e, 1}) {}
  range(int b, int e): b({b, 1}), e({e, 1}) {}
  range(int b, int e, int s): b({b, s}), e({e, s}) {}
  iterator begin() {
    return b;
  }
  iterator end() {
    return e;
  }
};

int rint() {
  int n;
  scanf("%lld", &n);
  return n;
}

void wint(int n) {
  printf("%lld\n", n);
}

template<typename T>
inline int size(T container) {
  return static_cast<int>(container.size());
}

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

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

signed main() {
  const int k = rint();
  int rf[8] = {}, p[8 - k];
  bool cf[8] = {};
  dotimes(i, k) {
    int r = rint();
    int c = rint();
    rf[r] = c + 1;
    cf[c] = true;
  }
  for (int i = 0, j = 0, c = 0; i < 8; i++)
    if (!rf[i]) {
      while (cf[c])
        c++;
      p[j++] = ++c;
    }
  int a[8];
  for (;; next_permutation(p, p + 8 - k)) {
    for (int i = 0, j = 0; i < 8; i++)
      a[i] = rf[i] ? rf[i] : p[j++];
    dotimes(i, 8)
      dotimes(j, i)
        if (abs(a[j] - a[i]) == abs(j - i))
          goto failed;
    break;
  failed:
    ;
  }
  dotimes(i, 8) {
    dotimes(j, 8)
      putchar(a[i] == j + 1 ? 'Q' : '.');
    putchar('\n');
  }
  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#include <sstream>
#include <complex>
#define BIT(nr) (1ULL << (nr))
#define int long long
#define ll long long
#define double complex<long double>
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807
#define LL_HALFMAX 9223372036854775807 / 2
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
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; }

using namespace std;

int G[15][15];

signed main() {
	map<int, int> x, y, d1, d2;
	// d1: 和が一定 d2: 差が一定
	int k;
	cin >> k;
	vector<string> ans(8, "........"), fans(8, "........");
	rep(i, k) {
		int r, c;
		cin >> r >> c;
		ans[r][c] = 'Q';

		y[r] = 1;
		x[c] = 1;
		d1[r + c] = 1;
		d2[c - r] = 1;
	}

	function<void(int)> dfs = [&](int xPos) {
		if (xPos == 8) {
			fans = ans;
			return;
		}
		// 既に置いてあったらNG
		if (x[xPos] == 1) {
			dfs(xPos+1);
		}

		rep(yPos, 8) {
			if (y[yPos] == 1) {
				continue;
			}
			if (d1[xPos + yPos] == 1) {
				continue;
			}
			if (d2[xPos - yPos] == 1) {
				continue;
			}

			y[yPos] = 1;
			d1[xPos + yPos] = 1;
			d2[xPos - yPos] = 1;
			ans[yPos][xPos] = 'Q';

			dfs(xPos + 1);

			y[yPos] = 0;
			d1[xPos + yPos] = 0;
			d2[xPos - yPos] = 0;
			ans[yPos][xPos] = '.';
		}
	};

	dfs(0);

	rep(i, 8) {
		cout << fans[i] << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) printf("%lld ",i);puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 2e5+10;
int n;

inline PA Get(int d,ll c){
	if (c==1) return {d,0};
	PA tmp=Get(d,c/2);
	tmp.se=(tmp.se*2+1+(tmp.fi*2>=10));
	if (tmp.fi*2>=10) tmp.fi=(tmp.fi*2)/10+(tmp.fi*2)%10;
		else tmp.fi*=2;
	if (c&1){
		tmp.se=tmp.se+1+(tmp.fi+d>=10);
		tmp.fi=(tmp.fi+d)/10+(tmp.fi+d)%10;
	}
	return tmp;
}

int main(){
	n=read();
	ll x=0,ans=0;
	For(i,1,n){
		ll d=read(),c=read();
		PA tmp=Get(d,c);
		ans+=tmp.se;
		if (i!=1) ans++;
		ans+=(x+tmp.fi>=10);
		x=(x+tmp.fi)/10+(x+tmp.fi)%10;
		//printf(" %lld %lld\n",x,ans);
	}
	printf("%lld\n",ans);
}

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

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 999999999

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long int ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int const MOD = 1000000007;

int dayOfTheWeek( int y, int m, int d )
{
    if( m < 3 ) {
        y--; m += 12;
    }
    return ( y + y/4 - y/100 + y/400 + ( 13*m + 8 )/5 + d )%7;
}

int main() {
    string dayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    int m,d;
    while(cin >> m, m) {
        cin >> d;
        cout << dayname[dayOfTheWeek(2004,m,d)] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define per(i, j, k) for (int i = j; i >= k; --i)
#define int long long
using namespace std;

const int N = 1000001;
int a[N], sum[N];

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

int Abs(int x) {return x < 0 ? -x : x;}

signed main() {
	int n = read();
	rep(i, 1, n) {
		a[i] = read();
		sum[i] = sum[i - 1] + a[i];
	}
	int ans = 1e18, s1 = a[1], s2 = a[2], s3 = a[3], s4 = sum[n] - sum[3], l = 2, r = 4;
	rep(i, 3, n) {
		while (l < i && Abs(s1 - s2) > Abs(s1 + a[l] - s2 + a[l])) {
			s1 += a[l];
			s2 -= a[l];
			l++;
		}
		while (r <= n && Abs (s3 - s4) > Abs(s3 + a[r] - s4 + a[r])) {
			s3 += a[r];
			s4 -= a[r];
			r++;
		}
		int mn = min(s1, min(s2, min(s3, s4)));
		int mx = max(s1, max(s2, max(s3, s4)));
		ans = min(ans, mx - mn);
		s2 += a[i];
		s3 -= a[i];
//		printf("%d %d %d %d %d %d %d\n", s1, s2, s3, s4, mn, mx, ans);
	}
	printf("%lld\n", ans);
	return 0;
}

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

using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2>
bool chmin(T1 &l, const T2 &r);
template <class T1, class T2>
bool chmax(T1 &l, const T2 &r);

lli sum[200005] = {};
lli rec(lli l, lli r)
// [l, r)
{
  return sum[r] - sum[l];
}
bool pivot(lli l, lli r, lli piv)
{
  //  assert(l <= piv + 1 && piv < r - 1);
  return rec(piv, r) - rec(l, piv) >= 0;
}
lli calc(int l, int r, int piv)
{
  return rec(piv, r) - rec(l, piv);
}
int good(int l, int r, int piv1, int piv2)
{
  if (abs(calc(l, r, piv1)) < abs(calc(l, r, piv2)))
    return piv1;
  return piv2;
}
pair<lli, lli> calc(lli l, lli r)
{
  lli low = l + 1, up = r;
  while (up - low > 1)
  {
    lli mid = (up + low) / 2;
    if (pivot(l, r, mid))
    {
      low = mid;
    }
    else
    {
      up = mid;
    }
  }
  int go = good(l, r, low, up);
  return make_pair(rec(l, go), rec(go, r));
}
void solve(long long N, std::vector<long long> A)
{
  rep(i, N) sum[i + 1] = sum[i] + A[i];
  lli ans = 1e18;
  for (int i = 2; i <= N - 2; i++)
  {
    // [0, i) , [i, N) に分割
    int ll = 0, lr = i;
    int rl = i, rr = N;
    auto ls = calc(ll, lr);
    auto rs = calc(rl, rr);
    chmin(ans, max({ls.first, ls.second, rs.first, rs.second}) - min({ls.first, ls.second, rs.first, rs.second}));
  }
  cout << ans << endl;
}

int main()
{
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++)
  {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2>
bool chmin(T1 &l, const T2 &r)
{
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2>
bool chmax(T1 &l, const T2 &r)
{
  return (l < r) ? (l = r, true) : false;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
//ll mod = 998244353;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a + b > c + d) {
        cout << "Left" << endl;
    }
    else if (a + b < c + d) {
        cout << "Right" << endl;
    }
    else {
        cout << "Balanced" << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<int,int>
#define endl "\n"
#define MAXN 100005
#define mod 1000000007
using namespace std;

void solve()
{

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    solve();
    ll d,t,s;
    cin>>d>>t>>s;
    float time = (float)d/s;
    if(time <= t)
    {
        cout<<"Yes"<<'\n';
    }
    else
    {
        cout<<"No"<<'\n';
    }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
const int MAXN = 15;

struct edg{
	int s, e, x;
};

vector<edg> v;
int cyc[1 << MAXN];
int dp[1 << MAXN][MAXN];
int adj[MAXN][MAXN];
int n, m;

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0; i<MAXN; i++){
		for(int j=0; j<MAXN; j++){
			adj[i][j] = -1e9;
		}
	}
	int ans = 0;
	for(int i=0; i<m; i++){
		int s, e, x;
		scanf("%d %d %d",&s,&e,&x);
		adj[s-1][e-1] = x;
		adj[e-1][s-1] = x;
		v.push_back({s-1, e-1, x});
	}
	for(int i=0; i<(1<<n); i++){
		for(auto &j : v){
			if((i >> j.s) % 2 == 1 & (i >> j.e) % 2 == 1){
				cyc[i] += j.x;
			}
		}
	}
	for(int i=0; i<(1<<n); i++){
		for(int j=0; j<n; j++){
			dp[i][j] = -1e9;
			if((i >> j) % 2 == 0){
				continue;
			}
			if(j == 0){
				dp[i][j] = cyc[i];
			}
			for(int k=i; k; k=(k-1)&i){
				if((k >> j) % 2 == 0) continue;
				for(int l=0; l<n; l++){
					dp[i][j] = max(dp[i][j], dp[i^k][l] + adj[l][j] + cyc[k]);
				}
			}
		}
	}
	int ret = dp[(1<<n)-1][n-1];
	for(auto &i : v) ret -= i.x;
	cout << -ret << endl;
}

#include <algorithm>
// #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double db;
const int oo=2139063143;
const int N=17;
const int P=1000000007;
const db eps=1e-7;
#define pritnf printf
//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T>
inline void sc (T &x)
{
    x=0; static int p; p=1; static char c; c=getchar();
    while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }
    while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }
    x*=p;
}
template <typename T>
inline void print (T x)
{
    if (x< 0) putchar('-'),x=-x;
    if (x>=10) print(x/10);
    putchar(x%10+'0');
}
template <typename T>
inline void pr (T x) { print(x),putchar('\n'); }
template <typename T1,typename T2>
void chkmax (T1 &A,T2 B) { A=A>=B?A:B; }
template <typename T1,typename T2>
void chkmin (T1 &A,T2 B) { A=A<=B?A:B; }
int n,m,e[N][N],e0=0,g[1<<N|1],f[1<<N|1][N];

int main ()
{
    // freopen (".in","r",stdin);
    // freopen (".out","w",stdout);
	sc(n),sc(m);
	for (int i=1; i<=m; i++)
	{
		int x,y; sc(x),sc(y),sc(e[x][y]),e[y][x]=e[x][y];
		e0+=e[x][y];
	}
	int S=(1<<n)-1;
	for (int s=1; s<=S; s++)
		for (int i=1; i<=n; i++) if (s>>(i-1)&1)
			for (int j=i; j<=n; j++) if (s>>(j-1)&1)
				g[s]+=e[i][j];
	for (int s=0; s<=S; s++)
		for (int i=0; i<=n; i++)
			f[s][i]=-2e9;
	f[1][1]=0;
	for (int s=1; s< S; s++)
		for (int u=1; u<=n; u++) if (s>>(u-1)&1)
		{
			for (int v=1; v<=n; v++) if (!(s>>(v-1)&1)&&e[u][v])
				chkmax (f[s|(1<<(v-1))][v],f[s][u]+e[u][v]);
			int ss=S^s;
			for (int t=ss; t; t=(t-1)&ss)
				chkmax (f[s|t][u],f[s][u]+g[t|(1<<(u-1))]);
		}
	pr(e0-f[S][n]);

    return 0;
}


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

using namespace std;

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

	int S, n;
	cin >> S >> n;
	vector<int> a(n);
	for (auto &t : a) {
		cin >> t;
	}
	int mx = *max_element(a.begin(), a.end());
	if (mx > (S + 1) / 2) {
		cout << (mx - (S + 1) / 2) * 2 - (S % 2 == 0) << endl;
	} else {
		cout << 0 << endl;
	}
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<cstring>
#include<iomanip> //cout << fixed << setprecision(15) << x << endl;

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1LL<<60;
#define Pint pair<int, int>
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
/* -- template -- */

int main() {
    int K, T; cin >> K >> T;
    int a[T]; rep(i, T) cin >> a[i];
    sort(a, a + T, greater<int>());
    int sum = 0;
    rep(i, T - 1)
        sum += a[i + 1];
    cout << ((a[0] - sum - 1 < 0) ? 0 : a[0] - sum - 1) << endl;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <climits>
using namespace std;
#define MOD int(1e9 + 7)
#define INF (long long)(1e18)

int main()
{
    long double N, M, D;
    cin >> N >> M >> D;

    cout.precision(18);
    cout << ((D) ? 2 : 1) * (M - 1) * (N - D) / (N * N) << endl;
}

//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <tuple>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>

using namespace std;
using ll=long long;
using intpair=pair<int,int>;
using intpv=vector<intpair>;
using llpair=pair<ll,ll>;
using llpv=vector<llpair>;
using intvec=vector<int>;
using llvec=vector<ll>;
using intq=queue<int>;
using llq=queue<ll>;
using intmat=vector<intvec>;
using llmat=vector<llvec>;
using pairmat=vector<llpv>;

template<typename T>
using matrix=vector<vector<T>>;
template<typename T>
using pque=priority_queue<T>;
template<typename T>
using lpque=priority_queue<T,vector<T>,greater<T>>;

#define PI 3.141592653589793
#define INTINF 1<<30
#define LLINF 1LL<<60
#define MPRIME 1000000007
#define MPRIME9 998244353

#define pushb push_back
#define fi first
#define se second

#define setpr fixed<<setprecision(15)
#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())
#define init(v,size) v.resize(size); for(auto &a__: v) cin>>a__
#define matin(mat,H,W) for(int i__=0; i__<H; i__++)for(int j__=0; j__<W; j__++) cin>>mat[i__][j__]
#define out(n) cout<<n<<endl

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

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

template<class T> inline void outmat(T& a, int y, int x) {
	for(int i=0; i<y; i++) {
		for(int j=0; j<x; j++) cout<<a[i][j]<<" ";
		cout<<endl;
	}
}

ll GCD(ll a, ll b) { //最大公約数
	if(a==0||b==0) return 0;
	if(a<b) swap(a,b);
	ll tmp = a%b;
	while(tmp!=0) {
		a = b;
		b = tmp;
		tmp = a%b;
	}
	return b;
}

ll binpow(ll a, ll ex, ll p) { //繰り返し二乗法
	ll result=1;
	while(ex>0) {
		if(ex&1) result=result*a%p;
		ex>>=1;
		a=a*a%p;
	}
	return result;
}

ll Fact(ll x, ll p) { //階乗
	ll f=1;
	for(ll i=2; i<=x; i++) {
		f*=i;
		f%=p;
	}
	return f;
}

ll nPr(ll n, ll r) {
	if(n<r) return 0;

	ll result=1LL;
	for(ll i=0; i<r; i++) result*=n-i;
	return result;
}

ll nPrP(ll n, ll r, ll p) { // mod pにおけるnPr
	if(n<r) return 0;

	ll result=1LL;
	for(int i=0; i<r; i++) {
		result*=n-i;
		result%=p;
	}
	return result;
}

ll nCr(ll n, ll r) {
	if (n == r) { return 1; }
	if (r > n) { return 0; }

	if (r > n / 2) { r = n - r; }

	if (n == 0) { return 0; }
	if (r == 0) { return 1; }
	if (r == 1) { return n; }

	double result = 1;
	for (double i = 1; i <= r; i++) {
		result *= (n - i + 1) / i;
	}

	return (ll)result;
}

int DigitNum(ll n) { //桁数
	int digit=0;
	ll div=1LL;
	while(n/div) {
		digit++;
		div*=10;
	}
	return digit;
}

bool Palind(string s) { //回文判定
	return s == string(s.rbegin(), s.rend());
}

double n,m,d;

void input() {
	cin>>n>>m>>d;
}

void solve() {
	double D;
	if(d) D=2.*(n-d)/n/n;
	else  D=1/n;

	cout<<setpr<<(m-1)*D<<endl;
}

int main() {
	while(1) {
		input();
		solve();
		break;
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define forg(i,x) for(int i=first[x];i;i=nxt[i])
#define uu unsigned
#define fi first
#define se second
#define lam(z,k) [&](const z &a,const z &b){ return k; }
#define od(x) ((x)&1)
#define ev(x) (od(x)^1)
#define mi2(x) (1<<(x))
#define scanf a1234=scanf
int a1234;
char buf[1<<18],*bufs=buf,*buft=buf;
inline int gc(){
    return bufs==buft&&(buft=(bufs=buf)+fread(buf,1,1<<18,stdin)),bufs==buft?-1:*bufs++;
}
inline void xxx(){for(;;);}

const int mxn=2003,mod=1e9+7;
int n,k;
int dp[mxn][mxn],fac[mxn*mxn],inv[mxn*mxn];
inline int por(long long a,int b){
    long long res=1;
    for(;b;b>>=1){
        if(b&1)res=res*a%mod;
        a=a*a%mod;
    }
    return res;
}
inline long long C(int a,int b){return 1ll*fac[a]*inv[a-b]%mod*inv[b]%mod;}
inline long long P(int a,int b){return 1ll*fac[a]*inv[a-b]%mod;}
inline long long cha(int a,int b){return C(a+b,a);}
template<class T>inline void tsum(T &a,int b){a+=b;if(a>=mod)a-=mod;}
int main(){
    scanf("%d%d",&n,&k);if(k==1)return puts("1"),0;
    fac[0]=inv[0]=1;for(int i=1;i<=n*k;++i)fac[i]=1ll*fac[i-1]*i%mod;
    inv[n*k]=por(fac[n*k],mod-2);for(int i=n*k-1;i;--i)inv[i]=1ll*inv[i+1]*(i+1)%mod;
    dp[1][1]=1;
    for(int i=1;i<=n;++i)for(int j=i;j;--j){
        //printf("%d %d %d\n",i,j,dp[i][j]);
        tsum(dp[i][j-1],dp[i][j]);
        if(!dp[i][j])continue;
        dp[i][j]=dp[i][j]*cha((i-1)*k-j+1,k-2)%mod;

        tsum(dp[i+1][j+1],dp[i][j]);
//        for(int jj=1;jj<=j+1;++jj)tsum(dp[i+1][jj],dp[i][j]*cha(i*k-jj+1,k-2)%mod);//,printf("%d %d %d\n",i+1,jj,dp[i+1][jj]);
    }
    long long ans=0;
    for(int i=1;i<=n;++i)tsum(ans,dp[n][i]);
    printf("%lld\n",ans*fac[n]%mod);
    return 0;
}

#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 2e3+5, M = 4e6+5, P = 1e9+7;
int n, m, f[N][N], fac[M], ifac[M], inv[M], ans;
int C(int n, int m) { return 1ll*fac[n]*ifac[n-m]%P*ifac[m]%P; }
int main()
{
    n = read(), m = read();
    inv[1] = ifac[0] = fac[0] = 1;
    for(int i = 2; i <= n*m; ++i) inv[i] = 1ll*(P-P/i)*inv[P%i]%P;
    for(int i = 1; i <= n*m; ++i) fac[i] = 1ll*i*fac[i-1]%P, ifac[i] = 1ll*inv[i]*ifac[i-1]%P;
    f[0][0] = 1;
    for(int i = 1; i <= n; ++i)
        for(int j = 0; j <= i; ++j)
        {
            f[i][j] = f[i-1][j];
            if(j) f[i][j] = (f[i][j]+1ll*f[i][j-1]*(n-j+1)%P*C(n*m-i-(j-1)*(m-1)-1, m-2)%P)%P;
        }
    printf("%d\n", m == 1?1:f[n][n]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define X ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x)

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

// long long p = 1e9+7;

typedef long long ll;
typedef pair<ll,ll> pl;
typedef vector<int> VI;
typedef vector<pair<ll,ll>> VP;
typedef vector<ll> VL;
typedef vector<bool> VB;

// typedef pair<ll, ll> PL;
typedef unordered_map<ll, ll> UMP;
#define FOR(i,b) for(i=0;i<b;i++)
#define pb push_back
#define mp make_pair

// typedef  unordered_set<ll>;

void printa(VI &x,ll n){
    ll i;
    FOR(i, n){
        cout<<x[i]<<" ";
    }
    cout<<endl;
}

/////GLOABLS VARS
ll MOD = 1e9+7;
ll gmx = 1e6+7;

VL fact(gmx, 1);
//////FUNCTIONS
ll powp(ll val, ll deg)
{
    // debug(val, deg);
    if (!deg)
        return 1;
    if (deg & 1)
        return (powp(val, deg - 1) * val) % MOD;
    ll res = powp(val, deg >> 1);
    // debug(res);
    return (res * res) % MOD;
}

//It is not easy but it can be fun, if you think!!!

//
// ll mx = 2*1e5+7;

int main(){
      ios::sync_with_stdio(0);
      cin.tie(0);
        // #ifndef ONLINE_JUDGE
        //     freopen("input.txt", "r", stdin);
        //     freopen("output.txt", "w", stdout);
        //     #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
        // #endif
    ll n,i,j;
    cin>>n;
    // priority_queue<ll> l;
    VL l(n+1);
    FOR(i,n){
        ll u;
        cin>>u;
        l[i+1] = u;
    }
    VL arr(n+1);
    ll m = sqrt(n)+1;
    VL ans;
    for(i=n;i>=1;i--){
        // debug(arr,l);
        if(l[i]!=arr[i]){
            // debug(i);
            ans.pb(i);
            unordered_set<ll> seen;
            for(j = 1;j<m;j++){
                if(i%j==0){
                    ll p =i/j;
                    // debug(p,j);
                    if(p==j){
                        if(seen.find(p)==seen.end()){
                        arr[p]  =1-arr[p];
                        seen.insert(p);
                    }
                        // ans.pb(p);

                    }
                    else{
                        if(seen.find(p)==seen.end()){
                        arr[p]  =1-arr[p];seen.insert(p);
                    }
                        if(seen.find(j)==seen.end()){
                        arr[j]  =1-arr[j];seen.insert(j);
                    }
                        // ans.pb(p);
                        // ans.pb(j);
                    }
                }
            }
        }
    }
    cout<<ans.size()<<'\n';
    for(auto k:ans){
        cout<<k<<'\n';
    }

    // }

}

#include <bits/stdc++.h>
int N;
using namespace std;
int main() {
    cin >> N;

    vector<queue<int>> q(N);
    for(int i = 0; i < N; ++i) {
        for(int j = 0; j < N-1; ++j) {
            int value;
            cin >> value;
            q.at(i).push(value-1);
        }
    }

    // 優先度が高い順にpushしてある
    // 1から順に試合をしていく。全部満たさないといけないので誰から試合を初めてもかまわない

    chrono::system_clock::time_point start, end;
    start = chrono::system_clock::now();
    int num = 0;
    int days = 0;
    int finish = N * (N-1) / 2;
    while(finish > num) {
        end = chrono::system_clock::now();
        double time = static_cast<double>(chrono::duration_cast<chrono::microseconds>(end-start).count() / 1000.0);
        // cout << setprecision(14) << time << endl;
        if(time > 1800.0) {
            cout << finish << endl;
            return 0;
        }
        days++;
        vector<int> match(N, 0);
        for(int i = 0; i < N; ++i) {
            if(q.at(i).empty()) {
                continue;
            }else {
                if(match.at(i) == 1) {
                    q.at(i).pop();
                }else {
                    int rival = q.at(i).front();
                    if(match.at(rival) == 0) {
                        if(q.at(rival).front() == i) {
                            match.at(i) = match.at(rival) = 1;
                            q.at(i).pop();
                            num++;
                        }
                    }
                }
            }
        }
        int flag = 0;
        for(int i = 0; i < N; ++i) {
            if(match.at(i) == 1) {
                flag = 1;
                break;
            }
        }
        int kazu = 0;
        for(int i = 0; i < N; ++i) {
            if(!q.at(i).empty()) kazu++;
        }
        if(kazu == 1) break;
        if(flag == 0) {
            break;
        }
    }

    if(finish == num) {
        cout << days << endl;
    }else {
        cout << "-1" << endl;
    }
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <climits>
#include <algorithm>
#include <utility>

using namespace std;
using Int = long long int;
using UInt = unsigned long long int;
using VI = vector<long long int>;

int main(void) {
    Int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<Int> v(n);
    v[0] = 0;
    for (Int i = 1; i < n; ++i) {
        if (s[i - 1] == 'A' && s[i] == 'C') {
            v[i] = v[i - 1] + 1;
        } else {
            v[i] = v[i - 1];
        }
    }
    for (Int i = 0; i < q; ++i) {
        Int l, r;
        cin >> l >> r;
        cout << v[r - 1] - v[l - 1] << endl;
    }
    return 0;
}

#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<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
const int M=(1<<20)+5;
const int p=1e9+7;

int read()
{
	int s=0;
	char c=getchar(),lc='+';
	while (c<'0'||'9'<c) lc=c,c=getchar();
	while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
	return lc=='-'?-s:s;
}
void write(int x)
{
	if (x<0)
	{
		putchar('-');
		x=-x;
	}
	if (x<10) putchar(x+'0');
	else
	{
		write(x/10);
		putchar(x%10+'0');
	}
}
void print(int x,char c='\n')
{
	write(x);
	putchar(c);
}
void add(int &x,int y)
{
	x+=y;
	if (x>=p) x-=p;
}
int sum1[M],sum2[M],dp[N],s[N],x[N];

signed main()
{
	x[0]=s[0]=0;
	memset(sum1,0,sizeof(sum1));
	memset(sum2,0,sizeof(sum2));
	int n=read();
	dp[0]=1;
	sum1[0]=1;
	for (int i=1;i<=n;i++)
	{
		x[i]=x[i-1]^read();
		s[i]=s[i-1]+(x[i]==0);
		dp[i]=((s[i-1]*sum1[x[i]]-sum2[x[i]]+1)%p+p)%p;
		add(sum1[x[i]],dp[i]);
		add(sum2[x[i]],dp[i]*s[i]%p);
	}
	if (x[n]) print(dp[n]);
	else
	{
		int ans=dp[n];
		for (int i=1;i<=n;i++) if (x[i]) add(ans,dp[i]);
		print(ans);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	long long sum1 = 0;
	long long numbers[200000];
	int N;
	cin >> N;
	for(int i = 0; i < N; ++i){
		cin >> numbers[i];
	}
	for(int i = 0; i < N-1; ++i){
		if(numbers[i+1] < numbers[i]){
			sum1+=numbers[i] - numbers[i+1];
			numbers[i+1] = numbers[i+1] + (numbers[i] - numbers[i+1]);
		}

	}
	cout << sum1;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <time.h>
#include <chrono>
#include <random>
//#include <math.h>
#include <cmath>
#include <fstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <stdlib.h>
#include <iomanip>
#include <cstdio>
#include <cassert>
#include <bitset>

#pragma comment(linker, "/STACK:36777216")
using namespace std;

#define flu fflush(stdout)
#define gg return 0
#define mp make_pair

typedef long double ptype;

long long mod = /*27449*//*998244353*/ 1e9 + 7 /*1e9 + 9*/;
int inf = 1e9;
double eps = 1e-8;
long double pi = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ifstream in("input.txt");
ofstream out("output.txt");

void solve()
{
	long long ans = 0;
	int n;
	cin >> n;
	int mx = 0;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		if (t < mx)
		{
			ans += mx - t;
		}
		else
		{
			mx = t;
		}
	}
	cout << ans;
}

int main()
{
	cout << setprecision(20) << fixed;
	cin.tie();
	cout.tie();
	clock_t t1 = clock();
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int t;
	//cin >> t;
	t = 1;
	while (t--) {
		solve();
		//test();
	}
	//cout << (clock() - t1) * 1.0 / CLOCKS_PER_SEC;
	gg;
}


#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
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(b<a) { a=b; return 1; } return 0; }

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int k,t; cin >> k >> t;
  int mx = 0;
  vi a(t);
  rep(i,t){
    cin >> a[i];
    chmax(mx,a[i]);
  }
  if((k+1)/2 < mx){
    cout << (mx-(k+1)/2)*2-1;
  }else{
    cout << 0;
  }
  cout << "\n";
  return 0;
}

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

int main() {
    int k, t;
    cin >> k >> t;
    int a[t];
    for (int i = 0; i < t; ++i) {
        cin >> a[i];
    }

    sort(a, a + t);

    cout << max(0, a[t - 1] - k + a[t - 1] - 1) << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define Opy ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)

typedef long long ll;

int main()
{
    //Opy;

/*-------------------------------------------------
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
-------------------------------------------------*/
    ll a, h = 0;

    cin>> a;

    h += (a - (a % 500)) * 2;

    a %= 500;

    h += a - (a % 5);

    cout<< h<< endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REV(i,a,n) for(int i=a;i>=n;i--)
#define all(a) a.begin(),a.end()
const int NUM = 2e5 + 5;

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int num_tests=1;
    //cin >> num_tests;
    while(num_tests-->0){
        int n; cin >> n;
        int ans = 0;
        while(n >= 500)
        {
            int x = n/500;
            ans += x*1000;
            n -= (x)*500;
        }
        while(n >= 5)
        {
            int y = n/5;
            ans += y*5;
            n -= y*5;
        }
        cout << ans;
    }
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define str to_string
#define endl "\n"
#define PI 3.141592653589
using namespace std;
using lint = long long;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

//AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC
int main(){
    int a,b,c;cin>>a>>b>>c;
    if(a>b)swap(a,b);
    if(a<=c&&c<=b)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

#include<iostream>
using namespace std;

int main(){
  int a,b,c;
  cin >> a >> b >> c;
  cout << ((a-c)*(b-c)<0?"Yes":"No") << endl;
}


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

double solve(vector<vector<vector<double>>>& dp, int a, int b, int c, double N){
	if(dp[a][b][c] != 0){
		return dp[a][b][c];
	}
	if(a==0 && b==0 && c==0) return 0;
	double f = 0;
	f +=  1 / (1 - (N-a-b-c)/N );
	if(a>0) f += solve(dp,a-1,b,c, N)   * (a/N) /(1 - (N-a-b-c)/N );
	if(b>0) f += solve(dp,a+1,b-1,c, N) * (b/N) /(1 - (N-a-b-c)/N );
	if(c>0) f += solve(dp,a,b+1,c-1, N) * (c/N) /(1 - (N-a-b-c)/N );
	return dp[a][b][c] = f;

}

int main(){
	int N;
	cin >> N;
	vector<vector<vector<double>>> dp(N+1,vector<vector<double>>(N+1,vector<double>(N+1,0)));
	int a=0, b=0, c=0;
	for(int i=0; i<N; i++){
		int t; cin >> t;
		if(t==1) a++;
		else if(t==2) b++;
		else c++;
	}
	printf("%.9lf\n",solve(dp,a,b,c,(double)N));
	return 0;
}

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

    }
    return 0;
}


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

using namespace std;

#define INF 10000001

int main()
{
	int a, n;
	int maxi, mini;
	int i;
	long long sum = 0ll;

	mini = INF;
	maxi = -10000001;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &a);
		maxi = max(a, maxi);
		mini = min(a, mini);
		sum += a;
	}

	printf("%d %d ", mini, maxi);
	cout << sum << endl;

	return (0);
}

#include <iostream>
using namespace std;

int main(){
    int min,max,n,i;
    int *data;
    long sum;
    cin>>n;
    data= new int[n];
    for (i=0;i<n;i++)cin>>data[i];
    min=data[0];max=data[0];sum=data[0];
    for (i=1;i<n;i++){
      sum+=data[i];
      if (min>data[i])min=data[i];
      else if (max<data[i])max=data[i];
    }
    cout<<min<<" "<<max<<" "<<sum<<endl;
    return 0;

}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <math.h>
#include <assert.h>
#include <set>
#include <map>
#include <bitset>
#include <ctime>
#include <time.h>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <stack>
#include <ctype.h>
#include <numeric>
#include <sstream>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <limits>
#include <random>
#include <chrono>
#include <iomanip>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
#define endl '\n'
#define debug(name) DEBUG(#name, (name))
template<typename T>
void DEBUG(string label, T value) {
    cerr << "[" << label << " = " << value << "]\n";
}

void solve(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
    }
    sort(arr.begin(),arr.end());
    arr.resize(unique(arr.begin(), arr.end()) - arr.begin());
    cout << ((int)arr.size() == n ? "YES" : "NO") << endl;
}

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007

int main() {
	ll n;
	cin>>n;
	map<ll,ll>ch;
	for(ll i=0;i<n;i++){
		ll x;
		cin>>x;
		ch[x]++;
		if(ch[x]==2){
			cout << "NO";
			return 0;
		}
	}
	cout << "YES";
	// your code goes here
	return 0;
}


#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <numeric>
#include <cmath>
#include <queue>
#include <iomanip>
#include <functional>

#define CHMAX(a,b) a = std::max(a,b)
#define CHMIN(a,b) a = std::min(a,b)
#define CHABS(a) a = std::abs(a)
#define COUT(a) std::cout << a << std::endl
#define CERR(a) std::cerr << a << std::endl
#define FOR(n) for(lli i = 0; i < n; i++)

using namespace std;
using lli = long long int;
using pll = pair<lli, lli>;
using tlll = tuple<lli, lli, lli>;
using vll = vector<lli>;

lli mod197 = 1000000007LL;
lli INF = 10000000000000;

// ax + by = gcd(a,b) 最大公約数
template< typename T >
T extgcd(T a, T b, T& x, T& y) {
	T d = a;
	if (b != 0) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	else {
		x = 1;
		y = 0;
	}
	return d;
}

int main(void) {

	int n, h, w;
	cin >> n >> h >> w;

	COUT((n - h + 1) * (n - w + 1));

	return 0;
}

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

int main() {
  int n,h,w;
  cin >> n >> h >> w;
  int a = 0;
  int b = 0;
  for(int i = 1; i <= n; i++){
    if(i+w-1 <= n) a++;
    if(i+h-1 <= n) b++;
  }
  cout << a*b << endl;
  return 0;
}


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

int main(){

	int w[10], k[10];

	for(int i=0; i<2; i++){
		for(int j=0; j<10; j++){
			if(i == 0) cin >> w[j];
			else cin >> k[j];
		}
	}

	sort(w, w+10, greater<int>());
	sort(k, k+10, greater<int>());

	cout << w[0] + w[1] + w[2] << " " << k[0] + k[1] + k[2] << endl;

	return 0;

}

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;
typedef long long ll;
using gra = vector<vector<ll>>;
using gram = vector<unordered_map<ll,ll>>;

typedef unsigned long long ull;

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

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> T dup(T a, T b) { return (a + b - 1) / b; }
template <class T> T modpow(T x, T p, T m) { T a=1; while(p){ if(p%2)a =a*x%m; x = x+x%m; p/=2; } return a; }
// template <class T> void prim(T a) { for(ll i=0;i<a.size();++i) { cout << a[i]; } cout << "\n"; }
//template< typename T >
//T mod_pow(T x, T n, const T &p) {
//  T ret = 1;
//  while(n > 0) {
//    if(n & 1) (ret *= x) %= p;
//    (x *= x) %= p;
//    n >>= 1;
//  }
//  return ret;
//}
///////////////////
//

#define mod 1000000007LL
#define INF 10000000000000000LL

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

#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
//#define modpow(x, p, m) { ll a=1; while(p){ if(p%2)a =a*x%m; x = x+x%m; p/=2; } return a; }

#define m0(h) memset(h, 0, sizeof(h))
#define m1(h) memset(h, 1, sizeof(h))
#define m(h, n) memset(h, (n), sizeof(h))
#define all(n) (n).begin(),(n).end()
#define bit(n) (1LL << (n))
#define pb push_back
#define mp make_pair

//#ifdef LOCAL
//
//#define dpri(...) fprintf(stderr, __VA_ARGS__)
//#define pria(g) rep(i,g.size()){ cout << i << "{"; rep(j,g[i].size()) cout << g[i][j] << " "; cout << "}" << "\n"; }
//#else
//#define dpri(...) 42
//#define pria(g) 42
//#endif
//
//
//

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // ---------------------
  // コーディング開始
  // ---------------------
  string a;
  cin >> a;
  if(a.length()==2) cout << a << endl;
  else cout << a[2] << a[1] << a[0] << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll H, W;
    cin >> H >> W;
    mat A(H, vec(W, 0));
    REP(i,0,H) {
        REP(j,0,W) {
            cin >> A[i][j];
            A[i][j] %= 2;
        }
    }

    mat Z;
    REP(i,0,H) {
        bool f = false;
        REP(j,0,W-1) {
            vec z = {i+1, j+1, i+1, j+2};
            if(A[i][j] == 1) {
                if(!f) {
                    A[i][j] = 0;
                    f = true;
                }
                else {
                    A[i][j] = 0;
                    f = false;
                }
            }
            if(f) Z.pb(z);
        }
        if(f) A[i][W-1] = 1 - A[i][W-1];
    }

    bool f = false;
    REP(i,0,H-1) {
        vec z = {i+1, W, i+2, W};
        if(A[i][W-1] == 1) {
            if(!f) {
                A[i][W-1] = 0;
                f = true;
            }
            else {
                A[i][W-1] = 0;
                f = false;
            }
        }
        if(f) Z.pb(z);
    }
    if(f) A[H-1][W-1] = 1 - A[H-1][W-1];

    PR(SZ(Z));
    REP(i,0,SZ(Z)) {
        PS(Z[i][0]); PS(Z[i][1]); PS(Z[i][2]); PR(Z[i][3]);
    }

    return 0;
}

/*

*/

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

const ll INF = 1LL << 62;
const double PI = acos(-1);
const double eps = 1e-10;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w)), ans;
  rep(i, h) rep(j, w) cin >> a[i][j];

  rep(i, h - 1) rep(j, w) {
    if (a[i][j] % 2 == 1) {
      ans.push_back({i + 1, j + 1, i + 2, j + 1});
      // cout << i + 1 << " " << j + 1 << " " << i + 2 << " " << j + 1 << endl;
      a[i][j]--;
      a[i + 1][j]++;
    }
  }
  rep(i, w - 1) {
    if (a[h - 1][i] % 2 == 1) {
      ans.push_back({h, i + 1, h, i + 2});
      // cout << h - 1 << " " << i + 1 << " " << h - 1 << " " << i + 2 << endl;
      a[h - 1][i]--;
      a[h - 1][i + 1]++;
    }
  }

  cout << ans.size() << endl;
  rep(i, ans.size()) rep(j, 4) printf("%d%c", ans[i][j], (j == 3 ? '\n' : ' '));
  // rep(i, h) rep(j, w)
  //     fprintf(stderr, "%d%c", a[i][j], (j == w - 1 ? '\n' : ' '));
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
ll pl(ll a, ll b) {
    a %= mod, b %= mod;
    return (a+b) % mod;
}
ll mu(ll a, ll b) {
    a %= mod, b %= mod;
    return (a*b) % mod;
}
ll pow_mod(ll a, ll b) {
    if (b == 0) return 1;
    a %= mod;
    ll k = pow_mod(a, b/2);
    k = mu(k, k);
    if (b & 1) k = mu(k, a);
    return k;
}
ll di(ll a, ll b) {
    a %= mod;
    return mu(a, pow_mod(b, mod-2));
}
ll nCk(ll n, ll k) {
    ll res = 1;
    for (int a = n, b = k; b > 0; a--, b--) {
        res = mu(res, a);
        res = di(res, b);
    }
    return res;
}
int main () {
    ll A[2], K;
    cin >> A[0] >> A[1] >> K;
    ll ans = 0;
    for (int k = 0; k < 2; k++) {
        ll p = A[0], q = A[1];
        for (int i = 1; i <= q-1; i++) {
            ans += mu(mu(mu(p, p), q-i), i);
        }
        swap(A[0], A[1]);
    }
    ans = di(ans, nCk(A[0]*A[1], 2));
    ans = mu(ans, nCk(A[0]*A[1], K));
    ans = mu(ans, nCk(K, 2));
    cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

ll Pow(ll A, ll N){
  if (N ==0) return 1;
  else if (N % 2 == 0) return Pow(A*A%MOD, N/2);
  else return Pow(A*A%MOD, N/2) * A % MOD;
}

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

  ll cnt1 = 0, cnt2 = 0;
  for (ll i = 0; i <= N - 1; i++){
    cnt1 += i * (N - i); cnt1 %= MOD;
  }
  cnt1 *= M * M % MOD; cnt1 %= MOD;
  for (ll i = 0; i <= M - 1; i++){
    cnt2 += i * (M - i); cnt2 %= MOD;
  }
  cnt2 *= N * N % MOD; cnt2 %= MOD;
  ll ans = (cnt1 + cnt2) % MOD;

  ll nCr = 1;
  for (ll i = 1; i <= K - 2; i++){
    nCr *= N * M - 1 - i; nCr %= MOD;
    nCr *= Pow(i, MOD-2); nCr %= MOD;
  }
  ans *= nCr; ans %= MOD;
  cout << ans << endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
	string s;
	int q;
	cin >> s >> q;
	cin.ignore();

	string buf;
	while (q)
	{
		getline(cin, buf);

		//cout << "DEBUG:" << buf << endl;

		if (buf.find("replace ") != string::npos)
		{
			char cmd[10] = {0};
			int a1, a2;
			char a3[1001] = {0};
			sscanf(buf.c_str(), "%s %d %d %s", cmd, &a1, &a2, a3);
			s.replace(a1, (a2+1)-a1, string(a3));
			//cout << "DEBUG:replaced:" << s << endl;
		}
		else if (buf.find("reverse ") != string::npos)
		{
			char cmd[10] = {0};
			int a1, a2;
			sscanf(buf.c_str(), "%s %d %d", cmd, &a1, &a2);
			string sub = s.substr(a1, (a2+1)-a1);
			//cout << "DEBUG:sub( ):" << sub << endl;
			reverse(sub.begin(), sub.end());
			//cout << "DEBUG:sub(r):" << sub << endl;
			size_t subsize = sub.size();
			for (int i = a1, j = 0; i < a1 + subsize; i++, j++) {
				s[i] = sub[j];
			}
			//cout << "DEBUG:reversed:" << s << endl;
		}
		else if (buf.find("print ") != string::npos)
		{
			char cmd[10] = {0};
			int a1, a2;
			sscanf(buf.c_str(), "%s %d %d", cmd, &a1, &a2);
			cout << s.substr(a1, (a2+1)-a1) << endl;
		}
		q--;
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    REP(i, n) cin >> a[i], a[i]--;

    ll ret = a[0]!=0;
    vector<vector<ll>> g(n);
    FOR(i, 1, n) {
        g[i].push_back(a[i]);
        g[a[i]].push_back(i);
    }

    function<ll(ll,ll,ll)> dfs = [&](ll v, ll p, ll d) {
        ll ma = 1;
        for(auto to: g[v]) if(p != to) {
            ll val = dfs(to, v, d+1);
            dump(v, to, val);
            chmax(ma, val+1);
        }
        bool flag = false;
        if(ma == k && d+ma-1 > k) {
            ma = 0;
            ret++;
            flag = true;
        }
        dump(v, d, ma, flag);
        return ma;
    };
    dfs(0, -1, 0);

    cout << ret << endl;

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#define INF (1<<21)
using namespace std;

int main(){
  int i,j,n,k,c,a,b,G[101][101],d[101],f[101],yen,p,min,s,g;
  while(1){
    cin >> n >> k;
    if(n == 0 && k == 0) break;

    for(i=0;i<=n;i++){
      for(j=0;j<=n;j++){
	G[i][j] = INF;
      }
    }

    for(i=0;i<k;i++){
      cin >> c;
      if(c == 1){
	cin >> a >> b >> yen;
	if(G[a][b] > yen) G[a][b] = G[b][a] = yen;
      }

      else if(c == 0){
	for(j=0;j<=n;j++){
	  d[j] = INF;
	  f[j] = 0;
	}
	cin >> s >> g;

	d[s] = 0;
	while(1){
	  min = INF;
	  for(j=0;j<=n;j++){
	    if(d[j]<min && f[j] == 0){
	      min = d[j];
	      p = j;
	    }
	  }
	  if(min == INF) break;
	  f[p] = 1;
	  for(j=0;j<=n;j++){
	    if(f[j] == 1) continue;
	    if(G[p][j] + d[p] < d[j]) d[j] = G[p][j] + d[p];
	  }
	}

	if(d[g] == INF) cout << "-1" << endl;
	else cout << d[g] << endl;
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
int main(){
    for(int n; cin >> n && n; ){
        int ans = INF;
        for(int z = 0; z * z * z <= n; z++)
          for(int y = 0; y * y + z * z * z <= n; y++){
              int x = n - (y * y + z * z * z);
              ans = min(ans, x + y + z);
          }
        cout << ans << endl;
    }
    return 0;
}

#include <iostream>

using namespace std;

int main(){
	int x;
	cin >> x;
	int pleasure = 0;
	int f = x / 500;
	x -= f * 500;
	pleasure += 1000 * f;

	f = x / 5;
	x -= f * 5;
	pleasure += 5 * f;

	cout << pleasure << endl;
}


#include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<map>
#include<math.h>

#define irep(i,n) for(int i = 0; i < (int)(n); i++)
#define irep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define irrep(i,n) for(int i = (int)(n-1); i > -1; i--)
#define irrep2(i,n,a) for(int i = (int)(n); i >= (int)(a); i--)

#define fi first
#define se second

using namespace std;

using ll = long long;
using v_int = vector<int>;
using v2_int = vector<v_int>;
using v_ll = vector<ll>;
using v2_ll = vector<v_ll>;
using v_string = vector<string>;
using v_bool = vector<bool>;
using v2_bool = vector<v_bool>;
using pii = pair<int, int>;
using mii = map<int, int>;

const double PI = 3.1415926535897932;
const int INF = (int)2e9;
const ll LINF = (ll)2e18;
const ll MOD = 1000000007;
const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

template<typename T, typename U> inline
ostream& operator<<(ostream &o, pair<T,U> &p) {
    return o << "{" << p.first << "," << p.second << "}";
}
template<typename T> inline
istream& operator>>(istream &is, vector<T> &vec) {
    for(auto &v : vec) { is >> v; }
    return is;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<T> &vec) {
    for(auto &v : vec) { os << v << ",";}
    return os;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<vector<T> > &mat) {
    for(auto &row : mat) { os << row << endl; }
    return os;
}

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

int main(void)
{
    int n;
    cin >> n;
    v_int a(n+1), b(n+1);

    irep2(i,1,n) {
        cin >> a[i];
    }

    for(int i = n; i >= 1; i--) {
        int cnt = 0;
        for(int j = i; j <= n; j += i) {
            if(b[j]) { cnt++; }
        }
        if(cnt % 2 != a[i]) {
            b[i] = 1;
        }
    }
    int m = 0;
    irep2(i,1,n) {
        if(b[i]) { m++; }
    }

    cout << m << endl;
    irep2(i,1,n) {
        if(b[i]) { cout << i << " "; }
    }

    return 0;
} /*atcoder*/

#include<cstdio>
const int N=1e5+5;
int n,i,a[N],ans;
int read(){
	char c=getchar();int k=0;for (;c<48||c>57;c=getchar());
	for (;c>47&&c<58;c=getchar()) k=(k<<3)+(k<<1)+c-48;return k;
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main(){
	for (n=read(),i=1;i<=n;i++) a[i]=read();
	if (n==1){puts("Second");return 0;}
	for (;;){
		int k0=0,k1=0,x;
		for (i=1;i<=n;i++)
			if (a[i]&1) k1++,x=i; else k0++;
		if (k0&1){ans^=1;break;}
		if (k1>1||a[x]==1) break;
		a[x]--;int k=a[1];ans^=1;
		for (i=2;i<=n&&k>2;i++) k=gcd(k,a[i]);
		for (i=1;i<=n;i++) a[i]/=k;
	}
	puts(ans?"First":"Second");
}


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

int main() {
  int N, X, T;
  cin >> N >> X >> T;

  cout << (((N-1)/X +1) * T ) << endl;
}

#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();
    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();
    }

}
int power(int a,int n){
    int res=1;
    for (int i = 0; i < n; i++) {
        res=(1LL*res*a)%mod;
    }
    return res%mod;
}
int add(int a){
    if(a<0) a+=mod;
    return a;
}
void solve(){
    int n,x,t;
    cin>>n>>x>>t;
    int myans=n/x;
    if(n%x) myans++;
    cout<<myans*t;

}

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 <stdio.h>

int main(){
	long long N, sum;
	scanf("%lld", &N);
	sum = N*(N+1)/2;

	long long Un3 = N/3;
	long long Sn3 = 3*(Un3*(Un3+1)/2);

	long long Un5 = N/5;
	long long Sn5 = 5*(Un5*(Un5+1)/2);

	long long Un15 = N/15;
	long long Sn15 = 15*(Un15*(Un15+1)/2);

	printf("%lld", sum-Sn3-Sn5+Sn15);

	return 0;
}


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

using namespace std;

class Dice
{
public:
    int label[6], tmp;

    void in()
    {
        rep(i, 6) scanf("%d", &label[i]);
    }

    void rollS()
    {
        tmp = label[0];
        label[0] = label[4];
        label[4] = label[5];
        label[5] = label[1];
        label[1] = tmp;
    }

    void rollN()
    {
        tmp = label[0];
        label[0] = label[1];
        label[1] = label[5];
        label[5] = label[4];
        label[4] = tmp;
    }

    void rollE()
    {
        tmp = label[0];
        label[0] = label[3];
        label[3] = label[5];
        label[5] = label[2];
        label[2] = tmp;
    }

    void rollW()
    {
        tmp = label[0];
        label[0] = label[2];
        label[2] = label[5];
        label[5] = label[3];
        label[3] = tmp;
    }

    void sideturn()
    {
        tmp = label[1];
        label[1] = label[2];
        label[2] = label[4];
        label[4] = label[3];
        label[3] = tmp;
    }
};

bool check2(Dice d1, Dice d2)
{
    bool c = true;
    FOR(i, 1, 5)
    {
        c = c && d1.label[i] == d2.label[i];
    }

    return c;
}

bool check1(Dice d1, Dice d2)
{
    bool b = false;

    if (d1.label[0] == d2.label[0] && d1.label[5] == d2.label[5])
    {
        rep(i, 4)
        {
            b = b || check2(d1, d2);
            d2.sideturn();
        }
    }
    return b;
}

bool isSame(Dice d1, Dice d2)
{
    bool a = false;

    a = a || check1(d1, d2);
    d2.rollN();
    a = a || check1(d1, d2);
    d2.rollE();
    a = a || check1(d1, d2);
    d2.rollE();
    a = a || check1(d1, d2);
    d2.rollE();
    a = a || check1(d1, d2);
    d2.rollN();
    a = a || check1(d1, d2);

    return a;
}

int main()
{
    int n;
    bool ans = false;
    scanf("%d", &n);
    Dice dice[n];
    rep(i, n) dice[i].in();

    rep(i, n)
    {
        FOR(j, i + 1, n)
        {
            ans = ans || isSame(dice[i], dice[j]);
        }
    }

    if (ans)
        printf("No\n");
    else
        printf("Yes\n");
}

#include<iostream>
using namespace std;

struct Directions
{
	int E[6] = { 2, 1, 5, 0, 4, 3 };
	int W[6] = { 3, 1, 0, 5, 4, 2 };
	int N[6] = { 4, 0, 2, 3, 5, 1 };
	int S[6] = { 1, 5, 2, 3, 0, 4 };
	int Rotate[6] = { 0, 2, 4, 1, 3, 5 };
};

class Dice
{
public:
	int num[6];
	void swap(int*);
	Directions dir_array;
	Dice();
	void roll(char);
	void show_num(int);
};

Dice::Dice()
{
	for (int i = 0; i < 6; i++)
		cin >> this->num[i];
}

void Dice::roll(char c)
{
	switch (c)
	{
	case 'E':
		this->swap(dir_array.E);
		break;
	case 'W':
		this->swap(dir_array.W);
		break;
	case 'N':
		this->swap(dir_array.N);
		break;
	case 'S':
		this->swap(dir_array.S);
		break;
	case 'R':
		this->swap(dir_array.Rotate);
		break;
	}
	return;
}

void Dice::swap(int *dir)
{
	int tmp[6];
	for (int i = 0; i < 6; i++)
		tmp[dir[i]] = num[i];
	for (int i = 0; i < 6; i++)
		num[i] = tmp[i];
	return;
}

void Dice::show_num(int i)
{
	cout << this->num[i] << endl;
	return;
}

int check(Dice dice1, Dice dice2)
{
	int tmpf, flag = 0;
	for (int i = 0; i < 4; i++) {
		tmpf = 1;
		for (int j = 0; j < 6; j++)
			if (dice1.num[j] != dice2.num[j])
			{
				tmpf = 0;
				break;
			}
		if (tmpf)
			flag = 1;
		dice2.roll('R');
	}
	return flag;
}
int dice_comp(Dice dice1, Dice dice2)
{
	int flag = 0;
	for (int i = 0; i < 4; i++)
	{
		flag += check(dice1, dice2);
		dice2.roll('E');
	}
	dice2.roll('N');
	flag += check(dice1, dice2);
	dice2.roll('N');
	dice2.roll('N');
	flag += check(dice1, dice2);
	return flag;
}

int main()
{
	int flag = 0, n;
	cin >> n;
	Dice dice1;
	for (int i = 1; i < n; i++)
	{
		Dice dice2;
		flag += dice_comp(dice1, dice2);
	}
	if (!flag)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}


#include <bits/stdc++.h>

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

//#define int long long
//signed main(){
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int h,w;
    cin >> h >> w;
    vector<int> a(h*w);
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            if(i%2) cin >> a[i*w+w-1-j];
            else    cin >> a[i*w+j];
        }
    }
    vector<string> ans;
    for(int i = 0; i < h*w-1; i++) {
        if(a[i]%2){
            a[i]--;
            a[i+1]++;
            int r=i/w;
            int c=i%w;
            int nr=(i+1)/w;
            int nc=(i+1)%w;
            if(r%2) c=w-1-c;
            if(nr%2) nc=w-1-nc;
            string tmp=to_string(r+1)+" "+to_string(c+1)+" "+to_string(nr+1)+" "+to_string(nc+1);
            ans.emplace_back(tmp);
        }
    }
    cout << ans.size() << "\n";
    for(int i = 0; i < ans.size(); i++) cout << ans[i] << "\n";

    return 0;
}

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

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

int H, W;
bool fid[510][510];
bool mem[510][510];
int cnt;

signed main()
{
    cin >> H >> W;

    rep(i, H)rep(j, W)
    {
        int a;
        cin >> a;
        fid[i][j] = a & 1;
        mem[i][j] = a & 1;
    }

    rep(i, H)rep(j, W - 1)
    {
        if(mem[i][j])
        {
            cnt++;
            mem[i][j + 1] ^= true;
        }
    }

    rep(i, H - 1)
    {
        if(mem[i][W - 1])
        {
            cnt++;
            mem[i + 1][W - 1] ^= true;
        }
    }

    cout << cnt << endl;

    rep(i, H)rep(j, W - 1)
    {
        if(fid[i][j])
        {
            cout << i + 1 << " " << j + 1 << " " << i + 1 << " " << j + 2 << endl;
            fid[i][j + 1] ^= true;
        }
    }

    rep(i, H - 1)
    {
        if(fid[i][W - 1])
        {
            cout << i + 1 << " " << W << " " << i + 2 << " " << W << endl;
            fid[i + 1][W - 1] ^= true;
        }
    }

    return 0;
}


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

bool barrier[2000][2000];
long long hist_barrier[2000][2001];
char S[2000][2001];

int main(){
    int H, W;
    cin >> H >> W;
    for(int i = 0; i < H; i++){
        cin >> S[i];
    }
    for(int h = 0; h < H - 1; h++){
        for(int w = 0; w < W - 1; w++){
            int cnt_black = 0;
            for(int i = 0; i < 4; i++){
                cnt_black += (S[h + (i / 2)][w + (i % 2)] == '#') ? 1 : 0;
            }
            barrier[h][w] = (cnt_black % 2 == 1);
        }
    }
    for(int h = 0; h < H; h++){
        hist_barrier[h][0] = h + 1;
    }
    for(int w = 0; w < W; w++){
        hist_barrier[0][w] = 1;
    }
    for(int h = 1; h < H; h++){
        for(int w = 1; w < W; w++){
            if(barrier[h-1][w-1]){
                hist_barrier[h][w] = 1;
            }else{
                hist_barrier[h][w] = hist_barrier[h-1][w] + 1;
            }
        }
    }

    long long ans = 0;
    for(int h = 0; h < H; h++){
        stack<pair<long long, int> > st;
        for(int w = 0; w < W + 1; w++){
            if(st.empty()){
                st.push(make_pair(h + 1, w));
            }else if(st.top().first > hist_barrier[h][w]){
                int last_pos;
                while(!st.empty() && st.top().first >= hist_barrier[h][w]){
                    ans = max(ans, (w - st.top().second) * st.top().first);
                    last_pos = st.top().second;
                    st.pop();
                }
                st.push(make_pair(hist_barrier[h][w], last_pos));
            }
            if(st.top().first < h + 1){
                st.push(make_pair(h + 1, w));
            }
        }
    }
    cout << ans << endl;

}

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

int main(){
  while(true){
    int n,m,p[1001];
    vector<int> a;
    scanf("%d %d",&n,&m);
    if(n==0&&m==0) return 0;
    p[0] = 0;
    for(int i=0;i<n;i++) scanf("%d",p+i+1);
    n++;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        a.push_back(p[i]+p[j]);
      }
    }
    sort(a.begin(),a.end());
    a.erase(unique(a.begin(),a.end()),a.end());
    int ans = 0;
    for(int i=0;i<a.size();i++){
      int x = m - a[i];
      int j = upper_bound(a.begin(),a.end(),x) - a.begin();
      if(j!=0) ans = max(ans,a[i]+a[j-1]);
    }
    printf("%d\n",ans);
  }
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

struct topological_sort{
    vector<vector<int>> G;
    vector<int> indegree;
    vector<int> degree;
    int V;

    topological_sort(int V) : V(V) {
        G.resize(V);
        indegree.resize(V);
        degree.resize(V);
    }
    void add_edge(int a, int b) {
        G[a].push_back(b);
        indegree[b]++;
    }
    vector<int> sort() {
        queue<int> q;
        vector<int> sorted;
        int deg = 1;
        for (int i = 0; i < V; i++) {
            if (indegree[i] == 0) {
                q.push(i);
                degree[i] = deg;
            }
        }
        while (!q.empty()) {
            queue<int> q_tmp;
            deg++;
            while (!q.empty()) {
                int v = q.front(); q.pop();
                for (int i = 0; i < G[v].size(); i++) {
                    int u = G[v][i];
                    indegree[u]--;
                    if (indegree[u] == 0) {
                        q_tmp.push(u);
                        degree[u] = deg;
                    }
                }
                sorted.push_back(v);
            }
            swap(q,q_tmp);
        }
        return sorted;
    }
};

int main() {
    int n; cin >> n;
    topological_sort ts(n*(n-1)/2);
    vvint to(n,vint(n));
    int num = 0;
    rep(i,n)rep(j,i) to[j][i] = to[i][j] = num++;
    vvint data(n,vint(n-1));
    rep(i,n)rep(j,n-1) {
        int x; cin >> x;
        x--;
        data[i][j] = x;
    }
    rep(i,n)rep(j,n-2) {
        ts.add_edge(to[i][data[i][j]],to[i][data[i][j+1]]);
    }
    vint res = ts.sort();
    if (res.size() != n*(n-1)/2) {cout << -1 << endl; return 0;}
    cout << ts.degree[res.back()] << endl;
}

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

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

using namespace std;

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

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

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

/* Macros reg. ends here */

const ll INF = 1LL << 50;

int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n;
  cin >> n;
  vector<queue<ll>> que(n);

  rep(i, n) {
    rep(_, n - 1) {
      ll a;
      cin >> a;
      a--;
      que[i].push(a);
    }
  }

  bool upd = true;
  ll tsukaday = 0;
  while (upd) {
    upd = false;
    vll done(n);
    rep(i, n) {
      if (done[i] || que[i].empty()) continue;
      ll toi = que[i].front();
      if (done[toi] == 0 && que[toi].front() == i) {
        que[i].pop();
        que[toi].pop();
        done[i] = 1;
        done[toi] = 1;
        upd = true;
      }
    }
    if(upd) tsukaday++;
  }

  bool ok = true;
  rep(i, n) if (!que[i].empty()) {
    ok = false;
    break;
  }

  ll ans = ok ? tsukaday : -1;
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int query(int i){
  cout << i <<endl;
  string s;
  cin >> s;
  if(s[0] == 'V')exit(0);
  return (s[0] == 'M');
}
int main() {
  int n;
  cin >> n;

  int l=0,r = n-1;
  int eve = query(l);
  query(r);
  while(true){
    int mid = (l+r)/2;
    int a = query(mid);
    if(mid%2){
      if(a == eve) r = mid;
      else l = mid;
    }else{
      if(a == eve) l = mid;
      else r = mid;
    }
  }
  return 0;
}

#include <iostream>
using namespace std;

void ask(int left, int right, bool male) {
	int mid = (left + right) / 2;
	cout << mid << endl;
	string s;
	cin >> s;
	if (s == "Vacant") return;
	if (((mid - left) % 2 == 0) == (male == (s == "Male"))) ask(left, mid - 1, male);
	else ask(mid + 1, right, s == "Male");
}

int main() {
	int N;
	cin >> N;
	cout << 0 << endl;
	string s;
	cin >> s;
	if (s == "Vacant") return 0;
	ask(1, N - 1, s == "Male");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
/*

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

  ll A, B; cin>>A >> B;
  ll ans = A * B;
  cout <<ans <<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
int main()
{
     ///"In the name of Allah,most gracious and most merciful"///

        ll i,j,x,y,z,count=0,sum=0,n;
        cin>>n;
        x=n+(n*n)+(n*n*n);
        cout<<x<<endl;

    return 0;
}


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

int main (void){
    int a, b;
    int i, j, k;
    cin >> a >> b;
    i = a + b;
    j = a - b;
    k = a * b;

   if(i < j){
       if(j < k){
           cout << k << endl;
       }else{
           cout << j << endl;
       }
   }else if(j < i){
       if(i < k){
           cout << k << endl;
       }else{
           cout << i << endl;
       }
   }else if(k < i){
       if(i < j){
           cout << j << endl;
       }else{
           cout << i << endl;
       }
   }else{
       cout << 0 << endl;
   }

    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;

unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

class UnionFind {
public:
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

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

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

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll,ll>;
const ll mod = 1e9+7;
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define REPS(i,n) for(ll (i)=1;(i)<=(n);(i)++)
#define RREP(i,n) for(ll (i)=(n-1);(i)>=0;(i)--)
#define RREPS(i,n) for(ll (i)=(n);(i)>0;(i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());

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

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

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(12);
    ll N,M;
    cin >> N >> M;
    ll gcm = 0;
    if(N <= 10){
        for(ll i = N;;i++){
            if(M < i)break;
            if(M % i == 0){
                chmax(gcm,M/i);
                break;
            }
        }
    }else{
        for(ll i = 1;;i++){
            ll test = M - i*(N-1);
            if(test <= 0)break;
            if(test < i)break;
            if(test % i == 0){
                chmax(gcm,i);
            }
        }
    }
    cout << gcm << "\n";
}

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE>
void print_vec(const vector<TYPE>& v){
	for(int i=0; i<v.size(); i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
  cout << endl; cout << "       ";
  for(int i=0; i<v[0].size(); i++) cout << i << "   ";
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

using P = pair<int ,int>;
using LP = pair<ll, int>;
int main(){
	int N;
	ll M;
	cin >> N >> M;

	vector<ll> divs;
	for(ll i=1; i*i<=M;  i++){
		if(M%i == 0){
			divs.push_back(i);
			if(i*i != M)divs.push_back(M/i);
		}
	}
	sort(divs.begin(), divs.end());
	ll ans = 0;
	for(int i=0; i<(int)divs.size(); i++){
		if(M/divs[i] >= N) ans = max(ans, divs[i]);
	}
	// cout << " divs: "; print_vec(divs);
	cout << ans << endl;

	// vector<LP> prime_divisors;
	// ll m = M;
	// //エラトステネスの篩
	// for(ll p=2; p*p<m; p++){
	// 	if(m % p != 0) continue;
	// 	int cnt = 0;
	// 	while(m%p == 0){
	// 		m /= p;
	// 		cnt++;
	// 	}
	// 	prime_divisors.emplace_back(p, cnt);
	// }
	// if(m != 1) prime_divisors.emplace_back(m, 1);

	// vector<ll> divs;// Mの因数となるモノを格納
	// ll ans = 0;

}


#include <bits/stdc++.h>
#define rep(i , n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
int main (){
    ll N, L[2]{};
    cin >> N;
    set <int> S;
    while (cin >> N) {
        if (!S.insert(N).second) {
            S.erase(N);
            if (N > L[0]){
                L[1] = L[0], L[0] = N;
            }
            else if(N > L[1]){
                L[1] = N;
            }
        }
    }
    cout << L[0] * L[1] << endl;
}

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<P>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T>inline bool chmax(T &a, T b){if (a < b){a = b;return 1;}return 0;}
template <typename T>inline bool chmin(T &a, T b){if (a > b){a = b;return 1;}return 0;}

int main(){
    ll n;cin>>n;
    map<ll,ll> a;

    rep(i,n){
        ll p;cin>>p;
        if(a.find(p)==a.end()) a[p]=1;
        else a[p]++;
    }

    ll ans=1;
    priority_queue<ll> que;
    que.push(0);
    que.push(0);
    for(auto itr=a.begin();itr!=a.end();itr++){
        rep(i,itr->second/2) que.push(itr->first);
    }

    ans*=que.top();
    que.pop();
    ans*=que.top();

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


#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define MAX(x, y, z) max(x, max(y, z))
#define MIN(x, y, z) min(x, min(y, z))
#define ANS cout << ans << "\n"
#define RETURN(x)    \
  cout << x << "\n"; \
  return 0
clock_t CLOCK;
#define START_TIMER CLOCK = clock()
#define SHOW_TIMER cerr << "time: " << (ld)(clock() - CLOCK) / 1000000 << "\n"
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(i, v.size()) cerr << " " << v[i]; \
  cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

signed main() {
  init();
  string a, b, c;
  cin >> a >> b >> c;

  if (a.back() == b[0] && b.back() == c[0]) {
    COUT("YES");
  } else {
    COUT("NO");
  }
  return 0;
}

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

int main(){
    char str1[25];
    char str2[25];
    char str3[25];

    scanf("%s %s %s", str1, str2, str3);

    if (str1[strlen(str1)-1] == str2[0]){
        if (str2[strlen(str2)-1] == str3[0]){
            printf("YES\n");
        }
        else
        {
        printf("NO\n");
        }
    }
    else
    {
        printf("NO\n");
    }

    return 0;
}