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

using lint = long long;

using namespace std;

struct Sub {
    lint b, l, r;
    Sub(int b = 0, int l = 0, int r = 0) : b(b), l(l), r(r) {}
};

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

    vector<Sub> subs(N);
    for (auto& s : subs) {
        cin >> s.b >> s.l >> s.r;
    }
    sort(subs.begin(), subs.end(), [&](Sub a, Sub b) {
        return a.r * (X - a.b) + a.l * a.b > b.r * (X - b.b) + b.l * b.b;
    });

    // 勉強時間で二分探索
    lint ok = N * X, ng = -1;
    while (ok - ng > 1) {
        lint mid = (ok + ng) / 2;

        lint score = 0;  // 先頭からmid/X科目だけX点を取り、他は0点
        for (int i = 0; i < N; ++i) {
            if (i < mid / X) {
                score += subs[i].r * (X - subs[i].b);
            } else {
                score -= subs[i].l * subs[i].b;
            }
        }

        lint R = mid % X;
        bool judge = (score >= 0);

        int n = mid / X;
        if (R > 0) {
            for (int r = 0; r < N; ++r) {
                lint nscore = score;
                if (r < n) {
                    // r番目を消してn番目を追加
                    nscore -= subs[r].r * (X - subs[r].b) + subs[r].l * subs[r].b;
                    nscore += subs[r].l * min(subs[r].b, R) + subs[r].r * max(0LL, R - subs[r].b);
                    nscore += subs[n].r * (X - subs[n].b) + subs[n].l * subs[n].b;
                } else {
                    // r番目を追加する
                    nscore += subs[r].l * min(subs[r].b, R) + subs[r].r * max(0LL, R - subs[r].b);
                }
                if (nscore >= 0) judge = true;
            }
        }

        (judge ? ok : ng) = mid;
    }

    cout << ok << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
template<typename T>inline bool chkmin(T &x,T y){return (y<x)?(x=y,1):0;}
template<typename T>inline bool chkmax(T &x,T y){return (y>x)?(x=y,1):0;}
inline int read(){
    int x;
    char c;
    int f=1;
    while((c=getchar())!='-' && (c>'9' || c<'0'));
    if(c=='-') f=-1,c=getchar();
    x=c^'0';
    while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
    return x*f;
}
inline ll readll(){
    ll x;
    char c;
    int f=1;
    while((c=getchar())!='-' && (c>'9' || c<'0'));
    if(c=='-') f=-1,c=getchar();
    x=c^'0';
    while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
    return x*f;
}
const int maxn=1e5+10;
int id[maxn],b[maxn],l[maxn],r[maxn];
ll v[maxn];
inline bool cmp(const int A,const int B){
	return v[A]>v[B];
}
int main(){
	int n=read(),lim=read();
	ll tot=0;
	REP(i,1,n){
		b[i]=read(),l[i]=read(),r[i]=read(),id[i]=i;
		v[i]=(ll)l[i]*b[i]+(ll)r[i]*(lim-b[i]);
		tot-=(ll)l[i]*b[i];
	}
	sort(id+1,id+n+1,cmp);
	ll sum=0,ans=1e18;
	ll val=0;
	REP(i,1,n){
		int u=id[i];
		val=v[u];
		if(tot+v[u]>=0) break;
		tot+=v[u];
		sum+=lim;
	}
	REP(i,1,n) if(tot+v[i]>=0){
		if(v[i]>val) tot+=val-v[i];
		if(tot+(ll)l[i]*b[i]>=0){
			chkmin(ans,b[i]-(tot+(ll)l[i]*b[i])/l[i]);
		}
		else{
			chkmin(ans,lim-(tot+v[i])/r[i]);
		}
		if(v[i]>val) tot-=val-v[i];
	}
	printf("%lld\n",ans+sum);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

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

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define  endl 			"\n"
#define  FOR(i, a, n)   for (ll i = a; i < n; i++)
#define  ROF(i, a, n)   for (ll i = a; i >= n; i--)
#define  all(x)         (x).begin(), (x).end()
#define  mset(x, val)   memset(x,val,sizeof(x))
#define  dline          cout<<"///REACHED///\n";

typedef long long ll;

const ll   MOD   =  1e+9+7;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   MAXN  =  4e+5+7;

// 6

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll a,b;
	cin>>a>>b;
	if(((16-(2*min(a,b))+1)/2+min(a,b))>= max(a,b)){
		cout<<"Yay!";
	}
	else{
		cout<<":(";
	}

	return 0;
}

#include <iostream>
using namespace std;

int main() {
	int X, Y;
    cin >> X >> Y;
	if (X < 9 && Y <9){
      cout << "Yay!" << endl;
      return 0;
    }
	cout << ":(" << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define INF 1000000001
int U,V;
int dist_toU;
struct node{
    vector<int> to;
    int dist;
    bool path_toU;
    node(){
        dist=0;
        path_toU=false;
    }
};
void dfs1(int v, int p, vector<node> &nodes){
    for (int i = 0; i < nodes[v].to.size(); i++){
        int v_to=nodes[v].to[i];
        if(v_to==p)continue;
        nodes[v_to].dist=nodes[v].dist+1;
        if(v_to==U){
            nodes[v_to].path_toU=true;
            dist_toU=nodes[v_to].dist;
        }
        dfs1(v_to, v, nodes);
        if(nodes[v_to].path_toU)nodes[v].path_toU=true;
    }
    for (int i = 0; i < nodes[v].to.size(); i++){
        int v_to=nodes[v].to[i];
        if(v_to==p)continue;
        nodes[v].dist=max(nodes[v].dist,nodes[v_to].dist);
    }
}

int main() {
    int N;
    cin>>N;
    cin>>U>>V;U--;V--;
    vector<node> nodes(N);
    for (int i = 0; i < N-1; i++){
        int A,B;
        cin>>A>>B;A--;B--;
        nodes[A].to.push_back(B);
        nodes[B].to.push_back(A);
    }
    dist_toU=0;
    dfs1(V,-1,nodes);
    int move=dist_toU/2+1;
    int ans=0;
    int v=V,p=-1;
    while(move>0){
        for(int v_to:nodes[v].to){
            if(v_to==p)continue;
            if(nodes[v_to].path_toU){
                p=v;
                v=v_to;
                ans=nodes[v].dist-1;
                break;
            }
        }
        move--;
    }
    cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
const int maxn=1e6+10;
const int mod=1e9+7;
const double pi=3.1415926535;
int w[3005],v[3005];
int dp[3005][2];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,t;
    cin>>n>>t;
    for(int i=1;i<=n;i++)
    {
        cin>>w[i]>>v[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=t-1;j>=0;j--)
        {
            for(int x=1;x>=0;x--)
            {
                if(x==0)
                {
                    if(j>=w[i])
                    {
                        dp[j][x]=max(dp[j][x],dp[j-w[i]][x]+v[i]);
                    }
                }
                else
                {
                    dp[j][x]=max(dp[j][x],dp[j][0]+v[i]);
                    if(j>=w[i])
                    {
                        dp[j][x]=max(dp[j][x],dp[j-w[i]][x]+v[i]);
                    }
                }
            }
        }
    }
    cout<<dp[t-1][1]<<'\n';

}


#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define numa(x,a) for(auto x: a)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL( c ) (c).begin(), (c).end()
#define gcd(a,b) __gcd(a,b)
#define endl "\n"
using namespace std;
template <int POS, class TUPLE> void deploy(std::ostream &os, const TUPLE &tuple){}
template <int POS, class TUPLE, class H, class ...Ts> void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get<POS>(t); deploy<POS + 1, TUPLE, Ts...>(os, t); }
template <class T> std::ostream& operator<<(std::ostream &os, std::vector<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T> std::ostream& operator<<(std::ostream &os, std::set<T> &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; }
template <class T, class K> std::ostream& operator<<(std::ostream &os, std::map<T, K> &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; }
#define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); }
#define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); }
#define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); }
#define DEBUG5(var0, var1, var2, var3, var4) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG4(var1,var2,var3,var4); }
#define DEBUG6(var0, var1, var2, var3, var4, var5) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG5(var1,var2,var3,var4,var5);}
typedef long long ll;

#define MAX_V 100100
int V;
vector <pair <int,ll> > edge[MAX_V];
vector <pair <int,ll> > Redge[MAX_V];
vector <int> vs;
bool used[MAX_V];
int cmp[MAX_V];
void reverse_edge(){
  rep2(i,1,V+1){//1-indexedにしてます
    rep(j,sz(edge[i])){
      Redge[edge[i][j].fir].pb(mp(i,edge[i][j].sec));
    }
  }
}

void dfs(int now){
  used[now] = true;
  rep(i,sz(edge[now])){
    if(!used[edge[now][i].fir]){
      dfs(edge[now][i].fir);
    }
  }
  vs.pb(now);
}

void rdfs(int now,int k){
  used[now] = true;
  cmp[now] = k;
  rep(i,sz(Redge[now])){
    if(!used[Redge[now][i].fir]){
      rdfs(Redge[now][i].fir,k);
    }
  }
}
int scc(){
  memset(used,0,sizeof(used));
  vs.clear();
  rep2(v,1,V+1){//1-indexed
    if(!used[v]){
      dfs(v);
    }
  }
  memset(used,0,sizeof(used));
  reverse_edge();
  int k = 0;
  for(int i = sz(vs)-1;i >= 0;i--){
    if(!used[vs[i]]){
      k += 1;
      rdfs(vs[i],k);
    }
  }
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> V;
  int E;
  cin >> E;
  rep(i,E){
    int x,y;
    cin >> x >> y;
    x += 1;
    y += 1;
    edge[x].pb(mp(y,1));
  }
  scc();
  int Q;
  cin >> Q;
  rep(i,Q){
    int x,y;
    cin >>  x >> y;
    x += 1;
    y += 1;
    if(cmp[x] == cmp[y]){
      cout << 1 << endl;
    }else{
      cout << 0 << endl;
    }
  }

  return 0;
}

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

using namespace std;

const int MAX_N = 10000;
const int V = 10000;

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs; //??°?????????????????????
bool used[MAX_N]; //?????§????????????????????????
int cmp[MAX_N];	//?±????????????£????????????????????????????????????

void add_edge(int from,int to)
{
	G[from].push_back(to);
	rG[to].push_back(from);
}

void dfs(int v)
{
	used[v] = true;
	for(int i=0;i<G[v].size();i++){
		if(!used[G[v][i]]){
			dfs(G[v][i]);
		}
	}
	vs.push_back(v);
}

void rdfs(int v,int k)
{
	used[v] = true;
	cmp[v] = k;
	for(int i=0;i<rG[v].size();i++){
		if(!used[rG[v][i]]){
			rdfs(rG[v][i],k);
		}
	}
}

int scc()	//?????£??????????????°?????????
{
	memset(used,0,sizeof(used));
	vs.clear();
	for(int v=0;v<V;v++){
		if(!used[v]){
			dfs(v);
		}
	}
	memset(used,0,sizeof(used));
	int k=0;
	for(int i=vs.size()-1;i>=0;i--){
		if(!used[vs[i]]){
			rdfs(vs[i],k++);
		}
	}
	return k;
}

int main()
{
	int n,m,from,to,k,l,u,v;
	cin >> n >> m;
	for(int i=0;i<m;i++){
		cin >> from >> to;
		add_edge(from,to);
	}
	k = scc();
	cin >> l;
	for(int i=0;i<l;i++){
		cin >> u >> v;
		printf("%d\n",(cmp[u] == cmp[v]));
	}
}


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

int main() {
	int N, Q;
	scanf("%d %d", &N, &Q);
	vector<tuple<int, int, int>> event;
	for(int i = 0; i < N; i++) {
		int s, t, x;
		scanf("%d %d %d", &s, &t, &x);
		event.push_back(make_tuple(s-x, 1, x));
		event.push_back(make_tuple(t-x, -1, x));
	}

	vector<int> D(Q);
	for(int i = 0; i < Q; i++) scanf("%d", &D[i]);

	sort(event.begin(), event.end());
	multiset<int> st;
	int k = 0;  //何番目のイベントまで処理したか
	for(int i = 0; i < Q; i++) {
		while(k < event.size() && get<0>(event[k]) <= D[i]) {
			if(get<1>(event[k]) == 1) {
				st.insert(get<2>(event[k]));
			} else {
				auto it = st.find(get<2>(event[k]));
				if(it != st.end()) st.erase(it); //1つだけ削除
			}
			k++;
		}

		if(st.empty()) printf("-1\n");
		else printf("%d\n", *st.begin());
	}

	return 0;
}

#include <cstdio>  //{{{
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>

#define ALL(c) (c).begin(), (c).end()
#define DUMP(x) std::cerr << #x << ':' << x << '\n';

typedef long long ll;
// clang-format off
template<typename T,typename U>inline bool chmax(T&x,U a){if(x>=a)return 0;x=a;return 1;}
template<typename T,typename U>inline bool chmin(T&x,U a){if(x<=a)return 0;x=a;return 1;}
inline int in(){int x;scanf("%d", &x);return x;}
// clang-format on
// }}}
//{{{ graph.hpp
#ifndef INCLUDE_GRAPH_HPP
#define INCLUDE_GRAPH_HPP
#include <vector>
#include <climits>
namespace orliv {
namespace graph {
struct Edge {
  typedef int weight_type;
  static constexpr weight_type INF =
      std::numeric_limits<weight_type>::max() - 100;

  int from, to;
  weight_type w;
  Edge(int from, int to, weight_type w = 1) : from(from), to(to), w(w) {}
  bool operator<(const Edge& rhs) const {
    if (w != rhs.w) return w > rhs.w;
    if (from != rhs.from) return from < rhs.from;
    return to < rhs.to;
  }
};

typedef Edge::weight_type Weight;
typedef std::vector<Edge> Edges;
typedef std::vector<Weight> Weights;
typedef std::vector<int> Path;
}
typedef std::vector<graph::Edges> Graph;
}
#endif
//}}}
//{{{ prim.cc
#ifndef INCLUDE_GRAPH_HPP
#include "graph.hpp"
#endif
#include <queue>

namespace orliv {
namespace graph {

Edges prim(const Graph& g, int s = 0) {
  const int V = g.size();
  Edges spanning_tree;
  spanning_tree.reserve(V - 1);

  std::vector<bool> visited(V, false);
  std::priority_queue<Edge> pq;
  for (pq.emplace(-2, s, 0); !pq.empty();) {
    auto e = pq.top();
    pq.pop();
    if (visited[e.to]) continue;
    visited[e.to] = true;
    if (e.from >= 0) spanning_tree.emplace_back(e);
    for (const auto& f : g[e.to]) pq.emplace(f);
  }
  return std::move(spanning_tree);
}
}
}
//}}}

using namespace std;

int main() {
  int V = in(), E = in();
  orliv::Graph g(V);
  for (int i = 0; i < E; i++) {
    int s = in(), t = in(), w = in();
    g[s].emplace_back(s, t, w);
    g[t].emplace_back(t, s, w);
  }
  auto es = orliv::graph::prim(g);
  ll tot = 0;
  for (const auto& e : es) {
    tot += e.w;
  }
  cout << tot << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Map = map<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF=1LL<<60;
ll MOD=1000000007;

int main(){
    ll N;
    cin >> N;
    vector<ll> L(N,0);
    vector<ll> R(N,0);
    ll recL=0;ll MaxL=0;
    for(ll i=0;i<N;i++){
        cin>>L[i]>>R[i];
        if(MaxL<L[i]){
            MaxL=L[i];
            recL=i;
        }
    ;}
    if(N==2){
        cout<<R[0]+R[1]-L[0]-L[1]+2<<endl;return 0;
    }
    vector<P> pq(0);
    for(ll i=0;i<N;i++){
        if(i!=recL)pq.push_back(P(R[i],L[i]))
    ;}
    sort(pq.begin(), pq.end());
    vector<ll> lmax(N-1,0);
    ll q=0;
    for(ll i=0;i<N-1;i++){
        q=max(q,pq[i].second);
        lmax[i]=q
    ;}
    //L[recL]が最大
    //最終的にrecL側のRの選考基準をｒとする
    //Rがpq[r]以上は仲間

    ll ans=0;

    //pq[0]が敵の時かつ仲間いるとき
    for(ll r=1;r<=N-2;r++){
        ans=max(ans,max(min(pq[r].first,R[recL])-MaxL+1,0LL)+max(pq[0].first-lmax[r-1]+1,0LL));
    ;}
    //全員敵
    ans=max(ans,max(pq[0].first-lmax[N-2]+1,0LL)+R[recL]-MaxL+1);
    //pq[0]が味方の時;
    ll ans2=0;
    ans2+=max(0LL,min(pq[0].first,R[recL])-MaxL+1);//味方
    ll check;
    for(ll i=0;i<N;i++){
        if(L[i]==pq[0].second&&R[i]==pq[0].first&&i!=recL){check=i;break;}
    ;}
    priority_queue<ll> vec;
    for(ll i=0;i<N;i++){
        if(i!=recL&&i!=check){
            vec.push(R[i]-L[i]+1);
        }
    ;}
    ans2+=vec.top();
    ans=max(ans,ans2);

    cout<<ans<<endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = (int)(j);i <= (int)(k);i ++)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define fix cout<<fixed<<setprecision(20);
const int maxn=(int)2e6+5;
vector<int> E[maxn];
int vis[maxn],ind[maxn],dis[maxn];
int reach[maxn];//longest can reach
inline int id(int x,int y){
    if(x>y)swap(x,y);
    //x--,y--;
    return x+1000*y;
}
int ans=-1;

int dfs(int x){
    //debug(x);
    if(vis[x]==2)return dis[x];
    vis[x]=1;
    for(auto v:E[x]){
        if(vis[v]==1)return -1;

        //dis[v]=dis[x]+1;ans=max(ans,dis[v]);
        int ret=dfs(v);
        if(ret==-1)return -1;
        dis[x]=max(dis[x],ret+1);//每个节点存最大值。

    }

    //debug(dis[x]);debug(x);

    vis[x]=2;
    return dis[x];
}
int main(){
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;cin>>n;
    rep(i,1,n){
        int last=0;
        rep(j,1,n)if(i!=j){
            int x;scanf("%d",&x);
            if(last==0){last=id(i,x);continue;}
            E[last].push_back(id(i,x));ind[id(i,x)]++;
            last=id(i,x);
        }
    }

    rep(i,1,n)rep(j,1,i-1)if(ind[id(i,j)]==0){
       // memset(vis,0,sizeof(vis));
        E[0].push_back(id(i,j));
        /* dis[id(i,j)]=1;
        if(dfs(id(i,j))==0){cout<<-1<<endl;return 0;};
        //debug("--"); */
    }
    dis[0]=-1;
    int ans=dfs(0);

    cout<<ans<<endl;

}
/*
建DAG，判环
topo 可判环（无法进入队列的点），但无法找最长路径
两种环
一种是链环，用
一种是纯环，
3
2 3
1 3
1 2

12-13
12-23
13-23

4
2 3 4
1 3 4
4 1 2
3 1 2

12-13
13-14
12-23
23-24
34-13
13-23
34-14
14-24

 queue<int> Q;
    rep(i,1,n)rep(j,1,i-1)if(ind[id(i,j)]==0)Q.push(id(i,j)),vis[id(i,j)]=1;
    while(!Q.empty()){

        int cur=Q.front();Q.pop();
        debug(cur);debug(vis[cur]);
        for(auto v:E[cur]){
            if(vis[v]){continue;}
            //if(vis[v]==1){cout<<-1<<endl;return 0;}
            ind[v]--;
            if(ind[v]==0)Q.push(v),vis[v]=vis[cur]+1,ans=max(ans,vis[v]);
        }
    }
   // rep(i,1,n)rep(j,1,n)if(i!=j)debug(vis[id(i,j)]),debug(i),debug(j);
 */


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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 1e9 + 7;

struct ad_set
{
	int tag;
	deque<int> que;

	void subt(int x)
	{
		tag += x;
	}

	void push_front(int x)
	{
		que.push_front(x + tag);
	}
	void push_back(int x)
	{
		que.push_back(x + tag);
	}
	int back()
	{
		return que.back() - tag;
	}
	void pop_back()
	{
		que.pop_back();
	}
	int size()
	{
		return que.size();
	}
}tre;

int n;
char s[1000005], t[1000005];
int term[1000005], dest[1000005];
vector<int> occ[26];

int main()
{
	scanf("%d%s%s", &n, s, t);
	rep(i, n) {
		s[i] -= 'a';
		t[i] -= 'a';
	}
	rep(i, n) occ[s[i]].push_back(i);
	int maxt = n - 1;
	for(int i = n - 1; i >= 0; i --) {
		maxt = min(maxt, i);
		vector<int>::iterator pos = upper_bound(occ[t[i]].begin(), occ[t[i]].end(), maxt);
		if(pos == occ[t[i]].begin()) {
			printf("-1\n");
			return 0;
		}
		else maxt = term[i] = *(-- pos);
	}

	rep(i, n) dest[i] = -1;
	term[n] = -1;
	for(int i = n - 1; i >= 0; i --)
	if(term[i] != term[i + 1]) dest[term[i]] = i;

	int ans = 0;
	for(int i = n - 1; i >= 0; i --) {
		if(dest[i] == -1) continue;
		tre.subt(1);
		tre.push_front(i);
		while(tre.size() > 0 && tre.back() >= dest[i]) tre.pop_back();
		tre.push_back(dest[i]);
		ans = max(ans, tre.size());
	}
	printf("%d\n", ans - 1);
	return 0;
}

#include<bits/stdc++.h>

constexpr auto MAXN = 1000000+500;

std::string s,t;
std::queue<int> que;

signed main(void)
{
  int len,down,up; size_t ans = 0;

  std::cin>>len>>s>>t;

  if(s == t){ puts("0"); return 0; }

  down = up = len-1;

  while(down >= 0){
    while(down && t[down-1]==t[down]) --down;
    while(up>=0 && (up>down || s[up]!=t[down])) --up;
    if(up < 0){ puts("-1"); return 0; }

    while(!que.empty() && que.front()-que.size() >= down) que.pop();
    if(up != down) que.push(up);
    ans = std::max(ans,que.size()+1);
    --down;
  }

  std::cout<<ans<<std::endl;

  return 0;
}


#include <stdio.h>

void dividing(int pm, int pe, int pj);

int main(void)
{
    int students_number;
    int pm[10000], pe[10000], pj[10000];
    scanf("%d", &students_number);
    while (1) {
        if (students_number == 0) {
            break;
        } else {
            for (int i = 0; i < students_number; ++i) {
                scanf("%d %d %d", &pm[i], &pe[i], &pj[i]);
            }
            for (int i = 0; i < students_number; ++i) {
                dividing(pm[i], pe[i], pj[i]);
            }
            scanf("%d", &students_number);
        }
    }
    return 0;
}

void dividing(int pm, int pe, int pj)
{
    char rank;
    if (pm == 100 || pe == 100 || pj == 100) {
        rank = 'A';
    } else if (pm + pe >= 90 * 2) {
        rank = 'A';
    } else if (pm + pe + pj >= 80 * 3) {
        rank = 'A';
    } else if (pm + pe + pj >= 70 * 3) {
        rank = 'B';
    } else if (pm + pe + pj >= 50 * 3 && (pm >= 80 || pe >= 80)) {
        rank = 'B';
    } else {
        rank = 'C';
    }
    printf("%c\n", rank);
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=1;i<=n;i++)
const ll MOD = 1000000007;
struct edge{
    int to,weight;
    edge(int to,int weight):to(to),weight(weight){}
};
using Graph = vector<vector<int>>;
using GraphEdge = vector<vector<int>>;
long long modinv(long long a, long long m=MOD) {long long b = m, u = 1, v = 0;while (b) {    long long t = a / b;    a -= t * b; swap(a, b);    u -= t * v; swap(u, v);}u %= m; if (u < 0) u += m;return u;}
//MODの割り算の際にa/＝b;をa *= modinv(b,MOD);とする。
int gcd(int a, int b){if (a%b == 0){return(b);}else{return(gcd(b, a%b));}}
int lcm(int a, int b){return a * b / gcd(a, b);}

int main(){
    int N;cin >> N;
    if(N==2){
        int a,b;cin >> a >> b;
        cout << a + b << endl;
    }else{
        puts("Hello World");
    }
    return 0;
}


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

#define int long long

#define ii pair<int,int>
#define fi first
#define sc second

#define all(x) (x).begin(),(x).end()

void solve() {
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  for (int i = sy; i < ty; i++) cout << 'U';
  for (int i = sx; i < tx; i++) cout << 'R';
  for (int i = sy; i < ty; i++) cout << 'D';
  for (int i = sx; i < tx; i++) cout << 'L';
  cout << 'L';
  for (int i = sy; i <= ty; i++) cout << 'U';
  for (int i = sx; i <= tx; i++) cout << 'R';
  cout << 'D';
  cout << 'R';
  for (int i = sy; i <= ty; i++) cout << 'D';
  for (int i = sx; i <= tx; i++) cout << 'L';
  cout << 'U';
  cout << '\n';
}

signed main() {
#ifdef _DEBUG
  // freopen("in" , "r", stdin );
  // freopen("out", "w", stdout);
#endif
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) solve();
}

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

long long MOD = 1000000007;

int main(){
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    string ans="";
    for(int i=0; i<ty-sy; i++){
        ans+="U";
    }
    for(int i=0; i<tx-sx; i++){
        ans+="R";
    }
    for(int i=0; i<ty-sy; i++){
        ans+="D";
    }
    for(int i=0; i<tx-sx; i++){
        ans+="L";
    }
    ans+="D";
    for(int i=0; i<tx-sx+1;i++){
        ans+="R";
    }
    for(int i=0; i<ty-sy+1; i++){
        ans+="U";
    }
    ans+="L";
    ans+="U";
    for(int i=0; i<tx-sx+1; i++){
        ans+="L";
    }
    for(int i=0; i<ty-sy+1; i++){
        ans+="D";
    }
    ans+="R";
    cout << ans << endl;
}


#include <bits/stdc++.h>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; ++i)
#define ll long long
using namespace std;
bool check(string x, string y) {
  int a = x.size()-1;
  return x[a] == y[0];
}

int main() {
  string a, b, c;
  cin >> a >> b >> c;
  if(check(a, b) && check(b, c)) cout << "YES" << endl;
  else cout << "NO" << endl;
  return 0;
}

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

int main (){
	char a[100];
	char b[100];
	char c[100];
	scanf("%s %s %s", a, b, c);

	int A = strlen(a);
	int B = strlen (b);

	for (int i = 0; i < 10; i++){
		if ( a[A - 1] == b[0] && b[B - 1]== c[0]){
			puts ("YES");
			break;
		}
		else{
			puts ("NO");
			break;
		}
	}

	return 0;
}


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

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<double> ps(n + 1);
  ps[0] = 0;
  double t;
  for (int i = 0; i < n; ++i) {
    cin >> t;
    ps[i + 1] = (t + 1) / 2 + ps[i];
  }

  double result = 0;
  for (int i = k; i <= n; ++i) {
    result = max(result, ps[i] - ps[i - k]);
  }

  cout << std::fixed << std::setprecision(10) << result << endl;
  return 0;
}

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

int main(){
   int n, k;
   cin>>n>>k;
   vector<int> p(n);
   rep(i, n) cin>>p[i];

   vector<int> ki2(n);
   rep(i, n) ki2[i]=(p[i]+1);

   int sum=0;
   rep(i, k) sum+=ki2[i];
   int mx=0;
   for(int i=0; i+k-1<n; i++){
      mx=max(mx, sum);
      if(i+k<n) sum+= -ki2[i] +ki2[i+k];
   }

   double ans=(double)mx/2;
   cout<<std::setprecision(15)<<ans<<endl;
   return 0;
}


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

int main() {
	int r, g;
	cin >> r;
	cin >> g;
	cout << g - r + g << endl;
}

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

int main() {
  // ここにプログラムを追記
  int rating, next_rating, performance;
  cin >> rating >> next_rating;

  performance = 2 * next_rating - rating;
  cout << performance << endl;
}


/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.21 02:11:21
**/

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

int main() {
    int n;cin >> n;
    vector<ll> x(n);
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    n++;
    int l;cin >> l;
    x.push_back(x[x.size()-1] + l);
    vector<int> next(n);
    for (int i = 0; i < n; i++) {
        auto t = upper_bound(x.begin(), x.end(), x[i] + l);
        t--;
        next[i] = t - x.begin();
    }
    vector<vector<int>> doubling(32,vector<int>(n));
    for (int i = 0; i < n; i++) {
        doubling[0][i] = next[i];
    }
    for (int i = 1; i < 32; i++) {
        for (int j = 0; j < n; j++) {
            doubling[i][j] = doubling[i-1][doubling[i-1][j]];
        }
    }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  vector<string> S(n);
  vector<vector<pair<int, int>>> v(4);
  int sum = 0;
  rep(i, n) {
    cin >> S[i];
    int cnt=0, mn=0;
    for(char c: S[i]) {
      cnt += (c=='(' ? 1 : -1);
      mn = min(cnt, mn);
    }
    sum += cnt;
    if(mn == 0 && cnt >= 0) {
      v[0].push_back({mn, cnt});
    } else if(mn < 0 && cnt >= 0) {
      v[1].push_back({mn, cnt});
    } else if(mn == cnt && cnt <= 0) {
      v[2].push_back({mn, cnt});
    } else {
      v[3].push_back({mn, cnt});
    }
  }
  auto no = [&]() {
    cout << "No" << "\n";
    exit(0);
  };
  if(sum != 0) no();
  sum = 0;
  for(auto p: v[0]) {
    sum += p.second;
  }
  sort(all(v[1]), greater<pair<int, int>>());
  for(auto p: v[1]) {
    if(sum + p.first < 0) no();
    sum += p.second;
  }
  sum = 0;
  for(auto p: v[2]) {
    sum += -p.second;
  }
  sort(all(v[3]), [&](pair<int, int> p1, pair<int, int> p2) {
      if(p1.first == p2.first) {
        return p1.second < p2.second;
      }
      return p1.first-p1.second > p2.first-p2.second;
    });
  for(auto p: v[3]) {
    if(sum + p.first-p.second < 0) no();
    sum += -p.second;
  }
  cout << "Yes" << "\n";
  return 0;
}


/*{{{*/
#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 = 2e3+10;
LL dp1[SIZE][SIZE],dp2[SIZE][SIZE];
void pre(){
    dp1[0][0]=1;
    REPP(i,1,SIZE){
        LL now=0;
        REP(j,SIZE){
            ADD(now,dp1[i-1][j]);
            dp1[i][j]=now;
        }
    }
    dp2[0][0]=1;
    REPP(i,1,SIZE){
        LL now=0;
        REP(j,SIZE){
            ADD(now,dp2[i-1][j]*2);
            dp2[i][j]=now-dp2[i-1][j];
            if(dp2[i][j]<0)dp2[i][j]+=MOD;
        }
    }
}
int main(){
    MOD=998244353;
    pre();
    int K,N;
    R(K,N);
    VL res;
    FOR(i,2,K+1){
        int n2=(i-1)/2;
        int n1=K-n2*2-(1-i%2);
        LL me=0;
        if(i&1){
            FOR(k,0,N)ADD(me,dp1[n1][k]*dp2[n2][N-k]);
        }
        else{
            FOR(k,0,N)ADD(me,dp1[n1][k]*dp2[n2][N-k]);
            FOR(k,0,N-1)ADD(me,dp1[n1][k]*dp2[n2][N-1-k]);
        }
        res.PB(me);
    }
    for(int i=SZ(res)-2;i>=0;i--)res.PB(res[i]);
    for(auto x:res)W(x);
    return 0;
}

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

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

const int Maxn = 100005;
const int Maxm = 524288;

int n, x;
int b[Maxn], l[Maxn], u[Maxn];
lli seq[Maxn];
ll cur;
ll st[Maxm], cnt[Maxm];
ll res;

ll Get(int ind, int y)
{
    if (y <= b[ind]) return ll(l[ind]) * y;
    return ll(y - b[ind]) * u[ind] + ll(l[ind]) * b[ind];
}

void Union(int v)
{
    st[v] = st[2 * v] + st[2 * v + 1];
    cnt[v] = cnt[2 * v] + cnt[2 * v + 1];
}

void Create(int v, int l, int r)
{
    if (l == r) { st[v] = seq[l].first; cnt[v] = 1; }
    else {
        int m = l + r >> 1;
        Create(2 * v, l, m);
        Create(2 * v + 1, m + 1, r);
        Union(v);
    }
}

void Update(int v, int l, int r, int x, bool add)
{
    if (l == r)
        if (add) { st[v] = seq[l].first; cnt[v] = 1; }
        else st[v] = cnt[v] = 0;
    else {
        int m = l + r >> 1;
        if (x <= m) Update(2 * v, l, m, x, add);
        else Update(2 * v + 1, m + 1, r, x, add);
        Union(v);
    }
}

lli Get(int v, int l, int r, ll val)
{
    if (val >= 0) return lli(0, 0);
    else if (l == r) return lli(st[v], cnt[v]);
    else {
        lli res = lli(0, 0);
        int m = l + r >> 1;
        if (val + st[2 * v + 1] >= 0)
            return Get(2 * v + 1, m + 1, r, val);
        else {
            res = lli(st[2 * v + 1], cnt[2 * v + 1]);
            lli got = Get(2 * v, l, m, val + st[2 * v + 1]);
            res.first += got.first; res.second += got.second;
            return res;
        }
    }
}

int main()
{
    scanf("%d %d", &n, &x);
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &b[i], &l[i], &u[i]);
        cur -= ll(b[i]) * l[i];
        seq[i] = lli(ll(b[i]) * l[i] + ll(x - b[i]) * u[i], i);
    }
    res = ll(n) * x;
    sort(seq, seq + n);
    Create(1, 0, n - 1);
    for (int i = 0; i < n; i++) {
        Update(1, 0, n - 1, i, false);
        lli got = Get(1, 0, n - 1, cur + seq[i].first);
        int lef = 0, rig = x;
        while (lef <= rig) {
            int mid = lef + rig >> 1;
            if (cur + got.first + Get(seq[i].second, mid) >= 0)
                rig = mid - 1;
            else lef = mid + 1;
        }
        res = min(res, ll(x) * got.second + ll(rig + 1));
        Update(1, 0, n - 1, i, true);
    }
    cout << res << endl;
    return 0;
}


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

int N;
const int MAX_E=100;
int label[MAX_E];
priority_queue<int,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > pq;

int main(){
  cin>>N;
  REP(i,N)
    label[i]=i;
  REP(i,N){
    REP(j,N){
      int a;cin>>a;
      if(a>=0&&i>j){
	pq.push(make_pair(a,make_pair(i,j)));
      }
    }
  }
  int size=0;
  while(!pq.empty()){
    int cost=pq.top().first;
    pair<int,int> e=pq.top().second;
    //printf("%d,%d,%d\n",cost,e.first,e.second);
    pq.pop();
    if(label[e.first]!=label[e.second]){
      int tmp=label[e.second];
      size+=cost;
      REP(i,N){
	if(label[i]==tmp)
	  label[i]=label[e.first];
	//printf("%d ",label[i]);
      }
      //printf("\n");
    }
  }
  printf("%d\n",size);
}

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

using namespace std;

class MCMult
{
public:
  vector<vector<int>> mat;
  int solve();
};

int MCMult::solve()
{
  int ms = mat.size();
  vector<vector<int>> dp(ms, vector<int>(ms, 0));

  for ( auto i = 0; i < ms - 1; ++i )
    dp[i][i+1] = mat[i][0] * mat[i][1] * mat[i+1][1];

  for ( auto k = 2; k < ms; ++k )
    {
      for ( auto i = 0; i + k < ms; ++i )
        {
          int ret = dp[i+1][i+k] + mat[i][0] * mat[i][1] * mat[i+k][1];
          ret = min( ret, dp[i][i+k-1] + mat[i+k][0] * mat[i+k][1] * mat[i][0]);

          for ( auto j = i + 1; j < i + k - 1; ++j )
            {
              ret = min( ret, dp[i][j] + dp[j+1][i+k] + mat[i][0] * mat[j][1] * mat[i+k][1]);
            }
          dp[i][i+k] = ret;
        }
    }

  cout << dp[0][ms-1] << endl;

  return 0;
}

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

  MCMult m;

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

      cin >> x;
      v.push_back( x );
      cin >> x;
      v.push_back( x );

      m.mat.push_back( v );
    }

  m.solve();

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,ll>;
using Tup = tuple<ll,ll,ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout<<(v)<<"\n";
#define fl cout<<flush;
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1000000007;

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N,T;
    string S;
    cin >> T;
    for(ll i=0;i<T;++i){
        cin >> N;
        vector<ll> A(N,0);
        for(ll i=0;i<N;++i)cin>>A[i];
        cin >> S;
        vector<ll> nl={0};
        bool ans=1;
        for(ll j=N-1;j>=0;--j){
            if(S[j]=='0'){
                ll x=A[j];
                for(auto p: nl)chmin(x,x^p);
                for(auto p: nl)chmin(p,x^p);
                if(x)nl.emplace_back(x);
            }
            else{
                ll x=A[j];
                for(auto p: nl)chmin(x,x^p);
                if(x){ans=0;break;}
            }
        }
        if(ans)prt(0)
        else prt(1)
    }
    return 0;
}

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;

#define Pr(f,...) fprintf(stderr,f,##__VA_ARGS__),fflush(stderr)

typedef long long ll;

template<typename T>
void read(T &x){
	x=0;
	char c;
	for(c=getchar();!isdigit(c);c=getchar());
	for(;isdigit(c);c=getchar())x=x*10+c-'0';
}

const int N=205,P=1e9+7,L=62;

int mul(int a,int b){ return 1ll*a*b%P; }
//int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
int gcd(int a,int b){ return !b?a:gcd(b,a%b); }

int n,T;
ll a[N],pr[L+2],bk[L+2],ans;
char s[N];
void add(ll *b,ll x){
	dec(i,L,0){
		if((x>>i)&1){
			if(b[i])x^=b[i];
			else {b[i]=x; break; }
		}
	}
}
bool test(ll *b,ll x){
	dec(i,L,0)if((x>>i)&1){
		x^=b[i];
	}
	return x==0;
}
bool equal(ll *b1,ll *b2){
	dec(i,L,0)if(b1[i]){
		if(!test(b2,b1[i]))return false;
	}
	return true;
}

int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
	freopen("d.in","r",stdin);
#endif
	read(T);
	while(T--){
		read(n);
		rep(i,1,n)read(a[i]);
		scanf("%s",s+1);
		ans=0;
		mem(bk,0);
		dec(i,n,1){
			if(s[i]=='0')add(bk,a[i]);
			else ans|=!test(bk,a[i]);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

signed main(void){
  int i, j, k;
  for(int testcase = 0;testcase >= 0;testcase++){
    int2(n, m);
    int a[n], cnt[n];
    scn(n, a);
    rep(i, n)cnt[i] = 0;

    rep(i, m){
      int(b);
      rep(j, n)if(b >= a[j]){
        cnt[j]++; break;
      }
    }

    int ma = 0;
    rep1(i, n)if(cnt[ma] < cnt[i])ma = i;
    pri(ma + 1);

/*/

//*/ break;
  }
  return 0;
}

#include<iostream>
using namespace std ;
int main()
{
	int n ;
	int a ,b ,c ,d ;
	cin >> n ;
	int t[ 101 ] = { 0 } ;
	for( int i = 0 ; i < n * ( n - 1 ) / 2  ; i++ )
	{
		cin >> a >> b >> c >> d ;

		if( c == d )
		{
			t[ a ]++ ;
			t[ b ]++ ;
			continue ;
		}
		t[ c > d ? a : b ] += 3 ;
	}
	for( int i = 1 ; i <= n ; i++ )
	{
		int juni = 1 ;
		for( int j = 1 ; j <= n ; j++ )
		{
			if( i == j )
				continue ;
			if( t[ i ] < t[ j ] )
			{
				juni++ ;
			}
		}
		cout << juni << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <vector>
#include <functional>

using namespace std;

int main() {
  while( true ) {
	int n, m;
	cin >> n >> m;
	if ( n == 0 && m == 0 ) break;

	vector<int> xs( n );
	for ( int i = 0; i < n; i++ ) {
	  cin >> xs[ i ];
	}
	sort( xs.begin(), xs.end(), greater<int>() );

	int sum = 0;
	for ( int i = 0; i < n; i++ ) {
	  sum += ( (i + 1) % m == 0 ? 0 : xs[ i ] );
	}
	cout << sum << endl;
  }
}

#include <cstdio>

int main(){
	int n, p;
	int x[50];
	while(scanf("%d%d", &n, &p)){
		if(n == 0 && p == 0) break;
		for(int i = 0; i < n; i++) x[i] = 0;
		int t = 0;
		int q = p;
		while(true){
			if(p > 0){
				x[t]++;
				if(x[t] == q){
					printf("%d\n", t);
					break;
				}
				p--;
			} else{
				p += x[t];
				x[t] = 0;
			}
			t++;
			t %= n;
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

double dot(P a,P b){return real(conj(a)*b);};
double cross(P a,P b){return imag(conj(a)*b);};

int main(){
  int n;
  while(~scanf("%d",&n)){
    if(n==0)break;
    ll cur;
    cin>>cur;
    ll mx = cur;
    while(--n){
      ll x;
      cin>>x;
      cur = max(x,cur+x);
      mx = max(mx,cur);
    }
    cout<<mx<<endl;
  }
  return 0;
}

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>

//#define PI 3.141592653589

using namespace std;

int main()
{
	int n;
	int seisuu[5000];
	int wa = 0;
	int wa_max = -100001;

	while (1)
	{
		wa_max = -100001;

		cin >> n;
		if (n == 0) break;

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

		for (int i=0; i<n; i++)
		{
			for (int j=i; j<n; j++)
			{
				wa += seisuu[j];
				wa_max = max(wa_max, wa);
			}
			wa = 0;
		}
		cout << wa_max << endl;

		/*
		for (int i=0; i<n; i++)
		{
			cout << seisuu[i] << " ";
		}
		*/
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()

int main() {
ll N;
  cin>>N;
  vector<vector<pair<ll,ll>>> vec(N,vector<pair<ll,ll>>(0));
  for(ll i=0;i<N;i++) {
ll A;
    cin>>A;
    for(ll j=0;j<A;j++) {
ll x,y;
      cin>>x>>y;
      vec[x-1].push_back(make_pair(i+1,y));
    }
  }

  ll ans=0;
  for(ll tmp=0;tmp<(1<<N);tmp++) {
bitset<15> S(tmp);
    bool a=true;

    for(ll i=0;i<N;i++) {
      ll k;
      if(S.test(i)) {
k=1;
      }
      else {
        k=0;
      }
for(auto x:vec[i]) {
  if(S.test(x.first-1)) {
  if(x.second!=k) {
a=false;
  }
}

}
    }

    if(a) {
      ll sum=S.count();
ans=max(ans,sum);
    }
  }

  cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int         long long
#define endl        "\n"
#define P           pair<int, int>

const int mod = 1e9+7;

inline int add(int a, int b) { int res = a + b; if(res >= mod) res -= mod;  return res; }
inline int sub(int a, int b) { int res = a - b; if(res < 0) res += mod; return res;}
inline int mul(int a, int b) { return (a * b)%mod; }

int modpow(int a, int b) {
    int ans = 1;
    while(b) {
        if(b%2) ans = mul(ans, a);
        b /= 2; a = mul(a, a);
    }   return ans;
}

const int N = 1e5+5;
vector<P> g[N];
int n;

bool cmp1(pair<int, P> a, pair<int, P> b) {
    return a.second.first < b.second.first;
}

bool cmp2(pair<int, P> a, pair<int, P> b) {
    return a.second.second < b.second.second;
}

int MST() {
    priority_queue<P, vector<P>, greater<P>> q;
    int cost = 0;
    q.push({0, 0});
    bool mark[N]; memset(mark, 0, sizeof mark);
    while(!q.empty()) {
        P tp = q.top(); q.pop();
        int cur = tp.second;
        if(mark[cur]) continue;
        cost += tp.first;
        mark[cur] = 1;
        for(auto ne : g[cur]) {
            int node = ne.second;
            if(mark[node] == false) q.push(ne);
        }
    }
    return cost;
}

void solve() {
    cin >> n;
    pair<int, P> a[n];
    for(int i=0; i<n; i++) {
        P p; cin >> p.first >> p.second;
        a[i] = make_pair(i, p);
    }
    sort(a, a+n, cmp1);
    for(int i=1; i<n; i++) {
        int d = a[i].second.first - a[i-1].second.first;
        int u = a[i-1].first, v = a[i].first;
        g[v].push_back({d, u});
        g[u].push_back({d, v});
    }
    sort(a, a+n, cmp2);
    for(int i=1; i<n; i++) {
        int d = a[i].second.second - a[i-1].second.second;
        int u = a[i-1].first, v = a[i].first;
        g[u].push_back({d, v});
        g[v].push_back({d, u});
    }

    cout << MST() << endl;
}

signed main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(12);

    int T = 1;
    //cin >> T;

    while(T--) {
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;
inline void read(long long &x) {
    char ch;
    bool flag = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
    for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}
inline void write(long long x) {
    static const long long maxlen = 100;
    static char s[maxlen];
    if (x < 0) {   putchar('-'); x = -x;}
    if (!x) { putchar('0'); return; }
    long long len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0';
    for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}

inline void read(int &x){
    char ch;
    bool flag = false;
    for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true;
    for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const long long MAXN =1001000;

long long n;
struct kt{
long long x,y;
long long id;
}a[ MAXN ];

long long fa[ MAXN ];

struct edge{
long long x,y,val;
}E[ MAXN*2 ];

bool cmp_x(kt A,kt B){
return A.x<B.x;
}

bool cmp_y(kt A,kt B){
return A.y<B.y;
}

bool cmp_e(edge A,edge B){
return A.val<B.val;
}

long long get_fa(long long x){
return (fa[x]==x)?(x):(fa[x]=get_fa(fa[x]));
}

int main(){
    read(n);
    for (long long i=1;i<=n;i++)
    {
        a[i].id=i;
        read(a[i].x);read(a[i].y);
    }
    sort(a+1,a+n+1,cmp_x);
    long long cnt=0;
    for (long long i=1;i<n;i++)
        {
            ++cnt;
            E[cnt].x=a[i].id;
            E[cnt].y=a[i+1].id;
            E[cnt].val=a[i+1].x-a[i].x;
        }

    sort(a+1,a+n+1,cmp_y);
    for (long long i=1;i<n;i++)
        {
            ++cnt;
            E[cnt].x=a[i].id;
            E[cnt].y=a[i+1].id;
            E[cnt].val=a[i+1].y-a[i].y;
        }
    for (long long i=1;i<=n;i++)
        fa[i]=i;

    sort(E+1,E+cnt+1,cmp_e);
    long long ans=0;
    for (long long i=1;i<=cnt;i++)
        {
            long long a=E[i].x,b=E[i].y;
            long long x=get_fa(a),y=get_fa(b);
            if ( x!=y )
                {
                    ans+=E[i].val;
                    fa[x]=y;
                }
        }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

const int N = 108;

int n;
int a[N], b[N];

int main() {
	int i, mid;
	scanf("%*d%d", &n);
	for (i = 0; i < n; ++i) scanf("%d", a + i);
	if (n == 1) return *a == 1 ? puts("1\n1\n1") : printf("%d\n2\n1 %d\n", *a, *a - 1), 0;
	mid = std::partition(a, a + n, [] (const int x) -> bool {return x % 2;}) - a;
	if (mid > 2) return puts("Impossible"), 0;
	a[n] = *a;
	for (i = 1; i <= n; ++i) printf("%d%c", a[i], i == n ? 10 : 32);
	memcpy(b + 1, a + 1, n << 2), ++b[1], --b[n], n -= !b[n];
	printf("%d\n", n);
	for (i = 1; i <= n; ++i) printf("%d%c", b[i], i == n ? 10 : 32);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
void read(pp& x){ scanf("%d%d",&x.first, &x.second); }
void read(pll& x){ scanf("%lld%lld",&x.first, &x.second); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define sz(x) (int)(x).size()

int n, m;

vector<int> ao, ae;

void Answer(vector<int> A, vector<int> B){
	vector<int> br;
	for(int x:B) if(x) br.pb(x);
	for(int x:A) printf("%d ", x); putchar(10);
	printf("%d\n", sz(br));
	for(int x:br) printf("%d ", x);
	exit(0);
}

int main()
{
	read(n, m);
	rep(i, m){
		int x; read(x);
		if(x&1) ao.pb(x); else ae.pb(x);
	}
	if(ao.size() > 2u){
		puts("Impossible"); return 0;
	}
	if(ao.size() == 0u){
		if(ae.size() == 1u){
			Answer(ae, {1, ae[0]-1});
		} else {
			vector<int> bt = ae;
			bt[0]--; bt.back()++;
			Answer(ae, bt);
		}
	} else if(ao.size() == 1u){
		if(ae.size() == 0u){
			Answer(ao, {1, ao[0]-1});
		} else {
			vector<int> a;
			a.pb(ao[0]); a.insert(a.end(), all(ae));
			vector<int> bt = a;
			bt[0]--; bt.back()++;
			Answer(a, bt);
		}
	} else if(ao.size() == 2u){
		vector<int> a;
		a.pb(ao[0]); a.insert(a.end(), all(ae)); a.pb(ao[1]);
		vector<int> bt = a;
		bt[0]--; bt.back()++;
		Answer(a, bt);
	}
	return 0;
}


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

int main(){
	int N;
	cin >> N;
	vector<ll> A(N),S(N+1);
	for(int i=0;i<N;i++){
		cin >> A[i];
		S[i+1] = S[i]+A[i];
	}
	ll ans = 1e18;
	auto sum = [&](int l,int r){
		return S[r]-S[l];
	};
	int l = 1,r = 3;
	for(int i=1;i<N;i++){
		while(true){
			ll s1 = abs(sum(l,0)-sum(i,l));
			ll s2 = abs(sum(l+1,0)-sum(i,l+1));
			if(s1>s2) l++;
			else break;
		}
		while(true){
			ll s1 = abs(sum(N,r)-sum(r,i));
			ll s2 = abs(sum(N,r+1)-sum(r+1,i));
			if(s1>s2) r++;
			else break;
		}
		ll ma = max({sum(l,0),sum(i,l),sum(r,i),sum(N,r)});
		ll mi = min({sum(l,0),sum(i,l),sum(r,i),sum(N,r)});
		ans = min(ans,ma-mi);
	}
	cout << ans << endl;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <limits>
#include <set>
#include <map>
#include <functional>

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

using namespace std;
typedef long long ll;

#ifndef PRId64
#define PRId64 "lld"
#endif
#ifndef SCNd64
#define SCNd64 "lld"
#endif
#define SC sc()
#define PT pr()
#define FORi(i,b,e) for (int i = (b), _ = (e); i < _; ++i)
#define FORe(i,b,e) for (int i = (b), _ = (e); i <= _; ++i)
#define FORre(i,b,e) for (int i = (b), _ = (e); i >= _; --i)

static int sc_ret = 0;
struct sc
{
    sc& operator()(char& v) { v = sc_ret = getchar(); return *this; }
    sc& operator()(int& v) { sc_ret = scanf("%d", &v); return *this; }
    sc& operator()(unsigned& v) { sc_ret = scanf("%u", &v); return *this; }
    sc& operator()(double& v) { sc_ret = scanf("%lf", &v); return *this; }
    sc& operator()(char* v) { sc_ret = scanf("%s", v); return *this; }
    sc& operator()(string& v) { sc_ret = (bool)(cin >> v); return *this; }
    sc& operator()(ll& v) { sc_ret = scanf("%" SCNd64, &v); return *this; }
    sc& ch(char& v) { sc_ret = scanf(" %c", &v); return *this; }
    sc& gets(char* v) { sc_ret = fgets(v, INT_MAX, stdin) != 0; v[strlen(v) - 1] = 0; return *this; }
    operator bool() const { return sc_ret > 0; }
    void read(int& v)
    {
        int x = 0, k = 1;
        char c = getchar();
        while (c < '0' || c > '9')
        {
            if (c == '-') k = -1;
            c = getchar();
        }
        while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c - 48), c = getchar();
        v = x * k;
    }
};
struct pr
{
    pr& ln() { putchar('\n'); return *this; }
    pr& operator()(char v) { putchar(v); return *this; }
    pr& operator()(int v) { printf("%d", v); return *this; }
    pr& operator()(double v) { printf("%.2f", v); return *this; }
    pr& operator()(const char* fmt, double v) { printf(fmt, v); return *this; }
    pr& operator()(const char* v) { printf("%s", v);return *this; }
    pr& operator()(string v) { printf("%s", v.c_str());return *this; }
    pr& operator()(ll v) { printf("%" PRId64, v);return *this; }

    void write(int v)
    {
        int cnt = 0; char c[15];
        if (v == 0)
        {
            putchar('0');
            return;
        }
        if (v < 0) putchar('-'), v = -v;
        while (v) c[++cnt] = (v % 10) + 48, v /= 10;
        while (cnt > 0) putchar(c[cnt--]);
    }
    template <typename T>
    void ln(T* arr, int size)
    {
        if (size > 0)
        {
            (*this)(arr[0]);
            for (int i = 1; i < size; ++i)
            {
                putchar(' ');
                (*this)(arr[i]);
            }
            putchar('\n');
        }
    }

    template <typename T>
    void muln(T* arr, int size)
    {
        for (int i = 0; i < size; ++i)
        {
            (*this)(arr[i]);
            putchar('\n');
        }
    }
};

const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 200000 + 10;
const int maxm = 100000 + 10;

ll a[maxn];

int main()
{
    int n, t, k;
    SC(n);
    FORe(i, 1, n)
    {
        int v;
        SC.read(v);
        a[i] = v + a[i - 1];
    }
    int l = 1, r = 3; ll ret = a[n];
    FORi(i, 2, n - 1)
    {
        for (; l < i - 1; ++l)
        {
            if (min(a[l], a[i] - a[l]) >= min(a[l + 1], a[i] - a[l + 1]))
                break;
        }
        for (; r < n - 1; ++r)
        {
            if (min(a[r] - a[i], a[n] - a[r]) >= min(a[r + 1] - a[i], a[n] - a[r + 1]))
                break;
        }
        ret = min(ret, max(max(a[r] - a[i], a[n] - a[r]), max(a[l], a[i] - a[l]))
            - min(min(a[l], a[i] - a[l]), min(a[r] - a[i], a[n] - a[r])));
    }
    PT(ret);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

using ll   = long long;
using ii   = pair   < int , int >;
using i3   = pair   < int , ii  >;
using li   = pair   < ll  , int >;
using lii  = pair   < ll  , ii  >;
using pll  = pair   < ll  , ll  >;
using vi   = vector < int       >;
using vl   = vector < ll        >;
using vii  = vector < ii        >;
using vli  = vector < li        >;
using vpll = vector < pll       >;
using vi3  = vector < i3        >;
using vlii = vector < lii       >;

const int N = 2e5 + 5;
const ll INF = 1e17 + 7;
const double eps = 1e-9 , PI = acos(-1);

int n , m;

void solve(int testCase){

    double d , t , s;
    scanf("%lf %lf %lf" , &d , &t , &s);

    if(s * t >= d) {
            printf("Yes\n");
    }
    else {
            printf("No\n");
    }
}

main(){

    int t = 1;
    // scanf("%d" , &t);

    for(int testCase = 1 ; testCase <= t ; testCase ++){
            solve(testCase);
    }

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define repr(i, n, m) for (int i = (int)(n) - 1; i >= (int)(m); i--)
using namespace std;
using ll = int64_t;
const int MOD = 1000000007; // 10^9+7

int main() {
    double n, m, d;
    cin >> n >> m >> d;

    double ans;
    if (d == 0) {
        ans = (m - 1) / n;
    } else {
        ans = 2 * (n - d) * (m - 1) / (n * n);
    }
    cout << fixed << setprecision(10) << ans << endl;
}


#include <iostream>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

#define rep(i,n) for(int i=0; i < n; i++)
using ll =  long long;
using namespace std;
#define pi 3.14159265358979

// 回文チェック関数

bool Pal(string a)
{
	int size = a.length();
	rep(i, size / 2) if (a[i] != a[size - i - 1]) return false;
	return true;
}

int main()
{
	int a, b, count = 0;
	cin >> a >> b;

	for (int i = a; i < b + 1; i++) if (Pal(to_string(i))) count++;

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

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n)for(int i=0;i<(int)(n);i++)
#define PI 3.141592653589793
//vector < vector<int>>a(0, vector<int>(0));二次元配列宣言

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


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define REP(i,n) for(auto i = 0 * n; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)

class UnionFind {
	typedef size_t U;
	vector<int32_t> v;
public:
	UnionFind(U n) : v(n, -1) {}
	U find(U x) { return v[x] < 0 ? x : (v[x] = find((U)v[x])); }
	bool same(U x, U y) { return find(x) == find(y); }
	U size(U x) { return (U)-v[find(x)]; }
	void unite(U x, U y) {
		x = find(x); y = find(y);
		if (x != y) {
			if (-v[x] < -v[y]) swap(x, y);
			v[x] += v[y];
			v[y] = x;
		}
	}
};

struct Edge{ uint32_t to, id; };
typedef vector<vector<Edge>> Graph;

template<class T> struct Kruskal{
	typedef uint32_t U;
	template<class V> Kruskal(U n, const V & u, const V & v, const vector<T> & w) : g(n) {
		vector<U> es(w.size());
		iota(es.begin(), es.end(), 0);
		sort(es.begin(), es.end(), [&](U i, U j) { return w[i] < w[j]; });
		UnionFind uf(n);
		for (U i : es) {
			if (!uf.same(u[i], v[i])) {
				uf.unite(u[i], v[i]); cost += w[i];
				g[u[i]].push_back({ (U)v[i], i }); g[v[i]].push_back({ (U)u[i], i });
			}
		}
	}
	Graph g; T cost = 0;
};

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

	ll n; cin >> n;
	vl u, v, w;
	REP(i, n) REP(j, n) {
		ll x; cin >> x;
		if (x != -1) {
			u.push_back(i);
			v.push_back(j);
			w.push_back(x);
		}
	}
	Kruskal<ll> mst(n, u, v, w);
	cout << mst.cost << endl;
}

#include<bits/stdc++.h>
#define inf 10000000
using namespace std;
int main(){
  int n,d[101][101],a[101],ans=0,c[101]={};
  cin>>n;
  for(int i=0;i<n;i++)a[i]=inf;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++){
      cin>>d[i][j];
      if(d[i][j]==-1)d[i][j]=inf;
    }
  a[0]=0;
  int s=0;
  while(1){
    c[s]=1;
    ans+=a[s];
    int mn=inf,ns=-1;
    for(int i=0;i<n;i++)
      a[i]=min(a[i],d[s][i]);

    for(int i=0;i<n;i++)
      if(!c[i]&&a[i]<mn)ns=i,mn=a[i];

    if(ns==-1)break;
    s=ns;
  }
  cout<<ans<<endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
template<class T> void operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) cin >> e; }

typedef long long ll;
int const inf = 1<<29;

int main() {

  int M, N[2];
  for(; cin >> M >> N[0] >> N[1] && M;) {
    vector<int> vs(M); cin >> vs;
    int maxgap = 0;
    int maxnum = -1;
    REP(num, N[0], N[1] + 1) {
      if(maxgap <= vs[num - 1] - vs[num]) {
        maxgap = vs[num - 1] - vs[num];
        maxnum = num;
      }
    }

    cout << maxnum << endl;
  }

  return 0;
}

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPEQ(i, n) FOREQ(i, 0, n)
#define ALL(c) (c).begin(), (c).end()

template <typename T> inline int size(const T& x) { return x.size(); }

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

    int m, n_min, n_max;
    while (cin >> m >> n_min >> n_max, m | n_min | n_max) {

        vector<int> P(m);
        REP(i, m) cin >> P[i];

        int max_gap = 0, ans = -1;
        FOR(i, n_min-1, n_max) {
            int gap = P[i] - P[i+1];
            if (gap >= max_gap) {
                max_gap = gap;
                ans = i+1;
            }
        }

        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
template<typename T> bool chmax(T &a, T b) {if(a <= b){a = b; return true;}return false;}
template<typename T> bool chmin(T &a, T b) {if(a >= b){a = b; return true;}return false;}

#define ABC 001
#define ARC 010
#define AGC 100

int main(void){
    int r;
    cin >> r;
    int frag = AGC;
    if(r < 1200){
        frag |= ABC;
        frag |= ARC;
    }else if( r < 2800){
        frag |= ARC;
    }
    if(frag & ABC)cout << "ABC" << endl;
    else if(frag & ARC)cout << "ARC" << endl;
    else cout << "AGC" << endl;
}

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

int main(void){
    int r; cin >> r;
  	if(r < 1200){
    	cout << "ABC" << endl;
      	return 0;
    }
  	else if(r < 2800){
    	cout << "ARC" << endl;
      	return 0;
    }
  	else{
    	cout << "AGC" << endl;
      	return 0;
    }
}


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

int main(){
  int n;  cin >> n;
  int table[2][100005] = {0};

  for(int i=0; i<n; i++){
    int v;  cin >> v;
    table[i%2][v]++;
  }

  vector<pair<int, int>> ruia, ruib;
  for(int i=0; i<1e5+5; i++){
    if(table[0][i])  ruia.push_back({table[0][i], i});
    if(table[1][i])  ruib.push_back({table[1][i], i});
  }
  sort(ruia.rbegin(), ruia.rend());
  sort(ruib.rbegin(), ruib.rend());

  long ans = 0;
  if(ruia[0].second == ruib[0].second){
    if(ruia.size() == 1 && ruib.size() == 1)
      ans = n / 2;
    else if(ruia.size() == 1)
      ans = n/2 - ruib[1].first;
    else if(ruib.size() == 1)
      ans = n/2 - ruia[1].first;
    else
      ans = min(n/2-ruib[0].first+n/2-ruia[1].first, n/2-ruia[0].first+n/2-ruib[1].first);
  }else{
    ans = n/2-ruia[0].first + n/2-ruib[0].first;
  }

  cout << ans << endl;

  return 0;
}

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

const ll MOD = 998244353;

ll cal(int a, int r){
  int p = 1;
  rep(i,r){
    p*=a;
    p%=MOD;
  }
  return p;
}

main(){
  int n;
  cin>>n;
  map<int,int> mp;
  int bottom=0;
  bool corner=0;
  rep(i,n){
    int d;
    cin>>d;
    if(i==0 && d!=0) corner=1;
    if(i!=0 && d==0) corner=1;
    mp[d]++;
    bottom=max(bottom,d);
  }
  if(corner) cout<<0<<endl;
  else{
    ll ans=1;
    for(int i = 1; i <= bottom; i++){
      if(mp[i]==0){
        cout<<0<<endl;
        return 0;
      }
      ans *= cal(mp[i-1],mp[i]);
      ans%=MOD;
    }
    cout<<ans<<endl;
  }
}


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

int n, p, ans;
long long sum[100005], top, d, m, v, a[100005];

bool cmp(int a, int b) { return a > b; }

int main() {
	cin>>n>>m>>v>>p;
	v = n - v;
	top = v*m;
	for(int i = 1; i <= n; i++)
		scanf("%lld", &a[i]);
	sort(a + 1, a + n + 1, cmp);
	for(int i = 1; i <= n; i++)
	    sum[i] = sum[i - 1] + a[i];
	ans = p;
	for(int i = p + 1; i <= n; i++) {
		d =  sum[i - 1] - sum[p - 1] - a[i]*(long long)(i - p);
		if(d > top) continue;
		if(a[p] - a[i] > m) continue;
		ans++;
	}
	cout<<ans<<endl;
}

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

#pragma warning(disable : 4996)

using namespace std;

int n, bit[262145];

vector<int> a, b;

void add(int i, int x)
{
	while (i <= n)
	{
		bit[i] += x; i += i & -i;
	}
}

int sum(int i)
{
	int ret = 0;

	while (i > 0)
	{
		ret += bit[i]; i -= i & -i;
	}

	return ret;
}

int sum(int l, int r)
{
	return sum(r - 1) - sum(l - 1);
}

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

	a.resize(n);

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

	b = a; sort(b.begin(), b.end());

	long long ret = 0;

	for (int i = 0; i < n; i++)
	{
		int ptr = lower_bound(b.begin(), b.end(), a[i]) - b.begin(); add(ptr + 1, 1);

		ret += sum(ptr + 2, n + 1);
	}

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

	return 0;
}


#include <iostream>
#include <vector>

std::vector<int> BIT;//その位置までの良い石の数の区間和
std::vector<int> color;
std::vector<int> right;//色番号ごとの良い石の場所

int RegionSum(int l,int r);
int getSum(int r);
void addBIT(int idx,int value);

int main(){
    int N,Q;
    std::cin >> N >> Q;
    BIT.resize(N+1,0);
    color.resize(N+1,0);
    right.resize(N+1,-1);

    for(int i=1;i<=N;i++){
        std::cin >> color[i];
    }

    std::vector<std::vector<int>> QueryL_(N+1);
    std::vector<std::vector<int>> QueryID(N+1);

    for(int i=0;i<Q;i++){
        int l,r;
        std::cin >> l >> r;
        QueryID[r].push_back(i);
        QueryL_[r].push_back(l);
    }

    std::vector<int> ans(Q);

    //BITの更新と答えの取得。BITは左から順番にi番目を更新していき更新するごとにiを右端とするクエリの答えを取得する
    for(int i=1;i<=N;i++){
        //BITの更新
        if(right[color[i]] != -1)
            addBIT(right[color[i]],-1);//すでにcolor[i]の良い石がある場合は前回までの良い石の位置までのBITを1減らす
        right[color[i]] = i;
        addBIT(i,1);
        //クエリごとの答えの取得
        for(int j=0;j<QueryL_[i].size();j++){
            ans[QueryID[i][j]] = RegionSum(QueryL_[i][j],i);//QueryL_[i][j]:区間右側をiとするクエリ集合のj番目のクエリのl
        }
    }

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

    return 0;
}

int RegionSum(int l,int r){
    return getSum(r) - getSum(l-1);
}

int getSum(int r){
    int Sum=0;
    for(int i=r;i>0;i-=i&(-i)){
        Sum += BIT[i];
    }
    return Sum;
}

void addBIT(int idx,int value){
    for(int i=idx;i<=BIT.size()-1;i+=i&(-i)){
        BIT[i] += value;
    }
    return;
}


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

int main()
{
	int n,k;
	cin>>n>>k;
	int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n,greater<int>());
	int result{};
	for(int i=0;i<k;i++)
		result += arr[i];
	cout<<result;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k,x; cin>>k>>x; k--;
  for(int i=x-k;i<=x+k;i++) cout<<i<<" ";
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int N = 2e6 + 5;
struct ed{
	int to, nxt;
}e[N << 1];
int head[N], tot = 1;
void add(int u, int v){
	e[tot].to = v, e[tot].nxt = head[u], head[u] = tot++;
}

int n, s, t, dep[N], mx[N], fa[N], ans, tp;
void dfs(int x, int last){
	fa[x] = last;
	dep[x] = dep[last] + 1;
	mx[x] = dep[x];
	for (int i = head[x]; i; i = e[i].nxt){
		int v = e[i].to;
		if (v == last)	continue;
		dfs(v, x);
		mx[x] = max(mx[x], mx[v]);
	}
}
int  main()
{
	scanf("%d%d%d", &n, &s, &t);
	for (int i = 0; i < n - 1; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		add(x, y);
		add(y, x);
	}
	dfs(t, 0);
	t = 1;
	ans = (dep[s] - dep[t]) >> 1;
	while (true){
		if (dep[s] <= t)	break;
		if ((dep[s] - dep[t]) & 1)
			ans = max(ans, mx[s] - t - 1 + tp);
		else
			ans = max(ans, mx[s] - t - 1 + tp);
		s = fa[s];
		t++;
		tp++;
	}
	printf("%d\n", ans);
}

#pragma GCC optimize("O3")
//#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
	int N;
	cin >> N;
	vector<vector<pair<int, int>>> person(N, vector<pair<int, int>>());
	for (int i = 0; i < N; i++) {
		int A;
		cin >> A;
		for (int j = 0; j < A; j++) {
			int x, y;
			cin >> x >> y;
			x--;
			person[i].push_back({ x,y });
		}
	}
	int ans = 0;
	//bitが立っている人が正直
	for (int i = 0; i < (1 << N); i++) {
		bool flag = true;
		int cnt = 0;
		for (int j = 0; j < N; j++) {
			if ((i & (1 << j)) == 0)continue;
			cnt++;
			for (int k = 0; k < person[j].size(); k++) {
				if (((i & (1 << person[j][k].first)) != 0) != person[j][k].second)flag = false;
			}
		}
		if (flag)ans = max(ans, cnt);
	}
	cout << ans << endl;
}


#include<iostream>
using namespace std;

bool search(int);

int q,T[500];

int main(){

  int n,S[10000],counter=0;

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

  cin >>q;
  for(int i=0 ; i < q ; i++)cin >>T[i];

  for(int i=0 ; i < n ; i++)counter+=search(S[i]);

  cout <<counter<<endl;

  return 0;
}

bool search(int s){

  for(int i=0 ; i < q ; i++){
    if( s == T[i] ){
      T[i]=-1;
      return 1;
    }
  }

  return 0;

}

#include <bits/stdc++.h>
using namespace std;
int main(){
  int H,W; cin>>H>>W;
  queue<int>A;
  queue<int>B;
  int ans=0;
  vector<vector<char>>C(H+2,vector<char>(W+2,'#'));
  for(int i=1;i<=H;i++){
    string S;cin>>S;
    for(int j=1;j<=W;j++){C[i][j]=S.at(j-1);if(C[i][j]=='#'){ans++;}}
  }
  A.push(1);
  B.push(1);
  vector<vector<bool>>Z(H,vector<bool>(W,false));
  Z[0][0]=true;
  int count=1;
  for(int i=2;;i++){
    int z=count;
    count=0;
    for(int j=0;j<z;j++){
      if(C[A.front()+1][B.front()]=='.'&&!Z[A.front()][B.front()-1]){count++;Z[A.front()][B.front()-1]=true;A.push(A.front()+1);B.push(B.front());if(A.front()+1==H&&B.front()==W){cout<<H*W-ans-i<<endl;return 0;}}
      if(C[A.front()-1][B.front()]=='.'&&!Z[A.front()-2][B.front()-1]){count++;Z[A.front()-2][B.front()-1]=true;A.push(A.front()-1);B.push(B.front());if(A.front()-1==H&&B.front()==W){cout<<H*W-ans-i<<endl;return 0;}}
      if(C[A.front()][B.front()+1]=='.'&&!Z[A.front()-1][B.front()]){count++;Z[A.front()-1][B.front()]=true;A.push(A.front());B.push(B.front()+1);if(A.front()==H&&B.front()+1==W){cout<<H*W-ans-i<<endl;return 0;}}
      if(C[A.front()][B.front()-1]=='.'&&!Z[A.front()-1][B.front()-2]){count++;Z[A.front()-1][B.front()-2]=true;A.push(A.front());B.push(B.front()-1);if(A.front()==H&&B.front()-1==W){cout<<H*W-ans-i<<endl;return 0;}}
      A.pop();B.pop();
    }
    if(count==0){cout<<-1<<endl; return 0;}
  }
}


#include <bits/stdc++.h>

using namespace std;

static const int INF = 99999;

int main(){

	int n,array[100][100];

	cin>>n;

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

		fill(array[i],array[i]+n,INF);

	}

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

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

			int tmp;

			cin>>tmp;

			if(tmp!=-1) array[i][j]=tmp;

		}

	}

	int ans=INF;

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

		bool flag[100];

		fill(flag,flag+n,false);

		flag[k]=true;

		int tmp=0;

		while(1){

			int u=-1,minv=INF;

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

				if(!flag[i]) continue;

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

					if(!flag[j] && array[i][j]<=minv){

						minv = array[i][j];

						u = j;

					}

				}

			}

			if(minv==INF){

				break;

			}else{

				tmp += minv;

				flag[u]=true;

			}

		}

		ans = min(ans,tmp);

	}

	cout<<ans<<endl;

	return 0;

}

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

int main(){
	int n;
    cin>>n;
    int ar[n];
    int count=0;
   for(int i=0;i<n;i++){
   cin>>ar[i];
  }
for(int i=0;i<n-2;i++){
   for(int j=i+1;j<n-1;j++){
     for(int k=j+1;k<n;k++){
       if(ar[i]+ar[j]>ar[k] && ar[i]+ar[k]>ar[j] && ar[j]+ar[k]>ar[i] && ar[i]!=ar[j] && ar[j]!=ar[k] && ar[i]!=ar[k]){
     count++;
         }
     }
  }
}
   cout<<count;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<functional>
#include<climits>
#include<stack>
#include<queue>
#include<cmath>
#define int long long
#define INF 100000000000
#define EPS 0.0000000001

using namespace std;

signed main() {
	int n, m;
	int a[1010], b[1010];
	int ans[1010] = { 0 };
	int vote = -1;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> b[i];
	}
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			if (a[j] <= b[i]) {
				ans[j]++;
				//cout << j + 1 << endl;
				break;
			}
		}
	}
	int temp = 0;
	for (int i = 0; i < n; i++) {
		//for (int j = 0; j < n; j++) {
			//cout << ans[i] << " " << endl;
			if (temp < ans[i]) {
				//cout << "[" << i << "]" << endl;
				temp = ans[i];
				vote = i;
			}
		//}
	}
	cout << vote + 1 << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

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();}
template<class T> inline T sqr(T x) {return x*x;}

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

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 24;

int main(void){
    for(int n, m; cin >> n >> m;){
        vi a(n); for(auto && aa : a) cin >> aa;
        vi b(m); for(auto && bb : b) cin >> bb;

        vi v(n);
        for(int bb : b){
            rep(i, n){
                if(a[i] <= bb){
                    v[i]++;
                    break;
                }
            }
        }

        cout << max_element(all(v)) - v.begin() + 1 << endl;
    }

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
//#define int long long
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr int inf = 1000000000;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

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

	int n, m, q;
	cin >> n >> m >> q;
	vector<string> a(n);
	rep(i, n) cin >> a[i];
	vector<vector<int>> sum(n + 1, vector<int>(m + 1));
	vector<vector<int>> sum2(n + 1, vector<int>(m + 1));
	vector<vector<int>> sum3(n + 1, vector<int>(m + 1));
	rep(i, n) {
		rep(j, m) {
			sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j];
			sum2[i + 1][j + 1] = sum2[i + 1][j] + sum2[i][j + 1] - sum2[i][j];
			sum3[i + 1][j + 1] = sum3[i + 1][j] + sum3[i][j + 1] - sum3[i][j];
			if (a[i][j] == '1') sum[i + 1][j + 1]++;
			if (i != 0 && a[i - 1][j] == '1'&&a[i][j] == '1') sum2[i + 1][j + 1]++;
			if (j != 0 && a[i][j - 1] == '1'&&a[i][j] == '1') sum3[i + 1][j + 1]++;
		}
	}
	rep(_, q) {
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		x1--; y1--;
		int ans = sum[x1][y1] + sum[x2][y2] - sum[x1][y2] - sum[x2][y1];
		ans -= sum2[x1 + 1][y1] + sum2[x2][y2] - sum2[x1 + 1][y2] - sum2[x2][y1];
		ans -= sum3[x1][y1 + 1] + sum3[x2][y2] - sum3[x1][y2] - sum3[x2][y1 + 1];
		cout << ans << '\n';
	}
}


#include<bits/stdc++.h>
using namespace  std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int x,y,z;
    while(cin>>x>>y>>z)
    {
        cout<<z<<' '<<x<<' '<<y<<' '<<endl;
    }
    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

void abc_160a();

void abc_161a();

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

}

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

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

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

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

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

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


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

int n;
vector <long long> L;

void solve(){
    sort(all(L));
    int ans = 0;

    for(int i=0; i<=n-3; i++){
        for(int j=i+1; j<=n-2; j++){
            for(int k=j+1; k<=n-1; k++){
                if(L[i] != L[j] && L[j] != L[k]){
                    if(L[k] < L[i] + L[j]){
                        ans ++;
                    }
                }
            }
        }
    }

    cout << ans << endl;
}

int main(){
    cin >> n;
    rep(i,n){
        int l;
        cin >> l;
        L.push_back(l);
    }

    solve();
}

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
using namespace std;

int main(){
  long long N;
  cin >> N;
  vector<long long> a(N+1,0);
  for(int i = 1;i < N + 1;i++){
    cin >> a.at(i);
  }
  vector<bool> b(N + 1,false);
  long long M = 0;
  for(long long i = N;i >= 1;i--){
    long long c = 0;
    for(long long j = 2*i;j <= N;j+= i){
      c += b.at(j);
    }
    b.at(i) = (c % 2 != a.at(i));
    M += b.at(i);
  }
  cout << M << endl;
  for(int i = 1;i < N + 1;i++){
    if(b.at(i)){
      cout << i << " ";
    }
  }
  cout << endl;
}

/*
int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  vector<int> b(N+1);
  for(int i = 0;i < N;i++){
    cin >> a.at(i);
    b.at(i) = 0;
  }
  b.at(N+1) = 0;
  int M = 0;
  /*for(int i = N - 1;i >= 0;i--){
    int count = 0;
    for(int j = i;j < N;j++){
      count += b.at(j);
    }
    if(count%2 != a.at(i)){
      b.at(i) = abs(a.at(i) - count);
      M ++;
    }
  }
  for(int i = N - 1;i >= 0;i--){
    int count = 0;
    for(int j = 2*i;j <= N;j+=i){
      count += b.at(j);
    }
    if(count%2 != a.at(i)){
      b.at(i) = abs(a.at(i) - count);
      M ++;
    }
  }
  cout << M << endl;
  if(M != 0){
  for(int i = 0;i < N;i++){
    if(b.at(i) != 0){
      cout << i + 1 << " ";
    }
  }
  cout << endl;
}
}
*/


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

int main() {
  int N, a;
  cin >> N;
  a=(N-N%111)/111;
  if (N%111==0) {
    cout << N << endl;
  }
  else {
    cout << 111*(a+1) << endl;
  }
}

#include <bits/stdc++.h>

using namespace std;

long long n,i;

int main()
{
    cin >> n;
    for(i=111; i<=999; i+=111)
        if(i>=n) {
            cout<<i<<endl;
            break;
        }

    return 0;
}


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

int main(){
  int A, B;
  cin>>A>>B;
  int sum=0;
  for(int i=A;i<=B;i++){
    if((i%10==i/10000)&&((i%100)/10==(i/1000)%10)){
      sum+=1;
    }
  }
  cout<<sum<<endl;
}

#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>

using namespace std;

int main() {
  int a,b,num = 0;
  cin >> a >> b;

  for(int i = 0; a <= b; i++) {
    bool flag = 0;
    string s = to_string(a);
    int n = s.size();
    for(int j = 0; j < double(n/2); j++) {
      if(s[j] == s[n-1-j])
        flag = 1;
      else {
        flag = 0;
        break;
      }
    }
    if(flag) num++;
    a++;
  }
  cout << num << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <numeric>
#include <algorithm>
using namespace std;

int N, Q;
int main() {
    while(cin >> N >> Q, N||Q){
        int num[101];
        for(int i=0; i<101; ++i) num[i]=0;
        for(int i=0; i<N; ++i){
            int M; cin >> M;
            for(int j=0; j<M; ++j){
                int a; cin >> a;
                num[a]++;
            }
        }
        bool ok=false;
        int maxV = 0;
        int ans=0;
        for(int i=0; i<101; ++i){
            if(num[i]>=Q){
                if(maxV<num[i]) {
                    ans = i;
                    maxV = num[i];
                }
                ok =true;
            }
        }
        if(!ok) cout << 0 << endl;
        else cout << ans << endl;
    }
    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;

int main(){

	int N,Q,M,Date[101],i,l,k;

	while(0<=scanf("%d%d",&N,&Q)){
		for(i=0;i<101;i++){
			Date[i]=0;
		}

		if((N==0) && (Q==0)){
			break;
		}

		for(k=0;k<N;k++){
			scanf("%d",&M);
			if(M==0){
				continue;
			}else{
				for(i=0;i<M;i++){
					scanf("%d",&l);
					++Date[l];
				}
			}
		}
		l=0;
		i=0;
		for(k=0;k<100;k++){
			if(i<Date[k] && Date[k]>=Q){
				i=Date[k];
				l=k;
			}
		}

		printf("%d\n",l);
	}

	return 0;
}


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

const ll MMM = -1e18;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	int a[n];
	for (int i=0; i<n; i++) cin >> a[i];
	int m = n/2;
	if (n&1) {
		ll ans = MMM;
		vector<ll> suff(m+1), pref(m+1), best_suff(m+1);
		// both end case
		for (int i=0; i<m; i++) {
			pref[i+1] = pref[i] + a[2*i];
		}
		for (int i=m-1; i>=0; i--) {
			suff[i] = suff[i+1] + a[2*i+2];
		}
		// single 3-jump
		for (int i=0; i<=m; i++) {
			ans = max(ans, pref[i] + suff[i]);
		}

		// double 2-jump
		for (int i=m-1; i>=0; i--) {
//			best_suff[i] = max(best_suff[i+1] + a[i*2+1], suff[i+1] + a[i*2+1]);
			best_suff[i] = max(best_suff[i+1], suff[i+1]) + a[i*2+1];
		}

		bool dbg = false;
		if (dbg) {
			cout << "PREF "; for (ll x : pref) cout << x << ' '; cout << '\n';
			cout << "SUFF "; for (ll x : suff) cout << x << ' '; cout << '\n';
			cout << "BEST "; for (ll x : best_suff) cout << x << ' '; cout << '\n';
		}

		ll best_pref = 0;
		ans = max(ans, best_suff[0]);
		for (int i=0; i<m; i++) {
			best_pref = max(best_pref, pref[i]) + a[i*2+1];
			ans = max(ans, best_pref + best_suff[i+1]);
		}
//		ans = max(ans, max(best_pref, pref[

		// one end case
		// should have covered by double 2-jump

		// neither end case
		ll tmp = 0;
		for (int i=0, idx=1; i<m; i++, idx+=2) tmp += a[idx];
		ans = max(ans, tmp);
		cout << ans;
	}
	else {
		vector<ll> suff(m+1);
		for (int i=m-1; i>=0; i--) {
			suff[i] = suff[i+1] + a[2*i+1];
		}
		ll ans = suff[0];
		ll pref = 0;
		for (int i=0; i<m; i++) {
			pref += a[i*2];
			ans = max(ans, pref + suff[i+1]);
		}
		cout << ans;
	}
}

#include <iostream>
using namespace std;
const long long OO = (long long) 1e18;

const int N = 200008;
int n;
long long a[N];

long long dp[N][3];
// dp[i][j] is maximum possible sum choosing (i+1-j)/2 elements from first i
// elements. This is NOT a floor! So half the dp elements will be impossible
// solely because it's impossible to choose half an element.

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

	dp[0][0] = -OO;
	dp[0][1] = 0;
	dp[0][2] = -OO;
	dp[1][0] = a[0];
	dp[1][1] = -OO;
	dp[1][2] = 0;

	for (int i = 2; i <= n; i++) {
		// note that we are computing dp[i] using a[i-1] and dp[i-1]
		for (int j = 0; j < 3; j++) {
			// take a[i-1]
			dp[i][j] = dp[i-2][j] + a[i-1];

			if (j) {
				// we have slack; don't take a[i-1]
				dp[i][j] = max(dp[i][j], dp[i-1][j-1]);
			}
		}
	}

	cout << dp[n][n % 2 == 0 ? 1 : 2];
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(v) v.begin(), v.end()
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))

using ll = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vll = vector<ll>;
using vvll = vector<vll>;

int main()
{
  int H, W,inf=100000000,ans=0;
  cin>>H>>W;
  vvc field(H, vc(W));
  vvi d(H, vi(W, inf));
  queue<PAIR> que;
  rep(i,H)
  {
    rep(j,W)
    {
      cin >> field[i][j];
      if (field[i][j]=='#')
      {
        que.push(PAIR(i, j));
        d[i][j] = 0;
      }
    }
  }

  while(que.size())
  {
    int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
    PAIR p = que.front();
    que.pop();

    rep(i, 4)
    {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if(0<=nx&&nx<H&&0<=ny&&ny<W&&field[nx][ny]=='.'&&d[nx][ny]==inf)
        {
          d[nx][ny] = d[p.first][p.second] + 1;
          que.push(PAIR(nx, ny));
          ans = max(ans, d[nx][ny]);
        }
    }
  }
  cout << ans << endl;
}

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

int main(void)
{
    long long n, m;

    while (scanf("%lld %lld", &n, &m), n){
        long long p[1024];

        for (int i = 0; i < n; i++) scanf("%lld", p + i);
        p[n++] = 0;

        vector<long long> score;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (!binary_search(score.begin(), score.end(), p[i] + p[j])) score.push_back(p[i] + p[j]);
            }
        }
        sort(score.begin(), score.end());
        score.erase(unique(score.begin(), score.end()), score.end());

        long long maxi = 0;
        for (auto i : score){
            if (i > m) continue;
            maxi = max(maxi, min(*(upper_bound(score.begin(), score.end(), m - i) - 1) + i, m));
        }

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

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
typedef pair<LL,int> LP;
const int INF=1<<30;

void array_show(int *a,int n){
	for(int i=0;i<n;i++)printf("%d%c",a[i],(i!=n-1?' ':'\n'));
}
void array_show(LL *a,int n){
	for(int i=0;i<n;i++)printf("%lld%c",a[i],(i!=n-1?' ':'\n'));
}

int t[100005];
int bef[100005];
bool used[100005];
vector<int> path_rev[100005];
vector<int> v1;
vector<P> v2;

void check(int a,int m){
	v1.push_back(a);
	if(v1.size()>=m){
		bef[a]=v1[v1.size()-m];
	}
	v2.push_back(make_pair(v1.size()-1,a));
	for(int i=0;i<path_rev[a].size();i++){
		check(path_rev[a][i],m);
	}
	v1.pop_back();
}

void set_used(int a){
	if(used[a])return;
	used[a]=true;
	for(int i=0;i<path_rev[a].size();i++){
		set_used(path_rev[a][i]);
	}
}

int main(){
	int n,m;
	int i,j,k;
	int a,b,c;
	int s=0;
	cin>>n>>m;
	memset(t,-1,sizeof(t));
	for(i=0;i<n;i++){
		cin>>t[i];
		t[i]--;
		if(i==0){
			if(t[i]!=0)s++;
			continue;
		}
		path_rev[t[i]].push_back(i);
	}
	check(0,m);
	sort(v2.rbegin(),v2.rend());
	for(i=0;i<n;i++){
		if(v2[i].first<=m)break;
		j=v2[i].second;
		if(used[j])continue;
		s++;
		set_used(bef[j]);
	}
	cout<<s<<endl;
	return 0;
}

/* be name khoda */

// #define stream_enable
#define long_enable

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

// #include <bits/stdc++.h>
// #include <numeric>
// #include <iomanip>
// #include <cmath>
// #include <bitset>
// #include <fstream>

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<vi> vii;
typedef vector<pii> vpii;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define debug(x) cout << #x << " -> " << (x) << endl;
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl;
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl;
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl;
#define debugp(x) cout << #x << " -> " << "(" << (x).ff << ", " << (x).ss << ")" << endl;
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].ff << ", " << (x)[i1_dap].ss << ")" << endl; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_da].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

#define forifrom(i, s, n) for(ll i = (s); i < (n); ++i)
#define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define Add(a, b) a = (a + (b)) % MOD
#define Mul(a, b) a = (a * (b)) % MOD

stringstream sss;

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

const ll maxn = 100010;

ll n, k;
vi subt[maxn];
ll H[maxn];
ll ans = 0;

ll dfs(ll x) {
    ll mx = 0;
    for (auto y : subt[x]) {
        H[y] = H[x] + 1;
        smax(mx, dfs(y));
    }
    if (mx == k - 1 && H[x] > 1) {
        ++ans;
        return 0;
    } else {
        return mx + 1;
    }
}

void MAIN() {

    cin >> n >> k;
    ll p;
    cin >> p;
    ans += p != 1;
    fori (i, n - 1) {
        ll p;
        cin >> p;
        subt[p - 1].pb(i + 1);
    }
    dfs(0);
    cout << ans << '\n';

}

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // cout << fixed << setprecision(10);
    sss << R"(
8 2
4 1 2 3 1 2 3 4
    )";
    MAIN();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,n,j) for(int i=(j);i<(n);++i)
#define ssort(n) sort((n).begin(),(n).end())
#define rsort(n) sort((n).begin(),(n).end(),greater<int>())
using ll=long long;
using ld=long double;
typedef pair<int,int> P;
typedef pair<P,int> COST;
#define repl(i,n) for(ll i=0;i<(n);++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf_char=vector<vector<char>>;
using Graf_int=vector<vector<int>>;
#define MAX 100000

int main()
{
    int n,m;
    cin >> n >> m;
    vector<int> s(m),c(m);
    rep(i,m){
        cin >> s[i] >> c[i];
    }
    vector<int> num(n,-1);
    rep(i,m){
        if(num[s[i]-1]!=-1){
            if(num[s[i]-1]!=c[i]){
                cout << -1 << endl;
                return 0;
            }
        }
        else{
            num[s[i]-1]=c[i];
            if(s[i]-1==0&&c[i]==0&&n>1){
                cout << -1 << endl;
                return 0;
            }
        }
    }
    rep(i,n){
        if(n>1&&i==0&&num[i]==-1){
            cout << 1;
        }else if(num[i]==-1){
            cout << 0;
        }else{
            cout << num[i];
        }
    }
    cout << endl;
}


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

using namespace std;

#define rep(i,N) for(int i=0;i<N;i++) //0から
#define REP(i,N) for(int j=i;j<N;j++) //iから

typedef long long ll;

ll binary_search(vector<ll> a, ll n, ll key){
    ll right = n, left = -1;
    ll md = (right + left) / 2;

    while(right - left > 1){
        if(a[md] <= key){
            right = md;
        }else{
            left = md;
        }
        md = (right + left) / 2;
    }
    if(left == -1) return -1; //無い場合
    return right;
}

vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
    vector<ll> p(n,0);
    p[0]=1;
    p[1]=1;
    for(ll i=2;i<n;i++){
        if(p[i]==0){
            prime.push_back(i);
            p[i]=i;
        }
        ll k=prime.size();
        for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
            p[i*prime[j]]=prime[j];
        }
    }
}

#define MOD ((ll)1e+9 + 7)

//cout<<fixed<<setprecision(10);
int main(){
    int n,m;
    cin>>n>>m;
    vector<int> L(m),R(m+1);
    rep(i,m){
        cin>>L[i]>>R[i];
        L[i]--;
    }
    vector<int> cnt(n+1,0);
    rep(i,m){
        cnt[L[i]]++;
        cnt[R[i]]--;
    }
    rep(i,n){
        cnt[i+1]+=cnt[i];
    }
    int ans=0;
    rep(i,n){
        if(cnt[i]==m){
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}

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

int main() {
	// your code goes here
	int l, r, n , m =0;

	cin>>n>>m;
	int mi= n;
	int ma=-1;

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

		cin>>l>>r;

		if(ma < l)ma= l;
		if(mi > r)mi=r;

	}

	cout<<max(mi-ma+1,0);

	return 0;
}