#include <algorithm>
#include <cstdio>
const int N=1000005;
int f[N];
char a[N],b[N];
bool check(int n){
	for (int i=1;i<=n;i++)
		if (a[i]!=b[i]) return 0;
	return 1;
}
int main(){
	int n;scanf("%d\n%s%s",&n,a+1,b+1);
	if (check(n)){
		printf("0\n");
		return 0;
	}
	int ans=1;
	for (int i=n,j=n,x=0;i;i--)
	if (b[i]!=b[i-1]){
		for (j=std::min(j,i);j && a[j]!=b[i];j--) f[j+x]=-x;
		if (!j){
			printf("-1\n");
			return 0;
		}
		x++;
		if (j--<i) ans=std::max(ans,f[i-1+x]+x+1);
	}
	printf("%d\n",ans);
}

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int mod=998244353;
const int maxn=100010;
int k,n,N,tmp,ans,jc[maxn],iv[maxn],ivjc[maxn],cnt;
int C(int x,int y) {
	if (x<y||y<0) return 0;
	return (ll)jc[x]*ivjc[x-y]%mod*ivjc[y]%mod;
}
void update(int &x,int y) {
	x+=y; if (x>=mod) x-=mod;
}
int main() {

	read(k); read(n);
	jc[0]=iv[0]=ivjc[0]=jc[1]=iv[1]=ivjc[1]=1;
	for (int i=2;i<=10000;i++) {
		jc[i]=(ll)jc[i-1]*i%mod;
		iv[i]=(ll)(mod-mod/i)*iv[mod%i]%mod;
		ivjc[i]=(ll)ivjc[i-1]*iv[i]%mod;
	}
	for (int m=2;m<=2*k;m++) {
		cnt=0;
		for (int i=1;i<=k;i++)
			if (i<=m-i&&m-i<=k) cnt++;
		ans=0;
		for (int i=0;i<=cnt;i++) {
			N=n-i*2;
			if (N<0) break;
			tmp=(ll)C(N+k-1,k-1)*C(cnt,i)%mod;
			if (i&1^1) update(ans,tmp);
			else update(ans,mod-tmp);
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/


#include<bits/stdc++.h>
#define rep(i,a) for(int (i)=0; (i)<(a); (i)++)
#define rrep(i,a) for(int (i)=a-1; (i)>=(0); (i)--)
#define rep2(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;

using namespace std;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n,u,v; cin >> n >> u >> v; u--; v--;
  vector<vector<ll>> G(n);
  rep(i,n-1){
    ll a,b; cin >> a >> b;
    a--; b--;
    G[a].push_back(b); G[b].push_back(a);
  }
  queue<ll> q;
  q.push(u);
  vector<ll> d_u(n, 100000000);
  d_u[u] = 0;
  while(!q.empty()){
    ll now = q.front(); q.pop();
    for(ll next:G[now]){
      if(d_u[next] !=  100000000) continue;
      d_u[next] = d_u[now] + 1;
      q.push(next);
    }
  }
  q.push(v);
  vector<ll> d_v(n, 100000000);
  d_v[v] = 0;;
  while(!q.empty()){
    ll now = q.front(); q.pop();
    for(ll next:G[now]){
      if(d_v[next] !=  100000000) continue;
      d_v[next] = d_v[now] + 1;
      q.push(next);
    }
  }
  ll ans = 0;
  rep(i,n){
    if(d_u[i] < d_v[i]) ans = max(ans,d_v[i]-1);
  }
  cout << ans;

  return 0;
}

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

#define int long long

class SCC {
    private:
        int V;
        int E;

        vector<int> G[100001];
        vector<int> rG[100001];
        vector<int> vs;
        bool used[100001];
        int cmp[100001];

        auto dfs(int v) -> void {
            used[v] = true;
            for (int i = 0; i < G[v].size(); ++i)
                if (!used[G[v][i]])
                    dfs(G[v][i]);

            vs.push_back(v);
        }

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

    public:
        SCC(int V, int E)
            : V(V), E(E) { }

        auto scc() -> int {
            fill(used, used + V, false);
            vs.clear();
            for (int v = 0; v < V; v++) {
                if (!used[v]) dfs(v);
            }
            fill(used, used + V, false);
            int k = 0;
            for (int i = vs.size() - 1; i >= 0; i--)
                if (!used[vs[i]])
                    rdfs(vs[i], k++);

            return k;
        }

        auto add_edge(int s, int t) {
            G[s].push_back(t);
            rG[t].push_back(s);
        }

        auto get_cmp(int a) -> int {
            return cmp[a];
        }

        auto is_union_cmp(int a, int b) -> bool {
            return cmp[a] == cmp[b];
        }
};

auto main() -> signed {
    int a, b;
    int V, E;
    cin >> V >> E;
    SCC scc(V, E);
    for (int i = 0; i < E; i++) {
        cin >> a >> b;
        scc.add_edge(a, b);
    }
    int k = scc.scc();

    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        cin >> a >> b;
        cout << scc.is_union_cmp(a, b) << endl;
    }
    return 0;
}


#include<stdio.h>
int main (){
	int A;
	int B;
	char str1[100];
	int hasil;

	scanf("%d %d", &A,&B);
	char str[(A+B)+1];
	scanf("%s", &str);

	for(int i=0;i<A+B+1;i++){
	if(i!=A){
		if(str[i]>='0'&&str[i]<='9'){
			hasil++;
		}
	}else if(str[i]=='-'){
			hasil++;
	}
	}

		if(hasil==A+B+1){
			printf("Yes");
		}else{
			printf("No");
		}
	return 0;
}

#include<bits/stdc++.h>
#define int long long
#define all(a) a.begin(),a.end()
#define sz(a) a.size()
#define re return
#define MP make_pair
#define pb push_back
#define pii pair<int,int>
#define se second
#define fi first
using namespace std;
int a,b;
string s;
signed main(){
	ios_base::sync_with_stdio(0);
	cin>>a>>b>>s;
	if(s[a]!='-'){
		cout<<"No";
		re 0;
	}
	s.erase(s.begin()+a);
	for(int i=0;i<a+b;i++){
		if(!isdigit(s[i])){
			cout<<"No";
			re 0;
		}
	}
	cout<<"Yes";
	re 0;
}


#include <iostream>
#include<string>
#include<cmath>
#include<ciso646>
#include<cstring>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<utility>
#include<map>
#include<math.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
using ll = long long;
ll Max = 10 + 1e12;
int N = 5 + 1e5;

int main()
{
	ll n;
	string rr = "";
	cin >> n;
	while (n>0) {
		int xx = n % 26;
		n /= 26;
		if (xx == 0) {
			rr += "z";
			n--;
		}
		else {
			rr += (char)('a' + (xx - 1));
		}
	}
	reverse(rr.begin(), rr.end());
	cout << rr;
}

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
  int e;
  while ((cin >> e) && e != 0) {
    int m = e;
    for (int z=0; z<=100; z++) {
      int zzz = z*z*z;
      if (zzz > e) break;
      for (int y=0; y<=100000; y++) {
        int yy = y*y;
        if (yy+zzz > e) break;
        int x = e - (yy+zzz);
        m = min(m, x+y+z);
      }
    }
    cout << m << endl;
  }
  return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<memory>
#include<cmath>
#include<algorithm>
#include<vector>
double min_double(double a,double  b){
    if(a<b) return a;
    else return b;
}
void swap(int *x,int *y){
    int tmp;
    tmp=*x;
    *x=*y;
    *y=tmp;
}
int gcd(int a,int b){
    if(a<b) swap(&a,&b);
    if(b<1) return -1;

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

int main(){
    int L,R;
    std::cin>>L>>R;
    if(R - L + 1 >= 2019){
        std::cout<<0<<std::endl;
        return 0;
    }

    int num = R - L + 1;
    int min=2019;
    for(long i=L;i<=R;i++){
        for(long j=i+1;j<=R;j++){
            int cal=(i * j)%2019;
            if(cal < min) min = cal;
        }
    }

    std::cout<<min<<std::endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define inf             0x3f3f3f3f
#define INF             2e18
#define eps             1e-9

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)
#define fRead           freopen("input.txt","r",stdin)
#define fWrite          freopen("output.txt","w",stdout)

#define LL              long long
#define ull            unsigned long long
#define PI              acos(-1.0)
#define pb              push_back
#define mk              make_pair
#define pii             pair<int,int>
#define pLL             pair<LL,LL>
#define ff              first
#define ss              second
#define all(a)          a.begin(),a.end()
#define SQR(a)          ((a)*(a))
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(min(a,b),min(c,d))
#define max4(a,b,c,d)   max(max(a,b),max(c,d))
#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
#define vi              vector <int>
#define vL              vector <LL>
#define LB(a,x)         (lower_bound(all(a),x)-a.begin()) //  first element in the range [first,last) which does not compare less than val.
#define UB(a,x)         (upper_bound(all(a),x)-a.begin()) //  first element in the range [first,last) which compares greater than val.
#define prec(n)         fixed << setprecision(n)

#define MEM(a,x)        memset(a,x,sizeof(a))
#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())

#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define IT(it,x)	    for(it=x.begin();it!=x.end();it++)
int main()
{
   LL l,r;
   cin>>l>>r;
   LL mn=INF;
   for(LL i=l;i<=min(l+2019,r);i++)
   {
       for(LL j=i+1;j<=min(l+2019,r);j++)
       {
           LL mod=((i%2019)*(j%2019))%2019;
           mn=min(mod,mn);
       }
   }
   cout<<mn;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;
using P = pair<int, int>;

int main() {
    int n, m;
    cin >> n >> m;
    vector<tuple<int, int, int>> t(m);
    vector<P> v;
    for (int i = 0; i < m; ++i) {
        int p, q, c;
        scanf("%d %d %d", &p, &q, &c);
        t[i] = make_tuple(p-1, q-1, c);
        v.emplace_back(p-1, c);
        v.emplace_back(q-1, c);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    auto s = v.size();
    vector<vector<P>> G(s+n);
    auto getidx = [&](P a){ return lower_bound(v.begin(), v.end(), a)-v.begin();};
    for (auto &&i : t) {
        int p, q, c;
        tie(p, q, c) = i;
        auto x = getidx(P(p, c)), y = getidx(P(q, c));
        G[x].emplace_back(y, 0);
        G[y].emplace_back(x, 0);
        G[x].emplace_back(p+s, 1);
        G[p+s].emplace_back(x, 1);
        G[y].emplace_back(q+s, 1);
        G[q+s].emplace_back(y, 1);
    }
    vector<int> d(s+n, INF<int>);
    d[s] = 0;
    deque<P> Q;
    Q.emplace_front(0, s);
    while (!Q.empty()){
        int dist, u;
        tie(dist, u) = Q.front(); Q.pop_front();
        if(dist > d[u]) continue;
        for (auto &&e : G[u]) {
            int to, c;
            tie(to, c) = e;
            if(d[to] > d[u]+c){
                d[to] = d[u]+c;
                if(c == 0) Q.emplace_front(d[u], to);
                else Q.emplace_back(d[u]+1, to);
            }
        }
    }
    cout << (d[s+n-1] == INF<int> ? -1 : d[s+n-1]/2) << "\n";
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int N=1e5+2;
int n,inv[2],flip[2],pos[N],map[3][N];
struct B_I_T{
	int tr[N];
	inline void init(){
		memset(tr,0,sizeof(tr));
	}
	inline void add(int x,int v){
		for(;x<=n;x+=x&-x){
			tr[x]+=v;
		}
	}
	inline int ask(int x){
		int ans=0;
		for(;x;x-=x&-x){
			ans+=tr[x];
		}
		return ans;
	}
	inline void mod(const int x,const int y,const int v){
		if(x>y) return;
		add(x,v);
		add(y+1,-v);
	}
}ad;

inline bool conts(const int x,const int y,const int z){
	if(x<z) return y==x+1&&z==y+1;
	return x==y+1&&y==z+1;
}

inline char get_c(){
	static char buf[20000],*h,*t;
	if(h==t){
		t=(h=buf)+fread(buf,1,20000,stdin);
	}
	return h==t?EOF:*h++;
}

inline int nxi(){
	int x=0;
	char c;
	while((c=getchar())>'9'||c<'0');
	while(x=x*10+c-48,(c=getchar())>='0'&&c<='9');
	return x;
}

int main(){
#ifndef ONLINE_JUDGE
//	freopen("b.in","r",stdin);
#endif
	n=nxi();
	for(int i=0;i<3;++i){
		for(int j=1;j<=n;++j){
			map[i][j]=nxi();
		}
	}
	for(int i=1;i<=n;++i){
		int x=map[0][i],y=map[1][i],z=map[2][i],w=x>z?x:z;
		if(!conts(x,y,z)||w%3){
			puts("No");
			return 0;
		}
		if((i-w/3)&1){
			puts("No");
			return 0;
		}
		if(z!=w) inv[i&1]^=1;
		pos[w/3]=i;
	}
	for(int i=1;i<=n;i+=2){
		int p=pos[i],add=ad.ask(p);
		flip[1]=(flip[1]+(abs(p+add-i)>>1))&1;
		ad.mod(1,p,2);
	}
	ad.init();
	for(int i=2;i<=n;i+=2){
		int p=pos[i],add=ad.ask(p);
		flip[0]=(flip[0]+(abs(p+add-i)>>1))&1;
		ad.mod(1,p,2);
	}
	if((flip[0]-inv[1])&1||(flip[1]-inv[0])&1) puts("No");
	else puts("Yes");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll=long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD  1000000007
const ll INF=1e18;
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<endl;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b){ a = b; return 1; } return 0; }

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n) cin >> a[i];
  ll ans = 0;
  for (int bit = 0; bit < 60; bit++){
    ll one = 0;
    rep(i, n) if (a[i] & 1ll << bit) one++;
    ans = ll(ans%MOD + (1ll << bit) % MOD * one%MOD * (n - one)%MOD) % MOD;
  }
  cout << ans << endl;
  return 0;
}

//Δ_E
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 1<<20;
const int MO = 1e9+7;
int n,m,a[N],b[N],s[N];
vector<int> v[N];
int cal(int o,int f=0){
	int i,j,x,y;
	x=1,y=0;
	for(i=0;i<v[o].size();i=i+1){
		j=v[o][i];
		y=(y+x)%MO;
		if(i+1<v[o].size())
			x=(x+(LL)y*(s[v[o][i+1]]-s[j])%MO)%MO;
	}
	if(f)
		return x;
	return y;
}
int main()
{
	int i,x,o;
	scanf("%d",&n);
	for(i=1;i<=n;i=i+1){
		scanf("%d",a+i);
		m^=a[i];
		b[i]=b[i-1]^a[i];
		s[i]=s[i-1];
		if(!b[i])
			s[i]++;
	}
	for(i=1;i<n;i=i+1)
		v[b[i]].push_back(i);
	if(m){
		v[m].push_back(n);
		cout<<cal(m,1);
		return 0;
	}
	x=0;
	for(i=1;i<N;i=i+1)
		x=(x+cal(i))%MO;
	o=1;
	for(i=1;i<=s[n-1];i=i+1)
		o=o*2%MO;
	cout<<(x+o)%MO;
	return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;cin>>n;
    char c; scanf("%c",&c);
    rep(p,n){
        string s;
    getline(cin,s);
        rep(i,s.length()){
        int b=(int)s.find("Hoshino",i);
        if(b!=-1){s.replace(b,7,"Hoshina");i+=6;}
    }
        cout<<s<<endl;
    }
}

#include<iostream>
using namespace std;

int main(){
  int n;
  char c;
  int f;

  cin >> n;
  getchar();

  f = 0;
  while(n){
    c = getchar();

    if(f == 1){
      if(c == 'o')f++;
      else f = 0;
    }else if(f == 2){
      if(c == 's')f++;
      else f = 0;
    }else if(f == 3){
      if(c == 'h')f++;
      else f = 0;
    }else if(f == 4){
      if(c == 'i')f++;
      else f = 0;
    }else if(f == 5){
      if(c == 'n')f++;
      else f = 0;
    }else if(f == 6){
      if(c == 'o'){
	putchar('a');
	f++;
      }else f = 0;
    }

    if(c == 'H')f = 1;

    if(c == '\n')n--;

    if(f==7)f = 0;
    else putchar(c);
  }
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for(int i = 1, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
using namespace std;
#define repi(itr, ds) for(auto itr = ds.begin(); itr != ds.end(); itr++)

// vector
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for(T &x : vec) is >> x;
  return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// vector
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for(int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}\n   ";
  return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  repi(itr, map_var) {
    os << *itr;
    itr++;
    if(itr != map_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  repi(itr, set_var) {
    os << *itr;
    itr++;
    if(itr != set_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}

#define DUMPOUT cerr

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
  DUMPOUT << head;
  if(sizeof...(Tail) > 0) {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...)                                                             \
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                             \
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
          << "    ",                                                          \
      dump_func(__VA_ARGS__)
#else
#define DEB if(false)
#define dump(...)
#endif

typedef long long int ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

class UnionFind {
 public:
  vector<ll> par;
  vector<ll> siz;

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for(ll i = 0; i < sz_; ++i) par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for(ll i = 0; i < sz_; ++i) par[i] = i;
  }

  // Member Function
  // Find
  ll root(ll x) {
    while(par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }

  // Union(Unite, Merge)
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) {  // 連結判定
    return root(x) == root(y);
  }

  ll size(ll x) {  // 素集合のサイズ
    return siz[root(x)];
  }
};

int main() {
  int N, K, L;
  cin >> N >> K >> L;
  auto road = UnionFind(N);
  auto railway = UnionFind(N);
  REP(i, K) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    road.merge(p, q);
  }
  REP(i, L) {
    int r, s;
    cin >> r >> s;
    r--;
    s--;
    railway.merge(r, s);
  }
  auto roots = map<pair<int, int>, int>();
  REP(i, N) {
    pair<int, int> p = {road.root(i), railway.root(i)};
    roots[p]++;
  }
  REP(i, N) {
    pair<int, int> p = {road.root(i), railway.root(i)};
    int cnt = roots[p];
    cout << cnt;
    if(i != N - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

int n,n2=1,q,a[500010],lst[500010],dat[2000010],ans[500010];
vector <pii> v[500010];

void upd(int k,int val)
{
	dat[k]+=val;
	while(k>0)
	{
		k=(k-1)/2;
		dat[k]+=val;
	}
}

int query(int k,int lb,int ub,int tlb,int tub)
{
	if(ub<tlb||tub<lb) return 0;
	if(tlb<=lb&&ub<=tub) return dat[k];
	return query(k*2+1,lb,(lb+ub)/2,tlb,tub)+query(k*2+2,(lb+ub)/2+1,ub,tlb,tub);
}

int main()
{
	cin>>n>>q;
	rep(i,n) scanf("%d",&a[i]);
	int x,y;
	rep(i,q)
	{
		scanf("%d%d",&x,&y);
		x--;
		y--;
		v[y].pb(mpr(x,i));
	}
	rep(i,n+5) lst[i]=-1;
	while(n2<n) n2*=2;
	rep(i,n)
	{
		if(lst[a[i]]>-1) upd(lst[a[i]]+n2-1,-1);
		upd(i+n2-1,1);
		lst[a[i]]=i;
		rep(j,v[i].size()) ans[v[i][j].se]=query(0,0,n2-1,v[i][j].fi,i);
	}
	rep(i,q) printf("%d\n",ans[i]);
	return 0;
}


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

int main(){
	int vote[101];
	int N,Q,M;
	int day;
	int tmp;
	int ans;
	int i,j;
	while(1){
		tmp=0;
		ans=0;
		for(i = 0 ; i < 101 ; i++){
			vote[i]=0;
		}
		cin >> N >> Q;
		if(N == 0 && Q == 0){
			break;
		}

		tmp=Q-1;

		for(i = 0 ; i < N ; i++){
			cin >> M;
			for(j = 0 ; j < M ; j++){
				cin >> day;
				 vote[day]++;
			}
		}
		for(i = 0 ; i < 101 ; i++){
			if(vote[i] > tmp){
				ans = i;
				tmp = vote[i];
			}
		}
		cout << ans << endl;
	}
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef pair<int, int>P;
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
const ll mod = 1000000007;
const ll INF = mod * mod;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
ll qp(ll a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
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 dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };

template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template<typename T1, typename T2> pair<T1, T2> operator<(const pair<T1, T2> l, const pair<T1, T2> r) { return (l.first < r.first); }
template<typename T1, typename T2> pair<T1, T2> operator>(const pair<T1, T2> l, const pair<T1, T2> r) { return (l.first > r.first); }

void solve() {
    ll N; cin>>N;
    vec a(N),b(N),c(N);
	ll ans=0;
	for(int i=0;i<N;i++){
		cin>>a[i]>>b[i];
		c[i]=a[i]+b[i];
		ans-=b[i];
	}
	sort(all(c));
	for(int i=N-1;i>=0;i-=2)ans+=c[i];
	cout<<ans<<endl;

}

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

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


#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT_SIZE  1024

int
main(int argc, char* argv[])
{
    char input[MAX_INPUT_SIZE];

    int n = atoi(fgets(input, sizeof(input), stdin));
    int ret = n * n * n;

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

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

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

  int h, w, a, b;
  cin >> h >> w >> a >> b;

  bool refl = true;
  for (int i = 0; i < h; i++) {
    int p = 0, q = 1;
    if (!refl) swap(p, q);
    for (int j = 0; j < w; j++) {
      if (j < a) cout << p;
      else cout << q;
    }
    cout << endl;
    if (i == b-1) refl = !refl;
  }
}


//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")

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

using namespace __gnu_pbds;
using namespace std;

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

#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define bpp __builtin_popcountll
#define sqr(x) ((x) * (x))
#define al 0x3F3F3F3F
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define in insert
#define ppf pop_front
#define endl '\n'
//#define int long long

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

const int mod = (int)1e9 + 7;
const int N = (int)3e5 + 123;
const ll inf = (ll)1e18 + 1;

const double pi = acos (-1.0);
const double eps = 1e-7;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

int n;
ll a[N], res, sum;
bool used[N];

inline void boost () {
   ios_base :: sync_with_stdio (NULL);
   cin.tie (NULL), cout.tie (NULL);
}

inline void Solve () {
   cin >> n;
   for (int i = 1; i <= n; i ++) cin >> a[i], sum ^= a[i];
   for (int bit = 60; bit >= 0; bit --) {
      if (!(sum & (1ll << bit))) continue;
      for (int i = 1; i <= n; i ++)
         if (a[i] & (1ll << bit))
            a[i] -= (1ll << bit);
   }
   for (int bit = 60; bit >= 0; bit --) {
      int pos = -1;
      for (int i = 1; i <= n; i ++) {
         if (used[i]) continue;
         if (a[i] & (1ll << bit)) {
            pos = i;
         }
      }
      if (pos != -1) {
         used[pos] = 1;
         for (int i = 1; i <= n; i ++)
            if (i != pos && (a[i] & (1ll << bit)))
               a[i] ^= a[pos];
      }
   }
   for (int i = 1; i <= n; i ++) res ^= a[i];
   cout << res + (sum ^ res);
}

main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
   boost ();
   int tt = 1;
   //cin >> tt;
   while (tt --) {
      Solve ();
   }
   return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n; cin >> n;
	vector<long long> a(n), base;
	for(auto & e : a) cin >> e;
	long long all = 0;
	for(auto & e : a) all ^= e;
	for(auto & e : a) e &= ~all;
	for(auto e : a){
		for(auto b : base) e = min(e, e ^ b);
		if(e){
			base.push_back(e);
		}
	}
      sort(base.rbegin(), base.rend());
	long long up = 0;
	for(auto b : base) up = max(up, up ^ b);
	cout << all + 2 * up << endl;
	return 0;
}


#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

const long long INF2 = (((ll)1<<31)-1);

int main(int argc, char* argv[])
{
    int n,q;
    scanf("%d%d", &n, &q);

    vector<pair<int, pair<int, pair<int,int> > > > z(q);     // type, s, t, x;
    int i;
    int cnt=0;
    for(i=0; i<q; i++) {
        int type, s, t, x;
        scanf("%d", &type);
        if(type==0) {
            scanf("%d%d%d", &s, &t, &x);
            z[i]=make_pair(type, make_pair(s, make_pair(t,x)));
        }
        else {
            scanf("%d", &s);
            z[i]=make_pair(type, make_pair(s, make_pair(0,0)));
            cnt++;
        }
    }
    int ansnum=cnt;

    set<pair<int,int> > zz;    // val, ansid
    vector<int> ans(ansnum, INF2);
    cnt=ansnum-1;
    for(i=q-1; i>=0; i--) {
        if(z[i].first==0) {
            int s=z[i].second.first;
            int t=z[i].second.second.first;
            int x=z[i].second.second.second;
            auto it=zz.lower_bound(make_pair(s,-INF));
            auto itend=zz.lower_bound(make_pair(t+1,-INF));
            while(it!=itend) {
                ans[it->second]=x;
                auto itnext=it; itnext++;
                zz.erase(*it);
                it=itnext;
            }
        }
        else {
            zz.insert(make_pair(z[i].second.first, cnt));
            cnt--;
        }
    }
    for(i=0; i<ansnum; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
//using Graph = vector<vector<edge>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

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

#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query int qq;std::cin >> qq;rep(qqq,qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
//#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout <<"Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define ssum(a) accumulate(a,0ll)

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
//typedef long long T;
ll ceil(ll a,ll b){
    return ((a+b-1)/b);
}
const int INF = 1000000000;
const ll INF64 = 9223372036854775807ll;
const ll MOD = 1000000007ll;
const dou pi=3.141592653589793;
//mapは、if関数などで覗いただけで構成されるので注意しよう！
//std::vector<vector<int>> g;

//std::vector<std::set<int>> g;

std::vector<int> c;//トポロジカルソート
std::set<int> al;
void topological(int p,std::vector<std::vector<int>> &g,std::vector<int> &ans){
    ans.pb(p);
    al.erase(p);
    erep(i,g[p]){
         c[i]--;
         if(c[i]==0){
             topological(i,g,ans);
         }
    }
    //return;
}
std::vector<int> topo(std::vector<std::vector<int>> &g){
    std::vector<int> ans;
    rep(i,g.size())al.insert(i);
    c.resize(g.size());
    rep(i,g.size()){
        erep(j,g[i]){
            c[j]++;
        }
    }
    rep(i,g.size()){
        if(c[i]==0&&al.find(i)!=al.end()){
            topological(i,g,ans);
        }
    }
    return ans;
}

int main(){
    itn n,m;
    std::cin >> n>>m;
    std::vector<std::vector<int> >g(n) ;
    rep(i,n+m-1){
        itn a,b;
        std::cin >> a>>b;
        a--;b--;
        g[a].pb(b);
    }
    std::vector<int> dum=topo(g);
    std::vector<int> ans(n,-1);
    erep(i,dum){
        erep(j,g[i])ans[j]=i;
    }
    erep(i,ans){
        std::cout << i+1 << std::endl;
    }
}


#include <algorithm>
#include <cstdio>
const int N=2005;
int l[N][N],r[N][N];
bool a[N][N];
char s[N];
int main(){
	int h,w;
	scanf("%d%d\n",&h,&w);
	for (int i=0;i<h;i++){
		scanf("%s",s);
		for (int j=0;j<w;j++) a[i][j]=(s[j]=='#');
	}
	for (int i=h-1;i;i--){
		for (int j=0;j<w;j++) a[i][j]^=a[i-1][j];
		for (int j=w-1;j;j--) a[i][j]^=a[i][j-1];
		a[i][0]=a[i][w]=1;
		for (int j=0;j<w;j++)
			if (a[i][j]) l[i][j]=j;else l[i][j]=l[i][j-1];
		for (int j=w;j;j--)
			if (a[i][j]) r[i][j]=j;else r[i][j]=r[i][j+1];
	}
	int ans=std::max(h,w);
	for (int j=1;j<w;j++)
		for (int i=1,k=0,L=0,R=w;i<h;i++)
		if (a[i][j]) k=i,L=0,R=w;
		else{
			L=std::max(L,l[i][j]);
			R=std::min(R,r[i][j]);
			ans=std::max(ans,(R-L)*(i-k+1));
		}
	printf("%d\n",ans);
}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=2200;
int a[N][N],n,m,d[N][N],h[N][N],ans,stack[N][2],top,tt;
char ch;
int main(){
//    freopen("1.in","r",stdin);
    scanf("%d%d\n",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%c",&ch);
            if(ch=='#')a[i][j]=1;
            else a[i][j]=0;
        }scanf("\n");
    } ans=max(n,m);n=n-1;m=m-1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            d[i][j]=1-((a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1])%2);
        }
    }

    for(int i=1;i<=n;i++){
        for(int j=m;j>=1;j--){
            if(d[i][j]==1)h[i][j]=h[i][j+1]+1;
            else h[i][j]=0;
        }
    }
//    for(int i=1;i<=n;i++){
//    	for(int j=1;j<=m;j++)printf("%d ",d[i][j]);
//    	printf("\n");
//    }printf("\n\n");
//    for(int i=1;i<=n;i++){
//    	for(int j=1;j<=m;j++)printf("%d ",h[i][j]);
//    	printf("\n");
//    }
    for(int j=1;j<=m;j++){
        top=0;memset(stack,0,sizeof stack);
        for(int i=1;i<=n+1;i++){
            tt=1;
            while(top>0&&stack[top][0]>=h[i][j]){
                tt+=stack[top][1];
                ans=max(ans,(stack[top][0]+1)*(tt));
                --top;
            }
            ++top;
            stack[top][0]=h[i][j];
            stack[top][1]=tt;
        }
    }
    printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll,lvector,greater<ll>>
#define P pair<ll,ll>
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i,n) for(ll i=0; i<n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n,cnt=0;cin>>n;
  lvector ans;
  bool is_prime;
  for(ll i=2;i<=55555;i++) {
    if(i%5!=1) continue;
    is_prime=true;
    for(ll div=2;div*div<=i;++div)
      if(i%div==0) {is_prime=false;break;}
    if(is_prime) {ans.emplace_back(i);cnt++;}
    if(cnt==n) break;
  }
  rep(i,n-1) cout<<ans[i]<<' ';print(ans[n-1]);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
using vi = vector<int64_t>;
using vvi = vector<vi>;
using ll = long long ;
using P = pair<ll,ll>;
ll INF = 1000000007;
// ll INF = 9223372036854775807;
// ll INF = 998244353;

struct Sieve {
    ll n;
    vi f;
    vi primes;
    // constructor
    Sieve(ll n=1) : n(n), f(n+1,0) {
        f.at(0) = -1;
        f.at(1) = -1;
        for(ll i = 2; i <= n; ++i){
            if (f.at(i)!=0) continue;
            primes.push_back(i);
            f.at(i) = i;
            for (ll j = i*i; j <= n; j += i) {
                if(f.at(j)==0) f.at(j) = i;
            }
        }
    }
    // xが素数かを判定
    bool isPrime(ll x) {
        return f.at(x) == x;
    }
    // xを素因数分解する関数(分解後の値を昇順の配列にして返す)
    // (100) => ([2,1,5,5]);
    vi primeFactorizedList(ll x) {
        vi res;
        while (x != 1) {
            res.push_back(f.at(x));
            x /= f.at(x);
        }
        return res;
    }
    // 素因数分解したときの各因数の個数をsecondに持つpairにする
    vector<P> primeFactorizedPair(ll x) {
        vi fl = primeFactorizedList(x);
        if (fl.size() == 0) return {};
        vector<P> res(1, P(fl.at(0), 0));
        for (ll p : fl) {
            if (res.back().first == p) {
                res.back().second++;
            } else {
                res.emplace_back(p, 1);
            }
        }
        return res;
    }
};

int main() {
    // fasten cin
    cin.tie(0);
    ios::sync_with_stdio(false);
    // implement
    ll n;
    cin >> n;
    Sieve s = Sieve(55556);
    vi cand;
    for(ll p:s.primes){
        string str = to_string(p);
        if((str.at(str.size()-1)-'0')==1){
            cand.push_back(p);
        }
        if(cand.size()>55) break;
    }
    rep(i,n) cout << cand.at(i) << " ";
    cout << endl;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;

const int MAX_N=10000;
vector<VI> g1(MAX_N);
vector<VI> g2(MAX_N);
VI flag(MAX_N);
int fcnt = 0;
int gcnt = 0;
vector<bool>vis(MAX_N, false);
VI group(MAX_N);

void dfs(int v) {
	vis[v] = true;
	for (int w : g1[v]) {
		if (!vis[w])dfs(w);
	}
	flag[fcnt] = v;
	//cout << fcnt << " " << v << endl;
	fcnt++;
}

void rdfs(int v) {
	vis[v] = false;
	group[v] = gcnt;
	//cout << v << " " << gcnt << endl;
	for (int w : g2[v]) {
		if (vis[w])rdfs(w);
	}
}

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

	int N, M;
	cin >> N >> M;
	REP(i, M) {
		int a, b;
		cin >> a >> b;
		g1[a].push_back(b);
		g2[b].push_back(a);
	}
	REP(i, N) {
		if (!vis[i])dfs(i);
	}
	//REP(i, N)cout << flag[i] << endl;

	for (int i = N - 1; i >= 0; i--) {
		if (vis[flag[i]]) {
			//cout << i << endl;
			rdfs(flag[i]);
			gcnt++;
		}
	}
	//REP(i, N)cout << group[i] << endl;

	int Q;
	cin >> Q;
	REP(i, Q) {
		int a, b;
		cin >> a >> b;
		cout << (group[a] == group[b]) << endl;
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n;cin >> n;
    vl a(n);rep(i,n)cin >> a[i];
    ll l;cin >> l;
    vvl v(21,vl(n));
    rep(i,n){
        auto itr=upper_bound(all(a),a[i]+l);
        itr--;
        v[0][i]=itr-a.begin();
        if(i==n-1)v[0][i]=inf;
    }
    rep(j,20){
        rep(i,n){
            if(v[j][i]==inf)v[j+1][i]=inf;
            else v[j+1][i]=v[j][v[j][i]];
        }
    }
    /*rep(j,20){
        rep(i,n){
            cout << v[j][i] <<" ";
        }
        cout << endl;
    }*/
    //exit(0);
    ll q;cin >> q;
    while(q--){
        ll a,b;cin >> a >> b;a--;b--;
        if(a>b)swap(a,b);
        ll ng=0,ok=100000;
        while(abs(ok-ng)>1){
            ll mid=(ok+ng)/2;
            ll t=a;
            rep(i,20){
                if(t==inf)break;
                if(mid&(1<<i)){
                    t=v[i][t];
                }
            }
            if(t>=b)ok=mid;
            else ng=mid;
        }
        cout << ok <<endl;
    }
}


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

int N,arr[200200],mod = 1e9 + 7;
long long prefix[200200],sum;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>N;
	for(int i = 1;i <= N;i++){
		cin>>arr[i];
		prefix[i] = prefix[i - 1] + arr[i];
	}
	for(int i = 2;i <= N;i++){
		sum += (prefix[i - 1] % mod) * (arr[i] % mod);
		sum %= mod;
	}
	cout<<sum<<endl;
}

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

class Combination {
    /*
        :)
    */
   public :

    const int mod = 1e9 + 7;

    ll add(ll x, ll y) {
        x += y;
        if (x >= mod) return x - mod;
        return x;
    }
    ll sub(ll x, ll y) {
        x -= y;
        if (x < 0) return x + mod;
        return x;
    }
    ll mult(ll x, ll y) {
        return (x * y) % mod;
    }
    ll bin_pow(ll x, ll p) {
        if (p == 0) return 1;
        if (p & 1) return mult(x, bin_pow(x, p - 1));
        return bin_pow(mult(x, x), p / 2);
    }
    ll rev(ll x) {
        return bin_pow(x, mod - 2);
    }
    ll fact(ll a) {
        ll S = 1;
        for (ll I = 1 ; I <= a; I++)S = S *  I % mod;
        return S;
    }
    ll Combin(ll x, ll y) {
        return mult(fact(x), rev(mult(fact(sub(x, y)), fact(y))));
    }
};
int main () {
    ios_base::sync_with_stdio(false), cin.tie(0);
    Combination cm;
    ll ans = 0;
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    ll araysum = 0;
    for (int i = 0 ; i < n; i++) araysum = cm.add(araysum, a[i]);
    ll S = cm.mult(araysum, araysum);
    ll Si = 0;
    for (int i = 0 ; i < n; i++) {
      Si = cm.add(Si, cm.mult(a[i], a[i]));
    }
    ans = cm.mult(cm.sub(S, Si), cm.rev(2));
    cout << ans ;
}


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

int main(){
	char A[11], B[11], C[11];
	scanf("%s  %s  %s", A, B, C);

	if (A[strlen(A) - 1] == B [0] && B[strlen(B) - 1]  == C [0]){
		printf ("YES\n");
	}
	else {
		printf ("NO\n");
	}

	return 0;
}

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;

int main()
{
	vector<string> a(3);

	string res = "YES";
	for (ll i = 0; i < 3; i++)
	{
		cin >> a[i];

		if (i > 0)
		{
			if (a[i - 1].back() != a[i].front())
			{
				res = "NO";
			}
		}
	}

	println(res);
}


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 1003;

int n, cnt = 0, d;
char a[N][N];

char get(int d)
{
	return (char)(d % 26 + 'a');
}

void addHor(int y, int x)
{
	a[y][x] = get(d);
	a[y][x + 1] = get(d);
	d++;
}

void addVer(int y, int x)
{
	a[y][x] = get(d);
	a[y + 1][x] = get(d);
	d++;
}

void add7(int y, int x)
{
	addVer(y, x);
	addHor(y, x + 1);
	addHor(y, x + 3);

	addHor(y + 1, x + 2);
	addHor(y + 1, x + 5);

	addVer(y + 2, x + 2);
	addVer(y + 2, x + 4);
	addHor(y + 2, x + 5);

	addVer(y + 3,x);

	addVer(y + 4, x + 1);
	addVer(y + 4, x + 6);

	addVer(y + 5, x + 3);

	addHor(y + 6, x);
	addHor(y + 6, x+4);
}

void add6(int y, int x)
{
	addHor(y, x);
	addVer(y, x + 2);
	addHor(y+2, x+1);
	addVer(y+1, x);

	addVer(y+3, x);
	addHor(y+3 + 2, x + 1);

	addVer(y, x + 3);
	addHor(y + 2, x + 3 + 1);

	addHor(y + 3, x+3);
	addVer(y + 3, x +3+ 2);
	addHor(y + 3 + 2, x+3 + 1);
	addVer(y + 3 + 1, x+3);

}

void add5(int y, int x)
{

	addHor(y, x);
	addHor(y, x + 2);

	addHor(y + 4, x + 1);
	addHor(y + 4, x + 3);

	addVer(y + 1, x);
	addVer(y + 3, x);

	addVer(y, x+4);
	addVer(y + 2, x+4);

	addVer(y+1,x+1);
	addHor(y + 3, x + 2);
}

void add3(int y, int x)
{
	addHor(y,x);
	addVer(y+1, x);
	addVer(y, x+2);
	addHor(y+2, x+1);
}

void solve(int l, int r)
{
	if (r - l + 1 <= 2)
		return;

	if ((r - l + 1) == 7)
	{
		add7(l, l);
		return;
	}

	if ((r - l + 1) == 6)
	{
		add6(l, l);
		return;
	}

	if ((r - l + 1) == 5)
	{
		add5(l,l);
		return;
	}

	if ((r - l + 1) == 3)
	{
		add3(l, l);
		return;
	}

	if ((r - l + 1) == 6)
	{
		add6(l, l);
		return;
	}

	a[l][l] = get(d);
	a[l][l + 1] = get(d);
	d++;
	a[l][r] = get(d);
	a[l + 1][r] = get(d);
	d++;
	a[r][r] = get(d);
	a[r][r - 1] = get(d);
	d++;
	a[r][l] = get(d);
	a[r - 1][l] = get(d);
	d++;

	a[l + 1][l] = get(d);
	a[l + 1][l + 1] = get(d);
	d++;
	a[l][r - 1] = get(d);
	a[l + 1][r - 1] = get(d);
	d++;
	a[r - 1][r] = get(d);
	a[r - 1][r - 1] = get(d);
	d++;
	a[r][l + 1] = get(d);
	a[r - 1][l + 1] = get(d);
	d++;

	l += 2;
	r -= 2;
	solve(l, r);
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			a[i][j] = '.';

	if (n > 2)
		solve(1, n);
	else
	{
		cout << -1 << endl;
		return 0;
	}

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
			cout << a[i][j];
		cout << endl;
	}
	return 0;
}

/*

100 20 81

*/

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<time.h>
#include<cstdlib>
typedef long long ll;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define mm(a) memset(a,0,sizeof(a))
#define lr rt<<1
#define rr rt<<1|1
#define sync std::ios::sync_with_stdio(false);std::cin.tie(0);
#define inf 0x3f3f3f3f
#define eqs 1e-8
#define lb(x) (x&(-x))
#define ch(a) (int(a-'a')+1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef pair<int,int> pii;
const double pi=acos(-1);
const int maxn=1100;
const ll Mod=1000000007;

char ans3[12][12] = {"aa.",
                     "..a",
                     "..a"};
char ans4[12][12] = {"aacd",
                     "bbcd",
                     "dcaa",
                     "dcbb"};
char ans5[12][12] = {"aabc.",
                     "..bcd",
                     "iij.d",
                     "n.jaa",
                     "nkkbb"};

char ans7[12][12] = {"aade...",
                     "bbde...",
                     "ccffg..",
                     "..i.gee",
                     "..ihhdd",
                     "....cba",
                     "....cba"};

int n;
char a[maxn][maxn];

void add(int st,int len,char (*T)[12])
{
    int x,y;
    for(int i=0;i<len;i++)
    {
        for(int j=0;j<len;j++)
        {
            x=st+i;
            y=st+j;
            a[x][y]=T[i][j];
        }
    }
}

int main()
{
    sync;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
            a[i][j]='.';
    }
    if(n<=2)
    {
        cout<<-1<<endl;
        return 0;
    }
    if(n==3)
        add(0,3,ans3);
    else if(n==4)
        add(0,4,ans4);
    else if(n==5)
        add(0,5,ans5);
    else if(n==7)
        add(0,7,ans7);
    else if(n==11)
    {
        add(0,4,ans4);
        add(4,7,ans7);
    }
    else if(n%3==0)
    {
        for(int i=0;i<=n;i+=3)
            add(i,3,ans3);
    }
    else
    {
        if(n%5==0)
        {
            for(int i=0;i<=n;i+=5)
                add(i,5,ans5);
        }
        else if(n%5==1)
        {
            add(0,4,ans4);
            add(4,7,ans7);
            for(int i=11;i<=n;i+=5)
            add(i,5,ans5);
        }
        else if(n%5==2)
        {
            add(0,7,ans7);
            for(int i=7;i<=n;i+=5)
            add(i,5,ans5);
        }
        else if(n%5==3)
        {
            add(0,4,ans4);
            add(4,4,ans4);
            for(int i=8;i<=n;i+=5)
            add(i,5,ans5);
        }
        else if(n%5==4)
        {
            add(0,4,ans4);
            for(int i=4;i<=n;i+=5)
            add(i,5,ans5);
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
            cout<<a[i][j];
        cout<<endl;
    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define mkp make_pair
#define fi first
#define se second
void read(int &x){
	char ch=getchar();x=0;
	for (;ch==' '||ch=='\n';ch=getchar());
	for (;ch!=' '&&ch!='\n';x=x*10+ch-'0',ch=getchar());
}
void write(int x){
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
int n,m,cnt;
vector<pii>G[100004];
vector<int>mycol[100004];
vector<pii>g[500004];
int dist[500004];
bool vis[500004];
map<pii,int>mp;
int main(){
	read(n),read(m);
	for (int i=0;i<m;i++){
		int a,b,c;read(a),read(b),read(c);
		G[a].push_back(mkp(b,c));
		G[b].push_back(mkp(a,c));
	}
	for (int i=1;i<=n;i++)
		for (int j=0;j<G[i].size();j++)
			if (mp.find(mkp(i,G[i][j].se))==mp.end())
				mp[mkp(i,G[i][j].se)]=++cnt,mycol[i].push_back(cnt);
	for (int i=1;i<=n;i++){
		for (int j=0;j<G[i].size();j++)
			g[mp[mkp(i,G[i][j].se)]].push_back(mkp(mp[mkp(G[i][j].fi,G[i][j].se)],0));
		for (int j=0;j<mycol[i].size();j++) g[mycol[i][j]].push_back(mkp(cnt+i,0)),g[cnt+i].push_back(mkp(mycol[i][j],1));
	}
	for (int i=1;i<=cnt+n;i++)dist[i]=1e8;
	deque<int>dq;dist[1+cnt]=0,dq.push_back(1+cnt);
	while (!dq.empty()){
		int x=dq.front();dq.pop_front();
		if (vis[x]) continue;vis[x]=1;
		for (int i=0;i<g[x].size();i++) if (dist[g[x][i].fi]>dist[x]+g[x][i].se){
			dist[g[x][i].fi]=dist[x]+g[x][i].se;
			if (g[x][i].se==0) dq.push_front(g[x][i].fi);
			else dq.push_back(g[x][i].fi);
		}
	}
	if (dist[cnt+n]==1e8) puts("-1");
	else write(dist[cnt+n]);
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k; cin >> n >> k;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  ll ans = 0;
  for(int i=0; i<=k; i++) { // push
    int j = k-i; // pop
    if(j >= n) {
      priority_queue<ll, vector<ll>, greater<ll>> q;
      rep(l, n) q.push(v[l]);
      rep(l, i) {
        if(q.empty()) break;
        if(q.top() >= 0) break;
        q.pop();
      }
      ll tmp = 0;
      while(!q.empty()) {
        tmp += q.top();
        q.pop();
      }
      chmax(ans, tmp);
    } else {
      for(int l=0; l<=j; l++) {
        priority_queue<ll, vector<ll>, greater<ll>> q;
        rep(m, l) q.push(v[m]);
        rep(m, j-l) q.push(v[n-1-m]);
        rep(m, i) {
          if(q.empty()) break;
          if(q.top() >= 0) break;
          q.pop();
        }
        ll tmp = 0;
        while(!q.empty()) {
          tmp += q.top();
          q.pop();
        }
        chmax(ans, tmp);
      }
    }
  }
  cout << ans << endk;
  return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define w(x) int x; cin>>x; while(x--)
#define pb push_back
#define mp make_pair
#define mod 1000000007
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll a,b;
	cin>>a>>b;
	ll c=1,d=0;
	while(c<b){
		c=c+a-1;
		d++;
	}
	cout<<d;
}

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

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

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

int main() {
    ll A,B;
    cin >>A>>B;
    if(A<=8 && B<=8){
        cout<<"Yay!"<<endl;
    }else{
        cout<<":("<<endl;
    }

    return 0;

}


#include <bits/stdc++.h>
#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 INF 1e9
typedef long long ll;
using namespace std;

int main() {
  int n;
  double avg = 0, count = 0;
  cin >> n;
  std::vector<int> v(n), x(n), y(n);
  REP(i,n){
    v.at(i) = i;
    cin >> x.at(i) >> y.at(i);
  }

  do {
    REP(i,n-1){
      avg += sqrt(pow(x[v[i]]-x[v[i+1]],2) + pow(y[v[i]]-y[v[i+1]],2));
    }
    count += 1;
  } while (std::next_permutation(v.begin(), v.end()));
  cout << fixed << setprecision(10) << avg / count;

  return 0;

}

#include <iostream>
#include <cstring>
#include<vector>
#include <algorithm>
#include<cstdlib>
#include<set>
#include<math.h>
#include<map>
#include<iomanip>

using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(ll i=0;i<n;i++)

int main()
{
	ll n;
	cin >> n;
	vector<ll> x(n);
	vector<ll> y(n);
	rep(i, n) cin >> x[i] >> y[i];
	double sum = 0;
	rep(i, n) for (ll j = i + 1; j < n; j++)
	{
		sum += sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
	}
	double ans = 2 * sum / n;
	cout << fixed << setprecision(8) << ans << "\n";
	return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;
//const int N = 1e3 + 5;
int main(){

	int n,d=0,t=0,count=0;
	cin >> n;
	if (0 <= n&&n <= 2){
		cout << 0 << endl;
		return 0;
	}
	while (n)
	{
		n -=3;
		if (n >= 3)
			count++;
		else{
			count++;
			break;
            }

	}
	cout << count << endl;

}

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    n=n/3;
    printf("%d\n",n);
}


#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for(Int i = 0; i < (Int)(n); ++i)
#define guard(x) if( not (x) ) continue;

struct DisjointSet {
   DisjointSet(Int n)
      : ps(n, -1), ns(n, 1) {}
   std::vector<Int> ps, ns;
   Int find(Int x) {
      while( ps[x] != -1 ) x = ps[x];
      return x;
   }
   void unite(Int x, Int y) {
      x = find(x), y = find(y);
      if( x != y ) {
         if( not ( ns[x] <= ns[y] ) ) std::swap(x, y);
         ps[x] = y;
         ns[y] += ns[x];
      }
   }
};

int main() {
   Int v, e;
   std::cin >> v >> e;
   std::vector<Int> ss(e), ts(e), ws(e);
   rep(i, e) std::cin >> ss[i] >> ts[i] >> ws[i];
   std::vector<std::pair<Int, Int>> xs;
   rep(i, e) xs.emplace_back(ws[i], i);
   std::sort(xs.begin(), xs.end());
   DisjointSet z(v);
   Int res = 0;
   rep(i, e) {
      Int k = xs[i].second;
      Int s = ss[k], t = ts[k];
      guard( z.find(s) != z.find(t) );
      z.unite(s, t);
      res += ws[k];
   }
   printf("%ld\n", res);
}

#include "bits/stdc++.h"

using namespace std;

int main(){
  int N,M;
  cin>>N>>M;
  vector<vector<int>>edge(N,vector<int>(N));
  for(int i=0;i<M;i++){
    int a,b,c;
    cin>>a>>b>>c;
    a--;
    b--;
    edge[a][b]=c;
    edge[b][a]=c;
  }
  vector<int>box(1<<N);
  for(int i=0;i<1<<N;i++){
    for(int j=0;j<N;j++){
      if((i>>j)&1){
      	for(int k=j+1;k<N;k++){
          if((i>>k)&1)box[i]+=edge[j][k];
        }
      }
    }
  }
  vector<vector<int>>dp(1<<N,vector<int>(N,-1e9));
  for(int i=1;i<1<<N;i+=2)dp[i][0]=box[i];
  for(int i=1;i<1<<N;i+=2){
    for(int j=0;j<N;j++){
      if(!((i>>j)&1))continue;
      int bag=(1<<N)-1-i;
      int c=bag;
      while(c>0){
        for(int k=1;k<N;k++){
          if(!edge[j][k])continue;
          if((c>>k)&1) dp[i|c][k]=max(dp[i|c][k],dp[i][j]+edge[j][k]+box[c]);
        }
        c--;
        c&=bag;
      }
    }
  }
  int ans=0;
  for(int i=0;i<N;i++){
    for(int j=i+1;j<N;j++)ans+=edge[i][j];
  }
  cout<<ans-dp.back().back()<<endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  deque<int> jewel;
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    jewel.push_back(num);
  }
  int ans = -1;
  for (int i = 0; i <= min(N, K); i++) {
    for (int j = 0; j <= min(N, K)-i; j++) {
      deque<int> jewel_copy = jewel;
      vector<int> tmp;
      for (int a = 0; a < i; a++) {
        tmp.push_back(jewel_copy.front());
        jewel_copy.pop_front();
      }
      for (int b = 0; b < j; b++) {
        tmp.push_back(jewel_copy.back());
        jewel_copy.pop_back();
      }
      sort(tmp.begin(), tmp.end());
      int value = 0, trash = K - i - j;
      for (auto x : tmp) {
        if (trash > 0 && x < 0) {
          trash--;
          continue;
        }
        value += x;
      }
      if (value > ans) ans = value;
    }
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int n,c,dp[55][55][55][55];
vi v;

int f(int i,int cnt,int mxgap,int lst){
    if(i==n){
        mxgap=max(mxgap,i-lst-1);
        if(cnt+(n-cnt-mxgap)*2<=c)return 0;
        return -INF;
    }
    int &ans=dp[i][cnt][mxgap][lst+1];
    if(ans!=-INF)return ans;
    ans=v[i]+f(i+1,cnt+1,max(mxgap,i-lst-1),i);
    ans=max(ans,f(i+1,cnt,mxgap,lst));
    return ans;
}

signed main(){
    FASTER;
    cin>>n>>c;
    v=vi(n);cin>>v;
    for(int i=0;i<55;i++){
        for(int j=0;j<55;j++){
            for(int k=0;k<55;k++){
                for(int l=0;l<55;l++){
                    dp[i][j][k][l]=-INF;
                }
            }
        }
    }
    cout<<f(0,0,0,-1);
}


#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
#define low lower_bound
#define upp upper_bound
#define rep(i,n) for (int i = 0; i < (int)(n); ++i)
#define Rep(i,n) for (int i = (int)(n); i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
#define rever(vec) reverse(all(vec));
#define cend printf("\n");
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define prique2(T) priority_queue<T>
struct edge { int to, cost; };
using P = pair<int, int>;
using PP = pair<P, int>;
using D = pair<double, double>;
const int mod2 = 998244353;
const int mod = (int)1e9 + 7, inf = (int)1e16;
const string sp = " ";

int modpow(int x, int n) {
	int ans = 1;
	while (n > 0) {
		if (n & 1) {
			ans *= x;
			if (mod <= ans) ans %= mod;
		}
		x *= x;
		if (mod <= x) x %= mod;
		n >>= 1;
	}
	return ans;
}

int fact[1000000];
void f_init(int n) {
	if (1000000 <= n) return;
	fact[0] = fact[1] = 1;
	for (int i = 2; i <= n; ++i) {
		fact[i] *= fact[i - 1];
		if (mod <= fact[i]) fact[i] %= mod;
	}
	return;
}

int comb(int n, int r) {
	if (n < r) return 0;
	int ans = fact[n] * modpow(fact[n - r], mod - 2) % mod * modpow(fact[r], mod - 2);
	if (ans < 0) return ans + mod;
	return ans;
}

int perm(int n, int r) {
	if (n < r) return 0;
	if (n - r < r) r = n - r;
	int ans= fact[n] * modpow(fact[n - r], mod - 2) % mod;
	if (ans < 0) return ans + mod;
	return ans;
}

int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

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

bool pri(int p) {
	for (int i = 2; i * i <= p; ++i) if (p % i == 0) return false;
	return p > 1;
}

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

P operator+(const P& a, const P& b) {
	return P(a.first + b.first, a.second + b.second);
}
P operator-(const P& a, const P& b) {
	return P(a.first - b.first, a.second - b.second);
}

class unionfind {
private:
	int par[200010], rankth[200010], sizeth[200020];
public:
	void init(int n) {
		rep(i, n) {
			par[i] = i;
			rankth[i] = 0;
			sizeth[i] = 1;
		}
	}
	int root(int x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return;
		if (rankth[x] < rankth[y]) {
			par[x] = y;
			sizeth[y] += sizeth[x];
		}
		else {
			par[y] = x;
			if (rankth[x] == rankth[y]) rankth[x]++;
			sizeth[x] += sizeth[y];
		}
	}
};

int n;
P x[100000],y[100000];
prique(PP) que;
int ans;

signed main() {
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x[i].first>>y[i].first;
		x[i].second=i;
		y[i].second=i;
	}
	sort(x,x+n);
	sort(y,y+n);
	for(int i=1;i<n;i++){
		que.push({{x[i].first-x[i-1].first,x[i-1].second},x[i].second});
		que.push({{y[i].first-y[i-1].first,y[i-1].second},y[i].second});
	}
	unionfind UF;
	UF.init(n);
	while((int)que.size()){
		PP p=que.top();que.pop();
		if(!UF.same(p.first.second,p.second)){
			ans+=p.first.first;
			UF.unite(p.first.second,p.second);
		}
	}
	cout<<ans<<endl;
}

#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<deque>
#include<queue>
using namespace std;
#define int long  long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int INF = 1e10;
const long double PI = (acos(-1));

signed main() {
	int N;
	cin >> N;
	vector<int>A(N), B(N);
	rep(i, N)cin >> A[i] >> B[i];
	priority_queue<P>q;
	rep(i, N) {
		P p = { A[i] + B[i] ,i };
		q.push(p);
	}
	int ans = 0;
	rep(i, N) {
		P p = q.top();
		q.pop();
		if (i % 2 == 0)ans += A[p.second];
		else ans -= B[p.second];
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

string S;
int K;
int dp[300][300][300];

int rec(int left, int right, int rest) {
  if(left >= right) return left == right;
  if(~dp[left][right][rest]) return dp[left][right][rest];
  int ret = max(rec(left + 1, right, rest), rec(left, right - 1, rest));
  if(rest > 0) ret = max(ret, rec(left + 1, right - 1, rest - 1) + 2);
  if(S[left] == S[right]) ret = max(ret, rec(left + 1, right - 1, rest) + 2);
  return dp[left][right][rest] = ret;
}

int main() {
  cin >> S >> K;
  memset(dp, -1, sizeof(dp));
  cout << rec(0, S.size() - 1, K) << endl;
}

// luogu-judger-enable-o2
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
#define R register
using namespace std;
char s[351];int len,k;
int f[351][351][351],ans;
inline void read(R int &x)
{
    x=0;R int f=1;
	R char c=getchar();
	while(c<'0'||c>'9')
	{
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>=48&&c<=57)
	{
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
	x=x*f;
}
inline void input_data()
{
	char ch=getchar();
	while(ch>='a'&&ch<='z')
		s[++len]=ch,ch=getchar();
	read(k);return;
}
int dfs(R int k,R int l,R int r)
{
	if(l>r) return false;
	if(l==r) return s[l]==s[r];
	if(f[k][l][r]) return f[k][l][r];
	f[k][l][r]=max(dfs(k,l+1,r),dfs(k,l,r-1));
	if(s[l]==s[r]) f[k][l][r]=max(f[k][l][r],dfs(k,l+1,r-1)+2);
	if(k>=1) f[k][l][r]=max(f[k][l][r],dfs(k-1,l+1,r-1)+2);
	return f[k][l][r];
}
int main()
{
	input_data();
	for(R int i=0;i<=k;++i)
		ans=max(ans,dfs(i,1,len));
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
long long N,A,B,counter=0;
cin>>N>>A>>B;
long long a[100000];
for(int j=0; j<N;j++){
cin>>a[j];}
for(int j=0; j<N-1;j++){
if((a[j+1]-a[j])*A<=B){
counter+=A*(a[j+1]-a[j]);}
else
counter+=B;
}
cout<<counter<<endl;}

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

typedef long long ll;
typedef pair<int,int> P;
int dx[] = {-1, 1, 0,  0};
int dy[] = { 0, 0, 1, -1};

int main(){
  int n;
  cin >> n;
  int a[n+1];
  a[0] = 0;
  for(int i=1; i<=n; i++) cin >> a[i];
  int s = 0;
  for(int i=0; i<n; i++){
    s += abs(a[i+1]-a[i]);
  }
  s += abs(a[0]-a[n]);
  int de[n];
  for(int i=0; i<n-1; i++){
    de[i] = abs(a[i+2]-a[i]) - abs(a[i+1]-a[i]) -abs(a[i+2]-a[i+1]);
  }
  de[n-1] = abs(a[0]-a[n-1]) -abs(a[n] - a[n-1]) - abs(a[n]-a[0]);
  for(int i=0; i<n; i++){
    cout << s+de[i] << endl;
  }
  return 0;
}


#include <iostream>

using namespace std;

const int MOD = 1000000007;

int dv[1000];

int sqrt(int x) {
  int l = 1, r = x;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    (m * m <= x ? l : r) = m;
  }
  return l;
}

void memoDivisor(int n) {
  while (n > 1) {
    for (int i = 2; i <= n; ++i) {
      if (n % i == 0) {
        dv[i]++;
        n /= i;
        break;
      }
    }
  }
}

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

  for (int i = 1; i <= N; ++i) memoDivisor(i);

  long long cnt = 1;
  for (int i = 2; i <= N; ++i) {
    cnt = cnt * (dv[i] + 1) % MOD;
  }

  cout << cnt << endl;
}

#include <bits/stdc++.h>
using namespace std;
int p[168], q[168], mod = 1000000007;
int primecheck(int n) {
    int d = 0;
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0) d++;
        if (d == 2) break;
    }
    if (d > 1) return 0;
    else return 1;
}
int main() {
    int k = 0, n, l;
    long long f = 1;
    for (int i = 0; i < 168; i++) q[i] = 0;
    for (int i = 2; i <= 997; i++)
    {
        if (primecheck(i) == 1)
        {
            p[k] = i;
            k++;
        }
    }
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        k = 0, l = i;
        while (l != 1)
        {
            if (l % p[k] == 0)
            {
                l /= p[k];
                q[k]++;
            }
            else k++;
        }
    }
    for (int i = 0; i < 168; i++)
    {
        f *= (q[i] + 1);
        f %= mod;
    }
    cout << f;
}


#include <stdio.h>
int main (){
	char name1[105];
	char name2[105];
	scanf("%s%s",&name1,&name2);
	printf("%s%s\n",name2,name1);
return 0;
}

#include<iostream>
#include<string>

using namespace std;

const int MAX_DICE_NUMBER = 5;

void moveNorth(int diceRot[3]) {
	int temp = diceRot[2];
	diceRot[2] = diceRot[1];
	diceRot[1] = MAX_DICE_NUMBER - temp;
}

void moveSouth(int diceRot[3]) {
	int temp = diceRot[1];
	diceRot[1] = diceRot[2];
	diceRot[2] = MAX_DICE_NUMBER - temp;
}

void moveEast(int diceRot[3]) {
	int temp = diceRot[1];
	diceRot[1] = diceRot[0];
	diceRot[0] = MAX_DICE_NUMBER - temp;
}

void moveWest (int diceRot[3]) {
	int temp = diceRot[0];
	diceRot[0] = diceRot[1];
	diceRot[1] = MAX_DICE_NUMBER - temp;
}

int main() {
	int diceValue[6];
	int diceRotation[3] = {3,0,4};

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

	string sequence;
	//cin.sync();
	cin >> sequence;
	//cin.sync();
	//cout << sequence << endl;

	for (int i = 0; i < sequence.length(); i++) {
		switch (sequence[i])
		{
		case 'N':
			moveNorth(diceRotation);
			break;
		case 'S':
			moveSouth(diceRotation);
			break;
		case 'E':
			moveEast(diceRotation);
			break;
		case 'W':
			moveWest(diceRotation);
			break;
		default:
			break;
		}
	}

	cout <<  diceValue[diceRotation[1]]<< endl;

	return 0;
}


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

// ----------- define --------------
#define int long long
#define vi vector<int>
#define vc vector<char>
#define ii pair<int,int>
#define fi first
#define sc second
#define stoi stoll
#define all(x) (x).begin(),(x).end()
#define get_bit(x, k) ((x >> k) & 1)
// ---------------------------------

void MAIN() {
  string s;
  cin >> s;
  set<char> d;
  for (char c = 'a'; c <= 'z'; c++) d.insert(c);
  for (auto &i : s) d.erase(i);
  if (d.size()) {
    s += *d.begin();
    cout << s << '\n';
    return;
  }
  set<char> g;
  while (s.size()) {
    char c = s.back();
    s.pop_back();
    auto it = g.upper_bound(c);
    if (it != g.end()) {
      s += *it;
      cout << s << '\n';
      return;
    }
    g.insert(c);
  }
  cout << -1 << '\n';
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) MAIN();
}

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

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

int main() {
    string s;
    cin >> s;
    int n = s.size();
    if (n != 26) {
        vector<bool> flg(26, false);
        rep(i, n) flg[s[i]-'a'] = true;
        cout << s;
        rep(i, 26) {
            if (!flg[i]) {
                cout << (char)(i + 'a') << endl;
                return 0;
            }
        }
    }
    else {
        string t = s;
        if (!next_permutation(t.begin(), t.end())) {
            cout << -1 << endl;
            return 0;
        }
        rep(i, n) {
            cout << t[i];
            if (t[i] != s[i]) {
                cout << endl;
                break;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 100100
inline int read()
{
	int x=0;bool t=false;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')t=true,ch=getchar();
	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
	return t?-x:x;
}
int a[MAX],n,m,S[MAX],top;
int main()
{
	n=read();m=read();
	for(int i=1;i<=m;++i)
	{
		a[i]=read();
		if(a[i]&1)S[++top]=i;
	}
	if(top>2){puts("Impossible");return 0;}
	if(top)swap(a[1],a[S[1]]);
	if(top>1)swap(a[m],a[S[2]]);
	if(m==1)
	{
		if(a[1]==1)printf("1\n1\n1\n");
		else printf("%d\n2\n%d 1\n",a[1],a[1]-1);
		return 0;
	}
	for(int i=1;i<=m;++i)printf("%d ",a[i]);puts("");
	printf("%d\n",m-(a[m]==1));
	printf("%d ",a[1]+1);
	for(int i=2;i<m;++i)printf("%d ",a[i]);
	if(a[m]>1)printf("%d ",a[m]-1);
	puts("");return 0;
}

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


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define SZ(X) ((int)(X).size())
#define endl "\n";
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int a,b;
	cin>>a>>b;
	cout<<(a+b-3)/(a-1);

   return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr long long MOD = 1000000007;
constexpr long long INF = 1LL << 60;
const long double PI = acosl(-1.0);
constexpr long double EPS = 1e-11;
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;
}
//O(k)愚直,nは大きいがkがそうでもないときに思い出す
long long modpow(long long x,long long n,long long mod){
    long long res = 1;
    while(n > 0) {
        if(n & 1) (res *= x) %= mod;
        (x *= x) %= mod;
        n >>= 1;
    }
    return res;
}
long long modinv(long long x, long long mod){
     return modpow(x, mod - 2, mod); }
long long modcomb(long long n,long long r,long long mod){
    long long numerator = 1;
    for(long long i = 0; i < r; i++) {
        (numerator *= (n - i)) %= mod;
    }
    long long denominator = 1;
    for(long long i = 1; i <= r; i++) {
        (denominator *= i) %= mod;
    }
    denominator = modinv(denominator, mod);
    return numerator * denominator % mod;
}
int main(){
    ll n, k;
    cin >> n >> k;
    cout << n - k + 1 << endl;
}


#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

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

ll N, x;
ll a[101010];

int main() {
  cin >> N >> x;
  rep(i, 0, N) { cin >> a[i]; }

  ll ans = max(0LL, a[0] - x);
  a[0] -= ans;
  ll prev = a[0];
  rep(i, 1, N) {
    ll diff = max(0LL, prev + a[i] - x);
    ans += diff;
    a[i] -= diff;
    prev = a[i];
  }

  cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef double ld;
typedef pair<ld, ld> pdd;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

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

const int N = 305;
int dp[N][N][N];

int main(){
  fastIO;
  string s;
  cin >> s;
  int k;
  cin >> k;
  int n = s.size();
  for(int i = 0 ; i < n; i ++ ){
    dp[i][i][0] = 1;
    if(i + 1 < n){
      if(s[i] == s[i + 1]){
        dp[i][i + 1][0] = 2;
      }
      else{
        dp[i][i + 1][0] = 1;
        dp[i][i + 1][1] = 2;
      }
    }
  }
  int r;
  int cost;
  for(int len = 3; len <= n; len ++ ){
    for(int l = 0 ; l + len - 1 < n; l ++ ){
      r = l + len - 1;
      for(int t = 0 ; t <= k ; t ++ ){
        dp[l][r][t] = max(dp[l][r-1][t], dp[l+1][r][t]);
        cost = (s[l] != s[r]);
        if(t - cost >= 0){
          dp[l][r][t] = max(dp[l][r][t], dp[l+1][r-1][t - cost] + 2);
        }
      }
    }
  }
  int ans = 0;
  for(int t = 0 ;t <= k ; t ++ ) ans = max(ans, dp[0][n-1][t]);
  cout << ans << "\n";
  return 0;
}


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

const int maxn = 100010;
int n, K, a[maxn];

int main() {
    scanf("%d %d", &n, &K);
    int g = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]), g = __gcd(a[i], g);
    }
    if (K <= *max_element(a + 1, a + n + 1) && !(K % g)) {
        printf("POSSIBLE\n");
    } else {
        printf("IMPOSSIBLE\n");
    }
    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> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
int main(){
    ios::sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    int g;
    cin >> g;
    int max=g;
    rep(i,n-1) {
        int a;
        cin >> a;
        chmax(max, a);
        g = __gcd(g,a);
    }
    if(k > max) cout << "IMPOSSIBLE" << endl;
    else if(k%g == 0) cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

string s; int n;
int K;
int dp[300][300][301];	//dp[l][r][x] = 区間[l, r]内における最長回文の長さ（変更数≦x)
int iter[2][300][26];	//iter[left or right][pos][moji] = (leftの場合)s[pos],…,s[n-1]の間において文字mojiが出現する最初の場所
						//, (rightの場合)s[0],…,s[pos]の間において文字mojiが出現する最後の場所

int dfs(int l, int r, int x) {
	int len = r - l + 1;
	if (len == 1) return 1;
	if (len <= 0) return 0;
	if (dp[l][r][x] != -1) return dp[l][r][x];

	int ret = 0;
	if (x > 0 && len >= 2) ret = 2 + dfs(l + 1, r - 1, x - (s[l] != s[r]));

	for (int i = 0; i < 26; i++) {
		int nl = iter[0][l][i];
		int nr = iter[1][r][i];
		if (nl == -1 || nr == -1 || nl > nr) continue;
		int res = 2 - (nl == nr) + dfs(nl + 1, nr - 1, x);
		ret = max(ret, res);
	}
	return dp[l][r][x] = ret;
}

int main() {
	int i, j, k;

	cin >> s >> K;
	n = s.length();
	rep(i, n) rep(j, n) rep(k, n + 1) dp[i][j][k] = -1;
	rep(i, 2) rep(j, n) rep(k, 26) iter[i][j][k] = -1;

	rep(i, n) {
		for (j = i; j < n; j++) {
			if (iter[0][i][s[j] - 'a'] == -1) {
				iter[0][i][s[j] - 'a'] = j;
			}
		}
		for (j = i; j >= 0; j--) {
			if (iter[1][i][s[j] - 'a'] == -1) {
				iter[1][i][s[j] - 'a'] = j;
			}
		}
	}
	int res = dfs(0, n - 1, K);
	cout << res << endl;
	return 0;
}

//K=0の場合で実験すると、最長部分列のうち一つは回文になっていると予想できる。これが正しければ、最長回文の長さが解の上界になる。
//まあ、AGCなので正しいだろう（は？）
//また、Tの回文部分列なら何でも、TとT'の共通部分列になっている。よって、最長回文の長さが解の下界になる。
//
//よって、「文字列Sの文字をK文字まで変更できるとき、作れる回文部分列の長さは最大でいくらになるか？」という問題になる。
//これは、
//１．文字を変更する→（回文になるように）左から文字を選ぶ
//２．左から文字を選ぶ→（回文になるように）文字を変更する→変更数≦Kかをチェックする。
//３．両端から文字を選ぶ→（回文になるように）文字を変更する
//のいずれでも解くことができるが、３の方針で考えると見通しがよい。このとき、以下の問題を解くのが本質になる。
//
//「両端からペアを作っていく。1文字だけのペアも作れるが、ペアは入れ子構造になっている必要がある。
//ペアを作るとき、違う文字ならコストが1発生する。コストK以下でいくつペアを作れるか？」
//
//この問題は、dp[左端l][右端r][変更数x] = そこからいくつのペアを作れるか？というDPで解くことができる。
//遷移は、「両端(l, r)を使う」「コストを消費せず、文字aでペアを作る」の2つだが、どちらもO(1)で行うことができる。
//計算量はO(N^3) * 26 / 2くらいになる。これ間に合うのか？

//beet

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
#define MAX 114514
vector<P> G[MAX*5];
map<int,int> M[MAX];
int dist[MAX*5];
signed main(){
  int n,m;
  cin>>n>>m;
  int cnt=n;
  for(int i=0;i<m;i++){
    int p,q,c;
    cin>>p>>q>>c;
    p--;q--;
    auto add_edge = [&](int x){
      if(!M[x].count(c)){
	M[x][c]=cnt++;
	G[M[x][c]].push_back(P(x,0));
	G[x].push_back(P(M[x][c],1));
      }
    };
    add_edge(p);
    add_edge(q);
    G[M[p][c]].push_back(P(M[q][c],0));
    G[M[q][c]].push_back(P(M[p][c],0));
  }
  memset(dist,-1,sizeof(dist));
  priority_queue<P,vector<P>,greater<P> > q;
  dist[0]=0;
  q.push(P(dist[0],0));
  while(!q.empty()){
    P p=q.top();q.pop();
    int d=p.first,v=p.second;
    if(dist[v]<d) continue;
    for(P e:G[v]){
      int u=e.first,c=e.second;
      if(dist[u]<0||dist[u]>d+c){
	dist[u]=d+c;
	q.push(P(dist[u],u));
      }
    }
  }
  cout<<dist[n-1]<<endl;
  return 0;
}


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

signed main(){
    // cout << fixed << setprecision(10) << flush;

    int n;
    cin >> n;

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

    int max_pop = 0;
    for(int i=0; i<(1<<n); i++){
        bool flag = true;
        for(int j=0; j<n; j++){
            if((i & (1<<j)) == (1<<j)){
                // j が正直者のとき
                for(int k=0; k<x[j].size(); k++){
                    if((i & (1<<x[j][k])) != y[j][k] * (1<<x[j][k])){
                        // 証言と違う
                        flag = false;
                    }
                }
            }
        }
        if(flag && max_pop < __builtin_popcount(i)){
            max_pop = __builtin_popcount(i);
        }
    }
    cout << max_pop << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
void init(){
  cin.tie(0);
  ios::sync_with_stdio(false);
}

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WG = vector< Edges< T > >;
using UG = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

struct UnionFind
{

  vector<int> par;
  vector<int> sizes;

  UnionFind(int n) : par(n), sizes(n, 1) {
    rep(i,n) par[i] = i;
  }

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

  bool unite(int x, int y) {

    x = find(x);
    y = find(y);

    if (x == y) return false;

    if (sizes[x] < sizes[y]) swap(x, y);

    par[y] = x;
    sizes[x] += sizes[y];

    return true;
  }

  int size(int x) {
    return sizes[find(x)];
  }
};

template<typename T>
T kruskal(Edges<T> &es,int n) {
  sort(all(es),[](const edge<T> & a,const edge<T> &b){
      return (a.cost < b.cost);
      });

  UnionFind uf(n);
  T min_cost = 0;

  for(auto &e:es) {
    if(uf.unite(e.src,e.to))
      min_cost += e.cost;
  }

  return min_cost;
}

main(){
  int n;
  cin>>n;
  vector<P> x,y;

  rep(i,n){
    int a,b;
    cin>>a>>b;
    x.PB(P(a,i));
    y.PB(P(b,i));
  }

  sort(all(x));
  sort(all(y));

  Edges<int> es;
  rep(i,n-1){
    P p=x[i+1],q=x[i];
    es.PB(edge<int>(p.S,q.S,abs(p.F-q.F)));
    es.PB(edge<int>(q.S,p.S,abs(p.F-q.F)));
    p=y[i+1],q=y[i];
    es.PB(edge<int>(p.S,q.S,abs(p.F-q.F)));
    es.PB(edge<int>(q.S,p.S,abs(p.F-q.F)));
  }

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


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long double ld;
typedef long long ll;
const int MOD =  1000000007;
const int IINF = INT_MAX;
const ll INF = 1LL << 60;

int main() {
    int n; cin >> n;
    if(n <= 111) cout << 111 << endl;
    else if(n <= 222) cout << 222 << endl;
    else if(n <= 222) cout << 222 << endl;
    else if(n <= 333) cout << 333 << endl;
    else if(n <= 444) cout << 444 << endl;
    else if(n <= 555) cout << 555 << endl;
    else if(n <= 666) cout << 666 << endl;
    else if(n <= 777) cout << 777 << endl;
    else if(n <= 888) cout << 888 << endl;
    else cout << 999 << endl;

    return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int x; cin>>x;
	for (int i=x; i<=999; i++) {
		string cur=to_string(i);
		set<char> st(cur.begin(),cur.end());
		if (st.size()==1) {
			cout<<i<<endl;
		break;
		}
	}
	return 0;
}


#include <vector>
#include <algorithm>

using namespace std;

void scc_dfs(int at, int &cnt, vector<int> &inum, vector<bool> &used, const vector<vector<int>> &graph)
{
    used[at] = true;
    for (int ne : graph[at]) {
        if (!used[ne]) scc_dfs(ne, cnt, inum, used, graph);
    }
    inum[cnt] = at;
    cnt++;
}

#include <iostream>
void scc_rdfs(int at, vector<int> &cs, vector<bool> &used, const vector<vector<int>> &inv)
{
    used[at] = true;
    cs.push_back(at);
    for (int ne : inv[at]) {
        if (!used[ne]) scc_rdfs(ne, cs, used, inv);
    }
}

vector<vector<int>> strongly_connected_components(const vector<vector<int>> &graph)
{
    const int V = graph.size();

    int cnt = 0;
    vector<int> inum(V);
    vector<bool> used(V);
    for (int i = 0; i < V; i++) {
        if (!used[i]) scc_dfs(i, cnt, inum, used, graph);
    }

    vector<vector<int>> inv(V);
    for (int i = 0; i < V; i++){
        for (int v : graph[i]) {
            inv[v].push_back(i);
        }
    }
    fill(used.begin(), used.end(), false);
    vector<vector<int>> ret;
    for (int i = V - 1; i >= 0; i--) {
        vector<int> cs;
        if (!used[inum[i]]) scc_rdfs(inum[i], cs, used, inv);
        ret.push_back(cs);
    }
    return ret;
}

#include <iostream>

int main()
{
    ios::sync_with_stdio(false);

    int V, E;
    cin >> V >> E;
    vector<vector<int>> graph(V);
    int s, t;
    for (int i = 0; i < E; i++) {
        cin >> s >> t;
        graph[s].push_back(t);
    }

    auto res = strongly_connected_components(graph);
    vector<int> num(V);
    for (int i = 0; i < res.size(); i++) {
        for (int v : res[i]) {
            num[v] = i;
        }
    }

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

#include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> P;
#define int long long
#define ll long long
#define mod 1000000007
#define INF (1LL<<60)

void bfs(int s, vector<vector<int>>& g, vector<int>& visited){
	queue<int> q;
	q.push(s);
	visited[s] = s;
	while (!q.empty()){
		int u = q.front(); q.pop();
		for (int v : g[u]){
			if (visited[v] < 0){
				visited[v] = s;
				q.push(v);
			}
		}
	}
}

signed main(){

	int N, K, L;
	vector<vector<int>> road, train;
	cin >> N >> K >> L;
	road.assign(N, vector<int>());
	train.assign(N, vector<int>());
	for (int i = 0; i < K; i++){
		int p, q;
		cin >> p >> q; p--; q--;
		road[p].push_back(q);
		road[q].push_back(p);
	}
	for (int i = 0; i < L; i++){
		int p, q;
		cin >> p >> q; p--; q--;
		train[p].push_back(q);
		train[q].push_back(p);
	}

	vector<int> visited_road(N), visited_train(N);
	visited_road.assign(N, -1);
	visited_train.assign(N, -1);

	for (int i = 0; i < N; i++){
		if (visited_road[i] < 0) bfs(i, road, visited_road);
		if (visited_train[i] < 0) bfs(i, train, visited_train);
	}

	map<pair<int, int>, int> cnt;
	for (int i = 0; i < N; i++){
		cnt[pair<int, int>(visited_road[i], visited_train[i])]++;
	}

	for (int i = 0; i < N; i++){
		cout << cnt[pair<int, int>(visited_road[i], visited_train[i])] << " ";
	}

	return 0;
}


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

int m;
int g[1000000];
int cnt=0;
std::vector<int> G;
void insertionSort(int array[], int n, int g){
	int tmp,j;
	for(int i=g;i<n;i++){
		tmp = array[i];
		j= i-g;
		while(j>=0 && array[j]>tmp){
			array[j+g] = array[j];
			j=j-g;
			cnt++;
		}
		array[j+g]=tmp;
	}
}

void shellSort(int array[],int n){
	cnt = 0;

	for(int h=1; ;){
		if(h>n) break;
		G.push_back(h);
		h = 3 * h + 1;
	}

	for(int i=G.size()-1;i>=0;i--){
		insertionSort(array,n,G[i]);
	}
}

int main(){
	int n;
	int array[1000001];
	cin >> n;

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

	shellSort(array,n);

	cout << G.size() << endl;
	for(int i=G.size()-1;i>=0;i--){
		cout<<G[i];
		if(i) cout<<" ";
	}
	cout << endl;

	cout << cnt << endl;

	for(int i=0;i<n;i++){
		cout << array[i]<<endl;
	}

	return 0;
}

#include <iostream>
#include <utility>
#include <cstdio>
using namespace std;

void insertionSort(int A[], const int n, const int g, int *cnt)
{
    for( int i = g; i < n; ++i ){
        int tmp = A[i];
        int j = i - g;
        while( -1 < j && tmp < A[j] ){
            //cout << A[j+g] << "<-->" << A[j] << endl;
            A[j+g] = A[j];
            j = j - g;
            ++*cnt;
        }
        A[j+g] = tmp;
    }
}

void shellSort(int A[], const int n)
{
    int cnt = 0;
    int m;
    int G[100];
    for( int i = 0; i < 100; ++i ){
        if( i == 0 ) G[i] = n / 2 + 1;
        else G[i] = G[i-1] / 2 - 1;
        if( G[i] < 0 ){
            G[i-1] = 1;
            G[i] = 0;
            m = i;
            break;
        }
    }

    for( int i = 0; i < m; ++i )
        insertionSort(A, n, G[i], &cnt);

    //出力
    printf("%d\n", m);
    for( int i = 0; i < m; ++i ){
        if( i ) printf(" ");
        printf("%d", G[i]);
    }
    printf("\n%d\n", cnt);
    for( int i = 0; i < n; ++i )
        printf("%d\n", A[i]);
}

int main()
{
    //入力
    int n;
    scanf("%d", &n);
    int A[n];
    for( int i = 0; i < n; ++i )
        scanf("%d", &A[i]);

    //シェルソート
    shellSort(A, n);

    return 0;
}


#ifdef __unix__
#include <bits/stdc++.h>
#else
#include <iostream>
#include <algorithm>
#include <math.h>
#include <functional>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_set>
#include <stack>
#include <bitset>
#endif

#define fastIO ios_base::sync_with_stdio(0), cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl '\n'
#define mkp make_pair
/*#define NUMTOSTR(x)  (unsigned char)((x) + 48);*/
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long  ull;
using namespace std;

int main(){
	fastIO;
	string s;
	cin >> s;
	if (s.size() == 2) cout << s;
	else {
		reverse(all(s));
		cout << s;
	}
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    cout << n * 3.141592653589 * 2;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
int main()
{
    int H, W;
    cin >> H >> W;

    vector<string> s(H);
    rep(i, H) cin >> s[i];

    rep(y, H) rep(x, W) {
        if (s[y][x] != '#') {
            continue;
        }
        bool ok =false;
        rep(i, 2) {
            int xx = x - 1 + 2 * i;
            int yy = y;
            if (0 <= xx && xx < W && 0 <= yy && yy < H) {
                if (s[yy][xx] == '#') {
                    ok = true;
                }
            }
        }
        rep(i, 2) {
            int xx = x;
            int yy = y - 1 + 2 * i;
            if (0 <= xx && xx < W && 0 <= yy && yy < H) {
                if (s[yy][xx] == '#') {
                    ok = true;
                }
            }
        }
        if (ok == false) {
            put("No");
            return 0;
        }
    }

    put("Yes");

    return 0;
}

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

int main(void){
  char buf[128] = { 0 };
  short grid[64][64] = { {0} };
  int H,W;

  fgets(buf, sizeof(buf), stdin);
  sscanf(buf, "%d %d", &H, &W);

  for(int i = 0; i < H; i++){
    memset(buf, 0, sizeof(buf));
    fgets(buf, sizeof(buf), stdin);
    for(int j = 0; j < W; j++)
      if(buf[j] == '#') grid[i+1][j+1] = 1;
  }

  int flag = 0;
  for(int i = 1; i < H+1; i++){
    for(int j = 1; j < W+1; j++){
      if(grid[i][j] == 0) continue;
      else{
        if(grid[i-1][j] != 1 && grid[i][j-1] != 1
           && grid[i+1][j] != 1 && grid[i][j+1] != 1){
          flag = 1;
          break;
        }
      }
    }
    if(flag) break;
  }
  if(flag == 0) printf("Yes\n");
  else printf("No\n");
  return 0;
}