//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
using namespace std;
const ll MAX_N=1e5,MAX_X=1e5;
ll N,X,psm[MAX_N+2],inx[MAX_N+1];
typedef tuple<ll,ll,ll> TUP;
TUP blu[MAX_N+1];

ll tsm(ll i){
	return get<1>(blu[i])*get<0>(blu[i])+get<2>(blu[i])*(X-get<0>(blu[i]));
}
ll ptsm(ll i,ll n){
	return get<1>(blu[i])*min(n,get<0>(blu[i]))+get<2>(blu[i])*(n-min(n,get<0>(blu[i])));
}
bool C(ll x){
	ll n=x/X,m=x%X;
	ll sm=0;
	REP(i,N+1){
		ll tmp=psm[n];
		if(i<n){
			tmp-=tsm(i);
			tmp+=ptsm(i,m);
			tmp+=tsm(n);
		}else{
			tmp+=ptsm(i,m);
		}
		sm=max(sm,tmp);
	}
	REP(i,N)sm-=get<1>(blu[i])*get<0>(blu[i]);
	return sm>=0;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>N>>X;
	REP(i,N)cin>>get<0>(blu[i])>>get<1>(blu[i])>>get<2>(blu[i]);
	blu[N]=make_tuple(0,0,0);
	REP(i,N+1)inx[i]=i;
	sort(blu,blu+N+1,[](TUP x,TUP y){
		return get<1>(x)*get<0>(x)+get<2>(x)*(X-get<0>(x))
			>get<1>(y)*get<0>(y)+get<2>(y)*(X-get<0>(y));
	});
	psm[0]=0;
	REP(i,N+1){
		psm[i+1]=psm[i]+tsm(i);
	}
	ll lb=-1,ub=N*X;
	while(ub-lb>1){
		ll m=(lb+ub)>>1;
		if(C(m)){
			ub=m;
		}else{
			lb=m;
		}
	}
	cout<<ub<<endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repb(i, n) for (int i = (n-1); i >= 0; i--)
#define rea(i, a, b) for (int i = (a); i < (b); i++)
#define reab(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
#define PRINT(V) for (auto v:(V)) {cerr << v << " ";} cerr << endl
#define dump(x) cerr << #x << " : " << (x) << endl
#define dump2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define dump3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")" << endl
inline ll GCD(ll a, ll b){return b ? GCD(b, a % b) : a;}
const ll MOD = 1000000007;
const ll INF = 100000000000000000;
/* END OF TEMPLATE */

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    ll n, x;
    cin >> n >> x;
    vll b(n), l(n), u(n);
    rep(i, n){
        cin >> b[i] >> l[i] >> u[i];
    }
    vector<tuple<ll, ll, ll, ll>> vt;
    ll val = 0;
    rep(i, n){
        vt.emplace_back(b[i]*l[i]+(x-b[i])*u[i], u[i], l[i], b[i]);
        val -= l[i]*b[i];
    }
    sort(vt.rbegin(), vt.rend());
    vll acc;
    acc.emplace_back(0);
    rep(i, n){
        acc.emplace_back(*acc.rbegin()+get<0>(vt[i]));
    }
    // ans : 0 ~ nx

    // binary search
    // https://qiita.com/drken/items/97e37dd6143e33a64c8c
    ll bsl = -1;
    ll bsr = n*x;
    while(bsr - bsl > 1){
        // dump2(bsl, bsr);
        ll mid = bsl + (bsr - bsl) / 2;
        ll q = mid / x;
        ll r = mid % x;
        ll m = 0;
        rep(i, q){
            ll mt;
            if(r > get<3>(vt[i])){
                mt = get<2>(vt[i])*get<3>(vt[i]) + get<1>(vt[i])*(r-get<3>(vt[i]));
            }else{
                mt = get<2>(vt[i])*r;
            }
            // dump(mt);
            m = max(m, acc[q+1] - get<0>(vt[i]) + mt);
        }
        rea(i, q, n){
            ll mt;
            if(r > get<3>(vt[i])){
                mt = get<2>(vt[i])*get<3>(vt[i]) + get<1>(vt[i])*(r-get<3>(vt[i]));
            }else{
                mt = get<2>(vt[i])*r;
            }
            // dump(mt);
            m = max(m, acc[q] + mt);
        }
        if(m + val >= 0){
            bsr = mid;
        }else{
            bsl = mid;
        }
    }
    cout << bsr << endl;
}


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

using namespace std;

void solve() {
    int n, k, p;
    cin >> n >> k;
    vector <int> am;
    vector <int> dig(10, 0);

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

    bool flag = false;
    int N = n;
    while (!flag) {
        n = N;
        flag = true;
        while (n > 0) {
            if (dig[n % 10]) {
                flag = false;
                n = 0;
                ++N;
            } else {
                n /= 10;
            }
        }
    }
    cout << N;
}

int main() {

    solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define poly vector<ll>
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
inline ll read(){
	ll x=0;char ch=getchar();bool d=1;
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') d=0;
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return d?x:-x;
}
inline void write(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>=10) write(x/10);
	putchar('0'+x%10);
}
inline void writeln(ll x){write(x);puts("");}
inline void writep(ll x){write(x);putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=1e5+5;
int fa[N],m,ans;
poly v[N];
int dfs(int u,int dep){
	int mx=dep;
	for(auto k:v[u]){
		mx=max(mx,dfs(k,dep+1));
	}
	if(fa[u]!=1&&mx-dep==m-1) return ans++,0;
	return mx;
}
int main(){
	int n=read();m=read();
	For(i,1,n) fa[i]=read();
	For(i,2,n) v[fa[i]].pb(i);
	if(fa[1]!=1) ans=fa[1]=1;
	dfs(1,0);cout<<ans;
}


#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>

using ll = long long;

using namespace std;

ll mod=1e9+7;

int main() {
	int h,w;
	cin>>h>>w;
	vector<vector<bool>>map(h,vector<bool>(w));
	queue<pair<pair<int,int>,int>>todo;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			char c;
			cin>>c;
			if(c=='.'){
				map[i][j]=true;
			}else{
				todo.push(pair<pair<int,int>,int>(pair<int,int>(i,j),0));
			}
		}
	}
	int ans=0;
	int dx[]={1,0,-1,0};
	int dy[]={0,1,0,-1};
	while(todo.empty()!=1){
		ans++;
		auto tmp=todo.front();
		todo.pop();
		int x=tmp.first.first,y=tmp.first.second;
		int depth=tmp.second;
		ans=depth;
		for(int k=0;k<4;k++){
			int nx=x+dx[k];
			int ny=y+dy[k];
			if(nx<0||nx>=h||ny<0||ny>=w)continue;
			if(map[nx][ny]){
				map[nx][ny]=false;
				todo.push(pair<pair<int,int>,int>(pair<int,int>(nx,ny),depth+1));
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include<iostream>
using namespace std;

int n,k;
int d[10]={-1,-1,-1, -1, -1, -1, -1 ,-1,-1,-1};

bool check(int n){
  bool flag=true;
  int a;
  for(int i=n;i!=0;i/=10){
    a=i%10;
    for(int j=0;j<10;j++){
      if(a==d[j]) flag=false;
    }
  }
  return flag;
}

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

  for(int i=n;i<100000;i++){
    bool flag=false;
    flag=check(i);
    if(flag) {
      cout << i << '\n';
      break;
    }
  }

  return 0;
}


#include<stdio.h>

int main()
{
    int n,i,dig,flag=0;

    scanf("%d",&n);

    while(n!=0)
    {
        dig = n%10;
        n = n/10;

        if(dig==7)
            flag++;
    }

    if(flag>0)
        printf("Yes\n");
    else
        printf("No\n");

    return 0;
}

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

int main(){
  string n;
  cin>>n;
  bool a=true;
  for(int i=0;i<3;i++){
    if(n.at(i)=='7'){
      a=false;
    }
  }
  if(a){
    cout<<"No"<<endl;
  }
  else{
    cout<<"Yes"<<endl;
  }
}


#include<bits/stdc++.h>
#include <array>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for(UL i = 0; i < (n); i++)

struct Problem {

	static const ULL M = 1000000007;

	void Solve() {
		UL N; cin >> N;
		vector<ULL> F(N + 1); F[0] = 1;
		for (UL i = 1; i <= N; i++) F[i] = F[i - 1] * i % M;
		ULL X; cin >> X;
		vector<ULL> S(N); rep(i, N) cin >> S[i];
		sort(S.rbegin(), S.rend());
		vector<ULL> dp(X + 1);
		dp[X] = 1;
		rep(i, N) {
			UL t = N - i;
			vector<ULL> buf(X + 1);
			rep(j, X + 1) {
				if (j < S[i]) buf[j] += dp[j] * t;
				else {
					buf[j % S[i]] += dp[j];
					buf[j] += dp[j] * (t - 1);
				}
			}
			rep(j, X + 1) dp[j] = buf[j] % M;
		}
		ULL ans = 0;
		rep(i, X + 1) ans += dp[i] * i % M;
		ans %= M;
		cout << ans << endl;
	}

	Problem();
};
int main() {
	unique_ptr<Problem> p(new Problem());
	p->Solve();
	return 0;
}
Problem::Problem() {
	cout << fixed << setprecision(10);
}

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

// type definitions
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vi > vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef complex<int> ci;
typedef complex<double> cd;
typedef complex<long double> cld;
typedef long double ld;

// macros
#define rep(i, a, b) for(int i=a; i<b; i++)
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define all(c) c.begin(), c.end()
#define tr(c, it) for(auto it=(c).begin(); it!=c.end(); it++)
#define PB push_back
#define MP make_pair
#define bitcount __builtin_popcount
#define watch(x) cerr<< (#x) << " is " << (x) <<"\n";
#define sz(x) ((int)((x).size()))
#define UNIQUE(c) (c).resize(unique(all(c)) - (c).begin())
#define pii2ll(p) ((ll)(p).first<<32 | (p).second)

// template functions
template <typename T>
inline void printV(vector<T>& a) { rep(i, 0, sz(a)) { cout<<a[i]<<" "; } cout<<"\n"; }
template <typename T>
inline void printV(vector<T>& a, int n) { rep(i, 0, n) { cout<<a[i]<<" "; } cout<<"\n"; }
template <typename T>
inline void printVV(vector<vector<T>>& a)
{
	rep(i, 0, sz(a)) {
		printV(a[i]);
	}
	cout<<"\n";
}

template <typename T>
inline void clearVV(vector<vector<T>>& a)
{
	for(auto& x:a) {
		x.clear();
	}
	a.clear();
}
template <typename T>
inline void assignVV(vector<vector<T>> &a, T val)
{
	for(int i=0; i<sz(a); i++) {
		for(int j=0; j<sz(a[i]); j++) {
			a[i][j]=val;
		}
	}
}

// random number generation
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// constants
const long double eps = LDBL_EPSILON;
const int inf = 5e8;
const int modn = 1e9+7;
const int MAX = 1e5;

inline int add(int a, int b)
{
	int ret=((ll)a+b)%modn;
	if(ret<0) {
		ret=(ret+modn)%modn;
	}
	return ret;
}

inline int mul(int a, int b)
{
	int ret=((ll)a*b)%modn;
	if(ret<0) {
		ret=(ret+modn)%modn;
	}
	return ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Your code here
	int N, X;
	cin>>N>>X;
	vector<int> a(N+1);
	for(int i=1; i<=N; i++) {
		cin>>a[i];
	}
	sort(all(a));
	vector<vector<int>> dp(N+1, vector<int>(X+1, -1));
	function<int(int, int)> solve = [&](int n, int x)
	{
		if(n==1) {
			return x%a[n];
		}

		int &memo = dp[n][x];
		if(memo!=-1) return memo;

		int ans = 0;
		ans = add(ans, solve(n-1, x%a[n]));
		ans = add(ans, mul(n-1, solve(n-1, x)));
		return memo=ans;
	};
	int ans = solve(N, X);
	cout<<ans<<"\n";
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
//================================================

int a = 0, b = 0, c, d, n, k=0;
string s, t;

long long kaizyou(int x)
{
	if (x == 1)
		return 1;
	else
		return x * kaizyou(x - 1);
}

int main()
{
	cin >> n >> k;
	cout << n-k+1 << endl;
	return 0;
}

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

int main()
{
    char ch[20];
	int i=0;
	while(cin >> ch[i]){
		i++;
	}
	for(int j=i-1 ; j>=0 ; j--){
		cout<<ch[j];
	}
	cout<<endl;
	return 0;
}


//{{{
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// clang-format off
void _print() { cerr << "]\033[0m\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifdef LOCAL
#define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}

const int N = 20;
const int mod = 1e9 + 7;

int n;
int a[N];

vector<pii> words[N];

bool is_h(int status, int idx) { return (status & (1 << idx)) > 0; }
bool check(int status)
{
    for (int i = 0; i < n; i++)
    {
        // check ith person
        bool is_honest = is_h(status, i);
        if (is_honest == false) continue;

        for (int j = 0; j < a[i]; j++)
        {
            int p = words[i][j].first;
            int val = words[i][j].second;

            bool is_p_h = is_h(status, p);
            if (is_p_h != val) return false;
        }
    }
    return true;
}

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
#endif

    while (cin >> n)
    {
        for (int i = 0; i < n; ++i)
        {
            words[i].clear();
            cin >> a[i];

            for (int j = 0; j < a[i]; ++j)
            {
                int x, y;
                cin >> x >> y;
                x--;
                words[i].push_back({x, y});
            }
        }

        int ans = -1;
        int lim = 1 << n;
        for (int st = 0; st < lim; st++)
        {
            // if (__builtin_popcount(st) <= ans) continue;

            // check
            if (check(st))
            {
                ans = max(ans, __builtin_popcount(st));
            }
        }

        // int v = __builtin_popcount(ans);
        cout << ans << endl;
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define lint long long
#define inf 1000000007
#define mod 1000000007
#define MAX 100005
#define pb push_back
#define mp make_pair
#define vit vector<int>::iterator
typedef pair<int,int> pii;
vector<pii> V[MAX];
int pos[MAX],flg,visited[MAX];
void dfs(int u){
    visited[u]=1;
    for(auto v:V[u]){
        if(v.second){
            if(!pos[v.first]){
                pos[v.first]=1;
                dfs(v.first);
            }
            else if(pos[v.first]==1){
                if(!visited[v.first])
                    dfs(v.first);
            }
            else flg=1;
        }
        else{
            if(!pos[v.first]) pos[v.first]=2;
            else if(pos[v.first]==1) flg=1;
        }
    }
}
int possible(int k,int n){
    vector<int>vec;
    for(int i=0;i<(n-k);i++) vec.pb(0);
    for(int i=0;i<k;i++) vec.pb(1);
    do{
        flg=0;
        for(int i=1;i<=n;i++){
            visited[i]=0;
            pos[i]=vec[i-1];
        }
        for(int i=1;i<=n;i++){
            if(!visited[i] && vec[i-1]){
                dfs(i);
            }
        }
        if(!flg)
            return 1;

    }while(next_permutation(vec.begin(),vec.end()));
    return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;

    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        for(int j=0;j<a;j++){
            int x,y;
            cin>>x>>y;
            V[i+1].pb(mp(x,y));
        }
    }
    int lo,hi,mid;
    lo=0;
    hi=n;
    while(hi>lo){
        mid=(lo+hi+1)/2;
        if(possible(mid,n)) lo=mid;
        else hi=mid-1;
    }
    cout<<lo;
}


#include <bits/stdc++.h>
#include <iostream>
#include <math.h>    //pow(x,y)=x^y 99999とかの場合+1するif文
#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <ctime>     //実行時間を測る
#define ll long long
#define vecvec(h, w, name) vector<vector<ll>> name(h, vector<ll>(w));
#define rep(init, i, n) for (ll i = init; i < (n); i++)
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end()) //昇順
#define PI 3.14159265358979323846264338327950L
//コード自動整形 Shift + Option + F @vscode
//変数名一括置換　command+fn+f2 @vscode
using namespace std;
using vec = vector<ll>;
//function
ll facctorialMethod(ll k);     //階乗を出力する
ll modpow(ll a, ll n, ll mod); // a^n mod を計算する
ll gcd(ll a, ll b);            //最大公約数
ll lcm(ll a, ll b);            //最小公倍数
//

//struct
// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
struct UnionFind
{
  vector<ll> d;
  UnionFind(ll n = 0) : d(n, -1) {}
  ll find(ll x)
  { //根っこを返す
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]); //根っこの場所を貼り付ける
  }
  bool unite(ll x, ll y)
  { //くっつける操作、連結成分の情報整理 ABC177,157D参照
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y); //dはマイナスをかけたサイズだからxの方がサイズ小さいようにする
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  //find(i)でi番目の属しているグループ番号を出力
  bool same(ll x, ll y) { return find(x) == find(y); } //同じ集合に属しているかを判定
  ll size(ll x) { return -d[find(x)]; }                //あるID、xが所属するグループに所属するIDの数を得る ABC177,157D参照
};
//

/////main/////
int main()
{
  int clock_on = 0; //提出時は0!!!!!!!!!!!!!!!!!
  int time_clock = clock();
  //
ll K;string S;
cin>>K>>S;
if(S.size()<=K){cout<<S<<endl;return 0;}
else{
    cout<<S.substr(0,K)<<"..."<<endl;

}

  //
  if (clock_on == 1)
    printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - time_clock) / CLOCKS_PER_SEC);
  return 0;
}
/////main/////

/////function/////
//階乗を出力する
ll facctorialMethod(ll k)
{
  ll sum = 1;
  for (ll i = 1; i <= k; ++i)
  {
    sum *= i;
  }
  return sum;
}

// a^n mod を計算する
ll modpow(ll a, ll n, ll mod)
{
  ll res = 1;
  while (n > 0)
  {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

//最大公約数
ll gcd(ll a, ll b)
{
  if (a % b == 0)
  {
    return b;
  }
  else
  {
    return gcd(b, a % b);
  }
}

//最小公倍数
ll lcm(ll a, ll b)
{
  return a * b / gcd(a, b);
}

#include <bits/stdc++.h>
using namespace std;
int main(){
	int k;
	char arr[100];
	cin >> k;
	scanf("%s", arr);
	if(strlen(arr) <=k){
		printf("%s", arr);
	}
	else{
		for(int i=0; i<k; ++i){
			cout << arr[i];
		}
		cout << "...";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <numeric>
#include <cstdint>
#include <iomanip>
#include <set>
#include <map>
#include <unordered_map>
#include <cassert>
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define fast_io(); std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);

int main(){
    int n;
    std::cin >> n;
    std::vector<int> v(n), o(100001,0), e(100001,0);
    rep(i,n){
        std::cin >> v[i];
        if(i%2) o[v[i]]++;
        else e[v[i]]++;
    }

    auto io = std::distance(o.begin(),std::max_element(o.begin(),o.end()));
    auto ie = std::distance(e.begin(),std::max_element(e.begin(),e.end()));

    std::sort(o.begin(),o.end());
    std::sort(e.begin(),e.end());
    int ans = 0;
    if(io==ie){ //最大の値が同じとき
        ans = std::min(n-o[o.size()-1]-e[e.size()-2],n-o[o.size()-2]-e[e.size()-1]);
    }else{ //最大の値が違うとき
        ans = n - o.back() - e.back();
    }
    std::cout << ans << "\n";
    return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define l first
#define r second
#define int long long

int n;
long long a[500000];
vector<long long> multi;

int upper_bit(long long c)
{
    for (int i = 60; i >= 0; i--)
    {
        if (c & (1LL << i)) return i;
    }
    return -1;
}

signed main()
{
    cin >> n;
    long long x = 0;
    for (int i = 0; i < n; i++) cin >> a[i], x ^= a[i];
    for (int i = 0; i < n; i++) a[i] -= a[i] & x;
    for (int i = 0; i < n; i++)
    {
        int kek = 0;
        for (int j = 0; j < multi.size(); j++)
        {
            if (upper_bit(multi[j]) == upper_bit(a[i])) kek = 1;
        }
        if (kek)
        {
            long long cur = a[i];
            for (int i = 0; i < multi.size(); i++)
            {
                if (upper_bit(cur) == upper_bit(multi[i]))
                {
                    cur ^= multi[i];
                    if (cur == 0) break;
                }
            }
            if (cur != 0)
            {
                multi.push_back(cur);
                sort(multi.begin(), multi.end());
                reverse(multi.begin(), multi.end());
            }
        }
        else
        {
            multi.push_back(a[i]);
            sort(multi.begin(), multi.end());
            reverse(multi.begin(), multi.end());
        }
    }
    sort(multi.begin(), multi.end());
    reverse(multi.begin(), multi.end());
    long long res = 0;
    long long b = (1LL << 61);
    for (int i = 0; i < multi.size(); i++)
    {
        //cout << multi[i] << "\n";
        while (b > multi[i]) b /= 2;
        if ((res & b) == 0) res ^= multi[i];
    }
    cout << x + 2 * res;
}


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

int main() {
  long long n, x;
  cin >> n >> x;
  long long ans = 0, now = 0;
  for(int i = 0; i < n; i++){
    long long a;
    cin >> a;
    long long dis = max((long long)0, now + a - x);
    ans += dis;
    now = a - dis;
  }

  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MOD 1000000007
int main(){
	fast;
	ll n,x,ans=0,diff=0;
	cin>>n>>x;
	ll arr[n];
	for(ll i=0;i<n;i++)
		cin>>arr[i];
	for(ll i=1;i<n;i++)
		if(arr[i]+arr[i-1]>x)
		{
			diff=arr[i-1]+arr[i]-x;
			ans+=diff;
			if(arr[i]-diff>=0)
			arr[i]-=diff;
			else
			{
			diff-=arr[i];
			arr[i]=0;
			if(arr[i-1]-diff>=0)
			arr[i-1]-=diff;
			else
			arr[i-1]=0;
			}
		}
	cout<<ans;
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
/*================================*/
#if DEBUG
#define SIZE 1000
#else
#define SIZE 423450
#endif

int N,Q,K;

set<int> um[2][SIZE],dm[2][SIZE];
int BASE = SIZE/2;

struct Item2 {
    int x,y;
    char u;
};
vector<Item2> V;

set<int> ds[SIZE],ls[SIZE];

void solve() {
    cin>>N;
    int x,y;
    char u;
    REP(i,N) {
        cin>>x>>y>>u;
        if (u=='D') {
            dm[0][y-x+BASE].insert(x);
            dm[1][y+x].insert(x);
        }
        if (u=='U') {
            um[0][y-x+BASE].insert(x);
            um[1][y+x].insert(x);
        }
        V.push_back({x,y,u});
        if (u=='D') ds[x].insert(y);
        if (u=='L') ls[y].insert(x);
    }
    int ans = INT_MAX;
    REP(i,N) {
        auto p = V[i];
        if (p.u=='R') {
            {
                auto s = &dm[0][p.y-p.x+BASE];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) chmin(ans, (*it - p.x) * 10);
            }
            {
                auto s = &um[1][p.y+p.x];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) chmin(ans, (*it - p.x) * 10);
            }
            {
                auto s = &ls[p.y];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) {
                    chmin(ans, (*it - p.x) * 5);
                }
            }
        }
        if (p.u=='L') {
            {
                auto s = &um[0][p.y-p.x+BASE];
                auto it = s->lower_bound(p.x);
                if (it != s->begin()) chmin(ans, (p.x - *(--it))*10);
            }
            {
                auto s = &dm[1][p.y+p.x];
                auto it = s->lower_bound(p.x);
                if (it != s->begin()) chmin(ans, (p.x - *(--it))*10);
            }
        }
        if (p.u=='U') {
            auto s = &ds[p.x];
            auto it = s->upper_bound(p.y);
            if (it != s->end()) {
                chmin(ans, (*it - p.y)*5);
            }
        }
    }

    if (ans==INT_MAX) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }
}

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                            \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                        \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e8;

int g(vector<pair<int, int>> a) {
  sort(all(a));
  int res = INF;
  int py = -INF * 2;
  for (int i = 1; i < sz(a); i++) {
    if (a.at(i - 1).second == 0 && a.at(i).second != 0) {
      res = min(res, a.at(i).first - a.at(i - 1).first);
    }
  }
  return res;
}

int f(map<int, vector<pair<int, int>>>& mp) {
  int res = INF;
  for (auto p : mp) {
    res = min(res, g(p.second));
  }
  return res;
}

void solve(ll N, std::vector<ll> X, std::vector<ll> Y, std::vector<char> U) {
  vector<ll> dir(N);  // URDL = 0123

  rep(i, N) {
    if (U.at(i) == 'U') dir.at(i) = 0;
    if (U.at(i) == 'R') dir.at(i) = 1;
    if (U.at(i) == 'D') dir.at(i) = 2;
    if (U.at(i) == 'L') dir.at(i) = 3;
  }

  int ans = INF;

  rep(ri, 4) {
    {  // UD
      map<int, vector<pair<int, int>>> mp;
      rep(i, N) {
        if (dir.at(i) != 0 && dir.at(i) != 2) continue;
        mp[X.at(i)].emplace_back(Y.at(i), dir.at(i));
      }
      ans = min(ans, f(mp) * 5);
    }
    {  // UR
      map<int, vector<pair<int, int>>> mp;
      rep(i, N) {
        if (dir.at(i) != 0 && dir.at(i) != 1) continue;
        mp[X.at(i) + Y.at(i)].emplace_back(Y.at(i), dir.at(i));
      }
      ans = min(ans, f(mp) * 10);
    }
    // rotate
    rep(i, N) {
      int px = X.at(i), py = Y.at(i);
      // 反時計に90度回転
      X.at(i) = py;
      Y.at(i) = -px;
      dir.at(i) = (dir.at(i) + 1) % 4;
    }
  }

  if (ans != INF) {
    cout << ans << endl;
  } else {
    cout << "SAFE" << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll N;
  cin >> N;
  std::vector<ll> X(N);
  std::vector<ll> Y(N);
  std::vector<char> U(N);
  for (int i = 0; i < N; i++) {
    cin >> X.at(i);
    cin >> Y.at(i);
    cin >> U.at(i);
  }
  solve(N, std::move(X), std::move(Y), std::move(U));
  return 0;
}


/*G - Travelling Salesman Problem*/

/*F - PLUR @ EDC*/

/*A - Headphones */

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

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int a, b, c;
	int ans;
	int maximo;

	cin >> a >> b >> c;

	ans = a + b + c;
	maximo = max(a, b);
	maximo = max(maximo, c);

	ans -= maximo;

	cout << ans;

	return 0;
}

#include <iostream>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <cstdio>
#include <new>
#include <vector>
#include <cstdlib>
#include <string>
#include <set>
#include <tuple>
#include<map>

using namespace std;
int main() {
	string A, B, C;
	cin >> A >> B >> C;

	bool ans = true;

	if (A.at(A.size() - 1) != B.at(0)) ans = false;
	if (B.at(B.size() - 1) != C.at(0)) ans = false;

	if (ans) cout << "YES" << endl;
	else cout << "NO" << endl;

}


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

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

template <typename T>
struct RUQ //calculateはminimum
{
    int n;
    const T ex = 1 << 30; //dat 初期値(minなので)
    const T em = 1 << 30; //lazy 初期値(minなので)
    vector<T> dat, lazy; //tree本体
    RUQ(vector<T> v){
        int x = 1;
        int i;
        n = v.size();
        while(x < n) x <<= 1;
        n = x;
        dat.resize(2*n-1, ex);
        lazy.resize(2*n-1, em);
        for(i = 0;i < v.size();++i){
            dat[i + n - 1] = v[i];
        }
        for(i = n-2;i >= 0;--i){
            dat[i] = calculate(dat[2*i+1], dat[2*i+2]);
        }
    }

    T calculate(T a, T b){
        return min(a, b); //minで使う
    }

    void eval(int now){
        if(lazy[now] == em) return;
        if(now < n - 1){ //最小値なのでそのまま代入
            lazy[2*now + 1] = lazy[now];
            lazy[2*now + 2] = lazy[now];
        }
        dat[now] = lazy[now];
        lazy[now] = em;
    }

    void update(int a, int b, T x, int now, int l, int r){
        eval(now);
        if(a <= l && r<= b){
            lazy[now] = x; //上から書き換える。最小値なので代入する
            eval(now);
        }else if(a < r && l < b){
            update(a, b, x, 2*now + 1, l, (l + r)/2);
            update(a, b, x, 2*now + 2, (l + r)/2, r);
            dat[now] = calculate(dat[2*now + 1], dat[2*now + 2]);
        }
    }
    void update(int a, int b, T x){update(a, b, x, 0, 0, n);}

    T query(int a, int b){return query_sub(a, b, 0, 0, n);}
    T query_sub(int a, int b, int now, int l, int r){
        eval(now);
        if(r <= a || b <= l) return ex;
        else if(a <= l && r <= b) return dat[now];
        else{
            T vl = query_sub(a, b, 2*now + 1, l, (l + r)/2);
            T vr = query_sub(a, b, 2*now + 2, (l + r)/2, r);
            return calculate(vl, vr);
        }
    }

    inline T operator[](int a) { return query(a, a + 1); }
    void print() {
        for (int i = 0; i < 2 * n - 1; ++i) {
            cout << (*this)[i];
            if (i != n) cout << ",";
        }
        cout << endl;
    }
};

int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    int i;
    int h,w;
    cin >> h >> w;
    vector<int> v(w, 0);
    const int INF = 1e9;
    set<int> st;
    rep(i,w) st.insert(i);
    RUQ<int> ruq(v);
    rep(i,h){
        int a,b;
        cin >> a >> b;
        --a;    --b;
        auto x = st.lower_bound(a);
        auto y = st.upper_bound(b);
        if(x == y){
            ruq.update(a, b+1, INF);

            int ans = ruq.query(0, w);

            if(ans == INF) cout << -1 << endl;
            else cout << ans + i + 1 << endl;
        }else if(b+1 == w){
            st.erase(x, y);
            ruq.update(a, b+1, INF);

            int ans = ruq.query(0, w);

            if(ans == INF) cout << -1 << endl;
            else cout << ans + i + 1 << endl;
        }else{
            auto it = prev(y);
            int ind = *it;
            st.erase(x, y);
            st.insert(b+1);
            int tmp = ruq.query(ind, ind+1) + (b + 1 - ind);
            ruq.update(b+1, b+2, min(tmp, ruq.query(b+1, b+2)));
            ruq.update(a, b+1, INF);

            int ans = ruq.query(0, w);

            if(ans == INF) cout << -1 << endl;
            else cout << ans + i + 1 << endl;
        }
    }

    return 0;
}

#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<vector>
#include<climits>
#include<map>
#include<string>
#include<functional>
#include<iomanip>
#include<deque>
#include<random>
#include<set>
#include<bitset>
#include<cassert>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define chmax(x,y) x = max(x,y)
ll gcd(ll a, ll b) { if (a % b == 0) { return b; } else  return gcd(b, a % b); }
ll lcm(ll a, ll b) { if (a == 0) { return b; }return a / gcd(a, b) * b; }
template<class T>ll LBI(vector<T>& ar, T in) { return lower_bound(ar.begin(), ar.end(), in) - ar.begin(); }
template<class T>ll UBI(vector<T>& ar, T in) { return upper_bound(ar.begin(), ar.end(), in) - ar.begin(); }

ll h, w, a, b;
map<ll, ll> m;
multiset<ll> vals;

int main() {
	cin >> h >> w;
	rep(i, w) {
		m[i] = i;
		vals.insert(0);
	}
	rep(i, h) {
		cin >> a >> b;
		a--;
		auto it = m.lower_bound(a);
		ll mr = -1;
		while (it != m.end() && it->first <= b) {
			mr = it->second;
			vals.erase(vals.find(it->first - it->second));
			it = m.erase(it);
		}
		if (b == w)mr = -1;
		if (mr != -1) {
			m[b] = mr;
			vals.insert(b - mr);
		}
		if (vals.size()) {
			cout << *vals.begin() + i + 1 << endl;
		}
		else {
			cout << -1 << endl;
		}
	}
	return 0;
}


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

const int N = 1e6 + 100;

int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    vector<int> v;
    for(int i = 0; i < n+n; i++) {
        int x;
        cin >> x;
        v.push_back(x);
    }
    sort(v.begin(), v.end());
    int ans = 0;
    while(v.size() > 0) {
        v.pop_back();
        ans += v.back();
        v.pop_back();
    }
    cout << ans << endl;

    return 0;
}

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG //FOR THE DEBUG! COMMENT OUT THIS WHEN SUBMITTING!
#endif
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip>
#include <limits>//setprecision
//#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
//#include <unordered_set> //unordered_set
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
//It is so troublesome that I include bits/stdc++.h !
using namespace std;

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; }
const long long INF = 10010010010010;
const long long MOD = 1000000007;
typedef pair<int,int> P;

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % MOD, M / 2);
    //最下位ビット(*N)が1の時は単独でNをかける
    if (M & 1) res = (res * N) % MOD;

    return res %= MOD;
}

long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}

int main() {
    int N;cin >> N;
    vector<int> retu(N*2);
    for(int i = 0;i<N*2;i++) cin >> retu.at(i);
    sort(retu.begin(),retu.end());
    int ans = 0;
    for(int i = 0;i<N*2;i+=2){
        ans += min(retu.at(i),retu.at(i+1));
    }
    cout << ans << endl;
}


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

//GCD,LCM
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()
{
    string S;
    int w;
    cin>>S>>w;

    for(int i=0;i<S.length();i++){
        if(i%w==0){
            cout<<S[i];
        }
    }
    cout<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define N 1000010
inline int read() {
	int x=0;
	char ch=getchar();
	while (!isdigit(ch)) ch=getchar();
	while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return x;
}
int x[N];
char s[N],t[N];
int main() {
	int n=read(),p=0;
	scanf("%s%s",&s,&t);
	for (int i=n-1,j=n,k=0,l=0;i>=0;i--) {
	   	if (j>i && t[i]!=s[i]) j=i;
	   	if (j<=i && t[i]!=s[j]) {
			for (;j>=0 && t[i]!=s[j];j--);
			if (j<0) return puts("-1"),0;
			k++,l++,x[j+l]++,p=max(p,k);
		}
	   	k-=x[i+l],x[i+l]=0;
    }
    printf("%d\n",p);
	return 0;
}


// 去吧！皮卡丘! 把AC带回来！
//      へ　　　　　／|
// 　　/＼7　　　 ∠＿/
// 　 /　│　　 ／　／								AC		AC		AC		AC
//    │　Z ＿,＜　／　　 /`ヽ						AC		AC		AC		AC
//    │　　　　　ヽ　　 /　　〉					AC		AC		AC		AC
// 　Y　　　　　`　 /　　/  /						AC		AC		AC		AC
// 　亻●　.　●　　〈　/　/						AC		AC		AC		AC
// 　()　 へ　　　　|　＼〈							AC		AC		AC		AC
// 　　>- ._　 ィ　 │ ／／							AC		AC		AC		AC
// 　 / へ　　 /　/＜| ＼＼							AC		AC		AC		AC
// 　 ヽ_/　　(_／　 │／／							AC		AC		AC		AC
// 　　 7　　　　　　　|／							AC		AC		AC		AC
// 　　 ＞―r￣￣`-―＿/							AC		AC		AC		AC
//**************************************Accepted*****************************************//

#include<bits/stdc++.h>

#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define Unique(x) x.erase(unique(all(x)),x.end());

#define CIN_FILE "input.txt"
#define COUT_FILE "output.txt"

#define Pig (OO)

using namespace std;

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

const int Days[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};

inline int read()
{
	#define gc getchar
	int ans=0;
	bool f=1;
	char ch=gc();

	while(!isdigit(ch))f^=ch=='-',ch=gc();

	while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();

	return f?ans:-ans;

	#undef gc
}

typedef long long ll;

int n,m;
vector<int> v[1010];
int vis[1010];
vector<int> path;
int indeg[1010],outdeg[1010];
bool edges[1010][1010];

bool OK()
{
	memset(indeg,0,sizeof(indeg));
	memset(outdeg,0,sizeof(outdeg));

	for(int i=0;i<path.size();i++)for(int j=0;j<path.size();j++)
	{
		if(indeg[path[i]]>1||outdeg[path[j]]>1)return 0;

		if(edges[path[i]][path[j]])indeg[path[i]]++,outdeg[path[j]]++;
	}

	for(int i=0;i<path.size();i++)if(indeg[path[i]]!=1||outdeg[path[i]]!=1)return 0;

	return 1;
}

void pprint()
{
	if(!OK())return;

	cout<<path.size()<<endl;for(int i=0;i<path.size();i++)cout<<path[i]<<endl;

	exit(0);
}

void DFS(int x)
{
	path.pb(x);
	vis[x]=2;

	for(int i=0;i<v[x].size();i++)
	{
		if(vis[v[x][i]]==0)DFS(v[x][i]);
		if(vis[v[x][i]]==2)pprint();
	}

	path.pop_back();
	vis[x]=1;
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);

	cin>>n>>m;

	for(int i=0;i<m;i++)
	{
		int x,y;
		cin>>x>>y;

		v[x].pb(y);

		edges[x][y]=1;
	}

	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof(vis));
		path.clear();

		if(!vis[i])DFS(i);
	}

	cout<<-1;

	//printf("Time used = %.12f",(double)(clock())/CLOCKS_PER_SEC);
	return 0;
}

/*  GOD BLESS  */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)

const int n_max=1e3+10;
const int inf=INT_MAX/4;
vector<int> g[n_max];
vector<int> rg[n_max];

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

  int n,m;
  cin >> n >>m;

  rep(i,m){
    int a,b;
    cin >> a >> b;
    --a;--b;
    g[a].pb(b);
    rg[b].pb(a);
  }
  int min_loop=inf;
  int min_ind=-1;
  {
  rep(i,n){
    vector<int> dp(n,inf);
    queue<int> qu;
    dp[i]=0;
    qu.push(i);
    while(!qu.empty()){
      int v = qu.front();qu.pop();
      for(auto nv:g[v]){
	if(nv==i){
	  if(min_loop>dp[v]+1){
	    min_loop = dp[v]+1;
	    min_ind = i;
	    //	    cout << "minloop ind " << min_loop << " " << min_ind << endl;
	  }
	}
	if(dp[nv] > dp[v]+1){
	  dp[nv] = dp[v]+1;
	  qu.push(nv);
	}
      }
    }
  }
  }
  if(min_ind==-1){
    cout << -1 << endl;
    return 0;
  }
  else{
    cout << min_loop << endl;
  }
  vector<int> dp(n,inf);
  queue<int> qu;
  dp[min_ind]=0;
  qu.push(min_ind);
  bool ok=true;
  while(!qu.empty()){
    int v = qu.front();qu.pop();
    for(auto nv:g[v]){
      if(nv==min_ind){
	dp[min_ind]=dp[v]+1;
	ok=false;
	break;
      }
      if(dp[nv] > dp[v]+1){
	dp[nv] = dp[v]+1;
	qu.push(nv);
      }
    }
    if(!ok) break;
  }
  //  cout << "ind dp " << min_ind << endl;
  //  cout << dp[min_ind] << endl;
  vector<int> ans;
  ans.pb(min_ind);
  while(ans.size()!=min_loop){
    for(auto i:rg[min_ind]){
      if(dp[i] == dp[min_ind]-1){
	//	cout << i << endl;
	ans.pb(i);
	min_ind = i;
	break;
      }
    }
  }
  rep(i,min_loop){
    cout << ans[i]+1 << endl;
  }

  return 0;

}


#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define getbit(x, i) (((x) >> (i)) & 1)
using namespace std;
typedef pair<int, int> pii;
#define hashset unordered_set
#define hashmap unordered_map
#define newline fast_writechar('\n')
#define unify(arr) arr.resize(unique(arr.begin(), arr.end()) - arr.begin())
#define getbit(x, i) (((x) >> (i)) & 1)

template <typename T>
vector<T> readvector(size_t sz) {
    vector<T> res(sz);
    for (size_t i = 0; i < sz; ++i) {
        cin >> res[i];
    }
    return res;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, " "));
    return out;
}

int mylog(int n) {
    return 63 - __builtin_clzll(n);
}

inline int binPow(int x, int deg, int mod) {
    int ans = 1;
    for (int i = sizeof(deg) * CHAR_BIT - 1; i >= 0; i--) {
        ans *= ans;
        ans %= mod;
        if (getbit(deg, i)) ans *= x;
        ans %= mod;
    }
    return ans;
}

/** Interface */

inline int readInt();
inline int readUInt();
inline void readWord(char *s);
inline int fast_readchar();  // you may use readchar() instead of it

inline void writeInt(int x);
inline void fast_writechar(int x);  // you may use putchar() instead of it
inline void writeWord(const char *s);
inline void fast_flush();

// ====================== END ======================
const int MAXN = 2e6 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e18;

void solve() {
    int n;
    cin >> n;
    int cnt = 0;
    int mn = 1e9;
    for (auto e : readvector<int>(n)) {
        cnt += e < mn;
        mn = min(mn, e);
    }
    cout << cnt << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // t = readInt();
    // cin >> t;
    for (int i = 1; i <= t; ++i) {
        // cout << "Case #" << i << ": ";
        solve();
    }
    // fast_flush();
    return 0;
}

/** Read */

static const int buf_size = 4096;

inline int fast_readchar() {
    static char buf[buf_size];
    static int len = 0, pos = 0;
    if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);
    if (pos == len) return -1;
    return buf[pos++];
}

inline int readUInt() {
    int c = fast_readchar(), x = 0;
    while (c <= 32) c = fast_readchar();
    while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = fast_readchar();
    return x;
}

inline int readInt() {
    int s = 1, c = fast_readchar();
    int x = 0;
    while (c <= 32) c = fast_readchar();
    if (c == '-') s = -1, c = fast_readchar();
    while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = fast_readchar();
    return x * s;
}

inline void readWord(char *s) {
    int c = fast_readchar();
    while (c <= 32) c = fast_readchar();
    while (c > 32) *s++ = c, c = fast_readchar();
    *s = 0;
}

/** Write */

static int write_pos = 0;
static char write_buf[buf_size];

inline void fast_writechar(int x) {
    if (write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
    write_buf[write_pos++] = x;
}

inline void fast_flush() {
    if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}

inline void writeInt(int x) {
    if (x < 0) fast_writechar('-'), x = -x;

    char s[24];
    int n = 0;
    while (x || !n) s[n++] = '0' + x % 10, x /= 10;
    while (n--) fast_writechar(s[n]);
}

inline void writeWord(const char *s) {
    while (*s) fast_writechar(*s++);
}

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

int main() {
    int N;cin>>N;
    int mini=N+1;
    int ans=0;
    for (int i = 0; i < N; i++){
        int P;cin>>P;
        if(P<=mini)ans++;
        mini=min(mini,P);
    }
    cout<<ans<<endl;
}


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

int H, W;

const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};

int main(void){
	cin >> H >> W;
	vector<vector<char>> m(H+2, vector<char>(W+2));
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			cin >> m[i][j];
		}
	}
	bool ans = true;
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			if(m[i][j] == '#'){
				if(m[i-1][j] == '.' && m[i+1][j] == '.' && m[i][j-1] == '.' && m[i][j+1] == '.') ans = false;
			}
		}
	}
	if(ans) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,n) for (int64_t i=0; i<(n); ++i)
#define P pair<int,int>
using ll=int64_t;
using namespace std;
#define ketasuu(n) fixed<<setprecision(n)
#define btoe(p) p.begin(),p.end()
#define etob(p) p.rbegin(),p.rend()

//UnionFind Template
struct UnionFind{ //構造体の定義
    vector<int> d;
    UnionFind(int n=0): d(n,-1) {} //コンストラクタ
    int find(int x){
        if(d[x]<0) return x;
        return d[x]=find(d[x]);
    }
    bool unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return false;
        if(d[x]<d[y]) swap(x,y);//d[x] is smaller. Then, connect d[x] to d[y].
        d[y]+=d[x];
        d[x]=y;
        return true;
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return -(d[find(x)]);
    }
    int rootsize(){
        int cnt=0;
        for(auto& v: d){
            if(v<0) cnt++;
        }
        return cnt;
    }
};
//How to use
//UnionFind uf(n); などと書けば使えるようになる.ufというobjectが生成される
//uf.unite(a,b);などで中の関数を使える

int main(){
    int h,w; cin>>h>>w;
    const vector<int> df={-1,1,w,-w};
    vector<string> p(h);
    rep(i,h) cin>>p[i];
    UnionFind uf(h*w);
    rep(i,h*w){
        if(p[i/w][i%w]=='.') continue;
        rep(j,4){
            int a=i+df[j];
            if(a<0 || a>=h*w || (a&w==0 && j==1) || (i%w==0 && j==0)) continue;
            if(p[a/w][a%w]=='#') uf.unite(i,a);
        }
    }
    bool ok=true;
    rep(i,h*w){
        if(uf.size(i)==1 && p[i/w][i%w]=='#') ok=false;
    }
    cout<<(ok? "Yes": "No")<<endl;
    return 0;
}


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

int levenshtein(string a, string b) {
  int alen = a.length(), blen = b.length();
  int dp[alen+1][blen+1];

  for (int i = 0; i <= alen; i++) dp[i][0] = i;
  for (int j = 0; j <= blen; j++) dp[0][j] = j;

  int l, m, r;
  for (int i = 1; i <= alen; i++) {
    for (int j = 1; j <= blen; j++) {
      l = dp[i-1][j] + 1;
      r = dp[i][j-1] + 1;
      if (a[i-1] == b[j-1]) {
        m = dp[i-1][j-1];
      } else {
        m = dp[i-1][j-1] + 1;
      }
      dp[i][j] = min(l, min(m, r));
    }
  }

  return dp[alen][blen];
}

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

  cout << levenshtein(s1, s2) << endl;

  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

//#define N 2000

template<size_t N>
const bitset<N> operator+(const bitset<N>& lhs, const bitset<N>& rhs)
{
    bitset<N> a(lhs), b(rhs), ret(lhs ^ rhs);

    for (b = (a & b) << 1, a = ret; b.any(); b = (a & b) << 1, a = ret) ret ^= b;

    return ret;
}

template<size_t N>
int edit_distance_bitset(const string& str1, const string& str2)
{
    char s1[N] = { ' ' };
    char s2[N] = { ' ' };
    char *p1, *p2;

    if (str1.size() >= str2.size()) { p1 = s1;  p2 = s2; }
    else { p1 = s2;  p2 = s1; }
    copy(str1.begin(), str1.end(), p1 + 1);
    copy(str2.begin(), str2.end(), p2 + 1);
    int m = strlen(s1);
    int n = strlen(s2);

    const bitset<N> ONE(1);
    bitset<N> Peq[256];
    bitset<N> Pv, Mv, Eq, Xv, Xh, Ph, Mh;
    int Score = m;

    for (int i = 0; i < m; i++) {
        Peq[s1[i]] |= ONE << i;
        Pv |= (ONE << i);
    }
    for (int j = 0; j < n; j++) {
        Eq = Peq[s2[j]];
        Xv = Eq | Mv;
        Xh = (((Eq & Pv) + Pv) ^ Pv) | Eq;
        Ph = Mv | ~(Xh | Pv);
        Mh = Pv & Xh;
        if ((Ph & (ONE << (m - 1))).any()) Score++;
        else if ((Mh & (ONE << (m - 1))).any()) Score--;
        Ph = Ph << 1 | ONE;
        Pv = (Mh << 1) | ~(Xv | Ph);
        Mv = Ph & Xv;
    }

    return Score;
}

int main(){
	string a,b;
  cin>>a>>b;
  cout<<edit_distance_bitset<2000>(a,b)<<endl;
  return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define out(x) cout << x << endl;
#define INF 1ll<<60;
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; }

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

int main(){
    vector<string> s(3);
    rep(i, 3) cin >> s[i];

    int turn = 0;

    while(true){
        if(s[turn].empty()){
            cout << char('A'+turn) << endl;
            return 0;
        }
        int n_turn = s[turn][0] - 'a';
        s[turn].erase(0, 1);
        turn = n_turn;
    }

    return 0;
}

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

void solve()
{
    int n, k;
    cin >> n >> k;
    int ans = 1;
    rep(i, n)
    {
        ans = min(ans * 2, ans + k);
    }
    cout << ans << endl;
}

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a)  (a).begin(),(a).end()
const ll INF = 1e18+18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
static const double pi = 3.141592653589793;

int main(){
    ll N, M;
    cin>>N>>M;
    vector<ll> A(N);
    map<ll, ll> B;
    set<ll> Q;
    B[0]=1;
    ll Sum=0;
    rep(i, N){
        cin>>A[i];
        Sum+=A[i];
        Sum%=M;
        Q.insert(Sum);
        B[Sum]++;
    }
    ll Ans=0;
    for (auto itr = Q.begin(); itr != Q.end(); ++itr) {
        Ans+=B[*itr]*(B[*itr]-1)/2;
    }
    cout<<Ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define INF (1ll<<60)
#define mod 1000000007
#define P pair<ll,ll>
#define Graph vector<vector<ll>>
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; }

vector<ll> sum;
void pre_process(vector<ll> &a,vector<ll> &s){//累積和のvector
    ll n=(ll)a.size();
    s.assign(n+1,0);
    for(ll i=0;i<n;i++){
        s[i+1]=s[i]+a[i];
    }
}
ll query(ll i,ll j,vector<ll> &s){//区間[i,j]の和
    return (s[j]-s[i]);
}

int main(){
    ll n,m;cin>>n>>m;
    vector<ll> a(n);
    rep(i,n) cin>>a[i];

    pre_process(a,sum);

    vector<ll> cnt;
    rep(i,n+1){
        if(i==0) continue;
        cnt.push_back(sum[i]%m);
    }
    sort(cnt.begin(),cnt.end());
    cnt.push_back(-1ll);

    ll num=1;
    ll answer=0;
    rep(i,cnt.size()-1){
        if(cnt[i]==cnt[i+1]){
            num++;
        }else{
            if(cnt[i]==0){
                answer+=num*(num+1)/2;
            }else{
                answer+=(num-1)*num/2;
            }
            num=1;
        }
    }

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


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

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

    vector<long long int> l(2*n);
    for(int i=0; i<2*n; i++){
        cin >> l[i];
    }
    sort(l.begin(),l.end());

    long long int ans = 0;
    for(int i=0; i<n; i++){
        ans += l[2*i];
    }
    cout << ans << endl;
    return 0;
}

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

int getResult(vector<int> &L, const int size){
  sort(L.begin(), L.end());
  int sum = 0;
  for(int i = 0; i < size; i += 2){
    sum += L[i];
  }
  return sum;
}

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

  const int N2 = N * 2;
  vector<int> L(N2);
  for(int i = 0; i < N2; i++){
    cin >> L[i];
  }

  int result = getResult(L, N2);
  cout << result;
  return 0;
}


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

vector<P> G[1001][1001];
int dp[1001][1001];

vector<P> tp;

int dfs(int x,int y,int px=1000,int py=1000)
{
	if(dp[x][y]!=0)return dp[x][y];
	if(G[x][y].empty())return dp[x][y] = 1;
	for(auto i:G[x][y])
	{
		dp[x][y] = max(dp[x][y],dfs(i.first,i.second,x,y)+1);
	}
	return dp[x][y];
}

int main()
{
	int n;
	cin >> n;
	vector<vector<P>> input(n);
	map<P,int> h;//入次数
	for(int i = 0;i<n;i++)
	{
		for(int j = 0;j<n-1;j++)
		{
			int x=i;
			int y;
			cin >> y;
			y--;
			if(x>y)swap(x,y);
			input[i].emplace_back(x,y);

		}
	}
	for(int i = 0;i<n;i++)
	{
		for(int j =0;j<n-2;j++)
		{
			h[input[i][j+1]]++;
			G[input[i][j].first][input[i][j].second].emplace_back(input[i][j+1]);
		}
	}
	stack<P> S; //入次数がゼロの頂点
	for(int i = 0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			if(h[P(i,j)]==0)
			{
				S.emplace(i,j);
			}
		}
	}
	while(S.size())
	{
		auto now = S.top();S.pop();
		tp.push_back(now);
		for(auto i:G[now.first][now.second])
		{
			h[i]--;
			if(h[i]==0)S.emplace(i);
		}
	}
	if(tp.size()!=n*(n-1)/2)
	{
		cout << -1 << endl;
		return 0;
	}

	int ans = 0;
	for(int i = 0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			ans = max(ans,dfs(i,j));
		}
	}
	cout << ans << endl;
}

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

int main()
{
  int n;
  cin >> n;
  vector<queue<int>> a(n);
  rep(i, n)
    rep(j, n - 1)
    {
      int ipt;
      cin >> ipt;
      ipt--;
      a[i].push(ipt);
    }
  set<pair<int, int>> s;
  rep(i, n - 1)
  {
    int op = a[i].front();
    if (i == a[op].front() && i < op)
      s.emplace(i, op);
  }
  int ans = 0;
  while (!s.empty())
  {
    ans++;
    set<pair<int, int>> s2;
    while (!s.empty())
    {
      auto p = *s.begin();
      s.erase(s.begin());
      a[p.first].pop();
      a[p.second].pop();
      if (!a[p.first].empty())
      {
        int op = a[p.first].front();
        if (!a[op].empty() && p.first == a[op].front())
          s2.emplace(min(p.first, op), max(p.first, op));
      }
      if (!a[p.second].empty())
      {
        int op = a[p.second].front();
        if (!a[op].empty() && p.second == a[op].front())
          s2.emplace(min(p.second, op), max(p.second, op));
      }
    }
    s.swap(s2);
  }
  rep(i, n)
    if (!a[i].empty())
    {
      puts("-1");
      return 0;
    }
  cout << ans << 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;

template<class T>
T gcd(T a, T b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}

int main() {
	cin.tie(0)->sync_with_stdio(false);

	ll a, b; cin >> a >> b;
	ll n = gcd(a, b);

	int res = 1;
	for (ll i = 2; i * i < n; i++) {
		if (n % i != 0) continue ;
		res++;
		while (n % i == 0)
			n /= i;
	}

	if (n != 1) res++;
	cout << res << '\n';
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

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

// 素因数分解 O(√n)
// たとえば 60 = 2^2 x 3 x 5 だったら {(2, 2), (3, 1), (5, 1)} を返す
// 素因数の個数はlogn
vector<pair<ll,ll>> prime_factorize(ll n){
    vector<pair<ll,ll>> res;
    for(ll p=2;p*p<=n;p++){
        if(n%p!=0) continue;
        int num=0; // 指数
        while(n%p==0){
            num++;
            n/=p;
        }
        res.push_back({p,num});
    }
    if(n!=1) res.push_back({n,1});
    return res;
}

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

    ll g=gcd(a,b);

    if(g==1){
        cout<<1<<endl;
        return 0;
    }

    auto pf=prime_factorize(g);
    cout<<pf.size()+1<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1000001

vi adj[2][N];

int vis[N];
int vit;
int wh[2][N];

void d1(int v){
    vis[v] = vit;
    wh[0][v] = vit;
    FORR(i,adj[0][v]){
        if(vis[i] != vit)
            d1(i);
    }
}

void d2(int v){
    vis[v] = vit;
    wh[1][v] = vit;
    FORR(i,adj[1][v]){
        if(vis[i] != vit)
            d2(i);
    }
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,k,l;
    sc(n,k,l);
    int f,s;
    while(k--){
        sc(f,s);
        --f,--s;
        adj[0][f].pb(s);
        adj[0][s].pb(f);
    }
    while(l--){
        sc(f,s);
        --f,--s;
        adj[1][f].pb(s);
        adj[1][s].pb(f);
    }
    FOR(i,0,n){
        if(!vis[i]){
            ++vit;
            d1(i);
        }
    }
    memset(vis,0,n<<2);
    FOR(i,0,n){
        if(!vis[i]){
            ++vit;
            d2(i);
        }
    }
    map<pi,int> st;
    FOR(i,0,n){
        ++st[mp(wh[0][i],wh[1][i])];
    }
    FOR(i,0,n){
        cout << st[mp(wh[0][i],wh[1][i])] << ' ';
    }
}

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

vector<int> pa, pb;

int find_roota(int x) {
  if (pa[x] != x) pa[x] = find_roota(pa[x]);
  return pa[x];
}

void unitea(int x, int y) {
  int nx = find_roota(x);
  int ny = find_roota(y);
  if (nx == ny) return;
  else if (nx > ny) pa[nx] = ny;
  else pa[ny] = nx;
}

int find_rootb(int x) {
  if (pb[x] != x) pb[x] = find_rootb(pb[x]);
  return pb[x];
}

void uniteb(int x, int y) {
  int nx = find_rootb(x);
  int ny = find_rootb(y);
  if (nx == ny) return;
  else if (nx > ny) pb[nx] = ny;
  else pb[ny] = nx;
}

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

  pa = vector<int>(n);
  pb = vector<int>(n);
  for (int i = 0; i < n; i ++) pa[i] = pb[i]= i;
  for (int i = 0; i < k; i ++) {
    int x, y;
    cin >> x >> y;
    x --, y --;
    unitea(pa[x], pa[y]);
  }
  for (int i = 0; i < l; i ++) {
    int x, y;
    cin >> x >> y;
    x --, y --;
    uniteb(pb[x], pb[y]);
  }
  for (int i = 0; i < n; i ++) {
    find_roota(i);
    find_rootb(i);
  }

  map<pair<int, int>, int> mp;
  for (int i = 0; i < n; i ++) mp[make_pair(pa[i], pb[i])] ++;

  for (int i = 0; i < n; i ++) {
    if (i) cout << " ";
    cout << mp[make_pair(pa[i], pb[i])];
  }
  cout << endl;
}


#include <bits/stdc++.h>

#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

bool isgood(vector<string> bd, int x, int y){
    int cnt = 0;

    //lateral direction
    rep(i, 8)if(bd[x][i] == 'Q')cnt++;
    if(cnt != 1)return false;

    //longitudinal direction
    cnt = 0;
    rep(i, 8)if(bd[i][y] == 'Q')cnt++;
    if(cnt != 1)return false;

    // //diagonally upward
    cnt = 0;
    for(int i = 1; x+i < 8 && y+i < 8; ++i){
        if(bd[x+i][y+i] == 'Q')return false;
    }
    for(int i = 1; x-i >= 0 && y-i >= 0; ++i){
        if(bd[x-i][y-i] == 'Q')return false;
    }

    //diagonally downward
    cnt = 0;
    for(int i = 1; x+i < 8 && y-i >= 0; ++i){
        if(bd[x+i][y-i] == 'Q')return false;
    }
    for(int i = 1; x-i >= 0 && y+i < 8; ++i){
        if(bd[x-i][y+i] == 'Q')return false;
    }

    return true;
}

int main(){
    ll k;
    cin >> k;

    vector<int> c(8, -1);
    rep(i, k){
        int r;
        cin >> r;
        cin >> c[r];
    }

    vector<ll> ary(8);
    iota(ALL(ary), 0);

    vector<string> board(8);
    fill(ALL(board), "........");

    do{
        vector<string> board_cp = board;
        vector<int> pos(8);

        rep(i, 8){
            if(c[i] < 0)pos[i] = ary[i];
            else pos[i] = c[i];

            board_cp[i][pos[i]] = 'Q';
        }

        bool isOk = true;

        rep(i, 8){
            isOk &= isgood(board_cp, i, pos[i]);
        }

        if(isOk){
            board = board_cp;
            break;
        }

    }while(next_permutation(ALL(ary)));

    rep(i, 8){
        rep(j, 8)cout << board[i][j];
        cout << endl;
    }
}

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
#include <chrono>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T>inline bool chmax(T &a, T b){return (a < b ? a = b, true : false);}
template<typename T>inline bool chmin(T &a, T b){return (a > b ? a = b, true : false);}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const long double EPS = 1e-9;

template<typename T>
class RangeUpdateQuery {
private:
	vector<T> data, lazy;
	vector<bool> valid, lazyFlag;
	int sz;
	void push(int k) {
		if (this->lazyFlag[k]) {
			this->valid[k] = true;
			this->data[k] = this->lazy[k];
			if (k * 2 + 2 < sz * 2 - 1) {
				this->lazy[k * 2 + 1] = this->lazy[k * 2 + 2] = this->lazy[k];
				this->lazyFlag[k * 2 + 1] = this->lazyFlag[k * 2 + 2] = true;
			}
			this->lazyFlag[k] = false;
		}
	}
	void update(int l, int r, int L, int R, int k, T val) {
		this->push(k);
		if (L <= l && r <= R) {
			this->lazy[k] = val;
			this->lazyFlag[k] = true;
			this->push(k);
		} else if (r < L || R < l) {
			return;
		} else if (r - l > 1) {
			const int mid = (l + r) / 2;
			this->update(l, mid, L, R, k * 2 + 1, val);
			this->update(mid, r, L, R, k * 2 + 2, val);
		}
	}
	T query(int l, int r, int idx, int k) {
		this->push(k);
		if (r - l == 1) {
			if (!valid[k]) {
				throw exception();
			}
			return this->data[k];
		} else if (l <= idx && idx <= r) {
			const int mid = (l + r) / 2;
			if (idx < mid) {
				return this->query(l, mid, idx, k * 2 + 1);
			} else {
				return this->query(mid, r, idx, k * 2 + 2);
			}
		}
	}
public:
	RangeUpdateQuery(const int n) {
		this->sz = 1;
		while (this->sz < n) this->sz <<= 1;
		this->data.resize(this->sz * 2 - 1);
		this->lazy.resize(this->sz * 2 - 1);
		this->valid.resize(this->sz * 2 - 1, false);
		this->lazyFlag.resize(this->sz * 2 - 1, false);
	}
	void update(int l, int r, T val) {
		this->update(0, sz, l, r, 0, val);
	}
	T query(int idx) {
		return this->query(0, sz, idx, 0);
	}
};

int main() {
	int n, q;
	cin >> n >> q;
	RangeUpdateQuery<int> ruq(n);
	while (q--) {
		int num; cin >> num;
		if (num) {
			int idx; cin >> idx;
			try {
				cout << ruq.query(idx) << endl;
			} catch (exception e) {
				cout << (1ll << 31) - 1 << endl;
			}
		} else {
			int s, t, x;
			cin >> s >> t >> x;
			ruq.update(s, t + 1, x);
		}
	}
}


#include <bits/stdc++.h>

#define For(i, j, k) for(int i = j; i <= k; i++)
#define Forr(i, j, k) for(int i = j; i >= k; i--)

using namespace std;

const int N = 1e6 + 10;

char S[N], T[N];
int n;
int delt[N];

int main(){
	scanf("%d%s%s", &n, S + 1, T + 1);

	bool flag = true;
	For(i, 1, n) if(S[i] ^ T[i]){
		flag = false;
		break;
	}
	if(flag){
		puts("0");
		return 0;
	}

	int ans = 0, cnt = 0, d = 0;
	int j = n;
	Forr(i, n, 1){
		cnt -= delt[i + d], delt[i + d] = 0;
		ans = max(ans, cnt + 1);

		//printf("%d %d %d\n", i, d, cnt);

		if(T[i] == T[i - 1]) continue;

		if(j > i) j = i;
		while(j && S[j] != T[i]) --j;
		if(!j){
			puts("-1");
			return 0;
		}

		++cnt, ++d;
		delt[j - 1 + d]++;
	}

	printf("%d\n", ans);
	return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>

/*
*    coder :: ATUL_PANDEY_2608
*        >>> INDIA <<<
*/
using namespace std;
using namespace __gnu_pbds;

// #define  part ..
#define pb(a) push_back(a)
#define all(a) a.begin(),a.end()
#define mod 1000000007
//#define maxx 200006
#define ll long long
#define quick    ios_base::sync_with_stdio(NULL),cin.tie(0);
#define listll vector< long long >
#define listi vector< int>
#define pii   pair<int , int>
#define pll pair<long long , long long >
#define minheap priority_queue<long long , vector< long long >, greater<long long > >
#define rep(i,a,b) for(int i=a;i<b;i++)
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

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

// end of #define

// define globals ...
const int maxx = 2e5+26;

//write function from here ...

int Main(){

int n =3;
ll arr[n];
for(int i=0 ; i  <n;i++)
cin>>arr[i];

sort(arr,arr+n);

int cnt  =0 ;
for(int i=0;i<=1;i++)
{

	if(arr[i]==arr[i+1])
	cnt++;

}

if(cnt==1)
cout<<"Yes\n";
else
cout<<"No\n";

return 0;

}

int main(){
 quick;
int t =1;
//cin>>t;
while(t-- )
 Main();

 return 0;

}


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

struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef queue<int> qi;
typedef stack<int> si;
typedef deque<int> dqi;

#define reps(i, a, b) for (int i = a; i < b; ++i)
#define rep(i, n) reps(i, 0, n)
#define REPS(i, a, b) reps(i, a, b)
#define REP(i, n) rep(i, n)
#define deps(i, a, b) for (int i = a; i >= b; --i)
#define dep(i, n) deps(i, n, 0)
#define DEPS(i, a, b) deps(i, a, b)
#define DEP(i, n) dep(i, n)

#define pf push_front
#define pb push_back
#define mp make_pair
#define fst first
#define FST fst
#define sec second
#define SEC sec
#define sz(obj) ((int)(obj).size())
#define all(v) (v).begin(), (v).end()
#define ALL(v) all(v)

const int INF = 999999999;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T>
void print(std::vector<T>& v, char c = ' ') {
  REP(i, v.size()) {
    if (i != 0) std::cout << c;
    std::cout << v[i];
  }
  std::cout << endl;
}
template <typename T> void print(T x) { std::cout << x << '\n'; }

signed main()
{
  vi v(4);

  rep(i, 4) v[i] = in();
  sort(all(v));
  print(v[1] + v[2] + v[3] + max(in(), in()));

  return (0);
}


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

int main(){
  int W[10],K[10];
  for(int i=0;i<10;i++) cin>>W[i];
  for(int i=0;i<10;i++) cin>>K[i];
  sort(W,W+10);
  sort(K,K+10);

  int max1,max2;
  max1=max2=0;

  max1=W[9]+W[8]+W[7];
  max2=K[9]+K[8]+K[7];

  cout<<max1<<" "<<max2<<endl;
  return 0;
}

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

int solve()
{
	int a,b;
	cin>>a>>b;
	return a*b;
}

int main()
{
	int t;
	t=1;
	while(t--)
	{
		cout<<solve()<<'\n';
	}
}


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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for(int i=0;i<n;i++){
  	cin >> a[i];
  }
  int len=0;
  sort(a,a+n);
  for(int i=n-1;i>=n-k;i--){
  	len+=a[i];
  }
  cout << len << endl;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

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

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

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

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

struct UnionFind {
  vector<int> parent;
  vector<int> num_node;

  UnionFind(int N) : parent(N), num_node(N, 1) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }

  // 根を求める
  int root(int x) {
    if (parent[x] == x) {
      return x;
    }

    // 経路圧縮
    parent[x] = root(parent[x]);
    return parent[x];
  }

  // 同一判定
  bool is_same(int x, int y) {
    return this->root(x) == this->root(y);
  }

  // 併合
  void unite(int x, int y) {
    x = this->root(x);
    y = this->root(y);

    // すでに一緒ならなにもしない
    if (x == y) {
      return;
    }
    parent[x] = y;
    num_node[y] += num_node[x];
  }

  // 同一グループのノード数
  int size(int x) {
    return num_node[root(x)];
  }
};

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

  ll h, w;
  cin >> h >> w;
  vs s(h);
  rep(i, h) {
    cin >> s[i];
  }

  ll n = h * w;
  UnionFind uf(n);
  vector<vb> visited(h, vb(w));

  rep(i, n) {
    ll y = i / w;
    ll x = i % w;

    if (visited[y][x]) {
      continue;
    }

    queue<P> q;
    q.push(make_pair(y, x));

    while (!q.empty()) {
      auto it = q.front();
      q.pop();

      ll v = it.first * w + it.second;
      uf.unite(i, v);

      ll y = it.first;
      ll x = it.second;

      if (y > 0) {
        if (!visited[y - 1][x] && s[y - 1][x] != s[y][x]) {
          q.push(make_pair(y - 1, x));
          visited[y - 1][x] = true;
        }
      }
      if (y < h - 1) {
        if (!visited[y + 1][x] && s[y + 1][x] != s[y][x]) {
          q.push(make_pair(y + 1, x));
          visited[y + 1][x] = true;
        }
      }

      if (x > 0) {
        if (!visited[y][x - 1] && s[y][x - 1] != s[y][x]) {
          q.push(make_pair(y, x - 1));
          visited[y][x - 1] = true;
        }
      }
      if (x < w - 1) {
        if (!visited[y][x + 1] && s[y][x + 1] != s[y][x]) {
          q.push(make_pair(y, x + 1));
          visited[y][x + 1] = true;
        }
      }
    }
  }

  vector<P> vp(n);

  rep(i, n) {
    ll y = i / w;
    ll x = i % w;
    if (s[y][x] == '.')
      vp[uf.root(i)].first++;
    else
      vp[uf.root(i)].second++;
  }

  ll ans = 0;
  rep(i, n) {
    ans += vp[i].first * vp[i].second;
  }

  cout << ans << endl;

  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
	int A, B;
	string S;
	int check = 1;
	cin >> A >> B >> S;
	for (int i = 0; i < A; i++) {
		if (S[i] < 48) check = 0;
	}
	if (S[A] != '-') check = 0;
	for (int i = A + 1; i < B + A + 1; i++) {
		if (S[i] < 48) check = 0;
	}
	if (check == 1) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}

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

int main() {
  int a,b,k;cin>>a>>b>>k;
  rep(i,k){
    if(i%2==0){
      if(a%2==1){
        a--;
      }
      a/=2;
      b+=a;
    }
    else {
      if(b%2==1){
        b--;
      }
      b/=2;
      a+=b;
    }
  }
  cout<<a<<" "<<b<<endl;
}


#if 0
2019.12.30

朴素 dp 设 f[i][p][q] 表示前 i 次操作后当前棋子为 (p, q) 的最小代价。
事实上由于第 i 次操作后一定有一个棋子是 x[i] ，因此可以优化一维：
f[i][j] 表示前 i 次操作后当前棋子为 (j, x[i]) 的最小代价。

转移有两种：
1) f[i - 1][j] + |x[i - 1] - x[i]| -> f[i][j]
2) min(f[i - 1][k] + |x[i] - k|) -> f[i][x[i - 1]]
用线段树维护 f[~][j] ，第一种转移就是全局加，第二种是单点取 min 。
第二种转移可以把左边的绝对值拆开：
f[i - 1][k] - k + x[i] (k < x[i])
f[i - 1][k] (k = x[i])
f[i - 1][k] + k - x[i] (k > x[i])
线段树分别维护 f[~][j] - j, f[~][j], f[~][j] + j 即可。
#endif
#include <cstdio>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
	inline operator ll () { ll x; return scanf("%lld", &x), x; }
	template<class T> inline void operator () (T &x) { x = *this; }
	template<class T, class ...A> inline void operator () (T &x, A &...a)
	{ x = *this; this -> operator ()(a...); }
} read;

const int maxn = 200005;
ll f[maxn << 2], fa[maxn << 2], fb[maxn << 2], tag;

void modify(int now, int L, int R, int p, ll x) {
	f[now] = std::min(f[now], x);
	fa[now] = std::min(fa[now], x + p);
	fb[now] = std::min(fb[now], x - p);
	if(L == R) return;
	int M = (L + R) >> 1;
	if(p <= M) modify(now << 1, L, M, p, x);
	else modify(now << 1 | 1, M + 1, R, p, x);
}

void build(int now, int L, int R) {
	f[now] = fa[now] = fb[now] = 1000000000000000000;
	if(L == R) return;
	int M = (L + R) >> 1;
	build(now << 1, L, M);
	build(now << 1 | 1, M + 1, R);
}

ll query(int now, int L, int R, int p) {
	if(R < p) return p + fb[now];
	if(L > p) return fa[now] - p;
	if(L == R) return f[now];
	int M = (L + R) >> 1;
	return std::min(query(now << 1, L, M, p), query(now << 1 | 1, M + 1, R, p));
}

int main() {
	int n = read, q = read, a = read, b = read;

	build(1, 1, n);
	modify(1, 1, n, a, 0);
	/* modify(1, 1, n, b, 0); */

	int last = b;
	while(q --) {
		int x = read;
		ll qu = query(1, 1, n, x) + tag;
		/* debug("qu = %lld\n", qu); */
		tag += std::abs(x - last);
		modify(1, 1, n, last, qu - tag);
		last = x;
		/* debug("tag=%lld\n", tag); */
	}

	printf("%lld\n", f[1] + tag);
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  printf("%3f",n*2*M_PI);
}


#include <bits/stdc++.h>
#define DAU  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define PLEC return 0;
using namespace std;
string s;
int res;
int main() {
    DAU
    cin >> s;
    s = '0' + s;
    for (int i = (int)s.size() - 1; i > 0; --i) {
        if (s[i] <= '4' || (s[i] == '5' && s[i-1] < '5'))
            res += s[i] - '0';
        else {
            res += '9' + 1 - s[i];
            ++s[i-1];
        }
    }
    res += s[0] - '0';
    cout << res;
    PLEC
}

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

int main()
{
    string s;
    cin >> s;
    s.at(3) = '8';

    cout << s << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <random>
//#include "toollib.h"
//#include "puzzle.h"
#define INT_MAX 2147483647
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#pragma warning (disable:4018)
#pragma warning (disable:4244)
#pragma warning (disable:4996)

using namespace std;
typedef long long int lint;

//***** Main Program *****

int main() {
	int n, q;
	cin >> n;
	vector<int> S(n);
	Loop(i, n) cin >> S[i];
	cin >> q;
	vector<int> T(q);
	Loop(i, q) cin >> T[i];
	int ret = 0;
	Loop(i, q) {
		Loop(j, n) {
			if (S[j] == T[i]) {
				ret++;
				break;
			}
		}
	}
	cout << ret << endl;
	return 0;
}

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

using namespace std;

void init(vector<int>& v){
  int n = 0;
  int input;
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> input;
    v.push_back(input);
  }
  sort(v.begin(), v.end());
  v.erase( unique(v.begin(), v.end()), v.end() );
}

int linearsearch(int key, vector<int> t, int count){
  int i = 0;
  t.push_back(key);
  while(t[i]!=key){
    i++;
  }
  if(t.size()-1!=i) return count+1;
  else return count;
}

int main(int argc, char* argv[]){
  vector<int> a, b;
  int count = 0;
  init(a);
  init(b);
  for (auto i : a) count = linearsearch(i, b, count);
  cout << count << endl;
}


#include <stdio.h>

int main(void){
  int i;
  scanf("%d", &i);
  i = i*i*i;
  printf("%d\n", i);
}

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
    int n = 0, n2 = 0;
    cin >> n;
    //n = atoi(argv[1]);
    if(n < 1 || n > 100) {
        return 1;
    }
    n2 = pow(n, 3.0);
    cout << n2 << endl;
}


///_________________________________ B I S M I L L A H I R  R A H M A N I R  R A H I M __________________________________________________

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

typedef bool                   boo;
typedef int                    li;
typedef long                   il;
typedef unsigned long          ul;
typedef long long int          ll;
typedef unsigned long long     ull;
typedef double                 dd;
typedef string                 str;

#define vli                    vector

#define vll                    vector<ll>
#define sli                    set

#define sll                    set<ll>
#define pli                    pair

#define pll                    pair<ll, ll>
#define vpi                    vector<pair
>
#define vpl                    vector<pair<ll, ll>>
#define mpl                    map<ll, ll>

#define test                   ll t; cin>>t; while(t--)
#define input(a,n)             for(int i=0;i<n;i++) cin>>a[i];
#define lp(a, i, b)            for(ll i=a; i=b; i--)
#define sz(x)                  x.size()
#define len(z)                 z.begin(), z.end()
#define ci(x)                  cin>>x;
#define co(x)                  cout<<x nl
#define fix(x)                 fixed<<setprecision(x)
#define mem(z, l)              memset(z, l, sizeof(z))
#define MP                     make_pair
#define pb                     push_back
#define F                      first
#define S                      second
#define nl                     <<endl
#define nll                    cout<<endl
#define sp                     <<" "<<
#define sl                     <<" "
#define cy                     cout<<"YES" nl
#define cn                     cout<<"NO" nl
#define rn                     return
#define good_bye               return 0
#define gcd(a,b)               __gcd(a,b)
#define lcm(a,b)               (a*(b/gcd(a,b)))
#define faster                 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define input_txt()            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

const double eps =             1e-9;
const int inf =                2000000000;
const ll infLL =               9000000000000000000;
const ll MOD =                 1e+7;
const double PI =              3.141592653589793238462643383279;

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

//ll Pow(ll P, ll Q)           { ll R=1; for(ll I=1; I<=Q; I++) R*=P; rn R; }
//ll fact(ll N)                { ll M=1; for(ll I=1; I<=N; I++) M*=I; return M; }
//void dfs(ll K)               { vis[K]=true; for(auto J: v[K]) { if(!vis[J]) dfs(J); } }
//void bfs(ll s)               { queue<ll>q; q.push(s); while(!q.empty()) { ll u=q.front(); q.pop(); for(auto i : v[u]) if(lev[i]==-1) { lev[i]=lev[u]+1; q.push(i); } } }
//void sieve(ll MX)            { S.pb(2); for(ll I=3; i<=MX; i+=2) { if(!vis[I]) { S.pb(I); for(ll J=I*I; j<=MX; J+=(2*I)) vis[J]=true; } } }
//bool isPrime(ll n)           { if(n%2==0) rn false; for(ll i=3; i*i<=n; i+=2) { if(n%i==0) rn false; } rn true; }

///_____________________________________________ L E T ' S  B E G I N __________________________________________________________________________

const int mX = 1e6+123;
ll n, l, i=0;
str s;

void case_i()
{
    cin>>n>>s; l=sz(s);
    while(i<min(l, n)) { cout<<s[i]; i++; }
    if(l>n) cout<<"...";
    nll;
}

int main()
{
    //  input_txt();
    faster;  //                                                                                test
    case_i();
    good_bye;
}

//                                                                                author : 5 A 1 F U D D 1 N  5 A 8 8 1 R

///|||||||||||||||||||||||||||||||||||||||||||||||||||||| E N D ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

#pragma region Macros
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;
#pragma endregion

void solve() {
    int a, b;
    cin >> a >> b;
    vector<string> res(100);
    for (int i=0; i<100; i++) {
        for (int j=0; j<100; j++) {
            if (i < 50) res.at(i).push_back('.');
            else res.at(i).push_back('#');
        }
    }
    int x = 0, y = 0;
    for (int i=0; i<b-1; i++) {
        res.at(x).at(y) = '#';
        y += 2;
        if (y >= 100) {
            x += 2;
            y = 0;
        }
    }
    x = 51, y = 0;
    for (int i=0; i<a-1; i++) {
        res.at(x).at(y) = '.';
        y += 2;
        if (y >= 100) {
            x += 2;
            y = 0;
        }
    }
    cout << 100 << ' ' << 100 << '\n';
    for (string s : res) cout << s << '\n';
}

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

    solve();

    return 0;
}


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

int main() {
    long l, r;
    cin>>l >>r;
    long ans=2019;
    for(long i=l+1;i<=r;i++){
        for(long k=l;k<i;k++){
            long a=(i*k)%2019;
            ans=min(ans,a);
            if(ans==0){
                cout<<ans<<endl;
                return 0;
            }
        }
    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MOD = 1e9 + 7;
const int MAX = 510000;

// https://drken1215.hatenablog.com/entry/2018/06/08/210000
ll fac[MAX], finv[MAX], inv[MAX];
void combinationInit() {
  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 combination(ll n, ll k) {
  if (n < k) {
    return 0;
  } else if (n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

vector<pair<ll, ll>> primeFactorize(ll n) {
  vector<pair<ll, ll>> r;
  for (int i = 2; i * i <= n; i++) {
    if (n % i != 0) {
      continue;
    }
    int ex = 0;
    while (n % i == 0) {
      ex++;
      n /= i;
    }
    r.push_back({i, ex});
  }
  if (n > 1) {
    r.push_back({n, 1});
  }
  return r;
}

int main() {
  ll n, m;
  cin >> n >> m;
  auto res = primeFactorize(m);
  ll v = 1;
  combinationInit();
  for (ll i = 0; i < res.size(); i++) {
    v *= combination(res[i].second + n - 1, n - 1);
    v %= MOD;
  }

  cout << v << endl;
}


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

int main()
{
    int N; cin >> N;
    vector<ll> A(N); rep (i, N) cin >> A[i];
    vector<ll> B(N); rep (i, N) cin >> B[i];
    vector<ll> C(N); rep (i, N) cin >> C[i];

    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());

    vector<ll> sums(N+1);
    sums[0] = 0;
    rep (i, N)
    {
        ll b = B[i];
        auto c = upper_bound(C.begin(), C.end(), b);
        sums[i+1] = sums[i] + distance(c, C.end());
    }

    ll cnt = 0;
    rep (i, N)
    {
        ll a = A[i];
        auto b = upper_bound(B.begin(), B.end(), a);
        ll index = distance(B.begin(), b);
        cnt += sums[N] - sums[index];
    }

    cout << cnt << endl;

    return 0;
}


#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#define rep(i,n) for(auto i=0;i<(n);i++)
#define all(vs) (vs).begin(),(vs).end()
#define roundup(A,B) (A)/(B)+((A)%(B)==0?0:1)
#define read(type,name) type name;cin>>name
using namespace std;

/*
  予想と答えをすべて比較するのが最速。O(n)
*/
void A() {
	string s, t;
	cin >> s >> t;
	int answer = 0;
	rep(i, s.length()) {
		if (s[i] == t[i])answer++;
	}
	cout << answer << endl;
}

/*
  タップを使うごとにA-1ずつ増える。Bと1の距離をA-1で割った（切り上げ）のが答え。
  A,Bの値によらないのでO(1)の解法である。
*/
void B() {
	int A, B; cin >> A >> B;
	cout << roundup(B-1, A-1) << endl;
}

/*
  左から走査することで最大長を求めることができる。最大長を0で初期化し、
  左端からスタートする。右に進める限り現在の長さをincrementし、進めなくなったところで最大長を更新する。
  そして止まったところからリセットして再開する。これを繰り返すことで最大長を求めることができる。
  計算量はO(n)、空間量はO(1)となる。
*/
void C() {
	int n; cin >> n;
	int length = 0, max_length = 0;
	int prev = 0, current;
	rep(i, n) {
		cin >> current;
		if (prev >= current) {
			length++;
		} else {
			max_length = max(max_length, length);
			length = 0;
		}
		prev = current;
	}
	max_length = max(max_length, length);
	cout << max_length << endl;
}

/*
　剰余を最大にするのは、MをM+1で割った場合であるから、
 iに対して1つずれた順列を作れば最適である。配列の最後Nについては、
 N+1を用意することはできないので、1で剰余を取る（必ず10になる）。
 このやり方で和を求めた場合は、1からN-1までの和に等しいので(N-1)(N)/2となる。
 　　->これが最大っていうのはどうわかるの?
*/
void D() {
	read(int, n);
	cout << n * (n - 1) / 2 << endl;
}

/*
　総当たりで調べてO(N^2)を試してみよう
*/
struct Player {
	queue<int> versus{};
	bool available{ true };
	bool matchable(int i) {
		return available && versus.front() == i;
	}
};
void E() {
	read(int, n);
	auto ps = vector<Player>(n);
	rep(i, n) {
		rep(j, n - 1) {
			read(int, p);
			ps[i].versus.push(p - 1);
		}
	}
	int days = 0;
	int remains = n;
	while (true) {
		bool matched = false;
		rep(i, ps.size()) {
			if (!ps[i].available||ps[i].versus.empty())continue;
			auto v = ps[i].versus.front();
			if (ps[v].matchable(i)) {
				matched = true;
				ps[i].versus.pop(); ps[i].available = false; if (ps[i].versus.empty()) remains--;
				ps[v].versus.pop(); ps[v].available = false; if (ps[v].versus.empty()) remains--;
			}
		}
		for (auto& p : ps)p.available = !p.versus.empty();
		days++;
		if (remains == 0) {
			cout << days << endl;
			return;
		}
		if (!matched) {
			cout << -1 << endl;
			return;
		}
	}
}

int main() {
	E();
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define MOD (long long int)(998244353)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

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

ll mpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
  else return mpow(a,b-1) * a % MOD;
}
ll lpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;}
  else return lpow(a,b-1) * a;
}
ll gcd(ll a, ll b){
  if(b==0) return a;
  else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
  if(kaijo_memo.size() > n) return kaijo_memo[n];
  if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
  while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n){
  if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n];
  if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1);
  while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r){
  if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く？不安.
  if(n < r || r < 0) return 0;
  ll ret = 1;
  ret *= kaijo(n); ret %= MOD;
  ret *= gyaku_kaijo(r); ret %= MOD;
  ret *= gyaku_kaijo(n-r); ret %= MOD;
  return ret;
}

vector<ll> G[(int)1e5+2];
bool checked[(int)1e5+2];
int color[(int)1e5+2];

bool saiki(int now, int col){
  if(checked[now]){
    return color[now] == col;
  }
  checked[now] = true;
  color[now] = col;
  rep(j, G[now].size()){
    int next = G[now][j];
    if(!saiki(next, (col+1) & 1)){
      return false;
    }
  }

  return true;
}

int main(void){
  fast_io
  cout<<fixed<<setprecision(15);

  ll n,m;
  cin>>n>>m;
  rep(i,m){
    ll a,b;cin>>a>>b;a--;b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  if(!saiki(0, 0)){
    cout<< n * (n-1) / 2 - m << endl;
    return 0;
  }

  ll C0 = 0, C1 = 0;
  rep(i,n){
    if(color[i] == 0){
      C0++;
    }else{
      C1++;
    }
  }
  cout<< C0 * C1 - m <<endl;

  return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main(){
  int n, m, x, y;
  cin >> n >> m >> x >> y;
  int mx = -1000, mn = 1000;
  rep(i, n){
    int in;
    cin >> in;
    mx = max(mx, in);
  }
  rep(i, m){
    int in;
    cin >> in;
    mn = min(mn, in);
  }
  bool f = false;
  if(mx >= mn) f = true;
  if(mn <= x || y < mn) f = true;
  if(f == false) cout << "No ";
  cout << "War" << endl;

  return 0;
}

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 200010
inline ll read()
{
	ll x=0; char c=getchar(),f=1;
	for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
	for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
	return x*f;
}
inline void write(ll x)
{
	static char buf[20];
	int len=0;
	if(x<0)putchar('-'),x=-x;
	for(;x;x/=10)buf[len++]=x%10+'0';
	if(!len)putchar('0');
	else while(len)putchar(buf[--len]);
}
inline void writesp(ll x){write(x); putchar(' ');}
inline void writeln(ll x){write(x); putchar('\n');}
int a[maxn];
int n,m;
int main()
{
	n=read(); m=read();
	if(m==1){
		writeln(n);
		if(n==1){
			writeln(1);
			writeln(n);
		}
		else{
			writeln(2);
			writesp(n-1); writeln(1);
		}
		return 0;
	}
	int cnt=0;
	for(int i=1;i<=m;i++){
		a[i]=read();
		cnt+=(a[i]&1);
	}
	if(cnt>2){
		puts("Impossible"); return 0;
	}
	int flag=0;
	for(int i=1;i<=m;i++)
		if(a[i]&1){
			if(!flag)std::swap(a[1],a[i]),flag=1;
			else std::swap(a[m],a[i]);
		}
	for(int i=1;i<=m;i++)
		writesp(a[i]);
	putchar('\n');
	if(a[1]==1){
		writeln(m-1);
		for(int i=2;i<m;i++)
			writesp(a[i]);
		writeln(a[m]+1);
	}
	else{
		writeln(m);
		writesp(a[1]-1);
		for(int i=2;i<m;i++)
			writesp(a[i]);
		writeln(a[m]+1);
	}
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int N;
	cin >> N;
	long long ans = 0, before;
	cin >> before;
	bool HasNoAns = (before > 0);
	for (int i = 1; i < N; i++) {
		int now;
		cin >> now;
		if (now == 0) {
			before = now;
			continue;
		}
		if (now == before + 1) ans++;
		else {
			if (now <= before) ans += now;
			else HasNoAns = true;
		}
		before = now;
	}
	cout << (HasNoAns ? -1 : ans) << endl;
}

#include <iostream>
using namespace std;

int main(void) {
    long a, n, min, max;
    long sum = 0;

    cin >> n;

    cin >> a;
    max = a;
    min = a;
    sum += a;
    for(int i = 1; i < n; ++i) {
        cin >> a;
        if(a < min) {
            min = a;
        }
        if(a > max) {
            max = a;
        }
        sum += a;
    }

    cout << min << ' ' << max << ' ' <<  sum << endl;

    return 0;
}


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

void solve()
{

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);
    solve();
    ll n,x,t;
    cin>>n>>x>>t;
    if(n%x)
    {
        cout<<(n/x + 1)*t<<'\n';
    }
    else
    {
        cout<<(n/x)*t<<'\n';
    }
}

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <vector>
#include <cctype>
#include <cstdio>
#include <iomanip>

using namespace std;

string in, t;
double a, b, c, d;

int main(){
    cin >> a >> c >> b >> d;
    cout << fixed << setprecision(5) << sqrt(fabs(a - b)*fabs(a - b)*1.0 + fabs(c - d)*fabs(c - d)*1.0) << endl;
}