#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

// {{{ModInt
template <int modulo>
class ModInt {
 public:
  ModInt() : val_(0) {}

  ModInt(int v) {
    val_ = v % modulo;
    if (val_ < 0) val_ += modulo;
  }

  ModInt(long long v) {
    val_ = v % modulo;
    if (val_ < 0) val_ += modulo;
  }

  inline int val() const { return val_; }

  ModInt& operator++() {
    ++val_;
    if (val_ == modulo) val_ = 0;
    return *this;
  }

  ModInt& operator--() {
    if (val_ == 0) val_ = modulo;
    --val_;
    return *this;
  }

  ModInt operator++(int) {
    auto ret = *this;
    ++*this;
    return ret;
  }

  ModInt operator--(int) {
    auto ret = *this;
    --*this;
    return ret;
  }

  ModInt& operator+=(const ModInt& o) {
    val_ += o.val_;
    if (val_ >= modulo) val_ -= modulo;
    return *this;
  }

  ModInt& operator-=(const ModInt& o) {
    val_ -= o.val_;
    if (val_ < 0) val_ += modulo;
    return *this;
  }

  ModInt& operator*=(const ModInt& o) {
    val_ = (1ll * val_ * o.val_) % modulo;
    return *this;
  }

  ModInt& operator/=(const ModInt& o) { return *this *= o.Recip(); }

  ModInt Pow(long long n) const {
    ModInt a = *this;
    ModInt res(1);
    while (n > 0) {
      if (n & 1) res *= a;
      a *= a;
      n >>= 1;
    }
    return res;
  }

  ModInt Recip() const { return Pow(modulo - 2); }

  friend ModInt operator+(const ModInt& lhs, const ModInt& rhs) {
    return ModInt(lhs) += rhs;
  }

  friend ModInt operator-(const ModInt& lhs, const ModInt& rhs) {
    return ModInt(lhs) -= rhs;
  }

  friend ModInt operator*(const ModInt& lhs, const ModInt& rhs) {
    return ModInt(lhs) *= rhs;
  }

  friend ModInt operator/(const ModInt& lhs, const ModInt& rhs) {
    return ModInt(lhs) /= rhs;
  }

  friend bool operator==(const ModInt& lhs, const ModInt& rhs) {
    return lhs.val_ == rhs.val_;
  }

  friend bool operator!=(const ModInt& lhs, const ModInt& rhs) {
    return lhs.val_ != rhs.val_;
  }

 private:
  int val_;
};
// }}}

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
using i64 = long long;
using mint = ModInt<(int)1e9 + 7>;

vector<vector<pair<int, i64>>> g;
int n;
vector<i64> cumXor;

void dfs(int v, int p = -1, i64 xr = 0) {
  cumXor[v] = xr;
  for (auto [u, w] : g[v]) {
    if (u != p) {
      dfs(u, v, xr ^ w);
    }
  }
}

void solve() {
  scanf("%d", &n);
  g.assign(n, {});
  cumXor.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    i64 w;
    scanf("%d%d%lld", &u, &v, &w);
    --u; --v;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  dfs(0);

  mint ans = 0;
  i64 p2 = 1;
  for (int l = 0; l < 60; ++l) {
    int ones = 0, zeros = 0;
    for (i64 x : cumXor) {
      if ((x >> l) & 1) {
        ones++;
      } else {
        zeros++;
      }
    }

    for (i64 x : cumXor) {
      if ((x >> l) & 1) {
        ans += mint(zeros) * mint(p2);
      } else {
        ans += mint(ones) * mint(p2);
      }
    }
    p2 *= 2;
  }
  cout << (ans / 2).val() << endl;
}

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

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

#define ll long long int
#define fast ios_base::sync_with_stdio(false)
#define fast_input cin.tie(NULL)
#define fast_output cout.tie(NULL)
#define vi vector<long long int>
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(a,b) for(int a=0;a<b;a++)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
#define ordered_set tree<pa, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
vector<pa>g[1000001];
ll dp[1000001];
void dfs(ll v,ll par=-1)
{
        for(pa d:g[v])
        {
                if(d.first-par)
                {
                      dp[d.first]=dp[v]^d.second;
                      dfs(d.first,v);
                }
        }
}
void solve()
{
        ll n;
        cin>>n;
        f(i,n-1)
        {
             ll x,y,w;
             cin>>x>>y>>w;
             x--,y--;
             g[x].pb({y,w});
             g[y].pb({x,w});
        }
        dfs(0);
        ll ans=0;
        f(j,61)
        {
                ll ones,zeros;
                ones=zeros=0;
                f(i,n)
                {
                        if(dp[i]&(1ll<<j))
                        {
                                ones++;
                        }
                        else
                        {
                                zeros++;
                        }
                }
                zeros*=ones;
                zeros%=mod;
                ans+=((1ll<<j)%mod*zeros%mod);
                ans%=mod;
        }
        cout<<ans<<endl;
}
int main() {
	fast;
	fast_input;
	fast_output;
	// ll t;
	// cin>>t;
	// while(t--)
	// {
	  solve();
	// }
	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
#define fr first
#define sc second
#define all(v) v.begin(),v.end()
#define unq(v) sort( all(v) ); v.erase( unique( all(v) ), v.end() )
#define bout(x) cout << bitset<sizeof(x)*8>(x) << endl
#define mkp(a,b) make_pair(a,b)
#define gcd(a,b) __gcd(a,b)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using cpl = complex<ld>;
const ld pi = 3.14159265358979323846264338327950288;
const ld tau = 2 * pi;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, ddy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int nx[8] = {1, 2, 2, 1, -1, -2, -2, -1}, ny[8] = {-2, -1, 1, 2, 2, 1, -1, -2};

#define DEBUG

map<pll, ll> mp;
ll arr[300020];
ll pre[300020];

void Main(){
	int n; cin >> n;
	for (int i = 1; i <= n; i++){ cin >> arr[i]; }
	ll ans = 0;
	mp[mkp(0, 0)] = 1;
	for (int i = 1; i <= n; i++){
		pre[i] = arr[i] - pre[i-1];
		pll p = {pre[i], i&1};
		if (mp.count(p) == 0){ mp[p] = 1; }
		else{ mp[p] = mp[p] + 1; }
		pll p1 = {pre[i], i&1}, p2 = {-pre[i], ~i&1};
		ll x1 = mp[p1], x2 = mp[p2];
		ans += x1-1 + x2;
	}
	cout << ans;
}

int main(){
#ifndef DEBUG
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#endif
	cout.setf(ios::fixed);
	cout.precision(0);
	Main();
}

#include <bits/stdc++.h>

using namespace std;
#define int long long

long long MOD =1e9+7;

vector<vector<long long> > m(3e3+10,vector<long long>(3e3+10,-1));
vector<long long> v;
vector<vector<int> > tps;
long long bourrin(int i, int from) {
    if(from == -1) return 0;
    if(m[i][from]!= -1) {
        return m[i][from];
    }
    if(from >= v.size()) {
        return 1;
    }

    long long res = bourrin(i,tps[from][i]);
    if(tps[from][i+1] != -1) {
        res += bourrin(i+1,tps[from][i+1]);
    }
    res%=MOD;
    m[i][from] = res;
    return res;

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin>>N;
    v=vector<long long>(N);
    for(int c=0;c<N;c++) cin>>v[c];

    vector<vector<int> > modulos(N+10,vector<int>(N+10,-2));
    for(int c=0;c<N+10;c++) modulos[c][0]=-1;
    vector<long long> current(N+10,0);

    tps=vector<vector<int> > (N+10,vector<int> (N+10,-1));

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

    for(int c=0;c<N;c++) {
        for(int c2=1;c2<=N;c2++){
            current[c2] += v[c];
            current[c2] %= c2;

            if(modulos[c2][current[c2]] != -2) {
                tps[N-1-c][c2] = N-1-modulos[c2][current[c2]];

            }
            modulos[c2][current[c2]]=c;
        }
    }

//    return 0;

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

    cout<<bourrin(1,1);

}


#include <iostream>
#include <vector>
#include <set>

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;
#define x first
#define y second
#define mp make_pair

const int MAXN = (int)2e5;
int n, q;
ll a[MAXN + 5], t[MAXN + 5], x[MAXN + 5], sol[2 * MAXN + 5];
vector <int> ms[2 * MAXN + 5];
set <pll> s;

void dfs(int x, int p){
	for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = *i;
		if(o == p)
			continue;
		sol[o] = sol[x];
		dfs(o, x);
	}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; ++i){
	cin >> a[i] >> t[i];
}
cin >> q;
for(int i = 1 ; i <= q ; ++i){
	cin >> x[i];
	s.insert(mp(x[i], i));
}
ll sad = 0;
for(int i = 1 ; i <= n ; ++i){
	if(t[i] == 1){
		sad += a[i];
	}
	else if(t[i] == 2){
		while(!s.empty() && (s.begin())->x <= a[i] - sad){
			int o = s.begin()->y;
			s.erase(s.begin());
			ms[i + q].push_back(o);
		}
		s.insert(mp(a[i] - sad, i + q));
	}
	else{
		while(!s.empty() && (--s.end())->x >= a[i] - sad){
			int o = (--s.end())->y;
			s.erase((--s.end()));
			ms[i + q].push_back(o);
		}
		s.insert(mp(a[i] - sad, i + q));
	}
}
for(set <pll>::iterator i = s.begin() ; i != s.end() ; ++i){
	pll o = *i;
	sol[o.y] = o.x + sad;
	dfs(o.y, -1);
}
for(int i = 1 ; i <= q ; ++i){
	cout << sol[i] << "\n";
}

return 0;
}

#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, ll>;
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i--)
const int MOD = 1000000007;
const int MOD2 = 998244353;
const ll INF = 1e18;
const ld PI = acos(-1);

class UnionFind {
public:
    vector<int> par;
    vector<int> rnk;
    vector<ll> siz;
    vector<int> isequal;
    UnionFind(int N): par(N), rnk(N), siz(N), isequal(N){
        for(int i = 0; i < N; i++){
            par[i] = i, rnk[i] = 0, siz[i] = 1;
        }
    }
    //根
    int find(int x){
        if(par[x] ==x )return x;
        return par[x] = find(par[x]);
    }
    void unite(int x, int y){
        int rx = find(x);
        int ry = find(y);
        if (rx == ry) return;
        if(rnk[rx] < rnk[ry]){
            par[rx] = ry;
            siz[ry] += siz[rx];
            siz[rx] = 0;
        }
        else{
            par[ry] = rx;
            siz[rx] += siz[ry];
            siz[ry] = 0;
            if(rnk[rx] == rnk[ry]){
                rnk[rx]++;
            }
        }
    }
    //集合が同じか
    bool same(int x, int y){
        return find(x) == find(y);
    }
    //集合の大きさ
    ll size(int x){
        return siz[find(x)];
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,Q;
    cin >> N;
    vector<ll> A(N),T(N);
    ll delta=0;
    rep(i,0,N-1){
        cin >> A[i] >> T[i];
        if(T[i]==1) delta+=A[i];
    }
    cin >> Q;
    vector<int> X(Q);
    rep(i,0,Q-1){
        cin >> X[i];
    }
    ll mx=INF,mn=-INF;
    rep(i,0,N-1){
        if(T[i]==1){
            mx+=A[i];
            mn+=A[i];
        }
        else if(T[i]==2){
            mx=max(A[i],mx);
            mn=max(A[i],mn);
        }
        else if(T[i]==3){
            mx=min(A[i],mx);
            mn=min(A[i],mn);
        }
    }
    /*cout << delta << endl;
    cout << mx << ' ' << mn << endl;*/
    rep(i,0,Q-1){
        if(X[i]+delta>=mx){
            cout << mx << endl;
        }
        else if(X[i]+delta<=mn){
            cout << mn << endl;
        }
        else{
            cout << X[i]+delta << endl;
        }
    }
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define int long long
#define ll long long
#define all(v) v.begin(), v.end()
#define intvect vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define fo(i, n) for (int i = 0; i < n; ++i)
#define Fo(i, k, n) for (int i = k; i < n; ++i)
#define ld long double
#define deb(x) cout << #x << " " << x << endl;
#define pb push_back
#define pob pop_back
#define lcm(a, b) (a / __gcd(a, b) * b)
#define F first
#define S second
#define ull unsigned long long

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const ll mod = 10000007;
const ll N = (ll)5e5 + 4;

template <typename... T>
void read(T &... args)
{
    ((cin >> args), ...);
}

template <typename... T>
void write(T &&... args)
{
    ((cout << args), ...);
}

void vjudge()
{
#ifndef RTE
    if (fopen("input.txt", "r"))
    {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

bool comp(pair<int, int> x, pair<int, int> y)
{
    if (x.first != y.first)
        return x.first < y.first;
    return x.second < y.second;
}
void solve()
{
    int n, w;
    cin >> n >> w;

    vector<pair<int, int>> e;
    for (int i = 0; i < n; ++i)
    {
        int a, b, p;
        cin >> a >> b >> p;
        e.emplace_back(a, -p);
        e.emplace_back(b - 1, p);
    }
    sort(e.begin(), e.end());
    pair<int, int> ans = {0, -1};
    int cur = 0;
    for (pair<int, int> p : e)
    {
        cur += -p.second;
        if (cur > ans.first)
        {
            ans = {cur, p.first};
        }
    }
    if (ans.first > w)
        cout << "No";
    else
        cout << "Yes";
}
int32_t main()
{
    clock_t start, end;
    start = clock();
    vjudge();
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    end = clock();
    double waste = double(end - start) / double(CLOCKS_PER_SEC);
    cerr << "Time wasted by program is : " << fixed
         << waste << setprecision(15);
    cerr << " sec " << endl;
}

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

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x)           (int)x.size()
#define db(x)           cout<<x<<"\n";

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//insert,find_by_order,order_of_key,lower_bound,upper_bound;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

//////////////////////////////code//////////////////////////////

const int N = 1e3 + 5;

vector<pair<int,char> >  adj[N];
int is[N][N];
int dp[N][N];

void solve()
{

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

		for(int i = 0;i < m; i++)
		{
				int u,v;
				char w;
				cin >> u >> v >> w;
				adj[u].pb({v,w});
				adj[v].pb({u,w});
				is[u][v] = is[v][u] = 1;
				//deb(v,u);
				//deb(sz(adj[u]),sz(adj[v]));
		}

		queue<pii>q;
	//	int dp[n + 1][n + 1];

		for(int i = 1;i <= n; i++)
			for(int j = 1;j <= m; j++)
				dp[i][j] = inf;

		dp[1][n] = 0;
		q.push({1,n});
		int ans = inf;

	//	deb(sz(adj[n]));

		while(!q.empty())
		{
				pii p = q.front();
				int node1 = p.ff, node2 = p.ss;
				q.pop();

				if(is[node1][node2])
				{
				 	 	ans = min(ans,dp[node1][node2] + 1);
				 	 	//deb(node1,node2,"1");
				  		continue;
				}
				//deb(node1,node2);

			    for(auto &c1 : adj[node1])
			    {
			    	//deb("OK");
			    		for(auto &c2 : adj[node2])
			    		{
			    			//deb("OK");

			    				int child1 = c1.ff , child2 = c2.ff;
			    				int col1 = c1.ss , col2 = c2.ss;

			    			//	deb("Ok");

			    				if(col1 != col2 || dp[child1][child2] != inf)
			    					continue;

			    				 dp[child1][child2] = 2 + dp[node1][node2];

			    				if(child1 != child2)
			    				{
			    					q.push({child1,child2});
			    				//	deb(child1,child2,"Push");

			    				}
			    				else
			    				{
			    					ans = min(ans,dp[child1][child2]);
			    				//	deb(child1,child2,"2");
			    				}

			    		}
			    }
		}

		if(ans == inf)
			db(-1)
		else
			db(ans)

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

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

    int t = 1;
   //cin >> t;

    while (t--)
        solve();

}

// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans = 0;
template<typename T>
struct HamiltonGraph {
    int n;
    T M;
    vector<vector<T>> DP, dt, rt;
    const T INF = numeric_limits<T>::max();

    HamiltonGraph (const int n_) : n(n_), M(1ll<<n_) {
        dt.assign(n_, vector<T>(n_, INF));
    }

    T Calc_MinimumCost (int s, int g = -1){
        DP.assign(M, vector<T>(n, INF));
        rt.assign(M, vector<T>(n, -1));
        DP[1<<s][s] = 0;
        for(ll i = 0; i < M; i++){
            for(int j = 0; j < n; j++){
                if(i & (1ll<<j)){
                    for(int k = 0; k < n; k++){
                        if(DP[i][j] != INF && DP[i|(1ll<<k)][k] > DP[i][j]+dt[j][k]){
                            DP[i|(1ll<<k)][k] = DP[i][j]+dt[j][k];
                            rt[i|(1ll<<k)][k] = j;
                        }
                    }
                }
            }
        }
        if(g < 0) g = min_element(DP[M-1].begin(), DP[M-1].end())-DP[M-1].begin();
        return DP[M-1][g];
    }

    vector<int> Build_ShortestPath (int s = 0, int g = -1){
        vector<int> path;
        T m = M-1;
        if(g < 0) g = min_element(DP[M-1].begin(), DP[M-1].end())-DP[M-1].begin();
        int now, nxt; now = nxt = g;
        if(s == g){
            path.push_back(g);
            now = nxt = rt[m][g];
            ans += dt[now][g];
        }
        while(m != (1<<s) || now != s){
            path.push_back(now);
            now = rt[m][now];
            ans += dt[now][nxt];
            m ^= (1<<nxt);
            nxt = now;
        }
        path.push_back(now);
        reverse(path.begin(), path.end());
        return path;
    }
};

int main()
{

    int n;
    cin >> n;

    vector<ll> x(n),y(n),z(n);

    for (ll i = 0; i < n; ++i) {
        cin >> x[i] >> y[i] >> z[i];
    }

    HamiltonGraph<ll> g(n);
    for (ll i = 0; i < n; i++){
        for (ll j = 0; j < n; j++){
            if(z[j]-z[i] > 0) g.dt[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]) + z[j]-z[i];
            else g.dt[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
        }
    }
    g.Calc_MinimumCost(0,0);
    g.Build_ShortestPath(0,0);
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

const int MAX_N = 18;

int dp[(1<<MAX_N)][MAX_N];

void init(){
  rep(i,(1<<MAX_N)){
    rep(j,MAX_N){
      dp[i][j] = INF;
    }
  }
}

int main() {
  int n; cin >> n;
  vector<vector<int> > dist(n,vector<int>(n,-INF));
  vi x(n), y(n), z(n);
  rep(i,n){
    cin >> x[i] >> y[i] >> z[i];
  }

  rep(i,n){
    rep(j,n){
      if(i == j){
        dist[i][j] = 0;
        continue;
      }
      dist[i][j] = abs(x[j]-x[i]) + abs(y[j]-y[i]) + max(0,z[j]-z[i]);
    }
  }

  init();
  rep(i,n){
    if(i == 0) continue;
    dp[1<<i][i] = dist[0][i];
  }

  rep(b,(1<<n)){
    if(b == 0) continue;
    rep(j,n){
      if(!((b>>j)&1)) continue;
      rep(k,n){
        if((b>>k)&1) continue;
        dp[b|(1<<k)][k] = min(dp[b|(1<<k)][k], dp[b][j]+dist[j][k]);
      }
    }
  }
  cout << dp[(1<<n)-1][0] << endl;

  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count());
#define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n"
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
template<int modd> struct modint {
	static const int mod = modd;
	int v; explicit operator int() const { return v;}
	modint() : v(0) {}
	modint(ll x) : v(int(x%mod)) {if (v < 0) v += mod;}
	friend bool operator==(const modint& a, const modint& b) {return a.v == b.v;}
	friend bool operator!=(const modint& a, const modint& b) {return a.v != b.v;}
	friend bool operator<(const modint& a, const modint& b) {return a.v < b.v;}
	modint& operator+=(const modint& a) {if ((v += a.v) >= mod) v -= mod; return *this;}
	modint& operator-=(const modint& a) {if ((v -= a.v) < 0) v += mod; return *this;}
	modint& operator*=(const modint& a) {v = int((ll)v*a.v%mod); return *this;}
	modint& operator/=(const modint& a) {return (*this) *= inv(a);}
	friend modint pow(modint a, ll p) {modint ret = 1; for (; p; p /= 2, a *= a){if (p&1) ret *= a;} return ret;}
	friend modint inv(const modint& a) {return pow(a,mod-2);}
	modint operator-() const {return modint(-v);}
	modint& operator++() {return *this += 1;}
	modint& operator--() {return *this -= 1;}
	friend modint operator+(modint a, const modint& b) {return a += b;}
	friend modint operator-(modint a, const modint& b) {return a -= b;}
	friend modint operator*(modint a, const modint& b) {return a *= b;}
	friend modint operator/(modint a, const modint& b) {return a /= b;}
	friend ostream& operator<<(ostream& out, modint a) {return out << a.v;}
	friend istream& operator>>(istream& in, modint& a) {ll b; in >> b; a.v = b; return in;}
};
using mint = modint<998244353>;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	if (n == 1) {
		if (m == 1) {
			cout << k << "\n";
		} else {
			mint x = k;
			cout << pow(x,m) << "\n";
		}
	} else {
		if (m == 1) {
			mint x = k;
			cout << pow(x,n) << "\n";
		} else {
			mint ans = 0;
			for (int q = 1; q <= k; q++) {
				mint x = pow(mint(q),n) - pow(mint(q-1),n);
				x *= pow(mint(k-q+1),m);
				ans += x;
			}
			cout << ans << "\n";
		}
	}
	return 0;
}

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

const int N = 40, M = 1<<(N / 2);

int n, t, a[N];

vector<ll> v1(M), v2(M);

int main() {
  cin >> n >> t;
  rep(i, n) cin >> a[i];

  rep(bit, M) {
    ll now = 0;
    rep(i, N / 2) {
      if (bit & 1<<i) {
        now += a[i];
      }
    }
    v1[bit] = now;
  }

  rep(bit, M) {
    ll now = 0;
    rep(i, N / 2) {
      if (bit & 1<<i) {
        now += a[N / 2 + i];
      }
    }
    v2[bit] = -now;
  }

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

  ll ans = 0;
  rep(i1, M) {
    ll now = v1[i1];
    if (now > t) continue;
    int i2 = lower_bound(v2.begin(), v2.end(), now - t) - v2.begin();
    now -= v2[i2];
    ans = max(ans, now);
  }
  cout << ans << endl;
  return 0;
}


#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
const ll MAXN = 400011;

struct fhqTreap {
	ll tot, rt;
	ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN];

	void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];}

	void split(ll u, ll k, ll &x, ll &y) {
		if(!u) {x = y = 0; return;}
		if(val[u] <= k) x = u, split(rs[u], k, rs[u], y);
		else y = u, split(ls[u], k, x, ls[u]);
		pushup(u);
	}

	ll merge(ll u, ll v) {
		if(!u || !v) return u | v;
		if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;}
		else {ls[v] = merge(u, ls[v]); pushup(v); return v;}
	}

	ll add(ll k) {
		rd[++tot] = rand();
		sz[tot] = 1;
		val[tot] = sum[tot] = k;
		return tot;
	}

	void insert(ll k) {
		ll x, y;
		split(rt, k, x, y);
		rt = merge(merge(x, add(k)), y);
	}

	ll cb(ll k) {
		ll x, y;
		split(rt, k, x, y);
		ll ans = sz[x] * k + sum[y];
		rt = merge(x, y);
		return ans;
	}

	void delet(ll k) {
		ll x, y, z;
		split(rt, k, x, z);
		split(x, k - 1, x, y);
		y = merge(ls[y], rs[y]);
		rt = merge(merge(x, y), z);
	}
} t[2];

ll q;
ll len[2];
ll w[2][MAXN];

ll read() {
	ll X = 0, F = 1; char C = getchar();
	while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();}
	while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();}
	return X * F;
}

int main() {
	ll ans = 0;
	len[1] = read(); len[0] = read(); q = read();
	for(ll i = 0; i < 2; ++i)
		for(ll j = 1; j <= len[i]; ++j)
			t[i].rt = t[i].merge(t[i].rt, t[i].add(0));
	for(ll i = 1, opt, x, y; i <= q; ++i) {
		opt = (read() & 1); x = read(); y = read();
		t[opt].delet(w[opt][x]);
		t[opt].insert(y);
		ans -= t[opt^1].cb(w[opt][x]);
		w[opt][x] = y;
		ans += t[opt^1].cb(w[opt][x]);
		printf("%lld\n", ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct binary_indexed_tree{
	int N;
	vector<T> BIT;
	binary_indexed_tree(int n){
		N = 1;
		while (N < n){
			N *= 2;
		}
		BIT = vector<T>(N + 1, 0);
	}
	void add(int i, T x){
		i++;
		while (i <= N){
			BIT[i] += x;
			i += i & -i;
		}
	}
	T sum(int i){
		T ans = 0;
		while (i > 0){
			ans += BIT[i];
			i -= i & -i;
		}
		return ans;
	}
	T sum(int L, int R){
		return sum(R) - sum(L);
	}
	T all(){
		return BIT[0];
	}
};
int main(){
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> T(Q), X(Q), Y(Q);
  for (int i = 0; i < Q; i++){
    cin >> T[i] >> X[i] >> Y[i];
    X[i]--;
  }
  vector<int> c = Y;
  c.push_back(0);
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
  int cnt = c.size();
  binary_indexed_tree<long long> Acnt(cnt), Bcnt(cnt);
  binary_indexed_tree<long long> Asum(cnt), Bsum(cnt);
  Acnt.add(0, N);
  Bcnt.add(0, M);
  vector<int> a(N, 0), b(M, 0);
  long long ans = 0;
  for (int i = 0; i < Q; i++){
    if (T[i] == 1){
      int curr_id = lower_bound(c.begin(), c.end(), a[X[i]]) - c.begin();
      int nxt_id = lower_bound(c.begin(), c.end(), Y[i]) - c.begin();
      ans -= Bsum.sum(curr_id, nxt_id);
      ans -= Bcnt.sum(0, curr_id) * a[X[i]];
      ans += Bcnt.sum(0, nxt_id) * Y[i];
      Acnt.add(curr_id, -1);
      Asum.add(curr_id, -a[X[i]]);
      Acnt.add(nxt_id, 1);
      Asum.add(nxt_id, Y[i]);
      a[X[i]] = Y[i];
    }
    if (T[i] == 2){
      int curr_id = lower_bound(c.begin(), c.end(), b[X[i]]) - c.begin();
      int nxt_id = lower_bound(c.begin(), c.end(), Y[i]) - c.begin();
      ans -= Asum.sum(curr_id + 1, nxt_id + 1);
      ans -= Acnt.sum(0, curr_id + 1) * b[X[i]];
      ans += Acnt.sum(0, nxt_id + 1) * Y[i];
      Bcnt.add(curr_id, -1);
      Bsum.add(curr_id, -b[X[i]]);
      Bcnt.add(nxt_id, 1);
      Bsum.add(nxt_id, Y[i]);
      b[X[i]] = Y[i];
    }
    cout << ans << endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define fi first
#define se second
#define UP(a,b,c) for(ll (a)=(b);(a)<(c);(a)++)
#define UU(a,b,c) for(ll (a)=(b);(a)<=(c);(a)++)
#define DN(a,b,c) for(ll (a)=(b);(a)>(c);(a)--)
#define DU(a,b,c) for(ll (a)=(b);(a)>=(c);(a)--)

int fen[300005], n, q, a[300005], t, x, y;

int get(int idx)
{
	int res = 0;
	while(idx > 0)
	{
		res  ^= fen[idx];
		idx -= (idx & -idx);
	}
	return res;
}

void update(int idx, int val)
{
	while(idx <= n)
	{
		fen[idx]  ^= val;
		idx += (idx & -idx);
	}
}

void reset()
{
}

void input()
{
	cin >> n >> q;
	UU(i, 1, n)
	{
		cin >> a[i];
		update(i, a[i]);
	}
}

void solve()
{
	while(q--)
	{
		cin >> t >> x >> y;
		if(t == 1)
		{
			update(x, y);
		}
		else
		{
			int a = get(x - 1);
			int b = get(y);
			cout << (a ^ b) << endl;
		}
	}
}

void LetsRock()
{
	solve();
}

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

		input();
		reset();
		LetsRock();

	return 0;
}

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

int main() {
  int N, M, T;
  int R = 0;
  cin >> N >> M >> T;
  int L = N;

 for (int i = 0; i < M; i++){
   int A = 0, B = 0;
   cin >> A >> B;
   L -= (A - R);
   if (L <= 0){
     break;
   }
   else{
     L += (B - A);
     if(L > N) {
       L = N;
     }
     R = B;
   }
 }
  if (L > 0) {
    L -= (T - R);
  }
  else{
  }
  if (L > 0) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }

}


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

#ifdef Dhiraj
    #include "D:/dhiraj/Programming/debug.h"
#else
    #define d(...) 11
    #define cerr if(0) cerr
#endif

#define ll long long int
#define endl '\n'

template <typename T, typename U>
inline istream& operator >> (istream& in, pair<T, U>& p) {
    in >> p.first >> p.second;
    return in;
}
template <typename T>
inline istream& operator >> (istream& in, vector<T>& v) {
    for(T& x : v) in >> x;
    return in;
}

const ll MX = 4e5 + 5;

ll n, m;
vector<vector<ll>> g;
vector<ll> vis;

void clearAll()
{
    vis.assign(MX, 0);
    g.assign(MX, vector<ll>());
}

ll cc, edges;
void dfs(ll u)
{
    if(vis[u] || g[u].empty()) return;
    cc++;
    vis[u] = 1;
    for(ll v : g[u]) {
        edges++;
        dfs(v);
    }
}

void solve(int &tc)
{
    cin >> n;
    clearAll();

    for(ll i = 0; i < n; i++)
    {
        ll x, y;
        cin >> x >> y;
        g[x].emplace_back(y);
        g[y].emplace_back(x);
    }

    ll ans = 0;
    for(ll i = 0; i < MX; i++)
    {
        cc = edges = 0;
        dfs(i);
        edges /= 2;
        ans += min(cc, edges);
    }
    cout << ans << endl;
}

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
        freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
        // freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
    #endif

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

    int tc = 1;
    for(int i = 1; i <= tc; i++) {
        cerr << "Case #" << i << "\n";
        solve(i);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int sum=0;
int dd=0;
int vg=0;
int d[400000];
vector<int> ed[400000];
int vis[400000];
void dfs(int v,int p)
{
	//cout<<v<<endl;
	sum+=ed[v].size();
	vis[v]=1;
	vg++;
	if(d[v]!=0) dd++;
	if(ed[v].size()==0) return;
	for(int i=0;i<ed[v].size();i++)
		if(vis[ed[v][i]]==0) dfs(ed[v][i],v);
}
int main() {
	int n;
	cin>>n;
		for(int i=0;i<400000;i++) {vis[i]=0;d[i]=0;}
	while(n--)
	{
		int x,y;
		cin>>x>>y;
		x--;y--;
		if(x!=y)
		{
			ed[x].push_back(y);
		    ed[y].push_back(x);
	    }
	    else d[x]++;
	}
	int ans=0;//cout<<d[110]<<endl;

	for(int i=0;i<400000;i++)
	 {
	   if(vis[i]==0)
	   {
	   	 dfs(i,-1);
	   	 //if(i==110) cout<<dd<<endl;
	   	 if(sum/2==vg-1 && dd==0) ans+=vg-1;
	   	 else ans+=vg;
	   	 sum=0;
	   	 dd=0;
	   	 vg=0;
	   }
	 }
	 cout<<ans<<endl;
	return 0;
}


#include <iostream>
using namespace std;

int main(){
  int n,sum=0;
  int a[n];

  cin>>n;

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

return 0;
}

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <queue>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

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

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

template <class T> inline void chadd(T &a, const T &b) {
  a += b, a %= MOD;
  // TODO minus case
}

template <class T>
std::ostream &operator<<(std::ostream &stream, const std::vector<T> &vec) {
  for (auto val : vec) {
    if (std::is_fundamental<T>::value)
      stream << " " << val;
    else
      stream << std::endl << val;
  }
  return stream;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  for (int i = 0; i < N; ++i)
    std::cin >> A[i];

  int res = 0;
  for (int i = 0; i < N; ++i) {
    if (A[i] >= 10) {
      res += A[i] - 10;
    }
  }
  std::cout << res << std::endl;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i, n) rep(i, 1, n)
#define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;

// char buf[1 << 20],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
template <class o, class... O> void qr(o& x, O&... y) {
    qr(x);
    qr(y...);
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
template <class o, class... O> void pr1(o x, O... y) {
    pr1(x);
    pr1(y...);
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(10);
}
template <class o, class... O> void pr2(o x, O... y) {
    pr2(x);
    pr2(y...);
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}

const int mod = 998244353;
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

const int N = 5010, inf = 2e9;
const ll INF = 1e15;
int n, m, f[N], g[N], C[N][N];

void solve() {
    qr(n, m);
    f[m] = 1;
    rep(i, 0, n) {
        C[i][0] = 1;
        FOR(j, i) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    }
    if (m & 1) {
        return pr2(0);
    }
    while (m) {
        memcpy(g, f, sizeof g);
        memset(f, 0, sizeof f);
        for (int i = 0; i <= m; i += 2)
            if (g[i]) {
                for (int j = 0; j <= i; j += 2)
                    ad(f[(i - j) / 2], (ll)g[i] * C[n][j] % mod);
            }
        m /= 2;
    }
    pr2(f[0]);
}

int main() {
#ifndef ONLINE_JUDGE
    clock_t start_time = clock();
#endif
    int T = 1;
    // qr(T);
    while (T--)
        solve();
#ifndef ONLINE_JUDGE
    cerr << 1.0 * (clock() - start_time) / CLOCKS_PER_SEC << ' ' << 's' << endl;
#endif
    return 0;
}

#include<bits/stdc++.h>
#define inf 1e18
#define endl "\n"
#define mp make_pair
#define pb push_back
#define loop(i,x,y) for(int i = x; i < y ; i++ )
#define all(x) (x).begin(),(x).end()
#define in(n)       int n; cin>>n;
#define inarr(arr,n) vector<int>arr(n); for(int i = 0; i < n ; i++){cin>>arr[i];}
#define maploop(x) for(auto itr = x.begin(); itr != x.end();itr++)
#define int long long
using namespace std;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

const int N = 50001;
const int mod = 998244353;

int fac[N];
int mul(int a, int b)
{
	return (a * b) % mod;
}
void pre()
{
	fac[0] = 1;
	loop(i, 1, N)
	{
		fac[i] = mul(fac[i - 1], i);
	}
}
int pow(int a, int b)
{
	int z = 1;
	while (b  > 0 )
	{
		if (b % 2)
		{
			z = mul(a, z);
		}
		a = mul(a, a);
		b = b / 2;

	}
	return z;
}
int div(int a)
{
	int ans;
	return ans = pow(a, mod - 2);
}
int comb(int n, int k)
{
	if (n >= k)
	{
		return mul(fac[n], div(mul(fac[k], fac[n - k])));
	}
	else
		return 0;
}
void Main()
{
	in(n)
	in(m)
	vector<int>vis(m + 1, 0);
	vis[0] = 1;
	loop(i, 0, 18)
	{
		vector<int>temp(m + 1, 0);
		//temp = vis;
		int inc = 0;
		for (int j = 0 ; j <= n; j += 2)
		{
			loop(k, 0, m + 1)
			{
				if (inc + k > m)
					break;
				//cout << inc + k << endl;
				temp[inc + k] += (vis[k] * comb(n, j)) % mod;
				if (temp[inc + k] >= mod)
				{
					temp[inc + k] -= mod;
				}
			}
			inc += 2 * (1 << i);
		}
		vis = temp;

	}
	cout << vis[m] << endl;
}

int32_t main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	pre();
//	cin >> t;
	while (t--)
	{
		Main();
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
typedef long long ll;
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.first>>v.second;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.first<<","<<v.second;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) { cerr << head; dump(); }
template <typename Head, typename... Tail> void dump(Head &&head, Tail &&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do { cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

const ll LINF = 1LL << 60;
const int INF = 1001001001;

/////////////////////////////////////////////////////////////////////

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

  vector<int> a(n-1);
  rep(i, n-1) a[i] = i+1;

  int ans = 0;
  do {
    int sum = t[0][a[0]] + t[a[n-2]][0];
    rep(i, n-2) {
      sum += t[a[i]][a[i+1]];
    }
    if (sum == k) ans++;
  } while (next_permutation(a.begin(), a.end()));
  cout << ans << "\n";

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)
#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
template<typename T> bool chmax(T& a, const T b) {if(a<b) {a=b;return true;} return false;}
template<typename T> bool chmin(T& a, const T b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
    for(auto &x:arr) {cout << x << " ";} cout << endl;
}

/*

## 關鍵詞：

## 題意：

## 思路：

## 雜多：

*/

void solve() {
    int n,m; cin>>n>>m;
    vector<vector<tuple<int,ll,ll>>> edge(n+1);

    range(i, 1, m) {
        int a,b; ll c,d; cin>>a>>b>>c>>d;
        edge[a].emplace_back(b,c,d);
        edge[b].emplace_back(a,c,d);
    }

    using P = pair<ll,int>;
    priority_queue<P, vector<P>, greater<P>> pq;
    lvector dist(n+1, LINF);
    dist[1] = 0; pq.emplace(0, 1);
    while(pq.size()) {
        auto [t, u] = pq.top(); pq.pop();
        if(dist[u] < t) continue;

        if(u == n) {
            cout << t << endl;
            return;
        }

        // 到達頂點u所需的最短時刻爲t
        for(auto [v,c,d]: edge[u]) {
            // 對每個鄰點v求最短時刻v_t
            // time of u->v:
            // = t + c + floor(d/t+1)
            // = floor(t + c + d/(t+1))
            // = floor(t + 1 + d/(t+1) + (c-1))
            // 所以答案大致在滿足 (t+1)^2 = d 的t附近
            double _x = sqrt((double)d); _x -= 1;
            int x = (int)_x;
            int bestx = 0; ll ans = LINF;
            for(int i = max(0, x-2); i <= x+2; ++i) {
                if(chmin(ans, i+c+d/(i+1))) bestx = i;
            }

            // 當 t = bestx 的時候這條路的經過時間取到最小值
            if(t >= bestx) { // 立即出發
                if(chmin(dist[v], t+c+d/(t+1)))
                    pq.emplace(dist[v], v);
            }else{ // 待機到bestx
                if(chmin(dist[v], bestx+c+d/(bestx+1)))
                    pq.emplace(dist[v], v);
            }
        }
    }

    cout << "-1" << endl;
}

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


                  /**Bismillahir Rahmanir Rahim.**/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int   ull;
typedef long long int            ll;
typedef long double              ld;
#define Faster  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi                 acos(-1);
#define test        int t; cin>>t; while(t--)
#define dot(x)      fixed<<setprecision(x)
const double EPS = 1e-12;
bool isEqual(ld a, ld b) {
    return abs(a - b) <= EPS;
}
bool isGreater(ld a, ld b) {
    return a > b + EPS;
}
bool isSmaller(ld a, ld b) {
    return a + EPS < b;
}
// cin.ignore();
//scanf("%[^\n]%*c", str);
//sort(s,s+n,greater<int>());
//cin.getline(array,size);
//s.erase(s.begin()+s.size()-1,s.end()); last index
int main()
{
    #ifndef ONLINE_JUDGE
        clock_t tStart = clock();
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    Faster
    ll n,m,t,a[10001],b[10001],s=0,p=0,q=0,i;
    cin>>n>>m>>t;
    s=n;
    for(i=0;i<m;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(i=0;i<m;i++)
    {
        if(i==0)
        {
            if(s-a[i]<=0)
            {
                s=0;
                p=1;
            }
            else
                s=s-a[i];
            s+=(b[i]-a[i]);
            if(s>n)
                s=n;
        }
        else
        {
            s-=(a[i]-b[i-1]);
            if(s<=0)
            {
                s=0;
                p=1;
            }
            s+=(b[i]-a[i]);
            if(s>n)
                s=n;
        }
    }
    if(t-b[m-1]>=s)
        p=1;
    if(p==1)
        cout<<"No"<<endl;
    else
        cout<<"Yes"<<endl;

    #ifndef ONLINE_JUDGE
        fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
    #endif
}

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

#define int long long
#define double long double

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

    int n;
    int x;
    cin >> n >> x;

    int curr = 0;

    for(int i = 0; i < n; i++){
        int a, b;
        cin >> a >> b;

        curr += a*b;

        if(curr > x*100){
            cout << i+1 << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
}


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

int main()
{
   int n,w;
   cin>>n>>w;
   cout<<n/w;
   return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,w;
	scanf("%d%d",&n,&w);
	printf("%d\n",n/w);
	return 0;
}


#include <bits/stdc++.h>
using namespace std ;
#define int         long long
#define pb			push_back
#define ppb			pop_back
#define pf			push_front
#define ppf			pop_front
#define all(x)		(x).begin() , (x).end()
#define sz(x)		(int)((x).size())
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define ppc			__builtin_popcount
#define mp          make_pair

  void solve(){
    float a,b,c,d;
    cin>>a>>b>>c>>d;
  //int i=0;
  if(d*c <= b)
    {
      cout<< "-1";
    return;
    }
   else{
      cout<<(int)ceil((a/(c*d - b))) ;
   }

  }

signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int k =1;
 //cin>>k;
while(k--){
    // cout<<n;
   solve();
}
}

#include<iostream>
#include<bitset>
#include<string>
#include<vector>
#include<queue>
#include<random>
#include<map>
#include<set>
#include<stack>
#include<functional>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<ctime>

using namespace std;
using Graph = vector<vector<int>>;
const long long INF = 1LL << 60;
typedef long long ll;

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

//ll の最大値　2147483647≒10^9
//long longの最大値　9,223,372,036,854,775,807≒10^18

ll gcd(ll a, ll b) {
    if (a % b == 0) {
        return b;
    }
    else {
        return gcd(b, a % b);
    }
}
ll digit(ll x) {
    ll d = 0;
    while (x) {
        d++;
        x /= 10;
    }
    return d;
}
ll modpow(ll a, ll n, ll mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) :par(n, -2), siz(n, 1) {}
    void red(int x) {
        par[x] = -1;
    }
    int root(int x) {
        if (par[x] == -1) {
            return x;
        }
        else {
            return par[x] = root(par[x]);
        }
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);

        if (x == y) {
            return false;
        }
        if (siz[x] < siz[y]) {
            swap(x, y);
        }
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int  size(int x) {
        return siz[root(x)];
    }
};
template<class T> void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}
template<class T> void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}

int main() {
    double A, B, C, D;
    cin >> A >> B >> C >> D;
    if (C * D - B <= 0) {
        cout << -1 << endl;
    }
    else {
        cout << ceil(A / (C * D - B)) << endl;
    }
}


// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(long long i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"

int gcd(int a,int b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
	ll g = a;
	x = 1;
	y = 0;
	if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
	return g;
}
ll crt(ll div1,ll mod1,ll div2,ll mod2){
	ll x,y;
	ll d=extgcd(div1,div2,x,y);
	if(d-1)assert(0);
	ll ndiv=div1*div2;
//	ll nmod=(x*(mod2-mod1)*div1+mod1)%ndiv;
	ll nmod=(mod1*div2%ndiv*(ndiv+y)%ndiv+mod2*div1%ndiv*(ndiv+x)%ndiv)%ndiv;
	return nmod;
}

typedef int SegT;
const SegT defvalue=0;
class SegTree{
	private:
		vector<SegT>val;
		int n;
		SegT combine(SegT a,SegT b){return a+b;}
	public:
		SegTree(int size){
			n=1;
			while(n<size)n<<=1;
			val=vector<SegT>(2*n,defvalue);
		}
		SegTree(const vector<SegT> &in){
			n=1;
			while(n<in.size())n<<=1;
			val=vector<SegT>(2*n,defvalue);
			for(int i=n-1+in.size()-1;i>=0;i--){
				if(n-1<=i)val[i]=in[i-(n-1)];
				else val[i]=combine(val[i*2+1],val[i*2+2]);
			}
		}
		void update(int i,SegT a){
			i+=n-1;
			val[i]=a;
			while(i>0){
				i=(i-1)/2;
				val[i]=combine(val[i*2+1],val[i*2+2]);
			}
		}
		SegT query(int a,int b,int k=0,int l=0,int r=-1){//[a,b)
			if(r==-1)r=n;
			if(r<=a||b<=l)return defvalue;
			if(a<=l&&r<=b)return val[k];
			else return combine(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
		}
		void tmp(){
			rep(i,val.size())cout<<" "<<val[i];cout<<endl;
		}
};
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n,m,x;
	cin>>n>>m>>x;
	vvi in(x,vi(2));
	rep(i,x)rep(j,2)cin>>in[i][j],in[i][j]--;
	vi h(n,m),w(m,n);
	rep(i,x){
		cmin(h[in[i][0]],in[i][1]);
		cmin(w[in[i][1]],in[i][0]);
	}
//	show1d(h);
//	show1d(w);
	int out=0;
	rep(i,w[0])out+=h[i];

	vp t;

	rep(i,w[0])t.pb({h[i],i});
	if(w[0]-n){
		t.pb({0,w[0]});
		loop(i,w[0]+1,n)t.pb({0,i});
	}
	sort(all(t));
	SegTree st(n);
	int it=0;
	rep(i,h[0]){
		//cout<<w[i]<<" "<<st.query(0,w[i])<<endl;
		out+=st.query(0,w[i]);
		while(it!=t.size()&&t[it].first==i){
//			cout<<t[it].first<<" "<<t[it].second<<endl;
			st.update(t[it].second,1);
			it++;
		}
	}
	cout<<out<<endl;
}
/*
 ....
 .#..
 #...
 ...#
 .#..

*/

/*
	int q;
	cin>>q;
	while(q--){
		int n,a,b;
		cin>>n>>a>>b;//b*x mod n == n-a
		int g=gcd(n,b);
		if(a%g){cout<<-1<<endl;continue;}
		a/=g;b/=g;n/=g;
//		cout<<n<<" "<<a<<" "<<b<<endl;
		cout<<crt(n,n-a,b,0)/b<<endl;

	}
}
// b*x mod n == n-a
// 3x mod 10 == 6
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 400005;

vector<int> BIT(N, 0);
vector<bool> seen(N, false);

void update(int x, int delta) {
	for (; x <= N; x += x & -x)
		BIT[x] += delta;
}
int query(int x) {
	int sum = 0;
	for (; x > 0; x -= x & -x)
		sum += BIT[x];
	return sum;
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int h, w, m;
	cin >> h >> w >> m;
	vector<int> row(h + 1, w + 1), col(w + 1, h + 1);
	vector<vector<int>> pos(N);

	for (int i = 1; i <= m; ++i) {
		int x, y;
		cin >> x >> y;
		pos[y].push_back(x);
		row[x] = min(row[x], y);
		col[y] = min(col[y], x);
	}
	ll res = 0;
	for (int i = 1; i < col[1]; ++i) res += row[i] - 1;
	for (int i = col[1] + 1; i <= h; ++i) {
		update(i, 1);
		seen[i] = 1;
	}
	for (int i = 1; i < row[1]; ++i) {
		res += query(col[i] - 1);
		for (int &v : pos[i]) {
			if (!seen[v]) update(v, 1);
			seen[v] = 1;
		}
	}
	cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
ll x, y;
map<ll, ll> vis, cache;
ll solve(ll val);
int main() {
	//freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> x >> y;
	cout << solve(y) << endl;
	return 0;
}
ll solve(ll val) {
	if (vis[val]) return cache[val];
	vis[val] = 1;
	cache[val] = 1e18;
	ll &ans = cache[val];
	ans = min(ans, abs(val - x));
	if (val == 1) return ans;
	if (!(val & 1)) ans = min(ans, 1 + solve(val / 2));
	else ans = min(ans, 1 + min(solve(val + 1), solve(val - 1)));
	return ans;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
    int x,y;
    cin>>x>>y;
    if(x>=y){
        cout<<x-y;
        return 0;
    }
    int ans=y-x;
    queue<pair<int,int>>q;
    q.push({0,y});
    set<int>s;
    while(!q.empty()){
        pair<int,int>f=q.front();
        q.pop();
        if(s.find(f.second)!=s.end())
            continue;
        s.insert(f.second);
        ans=min(ans,f.first+abs(x-f.second));
        if(f.second<x)
            continue;
        if(f.second&1){
            q.push({f.first+1,f.second-1});
            q.push({f.first+1,f.second+1});
        }
        else
            q.push({f.first+1,f.second>>1});
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db long double
#define fi first
#define se second
#define mem(x) memset(x,0,sizeof x)
db x0,y00,r;
bool isv(int x,int y){
	db dx=1.0*x-x0;
	db dy=1.0*y-y00;
	dx*=dx;dy*=dy;
	dx+=dy;
	return dx<=(r)+1e-10;
}
int main(){
	cin>>x0>>y00>>r;
	int xl=ceil(x0-r);
	int xr=floor(x0+r);ll ans=0;
	//cout<<xl<<" "<<xr<<endl;
	if(xl>xr) {
		cout<<0<<endl;
		return 0;
	}
	r*=r;
	for(int x=xl;x<=xr;x++){
		db dx=x-x0;
		dx*=dx;
		db dr=r-dx;
		if(dr<-1e-10) break;
		dr=abs(dr);
		dr=sqrt(dr);
		//cout<<dr<<endl;
		db yl=y00-dr;
		db yr=y00+dr;
	//	cout<<yyl<<" "<<yyr<<endl;
		int yyl=ceil(yl);
		int yyr=floor(yr);
		//cout<<yyl<<" "<<yyr<<endl;
		if(yyr-yyl<=20){
			for(int j=yyl-1;j<=yyr+1;j++){
				ans+=isv(x,j);
			}
			continue;
		}
		//if(yyl>yyr) break
		for(int j=yyl-5;j<=yyl+5;j++) ans+=isv(x,j);
		for(int j=yyr-5;j<=yyr+5;j++) ans+=isv(x,j);
	//	cout<<yyl<<" "<<yyr<<endl;
		ans+=(yyr-yyl-11);
		//cout<<"?"<<x<<" "<<yyr-yyl+1<<endl;
	}
	cout<<ans<<endl;
}

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

using ll = long long;
using PII = pair<char, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ll MOD = 998244353;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    if (N == 1) {
        cout << modpow(K, M, MOD) << endl;
        return 0;
    }
    if (M == 1) {
        cout << modpow(K, N, MOD) << endl;
        return 0;
    }
    ll ans = 0;
    for (int x = 1; x <= K; x++) {
        ll a = modpow(x, N, MOD);
        (a += MOD - modpow(x - 1, N, MOD)) %= MOD;
        ll b = modpow(K - x + 1, M, MOD);
        (ans += a * b % MOD) %= MOD;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
#define _GLIBCXX_DEBUG
#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define repr(i, n, s) for (int i = (int)n; (int)s < i; i--)
#define len(a) (int)a.size()
#define all(a) a.begin(), a.end()

const int INF32 = 1 << 30;
const long long INF64 = 1LL << 60;
const double PI = acos(-1);

/* 1次元ベクトルをスペースでつなぎ、改行をつけて出力
   vi, vl, list<int>, list<ll>に対応  */
template<class T> string print_elements(T a) {
    string str = "";
    for (auto itr = a.begin(); itr != a.end(); ++itr) {
        str += to_string(*itr) + ' ';
    }
    str.pop_back();  // 最後の空白を削除
    str += '\n';
    return str;
}

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

    double tax = (100.0 + t) / 100.0;

    double d = ceil(1.0 * N * 100.0 / (1.0 * t));

    ll ans = (ll)(d * (100.0 + t) / 100) - 1LL;

    cout << ans << endl;

}

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

#define fo(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    ll a[n];
    ll ma=0LL;
    fo(i,n){
        ll x;
        cin>>x;
        ma=max(ma,x);
        a[i]=ma;
    }
    ll b[n];
    fo(i,n){
        cin>>b[i];
    }
    ma=0LL;
    ll prefb[n];
    for(int i=n-1;i>=0;i--){
        ma=max(ma,b[i]);
        prefb[i]=ma;
    }
    ma=0;
    for(int i=0;i<n;i++){
        ma=max(a[i]*b[i],ma);
        cout<<ma<<"\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;

const int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
const int m = 20;
ll dp[1<<m];
int main() {
  ll a,b;
  cin >> a >> b;
  int n = b-a+1;
  vector<int> v(n);
  for(ll i=a;i<=b;++i)rep(j,m) if(i%p[j]==0) v[i-a] |= 1<<j;

  dp[0] = 1;
  rep(i,n)rep(x,1<<m){
    if(x&v[i]) continue;
    dp[x|v[i]] += dp[x];
  }
  ll ans = 0;
  rep(x,1<<m) ans += dp[x];
  cout << ans << endl;
}

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int n = ri();
	int x = ri();
	int a[n];
	for (auto &i : a) i = ri();

	bool beginning = true;
	for (int i = 0; i < n; i++) {
		if (a[i] == x) continue;
		if (beginning) beginning = false;
		else printf(" ");
		printf("%d", a[i]);
	}
	puts("");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(n) begin(n),end(n)
typedef vector<int> vi;
typedef vector<string> vs;
const int mod = 1000000007;
const int inf = 1061109567;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
signed main()
{
    int n;
    cin >> n;
    if(!(n%2)) cout << "White" << endl;
    else cout << "Black" << endl;
    return 0;
}

#include <iostream>
#include <set>
#include <cmath>

using namespace std;
int main(){
    long long int n,k; cin>>n;
    set<long long int> s;
    for(int a=2;a<=100000;a++){
        for(int b=2;b<=34;b++){
            k=pow(a,b);
            if(k>n) break;
            else s.insert(k);
        }
    }
    cout<<n-s.size()<<endl;

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef   long long int ll;
typedef long double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
long long int M = 1e9 + 7 ;
long long int inf = 9 * 1e18;
//CLOCK
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//CLOCK ENDED
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
	if (deg < 0)
		return 0;
	if (!deg)
		return 1 % M;
	if (deg & 1)
		return binpow(val, deg - 1) * val % M;
	ll res = binpow(val, deg >> 1);
	return (res * res) % M;
}
//binomial
ll modinv(ll n) {
	return binpow(n, M - 2);
}

int main() {
	// your code goes here
	IOS;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll i, j, t, k, x, y, z,  N;
	cin >> t;
	while (t--) {
		cin >> n;
		ll a[n];
		rep(i, n) cin >> a[i];
		if (n % 2) {
			cout << "Second";
		}
		else {
			map<ll, ll> cnt;
			rep(i, n) {
				cnt[a[i]]++;
			}
			ll flg = 0;
			for (auto u : cnt) {
				if (u.se % 2)flg = 1;
			}
			if (flg == 0) {
				cout << "Second";
			}
			else
				cout << "First";
		}
		cout << '\n';
	}

	return 0;

}

// Second
// Second
// First
// First
// Second

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>
#include <omp.h>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;
typedef pair<long long int, pll> lpll;

const long long int mod = 1000000007;
const long long int INF = 1e18;
const long double PI=3.14159265358979323;
//const long long int pl=1000000;

long long int T,N,M;
long long int A[300005],B,C,D,res=0,w[10],l[200200],v[200200];
int main()
{
    cout << fixed << setprecision(18);
    cin>>T;
    for(long long int q = 0; q < T; q++){
        cin>>N;
        for(long long int i = 0; i < N; i++){
            cin>>A[i];
        }
        sort(A,A+N);
        if(N%2==1){
            cout<<"Second"<<endl;
            continue;
        }
        bool key =true;;
        for(long long int i = 0; i < N; i+=2){
            if(A[i]!=A[i+1]){
                cout<<"First"<<endl;
                key=false;
                break;
            }
        }
        if(key)cout<<"Second"<<endl;
    }

}


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

void solve(){
    int n, s, d; cin>>n>>s>>d;
    for(int i=0; i < n; i++){
        int x, y; cin>>x>>y;
        if (x < s && y > d) {
            cout<<"Yes";
            return;
        }
    }
    cout<<"No";

}

int main(){

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

#ifndef USE_FILE_IO
    ios_base::sync_with_stdio(0); cin.tie();
#endif

    solve();
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 100001
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<ll,Pi> Pli;
typedef pair<Pi,Pi> Pi2;
typedef pair<P1,ll>P2;
const ll INF=1000000000000000001;
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct pos{
	// 1 変数を入れる;
	int t,x,y;
};
int main(){

	ll s,d;
	int n;
	cin>>n>>s>>d;
	rep(i,n){
		ll x,y;
		cin>>x>>y;
		if(x<s&&y>d){
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;

	return 0;
}


#include<bits/stdc++.h>				//Written by ThiDaiLoc
using namespace std;				//Team Three Wolves
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> trp;
typedef vector<ll> vi;
typedef vector<pll> vp;

#define fu(i,a,b)   for(ll i=a;i<=b;i++)
#define f1(i,n)     for(ll i=1;i<=n;i++)
#define fs(i,s)     for(ll i=0;i+1<=s.length();i++)
#define fd(i,b,a)   for(ll i=b;i>=a;i--)
#define fuv(i,a)    for(ll i=0;i<a.size();i++)
#define fdv(i,a)    for(ll i=(ll)a.size()-1;i>=0;i--)
#define ms(a,x)     memset(a, x, sizeof a)
#define prec(n)     fixed<<setprecision(n)
#define uni(a)      (a).erase(unique(all(a)), (a).end())
#define pb(i)       push_back(i)
#define pob         pop_back()
#define sc(a)       cin>>a
#define sc2(a,b)    cin>>a>>b
#define sc3(a,b,c)  cin>>a>>b>>c
#define pr(a)       cout<<a<<endl
#define pr2(a,b)    cout<<a<<" "<<b<<endl
#define rpr(a)      return cout<<a<<endl,0
#define prY         cout<<"YES"<<endl
#define prN         cout<<"NO"<<endl
#define bit(n,i)    (((n)>>(i))&1)
#define lowb(a,n,x) lower_bound(a,a+n,x) -a
#define lowb2(a,x)  lower_bound(all(a),x) -a.begin()
#define all(x)      (x).begin(), (x).end()
#define sz(a)       (ll)a.size()
#define le(s)       (ll)s.length()
#define re          return
#define	mp(a,b)     make_pair(a,b)
#define	mp3(a,b,c)  make_pair(a,make_pair(b,c))
#define se          second
#define fi          first
#define sse         second.second
#define sfi         second.first
#define soA         sort(A+1,A+1+n)
#define so(A,n)     sort(A+1,A+1+n)
#define sov(v)      sort(all(v))
#define sovr(v)     sort(all(v),greater<ll>())
#define debug(x)    cerr << #x << " = " << x << endl
#define INPUT       freopen("locin.txt", "r", stdin)
#define OUTPUT      freopen("locout.txt", "w", stdout)
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline ll mnz(ll& a,ll b){return a=(a>b?b:a);}
inline ll mxz(ll& a,ll b){return a=(a<b?b:a);}
inline string toString(ll n) {stringstream ss; ss << n;return ss.str();}
double const eps = 1e-6;
ll const Base=1e9+7,oo=1e17,MAXN=1e6;

pll A[MAXN+5];
pll B[MAXN+5];
pll add[MAXN+5];
pll xy[MAXN+5];
ll Solves(){

	ll n,m,k,cnt=0,ans=0,x,y,q,c,sum=0,v,t;
	// PROCESS IN HERE
	cin>>n;
	f1(i,n)sc2(A[i].fi,A[i].se);

	sc(m);
	xy[0]=mp(1,2);
	f1(i,m){
		sc(x);
		if(x>=3){
			sc(y);
		}
		if(x==1){
			xy[i]=mp(xy[i-1].se,-xy[i-1].fi);
			add[i]=mp(add[i-1].se,-add[i-1].fi);
		}
		if(x==2){
			xy[i]=mp(-xy[i-1].se,xy[i-1].fi);
			add[i]=mp(-add[i-1].se,add[i-1].fi);
		}
		if(x==3){
			xy[i]=xy[i-1];
			xy[i].fi=-xy[i].fi;
			add[i]=add[i-1];
			add[i].fi=-add[i].fi;
			add[i].fi+=y*2;
		}
		if(x==4){
			xy[i]=xy[i-1];
			xy[i].se=-xy[i].se;
			add[i]=add[i-1];
			add[i].se=-add[i].se;
			add[i].se+=y*2;
		}
	}
	sc(q);
	f1(i,q){
		sc2(x,y);
		if(abs(xy[x].fi)==1){
			v=A[y].fi;
			if(xy[x].fi<0)v=-v;
			t=A[y].se;
			if(xy[x].se<0)t=-t;
		}
		else{
			v=A[y].se;
			if(xy[x].fi<0)v=-v;
			t=A[y].fi;
			if(xy[x].se<0)t=-t;
		}

		v+=add[x].fi;
		t+=add[x].se;
		cout<<v<<" "<<t<<endl;
	}

	re 0;
	// Hack it if you can :)
}

int main(){
	//INPUT;
	if(fopen("locin.txt", "r"))INPUT;
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll test=1;
//	sc(test);
	fu(T,1,test){
//		cout<<"Case #"<<T<<": ";
		Solves();
	}
}


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

LL X, Y;
map<LL, LL> memo;
LL solve(LL y) {
    if(memo.count(y)) return memo[y];
    if(y == 1) return memo[y] = llabs(X-y);
    if(y % 2 == 1) return memo[y] = min(llabs(X-y), min(solve((y+1)/2) + 2, solve((y-1)/2) + 2));
    if(y % 2 == 0) return memo[y] = min(llabs(X-y), solve(y/2) + 1);
    return memo[y];
}

int main() {
    memo.clear();
    cin >> X >> Y;
    if(Y <= X){
        cout << X - Y << endl;
        return 0;
    }
    cout << solve(Y) << "\n";
}

//#include <atcoder/all>
//using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPR(i, n) for(int i=n-1; i>=0; i--)
#define FOR(i, m, n) for(int i=m; i<n; i++)
#define ALL(v) v.begin(), v.end()
#define bit(n) (1LL<<(n))
#define FIX(d) fixed << setprecision(d)
using P = pair<int, int>;
using LP = pair<ll, ll>;
using Graph = vector<vector<int>>;
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 = 1e9;
const ll LINF = (1LL<<60);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll X,Y; cin >> X >> Y;
  ll ans = LINF;
  map<ll, ll> memo;
  priority_queue<LP, vector<LP>, greater<LP>> que;
  que.emplace(0, Y);
  while(!que.empty()){
    auto [v, x] = que.top();
    que.pop();

    if(v>=ans) continue;
    chmin(ans, v+abs(X-x));

    int r = (x&1);
    ll nv = v+r+1;
    int d = 2;
    if(nv<ans && (!memo.count(x/d) || nv<memo[x/d])){
      que.emplace(nv, x/d);
      memo[x/d] = nv;
    }
    nv = v+(d-r)+1;
    if(nv<ans && (!memo.count(x/d+1) || nv<memo[x/d+1])){
      que.emplace(nv, x/d+1);
      memo[x/d+1] = nv;
    }
  }
  cout << ans << endl;
  return 0;
}


/*
    author:  Maksim1744
    created: 23.05.2021 16:09:04
*/

#include "bits/stdc++.h"

using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order, order_of_key

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

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) void(0)
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#endif

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

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    cin >> a >> b;
    for (int i = 0; i < n; ++i) {
        a[i] += i;
        b[i] += i;
    }
    map<int, deque<int>> where;
    for (int i = 0; i < n; ++i) {
        where[a[i]].pb(i);
    }
    {
        auto x = a;
        auto y = b;
        sort(x.begin(), x.end());
        sort(y.begin(), y.end());
        if (x != y) {
            cout << -1 << '\n';
            return 0;
        }
    }

    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        p[i] = where[b[i]][0];
        where[b[i]].pop_front();
    }

    ordered_set<int> s;
    ll ans = 0;
    for (int x : p) {
        ans += s.size() - s.order_of_key(x);
        s.insert(x);
    }
    cout << ans << '\n';

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i,a,b) for (ll i = (a); i < (ll)(b); i++)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define MT make_tuple
#define fr first
#define sc second

using namespace std;
template <class T, class U> inline bool chmax(T &a, const U &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return (a > b ? a = b, 1 : 0); }
template <class T> using vc = vector<T>;
template <class T> using vv = vc<vc<T>>;
template <class T> using vvv = vc<vv<T>>;
template <class T> using PQ = priority_queue<T, vc<T>, greater<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using ti3 = tuple<int, int, int>;
using tl3 = tuple<ll, ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
using vs = vc<string>;
using vpl = vc<pll>;
using vpd = vc<pdd>;
using vvi = vv<int>;
using vvl = vv<ll>;
using vvpl = vv<pll>;

const ld pi = 3.141592653589793;
const ll linf = 1001001001001001001LL;
const int inf = 1001001001;

int main() {
  vvpl primeFact(1010101);
  repr(i,2,1010101) {
    if (primeFact[i].size() > 0) continue;
    for (ll j = i; j < 1010101; j += i) {
      if (j%(i*i) != 0) primeFact[j].eb(i, 1);
      else primeFact[j].eb(i, 2);
    }
  }
  ll L, R;
  cin >> L >> R;
  ll ans = (R-L+1)*(R-L)/2;
  repr(i,max(2LL, L),R+1) ans -= R/i-(L-1)/i-1;
  ll sum = (R-L+1)*(R-L)/2;
  repr(i,2,R+1) {
    vpl &pf = primeFact[i];
    bool ok = true;
    for (auto [p, a] : pf) if (a >= 2) ok = false;
    if (ok) {
      ll m = pf.size();
      ll C = R/i-(L-1)/i;
      sum -= C*(C-1)/2*(m%2 == 0 ? -1 : 1);
    }
  }
  ans -= sum;
  ans *= 2;
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,t,n) for(int64_t i=t;i<n;i++)
#define Sort(a) sort(a.begin(),a.end())
#define rSort(a,n,m) sort(a.begin()+n-1,a.begin()+m)
#define Reverse(a) reverse(a.begin(),a.end())
#define rReverse(a,n,m) reverse(a.begin()+n-1,a.begin()+m)
#define MOD 1000000007
#define sign(i) -1*pow(-1,i)
#define vi(A,N,i)  vector<int64_t> A(N,i)
#define vs(A,N,i)  vector<string> A(N,i)
#define vb(A,N,i)  vector<bool> A(N,i)
#define INF ((1LL<<62)-(1LL<<31))
//((a+b+c)-max(a,max(b,c))-min(a,min(b,c)))

/*template <class T>

}*/

//cout << fixed << setprecision(桁数);

int64_t def_str(string x,string y){
  int64_t def=0;
  rep(i,0,(int64_t)x.size()){
    if(x[i]!=y[i]){
      def++;
    }
  }
  return def;
}

int main() {
  int64_t a,b,c,d,e,f,ans=0,now_ans=0,mlt_ans=1;
  //double a,b,c,d,e,f,ans=0,now_ans=0,mlt_ans=1;
  string x,y,z;
  char s,t,u;
  bool frag=false,frag1=false,frag2=false;
  vector<int64_t> A{},B{};
  vector<string> C{},D{};
  vector<char> E{},F{};
  vector<bool> G(26,false);
  cin>>a>>b;
  //cin>>x>>y;
  //cin>>s>>t;////////////////////////

  rep(i,0,a){
    cin>>c>>d;
    ans+=c*d;
    if(ans>b*100){
      cout<<i+1;
      return 0;
    }
  }
  cout<<-1;
}

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

int main() {
  long long N,X;
  cin>>N>>X;
  long long total=0;
  long long count=0;
  bool jud=0;
  for(long long a=0;a<N;a++){
    long long v,p;
    cin>>v>>p;
    total+=v*p;
    count++;
    if(total>(100*X)){

      jud=1;
      break;
    }
  }
  //cout<<total<<endl;
  if(jud){
    cout<<count;
  }
  else{
    cout<<"-1";
  }

}


#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <deque>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include <iostream>
#include <stdio.h>
#include <time.h>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=b-1LL;i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)

#define MOD 1000000007

long long modpow(long long a, long long b){
  long long r = 1LL;
  while(b){
    if(b & 1LL)r *= a;
    if(r >= MOD)r %= MOD;
    a *= a;
    if(a >= MOD)a %= MOD;
    b >>= 1LL;
  }
  return r%MOD;
}

ll d1[2010][2010];
ll d2[2010][2010];
ll d3[2010][2010];
ll d4[2010][2010];

int main(){
  ll h,w;
  cin>>h>>w;
  vector<string> vs;
  ll sp = 0;
  rep(y,0,h){
    string s;
    cin>>s;
    vs.pb(s);
  }
  // 1
  rep(x,0,w){
    ll c = 0;
    rep(y,0,h){
      if(vs[y][x]=='#'){
        c=0;
      }
      else{
        sp++;
        d1[y][x]=c;
        c++;
      }
    }
  }
  // 2
  rep(x,0,w){
    ll c = 0;
    per(y,0,h){
      if(vs[y][x]=='#'){
        c=0;
      }
      else{
        d2[y][x]=c;
        c++;
      }
    }
  }
  // 3
  rep(y,0,h){
    ll c = 0;
    rep(x,0,w){
      if(vs[y][x]=='#'){
        c=0;
      }
      else{
        d3[y][x]=c;
        c++;
      }
    }
  }
  // 4
  rep(y,0,h){
    ll c = 0;
    per(x,0,w){
      if(vs[y][x]=='#'){
        c=0;
      }
      else{
        d4[y][x]=c;
        c++;
      }
    }
  }
  ll ans = 0;
  rep(y,0,h){
    rep(x,0,w){
      if(vs[y][x]=='.'){
        ll a = d1[y][x] + d2[y][x] + d3[y][x] + d4[y][x] + 1;
      ll c = 1;
      c *= modpow(2,sp-a);
      c %= MOD;
      c *= modpow(2,a)+MOD-1;
      c %= MOD;
      ans += c;
      ans %= MOD;
    }
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
using namespace chrono;

typedef long long int ll;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define pb push_back
#define odd(x) ((x)&1)
#define even(x) (!odd(x))
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(auto i=0;i<n;++i)
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define TEST_CASE int tc;cin>>tc;while(tc--)
#define Clock high_resolution_clock::now()

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

#ifdef LOCAL
#define cerr cout
#else
#endif

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// #define TIME

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

const ll inf = 2e18;
const ll mod = 998244353;
const ll N = 2e3;

void prt(vll &v) {
    cout << v.size() << " ";
    for(auto i: v) cout << i << " ";
    cout << "\n";
}

int main()
{
    auto start_time = Clock;
    FASTIO

    ll n;
    cin >> n;
    vll v(n);
    rep(i,n) cin >> v[i];

    map<ll, vector<vll> > mp;

    ll m = min(8ll,n);

    rep(i,1<<m) {
        if(!i) continue;
        vll tmp;
        ll sum = 0;
        for(ll j=0;j<m;++j) {
            if(i&(1<<j)) {
                sum += v[j]; sum %= 200;
                tmp.pb(j+1);
            }
        }
        // cout << bitset<4>(i) << " " << sum << "\n";
        mp[sum].pb(tmp);
    }

    for(auto p: mp) {
        if(p.second.size()>1) {
            cout << "Yes\n";
            // trace(p.first);
            prt(p.second[0]), prt(p.second[1]);
            return 0;
        }
    }
    cout << "No\n";

    auto end_time = Clock;
    #ifndef TIME
        return 0;
    #endif
    cout << "\nTime elapsed: "
    << (double)duration_cast<milliseconds>(end_time-start_time).count()
    << "ms";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define vecp vector<pair<ll,ll> >
#define mapp map<ll,ll>
#define mapit map<ll,ll>::iterator
#define mapr map<pair<ll,ll>,ll>
#define maprit map<pair<ll,ll>,ll>::iterator
#define fs first
#define sc second
#define bg begin()
#define en end()
#define pb push_back
#define fin(x,y) for(x=0;x<y;x++)
#define fin1(x,y) for(x=1;x<y;x++)
#define mod1 1000000007
#define INF 1000000000000000000
#define twodp vector<vec >dp(n+1,vec(k+1,0))
#define all(x) x.bg,x.en
#define rel(x) x.rbegin(),x.rend()
using namespace std;
ll gcd(ll a,ll b)
{
  if(a==0 || b==0)
    return a+b;
  else
    return __gcd(a,b);
}
int main()
{
     ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  ll i,j,k,l,n;
  string s;
  cin>>s;
  n=s.size();
  bool p=true;
  fin(i,n)
  {
    if(i%2==1)
    {
      if(s[i]>='a'&& s[i]<='z')
      p=false;
    }
    else
    {
      if(s[i]>='A'&& s[i]<='Z')
      p=false;
    }
  }
  if(p)
  cout<<"Yes";
  else
  cout<<"No";
  return 0;
}

#include <iostream>
#include <string>

#include <ctype.h>
using namespace std;

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

  bool isDifficult = true;
  bool odd = true;
  for ( const auto &c : input )
  {
    if ( odd ) { if( isupper( c ) ) { isDifficult = false; break; } }
    else       { if( islower( c ) ) { isDifficult = false; break; } }

    odd = !odd;
  }

  cout << ( ( isDifficult ) ? "Yes" : "No" );

  return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

using _lint = int;
#define REP(i, n) for (_lint i = 0; i < (_lint)(n); i++)
#define FOR(i, a, b) for (_lint i = (_lint)(a); i < (_lint)(b); i++)
#define FORR(i, a, b) for (_lint i = (_lint)(b)-1; i >= (_lint)(a); i--)

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))
#define ALL(v) (v).begin(), (v).end()

#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v)                                                           \
  {                                                                            \
    cerr << #v << ": ";                                                        \
    REP(__i, size(v)) cerr << ((v)[__i]) << ", ";                              \
    cerr << endl;                                                              \
  }

int n, m;
bool g[25][21];
bool ok[1 << 20];

ll f[1<<20][21];
ll f2[1<<20][21];
ll ff[1<<20];

int main() {
  cin >> n >> m;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a][b] = g[b][a] = true;
  }
  REP(mask, 1 << n) {
    bool go = true;
    REP(i, n) REP(j, i) {
      if (mask >> i & 1) {
        if (mask >> j & 1) {
          if (g[i][j]) {
            go = false;
          }
        }
      }
    }
    ok[mask] = go;
  }

  // https://judge.yosupo.jp/submission/4085
  REP(i,1<<n)f[i][__builtin_popcount(i)] = ok[i];
  REP(k,n)REP(i,1<<n)if(((i>>k)&1)==0)REP(j,n+1)f[i|1<<k][j]+=f[i][j];
  REP(i,1<<n)REP(j,n+1)f2[i][j] = f[i][j];
  REP(i,1<<n)FORR(j,0,n+1){
    FOR(k,1,n-j+1)f[i][j+k] += f[i][j] * f2[i][k];
    f[i][j] *= f2[i][0];
  }
  REP(k,n)REP(i,1<<n)if(((i>>k)&1)==0)REP(j,n+1)f[i|1<<k][j]-=f[i][j];
  REP(i,1<<n)ff[i]=f[i][__builtin_popcount(i)];

  ll ans = 0;
  REP(mask, 1 << n) if (ok[mask]) {
    int ho = ~mask & ((1 << n) - 1);
    ans += ff[ho];
  }
  cout << ans << endl;
  return 0;
}

// -------- START : snippet -------- //

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

struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;

// -------- START : macro -------- //

#define ll long long
#define rep(i,n) for(int i=0;i<int(n);++i)
#define rep1(i,n) for(int i=1;i<=int(n);++i)
#define len(c) (ll)c.size()
#define all(v) (v).begin(),(v).end()
#define lb(v,x) (lower_bound(all(v),x)-v.begin())
#define ub(v,x) (upper_bound(all(v),x)-v.begin())
#define has(c,x) (c.find(x) != c.end())
#define uniq(v) v.erase(unique(all(v)),v.end())
#define bit(k) (1LL<<(k))
#define INF int(1e9)

template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

// -------- END : macro -------- //

// -------- START : debug macro -------- //

template<class T,class U> ostream& operator<<(ostream& ost, const pair<T,U> &p) { ost << "(" << p.first << ", " << p.second << ")"; return ost; }
#define ostream_container { ost << "{"; for(const auto &t : v) { if (&t != &*v.begin()) ost << ", "; ost << t; } ost << "}"; return ost; }
template<class T> ostream& operator<<(ostream &ost, const vector<T> &v) ostream_container
template<class T> ostream& operator<<(ostream &ost, const set<T> &v) ostream_container
template<class T,class U> ostream& operator<<(ostream &ost, const map<T,U> &v) ostream_container

template<class H> void recursive_debug(string s, H h) {
    cerr << "\033[33m" << s << " = " << h << endl << "\033[m";
}
template<class H,class... T> void recursive_debug(string s, H h, T... t) {
    int comma = s.find(',');
    cerr << "\033[33m" << s.substr(0,comma) << " = " << h << ", ";
    recursive_debug(s.substr(comma+1), t...);
}
#define debug(...) recursive_debug(#__VA_ARGS__, __VA_ARGS__)

// -------- END : debug macro -------- //

// -------- END : snippet -------- //

template <typename T>
class SegmentTree{
  public:
    int n;
    T element;
    vector<T> dat;
    function<T(T,T)> operate;

    SegmentTree(){}
    SegmentTree(int n_,T element_,function<T(T,T)> operate_){
        element = element_;
        operate = operate_;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(n*2,element);
    }

    void update(int k,T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = operate(dat[k*2+1],dat[k*2+2]);
        }
    }

    T query(int a,int b,int k,int l,int r){
        if(r<=a || b<=l) return element;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a,b,k*2+1,l,(l+r)/2);
        T vr = query(a,b,k*2+2,(l+r)/2,r);
        return operate(vl,vr);
    }

    T query(int a,int b) { return query(a,b,0,0,n); }

    T get(int k){ return query(k,k+1); }
};

int main(void) {
    int N, Q;
    cin >> N >> Q;
    vector<ll> a(N);
    SegmentTree<ll> seg(N, 0, [](ll x, ll y){ return x^y; });
    rep(i,N) {
        cin >> a[i];
        seg.update(i, a[i]);
    }
    rep(q,Q) {
        int t;
        ll x,y;
        cin >> t >> x >> y;
        if (t == 1) {
            x--;
            seg.update(x, seg.get(x) ^ y);
        } else {
            x--;
            cout << seg.query(x,y) << endl;
        }
    }
    return 0;
}


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

// Sieve of Eratosthenes
// https://youtu.be/UTVg7wzMWQc?t=2774
struct Sieve {
    int n;
    vector<int> f, primes;
    Sieve(int n = 1) : n(n), f(n + 1) {
        f[0] = f[1] = -1;
        for (ll i = 2; i <= n; ++i) {
            if (f[i]) continue;
            primes.push_back(i);
            f[i] = i;
            for (ll j = i * i; j <= n; j += i) {
                if (!f[j]) f[j] = i;
            }
        }
    }
    bool isPrime(int x) { return f[x] == x; }
    vector<int> factorList(int x) {
        vector<int> res;
        while (x != 1) {
            res.push_back(f[x]);
            x /= f[x];
        }
        return res;
    }
    vector<P> factor(int x) {
        vector<int> fl = factorList(x);
        if (fl.size() == 0) return {};
        vector<P> res(1, P(fl[0], 0));
        for (int p : fl) {
            if (res.back().first == p) {
                res.back().second++;
            } else {
                res.emplace_back(p, 1);
            }
        }
        return res;
    }
    vector<pair<ll, int>> factor(ll x) {
        vector<pair<ll, int>> res;
        for (int p : primes) {
            int y = 0;
            while (x % p == 0) x /= p, ++y;
            if (y != 0) res.emplace_back(p, y);
        }
        if (x != 1) res.emplace_back(x, 1);
        return res;
    }
};

ll A, B;
unordered_map<ll, int> mp;
ll dp[1 << 20][74];

ll dfs(ll t, int mask) {
    if (dp[mask][t - A] != 0) {
        return dp[mask][t - A];
    }
    if (t > B) {
        return dp[mask][t - A] = 1;
    }

    int tmask = mp[t];
    ll retVal = 0;
    // cerr << "tmask =" << tmask << "bit wise=" << bitset<20>(mask & tmask) << endl;
    if ((mask & tmask) == 0) {
        retVal += dfs(t + 1, mask | tmask);
    }
    retVal += dfs(t + 1, mask);
    return dp[mask][t - A] = retVal;
}

void solve() {
    Sieve sieve(72);
    for (ll i = A; i <= B; i++) {
        int mask = 0;
        rep(j, sieve.primes.size()) {
            if (i % sieve.primes[j] == 0) {
                mask |= (1 << j);
            }
        }
        // cerr << "i=" << i << " mask=" << mask << endl;
        mp[i] = mask;
    }
    dfs(A, 0);
    cout << dp[0][0] << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main() {
    scanf("%lld", &A);

    scanf("%lld", &B);
    solve();
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll =unsigned long long;
typedef pair<ll,ll> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}
void mswap(ll &a, ll &b){ if(a >= b) swap(a,b); }
void rswap(ll &a, ll &b){ if(a <= b) swap(a,b); }

const int dy[] = {0,0,1,-1};
const int dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
const double PI=3.14159265358979323846;
const int inf = 1001001001;
const ll INF = 1e18;
//Write From this Line
int main()
{
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	rep(i,n) cin >> a[i] >> b[i];
	int ans = inf;
	rep(i,n){
		rep(j,n){
			if(i == j){
				chmin(ans, a[i] + b[j]);
			}else{
				chmin(ans, max(a[i],b[j]));
			}
		}
	}
	cout << ans << endl;
}