#include <bits/stdc++.h>
using namespace std;
const long long INF = 123456789;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;
typedef pair<ll, ll> P;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

class UF
{
  public:
    vi rank, p;
    UF() {}
    UF(ll size)
    {
        rank.resize(size, 0);
        p.resize(size, 0);
        REP(i, size)
        {
            makeSet(i);
        }
    }

    void makeSet(ll x)
    {
        p[x] = x;
        rank[x] = 0;
    }

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

    void unite(ll x, ll y)
    {
        link(findSet(x), findSet(y));
    }

    void link(ll x, ll y)
    {
        if (rank[x] > rank[y])
        {
            p[y] = x;
        }
        else
        {
            p[x] = y;
            if (rank[x] == rank[y])
            {
                rank[y]++;
            }
        }
    }

    ll findSet(ll x)
    {
        if (x != p[x])
        {
            p[x] = findSet(p[x]);
        }
        return p[x];
    }
};

class Edge
{
  public:
    ll source, target, cost;
    Edge(ll source = 0, ll target = 0, ll cost = 0) : source(source), target(target), cost(cost) {}
    bool operator<(const Edge &e) const
    {
        return cost < e.cost;
    }
};

ll kruskal(ll n, vector<Edge> edges)
{
    ll totalCost = 0;
    sort(ALL(edges));
    UF dset = UF(n + 1);
    REP(i, n)
    {
        dset.makeSet(i);
    }
    ll source, target;
    REP(i, edges.size())
    {
        Edge e = edges[i];
        if (!dset.same(e.source, e.target))
        {
            totalCost += e.cost;
            dset.unite(e.source, e.target);
        }
    }
    return totalCost;
}

int main()
{
    ll cost, source, target;
    cin >> n >> m;
    vector<Edge> edges;
    REP(i, m)
    {
        cin >> source >> target >> cost;
        edges.push_back(Edge(source, target, cost));
    }

    cout << kruskal(n, edges) << endl;
}

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}

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

template<typename T>
struct edge {
    int src, to;
    T cost;
    // edge(int src, int to, T cost): src(src), to(to), cost(cost) {}
    // // G[i].push_back({src, to, cost}) requires no constructor
};

template<typename T>
using Graph = vector<vector<edge<T>>>;

template<typename T>
struct SCC {
    int sz, cnt, num;
    vi post, comp;
    vector<pair<int, int>> vp;
    vector<bool> sel;
    Graph<T> revg;

    SCC(int sz): sz(sz) {
        cnt = 0;
        num = 0;
        post.resize(sz, -1);
        comp.resize(sz, -1);
        sel.resize(sz, false);
        revg.resize(sz);
    }

    void build(const Graph<T> &g) {
        for(int i=0;i<sz;++i) {
            if(sel[i]) continue;
            sel[i] = true;
            dfs1(g, i);
        }

        rev(g, revg);

        for(int i=0;i<sz;++i) {
            vp.emplace_back(make_pair(post[i], i));
        }
        sort(vp.begin(), vp.end());
        reverse(vp.begin(), vp.end());
        sel.clear();
        sel.resize(sz, false);
        for(int i=0;i<sz;++i) {
            if(sel[vp[i].second]) continue;
            sel[vp[i].second] = true;
            comp[vp[i].second] = num;
            dfs2(revg, vp[i].second);
            num++;
        }
    }

    vi get_comp() {return comp;}

    Graph<T> build_graph(const Graph<T> &g) {
        build(g);
        vector<set<int>> s(sz);
        Graph<T> res(sz);
        for(int i=0;i<sz;++i) {
            for(int j=0;j<(int)(g[i].size());++j) {
                s[comp[i]].insert(comp[g[i][j].to]);
            }
        }
        for(int i=0;i<sz;++i) {
            for(auto j: s[i]) {
                if(i != j) res[i].push_back(edge<int>({i, j, 1}));
            }
        }
        return res;
    }

    void dfs1(const Graph<T> &g, int now) {
        for(int i=0;i<(int)(g[now].size());++i) {
            int nxt = g[now][i].to;
            if(sel[nxt]) continue;
            sel[nxt] = true;
            dfs1(g, nxt);
        }
        post[now] = cnt;
        cnt++;
    }

    void rev(const Graph<T> &g, Graph<T> &revg) {
        for(int i=0;i<sz;++i) {
            for(int j=0;j<(int)(g[i].size());++j) {
                revg[g[i][j].to].push_back({
                        g[i][j].to, g[i][j].src, g[i][j].cost});
            }
        }
    }

    void dfs2(const Graph<T> &revg, int now) {
        for(int i=0;i<(int)(revg[now].size());++i) {
            int nxt = revg[now][i].to;
            if(sel[nxt]) continue;
            sel[nxt] = true;
            comp[nxt] = num;
            dfs2(revg, nxt);
        }
    }
};

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

  int n, m; cin >> n >> m;
  Graph<int> g(n);
  for(int i=0;i<m;++i) {
    int s, t; cin >> s >> t;
    g[s].push_back({s, t ,1});
  }

  SCC<int> scc(n);
  scc.build(g);
  auto comp = scc.get_comp();

  int q; cin >> q;
  for(int i=0;i<q;++i) {
    int u, v; cin >> u >> v;
    cout << (comp[u] == comp[v]) << endl;
  }

}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    char a,b,c;
    cin>>a>>b>>c;
    int ans = 0;
    if(a=='R'&&b=='R'&&c=='R')
        ans = 3;
    else if((a=='R' && b=='R')|| (b=='R'&&c=='R'))
        ans= 2;
    else if(a=='R'||b=='R'||c=='R')
        ans =1;
    cout<<ans;
    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

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

int main() {
	int n = 10;
	vector<int> a(n), b(n);

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

	sort(a.begin(), a.end(), greater<int>());
	sort(b.begin(), b.end(), greater<int>());

	cout << a[0] + a[1] + a[2] << " " << b[0] + b[1] + b[2] << endl;

	return 0;
}


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

#include <bits/stdc++.h>

using namespace std;

const int mxN = 310;
double dp[mxN][mxN][mxN];
bool vis[mxN][mxN][mxN];
int N;

class TaskJ {
public:
	void solve(std::istream& in, std::ostream& out) {
		in >> N;
		int a[4] = {0};
		for (int i = 0; i < N; i++) {
			int x; in >> x;
			a[x]++;
		}
		memset(vis, false, sizeof(vis));
		double ans = rec(a[1], a[2], a[3]);
		out << fixed << setprecision(12) << ans;
	}

	double rec(int x, int y, int z) {
		double &ret = dp[x][y][z];
		if (x < 0 || y < 0 || z < 0) return 0;
		if (x == 0 && y == 0 && z == 0) return 0;
		if (vis[x][y][z]) return ret;
		vis[x][y][z] = true;
		int rem = x + y + z;
		double val = N + x * rec(x - 1, y, z) + y * rec(x + 1, y - 1, z) + z * rec(x, y + 1, z - 1);
		return ret = val / rem;
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	TaskJ solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

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

const int mxN = 301;
double dp[mxN][mxN][mxN] = {};
double p[mxN][mxN][mxN] = {};
ll n;

int main()
{
FIO

    cin >> n;
    int c[3] = {}, num;
    For(n) cin >> num, c[--num]++;
    ll one = c[0], two = c[1], three = c[2];
    p[one][two][three] = 1;
    for(int k = n; k >= 0; k--)
    {
        for(int j = n; j >= 0; j--)
        {
            for(int i = n; i >= 0; i--)
            {
                if(i+j+k == 0 || i+j+k > n)
                    continue;
                dp[i][j][k] += p[i][j][k] * n / (i+j+k);

                if(i > 0)
                {
                    p[i-1][j][k] += p[i][j][k] * i / (i+j+k);
                    dp[i-1][j][k] += dp[i][j][k] * i / (i+j+k);
                }
                if(j > 0)
                {
                    p[i+1][j-1][k] += p[i][j][k] * j / (i+j+k);
                    dp[i+1][j-1][k] += dp[i][j][k] * j / (i+j+k);
                }
                if(k > 0)
                {
                    p[i][j+1][k-1] += p[i][j][k] * k / (i+j+k);
                    dp[i][j+1][k-1] += dp[i][j][k] * k / (i+j+k);
                }
            }
        }
    }

    cout << fixed << setprecision(10) << dp[0][0][0] << endl;

}


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

int main() {
  int n,m;
  cin >> n >> m;
  if(n == m){
    cout << 2*m;
  }
  else{
    cout << 2*max(m,n)-1;
  }
}

#include <iostream>
using namespace std;

int main()
{
  int a, b, ans = 0;
  cin >> a >> b;
  ans += max(a , b);
  if(a == b) ans += max(a, b);
  else ans += ans - 1;

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


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair

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

const int INF = INT_MAX;

int main(void) {
    int v, e, s, t, d;
    scanf("%d %d", &v, &e);

    int g[v][v];
    rep(i, v) rep(j, v) g[i][j] = INF;
    rep(i, v) g[i][i] = 0;

    rep(i, e) {
        scanf("%d %d %d", &s, &t, &d);
        g[s][t] = d;
    }

    rep(k, v) {
        rep(i, v) {
            if (g[i][k] == INF) continue;
            rep(j, v) {
                if (g[k][j] == INF) continue;
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
            }
        }
    }

    int flag = 1;
    rep(i, v) {
        if (g[i][i] < 0) {
            flag = 0;
            break;
        }
    }

    if (flag) {
        rep(i, v) {
            rep(j, v) {
                if (j) printf(" ");
                if (g[i][j] == INF) printf("INF");
                else printf("%d", g[i][j]);
            }
            printf("\n");
        }
    }
    else printf("NEGATIVE CYCLE\n");

    return 0;
}

/*
    Problem 33
    https://atcoder.jp/contests/abc088/tasks/abc088_d
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
/* function */
/* main */
int main(){
    int h, w;
    cin >> h >> w;
    vector<vector<bool>> grid(h, vector<bool>(w));
    int ans = h * w;
    for (int i = 0; i < h*w; i++) {
        char c; cin >> c;
        grid[i/w][i%w] = c == '.';
        ans -= (c == '#');  // 黒マスの分あらかじめ引いとく
    }

    vector<vector<int>> dist(h, vector<int>(w, 0));
    queue<pii> q;
    q.emplace(0, 0);
    dist[0][0] = 1;
    while(!q.empty()) {
        pii p = q.front(); q.pop();
        int x = p.first, y = p.second;
        for (int i = 0; i < dx.size(); i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || h <= nx || ny < 0 || w <= ny) continue;
            if (!grid[nx][ny] || dist[nx][ny] != 0) continue;
            dist[nx][ny] = dist[x][y] + 1;
            q.emplace(nx, ny);
        }
    }
    cout << ((dist[h-1][w-1] == 0) ? -1 : ans - dist[h-1][w-1]) << '\n';
}


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

#define LL long long
#define DD long double

#define M 1000000007
#define INF 1e18

void setIO(string s = "") {
  cin.sync_with_stdio(0); cin.tie(0);
  if (s.size()) s += ".in", freopen(s.c_str(), "r", stdin);
}

int tt = 1;
void solve() {
  set<int> nums;
  int a;
  while (cin >> a) nums.insert(a);
  cout << ((nums.size() == 2) ? "Yes" : "No") << endl;
}

int main() {
  setIO();
  while (tt--) {
    solve();
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define endl "\n"
typedef long long ll;

const int BIG_NUM = 1e9;

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

    int a, b, c;
    cin >> a >> b >> c;
    if(a == b && b != c) {
        cout << "Yes";
    } else if(b == c && c != a) {
        cout << "Yes";
    } else if(c == a && a != b) {
        cout << "Yes";
    } else {
        cout << "No";
    }
    return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define rep2(i,n) for(int i=0;i<=n;i++)
#define repr(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define P pair<long long,long long>
#define uni(e) e.erase(unique(e.begin(),e.end()),e.end())
#define double long double
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int INF=1e10;
int MOD=1e9+7;
signed main(){
  string s;
  cin>>s;
  cout<<s.substr(0,s.size()-8)<<endl;
}

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

int main() {
    string s;
    cin >> s;
    if (s.length()%2 != 0) {
        cout << "No" << endl;
        return 0;
    }
    else {
        for (int i = 0; i < s.length(); i++) {
            if (i%2==0 && s[i]!='h' || i%2==1 && s[i]!='i') {
                cout << "No" << endl;
                return 0;
            }
        }
    }

    cout << "Yes" << endl;
}


#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(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
    int a, b, c, d, e, k;
    vector<int>at(5);
    rep(i, 5)cin >> at[i];
    cin >> k;
    bool ans = true;
    rep(i, 4)for (int j = i + 1; j < 5; j++)if (abs(at[i] - at[j]) > k)ans = false;
    if (ans) {
        cout << "Yay!" << endl;
    }
    else {
        cout << ":(" << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
	int x[7];
	bool q=true;
	for(int i=0;i<6;i++){
		cin>>x[i];
	}
	if(x[4]-x[0]>x[5])q=false;
	if(q==true) cout<<"Yay!";
	else cout<<":(";
	cout<<"\n";
	return 0;
}


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

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

int main() {

    string N_str;
    cin >> N_str;
    int n = N_str.length();

    vector<int> N(n, 0);
    rep(i,n) N[i] = N_str[n - 1 - i] - '0';

    vector<vector<int>> dp(n + 1, vector<int>(2, INF));
    dp[0][0] = 0;

    rep(i,n) rep(j,2) {
        N[i] += j;
        rep(A,10) {
            int p = i + 1, q = 0;
            int B = A - N[i];
            if (B < 0) {
                q = 1;
                B += 10;
            }
            dp[p][q] = min(dp[p][q], dp[i][j] + A + B);
        }
        N[i] -= j;
    }

    cout << min(dp[n][0], dp[n][1] + 1) << endl;

    return 0;
}

#include<iostream>
using namespace std;
int main(){
  string s;
  cin>>s;
  int r=0;
  for(int i=0;i<3;i++){
    if(s[i]=='R')
        r=1;
  }
  for(int i=0;i<2;i++){
    if(s[i]=='R'){
        if(s[i]==s[i+1])
            r++;
    }
  }
  cout<<r<<"\n";
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
    R(N,X);
    LL need=0;
    priority_queue<PLL>h0,h1,h2;
    REP(i,N){
        R(b[i],l[i],u[i]);
        need+=b[i]*l[i];
        h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
    }
    LL an=0;
    while(need>=h0.top().F){
        an+=X;
        used[h0.top().S]=1;
        need-=h0.top().F;
        h0.pop();
    }
    if(need<=0){
        W(an);
        return 0;
    }
    int add_id=h0.top().S;
    LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
    LL mi=1e18;
    REP(i,N){
        if(used[i]){
            LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);

            if(l[i]*b[i]>=need2){
                mi=min(mi,(need2+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
        else{
            if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
            if(l[i]*b[i]>=need){
                mi=min(mi,(need+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
    }
    an+=mi;
    W(an);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a),i##formax=(b);i<i##formax;i++)
#define FORR(i,a,b) for(int i=(a),i##formax=(b);i>=i##formax;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pcnt __builtin_popcount
#define sz(x) (int)(x).size()
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)
#define show(x) cout<<#x<<" = "<<x<<endl;
#define all(a) (a.begin()),(a.end())
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define perm(c) sort(all(c));for(bool c##p=1;c##p;c##p=next_permutation(all(c)))
#define bitComb(a,n,k) for(int a##x,a##y,a=(1<<k)-1;a<(1<<n);a##x=a&-a,a##y=a+a##x,a=(((a&~a##y)/a##x)>>1)|a##y)
#define uniq(v) sort(all(v));v.erase(unique(all(v)), v.end())
#define bit(n) (1LL<<(n))
#define randInt(l,r) (uniform_int_distribution<ll>(l,r)(rnd))
#define randDouble(l,r) (uniform_real_distribution<double>(l,r)(rnd))
#define dout(d) printf("%.12f\n",d)

typedef long long ll;
typedef __int128_t lll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T>using V=vector<T>;
template<class T>using VV=V<V<T>>;
template<class T,class Y>ostream& operator<<(ostream& o,const pair<T,Y>& p){return o<<"("<<p.fi<<", "<<p.se<<")";}
template<typename A,size_t N,typename T>void Fill(A (&array)[N],const T&v){fill((T*)array,(T*)(array+N),v);}
lll gcd(lll a,lll b,lll &x,lll &y){if(!b){x=1;y=0;return a;}lll d=gcd(b,a%b,y,x);y-=a/b*x;return d;}
ll gcd(ll a,ll b){lll x=0,y=0;return gcd(a,b,x,y);}
ll modInv(ll a,ll m){lll x,y;gcd(a,m,x,y);return (x%m+m)%m;}
ll modPow(lll a,lll n,ll m){if(!a)return a;lll p=1;for(;n>0;n>>=1,a=a*a%m)if(n&1)p=p*a%m;return(ll)p;}
bool isPrime(ll n){if(n<2||n%2==0)return n==2;lll t=n-1,d=t/(t&-t);for(lll a:{2,325,9375,28178,450775,9780504,1795265022})if(a%n){for(t=d,a=modPow(a,t,n);t!=n-1&&a!=1&&a!=n-1;a=a*a%n,t=t*2%n);if(a!=n-1&&t%2==0)return 0;}return 1;}
const int IINF = 1e9+6;
const ll LINF = 1e18;
const int MOD = 1e9+7;
const double PI = acos(-1);
const double EPS = 1e-10;
static random_device rd;
static mt19937 rnd(rd());

const int N = 1e5+100;
ll n, x, s;
ll B[N], L[N], U[N], su[N], idx[N];
ll ans = LINF;
V<pair<ll, int>> v;
ll check(ll m, int i){
  int j = idx[i];
  ll ss = (j<m ? su[m+1]-v[j].fi : su[m]);
  if(ss>=s) return x*m;
  if(ss+L[i]*B[i]>=s) return x*m + (s-ss+L[i]-1)/L[i];
  if(ss+v[j].fi>=s) return x*m + (s-ss+(U[i]-L[i])*B[i]+U[i]-1)/U[i];
  return LINF;
}
main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> x;
  FOR(i, 0, n){
    ll b, l, u;
    cin >> b >> l >> u;
    B[i] = b;
    L[i] = l;
    U[i] = u;
    s += b*l;
    v.pb(mp(u*(x-b)+l*b, i));
  }
  sort(all(v));
  reverse(all(v));
  FOR(i, 0, n){
    su[i+1] += su[i]+v[i].fi;
    idx[v[i].se] = i;
  }
  FOR(i, 0, n){
    int j = idx[i], l = -1, r = n-1, m;
    while(r-l>1){
      m = (r+l)/2;
      if(check(m, i)<LINF){
        r = m;
      }else{
        l = m;
      }
    }
    mins(ans, check(r, i));
  }
  cout << ans << endl;
}


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

static long Gcd(long m, long n)
{
	long temp;
	while (m % n != 0)
	{
		temp = n;
		n = m % n;
		m = temp;
	}
	return n;
}

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

map< int, int > prime_factor(int n) {
  map< int, int > ret;
  for(int64_t i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

signed main(){
    int A, B;cin>>A>>B;

    int gcd = Gcd(A,B);

/*    vec_int factors;

    for(int i=1;i*i<=gcd;i++){
        if(gcd%i==0){
            factors.push_back(i);
            factors.push_back(gcd/i);
        }
    }
    sort(factors.begin(), factors.end());
    */

   map<int, int> pfs = prime_factor(gcd);

   cout<<pfs.size()+1<<endl;

    return 0;
}

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

long long mod = 1000000007;

class UnionFind
{
public:
   vector<int> parent;
   UnionFind(int N)
   {
      for (int i = 0; i < N; i++)
      {
         parent.push_back(-1);
      }
   }
   int root(int A)
   {
      if (parent[A] < 0)
         return A;
      return parent[A] = root(parent[A]);
   }
   int size(int A)
   {
      return -parent[root(A)];
   }

   bool unite(int A, int B)
   {
      A = root(A);
      B = root(B);
      if (A == B)
         return false;
      if (size(A) < size(B))
         swap(A, B);
      parent[A] += parent[B];
      parent[B] = A;
      return true;
   }
};
vector<pair<long long, long long>> prime(long long N)
{
   vector<pair<long long, long long>> res;
   for (long long i = 2; i * i <= N; i++)
   {
      if (N % i != 0)
         continue;
      long long ex = 0;
      while (N % i == 0)
      {
         ex++;
         N = N / i;
      }
      res.push_back({i, ex});
   }
   if (N != 1)
      res.push_back({N, 1});
   return res;
}
int main(void)
{
   long long A, B;
   cin >> A >> B;
   vector<pair<long long, long long>> PA = prime(A);
   vector<pair<long long, long long>> PB = prime(B);
   map<long long, long long> result;
   for (int i = 0; i < PA.size(); i++)
   {
      result[PA[i].first]++;
   }
   for (int i = 0; i < PB.size(); i++)
   {
      result[PB[i].first]++;
   }
   long long count = 1;
   for (auto ans : result)
   {
      if (ans.second == 2)
         count++;
   }
   cout << count << endl;
   return 0;
}


#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

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

vector<vector<pair<int, ll>>> eda(100000 + 5); // pair には num, dist
vector<ll> dist(100000 + 5);

void makeCost(int n, int k)
{
  queue<pair<pair<int,ll>, int>> q; // pair には ((num, dist) from)
  dist[k] = 0;
  for (auto a : eda[k]) {
    q.push(make_pair(a, k));
  }

  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    int number = now.first.first;
    int from = now.second;
    ll distance = now.first.second;
    dist[number] = dist[from] + distance;
    for (auto tmp : eda[number]) {
      if (tmp.first == from) continue;
      q.push(make_pair(tmp, number));
    }
  }
}

int main()
{
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    eda[a].push_back(make_pair(b, c));
    eda[b].push_back(make_pair(a, c));
  }

  int q, k;
  cin >> q >> k;

  makeCost(n, k);

  rep (i, q) {
    int x, y;
    cin >> x >> y;
    cout << dist[x] + dist[y] << endl;
  }
}

#include<iostream>
using namespace std;

int n, m, k;
int t[100][100], c[100][100];
int INF = 1<<21;

void solve(){
    for(int k = 0; k < m; k++){
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
                c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
            }
        }
    }
}

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

        if(n + m == 0)  break;

        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                c[i][j] = t[i][j] = INF;
            }
        }

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

        solve();

        cin >> k;
        int p, q, r;
        for(int i = 0; i < k; i++){
            cin >> p >> q >> r;
            p--; q--;
            if(r == 0)          cout << c[p][q] << endl;
            else if(r == 1)     cout << t[p][q] << endl;
        }

    }
    return 0;
}


#include <bits/stdc++.h>
#include <set>
using namespace std;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
const long long INF = 1<<29;

const int MOD = 998244353;
long long ruizyou(long long m,long long n){
  if(m == 0)return 0;
  if(m == 1)return 1;
  long long ans = 1;
  long long tmp = m;
  for(int i=0;i<=30;i++){
    if(n & (1<<i)){
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x){
  if(x == 0)return 1;
  return x * kaizyou(x-1) % MOD;
}
long long comb(long long x,long long y){
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x-y) * kaizyou(y) % MOD;
  return bunsi * ruizyou(bunbo,MOD-2) % MOD;
}
struct unionfind{
  vector<int> par;
  vector<vector<int>> children;
  vector<int> hukasa;
  vector<int> kazu;
  unionfind(int n){
    par = vector<int>(n);
    hukasa = vector<int>(n,0);
    kazu = vector<int>(n,1);
    for(int i=0;i<n;i++){
      par.at(i) = i;
    }
  }
  int root(int x){
    if(par.at(x) == x)return x;
    return root(par.at(x));
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry)return;
    if(hukasa.at(rx) >= hukasa.at(ry)){
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1,hukasa.at(rx));
      kazu.at(rx) += kazu.at(ry);
    }
    else{
      par.at(rx) = ry;
      kazu.at(ry) += kazu.at(rx);
    }
  }
  void newkazu(){
    int n = kazu.size();
    for(int i=0;i<n;i++){
      kazu.at(i) = kazu.at(root(i));
    }
  }
  bool same(int x,int y){
    return root(x) == root(y);
  }
};
int ctoi(char a){
  return a - '0';
}
long long gcd(long long a,long long b){
  long long c = max(a,b);
  long long d = min(a,b);
  if(d % c == 0)return c;
  return gcd(c,d%c);
}
long long lcm(long long a,long long b){
  return a * b / gcd(a,b);
}
int main(){
  int h,w;
  cin >> h >> w;
  vector<vector<char>> s(h,vector<char>(w));
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++)cin >> s.at(i).at(j);
  }
  vector<vector<int>> table(h,vector<int>(w,-1));
  queue<pii> q;
  q.push(pii(0,0));
  table.at(0).at(0) = 1;
  bool flag = false;
  int count;
  while(!q.empty()){
    pii tmppair = q.front();
    q.pop();
    int x,y;
    tie(x,y) = tmppair;
    set<pii> se;
    if(x-1 >= 0)se.insert(pii(x-1,y));
    if(x+1 < h)se.insert(pii(x+1,y));
    if(y-1 >= 0)se.insert(pii(x,y-1));
    if(y+1 < w)se.insert(pii(x,y+1));
    for(auto t:se){
      int tx,ty;
      tie(tx,ty) = t;
      if(s.at(tx).at(ty) == '.' && table.at(tx).at(ty) == -1){
        table.at(tx).at(ty) = table.at(x).at(y) + 1;
        q.push(t);
      }
      if(t == pii(h-1,w-1)){
        count = table.at(tx).at(ty);
        flag = true;
        break;
      }
    }
    if(flag) break;
  }
  if(!flag) cout << -1 << endl;
  else{
    int count2 = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(s.at(i).at(j) == '#')count2++;
      }
    }
    cout << h * w - count - count2 << endl;
  }
}

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int h,w;
int arr[105][105];
bool visited[105][105];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int main(){
	int white=0;
	cin>>h>>w;
	for(int i=1; i<=h; i++){
		string s;
		cin>>s;
		for(int j=0; s[j]; j++){
			if(s[j]=='.'){
				arr[i][j+1] = 1;
				white++;
			}
		}
	}
	queue<pair<pi,int> >q;
	q.push(make_pair(pi(1,1),1));
	visited[1][1] = true;
	while(!q.empty()){
		pair<pi,int> curr = q.front();
		int y = curr.first.first;
		int x = curr.first.second;
		int d = curr.second;
		q.pop();
		if(y==h && x==w){
			cout<<white-d;
			return 0;
		}
		for(int i=0; i<4; i++){
			int yy = y+dy[i];
			int xx = x+dx[i];
			if(yy<1 || xx<1 || yy>h || xx>w) continue;
			if(!visited[yy][xx] && arr[yy][xx]){
				visited[yy][xx] = true;
				q.push(make_pair(pi(yy,xx),d+1));
			}
		}
	}
	cout<<-1;
}


#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#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 SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define YN YES else NO
#define yn Yes else No
#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(z, v.size()) cerr << " " << v[z]; \
  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>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

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

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

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

void rec(ll v, const vvll& G, vector<bool>& seen, vll& order) {
  seen[v] = true;
  for (auto next : G[v]) {
    if (seen[next]) continue;
    rec(next, G, seen, order);
  }
  order.push_back(v);
}

signed main() {
  init();
  ll N, M;
  cin >> N >> M;

  vvll G(N);
  vvll G2(N);
  rep(i, N + M - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G2[b].pb(a);
  }

  vector<bool> seen(N, 0);
  vll order;
  rep(i, N) {
    if (seen[i]) continue;
    rec(i, G, seen, order);
  }
  REVERSE(order);

  vll order2(N);
  rep(i, N) order2[order[i]] = i;

  rep(i, N) {
    ll max_index = -1;
    ll ma = -1;
    for (auto gg : G2[i])
      if (ma < order2[gg]) {
        max_index = gg;
        ma = order2[gg];
      }

    COUT(max_index + 1);
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false)
int a[200000], vs[200000],vs1[100006],p[300000];
vector<int>v[200000];
queue<int>q;
int f(int n)
{
    int i, k=0;
    vs[n]=1; q.push(n);
    while(q.size()){
            n=q.front(); q.pop();
    for(i=0; i<v[n].size(); i++){
        if(vs1[v[n][i]]==1){
        p[v[n][i]]=n;
        q.push(v[n][i]);
    }
    else vs1[v[n][i]]--;
    }
    }
    return 1;
}
int main()
{
    IO;
    int i, j, m, k, n, t, l=0, mx,ll,mxx,mx1;
    cin>>n>>m;
    for(i=1; i<=n+m-1; i++){
        cin>>j>>k;
        v[j].push_back(k);
        vs1[k]++;
    }
    for(i=1; i<=n; i++)
        if(vs1[i]==0)k=i;
    f(k);
    for(i=1; i<=n; i++) cout<<p[i]<<endl;
    return 0;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define sz(x) (int)(x).size()
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;

struct int_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct pair_hash {
    size_t operator()(pii x) const {
        return int_hash{}(x.first) ^ (int_hash{}(x.second) << 16);
    }
};

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<cd> vcd;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef vector<vector<pii>> vvpii;
typedef vector<vector<pll>> vvpll;
typedef unordered_map<int, int, int_hash> umpii;
typedef unordered_map<ll, ll, int_hash> umpll;
typedef unordered_set<int, int_hash> usi;
typedef unordered_set<ll, int_hash> usll;
typedef unordered_set<pii, pair_hash> uspii;
typedef queue<int> qi;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

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

const int MOD = 1000000007;
const ll INF = numeric_limits<ll>::max();
const int inf = numeric_limits<int>::max();
const int MX = 100001; //check the limits, dummy

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

    int n; cin >> n;
    vpii v(n, {0, 0});
    F0R(i, n) {
        cin >> v[i].f;
        cin >> v[i].s;
    }

    sort(v.begin(), v.end(), [](const pii &p1, const pii &p2){ return p1.f + p1.s < p2.f + p2.s; });
    vvi dp(n + 1, vi(n + 1, inf));
    dp[0][0] = 0;
    int ans = 0;
    F0R(i, n) {
        F0R(j, i + 1) {
            dp[i + 1][j + 1] = min(dp[i][j + 1], v[i].f >= dp[i][j] ? dp[i][j] + v[i].s : inf);
            ans = max(ans, dp[i + 1][j + 1] == inf ? 0 : j + 1);
        }
    }

    cout << ans << endl;
}

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

using namespace std;

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

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

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

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

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

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

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

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


#include<iostream>
#include<vector>
#include<algorithm>
int n,x;
std::vector<int> vec;
long long int num(){
	long long int lli=0;
	for(auto v:vec){
		lli+=static_cast<long long int>(v);
	}
	return lli;
}
int main(){
	std::cin>>n;
	for(int i=0;i<n;++i){
		std::cin>>x;
		vec.push_back(x);
	}
	sort(vec.begin(),vec.end());
	std::cout<<*vec.begin()<<" "<<*(vec.end()-1)<<" "<<num()<<std::endl;
	return 0;
}

/*      ░░█ ▄▀█ █   █▀ █░█ █▀█ █▀▀ █▀▀   █▀█ ▄▀█ █▀▄▀█
        █▄█ █▀█ █   ▄█ █▀█ █▀▄ ██▄ ██▄   █▀▄ █▀█ █░▀░█     */

// Author : Udit "luctivud" Gupta @ (https://www.linkedin.com/in/udit-gupta-1b7863135/)

#include <bits/stdc++.h>
#pragma GCC optimize "trapv"

using namespace std;

typedef long long int lld;
typedef unsigned long long int llu;

#define       TESTCASES()  cin >> (T3X0); T353 = T3X0; while(T3X0--)
#define       _input(V3C)  for(auto &V3C_I7 : (V3C)) cin >> (V3C_I7)
#define mems(A77AY, V4LU)  memset((A77AY), (V4LU), sizeof((A77AY)))
#define  CH3K(I7, E4, S7)  (((S7)<0) ? (I7)>(E4) : (I7)<(E4))
#define for4(I7,S4,E4,S7)  for(auto I7=(S4); CH3K(I7,E4,S7); (I7)+=(S7))

void solveEachTest(lld T35TC453N = 1) {
    int ans = 0;
    int n ; cin >> n;
    string a, b, c;
    cin >> a >> b >> c;
    for (int i = 0; i < n; i++) {
        set <char> se;
        se.insert(a[i]);
        se.insert(b[i]);
        se.insert(c[i]);
        ans += (se.size() - 1);
    }
    cout << ans;
    cout << "\n";

    return;
}

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

    lld T3X0 = 0, T353 = 1;

    // TESTCASES()
    solveEachTest(T353 - T3X0);
    return 0;
}


#include<bits/stdc++.h>
#define ls (p<<1)
#define rs (p<<1|1)
using namespace std;

/*
	需要维护的是
	a[i]区间最大值
	a[i]-i的区间最小值
*/
#define inf 1000000
#define M 200005
int f[M<<2],laz[M<<2],mx[M<<2];
int n,m;
int res,pos;
void up(int p){
	mx[p]=max(mx[ls],mx[rs]);
	f[p]=min(f[ls],f[rs]);
}
void down(int l,int mid,int r,int p){
	if (laz[p]){
		laz[ls]=laz[rs]=laz[p];
		mx[ls]=mx[rs]=laz[p];
		f[ls]=laz[p]-mid;
		f[rs]=laz[p]-r;
		laz[p]=0;
	}
}
void build(int l,int r,int p){
	if (l==r){
		mx[p]=l;
		f[p]=0;
		return ;
	}

	int mid=(l+r)>>1;
	build(l,mid,ls);
	build(mid+1,r,rs);
	up(p);
}
void pt(int l,int r,int p){
	if (l==r){
		printf("%d ",mx[p]);
		return ;
	}
	int mid=(l+r)>>1;
	down(l,mid,r,p);
	pt(l,mid,ls);
	pt(mid+1,r,rs);
	up(p);
}
void qu(int l,int r,int p){
	if (mx[p]<pos)return;
	if (l==r){
		res=l;
		return;
	}
	int mid=(l+r)>>1;
	down(l,mid,r,p);
	qu(l,mid,ls);
	if (res)return;
	qu(mid+1,r,rs);
}
int qu(int k){
	res=0;
	pos=k;
	qu(1,m+1,1);
	return res;
}
void upd(int l,int r,int x,int y,int k,int p){
	if (l==x&&r==y){
		laz[p]=k;
		mx[p]=k;
		f[p]=k-r;
		return ;
	}
	int mid=(l+r)>>1;
	down(l,mid,r,p);
	if (y<=mid){
		upd(l,mid,x,y,k,ls);
	}
	else if (x>mid){
		upd(mid+1,r,x,y,k,rs);
	}
	else{
		upd(l,mid,x,mid,k,ls);
		upd(mid+1,r,mid+1,y,k,rs);
	}
	up(p);
}
void upd(int l,int r,int k){
	int resl=qu(l);
	int resr=qu(r+1)-1;
//	printf("%d %d\n",resl,resr);
	if (resl<=resr&&resr<=m)upd(1,m+1,resl,resr,k,1);
}
int qu(int l,int r,int x,int y,int p){
	if (l==x&&r==y){
//		printf("%d %d    %d\n",x,y,f[p]);
		return f[p];
	}
	int mid=(l+r)>>1;
	down(l,mid,r,p);
	if (y<=mid)return qu(l,mid,x,y,ls);
	if (x>mid)return qu(mid+1,r,x,y,rs);
	return min(qu(l,mid,x,mid,ls),qu(mid+1,r,mid+1,y,rs));
}
int calc(int i){
	int res=qu(1,m+1,1,m,1);
	if (res>=m)return -1;
	return res+i;
}
/*
4 4
2 4
1 1
2 3
2 4
*/

int main(){
//	freopen("1.in","r",stdin);
	scanf("%d%d",&n,&m);
	int i,l,r;
	build(1,m+1,1);
	for (i=1;i<=n;i++){
		scanf("%d%d",&l,&r);
		if (r==m){
			upd(l,r,inf);
		}
		else{
			upd(l,r,r+1);
		}
//		pt(1,m+1,1); printf("\n");
		printf("%d\n",calc(i));
	}
	return 0;
}

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;
static const LL INF = 1LL << 60;

template<typename T, int SIZE, T DEF_VAL, const T& Compare(const T&, const T&)>
class SegmentTree {
	vector<T> val;
public:
	SegmentTree() : val(SIZE * 2, DEF_VAL) { }
	void update(int i, const T& value) {
		i += SIZE;
		val[i] = value;
		while (i > 1) i >>= 1, val[i] = Compare(val[i * 2], val[i * 2 + 1]);
	}
	T get(LL a, LL b, int l = 0, int r = SIZE, size_t i = 1) {
		if (r <= a || b <= l) return DEF_VAL;
		if (a <= l && r <= b) return val[i];
		return Compare(get(a, b, l, (l + r) / 2, i * 2), get(a, b, (l + r) / 2, r, i * 2 + 1));
	}
	LL find_leftmost_index(LL a, LL b, const T& value, int l = 0, int r = SIZE, int i = 1) {
		if (Compare(val[i], value) != val[i]) return -1;
		if (r <= a || b <= l) return -1;
		if (i >= SIZE) return i - SIZE;
		LL leftmost_index = find_leftmost_index(a, b, value, l, (l + r) / 2, i * 2);
		if (leftmost_index >= 0) return leftmost_index;
		return find_leftmost_index(a, b, value, (l + r) / 2, r, i * 2 + 1);
	}
	LL find_rightmost_index(LL a, LL b, const T& value, int l = 0, int r = SIZE, int i = 1) {
		if (Compare(val[i], value) != val[i]) return -1;
		if (r <= a || b <= l) return -1;
		if (i >= SIZE) return i - SIZE;
		LL rightmost_index = find_rightmost_index(a, b, value, (l + r) / 2, r, i * 2 + 1);
		if (rightmost_index >= 0) return rightmost_index;
		return find_rightmost_index(a, b, value, l, (l + r) / 2, i * 2);
	}
};

void solve(long long H, long long W, std::vector<long long> &A, std::vector<long long> &B) {
	SegmentTree<long long, 1 << 18, INF, min> st;
	for (int i = 0; i < W; ++i) {
		st.update(i, 0);
	}
	for (LL y = 0; y < H; ++y) {
		LL a = A[y], b = B[y];
		while (true) {
			LL min_value = st.get(a, b);
			if (min_value == INF) break;
			LL index = st.find_leftmost_index(a, b, min_value);
			LL next_cost = min_value + b - index;
			if (b < W && next_cost < st.get(b, b + 1)) {
				st.update(b, next_cost);
			}
			st.update(index, INF);
		}
		LL cost = y + 1 + st.get(0, W);
		cout << (cost < INF ? cost : -1) << endl;
	}
}

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


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

#define PRINT "print"
#define REVER "reverse"
#define REPLA "replace"

int main(){
	int a,b,q,i,j,k;
	char tmp;
	char comm[20],str[1001],p[1001];

	scanf("%s %d",str,&q);
	for(i=0;i<q;i++){
		scanf("%s",comm);
		if(strcmp(comm,PRINT) == 0){
			scanf("%d %d",&a,&b);
			for(j=a;j<=b;j++){
				printf("%c",str[j]);
			}
			printf("\n");
		}else if(strcmp(comm,REVER) == 0){
			scanf("%d %d",&a,&b);
			while(a<b){
				tmp = str[a];
				str[a] = str[b];
				str[b] = tmp;
				a++;
				b--;
			}
		}else if(strcmp(comm,REPLA) == 0){
			scanf("%d %d %s",&a,&b,p);
			for(j=a;j<=b;j++){
				str[j] = p[j-a];
			}
		}
	}
	return 0;
}

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

using namespace std;

int main () {
    // input
    string str;
    cin >> str;

    int q;
    cin >> q;

    string result = str;
    int res_len = result.length();
    for (int i=0; i<q; i++) {
        string order;
        int a, b;
        cin >> order >> a >> b;
        if (order == "print") {
            // output
            cout << result.substr(a, b-a+1) << endl;
        } else if (order == "reverse") {
            string reversed = "";
            for (int j=0; j<a; j++) {
                reversed += result[j];
            }
            for (int jj=b; jj>a-1; jj--) {
                reversed += result[jj];
            }
            for (int jjj=b+1; jjj<res_len; jjj++) {
                reversed += result[jjj];
            }
            result = reversed;
            //cout << reversed << endl;
        } else if (order == "replace") {
            string p;
            cin >> p;
            string replaced = "";
            for (int k=0; k<a; k++) {
                replaced += result[k];
            }
            for (int kk=a; kk<b+1; kk++) {
                replaced += p[kk-a];
            }
            for (int kkk=b+1; kkk<res_len; kkk++) {
                replaced += result[kkk];
            }
            result = replaced;
            //cout << result << endl;
        }
    }
}


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

int main() {
	ll K;cin >> K;
	ll x = K / 50;
	ll y = K % 50;
	vector<ll> ans;
	cout << 50 << endl;
	for (int i = 0;i < 51;i++) {
		if (i + y != 50)
			ans.push_back(i);
	}
	for (int i = 0;i < 50;i++) {
		cout << ans[i] + x << " ";
	}
}

#include "bits/stdc++.h"
#include "ext/pb_ds/tree_policy.hpp"
#include "ext/pb_ds/assoc_container.hpp"
using namespace std;
////////////// Prewritten code follows. Look down for solution. ////////////////
#define x first
#define y second
#define LEN(x) ((int)(x).size())
#define ALL(x) x.begin(), x.end()
using ll = long long;
using llu = long long unsigned;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vpii = vector<pii>;

const ld EPS = 1e-9;
inline int cmp(ld x, ld y = 0, ld tol = EPS) {
	return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

const int MOD = 1;
inline int mod(ll x, int m = MOD) {
	return (int)(((x%m) + m)%m);
}

template<typename T, typename M = __gnu_pbds::null_type>
using ordered_set = __gnu_pbds::tree<T, M, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

////////////////////////// Solution starts below. //////////////////////////////

vector<string> vec = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

int main () {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.precision(10);
	string s;
	cin >> s;
	int ans = 0;
	int p = 0;
	while(vec[p] != s) ++p;
	while(vec[p] != "SUN") {
		p = (p+1)%7;
		++ans;
	}

	if(!ans) ans += 7;
	cout << ans << '\n';

	return 0;
}


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

struct edge{
    int to,cost;
    edge(){
        to = 0; cost = 0;
    }
    edge(int t,int c){
        to = t;
        cost = c;
    }
};

int n;
vector<edge> G[100001];
long long int d[100001];

const long long int INF = 1000000000000;

void Dijkstra(int s){

    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que;

    fill(d,d+n,INF);

    d[s] = 0;

    que.push(make_pair(0,s));

    while(!que.empty()){
        pair<int,int> p = que.top(); que.pop();

        int v = p.second;
        if(d[v] < p.first){
            continue;
        }
        for(int i=0; i<G[v].size(); i++){
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(make_pair(d[e.to],e.to));
            }
        }

    }

}

int main() {

    cin >> n;

    int ed,r;

    cin >> ed >> r;

    int a, b, c;

    for (int i = 0; i < ed; i++) {
        scanf(" %d %d %d", &a, &b, &c);
        G[a].push_back(edge(b, c));
    }

    Dijkstra(r);

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

        if(d[i]!=INF){
            cout << d[i] << endl;
        }
        else{
            cout << "INF" << endl;
        }
    }

    return 0;
}

#include<stdio.h>
int main(void)
{
	int n,pm,pe,pj,i,j,pme,pmej;
	scanf("%d",&n);
	while(n!=0){
		for(i=0;i<n;i++){
				scanf("%d %d %d",&pm,&pe,&pj);
				pme=(pm+pe)/2;
				pmej=(pm+pe+pj)/3;
				if(pm==100 || pe==100 || pj==100){
					printf("A\n");
				}
				else if(pme>=90){
					printf("A\n");
				}
				else if(pmej>=80){
					printf("A\n");
				}
				else if(pmej>=70){
					printf("B\n");
				}
				else if(pmej>=50 && pm>=80 || pe>=80){
					printf("B\n");
				}
				else {
					printf("C\n");
				}
			}
		scanf("%d",&n);
	}
	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);
    string s;
    cin >> s;
    vector<int> cnt(26,0);
    for(int i = 0; i < s.size(); i++) {
        cnt[s[i]-'a']++;
    }
    if(s.size()==26) {
        int idx=25;
        while(idx>0 && s[idx-1]>s[idx]) idx--;
        if(idx==0) {
            cout << "-1" << "\n";
            return 0;
        }

        char e='z'+1;
        for(int i = idx; i < s.size(); i++) if(s[idx-1]<s[i]) e=min(e,s[i]);
        for(int i = 0; i < idx-1; i++) cout << s[i];
        cout << e << "\n";
    }
    else{
        cout << s;
        int idx=0;
        while(cnt[idx]!=0) idx++;
        cout << char('a'+idx) << "\n";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7,inv2=5e8+4;
inline int poww(long long x,int y)
{
	long long ret=1;
	while(y)
	{
		if(y&1)ret*=x,ret%=MOD;
		x*=x,x%=MOD;
		y>>=1;
	}
	return ret;
}
int n,k;
long long dp[2333][2333];
long long fac[4555555],invfac[4555555];
inline long long c(int a,int b)
{
	return fac[a]*invfac[b]%MOD*invfac[a-b]%MOD;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>n>>k;
	if(k==1)return cout<<1<<endl,0;
	fac[0]=1;
	for(int i=1;i<=4000000;i++)fac[i]=fac[i-1]*i%MOD;
	invfac[4000000]=poww(fac[4000000],MOD-2);
	for(int i=4000000;i>=1;i--)invfac[i-1]=invfac[i]*i%MOD;
	dp[n+1][n]=1;
	for(int i=n;i>=1;i--)
	{
		long long sum=0;
		for(int j=n;j>=i;j--)
		{
			sum+=dp[i+1][j];sum%=MOD;
			dp[i][j]=c(n-j+(n-i)*(k-1)+k-2,k-2)*sum%MOD;
//			cerr<<"dp "<<i<<','<<j<<'='<<dp[i][j]<<endl;
		}
	}
	long long sum=0;
	for(int i=1;i<=n;i++)sum+=dp[1][i];
	cout<<sum%MOD*fac[n]%MOD<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i)
#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)
#define mp(x,y) make_pair((x),(y))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
inline int read(){
    register int x=0,f=1;register char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}
    return f?x:-x;
}

const int mod=1000000007;
int n,k,f[4005][2005],fac[16000005],ifc[16000005];
inline void inc(int& x,int y){x=x+y<mod?x+y:x+y-mod;}
inline void dec(int& x,int y){x=x-y>=0?x-y:x-y+mod;}
inline int power(int b,int n){int ans=1;for(;n;n>>=1,b=1ll*b*b%mod)if(n&1)ans=1ll*ans*b%mod;return ans;}
inline int C(int n,int m){return (n<0||m<0||n<m)?0:1ll*fac[n]*ifc[m]%mod*ifc[n-m]%mod;}

int main(){
	// freopen("in.in","r",stdin);
	n=read(),k=read();
	REP(i,fac[0]=1,16000001)fac[i]=1ll*fac[i-1]*i%mod;
	ifc[16000001]=power(fac[16000001],mod-2);
	DREP(i,16000001,1)ifc[i-1]=1ll*ifc[i]*i%mod;
	if(k==1)return puts("1"),0;
	f[0][0]=1;
	REP(i,0,n+n-1)REP(j,0,i>>1)if(f[i][j]){
		if(i-j>j)inc(f[i+1][j+1],f[i][j]);
		int num=(i-j)*(k-1)+j+1;
		inc(f[i+1][j],1ll*(n-i+j)*f[i][j]%mod*C(k-2+num-1,num-1)%mod);
	}
	printf("%d\n",f[n+n][n]);
	return 0;
}


//    Have a nice day
//    ∠( ᐛ 」∠)

#include<bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define x first
#define y second

using namespace std;

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

template <typename T> inline T read()
{
	T sum=0, fl=1; char ch=getchar();
	for(; !isdigit(ch); ch=getchar()) if(ch=='-') fl=-1;
	for(; isdigit(ch); ch=getchar()) sum=(sum<<3)+(sum<<1)+ch-'0';
	return sum*fl;
}

const int maxn=2000+5;
int n, m, ans;
char s[maxn][maxn];
int l[maxn], r[maxn], h[maxn], c[maxn][maxn];

void Solve()
{
	n=read<int>(), m=read<int>(); ans=max(n, m);
	for(int i=1; i<=n; ++i) scanf("%s", s[i]+1);
	--n, --m;
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=m; ++j)
			if(((s[i][j]=='#')+(s[i+1][j]=='#')+(s[i][j+1]=='#')+(s[i+1][j+1]=='#'))&1);
			else c[i][j]=1;

	for(int i=1; i<=m; ++i) l[i]=r[i]=maxn;
	for(int i=1; i<=n; ++i)
	{
		for(int j=1, p=0; j<=m; ++j)
		{
			if(c[i][j]) ++p; else p=0;
			l[j]=min(l[j], p);
		}
		for(int j=m, p=0; j>=1; --j)
		{
			if(c[i][j]) ++p; else p=0;
			r[j]=min(r[j], p);
		}
		for(int j=1; j<=m; ++j)
		{
			if(c[i][j]) ++h[j], ans=max(ans, (l[j]+r[j])*(h[j]+1));
			else l[j]=r[j]=maxn, h[j]=0;
		}
	}
	printf("%d\n", ans);
}

int main()
{

   // freopen("081d.in","r",stdin);
    //freopen("081d.out","w",stdout);

    Solve();

    return 0;
}


#include<stdio.h>

#define N 100
int A[N + 1][N + 1];
int p[N + 1];

int solve(int i, int j)
{
    if(!(A[i][j] < 0)){ return A[i][j]; }
    if(j == i){ A[i][i] = 0; return 0; }
    if(j == i + 1){
        A[i][j] = p[i - 1] * p[i] * p[i + 1];
        return A[i][j];
    }
    int min = -1, x;
    for(int k = i; k < j; k++){
        x = solve(i, k) + solve(k + 1, j) + p[i - 1] * p[k] * p[j];
        if(min < 0){ min = x; }
        else{ if(min > x) min = x; }
    }
    A[i][j] = min;
    return min;
}

int main()
{
    int i, j, n;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        scanf("%d %d", &p[i - 1], &p[i]);
        for(j = 1; j <= n; j++){
            A[i][j] = -1;
        }
    }
    printf("%d\n", solve(1, n));
    return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll memo[111][111];
int n, a[111], b[111];
ll rec(int l, int r){
    if(memo[l][r] != 1e9) return memo[l][r];
    if(l == r) return memo[l][r] = 0;
    ll ret = 1e9;
    FOR(m, l, r){
      ret = min(ret, rec(l, m) + rec(m + 1, r) + a[l] * b[m] * b[r]);
    }
    return memo[l][r] = ret;
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  FOR(i,0,n) cin >> a[i] >> b[i];
  FOR(i,0,111)FOR(j,0,111)memo[i][j]=1e9;
  cout << rec(0,n-1) << endl;
}


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

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

int main()
{
string s ,t;
cin>>s>>t;

cout<<t<<s<<endl;
 return 0;
}

#include<stdio.h>

int main()
{
	char a[105];
	char b[105];
	scanf("%s %s", &a, &b);
	printf("%s%s", &b, &a);
	return 0;
}


#include "assert.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <vector>

using namespace std;

#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;
mt19937 mrand(random_device{}());

inline int in() {
  char ch = getchar();
  int f = 1, x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return f * x;
}

const int inf = (1 << 30);
const int mod = 998244353;
const int N = 100010;

LL cnt[50][30];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  // freopen("out.out", "w", stdout);
#endif
  int n = in();
  int tmp = 1e9, A = 0, B = 0;
  while (tmp % 2 == 0)
    ++A, tmp /= 2;
  while (tmp % 5 == 0)
    ++B, tmp /= 5;
  cerr << A << " " << B << endl;
  LL ans = 0;
  rep(_, 1, n) {
    double x;
    cin >> x;
    LL tmp = llround(x * 1e9) ;
    int a = 0, b = 0;
    while (tmp % 2 == 0)
      ++a, tmp /= 2;
    while (tmp % 5 == 0)
      ++b, tmp /= 5;
    rep(i, 0, 49) rep(j, 0, 29) {
      if (i + a >= A * 2 && j + b >= B * 2)
        ans += cnt[i][j];
    }
    cnt[a][b]++;
    cerr << ans << endl;
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

	vector<vector<ll>> cnt(62, vector<ll>(62, 0));
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		if (find(s.begin(), s.end(), '.') == s.end()) s.push_back('.');
		int pos = 0;
		while (s[pos] != '.') pos++;
		s += string(10 - s.size() + pos, '0');
		ll y = 0;
		for (auto c : s) {
			if (isdigit(c)) y = y * 10 + c - '0';
		}
		int p2 = 0, p5 = 0;
		for (; y % 2 == 0; y /= 2, p2++);
		for (; y % 5 == 0; y /= 5, p5++);
		cnt[p2][p5]++;
	}
	ll ans = 0;
	for (int p2 = 0; p2 < 62; p2++) {
		for (int p5 = 0; p5 < 62; p5++) {
			for (int pp2 = 0; pp2 < 62; pp2++) {
				for (int pp5 = 0; pp5 < 62; pp5++)
					ans += cnt[p2][p5] * cnt[pp2][pp5] * (min(p2 + pp2, p5 + pp5) >= 18) *
					       (pair<int, int>{p2, p5} != pair<int, int>{pp2, pp5});
			}
		}
	}
	for (int p2 = 0; p2 < 62; p2++) {
		for (int p5 = 0; p5 < 62; p5++) ans += cnt[p2][p5] * (cnt[p2][p5] - 1) * (min(p2, p5) * 2 >= 18);
	}
	cout << ans / 2 << '\n';
}


/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

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

  int64_t N;
  cin >> N;
  // const int64_t L = static_cast<int64_t>(T.length());
  vector<int64_t> A(N + 1, 0);
  for (int64_t i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  vector<int64_t> result;
  vector<int64_t> B(N + 1, 0);
  for (int64_t i = N; i >= 1; --i) {
    int64_t temp = 0;
    for (int64_t j = 2 * i; j <= N; j += i) {
      temp += B[j];
      temp %= 2;
    }
    if (temp != A[i]) {
      B[i] = 1;
      result.emplace_back(i);
    }
  }
  cout << result.size() << endl;
  for (int64_t i = 0; i < static_cast<int64_t>(result.size()); ++i) {
    if (i != 0) {
      cout << " " << endl;
    }
    cout << result[i];
  }
  cout << endl;
  return 0;
}

/*
    Written by Nitrogens
    Desire for getting accepted!!
*/
#include <cstdio>
#include <ctime>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <bitset>
#include <stack>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

const int maxn = 1e5+5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl

ll x[maxn], y[maxn];
ll a[maxn], b[maxn];

ll sum(ll n) {
    return n * (n + 1) / 2 % Mod;
}

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);

    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &x[i]);
    for (int i = 1; i <= m; i++) scanf("%lld", &y[i]);
    for (int i = 1; i < n; i++) a[i] = (x[i + 1] - x[i] + Mod) % Mod;
    for (int i = 1; i < m; i++) b[i] = (y[i + 1] - y[i] + Mod) % Mod;
    n--, m--;
    ll X = 0, Y = 0;
    for (int i = 1; i <= n; i++) {
        ll cnt = sum(n) - sum(i - 1) - sum(n - i);
        cnt %= Mod;
        cnt = (cnt + Mod) % Mod;
        ll delta = cnt * a[i] % Mod;
        X = (X + delta) % Mod;
    }
    for (int i = 1; i <= m; i++) {
        ll cnt = sum(m) - sum(i - 1) - sum(m - i);
        cnt %= Mod;
        cnt = (cnt + Mod) % Mod;
        ll delta = cnt * b[i] % Mod;
        Y = (Y + delta) % Mod;
    }
    ll ans = X * Y % Mod;
    printf("%lld\n", ans);

    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repSE(i,s,n) for (int i = (s); i < (n); i++)
#define rrepSE(i,s,e) for (int i = (s); i > (e); i--)
#define ssort(v) sort(v.begin(), v.end())
#define gsort(v) sort(v.rbegin(), v.rend())
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
#define DEBUG_PRINT(var) std::cout << #var << " = " << var << "\n";

int main()
{
    ll a, b;
    cin >> a >> b;
    map<ll,int> ad, bd;
    ll na = a, nb = b;
    for (ll i = 2; i*i <= a; i++) {
        while (na%i == 0) {
            ad[i]++;
            na /= i;
        }
    }
    if (na > 1) ad[na] ++;
    for (ll i = 2; i*i <= b; i++) {
        while (nb%i == 0) {
            bd[i]++;
            nb /= i;
        }
    }
    if (nb > 1) bd[nb] ++;
    ll ans = 1;
    for (auto ea : ad) {
        if (bd[ea.first] > 0) ans++;
    }
    cout << ans << "\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(ll a,ll b){
  while(b>0){
    ll r=a%b;
    a=b;
    b=r;
  }
  return a;
}
int main(){
  ll a,b;
  cin >> a >> b;
  ll p=gcd(a,b);
  ll i;
  ll cp=p;
  vector<ll> ans(2000);
  ans.at(0)=1;
  ll j=1;
  for(i=2;i*i<=p;i++){
    bool is=false;
    while(cp%i==0){
      cp/=i;
      is=true;
    }
    if(is){
      ans.at(j)=i;
      j++;
    }
  }
  if(cp!=1){
    ans.at(j)=cp;
  }
  rep(i,2000){
    if(ans.at(i)==0){
      break;
    }

  }
  rep(i,2000){
    if(ans.at(i)==0){
      cout << i;
      return 0;
    }
  }
}


#include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); ++i)
// using namespace atcoder;
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
//マクロ
// forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())       // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))    //最大値を求める
#define MIN(x) *min_element(ALL(x))    //最小値を求める
//定数
#define INF 1000000000000  // 10^12:極めて大きい値,∞
#define inf 2147483647     // int値の最大値
#define MOD 1000000007     // 10^9+7:合同式の法
#define MAXR 100000        // 10^5:配列の最大のrange(素数列挙などで使用)
//略記
#define PB push_back                             // vectorヘの挿入
#define MP make_pair                             // pairのコンストラクタ
#define F first                                  // pairの一つ目の要素
#define S second                                 // pairの二つ目の要素
#define CST(x) cout << fixed << setprecision(x)  //小数点以下の桁数指定
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;
}
int gcd(int a, int b) {
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
//各桁の和
int sumDight(int x) {
    int sum = 0;
    while (x > 0) {
        sum += (x % 10);
        x /= 10;
    }
    return sum;
}
//回文数
int divideReverse(int x) {
    int reverse = 0;
    int r;
    while (x > 0) {
        r = x % 10;
        reverse = reverse * 10 + r;
        x /= 10;
    }
    return reverse;
}

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

    int n, m;
    cin >> n;
    vector<string> s(110);
    rep(i, n) cin >> s[i];
    cin >> m;
    vector<string> t(110);
   rep(i, m) cin >> t[i];

    map<string, int> a;
    rep(i, n) { a[s[i]] += 1; }
    rep(i, m) a[t[i]] -= 1;
    int ma =0;
    for (auto p :a){
        if (ma < a.at(p.first)) ma = a.at(p.first);

    }
    if(ma>0)cout << ma << endl;
    else
        cout << 0 << endl;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int Max = 1e6 + 5;

int lst[Max];
string ls[Max];
map<string, int> ma1, ma2;

int main()
{
	int n, m, g = 0;
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		string t;
		cin >> t;
		ma1[t]++;
		ls[++g] = t;
	}
	cin >> m;
	for (int i = 1;i <= m;i++)
	{
		string t;
		cin >> t;
		ma2[t]++;
		ls[++g] = t;
	}
	int ma = -1e9 - 5;
	for (int i = 1;i <= g;i++)
	{
		ma = max(ma, ma1[ls[i]] - ma2[ls[i]]);
	}
	cout << max(0,ma);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i, n) for(ll i=1;i<=(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000
#define LLINF 1e18

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

void solve_abc_a() {
	ll n, m;
	ll x, y;
	ll a, b, d;
	ll c;
	//char c;
	ll h, w;
	ll k;
	string s, t;
	//ll s, t;
	ll l_ans = 0;
	string s_ans = "NG";

	cin >> a >> b;
	if (a<=8 && b<=8){
		s_ans = "Yay!";
	}
	else{
		s_ans = ":(";
	}

	cout << s_ans << endl;

	return;
}

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

	solve_abc_a();

	return 0;
}

//#include "stdafx.h"
#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int a,b,ans,q;
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	cin>>a>>b;
	q=abs(a-b);
	ans=16-(a+b);
	if(q>ans/2)
		cout<<":(\n";
	else
		cout<<"Yay!\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long INF = numeric_limits<long long>::max();
int main()
{
	int R, G;
	cin >> R >> G;
	cout << 2 * G - R << endl;
}

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

int main() {
  int a,b,ans=0;
  cin >> a >> b;
  for(int i = a;i<b+1;i++){
   if(i/10000==i%10&&(i/1000-(i/10000)*10)==(i%100-i%10)/10)ans++;
  }
  cout << ans;
}


//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

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

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 10000000
#define MAX_V 101
struct edge{int to;LL cost;};
typedef pair<LL,int> P;//firstは最短距離,secondは頂点番号
int V;
vector<edge> G[MAX_V];
LL d[MAX_V];

void dijkstra(int s){
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+V,INF);
	d[s]=0;
	que.push(P(0,s));

	while(!que.empty()){
		P p=que.top();que.pop();//仮の最短距離が短い順に取り出す
		int v=p.second;
		if(d[v]<p.first)continue;
		REP(i,G[v].size()){
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));//仮の最短距離と頂点の組を更新が行われる度に追加していく
			}
		}
	}
}

int main(){
	long n,k;
	while(cin>>n>>k){
		if(n==0&&k==0)break;
		V=n;
		REP(i,n)G[i].clear();
		REP(i,k){
			int ch;
			cin>>ch;
			if(ch){
				int ct,dt;
				LL et;
				cin>>ct>>dt>>et;
				ct--;dt--;
				edge ed={dt,et};
				G[ct].PB(ed);
				ed.to=ct;
				G[dt].PB(ed);
			}else{
				int a,b;
				cin>>a>>b;
				a--;b--;
				dijkstra(a);
				if(d[b]<INF)cout<<d[b]<<endl;
				else cout<<"-1"<<endl;
			}
		}
	}

	return 0;
}

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

typedef long long ll;
const ll INF = 10000000000;

int main(){
	int v,e;
	ll d[100][100];
	scanf("%d %d",&v,&e);
	fill(d[0],d[0]+10000,INF);
	for(int i=0;i<v;i++) d[i][i] = 0;
	for(int i=0;i<e;i++){
		int s,t;
		ll dist;
		scanf("%d %d %lld",&s,&t,&dist);
		d[s][t] = dist;
	}
	for(int k=0;k<v;k++){
		for(int i=0;i<v;i++){
			for(int j=0;j<v;j++){
				if(d[i][k]!=INF&&d[k][j]!=INF) d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
			}
		}
	}
	bool flag = false;
	for(int i=0;i<v;i++){
		if(d[i][i]<0) flag = true;
	}
	if(flag){
		printf("NEGATIVE CYCLE\n");
	}else{
		for(int i=0;i<v;i++){
			for(int j=0;j<v;j++){
				if(d[i][j]==INF) printf("INF");
				else printf("%lld",d[i][j]);
				if(j==v-1) printf("\n");
				else printf(" ");
			}
		}
	}
	return 0;
}


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

using namespace std;

int n;
int k;

vector<int> card;
set< string > cd;
set< vector<int> > ccd;

/*
string IteratorToString(vector<int>::iterator begin, vector<int>::iterator end){
	string str = "";
	char ch[256];
	for(;begin < end;begin++){
		sprintf(ch,"%d",*begin);
		str += ch;
	}
	return str;
}
*/
string VectorToString(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
	}
	return str;
}

string VectorToDebugText(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
		str += ":";
	}
	return str;
}

void solve(int a,vector<int> v){
	if(v.size() == k){
		sort(v.begin(),v.end());
		ccd.insert(v);
		return;
	}
	if(a == n){return;}
	solve(a + 1,v);
	v.push_back(card[a]);
	solve(a + 1,v);
}

int main(){
	while(1){
		int self;
		scanf("%d %d",&n,&k);
		if(n == 0 && k == 0){
			break;
		}
		card.clear();
		cd.clear();
		ccd.clear();
		for(int i=0;i<n;i++){
			scanf("%d",&self);
			card.push_back(self);
		}
		vector<int> cc(k);

		sort(card.begin(),card.end());
		/*
		while(next_permutation(card.begin(),card.end())){
			 cd.insert(VectorToString(card.begin(),card.begin()+k));
		}
		*/
		solve(0,vector<int>());
		for(set<vector<int> >::iterator it = ccd.begin();it != ccd.end();it++){
			vector<int> vec = *it;
			sort(vec.begin(),vec.end());
			cd.insert(VectorToString(vec));
			//printf("[%s]\n",VectorToDebugText(vec).c_str());
			while(next_permutation(vec.begin(),vec.end())){
				//printf("%s\n",VectorToDebugText(vec).c_str());
				//printf("%s\n",VectorToString(vec).c_str());
				cd.insert(VectorToString(vec));
			}
		}
		printf("%d\n",cd.size());
	}
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool compareg(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;

    }else{
        return a.first > b.first;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

const vector<ll> d2{0, 1, 0, 2, 0, 1};
const vector<ll> d3{0, 0, 1, 0, 0, 1};
const vector<ll> d5{0, 0, 0, 0, 1, 0};

int main() {
    string s1,s2;
    cin>>s1>>s2;

    ll N = s1.size();
    ll M = s2.size();
    vector<vector<ll>>dp(N+1,vector<ll> (M+1));

    rep(i,N){
        dp[i+1][0] = i+1;
    }

    rep(i,M){
        dp[0][i+1] = i+1;
    }

    rep(i,N){
        rep(j,M){
            if(s1[i] == s2[j]){
                dp[i+1][j+1] = min({dp[i][j],dp[i+1][j]+1,dp[i][j+1]+1});
            }else{
                dp[i+1][j+1] = min({dp[i][j]+1 ,dp[i+1][j]+1,dp[i][j+1]+1});
            }
        }
    }

    cout<<dp[N][M]<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
vector<vector<string>>ans={
        {"-1"},                 //0
        {"-1"},                 //1
        {"-1"},                 //2
        {                       //3
         "..a",
                "..a",
                "bb."
        },
        {                       //4
         "abcc",
                "abdd",
                "eefg",
                "hhfg"
        },
        {                       //5
         "..abc",
                "..abc",
                "ddeff",
                "gge.h",
                "iijjh"
        },
        {                       //6
         "...abc",
                "...abc",
                "d.ee.f",
                "dgg..f",
                "hhi.jj",
                "kkill."
        },
        {
         "....abc",
                "....abc",
                "....def",
                "....def",
                "gghh..i",
                "jjkk..i",
                "llmmnn."
        }
};
int n;
void printA(vector<string>ans){
    for(auto x:ans){
        cout<<x<<endl;
    }
}
void manage(vector<string>ans,int prev){
    for(auto x:ans){
        cout<<string(prev,'.')
            <<x
            <<string(n-prev-x.length(),'.')
            <<endl;
    }
}
void rec(int n,int prev){
    if(n==0)return;
    if(n>9&&n!=13||n==6){
//        cout<<6<<" ";
        manage(ans[6],prev);
        rec(n-6,prev+6);

    }
    else if(n>8||n%5==0){
//        cout<<5<<" ";
        manage(ans[5],prev);
        rec(n-5,prev+5);
    }
    else{
//        cout<<"4"<<" ";
        manage(ans[4],prev);
        rec(n-4,prev+4);
    }
}
int main(){
    cin>>n;

    if(n<8)printA(ans[n]);
//    for(int i=9;i<30;i++){
//        rec(i,0);
//        cout<<endl;
//    }
    else rec(n,0);

}
//..abc
//..abc
//ddeff
//gge.h
//iijjh

//abcc
//abdd
//eefg
//hhfg

//..a
//..a
//bb.

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
const int INF = 1e18;

using namespace std;

signed main() {
  int N;
  cin >> N;
  if(N == 2){
    cout << -1 << endl;
    return 0;
  }else if(N == 3){
    cout << "aa." << endl;
    cout << "..a" << endl;
    cout << "..a" << endl;
    return 0;
  }

  int x, y;
  if(N%4 == 0){
    x = N/4;
    y = 0;
  }else{
    x = N/4-1;
    y = N-(N/4-1)*4;
  }

  vector<vector<char>> vec(N, vector<char>(N, '.'));

  REP(i, x){
    vec[i*4+0][i*4+0] = 'a';
    vec[i*4+0][i*4+1] = 'a';
    vec[i*4+0][i*4+2] = 'b';
    vec[i*4+0][i*4+3] = 'c';
    vec[i*4+1][i*4+0] = 'd';
    vec[i*4+1][i*4+1] = 'd';
    vec[i*4+1][i*4+2] = 'b';
    vec[i*4+1][i*4+3] = 'c';
    vec[i*4+2][i*4+0] = 'b';
    vec[i*4+2][i*4+1] = 'c';
    vec[i*4+2][i*4+2] = 'a';
    vec[i*4+2][i*4+3] = 'a';
    vec[i*4+3][i*4+0] = 'b';
    vec[i*4+3][i*4+1] = 'c';
    vec[i*4+3][i*4+2] = 'd';
    vec[i*4+3][i*4+3] = 'd';
  }
  REP(i, x*4) REP(j, x*4){
    cout << vec[i][j];
    if(j == x*4-1){
      REP(k, y) cout << '.';
      cout << endl;
    }
  }

  if(y == 5){
    REP(i, x*4) cout << '.'; cout << "aabba" << endl;
    REP(i, x*4) cout << '.'; cout << "bcc.a" << endl;
    REP(i, x*4) cout << '.'; cout << "b..cb" << endl;
    REP(i, x*4) cout << '.'; cout << "a..cb" << endl;
    REP(i, x*4) cout << '.'; cout << "abbaa" << endl;
  }else if(y == 6){
    REP(i, x*4) cout << '.'; cout << "aabc.." << endl;
    REP(i, x*4) cout << '.'; cout << "ddbc.." << endl;
    REP(i, x*4) cout << '.'; cout << "..aabc" << endl;
    REP(i, x*4) cout << '.'; cout << "..ddbc" << endl;
    REP(i, x*4) cout << '.'; cout << "bc..aa" << endl;
    REP(i, x*4) cout << '.'; cout << "bc..dd" << endl;
  }else if(y == 7){
    REP(i, x*4) cout << '.'; cout << "aabbcc." << endl;
    REP(i, x*4) cout << '.'; cout << "dd.dd.a" << endl;
    REP(i, x*4) cout << '.'; cout << "..d..da" << endl;
    REP(i, x*4) cout << '.'; cout << "..d..db" << endl;
    REP(i, x*4) cout << '.'; cout << "dd.dd.b" << endl;
    REP(i, x*4) cout << '.'; cout << "..d..dc" << endl;
    REP(i, x*4) cout << '.'; cout << "..d..dc" << endl;
  }

  return 0;
}


#include<iostream>
#include<string>

using namespace std;

int main( )
{
	string str, cmd, p;
	int q;
	int a, b;

	cin>>str;
	cin>>q;

	while( q-- ) {
		cin>>cmd;
		if( "print" == cmd ) {
			cin>>a>>b;
			cout<<str.substr( a, b-a+1 )<<endl;
		} else if( "reverse" == cmd ) {
			cin>>a>>b;
			char t;
			for( int i=0; i<=(b-a)/2; i++ ) {
				t = str[ a+i ];
				str[ a+i ] = str[ b-i ];
				str[ b-i ] = t;
			}
		} else if( "replace" == cmd ) {
			cin>>a>>b>>p;
			str.replace( a, b-a+1, p );
		}
	}

	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>;
using VVI = vector<vector<int>>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

int main()
{
    int n;
    cin >> n;
    vector<ll> a(n), b(n), c(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    sort(b.begin(), b.end());
    for (int i = 0; i < n; i++)
    {
        cin >> c[i];
    }
    sort(c.begin(), c.end());

    vector<ll> mid(n + 1);
    for (int i = 0; i < n; i++)
    {
        int idx = distance(a.begin(), lower_bound(a.begin(), a.end(), b[i]));
        mid[i + 1] = mid[i] + idx;
    }
    ll ans = 0;
    for (int i = 0; i < n; i++)
    {
        int idx = distance(b.begin(), lower_bound(b.begin(), b.end(), c[i]));
        ans += mid[idx];
    }
    cout << ans << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << " ";
        cout << v[i];
    }
    cout << endl;
}

/*input
5
3 3 3 3 3
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <climits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <tgmath.h>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define ar array
#define scd(t) scanf("%d",&t)
#define scld(t) scanf("%ld",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define tests int r; cin >> r; while(r--)
#define MP make_pair
#define PI acos(-1)
typedef pair<long long, long long> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;
#define godspeed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);

#define M(x) ((x)%(1000000007))

vector<int> adj_list[100010];
bool visited[10010];

void dfs(int node){
	visited[node] = true;
	for(int n: adj_list[node]){
		if(!visited[n]){
			dfs(n);
		}
	}
}

int main() {
 	int64 n;
 	cin>>n;
 	int64 ma = 1000;
 	int64 count = 0;
 	rep(i,n){
 		int64 a;
 		cin>>a;
 		if(i==0){
 			ma = a;
 		}
 		else{
 			if(ma>a){
 				count+=ma-a;
 			}
 			else{
 				ma = a;
 			}

 		}
 	}
 	cout<<count<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;
        for(int i = 0; i < n; i++){
            int pm,pe,pj;
            int heikin = 0;
            cin >> pm >> pe >> pj;
            heikin = pm + pe + pj;
            heikin /= 3;
            if(pm == 100 || pe == 100 || pj == 100) cout << "A" << endl;
            else if((pm + pe) / 2 >= 90) cout << "A" << endl;
            else if(heikin >= 80) cout << "A" << endl;
            else if(heikin >= 70) cout << "B" << endl;
            else if(heikin >= 50 && pm >= 80 || pe >= 80) cout << "B" << endl;
            else cout << "C" << endl;
        }
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
typedef pair<double, double> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<string> &o) {
    string a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
//const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;

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

    rep(i, k) {
        if (i % 2 == 0) {
            if (a % 2 == 1) a--;
            a /= 2;
            b += a;
        } else {
            if (b % 2 == 1) b--;
            b /= 2;
            a += b;
        }
    }

    cout << a << ' ' << b << endl;
}

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

#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define FORW(a, b, c) for(int a = b; a >= c; --a)
#define fi first
#define se second
#define pb push_back
#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 2e5 + 100;
const int maxN = 6e6;
const int oo = 1e18;
const int mod  = 1e9 + 7;

signed main()  {
//    freopen("test.inp", "r", stdin);
//    freopen("spm.out", "w", stdout);
    ios_base::sync_with_stdio(false); cout.tie(0);
    int n, m, d; cin >> n >> m >> d;
    double ans = (double) (m - 1.00) * 2.00 * (n - d) * 1.00 / (n * 1.00 * n);
    if(d == 0) ans /= 2.00;
    cout << fixed << setprecision(10) << ans;
}


#include "bits/stdc++.h"
#define MOD 1000000007
#define inf 0x3f3f3f3f3f3f3f3f
#define pi acos(-1.0)
#define ri register int
#define Abs(x) ((x) < 0 ? (-(x)) : (x))
#define int long long
const int maxn = 2e3 + 10;
inline int Max(int a, int b) { return a > b ? a : b; }
inline int Min(int a, int b) { return a > b ? b : a; }
namespace FastIO {
inline int read()
{
    int f = 1, r = 0;
    char c = getchar();
    while (!isdigit(c)) {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (isdigit(c)) {
        r = 10 * r + c - '0';
        c = getchar();
    }
    return f * r;
}
inline void write(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
inline void writesp(int x) { write(x), putchar(' '); }
inline void writeln(int x) { write(x), puts(""); }
};
using namespace FastIO;
int N, K, dp[maxn][maxn], fac[maxn * maxn], ifac[maxn * maxn];
inline int exgcd(int a, int b, int& x, int& y)
{
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    int d = exgcd(b, a % b, x, y);
    int tmp = y;
    y = x - a / b * y;
    x = tmp;
    return d;
}
inline int inv(int a, int b)
{
    int x, y;
    exgcd(a, b, x, y);
    return (x + MOD) % MOD;
}
inline int C(int m, int n)
{
    int tmp = (fac[m] * ifac[n] % MOD) * ifac[m - n] % MOD;
    return tmp;
}
signed main()
{
    //freopen("C:\Users\hjw\Downloads\testdata (4).in", "r", stdin);
    N = read(), K = read();
    if (K == 1) {
        puts("1");
        return 0;
    }
    fac[0] = 1, ifac[0] = inv(1, MOD);
    for (ri i = 1; i <= N * K; ++i) {
        fac[i] = fac[i - 1] * i % MOD;
        ifac[i] = inv(fac[i], MOD);
    }
    dp[0][1] = 1;
    for (ri j = 2; j <= N; ++j) {
        dp[0][j] = dp[0][j - 1] * C(j * (K - 1) - 1, K - 2) % MOD;
    }
    for (ri i = 1; i <= N; ++i) {
        for (ri j = i; j <= N; ++j) {
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] * C(j * (K - 1) + i - 1, K - 2)) % MOD;
        }
    }
    dp[N][N] = (dp[N][N] * fac[N] % MOD);
    writeln(dp[N][N]);
    return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const ll MOD = 1000000007;
const int SIZE = 1010;
ll inv[SIZE],fac[SIZE],finv[SIZE];
void make(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(int i=2;i<SIZE;i++){
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        fac[i]=fac[i-1]*(ll) i%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}
ll Combination (int a,int b) {
    if(a<b) return 0;
    return fac[a]*(finv[b]*finv[a-b]%MOD)%MOD;
}
ll dp[1010][1010];
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n,a,b,c,d;
  cin>>n>>a>>b>>c>>d;
  make();
  FOR(i,0,1010) dp[i][0] = 1;
  FOR(i,a,b+1) {
    FOR(j,0,n+1) {
      if(dp[i][j] == 0) continue;
      if(j != 0) (dp[i+1][j] += dp[i][j]) %= MOD;
      ll p = 1;
      for(int k = 1; k <= d && j + i * k <= n; k++) {
        p = (((p * Combination(n - j - i * (k - 1), i) % MOD) * inv[k]) % MOD) % MOD;
        if(k >= c) (dp[i+1][j+i*k] += (dp[i][j] * p) % MOD) %= MOD;
      }
    }
  }
  cout << dp[b+1][n] << endl;
  return 0;
}


#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 200
#define LN 63
#define ull unsigned long long
using namespace std;
int n,ans;ull a[N+5],v[LN+5];char op[N+5];
I void Clear() {ans=0;for(RI i=LN;~i;--i) v[i]=0;}
I void Ins(ull x)
{
	for(RI i=LN;~i;--i) if((x>>i)&1) {if(!v[i]) return (void)(v[i]=x);x^=v[i];}
}
I bool Q(ull x)
{
	for(RI i=LN;~i;--i) if((x>>i)&1) {if(!v[i]) return 0;x^=v[i];}return 1;
}
int main()
{
	RI Tt,i;scanf("%d",&Tt);W(Tt--)
	{
		for(Clear(),scanf("%d",&n),i=1;i<=n;++i) scanf("%llu",a+i);scanf("%s",op+1);
		for(ans=1,i=n;i;--i) if(a[i])
			{if(op[i]=='1'&&!Q(a[i])) {ans=0;break;}op[i]=='0'&&(Ins(a[i]),0);}
		putchar(49-ans),putchar('\n');
	}return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <functional>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) for (int i = 1; i < (int)(n); ++i)
using namespace std;
using lint = int64_t;

void solve() {
    int N;
    cin >> N;
    vector<lint> A(N);
    rep(i, N) cin >> A[i];
    string S;
    cin >> S;

    vector<lint> base;
    for (int i = N - 1; i >= 0; --i) {
        lint x = A[i];
        for (auto b : base) {
            x = min(x, x ^ b);
        }
        if (x) {
            if (S[i] == '0') {
                base.push_back(x);
                sort(base.rbegin(), base.rend());
            } else {
                cout << 1 << "\n";
                return;
            }
        }
    }
    cout << 0 << "\n";
}

int main() {
    int T;
    cin >> T;
    rep(i, T) solve();
    return 0;
}


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

int main(){
	int n , x;
	cin>>n;
	for(int i=1 ; i<=n ; i++){
		x=i;
		if(i%3==0 && x%10!=3){
			cout << " " << i;
		} else if(x%10==3) {
			cout << " " << i;
		} else {
			for(int j=0 ; j<(int(log10((double)n))) ; j++){
				x/=10;
				if(x){
					if(x%10==3){
						cout << " " << i;
						break;
					}
				}
			}
		}
	}
	cout << endl;
	return 0;
}

#include <iostream>
#include <string>

using namespace std;

int main() {
  int n;
  string s1 = "Hoshino", s2 = "Hoshina";
  string::size_type pos;
  cin >> n;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    string line;
    getline(cin, line);
    for (pos = line.find(s1); pos != string::npos; pos = line.find(s1, s2.length() + pos))
      line.replace(pos, s1.length(), s2);
    cout << line << endl;
  }

  return 0;
}