#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define EPS 1e-9
//#define PI 3.14159265358979

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

using namespace std;

template < typename T >
ostream &operator<<(ostream &os, const vector< T > &A) {
	for (int i = 0; i < A.size(); i++)
		os << A[i] << " ";
	os << endl;
	return os;
}
template <>
ostream &operator<<(ostream &os, const vector< vector< int > > &A) {
	int N = A.size();
	int M;
	if (N > 0)
		M = A[0].size();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++)
			os << A[i][j] << " ";
		os << endl;
	}
	return os;
}

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

struct edge {
	int from, to, d, c;
	edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
		from = _from;
		to = _to;
		d = _d;
		c = _c;
	}
	bool operator<(const edge &rhs) const {
		return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
	}
};
struct aabb {
	int x1, y1, x2, y2;
	aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};

typedef vector< edge > edges;
typedef vector< edges > graph;
struct flow {
	int to, cap, rev, cost;
	flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector< vector< flow > > flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

int pow2(int n) { return 1LL << n; }
template < typename T, typename U >
bool chmin(T &x, const U &y) {
	if (x > y) {
		x = y;
		return true;
	}
	return false;
}
template < typename T, typename U >
bool chmax(T &x, const U &y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}

struct initializer {
	initializer() {
		cout << fixed << setprecision(20);
	}
};
initializer _____;

int N, M, K, T, Q, H, W;
struct event {
	int t, x, d;
};
bool operator<(const event &lhs, const event &rhs) {
	if (lhs.t != rhs.t)
		return lhs.t < rhs.t;
	return lhs.d < rhs.d;
}
signed main() {
	cin >> N >> Q;
	vector< event > A(N * 2);
	rep(i, N) {
		int s, t, x;
		cin >> s >> t >> x;
		A[i * 2].t = s - x;
		A[i * 2].x = x;
		A[i * 2].d = 1;
		A[i * 2 + 1].t = t - x;
		A[i * 2 + 1].x = x;
		A[i * 2 + 1].d = -1;
	}
	sort(A.begin(), A.end());
	set< int > s;
	vector< int > D(Q);
	rep(i, Q) cin >> D[i];
	int j = 0;
	rep(i, Q) {
		int q = D[i];
		while (j < N * 2 && A[j].t <= q) {
			if (A[j].d == 1) {
				s.insert(A[j].x);
			} else {
				s.erase(A[j].x);
			}
			j++;
		}
		if (!s.empty()) {
			cout << (*(s.begin())) << endl;
		} else {
			cout << -1 << endl;
		}
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define INF 1000000007
#define llINF 1000000000000000007
#define v(i) vector<i>
#define vv(i) vector<vector<i>>
// cin.eof() 入力個数の問題
// int num = atoi(string.c_str()); 文字列の整数変換

int main() {
    int h, w;
    cin >> h >> w;
    char s[h+1][w+1];
    int cnt = 0;
    int visited[h+1][w+1];
    int a[] = {1,0,-1,0};
    int b[] = {0,1,0,-1};
    rep(i, h) {
        rep(j, w) {
            cin >> s[i][j];
            if(s[i][j] == '.')
                cnt++;
                visited[i][j] = -1;
        }
    }
queue<pair<int,int>> que;
visited[0][0] = 0;
que.push(make_pair(0,0));
while(!que.empty()){
  pair<int,int> now = que.front();
    //int ny=que.front().first,nx=que.front().second;
    que.pop();
  int ny=now.first,nx=now.second;

    rep(i,4){
        int y = ny+a[i],x = nx + b[i];
        if(y<0 || y>=h || x<0 || x>=w || visited[y][x] != -1) continue;
        if(s[y][x] == '#') continue;
        visited[y][x] = visited[ny][nx] + 1;
        que.push(make_pair(y,x));
    }
}
if(visited[h-1][w-1] == -1) cout<<-1<<endl;
else
cout<<cnt - visited[h-1][w-1] - 1<<endl;
    return 0;
}


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

int main() {
	int ining;
	int ten[50];
	int base[3];
	char act[8];
	int outCnt = 0;

	for(int i=0;i<50;i++){
		ten[i] = 0;
	}

	for(int i=0;i<3;i++){
		base[i] = 0;
	}

	scanf("%d",&ining);

	for(int i=0;i<ining;i++){
		while(outCnt < 3){
			scanf("%s",act);
			if(strcmp(act,"HIT") == 0) {
				ten[i] = ten[i] + base[2];
				base[2] = base[1];
				base[1] = base[0];
				base[0] = 1;
			} else if(strcmp(act,"HOMERUN") == 0) {
				for(int l=0;l<3;l++){
					ten[i] = ten[i] + base[l];
					base[l] = 0;
				}
				ten[i] = ten[i] + 1;
			} else if(strcmp(act,"OUT") == 0) {
				outCnt++;
			}
		}
		outCnt = 0;
		for(int i=0;i<3;i++){
			base[i] = 0;
		}
	}

	for(int i=0;i<ining;i++)
		printf("%d\n",ten[i]);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
using ll=long long;
typedef pair<int,int> P;
const int INF=1e9;
int main(){
    string s;
    cin>>s;
    rep(i,1<<3){
        int sum=0;
        sum+=s[0]-'0';
        for(int j=1;j<4;j++){
            if(i>>(j-1)&1) sum+=s[j]-'0';
            else sum-=s[j]-'0';
        }
        if(sum==7){
            cout<<s[0];
            rep(j,3){
                if(i>>j&1) cout<<'+';
                else cout<<'-';
                cout<<s[j+1];
            }
            break;
        }
    }
    cout<<"=7";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100003,INF=1<<30;
int N,K,maxi=0,ans=0;
vector<int> G[MAX],dis(MAX);

void DFS(int u,int p){
    for(int i=0;i<G[u].size();i++){
        int a=G[u][i];
        if(a==p) continue;
        DFS(a,u);
        dis[u]=max(dis[u],dis[a]+1);
    }
    if(u==0) return;

    if(p==0){
        if(dis[u]==K){
            ans++;
            dis[u]=-1;
        }
    }else{
        if(dis[u]==K-1){
            ans++;
            dis[u]=-1;
        }
    }
}

int main(){

    cin>>N>>K;
    int a;cin>>a;
    if(a!=1) ans++;
    for(int i=1;i<N;i++){
        int a;cin>>a;
        a--;
        G[a].push_back(i);
    }
    DFS(0,-1);

    cout<<ans<<endl;

}

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N,K;

int ans[100010];
vector<int> g[100010];
bool used[100010];
int dist[100010];

vector<int> rg[100010];

void dfs(int now,int len){
    used[now]=true;
    dist[now]=len;
    for(int i=0;i<g[now].size();i++){
        int next=g[now][i];
        if(used[next]) continue;

        dfs(next,len+1);
    }
    return;
}

int rdfs(int now,int len){
    used[now]=true;
    if(len==K-1) return now;
    for(int i=0;i<rg[now].size();i++){
        int next=rg[now][i];
        if(used[next]) continue;

        return rdfs(next,len+1);
    }
    return -1;
}

void visit(int now){
    used[now]=true;
    for(int i=0;i<g[now].size();i++){
        int next=g[now][i];
        if(used[next]) continue;

        if(dist[next]>dist[now]) visit(next);
    }
    return;
}

int main(){
  cin>>N>>K;
  int a[N+1]={};
  for(int i=0;i<N;i++){
      cin>>a[i];
      a[i]--;
      ans[i]=a[i];
      if(i>0){
          g[a[i]].push_back(i);
          rg[i].push_back(a[i]);
      }
  }
  ans[0]=0;

  dfs(0,0);

  priority_queue<pair<int,int>> PQ;
  for(int i=0;i<N;i++){
      used[i]=false;
      PQ.push(make_pair(dist[i],i));
  }
  while(!PQ.empty()){
      pair<int,int> tp=PQ.top();
      PQ.pop();
      int now=tp.second;
      if(used[now]) continue;

      int x=rdfs(now,0);
      if(x!=-1){
          ans[x]=0;
          visit(x);
      }
  }

  int res=0;
  for(int i=0;i<N;i++){
      if(ans[i]!=a[i]) res++;
  }
  cout<<res<<endl;

  return 0;
}


#include <cstdio>
#include <algorithm>
#define repu(i,x,y) for (int i=x; i<=y; ++i)
#define repd(i,x,y) for (int i=x; i>=y; --i)
using namespace std;

int n,m,len[2010],l[2010],r[2010],stk[2010],top,ans;
char a[2010][2010];

int main()
{
    scanf("%d%d",&n,&m);
    repu(i,1,n)
        scanf("%s",a[i]+1);
    ans=m;
    repu(i,1,m)
    {
        stk[top=0]=0;
        repu(j,1,n-1)
        {
            len[j]=i>1 && (a[j][i]==a[j+1][i])==(a[j][i-1]==a[j+1][i-1])?len[j]+1:1;
            for (; top && len[j]<=len[stk[top]]; --top);
            l[j]=stk[top],stk[++top]=j;
        }
        stk[top=0]=n;
        repd(j,n-1,1)
        {
            for (; top && len[j]<=len[stk[top]]; --top);
            r[j]=stk[top],stk[++top]=j;
            ans=max(ans,len[j]*(r[j]-l[j]));
        }
    }
    printf("%d\n",ans);
    return 0;
}

#include <bits/stdc++.h>

#define For(i, a, b) for (register int i = (a), i##_end = (b); i <= i##_end; ++ i)
#define getc getchar_unlocked
#define inf (0x3f3f3f3f)
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define y1 igniubi

using namespace std;

inline int read() {
	int _ = 0, __ = getc(), ___ = 1;
	for(; !isdigit(__); __ = getc()) if(__ == '-') ___ = -1;
	for(; isdigit(__); __ = getc()) _ = (_ << 3) + (_ << 1) + (__ ^ 48);
	return _ * ___;
}

const int N = 2e3 + 10;

int n, m, a[N][N], b[N][N], f[N][N], S[N], w[N], ans, top;

int main() {
	//freopen("speech.in", "r", stdin);
	//freopen("speech.out", "w", stdout);

	n = read(), m = read();
	For(i, 1, n) {
		char ss[N];
		scanf("%s", ss + 1);
		For(j, 1, m) {
			a[i][j] = ss[j] == '#';
			if(i > 1 && j > 1 && !((a[i][j] + a[i - 1][j] + a[i][j - 1] + a[i - 1][j - 1]) & 1))
				b[i - 1][j - 1] = 1;
		}
	}
	For(i, 1, n) For(j, 1, m) if(b[i][j]) f[i][j] = f[i][j - 1] + 1;

	For(j, 1, m) {
		top = 0;
		For(i, 1, n) {
			int kuan = 0;
			for(; top && f[S[top]][j] >= f[i][j]; -- top) {
				kuan += w[S[top]];
				ans = max(ans, min(n, kuan + 1) * min(m, f[S[top]][j] + 1));
			}
			w[i] = kuan + 1, S[++ top] = i;
		}
		int kuan = 0;
		for(; top; -- top) {
			kuan += w[S[top]];
			ans = max(ans, min(n, kuan + 1) * min(m, f[S[top]][j] + 1));
		}
	}

	printf("%d\n", max(max(n, m), ans));

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<int> a(n);
    for (int i=0; i<n; i++) cin>>a[i];
    int ans = 0;
    sort(a.begin(), a.end());
    for (int i = 0; i<n-2; i++) {
        int j1 = i+1;
        while (j1 < n-1 && a[j1] == a[i]) j1++;
        for (int j=j1; j<n-1; j++) {
            int k1 = j+1;
            while (k1<n && a[k1] == a[j]) k1++;
            for (int k = k1; k<n; k++) {
                if (a[k] < a[i] + a[j]) ans++;
                else break;
            }
        }
    }
    cout << ans << '\n';
    return 0;
}

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define  fi0 for(ll i=0;i<n;i++)
string s;
string strd(int n){
    string x;
    while(n){
        int d=n%10;
        x+=(d+'0');
        n/=10;
    }
    reverse(x.begin(),x.end());
    return x;
}
void digit()
{
    cin>>s;
    ll sum=0;
    for(int i=0;i<s.size();i++){
        char a=(char)s[i];
        int d=(int) a-'0';
        sum+=d;
    }
    if(sum%9==0)cout<<"Yes\n";
    else cout<<"No\n";
}
int main()
{
   ll n;
   cin>>n;
   ll cnt=0;
   ll a[n];
  fi0{
    cin>>a[i];
   }

   sort(a,a+n);
   ///fi0 cout<<a[i]<<' ';
   for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
        for(int k=j+1;k<n;k++){
             if(a[i]!=a[j]&&a[j]!=a[k]){
                vector<ll>xxx;
                xxx.push_back(a[i]);
                xxx.push_back(a[j]);
                xxx.push_back(a[k]);
                sort(xxx.begin(),xxx.end());
                if(xxx[0]+xxx[1]>xxx[2])cnt++;
             }
        }
    }
   }
   cout<<cnt<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int n,m,p1,p2,mn,mx;
int main() {
    cin>>n>>m; mx=1; mn=n;
    for (int i=0; i<m; i++) {
        cin>>p1>>p2;
        mx=max(mx,p1);
        mn=min(mn,p2);
    }
    cout<<max(0,mn-mx+1)<<'\n';
}

#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e5)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) a.begin(),a.end()
using namespace std;

int main() {
	FIO;
	int t = 1;
	//cin >> t;
	while (t--) {
		ll n,m;
		cin >> n >> m;
		vl v(n,0);
		for(int i=0;i<m;i++){
			ll l,r;
			cin >> l >> r;
			l--;
			v[l]++;
			if(r < n) v[r]--;
		}
		ll ans = 0;
		if(v[0] == m) ans++;
		for(int i=1;i<n;i++){
			v[i] += v[i-1];
			if(v[i] == m) ans++;
		}
		cout << ans << "\n";
	}

	return 0;
}


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

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(A v) {
bool first = true; string res = "{";
for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); }
res += "}"; return res; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

vector<PII> G[1005][1005];
int dist[1005][1005];
bool visited[1005][1005];
bool cycleFound;

int dfs(PII v) {
    if (visited[v.first][v.second]) {
        cycleFound = true;
        return 0;
    }
    if (SZ(G[v.first][v.second]) == 0) {
        return 1;
    }
    visited[v.first][v.second] = true;
    if (dist[v.first][v.second] != -1) return dist[v.first][v.second];
    int d = 0;
    for (auto p : G[v.first][v.second]) {
        d = max(d, dfs(p));
    }
    visited[v.first][v.second] = false;
    return dist[v.first][v.second] = d + 1;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int N;
    cin >> N;
    int totalM = (N * (N - 1)) / 2;
    vector<VI> V(N + 1, VI(N - 1));
    FOR(i, 1, N) REP(j, N - 1) cin >> V[i][j];
    memset(dist, -1, sizeof(dist));
    FOR(i, 1, N) {
        REP(j, N - 2) {
            int p = V[i][j];
            int q = V[i][j + 1];
            G[min(i, p)][max(i, p)].pb({min(i, q), max(i, q)});
        }
    }

    int d = 0;

    FOR(i, 1, N) {
        int p = V[i][0];
        d = max(d, dfs({min(i, p), max(i, p)}));
        if (cycleFound) {
            cout << "-1\n";
            return 0;
        }
    }
    cout << d << "\n";

}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main()
{
   int x, y, n, m, xi, yi, xmax=-100, ymin=100;
   cin>>n>>m>>x>>y;
   for (int i = 0; i < n; i++)
   {
       cin>>xi;
       xmax = max(xmax, xi);
   }
   for (int i = 0; i < m; i++)
   {
       cin>>yi;
       ymin = min(ymin, yi);
   }
   if(max(x,xmax) < min(y,ymin)){
       cout<<"No War";
   }
   else
   {
       cout<<"War";
   }

   return 0;
}


#include <iostream>
#include <vector>
#include<math.h>
#include <algorithm>
using namespace std;
int main()
{
int a;
long long k;
cin>>a;
k=a+a*a+a*a*a;
cout<<k;
}

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int a;
	cin>>a;
	a=a+pow(a,2)+pow(a,3);
	cout<<a;
	}


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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
    #define gc getchar()
    template <typename T>
    inline void read(T& x) {
        x = 0; bool f = 1; char ch;
        for (ch = gc; ch < '0' || '9' < ch; ch = gc) if (ch == '-') f ^= 1;
        for (; '0' <= ch && ch <= '9'; ch = gc) x = (x << 3) + (x << 1) + (ch ^ 48);
        x = f ? x : -x;
    }
    #undef gc
}

const int MAXN = 2e5 + 10;

int n, arr[2][MAXN], cnt[2][MAXN], val;
vector<int> pos;

int main() {
    IO::read(n);
    rep(_, 0, 1) rep(i, 1, n) IO::read(arr[_][i]), cnt[_][arr[_][i]]++;
    reverse(arr[1] + 1, arr[1] + n + 1);
    rep(i, 1, n) if (arr[0][i] == arr[1][i]) val = arr[0][i], pos.push_back(i);
    int l = 1, r = n; bool flg = false;
    for (auto cur_pos : pos) {
        if (flg == false) {
            if (arr[0][l] != val && arr[1][l] != val) {
                swap(arr[1][l], arr[1][cur_pos]);
                ++l;
            } else flg = true;
        }
        if (flg == true) {
            if (arr[0][r] != val && arr[1][r] != val) {
                swap(arr[1][r], arr[1][cur_pos]);
                --r;
            } else return puts("No"), 0;
        }
    }
    puts("Yes");
    rep(i, 1, n) printf("%d ", arr[1][i]);
    return 0;
}

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#define int long long
#define endl '\n'
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define line cout<<"here - "<<__LINE__<<"\n";
#define dbg(a) cout<<#a<<" --> "<<(a)<<"\n";
#define infi INT_MAX
#define rinfi INT_MIN
#define inf LLONG_MAX
#define rinf LLONG_MIN
#define ff first
#define ss second
using namespace std;
main()
{
    fast
    int tc=1,i,j,k;
//    cin>>tc;
    while(tc--)
    {
       int a;
       cin>>a;
       int aa[a];
       map<int,int>mp;
       for(i=0;i<a;i++)
        cin>>aa[i],mp[aa[i]]++;
        sort(aa,aa+a);
        vector<int>tmp;
        for(i=a-1;i>=0;i--)
        {
            while(mp[aa[i]]>=2)
            {
                tmp.push_back(aa[i]);
                mp[aa[i]]-=2;
            }
        }
        if(tmp.size()<2)
            cout<<0<<endl;
        else cout<<tmp[0]*tmp[1]<<endl;

    }

}


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

int main (){
  int n, m;
  int b, count, answer;
  int *list, *result;
  list = (int*)malloc(sizeof(int)*n);
  result = (int*)malloc(sizeof(int)*n);

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

  for(int i= 0; i< m; i++){
    count = 0;
    cin >>b;
    while(1){
      if(list[count] < b+1) break;
      count++;
    }
    //cout <<count <<endl ;
    result[count] ++;
  }
  answer = 0;
  for(int i =0; i<n; i++){
    if(result[answer] < result[i])
      answer = i ;
  }

  cout << answer + 1 <<endl;
  return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(I, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ll = long long;

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

  vector<int> l(m,0);
  vector<int> r(m,0);
  vector<bool> can(n,true);

  REP(i,m)  cin >> l[i] >> r[i];

  sort(l.begin(), l.end());
  sort(r.begin(), r.end());

  REP(i,n){
    if(i < (l[m-1]-1) || (r[0]-1) < i){
      can[i] = false;
    }
  }

  int ans = 0;
  REP(i,n){
    if(can[i] == true){
      ans++;
    }
  }

  cout << ans << endl;

}

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

int main(){
    int n,m,maxl=0,minr=1000000;
    cin >> n >> m;
    vector<int> l(m),r(m);
    for(int i=0;i<m;i++){
        cin >> l.at(i) >> r.at(i);
        if(l.at(i)>maxl)maxl=l.at(i);
        if(r.at(i)<minr)minr=r.at(i);
    }
    if(minr-maxl+1>0)cout << minr-maxl+1 << endl;
    else cout << 0 << endl;
    return 0;
}


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

int n,a[1010][1010],h[1000100],dist[1000100];
vector<int>to[1000100];

int encode(int i, int j){
  if(i>j)swap(i,j);
  return i*n+j;
}

int main(){
  cin>>n;
  rep(i,n)rep(j,n-1){
    cin>>a[i][j];
    a[i][j]--;
  }
  rep(i,n)rep(j,n-2){
    int v=encode(i,a[i][j]);
    int u=encode(i,a[i][j+1]);
    to[v].push_back(u);
    h[u]++;
  }
  stack<int>st;
  rep(i,n*n)if(h[i]==0)st.push(i),dist[i]=1;
  while(st.size()){
    int v=st.top(); st.pop();
    for(int u:to[v]){
      h[u]--;
      if(h[u]==0)st.push(u);
      dist[u]=max(dist[u],dist[v]+1);
    }
  }
  int mx=0,in=0;
  rep(i,n*n)mx=max(mx,dist[i]);
  rep(i,n*n)in=max(in,h[i]);
  if(in!=0)cout<<-1<<endl;
  else cout<<mx<<endl;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int maxn = 200005;
const int inf = 1000000007;
int x[maxn],yy[maxn];
char c[maxn];
vector<pair<int,int>>vv1;
vector<int>uu,rr,dd,ll;
int cnt = 0 ;
map<int,int>UL,UR,DL,DR,D,L;
vector<int>GG[5*maxn];
void addnodeUL(int num,int y){if(!UL[num])UL[num]=++cnt;GG[UL[num]].push_back(y);}
void addnodeUR(int num,int y){if(!UR[num])UR[num]=++cnt;GG[UR[num]].push_back(y);}
void addnodeDL(int num,int x){if(!DL[num])DL[num]=++cnt;GG[DL[num]].push_back(x);}
void addnodeDR(int num,int x){if(!DR[num])DR[num]=++cnt;GG[DR[num]].push_back(x);}
void addnodeD(int num,int y){if(!D[num])D[num]=++cnt;GG[D[num]].push_back(y);}
void addnodeL(int num,int x){if(!L[num])L[num]=++cnt;GG[L[num]].push_back(x);}
int query(int num,int num2,int mul){
    int ind=lower_bound(GG[num].begin(),GG[num].end(),num2)-GG[num].begin();
    if(ind!=GG[num].size())
        return mul*(GG[num][ind]-num2);
    return inf;
}
int query2(int num,int num2,int mul){
    int ind=lower_bound(GG[num].begin(),GG[num].end(),num2)-GG[num].begin();
    if(ind-1>=0)
        return mul*(num2-GG[num][ind-1]);
    return inf;
}
int queryUL(int num,int y){
    if(!UL[num])return inf;num=UL[num];
    return query(num,y,10);
}
int queryUR(int num,int y){
    if(!UR[num])return inf;num=UR[num];
    return query(num,y,10);
}
int queryDL(int num,int x){
    if(!DL[num])return inf;num=DL[num];
    //cout << num <<endl;
    return query2(num,x,10);
}
int queryDR(int num,int x){
    if(!DR[num])return inf;num=DR[num];
    return query2(num,x,10);
}
int queryD(int num,int y){
    if(!D[num])return inf;num=D[num];
    return query(num,y,5);
}
int queryL(int num,int x){
    if(!L[num])return inf;num=L[num];
    return query(num,x,5);
}
int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> x[i] >>yy[i] >> c[i];
        if(c[i]=='R'){
            addnodeUR(x[i]+yy[i],yy[i]);
            addnodeDR(x[i]-yy[i],yy[i]);
        }else if(c[i]=='L'){
            addnodeDL(x[i]+yy[i],yy[i]);
            addnodeUL(x[i]-yy[i],yy[i]);
            addnodeL(yy[i],x[i]);
        }
        else if(c[i]=='D')
            addnodeD(x[i],yy[i]);
    }
    for(int i=1;i<=cnt;i++)
        sort(GG[i].begin(),GG[i].end());
    int point = inf ;
    for(int i=0;i<n;i++){
        if(c[i]=='U'){
            point=min(point,queryUL(x[i]-yy[i],yy[i]));
            point=min(point,queryUR(x[i]+yy[i],yy[i]));
            point=min(point,queryD(x[i],yy[i]));
            //cout << point << endl;
        }
        if(c[i]=='D'){
            point=min(point,queryDL(x[i]+yy[i],yy[i]));
            point=min(point,queryDR(x[i]-yy[i],yy[i]));
        }
        if(c[i]=='R'){
            point=min(point,queryL(yy[i],x[i]));
            //cout << point << endl;
        }
    }
    if(point==inf)
        cout << "SAFE\n" << endl;
    else
        cout << point <<endl;
    return 0;
}


#include <iostream>
#include <memory>
#include <string>
#include <vector>

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

using namespace std;

int main() {
    string x;
    cin >> x;
    for (auto ch : x) {
        if (ch == '7') {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}

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

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

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


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

int main() {
	int N;
	cin >> N;
	vector<int> a(N + 1), b(N + 1);
	for (int i = 1; i <= N; i++) cin >> a[i];
	for (int i = N; i >= 1; i--){
		int s = 0;
		for (int j = i + i; j <= N; j += i){
			s ^= b[j];
		}
		b[i] = s ^ a[i];
	}
	vector<int> ans;
	for (int i = 1; i <= N; i++){
		if (b[i]) ans.push_back(i);
	}
	cout << ans.size() << endl;
	rep(i, ans.size()){
		cout << ans[i] << " ";
	}
	return 0;
}

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

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

  vector<int>A(N);
  vector<int>B(N,0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i >= N/2){
      B.at(i) = A.at(i);
    }
  }

  for (int i = N/2; i > 0; i--) {
    int judge = 0;
    for (int j = 1; j <= N/i; j++) {
      if (B.at(i*j-1) == 1){
        judge++;
      }
    }
    if (judge % 2 != A.at(i-1)){
      B.at(i-1) = 1;
    }
  }

  int count = 0;
  for (int i = 0; i < N; i++) {
    if (B.at(i) == 1){
      count++;
    }
  }
  cout << count << endl;
  for (int i = 0; i < N; i++) {
    if (B.at(i) == 1){
      cout << i+1;
      count--;
      if (count != 0){
        cout << ' ';
      }
    }
  }
  cout << endl;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#define ll long long
#define LINF 0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
int N, M, K, Q;
ll xA[MAX], yA[MAX];

int main()
{
	int i, j, k;
	scanf("%d%d", &N, &M);
	for (i = 1; i <= N; ++i) scanf("%lld", &xA[i]);
	for (i = 1; i <= M; ++i) scanf("%lld", &yA[i]);
	ll xsum, ysum;
	xsum = ysum = 0;
	for (i = 1; i < N; ++i) xsum = (xsum + (xA[i + 1] - xA[i]) * (ll)(N - i) %MOD * (ll)i % MOD) % MOD;
	for (i = 1; i < M; ++i) ysum = (ysum + (yA[i + 1] - yA[i]) * (ll)(M - i) %MOD * (ll)i % MOD) % MOD;
	printf("%lld", xsum*ysum %MOD);
}

#include <bits/stdc++.h>

using namespace std;

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

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

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

const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
void DEBUG_DP(T* viewdp, int ilen, int jlen){
    REP(i,ilen){
        REP(j,jlen){
            if(viewdp[i][j] != -1)
                printf("[% 4d]", viewdp[i][j]);
            else
                printf("[    ]");
        }
        puts("");
    }
}
const int N=81;
const int C=6400;
const int K=C*2+1;
int h,w;
int a[N][N], b[N][N];
bitset<K> dp[N][N];
int ans = INT_MAX;

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

    REP(i,N){
        fill(a[i], a[i]+N, -1);
        fill(b[i], b[i]+N, -1);
    }

    cin>>h>>w;

    REP(i,h) REP(j,w) cin>>a[i][j];
    REP(i,h) REP(j,w) cin>>b[i][j];

    //int sx=0,sy=0, gx=w-1, gy=h-1;
    dp[0][0][abs(a[0][0]-b[0][0]) + 6400]=true;

    REP(i,h) REP(j,w){
        int ab =abs(a[i+1][j]-b[i+1][j]);

        dp[i+1][j] |= dp[i][j] << ab;
        dp[i+1][j] |= dp[i][j] >> ab;

        ab =abs(a[i][j+1]-b[i][j+1]);
        dp[i][j+1] |= dp[i][j] << ab;
        dp[i][j+1] |= dp[i][j] >> ab;
    }

    REP(i,K){
        if(dp[h-1][w-1][i]) chmin(ans, abs(6400-i));
    }

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


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

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

    int mn = 1000, sum = 0;
    rep(i,n) {
        int t = i + l;
        sum += t;
        mn = min(abs(t),abs(mn));
        if(t < 0)
            mn *= -1;
    }

    cout << sum - mn << endl;

    return 0;
}

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

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

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

int main()
{
    int n, m;
    cin >> n >> m;
    if (n % 2 == 1) {
        REP(i, m)
        {
            cout << i + 1 << " " << n - 1 - i << endl;
        }
    } else {
        REP(i, m)
        {
            if (i > (n-4)/4)
                cout << n / 2 - i << " " << n / 2 + 2 + i << endl;
            else
                cout << n / 2 - i << " " << n / 2 + 1 + i << endl;
        }
    }
}


#include<iostream>
using namespace std;

int main(){
  int a, b, n;
  int suma = 0;
  int sumb = 0;
  while(1){
    cin >> n;
    if(n == 0) break;
    for(int i = 0; i < n; i++){
      cin >> a >> b;
      if(a > b) suma += a + b;
      else if(a < b) sumb += a + b;
      else if(a == b){
        suma += a;
        sumb += b;
      }
    }
    cout << suma << " " << sumb << endl;
    suma = 0;
    sumb = 0;
  }
  return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  map<int, int> odd, even;
  rep(i, n) {
    if (i % 2 == 0)
      odd[v[i]]++;
    else
      even[v[i]]++;
  }

  int num_even1 = 0, num_even2 = 0, num_odd1 = 0, num_odd2 = 0;
  int times_even1 = 0, times_even2 = 0, times_odd1 = 0, times_odd2 = 0;

  //偶数番目最大値
  int count = 0;
  for (auto itr = even.begin(); itr != even.end(); itr++) {
    if (itr->second > count) {
      count = itr->second;
      num_even1 = itr->first;
    }
  }
  times_even1 = count;

  //偶数番目二番目に大きい値
  count = 0;
  for (auto itr = even.begin(); itr != even.end(); itr++) {
    if (itr->second > count && itr->first != num_even1) {
      count = itr->second;
      num_even2 = itr->first;
    }
  }
  times_even2 = count;

  //奇数番目最大値
  count = 0;
  for (auto itr = odd.begin(); itr != odd.end(); itr++) {
    if (itr->second > count) {
      count = itr->second;
      num_odd1 = itr->first;
    }
  }
  times_odd1 = count;

  //奇数番目二番目に大きい値
  count = 0;
  for (auto itr = odd.begin(); itr != odd.end(); itr++) {
    if (itr->second > count && itr->first != num_odd1) {
      count = itr->second;
      num_odd2 = itr->first;
    }
  }
  times_odd2 = count;

  //出力
  if (num_even1 == num_odd1) {
    cout << min(n - times_even1 - times_odd2, n - times_even2 - times_odd1)
         << endl;
  } else {
    cout << n - times_even1 - times_odd1 << endl;
  }
  return 0;
}


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

template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
    if (v.size()) {
        os << v.front();
        for (auto vi = v.begin() + 1; vi != v.end(); vi++)
            os << " " << *vi;
    }
    return os;
}

int bubble_count(vector<int> &a)
{
    int count = 0;

    for (int i = 0; i < a.size() - 1; i++)
        for (int j = a.size() - 1; j > i; j--)
            if (a[j] < a[j - 1]) {
                swap(a[j], a[j - 1]);
                count++;
            }

    return count;
}

int main()
{
    int n, count;
    cin >> n;
    vector<int> array(n);

    for (int x, i = 0; i < n && cin >> x; i++)
        array[i] = x;

    count = bubble_count(array);

    cout << array << endl;
    cout << count << endl;

    return 0;
}

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

int main() {
  vector<int> w, k;
  int w_max = 0;
  int k_max = 0;

  for(int i = 0; i < 10; i++) {
    int W;
    cin >> W;
    w.push_back(W);
  }

  for(int i = 0; i < 10; i++) {
    int K;
    cin >> K;
    k.push_back(K);
  }

  sort(w.begin(), w.end(), greater<int>());
  sort(k.begin(), k.end(), greater<int>());

  for(int i = 0; i < 3; i++) {
    w_max += w[i];
    k_max += k[i];
  }

  cout << w_max	<< ' ' << k_max << endl;

  return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

const int INF = INT_MAX / 2;

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

	while ( true )
	{
		int n, k;
		cin >> n >> k;

		if ( !( n | k ) )
		{
			break;
		}

		VVI G( n, VI( n, INF ) );
		REP( i, 0, n )
		{
			G[i][i] = 0;
		}

		REP( i, 0, k )
		{
			int q, a, b, e;
			cin >> q;
			switch ( q )
			{
				case 0:
					cin >> a >> b;
					a--;
					b--;
					cout << ( G[a][b] == INF ? -1 : G[a][b] ) << endl;
					break;
				case 1:
					cin >> a >> b >> e;
					a--;
					b--;
					G[a][b] = min( G[a][b], e );
					G[b][a] = min( G[b][a], e );

					REP( i, 0, n )
					{
						G[i][a] = min( G[i][a], G[i][b] + G[b][a] );
						G[i][b] = min( G[i][b], G[i][a] + G[a][b] );
					}

					REP( i, 0, n )
					{
						REP( j, 0, n )
						{
							G[i][j] = min( G[i][j], min( G[i][a] + G[a][j], G[i][b] + G[b][j] ) );
						}
					}
			}
		}
	}

	return 0;
}

#include<cstdio>

int comparedice(void);
class dice{
private:
	int a,b,c,d,e,f;
public:
	void setnumber(void);
	void E(void);
	void W(void);
	void S(void);
	void N(void);
	void R(void);
	void V(void);
	int frontside(int x,int y);
	int putme(char me);
};

void dice::setnumber(void){
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
}

void dice::E(void){
int a1=a,c1=c,d1=d,f1=f;
a=d1;
c=a1;
d=f1;
f=c1;
}

void dice::W(void){
int a1=a,c1=c,d1=d,f1=f;
a=c1;
c=f1;
d=a1;
f=d1;
}

void dice::S(void){
int a1=a,b1=b,e1=e,f1=f;
a=e1;
b=a1;
e=f1;
f=b1;
}

void dice::N(void){
int a1=a,b1=b,e1=e,f1=f;
a=b1;
b=f1;
e=a1;
f=e1;
}

int dice::frontside(int x,int y){
int i=0,a1,b1,c1,d1,e1,f1;
while(1){
	a1=a;
	b1=b;
	c1=c;
	d1=d;
	e1=e;
	f1=f;
	a=b1;
	b=c1;
	c=a1;
	d=f1;
	e=d1;
	f=e1;
	i++;
	if(i==4)break;
}

if (b!=x){
	a=e1;
	b=f1;
	c=d1;
	d=c1;
	e=a1;
	f=b1;
}
//printf("%d %d %d %d %d %d\n",a,b,c,d,e,f);
i=0;
while (b!=x){
	a1=a;
	b1=b;
	c1=c;
	d1=d;
	e1=e;
	f1=f;
	a=b1;
	b=c1;
	c=a1;
	d=f1;
	e=d1;
	f=e1;
	i++;
	if(i==4)break;
}
if(i==4){ return 0;}
i=0;
while(c!=y){
	a1=a;
	c1=c;
	d1=d;
	f1=f;
	a=d1;
	c=a1;
	d=f1;
	f=c1;
	i++;
	if(i==5) break;
}
if(i==5) {return 0;}
return 1;
}

int dice::putme(char me){
if(me=='a'){
return a;
}else if(me=='b'){
return b;
}else if(me=='c'){
return c;
}else if(me=='d'){
return d;
}else if(me=='e'){
return e;
}else {
return f;
}
}
int comparedice(dice,dice);
int main(void){
int n;
scanf("%d",&n);
dice koro[n];
int i,j;
for (i=0;i<n;i++){
	koro[i].setnumber();
}
for (i=0;i<n;i++){
	for(j=0;j<i;j++){
		if (comparedice(koro[i],koro[j])==0){
			printf("No\n");
		}
		if (comparedice(koro[i],koro[j])==0){return 0;}
	}
}
printf("Yes\n");
return 0;
}
int comparedice(dice koro1,dice koro2){
int i,j;
for(i=0;i<3;i++){
	for(j=0;j<4;j++){
		if(
		(koro1.putme('a')==koro2.putme('a'))
		&&(koro1.putme('b')==koro2.putme('b'))
		&&(koro1.putme('c')==koro2.putme('c'))
		&&(koro1.putme('d')==koro2.putme('d'))
		&&(koro1.putme('e')==koro2.putme('e'))
		&&(koro1.putme('f')==koro2.putme('f'))
	){return 0;}
		koro2.E();
	}
koro2.E();
koro2.N();
}
koro2.N();
koro2.E();
koro2.E();
for(i=0;i<3;i++){
	for(j=0;j<4;j++){
		if(
	(koro1.putme('a')==koro2.putme('a'))
	&&(koro1.putme('b')==koro2.putme('b'))
	&&(koro1.putme('c')==koro2.putme('c'))
	&&(koro1.putme('d')==koro2.putme('d'))
	&&(koro1.putme('e')==koro2.putme('e'))
	&&(koro1.putme('f')==koro2.putme('f'))
	){return 0;}
		koro2.E();
	}
koro2.E();
koro2.N();
}
return 1;
}


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

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const ll Mod = 1000000007LL;
const int Maxn = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 21;

vector<ll> A, B;

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n;
	cin >> n;
	str s;
	cin >> s;
	for(int i = 0; i< n + n;i++){
		int al = (s[i] == 'B') + i;
		al &= 1;
		if(al == 1) A.pb(i);
		else B.pb(i);
	}
	ll ans = 1;
	if(A.size() != B.size()) return cout << 0, 0;
	for(int i = n - 1; i >= 0; i--){
		int cn = n - (lower_bound(all(B), A[i]) - B.begin());
		ans *= (cn - (n - 1 - i));
		ans%=Mod;
	}
	for(int i = 1; i <= n; i++) ans = (ans*i)%Mod;
	cout << ans;
	return 0;
}

/*

                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef int_fast64_t ll;

template<int N, typename T, T M>
struct ModFact {
    T fact[N+1];
    constexpr ModFact() : fact() {
        fact[0] = 1;
        for (int i=0; i<N; ++i)
            fact[i+1] = (fact[i]*(i+1))%M;
    }
};

ModFact<100000, ll, MOD> fact;

ll N, M, ans = 1;
int tmp, cnt = 0;
constexpr ll th = 1LL<<45;
char S[200000];

int main() {
    scanf("%lld\n", &N);
    M = N << 1;
    fread_unlocked(S, 1, M, stdin);
	for(size_t i = 0; i < M;) {
		tmp = (S[i++]=='B') + cnt;
		if(tmp&1) cnt++;
		else {
            ans *= cnt;
            if(ans > th) ans %= MOD;
            if(--cnt < 0) break;
        }
	}
    ans = cnt == 0 ? ((ans % MOD) * fact.fact[N]) % MOD : 0;
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<ll>>;
struct edge{ll to ; ll cost ;} ;
using graph =vector<vector<edge>> ;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout<<fixed<<setprecision(20) ;
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
double pi=acos(-1) ;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

 int main(){
ll n ; cin>> n ;
vector<string> A(n) ;
rep(i,n) cin>>A[i] ;
vector<ll> sum(n) ,sa(n) ;
rep(i,n){
  ll k= A[i].size() ;
  ll now=0 ;
  ll ans=0 ;
  rep(j,k){
   if(A[i][j]==')') now-- ;
   else now++ ;
   chmin(ans,now)  ;
  }
  sum[i]= now ;
  sa[i]= ans ;
}
 vector<P> B, D ;
 ll zero =0 ;

  rep(i,n){
   if(sum[i]>0){
   P xx= mpa(sa[i],sum[i]) ;
   B.pb(xx) ;
   }
   if(sum[i]==0) chmin(zero,sa[i]) ;
   if(sum[i]<0){
     P yy= mpa(sum[i]-sa[i],sum[i]) ;
     D.pb(yy) ;
   }

  }

  sort(B.rbegin(),B.rend()) ;
  sort(D.begin(),D.end()) ;
  ll ok= B.size() ;
  ll ko= D.size() ;
  ll start= 0 ;
  bool flag= 0;

  rep(i,ok){
  if(start+ B[i].fi<0) flag=1;
  start+= B[i].se ;
  }

  if(start+zero<0) flag=1 ;
  ll goal =0 ;
  rep(i,ko){
    if(goal-D[i].fi<0) flag=1 ;
     goal -= D[i].se ;
  }
  if(start!=goal) flag=1 ;

  if(flag) cout<<"No"<<endl ;
  else cout<<"Yes"<<endl ;
   return 0 ;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<deque>
#include<bitset>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
typedef pair<ll,ll> P;
template<class T> inline bool chmax(T&a , T b){ if(a<b){ a=b; return true; } else{ return false; } }
template<class T> inline bool chmin(T&a , T b){ if(a>b){ a=b; return true; } else{ return false; } }

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

bool check(vector<P> s){
    int h=0;
    for (P p : s){
        int b = h+p.first;
        if(b<0) return false;
        h += p.second;
    }
    return true;
}

int main(){
    int n;
    cin >> n;
    vector<P> ls, rs;
    int total = 0;
    rep(i,n){
        string s;
        cin >> s;
        int h = 0, b = 0;
        for(char c : s){
            if(c=='(') ++h; else --h;
            b = min(b,h);
        }
        if(h>0) ls.emplace_back(b,h);
        else rs.emplace_back(b-h,-h);
        total += h;
    }
    sort(ls.rbegin(),ls.rend());
    sort(rs.rbegin(),rs.rend());
    if(check(ls) && check(rs) && total==0){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}


#include <stdio.h>

int main(){
	int x;
	int y;
	int z;
	scanf("%d/%d/%d",&x , &y ,&z);
	printf ("%d/%02d/%02d",x+1,y,z);

	return 0;
}

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

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

	cout << s.replace(3, 1, "8") << endl;
}


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

int main() {
  vector<long long> A(3);
  for (int i = 0; i < 3; ++i) cin >> A[i];
  long long K;
  cin >> K;
  sort(A.begin(), A.end());
  for (int i = 0; i < K; ++i) A.back() *= 2;
  cout << A[0] + A[1] + A[2] << endl;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    vector<int> a(3);
    int k;
    cin >> a[0] >> a[1] >> a[2];
    cin >> k;

    int check;
    int amax=max({a[0],a[1],a[2]});
    for(int i=0;i<3;i++){
        if(amax==a[i]) check=i;
    }

    for(int i=0;i<k;i++) amax*=2;
    cout << a[0]+a[1]+a[2]-a[check]+amax << endl;
}


#include <bits/stdc++.h>

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

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

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

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

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

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

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

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

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

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

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

int n, m, v, p, a[100005];
long long pre[100005];

int main() {
	read(n), read(m), read(v), read(p);
	for (int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	std::sort(a + 1, a + 1 + n, std::greater<int>());
	long long sum = a[p];
	int ans = p;
	for (int i = p + 1; i <= n; ++i) {
		if (a[i] + m >= a[p] && 1ll * (a[i] + m) * (i - p) - sum >= 1ll * m * (v - n + i - p)) {
			++ans;
		}
		sum += a[i];
	}
	print(ans);
}

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

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

#define rep(i,n) for (int i = 0; i < n; ++i)
#define reps(i,s,n) for (int i = s; i < n; ++i)
#define rep1(i,n) for (int i = 1; i <= n; ++i)
#define per(i,n) for (int i = n - 1; i >= 0; --i)
#define per1(i,n) for (int i = n; i >= 1; --i)
#define all(c) begin(c),end(c)
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline T intceil(T a, T b) { return (a + (b - 1)) / b; }
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
const long long MOD = 1e9+7;
#define precout() cout << std::fixed << std::setprecision(20);
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const int dy[4] = { 0, 1, 0, -1 };
const int dx[4] = { 1, 0, -1, 0 };
// if(nextY >= 0 && nextY < H && nextX >= 0 && nextX < W)
static const long double pi = acos(-1.0);
typedef complex<ld> cd;

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

    ll n,m,v,p; cin >> n >> m >> v >> p;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    sort(all(a));
    // debug(a);

    int lo = -1, hi = n - 1;
    while(hi - lo > 1) {
        int mid = (lo + hi) / 2;
        vector<ll> b;
        rep(i, n) {
            if(i != mid) {
                b.push_back(a[i]);
            }
        }
        reverse(all(b));

        ll base = a[mid] + m;
        ll ok = 0;

        bool ng = 0;
        rep(i, b.size()) {
            if(i < p - 1) ok += m;
            else if(base >= b[i]) ok += min(m, base - b[i]);
            else ng = 1;
        }

        // debug(mid, base, ok, b);

        if(ng || ok < (m * (v - 1))) {
            lo = mid;
        } else {
            hi = mid;
        }
    }

    cout << n - hi << endl;
}


#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#include <random>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("-1"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
#define bn(x) ((1<<x)-1)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;

int main() {
  int a,b;
  scanf("%d%d",&a,&b);
  if (a == b) {
    cout<<0<<endl;
    return 0;
  }
  if (abs(a) == abs(b)) {
    cout<<1<<endl;
    return 0;
  }
  if (abs(a) < abs(b)) {
    int ans = abs(b)-abs(a);
    if (a < 0) ans++;
    if (b < 0) ans++;
    cout<<ans<<endl;
    return 0;
  } else {
    int ans = abs(a)-abs(b);
    if (a > 0) ans++;
    if (b > 0) ans++;
    cout<<ans<<endl;
    return 0;
  }
  return 0;
}

#include <bits/stdc++.h>
#define mod 998244353
using namespace std;
int fac[1000005],inv[1000005],n,m;
inline int qpow(int a,int b)
{int tp=a,res=1;
while (b)
{if (b&1) res=1ll*res*tp%mod;
tp=1ll*tp*tp%mod;b>>=1;
}
return res;
}
inline int cal(int a,int b)
{return 1ll*(1ll*fac[a+b]*inv[a]%mod)*inv[b]%mod;}
int main (){
	int i,ans;
	scanf ("%d%d",&n,&m);
	if (n>m) swap(n,m);
	for (fac[0]=1,i=1;i<=n+m;i++)fac[i]=1ll*fac[i-1]*i%mod;
	for (inv[n+m]=qpow(fac[n+m],mod-2),i=n+m-1;i>=0;i--)inv[i]=1ll*inv[i+1]*(i+1)%mod;
	int tot=cal(n,m),now=0;
	for (i=1;i<=n;i++)
	{now+=1ll*cal(i,i)*cal(n-i,m-i)%mod;
	if (now>=mod) now-=mod;
	}
	now=1ll*now*qpow(tot,mod-2)%mod;
	now=1ll*now*qpow(2,mod-2)%mod;
	now=(now+m>=mod?now+m-mod:now+m);
	printf ("%d\n",now);
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
/*int main()
{
	int n; cin>>n;
	int a[n];
	for(int i=0;i<n;i++) cin>>a[i];
	sort(a,a+n);
//	for(int i=0;i<n;i++) cout<<a[i];
	int sum=0;
	for(int i=n-1;i>1;i--)
	{
		for(int j=i-1;j>0;j--)
		{
			if(a[i]==a[j]) continue;
			for(int k=j-1;k>=0;k--)
			{
				if(a[k]!=a[i] and a[k]!=a[j] and a[i] < (a[k] + a[j]))
				{
					sum++;
				}

				if(a[k]!=a[i] and a[k]!=a[j] and a[i] >= (a[k] + a[j]))
				{
					break;
				}

			}
		}
	}

	cout<<sum;
}

int main()
{
	int n,d,ans=0;
	cin>>n>>d;
	while(n--)
	{
		int a,b; cin>>a>>b;
		long long int fl=a*a + b*b;
		float sum=sqrt(fl);
		if(sum<=d) ans++;

	}
	cout<<ans;
}
int main()
{
	int n,k; cin>>n>>k;
	int a[n],sum=0;
	for(int i=0;i<n;i++) cin>>a[i];
	sort(a,a+n);
	for(int i=0;i<k;i++) sum+=a[i];
	cout<<sum;
}

int main()
{
	int n,k; cin>>n>>k;
	k%2==0 and k>=2*n and k<=4*n ? cout<<"Yes" : cout<<"No";
}

int main()
{
	int n; cin>>n;
	int a[n];
	for(int i=0;i<n;i++) cin>>a[i];
	unsigned long long ans=1,ANS;
	vector<int>v;
	for(int i=0;i<n;i++)
	{
		ans*=a[i];
		ANS=ans;
		while(ANS)
		{
			v.push_back(ANS%10);
			ANS/=10;
		}
		if(v.size()>1000000000000000000)
		{
			cout<<"HRE";
			cout<<"-1";
			return 0;
		}
		v.clear();
	}
	cout<<ans;
}

int main()
{
	int n; cin>>n;
	string s; cin>>s;
	if(s.size()<=n) cout<<s;
	else {

	for(int i=0;i<n;i++) cout<<s[i];
	cout<<"...";
}}

int main()
{
	long long int a,b,c,d; cin>>a>>b>>c>>d;
	long long int sum=0,count=0;
	if(a<=d)
	{
		sum+=a;
		d-=a;
		if(d==0)
		{
			cout<<sum;
			return 0;
		}
	}
	else
	{
		cout<<d;
		return 0;
	}

		if(b<=d)
	{

		d-=b;
		if(d==0)
		{
			cout<<sum;
			return 0;
		}
	}
	else
	{
		cout<<d;
		return 0;
	}

		if(c<=d)
	{
		sum-=c;
		d-=c;
		if(d==0)
		{
			cout<<sum;
			return 0;
		}
	}
	else
	{
		cout<<sum-d;
		return 0;
	}

}

int main()
{
	int n,k; cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++) a[i]=0;
	while(k--)
	{
		int d; cin>>d;
		for(int i=0;i<d;i++)
		{
			int x; cin>>x;
			a[x-1]++;
		}
	}
	//cout<<a[2];
	int ans=0;
	for(int i=0;i<n;i++) if(a[i]==0) ans++; cout<<ans;
}
int main()
{
	long long int n,year=0; cin>>n;
	int a=100;
	while(a<n)
	{
		float aa=(1.0/100)*a;
		a+=floor(aa);
		year++;
	}
	cout<<year;
}
int main()
{
	long long int n,m; cin>>n>>m;
	long long int sum=0,a[n];
	for(long long int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	sort(a,a+n,greater<int>());
	float min=sum*1.0/(4*m);
	a[m-1]<min?cout<<"No": cout<<"Yes\n";
}*/

int main()
{
	int n; cin>>n;
	long long sum=(n/500)*1000 + ((n-(n/500)*500)/5)*5;
	cout<<sum;
}

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

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

  int ans = 1;
  for(int i=2;i*i<=N;i++){
    int work = i*i;
    ans = max(ans,work);
  }

  cout << ans << endl;

  return 0;
}


//auto 関数名 = [(&:ラムダ外の引数をとる時))](引数の型1 引数名1, 引数の型2, 引数名2, ...) { 関数の処理 }; //inside main() define function. take care of };
// for (int tmp = 0; tmp < (1 << ビット数); tmp++) {
//   bitset<ビット数> s(tmp);
//   // (ビット列sに対する処理)
// }
// sort(配列変数.begin(), 配列変数.end());
// do {
//   // 順列に対する処理
// } while (next_permutation(配列変数.begin(), 配列変数.end()));
//const double PI = acos(-1); M_PI
// cout << fixed << setprecision(10);
#include <bits/stdc++.h>
#include <regex>
using namespace std;
using ll = int64_t;
using P = pair<int,int>;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end() //sort(all(vec)); ,reverse(all(vec));etc
int main(){
 string s;
 cin>>s;
 vector<bool>vec(26);
 rep(i,s.size()){
   vec.at((int)(s.at(i))-97)=1;
 }
 rep(i,26){
   if(!vec.at(i)){
     cout<<(char)(i+97)<<endl;
     return 0;
   }
 }
 cout<<"None"<<endl;

}

#include<iostream>
#include<cmath>

int main() {
	int a = {};
	int a2 = {};
	int a3 = {};
	std::cin >> a;

	a2 =std::pow(a, 2);
	a3 = std::pow(a, 3);

	a = a + a2 + a3;
	std::cout << a << std::endl;
}


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

using ll = long long;
#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = b-1; i >= a; i--)

const int mod = 998244353;
const int mxn = 1e5;
int n,K;

void add(ll &a, ll b) { if (b>=mod) b%=mod; a+=b; if (a>=mod) a-=mod; }
ll modpow(ll a, ll b) {
	return b?modpow(a*a%mod,b/2)*(b&1?a:1)%mod:1;
}
struct Comb {
	ll fac[mxn], inv[mxn], twopow[mxn];
	Comb() {
		f(i,0,mxn) {
			fac[i]=i?fac[i-1]*i%mod:1;
			twopow[i]=i?twopow[i-1]*2%mod:1;
		}
		inv[mxn-1]=modpow(fac[mxn-1],mod-2);
		fr(i,0,mxn-1) {
			inv[i]=inv[i+1]*(i+1)%mod;
		}
	}
	ll choose(int n, int r) {
		if (n<0||n<r||r<0) return 0;
		return fac[n]*inv[r]%mod*inv[n-r]%mod;
	}
	ll multichoose(int n, int k) {
		if (!n&&!k) return 1;
		return choose(n+k-1,k);
	}
	ll pow2(int n) {
		if (n<0) return 0;
		return twopow[n];
	}
} C;

ll solve(int x) {
	ll ans = 0;
	int a = 0, b = 0;
	f(i,1,K+1) {
		if (i+i==x) b++;
		if (i<=K && x-i<=K && x-i>0) a++;
	}
	a/=2;
	f(na,0,a+1) {
		f(nb,0,b+1) {
			add(ans,C.pow2(na)*C.choose(a,na)%mod*C.multichoose(na+K-2*a-b,n-na-nb));
		}
	}
	return ans;
}

const int DEBUG = 1;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL
	if (DEBUG) freopen("input.txt", "r", stdin);
	if (DEBUG) freopen("output.txt", "w", stdout);
#endif

	cin >> K >> n;
	for (int x = 2; x <= 2*K; x++)
		cout << solve(x) << endl;
	cout << endl;

	return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define MOD 998244353
using namespace std;
typedef long long ll;

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

inline int tasu(int a, const int &b) {return (a += b) >= MOD ? a - MOD : a;}

inline ll fastpow(ll bas, ll ex){
	register ll res = 1; bas %= MOD;
	for(; ex; ex >>= 1, bas = bas * bas % MOD) if(ex & 1) res = res * bas % MOD;
	return res;
}

int K, N;
ll p2[4005] = {1}, fac[4005] = {1}, ifac[4005];

inline ll binom(const int &n, const int &m) {return n < m || m < 0 ? 0 : fac[n] * ifac[m] % MOD * ifac[n - m] % MOD;}

inline int calc(const int &x, const int &y, const int &n){  // x pairs of conflict numbers, y free numbers, n sides of dices
	register int res = 0;
	for(register int i = 0, lim = min(x, n); i <= lim; i++)
		res = tasu(res, binom(x, i) * p2[i] % MOD * binom(n + y - 1, i + y - 1) % MOD);
	return res;
}

int main(){
	getint(K), getint(N);
	for(register int i = 1; i <= N + K; i++) fac[i] = fac[i - 1] * i % MOD, p2[i] = (p2[i - 1] << 1) % MOD;
	ifac[N + K] = fastpow(fac[N + K], MOD - 2);
	for(register int i = N + K; i >= 1; i--) ifac[i - 1] = ifac[i] * i % MOD;
	for(register int i = 2; i <= (K << 1); i++){
		const int cnt = min(i, (K + 1 << 1) - i) - 1 >> 1;
		if(i & 1) printf("%d\n", calc(cnt, K - (cnt << 1), N));
		else printf("%d\n", tasu(calc(cnt, K - (cnt << 1) - 1, N), calc(cnt, K - (cnt << 1) - 1, N - 1)));
	}
	return 0;
}


#include <bits/stdc++.h>

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

using namespace std;
using lint=long long;

int main(){
	string s; cin>>s;
	int n=s.length();

	lint hist[128]={};
	rep(i,n) hist[s[i]]++;

	lint ans=1;
	for(int c='a';c<='z';c++) for(int d=c+1;d<='z';d++) {
		ans+=hist[c]*hist[d];
	}
	cout<<ans<<'\n';

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;cin>>S;
  vector<long long>v(26);
  for(int X=0;X<S.size();X++){
    v[S[X]-97]++;
  }
  sort(v.begin(),v.end());
  long long H=0;
  for(int X=25;X>=0;X--){
    if(v[X]>1){
      H+=v[X]*(v[X]-1)/2;
    }
    else{
      break;
    }
  }
  cout<<S.size()*(S.size()-1)/2-H+1<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    rep(i, k) {
        vector<ll> b(n + 1, 0);
        rep(j, n) {
            ll l = max(0LL, j - a[j]);
            ll r = min(n, j + a[j] + 1);
            b[l]++;
            b[r]--;
        }
        ll cnt = 0;
        rep(j, n) {
            b[j + 1] += b[j];
            a[j] = b[j];
            if (a[j] == n) cnt++;
        }
        if (cnt == n) break;
    }
    rep(i, n) printf("%lld%s", a[i], (i == (n - 1)) ? "\n" : " ");
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
bool col[1000005];
bool row[1000005];
long long mod=1e9+7;
int main(){
	int n,m,a,b;
	long long ans=1;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
	cin>>a;
	if(row[a])
	{
		cout<<0;
		return 0;
	}
	row[a]=1;
	}
	for(int i=1;i<=m;i++)
	{
	cin>>b;
	if(col[b])
	{
		cout<<0;
		return 0;
	}
	col[b]=1;
	}
	long long c=0,r=0;
	for(int i=n*m;i>0;i--){
        long long tc=c,tr=r;
        if(col[i])
		{
		tc=1;
		c++;
		}
        if(row[i])
		{
		tr=1;
		r++;
		}
        if(col[i]||row[i])
		ans*=(tc*tr)%mod;
        else
		ans*=(tc*tr-(n*m-i))%mod;
		ans%=mod;
    }
	cout<<ans%mod;
}


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

//upper_bound, lower_boundを用いた
//のになぜか通らない・・・

int main(){
	int n;
	long long a;
	if(scanf("%d", &n)==EOF){return 0;}

	long long top[n];
	long long center[n];
	long long down[n];

	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} top[i] = a;}
	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} center[i] = a;}
	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} down[i] = a;}

	// top, downをソート
	sort(top, top+n);
	sort(down, down+n);
	//for(int i = 0; i<n; i++){cout << top[i] << ", "<<endl;}
	//for(int i = 0; i<n; i++){cout << down[i] << ", "<<endl;}

	long long sum = 0;
	//int ok1, ng1, ok2, ng2, mid;
	int ok1, ok2;

	for(int i = 0; i< n; i++){
		//center[i]より小さいtopの数を探す
		ok1 = lower_bound(top, top+n, center[i]) - top;
		ok2 = upper_bound(down, down+n, center[i]) - down;

		//cout << "top[ok1-1]" <<  top[ok1-1] << ", center:"<< center[i] <<", top[ok1]:"<< top[ok1] << endl;
		//cout << "down[ok2-1]:" << down[ok2-1] <<", center:"<< center[i] <<", down[ok2]:" << down[ok2] << endl;
		sum += (long long)ok1 * (long long)(n - ok2);
	}
	cout << sum << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll INF = 1LL << 60; //MAX 9223372036854775807
ll ans = INF;

int main()
{
ll n , x ;
cin >> n >> x ;
vector<vector<ll>> v(n, vector<ll>(3,0) );
vector<pair<ll,ll>> s;
ll d=0;
rep(i,n){
  cin >> v[i][0] >> v[i][1] >> v[i][2] ;
  d+=v[i][0]*v[i][1];
  s.emplace_back(v[i][0]*v[i][1]+(x-v[i][0])*v[i][2],i);
}
sort(s.rbegin(),s.rend());
vector<ll> sum;
sum.push_back(s[0].first);
rep(i,n-1)sum.push_back(s[i+1].first+sum.back());
rep(i,n){
  ll z=0,an;
  auto itr = lower_bound(sum.begin(), sum.end(), d);
  z=d-((itr-sum.begin()-1)<0 ? 0 : sum[itr-sum.begin()-1] );
  an=x*(itr-sum.begin());
  if(itr-sum.begin()>i){
    itr = lower_bound(sum.begin(), sum.end(), d+s[i].first);
    z=d-sum[itr-sum.begin()-1]+s[i].first;
    an=x*(itr-sum.begin()-1);
  }
  if(z-v[s[i].second][0]*v[s[i].second][1]>0)an+=v[s[i].second][0]+(z-v[s[i].second][0]*v[s[i].second][1]+v[s[i].second][2]-1)/v[s[i].second][2];
  else an+=(z+v[s[i].second][1]-1)/v[s[i].second][1];
  ans=min(ans,an);
}
cout << ans << endl;
return 0;
}


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

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

int main() {
    int a[4];
    rep(i,4)cin>>a[i];
    cout<<min(a[0], a[1])+min(a[2], a[3])<<endl;
	return 0;
}

#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>
#include <deque>
#include <set>
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define fore(i,a) for(auto &i:a)
#define INF INT_MAX/2;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
//ll llmax = 10000000000000000000ll;
using namespace std;
using Graph = vector<vector<int>>;

int cnt_digit(ll N)
{
	int digit = 0;

	while (N > 0)
	{
		N /= 10;
		digit++;
	}
	return digit;
}

 // 最大公約数計算

ll gcd(ll a, ll b)
{
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
// 最小公倍数の計算

ll lcm(ll a, ll b)
{
	ll g = gcd(a, b);

	return a / g * b;
}

struct union_find
{
	vector<int> par, r;
	union_find(int n)
	{
		par.resize(n);
		r.resize(n);
		init(n);
	}

	void init(int n)
	{
		for (int i = 0; i < n; i++) par[i] = i;
		for (int i = 0; i < n; i++) r[i] = 0;
	}

	int find(int x)
	{
		if (par[x] == x)return x;
		else return find(par[x]);
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (r[x] < r[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (r[x] == r[y])
			{
				r[x]++;
			}
		}
	}
};

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int a, b, c, d;
	cin >> a >> b >> c >> d;

	int e = min(a, b);
	int f = min(c, d);

	cout << e + f << endl;

}


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

int main() {
  int N,A,a,imin,j;
  cin >> N >> A;
  for (imin = 0; A % 2 == 0; imin++)
    A /= 2;
  for (int i = 0;i < N-1; i++) {
    cin >> a;
    for (j = 0;a % 2 == 0;j++)
      a /= 2;
    if (j < imin)
      imin = j;
  }
  cout << imin << endl;
}

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

int main() {
  ll n; cin >> n;
  vector<ll> v(n);
  rep(i, n)
    cin >> v.at(i);

  bool fin = false;
  ll ans = 0;

  rep(i, 30){

    rep(j, n){
      if(v.at(j)%2 == 1){
        fin = true;
        break;
      }
      else
        v.at(j) /= 2;
    }

    if(fin)
      break;

    ans++;
  }
  cout << ans << endl;
}


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

ll H[5010], P[5010], dp[5010], lmax=1e12;
pair<pair<ll, ll>, int> pr[5010];

int main() {
	int N;
	cin >> N;
	for(int i=0; i<N; ++i){
		cin >> H[i] >> P[i];
		pr[i]=make_pair(make_pair(H[i]+P[i], P[i]), i);
	}
	sort(pr, pr+N);
	dp[0]=0;
	for(int i=1; i<=N+1; ++i) dp[i]=lmax;
	for(int i=0; i<N; ++i){
		int t=pr[i].second;
		for(int j=i; j>=0; --j){
			if(dp[j]<=H[t]){
				dp[j+1]=min(dp[j+1], dp[j]+P[t]);
			}
		}
	}
	int ans=0;
	while(dp[ans+1]<lmax) ++ans;
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define INF 1000000001
//A=65,a=97

int main() {
    int k,t;cin >> k >> t;
    vector<int> a(t);for(int i=0;i<t;i++) cin >> a[i];
    sort(a.begin(),a.end());
    if(a[t-1]<=(k+1)/2){
        cout << 0 << endl;
        return 0;
    }

    a[t-1] -= k-a[t-1];
    cout << a[t-1]-1 << endl;

    return 0;
}

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

int main() {
  ll K, T;
  cin >> K >> T;
  vector<ll> a(T);
  for (int i = 0; i < T; i++) {
    cin >> a[i];
  }
  sort(a.rbegin(), a.rend());
  for (int i = 0; i < T; i++) {
    if (a[i] <= 1) continue;
    ll j = i + 1;
    while (a[i] > 0 && j < T) {
      if (a[j] < 1) {
        j++;
      } else {
        a[i]--;
        a[j]--;
      }
    }
  }
  ll ans = 0;
  for (auto res : a) {
    ans += max(0LL, res - 1);
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int N, x;
	int a[100];
	long long num = 0LL;

	cin >> N >> x;
	for (int i = 0;i < N;i++) cin >> a[i];
	sort(a, a + N);
	int count=0;

		for (int k = 0;k < N-1;k++) {
				num += a[k];
				if(num<=x)
				count++;

		}
		if (num <= x) {
			if (x - num == a[N - 1]) count++;
		}
		cout << count;

}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
ll a[123];
void solve(){
	int n;
	ll x;
	cin>>n>>x;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+1+n);
	ll sum = 0;
	int ans = 0;
	for(int i=1;i<=n;i++){
		if(sum+a[i]<=x){
			ans = i;
		}
		sum+=a[i];
	}
	if(sum<x)ans = n-1;
	cout<<ans<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define int long long
#define endl "\n"
#define all(x) (x).begin(),(x).end()
const int MOD = 1e9+7;
const int mxN = 2e5+3;

void solve(){
	int n;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; ++i)cin >> a[i];
	int ans = 0;
	for(int i = 1; i < n; ++i){
		if(a[i] < a[i - 1]){
			ans += (a[i - 1] - a[i]);
			a[i] = a[i - 1];
		}
	}
	cout << ans << endl;
}

int32_t main(){
	IO;

	int nT = 1;
	//cin >> nT;
	for(int i = 1; i <= nT; ++i)
		solve();
	return 0;
}

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

signed main() {
    int a[5], k;

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

    cout << (a[4] - a[0] > k ? ":(\n" : "Yay!\n");
    return (0);
}


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

typedef long long LL;

const int N = 3e5;
const LL MOD = 1e9+7;

inline LL dmod(LL x){
	return x%MOD;
}

LL fakto[N+5];

void pre(){
	fakto[0] = 1LL;
	for(int i=1;i<=N;i++){
		fakto[i] = dmod(fakto[i-1]*(LL)(i));
	}
	return;
}

LL fe(LL x, LL y){
	if(y==0) return 1LL;
	LL ret = fe(x,y/2);
	ret = dmod(ret*ret);
	if(y&1){
		ret = dmod(ret*x);
	}
	return ret;
}

LL nCr(LL x, LL y){
	LL tmp = dmod(fakto[y]*fakto[x-y]);
	tmp = fe(tmp,MOD-2);
	tmp = dmod(tmp*fakto[x]);
	return tmp;
}

LL step(int x, int y, int a, int b){
	int dx = a-x;
	int dy = b-y;
	int tot = dx+dy;
	return nCr(tot,dx);
}

int h,w,a,b;
int main(){
	pre();
	cin >> h >> w >> a >> b;
	LL ret = 0LL;
	for(int i=1;i<=h-a;i++){
		LL tmp = step(1,1,i,b);
		tmp = dmod(tmp*step(i,b+1,h,w));
		ret = dmod(ret+tmp);
	}
	cout << ret << endl;
	return 0;
}

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

const ll MAX = 200200, MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
// 前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数の計算
ll COM(int n, int k){
    if (n < 0 || k < 0 || n < k) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(){
    ll n, m, k; cin >> n >> m >> k;
    COMinit();
    ll ans = 0;
    rep(i,n) ans += i*((n-i)*m*m % MOD) * COM(n*m-2, k-2) % MOD, ans %= MOD;
    rep(i,m) ans += i*((m-i)*n*n % MOD) * COM(n*m-2, k-2) % MOD, ans %= MOD;
    cout << ans << endl;
    return 0;
}


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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

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

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

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

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

vector<int> chidx;
int n;

int check(){
    REP(i, 0, n){
        if(chidx[i] != n-1){
            return 1;
        }
    }
    return 0;
}

int main(){
    cin >> n;
    chidx = vector<int>(n, 0);
    vector<vector<int> > lis(n, vector<int>(n-1));
    REP(i, 0, n){
        REP(l, 0, n-1){
            cin >> lis[i][l];
            lis[i][l]--;
        }
    }
    int ans = 0;
    while(check() == 1){
        ans++;
        vector<int> used(n, 0);
        int f = 0;
        REP(i, 0, n){
            if(chidx[i] == n-1){
                continue;
            }
            int to = lis[i][chidx[i]];
            if(used[i] == 1 || used[to] == 1){
                continue;
            }
            if(chidx[to] == n-1){
                cout << -1 << endl;
                return 0;
            }
            if(lis[to][chidx[to]] == i){
                chidx[i]++;
                chidx[to]++;
                used[i] = 1;
                used[to] = 1;
                f = 1;
            }
        }
        if(f == 0){
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <sstream>
#include <cmath>
#include <string>
#include <array>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <utility>
#include <iomanip>
#include <numeric>
#include <climits>
#include <algorithm>
#include <cstdint>
using namespace std;

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

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

    int ans = 0;
    for (int i = 0; i < k; i++)
        ans += niz[i];
    cout << ans;
}

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

int main(){
    priority_queue<int>q1,q2;
    int a,b;
    for(int i=0;i<4;i++){
        cin>>a;
        q1.push(a);
    }
    for(int i=0;i<2;i++){
        cin>>a;
        q2.push(a);
    }
    for(int i=0;i<3;i++){
        b+=q1.top();
        q1.pop();
    }
    cout<<b+q2.top()<<endl;
}


#include<iostream>
#include<string>
#include<vector>
#define rep(i,n) for(int i=0;i<(int)n;i++)
using namespace std;
int keta(int i) {
	int ret=0;
	while (i > 0) {
		i /= 10;
		ret++;
	}
	return ret;

}
void ita(int y, vector<int> *x) {
	int i = x->size()-1;
	while (y > 0) {
		x->at(i--) = y % 10;
		y /= 10;
	}
}
int main() {
	int a, b;
	cin >> a >> b;
	int count = 0;
	for (int i = a; i <= b; i++) {
		vector<int> x(keta(i));
		ita(i, &x);
		int l = 0, r = x.size() - 1;
			while (x.at(l++) == x.at(r--)) {
				if (l > r) {
					count++;
					break;
				}
			}

	}
	cout << count << endl;
}

#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<ll,ll>
#define int ll

char grid[410][410];
bool seen[410][410];
vector<P> gr[1001001];
int ans;
int h,w;

void dfs(int i, int j, int &g, int pi=-1, int pj=-1){
  if(seen[i][j]==1) return;
  if(i<0 || i>=h || j<0 || j>=w) return;
  if(pi!=-1 && grid[pi][pj]==grid[i][j]) return;
  seen[i][j]=1;
  gr[g].push_back(make_pair(i,j));
  dfs(i+1,j,g,i,j);
  dfs(i-1,j,g,i,j);
  dfs(i,j+1,g,i,j);
  dfs(i,j-1,g,i,j);
}

main(){
  cin>>h>>w;
  rep(i,h)rep(j,w){
    cin>>grid[i][j];
    seen[i][j]=0;
  }
  int tmp=0;
  rep(i,h)rep(j,w){
    if(seen[i][j]) continue;
    else{
      dfs(i,j,tmp);
      tmp++;
    }
  }
  ans=0;
  rep(i,tmp){
    int b=0, w=0;
    for(auto p : gr[i]){
      int hi=p.first, wi=p.second;
      if(grid[hi][wi]=='#') b++;
      else w++;
    }
    ans+=b*w;
  }
  cout<<ans<<endl;
}


#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
  ll n,m,t,i,res=0;
  cin >> n;
  m=(ll)sqrt(n);
  for(i=m;i>0;i--)
    if(n%i==0){
      t=n/i;
      while(t>0){
        t/=10;
        res++;
      }
      cout << res << endl;
      return 0;
    }
  return 0;
}

/*Allah Vorosha*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define n_p next_permutation
#define p_p prev_permutation
#define in insert
#define rev reverse
#define pf push_front
#define pob pop_back
#define uniq(v)         v.resize(distance(v.begin(),unique(v.begin(),v.end())))
#define all(x) (x).begin(),(x).end()
#define pof pop_front
#define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie();
#define scn scanf
#define prt printf
#define rep(i, a, n) for(int i = a; i < n; i++)
#define mod 1000000007
#define yes cout << "YES\n"
#define no cout << "NO\n";
#define take for(auto &it : a) cin >> it;
#define out cout << a << "\n";
#define l_b lower_bound
#define u_b upper_bound
#define Max 100005
template<typename T> T mymax(T x, T y) {
    return (x > y ? x : y);
}

using namespace std;

const int N = 1e5 + 5;

long long be(long long x,long long y){
    long long res=1;
    while(y){
        if(y&1){
            res*=x;
            res%=mod;
        }
        x*=x;
        x%=mod;
        y>>=1;
    }
    return res;
}

void solve() {
    auto cnt = [&](ll a)
    {
        int c = 0;
        while(a) {
            a /= 10;
            c++;
        }
        return c;
    };
    ll n;
    cin >> n;
    int mn = 1e8;
    for(ll i = 1; i * i <= n; i++) {
        if(n % i == 0) {
            ll b = n / i;
            int p = max(cnt(i), cnt(b));
            mn = min(mn, p);
        }
    }
    cout << mn;
}

int main() {

    solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//(a+b-1)/b

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

	return 0;
}


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

// i桁目まで見て j枚多く払う時(0 or 1)
ll dp[1000100][2];

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

  s = '0' + s;
  int n = (int)s.size();
  memset(dp, 0, sizeof(dp));
  dp[0][1] = 1;

  for (int i = 1; i < n; i++) {
    int current = s[i] - '0';
    dp[i][0] = min(dp[i - 1][0] + current, dp[i - 1][1] + (10 - current));
    dp[i][1] = min(dp[i - 1][0] + current + 1, dp[i - 1][1] + (9 - current));
  }
  cout << dp[n - 1][0] << '\n';
}

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

char S[1001001];
int D[10], F[10];

int main()
{
	scanf ("%s", S);
	for (int i = 0; i < 10; i++) D[i] = i;

	for (int t = 0; S[t]; t++){
		for (int j = 0; j < 10; j++) F[j] = 1e9;
		for (int j = 0; j < 10; j++) for (int i = 0; i < 10; i++){
			int k = j * 10 + i - (S[t] - '0');
			if (k < 0) continue;
			int c = i + k / 10;
			k %= 10;
			if (F[k] > D[j] + c)
				F[k] = D[j] + c;
		}
		for (int j = 0; j < 10; j++) D[j] = F[j];
	}

	int ans = 1e9;
	for (int j = 0; j < 10; j++) if (ans > D[j] + j) ans = D[j] + j;
	printf ("%d\n", ans);
	return 0;
}


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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
    #define gc getchar()
    template <typename T>
    inline void read(T& x) {
        x = 0; bool f = 1; char ch;
        for (ch = gc; ch < '0' || '9' < ch; ch = gc) if (ch == '-') f ^= 1;
        for (; '0' <= ch && ch <= '9'; ch = gc) x = (x << 3) + (x << 1) + (ch ^ 48);
        x = f ? x : -x;
    }
    #undef gc
}

const int MAXN = 2e5 + 10;

int n, arr[2][MAXN], cnt[2][MAXN], val;
vector<int> pos;

int main() {
    IO::read(n);
    rep(_, 0, 1) rep(i, 1, n) IO::read(arr[_][i]), cnt[_][arr[_][i]]++;
    reverse(arr[1] + 1, arr[1] + n + 1);
    rep(i, 1, n) if (arr[0][i] == arr[1][i]) val = arr[0][i], pos.push_back(i);
    int l = 1, r = n; bool flg = false;
    for (auto cur_pos : pos) {
        if (flg == false) {
            if (arr[0][l] != val && arr[1][l] != val) {
                swap(arr[1][l], arr[1][cur_pos]);
                ++l;
            } else flg = true;
        }
        if (flg == true) {
            if (arr[0][r] != val && arr[1][r] != val) {
                swap(arr[1][r], arr[1][cur_pos]);
                --r;
            } else return puts("No"), 0;
        }
    }
    puts("Yes");
    rep(i, 1, n) printf("%d ", arr[1][i]);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pb push_back
#define nl cout<<"\n"
#define db(x) cout<<x<<" "
#define fi(a,b) for(int i=a;i<b;i++)
#define fj(a,b) for(int j=a;j<b;j++)
#define fk(a,b) for(int k=a;k<b;k++)
#define F first
#define S second
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
#define mxsize (int)1e6
ll fastexp(ll a,ll n,ll nod){
    if(nod==0)	nod=LLONG_MAX;
    ll ans=1;
    while(n){
        if(n&1)
            ans=(ans*a)%nod;
        a=(a*a)%nod;
        n>>=1;
    }
    return ans;
}
void fun(){
    int n;
    cin>>n;
    int a[n],b[n];
    fi(0,n) cin>>a[i];
    fi(0,n) cin>>b[i];
    reverse(b,b+n);
    int i=0,j=n-1;
    int k=0;
    while(k<n){
        if(a[k]==b[k]){
            if(i!=-1&&(a[i]==a[k]||b[i]==a[k])){
                i=-1;
            }
            else if(i!=-1){
                swap(b[i++],b[k]);
            }
            if(i==-1&&(a[j]==a[k]||b[j]==a[k])){
                j=-1;
            }
            else if(i==-1){
                swap(b[j--],b[k]);
            }
            if(i==-1&&j==-1){
                cout<<"No\n";
                return ;
            }
        }
        k++;
    }
    cout<<"Yes\n";
    fi(0,n) db(b[i]);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("/ATOM/input.txt", "r", stdin);
        freopen("/ATOM/output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    while(t--){
        fun();
    }
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define INF 1e9 + 1
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;

int n, A[100010];

int partition(int p, int r) {
    int x = A[r];
    int i = p - 1;
    for (int j = p; j < r; j++) {
        if (A[j] <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i + 1;
}

int main() {
    cin >> n;
    REP(i, n) cin >> A[i];
    int q = partition(0, n - 1);
    REP(i, n) {
        if (i) cout << " ";
        if (i == q) cout << "[";
        cout << A[i];
        if (i == q) cout << "]";
    }
    cout << endl;
}

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

using namespace std;

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

int partition(vector<int> &A, int p, int r) {
	int x = A[r];
	int i = p;
	for (int j = p; j < r; j++) {
		if (A[j] <= x) {
			swap(A[i], A[j]);
			i++;
		}
	}
	swap(A[i], A[r]);
	return i;
}

int main() {
	int n = 0;
	cin >> n;
	vector<int> v;
	v.resize(n);

	{
		vector<int>::iterator i = v.begin();
		copy(istream_iterator<int>(cin), istream_iterator<int>(), i++);
	}
	int p = partition(v, 0, n-1);

	for (int i = 0; i < n; i++) {
		if (i == p) {
			cout << "[" << v[i] << "] ";
		}
		else if (i == n - 1) {
			cout << v[i] << "\n";
		}
		else {
			cout << v[i] << " ";
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for(ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main(){
  ll k, a;
  string s;
  in2(k, s);
  if(s.size() <= k)
    out(s);
  else{
    rep(i, k)
      out(s[i]);
    rep(i, 3)
      out('.');
  }
  memi;
}

#include<iostream>
#include<bits/stdc++.h>
#include<climits>

#define forn(x,n,s) for(int i = x; i < n; i += s )
#define forr(x,n,s) for(int i = x; i>=n; i -= s)

using namespace std;

void go(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  #ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
  #endif
}
long long int mod = 1e9+7;

int main(){
    go();

    // code here
    int k;cin>>k;
    string s;cin>>s;
    if(s.length() <= k) cout<<s;
    else cout<<s.substr(0,k)+"...";
}


#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

// ???????????¢?????????
class DiceSurface
{
private:
	unsigned short	number_;	// ????????¢??????????????????

public:
	DiceSurface() {}
	DiceSurface(const unsigned short& number)
		: number_(number)
	{}

	~DiceSurface() {}

	// ?????????
	void setNumber(const unsigned short& number) { number_ = number; }
	// ?????????
	unsigned short getNumber() const { return number_; }

};

class Dice
{
private:
	const DiceSurface* top_;	// ?????¢
	const DiceSurface* north_;	// ?????¢
	const DiceSurface* south_;	// ?????¢
	const DiceSurface* east_; 	// ??±??¢
	const DiceSurface* west_; 	// ?\???¢
	const DiceSurface* bottom_;	// ?????¢

private:
	Dice() {}

public:
	Dice( const unsigned short& topNumber
		, const unsigned short& northNumber
		, const unsigned short& southNumber
		, const unsigned short& eastNumber
		, const unsigned short& westNumber
		, const unsigned short& bottomNumber )
	{
		top_	= new DiceSurface(topNumber);
		north_	= new DiceSurface(northNumber);
		south_	= new DiceSurface(southNumber);
		east_	= new DiceSurface(eastNumber);
		west_	= new DiceSurface(westNumber);
		bottom_	= new DiceSurface(bottomNumber);
	}

	~Dice() {
		delete top_;
		delete north_;
		delete south_;
		delete east_;
		delete west_;
		delete bottom_;
	}

	unsigned short getTopNumber() {
		return top_->getNumber();
	}

	void rotateNorth() {
		const DiceSurface* tmp = top_;
		top_	= south_;
		south_	= bottom_;
		bottom_	= north_;
		north_	= tmp;
	}

	void rotateSouth() {
		const DiceSurface* tmp = top_;
		top_	= north_;
		north_	= bottom_;
		bottom_	= south_;
		south_	= tmp;
	}

	void rotateEast() {
		const DiceSurface* tmp = top_;
		top_	= west_;
		west_	= bottom_;
		bottom_	= east_;
		east_	= tmp;
	}

	void rotateWest() {
		const DiceSurface* tmp = top_;
		top_	= east_;
		east_	= bottom_;
		bottom_ = west_;
		west_	= tmp;
	}

	void rotateClockwise() {
		const DiceSurface* tmp = north_;
		north_	= west_;
		west_	= south_;
		south_	= east_;
		east_	= tmp;
	}

	void rotateAnticlockwise() {
		const DiceSurface* tmp = north_;
		north_	= east_;
		east_	= south_;
		south_	= west_;
		west_	= tmp;
	}
};

int main(int, char**)
{
	const unsigned short surfaceSize = 6;
	int numbers[surfaceSize];
	for ( int i = 0; i < surfaceSize; ++i) {
		cin >> numbers[i];
	}

	Dice dice(numbers[0]	// ?????¢
			, numbers[4]	// ?????¢
			, numbers[1]	// ?????¢
			, numbers[2]	// ??±??¢
			, numbers[3]	// ?\???¢
			, numbers[5]);	// ?????¢

	string commands;
	cin >> commands;
	for ( unsigned int i = 0; i < commands.size(); ++i ) {
		char& command  = commands[i];
		if ( command == 'N' ) {
			dice.rotateNorth();
		} else if ( command == 'S' ) {
			dice.rotateSouth();
		} else if ( command == 'E' ) {
			dice.rotateEast();
		} else if ( command == 'W' ) {
			dice.rotateWest();
		}
	}

	cout << dice.getTopNumber() << endl;

	return 0;
}


#include <iostream>
using namespace std;
void output(int i){
	cout << " " << i;
}
bool include3(int n){
	while(n){
		if (n % 10 == 3){
			return true;
		}
		n /= 10;
	}
	return false;
}
void call(int n){
	for(int i = 1; i <= n; i++){
		if(include3(i)){
			output(i);
		}else if(i % 3 == 0){
			output(i);
		}
	}
	cout << endl;
}

int main(int argc,char *argv[]){
	int n;
	cin >> n;
	call(n);
	return 0;
}

#include <iostream>

int main(){
    int d;

    while(std::cin >> d){
        int sum = 0;
        for(int i = d; i < 600; i += d){
            sum += i*i*d;
        }

        std::cout << sum << std::endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,ans;
    while(cin >> a >> b >> c >> d){
        ans = 0;
        ans+=min(a,b);
        ans+=min(c,d);
        cout << ans << endl;
    }
}

#include<stdio.h>
int main()
{
    int A, B, C, D, X, Y;
    scanf("%d%d%d%d",&A, &B, &C, &D);
    if(A>B){X=B;}
       else{
        X = A;
    }
    if (C>D){Y=D;}
        else {
        Y = C;
    }
    printf("%d\n", (X+Y) );

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pi>
#define vpl vector<pl>
#define pb push_back
#define INF 1000000005
#define LINF 1000000000000000005

int n,p[200005],r[200005];
ll d,a[200005],f[200005],g[200005],ans;

vector <pair<ll,pi>> s;

int Par(int nod){
	if(p[p[nod]]==p[nod]) return p[nod];
	else return p[nod]=Par(p[nod]);
}

void Unite(int x, int y){
	x=Par(x); y=Par(y);
	if(x==y) return;
	if(r[x]>r[y]) swap(x,y);
	if(r[x]==r[y]) r[y]++;
	p[x]=y;
}

void DC(int l, int r){
	if(l==r) return;
	int mid=(l+r)/2;
	DC(l,mid);
	DC(mid+1,r);
	int x=0,y=0;
	for(int i=l;i<=mid;i++){
		if(f[i]<f[x]) x=i;
	}
	for(int i=mid+1;i<=r;i++){
		if(g[i]<g[y]) y=i;
	}
	for(int i=l;i<=mid;i++){
		s.pb({f[i]+g[y],{i,y}});
	}
	for(int i=mid+1;i<=r;i++){
		s.pb({f[x]+g[i],{x,i}});
	}
}

int32_t main(){
	ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
#ifdef LOCAL_DEFINE
	ifstream cin("input.in");
#endif

	cin >> n >> d;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		p[i]=i;
		f[i]=-d*i+a[i];
		g[i]=d*i+a[i];
	}
	f[0]=g[0]=LINF;
	DC(1,n);
	sort(s.begin(),s.end());
	for(pair<ll,pi> i : s){
		if(Par(i.y.x)==Par(i.y.y)) continue;
		Unite(i.y.x,i.y.y);
		ans+=i.x;
	}
	cout << ans;

#ifdef LOCAL_DEFINE
	cerr << "\nTime elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << " \n";
#endif
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#ifndef ONLINE_JUDGE
  #define debug(x) cerr << #x << ": " << x << endl;
#else
  #define debug(x)
#endif

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

  ll N;
  cin >> N;

  vpll vl, vr;
  vll xmin(N, 0), xlast(N, 0);
  rep(i,N) {
    string s;
    cin >> s;
    for(auto c : s) {
      if(c =='(') xlast[i]++;
      else xlast[i]--;
      chmin(xmin[i], xlast[i]);
    }
    if(xlast[i] >= 0) vl.emplace_back(-xmin[i], i);
    else vr.emplace_back(xmin[i] - xlast[i], i);
  }

  sort(vl.begin(), vl.end());
  sort(vr.begin(), vr.end());

  ll tmin = 0, tlast = 0;
  for(auto p : vl) {
    ll i = p.second;
    chmin(tmin, tlast + xmin[i]);
    tlast += xlast[i];
  }
  for(auto p : vr) {
    ll i = p.second;
    chmin(tmin, tlast + xmin[i]);
    tlast += xlast[i];
  }

  if(tlast == 0 && tmin == 0) cout << "Yes" << endl;
  else cout << "No" << endl;
}