#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cerr << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cerr<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; //use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////

ll X, Y;
map<ll, ll> DP;

void solve() {
	cin >> X >> Y;
	deque<ll> que;

	DP[Y] = 0;
	que.push_back(Y);

	while(!que.empty()) {
		ll t = que.front(); que.pop_front();
		ll v = DP[t];
		if(t == 0) continue;
		rep(i, -1, 1 + 1) {
			if((t + i) % 2 == 0 && (!DP.count((t + i) / 2) || DP[(t + i) / 2] > v + 1 + abs(i))) {
				DP[(t + i) / 2] = v + 1 + abs(i);
				que.push_back((t + i) / 2);
			}
		}
	}
	ll res = linf;
	for(auto p: DP) {
		// debug(p);
		MIN(res, p.sec + abs(p.fst - X));
	}
	cout << res << "\n";
}

uint32_t rd() {
	uint32_t res;
#ifdef __MINGW32__
	asm volatile("rdrand %0" :"=a"(res) ::"cc");
#else
	res = std::random_device()();
#endif
	return res;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
    cerr << fixed;
	cerr.precision(6);
	rng.seed(rd());
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
	if(!freopen("in.txt", "rt", stdin)) return 1;
#endif
	solve();
    cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define PB push_back
#define MP make_pair
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 28)
#define LLINF (1LL << 60)
//#define MOD 998244353
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;
typedef pair<int, int> P;

int main()
{
    int n, m;
    ll a[200005], b[200005];
    vector<int> graph[200005];
    vector<vector<int>> connected;
    bool ischecked[200005] = {}; //false:not checked, true:checked

    cin >> n >> m;
    rep(i, n)
    {
        cin >> a[i];
    }
    rep(i, n)
    {
        cin >> b[i];
    }
    rep(i, m)
    {
        int c, d;
        cin >> c >> d;
        graph[c].push_back(d);
        graph[d].push_back(c);
    }

    for (int i = 1; i <= n; i++)
    {
        vector<int> t_vec;
        queue<int> t_que;
        if (!ischecked[i])
        {
            t_vec.push_back(i);
            ischecked[i] = true;
            rep(j, graph[i].size())
            {
                if (!ischecked[graph[i][j]])
                {
                    t_que.push(graph[i][j]);
                    t_vec.push_back(graph[i][j]);
                    ischecked[graph[i][j]] = true;
                }
            }
            while (!t_que.empty())
            {
                int t = t_que.front();
                t_que.pop();
                rep(j, graph[t].size())
                {
                    if (!ischecked[graph[t][j]])
                    {
                        t_que.push(graph[t][j]);
                        t_vec.push_back(graph[t][j]);
                        ischecked[graph[t][j]] = true;
                    }
                }
            }
            connected.push_back(t_vec);
        }
    }

    bool ans = true;//true:Yes
    for (int i = 0; i < connected.size(); i++)
    {
        ll a_sum = 0, b_sum = 0;
        for (int j = 0; j < connected[i].size();j++){
            a_sum += a[connected[i][j] - 1];
            b_sum += b[connected[i][j] - 1];
        }
        if (a_sum != b_sum)
        {
            ans = false;
        }
    }
    if(ans){
        Yes;
    }
    else{
        No;
    }

        /* cout << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << i << ' ';
        for (int j = 0; j < graph[i].size(); j++)
        {
            cout << graph[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
    rep(i, connected.size())
    {
        cout << i << ' ';
        rep(j, connected[i].size())
        {
            cout << connected[i][j] << ' ';
        }
        cout << endl;
    } */

        return 0;
}


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

 typedef vector<int> vi;
 typedef vector<ll> vl;
 typedef vector<vi> vvi;
 typedef set<int> si;
 typedef set<ll> sl;
 typedef queue<int> qi;
 typedef queue<ll> ql;
 typedef stack<int> sti;
 typedef deque<int> dqi;
 typedef priority_queue<int> pqi;
 typedef priority_queue<ll> pql;
 typedef deque<ll> dql;
 typedef vector<vl> vvl;
 typedef pair<int,int> pii;
 typedef pair<double, double> pdd;
 typedef pair<ll, ll> pll;
 typedef vector<pii> vii;
 typedef vector<pll> vll;
 typedef double dl;

 #define PB push_back

 #define F first
 #define S second
 #define MP make_pair
 #define el  '\n'
 #define sl(x) (int)x.length()
 #define all(a) (a).begin(),(a).end()
 #define sz(x) (int)x.size()
 #define mid(l,r) ((r+l)/2)
 #define left(node) (node*2)
 #define right(node) (node*2+1)
 #define mx_int_prime 999999937
#define loop(i,n)        for(int i=0;i<n;i++)

 const double PI = acos(-1);
 const double eps = 1e-9;
 const int inf = 2000000000;
 const ll infLL = 9000000000000000000;
 #define MOD 1000000007
 #define in(n)  ll n;cin>>n;
 #define mem(a,b) memset(a, b, sizeof(a) )
 #define gcd(a,b) __gcd(a,b)
 #define sqr(a) ((a) * (a))
 #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 #define fraction(a) cout.unsetf(ios::floatfield); cout.precision(a); cout.setf(ios::fixed,ios::floatfield);
 typedef vector<int>::iterator vit;
 typedef set<int>::iterator sit;
 int dx[] = {0, 0, +1, -1};
 int dy[] = {+1, -1, 0, 0};
 //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
 //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
int t; ll c[69], a[69];

  int solve(int x){
      string s = to_string(x);
    sort(s.begin(), s.end());
    string t(s.rbegin(), s.rend());
    return stoi(t) - stoi(s);

}

 int main(){

 int t,k;
 cin>>t>>k;

   while(k--){
  t=solve(t);

   }
cout<<t<<el;

    return 0 ;
 }

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

int main(){
    string s;
    int k;
    cin >> s >> k;
    rep(i,k){
        string g1 = s;
        string g2 = s;
        sort(g1.begin(), g1.end(), greater<int>());
        sort(g2.begin(), g2.end());
        ll x = stoi(g1) - stoi(g2);
        stringstream iss;
        iss << x;
        s = iss.str();
    }
    cout << s << endl;
}


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

//take care of long long

//fast I/O
auto optimizer = []() {  // makes I/O fast
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();

int main() {
	ll t=1;
	//cin>>t;
	while(t--)
	{
	    ll a,b,c;
	    cin>>a>>b>>c;

	    if(a==b)
	        cout<<c<<endl;
	    else if(b==c)
	        cout<<a<<endl;
	    else if(c==a)
	        cout<<b<<endl;
	    else
	        cout<<0<<endl;

	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define M (int)(1e9 + 7)

int ar[100005][26];
int N;
int sum(int r, int l)
{
    int res[26] = {0};
    for (; r > 0; r -= (r & -r))
    {
        for (int j = 0; j < 26; j++)
        {
            res[j] += ar[r][j];
        }
    }
    int res1[26] = {0};
    for (; l > 0; l -= (l & -l))
    {
        for (int j = 0; j < 26; j++)
        {
            res1[j] += ar[l][j];
        }
    }
    int count = 0;
    for (int i = 0; i < 26; i++)
    {
        if (res[i] - res1[i] > 0)
        {
            count++;
        }
    }
    return count;
}
void update(int val, int i, int num)
{
    for (; i <= N; i += (i & -i))
    {
        ar[i][val]++;
        if (num > -1)
        {
            ar[i][num]--;
        }
    }
}
bool rounder(int n)
{
    if (n == 3 || n == 4 || n == 5 || n == 6)
        return true;
    else
        return false;
}
int solve()
{

    int a,b,c;
    cin >> a >> b >> c;
    if(a==b)
    {
        return c;
    }
    else if(b==c)
    {
        return a;
    }
    else if(a==c)
    {
        return b;
    }
    else return 0;
}

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

    // int t;
    // cin >> t;
    // while (t--)
    // {
    //     cout << solve()<<"\n";
    // }
    //cout << __gcd(69,138);
    cout << solve() << "\n";
}


//#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x)                             \
    if (dbg) {                              \
        cerr << #x << " = " << (x) << endl; \
    }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    } else
        return 0;
}
void Yes(bool flag = true) {
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
void No(bool flag = true) {
    Yes(!flag);
}
void YES(bool flag = true) {
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
void NO(bool flag = true) {
    YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
}  // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans

int T;

bool check(const string& S, const string& T) {
    //S > T;
    int N = S.size();
    int now = 0;
    for (int i = 0; i < T.size(); i++) {
        if (now >= N)
            return false;
        while (T[i] != S[now]) {
            now++;
            if (now >= N)
                return false;
        }
        now++;
    }
    return true;
}

void solve() {
    while (T--) {
        int N;
        cin >> N;
        string S[3];
        cin >> S[0] >> S[1] >> S[2];
        vector<string> T;
        for (int a = 0; a < 2; a++) {
            for (int b = 0; b < 2; b++) {
                string A, B;
                for (int i = 0; i < N; i++) {
                    A += (char)('0' + a);
                    B += (char)('0' + b);
                }
                T.emplace_back('0' + A + B);
                T.emplace_back('1' + A + B);
                T.emplace_back(A + '0' + B);
                T.emplace_back(A + '1' + B);
                T.emplace_back(A + B + '0');
                T.emplace_back(A + B + '1');
            }
        }
        for (int i = 0; i < 3; i++) {
            S[i] += S[i];
        }
        string ans;
        for (auto& s : T) {
            if (check(S[0], s) && check(S[1], s) && check(S[2], s)) {
                ans = s;
                break;
            }
        }
        cout << ans << endl;
    }
}

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

    cin >> T;

    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

vector<vector<ll> > vertexes(200, vector<ll>(4));
vector<ll> rlist(200);

bool check_no_overlap(ll label, ll n){
    ll ax1 = vertexes.at(label).at(0), ay1 = vertexes.at(label).at(1), ax2 = vertexes.at(label).at(2), ay2 = vertexes.at(label).at(3);
    REP(i,n){
        if (i==label){continue;}
        ll bx1 = vertexes.at(i).at(0), by1 = vertexes.at(i).at(1), bx2 = vertexes.at(i).at(2), by2 = vertexes.at(i).at(3);
        if ((max(ay1,by1)<=min(ay2,by2)) && (max(ax1,bx1)<=min(ax2,bx2))){return false;}
    }
    return true;
}

bool meet(ll x,ll n){
    if (check_no_overlap(x,n) && (0 <= vertexes.at(x).at(0) && 0 <= vertexes.at(x).at(1) && vertexes.at(x).at(2) < 10000 && vertexes.at(x).at(3) < 10000)){
        return true;
    }else{
        return false;
    }
}

int main() {

    ll n;
    cin >> n;

    clock_t start = clock();

    REP(i,n){
        ll x,y,r;
        cin >> x >> y >> r;
        vertexes.at(i).at(0) = x; vertexes.at(i).at(1) = y; vertexes.at(i).at(2) = x+1; vertexes.at(i).at(3) = y+1;
        rlist.at(i) = r;
    }

    while (static_cast<double> (clock()-start) / CLOCKS_PER_SEC * 1.0 < 4.8){
        ll label = rand() % n;
        ll rl = rlist.at(label);

        double t = static_cast<double> (clock()-start) / CLOCKS_PER_SEC * 1.0;
        ll w = (ll) (-13*t + 53);
        if (t >= 4){w = 1;}

        ll pre_sl = (vertexes.at(label).at(2)-vertexes.at(label).at(0))*(vertexes.at(label).at(3)-vertexes.at(label).at(1));
        double pre_score = 1-(1-((double) min(rl,pre_sl))/max(rl,pre_sl))*(1-((double) min(rl,pre_sl))/max(rl,pre_sl));

        REP(dir,4){

            if (dir<2){vertexes.at(label).at(dir) -= w;}
            else{vertexes.at(label).at(dir)+=w;}

            ll sl = (vertexes.at(label).at(2)-vertexes.at(label).at(0))*(vertexes.at(label).at(3)-vertexes.at(label).at(1));
            double score =  1-(1-((double) min(rl,sl))/max(rl,sl))*(1-((double) min(rl,sl))/max(rl,sl));

            if (meet(label,n) && score > pre_score) {pre_score=score;}
            else{
                if (dir<2){vertexes.at(label).at(dir) += w;}
                else{vertexes.at(label).at(dir)-=w;}
            }
        }
    }

    REP(i,n){
        cout << vertexes.at(i).at(0) << " " << vertexes.at(i).at(1) << " " << vertexes.at(i).at(2) << " " << vertexes.at(i).at(3) << endl;
    }
}


#include <vector>
#include <functional>

using namespace std;

template<typename T>
class SegmentTree
{
    int n;
    function<T(T,T)> op;
    vector<T> V;

    T q(int l, int r)
    {
        if (l >= r)
            return V[0];
        T a = q((l+1)/2, r/2);
        if (l%2 != 0)
            a = op(a, V[l]);
        if (r%2 != 0)
            a = op(a, V[r-1]);
        return a;
    }

public:
    SegmentTree(int n_, function<T(T,T)> op, T init):
        op(op)
    {
        n = 1;
        while (n < n_)
            n *= 2;
        V = vector<T>(2*n, init);
        for (int i=n-1; i>0; i--)
            V[i] = op(V[i*2], V[i*2+1]);
    }

    T get(int p) const
    {
        return V[p+n];
    }

    void set(int p, T v)
    {
        p += n;
        V[p] = v;
        for (p/=2; p>0; p/=2)
            V[p] = op(V[p*2], V[p*2+1]);
    }

    //  init OP v[l] OP v[l+1] OP ... OP v[r-1]
    T query(int l, int r)
    {
        return q(l+n, r+n);
    }
};

#include <iostream>
#include <set>
#include <map>

int main()
{
    int N, M, Q;
    cin>>N>>M>>Q;
    vector<int> T(Q), X(Q);
    vector<long long> Y(Q);
    for (int i=0; i<Q; i++)
    {
        cin>>T[i]>>X[i]>>Y[i];
        X[i]--;
    }

    set<long long> YS;
    YS.insert(0);
    for (long long y: Y)
        YS.insert(y);
    vector<long long> YV;
    map<long long, int> YI;
    for (long long y: YS)
    {
        YI[y] = (int)YV.size();
        YV.push_back(y);
    }
    int n = (int)YS.size();

    SegmentTree<long long> AC(n, [&](long long x, long long y){return x+y;}, 0);
    SegmentTree<long long> AS(n, [&](long long x, long long y){return x+y;}, 0);
    SegmentTree<long long> BC(n, [&](long long x, long long y){return x+y;}, 0);
    SegmentTree<long long> BS(n, [&](long long x, long long y){return x+y;}, 0);
    AC.set(0, N);
    BC.set(0, M);
    long long ans = 0;

    vector<long long> a(N), b(M);

    for (int q=0; q<Q; q++)
    {
        auto &a2 = T[q]==1 ? a : b;
        auto &AC2 = T[q]==1 ? AC : BC;
        auto &AS2 = T[q]==1 ? AS : BS;
        auto &b2 = T[q]==2 ? a : b;
        auto &BC2 = T[q]==2 ? AC : BC;
        auto &BS2 = T[q]==2 ? AS : BS;

        ans -= BS2.query(YI[a2[X[q]]], n) + BC2.query(0, YI[a2[X[q]]])*a2[X[q]];
        AC2.set(YI[a2[X[q]]], AC2.get(YI[a2[X[q]]])-1);
        AS2.set(YI[a2[X[q]]], AS2.get(YI[a2[X[q]]])-a2[X[q]]);
        a2[X[q]] = Y[q];
        AC2.set(YI[a2[X[q]]], AC2.get(YI[a2[X[q]]])+1);
        AS2.set(YI[a2[X[q]]], AS2.get(YI[a2[X[q]]])+a2[X[q]]);
        ans += BS2.query(YI[a2[X[q]]], n) + BC2.query(0, YI[a2[X[q]]])*a2[X[q]];
        cout<<ans<<endl;
    }
}

#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <unordered_map>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int MAXN = 234567;
int bit[MAXN];
bool vis[MAXN];
vector<int> candH[MAXN];
vector<int> candW[MAXN];

int query(int x) {
    int res = 0;
    while (x) {
        res += bit[x];
        x -= (x & -x);
    }
    return res;
}

void add(int x, int N) {
    vis[x] = 1;
    while (x <= N) {
        bit[x] += 1;
        x += (x & -x);
    }
}

int main() {
    int H, W, M;
    scanf("%d %d %d", &H, &W, &M);
    int mxH = H, mxW = W;
    for (int i = 0; i < M; i++) {
        int X, Y;
        scanf("%d %d", &X, &Y);
        if (Y == 1) mxH = min(X - 1, mxH);
        if (X == 1) mxW = min(Y - 1, mxW);
        candH[X].push_back(Y);
        candW[Y].push_back(X);
    }
    LL ans = (LL)mxH * mxW;
    for (int i = 1; i <= mxH; i++) {
        if (candH[i].empty()) continue;
        sort(candH[i].begin(), candH[i].end());
        for (int w : candH[i]) {
            if (w > mxW || vis[w]) continue;
            add(w, W);
        }
        int ft = candH[i].front();
        int offs = query(W) - query(ft - 1);
        assert((ft > mxW && offs == 0) || (ft <= mxW));
        ans -= offs;
    }
    // printf("ans = %lld\n", ans);
    for (int i = 1; i <= mxH; i++) {
        sort(candH[i].begin(), candH[i].end());
        if (candH[i].empty()) ans += (W - mxW);
        else if (candH[i].front() > mxW) {
            ans += (candH[i].front() - mxW - 1);
        }
    }
    for (int i = 1; i <= mxW; i++) {
        sort(candW[i].begin(), candW[i].end());
        if (candW[i].empty()) ans += (H - mxH);
        else if (candW[i].front() > mxH) {
            ans += (candW[i].front() - mxH - 1);
        }
    }
    printf("%lld\n", ans);
    return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
by Benq;
*/


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  vector<int> t(n);
  for (auto& x : t) cin >> x;
  constexpr int M = 1000001;
  vector<int> dp(M, 0);
  dp[0] = 1;
  for (auto x : t) {
    for (int i = M - 1; i >= 0; i--) {
      if (i + x < M && dp[i]) dp[i + x] = 1;
    }
  }
  int total = accumulate(t.begin(), t.end(), 0);
  int ans = M * 2;
  for (int i = 0; i < M; i++) if (dp[i]) {
    ans = min(ans, max(i, total - i));
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

ll dp[111111];

void Main() {
  ll n;
  R n;
  ll a[n];
  rep(i,n) R a[i];
  ll sum=0;
  rep(i,n) sum+=a[i];
  mem(dp);
  dp[0]=1;
  rep(i,n) {
    rrep(j,100000) dp[j+a[i]]|=dp[j];
  }
  ll ans=MAX;
  rep(i,111111) {
    if(dp[i]) ans=min(ans,max(i,sum-i));
  }
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


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

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]"<<endl;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << #x << " = ["; _print(x)
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
// =================
#define f(i, k, n) for (int i = k; i < n; i++)
#define r(i, k, n) for (int i = k; i >= n; i--)
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define len(s) s.size()
#define all(v) v.begin(), v.end()
#define tr(it, v) for (auto &it : v)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<vi> vvi;
// =================

const int mod = 1e9+7;
const int N = 3e5 + 1;

int is(ll n) {
    int j=0;
    while (n%5==0)
    {
        j++;
        n/=5;
    }
    if(n==1) return j;
    else return -1;
}

void solve()
{
    ll n;
    cin>>n;
    vector<ll> p3,p5;
    ll p=1;
    f(i,0,37) {
        p*=3;
        p3.pb(p);
    }
    p=1;
    f(i,0,25) {
        p*=5;
        p5.pb(p);
    }
    f(i,0,37) {
        f(j,0,25) {
            if(p3[i]+p5[j]==n) {
                cout<<i+1<<" "<<j+1;
                return;
            }
            else if(p3[i]+p5[j]>n) break;
        }
    }
    cout<<"-1";

}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    while (t--)
        solve();
    return 0;
}

/*
Author : Hocky Yudhiono
1/23/2021 7:55:58 PM

1. You can sort the query if offline!
2. Don't bring the dp remaining state when dfsing on DP on Tree.
3. Try to reverse (Think from the back) if you stuck.
4. Be careful when submitting Div. 2 D-F, dont waste it on stupid WAs.
5. Try to reduce a problem, think of it when you're making subtasks
   like when problemsetting.
*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef long double LD;
typedef vector<int> vi;
typedef pair<LL,LL> PLL;
typedef pair<LL,int> PLI;
typedef pair<int,int> PII;
typedef pair<int,int> pii;
typedef vector<vector<LL>> VVL;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define mp make_pair
#define pb push_back
#define remove erase
#define fi first
#define se second

// If the time limit is strict, try not to use long double

// Remember to undefine if the problem is interactive
#define endl '\n'
#define DEBUG(X) cout << ">>> DEBUG(" << __LINE__ << ") " << #X << " = " << (X) << endl

const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const LL LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const LL MOD = 1000000007;
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
// Do dir^1 to get reverse direction
const char dch[4] = {'R','L','D','U'};
// const string ds[8] = {"E","W","S","N","SE","NW","SW","NE"};
const double PI = 3.141592653589793;

inline void open(string a){
    freopen((a+".in").c_str(),"r",stdin);
    freopen((a+".out").c_str(),"w",stdout);
}

inline void fasterios(){
    // Do not use if interactive
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(10);
}

typedef pair<double, double> PDD;
LL n, m, k;
bool isi[200005];
PDD sum[200005];

int main(){
    fasterios();
    cin >> n >> m >> k;
    for(int i = 1;i <= k;i++){
        int tmp; cin >> tmp;
        isi[tmp] = 1;
    }
    // Dp[n] = sum of dp(i = n .. n + m)/m + 1
    for(int i = n-1;i >= 0;i--){
        PDD memo;
        if(isi[i]) memo = {1, 0};
        else{
            memo = {sum[i + 1].fi - sum[i + m + 1].fi, sum[i+1].se - sum[i+m+1].se};
            memo.fi /= m;
            memo.se /= m;
            memo.se++;
        }
        // cout << memo.fi << " " << memo.se << endl;
        sum[i] = {sum[i+1].fi + memo.fi, sum[i+1].se + memo.se};
    }
    if(fabs(1.0-(sum[0].fi-sum[1].fi)) < EPS) cout << -1 << endl;
    else cout << (sum[0].se-sum[1].se)/(1.0-(sum[0].fi-sum[1].fi)) << endl;

    return 0;
}


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

const int MAX_N = 2e5+2;
int n;
pair<ll,ll> p[MAX_N];
int m;
ll t[MAX_N],axis[MAX_N];
int q;
ll A[MAX_N], B[MAX_N];
pair<ll,ll> ans[MAX_N];

struct query
{
    ll a, b, id;
    bool operator<(const query& other) const
    {
        return a < other.a;
    }
};

void solve()
{
	vector<query> Q;
	for(int i=0;i<q;++i)Q.emplace_back(query{A[i],B[i],i});
	sort(Q.begin(),Q.end());
	ll sw=0,sx=1,sy=1;
	ll cx=0,cy=0;
	int idx=1;
	for(int i=0;i<q;++i){
		for(; idx<=m && idx<=Q[i].a; ++idx){
			if(t[idx]==1){
				sw ^= 1;
				swap(cx,cy), swap(sx,sy);
				cy*=-1,sy*=-1;
			}else if(t[idx]==2){
				sw ^= 1;
				swap(cx,cy), swap(sx,sy);
				cx*=-1,sx*=-1;
			}else if(t[idx]==3){
				sx*=-1,cx*=-1;
				cx+=2LL*axis[idx];
			}else{
				sy*=-1,cy*=-1;
				cy+=2LL*axis[idx];
			}
		}
		if(!sw){
		    ans[Q[i].id].first =sx*p[Q[i].b].first+cx;
		    ans[Q[i].id].second=sy*p[Q[i].b].second+cy;
		}
		else{
		    ans[Q[i].id].first=sx*p[Q[i].b].second+cx;
		    ans[Q[i].id].second=sy*p[Q[i].b].first+cy;
		}
	}
	for(int i=0;i<q;++i)cout<<ans[i].first<<' '<<ans[i].second<<'\n';
}

int main()
{
  	ios::sync_with_stdio(false);
  	cin.tie(nullptr);
    cout.tie(nullptr);
	cin >> n;
	for(int i=1;i<=n;++i)cin>>p[i].first>>p[i].second;
	cin >> m;
	for(int i=1;i<=m;++i){
		cin >> t[i];
		if(t[i]==3 || t[i]==4) cin>>axis[i];
	}
	cin >> q;
	for(int i=0;i<q;++i)cin>>A[i]>>B[i];
	solve();
	return 0;
}

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1001001001;
const long long LINF = 1001002003004005006;
const double PI = acos(-1);
int d4r[] = {1, 0, -1, 0};
int d4c[] = {0, 1, 0, -1};
int d8r[] = {1, 1, 0, -1, -1, -1, 0, 1};
int d8c[] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<long long>> prod(vector<vector<long long>> a, vector<vector<long long>> b) {
    vector<vector<long long>> res(a.size(), vector<long long>(b[0].size()));
    for(int i = 0;i < res.size();++i) {
        for(int j = 0;j < res[0].size();++j) {
            res[i][j] = 0;
            for(int k = 0;k < a[0].size();++k) {
                res[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    return res;
}

struct operation{
    vector<vector<vector<long long>>> op_matrix;
    operation(int m){
        op_matrix.push_back({{1,0,0},{0,1,0},{0,0,1}});
    }
    void make_matrix() {
        int op_num; cin >> op_num;
        vector<vector<long long>> new_matrix(3, vector<long long>(3));
        if(op_num == 1) {
            new_matrix = prod({{0,1,0},{-1,0,0},{0,0,1}},op_matrix.back());
            op_matrix.push_back(new_matrix);
        }
        else if(op_num == 2) {
            new_matrix = prod({{0,-1,0},{1,0,0},{0,0,1}},op_matrix.back());
            op_matrix.push_back(new_matrix);
        }
        else if(op_num == 3) {
            long long p; cin >> p;
            new_matrix = prod({{-1,0,2 * p},{0,1,0},{0,0,1}},op_matrix.back());
            op_matrix.push_back(new_matrix);
        }
        else {
            long long p; cin >> p;
            new_matrix = prod({{1,0,0},{0,-1,2 * p},{0,0,1}},op_matrix.back());
            op_matrix.push_back(new_matrix);
        }
        return;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    vector<long long> x(n), y(n);
    for(int i = 0;i < n;++i) {
        cin >> x[i] >> y[i];
    }
    int m; cin >> m;
    operation op(m);
    for(int i = 0;i < m;++i) {
        op.make_matrix();
    }
    // for(int i = 0;i < 3;++i) {
    //     for(int j = 0;j < 3;++j) {
    //         cout << op.op_matrix[0][i][j] << " ";
    //     }
    //     cout << endl;
    // }
    int q; cin >> q;
    for(int i = 0;i < q;++i) {
        int a, b; cin >> a >> b;
        b--;
        vector<vector<long long>> init(3,vector<long long>(1));
        init[0][0] = x[b];
        init[1][0] = y[b];
        init[2][0] = 1;
        vector<vector<long long>> ans = prod(op.op_matrix[a],init);
        cout << ans[0][0] << " " << ans[1][0] << '\n';
    }
    return 0;
}


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

int main(){

    string a;
    cin>>a;
    int a1 = a.length();
    for(int i=0 ; i<a1-1 ;i++){
        swap(a[i] , a[i+1]);
    }
    cout<<a<<endl;

    return 0;
}

// Problem : A - Rotate
// Contest : AtCoder - AtCoder Beginner Contest 197（Sponsored by Panasonic）
// URL : https://atcoder.jp/contests/abc197/tasks/abc197_a
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
using namespace std;
const long long N=100005, INF=2000000000000000000;
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }
vi prime;
bool isprime[N];
void pre()
{
    for(int i=2;i<N;i++)
    {
        if(isprime[i])
        {
            for(int j=i*i;j<N;j+=i)
            isprime[j]=false;
            prime.pb(i);
        }
    }
    return;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

int spf[N+1];
void spfFun(){
    for(int i=0;i<=N;i++) spf[i]=i;
    for(int i=2;i*i<=N;i++){
        if(spf[i]==i){
            for(int j=i*i;j<=N;j+=i){
                spf[j] = min(spf[j],i);
            }
        }
    }
}

bool isPerfectSquare(long double x)
{
  // Find floating point value of
  // square root of x.
  long double sr = sqrt(x);

  // If square root is an integer
  return ((sr - floor(sr)) == 0);
}
void print(bool n){
    if(n){
        cout<<"YES";
    }else{
        cout<<"NO";
    }
}

int32_t main()
{
    IOS;
    // pre();
    // fill(isprime,true);
    string s;
    cin>>s;
    cout<<s[1]<<s[2]<<s[0];
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
using namespace __gnu_pbds;
using namespace std;
using ld = long double;
using ll = long long;

int n, m;

int main(){
    //IO("input.txt", "output.txt");
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    if(n == 1 && !m){
        cout << "1 2\n";
        return 0;
    }else if(m < 0 || m + 2 > n){
        cout << "-1\n";
        return 0;
    }
    cout << 1 << ' ' << (int)1e8 << '\n';
    for(int i = 0, j = 2; i < m + 1; i++, j += 2)
        cout << j << ' ' << j + 1 << '\n';
    for(int i = 0, j = 1e8 + 1; i < n - m - 2; i++, j += 2)
        cout << j << ' ' << j + 1 << '\n';
    return 0;
}

/*
このコード、と～おれ!
Be accepted!
∧＿∧
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>

////多倍長整数, cpp_intで宣言
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
//
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) //少数出力
#define printLdb(a) printf("%.50Lf\n", a) //少数出力
#define printdbd(a) printf("%.16lf\n", a) //少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) //string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

template<typename T>
inline T chmin(T& a, const T& b) {
	if (a > b)a = b;
	return a;
}

template<typename T>
inline T chmax(T& a, const T& b) {
	if (a < b)a = b;
	return a;
}

////atcoder library
//#include <atcoder/all>
//using namespace atcoder;

/*-----------------------------------------ここからコード-----------------------------------------*/

int main() {

	int n, m;
	scanf("%d%d", &n, &m);
	vector<pair<int, int>> ans;
	if (m == 0) {
		int l = 1, r = 2;
		rep(i, n) {
			ans.emplace_back(l, r);
			l = r + 1;
			r = l + 1;
		}
	}
	else if(m > 0) {
		if (n - m - 2 < 0) {
			puts("-1");
			return 0;
		}
		int l = 2, r = 3;
		rep(i, m + 1) {
			ans.emplace_back(l, r);
			l = r + 1;
			r = l + 1;
		}
		ans.emplace_back(1, l);
		l = r;
		r = l + 1;
		n -= m + 2;
		rep(i, n) {
			ans.emplace_back(l, r);
			l = r + 1;
			r = l + 1;
		}
	}
	else {
		puts("-1");
		return 0;
	}
	for (const auto& [p, q] : ans)printf("%d %d\n", p, q);

	Please AC;
}


#include<bits/stdc++.h>
#define int ll
#define sz(x) int((x).size())
#define all(x) (x).begin(),(x).end()
#define x first
#define y second
using namespace std;
using ll = long long;
using pi = pair<int,int>;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int minf = 0xc0c0c0c0c0c0c0c0;

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

    int n; string s,t; cin>>n>>s>>t;
    if (count(all(s), '0') != count(all(t), '0')) {
        cout<<-1<<'\n';
        return 0;
    }

    int _01 = 0, _10 = 0;
    int res = 0;
    for (int i=0; i<n; i++) {
        if (s[i] == '0') {
            // 00
            if (t[i] == '0') {
                if (_10 || _01) ++res;
            }
            // 01
            else {
                if (_10) {
                    ++res;
                    --_10;
                }
                else {
                    ++_01;
                }
            }
        }
        else {
            // 10
            if (t[i] == '0') {
                if (_01) {
                    ++res;
                    --_01;
                }
                else {
                    ++_10;
                }
            }
        }
    }
    assert(_01 == 0 && _10 == 0);
    cout<<res<<'\n';

    return 0;
}

#include "bits/stdc++.h"
using namespace std;
using li = long long;
const char newl = '\n';
template <class T> vector<vector<T>> vv(const unsigned int n, const unsigned int m, const T x) { return vector<vector<T>>(n, vector<T>(m, x)); }
template <class T> vector<vector<vector<T>>> vvv(const unsigned int n, const unsigned int m, const unsigned int k, const T x) { return vector<vector<vector<T>>>(n, vv(m, k, x)); }
template <class T> bool contains(const set<T>& s, const T& v) { return s.find(v) != s.end(); }
template <class T> bool contains(const unordered_set<T>& s, const T& v) { return s.find(v) != s.end(); }
template <class K, class V> bool contains_key(const map<K, V>& m, const K& v) { return m.find(v) != m.end(); }

auto memo = vvv(101, 101, 101, (double)-1.);
double recur(li a, li b, li c) {
    if (a >= 100 || b >= 100 || c >= 100) {
        return 0;
    }
    double &ans = memo[a][b][c];
    if (ans != -1.) {
        return ans;
    }
    double sum = a + b + c;
    ans = 1;
    ans += recur(a + 1, b, c) * a / sum;
    ans += recur(a, b + 1, c) * b / sum;
    ans += recur(a, b, c + 1) * c / sum;
    return ans;
}

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

    li a, b, c;
    cin >> a >> b >> c;
    cout.precision(12);
    cout << recur(a, b, c) << endl;

    return 0;
}


//https://atcoder.jp/contests/agc048/tasks/agc048_b
//B - Bracket Score
#include <bits/stdc++.h>

using namespace std;

const int MAXN=2e5+4;
long long a[MAXN];
long long b[MAXN];

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

    int n;
    cin>>n;
    for (int i=1; i<=n; i++) {
        cin>>a[i];
    }
    for (int i=1; i<=n; i++) {
        cin>>b[i];
        if (0==i%2) {
            swap(a[i], b[i]);
        }
    }

    long long ans=0;
    for (int i=1; i<=n; i++) {
        ans+=a[i];
        b[i]-=a[i];
    }
    sort(b+1, b+n+1, greater<int>());//降序
    for (int i=1; i<=n/2; i++) {
        ans+=b[i];
    }

    cout<<ans<<"\n";

    return 0;
}

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define f(i,a,b) for( int i=a;i<=b;++i)
#define ff(i,a,b) for( int i=a;i>=b;--i)
#define debug(x) cerr << #x << " : " << x << " " << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 2e18;
const double tiaohe = 0.57721566490153286060651209;
ll oula(ll x) { ll res = x;f(i, 2, x / i) { if (x % i == 0) { res = res / i * (i - 1);while (x % i == 0) x /= i; } }if (x > 1) res = res / x * (x - 1);return res; }
ll quickmod(ll a, ll n, ll m) { ll s = 1;while (n) { if (n & 1) { s = s * a % m; }a = (a*a) % m;n = n / 2; }return s; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) { if (!b) { d = a, x = 1, y = 0; } else { ex_gcd(b, a % b, y, x, d);y -= x * (a / b); } }
ll inv(ll t, ll p) { ll d, x, y;ex_gcd(t, p, x, y, d);return d == 1 ? (x % p + p) % p : -1; }
bool isPrime(ll x) { if (x == 2)return true;if (x % 2 == 0)return false;for (ll i = 2;i*i <= x;i++) if (x % i == 0)return false; return true; }
inline ll in() { char ch = getchar();ll x = 0, f = 1;while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }return x * f; }
void print(double x) { printf("%.6lf\n", x); }
//double a = log(n) +tiaohe + 1.0 / (2 * n);
double eqa = (1 + sqrt(5.0)) / 2.0;
double E = 2.7182818284;
const double eps = 1e-10;
const int N = 6e5 + 100;
ll a[N];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	int n;
	cin >> n;
	f(i, 1, n)a[i] = in();
	ll ans = 0;
	ll pre = 0, sum = 0;
	sort(a + 1, a + 1 + n);
	f(i, 1, n) {
		sum = sum * 2 % mod2;
		ll nx = (sum + pre) % mod2;
		ll tmp = (sum + pre+a[i]) % mod2;
		ans = (ans + tmp * a[i] % mod2) % mod2;
		pre = a[i];
		sum = nx;
	}
	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--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(ll x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
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 person { ll A, B; };

int main() {
  ll N; cin >> N;
  ll A[N], B[N]; rep(i,N) { cin >> A[i] >> B[i]; if (A[i]!=-1) A[i]--; if (B[i]!=-1) B[i]--; }
  ll from[N*2] = {}; rep(i,N) if (A[i]!=-1) from[A[i]]++;
  ll to[N*2] = {}; rep(i,N) if (B[i]!=-1) to[B[i]]++;
  rep(i,N) if (A[i]!=-1&&B[i]!=-1&&A[i]>=B[i]) { cout << "No" << endl; return 0; }
  rep(i,N*2) if (from[i]+to[i]>=2) { cout << "No" << endl; return 0; }

  bool ok[N*2][N*2] = {}; rep(i,N*2) repr(j,i+1,N*2) ok[i][j] = true;
  rep(i,N) {
    if (A[i]!=-1) {
      rep(j,N*2) {
        ok[j][A[i]] = false;
        if (to[j]) ok[A[i]][j] = false;
      }
    }
    if (B[i]!=-1) {
      rep(j,N*2) {
        ok[B[i]][j] = false;
        if (to[j]) ok[j][B[i]] = false;
      }
    }
    if (A[i]!=-1&&B[i]!=-1) rep(j,N*2) { ok[A[i]][j] = false; ok[j][B[i]] = false; }
  }
  rep(i,N) if (A[i]!=-1&&B[i]!=-1) ok[A[i]][B[i]] = true;
  // rep(i,N*2) debug(ok[i],ok[i]+N*2);

  bool dp[N+1] = {}; dp[0] = true;
  rep(i,N) {
    if (!dp[i]) continue;
    repr(j,i+1,N+1) {
      bool now = true;
      rep(k,j-i) now &= ok[i*2+k][i*2+k+(j-i)];
      dp[j] |= now;
    }
  }
  // debug(dp,dp+(N+1));

  string result = dp[N] ? "Yes" : "No";
  cout << result << endl;
  return 0;
}

//#pragma GCC optimize "trapv"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define ll long long int
#define fab(a,b,i) for(ll i=a;i<b;i++)
#define pb push_back
#define db long double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(),x.end()
#define MOD 1000000007
#define vll vector<ll>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define quick ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

using namespace std;

ll add(ll x, ll y) {ll res = x + y; return (res >= MOD ? res - MOD : res);}
ll mul(ll x, ll y) {ll res = x * y; return (res >= MOD ? res % MOD : res);}
ll sub(ll x, ll y) {ll res = x - y; return (res < 0 ? res + MOD : res);}
ll power(ll x, ll y) {ll res = 1; x %= MOD; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, MOD - 2);}
ll lcm(ll x, ll y) { ll res = x / __gcd(x, y); return (res * y);}

db x, y, r;

bool dist(db a, db b)
{
	db dis = (x - a) * (x - a) + (b - y) * (b - y);

	return (dis <= (r * r));
}

int main()
{	quick;
#ifndef ONLINE_JUDGE
	freopen("D:/sublime/input.txt", "r", stdin);
	freopen("D:/sublime/output.txt", "w", stdout);
#endif
	// int T = 1;
	// cin >> T;
	// while (T--)
	{
		cin >> x >> y >> r;

		r += 1e-14;

		ll val = x - r - 400;
		ll op = x + r + 400;
		ll ans = 0;
		fab(val, op + 1, i)
		{
			//cout << "i:" << i << endl;
			ll l = 0, h = 1e10;
			ll op = 0;

			while (l <= h)
			{
				ll mid = (l + h) / 2;
				//cout << "mid:" << mid << endl;

				db coord = floor(y + mid);
				//cout << "coord:" << i << " " << coord << endl;
				if (dist(i, coord))
				{
					//cout << "have:" << i << " " << coord << endl;
					//cout << "mid:" << mid << endl;
					l = mid + 1;
					op = coord - ceil(y) + 1;

				}
				else
					h = mid - 1;
			}
			//cout << "jL" << l << endl;

			ans += op;

			l = (y == int(y) ? 1 : 0), h = 1e10;
			op = 0;
			while (l <= h)
			{
				ll mid = (l + h) / 2;
				//cout << "mid:" << mid << endl;

				db coord = floor(y - mid * 1.0);
				//cout << "coord:" << i << " " << coord << endl;
				if (dist(i, coord))
				{
					//cout << "have1:" << i << " " << coord << endl;
					//cout << "mid1:" << floor(y) - coord + 1 << endl;
					l = mid + 1;
					op = floor(y) - coord + (y == int(y) ? 0 : 1);

				}
				else
					h = mid - 1;
			}
			//cout << "jL2" << l << endl;

			ans += op  ;

		}
		cout << ans << endl;

	}

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


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

#define repr(i,a,b) for (int i=a; i<b; i++)
#define rep(i,n) for (int i=0; i<  n; i++)
#define PI 3.14159265359
const long long INF = 1LL << 60;
long long MOD = 1000000007;
long long gcd(long long a, long long b) { return b ? gcd(b, a%b) : a; }
long long lcm (int a, int b){return  (long long) a*b /gcd(a,b);}

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

    vector<int>ans;
    rep(i,N){
        int a;
        cin >> a;

        if(a == X)continue;
        else ans.push_back(a);
    }

    for(int i = 0; i<ans.size(); i++){
        if ( i == 0){
        cout << ans[i];
        }
        else cout << " " << ans[i];
    }
    cout << endl;
}

#pragma region head
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using vi = vector<int>;
    using vll = vector<ll>;
    using pi = pair<int, int>;
    using pll = pair<ll,ll>;
    template <class T> using vv = vector<vector<T>>;
    #define _roverload(_a1,_a2,_a3,name,...) name
    #define _rep(i,n) repi(i,0,n)
    #define repi(i,a,b) for(int i=int(a);i<int(b);++i)
    #define rep(...) _roverload(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
    #define _rrep(i,n) rrepi(i,0,n)
    #define rrepi(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);--i)
    #define rrep(...) _roverload(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
    #define all(x) (x).begin(), (x).end()
    #define rall(x) (x).rbegin(), (x).rend()
    #define bit(n) (1LL<<(n))
    template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
    template<class T> inline bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
    void print() {cout << '\n';}
    template<class T> void print(const vector<T> &v)
    {bool f=false; for(const auto &e: v){if(f)cout << ' '; cout << e; f=true;} cout << '\n';}
    template<class... T> void print(const T&... args)
    {bool f=false; for(const auto &e: {args...}){if(f)cout << ' '; cout << e; f=true;} cout << '\n';}
    const int INF = 1002003004;
    const ll LINF = 1002003004005006007ll;
    struct preprocess{preprocess()
    {cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}}____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main(){
    int n, x; cin >> n >> x;
    vi a(n); rep(i,n) cin >> a[i];
    rep(i,n){
        if(a[i] != x) cout << a[i] << ' ';
    }
    print();
}


#include<bits/stdc++.h>
#define maxn 1000005
#define inf 0x7fffffffffff
#define mod 1000000007
#define mst(a,x) memset(a,x,sizeof(a))
#define up(l,r,i) for(LL i=l;i<=r;i++)
#define down(l,r,i) for(LL i=l;i>=r;i--)
#define in(l,r,a) for(LL i=l;i<=r;i++)cin>>a[i]
#define out(l,r,a) for(LL i=l;i<=r;i++)cout<<a[i]<<" "
#define injz(n,m,a) for(LL i=1;i<=n;i++)for(LL j=1;j<=m;j++)cin>>a[i][j]
#define st(a,n) sort(a+1,a+n+1)
#define endl "\n"
#define hh cout<<endl
#define pb push_back
#define cf cin>>T;while(T--)
#define xh(n) U=n;while(U--)
#define cfout(x) cout<<(x?"YES":"NO")<<endl
#define cs cout<<"qaq"<<endl
typedef long long LL;LL T,U;
typedef double db;
using namespace std;
LL ans,a[10];
string s,ok;
vector<LL>qaq;
char tat[10];
map<string,LL>mp;
void solve()
{
    if(qaq.size()==4)
    {
        mst(a,0);
        string qwq="";
        for(LL i:qaq)
        {
            qwq+=to_string(i);
            a[i]++;
        }
        LL ansnow=24;
        up(0,9,i)
        {
            if(!a[i])continue;
            LL now=1;
            up(1,a[i],j)now*=j;
            ansnow/=now;
        }
        up(0,3,i)tat[i]=qwq[i];
        sort(tat,tat+4);
        up(0,3,i)qwq[i]=tat[i];
        if(!mp[qwq])
        {
            mp[qwq]++;
            ans+=ansnow;
        }
        return;
    }
    up(0,9,i)
    {
        if(s[i]=='o'||s[i]=='?')
        {
            qaq.pb(i);
            solve();
            qaq.pop_back();
        }
    }
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    #ifdef ONLINE_JUDGE
    #else
    //freopen("bwxnQAQin.txt","r",stdin);
    //freopen("bwxnQAQout.txt","w",stdout);
    #endif
    cin>>s;
    LL ok=0;
    up(0,9,i)
    {
        if(s[i]=='o')
        {
            ok++;
            qaq.pb(i);
        }
    }
    if(ok>4)
    {
        cout<<"0"<<endl;
        exit(0);
    }
    solve();
    cout<<ans<<endl;
    cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms";
    return 0;
}

/*  Solution By:
    Ashish Kudar */
                                /* When you get down to nothing.. */
#include<bits/stdc++.h>
#define sync            ios_base::sync_with_stdio(0);cin.tie(NULL);
#define ll              long long
#define ull             unsigned ll
#define ld              long double
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define vi              vector<int>
#define vll             vector<ll>
#define vs              vector<string>
#define vb              vector<bool>
#define vpii            vector<pair<int,int>>
#define vpll            vector<pair<ll,ll>>
#define vvi             vector<vector<int>>
#define vvll            vector<vector<ll>>
#define all(x)          x.begin(),x.end()
#define FOR(i,a,b)      for(int i=a;i<b;++i)
using namespace std;

const int MAX=1e6+5;
const int mod=1e9+7;
const ld pi=acos(-1);

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

string s,t;

ll calcNoOfStrings(string res,ll pos){
    if(pos == 4){
        t=s;
        FOR(i,0,s.length()){
            if(s[i] == 'o') t[i]='0';
            else if(s[i] == 'x')    t[i]='1';
            else    t[i]='1';
        }
        FOR(i,0,4){
            if(s[res[i]-'0'] == 'o')
                t[res[i]-'0']='1';
            else if(s[res[i] -'0'] == 'x')
                t[res[i]-'0']='0';
        }
        if(t == string(10,'1')){
            return 1;
        }else
            return 0;
    }
    ll ans=0;
    FOR(i,0,10){
        res[pos]=i+'0';
        ans+=calcNoOfStrings(res,pos+1);
    }
    return ans;
}

int main(){
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif
    clock_t clk = clock();
    sync
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;++t){
        // test cases starts here
        cin>>s;
        string ans(4,'X');
        cout<<calcNoOfStrings(ans,0)<<endl;

        // cout<<"Case #"<<t<<": ";
    }

    // cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
}

                                /* God is always upto something! */


#include <bits/stdc++.h>
#include <unordered_map>

#define eps 1e-8
#define PI 3.141592653589793
#define bsize 512

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define bs 1000000007

using namespace std;

int main()
{

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

    int n;
    cin>>n;

    int arr[(1<<n)+1];
    arr[0]=-1;

    unordered_map<int, int> pos;

    for(int i=1;i<=(1<<n);i++){
        cin>>arr[i];
        pos[arr[i]]=i;
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=(1<<(n-i));j++){
            if(i==n)    arr[j]=min(arr[2*j-1],arr[2*j]);
            else        arr[j]=max(arr[2*j-1],arr[2*j]);
        }
    }

    cout<<pos[arr[1]];

    return 0;
}

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

const long long MOD = 1e9 + 7;
const int INF = 1e9;
const long long inf = 1LL<<60;

//cout << fixed << setprecision(10) <<  << endl;

bool solve(int left, vector<int> &res, int n, vector<int> &p, vector<int> &ip) {
  if ((int)res.size() == n-1) {
    for (int i=0; i<n; i++) {
      if (p.at(i) != i) return false;
      return true;
    }
  }
  if (ip.at(left) <= left) return false;
  for (int j=ip.at(left); j>left; j--) {
    res.push_back(j);
    swap(p.at(j), p.at(j-1));
  }
  for (int j=left; j < ip.at(left); j++) {
    if (p.at(j) != j) return false;
  }
  return solve(ip.at(left), res, n, p, ip);
}
int main() {
  int n;
  cin >> n;
  vector<int> p(n), ip(n);
  for (int i=0; i<n; i++) {
    cin >> p.at(i); p.at(i)--;
    ip.at(p.at(i)) = i;
  }
  vector<int> res;
  if (!solve(0, res, n, p, ip)) cout << -1 << endl;
  else for (auto v : res) cout << v << endl;
}


///  ~~~~~Bismillah'hir Rah'manir Rahim~~~~~  ///

/*  "  Allah(SWT) blesses with knowledge whom he wants. "   */
/*  " Indeed with Hardship, comes Ease. "    */

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

#define  Fast      ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ULL       unsigned long long
#define  LL         long long
#define ST          string

#define  F           first
#define  S           second
#define  PB        push_back
#define  MP       make_pair

#define  pi         acos(-1.0)

///for Unordered map
/* mpp.max_load_factor(0.25); mpp.reserve(1024); */
/* cout << fixed << setprecision(12);*/

int main ()
{
    int t, n, i, j, x, y, sum=0 ;
    //double p, q ;
    //char c ;
    string s, s1, s2 ;
    //cin>>t ;
    //while(t--)
    //{
    cin>>n ;
    int a[n+5] , b[n+5] , c[n+5] ;

    //}
    for(i=1; i<=n; i++)
    {
        cin>>a[i] ;
        if(a[i]>10) sum+=(a[i]-10) ;
    }
    cout<<sum ;

    return 0 ;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>
#include <cmath>
#include <numeric>
#include <set>
#include <map>
#include <array>
#include <complex>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

void solve()
{
  int n; cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
  }
  ll res = INF;
  for (int S = 0; S < (1 << n); ++S)
  {
    if(~S >> (n - 1) & 1)
      continue;
    int XOR = 0;
    int OR = 0;
    for (int i = 0; i < n; ++i)
    {
      OR |= a[i];
      if(S >> i & 1)
      {
        XOR ^= OR;
        OR = 0;
      }
    }
    chmin(res, (ll)XOR);
  }
  cout << res << "\n";
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int kkt = 1;
  //cin >> kkt;
  while(kkt--)
  {
    solve();
  }
  return 0;
}


//switch between terminal and editor: ctrl + `
#include"bits/stdc++.h"
#define endl '\n'
using namespace std;
typedef long long ll;
// #define comparator_datatype pair<int,pair<ll,ll>>
#define loop(i,x,n,u) for(ll i = x;i < n;i+=u)
#define rloop(i,x,n,u) for(ll i = x;i > n;i-=u)
#define mod 1000000007
#define read(X,n) loop(i,0,n,1){cin>>X[i];}

void compressArray(vector<ll>& array){
  map<ll,int> rank_;
  for(auto& i:array){
    rank_[i] = 1;
  }
  int r = 1;
  for(auto& i:rank_){
    i.second = r++;
  }
  loop(i,0,array.size(),1){
    array[i] = rank_[array[i]];
  }
}

//void solve(){}
int main(){
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  int t = 1;
  //cin>>t;
  while(t--){
    //solve();
    int n,type;
    cin>>n;
    vector<pair<double,double>> arr(n);
    loop(i,0,n,1){
      cin>>type>>arr[i].first>>arr[i].second;
      if(type==2)
        arr[i].second-=0.1;
      else if(type==3)
        arr[i].first+=0.1;
      else if(type==4){
        arr[i].first+=0.1,arr[i].second-=0.1;
      }
    }
    // for(auto& i:arr)
    //   cout<<i.first<<" "<<i.second<<endl;
    ll count = 0;
    loop(i,0,n,1){
      loop(j,i+1,n,1){
        if((arr[i].second < arr[j].first) || (arr[i].first > arr[j].second))
          continue;
        else{
          count++;
          // cout<<arr[i].first<<":"<<arr[i].second<<" "<<arr[j].first<<":"<<arr[j].second<<endl;
        }
      }
    }
    cout<<count<<endl;
  }
  return 0;
}

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//#define ENVIRONMENT_LINKED_BOOST

#ifdef ENVIRONMENT_LINKED_BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;
#endif

using namespace std;
using uint = unsigned int;
using ll   = long long;
using ull  = unsigned long long;
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define ALL(v)       v.begin(), v.end()

template <typename A, typename B>
void chmin(A &a, const B &b) {
    a = min(a, static_cast<A>(b));
};
template <typename A, typename B>
void chmax(A &a, const B &b) {
    a = max(a, static_cast<A>(b));
};

ll solve(const vector<pair<ll, ll>> &V, multiset<ll> S) {
    ll ans = 0;
    REP(i, 0, V.size()) {
        auto it = S.lower_bound(V[i].second);
        if (it == S.end()) continue;
        ans += V[i].first;
        S.erase(it);
    }
    return ans;
}

int main() {
    ll N, M, Q;
    cin >> N >> M >> Q;
    vector<pair<ll, ll>> V(N);
    REP(i, 0, N) {
        cin >> V[i].second >> V[i].first;
    }
    sort(V.begin(), V.end(), greater<pair<ll, ll>>());
    vector<ll> s(M);
    REP(i, 0, M)
    cin >> s[i];
    REP(q, 0, Q) {
        ll l, r;
        cin >> l >> r;
        --l;
        --r;
        multiset<ll> S;
        REP(i, 0, l)
        S.insert(s[i]);
        REP(i, r + 1, M)
        S.insert(s[i]);
        cout << solve(V, S) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second

using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

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

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct coord_comp {
    vector<T> v;
    bool sorted = false;

    coord_comp() {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 100010;

int main() {
    lint a, b, c, d;
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    printf("%lld\n", c * d - b > 0 ? div_ceil(a, c * d - b) : -1);
}

#include <bits/stdc++.h>

#define index(i, n) for (long long i = 0; i < n; i++)
#define index1(i, n) for (long long i = 1; i <= n; i++)
#define range(i, a, b) for (long long i = a; i < b; i++)
#define indexD(i, n) for (long long i = n - 1; i >= 0; i--)
#define index1D(i, n) for (long long i = n; i >= 1; i--)
#define rangeD(i, a, b) for (long long i = a - 1; i >= b; i--)
#define ceild(a, b) ((a) / (b) + ((a) % (b) != 0))
#define fast_io std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define print(x) cout << (x) << '\n'
#define trace(x) cout << '>' << #x << ':' << x << endl;
#define trace2(x, y) cout << '>' << #x << ':' << x << " | " << #y << ':' << y << endl;
#define trace3(x, y, z) cout << '>' << #x << ':' << x << " | " << #y << ':' << y << " | " << #z << ':' << z << endl;
#define trace4(x, y, z, w) cout << '>' << #x << ':' << x << " | " << #y << ':' << y << " | " << #z << ':' << z << " | " << #w << ':' << w << endl;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define First first
#define Second second
#define MOD 998244353
#define INF (9223372034707292159ll)

typedef unsigned long long ull;
typedef long long int ll;

using namespace std;

int main()
{
        fast_io;

        ll t, n, m, k, x, y, r, ans, c;
        cin >> n;
        ll arr[n];
        index(i, n) cin >> arr[i];
        sort(arr, arr + n);
        ll pre = 0;
        ans = 0;

        index(i, n)
        {
                ans = (ans + ((pre + arr[i]) % MOD) * (arr[i]) % MOD) % MOD;
                pre = (pre * 2) % MOD;
                pre = (pre + arr[i]) % MOD;
        }
        print(ans);

        return 0;
}


#include <bits/stdc++.h>
/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

*/

#define pii pair<int,int>
#define fi first
#define pb push_back
#define si second
#define int   long long
#define mod  1000000007 //998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define double long double
#define all(o) o.begin(),o.end()

/*
#define T pair<int, int>
#define ordered_set tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
*/

using namespace std;

int power(int x, int y)
{
    int res = 1LL;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1; // y = y/2
        x = (x*x) % mod;
    }
    return res%mod;
}
int inv(int  n)
{
    return power(n,mod-2)%mod;
}
int isprime(int n){
    if(n<2)return 0;
    int i;
    for(i=2;i*i<=n;i++)
        if(n%i==0)
            return 0;
    return 1;
}
void files(){
    // For getting input from input.txt file
    freopen("input.txt", "r", stdin);
    // Printing the Output to output.txt file
    freopen("output.txt", "w", stdout);

}

int egcd(int a,int b,int &x ,int & y){
    if(b==0){
        x=1;y=0;
        return a;
    }
    int x1,y1;
    int g=egcd(b,a%b,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
    return g;
}
const int N=2e6;
//https://codeforces.com/blog/entry/22747

int a[500005],n,k;
void solve(){

    cin>>n>>k;
    int i;
    for(i=1;i<=n;i++){
        int x;
        cin>>x;
        a[x]++;
    }

    a[0]=min(a[0],k);
    for(i=1;i<n;i++)
        a[i]=min(a[i],a[i-1]);

    // for(i=0;i<n;i++)
    //     cout<<i<<" : "<<a[i]<<"\n";

    int ans=0;
    for(i=n-1;i>=0;i--){
        ans+=(a[i]-a[i+1])*(i+1);
    }
    cout<<ans<<"\n";

}

int32_t main(){
         fast

    int t=1;

    // cin>>t;
    while(t--)
    {
        solve();
    }
}

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

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

const int MAX = 300005;

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

    vector<ll> cnt(MAX);
    rep(i, n) cnt[a[i]]++;
    chmin(cnt[0], k);
    rep(i, MAX - 1) { chmin(cnt[i + 1], cnt[i]); }
    ll ans = 0;
    rep(i, MAX) ans += cnt[i];
    cout << ans << endl;
}


#include <bits/stdc++.h>

#include <iostream>
//#include <algorithm>
// #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
// 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; }

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}

// if (e > o) {
//   cout << ("Even") << endl;
// } else if (e < o) {
//   cout << ("Odd") << endl;
// } else {
//   cout << ("Same") << endl;
// }

int main() {
  // std::cout << std::fixed << std::setprecision(10);

  // ll ans = 0;

  //

  // cout << ans << endl;

  ll T;
  scanf("%lld", &T);

  for (ll i = 0; i < T; i++) {
    ll v;
    scanf("%lld", &v);

    if (v % 4 == 0) {
      cout << ("Even") << endl;
    } else if (v % 2 == 0) {
      cout << ("Same") << endl;
    } else {
      cout << ("Odd") << endl;
    }
  }
}

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)
//
////#define ATCODER
//#ifdef ATCODER
//#include<atcoder/all>
//using namespace atcoder;
//#endif

#include <algorithm>
#include <utility>
#include <vector>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <numeric>
#include <type_traits>

using namespace std;

#ifdef _MSC_VER
#include"stdafx.h"
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
void debug_out() { cerr << endl; }

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

#define ll long long
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define VSORT(v) sort(v.begin(), v.end());
#define VREVERSE(v) reverse(v.begin(), v.end())

#define print(x) cout << (x) << '\n'
#define spa <<" "<<
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline bool chmin(T1 & a, T2 b) { if (a > b) { a = b; return true; }return false; }
template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; }return false; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 400040;
const double pi = acos(-1);
constexpr long double EPS = 1e-14;
const int dx[4] = { 0,0,-1,1 }, dy[4] = { -1,1,0,0 };
using ld = long double;

void solve() {
	int N; cin >> N;
	REP(i, N) {
		cout << (i * 2 + 1)%N + 1 << " " << (i * 2 + 2)%N + 1 << endl;
	}
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//int q; cin >> q;
	//while (q--)
	solve();
}


#include <iostream>

using namespace std;

int a, b;

void swap(int &a, int &b) {
    if (a > b) {
        int temp = a;
        a = b;
        b = temp;
    }
}
void solve() {
    cin >> a >> b;

    swap(a, b);
    int diff = b - a;

    if(diff >= 3) cout << "No" <<endl;
    else cout << "Yes" << endl;
}

int main() {
    ios_base::sync_with_stdio(0);

    solve();
}

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

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

int main(){
  int X,Y;
  cin>>X>>Y;
  if(X>Y){
    if((X-Y)<3){
      cout<<"Yes";
    }
    else{
      cout<<"No";
    }
  }
  else{
    if((Y-X)<3){
      cout<<"Yes";
    }
    else{
      cout<<"No";
    }
  }
  return 0;
}


//@formatter:off
#include<bits/stdc++.h>
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(i,n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i)
#define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d)
#define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep(i,n) for (ll i = ll(n)-1; i >= 0; i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define popcount(x) __builtin_popcount(x)
#define pb push_back
#define eb emplace_back
#ifdef __LOCAL
#define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); cout << flush; }
#else
#define debug(...) void(0)
#endif
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using LP = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vs = vector<string>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vp = vector<P>;
using vvp = vector<vector<P>>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
template<class T> void vecout(const vector<T> &v,char div='\n') { rep(i,v.size()) cout<<v[i]<<(i==int(v.size()-1)?'\n':div);}
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
void scan(){}
template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); }
template<class T> void print(const T& t){ cout << t << '\n'; }
template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); }
template<class... T> void fin(const T&... a) { print(a...); exit(0); }
const string yes[] = {"no","yes"};
const string Yes[] = {"No","Yes"};
const string YES[] = {"NO","YES"};
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    INT(n);
    vvi G(n);
    rep(i, 1, n) {
        INT(p);
        p--;
        G[p].pb(i);
    }
    auto dfs = [&](auto &self, int u) -> P {
        vi ev, od;
        int st = 1;
        for (int v : G[u]) {
            auto[sc, t] = self(self, v);
            if (t) od.pb(-sc);
            else ev.pb(-sc);
            st += t;
        }
        sort(all(ev));
        sort(all(od));
        int res = 1;
        while (ev.size() and ev.back() >= 0) {
            res -= ev.back();
            ev.pop_back();
        }
        int t = -1;
        while (od.size()) {
            res += od.back() * t;
            od.pop_back();
            t *= -1;
        }
        for (int i : ev) res += i * t;
        return {res, st & 1};
    };
    auto[ans, _] = dfs(dfs, 0);
    ans += n;
    assert(ans % 2 == 0);
    ans /= 2;
    print(ans);
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define F first
#define S second
#define um map <ll,ll>
#define rep(i,z,n)  for(int i=z;i<n;i++)
#define repi(i,z,n) for(int i=z;i<=n;i++)
#define repn(i,z,n) for(int i=n-1;i>=z;i--)
#define vec vector<ll>
#define vecp vector<pair<ll,ll>>
// #define pi (double)3.14159265358979323846
#define ld long double
#define all(z) z.begin(),z.end()
#define pii pair<ll,ll>

ll power(ll a, ll b, ll m) { ll ans = 1; a = a % m; if (a == 0) return 0; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; }
ll modInverse(ll a, ll m) {return power(a, m - 2, m);}
ll binpow(ll a, ll b) {ll res = 1; while (b > 0) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;}
ll binlog2(ll n) {ll logValue = -1; while (n) {logValue++; n >>= 1;} return logValue;}
ll ceil(ll t1, ll t2) {ll ans = (t1 + t2 - 1) / t2; return ans;}
void print(vec a) {rep(i, 0, a.size())cout << a[i] << " "; cout << endl;}
const ll mod = 1e9 + 7;
const ll N = 30 + 5;
const ll inf = 2e18;

void solve() {
    vec a(3);
    rep(i, 0, 3)
    cin >> a[i];

    sort(all(a));

    if (a[1] - a[0] == a[2] - a[1])
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    speed;
    int test = 1;
    // init();
    // cin >> test;
    // ll t1 = 1;
    while (test--) {
        // cout << "Case #" << t1 << ": ";
        solve();
        // t1++;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=(int)(x);i<(int)(y);i++)
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=1e5;
const int inf=1e9;
int main(){
	string s; cin>>s;
	int n = s.size();
	rep(i,0,n){
		bool flag = true;
		rep(j,0,s.size()){
			if(s[j] != s[s.size()-1-j]) flag = false;
		}
		if(flag){ cout<<"Yes"<<endl; return 0; }
		s = "0" + s;
	}cout<<"No"<<endl;
}

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

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define m0(x) memset(x,0,sizeof(x))

int dx4[4] = {1,0,-1,0}, dy4[4] = {0,1,0,-1};

int main()
{
    ll n;
    cin>>n;
    set<ll> s;
    for (ll a = 2; a <= sqrt(n); a++)
    {
        if(s.find(a)!=s.end()) continue;
        for (ll b = a*a; b<=n; b*=a)
        {
            s.insert(b);
        }
    }
    cout<<n-s.size()<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pll pair<ll,ll>
#define vpll vector< pll >
#define mpll map<ll,ll>
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define print(x) {cout<<x<<nl;continue;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define err1(a) {cout<<#a<<' '<<a<<nl;}
#define err2(a,b) {cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<nl;}

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
	if (b==0)return a;
	return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
ll pow(ll a, ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans;
}
struct node{
    ll x,t,k;
};
const int N=1e5+5;
vec d(N,(ll)1e18);
vec v[N];
vector<node> g[N];
ll dis(ll x,ll y)
{
    ll p=x/y;
    if(x%y!=0)p++;
    p*=y;
    return p;
}
void solve(ll n,ll u)
{
    multiset<pll> q;
    d[u]=0;
    q.insert({0,u});
    while(!q.empty())
    {
        pll p=*q.begin();
        q.erase(q.begin());
        trav(it,g[p.sc])
        {
            ll c=dis(p.f,it.k);

            if(d[it.x]>c+it.t)
            {
                d[it.x]=c+it.t;
                q.insert({d[it.x],it.x});
            }
        }
    }

}
int main()
{
	fast
	int n,m,x,y;
	cin>>n>>m>>x>>y;
	FOR(i,1,m)
	{
	    ll a,b,c,d;
	    cin>>a>>b>>c>>d;
	    node temp;
	    temp.x=b;temp.t=c;temp.k=d;
	    g[a].pb(temp);
	    node kemp;
	    kemp.x=a;kemp.t=c;kemp.k=d;
	    g[b].pb(kemp);
	}
    solve(n,x);
    if(d[y]==(ll)1e18)cut(-1)
	//FOR(i,1,n)cout<<d[i]<<' ';
    cut(d[y])

}

// Problem: E - Train
// Contest: AtCoder - AtCoder Beginner Contest 192
// URL: https://atcoder.jp/contests/abc192/tasks/abc192_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define mod 1000000007

// templates
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define po(x, y) fixed << setprecision(y) << x
#define ss(s) scanf(" %[^\n]%*c", s)
#define sc(n) scanf("%d", &n)
#define sl(n) scanf("%lld", &n)
#define ps(s) printf("%s\n", s)
#define pr(n) printf("%d\n", n)
#define pl(n) printf("%lld\n", n)
#define prs(n) printf("%d ", n)
#define pls(n) printf("%lld ", n)
using namespace std;

const ll inf = (ll)1e16 + 10;
const int N = (int)1e5 + 10;
int a[N], b[N];
vector<array<ll, 3>> adj[N];  // u->v,t,k
vector<ll> dist(N);
ll n, m, x, y;
void bfs() {
  set<array<ll, 2>> s;  // time,node
  s.insert({0, x});
  for (int i = 0; i <= n; i++) {
    dist[i] = inf;
  }
  dist[x] = 0;
  while (sz(s)) {
    auto temp = *s.begin();
    s.erase(s.begin());
    ll node = temp[1], kete = temp[0];
    for (auto c : adj[node]) {
      ll add = 0;
      if (kete < c[2] && kete != 0) {
        add = (c[2] - kete);
      } else if (kete != 0) {
        if (kete % c[2]) {
          add = c[2] - (kete % c[2]);
        }
      }
      ll curr = c[1] + add + kete;
      // printf("node-> %lld curr-> %lld\n", node, curr);
      if (dist[c[0]] > curr) {
        s.erase({dist[c[0]], c[0]});
        dist[c[0]] = curr;
        s.insert({curr, c[0]});
      }
    }
  }
  if (dist[y] == inf) {
    dist[y] = -1;
  }
  pl(dist[y]);
}
void solve() {
  sl(n), sl(m), sl(x), sl(y);
  for (int i = 1; i <= m; i++) {
    ll u, v, t, k;
    sl(u), sl(v), sl(t), sl(k);
    adj[u].push_back({v, t, k});
    adj[v].push_back({u, t, k});
  }
  bfs();
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  int t = 1;
  // sc(t);
  while (t--) {
    solve();
  }
  // cerr << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
  return 0;
}


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

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using pint = pair<int,int>;
using pll = pair<ll,ll>;
using Graph = vector<vector<int>>;

int main(){
    //input part
    //0 index
    int N,M; cin>>N>>M;
    Graph G(N);
    rep(i,M){
        int a,b; cin>>a>>b;
        G[a-1].push_back(b-1);
        G[b-1].push_back(a-1);
    }
    int K; cin>>K;
    vector<int> C(K);
    rep(i,K){
        cin>>C[i];
        C[i]--;
    }
    //BFS part
    auto BFS = [&](int s){
        vector<ll> dist(N,INF);
        dist[s] = 0;
        queue<int> Q;
        Q.push(s);
        while(!Q.empty()){
            int now = Q.front();
            Q.pop();
            for(auto next: G[now]){
                if(dist[next]!=INF)continue;
                dist[next] = dist[now]+1;
                Q.push(next);
            }
        }
        vector<ll> res(K,INF);
        rep(i,K)res[i] = dist[C[i]];
        return res;
    };
    //bit dp part
    vector<vector<ll>> dist(K);
    rep(i,K) dist[i] = BFS(C[i]);

    vector<vector<ll>> dp(1<<K, vector<ll> (K,INF));
        //init
    rep(i,K)dp[1<<i][i] = 1;
    for(int bit = 0; bit < (1<<K); bit++){
        rep(i,K){
            if(bit & 1<<i){
                int bit2 = bit ^ 1<<i;
                rep(j,K){
                    if(bit2 & 1<<j){
                        dp[bit][i] = min(dp[bit][i],dp[bit2][j]+dist[i][j]);
                    }
                }
            }
        }
    }
    ll res = *min_element(dp.back().begin(),dp.back().end());
    if(res>=INF) res =  -1;
    cout<<res<<endl;
}

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>

using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
template<class T>
using vv = std::vector<std::vector<T> >;

#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)

#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)

void do_swap(vl &swaps, ll &Times, vector<bool> &Is_swaped, ll pos, vl &diffs, queue<ll> &Plus)
{
    swaps[Times ++] = pos+1;
    Is_swaped[pos] = true;
    swap(diffs[pos], diffs[pos+1]);

    diffs[pos] ++;
    diffs[pos+1] --;
    if(diffs[pos+1] > 0) Plus.push(pos+1);
}

int main(void)
{

    ll Sc, P, N, M, Q, A, B, C, D, K;

    cin >> N;
    queue<ll> Plus;
    vl diffs(N);
    bool resres = true;
    REP(i, N)
    {
        cin >> diffs[i];
        diffs[i] = diffs[i]-i-1;
        if(diffs[i] > 0)
        {
            Plus.push(i);
        }
        else if(diffs[i]==0)
        {
            resres = false;
        }
    }

    if(resres==false)
    {
        cout << -1 << endl;
        return 0;
    }

    REP(i, N-1)
    {
        if(diffs[i]>0 && diffs[i+1] >0 && diffs[i] > diffs[i+1])
        {
            cout << -1 << endl;
            return 0;
        }
        else if(diffs[i]<0 && diffs[i+1] <0 && diffs[i] > diffs[i+1])
        {
            cout << -1 << endl;
            return 0;
        }
    }

    vl swaps(N);
    ll Times = 0;
    vector<bool> Is_swaped(N-1, false);
    vl Move_Afts(N, -1);

    bool res_moved = true;

    while(!Plus.empty() )
    {
        ll pos;

        pos = Plus.front(); Plus.pop();

        if(Is_swaped[pos] == true || diffs[pos+1]==0)
        {
            break;
        }
        else if(diffs[pos+1]<0)
        {
            do_swap(swaps, Times, Is_swaped, pos, diffs, Plus);
            /*
            swaps[Times ++] = pos+1;
            Is_swaped[pos] = true;
            swap(diffs[pos], diffs[pos+1]);

            diffs[pos] ++;
            diffs[pos+1] --;
            if(diffs[pos+1] > 0) Plus.push(pos+1);
            */

            ll aft = Move_Afts[pos];
            while(aft >= 0 && diffs[aft+1]<0)
            {
                do_swap(swaps, Times, Is_swaped, aft, diffs, Plus);
                aft = Move_Afts[aft];
            }
        }
        else
        {
            Move_Afts[pos+1] = pos;
        }

    }

    if(Times == N-1)
    {
        // cout << Times << "  ";
        REP(i, Times)
        {
            cout << swaps[i] << endl;
        }
    }
    else
    {
        cout << -1 << endl;
    }

    return 0;
}


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

int main(){
    ll a,b,c; cin>>a>>b>>c;
    ll mod = 998244353;
    a = (a-1+1)*(1+a) / 2;
    a %= mod;
    b = (b - 1 + 1) * (1 + b) / 2;
    b %= mod;
    c = (c - 1 + 1) * (1 + c) / 2;
    c %= mod;
    ll ans = (((a*b)%mod)*c)%mod;
    cout << ans << endl;
    return 0;
}

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

int main()
{
    int X,Y,Z;
    cin >>X>>Y>>Z;
    cout<<(Y*Z-1)/X<<endl;
}


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

#define INF 1e18
typedef pair<long long, int> iPair;

const int maxN = 1e5;
vector<pair<int,pair<int,int> > > adj[maxN];

void shortestPath(int n, int src,int dest)
{

    priority_queue< iPair, vector <iPair> , greater<iPair> > pq;

    vector<long long> dist(n, INF);

    pq.push(make_pair(0, src));
    dist[src] = 0;

    while (!pq.empty())
    {
        int u = pq.top().second;
        pq.pop();

  		if(u == dest)
  		{
  			cout<<dist[dest]<<endl;
			return;
		}

        for (auto pr:adj[u])
        {
            int v = pr.first;
			long long t = pr.second.first;
			long long k = pr.second.second;

			long long curr_dist = ((dist[u] + k-1)/k)*k;

            if (dist[v] > curr_dist + t)
            {
                dist[v] = curr_dist + t;
                pq.push(make_pair(dist[v], v));
            }
        }
    }

   	cout<<-1<<endl;
}

int main()
{
    int n,m,x,y;
    cin>>n>>m>>x>>y;
    x--,y--;

    int u,v;
    int t,k;

    while(m--)
    {
    	cin>>u>>v>>t>>k;
		u--,v--;

		adj[u].push_back({v,{t,k}});
		adj[v].push_back({u,{t,k}});
	}

	shortestPath(n,x,y);

	return 0;
}

// Author: HarshKumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 1e6 + 6, M = 1e9 + 7;
const ll INF = (1LL << 62) - 1;
vector<vector<tuple<ll, ll, ll>>> G;
vector<ll> dijkstra(ll v)
{
    vector<ll> ans(G.size(), INF), vis(G.size());
    ans[v] = 0;
    priority_queue<pair<ll, ll>> que;
    que.push({0, v});
    while (!que.empty())
    {
        auto [x, u] = que.top();
        que.pop();
        if (vis[u])
            continue;
        vis[u] = 1;
        for (auto [v, w, d] : G[u])
        {
            ll x = max(0LL, llround(ceil(sqrtl(d))) - ans[u] - 1);
            w += ans[u] + x + d / (x + ans[u] + 1);
            if (w < ans[v])
                ans[v] = w, que.push({-ans[v], v});
        }
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    ll n, m;
    cin >> n >> m;
    G.resize(n);
    while (m--)
    {
        ll a, b, c, d;
        cin >> a >> b >> c >> d;
        a--;
        b--;
        G[a].push_back({b, c, d});
        G[b].push_back({a, c, d});
    }
    auto d = dijkstra(0);
    if (d.back() < INF)
        cout << d.back();
    else
        cout << -1;
}


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

#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fs first
#define sc second
#define MP make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define sqr(x) ((x) * (x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 51;
vector<int> primes;
int cnt[N], d[N];

void init() {
    for (int i = 2; i < N; i++) {
        if (d[i] == 0) {
            d[i] = i;
            primes.push_back(i);
        }
        for (int x : primes) {
            if (x > d[i] || x * i >= N) break;
            d[x * i] = x;
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef FLameDragon
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        cnt[x] += 1;
    }
    const int INF = 1e18;
    int ans = INF;
    init();
    for (int mask = 0; (mask < (1 << sz(primes))); mask++) {
        int curAns = 1;
        for (int bit = 0; bit < sz(primes); bit++) {
            if ((mask >> bit) & 1) {
                curAns *= primes[bit];
            }
        }
        bool ok = 1;
        for (int i = 2; i < N; i++) {
            if (cnt[i] && __gcd(curAns, i) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) {
            ans = min(ans, curAns);
        }
    }
    cout << ans << '\n';
}

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

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

int main(){
    ll n;
    cin >> n;
    vector<ll> a(405,0);
    rep(i,n){
        ll x;
        cin >> x;
        a[x+200]++;
    }

    ll ans=0;

    for(ll i=0;i<=400;i++){
        if(a[i]==0) continue;
        for(ll j=i+1;j<=400;j++){
            ll now=(j-i)*(j-i);
            ans+=now*a[i]*a[j];
        }
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
    for (in_vec_cout res : vec) {cout << res << " ";}
    cout << endl;
}

const ll inf = 1e18;
const ll mod = 1e9 + 7;

struct xyz{ll y, z;};

ll popcount(ll bit, ll N) {
    ll res = 0;
    rep(i,N) if ((bit>>i)&1) res++;
    return res;
}

int main() {
    ll N, M; cin >> N >> M;
    vector<vector<ll>> A(N,vector<ll>(N,inf));
    rep(i,M) {
        ll x, y, z; cin >> x >> y >> z;
        x--; y--;
        chmin(A[x][y],z);
    }

    vector<vector<ll>> dp(N+1,vector<ll>(1<<N));
    dp[0][0] = 1LL;
    rep(i,N) {
        rep(bit,1<<N) {
            ll cnt = popcount(bit,N);
            if (cnt!=i) continue;
            rep(j,N) {
                if ((bit>>j)&1) continue;
                ll nbit = bit | (1<<j);
                ll ncnt = 0;
                bool NG = false;
                rep(k,N) {
                    if ((nbit>>k)&1) ncnt++;
                    if (ncnt>A[i][k]) {
                        NG = true;
                        break;
                    }
                }
                if (NG) continue;
                dp[i+1][nbit] += dp[i][bit];
            }
        }
    }

    ll ans = dp[N][(1<<N)-1];

    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define vec2(name,i,j,k) vector<vector<ll>> name(i,vector<ll>(j,k))
#define vec3(name,i,j,k,l) vector<vector<vector<ll>>> name(i,vector<vector<ll>>(j,vector<ll>(k,l)))
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

ll powe(ll a, ll b, ll mod){//aのb乗のmod
    ll ret = 1;
    while(0 < b){
        if((b%2) == 0){
            a = (a*a)%mod;
            b >>= 1;
        }
        else{
            ret = (ret*a)%mod;
            b--;
        }
    }
    return ret;
}

int main(){
    ll a, b, c; cin >> a >> b >> c;
    a%=10;
    if (a==0) {
        cout << 0 << endl;
        return 0;
    }
    vector<vector<ll>> k(10);
    REP(i,10) k[i].pb(i);
    REP(i,10) {
        ll val = (i*i)%10;
        while (val!=k[i][0]) {
            k[i].pb(val);
            val = (val*i)%10;
        }
    }
    ll cnt = k[a].size();
    ll divd = powe(b,c,cnt);
    // cout << cnt << " " << divd << " " << a << endl;
    if (divd!=0) cout << k[a][divd-1] << endl;
    else cout << k[a][k[a].size()-1] << endl;
}