#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

int main(){

  int T,A,n;
  cin >> n >> T >> A;
  T--,A--;
  vector<vector<int>> v(n);
  rep(i,n-1){
    int a,b;cin >> a >> b;
    a--,b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ll res = 0;
  bool is = true;
  vector<int> next(n,T);
  {
    vector<int> dt(n,-1);
    dt[T] = 0;
    queue<int> que;
    que.push(T);
    while(!que.empty()){
      int ov = que.front();que.pop();
      for(int nv : v[ov]){
        if(dt[nv] != -1)continue;
        next[nv] = ov;
        dt[nv] = dt[ov] + 1;
        que.push(nv);
      }
    }
    if(dt[A] & 1){
      int d = dt[A];
      while(dt[next[A]] >= d-d/2)A = next[A];
      T = A;
      while(dt[next[T]] >= d/2 && next[T] != T)T = next[T];
      res += d/2;
    }else{
      int d = dt[A];
      while(dt[next[A]] >= d-(d-2)/2)A = next[A];
      T = A;
      while(dt[next[T]] >= (d-2)/2 && next[T] != T)T = next[T];
      res += (d-2)/2;
      is = false;
    }
  }
  int L = -1;
  {
    for(int ov : v[T])for(int nv : v[ov])if(nv == A){
      L = ov;
      break;
    }
  }
  queue<int> que;
  que.push(T);
  vector<int> d(n,-1);
  d[T] = 0;
  while(!que.empty()){
    int ov = que.front();que.pop();
    for(int nv : v[ov]){
      if(nv == A || d[nv] != -1 || nv == L)continue;
      d[nv] = d[ov]+1;
      que.push(nv);
    }
  }
  int K = T;
  rep(i,n)if(d[i] > d[K])K = i;
  if(is)res += d[K];
  else res += d[K]+1;
  cout << res << endl;

  /*////////////
  cout << "L :" << L << endl;
  printf("T :%d A :%d\n",T+1,A+1);

  */////////////

  return 0;
}
/*
7
4 7
1 2
1 3
2 4
2 5
3 6
3 7
*/

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

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

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

const int N = 100005;
vector<VI> tree;
int dist1[N], dist2[N];

void dfs(int v, int d, int from, int *arr) {
    arr[v] = d;
    for (int u : tree[v]) {
        if (u == from) continue;
        dfs(u, d + 1, v, arr);
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n, u, v;
    cin >> n >> u >> v;
    tree.assign(n + 2, VI());
    REP(i, n - 1) {
        int x, y;
        cin >> x >> y;
        tree[x].pb(y);
        tree[y].pb(x);
    }
    dfs(u, 0, 0, dist1);
    dfs(v, 0, 0, dist2);
    int mx = 0;
    for (int i = 1; i <= n; i++) {
        if (dist1[i] < dist2[i]) {
            mx = max(mx, dist2[i]);
        }
    }
    cout << mx - 1 << "\n";
}


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

using namespace std;

int main() {
    int n, x;

    vector<int> a;

    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> x;
        a.push_back(x);
    }

    reverse(a.begin(), a.end());

    for(int i = 0; i < n; i++) {
        if(i == n-1) cout << a[i] << endl;
        else cout << a[i] << " ";
    }

}

#include<stdio.h>
int main(void)
{
	int a[101],s,i,d;
	scanf("%d",&s);
	for(i=1;i<=s;i++){
		scanf("%d",&a[i]);
	}
	d=1;
	for(i=s;i>=1;i--){
		d++;
		printf("%d",a[i]);
		if(d<=s){
			printf(" ");
		}
	}
	printf("\n");
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

const int MOD = 998244353;

int main(){
    int K, N;
    while(cin >> K >> N){
        vector<vector<long long>> pair(K+1, vector<long long>(N+1, 0));
        vector<vector<long long>> solo(K+1, vector<long long>(N+1, 0));
        pair[0][0] = 1;
        solo[0][0] = 1;
        for(int i=1;i<=K;i++){
            long long sum = 0;
            for(int j=0;j<=N;j++){
                pair[i][j] += pair[i-1][j] + 2 * sum;
                pair[i][j] %= MOD;
                sum += pair[i-1][j];
                sum %= MOD;
            }
        }
        for(int i=1;i<=K;i++){
            long long sum = 0;
            for(int j=0;j<=N;j++){
                sum += solo[i-1][j];
                sum %= MOD;
                solo[i][j] = sum;
            }
        }
        vector<int> invalid(2*K+1, 0);
        for(int i=1;i<=K;i++){
            for(int j=i+1;j<=K;j++){
                ++invalid[i+j];
            }
        }
        for(int k=2;k<=2*K;k++){
            long long res = 0;
            int pr = invalid[k];
            if(k%2 == 0){
                for(int i=0;i<=N;i++){
                    res += pair[pr][i] * (solo[K-2*pr-1][N-i] + (i==N ? 0 : solo[K-2*pr-1][N-i-1]));
                    res %= MOD;
                }
            } else {
                for(int i=0;i<=N;i++){
                    res += pair[pr][i] * solo[K-2*pr][N-i];
                    res %= MOD;
                }
            }
            cout << res << endl;
        }
    }
}

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> P;
const int inf = 1 << 28;

int w, h;
int a[21][21];
int sx, sy, gx, gy;

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

bool check(int x, int w) { return 0 <= x && x < w; }

int dfs(int x, int y, int cnt) {
    if (cnt >= 10) {
        return inf;
    }

    int ret = inf;

    for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k], ny = y + dy[k];

        while (check(nx, w) && check(ny, h) && a[ny][nx] != 1 &&
               a[ny][nx] != 3) {
            nx += dx[k];
            ny += dy[k];
        }

        if (!check(nx, w) || !check(ny, h)) {
            continue;
        }

        if (a[ny][nx] == 3) {
            return 1;
        }

        if (nx - dx[k] == x && ny - dy[k] == y) {
            continue;
        }

        a[ny][nx] = 0;

        ret = min(ret, dfs(nx - dx[k], ny - dy[k], cnt + 1) + 1);

        a[ny][nx] = 1;
    }

    return ret;
}

int main() {
    while (cin >> w >> h, !(w == 0 && h == 0)) {
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> a[i][j];

                if (a[i][j] == 2) {
                    sy = i;
                    sx = j;
                }

                if (a[i][j] == 3) {
                    gy = i;
                    gx = j;
                }
            }
        }

        int ans = dfs(sx, sy, 0);

        if (ans <= 10) {
            cout << ans << endl;
        } else {
            cout << -1 << endl;
        }
    }
}


#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
using namespace std;

struct Node{
    int key;
    Node *right, *left, *parent;
};

Node *root, *NIL;

Node * treeMinimum(Node *x){
    while(x -> left != NIL)x = x -> left;
    return x;
}

Node * treeSuccessor(Node *x){
    if(x -> right != NIL)return treeMinimum(x -> right);
    Node *y = x -> parent;
    while(y != NIL && x == y -> right){
        x = y;
        y = y->parent;
    }
    return y;
}

Node * find(Node *u, int k){
    while(u != NIL && k != u -> key){
        if(k < u -> key) u = u -> left;
        else u = u -> right;
    }
    return u;
}

void treeDelete(Node *z){
    Node *y, *x;
    if(z -> left == NIL || z -> right == NIL)y = z;
    else y = treeSuccessor(z);
    if(y -> left != NIL)x = y -> left;
    else x = y -> right;
    if(x != NIL)x -> parent = y -> parent;
    if(y -> parent == NIL)root = x;
    else{
        if(y == y->parent->left)y -> parent -> left = x;
        else y -> parent -> right = x;
    }
    if(y != z)z -> key = y -> key;
    free(y);
}

void insert(int k){
    Node *y = NIL, *x = root, *z;
    z = (Node*)malloc(sizeof(Node));
    z -> key = k;
    z -> right = NIL;
    z -> left = NIL;
    while(x != NIL){
        y = x;
        if(z->key < x->key){
            x = x -> left;
        }
        else{
            x = x -> right;
        }
    }
    z -> parent = y;
    if(y == NIL) root = z;
    else{
        if(z -> key < y -> key){
            y -> left = z;
        }
        else{
            y -> right = z;
        }
    }
    return;
}

void inParse(Node *u){
    if(u == NIL)return;
    inParse(u -> left);
    cout << " " << u -> key;
    inParse(u -> right);
}

void preParse(Node *u){
    if(u == NIL)return;
    cout << " " << u -> key;
    preParse(u -> left);
    preParse(u -> right);
}

int main() {
    int n, x;
    string q;
    cin >> n;
    rep(i, n){
        cin >> q;
        if(q == "insert"){
            cin >> x;
            insert(x);
        }
        else if(q == "print"){
            inParse(root);
            cout << endl;
            preParse(root);
            cout << endl;
        }
        else if(q == "delete"){
            cin >> x;
            treeDelete(find(root, x));
        }
        else {
            cin >> x;
            if(find(root, x) != NIL)cout << "yes" << endl;
            else cout << "no" << endl;
        }
    }
}

//C
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int main(){
	string d="";
	d.resize(52);
	for(int i=0;i<26;i++)d[i]='a'+i;
	for(int i=0;i<26;i++)d[i+26]='A'+i;
	int num;
	//cout<<d<<endl;
	while(true){
	cin>>num;
	if(num==0)break;
	string ans="",next;
	int key[200];
	for(int i=0;i<num;i++)cin>>key[i];
	cin>>next;
	for(int i=0;i<next.size();i++){
		int tmp;
		for(int j=0;j<52;j++)if(d[j]==next[i])tmp=j;
		int index;
		if(i==0)index=0;
		else index=i%num;
		tmp-=key[index];
		if(tmp<0)tmp+=52;
		cout<<d[tmp];
	}
	cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

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

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

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

int main()
{
    int a, b, c;

    while(cin >> a >> b >> c, a, b, c)
    {
        vector<int> sta(a + b + c + 1, 2);

        int N;
        cin >> N;

        vector<tuple<int, int, int, int>> vec;

        rep(num, N)
        {
            int i, j, k, r;

            cin >> i >> j >> k >> r;

            vec.push_back(make_tuple(r, i, j, k));
        }

        sort(vec.begin(), vec.end(), greater<tuple<int, int, int, int>>());

        rep(num, N)
        {
            int i, j, k, r;
            tie(r, i, j, k) = vec[num];

            if(r == 1)
            {
                sta[i] = 1;
                sta[j] = 1;
                sta[k] = 1;
            }
            else if(r == 0)
            {
                int cnt = 0;

                 for(int pro : {i, j, k})
                {
                    if(sta[pro] == 1) cnt++;
                }

                if(cnt != 2) continue;

                for(int pro : {i, j, k})
                {
                    if(sta[pro] == 1) continue;

                    sta[pro] = 0;
                }
            }
        }

        rep(i, a + b + c) cout << sta[i + 1] << endl;
    }
    return 0;
}


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

vector<ll> f(ll n) {
    vector<ll> res;
    for (ll a = 2; a*a <= n; a++) {
        if (n%a != 0) continue;
        while (n%a == 0) {
            n /= a;
        }
        res.push_back(a);
    }
    if (n != 1) res.push_back(n);
    return res;
}

int main() {
    ll a, b;
    cin >> a >> b;
    auto ares = f(a);
    auto bres = f(b);
    int ans = 0;
    for (auto x : ares) {
        for (auto y : bres) {
            if (x == y) ans++;
        }
    }
    ans++;
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define max 1000007
#define ll long long int
using namespace std;
vector <ll> primes;
void sieve()
{
    bool isprime[max+1];
    for(ll i=0;i<=max;i++) {
        isprime[i] = true;
    }
    isprime[0]=false;
    isprime[1]=false;
    for(ll i=3;i*i<=(max);i=i+2)
    {
        if(isprime[i]) {
            for (ll j = i*i; j  <= max; j=j+i) {
                isprime[j] = false;
            }
        }
    }
    primes.push_back(2);
    for(ll i=3;i<max;i=i+2)
    {
        if(isprime[i])
            primes.push_back(i);
    }
}
int main() {
    sieve();
    ll n,d,b,i,flag,x;
   scanf("%lld %lld",&d,&b);
    n=__gcd(d%b,b);
    set <int> s;
    s.insert(1);
    for(i=0;primes[i]*primes[i]<=n;i++)
    {
        while(n%primes[i]==0)
        {
            s.insert(primes[i]);
            n=n/primes[i];
        }
    }
    if(n>1)
        s.insert(n);
    printf("%lu",s.size());
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <queue>
using namespace std;
#define loop(i,N) for(ll (i)=0; (i)<(N);++(i))
struct TEMP_OMAZINAI_ { TEMP_OMAZINAI_() { std::ios::sync_with_stdio(false); std::cin.tie(0); } } TEMP__omazinai_instance__;
#define INF ((1LL<<61) + (1<<29))
#define YesNo(x) {std::cout << ((x)?"Yes":"No") << std::endl;}
#define YESNO(x) {std::cout << ((x)?"YES":"NO") << std::endl;}
#define OddEven(x) {std::cout << ((x)%2?"Odd":"Even") << std::endl;}
#define ODDEVEN(x) {std::cout << ((x)%2?"ODD":"EVEN") << std::endl;}
typedef long long int ll;
ll my_gcd(ll a, ll b) { return b ? my_gcd(b, a%b) : a; };
void TEMP_INPUT_IMPL_() {};
template <class T, class... Args> void TEMP_INPUT_IMPL_(T& dest, Args&... args) { cin >> dest; TEMP_INPUT_IMPL_(args...); }
void TEMP_OUTPUT_IMPL_() {};
template <class T, class... Args> void TEMP_OUTPUT_IMPL_(T dest, Args... args) { cout << dest << (sizeof...(Args) ? " " : ""); TEMP_OUTPUT_IMPL_(args...); }
#define in(type, ...) type __VA_ARGS__; TEMP_INPUT_IMPL_(__VA_ARGS__)
#define in_v(type, name, cnt) vector<type> name(cnt); loop(i, cnt) cin >> name[i];
#define sort_v(v) std::sort(v.begin(), v.end())
#define rsort_v(v) std::sort(v.rbegin(), v.rend())
#define unique_v(v) v.erase(std::unique(v.begin(), v.end()), v.end()) //必ずソート後に実行
#define out(...) (TEMP_OUTPUT_IMPL_(__VA_ARGS__), std::cout<<std::endl)
#define set_fix(x) ((std::cerr<<std::fixed<<std::setprecision(x)),(std::cout<<std::fixed<<std::setprecision(x)))
//小さい方に寄る
#define space_op(x, y, a, b, c) ([](auto x_, auto y_, auto a_, auto b_, auto c_){if(x_<y_)return a_;if(x_>y_)return c_;return b_;}(x,y,a,b,c))
#define space_out(x, y, a, b, c) out(space_op(x, y, a, b, c))

 // ^^^ Template ^^^ //

struct UnionFind{
	vector<ll> parent;
	vector<ll> size;
	UnionFind() = delete;
	UnionFind(ll n) : parent(n), size(n, 1){
		loop(i, n)
			parent[i] = i;
	}

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

	void unite(ll x_, ll y_){
		ll x = find(x_);
		ll y = find(y_);
		if(x==y) return ;
		if(size[x] < size[y]) swap(x, y);
		parent[y] = x;
		size[x] += size[y];
	}

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

int main() {
	in(ll, N);
	vector<pair<ll, ll>> xv(N), yv(N);
	vector<pair<ll, pair<ll, ll>>> ev;
	ev.reserve(N*3);
	//set<ll> u;
	ll ans = 0;
	loop(i, N){
		in(ll, x, y);
		xv[i] = {x, i};
		yv[i] = {y, i};
	}
	sort_v(xv);
	sort_v(yv);
	loop(i, N-1){
		// x
		ev.push_back({
			xv[i+1].first - xv[i].first,
			make_pair(xv[i].second, xv[i+1].second)
		});
		ev.push_back({
			xv[i+1].first - xv[i].first,
			make_pair(xv[i+1].second, xv[i].second)
		});
		// y
		ev.push_back({
			yv[i+1].first - yv[i].first,
			make_pair(yv[i].second, yv[i+1].second)
		});
		ev.push_back({
			yv[i+1].first - yv[i].first,
			make_pair(yv[i+1].second, yv[i].second)
		});
	}
	sort_v(ev);
	UnionFind uf(N);
	for(auto e : ev){
		if(!uf.same(e.second.first, e.second.second)){
			ans += e.first;
			uf.unite(e.second.first, e.second.second);
		}
	}
	out(ans);
}

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

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

	int n;	cin >> n;

	int ans = 0;

	multiset <ll> m;

	for (int i = 0; i < n; i++) {
		ll take;	cin >> take;
		m.insert(take);
	}

	while (!m.empty()) {
		auto x = *m.rbegin();
		m.erase(m.find(x));
		ll stepval = pow(2, logb(x) + 1);
		auto it = m.find(stepval - x);
		if (it == m.end())
			continue;

		m.erase(it);
		ans++;
	}

	cout << ans << endl;

	return 0;
}


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

using namespace std;

int main(void){
  vector<int> hands;
  int tmp;

  while(cin >> tmp){
	hands.push_back(tmp);
	cin.ignore();
	cin >> tmp;
	hands.push_back(tmp);
	cin.ignore();
	cin >> tmp;
	hands.push_back(tmp);
	cin.ignore();
	cin >> tmp;
	hands.push_back(tmp);
	cin.ignore();
	cin >> tmp;
	hands.push_back(tmp);

	sort(hands.begin(), hands.end());
	if(hands[0] == hands[3] || hands[1] == hands[4]){ //four card
	  cout << "four card" << endl;
	}else if((hands[0] == hands[1] && hands[2] == hands[4]) ||
			 (hands[0] == hands[2] && hands[3] == hands[4])){ //full house
	  cout << "full house" << endl;
	}else if((hands[0]+1 == hands[1] && hands[1]+1 == hands[2] && hands[2]+1 == hands[3] && hands[3]+1 == hands[4]) ||
			 (hands[0] == 1 && hands[1] == 10 && hands[2] == 11 && hands[3] == 12 && hands[4] == 13)){
	  cout << "straight" << endl;
	}else if(hands[0] == hands[2] || hands[1] == hands[3] || hands[2] == hands[4]){//three cards
	  cout << "three card" << endl;
	}else if((hands[0] == hands[1] && (hands[2] == hands[3] || hands[3] == hands[4])) || (hands[1] == hands[2] && hands[3] == hands[4])){//two pairs
	  cout << "two pair" << endl;
	}else if(hands[0] == hands[1] || hands[1] == hands[2] || hands[2] == hands[3] || hands[3] == hands[4]){
	  cout << "one pair" << endl;
	}else{
	  cout << "null" << endl;
	}

	hands.clear();
  }

  return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int straight(int[]);

int main() {

	int ans, h[5];

	while(scanf("%d,%d,%d,%d,%d", &h[0], &h[1], &h[2], &h[3], &h[4])!=EOF) {
		ans=0;
		for(int i=0; i<4; i++) {
			for(int j=i+1; j<5; j++) {
				if(h[i]==h[j])
					ans++;
			}
		}
		switch(ans) {
			case 1:
				cout << "one pair" << endl;
				break;
			case 2:
				cout << "two pair" << endl;
				break;
			case 3:
				cout << "three card" << endl;
				break;
			case 4:
				cout << "full house" << endl;
				break;
			case 6:
				cout << "four card" << endl;
				break;
			default:
				if(straight(h))
					cout << "straight" << endl;
				else
					cout << "null" << endl;
		}
	}

	return 0;

}

int straight(int h[5]) {

	sort(h, h+5);
	for(int i=0; i<9; i++) {
		if(h[0]==i&&h[1]==i+1&&h[2]==i+2&&h[3]==i+3&&h[4]==i+4||h[0]==1&&h[1]==10&&h[2]==11&&h[3]==12&&h[4]==13)
			return 1;
	}

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << # x << "; "<< x << endl

int main() {
	long long X, Y;
	cin >> X;
	Y = X;
	long long happy = 0;
	X = X / 500;
	happy += 1000 * X;
	Y -= X * 500;
	Y = Y / 5;
	happy += Y * 5;
	cout << happy << endl;
}

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define bit(n) (1LL<<(n))

// #define DEBUG

#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif

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

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int inf = INT_MAX;
const ll INF = LLONG_MAX;
const ll MOD = 1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b, const ll mod=MOD) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b, const ll mod=MOD) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b, const ll mod=MOD) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b, const ll mod=MOD) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}

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

    int R; cin >> R;

    string res = "AGC";
    if(R < 1200){
        res = "ABC";
    }
    else if(R < 2800){
        res = "ARC";
    }

    cout << res << endl;

    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <ctime>
#include<list>
#include <numeric>
#include<bitset>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include <array>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

typedef long long int LL;
//typedef __int128 LLL;
typedef long long LLL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef long double ld;

#define MAXN 1000006
//#define MAXN 1006

const LL MOD = 1000000007;
//const LL MOD = 998244353;
const LL INF = 2000000000000000001LL; //2e18 + 1

void solve(int ks) {
	int h1, m1, h2, m2, k;
	scanf("%d %d %d %d %d", &h1, &m1, &h2, &m2, &k);
	int t1 = h1 * 60 + m1;
	int t2 = h2 * 60 + m2;
	if (t2 < t1) t2 += 60 * 24;
	int t = t2 - t1 - k;
	printf("%d\n", t);
}

void gen() {
}

int main()
{
	double start_time = clock();
#ifdef LOCAL
	freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
	// freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
	//	freopen("out.out", "w", stdout);
#endif

	gen();

	if (0) {
		int T;
		scanf("%d", &T);
		//AIN(T, 1, 5000);
		for (int ks = 1; ks <= T; ks++) {
			solve(ks);
			if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks);
		}
	}
	else {
		solve(0);
	}

	double end_time = clock();
	fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);

	return 0;
}

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

int main(){
  int h1,m1,h2,m2,k;
  cin>>h1>>m1>>h2>>m2>>k;
  int x=h1*60+m1;
  int y=h2*60+m2;
  cout<<y-x-k;
}


#include <iostream>
#include <math.h>

using namespace std;
void Solve(int a)
{
	const double pi = 3.14;
	float ans;
	ans = 2 * pi * a;
	cout << ans;
}
int main()
{
	int x;
	cin >> x;
	Solve(x);
	return 0;
}

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

int main() {
  long N, x, ans = 0;
  cin >> N;

  for (int i = N; i > 0; i--) {
    x = sqrt(i);
    x = x * x;
    if (i == x) {
      ans = i;
      break;
    }
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
ll MOD=1000000007;
int inf=1000000000;
ll INF=10000000000000000;
int main(){
    int n;
    ll x;
    cin >> n >> x;
    vector<ll> b(n),l(n),u(n);
    vector<pair<ll,ll>> a(n);
    ll uu=0;
    rep(i,n){
        cin >> b.at(i) >> l.at(i) >> u.at(i);
        a.at(i).first=u.at(i)*(x-b.at(i))+l.at(i)*b.at(i);
        a.at(i).second=i;
        uu+=l.at(i)*b.at(i);
    }
    sort(a.begin(),a.end());
    reverse(a.begin(),a.end());
    ll sum=0,v;
    rep(i,n){
        sum+=a.at(i).first;
        if (sum>uu) {sum-=a.at(i).first;v=i;break;}
        if (sum==uu) {cout << (i+1)*x << endl;return 0;}
    }
    ll ans=INF;
    rep(i,n){
        ll y=sum;
        int p=a.at(i).second;
        if (i<v) y=sum-a.at(i).first+a.at(v).first;
        ll t=uu-y+(u.at(p)-l.at(p))*b.at(p);
        ll o=t/u.at(p);
        if (t%u.at(p)!=0) o++;
        ans=min(ans,v*x+o);
        t=uu-y;
        o=t/l.at(p);
        if (t%l.at(p)!=0) o++;
        ans=min(ans,v*x+o);
    }
    cout << ans << endl;
}

#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <memory.h>
#include <cmath>
#include <array>

using namespace std;

void re(int& x);

template<class T, class... Ts> void re(T& t, Ts&... ts);

void pr(long long x);

void pr(const char *x);

void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);

#ifdef FFDBG

#else
#define dbg(x...) dsfdsfsdfasd
#endif

typedef long long ll;

#define rep(i, a, b) for(int i = a; i < (b); ++i)

int n, x;
long long tgt = 0;

struct ex {
    int l, r, b;
} exam[110000];

long long score(const ex& a, long long h) {
    long long sc = a.l * 1ll * min(h, (ll)a.b);
    sc += a.r * 1ll * max(0LL, h-a.b);
    return sc;
}

bool check(long long hrs) {
    int full = hrs / x;
    int rem = hrs % x;

    long long sc = 0;
    rep(i,0,full) sc += score(exam[i], x);
    long long best = sc;

    rep(i,full,n) best = max(best, sc + score(exam[i], rem));

    if (full < n) {
        sc += score(exam[full], x);
        rep(i,0,full) best = max(best, sc - score(exam[i], x) + score(exam[i],rem));
    }

    return best >= tgt;
}

void solve()
{
    re(n,x);
    rep(i,0,n) re(exam[i].b, exam[i].l, exam[i].r);

    rep(i,0,n) tgt += exam[i].l * 1ll * exam[i].b;

    sort(exam, exam+n, [](const ex& a, const ex& b) {
        return score(a,x) > score(b,x);
    });

    long long st = 0, ed = n*1ll*x;
    while (st < ed) {
        long long md = (st+ed)/2;
        if (check(md)) ed = md;
        else st = md+1;
    }

    ps(st);
}

int main() {

    solve();
}

void re(int& x) { scanf("%d", &x); }

template<class T, class... Ts> void re(T& t, Ts&... ts) {
    re(t); re(ts...);
}

void pr(long long x) { printf("%lld", x); }

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) {
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main ()
{
	ll a, z;
	cin>>a;
	z = a+a*a+a*a*a;
	cout<<z<<endl;
	return 0;
}

#include<iostream>
#include<algorithm>

using namespace std;

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

	int s_Len = S.length();

	if (s_Len == 3) {
		reverse(S.begin(), S.end());
	}

	cout << S << endl;

}


#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define EPS 1e-6
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;

int T;

int main()
{
    int a, b, c;
    while(cin >> a >> b >> c){
        cout << c << " " << a << " " << b << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
// #include <chrono>
// #include <math.h>
using namespace std;
using ll = long long;
using ld = long double;
using namespace std::chrono;
#define fo(i,z,n) for(int i = z;i<n;i++)
#define Fo(i,z,n) for(ll i = z;i<n;i++)
#define INT_BITS 32
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define pb push_back
#define f first
// #define s second
#define int long long
#define float long double
#define all(c) (c).begin(), (c).end()
#define endl '\n'
#define pi 3.14159265358979323846

ll power(ll a,ll p,ll m=mod)
{ll res=1;while(p>0){if(p&1)res=(res*a)%m;a=(a*a)%m;p>>=1;}
return res;}

int pow(int a,int b){
	if(b == 0)return 1;
	int res = pow(a,b/2);
	res = res*res;
	if(b&1)res *= a;
	return res;
}

int32_t main()
{

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

// int t;
// cin >> t;
// while(t--){

// }
	int a,b,c;
cin >> a >> b>> c;
swap(a,b);
swap(a,c);
cout << a << ' ' << b << ' ' << c << endl;

return 0;

	}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
//ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
int main() {
	int n, a, b;
	ll s = 0, sb = 0;
	cin >> n;
	vector<int> v(n);
	rep(i, 0, n)cin >> a >> b, v[i] = a + b, sb += (ll)b;
	sort(all(v));
	for (int i = v.size() - 1; i >= 0; i -= 2)s += (ll)v[i];
	printf("%lld\n", s - sb);
	return 0;
}

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

using namespace std;

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

  int n;
  cin >> n;

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

  sort(cur.begin(), cur.end());

  int ans = 0;
  for (int k = 30; k >= 1; k--) {
    int target = 1 << k;

    vector<int> nxtl;
    vector<int> nxtr;

    int lptr = 0, rptr = cur.size() - 1;
    while (lptr <= rptr) {
      if (lptr == rptr) {
        nxtl.push_back(cur[lptr]);
        break;
      } else if (cur[lptr] + cur[rptr] == target) {
        ans++;
        lptr++;
        rptr--;
      } else if (cur[lptr] + cur[rptr] < target) {
        nxtl.push_back(cur[lptr]);
        lptr++;
      } else if (cur[lptr] + cur[rptr] > target) {
        nxtr.push_back(cur[rptr]);
        rptr--;
      }
    }

    reverse(nxtr.begin(), nxtr.end());
    for (int u : nxtr) {
      nxtl.push_back(u);
    }

    cur = nxtl;
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define en "\n"
#define ITER(i,a) for( typeof(a.begin()) i=a.begin();i!=a.end();i++)
#define mod 1000000007
#define mpp make_pair
#define mes(a,b) memset((a),(b),sizeof((a))
typedef map<int,int> MI;
typedef pair<int,int>   PI;
typedef vector<int> VI;
typedef long long int LL;

int main() {
	// your code goes here
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    n=(2*n);
    int a[n];
    for(int i=0;i<n;i++)
    cin>>a[i];
    sort(a,a+n);
    int ans=0;
    for(int i=0;i<n;i+=2)
    {
        ans=ans+a[i];
    }
    cout<<ans;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=1e9;
const ll INFS=1e18;
const int MOD=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};

int main() {
  int n;
  cin>>n;
  map<int,int> mp;
  rep(i,n){
    int a;
    cin>>a;
    if(mp[a]!=1){
      mp[a]=1;
    }else{
      mp[a]=0;
    }
  }
  int ans=0;
  for(auto x:mp){
    if(x.second==1){
      ans++;
    }
  }
  cout<<ans<<endl;
}


#include<stdio.h>
#include<string.h>
int main(void) {
	int a[10000],n,b=0,i,x,d=3;
	char r[10000];
	char s[10000];
	scanf("%d", &n);
	sprintf(s, "%d", d);
	for(i = 1; i <= n; i++) {
		sprintf(r,"%d", i);
		if (i % 3 == 0) {
			a[b] = i;
			b++;
		}
		else if (strstr(r,s) != NULL) {
			a[b] = i;
			b++;
	}

	}
	for (x = 0; x < b; x++) {
		if (x == 0) {
			printf(" ");
	}
		printf("%d",a[x]);
		if (x == b - 1) {
			printf("\n");
		}
		else {
			printf(" ");
		}
	}
	return 0;
}

#include<iostream>
using namespace std;

int main(int argc,char* argv[]){
  int input,num,tmp;num=1;cin >> input;
  while(num<=input){
    if(num%3==0){cout<<" "<<num;num++;}
    if(num>input) break;tmp=num;
    while(tmp!=0){if(tmp%10==3){ cout<<" "<<num;break;}tmp/=10;}
    num++;}
  cout<<endl;}


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

double dist(int x1, int y1, int x2, int y2)
{
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

int main()
{
    int n; scanf("%d", &n);
    int x[8], y[8];
    double ans = 0.0;

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

    for (int i=0; i<n; i++)
    {
        for (int j=i+1; j<n; j++)
        {
            ans += dist(x[i], y[i], x[j], y[j]) * 2 / n;
        }
    }

    printf("%.10lf\n", ans);
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
void printVector(const vector<int>& vec) {
    for (int value : vec) {
        cout << value << " ";
    }
    cout << endl;
}

int main()
{
    int n;
    double x[10], y[10], ans = 0.0, count = 0.0;
    cin >> n;
    vector<int> num(n);
    rep(i, n) cin >> x[i] >> y[i];
    rep(i, n) num[i] = i;
    do
    {
        for (int i = 1; i < n; i++)
        {
            ans += sqrt(pow(x[num[i]] - x[num[i - 1]], 2) + pow(y[num[i]] - y[num[i - 1]], 2));
        }
        count++;
    } while (next_permutation(num.begin(), num.end()));
    cout << fixed << setprecision(10) <<ans / count << endl;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (i != n / i) res.push_back(n / i);
    }
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  auto divs = divisor(n);
  sort(divs.begin(), divs.end());
  ll left = -1;
  ll right = divs.size();
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    ll v1 = divs[mid];
    ll v2 = n / divs[mid];
    if (to_string(v1).size() >= to_string(v2).size())
      right = mid;
    else
      left = mid;
  }
  ll v1 = divs[right];
  ll v2 = n / divs[right];
  cout << max(to_string(v1).size(), to_string(v2).size()) << endl;
  return 0;
}

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

#define ll long long

ll F(ll A, ll B){
    string bufA = to_string(A), bufB = to_string(B);
    return max(bufA.size(), bufB.size());
}

int main(){
    ll N;
    ll ans = to_string(N).size();
    cin >> N;
    for(ll i = 1; i <= sqrt(N); ++i){
        if(N % i == 0) ans = min(ans, F(i, N/i));
    }
    cout << ans << endl;
    return 0;
}


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

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

int main()
{
    int64_t n;
    string s = "";

    cin >> n;

    while(n!=0)
    {
        n--;
        s += 'a' + n % 26;
        n /= 26;
    }

    reverse(s.begin(), s.end());

    cout << s << endl;

}

//https://atcoder.jp/contests/abc146/tasks/abc146_a
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	string x;
	cin>>x;
	if(x=="SUN")
	{
		cout<<"7"<<"\n";
	}
	else if(x=="MON")
	{
		cout<<"6"<<"\n";
	}
	else if(x=="TUE")
	{
		cout<<"5"<<"\n";
	}
	else if(x=="WED")
	{
		cout<<"4"<<"\n";
	}
	else if(x=="THU")
	{
		cout<<"3"<<"\n";
	}
	else if(x=="FRI")
	{
		cout<<"2"<<"\n";
	}
	else if(x=="SAT")
	{
		cout<<"1"<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
random_device rnd;
mt19937 mt(rnd());

const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;
int main()
{
    int n, m;
    cin >> n >> m;
    VVI v(n, VI(m));
    rep(i, n) rep(j, m)
    {
        cin >> v[i][j];
        v[i][j]--;
    }
    VI p(n, 0);
    int ans = IINF;
    map<int, bool> used;
    rep(j, m)
    {
        map<int, int> mp;
        rep(i, n)
        {
            mp[v[i][p[i]]]++;
        }
        int delid = -1;
        int delnum = 0;
        rep(i, m)
        {
            if (mp[i] > delnum)
            {
                delnum = mp[i];
                delid = i;
            }
        }
        chmin(ans, delnum);
        used[delid] = true;
        if (j == m - 1)
            break;
        rep(i, n)
        {
            while (used[v[i][p[i]]])
                p[i]++;
        }
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<class F>auto recursive(F f){return[f](auto...a){return f(f,a...);};}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int p;fixprec(int p):p(p){}};
ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.p);}

int main() {
    int N, M, lK;
    i64 K;
    cin >> N;
    vector<i64> x(N), dx(N - 1), y(N);
    for(i64 &x_ : x) cin >> x_;
    for(int i = 0; i < N - 1; i++) {
        dx[i] = x[i + 1] - x[i];
    }
    cin >> M >> K;
    vector<int> perm_base(N - 1), perm_ans(N - 1), perm_tmp(N - 1);
    for (int i = 0; i < N - 1; i++) {
        perm_base[i] = perm_ans[i] = i;
    }
    for(int i = 0; i < M; i++) {
        int a;
        cin >> a;
        a--;
        swap(perm_base[a - 1], perm_base[a]);
    }
    // 繰り返し二乗法
    for(int b = 0; (1ll << b) <= K; b++) {
        if (K & (1ll << b)) {
            for (int i = 0; i < N - 1; i++) {
                perm_ans[i] = perm_base[perm_ans[i]];
            }
        }
        for (int i = 0; i < N - 1; i++) perm_tmp[i] = i;
        for (int i = 0; i < N - 1; i++) {
            perm_tmp[i] = perm_base[perm_base[i]];
        }
        swap(perm_tmp, perm_base);
    }
    for(int i = 0; i < N; i++) {
        y[i] = i ? dx[perm_ans[i - 1]] + y[i - 1] : x[0];
        cout << y[i] << endl;
    }
    return 0;
}


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

using namespace std;

int A, B, C, D;

int main(int argc, char* argv[]) {
  cin >> A >> B >> C >> D;
  int L = A + B;
  int R = C + D;

  if (L > R)
    cout << "Left" << endl;
  else if (L == R)
    cout << "Balanced" << endl;
  else
    cout << "Right" << endl;

  return 0;
}

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int H, W, A, B;
  cin>>H>>W>>A>>B;
  int ans[H][W];
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      ans[i][j] = 0;
    }
  }
  map<int, int> gyo[H], retu[W];
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      if(gyo[i][1]<W-A && retu[j][1]<H-B) {
        ans[i][j] = 1;
        gyo[i][1]++;
        retu[j][1]++;
      }
    }
  }
  bool can = true;
  for(int i=0;i<H;++i) {
    if(gyo[i][1]!=A && gyo[i][1]!=W-A) can = false;
    if(retu[i][1]!=B && retu[i][1]!=H-B) can = false;
  }
    for(int i=0;i<H;++i) {
      for(int j=0;j<W;++j) {
        cout<<ans[i][j];
      }
      cout<<endl;
    }
}


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
using ld = long double;
const int MOD = 1e9+7;
const ll INF = 1LL << 60;

int main() {
    int a, b; cin >> a >> b;
    if(a == b || a - 1 == b || a == b - 1) cout << "Yay!" << endl;
    else cout << ":(" << endl;

    return 0;
}

#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array)     (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define int long long
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define _GLIBCXX_DEBUG
int INF = 1e9+7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1)
{
unsigned day;
if(dow < dow1) dow += 7;
day = dow - dow1;
day += 7 * n - 6;
return day;
}
unsigned DayToWeekNumber(unsigned day)
{
  return (day - 1) / 7 + 1;
}
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0)
{
  return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main(){
  int a,b;
  cin>>a>>b;
  cout<<(a<9&&b<9? "Yay!":":(")<<endl;
}


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

int main(){
    string s;
    cin >> s;
    vector<int> alp(26, 0);
    for(char d : s){
        alp[d - 'a'] = 1;
    }
    bool f = true;
    for(int i = 0; i < 26; i++){
        if(alp[i] == 0){
            f = false;
            cout << char(i + 'a') << endl;
            break;
        }
    }
    if(f) puts("None");
}

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

int main(void) {
    string s;
    cin >> s;
    map<char, int> m;
    for (int i = 0; i < (int)s.size(); i++) {
        m[s.at(i)]++;
    }
    for (char i = 'a'; i <= 'z'; i++) {
        if (m[i] == 0) {
            cout << i << endl;
            return 0;
        }
    }
    cout << "None" << endl;
    return 0;
}


#include <bits//stdc++.h>
using namespace std;
#define int long long
signed main() {
    int A,N,Max=0;
    cin >> N;
    long long goukei=0;
    for(int i=0;i<N;i++){
        cin >> A;
        goukei+=max(Max-A,(int)0);
        Max=max(A,Max);
    }
    cout << goukei << endl;

}

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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(1e5 + 5);

int n;

ll a[MAX_N];
ll b[MAX_N];

int main(void) {
  // Here your code !
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    scanf("%lld %lld", &(a[i]), &(b[i]));
  }

  vector<pair<ll, int>> sum_index;

  for (int i = 0; i < n; ++i) {
    sum_index.emplace_back(a[i] + b[i], i);
  }

  sort(sum_index.begin(), sum_index.end(), greater<pair<ll, int>>());

  ll ans = 0LL;

  for (int i = 0; i < n; ++i) {
    if (i % 2 == 0) {
      ans += a[sum_index[i].second];
    } else {
      ans -= b[sum_index[i].second];
    }
  }

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

  return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX_P = 10020;
bool prime[MAX_P];

void init()
{
    for (int i = 0; i < MAX_P; i++)
        prime[i] = true;
    prime[0] = prime[1] = false;

    for (int i = 2; i * i < MAX_P; i++)
        if (prime[i])
            for (int j = 2 * i; j < MAX_P; j += i)
                prime[j] = false;
}

int main()
{
    init();

    int n;
    while (cin >> n, n) {
        while (n >= 2) {
            if (prime[n] && prime[n - 2]) {
                printf("%d %d\n", n - 2, n);
                break;
            }
            n--;
        }
    }

    return 0;
}

#include<iostream>
#include<string>
#include<cstring>

bool find(const std::string &text, const std::string pattern){
	int text_i=0,pattern_i=0;
	while(text_i<text.size()&&pattern_i<pattern.size()){
		if(text[text_i]==pattern[pattern_i]) {
			text_i++;
			pattern_i++;
		}else{
			pattern_i=0;
			text_i=text_i-pattern_i+1;
		}
	}
	return (pattern_i==pattern.size()?true:false);
}

void decode( std::string &coded_data,int diff){
	for(int i=0;i<coded_data.size();i++){
		if(isalpha(coded_data[i])){
			coded_data[i]-=diff;
			if(!isalpha(coded_data[i])) coded_data[i]+=26;
		}
	}
}

int main(void){
	std::string coded_data;
	while(std::getline(std::cin,coded_data)){
		for(int i=0;i<26;i++){
			decode(coded_data,1);
			if(find(coded_data,"the") || find(coded_data,"this")||find(coded_data,"that"))
				std::cout<<coded_data<<std::endl;
		}
	}

	return 0;
}


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

int main(){
  int N;
  cin>>N;
  vector<int> p(N);
  for(int i=0;i<N;i++){
    cin>>p[i];
  }
  int L;
  cin>>L;
  vector<vector<int>> dp(N,vector<int>(17));
  for(int j=0;j<17;j++){
    for(int i=0;i<N;i++){
      if(j==0){
        if(p[i]+L>=p[N-1]){
          dp[i][0]=N-1;
        }
        else{
          int b=lower_bound(p.begin(),p.end(),p[i]+L+1)-p.begin();
          dp[i][0]=b-1;
        }
      }
      else{
        dp[i][j]=dp[dp[i][j-1]][j-1];
      }
    }
  }
  int Q;
  cin>>Q;
  for(int l=0;l<Q;l++){
    int a,b;
    cin>>a>>b;
    a--,b--;
    if(a>b){
      swap(a,b);
    }
    int Z=1;
    for(int i=16;i>=0;i--){
      if(dp[a][i]<b){
        Z+=(1<<i);
        a=dp[a][i];
      }
    }
    cout<<Z<<endl;
  }
}

#include<bits/stdc++.h>
using namespace std;
const long long N=2e5+5;
const long long inf=0x3f3f3f3f;
struct pl
{
	long long x;long long y;long long jyz;
}p1[N];
vector <pl> xxx[N],yyy[N],zzz[N*2],fff[N*2];
long long cntx,cnty,cntz,cntf,n,ans=inf;
string s;
bool cjy(pl a,pl b)
{
	if (a.x>b.x) return 0;
	if (a.x<b.x) return 1;
	return a.y<b.y;
}
signed main()
{
	;;;;;
	cin>>n;
	for (long long i=1;i<=n;i++)
	{
		scanf("%lld%lld",&p1[i].x,&p1[i].y);
		cin>>s;
		if (s=="U") p1[i].jyz=1;
		if (s=="R") p1[i].jyz=2;
		if (s=="D") p1[i].jyz=3;
		if (s=="L") p1[i].jyz=4;
	}
	sort(p1+1,p1+n+1,cjy);
	for (long long i=1;i<=n;i++)
	{
		xxx[p1[i].x].push_back(p1[i]);
		yyy[p1[i].y].push_back(p1[i]);
		fff[p1[i].x+p1[i].y].push_back(p1[i]);
		zzz[p1[i].x-p1[i].y+N-5].push_back(p1[i]);
	}
	for (long long i=0;i<N;i++) if (xxx[i].size()>=2)
	{
		long long tans=inf,ty=-1;
		for (long long j=0;j<xxx[i].size();j++)
		{
			if (xxx[i][j].jyz==1) ty=xxx[i][j].y;
			if (xxx[i][j].jyz==3&&ty!=-1) tans=min(tans,xxx[i][j].y-ty);
		}
		ans=min(ans,5*tans);
	}
	for (long long i=0;i<N;i++) if (yyy[i].size()>=2)
	{
		long long tans=inf,tx=-1;
		for (long long j=0;j<yyy[i].size();j++)
		{
			if (yyy[i][j].jyz==2) tx=yyy[i][j].x;
			if (yyy[i][j].jyz==4&&tx!=-1) tans=min(tans,yyy[i][j].x-tx);
		}
		ans=min(ans,5*tans);
	}
	for (long long i=0;i<2*N;i++) if (fff[i].size()>=2)
	{
		long long tans=inf,td=-1,tr=-1;
		for (long long j=0;j<fff[i].size();j++)
		{
			if (fff[i][j].jyz==2) tr=fff[i][j].x;
			if (fff[i][j].jyz==3) td=fff[i][j].x;
			if (fff[i][j].jyz==1&&tr!=-1) tans=min(tans,fff[i][j].x-tr);
			if (fff[i][j].jyz==4&&td!=-1) tans=min(tans,fff[i][j].x-td);
		}
		ans=min(ans,10*tans);
	}
	for (long long i=0;i<2*N;i++) if (zzz[i].size()>=2)
	{
		long long tans=inf,tu=-1,tr=-1;
		for (long long j=0;j<zzz[i].size();j++)
		{
			if (zzz[i][j].jyz==2) tr=zzz[i][j].x;
			if (zzz[i][j].jyz==1) tu=zzz[i][j].x;
			if (zzz[i][j].jyz==3&&tr!=-1) tans=min(tans,zzz[i][j].x-tr);
			if (zzz[i][j].jyz==4&&tu!=-1) tans=min(tans,zzz[i][j].x-tu);
		}
		ans=min(ans,10*tans);
	}
	if (ans==inf) cout<<"SAFE"<<endl;
	else cout<<ans<<endl;
}


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

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define FORR(i, a, b) for (long long i = (a); i >= (b); i--)
#define REPR(i, N) for (long long i = n; i >= 0; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ALL(s) (s).begin(), (s).end()
#define pb(a) push_back(a)

#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

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

int main()
{
    ll N;
    string s;
    cin >> N;
    cin >> s;
    string ans;
    string init_pattern[4] = {"SS", "SW", "WS", "WW"};
    for(string p: init_pattern){
        ans = "";
        ans += p;
        FOR(i,2,N+2){
            if(s[(i-1)%N] == 'o'){
                if(ans[i-1] == 'S'){
                    ans += ans[i-2];
                }else{
                    ans += (ans[i-2] == 'S') ? 'W' : 'S';
                }
            }else {
                if(ans[i-1] == 'S'){
                    ans += (ans[i-2] == 'S') ? 'W' : 'S';
                }else{
                    ans += ans[i-2];
                }
            }
        }
        if (ans[0] == ans[N] && ans[1] == ans[N+1]){
            break;
        }
        ans = "-1";
    }
    cout << ans.substr(0,N) << endl;
}

#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;
using usize = ::std::size_t;
using u64 = ::std::int_least64_t;
//using u64 = long long;
static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

int getrandmax() {
    static uint32_t y = time(NULL);
    y ^= (y << 13); y ^= (y >> 17);
    y ^= (y << 5);
    return abs((int)y);
}
int getrand(int l, int r) { // [l, r]
    return getrandmax() % (r - l + 1) + l;
}

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

    cin.tie(0) ;
    ios::sync_with_stdio(false) ;

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

    int i = 0;
    bool found = false;
    while (i < n && clock() < 1.95 * CLOCKS_PER_SEC) {
        if (A[i] == B[i]) {
            int j = getrand(i, n - 1);
            if (B[j] == B[i]) {
                j = getrand(0, i);
                if(B[j] != A[i] && B[i] != A[j]){
                    swap(B[i], B[j]);
                }
            } else {
                swap(B[i], B[j]);
            }
            i--;
        }
        i++;
        found = (i == n);
    }

    if (found) {
        cout << "Yes" << endl;
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                cout << B[i];
            } else {
                cout << " " << B[i];
            }
            if(i == n - 1){
                cout << endl;
            }
        }
    } else {
        cout << "No" << endl;
    }

    return 0;

}


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

vector<long long> create_table(vector<long long> pattern) {
    vector<long long> table(pattern.size()+1, 0);

    long long j = 0;
    for (long long i = 1; i < pattern.size(); i++) {
        if (pattern[i] == pattern[j])
            table[i] = j++;
        else {
            table[i] = j;
            j = 0;
        }
    }
    table[pattern.size()] = j;

    return table;
}

vector<long long> kmp_search(vector<long long> target, vector<long long> pattern) {
    vector<long long> table = create_table(pattern);
    /*
    for (int i = 0; i < table.size(); i++)
        cout << table[i] << " ";
    cout << endl;
    */

    vector<long long> result;
    long long p = 0;
    for (long long i = 0; i < target.size(); i++) {
        if (target[i] == pattern[p])
            p++;
        else if (p != 0 && target[i] != pattern[p]) {
            p = table[p];
            i--;
        }
        //cout << i << " " << p << endl;

        if (p == pattern.size()) {
            result.push_back((i+1)-p);
            p = table[p];
        }
    }

    return result;
}

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

    vector<ll> copy_a(a);
    copy_a.insert(copy_a.end(), a.begin(), a.end()-1);
    vector<ll> new_a(2*n-2), new_b(n-1);
    rep(i, 2*n-2) new_a[i] = copy_a[i] ^ copy_a[i+1];
    rep(i, n-1) new_b[i] = b[i] ^ b[i+1];

    vector<ll> kmp = kmp_search(new_a, new_b);
    rep(i, kmp.size())
        cout << kmp[i] << " " << (a[kmp[i]] ^ b[0]) << endl;
}

///
// File:  alds1_8_c.cpp
// Author: ymiyamoto
//
// Created on Sun Oct 15 01:12:42 2017
//
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct tree_t {
  tree_t *left;
  tree_t *right;
  tree_t *parent;
  int64_t value;
  tree_t(tree_t *par, int64_t val)
  {
    left = NULL;
    right = NULL;
    parent = par;
    value = val;
  }
};

static tree_t *tree;
static vector<int64_t> pre, in;

void insert(tree_t *top, int64_t val)
{
  if (top == NULL) {
    tree = new tree_t(NULL, val);
    return;
  }

  if (val < top->value) {
    if (top->left == NULL) {
      tree_t *node = new tree_t(top, val);
      top->left = node;
    } else {
      insert(top->left, val);
    }
  } else if (val > top->value) {
    if (top->right == NULL) {
      tree_t *node = new tree_t(top, val);
      top->right = node;
    } else {
      insert(top->right, val);
    }
  }
}

void walk(tree_t *top)
{
  if (top == NULL) return;

  pre.push_back(top->value);
  walk(top->left);
  in.push_back(top->value);
  walk(top->right);
}

void print()
{
  pre.clear();
  in.clear();
  walk(tree);
  for (uint32_t i = 0; i < in.size(); i++) {
    cout << " " << in[i];
  }
  cout << endl;

  for (uint32_t i = 0; i < pre.size(); i++) {
    cout << " " << pre[i];
  }
  cout << endl;
}

bool find(tree_t *top, int64_t val)
{
  if (top == NULL)
    return false;
  else {
    if (val == top->value) {
      return true;
    } else if (val < top->value) {
      return find(top->left, val);
    } else {
      return find(top->right, val);
    }
  }
}

int64_t find_min(tree_t *top)
{
  if (top->left == NULL)
    return top->value;
  else
    return find_min(top->left);
}

void del(tree_t *top, int64_t val)
{
  if (val == top->value) {
    if (top->left == NULL && top->right == NULL) {
      tree_t *parent = top->parent;
      if (parent->left == top) {
        parent->left = NULL;
      } else {
        parent->right = NULL;
      }
      delete top;
    } else if (top->left == NULL) {
      tree_t *parent = top->parent;
      if (parent->left == top) {
        parent->left = top->right;
      } else {
        parent->right = top->right;
      }
      top->right->parent = parent;
      delete top;
    } else if (top->right == NULL) {
      tree_t *parent = top->parent;
      if (parent->left == top) {
        parent->left = top->left;
      } else {
        parent->right = top->left;
      }
      top->left->parent = parent;
      delete top;
    } else {
      top->value = find_min(top->right);
      del(top->right, top->value);
    }
  } else if (val < top->value) {
    del(top->left, val);
  } else {
    del(top->right, val);
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str;
    int64_t val;
    cin >> str;
    if (str[0] == 'i') {
      cin >> val;
      insert(tree, val);
    } else if (str[0] == 'f') {
      cin >> val;
      if (find(tree, val)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else if (str[0] == 'd') {
      cin >> val;
      del(tree, val);
    } else {
      print();
    }
  }

  return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; ++i) // マクロ
using namespace std; // stdの省略
using pii = pair<int, int>; // pairの略記
using ll = long long; // long longの略記

int main(void) {

   int r;
   cin >> r;

   cout << 2*r*M_PI << endl;

   return 0;
}

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

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

  cout << A * B << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,count=0;
    cin>>a>>b;
    for(int i=a;i<=b;i++)
    {
        string s=to_string(i);
        int n=s.length();
        if(s[0]==s[n-1] && s[1]==s[n-2])
        {
            count++;
        }

    }
    cout<<count<<"\n";
    return 0;
}

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

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


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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

vector<ll> seen, deg, par;
vector<vector<ll>> G;

void dfs(ll v, ll pav)
{
	seen[v] = true;
	par[v] = pav+1;

	for (auto &nv : G[v])
		--deg[nv];

	for (auto &nv : G[v])
	{
		if (!deg[nv] && !seen[nv])
			dfs(nv, v);
	}
}

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll N, M; cin >> N >> M;
	G.resize(N);
	par.resize(N);
	deg.assign(N, 0);
	seen.assign(N, false);
	REP(i, N-1+M)
	{
		ll a, b; cin >> a >> b;
		--a, --b;
		G[a].push_back(b);
		deg[b]++;
	}

	ll root;
	REP(i, N)
		if (!deg[i])
			root = i;

	dfs(root, -1);

	REP(i, N)
		std::cout << par[i] << std::endl;
	return 0;
}

#include <stdio.h>
#include <math.h>
#define MAXINPUT 100

int main(void){
	int h[MAXINPUT],w[MAXINPUT];
	int ansh[MAXINPUT]={},answ[MAXINPUT]={};
	int i=0,j=0;
	int ans=0;
	int num=0;
	int k;
	while(i<MAXINPUT){
		scanf("%d %d",&h[i],&w[i]);
		if(h[i]==0 && w[i]==0)break;
		i++;
		num++;
	}

	for(i=0;i<num;i++,ans=0){
		ans=h[i]*h[i]+w[i]*w[i];
		for(;(ansh[i]==0||answ[i]==0);){
			for(j=1;k==0;j++){
				double temp=ans-(j*j);
				double tempa=0;
				if(temp<=0){
					k=1;
					break;
				}
				temp=sqrt(temp);
				temp=modf(temp,&tempa);
				if(temp==0 && tempa<=150 && j<tempa){
					if(tempa*tempa+j*j==h[i]*h[i]+w[i]*w[i] && j<=h[i]){
						continue;
					}
					ansh[i]=j;
					answ[i]=(int)tempa;
					printf("%d %d\n",ansh[i],answ[i]);
					break;
				}
			}
		ans++;
		k=0;
		}
	}

	return 0;
}


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

int main(){
  int N;
  cin >> N;
  double K=0;
  vector<int> X(N),Y(N);
  for(int i=0;i<N;i++){
    cin >> X.at(i) >> Y.at(i);
  }
  for(int i=0;i<N-1;i++){
    for(int j=i+1;j<N;j++){
      int X2=X.at(i)-X.at(j);
      int Y2=Y.at(i)-Y.at(j);
      K+=sqrt(X2*X2+Y2*Y2);
    }
  }
  cout << fixed << setprecision(10) << 2*K/N << endl;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

int main()
{
    int n;
    cin >> n;
    vector<int> x(n), y(n), per(n);
    rep(i, n) cin >> x[i] >> y[i];

    rep(i, n) per[i] = i;
    double ans = 0;
    int cnt = 0;
    do
    {
        cnt++;
        rep(i, n - 1)
        {
            ans += sqrt(pow(x[per[i + 1]] - x[per[i]], 2.0) + pow(y[per[i + 1]] - y[per[i]], 2.0));
        }

    } while (next_permutation(all(per)));

    printf("%6lf\n", ans / cnt);
    return 0;
}


# 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 _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++)
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

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

int n, m, k;
int a[N];
string s;

void sol (int x, int y) {
	for (int i = 1; i <= n; i ++) {
		a[i] = -1;
	}

	a[1] = x;
	a[2] = y;
	a[n+1] = x;
	for (int i = 2; i < n; i++) {
		bool nxt = 0;
		if (a[i] == 1) nxt ^= 1;
		if (s[i] == 'x') nxt ^= 1;
		a[i+1] = (a[i-1] ^ nxt);
	}

	for (int i = 1; i <= n; i ++) {
		int l = i-1;
		int r = i+1;
		if (r > n) r = 1;
		if (l < 1) l = n;

		bool nxt = 0;
		if (a[i] == 1) nxt ^= 1;
		if (s[i] == 'x') nxt ^= 1;

		if(!nxt) {
			if (a[l] != a[r]) return;
		} else {
			if (a[l] == a[r]) return;
		}

	}

	for (int i = 1; i <= n; i ++)
		cout << (a[i] ? "W" : "S");
	exit(0);
}

int main () {
	SpeedForce;
	cin >> n >> s;
	s = '#' + s;

	for (int a = 0; a < 2; a++) {
		for (int b = 0; b < 2; b++) {
			sol(a, b);
		}
	}

	puts("-1");

   	return Accepted;
}

// B...a

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int ed(string x,string y){
	vector<vector<int>> tb(vector<vector<int>>((int)x.size()+1,vector<int>(y.size()+1,0)));
	for(int i=0;i<(int)x.size()+1;i++) tb[i][0]=i;
	for(int i=0;i<(int)y.size()+1;i++) tb[0][i]=i;
	int tmp;
	for(int i=1;i<(int)x.size()+1;i++){
		for(int j=1;j<(int)y.size()+1;j++){
			if(x[i-1]==y[j-1])  tmp=tb[i-1][j-1];
			else tmp=tb[i-1][j-1]+1;
			tb[i][j]=min(tmp,min(tb[i-1][j],tb[i][j-1])+1);
		}
	}
	return tb[(int)x.size()][(int)y.size()];
}
int main(){
	string x,y;
	cin>>x>>y;
	cout<<ed(x,y)<<endl;
	return 0;
}


#include<cstdio>
const int N=1000005;
int n,he[128],nx[N],a,qu[N*2],hd,tl,d;char s[N],t[N];
inline int mx(int x,int y){return x>y?x:y;}
int main()
{
	scanf("%d%s%s",&n,s+1,t+1),hd=n+1,tl=n;
	for(int i=1;i<=n;++i)nx[i]=he[s[i]],he[s[i]]=i;
	for(int i=n;i;--i)
	{
		if(hd>tl||i<qu[hd]-d)
		{
			hd=n+1,tl=n,d=0;
			if(s[i]!=t[i])
			{
				for(;he[t[i]]&&he[t[i]]>=i;he[t[i]]=nx[he[t[i]]]);
				if(!he[t[i]])return printf("-1"),0;
				qu[--hd]=he[t[i]],qu[++tl]=i;
			}
		}
		else
		{
			if(s[qu[hd]-d]!=t[i])
			{
				for(;he[t[i]]&&he[t[i]]>=qu[hd]-d;he[t[i]]=nx[he[t[i]]]);
				if(!he[t[i]])return printf("-1"),0;
				for(;qu[tl]-d>i;--tl);++d,qu[--hd]=he[t[i]]+d,qu[++tl]=i+d;
			}
		}
		a=mx(a,tl-hd);
	}
	printf("%d",a);
	return 0;
}

#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;
}
const int maxn=(1e6)+10;
int n,m,sum,s,a[maxn],b[maxn];
char S[maxn];
int N,id[maxn],d[maxn],tot;
bool cmpa(int x,int y) { return a[x]<a[y]; }
bool cmpb(int x,int y) { return b[x]>b[y]; }
int main() {
	//freopen("1.txt","r",stdin);
	read(n);
	for (int i=1;i<=n;i++) {
		scanf("%s",S+1);
		m=strlen(S+1);
		s=0;
		for (int j=1;j<=m;j++) {
			if (S[j]=='(') s++; else s--;
			a[i]=max(a[i],-s);
		}
		sum+=s;
		s=0;
		for (int j=m;j>=1;j--) {
			if (S[j]=='(') s++; else s--;
			b[i]=max(b[i],s);
		}
		//printf("%d %d\n",a[i],b[i]);
	}
	if (sum!=0) { printf("No\n"); return 0; }

	sum=0;
	for (int i=1;i<=n;i++) if (b[i]>=a[i]) d[++tot]=i;
	sort(d+1,d+tot+1,cmpa);
	for (int i=1;i<=tot;i++) id[++N]=d[i];
	tot=0;
	for (int i=1;i<=n;i++) if (b[i]<a[i]) d[++tot]=i;
	sort(d+1,d+tot+1,cmpb);
	for (int i=1;i<=tot;i++) id[++N]=d[i];
	for (int i=1;i<=N;i++) {
		if (sum-a[id[i]]<0) { printf("No\n"); return 0; }
		sum+=b[id[i]]-a[id[i]];
	}
	printf("Yes\n");
	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?
*/


//高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INTINF 1999999999
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007

int main() {
	int ans = 0;
	int tmp;
	int n;
	cin >> n;
	string s;
	cin >> s;
	s = "-" + s;
	bool sw[n+2] = {}, las, fir, ch = false;
	REP(i, 2){
		if(ch) break;
		REP(j, 2){
			if(ch) break;
			sw[1] = i;
			sw[n] = j;
			sw[0] = j;
			las = j;
			fir = i;
			for(int k = 1; k <= n; k++){
				if(s[k] == 'o') sw[k+1] = sw[k-1];
				else sw[k+1] = !sw[k-1];
				if(!sw[k]) sw[k+1] = !sw[k+1];
				//cout << k << " " << sw[k-1] << " " << sw[k] << " " << sw[k+1] << endl;
			}
			if(fir == sw[n+1] && las == sw[n]) ch = true;
		}
	}
	if(ch){
		REP1(i, n){
			if(sw[i]) cout << "S";
			else cout << "W";
		}
		cout << endl;
	}
	else cout << -1 << endl;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
//#pragma GCC optimize("unroll-loops")

#ifdef __APPLE__
    # include <iostream>
    # include <cmath>
    # include <algorithm>
    # include <stdio.h>
    # include <cstdint>
    # include <cstring>
    # include <string>
    # include <cstdlib>
    # include <vector>
    # include <bitset>
    # include <map>
    # include <queue>
    # include <ctime>
    # include <stack>
    # include <set>
    # include <list>
    # include <random>
    # include <deque>
    # include <functional>
    # include <iomanip>
    # include <sstream>
    # include <fstream>
    # include <complex>
    # include <numeric>
    # include <immintrin.h>
    # include <cassert>
    # include <array>
    # include <tuple>
    # include <unordered_map>
    # include <unordered_set>
    # include <thread>
#else
    # include <bits/stdc++.h>
#endif

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) name!=""?freopen(name".in","r",stdin),freopen(name".out","w",stdout):0
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;
#define int long long

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

const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ld eps=1e-10;
const ll md=1e9+7;
const ll inf=1e18+10;

///---program start---///

#define arr (int)(1e6+10)

bool use[arr];
vi reb_one[arr];
vector<pii> reb[arr];
vi rem_comps[arr];
int comps=0;

int dist_bfs[arr];
vi reb_bfs[arr];

void add_bfs_edge(int u,int v)
{
//    cout<<"bfs_edge :: "<<u<<" "<<v<<"\n";
    reb_bfs[u].pb(v);
    reb_bfs[v].pb(u);
}

void dfs(int now)
{
    use[now]=1;
    rem_comps[now].pb(comps);
    for (auto wh:reb_one[now]){
        if (!use[wh]){
            dfs(wh);
        }
    }
}

void process(int v)
{
    dfs(v);
    comps++;
}

signed main()
{
    #ifdef __APPLE__
        files("barik");
        freopen("debug.txt","w",stderr);
    #endif

    int n,m;
    cin>>n>>m;
    for (int i=0;i<m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        reb[w].pb(mp(u,v));
    }
    for (int i=0;i<arr;i++){
        for (auto j:reb[i]){
            reb_one[j.fir].pb(j.sec);
            reb_one[j.sec].pb(j.fir);
        }

        for (auto j:reb[i]){
            if (!use[j.fir]) {
                process(j.fir);
            }
            if (!use[j.sec]) {
                process(j.sec);
            }
        }

        for (auto j:reb[i]){
            reb_one[j.fir].clear();
            reb_one[j.sec].clear();
            use[j.fir]=0;
            use[j.sec]=0;
        }
    }

//    for (int i=1;i<=n;i++){
//        cout<<"rem_comps["<<i<<"] :: ";
//        for (auto j:rem_comps[i]){
//            cout<<j<<" ";
//        }
//        cout<<"\n";
//    }

    for (int i=1;i<=n;i++){
        int fake = comps++;
        for (auto j:rem_comps[i]){
            add_bfs_edge(fake,j);
        }
    }

    queue<int> q;
    for (int i=0;i<comps;i++){
        dist_bfs[i]=inf;
    }
    for (auto j:rem_comps[1]){
        dist_bfs[j]=0;
        q.push(j);
    }
    while (!q.empty()){
        int now=q.front();
        q.pop();
        for (auto wh:reb_bfs[now]){
            if (dist_bfs[wh]>dist_bfs[now]+1){
                dist_bfs[wh]=dist_bfs[now]+1;
                q.push(wh);
            }
        }
    }
    int ans=inf;
    for (auto j:rem_comps[n]){
        ans=min(ans,dist_bfs[j]);
    }
    cout<<(ans==inf?-1:ans/2 + 1)<<"\n";
}

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD ((ll)1e9+7)
ll N,S;
vector<pll> xpv[2];
ll i, j, k,ans;
bool ck(ll t,ll aa,ll bb){
    return t ? (aa <= bb) : (aa < bb);
}
int main(){
    //printf("%lld\n", MOD);
    scanf("%lld%lld", &N,&S);
    fornum(i,0,N){
        ll x, p;
        scanf("%lld%lld", &x, &p);
        xpv[S < x].push_back(mp(abs(x - S), p));
    }
    sort(xpv[0].begin(), xpv[0].end(), greater<pll>());
    sort(xpv[1].begin(), xpv[1].end(), greater<pll>());
    if(xpv[0].size()*xpv[1].size()==0){
        printf("%lld",xpv[0].size()?xpv[0][0].first:xpv[1][0].first);
        return 0;
    }

    ll n = 0, t = 0;
    ll ii[]={0,0};
    n= xpv[0][0].second+xpv[1][0].second;
    if(xpv[0][0].second<xpv[1][0].second){
        t = 0;
    }else{
        t = 1;
    }
    ii[t]++;
    ans += xpv[t][0].first + xpv[!t][0].first * 2;
    //printf("%lld,%lld\n", xpv[t][0].second, xpv[!t][0].second);
    while(true){
        while(ii[t]<xpv[t].size()&&ck(t,xpv[t][ii[t]].second,n)){
            //printf("%lld,%lld\n", xpv[t][ii[t]].second, n);
            n += xpv[t][ii[t]].second;
            ii[t]++;
        }
        //printf("%lld,%lld,%lld\n", t, ii[t],n);
        if(ii[t]==xpv[t].size())
            break;
        n += xpv[t][ii[t]].second;
        ans += xpv[t][ii[t]].first*2;
        t = !t;
        ii[t]++;
    }
    printf("%lld", ans);

    return 0;
}


#include<iostream>
#include<stdio.h>
//#include <bits/stdc++.h>
#include<vector>
#include<float.h>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include <cassert>
#include <cmath>
#include<cstdint>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define vec(type,n) vector<type>(n)
#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))
#define ALL(a)  (a).begin(),(a).end()

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

struct edge{
    int to;
    int dist;
};

vector<bool> seen(100000, false);
vector<int> pnt(100000, INF);
vector<vector<edge>> G(100000);
int n, m;
bool f = true;

void dfs(int v, int dist){
    seen[v] = true;
    if(pnt[v] == INF)pnt[v] = dist;
    else {
        if(pnt[v] != dist)f = false;
        return;
    }

    for(int i = 0; i < (int)(G[v].size()); i++){
        edge e = G[v][i];
        if(pnt[e.to] != INF && pnt[e.to] != pnt[v] + e.dist){
            f = false;
            return;
        }
        if(seen[e.to])continue;
        dfs(e.to, e.dist + pnt[v]);
    }
}

int main(){
    ll n, m;
    cin >> n >> m;
    vector<ll> rui(n + 1, 0);
    rep(i, n){
        ll a;
        cin >> a;
        rui[i + 1] = rui[i] + a;
    }
    ll res = 0;
    REP(i, 1, n){
        rui[i] %= m;
        if(rui[i] == 0)res++;
    }
    map<ll, ll> mp;
    REP(i, 1, n)mp[rui[i]]++;

    for(auto m : mp){
        res += m.second * (m.second - 1) / 2;
    }
    cout << res << endl;
}

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

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

  int n, k;
  cin >> n >> k;

  vector<int> v(n);
  rep(i, 0, n) cin >> v[i];

  ll ans = 0;
  rep(l, 0, k + 1) {
    rep(r, 0, k - l + 1) {
      if (l + r > n)
        continue;
      int d = k - l - r;
      ll now = 0;
      vector<int> s;
      rep(i, 0, l) {
        now += v[i];
        s.push_back(v[i]);
      }
      rep(i, n - r, n) {
        now += v[i];
        s.push_back(v[i]);
      }

      sort(s.begin(), s.end());
      rep(i, 0, d) {
        if (i >= s.size() || s[i] > 0)
          break;
        now -= s[i];
      }

      ans = max(ans, now);
    }
  }

  cout << ans << endl;
}


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

#define vec              vector<int>
#define vecll            vector<ll>
#define vecd             vector<double>
#define vecst            vector<string>
#define vecb             vector<bool>
#define v2(v,n,m,init)   vector<vector<int>>  v(n, vector<int>(m, init))
#define vb2(v,n,m,init)  vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v,n,m,init) vector<vector<ll>>   v(n, vector<ll>(m, init))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2)     cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
template <typename T>
bool chmax(T &a, const T& b){if(a<b){a=b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T& b){if(a>b){a=b; return true;} return false;}
//------------------------------------------------------------------

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

	if(n==0){
		prt(0);
		return 0;
	}
	string ans;
	ll b=1, bd=2;
	while(n!=0){
		if(n%bd!=0) ans.push_back('1'), n-=b;
		else ans.push_back('0');
		b*=-2;
		bd*=2;
	}
	reverse(all(ans));
	prt(ans);
}

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

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

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    string s;
    cin >> s;
    if(s.size() == 3) reverse(s.begin(), s.end());
    cout << s << endl;
    return 0;
}


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

typedef long long ll;
typedef vector<int> vi;

const double PI=acos(-1.0);

int N,Q; string S; vi l,r;
void input()
{
    cin>>N>>Q;
    cin>>S;
    for(int i=0;i<Q;++i){
        int a,b; cin>>a>>b;
        l.emplace_back(a); r.emplace_back(b);
    }
}

void solve()
{
    vi rui(N+1,0);
    for(int i=2;i<N+1;++i){
        rui[i]=rui[i-1];
        if(S[i-2]=='A' && S[i-1]=='C') ++rui[i];
    }
    for(int i=0;i<Q;++i){
        int ans=rui[r[i]]-rui[l[i]-1];
        if(l[i]>1 && S[l[i]-2]=='A' && S[l[i]-1]=='C') --ans;
        cout<<ans<<endl;
    }
}

int main()
{
    cin.tie();
    ios::sync_with_stdio(false);
    input();
    solve();
    return 0;
}

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

int main() {
    vector<string> vec(3);
    rep(i, 3) {
        cin >> vec[i];
        vec[i] += 'z';
    }
    vector<int> card_idx(3, 0);
    int player = 'a' - 'a';
    char now = vec[player][card_idx[player]];
    card_idx[player] += 1;
    while (now != 'z') {
        player = now - 'a';
        now = vec[player][card_idx[player]];
        card_idx[player] += 1;
    }
    cout << (char)('A' + player) << endl;
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Item {
  string name;
  double value;
  Item() {;}
  Item(string name, double p, double a, double b, double c, double d, double e, double f, double s, double m) : name(name) {
    value = (m * s * f - p) / (a + b + c + (d + e) * m);
  }
  bool operator<(const Item &rhs) const {
    if (fabs(value - rhs.value) > EPS) { return value > rhs.value; }
    return name < rhs.name;
  }
};

int n;
Item item[100];

int main() {
  while (scanf("%d", &n), n) {
    REP(i, n) {
      string name;
      double p, a, b, c, d, e, f, s, m;
      cin >> name >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      item[i] = Item(name, p, a, b, c, d, e, f, s, m);
    }
    sort(item, item + n);
    REP(i, n) {
      cout << item[i].name << endl;
    }
    puts("#");
  }
}

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

using namespace std;

int s[10001];

void sieve() {
    for(int i = 0; i < 10001; i++) s[i] = 1;
    s[0] = s[1] = 0;

    for(int i = 2; i <= sqrt(10001); i++) {
        if(s[i]) {
            for(int j = i * i; j < 10001; j += i) {
                s[j] = 0;
            }
        }
    }
}

int main() {
    sieve();
    int n;
    while(~scanf("%d", &n)) {
        if(n == 0) break;
        for(int i = n; i >= 2; i--) {
            if(s[i] && s[i - 2]) {
                printf("%d %d\n", i - 2, i);
                goto end;
            }
        }
        end:;
    }
}


#include <cstdio>

using namespace std;
typedef long long int LL;
const int Max_N(100050);

namespace io
{
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush (){fwrite (obuf, 1, oS - obuf, stdout);oS = obuf;}
	inline void putc (char x){*oS ++ = x;if (oS == oT) flush ();}
	template <class I>
	inline void gi (I &x) {for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;}
	template <class I>
	inline void print (I x){
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;while(x) qu[++ qr] = x % 10 + '0',  x /= 10;while (qr) putc (qu[qr--]);}
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::gi;
using io::putc;
using io::print;

int N, S, X[Max_N], V[Max_N];
LL P[Max_N], Ans;

void solve(int l, int r)
{
	if (l == r)
	{
		V[++V[0]] = X[l], V[++V[0]] = S;
		return;
	}
	if (S < X[l])
	{
		V[++V[0]] = X[r], solve(l, r - 1);
		return;
	}
	if (S > X[r])
	{
		V[++V[0]] = X[l], solve(l + 1, r);
		return;
	}
	if (P[l] >= P[r])
		V[++V[0]] = X[r], P[l] += P[r], solve(l, r - 1);
	else
		V[++V[0]] = X[l], P[r] += P[l], solve(l + 1, r);
}

constexpr int Abs(int x)
{
	return x >= 0 ? x : -x;
}

int main()
{
	gi(N), gi(S);
	for (int i = 1;i <= N;++i)
		gi(X[i]), gi(P[i]);
	solve(1, N);
	for (int i = V[0] - 1;i >= 1;--i)
		Ans += Abs(V[i + 1] - V[i]);
	print(Ans);
	return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+2;
int ar[N],num[N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,r,s,ans=0,lst=-1;
	cin>>n>>s;
	for(i=1;i<=n;i++){
		cin>>ar[i]>>num[i];
	}
	l=1;
	r=n;
	while(true){
		if(ar[r]<s){
			ans+=s-ar[l];
			break;
		}
		if(ar[l]>s){
			ans+=ar[r]-s;
			break;
		}
		if(num[l]>=num[r]){
			num[l]+=num[r];
			if(lst!=l){
				ans+=ar[r]-ar[l];
			}
			r--;
			lst=l;
		}
		else{
			num[r]+=num[l];
			if(lst!=r){
				ans+=ar[r]-ar[l];
			}
			l++;
			lst=r;
		}
	}
	cout<<ans;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long

const int MOD = 1e9+7;

string s;
ll d, n;
ll dp[10005][101][2];  // k = 0, cannot cross limit, k = 1 can

ll solve(ll i, ll sum, ll k){
    if(i == n) return sum == 0;
    if(dp[i][sum][k] == -1) {
    ll dig = s[i] - '0';
    if(k) dig = 9;

ll res = 0;
    for(int j = 0; j <= dig; j++){
    res += solve(i+1, (sum + j)%d, (k == 1 ||  (k == 0 && j < dig ) ? 1 : 0)  );
    res %= MOD;
}
dp[i][sum][k] = res;
}
return dp[i][sum][k];
}

int main(){
    cin >> s >> d;
    n = s.size();

    memset(dp, -1, sizeof(dp));

    cout << (solve(0, 0, 0) - 1 + MOD)%MOD;

}

#include <bits/stdc++.h>
using namespace std;
#pragma region
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
#define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

int MOD = 998244353;
int fact[222'222];
int power(int a, int n)
{
    int res = 1;
    int now = a;
    while(n)
    {
        if (n&1)
        {
            res  = (res*now) %MOD;
        }
        n>>=1;
        now = (now*now)%MOD;
    }
    return res;
}
int inv(int a)
{
    return power(a, MOD-2);
}
int C(int n, int r)
{
    if (n < r) return 0;
    return fact[n] %MOD * inv(fact[n-r])%MOD * inv(fact[r])%MOD;
}
int n, m, k;
int f(int p)
{
    return m * power(m-1, n-1-p)%MOD*C(n-1,p)%MOD;
}
void solve()
{
    fact[0] = 1;
    forn(i, 222'222-1) fact[i+1] = fact[i] * (i+1)%MOD;
    cin>>n>>m>>k;
    int ans = 0;
    forn(i, k+1)
    {
        ans =(ans + f(i))%MOD;
    }
    cout << ans <<'\n';
}

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


/*
 * RD
*/
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#ifndef rd
#define endl '\n'
#endif
#define deb cout<<"Line: "<<__LINE__<<" plz "
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
#define pii pair<int,int>
#define sz(x) ((int)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define all(con) con.begin(),con.end()
const ll inf=1e18;
const int mod=1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//12367769,266916112,123798211
ll powm(ll a, ll b, ll m);

int n,d;
pii segtree1[400005];
pii segtree2[400005];
pii query1(int l, int r)
{
	r++;
	pii res={inf+1,inf};
	for(l+=n+1,r+=n+1; l<r; l>>=1,r>>=1)
	{
		if(l&1) res=min(res,segtree1[l++]);
		if(r&1) res=min(res,segtree1[--r]);
	}
	return res;
}
pii query2(int l, int r)
{
	r++;
	pii res={inf+1,inf};
	for(l+=n+1,r+=n+1; l<r; l>>=1,r>>=1)
	{
		if(l&1) res=min(res,segtree2[l++]);
		if(r&1) res=min(res,segtree2[--r]);
	}
	return res;
}
int a[200005];
int solve(int l, int r, int l1, int r1)
{
	if(l+1==r)
		return 0;
	pii lef=query1(l+1,r-1);
	pii rig=query2(l+1,r-1);
//	deb<<l<<" "<<r<<endl;
//	deb<<lef.fi<<" "<<lef.se<<" "<<rig.fi<<" "<<rig.se<<endl;
	ll ans=0;
	if(lef.fi+a[l1]-l1*d>rig.fi+a[r1]+r1*d)
	{
		ans+=rig.fi+a[r1]+r1*d;
		lef=rig;
	}
	else
		ans+=lef.fi+a[l1]-l1*d;
	if(a[lef.se]+lef.se*d<a[r1]+r1*d)
		ans+=solve(l,lef.se,l1,lef.se);
	else
		ans+=solve(l,lef.se,l1,r1);
	if(a[lef.se]-lef.se*d<a[l1]-l1*d)
		ans+=solve(lef.se,r,lef.se,r1);
	else
		ans+=solve(lef.se,r,l1,r1);
	return ans;
}
void solve()
{
	cin>>n>>d;
	for(int i=0; i<n; i++)
		cin>>a[i];
	a[n]=inf;
	for(int i=n+1; i<2*n+2; i++)
	{
		segtree1[i]={a[i-n-1]+(i-n-1)*d,i-n-1};
		segtree2[i]={a[i-n-1]-((i-n-1)*d),i-n-1};
	}
	for(int i=n; i>0; i--) {
		segtree1[i]=min(segtree1[i<<1],segtree1[i<<1|1]);
		segtree2[i]=min(segtree2[i<<1],segtree2[i<<1|1]);
	}
	cout<<solve(0,n,0,n)<<endl;
}
signed main()
{
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	srand(chrono::steady_clock::now().time_since_epoch().count());
	auto clk=clock();
	int t=1;
//	cin>>t;
	while(t--)
		solve();
#ifdef rd
	cout<<endl<<endl<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
	return 0;
}
ll powm(ll a,ll b, ll m) {
	ll te=1;
	while(b) {
		if(b&1) te=(te*a)%m;
		a=(a*a)%m;
		b>>=1;
	}
	return te;
}

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define lc now<<1
#define rc now<<1|1
using namespace std;
inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
inline void write(LL x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
inline void pr1(int x){write(x);putchar(' ');}
inline void pr2(LL x){write(x);putchar('\n');}
const LL inf=1e18;
const int MAXN=200005;
int n,D,a[MAXN];
struct node
{
	int p;LL c;
	node(){}
	node(int _p,LL _c){p=_p;c=_c;}
};
node _min(node u1,node u2){return u1.c<u2.c?u1:u2;}
struct segtree1
{
	node mn[2][MAXN*4];
	void buildtree(int now,int l,int r)
	{
		if(l==r){mn[0][now]=mn[1][now]=node(l,inf);return ;}
		int mid=(l+r)/2;
		buildtree(lc,l,mid);buildtree(rc,mid+1,r);
		mn[0][now]=mn[1][now]=_min(mn[0][lc],mn[1][rc]);
	}
	void modify(int now,int l,int r,int p,node c,int opt)
	{
		if(l==r){mn[opt][now]=c;return ;}
		int mid=(l+r)/2;
		if(p<=mid)modify(lc,l,mid,p,c,opt);
		else modify(rc,mid+1,r,p,c,opt);
		mn[opt][now]=_min(mn[opt][lc],mn[opt][rc]);
	}
	node qry(int now,int l,int r,int ql,int qr,int opt)//0 左边   1 右边
	{
		if(l==ql&&r==qr)return mn[opt][now];
		int mid=(l+r)/2;
		if(qr<=mid)return qry(lc,l,mid,ql,qr,opt);
		else if(mid+1<=ql)return qry(rc,mid+1,r,ql,qr,opt);
		else return _min(qry(lc,l,mid,ql,mid,opt),qry(rc,mid+1,r,mid+1,qr,opt));
	}
}seg1,seg2;//1  未加入   2  加入
struct segtree2
{
	node mn[MAXN*4];
	void buildtree(int now,int l,int r)
	{
		if(l==r){mn[now]=node(l,inf);return ;}
		int mid=(l+r)/2;
		buildtree(lc,l,mid);buildtree(rc,mid+1,r);
		mn[now]=_min(mn[lc],mn[rc]);
	}
	void pushup(int now,int l,int r)
	{
		int mid=(l+r)/2;
		mn[now]=_min(mn[lc],mn[rc]);
		node u1=seg1.mn[0][lc],u2=seg2.mn[1][rc];
//		u1=seg1.qry(1,1,n,l,mid,0),u2=seg2.qry(1,1,n,mid+1,r,1);
		mn[now]=_min(mn[now],node(u1.p,u1.c+u2.c));
		u1=seg1.mn[1][rc],u2=seg2.mn[0][lc];
//		u1=seg1.qry(1,1,n,mid+1,r,1),u2=seg2.qry(1,1,n,l,mid,0);
		mn[now]=_min(mn[now],node(u1.p,u1.c+u2.c));
	}
	void modify(int now,int l,int r,int p)
	{
		if(l==r){mn[now].c=inf;return ;}
		int mid=(l+r)/2;
		if(p<=mid)modify(lc,l,mid,p);
		else modify(rc,mid+1,r,p);
		pushup(now,l,r);
	}
}seg3;
int main()
{
	n=read();D=read();
	if(n==1)return puts("0"),0;
	for(int i=1;i<=n;i++)a[i]=read();
	seg1.buildtree(1,1,n);
	seg2.buildtree(1,1,n);
	seg3.buildtree(1,1,n);
	for(int i=2;i<=n;i++)
	{
		seg1.modify(1,1,n,i,node(i,(LL)-D*i+a[i]),0);
		seg1.modify(1,1,n,i,node(i,(LL)D*i+a[i]),1);
	}
	seg2.modify(1,1,n,1,node(1,(LL)-D*1+a[1]),0);
	seg2.modify(1,1,n,1,node(1,(LL)D*1+a[1]),1);

	seg3.modify(1,1,n,1);
	LL ans=0;
	for(int i=1;i<n;i++)
	{
		ans+=seg3.mn[1].c;
		int u=seg3.mn[1].p;
		seg1.modify(1,1,n,u,node(u,inf),0);seg1.modify(1,1,n,u,node(u,inf),1);
		seg2.modify(1,1,n,u,node(u,(LL)-D*u+a[u]),0);seg2.modify(1,1,n,u,node(u,(LL)D*u+a[u]),1);
		seg3.modify(1,1,n,u);
	}
	pr2(ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  string ns;
  getline(cin, ns);
  int a = 0, b = 1;
  for (auto c : ns) {
    int d = c - '0';
    int a_ = min(a + d, b + 10 - d);
    b = min(a + d + 1, b + 9 - d);
    a = a_;
  }
  cout << a << '\n';
}


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

int main() {
	int N, T;
	cin >> N >> T;
	vector<int>t(N);
	for (int i = 0;i<N;++i) {
		cin >> t[i];
	}
	int ans = 0;
	for (int i = 0;i<N-1;++i) {
		ans += min(T, t[i + 1] - t[i]);
	}
	ans += T;
	cout << ans << endl;
	return 0;
}

// review
#include <iostream>

using namespace std;

int n, m;

void solve() {
  int second = m & 1 ? m + 1 : m + 2;
  int cnt = 1;
  int cnt2 = 0;
  for (int i=0;i<m;i++) {
    if (i < m / 2) {
      cout << cnt << " " << (second - cnt) << endl;
      cnt++;
    } else {
      cout << cnt2 + second << " " << (2 * m + 1 - cnt2) << endl;
      cnt2++;
    }
  }
}

int main() {
  cin >> n >> m;
  solve();
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>

using namespace std;
typedef long long ll;

int main() {
	ll N; cin >> N;
	ll* A = new ll[N];
	ll m = 0;
	ll sum = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		sum = sum ^ A[i];
		m = m | A[i];
	}

	ll filter = m - sum;

	set<ll, greater<ll>> st;
	for (int i = 0; i < N; i++) {
		ll a = A[i] & filter;
		for (auto itr = st.begin(); itr != st.end(); itr++) {
			a = min(a, a ^ *itr);
		}
		if (a != 0) {
			st.insert(a);
		}
	}

	ll s = filter;
	for (auto itr = st.begin(); itr != st.end(); itr++) {
		s = min(s, (s ^ *itr) & filter);
	}
	cout << sum + (filter - s) * 2 << endl;

	return 0;
}

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}

ll msb(ll v){
    v = v | (v >> 1LL);
    v = v | (v >> 2LL);
    v = v | (v >> 4LL);
    v = v | (v >> 8LL);
    v = v | (v >> 16LL);
    v = v | (v >> 32LL);
    return v ^ (v >> 1LL);
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll A[100010];
    rep(i,0,N) cin >> A[i];
    ll ans = 0;
    rep(d,0,60){
        ll cnt = 0;
        rep(i,0,N){
            if((A[i]>>d)&1) cnt++;
        }
        if(cnt % 2 == 1){
            ans += (1LL<<d);
            rep(i,0,N){
                if((A[i]>>d)&1){
                    A[i] -= (1LL<<d);
                }
            }
        }
    }
    vector<ll> V;
    rep(i,0,N){
        for(auto &v: V) if(msb(v) & A[i]) A[i] ^= v;
        if(A[i] > 0){
            for(auto &v: V) if(msb(A[i]) & v) v ^= A[i];
            V.push_back(A[i]);
            sort(V.begin(), V.end(), greater<ll>());
        }
    }

    ll res = 0;
    for(auto &v: V) res ^= v;
    ans += res * 2;
    print(ans);

}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=300100,MOD=1e9+7;
const int automata[8][2]={{3,1},{5,2},{2,2},{4,7},{3,3},{6,1},{5,5},{3,1}};
char ch[MAXN];
int dp[MAXN][8],len;
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>ch;
	len=strlen(ch);
	dp[len][1]=dp[len][2]=1;
	for(register int i=len-1;~i;i--)
		for(register int j=0;j<8;j++)
		{
			if(ch[i]!='0') dp[i][j]=(dp[i][j]+dp[i+1][automata[j][1]])%MOD;
			if(ch[i]!='1') dp[i][j]=(dp[i][j]+dp[i+1][automata[j][0]])%MOD;
		}
	cout<<dp[0][0]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
  int n;
  ll t;
  cin >> n >> t;
  vector<ll> a(n);
  ll ans = 0;
  ll lim = a[0] + t;
  for(int i=0;i<n;i++) cin >> a[i];
  for(int i=0;i<n;i++){
    if(i == 0) {
      ans = t;
      continue;
    }
    if(lim > a[i]){
      ll ext = (a[i]+t-lim);
      if(ext < 0) continue;
      lim = a[i]+t;
      ans +=  ext;
    }else{
      lim = a[i] + t;
      ans += t;
    }
  }
  cout << ans << endl;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main (void)
{
  int a,b,ans;
  cin>>a>>b;
  ans=0;
  ans=ans+max(a,b);
  ans+=max(max(a-1,b-1),min(a,b));
  cout<<ans<<endl;
}