#include <cstdio>

const int md = 1000000007;

inline int add(int a, int b)
{
	a += b;
	if (a >= md) a -= md;
	return a;
}
inline int sub(int a, int b)
{
	a -= b;
	if (a < 0) a += md;
	return a;
}
inline int mul(int a, int b)
{
	return (long long)a * b % md;
}
inline int po(int a, int b)
{
	int r = 1;
	while (b) {
		if (b & 1) r = mul(r, a);
		a = mul(a, a);
		b >>= 1;
	}
	return r;
}
inline int inv(int a)
{
	return po(a, md - 2);
}

const int N = 1000000;
int fact[2 * N + 1];

inline int nCr(int n, int r)
{
	if (r < 0) return 0;
	return mul(fact[n], inv(mul(fact[r], fact[n - r])));
}

int n, m, k;

int main()
{
	fact[0] = 1;
	for (int i = 1; i <= 2 * N; ++i) fact[i] = mul(fact[i - 1], i);
	scanf("%d%d%d", &n, &m, &k);
	if (n > m + k) printf("0\n");
	else printf("%d\n", sub(nCr(n + m, n), nCr(n + m, n - k - 1)));
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
/* * * *   * * *   	* * * */
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
/* * * *   * * *   	* * * */
/* *
 *
 * Too many mind, no mind.
 *
 * */
const int mod = 3;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a >= b ? a - b : a - b + mod; }
int mul(int a, int b) { return a * b % mod; }
int pasc[3][3];
int choose(int n, int k) {//Luca's Theorem
	if (k < 0 || k > n) return 0;
	int ans = 1;
	while (n) {
		int x = n % mod, y = k % mod;
		n /= mod, k /= mod;
		ans = mul(pasc[x][y], ans);
	}
	return ans;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	pasc[0][0] = 1;
	for (int i = 1; i < 3; ++i) {
		pasc[i][0] = pasc[i][i] = 1;
		for (int j = 1; j < 3; ++j) pasc[i][j] = add(pasc[i - 1][j], pasc[i - 1][j - 1]);
	}
	int n; cin >> n;
	string s; cin >> s;
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		int ai;
		if (s[i] == 'B') ai = 0;
		else if (s[i] == 'W') ai = 1;
		else ai = 2;
		ans = add(ans, mul(ai, choose(n - 1, i)));//Binomial coefficient
	}
	if (!(n & 1)) ans = (-ans + 6) % mod;
	/*
	for (int i = 0; i < n - 1; ++i) {
		if (ans & 1) ans += 3;
		ans /= 2;
	}
	*/
	if (ans == 0) cout << 'B';
	else if (ans == 1) cout << 'W';
	else cout << 'R';
	cout << '\n';
	return 0;
}


#include "bits/stdc++.h"
#define pb push_back

using namespace std;

using ll = long long;
const ll N = 2e3 + 5;

vector<ll>g[N];
bool vis[N];

void dfs(ll v){
    vis[v] = true;
    for(ll u : g[v]){
        if(!vis[u]){
            dfs(u);
        }
    }
}
signed main(){
    ll n,m;
    cin >> n >> m;

    for(ll i=0;i<m;i++){
        ll x,y;
        cin >> x >> y;

        g[x].pb(y);
    }

    ll ans = 0;

    for(ll i=1;i<=n;i++){
        memset(vis,false,sizeof(vis));
        dfs(i);

        ll cnt = 0;
        for(ll i=0;i<=n;i++){
            if(vis[i]){
                cnt++;
            }
        }

        ans = ans + cnt;
    }

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

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if (ch == '-') w = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int N(2e5 + 10), INF(1e9);
std::vector<int> G[N];
int n, K, f[N], g[N], R, cnt;

void dfs(int x, int fa)
{
	f[x] = 0, g[x] = INF;
	for (auto i : G[x]) if (i != fa)
		dfs(i, x), f[x] = std::max(f[x], f[i] + 1), g[x] = std::min(g[x], g[i] + 1);
	if (f[x] + g[x] <= R) f[x] = -INF;
	if (f[x] == R) f[x] = -INF, g[x] = 0, ++cnt;
	if (x == 1 && f[x] >= 0) ++cnt;
}

int main()
{
#ifndef ONLINE_JUDGE
	file(cpp);
#endif
	n = read(), K = read();
	for (int i = 1, a, b; i < n; i++)
		a = read(), b = read(), G[a].push_back(b), G[b].push_back(a);
	int l = 1, r = n, ans = n;
	while (l <= r)
	{
		R = (l + r) >> 1, cnt = 0, dfs(1, 0);
		if (cnt <= K) ans = R, r = R - 1;
		else l = R + 1;
	}
	printf("%d\n", ans);
	return 0;
}


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

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <vector>
#include <limits>
#include <cmath>
#include <queue>
#include <unordered_map>

typedef long long ll;
const auto llMax = std::numeric_limits<ll>::max();

typedef struct {
    ll a; // from
    ll b; // to
    ll c; // time
} Route;

ll search(const int n, const std::vector<Route> r, int start, ll minTime)
{
    std::vector<ll> t(n, llMax);

    // make index
    std::vector<ll> index(n + 1, llMax);
    index[0] = 0;
    index[n] = r.size();
    for (ll i = 0; i < r.size(); i++) {
        ll a = r[i].a;
        index[a] = std::min(index[a], i);
    }
    for (ll i = n; i >= 0; i--) {
        if (index[i] == llMax) {
            index[i] = index[i + 1];
        }
    }

    t[start] = 0;
    std::queue<ll> pos;
    pos.push(start);
    while(!pos.empty()) {
        ll now = pos.front();
        pos.pop();

        for (ll i = index[now]; i < index[now + 1]; i++) {
            Route nowRoute = r[i];
            const ll time = t[now] + nowRoute.c;
            if (time > minTime) {continue;}
            if (nowRoute.b == start) {
                minTime = time;
                continue;
            }
            if (time < t[nowRoute.b]) {
                t[nowRoute.b] = time;
                pos.push(nowRoute.b);
            }
        }
    }

    return minTime;
}

int main(int argc, char *argv[])
{
    ll n, m;
    std::vector<Route> r;

    std::cin >> n >> m;
    r.reserve(m);

    std::vector<ll> loopTime(n, llMax);

    for (int i = 0; i < m; i++) {
        ll a, b, c;
        std::cin >> a >> b >> c;
        a--; b--;

        if (a == b) {
            loopTime[a] = std::min(loopTime[a], c);
        } else {
            r.push_back({a, b, c});
        }
    }

    std::sort(r.begin(), r.end(), [](auto const& lhs, auto const& rhs){
        return lhs.a < rhs.a;
    });

    for (int i = 0; i < n; i++) {
        ll time = search(n, r, i, loopTime[i]);
        ll ans = (time != llMax) ? time : -1;
        std::cout << ans << std::endl;
    }

    return 0;
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

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

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<s64, s64>;

void dijkstra(const vector< vector< P > >& g, vs64& d, int s)
{
    static const s64 INF = 1e18;
    rep (i, d.size()) d[i] = INF;
    d[s] = 0;

    priority_queue< P, vector<P>, greater<P> > pq;
    pq.push(P{d[s], s});
    while (!pq.empty())
    {
        auto p = pq.top(); pq.pop();
        int from = p.second;
        s64 d1 = p.first;
        if (d[from] < d1) continue;
        for (auto e : g[from])
        {
            int to = e.first;
            s64 d2 = e.second;
            if (d[to] > d1 + d2)
            {
                d[to] = d1 + d2;
                pq.push(P{d[to], to});
            }
        }
    }
}

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

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

    vector< vector< P > > g1(n);
    vector< vector< P > > g2(n);
    vs64 ans(n, (s64)1e18);
    rep (i, m)
    {
        int u, v;
        s64 c;
        cin >> u >> v >> c;
        --u, --v;
        g1[u].push_back(P{v, c});
        g2[v].push_back(P{u, c}); // reverse
        if (u == v) ans[u] = min(ans[u], c);
    }

    rep (i, n)
    {
        vs64 d1(n), d2(n);
        dijkstra(g1, d1, i);
        dijkstra(g2, d2, i);
        rep (j, n)
        {
            if (i == j) continue;
            ans[i] = min(ans[i], d1[j] + d2[j]);
        }
        if (ans[i] == (s64)1e18) ans[i] = -1;
        cout << ans[i] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i=0; i<(n); ++i)
#define RREP(i, n) for(int i=(n);i>=0;--i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, b) for(int i=(a);i>=(b);--i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if (i) os << " ";
    os << v[i];
  }
  return os;
}

template <class T>
void debug(const vector<T> &v) {
  cout << "[";
  REP(i, SZ(v)) {
    if(i) cout << ", ";
    cout << v[i];
  }
  cout << "]" << endl;
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << " " << p.second;
}

template <class T, class U>
void debug(const pair<T, U> &p) {
  cout << "(" << p.first << " " << p.second << ")" << endl;
}

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const ll MOD998 = 998244353;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int inf = INT_MIN;
const ll linf = LLONG_MIN;
const ld eps = 1e-9;

template<int m>
struct mint {
  int x;

  mint(ll x = 0) : x(((x % m) + m) % m) {}

  mint operator-() const { return x ? m - x : 0; }

  mint &operator+=(mint r) {
    if ((x += r.x) >= m) x -= m;
    return *this;
  }

  mint &operator-=(mint r) {
    if ((x -= r.x) < 0) x += m;
    return *this;
  }

  mint &operator*=(mint r) {
    x = ((ll) x * r.x) % m;
    return *this;
  }

  mint inv() const { return pow(m - 2); }

  mint &operator/=(mint r) { return *this *= r.inv(); }

  friend mint operator+(mint l, mint r) { return l += r; }

  friend mint operator-(mint l, mint r) { return l -= r; }

  friend mint operator*(mint l, mint r) { return l *= r; }

  friend mint operator/(mint l, mint r) { return l /= r; }

  mint pow(ll n) const {
    mint ret = 1, tmp = *this;
    while (n) {
      if (n & 1) ret *= tmp;
      tmp *= tmp, n >>= 1;
    }
    return ret;
  }

  friend bool operator==(mint l, mint r) { return l.x == r.x; }

  friend bool operator!=(mint l, mint r) { return l.x != r.x; }

  friend ostream &operator<<(ostream &os, mint a) {
    return os << a.x;
  }

  friend istream &operator>>(istream &is, mint &a) {
    ll x;
    is >> x;
    a = x;
    return is;
  }
};

using Int = mint<MOD>;

template<typename T>
struct BIT {
  vector<T> bit;
  int sz;

  BIT(int n) : sz(n + 1), bit(n + 1) {}

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

  T sum(int i) {
    i += 1;
    T s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
};

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

  int h, w, m; cin >> h >> w >> m;
  vi x(m), y(m);

  vi mir(h, w), mic(w, h);
  REP(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    chmin(mir[x[i]], y[i]);
    chmin(mic[y[i]], x[i]);
  }

  int mar = mic[0], mac = mir[0];
  ll ans = 0;
  REP(i, mar) {
    ans += mir[i];
  }
  REP(i, mac) {
    ans += mic[i];
  }

  vector<P> v;
  REP(i, mar) {
    v.emplace_back(mir[i], i);
  }
  sort(ALL(v));

  BIT<int> bit(h+1);
  REP(i, mar) {
    bit.add(i, 1);
  }

  int pos = 0;
  REP(i, mac) {
    while(pos < mar && v[pos].first == i) {
      bit.add(v[pos].second, -1);
      pos++;
    }

    if(mic[i] != 0) ans -= bit.sum(mic[i]-1);
  }
  cout << ans << endl;

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define MOD 1000000007
#define MAXN 200000

int ft[MAXN + 3];

void update(int idx, int val) {
    while (idx <= MAXN) {
        ft[idx] += val;
        idx += (idx & -idx);
    }
}

int query(int idx) {
    int result = 0;
    while (idx > 0) {
        result += ft[idx];
        idx -= (idx & -idx);
    }

    return result;
}

int H, W, N, firstBlockRight, firstBlockBelow;
vector<int> blockAtColumn[MAXN + 3];
bool seen[MAXN + 3];
LL answer;

int main() {
    scanf("%d%d%d", &H, &W, &N);

    firstBlockRight = W + 1;
    firstBlockBelow = H + 1;
    for (int i = 1; i <= N; i++) {
        int row, col;
        scanf("%d%d", &row, &col);

        if (row == 1) {
            firstBlockRight = min(firstBlockRight, col);
        }

        if (col == 1) {
            firstBlockBelow = min(firstBlockBelow, row);
        }

        blockAtColumn[col].push_back(row);
    }

    memset(ft, 0, sizeof(ft));
    memset(seen, false, sizeof(seen));

    answer = 0;
    for (int col = 1; col <= W; col++) {
        sort(blockAtColumn[col].begin(), blockAtColumn[col].end());

        for (auto row: blockAtColumn[col]) {
            if (!seen[row]) {
                seen[row] = true;
                update(row, 1);
            }
        }

        if (col < firstBlockRight) {
            if (blockAtColumn[col].empty()) {
                answer += H;
            } else {
                int r = blockAtColumn[col][0];
                if (r < firstBlockBelow) {
                    int cntBlocked = query(firstBlockBelow - 1) - query(r - 1);
                    answer += (firstBlockBelow - 1 - cntBlocked);
                } else {
                    answer += (r - 1);
                }
            }
        } else {
            int cntBlocked = query(firstBlockBelow - 1);
            answer += (firstBlockBelow - 1 - cntBlocked);
        }
    }

    printf("%lld\n", answer);
}


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

#define pb push_back
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define fr first
#define sc second
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for(int i = a; i < b; i++)
#define int long long

const int N = 1e5 + 20;
const int K = 18;
const int INF = 1e9;
const int MOD = 1e9 + 7;

vector <int> adj[N];
int dist[N][K], n, k, dp[1 << K][K], m, arr[K];

void bfs(int idx) {
	rep(node, 1, n + 1) dist[node][idx] = INF;
	queue <int> q;
	q.push(arr[idx]);
	dist[arr[idx]][idx] = 0;
	while(!q.empty()) {
		int node = q.front();
		q.pop();
		for(auto &child: adj[node]) {
			if(dist[child][idx] == INF) {
				dist[child][idx] = dist[node][idx] + 1;
				q.push(child);
			}
		}
	}
}

int calc(int last, int mask) {
	if(mask == ((1 << k) - 1))
		return 0;
	if(dp[mask][last] != -1)
		return dp[mask][last];
	int ans = INF;
	for(int i = 0; i < k; i++) {
		if(mask & (1 << i)) continue;
		ans = min(ans, calc(i, mask | (1 << i)) + dist[arr[last]][i]);
	}
	return dp[mask][last] = ans;
}

void solve() {
	cin >> n >> m;
	rep(i, 0, m) {
		int x, y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	cin >> k;
	rep(i, 0, k) cin >> arr[i];
	rep(i, 0, k) bfs(i);
	rep(i, 0, k) {
		if(dist[arr[0]][i] == INF) {
			cout << "-1\n";
			return;
		}
	}
	memset(dp, -1, sizeof dp);
	int ans = INF;
	rep(i, 0, k) ans = min(ans, 1 + calc(i, (1 << i)));
	cout << ans << "\n";
}

signed main() {
  	ios_base::sync_with_stdio(false);
  	cin.tie(NULL); cout.tie(NULL);
  	int t = 1;
  	// cin >> t;
  	while(t--)
    	solve();
  	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }

int main() {
    init();

    int n, m; cin >> n >> m;
    vector<vector<pair<int, int>>> g(n);
    VI c(m);
    FOR(i, 0, m) {
        int a, b; cin >> a >> b >> c[i];
        --a, --b, --c[i];
        g[a].emplace_back(b, i);
        g[b].emplace_back(a, i);
    }

    VI ans(n, -1);
    ans[0] = 0;
    vector<bool> dp(n);
    dp[0] = true;
    queue<int> bfs;
    bfs.push(0);
    while (!bfs.empty()) {
        int cur = bfs.front();
        bfs.pop();
        for (auto [to, i] : g[cur]) {
            if (dp[to])continue;
            dp[to] = true;
            if (ans[cur] == c[i]) {
                if (c[i] == 0)ans[to] = 1;
                else ans[to] = 0;
            }
            else ans[to] = c[i];
            bfs.push(to);
        }
    }

    FOR(i, 0, n)print(++ans[i]);

    return 0;
}


#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int n;
double f[maxn];
signed main()
{
	n=read();
	for(int i=n-1;i;i--) f[i]=f[i+1]+1.*n/(n-i);
	cout<<fixed<<setprecision(10)<<f[1];
	return 0;
}

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

int main(){
    int n;cin>>n;
    double res = 0;

    for(int i=1;i<n;++i){
        double tmp = 1.0 * n / i;
        res += tmp;
    }

    cout<<setprecision(10)<<res<<endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 4 * 1e5;

// union-find
struct union_find {
    vector<int> dat;

    union_find(int size) {dat.resize(size, -1);}

    int find(int x) {
        if (dat[x] < 0) {return x;}
        return dat[x] = find(dat[x]);
    }

    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) {return false;}
        if (dat[x] > dat[y]) swap(x, y);
        dat[x] += dat[y];
        dat[y] = x;
        return true;
    }

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

    vvint groups() {
        int n = dat.size();
        vector<vector<int>> res(n);
        for (int i = 0; i < n; i++) {
            res[find(i)].push_back(i);
        }
        res.erase(
            remove_if(res.begin(), res.end(),
                [](const vint &v) {return v.empty();}),
            res.end());
        return res;
    }
};

int main() {
    int n; cin >> n;
    union_find uf(MAX);
    vector<bool> B(MAX, false);
    rep(i,n) {
        int a, b; cin >> a >> b;
        a--; b--;
        if (a != b) {
            if (!uf.unite(a, b)) {
                B[a] = true; B[b] = true;
            }
        } else B[a] = true;
    }
    rep(i,MAX) {
        if (B[i]) B[uf.find(i)] = true;
    }
    vvint group = uf.groups();
    ll ans = 0;
    rep(i,group.size()) {
        if (B[uf.find(group[i][0])]) ans += group[i].size();
        else ans += group[i].size() - 1;
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
vector<int> c[100005];
int n,p,s[100005];
bool cmp(pair<int,int> a,pair<int,int> b)
{
	if(a.first!=b.first) return a.first>b.first;
	return a.second>b.second;
}
void dfs1(int x)
{
	s[x]=1;
	for(int i=0;i<c[x].size();++i)
	{
		int y=c[x][i];
		dfs1(y);
		s[x]+=s[y];
	}
}
int dfs(int x)
{
	int nf=0;bool f=0;
	vector<pair<int,int> > k,k1;
	for(int i=0;i<c[x].size();++i)
	{
		int y=c[x][i],u=dfs(y);
		if(s[y]%2) k.push_back(make_pair(u*2-s[y],u));
		else k1.push_back(make_pair(u*2-s[y],u));
	}
	sort(k.begin(),k.end(),cmp);
	sort(k1.begin(),k1.end(),cmp);
	if(k.size()%2) f=1;
	for(int i=0;i<k1.size();++i)
	 if(k1[i].first>=0||!f) nf+=k1[i].second;
	 else nf+=k1[i].second-k1[i].first;
	for(int i=0;i<k.size();++i)
	 if(i%2==0) nf+=k[i].second;
	 else nf+=k[i].second-k[i].first;
	return nf;
}
int main()
{
	cin>>n;
	for(int i=1;i<n;++i)
	{
		cin>>p;
		c[p].push_back(i+1);
	}
	dfs1(1);
	cout<<n-dfs(1);
	return 0;
}


#define TEMPLATE_USED
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pl;
typedef vector<pl> vp;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
const ll D4[] = {0, 1, 0, -1, 0};
const ll D8[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
#define _overload3(_1, _2, _3, name, ...) name
#define _rep2(i, n) for (ll i = 0; i < (n); ++i)
#define _rep3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2)(__VA_ARGS__)
#define _repe2(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define _repe3(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define repe(...) _overload3(__VA_ARGS__, _repe3, _repe2)(__VA_ARGS__)
#define _rrep2(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define _rrep3(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(...) _overload3(__VA_ARGS__, _rrep3, _rrep2)(__VA_ARGS__)
#define _rrepe2(i, n) for (ll i = (ll)(n); i >= 0; i--)
#define _rrepe3(i, a, b) for (ll i = (ll)(b); i >= (ll)(a); i--)
#define rrepe(...) _overload3(__VA_ARGS__, _rrepe3, _rrepe2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
const string YES = "Yes";
const string NO = "No";

vl A;

void solve();

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    A = vl(3);
    for (int i = 0; i < 3; ++i)
    {
        cin >> A[i];
    }
    solve();
}

// 1 <= A_i <= 100
// 入力は全て整数

// vl A
// A を並び替えて等差数列にできるなら Yes、できないなら No と出力せよ。
void solve()
{
    sort(all(A));
    cout << (A[2] - A[1] == A[1] - A[0] ? "Yes" : "No") << endl;
}

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

using namespace std;
typedef long long ll;
typedef vector<int> vi;

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

	vi A(3);
	for(int i = 0; i < 3; ++i) cin >> A[i];
	sort(A.begin(), A.end());
	if(A[1]-A[0] == A[2]-A[1]) cout << "Yes\n";
	else cout << "No\n";

	return 0;
}


#include <iostream>
using namespace std;
#define flash ios::sync_with_stdio(0);cin.tie(0);
int main(){
  flash;
int x,u;
cin >> x >> u;
if(u % x == 0){
cout << "Yes";
}
else{
cout << "No";
}
}

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define mem(a,x) memset(a,x,sizeof(a))
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
using namespace std;
const double eps=1e-8;
typedef long long ll;
const int MAXN=100005;
const ll mod=1e7+9;
const int inf=0x3f3f3f3f;

int main(){

	int x;
	cin>>x;
	cout<<100-(x%100)<<endl;

	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
const double PI = acos(-1.0);
const long mod = 1000000007;
const long INF = 1e9 + 1;

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

  for(int i = 0; i < t; i++) {
    int n,a,b; cin >> n >> a >> b;
    long ans,x1,x2,x3,x4;
    if(n-a-b < 0) x4 = 0;
    else x4 = (n-a-b+2) % mod * ((n-a-b+1) % mod) / 2 % mod;
    x3 = 2 * x4 % mod;
    x2 = ((n-a+1)%mod * ((n-b+1)%mod) % mod - x3 ) % mod;
    x1 = (x2 % mod) * (x2 % mod) % mod;
    long aa = (n-a+1) % mod * ((n-a+1) % mod) % mod;
    long bb = (n-b+1) % mod * ((n-b+1) % mod) % mod;
    ans = (aa % mod * (bb % mod) - x1) % mod;
    cout << ans % mod << endl;
  }
}

// ######      ###      #######    #######    ##     #     #####        ###     ##### //
// #     #    #   #          #        #       # #    #    #     #      #   #     ###  //
// #     #   #     #        #         #       #  #   #   #       #    #     #    ###  //
// ######   #########      #          #       #   #  #   #           #########    #   //
// #     #  #       #     #           #       #    # #   #    ####   #       #    #   //
// #     #  #       #    #            #       #     ##   #    #  #   #       #        //
// ######   #       #   #######    #######    #      #    #####  #   #       #    #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i)  cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 1000005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1000005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll  b) {
    return (ceil(1.0 * a / b * 1.0));
}
ll binexp(ll a, ll b, ll m) {
    a %= m;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
ll modinvfermat(ll a, ll m)
{
    return binexp(a, m - 2, m);
}
void task(bool flag)
{
    if (flag)
        cout << "YES\n";
    else
        cout << "NO\n";
}
ll lcm(ll a, ll b)
{
    return ((1LL * a * b) / (__gcd(a, b)));
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    fast
    cases
    {
        ll n, a, b;
        cin >> n >> a >> b;
        ll ans = 0;

        if (a > b)
            swap(a, b);
        ll sum = 0;
        if (n - b >= a)
        {
            ll x = n - b - a + 1;
            sum = (x * (x + 1) / 2) % MOD;
        }
        ans = (4 * (n - b + 1) % MOD * ((n - a + 1) % MOD * sum % MOD) % MOD) % MOD;
        // cout << ans << END;
        ll val = (sum % MOD * sum % MOD) % MOD;
        (ans -= 4 * val) %= MOD;
        (ans += MOD) %= MOD;
        cout << ans << END;
    }

#ifndef ONLINE_JUDGE
    cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define reps(i, c, n) for(int i = c; i < n; i++)
#define inv(n, a) for(int i = 0; i < n; i++) cin >> a[i]
#define MOD 1000000007
#define INF 1e7
#define LINF 1e15
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
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 print(){ cout << endl; }
template <class H, class... T> void print(H&& h, T&&... t){ cout << h << ", "; print(forward<T>(t)...); }
template <class C> void dump(C o){ rep(i, o.size()-1) cout << o[i] << ", "; cout << o.back() << endl; }

int main() {
	int n, x; cin >> n >> x;
	bool f = false;
	rep(i, n){
		int a; cin >> a;
		if (a != x){
			if (f)
				cout << " ";
			cout << a;
			f = true;
		}
	}
	cout << endl;
}

#include<bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<=b;++i)
#define ff(i,a,b) for( int i=a;i>=b;--i)
#define debug(x) cerr << #x << " : " << x << " " << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 2e18;
const double tiaohe = 0.57721566490153286060651209;
inline ll in() { char ch = getchar();ll x = 0, f = 1;while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }return x * f; }
const int N = 2e5 + 10;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	int n, x;cin >> n >> x;
	f(i, 1, n) {
		int now = in();
		if (now == x)continue;
		cout << now << " ";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
const int mxn = 4e5+10;
const int mod = 3;

int powmod(int a, int b) {
	int ret = 1;
	while(b) {
		if(b&1) ret*=a, ret%=mod;
		a*=a; a%=mod;
		b>>=1;
	}
	return ret;
}

int c[3][3];

void pre() {
	for(int i=0; i<3; i++) for(int j=0;j<=i; j++) {
		if(j==0) c[i][j] = 1;
		else c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod;
	}
}

int binom(int n, int k) {
	if(k>n) return 0;
	if(!k) return 1;
	vector<int> bn, bk;
	while(n) {
		bn.push_back(n%3);
		n/=3;
	}
	while(k) {
		bk.push_back(k%3);
		k/=3;
	}
	bn.resize(max(bn.size(), bk.size()), 0);
	bk.resize(max(bn.size(), bk.size()), 0);

	int ret = 1;
	for(int i=0; i<(int)bn.size(); i++) {
		ret*=c[bn[i]][bk[i]];
		ret%=mod;
	}

	return ret;
}

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

	pre();

	int n; cin>>n;
	vector<int> color(n, 0);
	for(int i=0;i<n; i++) {
		char b; cin>>b;
		if(b=='B') color[i] = 0;
		else if(b=='W') color[i] = 1;
		else color[i] = 2;
	}

	int sum = 0;
	for(int i=0; i<n; i++) {
		//cout << "c: " << C(n-1, i) << '\n';
		sum+=binom(n-1, i)*color[i];
		sum%=mod;
	}

	sum*=powmod(2, n-1);
	sum%=mod;

	if(sum==0) {
		cout << "B" << '\n';
	}
	else if(sum==1) {
		cout << "W" << '\n';
	}
	else cout << "R" << '\n';

	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;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;

#define int ll
#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d",&x)
#define gll2(x,y) scanf("%d%d",&x,&y)
#define gll3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)

const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
inline ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
inline ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}

void solve() {
	int n;
	cin>>n;
	int a[n];
	f(i, n) cin>>a[i];
	int ct = 0, ans = -1;
	for(int i = 2; i <= 1001; i++) {
		int cct = 0;
		f(j, n) {
			if(a[j] % i) continue;
			cct++;
		}
		if (cct > ct) {
			ct = cct;
			ans = i;
		}
	}
	cout<<ans;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (LOCAL) {
        freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r", stdin);
        freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w", stdout);
    }
    int t = 1;
    //cin>>t;
    for(int test = 1; test <= t; test++) {
        //cout<<"Case #"<<test<<": ";
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n);i++)
using ll = long long;
const ll INF = 9e18;
const ll mod=1000000007;

int main(){
    int n, m, l, x;
    cin >> n;
    vector<int> a(n);
    int amax = 0;
    rep(i,n){
        cin >> a[i];
        amax = max(a[i], amax);
    }
    int ans = 0;
    int res = 0;
    for(int i=2; i<=amax; i++){
        int cans = 0;
        rep(j,n){
            if(a[j]%i ==0){
                cans ++;
            }
        }
        if(cans > ans){
            ans = cans;
            res = i;
        }
    }

    cout << res << endl;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
ll A[101010],B[101010];
vector<ll> V[2];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N;
	FOR(i,N) cin>>A[i];
	FOR(i,N) cin>>B[i];

	ll tsum=0;
	for(i=0;i<N;i+=2) {
		ll a=max(A[i]+A[i+1],B[i]+B[i+1]);
		tsum+=a;
		V[0].push_back(A[i]+B[i+1]-a);
		V[1].push_back(B[i]+A[i+1]-a);
	}
	ll ret=tsum;

	sort(ALL(V[0]));
	sort(ALL(V[1]));
	reverse(ALL(V[0]));
	reverse(ALL(V[1]));

	FOR(i,N/4) {
		tsum+=V[0][i]+V[1][i];
		ret=max(ret,tsum);

	}

	cout<<ret<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}

#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(),v.end()
#pragma gcc optimize("O3")
#pragma gcc optimize("Ofast")
#pragma gcc optimize("unroll-loops")
using namespace std;
const int INF = 1e9;
const long long llINF = 5e18;
long long mod;
typedef long long ll;
typedef long double ld;
typedef pair <int,int> pi;
typedef pair <ll,ll> pl;
typedef vector <int> vec;
typedef vector <pi> vecpi;
typedef long long ll;
int n;
int a[100005],b[100005];

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++) cin >> a[i];
	for(int i = 1;i <= n;i++) cin >> b[i];
	ll ans = 0;
	for(int i = 1;i <= n;i++) {
		if((i & 1) == 0) swap(a[i],b[i]);
		ans += a[i];
		b[i] -= a[i];
	}
	sort(b+1,b+n+1);
	reverse(b+1,b+n+1);
	for(int i = 1;i <= n/2;i++) ans += b[i];
	cout << ans;
}


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

template <class E> struct FenwickTree {
    int _n;
    std::vector<E> data;

    FenwickTree(int n) : _n(n), data(n) { }

    void add(int p, E x) {
        ++p;
        while (p <= _n) {
            data[p - 1] += x;
            p += p & -p;
        }
    }
    E sum(int r) const {
        E s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
    E sum(int l, int r) const {
        return sum(r) - sum(l);
    }
};

long long proc(
    std::vector<int>             &cur,
    int                          x,
    int                          y,
    FenwickTree<int>             &ftcnt0,
    FenwickTree<long long>       &ftsum0,
    const FenwickTree<int>       &ftcnt1,
    const FenwickTree<long long> &ftsum1,
    const std::vector<int>       &arr)
{
    int v = cur[x];
    long long dem = (long long)ftcnt1.sum(0, v) * arr[v];
    long long cre = (long long)ftcnt1.sum(0, y) * arr[y];
    long long adj = (long long)ftsum1.sum(y, v);

    ftsum0.add(v, -arr[v]);
    ftcnt0.add(v, -1);
    cur[x] = y;
    ftsum0.add(y, arr[y]);
    ftcnt0.add(y, 1);

    return cre + adj - dem;
}

int main(int argc, char **argv)
{
    int n, m, q;
    std::cin >> n >> m >> q;
    std::set<int> s = { 0 };
    std::vector<int> ta(q), xa(q), ya(q);

    for (int i = 0; i < q; i++) {
        std::cin >> ta[i] >> xa[i] >> ya[i];
        xa[i]--;
        s.insert(ya[i]);
    }
//    std::vector<int> arr(s.begin(), s.end());
    int ll = s.size();
    std::vector<int> arr(ll);
    std::map<int, int> map;
    int ii = 0;
    for (auto &k : s) {
        arr[ii] = k;
        map.emplace(k, ii);
        ++ii;
    }

    FenwickTree<long long> ftsuma(ll);
    FenwickTree<long long> ftsumb(ll);
    FenwickTree<int>       ftcnta(ll);
    FenwickTree<int>       ftcntb(ll);
    std::vector<int> cura(n);
    std::vector<int> curb(m);
    ftcnta.add(0, n);
    ftcntb.add(0, m);
    long long sum = 0;

    for (int i = 0; i < q; i++) {
        int cy = map[ya[i]];
        sum += (ta[i] == 1) ? proc(cura, xa[i], cy, ftcnta, ftsuma, ftcntb, ftsumb, arr)
                            : proc(curb, xa[i], cy, ftcntb, ftsumb, ftcnta, ftsuma, arr);
        std::cout << sum << std::endl;
    }
    return 0;
}

#include<cstdio>
#include<iostream>
using namespace std;
int n,k;
long long MO,dp[110][180000];
int main()
{
	scanf("%d%d%lld",&n,&k,&MO);
	long long sum;
	dp[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		sum=((1+min(i,n-i-1))*min(i,n-i-1)/2)*k;
		for(int kk=0;kk<=k;kk++)
			for(int j=kk*i;j<=sum;j++)
				(dp[i][j]+=dp[i-1][j-kk*i])%=MO;
	}
	for(int i=1;i<=n;i++)
	{
		long long ans=0;
		sum=((1+min(i-1,n-i))*min(i-1,n-i)/2)*k;
		for(int j=1;j<=sum;j++)
			(ans+=(dp[i-1][j]*dp[n-i][j]%MO)*(k+1))%=MO;
		printf("%lld\n",(ans+k)%MO);
	}
	return 0;
}


#include <bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define  rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define ll  long long
#define endl '\n'
string tostr(ll n)
{ stringstream rr;rr<<n;return rr. str();}
ll toint(string s)
{stringstream ss(s);ll x;ss>>x;return x;}
void solve()
{
  ll n, a,m=0,b=0, c=0,k=0, i, j,l=998244353;
  char s, r, y;

  ll hit=0, yog;
  cin>>s>>r>>y;
  if(s==r && s==y) cout<<"Won";
  else cout<<"Lost";
}
int main()
{

 ios_base::sync_with_stdio(false);
  cin. tie(0);cout. tie(0);
    ll t=1;
   //cin>>t;
    while(t--)
    {
      solve();
    }
    return 0;
}

#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'

// if two adjacent nodes have same reachability, then they are in SCC
// now we have a DAG to solve with
// if two adjacent nodes have different reachability, then their direction is easily found
// reduced to making graph SCC by directing edges
// read CF blog post for details on how to do that (is-this-fft)

int n, m;
vector<vector<ii>> adj;
vector<ii> ans;
vi vis;

int dfs(int p, int parent) {
	if (vis[p]) return 1;
	vis[p] = 1;

	for (auto i: adj[p]) if (i.first != parent and ans[i.second].first == -1) {
		int v = dfs(i.first, p);
		// cout << "going edge " << p << " - " << i.first << " ... " << v << endl;
		ans[i.second] = {i.first, p};
		// if (v) ans[i.second] = {p, i.first};
	}

	vis[p] = 2;
	return 0;
}

int main() {
	#ifdef mlocal
	freopen("test.in", "r", stdin);
	#endif

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

	cin >> n >> m;
	vector<ii> edges(m);
	adj.resize(n); ans.resize(m, {-1, -1}); vis.resize(n);

	for_(i, 0, m) {
		int a, b; cin >> a >> b;
		a -= 1; b -= 1;
		adj[a].push_back({b, i}); adj[b].push_back({a, i});
		edges[i] = {a, b};
	}

	vi reach(n);
	for_(i, 0, n) cin >> reach[i];
	for_(i, 0, m) if (reach[edges[i].first] != reach[edges[i].second]) {
		ans[i] = {edges[i].first, edges[i].second};
		if (reach[edges[i].first] < reach[edges[i].second]) ans[i] = {edges[i].second, edges[i].first};
	}

	for_(i, 0, n) if (!vis[i]) dfs(i, i);

	for_(i, 0, m) {
		if (edges[i] == ans[i]) cout << "->";
		else cout << "<-";
		cout << endl;
	}

	return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

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

    int T;
    cin >> T;
    int ans[100];
    REP(i, T) {
        string S;
        cin >> S;
        ans[i] = -1;
        if ("atcoder" < S)
            ans[i] = 0;
        else{
            REP(j, S.length())
            {
                if(S[j] > 'a') {
                    if(S[j] > 't')
                        ans[i] = j-1;
                    else
                        ans[i] = j;
                    break;
                }
            }
        }

    }
    REP(i,T)
    cout << ans[i] << "\n";
}

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
#define md 998244353
#define ll long long
int dp[maxn][30];
constexpr int top = 300000;
vector<int> fac(top + 1), ifac(top + 1);
inline int add(int a, int b) {
    a += b;
    if (a >= md) a -= md;
    return a;
}

inline int sub(int a, int b) {
    a -= b;
    if (a < 0) a += md;
    return a;
}

inline int mul(int a, int b) {
    return (int)((long long)a * b % md);
}

inline int powmod(int a, long long b) {
    int res = 1;
    while (b > 0) {
        if (b & 1) res = mul(res, a);
        a = mul(a, a);
        b >>= 1;
    }
    return res;
}

inline int inv(int a) {
    a %= md;
    if (a < 0) a += md;
    int b = md, u = 0, v = 1;
    while (a) {
        int t = b / a;
        b -= t * a; swap(a, b);
        u -= t * v; swap(u, v);
    }
    assert(b == 1);
    if (u < 0) u += md;
    return u;
}
int binom(int n, int m) {
    if (n < m || n < 0 || m < 0) return 0;
    return mul(fac[n], mul(ifac[m], ifac[n - m]));
}

void init() {
    fac[0] = 1;
    for (int i = 1; i <= top; i++) fac[i] = mul(fac[i - 1], i);
    ifac[top] = inv(fac[top]);
    for (int i = top; i; i--) ifac[i - 1] = mul(ifac[i], i);
}
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    init();
    dp[0][0] = 1;
    for (int i = 1; i < 20; i++) dp[0][i] = 1;
    for (int i = 1; i <= m; i++) {
        for (int j = 0; j < 20; j++) {
            for (int k = 0; k * (1 << j) <= i && k <= n; k += 2) {
                dp[i][j + 1] = add(dp[i][j + 1], mul(dp[i - k * (1 << j)][j], binom(n, k)));
            }
        }
    }
    int ans = 0;
    //for(int i=0;i<18;i++) ans=add(ans,dp[m][i]);
    printf("%d\n", dp[m][19]);
    return 0;
}


#include <bits/stdc++.h>
#define DEBUG if(0)
typedef long long ll;
using namespace std;

const int maxSz = 2e5, maxCnt = 16;
const ll mod = 1e9 + 7;
ll dp[maxSz][maxCnt + 1][2][2];

char s[maxSz + 1];
int a[maxSz];
int sz, k;

ll solve(int mask, int pos, bool lmt, bool started) {
    int cnt = __builtin_popcount(mask);
    if (cnt > k) return 0;
    if (pos == sz) return cnt == k && started;

    ll &ans = dp[pos][cnt][lmt][started];
    if (ans != -1) return ans;

    ans = 0;
    int llmt = (lmt ? a[pos] : 15);
    for (int i = 0; i <= llmt; i++)
        ans = (ans + solve((!i && !started) ? mask : mask | (1 << i), pos + 1, lmt & (i == llmt), started | (i > 0))) % mod;
    return ans;
}

int main() {
    cin >> s >> k;
    sz = strlen(s);
    for (int i = 0; i < sz; i++) {
        a[i] = s[i] - (isdigit(s[i]) ? '0' : 'A' - 10);
    }

    DEBUG
    {
        printf("%s\n", s);
        for (int i = 0; i < sz; i++)
        printf("%d ", a[i]);
        printf("\n");
    }
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0, 1, 0) << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define inf 2000000000
#define infLL 2000000000000000000
#define MAX5 100005
#define MAX6 1000006
#define MAX7 10000007
#define sf(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sfs(a) scanf("%s", a)
#define sline(a) scanf("%[^\n]%*c", a);
#define pf(a) printf("%d\n", a)
#define pfl(a) printf("%lld\n", a)
#define pfs(a) printf("%s\n", a)
#define Case(t) printf("Case %d: ", t)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1000000007
#define Mod 998244353
#define PI acos(-1.0)
#define eps 1e-9
#define mem(a, b) memset(a, b, sizeof(a))
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

const int N = MAX5;

int dx[] = {0, 0, 1, 1}; ///Up-down, Left-Right
int dy[] = {0, 1, 0, 1};

string s[15];

int check(int i, int j)
{
    int cnt = 0;
    for(int d = 0; d < 4; d++) {
        cnt += (s[i + dx[d]][j + dy[d]]=='#');
    }
    return (cnt & 1);
}

int main()
{
    //#ifndef ONLINE_JUDGE
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
    //#endif

    FASTIO;
    int n, m;
    cin>>n>>m;
    for(int i = 0; i < n; i++) cin>>s[i];
    int res = 0;
    for(int i = 0; i < n - 1; i++) {
        for(int j = 0; j < m - 1; j++) {
            res += check(i, j);
        }
    }
    cout<<res<<endl;

    return 0;
}

//https://atcoder.jp/contests/abc191/tasks/abc191_c
#include<iostream>
#include<iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
#define ll long long int
#define vl vector<ll>
#define vvll vector<vl>
#define vpll vector<pair<ll, ll>>
#define sz(a) (ll)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb(a) push_back(a)
#define MAX 100000009

#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (ll i = (a); i >= (b); --i)

#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
   return a.second > b.second;
}

ll _ceil(ll a, ll b) { return (a + b - 1) / b; }

int main()
{
   ll t=1;
   //cin>>t;
   while(t--)
   {
      ll h,w;
      cin>>h>>w;
      string line;
      vector<vector<bool>>grid(h,vector<bool>(w));
      FOR(i,0,h-1)
      {
          cin>>line;
          FOR(j,0,w-1)
          {
            grid[i][j] = line[j] == '#';
          }
      }
      ll ans = 0,sum = 0;
      FOR(i,0,h-2)
      {
          FOR(j,0,w-2)
          {
              sum = 0;
              sum^=grid[i][j];
              sum^=grid[i+1][j];
              sum^=grid[i][j+1];
              sum^=grid[i+1][j+1];
              ans+=sum;
          }
      }
      cout<<ans<<"\n";
   }
}


#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<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define N 100012
#define ll long long
#define int ll
#define pi pair<int , int>
#define pip pair<pair ,int>
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
ll Mod(ll x, ll y, int p)
{	ll res = 1; x = x % p;
	while (y > 0)
	{	if (y & 1) res = (res * x) % p;
		y = y >> 1; x = (x * x) % p;
	} return res;
}
int* getlps(string pattern) {
	int len = pattern.length();
	int * lps = new int[len];
	lps[0] = 0;
	int i = 1 , j = 0;
	while (i < len) {
		if (pattern[i] == pattern[j]) {
			lps[i] = j + 1;
			i++;
			j++;
		}
		else {
			if (j != 0) {
				j = lps[j - 1];
			}
			else {
				lps[i] = 0;
				i++;
			}
		}
	}
	return lps;
}
class Triplet {
public:
	int x ;
	int y ;
	int gcd;
};
Triplet extendedEuclid(int a , int b) {
	if (b == 0) {Triplet ans; ans.gcd = a; ans.x = 1; ans.y = 0; return ans;}
	Triplet smallAns = extendedEuclid(b , a % b);
	Triplet ans;
	ans.gcd = smallAns.gcd;
	ans.x = smallAns.y;
	ans.y = smallAns.x - (a / b) * smallAns.y;
	return ans;
}
int mmInverse(int a , int m) {
	Triplet ans = extendedEuclid(a , m);
	return (ans.x + m) % m;
}
int fact[N];
void calfac(int n ) {
	fact[0] = 1;
	for (int i = 1 ; i <= n + 2; i++) {
		fact[i] = (((fact[i - 1] % mod) * (i % mod)) % mod + mod) % mod;
	}
}
int calc(int n , int r) {
	if (r > n)return 0;
	if (r == n)return 1;
	int ans = 1;
	ans = ((ans % mod) * (fact[n]) % mod + mod) % mod;
	ans = ((ans % mod) * (mmInverse(fact[n - r] , mod) % mod) + mod) % mod;
	ans = ((ans % mod) * (mmInverse(fact[r] , mod) % mod) + mod) % mod;
	return (ans + mod) % mod;
}

void solve(int cntt) {
	int n;
	cin >> n;
	int x = 1;
	int cnt = 1;
	cout << (n + 99) / 100 << endl;

}

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

	// preprocess();
	int t;
	t = 1;

	int l = 0;
	while (t--) {
		l++;
		solve(l);
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f;
const double pi=3.1415926535897932384626;
inline ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
ll a,b,ans;
struct node{
	ll x,y;
};
set<ll> s;
queue<node> q;
void calc(ll x,ll y){
	if(s.find(x)!=s.end()) return ;
	s.insert(x);
	q.push({x,y});
}
void bfs(){
	if(a>=b){
		ans=a-b;
		return ;
	}
	ans=b-a;
	q.push({b,0ll});
	s.insert(b);
	while(!q.empty()){
		node now=q.front();
		q.pop();
		ans=min(ans,now.y+abs(now.x-a));
		if(now.x<=a) break;
		if(now.x&1){
			calc(now.x+1,now.y+1);
			calc(now.x-1,now.y+1);
		}
		else calc(now.x/2,now.y+1);
	}
}
int main(){
	a=read(),b=read();
	bfs();
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=998244353;
const ll mod=MOD;
const int MAX=200010;

//Union-Find木

struct UnionFind {
    vector< int > data;

    UnionFind() = default;

    explicit UnionFind(size_t sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }

    int find(int k) {
        if(data[k] < 0) return (k);
        return data[k] = find(data[k]);
    }

    int size(int k) {
        return -data[find(k)];
    }

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

signed main(){
    ll n,k;cin>>n>>k;
    vector<ll>fac(n*n+1);
    fac[0]=1;
    REP(i,1,n+1){
        fac[i]=fac[i-1]*i%mod;
    }
    UnionFind uf1(n);
    UnionFind uf2(n);
    vector<vector<ll> >a(n,vector<ll>((n)));
    rep(i,n){
        rep(j,n){
            cin>>a[i][j];
        }
    }
    rep(i,n){
        REP(j,i+1,n){
            bool f=1;
            rep(l,n){
                if(a[i][l]+a[j][l]>k)f=0;
            }
            if(f)uf1.unite(i,j);
        }
    }
    rep(i,n){
        REP(j,i+1,n){
            bool f=1;
            rep(l,n){
                if(a[l][i]+a[l][j]>k)f=0;
            }
            if(f)uf2.unite(i,j);
        }
    }
    ll ans=1;
    vector<ll>used1(n);
    vector<ll>used2(n);
    rep(i,n){
        if(used1[uf1.find(i)])continue;
        used1[uf1.find(i)]=1;
        ans=ans*fac[uf1.size(i)]%mod;
    }
    rep(i,n){
        if(used2[uf2.find(i)])continue;
        used2[uf2.find(i)]=1;
        ans=ans*fac[uf2.size(i)]%mod;
    }
    cout<<ans<<endl;
}

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <random>

const int32_t MOD = 998244353;

int32_t dfs(int32_t x, std::vector< bool > &vis, const std::vector< std::vector< bool > > &adj) {
    vis[x] = true;
    int32_t comp = 1;

    int32_t n = adj.size();
    for(int32_t i = 0; i < n; i++) {
        if(adj[x][i] && !vis[i]) {
            comp += dfs(i, vis, adj);
        }
    }

    return comp;
}

int32_t fact(int32_t x) {
    int32_t ans = 1;
    for(int32_t i = 2; i <= x; i++) {
        ans = (int64_t) ans * i % MOD;
    }
    return ans;
}

int32_t count(const std::vector< std::vector< bool > > &adj) {
    int32_t n = adj.size(), ans = 1;

    std::vector< bool > vis(n, false);
    for(int32_t i = 0; i < n; i++) {
        if(!vis[i]) {
            ans = (int64_t) ans * fact(dfs(i, vis, adj)) % MOD;
        }
    }

    return ans;
}

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

    int32_t n, k;
    std::cin >> n >> k;

    std::vector< std::vector< int32_t > > v(n, std::vector< int32_t >(n));
    for(int32_t i = 0; i < n; i++) {
        for(int32_t j = 0; j < n; j++) {
            std::cin >> v[i][j];
        }
    }

    std::vector< std::vector< bool > > rows(n, std::vector< bool >(n, false));
    for(int32_t i1 = 0; i1 < n; i1++) {
        for(int32_t i2 = i1 + 1; i2 < n; i2++) {
            int32_t cnt = 0;
            for(int32_t j = 0; j < n; j++) {
                if(v[i1][j] + v[i2][j] <= k) {
                    cnt++;
                }
            }
            if(cnt == n) {
                rows[i1][i2] = true;
                rows[i2][i1] = true;
            }
        }
    }

    std::vector< std::vector< bool > > cols(n, std::vector< bool >(n, false));
    for(int32_t i1 = 0; i1 < n; i1++) {
        for(int32_t i2 = i1 + 1; i2 < n; i2++) {
            int32_t cnt = 0;
            for(int32_t j = 0; j < n; j++) {
                if(v[j][i1] + v[j][i2] <= k) {
                    cnt++;
                }
            }
            if(cnt == n) {
                cols[i1][i2] = true;
                cols[i2][i1] = true;
            }
        }
    }

    std::cout << (int64_t) count(rows) * count(cols) % MOD << '\n';
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define ins insert
#define pb push_back
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define fk(x, a, b) for (int x = (a); x < (b); ++x)
#define fb(x, a, b) for (int x = (a); x <= (b); ++x)
#define fan(x, a, b) for (int x = (a); x >= (b); --x)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
map<ll, ll> is;
ll ff(ll x, ll y) {
	if (is[y]) return is[y];
	if (x >= y) return is[y] = x - y;
	if (y % 2) {
		return is[y] = min(y - x, min(ff(x, y - 1) + 1, ff(x, y + 1) + 1));
	}
	else {
		return is[y] = min(y - x, ff(x, y / 2) + 1);
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll x, y;
	cin >> x >> y;
	cout << ff(x, y) << endl;
    return 0;
}

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

int main(){
    long X, Y;
    cin >> X >> Y;
    unordered_map<long, long> dp;
    function<long(long)> solve = [&](long y){
        if(dp[y]) return dp[y];
        if(y == 1) return dp[y] = abs(X - y);
        if(y & 1) return dp[y] = min({abs(X - y), solve((y + 1) / 2) + 2, solve((y - 1) / 2) + 2});
        else return dp[y] = min(abs(X - y), solve(y / 2) + 1);
    };
    cout << solve(Y) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

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

	int ignore;
	string inp;
	cin >> ignore >> inp;

	string stac;
	for (const auto& x : inp)
	{
		stac.push_back(x);
		while (stac.length() >= 3 && stac[stac.length() - 1] == 'x' &&\
		stac[stac.length() - 2] == 'o' && stac[stac.length() - 3] == 'f')
			stac.pop_back(), stac.pop_back(), stac.pop_back();
	}
	cout << stac.length() << "\n";

	return 0;
}

#include<iostream>
using namespace std;

int main() {
  int N,i;
  string s,t="";
  cin>>N>>s;

  for (i=0;i<N;i++) {
    t += s[i];
    if (t.size() >= 3 && t.substr(t.size()-3) == "fox") t = t.substr(0, t.size()-3);
  }

  cout<<t.size()<<endl;
  return 0;
}


//GIVE ME AC!!!

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n);++i)
using ll = long long;
using P = pair<int,int>;
#define _GLIBCXX_DEBUG
int a,b,c,i,j,k,l,m,n,ans,tmp,hoge,fuga,piyo,count,N,M;
int x[10010],y[10010],d[110][110];

void solve(){
    for(k=0;k<n;k++){
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(d[i][j]>d[i][k]+d[k][j])d[i][j]=d[i][k]+d[k][j];
            }
        }
    }
}

int main(){
    cin >> N >> M;
    n = N/100 + (N%100)/10 + (N%100)%10;
    m = M/100 + (M%100)/10 + (M%100)%10;

    cout << max(n,m) <<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define all(v) v.begin(),v.end()

const int mod = 1e9 + 7;

int main (){
    fast_io;
    ll a, b;
    cin >> a >> b;
    bool flag = false;
    if (a < b) {
        swap(a, b);
        flag = true;
    }
    vector<ll> v;
    for (int i = 1; i <= a; i++)
        v.push_back(i);
    ll sum = (a * (a + 1)) / 2;
    for (int i = 1; i <= b; i++) {
        ll left = b - i;
        ll start = 1, last = sum, ans = -1;
        while (start <= last) {
            ll mid = (start + last) / 2;
            ll leftsum = sum - mid;
            if (leftsum >= (left * (left + 1)) / 2) {
                ans = mid;
                start = mid + 1;
            }
            else last = mid - 1;
        }
        v.push_back(-ans);
        sum -= ans;
    }
    if (flag == false) {
        for (auto it: v)
            cout << it << ' ';
    }
    else {
        for (auto it: v)
            cout << -it << ' ';
    }
    cout << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
 ios_base::sync_with_stdio(0);
 cin.tie(0);
 int n;
 cin >> n;
 long long w;
 cin >> w;
 const int maxa = 2e5 + 1;
 vector<long long> c (maxa, 0);
 while (n--) {
  long long s, t, p;
  cin >> s >> t >> p;
  c[s] += p;
  c[t] -= p;
 }
 long long sum = 0;
 for (int i = 0; i < maxa; i++) {
  sum += c[i];
  if (sum > w) {
   cout << "No" << '\n';
   return 0;
  }
 }
 cout << "Yes" << '\n';
 return 0;
}

//                               //
    //    author :  S4M4R    //
    // 14:51:47 | 13-10-2020 //
//                               //

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

using namespace std;

void IN()
{
    // cin.tie(nullptr)->sync_with_stdio(false);

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

void solve()
{
    vector <long long> v(4);
    for (auto & a : v) cin >> a;
    sort(v.begin(), v.end());
    long long a(v[0]), b(v[1]), c(v[2]), d(v[3]);
    if (a+b == c+d || a+b+c == d || a+d == b+c) {
        cout << "Yes" << '\n'; return;
    }
    cout << "No" << '\n';
}

int32_t main()
{
    IN();
    int t(1); //cin >> t;
    while(t--) solve(); return 0;
}


// Created at 2021/01/23 20:56
// {TODO}WA, {TODO}min, {TODO}diff
#include <bits/stdc++.h>
//#include <atcoder/all>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (lint i = 0, i##_len = n; i < i##_len; i++)
#define rep2(i, n) for (lint i = 1, i##_len = n; i <= i##_len; i++)
#define rep3(i, s, e) for (lint i = s, i##_len = e; i < i##_len; i++)
#define all(i) (i).begin(), (i).end()
#define print(s) cout << s << "\n";
#define print2(s1, s2) cout << s1 << " " << s2 << "\n";
#define mk_p make_pair
using namespace std;
//using namespace atcoder;
using lint = long long;
using pi = pair<int, int>;
using pl = pair<lint, lint>;
using vi = vector<int>;
using vl = vector<lint>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using qi = queue<int>;
using ql = queue<lint>;
constexpr int INF = 1 << 30;
constexpr lint INFl = 1LL << 62;

template<class T, class U>
istream &operator>>(istream &is, pair <T, U> &pair) {
    is >> pair.first >> pair.second;
    return is;
}

template<class T, class U>
ostream &operator<<(ostream &os, pair <T, U> &pair) {
    os << pair.first << " " << pair.second << "\n";
    return os;
}

template<class T>
istream &operator>>(istream &is, vector <T> &vec) {
    for (auto &v : vec) is >> v;
    return is;
}

template<class T>
ostream &operator<<(ostream &os, const vector <T> &vec) {
    os << '[';
    for (auto v : vec) os << v << ',';
    os << ']';
    return os;
}

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

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

template<class T>
T gcd(T x, T y) {
    if (x == 0) {
        if (y == 0)
            return 1;
        else
            return y;
    }
    if (y == 0) return x;
    if (x % y == 0)
        return y;
    else
        return gcd(y, x % y);
}

template<class T>
inline T lcm(T x, T y) {
    return x / gcd(x, y) * y;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // cout << fixed << setprecision(15);
    /*----------------------------------------------------*/
    int n, x; cin >> n >> x;
    x *= 100;
    vpi vp(n); cin >> vp;
    int now = 0;
    rep(i, n) {
        now += vp.at(i).first * vp.at(i).second;
        if (now > x) {
            print(i + 1)
            return 0;
        }
    }
    print(-1)
}
// map->bucket?
// あまり->非負か確認
// set? multiset?
// オーバーフロー確認(最大値を求める)
// 入力確認
// 型確認
// ループ回数確認(TLE)
// 小数の計算は避けよう(割り算)(0割りだめ！絶対！)
// Python を使っては?
// 擬似コードを作っては?
// 汚くてもok

#include <bits/stdc++.h>
using namespace std;
#define let const auto
#define var auto
#define ref var &
#define cref let &
typedef uint32_t uint;
typedef int64_t int64;
typedef uint64_t uint64;
template<class T>
T read() {
  T x;
  cin >> x;
  return x;
}

let MULTIPLE_TEST_CASES = false;

void solve() {
  let n = read<uint>();
  var k = read<int64>();
  var friends = vector<pair<int64, int64>>(n);
  for (uint i = 0; i < n; i++) {
    cin >> friends[i].first;
    cin >> friends[i].second;
  }
  sort(friends.begin(), friends.end());
  int64 curr = 0;
  for (cref p : friends) {
    let max = curr + k;
    if (max >= p.first) {
      k -= p.first - curr;
      k += p.second;
      curr = p.first;
    } else {
      curr += k;
      k = 0;
      break;
    }
  }
  curr += k;
  cout << curr;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  let testCases = MULTIPLE_TEST_CASES ? read<uint>() : 1;
  for (uint testCase = 1; testCase <= testCases; testCase++) {
    solve();
  }
}


/*
            ********************
			**    MEET SHAH   **
			**     DA-IICT    **
			**  decoder_0712  **
			********************
*/
#include <bits/stdc++.h>
using namespace std;

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

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define setpre(x,d) fixed<<setprecision(d)<<x
//#define fill(a,x) memset(a,x,sizeof(a));
#define inarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define outarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define maxa(a,n) *max_element(a,a+n)
#define mina(a,n) *min_element(a,a+n)
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define rf(i,s,e) for(ll i=s-1;i>=e;i--)
#define deb1(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;
#define deb4(x,y,z,w) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<"\t"<<#w<<" : "<<w<<endl;
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz size()
#define len length()
#define all(v) v.begin(),v.end()
#define PI 3.141592653589793
#define MOD 1000000007
#define NN (const int)5e5+10
#define ub upper_bound

typedef long long int ll;
typedef long double lld;
typedef pair<long long,long long> pll;
typedef vector<long long> vll;
typedef vector<pair<long long,long long> > vpll;

bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); }
// void fileio()	{	freopen("input.txt","r",stdin);	freopen("output.txt","w",stdout);	}
//	vll fact(NN),invfact(NN);
//	void EE(ll a,ll b,ll &x,ll &y) {	if(b==0) {	x=1,y=0; return ;	}		EE(b,a%b,x,y);	ll temp=x; x=y; y=temp-(a/b)*y;	}
//	ll inverse(ll a,ll mod) 	{	ll x,y;	EE(a,mod,x,y);	return (x+mod)%mod;	}
//	void build() { fact[0]=1; fr(i,1,NN) fact[i]=(fact[i-1]*i)%MOD;	invfact[NN-1]=inverse(fact[NN-1],MOD); rf(i,NN-1,0) invfact[i]=(invfact[i+1]*(i+1))%MOD; }
//	ll ncr(ll n,ll r)       	{	if(n<0 || r<0 || r>n) return 0;	return (fact[n]*((invfact[n-r]*invfact[r])%MOD))%MOD;	}
//	ll choose(ll n,ll k) 	  	{	if(k==0)  return 1; return (n* choose(n-1,k-1))/k;	}
ll logy(ll x,ll y)	{	return ceil((lld)(log(x)/log(y)));	}
ll powe(ll a,ll b)	{	ll re=1;	while(b){	if(b&1) re=(re*a)%MOD;	a=(a*a)%MOD;	b=b>>1;}	return re;	}
/*
ll i,j,x,y,k,z,cnt;
vector<vector<ll> > a(100005);
vector<ll> vis(100005,0),visited(100005,0);
void dfs(int u)
{
    visited[u]=1;

    vector<ll>::iterator it;

    for(it=a[u].begin();it!=a[u].end();it++)
    {
        if(visited[*it]==0)
        {
            dfs(*it);
        }
    }
}
*/

bool flag=0,flag1=0,flag2=0;

int main()
{
	IOS
	ll a,b,c,d,ans;
	cin>>a>>b>>c>>d;
	ll t1=a-c;
	ll t2=a-d;
	ll t3=b-c;
	ll t4=b-d;
	ll mx1=max(t1,t2);
	ll mx2=max(t3,t4);
	cout<<max(mx1,mx2)<<endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
//#define cccgift
#define lowbit(x) ((x)&-(x))
#define rep(i,l,r) for(res i=l,_r=r;i<=_r;++i)
#define per(i,r,l) for(res i=r,_l=l;i>=_l;--i)
#define mkp make_pair
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem0n(a,n) memset(a,0,(n)*sizeof(a[0]))
#define iter(x,v) for(res v,_p=head[x];v=ver[_p],_p;_p=nxt[_p])
#ifdef cccgift //by lqh
#define SHOW(x) cerr<<#x"="<<(x)<<endl
#else
#define SHOW(x) 0
#endif
//#define getchar()(ip1==ip2&&(ip2=(ip1=ibuf)+fread(ibuf,1,1<<21,stdin),ip1==ip2)?EOF:*ip1++)
//char ibuf[1<<21],*ip1=ibuf,*ip2=ibuf;
template<typename T>
inline void read(T &x)
{
    static char ch;bool f=1;
    for(x=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') f=0;
    for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=getchar());x=f?x:-x;
}
template<typename T>
void print(T x)
{
    if (x<0) x=-x,putchar('-');
    if (x>9) print(x/10);
    putchar(x%10+48);
}
template<typename T>
inline void print(T x,char ap) {print(x);if (ap) putchar(ap);}
template<typename T>
inline void chkmax(T &x,const T &y) {x=x<y?y:x;}
template<typename T>
inline void chkmin(T &x,const T &y) {x=x<y?x:y;}
unordered_map<int,bool> mp;
int t,n,x;
int main()
{
	read(t);
	while(t--) {
		read(n),mp.clear();
		rep(i,1,n) read(x),mp[x]^=1;
		if(n&1) {puts("Second");continue;}
		bool flag=false;
		for(auto &p:mp) if(p.second) flag=true;
		puts(flag?"First":"Second");
	}
	return 0;
}
/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do something instead of nothing and stay organized
*/


#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T1, class T2>
bool chmax(T1 &a, const T2 b){if(a < b){a = b; return true;} return false;}
template <class T1, class T2>
bool chmin(T1 &a, const T2 b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (const T &i : v) os << i << ' ';
    return os;
}
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    cout << head << ' ';
    co(forward<Tail>(tail)...);
}
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    cerr << head << ' ';
    ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1000000001;
constexpr ll LINF = 1000000000000000001;
constexpr ll MOD = 1000000007;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);

struct S {
    ll l, v;

    bool operator>(const S &rhs) const {
        return v > rhs.v;
    }
    bool operator<(const S &rhs) const {
        return v < rhs.v;
    }
    bool operator==(const S &rhs) const {
        return v == rhs.v;
    }

    void input() {
        cin >> l >> v;
    }

};

int main(void) {
    ll n, m;
    cin >> n >> m;
    Vec w(n);
    rep(i, n) cin >> w[i];
    ll max_w = *max_element(all(w));

    vector<S> ss(m);
    rep(i, m) ss[i].input();
    rep(i, m) {
        if(max_w > ss[i].v) {
            co(-1);
            return 0;
        }
    }

    sort(all(ss));
    Vec max_l(m);
    rep(i, m) {
        max_l[i] = ss[i].l;
        if (i) chmax(max_l[i], max_l[i - 1]);
    }

    Vec p(n);
    rep(i, n) p[i] = i;
    ll ans = LINF;
    do {
        Vec cs(n);
        rep(i, n) {
            cs[i] = w[p[i]];
            if (i) cs[i] += cs[i - 1];
        }
        // ce(cs);

        Vec q(n, 0);
        rep(i, n) {
            REP(j, i + 1, n) {
                ll pos = lower_bound(all(ss), S{0, cs[j]}) - ss.begin();
                if (pos == 0) continue;
                chmax(q[j], q[i] + max_l[pos - 1]);
            }
            REP(j, i + 1, n) cs[j] -= w[p[i]];
        }
        chmin(ans, q.back());
        // ce(ans);
    } while (next_permutation(all(p)));
    co(ans);

    return 0;
}

//include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)

template <typename T>
T my_abs(T x){
    return (x >= 0)? x : -x;
}

template <typename T>
void chmax(T &a, T b){
    a = max(a, b);
}

template <typename T>
void chmin(T &a, T b){
    a = min(a, b);
}

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

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

int main(void){
    int i, j;
    int n, a[200003];

    cin >> n;
    rep0(i, n){
        cin >> a[i];
    }

    ll amax, asum, basesum;
    amax = 0;
    asum = 0;
    basesum = 0;
    rep0(i, n){
        chmax(amax, (ll)a[i]);
        asum += a[i];
        basesum += asum;
        cout << basesum + amax * (i + 1) << endl;
    }

    return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, e) for (int i = (int)(s); i < (int)(e); i++)
#define _rep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define _REP(i, e, s) for (int i = (int)(e - 1); i >= (int)(s); i--)

#define yes cout << "yes" << endl;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;

#define no cout << "no" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;

#define AC cout << "AC" << endl;
#define WA cout << "WA" << endl;

#define out(s) cout << s << endl;

#define ll long long
#define ull unsigned long long

#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

const unsigned int BIT_FLAG_0 = (1 << 0);  // 0000 0000 0000 0001
const unsigned int BIT_FLAG_1 = (1 << 1);  // 0000 0000 0000 0010
const unsigned int BIT_FLAG_2 = (1 << 2);  // 0000 0000 0000 0100
const unsigned int BIT_FLAG_3 = (1 << 3);  // 0000 0000 0000 1000
const unsigned int BIT_FLAG_4 = (1 << 4);  // 0000 0000 0001 0000
const unsigned int BIT_FLAG_5 = (1 << 5);  // 0000 0000 0010 0000
const unsigned int BIT_FLAG_6 = (1 << 6);  // 0000 0000 0100 0000
const unsigned int BIT_FLAG_7 = (1 << 7);  // 0000 0000 1000 0000

const double PI = 3.14159265358979323846;

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

#define H_MAX 500
#define W_MAX 500
#define INF 1e9 + 7
const long long MOD = 998244353;

bool IsPrime(int num) {
    if (num < 2)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;  // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2) {
        if (num % i == 0) {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}

int main() {
    ll N, cnt = 0;
    unordered_set<ll> us;
    cin >> N;

    for (ll i = 2; i * i <= N; i++) {
        ll tmp = i * i;
        while (tmp <= N) {
            us.insert(tmp);
            tmp *= i;
        }
    }
    cout << N - us.size() << endl;
}

#include <bits/stdc++.h>

#define LL long long
#define ull unsigned long long
#define F(i, j, k) for (int i = j; i <= k; ++i)
#define DF(i, j, k) for (int i = j; i >= k; --i)

using namespace std;

template <typename T> inline void read(T &n) {
	T w = 1;
	n = 0;
	char ch = getchar();
	while (!isdigit(ch) && ch != EOF) {
		if (ch == '-') w = -1;
		ch = getchar();
	}
	while (isdigit(ch) && ch != EOF) {
		n = (n << 3) + (n << 1) + (ch & 15);
		ch = getchar();
	}
	n *= w;
}

template <typename T> inline void write(T x) {
	T l = 0;
	ull y = 0;
	if (!x) { putchar(48); return; }
	if (x < 0) { x = -x; putchar('-'); }
	while (x) { y = y * 10 + x % 10; x /= 10; ++l; }
	while (l) { putchar(y % 10 + 48); y /= 10; --l; }
}

template <typename T> inline void writes(T x) {
	write(x);
	putchar(' ');
}

template <typename T> inline void writeln(T x) {
	write(x);
	puts("");
}

template <typename T> inline void checkmax(T &a, T b) { a = a > b ? a : b; }

template <typename T> inline void checkmin(T &a, T b) { a = a < b ? a : b; }

const int N = 2e5 + 100;

int cnt, head[N], to[N << 1], nxt[N << 1];

inline void addedge(int x, int y) {
	nxt[++cnt] = head[x];
	to[cnt] = y;
	head[x] = cnt;
}

int tp[N], rt, depth[N], mx = 0;

inline void dfs(int x, int fa) {
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa) continue;
		depth[to[i]] = depth[x] + 1;
		if (depth[to[i]] >= mx) { rt = to[i]; mx = depth[to[i]]; }
		dfs(to[i], x);
	}
}

int tot, ans[N];

inline void dfs2(int x, int fa) {
	ans[x] = ++tot;
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa || tp[to[i]]) continue;
		dfs2(to[i], x);
	}
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa || !tp[to[i]]) continue;
		dfs2(to[i], x);
	}
	++tot;
}

inline void dfs3(int x, int fa) {
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa) continue;
		dfs3(to[i], x);
		if (tp[to[i]]) tp[x] = 1;
	}
}

int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	int n;
	read(n);
	F(i, 1, n - 1) {
		int x, y;
		read(x); read(y);
		addedge(x, y);
		addedge(y, x);
	}
	dfs(1, 0);
	depth[rt] = 0;
	tp[rt] = 1;
	dfs(rt, 0);
	dfs3(rt, 0);
	dfs2(rt, 0);
	F(i, 1, n) writes(ans[i]);
	return 0;
}


#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <unordered_set>

#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int mod = 1000000007;

int f[maxn];
int pd(int x){
  if(x!=f[x])f[x]=pd(f[x]);
  return f[x];
}
int a[maxn],b[maxn];
ll c[maxn];
int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  //freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n,m;
  cin>>n>>m;
  for(int i=1;i<=n;i++)f[i]=i;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=n;i++)cin>>b[i];
  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    int px = pd(x);
    int py = pd(y);
    if(px!=py)f[px] = py;
  }
  memset(c, 0,sizeof(c));
  for(int i=1;i<=n;i++){
    int x = pd(i);
    c[x] +=(a[i]-b[i]);
  }
  bool sign = 0;
  for(int i=1;i<=n;i++)if(c[i]!=0)sign=1;
  cout<<(sign?"No":"Yes")<<endl;
  return 0;
}

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

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

/* // Ordered Set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
typedef tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define cntleadz(x) __builtin_clz(x) // or add ll at the end for long long
#define cnttrailz(x) __builtin_ctx(x)
#define cntpop(x) __builtin_popcount(x)
#define binparity(x) __builtin_parity(x)
#define pb push_back
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)

int binExp(int x,int n)
{
    int res=1;
    while(n)
    {
        if(n&1) res=(res*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return res;
}

// int fact[100001];
// int modInv(int i) {return binExp(i,mod-2);}
// int ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r]))%mod*modInv(fact[n-r])%mod:0);}

int a[200001];
int b[200001];
vi adj[200001];
bool vis[200001];
int sum;

void dfs(int v)
{
    vis[v] = 1;
    sum += a[v] - b[v];

    for(auto x : adj[v])
    {
        if(!vis[x])
        {
            dfs(x);
        }
    }
}

signed main()
{
    fast

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

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

    f1(i,1,n) cin>>a[i];
    f1(i,1,n) cin>>b[i];

    make_graph(m);

    bool yes = 1;
    f1(i,1,n)
    {
        sum = 0;
        if(!vis[i])
        {
            dfs(i);
        }

        if(sum!=0)
        {
            yes = 0;
            break;
        }
    }

    cout<<(yes?"Yes":"No");
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

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

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

const int N = 1 << 11;
const ll inf = 1e18;
ll d[N][N];
vector<pair<int, int>> g[N];
int n;

void dijkstra(int s, ll d[]) {
	fill_n(d, n, inf);
    d[s] = 0;

    using P = pair<ll, int>;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push({0, s});
    while (!q.empty()) {
		auto [d_v, v] = q.top(); q.pop();
        if (d_v != d[v]) continue;
        for (auto [to, len] : g[v])
            if (ckmin(d[to], d[v] + len))
				q.push({d[to], to});
    }
}

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

	int m; cin >> n >> m;
	rep(e, 0, m) {
		int u, v, w; cin >> u >> v >> w; --u; --v;
		g[u].emplace_back(v, w);
	}
	rep(u, 0, n) dijkstra(u, d[u]);
	rep(u, 0, n) {
		ll ans = inf;
		for (auto [v, w] : g[u])
			ckmin(ans, w + d[v][u]);
		cout << (ans < inf ? ans : -1) << '\n';
	}
}

#include <bits/stdc++.h>
using namespace std;
template<class C>constexpr int sz(const C&c){return int(c.size());}
using ll=long long;using ld=long double;constexpr const char nl='\n',sp=' ';

vector<int> solve(vector<int> P) {
  int N = P.size();
  vector<int> ind(N);
  vector<bool> done(N - 1, false);
  for (int i = 0; i < N; i++) ind[--P[i]] = i;
  vector<int> ans;
  for (int i = 0; i < N - 1; i++) {
    for (int j = ind[i]; j > i; j--) {
      if (done[j - 1]) return vector<int>{-1};
      done[j - 1] = true;
      ans.push_back(j);
      swap(ind[P[j - 1]], ind[P[j]]);
      swap(P[j - 1], P[j]);
    }
  }
  if (sz(ans) != N - 1) return vector<int>{-1};
  return ans;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // freopen("err.txt", "w", stderr);
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &&a : A) cin >> a;
  for (auto &&a : solve(A)) cout << a << nl;
  return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stdlib.h>
#include <map>
#include <cmath>
#define MOD_P 1000000007
#define MOD_Q 998244353
#define PI 3.14159265358979
#define ll long long
using namespace std;

bool rec(int i, int w, vector<vector<int>> dp, vector<int> a) {
	if (i == 0) {
		if (w == 0) {
			return true;
		} else {
			return false;
		}
	}
	bool res = false;

	if (w >= a[i - 1] && rec(i - 1, w - a[i - 1], dp, a))res = true;
	if (rec(i - 1, w, dp, a))res = true;
	return dp[i][w] = res;

}

//int main() {
//	int n;
//	cin >> n;
//	vector<int> t(n);
//	int sum = 0;
//	int left, right;
//	for (int i = 0; i < n; i++) {
//		cin >> t[i];
//		sum += t[i];
//	}
//
//	left = (sum + 1) / 2;
//
//	right = sum;
//	int half;
//	while (left + 1 != right) {
//		half = (right + left) / 2;
//		printf("[%d %d %d]", left, right, half);
//		vector<vector<int>> dp(n + 1, vector<int>(half + 1, -1));
//		if (rec(n, half, dp, t)) {
//			printf("can\n");
//			right = half;
//		} else {
//			printf("can't\n");
//			left = half;
//		}
//	}
//
//
//
//	printf("%d", right);
//
//	return 0;
//}

int main() {
	int n, m, a, b;
	cin >> n >> m;
	vector<vector<int>>edge(n);
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		a--;
		b--;
		edge[a].emplace_back(b);
	}

	vector<int>st;
	int now;
	int ans = n;
	for (int i = 0; i < n; i++) {
		vector<bool>first(n, true);
		st.push_back(i);
		first[i] = false;
		//printf("%d:", i + 1);
		while (!st.empty()) {
			now = st.back();
			st.pop_back();

			for (int v : edge[now]) {
				if (first[v]) {
					first[v] = false;
					//printf("%d ", v + 1);
					st.push_back(v);
					ans++;
				}
			}
		}
		//printf("\n");
	}

	printf("%d", ans);

}

#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <list>
#include <unordered_set>
#include <tuple>
#include <cmath>
#include <limits>
#include <type_traits>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <regex>
#include <random>
#include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;

typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exout(x) printf("%.12f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 1010101;

//組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX_N; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// mod.m での a の逆元 a^ { -1 } を計算する
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

ll lcm(ll x, ll y) {
	if (x == 0 || y == 0)return 0;
	return (x / gcd(x, y) * y);
}

//union-find木について
//サイズによる更新をしている
ll par[4010101];
ll rank2[4010101];

void init(ll n) {
	rep(i, n) {
		par[i] = i;
		rank2[i] = 1;
	}
}

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

void unite(ll x, ll y) {
	x = find(x);
	y = find(y);
	if (x == y)return;
	if (rank2[x] < rank2[y]) {
		par[x] = y;
		rank2[y] += rank2[x];
	}
	else {
		par[y] = x;
		rank2[x] += rank2[y];
	}
}

//セグ木
class segment_tree {
private:
	ll sz;
	std::vector<ll> seg;
	std::vector<ll> lazy;
	void push(ll k) {
		if (k < sz) {
			lazy[k * 2] = max(lazy[k * 2], lazy[k]);
			lazy[k * 2 + 1] = max(lazy[k * 2 + 1], lazy[k]);
		}
		seg[k] = max(seg[k], lazy[k]);
		lazy[k] = 0;
	}
	void update(ll a, ll b, ll x, ll k,ll l, ll r) {
		push(k);
		if (r <= a || b <= l) return;
		if (a <= l && r <= b) {
			lazy[k] = x;
			push(k);
			return;
		}
		update(a, b, x, k * 2, l, (l + r) >> 1);
		update(a, b, x, k * 2 + 1, (l + r) >> 1, r);
		seg[k] = max(seg[k * 2], seg[k * 2 + 1]);
	}
	ll range_max(ll a, ll b, ll k, ll l, ll r) {
		push(k);
		if (r <= a || b <= l) return 0;
		if (a <= l && r <= b) return seg[k];
		ll lc = range_max(a, b, k * 2, l, (l + r) >> 1);
		ll rc = range_max(a, b, k * 2 + 1, (l + r) >> 1, r);
		return max(lc, rc);
	}
public:
	segment_tree() : sz(0), seg(), lazy() {};
	segment_tree(ll N) {
		sz = 1;
		while (sz < N) {
			sz *= 2;
		}
		seg = std::vector<ll>(sz * 2, 0);
		lazy = std::vector<ll>(sz * 2, 0);
	}
	void update(ll l, ll r, ll x) {
		update(l, r, x, 1, 0, sz);
	}
	ll range_max(ll l, ll r) {
		return range_max(l, r, 1, 0, sz);
	}
};

ll dx[4] = { 1,-1,0,0 };
ll dy[4] = { 0,0,1,-1 };
ll a[1010][1010];
vector<vector<ll>>to;
bool flag[2020];
ll ans = 0;

void dfs(ll x) {
	ans++;
	flag[x] = true;
	for (auto nx : to[x]) {
		if (flag[nx] == false) {
			dfs(nx);
		}
	}
}

//long longしか使わない
//素数は1より大きい
//lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
//upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
	ll n, m;
	cin >> n >> m;
	to.resize(n);
	rep(i, m) {
		ll x, y;
		cin >> x >> y;
		x--, y--;
		to[x].emplace_back(y);
	}
	rep(i, n) {
		dfs(i);
		rep(j, n) {
			flag[j] = false;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()

vector<vector<ll>> memo;
vector<vector<ll>> g;
   ll N,M;
vector<ll> ind;
bool p;

bool dp(ll i,ll j) {

if(memo[i][j]>=0) {
    if(memo[i][j]==0) {
        return false;
    }
    else {
        return true;
    }
}

bool a=false;
ll now=g[i][j];

if(i<ind.size()-1&&ind[i+1]==ind[i]+1) {
    ll x=lower_bound(all(g[i+1]),now)-g[i+1].begin();
    x--;
    if(x>=0) {
    a|=dp(i+1,x);
    }

    if(x<0&&ind[i+1]==N) {
        a=true;
    }
}

if(ind[i]>0&&i>0&&ind[i-1]==ind[i]-1) {
    ll x=lower_bound(all(g[i-1]),now)-g[i-1].begin();
    x--;
    if(x>=0) {
        a|=dp(i-1,x);
    }
    if(x<0&&ind[i-1]==N) {
        a=true;
    }
}

if(ind[i]-1==N&&!p) {
    a=true;
}
if(ind[i]+1==N&&!p) {
    a=true;
}

if(a) {
    memo[i][j]=1;
}
else {
    memo[i][j]=0;
}

return a;

}

int main() {
 cin>>N>>M;
vector<pair<ll,ll>> vec(M);
for(ll i=0;i<M;i++) {
    ll X,Y;cin>>X>>Y;
    vec[i]=make_pair(Y,X);
}
sort(all(vec));

ind=vector<ll> (0);

for(ll i=0;i<M;i++) {
    ll x,y;
    tie(x,y)=vec[i];
    if(ind.size()==0||ind.back()!=x) {
        ind.push_back(x);
    }

}

g=vector<vector<ll>> (ind.size(),vector<ll> (0));
memo=vector<vector<ll>> (ind.size(),vector<ll> (0));

ll count=-1;
ll now=-1;
for(ll i=0;i<vec.size();i++) {
    if(vec[i].first!=now) {
        count++;
        now=vec[i].first;
    }
    g[count].push_back(vec[i].second);
    memo[count].push_back(-1);

}

ll ans=1;
p=false;
for(auto x:ind) {
    if(x==N) {
        p=true;
        continue;
    }
    ans++;
}

for(ll i=0;i<ind.size();i++) {
    if(!dp(i,g[i].size()-1)) {
        ans--;
    }
}

cout<<ans<<endl;
}

#include<iostream>
#include <string>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#define ll long long int
#define ld long double
#define pb push_back
#define endl "\n"
#define mp make_pair
#define sz(v) (int)(v.size())
#define len(s) (ll)(s.length())
#define M 1000000007
#define all(v) v.begin(),v.end()
#define fr(a,b,c) for(ll i=a;i<=b;i+=c)
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll inf = 9e18;
const ld pi = 2*acos(0.0);
using namespace std;
ll power(ll a,ll n)
{
    if(n==0){
        return 1;
    }
    ll p=power(a,n/2) % M;
    p = (p * p) % M;
    if (n%2==1)
    {
        p=(p*a)%M;
    }
    return p;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
const int N=1e6+5;
vector <ll> sieve(N,0);
void si()
{
    sieve[1]=1;
    for(int i=2;i<N;i++){
        if(sieve[i]==0){
            sieve[i]=1;
            for(int j=2*i;j<N;j+=i){
                sieve[j]=i;
            }
        }
    }
}
int main(){
    ios;
    ll n,m,t;
    cin>>n>>m>>t;
    ll prev=0;
    ll n1=n;
    for(int i=0;i<m;i++){
        ll l,r;
        cin>>l>>r;
        n-=(l-prev);
        if(n<=0){
            cout<<"No"<<endl;
            return 0;
        }
        n+=r-l;
        if(n>n1){
            n=n1;
        }
        prev=r;
    }
    n-=(t-prev);
    if(n<=0){
        cout<<"No"<<endl;
        return 0;
    }
    cout<<"Yes"<<endl;
    return 0;
}


#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;

typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}

#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power (int a, int b = mod - 2)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int dp[3001][3001] = {}, sum[3001][6001] = {};
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	dp[0][0] = sum[0][0] = 1;
	readb(n, k);
	FOR (i, 1, n)
	{
	    FORD (j, 2*n, n + 1) sum[i][j] = sum[i - 1][j - 2];
	    FORD (j, n, 1)
	    {
	        dp[i][j] = sum[i][2*j];
	        if (j >= 2) sum[i][j] = (dp[i][j] + sum[i - 1][j - 2]) % mod;
	        else sum[i][j] = dp[i][j];
	    }
	}

    print(dp[n][k]);
}

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

template <typename T>
int len(const T& a) {
	return a.size();
}
using ll = long long;

const int MOD = 998244353;

void Ok(int& a) {
	if (a >= MOD)
		a -= MOD;
}

const int N = 3010;

int n, k;
int dp[N][N << 1];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	scanf("%d%d", &n, &k);
	dp[0][0] = 1; // empty set
	for (int i = 1; i <= n; ++i) { // size of set
		for (int j = n; j >= 1; --j) {
			Ok(dp[i][j] = dp[i - 1][j - 1] + dp[i][j << 1]);
		}
	}
	printf("%d", dp[n][k]);
	return 0;
}


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

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m;
    cin >> n >> m;
    vector <pair <int,int> > cond[n];
    for(int i=0;i<m;i++){
    	int x,y,z;
    	cin >> x >> y >> z;
    	--y;
    	cond[x-1].push_back({y,z});
    }
    int dp[(1<<n)]={};
    dp[0]=1;
    for(int msk=0;msk<(1<<n);msk++){
    	int s=__builtin_popcount(msk);
    	int  cnt[n]={};
    	for(int i=0;i<n;i++){
    		if(msk&(1<<i))cnt[i]++;
    	}
    	for(int i=1;i<n;i++)
    		cnt[i]+=cnt[i-1];
    	for(int i=0;i<n;i++){
    		if(msk&(1<<i))continue;
    		int ok=1;
    		for(auto [y,z]:cond[s]){
    			int c=cnt[y];
    			if(i<=y)c++;
    			if(c>z)ok=0;
    		}
    		if(ok)dp[msk^(1<<i)]+=dp[msk];
    	}
    }
    cout << dp[(1<<n)-1];
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)

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

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

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

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

	vector<vector<pair<int, int>>> constraint(n);
	rep(i, 0, m) {
		int x, y, z; cin >> x >> y >> z;
		constraint[--x].emplace_back(y, z);
	}

	vector<ll> dp(1 << n);
	dp[0] = 1;
	rep(S, 0, 1 << n) {
		int x = __builtin_popcount(S);
		rep(i, 0, n) {
			if (S >> i & 1) continue;

			int T = S | 1 << i;
			bool valid = 1;
			for (auto [y, z] : constraint[x])
				valid &= __builtin_popcount(T & ((1 << y) - 1)) <= z;
			if (valid) dp[T] += dp[S];
		}
	}

	cout << dp.back() << '\n';
}