#include <set>
#include <cstdio>
using namespace std;
#define maxn 200005
#define int long long
set < pair < int, int > > st;
set < pair < int, int > > :: iterator it;
int n;
int t[maxn];

int lowbit( int x ) {
	return x & ( -x );
}

void add( int x, int val ) {
	for( int i = x;i <= n;i += lowbit( i ) ) t[i] += val;
}

int query( int x ) {
	int ans = 0;
	for( int i = x;i;i -= lowbit( i ) ) ans += t[i];
	return ans;
}

signed main() {
	scanf( "%lld", &n );
	for( int i = 1, x;i <= n;i ++ ) {
		scanf( "%lld", &x );
		st.insert( make_pair( x + i, i ) );
	}
	bool flag = 0; int ans = 0;
	for( int i = 1, x;i <= n;i ++ ) {
		scanf( "%lld", &x );
		it = st.lower_bound( make_pair( x + i, 0 ) );
		if( it == st.end() ) flag = 1;
		else {
			int v = query( ( *it ).second );
			add( 1, 1 ), add( ( *it ).second, -1 );
			ans += ( *it ).second + v - i;
			st.erase( it );
		}
	}
	if( flag ) printf( "-1\n" );
	else printf( "%lld\n", ans );
	return 0;
}

#include <bits/stdc++.h>
typedef long long i64;
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;

const int xn = 2e5;
int n, a[xn], b[xn], p[xn], c[xn];
map<int, queue<int>> mp;

void ap(int i) {for (; i < n; i |= i + 1) {++c[i];}}
int sp(int i) {int x = 0;
 for (; i; i &= i - 1) {x += c[i - 1];} return x;}

int main() {
  // freopen("#.in", "r", stdin); // @
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  cin >> n;
  rep(i, 0, n) {cin >> a[i]; a[i] += i; mp[a[i]].push(i);}
  bool ok = true;
  rep(i, 0, n) {
    cin >> b[i]; b[i] += i;
    queue<int> &q = mp[b[i]];
    if (!sz(q)) {ok = false;}
    else {p[i] = q.front(); q.pop();}
  }

  if (ok) {
    i64 res = 0;
    rep(i, 0, n) {res += i - sp(p[i]); ap(p[i]);}
    cout << res << '\n';
  } else {cout << "-1\n";}
  return 0;
}

/* 📌https://www.cnblogs.com/George1123/p/tips.html */


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+3;
int num[N];
vector<pair<int,int> > adj[N];

void dfs(int u, int p){
	for(auto it : adj[u]){
		if(num[it.first] != -1){
			continue;
		}
		if(num[u] == it.second){
			num[it.first] = (it.second == 1 ? 2 : 1);
		}else{
			num[it.first] = it.second;
		}
		dfs(it.first,u);
	}
}

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

	memset(num,-1,sizeof(num));
	int n,m;
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int u,v,m;
		cin >> u >> v >> m;
		u--,v--;
		adj[u].emplace_back(v,m);
		adj[v].emplace_back(u,m);
	}
	if(adj[0].empty()){
		cout << "No\n";
		return 0;
	}
	num[0] = adj[0][0].second;
	dfs(0,-1);
	bool val = true;
	for(int i=0;i<n;i++){
		if(num[i] == -1){
			val = false;
			break;
		}
	}
	if(!val){
		cout << "No\n";
	}else{
		for(int i=0;i<n;i++){
			cout << num[i] << '\n';
		}
	}
}

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
    int n, m;
    cin >> n >> m;
    vector< int > a(m+1), b(m+1), c(n+1), dir(m+1, 0);
    for(int i=1;i<=m;i++) cin >> a[i] >> b[i];
    for(int i=1;i<=n;i++) cin >> c[i];

    for(int i=1;i<=m;i++){
        if(c[a[i]] > c[b[i]]){
            dir[i] = 1;
        }else if(c[a[i]] < c[b[i]]){
            dir[i] = -1;
        }
    }

    auto f = [&](auto f, int v)->void{
        for(int i=1;i<=m;i++){
            if(dir[i] != 0 || (a[i] != v && b[i] != v)) continue;
            if(a[i] == v){
                dir[i] = 1;
                f(f, b[i]);
            }
            if(b[i] == v){
                dir[i] = -1;
                f(f, a[i]);
            }
        }
    };

    for(int i=1;i<=m;i++){
        if(dir[i] != 0) continue;
        f(f, a[i]);
    }

    for(int i=1;i<=m;i++){
        cout << (dir[i] == 1 ? "->" : "<-") << endl;
    }
}


#include <iostream>
using namespace std;

int n ;

int main()
{
    cin >> n;

    if (n%2==0) {
        cout << "White" << endl;
    } else {
        cout << "Black" << endl;
    }

}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define sz(x) (int)(x).size()
#define dup(x,y) ((x+y-1)/y)
#define dame0 { puts("0"); return 0;}
#define dame1 { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define outa cout << ans << endl
#define out(x) cout << x << endl
#define ul(x) x^=32  // 大文字小文字を逆にする
#define shousuu cout << fixed << setprecision(15)
bool flg = false;

int main(){
    int n; cin>>n;
    if(n%2==0) out("White");
    else out("Black");
}

 /*
g++ a.cpp
./a.out
*/


/**
 *  AtCoder Beginner Contest
**/
// >>> ngng628's library
// >>> includes
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>
#include <bitset>
// <<<
// >>> macros
# define cauto const auto
# define int long long
# define float long double
# define FI first
# define SE second
# define mkp make_pair
# define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i,n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i,n) for(int i=((int)(n)); i>0; --i)
# define repr(i,b,e) for(int i=(b), i##_len=(e); i<i##_len; ++i)
# define reprs(i,b,e) for(int i=(b), i##_len=(e); i<=i##_len; ++i)
# define step(n) rep(_, n)
# define all(x) std::begin(x), std::end(x)
# define rall(x) std::rbegin(x), std::rend(x)
# define pb push_back
# define eb emplace_back
# define len(x) ((int)(x).size())
# define contains(x,key) ((x).find(key) != (x).end())
# define lb(v,x) distance(std::begin(v), lower_bound(all(v), (x)))
# define ub(v,x) distance(std::begin(v), upper_bound(all(v), (x)))
// <<<
// >>> usings
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using mii = map<int, int>;
using mci = map<char, int>;
using msi = map<string, int>;
// <<<
// >>> functions
template<class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& x : v) is >> x; return is; }
template<class T> istream& operator>>(istream& is, vector<vector<T>>& v) { for(auto& x : v) for (auto& y : x) is >> y; return is; }
template<class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.FI >> p.SE; }
template<class T> string join(const vector<T> &v){ stringstream s; rep (i, len(v)) s<<' '<<v[i]; return s.str().substr(1); }
template<class T> ostream& operator<<(ostream& os, const vector<T>& v){ if (len(v)) os << join(v); return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){ rep (i, len(v)) { if (len(v[i])) os << join(v[i]) << (i-len(v)+1 ? "\n" : ""); } return os; }
template<class T, class U> ostream& operator<<(ostream& os, const pair<T,U>& p){ cout << p.FI << " " << p.SE; return os; }
void print(){ cout << "\n"; }
template<class T, class... A>void print(const T& v, const A&...args){cout << v; if(sizeof...(args))cout << " "; print(args...);}
void eprint() { cerr << "\n"; }
template<class T, class... A>void eprint(const T& v, const A&...args){cerr << v; if(sizeof...(args))cerr << " "; eprint(args...);}
template<class T> inline void Unique(T& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); }
template<class T> inline constexpr bool chmax(T &a, T b) { return a < b && (a = b, true); }
template<class T> inline constexpr bool chmin(T &a, T b) { return a > b && (a = b, true); }
constexpr int ctoi(const char c) { return ('0' <= c && c <= '9') ? (c - '0') : -1; }
const char* YesNo(bool b) { return b ? "Yes" : "No"; }
const char* YESNO(bool b) { return b ? "YES" : "NO"; }
const char* yesno(bool b) { return b ? "yes" : "no"; }
const char* yn(bool b) { return YesNo(b); }
// <<<
// <<<

int32_t main() {
   int x;
   cin >> x;
   if (x >= 0) {
      print(x);
   }
   else {
      print(0);
   }

   return 0;
}

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

using ll = long long;

int main(){
    int N;
    cin >> N;
    vector<int> v;
    for(int i = 0;i < N;i++){
        int a;cin >> a;
        v.push_back(a);
    }
    int ma = 0;
    int ans = -1;
    for(int i = 2;i <= 1000;i++){
        int cnt = 0;
        for(auto a : v){
            if(a % i == 0)cnt++;
        }
        if(cnt > ma){
            ma = cnt;
            ans = i;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define rei register int
#define pb push_back
using namespace std;
const int maxn = 100010;
int n;
int dp[maxn],g[maxn],sz[maxn];
vector<int>ver[maxn];
void dfs(int x){ g[x] = sz[x] = 1; int cnt = 0;
	for(rei i = 0;i < ver[x].size();++i) dfs(ver[x][i]);
	for(rei i = 0;i < ver[x].size();++i) cnt ^= g[ver[x][i]];
	for(rei i = 0;i < ver[x].size();++i) g[x] ^= g[ver[x][i]],sz[x] += sz[ver[x][i]];
	for(rei i = 0;i < ver[x].size();++i) if(g[ver[x][i]] == 0){
		if(sz[ver[x][i]] - dp[ver[x][i]] <= dp[ver[x][i]]) dp[x] += dp[ver[x][i]];
		else if(cnt == 1) dp[x] += sz[ver[x][i]] - dp[ver[x][i]];
		else dp[x] += dp[ver[x][i]];
	} vector<pair<int,int > >st;
	for(rei i = 0;i < ver[x].size();++i) if(g[ver[x][i]] == 1) st.pb(make_pair(2 * dp[ver[x][i]] - sz[ver[x][i]],dp[ver[x][i]]));
	sort(st.begin(),st.end()); reverse(st.begin(),st.end());
	for(rei i = 0;i < st.size();i += 2) dp[x] += st[i].second;
	for(rei i = 1;i < st.size();i += 2) dp[x] += st[i].second - st[i].first;
} int main(){
	cin >> n; for(rei i = 1;i < n;++i){
		int x; scanf("%d",&x); ver[x].pb(i + 1);
	} dfs(1);
//	for(rei i = 1;i <= n;++i) cerr << dp[i] << " " << g[i] << endl;

	cout << n - dp[1];
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;//10の18乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}

int main(){
    ll n;
    cin >> n;
    Vl v(n);
    VVl to(n);
    rep(i,n-1){
        cin >> v[i+1];
        v[i+1]--;
        to[v[i+1]].PB(i+1);
    }
    Vl dp(n),dp2(n);
    repd(i,n){
        ll sen=1,go=0;
        vector<P> can;//奇数の行き先
        ll last=0,last2=0;
        for(int j:to[i]){
            ll x=dp[j],y=dp2[j];
            if((x+y)%2==0){
                if(x<=y){
                    sen+=x;go+=y;
                }
                else{
                    last+=x;
                    last2+=y;
                }
            }
            else{
                can.PB(MP(x-y,x));//相手に利益が低いものを押し付ける
            }
        }
        sort(all(can));
        rep(j,can.size()){
            if(j%2==1){
                go+=can[j].S;
                sen+=(-can[j].F+can[j].S);
            }
            else{
                sen+=can[j].S;
                go+=(-can[j].F+can[j].S);

            }
        }
        if(can.size()%2==0){//?
            sen+=last;
            go+=last2;
        }
        else{
            sen+=last2;
            go+=last;
        }
        dp[i]=sen;dp2[i]=go;
        //cout << last << endl;
    }
    cout << dp[0] << endl;
    //rep(i,n) cout << dp[i] << endl;
    //rep(i,n) cout << dp2[i] << endl;
}


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

vector<int> a(10010);

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a.at(i);
	int ans = 0;
	for (int i = 0; i < n; i++) {
		int x = a[i];
		for (int j = i; j < n; j++) {
			x = min(x, a[j]);
			ans = max(ans, x * (j - i + 1));
		}
	}
	cout << ans << endl;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll h[100005],s[5000005],w[5000005];
int n,p;
int main()
{
    while (scanf ("%d",&n)!=EOF)
    {
        if (n==0) return 0;
        memset(h,0,sizeof(h));
        memset(s,0,sizeof(h));
        for (int i=1;i<=n;i++)
          scanf ("%d",&h[i]);
        h[n+1]=p=0;//增加一个高度为0的矩形，避免扫描栈结束后有剩余矩形
        ll ans=0;
        for (int i=1;i<=n+1;i++)
        {
            if (h[i]>s[p])
            {
                s[++p]=h[i];
                w[p]=1;
            }
            else //如果当前矩形比之前矩形低，那就出栈栈中矩形，直到有矩形比它低
            {//用一个  宽度为出栈矩形总宽 高度为当前矩形  的新矩形代替之前矩形
                ll wid=0;
                while (s[p]>h[i])
                {
                    wid+=w[p];//统计宽度
                    ans=max(ans,1LL*(wid*s[p]));//计算出栈的矩形面积，更新答案
                    p--;//出栈
                }
                w[++p]=wid+1;//将新矩形入栈
                s[p]=h[i];
            }
        }
        while (p!=0)//将所有矩形出栈，按照上面的方法计算面积，更新答案
        {
            ans=max(ans,1LL*(s[p]*w[p]));
            p--;
        }
        printf("%lld\n",ans);
    }
}


#include <stdio.h>
int main()
{
  	int n;
	scanf("%d", &n);
	if (n*108/100 > 206)
	{
		printf(":(");
	}
	else if (n*108/100 == 206){
		printf("so-so");
	}
	else printf("Yay!");
	return 0;
}

#include <bits/stdc++.h>
int main(){
  int n;
  scanf("%d",&n);
  printf("%d",n-1);
}


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

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef vector<bool> VB;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) LL((a).size())

#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ROF(i,a,b) for(LL i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL LARGE_INT = 1e9+100;
const LL INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const LL MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

LL TIME_LIMIT = 1992;

LL start_time;
string result_string;
LL result_point=0;
LL h=50;
LL w=50;
VVL t;
VVL p;

void check(string result, LL point){
    if(point > result_point){
        result_point = point;
        result_string = result;
    }
}

void dfs(LL x, LL y, VB v, string result, LL point){
    LL time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
    if(time-start_time > TIME_LIMIT){
        check(result, point);
        return;
    }

    v[t[x][y]]=true;

    VL dx={1,-1,0,0};
    VL dy={0,0,1,-1};

    VVL tp;

    REP(i,4){
        LL nx = x+dx[i];
        LL ny = y+dy[i];
        if(nx >= h || nx < 0 || ny >= w || ny < 0){
            continue;
        }
        if(v[t[nx][ny]]){
            continue;
        }
        LL dist = min(nx,abs(h-nx)) + min(ny,abs(w-ny));

        tp.PB({-dist,p[nx][ny],i,nx,ny});
    }

    if(tp.empty()){
        check(result, point);
        return;
    }

    SORT_DESC(tp);

    for(auto w : tp){
        LL np = w[1];
        LL i = w[2];
        LL nx = w[3];
        LL ny = w[4];
        //cerr<<np<<" "<<i<<" "<<nx<<" "<<ny<<endl;
        string d;
        if(i==0){
            d = "D";
        }
        if(i==1){
            d = "U";
        }
        if(i==2){
            d = "R";
        }
        if(i==3){
            d = "L";
        }
        dfs(nx, ny, v, result+d, point+np);
    }
}

void Main()
{
    start_time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
    //dump(start_time)
    LL si,sj;cin>>si>>sj;

    t.resize(h);
    REP(i,h){
        t[i].resize(w);
        REP(j,w){
            cin>>t[i][j];
        }
    }
    p.resize(h);
    REP(i,h){
        p[i].resize(w);
        REP(j,w){
            cin>>p[i][j];
        }
    }

    VB v(h*w);
    string result = "";
    LL point = 0;

    dfs(si, sj, v, result, point);

    cout<<result_string<<endl;

	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}

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

int si,sj,mx = 0;
int t[55][55],p[55][55];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

bool used[2505];

string base = "RDLU";
string fans = "";

void input() {
    cin >> si >> sj;
    for(int i = 0; i < 50; i++) {
        for(int j = 0; j < 50; j++) {
            cin >> t[i][j];
        }
    }
    for(int i = 0; i < 50; i++) {
        for(int j = 0; j < 50; j++) {
            cin >> p[i][j];
        }
    }
}

int score(string tmp) {
    int nx = si;
    int ny = sj;
    int ans = p[si][sj];
    for(int i = 0; i < tmp.size(); i++) {
        for(int j = 0; j < 4; j++) {
            if(base[j] == tmp[i]) {
                nx += dx[j];
                ny += dy[j];
                ans += p[nx][ny];
            }
        }
    }
    return ans;
}

void chmax(string tmp) {
    if(mx < score(tmp)) {
        mx = score(tmp);
        fans = tmp;
    }
}

string BFS() {
    used[t[si][sj]] = true;
    int nx = si;
    int ny = sj;
    string ans = "";
    int r = rand()%4;
    while (true) {
        bool flag = false;
        if(rand()%25 == 0) {
            r = rand()%4;
        }
        for(int i = r; i < r+4; i++) {
            int nnx = nx+dx[i%4];
            int nny = ny+dy[i%4];
            if(nnx >= 0 && nny >= 0 && nnx < 50 && nny < 50 && !used[t[nnx][nny]]) {
                nx = nnx;
                ny = nny;
                ans += base[i%4];
                flag = true;
                used[t[nnx][nny]] = true;
                break;
            }
        }
        if(!flag) {
            break;
        }
    }
    memset(used,false,sizeof(used));
    return ans;
}

string BFS2(int r) {
    used[t[si][sj]] = true;
    int nx = si;
    int ny = sj;
    string ans = "";
    while (true) {
        bool flag = false;
        for(int i = r; i < r+4; i++) {
            int nnx = nx+dx[i%4];
            int nny = ny+dy[i%4];
            if(nnx >= 0 && nny >= 0 && nnx < 50 && nny < 50 && !used[t[nnx][nny]]) {
                nx = nnx;
                ny = nny;
                ans += base[i%4];
                flag = true;
                used[t[nnx][nny]] = true;
                break;
            }
        }
        if(!flag) {
            break;
        }
    }
    memset(used,false,sizeof(used));
    return ans;
}

int main() {
    srand((unsigned)time(NULL));
    input();
    for(int i = 0; i < 1000000; i++) {
        chmax(BFS());
    }
    chmax(BFS2(0));
    chmax(BFS2(1));
    chmax(BFS2(2));
    chmax(BFS2(3));
    cout << fans << endl;
}


#include<bits/stdc++.h>
#define endl ("\n")
#define int long long
#define read(a,n) rep(i,0,n){cin>>a[i];}
#define print(a,n) rep(i,0,n) cout<<a[i]<<" ";
#define yy cout<<"Yes"<<endl;
#define nn cout<<"No"<<endl;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = clock();
    int t = 1 ;
    // cin >> t ;
    while (t--)
    {
        int n, k, a, b;
        cin >> n >> k;
        map<int, int> m;
        int tb = k;
        rep(i, 0, n)
        {
            cin >> a >> b;
            m[a] += b;

        }
        for (auto it : m) {

            a = it.ff;
            b = it.ss;
            if (tb < a)
            {
                break;
            }

            tb += b;
        }

        cout << tb << endl;

    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(v) v.begin(), v.end()
using P = pair<int,int>;
constexpr int inf = numeric_limits<int>::max() / 2;
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	int n, m; cin >> n >> m;
	vector<int> w(n); rep(i,n) cin >> w[i];
	vector<P> b(m+1);
	rep(i,m){
		int l, v; cin >> l >> v;
		b[i] = {v,l};
	}
	b[m] = {0,0};
	sort(all(b));
	int e = 1;
	for(int i=1; i<=m; i++){
		if(b[i].first == b[e-1].first) b[e-1].second = b[i].second;
		else if(b[i].second > b[e-1].second) b[e++] = b[i];
	}
	int ans = inf;
	vector<int> v(n); iota(all(v), 0);
	rep(i,n) if(b[1].first < w[i]) return 0 * puts("-1");
	do {
		vector<int> len(n,0), ac(n+1,0);
		rep(i,n) ac[i+1] = ac[i] + w[v[i]];
		rep(i,n) for(int j=i+1; j<n; j++){
			int l = len[i] + (--lower_bound(b.begin(), b.begin()+e, P{ac[j+1]-ac[i],0}))->second;
			len[j] = max(len[j], l);
		}
		ans = min(ans, len[n-1]);
	}while(next_permutation(all(v)));
	cout << ans << "\n";
}


// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define _rep(i, x, y) for(int i = (int)x; i < (int)y; ++i)
#define _dep(i,x,y) for(int i = (int)x; i > (int)y; i--)
#define PII pair<int,int>
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define PQ priority_queue
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<ll> VL;
typedef vector<vector<ll>> VLL;
constexpr int mod = 1e9 + 7;
constexpr int KINF = 0x3f3f3f3f;
constexpr int INF = 0x7f7f7f7f;
constexpr double eps = 1e-7;
int main(){
	ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
	int r, x, y;
	cin >> r >> x >> y;
	ll dist = (1ll * x * x) + (1ll * y * y);
	//dist = ceil(sqrt(dist));
	if(dist < 1ll * r * r) {
		cout << 2 << endl;
		return 0;
	}
	ll k = 1;
	while(1ll * k * k * r * r < dist) k ++;
	cout << k << endl;
	return 0;
}
// 小心左移运算符导致的int溢出
// dp记得处理非法状态
// resize并不会初始化数组
// 全局函数一定要记得inline
// 带空格字符串不能直接cin,要用getline
// 最好用用"\n"代替endl

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

#include <bits/stdc++.h>

using namespace std ;

#define int int64_t //be careful about this
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)

#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue

#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))

template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}

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

template<typename T>
void __p(T a) {
  cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
  cout<<"{";
  __p(a.first);
  cout<<",";
  __p(a.second);
  cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
  cout<<"{";
  for(auto it=a.begin(); it<a.end(); it++)
    __p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
  __p(a1);
  __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cout<<name<<" : ";
  __p(arg1);
  cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  int bracket=0,i=0;
  for(;; i++)
    if(names[i]==','&&bracket==0)
      break;
    else if(names[i]=='(')
      bracket++;
    else if(names[i]==')')
      bracket--;
  const char *comma=names+i;
  cout.write(names,comma-names)<<" : ";
  __p(arg1);
  cout<<" | ";
  __f(comma+1,args...);
}

void setIO(string s = "") {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin.exceptions(cin.failbit);
	cout.precision(15);	cout << fixed;
  #ifdef ONLINE_JUDGE
  if(sz(s)){
  	freopen((s+".in").c_str(),"r",stdin);
  	freopen((s+".out").c_str(),"w",stdout);
  }
  #endif
}
const int inf = 1e9, A = 26;
signed main(){
  setIO() ;
  int n,m; rd(n,m);
  int dp[n][n];
  f(i,0,n) f(j,0,n) dp[i][j] = inf ;
  f(i,0,n) dp[i][i] = 0;
  vt<int> g[n][A];
  f(_,0,m){
  	int u,v; char c;
  	rd(u,v,c);
  	--u,--v;
  	ckmin(dp[u][v],int(1));
  	ckmin(dp[v][u],int(1));
  	g[u][c-'a'].pb(v);
  	g[v][c-'a'].pb(u);
  }
  PQ<ar<int,3>> q;
  f(i,0,n) f(j,0,n) q.push({-dp[i][j],i,j});
  while(sz(q)){
  	auto a = q.top(); q.pop();
  	int v = -a[0], i = a[1], j = a[2];
  	//if(dp[i][j] != v) continue;
  	f(k,0,A){
  		for(auto x : g[i][k]){
  			for(auto y : g[j][k]){
  				if(ckmin(dp[x][y],v + 2)){
  					q.push({-dp[x][y],x,y});
  				}
  			}
  		}
  	}
  }
  cout << (dp[0][n-1] == inf ? int(-1) : dp[0][n-1]);
}


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

struct SegmentTree{
    int N;
    vector<int> tree;
    SegmentTree(vector<int>& v) {
        int sz = v.size();
        N = 1;
        while(N < sz) N *= 2;
        tree = vector<int>(2 * N, 0);
        for(int i = 0;i < sz;++i) tree[N + i] = v[i];
    }
    void update(int index) {
        index += N;
        tree[index]++;
        while(index > 0) {
            index /= 2;
            tree[index] = tree[2 * index] + tree[2 * index + 1];
        }
    }
    int sum(int left, int right) {
        left += N, right += N;
        int res = 0;
        while(right - left > 1) {
            if(left % 2 == 0) left /= 2;
            else {
                res += tree[left];
                left = left / 2 + 1;
            }
            if(right % 2 == 0) {
                res += tree[right];
                right = right / 2 - 1;
            }
            else right /= 2;
        }
        res += (left == right ? tree[left] : tree[left] + tree[right]);
        return res;
    }
};

int main() {
    int n; cin >> n;
    vector<int> a(n), b(n);
    for(auto& e : a) cin >> e;
    for(auto& e : b) cin >> e;
    for(int i = 0;i < n;++i) a[i] += i, b[i] += i;
    map<int, int> mpa, mpb;
    for(int i = 0;i < n;++i) mpa[a[i]]++, mpb[b[i]]++;
    for(auto p : mpa) {
        if(mpb[p.first] != p.second) {
            cout << -1 << '\n';
            return 0;
        }
    }
    map<int, queue<int>> mp;
    for(int i = 0;i < n;++i) mp[b[i]].push(i);
    vector<int> v1(n), v2(n);
    for(int i = 0;i < n;++i) {
        v1[i] = mp[a[i]].front();
        mp[a[i]].pop();
    }
    SegmentTree seg(v2);
    long long ans = 0;
    for(int i = 0; i < n;++i) {
        ans += seg.sum(0, v1[i]);
        seg.update(v1[i]);
    }
    ans = (long long)n * (n - 1) / 2 - ans;
    cout << ans << '\n';
    return 0;
}

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

const int maxn=200000+10;
ll tree[maxn],n,a[maxn],b[maxn],c[maxn];

int lowbit(int i) {return i&-i;}
ll ask(int x)
{
    int res=0;
    for(int i=x;i;i-= lowbit(i)) res+=tree[i];
    return res;
}
void modify(int x,int val)
{
    for(int i=x;i<=n;i+=lowbit(i)) tree[i]+=val;
}

int main()
{
    cin>>n;
    vector <P> vec1,vec2;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        a[i]+=i;
        vec1.push_back(P(a[i],i));
    }
    for(int i=0;i<n;i++)
    {
        cin>>b[i];
        b[i]+=i;
        vec2.push_back(P(b[i],i));
    }
    sort(vec1.begin(),vec1.end());
    sort(vec2.begin(),vec2.end());
    for(int i=0;i<n;i++)
    {
        if(vec1[i].first!=vec2[i].first)
        {
            cout<<"-1\n";
            return 0;
        }
        c[vec1[i].second]=vec2[i].second;
    }
    ll ans=0;
    for(int i=n-1;i>=0;i--)
    {
        ans+=ask(c[i]+1);
        //cout<<ans<<endl;
        modify(c[i]+1,1);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define per(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int N=59;

inline long long read() {
    register long long x=0, f=1; register char c=getchar();
    while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
    while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+c-48,c=getchar();}
    return x*f;
}

int n,m,col[N],res=0,ans=1;
bool vst[N];
bool ed[N][N];
vector<int>e[N],vec;

void dfs(int u,int ret=0) {
	vst[u]=1;
	vec.push_back(u);
	for(auto v:e[u]) if(!vst[v]) dfs(v);
}
void sfd(int pos) {
	if(pos==vec.size()) {
		res++;
		return;
	}
	rep(i,1,3) {
		bool qwq=1;
		rep(j,0,pos-1) if(ed[vec[pos]][vec[j]]&&col[j]==i) qwq=0;
		if(!qwq) continue;
		col[pos]=i;
		sfd(pos+1);
	}
}

signed main() {
	n=read(), m=read();
	rep(i,1,m) {
		int u=read(), v=read();
		e[u].push_back(v), e[v].push_back(u);
		ed[u][v]=ed[v][u]=1;
	}
	rep(i,1,n) if(!vst[i]) {
		vec.clear(), res=0;
		dfs(i);
		sfd(0);
		ans*=res;
	}
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
clock_t start=clock();
int main()
{
  int N,M;
  cin>>N>>M;
  vector<string> s(M);
  string answer[N][N];
  for(int i=0;i<M;i++) cin>>s[i];
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      answer[i][j]=".";
    }
  }
  int num=0;
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      if(answer[i][j]=="."&&s[num].size()+j<N){
        for(int k=0;k<s[num].size();k++){
          answer[i][j+k]=s[num][k];
        }
        num++;
      }
    }
  }
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      cout<<answer[i][j];
    }
    cout<<endl;
  }
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>

using namespace std;
typedef long long ll;
const ll  maxn = 1e3 + 10;
ll n, i, j, k, a, b;
ll vis[maxn];
ll ans[maxn];
ll cnt = 0;
ll arr[maxn];
int main() {
	scanf("%lld", &n);
	i = 0;
	b = 0;
	while (n) {
		a = n % 10;
		arr[i] = a;
		n /= 10;
		i++;
		b += a;
	}
	b = b % 3;
	if (b == 0)
		printf("0");
	else
	{
		if (b == 1)
		{
			if (i == 1)
				printf("-1");
			else {
				ll ok = 0;
				for (j = 0; j < i; j++)
					if (arr[j] % 3 == 1)
						ok = 1;
				ll x = 0;
				for (j = 0; j < i; j++)
					if (arr[j] % 3 == 2)
						x++;
				if (ok == 1)
					printf("1");
				else if (x >= 2 && i >= 3)
				{
					printf("2");
				}
				else
					printf("-1");
			}
		}
		else
		{
			ll ok = 0;
			for (j = 0; j < i; j++) {
				if (arr[j] % 3 == 2)
					ok = 1;
			}
			ll x = 0;
			for (j = 0; j < i; j++) {
				if (arr[j] % 3 == 1)
					x++;
			}
			if (ok == 1 && i >= 2)
				printf("1");
			else if (x >= 2 && i >= 3)
				printf("2");
			else
				printf("-1");
		}
	}
	return 0;
}

#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <string>
#include <list>
using namespace std;

long long min(long long int a,long long int b){
    if(a>b){
        return b;
    }else{
        return a;
    }
}

long long int max(long long int a,long long int b){
    if(a>b){
        return a;
    }else{
        return b;
    }
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

void f(vector<vector<int>>& a,vector<vector<int>>& b,int N,int K){
    for(int i=0;i<N-1;i++){
        for(int j=i+1;j<N;j++){
            int l=0;
            for(int k=0;k<N;k++){
                if(a[k][i]+a[k][j]>K){
                    break;
                }
                l+=1;
            }
            if(l==N){
                b[i][j]=1;
                b[j][i]=1;
            }
        }
    }
}

void g(vector<vector<int>>& a,vector<vector<int>>& c,int N,int K){
    for(int i=0;i<N-1;i++){
        for(int j=i+1;j<N;j++){
            int l=0;
            for(int k=0;k<N;k++){
                if(a[i][k]+a[j][k]>K){
                    break;
                }
                l+=1;
            }
            if(l==N){
                c[i][j]=1;
                c[j][i]=1;
            }
        }
    }
}

int h(vector<vector<int>>& b,vector<int>& x,int N){
    int M=1;
    vector<int> a(N);
    for(int i=0;i<N;i++){
        a[i]=0;
    }
    stack<int> d;
    for(int i=0;i<N;i++){
        int j=0;
        if(a[i]==0){
            d.push(i);
            a[i]=1;
        }
        while(!d.empty()){
            int k=d.top();
            d.pop();
            j+=1;
            for(int l=0;l<N;l++){
                if(b[k][l]==1 && a[l]==0){
                    d.push(l);
                    a[l]=1;
                }
            }
        }
        if(j>1){
            x.push_back(j);
            M=max(M,j);
        }
    }
    return M;
}

int main(){
    long long int N;
    int max=-1;
    int ans=-1;
    cin>>N;
    vector<long long int> A;
    while(N!=0){
        A.push_back(N%10);
        N=N/10;
    }
    int M=A.size();
    for(long long int k=0;k<(1<<M);k++){
        int S=0;
        int l=0;
        for(int i=0;i<M;i++){
            if(k&(1<<i)){
                l+=1;
                S+=A[i];
            }
        }
        if(S%3==0 && l!=0){
            if(l>max){
                max=l;
            }
        }
    }
    if(max>-1){
        ans=M-max;
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define M 998244353
int binary_expo(int a,int b)
{
    int res=1;
    while(b>0)
    {
        if(b&1>0)
        {
            res=(((res%M)*(a%M))%M);
        }
        b=b>>1;
        a=(((a%M)*(a%M))%M);
    }
    return res;
}
int inv(int a,int b)
{
    a=(((a%M)*(binary_expo(b,M-2)))%M);
    return a;
}
int32_t main()
{
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n;cin>>n;
        vector<int> a(n,0);
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a.begin(),a.end());
        int p=0;
        int i=n-2;
        int s=0;
        while(i>=0)
        {
            s+=((a[i]*(binary_expo(2,p)))%M);
            s%=M;
            p++;
            i--;
        }
        int c=0;
        for(int i=n-1;i>0;i--)
        {
            int k=((s*a[i])%M);
            c+=k;
            c%=M;
            s=((s-a[i-1]+M)%M);
            s=(inv(s,(int)2)%M);
            s%=M;
        }
        for(int i=0;i<n;i++)
        {
            c+=((a[i]*a[i])%M);
            c%=M;
        }
        cout<<c<<endl;

    }
    return 0;
}

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <tuple>

#define ll long long
#define MAX 1000000007
#define mod % 998244353
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

vector<ll> beki;

int main(){
  ll n;
  cin >> n;
  beki.push_back(1);
  rep(i,n+3){
    beki.push_back((beki[i]*2)mod);
  }
  vector<ll> a(n);
  rep(i,n){
    cin >> a[i];
  }
  sort(a.begin(),a.end());
  ll ans = 0;
  ll sum = 0;
  rep(i,n){
    ans = (ans+a[i]*sum) mod;
    sum = (sum*2+a[i]) mod;
    ans = (ans + (a[i]*a[i]))mod;
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

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

struct dijkstra {

  ll N;
  struct edge { ll to, d; };
  vector<vector<edge>> adj;
  vector<ll> minlen;

  dijkstra(ll n) { N = n; adj.resize(N); minlen.assign(N,LLINF); }
  ll operator[](const ll &x) const { return minlen[x]; }

  void add_edge(ll from, ll to, ll d){
    adj[from].push_back({to,d});
  }
  vector<ll> calc(ll n=0) {
    auto c = [](const p_ll &x, const p_ll &y){return x.second>y.second;};
    priority_queue<p_ll, vector<p_ll>, decltype(c)> q(c);
    minlen[n] = 0; q.push(make_pair(n,0));
    while(q.size()) {
      p_ll now = q.top(); q.pop();
      ll np = now.first, nd = now.second;
      if (nd>minlen[np]) continue;
      for (auto x: adj[np]) {
        if (minlen[x.to]<=minlen[np]+x.d) continue;
        q.push(make_pair(x.to, minlen[np]+x.d));
        minlen[x.to] = minlen[np]+x.d;
      }
    }
    return minlen;
  }

};

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

ll R, C;
ll gid(ll i, ll j, bool ura) { return i*C + j + ura*(R*C); }

int main() {
  cin >> R >> C;
  ll A[R][C-1]; rep(i,R) rep(j,C-1) cin >> A[i][j];
  ll B[R-1][C]; rep(i,R-1) rep(j,C) cin >> B[i][j];

  dijkstra ds(R*C*2);
  rep(i,R) rep(j,C-1) {
    ds.add_edge(gid(i,j,false), gid(i,j+1,false), A[i][j]);
    ds.add_edge(gid(i,j+1,false), gid(i,j,false), A[i][j]);
  }
  rep(i,R-1) rep(j,C) {
    ds.add_edge(gid(i,j,false), gid(i+1,j,false), B[i][j]);
    ds.add_edge(gid(i+1,j,true), gid(i,j,true), 1);
  }
  rep(i,R) rep(j,C) {
    ds.add_edge(gid(i,j,false), gid(i,j,true), 1);
    ds.add_edge(gid(i,j,true), gid(i,j,false), 0);
  }

  vector<ll> d = ds.calc();
  ll result = ds[gid(R-1,C-1,false)];
  cout << result << endl;
  return 0;
}

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

#define amax(a, b) a = std::max(a, b)
#define amin(a, b) a = std::min(a, b)

using ll = long long;

int main() {
    ll n; int k;
    std::cin >> n >> k;
    for (int i=0; i<k; i++) {
        std::string ns = std::to_string(n);
        std::sort(ns.begin(), ns.end());
        ll g2 = std::stoll(ns);
        std::reverse(ns.begin(), ns.end());
        ll g1 = std::stoll(ns);
        n = g1 - g2;
    }
    std::cout << n << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

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

int N,M;
int stat(int i, int j){return i*N+j;}

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin>>N>>M;
    vector<vector<pi>> G(N);
    rep(i,M){
        int a,b; char c; cin>>a>>b>>c; a--; b--;
        G[a].emplace_back(b,c-'a');
        G[b].emplace_back(a,c-'a');
    }
    vector<vi> F(N*N);
    rep(i,N)rep(j,N){
        int st = stat(i,j);
        vector<vi> v1(26),v2(26);
        for(auto u:G[i]){
            v1[u.se].pb(u.fi);
        }
        for(auto u:G[j]){
            v2[u.se].pb(u.fi);
        }
        rep(ch,26){
            auto &vv1 = v1[ch];
            auto &vv2 = v2[ch];
            int V1 = vv1.size();
            int V2 = vv2.size();
            rep(k,V1)rep(l,V2){
                int to = stat(vv1[k],vv2[l]);
                F[st].pb(to);
                F[to].pb(st);
            }
        }
    }
    vector<int> dist(N*N,-1); dist[stat(0,N-1)]=0;
    queue<int> que; que.push(stat(0,N-1));
    while(!que.empty()){
        int v = que.front(); que.pop();
        for(auto u:F[v]){
            if (dist[u]!=-1) continue;
            dist[u]=dist[v]+1;
            que.push(u);
        }
    }
    int ans = INT_MAX;
    rep(i,N){
        if (dist[stat(i,i)]!=-1)
            chmin(ans,dist[stat(i,i)]*2);
        for(auto e:G[i]){
            if (dist[stat(i,e.fi)]==-1) continue;
            chmin(ans,dist[stat(i,e.fi)]*2+1);
        }
    }
    if (ans==INT_MAX){
        cout<<"-1\n";
    }else{
        cout<<ans<<endl;
    }
    return 0;
}

//g++  7.4.0

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 1005
vector<pair<ll,char>> adj[MAXN];
map<pair<ll,ll>,ll> vis;
map<pair<ll,ll>,ll> edge;
map<pair<ll,ll>,ll> dis;

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

    ll N,M; cin>>N>>M;
    for(ll i=0;i<M;++i)
    {
        ll u,v; cin>>u>>v;
        char ch; cin>>ch;
        adj[u].push_back({v,ch});
        adj[v].push_back({u,ch});
        edge[{u,v}] = 1;
        edge[{v,u}] = 1;
    }

    queue<pair<ll,ll>> q;
    dis[{1,N}] = 0;
    q.push({1,N});
    vis[{1,N}] = 1;
    ll ans = LLONG_MAX;

    while(!q.empty())
    {
        ll a = q.front().first;
        ll b = q.front().second;
        //cout<<a<<" "<<b<<endl;
        q.pop();

        if(a == b)
        {
            ans = min(ans,2LL * dis[{a,b}]);
            break;
        }

        if(edge[{a,b}])
        {
            ans = min(ans,2LL * dis[{a,b}] + 1);
            continue;
        }

        for(auto it1: adj[a])
        {
            for(auto it2: adj[b])
            {
                ll c = it1.first;
                ll d = it2.first;

                if(!vis[{c,d}] && (it1.second == it2.second))
                {
                    dis[{c,d}] = dis[{a,b}] + 1;
                    vis[{c,d}] = 1;
                    q.push({c,d});
                }
            }
        }
    }

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


#include"bits/stdc++.h"
using namespace std;
#define int long long
int n,m,c[21];
vector<int> g[21];
int dfs(int cur){
	if(cur==n+1)return 1;
	if(g[cur].empty())return dfs(cur+1)*3;
	int ret=0;
	for(int i=1;i<=3;i++){
		int d=0;
		for(int v:g[cur]){
			if(c[v]==i){d=1;break;}
		}
		if(d)continue;
		c[cur]=i;
		ret+=dfs(cur+1);
		c[cur]=0;
	}
	return ret;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1,u,v;i<=m;i++){
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	cout<<dfs(1)<<"\n";
}

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<bitset>
#include<functional>
#include <fstream>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define rep3(i, x, y) for(ll i = x; i < y; i++)
#define rep4(i, x) for(ll i = x; i >= 0; i--)
#define all(a) (a).begin(),(a).end()
#define puts(x) cout << (x) << "\n"
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//const ld EPS = 1e-9;

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (a % b == 0)return b;
	return gcd(b, a % b);
}
//ax + by = cが整数解をもつための必要十分条件は c が gcd(a,b) で割り切れること。
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
//main関数内に extGCD(a, b, x, y); でx, yに解が格納
ll extGCD(ll a, ll b, ll& x, ll& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	ll d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		res += n % 10; n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y, ll m) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= m; }
		x = x * x % m; y /= 2;
	}
	return res;
}
ll kaijyo[600005];
void nCkinit(ll n, ll m) {
	ll cnt = 1; kaijyo[0] = 1;
	for (int h = 1; h <= n; h++) { cnt *= h; cnt %= m; kaijyo[h] = cnt; }
}
ll nCk(ll n, ll k, ll m) {
	/*ll a = 1, b = 1;
	for (int h = 1; h <= n; h++) { a *= h; a %= m; }
	for (int h = 1; h <= k; h++) { b *= h; b %= m; }
	for (int h = 1; h <= n - k; h++) { b *= h; b %= m; }*/
	ll a = kaijyo[n], b = kaijyo[k] * kaijyo[n - k] % m;
	return a * modpow(b, m - 2, m) % m;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
typedef pair <char, ll> pp;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to, cost; };
struct status {
	ll d;
	ll c;
	ll s;

	bool operator<(const status& rhs) const { return d < rhs.d; };
	bool operator>(const status& rhs) const { return d > rhs.d; };
};

ll n, m; vector<vector<ll>> G(22);
vector<bool> visit(22);
vector<ll> dfs(ll k) {
	vector<ll>r; r.push_back(k);
	rep(i, G[k].size()) {
		if (!visit[G[k][i]]) {
			visit[G[k][i]] = true; vector<ll> kr = dfs(G[k][i]);
			rep(j, kr.size()) { r.push_back(kr[j]); }
		}
	}
	return r;
}
vector<vector<bool>> no(22, vector<bool>(3)); vector<ll>v;
ll cnt(ll k) {
	if (k == v.size()) { return 1; }
	ll r = 0;
	rep(j, 3) {
		if (!no[v[k]][j]) {
			vector<ll> kno;
			rep(i, G[v[k]].size()) {
				if (!no[G[v[k]][i]][j]) {
					kno.push_back(i);
					no[G[v[k]][i]][j] = true;
				}
			}
			r += cnt(k + 1);
			rep(i, kno.size()) {
				no[G[v[k]][kno[i]]][j] = false;
			}
		}
	}
	return r;
}

int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	//cout << fixed << setprecision(15);

	//input
	cin >> n >> m;
	rep(i, m) {
		ll a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b); G[b].push_back(a);
	}
	ll ans = 1;
	rep(i, n) {
		if (!visit[i]) {
			visit[i] = true; v = dfs(i); ans *= cnt(0);
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
const int maxn=105;
int a[maxn][maxn];
int b[maxn][maxn];
int cn[maxn];
bool used[maxn];
int cnt;
void dfs(int x)
{
    ++cnt;
    used[x]=true;
    for(int i=0;i<maxn;++i)
    {
        if(b[x][i] && !used[i])
        {
            dfs(i);
        }
    }
}
double c(int n,int k)
{
    double ans=1;
    for(int i=(n-k+1);i<=n;++i) ans*=i;
    for(int i=1;i<=k;++i) ans/=i;
    return ans;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
    {
        string s;
        cin>>s;
        for(int j=0;j<n;++j) {a[i][j]=(s[j]-'0');b[j][i]=a[i][j];}
    }
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<maxn;++j) used[j]=false;
        cnt=0;
        dfs(i);
        cn[i]=cnt;
    }
    double ans=0;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<n;++j)
        {
            ans+=c(n-cn[i],j)/c(n-1,j);
        }
    }
    cout<<setprecision(25)<<ans/(n+0.0);
    return 0;
}

#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
#include <cassert>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++)
#define CONTAINS_MAP(m, a) ((m).find((a)) != m.end())
#define CONTAINS_SET(m, a) ((m).find((a)) != m.end())
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); }
void Yes() { cout << "Yes" << endl; exit(0); }
void No() { cout << "No" << endl; exit(0); }

//---------- セグ木 ----------
const int  SEG_MAX = (1 << 18); //1<<18 = 2^18 = 262144
struct SegTree
{
	vector<int> seg;

	SegTree() : seg(SEG_MAX * 2, 0) {}

	//[a,b]にxを加算 (節点kをベースに[l,r]に足す)
	void add(int a, int b, int x, int k = 0, int l = 0, int r = SEG_MAX - 1)
	{
		if (a <= l && r <= b)
		{
			seg[k] += x;
		}
		else
		{
			int l0 = l;
			int r0 = l + (r - l) / 2;
			int l1 = r0 + 1;
			int r1 = r;
			if (a <= r0)
			{
				int kk = 2 * k + 1;
				add(a, b, x, kk, l0, r0);
			}
			if (l1 <= b)
			{
				int kk = 2 * k + 2;
				add(a, b, x, kk, l1, r1);
			}
		}
	}

	int get(int index)
	{
		int sum = 0;
		int k = SEG_MAX - 1 + index; //根
		while (true)
		{
			sum += seg[k];
			if (k == 0) break;
			k = (k - 1) / 2;
		}
		return sum;
	}
};

struct St
{
	int index;
	int n;
	bool operator < (const St& st) const
	{
		return (index > st.index); //(昇順)
	}
};

int N;
map<int, priority_queue<St>> _m;
int B[200010];

signed main()
{
	cin >> N;
	REP(i, N)
	{
		St st;
		cin >> st.n;
		st.n += i;
		st.index = i;
		_m[st.n].push(st);
	}

	REP(i, N)
	{
		cin >> B[i];
	}

	SegTree offset;

	int ans = 0;
	for (int i = 0; i < N; i++)
	{
		int b = B[i] + i;
		if (_m[b].size() == 0)
		{
			cout << -1 << endl;
			return 0;
		}

		St st = _m[b].top();
		_m[b].pop();

		int a = st.index + offset.get(st.index);
		int sub = a - i;
		ans += sub;

		offset.add(0, st.index, 1);

	//	//for (int i = 0; i < N; i++)
	//	//{
	//	//	cout << offset.get(i) << " ";
	//	//}
	//	//cout << endl;
	}
	cout << ans << endl;
}


#include "bits/stdc++.h"
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define maxn 100000
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef double db;

int main()
{
    int cas; scanf("%d",&cas); while(cas--)
    {
        int n; scanf("%d",&n); vi a(n+1,0);
        rep(i,1,n) scanf("%d",&a[i]);
        if(n&1) puts("Second");
        else
        {
            map<int,int> cnt;
            rep(i,1,n) cnt[a[i]]++;
            bool ok=1;
            for(auto it: cnt) if(it.SE&1) ok=0;
            puts(ok?"Second":"First");
        }
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define ll long long
#define ar array
const ll mxN=2e5;
const int mod=1e9+7;
const ll INF=1e18;
#define what_is(x) cerr << #x << " is " << x << endl;
ll a,b,n,k,m,x,y,h,l,r,t;

void solve(){
	cin>>n;
	string s[n];
	map<string,int> mp;
	for(int i=0;i<n;i++){
		cin>>s[i];
		string s2;
		mp[s[i]]=1;
	}

	for(int i=0;i<n;i++){
		if(s[i][0]!='!'){
			string check='!'+s[i];
			if(mp[check]){
				cout<<s[i]<<endl;
				return;
			}
		}
	}
	cout<<"satisfiable"<<endl;
}

signed main() {
	#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
    #endif
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    cout<<setprecision(12)<<fixed;
    // cin>>t;
    // while(t--)
    	solve();
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define int long long
int read(){
	int w=0,f=1;
	char c=' ';
	while(c<'0'||c>'9')c=getchar(),f=c=='-'?-1:f;
	while(c>='0'&&c<='9')w=w*10+c-48,c=getchar();
	return w*f;
}
int n,a[N];
bool vis[N];
signed main(){
	n=read();
	a[1]=1;
	for(int i=2;i<=n;i++){
		vis[1]=1;
		for(int j=2,k=sqrt(i);j<=k;j++)
			if(i%j==0)vis[a[j]]=vis[a[i/j]]=1;
		for(int j=1;!a[i];j++)
			if(!vis[j])a[i]=j;
		for(int j=2,k=sqrt(i);j<=k;j++)
			if(i%j==0)vis[a[j]]=vis[a[i/j]]=0;
	}
	for(int i=1;i<=n;i++)
		printf("%lld%c",a[i],i==n?'\n':' ');
	return 0;
}

#include <bits/stdc++.h>
#include <cmath>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<long long,long long>;
//ll INF = 1e18;
//int INF = 1e9;
//printf("%.10f\n",ans);

int main() {

  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n) cin >> a[i];
  rep(i,n) a[i] %= 200;
  vector<vector<int>> b(200,vector<int>(0));
  int cnt = min(n,8);
  for(int bit = 1; bit < (1<<cnt); ++bit){
    ll sum = 0;
    vector<int> s;
    for(int i = 0; i < cnt; ++i){
      if(bit & (1<<i)){
        sum += a[i];
        sum %= 200;
        s.push_back(i+1);
      }
    }
    sum %= 200;

    if(b[sum].size() != 0){
      cout << "Yes" << endl;

      int m1 = b[sum].size();
      cout << m1 << " ";
      rep(j,m1) cout << b[sum][j] << " ";
      cout << endl;

      int m2 = s.size();
      cout << m2 << " ";
      rep(j,m2) cout << s[j] << " ";
      return 0;
    }
    b[sum] = s;
  }

  cout << "No" << endl;

  return 0;
}


// Atcoder.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#define _USE_MATH_DEFINES

#include<math.h>
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
#include <sstream>

using namespace std;
typedef long long ll;
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i,n) for(int i=0;i<n;i++)
#define show(s) cout<<s<<endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define LINF (1e18)
#define MOD (1e9+7)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define all(x) (x).begin(),(x).end()
//#define int long long
typedef pair < ll, ll> P;

//元がintの分けたものを返す p[0]は1桁目
vector<int> divnum(ll num) {
	int dig;
	vector<int>p;
	while (num) {
		dig = num % 10;
		p.push_back(dig);
		num /= 10;
	}
	return p;
}
//桁数を返す
int digiter(ll num) {
	int dig;
	vector<int>p;
	while (num) {
		dig = num % 10;
		p.push_back(dig);
		num /= 10;
	}
	return p.size();
}
//元がstringの分けたものを返す d[0]は最高位
vector<int> convertstring(string s) {
	vector<int> d;
	ll n = s.size();
	reverse(s.begin(), s.end());
	rep(i, n) {
		d.push_back(s[i] - '0');
	}
	return d;
}

/*long double　は%Lf*/
//queue古い順,stack新しい
//bool operator<(const Info &another) const {return x > another.x;}//struct(構造体)内の比較演算子のオバーロード
string S; ll M; int d;
int32_t main() {
	cin >> S >> M;
	rep(i, S.size()) {
		chmax(d, S[i] - '0');
	}
	reverse(S.begin(), S.end());

	rep(i, S.size()) {
		if (pow(d + 1, i) > M) {

			cout << 0 << endl;
			return 0;
		}
	}
	ll su = 0;
	rep(i, S.size()) {
		su += ll(S[i] - '0') * pow(d + 1, i);
	}
	if (su > M) {
		cout << 0 << endl;
		return 0;
	}
	//cout << d << endl;
	ll ul = LINF+1LL; ll bl = d + 1;
	ul++;
	while (ul - bl > 1) {
		ll mid = (ul + bl) >> 1LL;
		ll sum = 0;
		rep(i, S.size()) {
			if (pow(mid, i) > M) {
				ul = mid;
				//cout << "work"<<mid << endl;
				break;
			}
		}
		if (ul == mid) {
			continue;
		}
		rep(i, S.size()) {
			sum += ll(S[i] - '0') * pow(mid, i);
			if (sum > M) {
				ul = mid;
				break;
			}

		}

		if (sum <= M) {
			bl = mid;
		}
		else {
			ul = mid;
		}
	}
	if (S.size() == 1) {
		cout << min(1LL, bl - d) << endl;
		return 0;
	}
	cout << bl - d << endl;

}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define nl cout<<endl

#define pii pair<ll,ll>
#define vi  vector<ll>
#define vii vector<pii>
#define mi  map<ll,ll>
#define all(a)  (a).begin(),(a).end()

#define pb push_back
#define ff first
#define ss second
#define hell 1000000007

#define test4(x,y,z,a) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<"		a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<"		y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
#define N 100009

ll power(ll a,ll b,ll m)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}
ll dist[N];
bool vis[N];
//ll parent[N];
vector<pair<ll,pii>> v[N];
void initialise()
{
	rep(i,1,N)
	{
		//parent[i]=-1;
		v[i].clear();
		dist[i]=LLONG_MAX;
		vis[i]=0;
	}
	return ;
}
ll dijkstra(ll src,ll dest)
{
	vis[src]=1;
	multiset<pii>s;
	s.insert({0,src});
	dist[src]=0;

	while(s.size())
	{
		auto it=s.begin();
		ll d=it->ff;
		ll num=it->ss;
		s.erase(it);

		vis[num]=1;
		rep(i,0,v[num].size())
		{

			if(vis[v[num][i].ff])
			continue;

			// time for this

			ll tm;
			if(dist[num] % v[num][i].ss.ss == 0) {
				tm = dist[num] + v[num][i].ss.ff;
			}
			else {
				ll mc = dist[num]/ v[num][i].ss.ss;
				mc ++;
				mc *= v[num][i].ss.ss;

				tm = mc - dist[num];
				tm += v[num][i].ss.ff;
				tm += dist[num];
			}
			//tm += dist[num];

			if(tm >= dist[v[num][i].ff])
			continue;

			dist[v[num][i].ff]=tm;
			//parent[v[num][i].ff]=num;
			s.insert({tm,v[num][i].ff});
		}
	}
	return dist[dest];
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	initialise();
	ll n,m,source,dest;
	cin >> n >> m >> source >> dest;

	rep(i,1,m+1) {
		ll l,r;cin>> l >> r;
		ll a,b;cin>> a>> b;		// a is the time and b is the multiples value
		v[l].pb({r,{a,b}});
		v[r].pb({l,{a,b}});
	}

	dijkstra(source,dest);
	/*rep(i,1,n+1) {
		test2(i,dist[i]);
	}
	test1(vis[dest]);*/
	if( vis[dest])
		cout << dist[dest] << endl;
	else
		cout << -1 << endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;

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

    for (int i = 0; i < n; i++) {
        int a = (i * 2 + 0) % n;
        int b = (i * 2 + 1) % n;
        cout << a + 1 << ' ' << b + 1 << '\n';
    }

    return 0;
}

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

#define fastio() ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define ll long long int
#define loop(i,k,n) for(i=k; i<n; i++)
#define rloop(i,n,k) for(i = n-1; i >= k; i--)
#define trv(x,a) for(auto& x:a)
#define all(x) x.begin(),x.end()
#define between(v,x,y) v.begin()+x,v.begin()+y
#define pb push_back
#define mp make_pair
#define sz(x) x.size()
#define clr(x) memset(x,0,sizeof(x))
#define oparr(v) for(auto x:v) cout << x << " " ; cout << '\n'
#define f first
#define s second

template<class T> void inp(T& x) {
	cin >> x;
}
template<class H, class... T> void inp(H& h, T&... t) {
	inp(h);
	inp(t...);
}
template<class A> void inp(vector<A>& x) {
	trv(a, x)
		inp(a);
}
void deb_out() { cerr << endl; }
template<typename Head, typename... Tail> void deb_out(Head H, Tail... T) { cerr << ' ' << H; deb_out(T...); }
#define deb(...) cerr << "(" << #__VA_ARGS__ << "):", deb_out(__VA_ARGS__)

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vll;

const ll mod = 1e9 + 7;
const char nl = '\n';
const int N = 1e6;
const ll lim = 2e7 + 1;

#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define msb(n) __builtin_clz(n)

void solve(){
	int i,j;
	int a,b,c;
	inp(a,b,c);
	if(a == b) cout << c;
	else if(b == c) cout << a;
	else if(a == c) cout << b;
	else cout << 0;
}

int main(){

	fastio();
	int t = 1;
    //inp(t);
	int x = t;
    while(t--){
		//cout << "Case #" << x-t << ": ";
		solve();
	}
 return 0;
}


//Think simple yet elegant.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll  long long
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
#define REP(i,n) for(int i=0;i<n;i++)
const ll maxn=100050;
const ll mod=1e9+7;

int main(){
    fast;
	int n,m,i,j,k,u,v,mask;
	cin >> n >> m;
	vector<set<int>> g(20);
	for(i=0;i<m;i++){
		cin >> u >> v;
		--u,--v;
		g[u].insert(v);
		g[v].insert(u);
	}
	vector<bool> can(1<<n,true);
	for(mask=0;mask<(1<<n);++mask){
		vector<int> str;
		for(j=0;j<n;j++){
			if(mask&(1<<j))
				str.pb(j);
		}
		for(i=0;i<str.size();++i){
			for(j=i+1;j<str.size();++j){
				if(g[str[i]].count(str[j])==false){
					can[mask]=false;
					break;
				}
			}
		}
	}
	vector<int> dp(1<<n,1e9);
	dp[0]=0;
	//dp[mask]-> minimum number of connected components formed from mask subset of nodes.
	for(i=0;i<(1<<n);i++){
		if(can[i])
			dp[i]=1;
		else{
			for(j=i;j;j=(j-1)&i){
				dp[i]=min(dp[i],dp[j]+dp[i^j]);
			}
		}
	}
	cout<<dp[(1<<n)-1];
}

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

struct fastio{fastio(){cin.tie(nullptr);ios_base::sync_with_stdio(false);std::cout<<std::fixed<<setprecision(10);}}oitsaf;
#define rep(i,n) for(int i=0;i<int(n);++i)
constexpr int inf = 1<<28;
constexpr char newl = '\n';
template<class T>std::istream&operator>>(std::istream&is,std::vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T>std::ostream&operator<<(std::ostream&os,std::vector<T>const&v){for(auto const& vi:v)os<<vi<<" ";return os;}
#if _local
struct debug_structure{template<class T>debug_structure&operator<<(const T&elm){std::cerr<<elm<<std::flush;return*this;}}dout;
#else
struct debug_structure{template<class T>debug_structure&operator<<(const T&elm){return*this;}}dout;
#endif
template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a] |= 1 << b;
    adj[b] |= 1 << a;
  }
  vector<int> dp(1 << n, inf);
  rep(S, 1 << n) {
    bool kn = true;
    rep(i, n) if ((S >> i) & 1) kn &= ((adj[i] | 1 << i) & S) == S;
    if (kn)
      dp[S] = 1;
  }
  rep(S, 1 << n) {
    for (int T = S;; T = (T - 1) & S) {
      if (T == 0)
        break;
      chmin(dp[S], dp[S ^ T] + dp[T]);
    }
  }
  cout << (dp.back()) << endl;
  rep(i, 1 << n) dout << i << ":" << bitset<10>(i) << ":" << dp[i] << newl;
}


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

const ll N = 100000000;

string S[3];
string T;
ll mp[N];

ll g(string S){
	ll ret = 0;
	for(int i = 0; i<S.size(); i++){
		ret*=10;
		ret+=mp[S[i]];
	}
	return ret;
}

bool f(vector<int>  v){
	for(int i = 0; i<T.size(); i++){
		mp[T[i]] = v[i];
	}
	for(int i = 0; i<3; i++){
		if(mp[S[i][0]]==0) return false;
	}
	if(g(S[0])+g(S[1])==g(S[2])){
		cout<<g(S[0])<<'\n'<<g(S[1])<<'\n'<<g(S[2])<<'\n'; return true;
	}
	return false;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	for(int i = 0; i<3; i++){
		cin>>S[i];
		T+=S[i];
	}
	sort(T.begin(), T.end());
	T.erase(unique(T.begin(), T.end()), T.end());
	if(T.size()>10){
		cout<<"UNSOLVABLE\n";
		return 0;
	}
	vector<int> v;
	for(int i = 0; i<10; i++) v.push_back(i);
	do{
		if(f(v)) return 0;
	} while(next_permutation(v.begin(), v.end()));
	cout<<"UNSOLVABLE\n";
	return 0;
}

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

using namespace std;
using ll = long long;

int main(){
  string S1,S2,S3; cin>>S1>>S2>>S3;
  set<char> se;
  for(auto c:S1) se.insert(c);
  for(auto c:S2) se.insert(c);
  for(auto c:S3) se.insert(c);

  if (se.size() > 10){
    cout<<"UNSOLVABLE\n";
    return 0;
  }
  int N = se.size();

  map<char,int> mp;
  int idx=0;
  for(auto c:se){
    mp[c] = idx++;
  }

  vector<int> v(10);
  for(int i=0; i<10; i++) v[i]=i;
  auto calc = [&](char c){
    return v[mp[c]];
  };
  do{
    if (calc(S1[0])==0||calc(S2[0])==0||calc(S3[0])==0){
      continue;
    }
    ll A=0,B=0,C=0;
    for(int i=0; i<S1.size(); i++){
      A=A*10+calc(S1[i]);
    }
    for(int i=0; i<S2.size(); i++){
      B=B*10+calc(S2[i]);
    }
    for(int i=0; i<S3.size(); i++){
      C=C*10+calc(S3[i]);
    }
    if (A+B==C){
      cout<<A<<endl;
      cout<<B<<endl;
      cout<<C<<endl;
      return 0;
    }
  }while(next_permutation(v.begin(), v.end()));
  cout<<"UNSOLVABLE\n";
  return 0;
}


#include<cstdio>
#include<iostream>
int n,bj,q;
char c[400005];
int read()
{
	int s=0;
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') s=s*10+c-'0',c=getchar();
	return s;
}
int main()
{
	scanf("%d",&n);
	getchar();
	for(int i=1;i<=2*n;i++)
	c[i]=getchar();
	scanf("%d",&q);
	for(int i=1,az,x,y;i<=q;i++)
	{
		az=read();x=read();y=read();
		if(az==1) std::swap(c[(x+bj*n-1)%(2*n)+1],c[(y+bj*n-1)%(2*n)+1]);
		else bj^=1;
	}
	if(bj)
	{
		for(int i=n+1;i<=2*n;i++) putchar(c[i]);
		for(int i=1;i<=n;i++) putchar(c[i]);
	}
	else
	{
		for(int i=1;i<=n;i++) putchar(c[i]);
		for(int i=n+1;i<=2*n;i++) putchar(c[i]);
	}
}

#include <iostream>
#include <climits>
#include <cmath>
using namespace std;

int main(){

  int n;
  cin>>n;
  char s[2*n];
  cin>>s;
  int q;
  cin>>q;
  int count=0;

  for(int i=0;i<q;i++){
  	int t,a,b;

  	cin>>t>>a>>b;
  	if(t==1){
  		a--;
  		b--;
  		if(count%2!=0){
  			if(a>=n && b>=n){
  				swap(s[a-n],s[b-n]);
			  }
			  else if(a<n && b<n){
			  	swap(s[a+n],s[b+n]);
			  }
			  else if(a<n && b>=n){
			  	swap(s[a+n],s[b-n]);
			  }
			  else if(a>=n && b<n){
			  	swap(s[a-n],s[b+n]);
			  }

		  }
		  else swap(s[a],s[b]);
	  }
	else{
	  	count++;
	  }
  }
  if(count%2==0){

  }
  else{
  	for(int i=0;i<n;i++){
  		swap(s[i],s[i+n]);
	  }
  }
  cout<<s;

}


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

#define dbg(v) cout << __LINE__ << ": " << #v << " = " << v << endl

const int N = 2e3 + 5;
const int M = 1e9 + 7;

char str[N];

int add (int a, int b) {
    return (a + b < M) ? (a + b) : (a + b - M);
}

int main() {
    int h, w;
    scanf("%d %d", &h, &w);
    vector <string> s(h);
    for (string &v : s) {
        scanf(" %s", str);
        v = str;
    }
    vector <int> r(h), c(w), d(h + w);
    int ans = 0;
    for (int i = h - 1; i >= 0; i--) {
        for (int j = w - 1; j >= 0; j--) {
            if (i == h - 1 and j == w - 1) {
                r[i] = 1;
                c[j] = 1;
                d[i - j + w] = 1;
                continue;
            }
            if (s[i][j] == '#') {
                r[i] = c[j] = d[i - j + w] = 0;
            }
            else {
                ans = 0;
                ans = add(ans, r[i]);
                ans = add(ans, c[j]);
                ans = add(ans, d[i - j + w]);
                r[i] = add(r[i], ans);
                c[j] = add(c[j], ans);
                d[i - j + w] = add(d[i - j + w], ans);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define flush cout.flush

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, m;
    cin >> n >> m;
    vector<vector<char>> mx(n, vector<char>(m));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> mx[i][j];
        }
    }
    ll res = 1;
    for (int i = 0; i < n + m - 1; ++i) {
        bool red = false, blue = false;
        for (int j = 0; j < m; ++j) {
            if (i - j < 0)continue;
            if (i - j >= n)continue;
            if (mx[i - j][j] == 'R') {
                red = true;
            }
            if (mx[i - j][j] == 'B') {
                blue = true;
            }
        }
        if (red && blue) {
            cout << 0 << "\n";
            return 0;
        }
        if (!red && !blue) {
            res *= 2;
            res %= mod2;
        }
    }
    cout << res << "\n";
    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::chrono;
using namespace std;
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define loop(i, start, end) for (int i = start; i <= end; i++)
#define rloop(i, start, end) for (int i = start; i >= end; i--)
#define read(a,n) loop(i,0,n-1) cin>>a[i];
#define vi vector<int>
#define vpii vector<pair<int,int>>
#define pii pair<int,int>
#define vec(x) vector<x>
#define sz(v) (int)v.size()
#define UB upper_bound
#define LB lower_bound
#define all(v) (v).begin(),(v).end()
#define arl(v) (v).rbegin(),(v).rend()
#define fsp(a) fixed<<setprecision(a)
#define mem(a,with) memset(a, with, sizeof (a))
#define vmn(a) (*min_element(a.begin(),a.end()))
#define vmx(a) (*max_element(a.begin(),a.end()))
#define bs(a,key) binary_search(all(a),key)    /// return bool.
#define rotl(a,x) rotate(a.begin(),a.begin()+x,a.end());
#define rotr(a,x) rotl(a,a.size()-x);
#define nl cout<<"\n"
#define dbg(x) cerr << #x << " :: " << x << endl;
#define dbg2(x, y) cerr << #x << " :: " << x << "\t" << #y << " :: " << y << endl;
#define MOD 1000000007
#define f first
#define s second
int add(int x, int y) {int res = x + y; return (res >= MOD ? res - MOD : res);}
int mul(int x, int y) {int res = x * y; return (res >= MOD ? res % MOD : res);}
int power(int x, int y) {int res = 1; x %= MOD; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}

void solve(){
    //code goes here
    int n; cin>>n;
    int a[n]; read(a,n);
    int b[n]; read(b,n);

    int ans=0;
    loop(i,0,n-1){
        ans+=(a[i]*b[i]);
    }
    if(ans==0) cout<<"Yes";
    else cout<<"No";
}

int32_t main()
{
auto start = chrono::high_resolution_clock::now();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
    int t=1;
    // cin>>t;
    while (t--)
    {
        solve();
    }
#ifndef ONLINE_JUDGE
    auto stop = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<microseconds>(stop - start);
    cout<<"\n\nExecuted In: "<<duration.count()<<" ms";
#endif
    return 0;
}

#include <iostream>

using namespace std;

int n ;

struct pr
{

    int x, y ;

}
v[100005];

int main()
{

    int s = 0 ;

    cin >> n ;

    for (int i = 1 ; i <= n ; i ++)
        cin >> v[i].x ;

    for (int i = 1 ; i <= n ; i ++)
        cin >> v[i].y ;

    for (int i = 1 ; i <= n ; i ++)
        s = s + (v[i].x * v[i].y) ;

    if (s != 0)
        cout << "No" ;

    else
        cout << "Yes" ;

    return 0 ;

}


#include <bits/stdc++.h>
using namespace std;using ll=long long;
using vl=vector<ll>;using vi=vector<int>;
using vs=vector<string>;using vc=vector<char>;
using vvl=vector<vl>;using pl=pair<ll,ll>;
using vvc=vector<vc>;using vd=vector<double>;
using vpl=vector<pl>;using vb=vector<bool>;
#define rep(i,r) for(ll i=0;i<(r);i++)
#define Rep(i,l,r) for(ll i=(l);i<(r);i++)
#define print(n) cout<<(n)<<endl
#define all(n) n.begin(),n.end()
#define sortp(d) sort(all(d)) //1234
#define sortm(d) sort(d.rbegin(),d.rend()) //4321
/*-----------順列生成-----------------
do
{
}while(next_permutation(all(A)));
*/
//'A'65,'a'97
//cout<<fixed<<setprecision(9)<<;
ll gcd(ll a,ll b) {if(b==0)return a;else return gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll modpow(ll a,ll n,ll mod) {ll res=1;while(n>0)
{if(n&1)res=res*a%mod;a=a*a%mod;n >>= 1;}return res;}
ll modinv(ll a,ll mod){return modpow(a, mod - 2, mod);}

ll p=0,q=0,r=0;
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll inf=1000000000000; //10^12:∞
ll mod=1000000007; //10^9+7:合同式の法
struct edge{ll to,cost;};
const double pi = acos(-1);

int main()
{
    ll n;cin>>n;map<ll,ll>m;
    for (ll i = 2; i * i <= n; i++)
    {
        q=i;
        rep(j,100)
        {
            q*=i;if(q>n)break;
            m[q]++;
            //print(q);
        }
    }
    print(n-m.size());
}

#include <bits/stdc++.h>

using namespace std;

//#define MULTITEST

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e8);
const li INF64 = li(1e18);
const int MOD = int(1e9) + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

int n, m;

bool read () {
	if (scanf("%d%d", &n, &m) != 2)
		return false;
	return true;
}

void solve() {
	if (m == 0){
		forn(i, n) printf("%d %d\n", 2 * i + 1, 2 * i + 2);
		return;
	}

	if (m < 0 || m >= n - 1){
		puts("-1");
		return;
	}

	vector<pt> ans;
	ans.pb(mp(0, 2 * m + 2));
	forn(i, m) ans.pb(mp(2 * i + 1, 2 * i + 2));
	ans.pb(mp(2 * m + 1, 2 * m + 3));

	int lst = 2 * m + 3;
	while (sz(ans) < n){
		ans.pb(mp(lst + 1, lst + 2));
		lst += 2;
	}

	for (auto it : ans)
		printf("%d %d\n", it.x + 1, it.y + 1);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

#ifdef MULTITEST
	int tc;
	scanf("%d", &tc);
	while(tc--){
		read();
#else
	while(read()) {
#endif
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int main()
{
    ll n , i , j ; cin>>n ;
    ll a[n] ;
    for(i=0;i<n;i++){
        cin>> a[i] ;
    }
    ll stt=0 , st=0 , s=0  ;
    for(i=0;i<n;i++){
        stt+= i*a[i]*a[i] - 2*s* a[i]+st ;
        s+=a[i] ;
        st+= a[i]*a[i] ;
    }
    cout<< stt <<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+3;
template <typename T>
inline void read(T &x){
    x=0;char ch=getchar();bool f=false;
    while(!isdigit(ch)) f|=ch=='-',ch=getchar();
    while(isdigit(ch))  x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=f?-x:x;
}
template <typename T>
inline void print(T x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) print(x/10);
    putchar(x%10^48);
}
int n;
ll tong[405];
ll ans;
int main(){
    read(n);
    for(register int i=1;i<=n;++i){
        int tep;read(tep);
        tong[tep+200+1]++;
    }
    for(register int i=1;i<=401;++i){
        for(register int j=i+1;j<=401;++j){
            ll tot=tong[i]*tong[j];
            ans+=tot*(j-i)*(j-i);
        }
    }
    print(ans);
    return 0;
}


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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

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

void func(std::string S){

    deque<char> T;
    lli rev = 0;

    for(auto &c:S){
        if(c == 'R'){
            rev++;
        }
        else{
            if(rev % 2 == 0){
                T.push_back(c);
                if(T.size()>=2){
                    if(T[T.size()-1]==T[T.size()-2]){
                        T.pop_back();
                        T.pop_back();
                    }
                }
            }
            else{
                T.push_front(c);
                if(T.size()>=2){
                    if(T[0]==T[1]){
                        T.pop_front();
                        T.pop_front();
                    }
                }
            }
        }
    }

    string s;
    REP(i,0,T.size()){
        if(rev%2==0)s.push_back(T[i]);
        else s.push_back(T[T.size()-1-i]);
    }
    cout<<s<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    std::string S;
    std::cin >> S;
    func(S);
    return 0;
}

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

#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 ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())

using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;

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

const ll ZER = 0;
const ll MOD = 998244353;
const ll INF = 1e9;

int main(){
    int n;
    cin >> n;
    double res = 0;
    REP(i, 1, n - 1){
      res += (double)1.0 / (double)i;
    }
    cout << fixed << setprecision(10) << (double) n * res<< endl;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200010, mod = 1000000007;
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
struct Seg{
	int tot,s[maxn*100],v[maxn*100],rt,ls[maxn*100],rs[maxn*100];
	void psp(int k) {s[k]=s[ls[k]]+s[rs[k]],v[k]=v[ls[k]]+v[rs[k]];}
	void ins(int&k,int l,int r,int p,int VL,int VL2)
	{
		if(!k) k=++tot;
		if(l==r) return s[k]+=VL,v[k]+=VL2,void();int mid=l+r>>1;
		p>mid?ins(rs[k],mid+1,r,p,VL,VL2):ins(ls[k],l,mid,p,VL,VL2),psp(k);
	}
	int asks(int k,int l,int r,int L,int R)
	{
		if(l>R||L>r||!k) return 0;
		if(L<=l&&r<=R) return s[k];int mid=l+r>>1;
		return asks(ls[k],l,mid,L,R)+asks(rs[k],mid+1,r,L,R);
	}
	int askv(int k,int l,int r,int L,int R)
	{
		if(l>R||L>r||!k) return 0;
		if(L<=l&&r<=R) return v[k];int mid=l+r>>1;
		return askv(ls[k],l,mid,L,R)+askv(rs[k],mid+1,r,L,R);
	}
}s1,s2;
int ans,n,m,Q,a[maxn],b[maxn];
signed main()
{
	n=read(),m=read(),Q=read();
	s1.ins(s1.rt,0,1e8,0,0,n);
	s2.ins(s2.rt,0,1e8,0,0,m);
	for(int i=1,x,y;i<=Q;cout<<ans<<'\n',i++)
		if(read()==1)
		{
			x=read();
			ans-=s2.asks(s2.rt,0,1e8,a[x]+1,1e8)+a[x]*s2.askv(s2.rt,0,1e8,0,a[x]);
			s1.ins(s1.rt,0,1e8,a[x],-a[x],-1);
			a[x]=read();
			s1.ins(s1.rt,0,1e8,a[x],a[x],1);
			ans+=s2.asks(s2.rt,0,1e8,a[x]+1,1e8)+a[x]*s2.askv(s2.rt,0,1e8,0,a[x]);
		}
		else
		{
			x=read();
			ans-=s1.asks(s1.rt,0,1e8,b[x]+1,1e8)+b[x]*s1.askv(s1.rt,0,1e8,0,b[x]);
			s2.ins(s2.rt,0,1e8,b[x],-b[x],-1);
			b[x]=read();
			s2.ins(s2.rt,0,1e8,b[x],b[x],1);
			ans+=s1.asks(s1.rt,0,1e8,b[x]+1,1e8)+b[x]*s1.askv(s1.rt,0,1e8,0,b[x]);
		}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define int long long int
#define watch(x) cout << (#x) << " is " << (x) << endl
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define inf 1e17
vector<vector<int>> grid;
int n, m;
int dp[2005][2005];
int helper(int r, int c){
  if(r == n-1 && c == m-1)
    return 0;
  if(dp[r][c] != -1)
    return dp[r][c];
  int op1 = -inf, op2 = -inf;
  if(r+1 < n)
   op1 = grid[r+1][c] - helper(r+1, c);
  if(c+1 < m)
    op2 = grid[r][c+1] - helper(r, c+1);
  return dp[r][c] = max(op1, op2);
}
void solve(int T) {
  cin >> n >> m;
  grid.assign(n, vector<int>(m, 0));
  for(int i = 0 ; i < n ; i++){
    string s;
    cin >> s;
    for(int j = 0 ; j < m ; j++){
      grid[i][j] = (s[j]=='+'?1:-1);
    }

  }
  memset(dp, -1, sizeof dp);
  int ans = helper(0, 0);
  if(ans > 0){
    cout << "Takahashi" <<endl;
  }else if(ans < 0){
    cout <<"Aoki"<<endl;
  }else{
    cout <<"Draw"<<endl;
  }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

#ifndef ONLINE_JUDGE
    freopen("inputf.txt", "r", stdin);
    freopen("outputf.txt", "w", stdout);
#endif

    int T = 1;
    //cin >> T;
    for (int t = 1; t <= T; t++) {
        solve(t);
    }

    cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int mod = 1000000007;

LL _pow(LL a, LL b) {
  if(!b) return 1;
  if(b == 1) return a;
  if(b == 2) return a * a % mod;
  if(b & 1) {
    return a * _pow(a,b-1) % mod;
  }
  return _pow(_pow(a,b/2),2);
}

int main() {
  int n,m;
  S2(n,m);
  int num = n + m;
  int den = n;
  rep(i,0,n) {
    int x;
    S(x);
    den += x;
  }
  if(den > num) {
    P(0);
    return 0;
  }
  LL ans = 1;
  rep(i,1,den+1) {
    ans = ans * (num + 1 - i) % mod;
    ans = ans * _pow(i, mod - 2) % mod;
  }
  cout << ans << "\n";
  return 0;
}

#include <bits/stdc++.h>

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

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

constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

constexpr int MOD = 1e9 + 7;

long long inv(long long n, long long mod) {
    long long res = 1;
    while (n > 1) {
        (res *= (mod - mod / n)) %= mod;
        n = mod % n;
    }
    return res;
}

long long comb_mod(long long n, long long r, long long mod) {
    r = (r <= n/2) ? r : n-r;
    if (r < 0) return 0;
    if (r == 0) return 1;

    long long nume = 1, deno = 1;
    for (long long i=1; i<=r; i++) {
        deno *= i % mod;
        deno %= mod;
    }
    for (long long i=n-r+1; i<=n; i++) {
        nume *= i % mod;
        nume %= mod;
    }
    deno = inv(deno, mod);
    return (deno * nume) % mod;
}

void solve() {
    int N, M;
    cin >> N >> M;
    vector<int> A(N);
    for (int i=0; i<N; ++i) cin >> A[i];

    int sumA = 0;
    for (int x : A) sumA += x;
    cout << comb_mod(M + N, sumA + N, MOD) << '\n';
}

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

    solve();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ff first
#define ss second
#define INF 1000000000000000000
#define pb push_back
#define vl vector<ll>
#define vi vector<int>
#define sz(a) (ll)a.size()
#define all(v) v.begin(), v.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r)
{
    uniform_int_distribution<int> uid(l, r);
    return uid(rng);
}
ll power(ll b, ll e, ll m) {
    if (e == 0) return 1;
    if (e & 1) return b * power(b * b % m, e / 2, m) % m;
    return power(b * b % m, e / 2, m);
}
ll power( ll b, ll e)
{
    if (e == 0) return 1;
    if (e & 1) return b * power(b * b, e / 2);
    return power(b * b, e / 2);
}
ll modI(ll a, ll m)
{
    ll m0 = m, y = 0, x = 1;
    if (m == 1)return 0;
    while (a > 1) {
        ll q = a / m;
        ll t = m; m = a % m;
        a = t; t = y;
        y = x - q * y; x = t;
    }
    if (x < 0) {x += m0;}
    return x;
}
const int N = 2e5 + 5;
ll a, b;
ll ncr[61][31];
ll nCr(ll n, ll r) {
    if (r > n or r < 0) {return 0;}

    if (n == r || r == 0) {return 1;}
    if (r == 1 || r == n - 1) {return n;}
    if (ncr[n][r] != -1) {return ncr[n][r];}
    ncr[n][r] = nCr(n - 1, r) + nCr(n - 1, r - 1);
    return ncr[n][r];
}

// if our approach is correct p = ans and k =1
ll f(ll k) {
    // find the number of numbers less than k with a 0s and b 1s

    string curr = "";
    int num_zeroes = 0;
    for (int i = 0; i < a + b; i++) {
        if (k & (1LL << i)) {
            curr += '1';
        }
        else {
            curr += '0';
            num_zeroes++;
        }
    }
    reverse(all(curr));
    ll zeroes_used = 0;
    ll ans = 0;
    if (num_zeroes == a) {
        ans++;
    }
    // ll num_ones = 0;
    for (ll i = 0; i < a + b; i++) {
        if (zeroes_used > a) {break;}
        if (curr[i] == '1') {
            // use zero here
            if (a < zeroes_used + 1) {break;}
            ans += nCr(a + b - i - 1, a - zeroes_used - 1);
        }
        else {zeroes_used++;}
    }
    return ans;
}
void print(ll x) {
    for (int i = 59; i >= 0; i--) {
        if (x & (1LL << i)) {
            cout << 1;
        }
        else {cout << 0;}
    }
    cout << "\n";
    return;
}
void solve() {
    memset(ncr, -1, sizeof(ncr));
    ll k;
    cin >> a >> b >> k;
    ll l = 0, r = (1LL << 60);
    // print(r - 1);
    // for (int i = 0; i < b; i++) {
    //     r |= (1LL << (a + i));
    // }
    // r++;
    ll ans = -1;
    while (l <= r) {
        ll mid = (l + r) >> 1;
        ll g = f(mid);
        // cout << mid << " " << g << "\n";
        if (g >= k) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    // cout << ans << "\n";
    string out = "";
    for (int i = 0; i < a + b; i++) {
        if (ans & (1LL << i)) {
            out += 'b';
        }
        else {
            out += 'a';
        }
    }
    reverse(all(out));
    cout << out;
}

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

    // freopen("test_input.txt", "r", stdin);
    // freopen("test_output.txt", "w", stdout);
    int t = 1;
    // cin >> t ;
    // cout << t % 3 << "\n";
    // return 0;
    for (int ntc = 1; ntc <= t; ntc++) {
        // cout << "Case #" << ntc << ": ";
        solve();
    }
}

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

inline ll read()
{
	ll f=1,x=0;char s=getchar();
	while(s<'0'||s>'9')
	{
		if(s=='-') f=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9')
	{
		x=x*10+s-48;
		s=getchar();
	}
	return f*x;
}

ll C[100][100];

int main()
{
	int a=read(),b=read();
	int n=a+b;
	ll k=read();
	for(int i=0;i<=n;i++) C[i][i]=C[i][0]=1;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++)
			C[j][i]=C[j-1][i-1]+C[j-1][i];
	for(int i=1;i<=n;i++)
	{
//		cout<<a<<" "<<b<<" "<<k<<endl;
		if(a==0)
		{
			for(int j=1;j<=b;j++) putchar('b');
			break;
		}
		if(b==0)
		{
			for(int j=1;j<=a;j++) putchar('a');
			break;
		}
		ll res=C[a+b-1][a-1];
//		cout<<res<<endl;
		if(k>res)
		{
			putchar('b');
			k-=res;
			b--;
		}
		else
		{
			putchar('a');
			a--;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rrepi(i,a,b) for(int i=((int)(b)-1);i>=(a);--i)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) rrepi(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define ZERO(x) memset(x,0,sizeof(x))
#define MINUS(x) memset(x,-1,sizeof(x))
#define UNIQUE(x) (x).erase(unique(ALL(x)), (x).end())
#define TOUPPER(x) transform(ALL(x), (x).begin(), ::toupper)
#define TOLOWER(x) transform(ALL(x), (x).begin(), ::tolower)
#define debug(var)  do{std::cout << #var << " : "; view(var);}while(0)
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){for(const auto& v : vv){ view(v); } }
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void viewarr(T arr[], int n) { for(int i = 0; i < n; ++i) std::cout << arr[i] << " "; std::cout << std::endl; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll qp(ll a,ll b){ll ans=1;do{if(b&1)ans=1ll*ans*a;a=1ll*a*a;}while(b>>=1);return ans;}
ll qp(ll a,ll b,ll mo){ll ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const int INF = 1e9;
const ll INFL = 1e18;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5;

struct Dijkstra {
    struct edge{
        int to, cost;
        edge(int to, int cost) : to(to), cost(cost) {}
    };

    int V;
    vector<vector<edge>> G;
    vector<ll> d;
    vector<int> prev;

    Dijkstra(int n) : V(n), G(n), d(n, INFL), prev(n, -1) {}

    void set(int from, int to, int cost) {
        G[from].push_back(edge(to, cost));
    }

    void calc(int s) {
        priority_queue<P, vector<P>, greater<P>> que;
        d[s] = 0;
        que.emplace(0, s);

        while(!que.empty()) {
            P p = que.top(); que.pop();
            int v = p.se;
            if(d[v] < p.fi) continue;
            for(edge e : G[v]) {
                if(d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    que.emplace(d[e.to], e.to);
                    prev[e.to] = v;
                }
            }
        }
    }

    ll dist(int to) {
        return d[to];
    }

    vector<int> get_path(int t) {
        vector<int> path;
        for(; t != -1; t = prev[t]) path.push_back(t);
        reverse(ALL(path));
        return path;
    }
};

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

    vector<vector<ll>> G(K, vector<ll>(K, INFL));
    rep(i,K) {
        Dijkstra d(N);
        rep(j,M) {
            d.set(A[j]-1, B[j]-1, 1);
            d.set(B[j]-1, A[j]-1, 1);
        }
        d.calc(C[i]-1);
        rep(j,K) G[i][j] = d.dist(C[j]-1);
    }

    vector<vector<ll>> dp(1 << K, vector<ll>(K, INFL));
    rep(i,K) dp[1<<i][i] = 0;
    for(int s = 0; s < 1 << K; s++) {
        for(int v = 0; v < K; v++) {
            for(int u = 0; u < K; u++) {
                if(s >> v & 1)chmin(dp[s][v], dp[s & ~(1<<v)][u] + G[u][v]);
            }
        }
    }

    ll ans = INFL;
    rep(i,K) chmin(ans, dp[(1 << K) -1][i] + 1);
    cout << (ans == INFL ? -1 : ans) << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
const int K = 20;
int n, m, k, a[K], dis[K][N], dp[K][1 << K];
vector<int> adj[N];
void bfs(int idx)
{
    queue<int> q;
    q.push(a[idx]);
    for (int i = 1; i <= n; ++i)
        dis[idx][i] = -1;
    dis[idx][a[idx]] = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int v: adj[u])
        {
            if (dis[idx][v] == -1)
            {
                dis[idx][v] = dis[idx][u] + 1;
                q.push(v);
            }
        }
    }
}
int solve(int last, int mask)
{
    if (mask == (1 << k) - 1)
        return dp[last][mask] = 0;
    if (dp[last][mask] != -1)
        return dp[last][mask];
    int cur = INT_MAX;
    for (int i = 0; i < k; ++i)
    {
        if (mask & (1 << i))
            continue;
        cur = min(cur, dis[last][a[i]] + solve(i, mask | (1 << i)));
    }
    return dp[last][mask] = cur;
}
int main(int argc, char const *argv[])
{
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cin >> k;
    for (int i = 0; i < k; ++i)
    {
        cin >> a[i];
        bfs(i);
    }
    for (int i = 0; i < k; ++i)
    {
        if (dis[0][a[i]] == -1)
            return cout << -1, 0;
    }
    int ans = INT_MAX;
    for (int i = 0; i < k; ++i)
    {
        memset(dp, -1, sizeof(dp));
        int cur = solve(i, (1 << i));
        ans = min(ans, 1 + cur);
        // cout << cur << '\n';
    }
    cout << ans << '\n';
    return 0;
}