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

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
//mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read(){
  #define nc getchar()
  bool f=0;int x=0;char c=nc;
  while(c<48)f|=c=='-',c=nc;
  while(c>47)x=x*10+(c^48),c=nc;
  return f?-x:x;
  #undef nc
}

typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

string R(string str){
  string res;
  for(char c:str)res.push_back(c=='A'?'B':'A');
  return res;
}

vector<string>divide(int N){
  vector<string>res;
  if(N==1)return res;
  vector<string>vec=divide(N/2);
  string ini="";
  rep(i,1,N/2)ini+='A';
  rep(i,1,N/2)ini+='B';
  res.push_back(ini);
  for(string str:vec)res.push_back(str+str);
  for(string str:vec)res.push_back(str+R(str));
  return res;
}

void solve(){
  int N;cin>>N;
  auto ans=divide(1<<N);
  printf("%d\n",(int)ans.size());
  for(string str:ans)cout<<str<<endl;
}

signed main(){
//  int T=read();
//  while(T--)solve();
  solve();
  return 0;
}

//g++ -std=c++14 test.cpp -o test.out

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;

#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

//https://github.com/atcoder-live/library/blob/master/mint.cpp
struct mint {
  LL x;
  mint(LL x=0):x((x%mod+mod)%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(LL t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

struct combination {
  vector<mint> fact, ifact;//factに階乗を計算、ifactはその各々に対する逆元
  combination(LL n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (LL i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (LL i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(LL n,LL k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

LL N;
char CAA,CAB,CBA,CBB;

mint ans = 1;

mint dp(){
    //dp[i][c]:
    //i文字目までカウント、最後に使った文字がc
    vector<map<char,mint>> dp((N-3)+1);
    dp[0]['A'] = 1;
    dp[0]['B'] = 1;//Bは2文字以上連続しない
    for(int i=0;i<N-3;i++){
        dp[i+1]['A'] = (dp[i]['A'] + dp[i]['B']);
        dp[i+1]['B'] = dp[i]['A'];
    }
    return dp[N-3]['A'];
}
void solve(){
    if(N<=3) return;
    if(CAB=='A'){
        if(CAA=='A') return;
        if(CAA=='B'){
            if(CBA=='A'){
                ans = dp();
                return;
            }
            if(CBA=='B'){
                ans = mint(2).pow(N-3);
                return;
            }
        }
    }
    if(CAB=='B'){
        if(CBB=='B') return;
        if(CBB=='A'){
            if(CBA=='B'){
                ans = dp();
                return;
            }
            if(CBA=='A'){
                ans = mint(2).pow(N-3);
                return;
            }
        }
    }
}

int main(){
    cin >> N;
    cin >> CAA >> CAB >> CBA >> CBB;

    solve();

    cout << ans.x << endl;
    return 0;
}


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

int main() {

	int a, b, c, d;
	cin >> a >> b >> c >> d;
	if(c * d <= b) cout << -1 ;
	else {
		cout << (a + c * d - b - 1) / (c * d - b) ;
	}

	return 0 ;
}

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

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#define PI 3.14159265358979323846264338327950L
#define RADIAN PI/180.0
typedef long long ll;

int main(){
  ll a, b, c, d; cin >> a >> b >> c >> d;
	if (c * d <= b) {
		cout << -1 << endl;
		return 0;
	}

	ll ans = 0,buff_c=c,buff_b=b;

	c = 0LL;
	b = 0LL;

	while (true) {

		if (a + b <= c * d) {
			cout << ans << endl;
			break;
		}

		b += buff_b;
		c += buff_c;
		ans++;

	}
}


/*
    author : aryan57
    created : 06-June-2021 18:04:35 IST
*/
#include <bits/stdc++.h>
using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

#define int long long
#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)

const int mxn = 1e5;
const long long INF = 1e16;
const int32_t M = 1000000007;
// const int32_t M = 998244353;
const long double pie = acos(-1);

struct Edge
{
    int u, v, d, c;
};

void solve_LOG()
{
    int n, m;
    cin >> n >> m;

    vector<Edge> adj[n + 1];

    F(i, 1, m)
    {
        Edge e;
        cin >> e.u >> e.v >> e.c >> e.d;
        // if(e.u==e.v)continue;
        adj[e.u].push_back(e);
        adj[e.v].push_back(e);
    }

    vector<int> mn(n + 1, INF);

    mn[1] = 0;

    using pii = pair<int, int>;
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push({0, 1});
    while (!q.empty())
    {
        int v = q.top().second;
        int mn_v = q.top().first;
        q.pop();
        if (mn_v != mn[v])
            continue;

        for (auto edge : adj[v])
        {
            int c=edge.c;
            int d=edge.d;

            int to=-1;
            if(edge.u==v)to=edge.v;
            else to=edge.u;

            int temp=INF;

            int dd=(int)sqrt(d);

            F(t,max(0ll,dd-(mn[v]+1)-5),max(0ll,dd-(mn[v]+1)+5))
            {
                temp=min(temp,mn[v]+t+c+d/(mn[v]+t+1));
            }

            if (temp < mn[to])
            {
                mn[to] = temp;
                q.push({mn[to], to});
            }
        }
    }

    if (mn[n] == INF)
    {
        cout << "-1\n";
        return;
    }

    cout << mn[n];
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
    sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
    segmented_sieve();
#endif
#ifdef ARYAN_FACT
    fact_init();
#endif
    // cout<<fixed<<setprecision(10);
    int _t = 1;
    // cin>>_t;
    for (int i = 1; i <= _t; i++)
    {
        // cout<<"Case #"<<i<<": ";
        solve_LOG();
    }
    return 0;
}
//	parsed : 06-June-2021 18:04:32 IST

// abc181_f
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e9)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    vector<int> rank;
    vector<int> size; // size[i]: iが親なら連結成分の数

    UnionFind(int N) : par(N), rank(N), size(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) {par[i] = i; rank[i] = 0; size[i] = 1;}
    }

    int find(int x) { // データxが属する木の根を再帰で得る：find(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = find(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = find(x); //xの根をrx
        int ry = find(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま

        // rank[rx] >= rank[ry] となるように rx と ry を swap
        if(rank[rx] < rank[ry]) swap(rx, ry);

        // 同じ高さだったならばまとめたことによって高さ +1
        if(rank[rx] == rank[ry]) ++rank[rx];

        par[ry] = rx; // root rx に ry の木をつなぐ
        size[rx] += size[ry];
        size[ry] = 0;

    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = find(x);
        int ry = find(y);
        return rx == ry;
    }

    int get_size(int x){
        int rx = find(x);
        return size[rx];
    }
};

int N;
vector<vector<double>> dist;

bool valid(double r){

    UnionFind uf(N+2);
    REP(i, N+2) REPN(j, i+1, N+2){
        if (dist[i][j] < 2 * r){
            uf.unite(i, j);
        }
    }
    return not uf.same(N, N+1);
}

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    cin >> N;

    vector<P> points(N);
    REP(i, N) cin >> points[i].first >> points[i].second;

    dist.assign(N+2, vector<double>(N+2));
    REP(i, N) {
        REP(j, N){
            dist[i][j] = sqrt((points[i].first - points[j].first) * (points[i].first - points[j].first) + (points[i].second - points[j].second) * (points[i].second - points[j].second));
        }
        dist[i][N] = 100 - points[i].second;
        dist[i][N+1] = points[i].second + 100;
    }
    REP(j, N){
        dist[N][j] = 100 - points[j].second;
        dist[N+1][j] = points[j].second + 100;
    }
    dist[N][N] = 0;
    dist[N+1][N+1] = 0;
    dist[N][N+1] = 200;
    dist[N+1][N] = 200;

    double ok = 0;
    double ng = 101;
    while (ng - ok > 1e-8){
        double mid = (ok + ng) / 2;
        if (valid(mid)){
            ok = mid;
        } else {
            ng = mid;
        }
    }

    print(ok);

    return 0;
}


#include <bits/stdc++.h>

void check_and_add(std::map< long long, long long >& mp, long long num, long long add){
    auto itr = mp.find(num);
    if(itr == mp.end()){
        mp[num] = add;
    }else{
        mp[num] += add;
    }
}

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

    std::vector< std::map< long long, long long > > dp(N);
    dp[N-1][X % A[N-1]] = 1;
    dp[N-1][X % A[N-1] - A[N-1]] = 1;

    for(int i=N-2; i>=0; i--){
        for(auto itr=dp[i+1].begin(); itr!=dp[i+1].end(); itr++){
            if(std::abs(itr->first) >= A[i+1]){
                continue;
            }
            if(itr->first == 0){
                check_and_add(dp[i], 0, itr->second);
            }else if(itr->first > 0){
                long long money = itr->first % A[i];
                check_and_add(dp[i], money, itr->second);

                if(money != 0){
                    money -= A[i];
                    if(itr->first - money < A[i+1]){
                        check_and_add(dp[i], money, itr->second);
                    }
                }
            }else{
                long long money = itr->first % A[i];
                check_and_add(dp[i], money, itr->second);

                if(money != 0){
                    money += A[i];
                    if(-1 * itr->first + money < A[i+1]){
                        check_and_add(dp[i], money, itr->second);
                    }
                }
            }
        }
    }

    std::cout << dp[0][0] << std::endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define int long long

int n;

map<pair<int, int>, int> ans;

int solve(vector<int> & a, int x, int st)
{
    if (ans[{x, st}])
    {
        return ans[{x, st}];
    }
    if (st == n - 1)
    {
        return 1;
    }
    if (x % (a[st + 1] / a[st]) == 0)
    {
        int k = (a[st + 1] / a[st]);
        int res = solve(a, x / k, st + 1);
        ans[{x, st}] = res;
        return res;
    }
    int k = (a[st + 1] / a[st]);
    int res = solve(a, x / k, st + 1) + solve(a, x / k + 1, st + 1);
    ans[{x, st}] = res;
    return res;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); srand(clock());
    int x;
    cin >> n >> x;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }
    cout << solve(a, x, 0) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(v) v.begin(), v.end()
#define rep(i, n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
#define rrep(i, n) for(int i=(n); i>=0; i--)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
    int N; cin >> N;
    vector<ll> A(N);
    rep(i, N){
        cin >> A[i];
    }

    ll res = 0;
    for(int i=0; i<N; i++){
        ll min_ = LINF;
        for(int j=i; j<N; j++){
            chmin(min_, A[j]);
            ll tmp = min_ * (j+1-i); //[i, j) の区間の個数はj+1-i個
            chmax(res, tmp);
        }
    }
    cout << res << ln;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

#define f first
#define s second
#define pb push_back

#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define sz(li) (int) (li).size()
#define sum(a) (accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>

#define endl "\n"

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "[ "; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << " ]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<vector<A>> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) { cout << " "; } cout << v[i]; if (i < v.size() - 1) { cout << endl; } } return cout << "]"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }
template<typename T, typename U> void cmin(T& a, U b){ if (a > b) a = b; }
template<typename T, typename U> void cmax(T& a, U b){ if (a < b) a = b; }

void solve() {
  string s; cin >> s;
  int n = sz(s);
  deque<char> T;

  bool r = false;

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') r = !r;
    else {
      if (r) T.push_front(s[i]);
      else T.push_back(s[i]);
    }
  }

  string t;
  for (int i = 0; i < sz(T); i++) t += T[i];
  if (r) reverse(all(t));

  string res;

  for (char c : t) {
    if (sz(res) && res.back() == c) res.pop_back();
    else res.pb(c);
  }

  cout << res << endl;
}

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

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

  while (t--) {
    solve();
  }

  cerr << "Time: " << 1000.0*clock()/CLOCKS_PER_SEC << " ms\n";
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

bool exi[1 << 18];
void solve() {
	int ans = 0;
	int n; cin >> n;
	vector<int> p(n);
	rep(i, n) {
		cin >> p[i];
	}
	rep(i, n) {
		exi[p[i]] = true;
		while (exi[ans]) {
			ans++;
		}
		cout << ans << "\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(15);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(),_obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
ll n,m,a,b,c,k,temp,x,y;
const int MAXN=1e5+11,mod=1e9+7;
ll max(ll a,ll b) {return ((a>b)?a:b);}
ll min(ll a,ll b) {return ((a>b)?b:a);}
vll read(int n) {vll v(n);for (int i = 0; i < v.size(); i++)cin>>v[i];return v;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
map<int,int> mp[2*MAXN];
int cl[2*MAXN];
class DisjSet {
    int *parent, n,*sze;
public:
    DisjSet(int n)
    {
        parent = new int[n];
        sze=new int [n];
        this->n = n;
        makeSet();
    }
    void makeSet()
    {
        for (int i = 0; i < n; i++)
        {
            parent[i] = i; sze[i]=1;
            mp[i][cl[i]]=1;
        }
    }
    int find(int x)
    {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }
    void Union(int x, int y)
    {
        int xset = find(x);
        int yset = find(y);
        if (xset == yset)
            return;
        if (sze[xset] < sze[yset])
       {
            parent[xset] = yset;
            sze[yset]+=sze[xset];
            for(auto x:mp[xset])
            mp[yset][x.F]+=x.S;
            mp[xset].clear();
        }
        else if (sze[xset] > sze[yset])
       {
            parent[yset] = xset;
            sze[xset]+=sze[yset];
            for(auto x:mp[yset])
            mp[xset][x.F]+=x.S;
            mp[yset].clear();
        }
        else
        {
            parent[yset] = xset;
            sze[xset]+=sze[yset];
            for(auto x:mp[yset])
            mp[xset][x.F]+=x.S;
            mp[yset].clear();
        }
    }
};
void sol(void)
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>cl[i];
DisjSet s(n);
while(m--)
{
    int t;
    cin>>t;
    if(t==1)
    {
        cin>>a>>b;
        a--;
        b--;
        s.Union(a,b);
    }
    else
    {
       cin>>a>>b;
       a--;
       cout<<mp[s.find(a)][b]<<'\n';
    }
}
return ;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int test=1;
    //cin>>test;
    while(test--) sol();
}


#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
//#include <bits/stdc++.h>
# include <iostream>
# include <array>
# include <string>
# include <vector>
# include <algorithm>
# include <functional>
# include <cmath>
# include <iomanip>
# include <stack>
# include <queue>
# include <numeric>
# include <map>
# include <set>
# include <unordered_map>
# include <unordered_set>
# include <chrono>
# include <random>
# include <bitset>
# include <cassert>
# include <climits>

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

#define rep2(i,t,n) for (int i = (t); i < (int)(n); i++)
#define rep(i,n) rep2(i,0,n)
#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

using namespace std;
typedef long long ll;

ll a,b,c,d;

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

  cin >> a >> b >> c >> d;

  if(d*c <= b) {
    cout << -1 << endl;
    return 0;
  }

  ll aka = 0;

  ll count = 0;

  while(a > d*aka) {
    a += b;
    aka += c;
    count++;
  }

  cout << count << endl;

  return 0;
}

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

int dfs(vector<vector<bool>>& used, int a, int b, int r, int c) {
	int h = used.size();
	int w = used[0].size();

	if(r == h) return 1;
	int r2, c2;
	if(c == w-1) r2=r+1, c2=0;
	else r2=r, c2=c+1;

	assert(0<=c && c<w && 0<=r && r<h);

	if(used[r][c]) return dfs(used, a, b, r2, c2);
	int ans = 0;
	if(b > 0) {
		used[r][c] = true;
		ans += dfs(used, a, b-1, r2, c2);
		used[r][c] = false;
	}
	if(a > 0 && c < w-1) {
		if (!used[r][c + 1]){
      used[r][c] = true;
      used[r][c + 1] = true;
      ans += dfs(used, a-1, b, r2, c2);
      used[r][c] = false;
      used[r][c + 1] = false;
    }
  }
  if (a > 0 && r < h-1){
    if (!used[r + 1][c]){
      used[r][c] = true;
      used[r + 1][c] = true;
      ans += dfs(used, a-1, b, r2, c2);
      used[r][c] = false;
      used[r + 1][c] = false;
    }
  }
  return ans;
}

void solve() {
	int h, w, a, b;
	cin >> h >> w >> a >> b;

	vector<vector<bool>> used(h, vector<bool>(w, false));
	cout << dfs(used, a,b,0,0) << '\n';
}

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

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

	return 0;
}


#include<bits/stdc++.h>
#define int long long

using namespace std;

bool cmp(pair<int,int> &a,pair<int,int> &b)
{
    return 2*a.first+a.second>2*b.first+b.second;
}

int32_t main()
{
    int n;
    cin>>n;
    vector< pair<int,int> > v(n);
    int a=0,b=0;
    for(int i=0;i<n;i++)
    {
        cin>>v[i].first >>v[i].second;
        a+=v[i].first;
    }

    sort(v.begin(),v.end(),cmp);
    /*for(auto u: v)
    {
        cout<< u.first <<" "<<u.second<<endl;
    }*/
    int ans = 0;
    for(int i=0;i<n;i++)
    {
        ans++;
        b+=2*v[i].first + v[i].second;
        if(b>a) break;
    }
    cout<<ans<<endl;
    return 0;
}

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

// aoki = sum(not go)ai
// takahashi = sum(go)(ai+bi)
//takahashi>aoki => sum(go)bi+sum(go)(ai)> sum(not go)(ai)
//takahashi>aoki => sum(go)bi + 2sum(go)(ai) > sum(all)(ai)

bool OK( int t, int n, ll asum, vector<ll> &win ){
    ll ans = 0;
    rep(i,t) ans += win[n-1-i];
    return ans>asum;
}

int main(){
    int n;
    cin >> n;
    vector<ll> win(n);
    ll asum = 0;
    rep(i,n){
        ll a,b;
        cin >> a >> b;
        asum += a;
        win[i] = b + 2*a;
    }
    sort(win.begin(),win.end());
    int t1 = 0, t2=n;
    while(t2-t1>1){
        int piv = (t1+t2)/2;
        if( OK(piv,n,asum,win) ) t2=piv;
        else t1=piv;
    }
    cout << t2 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
	string res = "{";
	for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
	return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
int main() {
	int R, C;
	scanf("%d%d", &R, &C);
	vector<vector<int>> A(R, vector<int>(C - 1)), B(R - 1, vector<int>(C));
	for (int r = 0; r < R; ++r) {
		for (int i = 0; i + 1 < C; ++i) scanf("%d", &A[r][i]);
	}
	for (int r = 0; r + 1 < R; ++r) {
		for (int i = 0; i < C; ++i) scanf("%d", &B[r][i]);
	}
	const int INF = 1e9;
	typedef pair<ll, array<int, 3>> Pair;
	priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
	vector<vector<vector<int>>> dist(R, vector<vector<int>>(C, vector<int>(2, INF)));
	dist[0][0][0] = 0;
	pq.push({0, {0, 0, 0}});
	auto enq = [&](int r, int c, int t, int ndist) {
		db(r, c, t, ndist);
		if (ndist < dist[r][c][t]) {
			dist[r][c][t] = ndist;
			pq.push({ndist, {r, c, t}});
		}
	};
	while (!pq.empty()) {
		auto u = pq.top().second;
		int cdist = pq.top().first;
		pq.pop();
		int r = u[0], c = u[1], t = u[2];
		db(u, cdist);
		if (r == R - 1 && c == C - 1) {
			printf("%d\n", cdist);
			return 0;
		}
		if (cdist > dist[r][c][t]) continue;
		if (c + 1 < C) enq(r, c + 1, 0, cdist + A[r][c]);
		if (c > 0) enq(r, c - 1, 0, cdist + A[r][c - 1]);
		if (r + 1 < R) enq(r + 1, c, 0, cdist + B[r][c]);
		enq(r, c, 1, cdist + 1);
		if (t == 1 && r > 0) enq(r - 1, c, t, cdist + 1);
	}
}

// E - 潜入
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;
using PR = pair<int,int>;
#define rep(i,n) for(int i=0;i<(int)(n);++i)

int V; // 頂点数
vec<vec<PR>> G; // [from]<to, cost> 隣接リスト
int INF = 1e9;

vi dijkstra(int s){
	vi dist(V, INF);
	dist[s] = 0;
	priority_queue<PR, vec<PR>, greater<PR>> pq;
	pq.push({0, s});
	while(pq.size()){
		auto[from_d, from_v] = pq.top(); pq.pop();
		if(dist[from_v] < from_d) continue;
		for(auto[to_u, to_d]:G[from_v]){
			to_d += dist[from_v];
			if(dist[to_u] <= to_d) continue;
			dist[to_u] = to_d;
			pq.push({to_d, to_u});
		}
	}
	return dist;
}

int main(){
	int R, C; cin>>R>>C;

	auto rc2id = [&](int r, int c){ return r*C + c; };

	V = R*C*2;
	G.resize(V);

	int x, a, b;
	rep(r, R) rep(c, C-1){
		cin>>x;
		a = rc2id(r, c), b = rc2id(r, c+1);
		G[a].emplace_back(b, x), G[b].emplace_back(a, x);
	}

	rep(r, R-1) rep(c, C){
		cin>>x;
		G[rc2id(r, c)].emplace_back(rc2id(r+1, c), x);
	}

	rep(r, R) rep(c, C){
		a = rc2id(r, c), b = rc2id(r, c+R*C);
		G[a].emplace_back(b, 1), G[b].emplace_back(a, 0);
		if(r != R-1) G[rc2id(r+1, c+R*C)].emplace_back(rc2id(r, c+R*C), 1);
	}

	cout<< dijkstra(0)[rc2id(R-1,C-1)] <<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	if(a==b+c+d)return puts("Yes"),0;
	if(b==a+c+d)return puts("Yes"),0;
	if(c==b+a+d)return puts("Yes"),0;
	if(d==b+c+a)return puts("Yes"),0;
	if(b+a==c+d)return puts("Yes"),0;
	if(a+c==b+d)return puts("Yes"),0;
	if(b+c==a+d)return puts("Yes"),0;
	puts("No");
	return 0;
}

#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define RI register int
typedef long long LL;
#define int LL

#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);

using namespace std;

namespace IO {
  char buf[1000000], *p1 = buf, *p2 = buf;
  inline char gc() {
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
    return p1 == p2 ? EOF : *(p1++);
  }
  template <class T> inline void read(T &n) {
    n = 0; RI ch = gc(), f;
    while ((ch < '0' || ch > '9') && ch != '-') ch = gc();
    f = (ch == '-' ? ch = gc(), -1 : 1);
    while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc();
    n *= f;
  }
  char Of[105], *O1 = Of, *O2 = Of;
  template <class T> inline void print(T n, char ch = '\n') {
    if (n < 0) putchar('-'), n = -n;
    if (n == 0) putchar('0');
    while (n) *(O1++) = (n % 10) ^ 48, n /= 10;
    while (O1 != O2) putchar(*(--O1));
    putchar(ch);
  }
}

using IO :: read;
using IO :: print;

int const MAXN = 1e5 + 15;
int a[MAXN], b[MAXN], c[MAXN];

int MoveL(int L, int R) {
  int now = R, last = R, re = 0;
  for (RI i = R; i > L; --i) {
    while (now >= L && a[now] + (i - now) > b[i]) --now;
    if (now < L || a[now] + (i - now) != b[i])
      puts("-1"), exit(0);
    if (now != last)
      re += i - now, last = now;
  }
  return re;
}
int MoveR(int L, int R) {
  int now = L, last = L, re = 0;
  for (RI i = L; i < R; ++i) {
    while (now <= R && a[now] - (now - i) < b[i]) ++now;
    if (now > R || a[now] - (now - i) != b[i])
      puts("-1"), exit(0);
    if (now != last)
      re += now - i, last = now;
  }
  return re;
}

signed main() {

#ifdef LOCAL
  FILEIO("a");
#endif

  int n, L; read(n), read(L); n += 2;
  for (RI i = 2; i < n; ++i) read(a[i]);
  for (RI i = 2; i < n; ++i) read(b[i]);
  a[1] = b[1] = 0, a[n] = b[n] = L + 1;
  for (RI i = 1; i <= n; ++i)
    if (a[i] > b[i])
      c[i] = 0;
    else if (a[i] < b[i])
      c[i] = 1;
    else
      c[i] = 2;
  for (RI i = 1; i < n; ++i)
    if (c[i] == 1 && c[i + 1] == 0)
      return puts("-1"), 0;
  int ans = 0;
  for (RI i = 1; i <= n; ++i) {
    if (c[i] == 2) continue;
    if (c[i] == 0) {
      int L = i, R = i;
      while (i < n && c[i + 1] == 0) ++i, ++R;
      ans += MoveL(L - 1, R);
    }
    if (c[i] == 1) {
      int L = i, R = i;
      while (i < n && c[i + 1] == 1) ++i, ++R;
      ans += MoveR(L, R + 1);
    }
  }
  print(ans);

  return 0;
}

// created by Daniel yuan
/*
     ________
    /        \
   / /      \ \
  / /        \ \
  \            /
   \  ______  /
    \________/
*/


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

typedef long long ll;

ll n, m;
bool v[1005][1005];
ll cp[2005];
bool v1[2005], v2[2005];
ll cnt1, cnt2;

ll fnd(ll p)
{
    if(cp[p] == p) return p;
    return cp[p] = fnd(cp[p]);
}

void uni(ll p, ll q)
{
    if(fnd(p) == fnd(q)) return;
    cp[fnd(p)] = fnd(q);
}

int main()
{
    scanf("%lld%lld", &n, &m);
    for(ll i = 0; i < n + m; i++) cp[i] = i;
    for(ll i = 0; i < n; i++)
    {
        string s;
        cin>>s;
        for(ll j = 0; j < m; j++)
        {
            if(s[j] == '#') v[i][j] = true, uni(i, j + n);
        }
    }
    uni(0, n - 1);
    uni(0, n);
    uni(0, m + n - 1);
    for(ll i = 0; i < n; i++)
    {
        if(!v1[fnd(i)]) cnt1++, v1[fnd(i)] = true;
    }
    for(ll i = 0; i < m; i++)
    {
        if(!v2[fnd(i + n)]) cnt2++, v2[fnd(i + n)] = true;
    }
    printf("%lld\n", min(cnt1 - 1, cnt2 - 1));
    return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

int n,m,ans=0;
string s[1010];
bool visr[1010],visc[1010],vis[1010][1010],exbr[1010],exbc[1010];
vector <int> gr_c[1010],gc_r[1010];
vector <pair <vector <int>,vector <int> > > v;
pair <vector <int>,vector <int> > tmp;
queue <pii> q;

void bfs(int x,int y)
{
  q.push(mpr(x,y));vis[x][y]=visr[x]=visc[y]=true;
  while(!q.empty())
  {
    pii f=q.front();q.pop();
    if(!exbr[f.fi])
    rep(i,gr_c[f.fi].size())
    {
      if(vis[f.fi][gr_c[f.fi][i]]) continue;
      q.push(mpr(f.fi,gr_c[f.fi][i]));
      vis[f.fi][gr_c[f.fi][i]]=visc[gr_c[f.fi][i]]=true;
    }
    exbr[f.fi]=true;
    if(!exbc[f.se])
    rep(i,gc_r[f.se].size())
    {
      if(vis[gc_r[f.se][i]][f.se]) continue;
      q.push(mpr(gc_r[f.se][i],f.se));
      vis[gc_r[f.se][i]][f.se]=visr[gc_r[f.se][i]]=true;
      //cout<<gc_r[f.se][i]<<'h'<<endl;
    }
    exbc[f.se]=true;
  }
}

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

  cin>>n>>m;
  rep(i,n) cin>>s[i];
  rep(i,n) rep(j,m)
  {
    if(s[i][j]=='.') continue;
    gr_c[i].pb(j);gc_r[j].pb(i);
  }
  rep(i,n) gr_c[i].pb(0),gr_c[i].pb(m-1);
  rep(i,m) gc_r[i].pb(0),gc_r[i].pb(n-1);

  bfs(0,0);
  rep(i,n) rep(j,m) if(s[i][j]=='#'&& !vis[i][j]) bfs(i,j),++ans;
  LL res1=0,res2=0;
  rep(i,n) if(!visr[i]) ++res1;rep(i,m) if(!visc[i]) ++res2;
  ans+=min(res1,res2);
  cout<<ans<<endl;

	return 0;
}


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

int n;
vector<long long> a;

int main() {
  cin >> n;
  a.resize(n);
  long long sum = 0, num = 0, res = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    num = max(a[i], num);
    res += a[i];
    res += sum;
    sum += a[i];
    cout << num * (i + 1) + res << endl;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define For(i,a,b) for(reg int i=a;i<=b;++i)
#define Down(i,a,b) for(reg int i=a;i>=b;--i)
#define ull unsigned long long
#define ll long long
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline int gcd(int n,int m){return m?gcd(m,n%m):n;}
inline int lcm(int x,int y){return x/gcd(x,y)*y;}
inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
namespace yspm{
	inline int read(){
	    int res=0,f=1; char k;
	    while(!isdigit(k=getchar())) if(k=='-') f=-1;
	    while(isdigit(k)) res=res*10+k-'0',k=getchar();
	    return res*f;
    }
    const int N=2e5+10;
    int a[N],sum[N],n;

	signed main(){
	    n=read(); For(i,1,n) sum[i]=sum[i-1]+(a[i]=read());
	    int Sum=0,mx=0;
	    for(reg int i=1;i<=n;++i){
	        mx=max(mx,a[i]); Sum+=sum[i];
	        printf("%lld\n",mx*i+Sum);
	    }
		return 0;
    }
}signed main(){return yspm::main();}
//Use The Time To Enrich This Selfclosing Youth


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

#pragma GCC optimize ("O3")
#pragma GCC target ("avx,avx2,fma")

#ifdef LOCAL
    #define D(a) cerr << #a << " = " << a << endl
#else
    #define D(a) 8
#endif
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define dforsn(i,s,n) for(int i=int(n-1);i>=int(s);i--)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define all(a) (a).begin(),(a).end()
#define dforn(i,n) dforsn(i,0,n)
#define forn(i,n) forsn(i,0,n)
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
using pii = pair<long long,long long>;
using vi = vector<int>;
using ll = long long;

pii puntos[200000];

struct momento {
  ll a,b,dx,dy;
  bool haySwap;
};

momento resultados[200000+1];

int main() {
    fastio;

    resultados[0].a = 1;
    resultados[0].b = 1;
    resultados[0].dx = 0;
    resultados[0].dy = 0;
    resultados[0].haySwap = false;

    ll n,m,q;

    cin >> n;
    forn(i,n) {
		cin >> puntos[i].fst;
		cin >> puntos[i].snd;
	}

	cin >> m;

	ll numop,p;

	forn(i,m) {
		cin >> numop;

		resultados[i+1] = resultados[i];

		switch(numop){
			case 1:
				swap(resultados[i+1].a,resultados[i+1].b);
				swap(resultados[i+1].dx,resultados[i+1].dy);
				resultados[i+1].b *= -1;
				resultados[i+1].dy *= -1;
				resultados[i+1].haySwap = !resultados[i+1].haySwap;
				break;
			case 2:
				swap(resultados[i+1].a,resultados[i+1].b);
				swap(resultados[i+1].dx,resultados[i+1].dy);
				resultados[i+1].a *= -1;
				resultados[i+1].dx *= -1;
				resultados[i+1].haySwap = !resultados[i+1].haySwap;
				break;
			case 3:
				cin >> p;
				resultados[i+1].dx *= -1;
				resultados[i+1].a *= -1;
				resultados[i+1].dx += 2*p;
				break;
			case 4:
				cin >> p;
				resultados[i+1].dy *= -1;
				resultados[i+1].b *= -1;
				resultados[i+1].dy += 2*p;
				break;
		}

	}

	cin >> q;
	ll A,B;
	forn(i,q){
		cin >> A >> B;
		B--;
		if(!resultados[A].haySwap)
			cout << puntos[B].fst * resultados[A].a + resultados[A].dx << " " << puntos[B].snd * resultados[A].b + resultados[A].dy << endl;
		else
			cout << puntos[B].snd * resultados[A].a + resultados[A].dx << " " << puntos[B].fst * resultados[A].b + resultados[A].dy << endl;

	}

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) {
		printf("%d %d\n", 1 + 2 * i % n, 1 + (2 * i + 1) % n);
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fast                         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int                          long long int
#define double                       long double
#define ff                           first
#define ss                           second
#define pb                           push_back
#define eb                           emplace_back
#define all(x)                       (x).begin(), (x).end()
#define sz(x)                        ((int)(x).size())
#define len(x)                       ((int)(x).length())
#define pii                          pair<int,int>
#define vi                            vector<int>
#define vd                           vector<double>
#define vs                            vector<string>
#define mp                          map<int,int>
#define ms(x,y)                     memset(x,y,sizeof(x))
#define ump                         unordered_map<int,int>

#define lb                            lower_bound
#define ub                            upper_bound
#define nx                            cout<<"\n";
#define inf                           9223372036854775807
#define PI                            3.14159265

const int MOD = 1e9 + 7;

inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline int add(int a, int b) {    return ((a % MOD) + (b % MOD)) % MOD;}

inline int multi(int a, int b) {   return ((a % MOD) * (b % MOD)) % MOD;}

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

inline int power(int a, int b) {       a %= MOD; int res = 1; while (b > 0) {if (b & 1) {res = multi(res, a);} a = multi(a, a); b >>= 1;}  return res;}

bool isPrime(int  x) {      if (x <= 4 || x % 2 == 0 || x % 3 == 0) {return x == 2 || x == 3;} for (int i = 5; i * i <= x; i += 6) {if (x % i == 0 || x % (i + 2) == 0) { return 0;}} return 1;}

int32_t main()
{
	fast ;

	int n; cin>>n;
	int arr[n];
	int sum = 0;
	for(int i=0;i<n;i++){
		int a,b; cin>>a>>b;
		arr[i] = 2*a + b;
		sum+=a;
	}
	sort(arr,arr+n);
	int cnt = 0;
	int i = n-1;
	while(sum>=0){
		sum-=arr[i];
		i--;
		cnt++;
	}
	cout<<cnt<<endl;

}

// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
LL a[101010], aCum[101010];

int main(){

    // 1. 入力情報.
    int N;
    scanf("%d", &N);
    rep(i, N){
        scanf("%lld", &a[i]);
        a[i] *= 2;
    }

    // 2. sort.
    sort(a, a + N);

    // 3. 累積和.
    rep(i, N) aCum[i + 1] = aCum[i] + a[i];

    // 4. 期待値の最小化を計算.
    LL t = 202020202020202020;
    rep(i, N){
        LL x = a[i] / 2;
        LL l = N * x + aCum[N] - (N - i) * a[i] - aCum[i];
        t = min(t, l);
    }

    // 5. 出力.
    double ans = (double)t / (double)(N * 2.0);
    printf("%.12lf\n", ans);
    return 0;

}


//#undef DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll llinf = (1ll<<61)-1;
#define sz(a) int(a.size())
#define all(x) begin(x), end(x)
#ifdef DEBUG
const int DEBUG_END = 26;
#define DOS cout
#include <debug.h>
#else
#define bug(args...) void()
#define cbug(a, args...)
#endif
#define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b));
#define fi first
#define se second
const int inf = 1000000007;
int TC = 1, CN;

signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11), cout.setf(ios::fixed);
    //cin >> TC;// inputAll + resetAll -> solution
    auto kase = [&]()->void {
        string s;
        cin >> s;
        int o = 0, cnt[3] = {0};
        for (auto &i : s) {
            int x = (i-'0')%3;
            ++cnt[x];
            o += x;
        }
        o %= 3;
        cout << (o==0 ? 0 : (cnt[o] and sz(s)>1 ? 1 : (cnt[o^1^2] >= 2 and sz(s)>2 ? 2 : -1))) << '\n';
    };
    while (CN++!=TC) kase();
}

#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
using namespace std;
#define int long long
const int mod=1e9+7;
signed main(){
	int t,n,p[500];
	cin>>t;
	while(cin>>n){
		for(int i=0;i<n;i++){
			cin>>p[i];
			p[i]--;
		}
		if(n==2){
			if(p[0]==0)
				cout<<0<<endl;
			else
				cout<<1<<endl<<1<<endl;
			continue;
		}
		bool e=1;
		vector<int>ans;
		for(int i=n-1;i>=5;i--){
			int pos=-1;
			for(int j=0;j<n;j++){
				if(p[j]==i){
					pos=j;
					break;
				}
			}
			if(pos<i){
				if(pos%2==0&&!e){
					if(pos!=2){
						swap(p[1],p[2]);
						ans.push_back(1);
						e=!e;
					}else{
						swap(p[3],p[4]);
						ans.push_back(3);
						e=!e;
					}
				}else if(pos%2==1&&e){
					if(pos!=1){
						swap(p[0],p[1]);
						ans.push_back(0);
						e=!e;
					}else{
						swap(p[2],p[3]);
						ans.push_back(2);
						e=!e;
					}
				}
				for(int j=pos;j<i;j++){
					swap(p[j],p[j+1]);
					ans.push_back(j);
					e=!e;
				}
			}
		}
		if(n>=5){
			while(!(p[2]==4||p[3]==4||p[4]==4)){
				if(e){
					swap(p[0],p[1]);
					ans.push_back(0);
					e=!e;
				}else{
					swap(p[1],p[2]);
					ans.push_back(1);
					e=!e;
				}
			}
			while(!(p[4]==4)){
				if(e){
					swap(p[2],p[3]);
					ans.push_back(2);
					e=!e;
				}else{
					swap(p[3],p[4]);
					ans.push_back(3);
					e=!e;
				}
			}
		}
		if(n>=4){
			while(!(p[1]==3||p[2]==3||p[3]==3)){
				if(e){
					swap(p[0],p[1]);
					ans.push_back(0);
					e=!e;
				}else{
					swap(p[1],p[2]);
					ans.push_back(1);
					e=!e;
				}
			}
			while(!(p[3]==3)){
				if(e){
					swap(p[2],p[3]);
					ans.push_back(2);
					e=!e;
				}else{
					swap(p[1],p[2]);
					ans.push_back(1);
					e=!e;
				}
			}
		}
		if(n>=3){
			while(!(p[0]==0&&p[1]==1&&p[2]==2)){
				if(e){
					swap(p[0],p[1]);
					ans.push_back(0);
					e=!e;
				}else{
					swap(p[1],p[2]);
					ans.push_back(1);
					e=!e;
				}
			}
		}
		cout<<ans.size()<<endl;
		for(int i=0;i<ans.size();i++)
			cout<<ans[i]+1<<(i+1==ans.size()?'\n':' ');
	}
}


/***

⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁

   * Author : SUARABH UPADHAYAY
   * E-mail : officialsaurabhupadhayay@gmail.com

   PRACTICE LIKE YOU NEVER WIN, PLAY LIKE YOU NEVER LOOSE

***/

#include<bits/stdc++.h>
//#include <boost/math/common_factor.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace std;
#define lll cpp_int
#define ull unsigned long long
#define ll long long
#define ui unsigned int
#define ldb long double
//#define lcm(o,g) boost::math::lcm(o,g)
#define mod 1000000007
#define mod1 1000003
#define mod2 998244353
#define pi acos(-1)
#define full LLONG_MAX
#define V vector
#define VL vector<ll>
#define LL list<ll>
#define DL deque<ll>
#define PQL priority_queue<ll>
#define SL set<ll>
#define SC set<char>
#define M map
#define MLL map<ll,ll>
#define SS stringstream
#define mkp make_pair
#define mkt make_tuple
#define er equal_range
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define np next_permutation
#define ln length()
#define em empty()
#define DO greater<ll>()
#define DM greater<ll>
#define pb push_back
#define in insert
#define pob pop_back()
#define bg begin()
#define fr front()
#define ed end()
#define bk back()
#define sz size()
#define all(o) ((o).bg,(o).ed)
#define alld(o) ((o).bg,(o).ed,DO)
#define F first
#define S second
#define stf shrink_to_fit()
#define ig cin.ignore(1,'\n');
#define cg(g) getline(cin,(g))
#define f(g,h,o) for(ll g=h;g<o;g++)
#define f1(g,h,o) for(ll g=h;g>o;g--)
#define f2(g) for(auto E : (g))
#define fm(g) for(auto [X,Y] : (g))
#define AI(g,o) VL g(o); f(i,0,o)cin>>g[i]
#define AO(g,o) f(i,0,o)cout<<g[i]<<" "
#define T ui t; cin>>t; while(t--)
#define T1(g) ui g; cin>>g; while(g--)
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

int main(){

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

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

    T{
    	ll l,r;
    	cin>>l>>r;
    	if(l>r-l)
    		cout<<"0\n";
    	else
    		cout<<((r-(2*(l-1)))*((r-(2*l))+1))/2<<"\n";
    }
}

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

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

int main(){
    int T; std::cin >> T;
    {
        long long L, R, D;
        for(int tt(0); tt < T; ++tt){
            std::cin >> L >> R;
            D = R - 2*L + 1;
            if(L+L > R) D = 0;
            std::cout << D * (D+1) / 2 << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
int main(){
    int h,w,ans=0;
    cin >> h >> w;
    vector<string>s(h);
    rep(i,h)cin >> s[i];

    rep(i,h){
        rep(j,w){
            if(j!=w-1 and s[i][j]=='.' and s[i][j+1]=='.')ans++;
            if(i!=h-1 and s[i][j]=='.' and s[i+1][j]=='.')ans++;
        }
    }

    cout << ans;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(all(container), element) != container.end())
#define INF 0x3f3f3f3f
#define f1(i, n) for (int i = 0; i < n; ++i)
#define f2(i, n) for (int i = 1; i < n; ++i)
#define f3(i, start, end) for (int i = start; i < end; ++i)
#define ll long long
typedef pair<int, int> iPair;
const double epsilon = 1e-7;
ll n, m;
vector<iPair> v;
set<int> s, a, b;
// s: cols reachable currently
// a: cols rechable now onwards
// b: cols unreachable now onwards

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  f1 (i, m) {
    cin >> x >> y;
    v.emplace_back(x, y);
  }
  sort(begin(v), end(v));
  s.insert(n);
  f1 (i, m) {
    a.clear(); b.clear();
    int l = i;
    int row = v[i].first;
    while (v[i].first == row) i++;
    i--;
    int r = i;
    for (; l <= r; l++) {
      int col = v[l].second;
      // can reach this col ?
      if (s.count(col+1) or s.count(col-1))
        a.insert(col);
      else b.insert(col);
    }
    for (auto x: b) s.erase(x);
    for (auto x: a) s.insert(x);
  }
  cout << s.size() << endl;
  return 0;
}


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

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

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

const ll mod = 1e9 + 7;
const ll N = 1e5 + 8;

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

    ll a, b, c;
    cin >> a >> b >> c;
    cout << a + b + c - min({a, b, c});
    return 0;
}

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

int main(void){
    int n,s,d;
    bool ok = false;
    cin >> n >> s >> d;
    vector<int> x(n),y(n);

    rep(i,n) cin >> x[i] >> y[i];

    rep(i,n){
        if(x[i] < s && y[i] > d) ok = true;
    }

    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}


//#include <atcoder/maxflow.hpp>
#include <memory>
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <thread>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;
//using namespace atcoder;

ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//ll modd = 998244353;

long long gccd(long long a, long long b) {
    if (a == 0)  {   return b;  }
    return gccd(b % a, a);
};

ll gcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) {
    g = gcd(abs(a), abs(b), x0, y0);
    if (c % g) {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;
    return true;
}

int main()   {

    ios_base::sync_with_stdio(false);

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

    auto start = chrono::steady_clock::now();

    readint(test_cases);
//    int test_cases = 1;
    forr(t, 1, test_cases) {
        readll(n); readll(s); readll(k);

        ll gg = gccd(n,k);
        if (s % gg != 0) {
            cout << -1 << endl;
            continue;
        }
        ll nn = n/gg, kk = k/gg;

        ll dummy;
        ll x,y;

        find_any_solution(kk,nn,-s/gg,x,y,dummy);

        // (nn,-kk)

        if (x>0) {
            x %= nn;
        } else {
            x %= nn;
            if (x<0) {x+=nn;}
            x %= nn;
        }
        cout << x << endl;

    }
//        auto stop = chrono::steady_clock::now();
//        auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
//        cout << "Duration: " << duration.count() << endl;
    return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e6 + 50;

int k;
ll ans;
bool vis[105], is[10];

int qpow(int x, int k){
	int ans = 1;
	while(k){
		if(k & 1)  ans = 1LL * ans * x % 4;
		x = 1LL * x * x % 4,  k >>= 1;
	}
	return ans;
}
int main(){
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    b = qpow(b, c);
    a %= 10;
    if(!b)  b = 4;
    for(int i = 1, da = a; i < b; ++i)  a = a * da % 10;
    printf("%d", a);
	/*b %= 100;
    int db = b;
	b = 1;
    int cnt = 0;
    bool ok = 0;
    for(int i = 1; i <= c; ++i){
        b = b * db % 100;
        if(vis[b]){
            ok = 1;
            break;
        }
        vis[b] = 1;
        ++cnt;
    }
    a %= 10;
    int tt = 0, m = 1;
    for(int i = 1; i <= 10; ++i){
    	m = m * a % 10;
		if(is[m])  break;
		is[m] = 1;
		++tt;
	}
    if(!ok){
        int da = 1;
        b %= tt;
        if(!b)  b = tt;
        for(int i = 1; i <= b; ++i)  da = da * a % 10;
        printf("%d", da);
    }
    else{
        c %= cnt;
        if(c == 0)  c = cnt;
        b = 1;
        for(int i = 1; i <= c; ++i)  b = b * db % 100;
        int da = 1;
        printf("%d\n", cnt);
        b %= tt;
        if(!b)  b = tt;
        for(int i = 1; i <= b; ++i)  da = da * a % 10;
        printf("%d", da);
    }*/
}


//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define uni(x) sort(all(x)),x.resize(unique(all(x))-x.begin());
#define GETPOS(c,x) (lower_bound(all(c),x)-c.begin())
#define lown1(x,val) low(in(x),val)-x
#define lowm1(x,val) low(im(x),val)-x
#define low1(x,nums,val) low(x+1,x+nums+1,val)-x
#define mst(x,val) memset((x),val,sizeof((x)))
#define ls rt<<1
#define rs rt<<1|1
#define lson rt<<1,l,M
#define rson rt<<1|1,M+1,r
#define PI acos(-1)
#define MM int M=(l+r)>>1;
#define fu(i,r,t) for(int i=r;i<=t;i++)
#define fd(i,r,t) for(int i=r;i>=t;i--)
#define fh(i,be,e) for(int i=head[be];~i;i=e[i].next)
#define fa(i,V) for(auto i:V)
#define far(i,V) for(auto &i:V)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b))/__gcd(a,b)
#define cp(i,ans) printf("%.if",ans);
#define cpp(i,ans) cout<<setprecision(i)<<fixed<<ans<<endl;
#define ppb pop_back
#define ppf pop_front
#define pb push_back
#define pf push_front
#define pq priority_queue
#define lowbit(x) ((x)&(-x))
#define all(V) V.begin(),V.end()
#define ms multiset
#define mod(x) (((x)<0)?(x)%mo_num+mo_num:(x)%mo_num)
#define vc vector
#define vct vector<int>
#define SET set<int>
#define dq deque<int>
#define out(i) cout<<(i)<<endl;
#define fi first
#define se second
#define fun(i) fu(i,1,n)
#define fut(i) fu(i,1,t)
#define fum(i) fu(i,1,m)
#define ld long double
#define umap unordered_map
#define Umap unordered_map<int,int>
#define P pair<int,int>
#define mk make_tuple
#define eps 1e-6
//Remember cancel"#define endl '\n'" in interactive questions or use "<<flush"
#define endl '\n'
#define low lower_bound
#define upp upper_bound
#define yn(key) out(key?"YES":"NO")
//#define yn(key) out(key?"Yes":"No")
#define in(i) i+1,i+1+n
#define im(i) i+1,i+1+m
#define ik(i,k) i+1,i+1+k
#define bffs(i) __builtin_ffs(i)
#define bcount(i) __builtin_popcount(i)
#define bone(i) ((1<<i)-1)
#define db double
#define ll long long
#define got(container,num) get<num-1>(container)
#define int long long
#define print(a,n) fun(i)cout<<a[i]<<(i!=n?' ':endl);
#define outcase(x) cout<<"Case #"<<(++case_of_T)<<": "<<(x)<<endl;
#define ptcase(x) printf("Case #%d: %d\n",++case_of_T,x);
#define plcase(x) printf("Case #%lld: %lld\n",++case_of_T,x);
using namespace std;
//Remember to cancel the line below and declare INT=INT_MAX/2; when you want to change long to int
const int INF=LLONG_MAX/4,SINF=0x3f3f3f3f,Lim=1<<20,MINF=LLONG_MAX;
//const int INF=INT_MAX/4,SINF=0x3f;
// use C:printf("%.16f", x);  ->  printf("%.10f", x); can accelerate the program
const int dx[]={0,0,-1,1},dy[]={-1,1,0,0};//down up left right
const int maxn=1e6+1e5;
int mo_num=1e9+7;
//const int mo_num=998244353;
int n,m,t,a[maxn],b[maxn],ans,case_of_T;
const int N=105;
int f[N][N];
void solve()
{
    cin>>n;
    mst(f,SINF);
    fun(i)
    {
        string k;cin>>k;
        f[i][i]=0;
        fun(j)
        {
            if(k[j-1]=='1')f[i][j]=1;
        }
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
            }
        }
    }
    fun(i)fun(j)a[i]+=f[j][i]>100000?0:1;
    db Ans=0;
    fun(i)Ans+=(db)1/(db)a[i];
    cpp(11,Ans);
    return ;
}
main()
{
    IOS
    int T=1;
    //cin>>T;
    while(T--)solve();
    return 0;
}

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n; cin >> n;
    vector<string> s(n);
    vector<vector<int>> vis(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        for (int j = 0; j < n; j++) {
            if (s[i][j] == '1') {
                vis[i][j] = 1;
            }
        }
        vis[i][i] = 1;
    }
    double ans = 0;
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (vis[i][k] && vis[k][j]) {
                    vis[i][j] = 1;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        int num = 0;
        for (int j = 0; j < n; j++)
            num += vis[j][i];
        ans += 1.0 / num;
    }
    cout << fixed << setprecision(10) << ans;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
using Edge = pair<int, ll>;
const ll INF = 1LL << 60;
const int MAX = 100000;
const int MOD = 1000000007;

int main()
{
    // cin高速化
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    unordered_set<int> a;
    REP(i, n)
    {
        int tmp;
        cin >> tmp;
        a.insert(tmp);
    }
    REP(i, n)
    {
        // 見つからなかった
        if (a.find(i + 1) == a.end())
        {
            cout << "No"
                 << "\n";
            return 0;
        }
    }
    cout << "Yes"
         << "\n";
    return 0;
}

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define N 200100
#define MOD 1000000007 //998244353
#define ll long long
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>

int main() {
	ll n;
	vector<ll>a;
	ll x;
	ll s = 0;
	cin >> n;
	rep(i, n) {
		cin >> x;
		a.pb(x);
	}
	sort(all(a));
	rep(i, n) {
		x = i - (n - i - 1);
		s += (a[i] * x);
	}
	cout << s << endl;
	return 0;
}


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

int main()
{
    ll i,j,k,n,m,a,b;
    string st;

    cin>>st;
    n=st.size();
    if(n==1){
        cout<<0<<endl;
        return 0;
    }
    string st1="",st2="";
    if(n%2==1){
        for(i=0;i<n/2;i++){
            cout<<'9';
        }
    }
    else{
        for(i=0;i<n/2;i++){
            st1+=st[i];
        }
        for(i=n/2;i<n;i++){
            st2+=st[i];
        }

        //stringstream sstr(st1);
        //stringstream sstr(st2);
        ll x=stoll(st1);
        ll y=stoll(st2);

        if(x<=y){
            cout<<x<<endl;
        }
        else{
                x--;
            cout<<x<<endl;
        }

    }

}

#include <cstdio>
#include <cmath>
using namespace std;

typedef long long LL;

inline bool check(const LL& x, const LL& n)
{
	LL p = 1LL;
	while(p <= x) p *= 10LL;
	return x * p + x <= n;
}

int main()
{
	LL n;
	scanf("%lld", &n);
	LL l = 0LL, r = sqrt(n);
	while(l < r)
	{
		LL mid = l + r + 1LL >> 1LL;
		if(check(mid, n)) l = mid;
		else r = mid - 1;
	}
	printf("%lld\n", l);
	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using PLL = pair<pair<long long, long long>, long long>;
using Pxy = pair<long double, long double>;
using Tiib = tuple<int, int, bool>;
using vvl = vector<vector<ll>>;
const int INF = 2001001007;
const int modd = 1000000007;
const long long modl = 1000000007LL;
const long long mod = 998244353LL;
const ll inf = 1e18;
template <typename AT>
void priv(vector<AT> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void privv(vector<vector<Q>> &ar){
    rep(i,ar.size()){
        rep(j,ar[i].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[i].size()-1] << endl;
    }
}
template <typename S>
bool range(S a, S b, S x){return (a <= x && x < b);}
template <typename SS>
void sor(vector<SS> &ar){sort(ar.begin(),ar.end());}
template <typename SSS>
void rev(vector<SSS> &ar){reverse(ar.begin(),ar.end());}

void yes(){cout << "Yes" << endl;}
void no (){cout << "No" << endl;}
void yn (bool t){if(t)yes();else no();}

ll cel (ll a, ll b){
    if (a % b == 0) return a / b;
    else return a / b + 1;
}

ll gcds(ll a, ll b){
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}

ll h(ll a, ll b){
    a *= -1LL;
    ll ans = b / a;
    ans = -a * ans + b;
    if (ans < 0) ans += a;
    return ans;
}

int main(){
    int t; cin >> t;
    vector<ll> ans(t);vector<ll> d(t);
    rep(i,t){
        ll n, s, k; cin >> n >> s >> k;
        if (s % gcds(n,k) == 0LL){
            ll x, y;
            ll dd = extGCD(n,k,x,y); d[i] = dd;
            ll a = s / gcds(n,k);
            x *= a; y *= a;
            ans[i] = h(-n/dd,-y);
        }
        else ans[i] = -1;
    }
    rep(i,t) cout << ans[i] << endl;
}


#include <bits/stdc++.h>

#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define FILL0(x) memset(x,0,sizeof(x))
#define FILL1(x) memset(x,-1,sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

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; }

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

    ll b, c;
    cin >> b >> c;

if(b==0 && c==1){
    cout << 1 << endl;
    return 0;
}

    ll ans = 1;

    if(b>=0){
        ans += max(0LL,min(2*b-1,c/2));
        ll tmp = max(0LL,min(2*b-1,c/2));

        c-=1;
        if(c>-1)    ans++;
        ans += max(0LL,c/2);

        if(tmp+c/2>2*b-1){
            ans+=max(0LL,2*b-1-tmp);
        }else{
            ans+=max(0LL,c/2);
        }

        c-=1;
        ans += max(0LL,c/2);
    }
    else{
        b = abs(b);
        ans += max(0LL,c/2); //-inf

        c-=1;
        if(c>-1)    ans++;
        ans += max(0LL,min(2*b-1,c/2));
        ll tmp = max(0LL,min(2*b-1,c/2));

        c-=1;
        if(tmp+c/2>2*b-1){
            ans+=max(0LL,2*b-1-tmp);
        }else{
            ans+=max(0LL,c/2);
        }
        ans+= max(0LL,c/2); //+inf

    }

    cout << ans << endl;

    return 0;

}

#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>

#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }
template<class T, class U>
T &chmin(T &a, const U &b){ if(a > b) a = b; return a; }
int main(){
  int h,w;
  cin >> h >> w;
  vector<vector<char>> a(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> a[i][j];
  vvi dp(h, vi(w));
  rep(j, w - 1){
    if((h - 1 + j) & 1) dp[h - 1][j] = mod;
    else dp[h - 1][j] = -mod;
  }
  per(i, h) per(j, w){
    if(i > 0){
      //takahashi
      if((i + j) & 1){
        dp[i - 1][j] = dp[i][j] + (a[i][j] == '+' ? 1 : -1);
      }else{
        dp[i - 1][j] = dp[i][j] - (a[i][j] == '+' ? 1 : -1);
      }
    }
    if(j > 0){
      //takahashi
      if((i + j) & 1){
        chmax(dp[i][j - 1], dp[i][j] + (a[i][j] == '+' ? 1 : -1));
      }else{
        chmin(dp[i][j - 1], dp[i][j] - (a[i][j] == '+' ? 1 : -1));
      }
    }
  }
  if(dp[0][0] == 0) cout << "Draw\n";
  else if(dp[0][0] > 0) cout << "Takahashi\n";
  else cout << "Aoki\n";
}


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

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template <typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}

typedef tuple<int, int, int, int> node;

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

    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m-1)), b(n-1, vector<int>(m));
    for (int i=0; i<n; i++)
        for (int j=0; j<m-1; j++)
            cin >> a[i][j];
    for (int i=0; i<n-1; i++)
        for (int j=0; j<m; j++)
            cin >> b[i][j];

    vector<vector<vector<int>>> dist(n, vector<vector<int>>(m, vector<int>(2, 1e9)));
    priority_queue<node, vector<node>, greater<node>> pq;

    auto relax = [&] (int d, int r, int c, int f) -> void {
        if (d < dist[r][c][f])
            pq.emplace(dist[r][c][f] = d, r, c, f);
    };

    relax(0, 0, 0, 0);
    while (!pq.empty()) {
        auto [d, r, c, f] = pq.top();
        pq.pop();
        if (d > dist[r][c][f])
            continue;
        if (f) {
            relax(d, r, c, 0);
            if (r > 0) relax(d + 1, r - 1, c, f);
        } else {
            if (r > 0) relax(d + 2, r - 1, c, 1);
            if (r + 1 < n) relax(d + b[r][c], r + 1, c, f);
            if (c > 0) relax(d + a[r][c-1], r, c - 1, f);
            if (c + 1 < m) relax(d + a[r][c], r, c + 1, f);
        }
    }
    cout << dist[n-1][m-1][0] << "\n";

    return 0;
}

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;
typedef pair<int, int> P;

class E {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int R, C;
        cin >> R >> C;
        vector<vector<int>> A(R, vector<int>(C - 1));
        vector<vector<int>> B(R - 1, vector<int>(C));
        REP(i, R) REP(j, C - 1) cin >> A[i][j];
        REP(i, R - 1) REP(j, C) cin >> B[i][j];

        vector<int> dist(R * C * 2, INF);
        dist[0] = 0;
        priority_queue<P, vector<P>, greater<>> pq;
        pq.push({0, 0});
        while (!pq.empty()) {
            int cost, v;
            tie(cost, v) = pq.top();
            pq.pop();
            //cout << v << ":" << cost << endl;
            int x = v % R;
            int y = v / R;
            if (v >= R * C) {
                if (x - 1 >= 0) {
                    int nv = R * y + x - 1;
                    if (cost + 1 < dist[nv]) {
                        dist[nv] = cost;
                        pq.push({cost + 1, nv});
                    }
                }
                int nv = v - R * C;
                if (cost < dist[nv]) {
                    dist[nv] = cost;
                    pq.push({cost, nv});
                }
            } else {
                int nv = v + R * C;
                if (cost + 1 < dist[nv]) {
                    dist[nv] = cost + 1;
                    pq.push({cost + 1, nv});
                }
                if (y + 1 <= C - 1) {
                    nv = R * (y + 1) + x;
                    if (cost + A[x][y] < dist[nv]) {
                        dist[nv] = cost + A[x][y];
                        pq.push({cost + A[x][y], nv});
                    }
                }
                if (y - 1 >= 0) {
                    nv = R * (y - 1) + x;
                    if (cost + A[x][y - 1] < dist[nv]) {
                        dist[nv] = cost + A[x][y - 1];
                        pq.push({cost + A[x][y - 1], nv});
                    }
                }
                if (x + 1 <= R - 1) {
                    nv = R * y + x + 1;
                    if (cost + B[x][y] < dist[nv]) {
                        dist[nv] = cost + B[x][y];
                        pq.push({cost + B[x][y], nv});
                    }
                }
            }
        }
        cout << dist[R * C - 1] << endl;
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    E solver;
    solver.solve(in, out);
    return 0;
}


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

using ll=long long;
ll mod=1000000007;

int main(){
    int N,M;
    cin >> N >> M;
    vector<vector<int>> P(N);
    for(int i=0;i<N;i++){
        int A;
        cin >> A;
        P[A].push_back(i);
    }
    for(int i=0;i<N;i++){
        int prev=-1;
        P[i].push_back(N);
        for(int j:P[i]){
            if(j-prev>M){
                cout << i << endl;
                return 0;
            }
            prev=j;
        }
    }
    cout << N << endl;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6;
typedef long long ll;
template<class T> T read()
{
	T f = 1, k = 0;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		k = k * 10 + c - '0';
		c = getchar();
	}
	return f * k;
}

int n, m, a[maxn];
vector<int> v[maxn];
int main()
{
	n = read<int>();
	m = read<int>();
	for (int i = 1; i <= n; i++)
	{
		a[i] = read<int>();
		v[a[i]].emplace_back(i);
	}
	for (int i = 0; i < maxn; i++)
	{
		if ((int)v[i].size() == 0)
			return printf("%d\n", i), 0;
		for (int j = 0; j < (int)v[i].size(); j++)
		{
			if (j == 0 && v[i][j] > m)
				return printf("%d\n", i), 0;
			if (j != 0 && v[i][j] - v[i][j - 1] - 1 >= m)
				return printf("%d\n", i), 0;
			if (j == (int)v[i].size() - 1 && n - v[i][j] >= m)
				return printf("%d\n", i), 0;
		}
	}
}


#include <cstdio>
#include <cstring>
#define mo 998244353
#define N 200010

using namespace std;

int f[N], jl, n;
bool bz1[N], bz2[N];

void dfs(int x){
	bz2[x]=1;
	if (!bz2[f[x]]) dfs(f[x]);
	else if (!bz1[f[x]]) jl++;
	bz1[x]=1;
}

int ksm(int x, int y){
	int ans=1;
	while (y){
		if (y&1) ans=1ll*ans*x%mo;
		x=1ll*x*x%mo;
		y>>=1;
	}
	return ans;
}

int main(){
//	freopen("funny.in", "r", stdin);
	scanf("%d", &n);
	for (int i=1; i<=n; i++) scanf("%d", &f[i]);
	for (int i=1; i<=n; i++){
		if (!bz2[i]) dfs(i);
	}
	printf("%d\n", ksm(2, jl)-1);

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int MAXX = 2e5 + 10, MOD = 998244353;
long long n, tmp, ans = 1;
vector <int> adj[MAXX];
bool vis[MAXX];

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

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> tmp;
		adj[i].push_back(tmp), adj[tmp].push_back(i);
	}
	for(int i = 1; i <= n; i++)
		if(!vis[i])
			dfs(i), ans = (ans * 2) % MOD;
	return cout << (((ans - 1) % MOD) + MOD) % MOD << endl, 0;
}


#include <bits/stdc++.h>
#define li long int
#define lli long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define si size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define lb lower_bound
#define ub upper_bound
#define BS binary_search
#define pll pair < lli, lli >
#define pii pair < int, int>
#define vi vector < int >
#define vvi vector < vector < int > >
#define vlli vector < lli >
#define all(x) x.begin(), x.end()
#define vvlli vector < vector < lli > >
#define vp vector < pll >
#define vvp vector< vector< pll> >
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<lli  , null_type, less<lli>, rb_tree_tag,tree_order_statistics_node_update>
#define fill(a,n) for(lli iiii=0;iiii<n;iiii++)	cin>>a[iiii];
#define mpii map<int, int>
# define precision(x) cout << fixed << setprecision(x)
#define REP(i, a, b) for (int i=a; i<=b; i++)
using namespace __gnu_pbds;
lli mod=1e9+7;
using namespace std;

int main()
{
    fastio;
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int aa=(a*d)-(b*c);
	cout<<aa<<"\n";
}

#include <iostream>

void solve() {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    std::cout << a * d - b * c << "\n";
}

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

    solve();

    return 0;
}


// Problem: C - Many Segments
// Contest: AtCoder - AtCoder Beginner Contest 207
// URL: https://atcoder.jp/contests/abc207/tasks/abc207_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define int long long
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define vi vector<int>
#define vstr vector<string>
#define vdb vector<double>
#define vbool vector<bool>
#define vvi vector<vector<int> >
#define mii map<int,int>
#define pb push_back
#define pii pair<int,int>
#define vpair vector<pii >
#define mkp make_pair
#define scan(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) {cout<<a[i]<<' ';}cout<<'\n'
#define mem(a,v) memset(a,v,sizeof(a))
#define loop(i,a,b) for (int i = a; i < b; i++)
#define loope(i,a,b) for (int i = a; i <= b; i++)
#define bloop(i,a,b) for(int i = a;i>=b;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::fixed );std::cout.precision(9)
#define PI 3.14159265
#define S second
#define F first
#define CHECK cout<<"CHEDEDWB"<<endl
#define CHECK1(a) cout<<a<<endl
#define CHECK2(a,b) cout<<a<<' '<<b<<endl;
#define br '\n'
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
using namespace std;
int mod = 1e9+7;
int inf = 1e18;
int m_inf = INT_MIN;
// int extendedgcd(int a,int b,int &x,int &y){
	// if(a == 0){
		// x = 0; y = 1;
		// return b;
	// }
	// int x1,y1;
	// int d = extendedgcd(b%a,a,x1,y1);
	// x = y1 - (b/a)*x1;y = x1;
	// return d;
// }
// int expmod(int a,int b) {
// 	if(b == 1) return a;
// 	if(b == 0) return 1;
// 	int m1 = expmod(a,b/2)%mod;
// 	if(b%2) return ((m1*m1)%mod*a%mod)%mod;
// 	return (m1*m1)%mod;
// }
// int power(int a,int b) {
// 	if(b == 1) return a;
// 	if(b == 0) return 1;
// 	int m1 = power(a,b/2);
// 	if(b%2) return m1*m1*a;
// 	return m1*m1;
// }
// int logn(int n, int r)
// {
//     return (n > r - 1) ? 1 + logn(n / r, r) : 0;
// }

// int nCr(int n,int r)
// {
	// r = min(r,n-r);
	// if(r<0)
		// return 0;
	// if(r == 0)
		// return 1;
	// int ans = 1;
	// for(int i = 1;i<=r;i++)
	// {
		// ans = ans*(n-i+1)/i;
	// }
	// return ans;
//
// }
// vi factmod(int n)
// {
	// vi ans(n+1);
	// ans[0] = 1;
	// loop(i,1,n+1)
	// {
		// ans[i] = (ans[i-1]*i)%mod;
	// }
	// return ans;
// }
// int nCrmod(int n,int r,vi &fact)
// {
	// int x1,y1;
	// extendedgcd(fact[r],mod,x1,y1);
	// int x2,y2;
	// extendedgcd(fact[n-r],mod,x2,y2);
	// int ans = (fact[n]*((x1+mod)%mod))%mod;
	// ans = (ans*((x2+mod)%mod))%mod;
	// return ans;
// }

// void dfs(vector<vector<int> > &adj,int root,vbool &vis)
// {
// 	vis[root] = true;
// 	for(int i = 0;i<adj[root].size();i++){
// 		int v = adj[root][i];
// 		if(!vis[v])
// 			dfs(adj,v,vis);
// 	}
// }
void solve()
{
	int n;
	cin>>n;
	vvi arr(n,vi(3));
	loop(i,0,n)
	{
		cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
	}
	int ans = 0;
	for(int i = 0;i<n;i++)
	{
		for(int j = i+1;j<n;j++)
		{
			vi a = arr[i];
			vi b = arr[j];
			if(a[1]>b[1])
			{
				swap(a,b);
			}
			// print(a,3);
			// print(b,3);
			if(a[2]>b[1] || a[1] == b[1])
				ans++;
			else if(a[2] == b[1] && (a[0] == 1 || a[0] == 3) && (b[0] == 1 || b[0] == 2))
				ans++;
			// cout<<ans<<'\n';

		}
	}
	cout<<ans<<'\n';
}
int32_t main()
{
	FastIO; PRECISION;
	int t = 1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
}

#include <bits/stdc++.h>

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

using namespace std;
using lint=long long;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(){}
	Fenwick_tree(int n){ build(n); }
	Fenwick_tree(const vector<G>& a){ build(a); }
	void build(int n){
		a.assign(n,G{});
	}
	void build(const vector<G>& a){
		this->a=a;
		for(int i=1;i<a.size();i++) if(i+(i&-i)-1<a.size()) (this->a)[i+(i&-i)-1]+=(this->a)[i-1];
	}
	void add(int i,const G& val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(G val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(G val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

int main(){
	int h,w,q; scanf("%d%d%d",&h,&w,&q);
	vector<int> type(q),p(q);
	vector<lint> val(q);
	rep(i,q) scanf("%d%d%lld",&type[i],&p[i],&val[i]), p[i]--;

	vector<int> idx(q);
	{
		vector<pair<lint,int>> P(q+1);
		rep(i,q) P[i]={val[i],i};
		P[q]={0,-1};
		sort(P.begin(),P.end());
		rep(i,q+1) if(P[i].second!=-1) idx[P[i].second]=i;
	}

	Fenwick_tree<lint> Frow_cnt(q+1),Frow_sum(q+1);
	Fenwick_tree<lint> Fcol_cnt(q+1),Fcol_sum(q+1);
	Frow_cnt.add(0,h);
	Fcol_cnt.add(0,w);

	lint sum=0;
	vector<int> pre_row(h,0),pre_col(w,0);
	rep(i,q){
		if(type[i]==1){
			int pre=pre_row[p[i]];
			int val_pre=Frow_sum.sum(pre,pre+1);

			sum-=val_pre*Fcol_cnt.sum(0,pre)-Fcol_sum.sum(0,pre);
			sum+=val[i]*Fcol_cnt.sum(0,idx[i])-Fcol_sum.sum(0,idx[i]);

			Frow_cnt.add(idx[i],1);
			Frow_sum.add(idx[i],val[i]);
			if(pre!=-1){
				Frow_cnt.add(pre,-1);
				Frow_sum.add(pre,-val_pre);
			}
			pre_row[p[i]]=idx[i];
		}
		else{
			int pre=pre_col[p[i]];
			int val_pre=Fcol_sum.sum(pre,pre+1);

			sum-=val_pre*Frow_cnt.sum(0,pre)-Frow_sum.sum(0,pre);
			sum+=val[i]*Frow_cnt.sum(0,idx[i])-Frow_sum.sum(0,idx[i]);

			Fcol_cnt.add(idx[i],1);
			Fcol_sum.add(idx[i],val[i]);
			if(pre!=-1){
				Fcol_cnt.add(pre,-1);
				Fcol_sum.add(pre,-val_pre);
			}
			pre_col[p[i]]=idx[i];
		}
		printf("%lld\n",sum);
	}

	return 0;
}


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

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

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

ll pow(ll x,ll n){
	ll res = 1;
	while(n > 0){
		if(n & 1) res = res * x;
		x = x*x;
		n >>= 1;
	}
	return res;
}

bool is_prime(ll n){
	if(n == 0 || n == 1) return false;
	else if(n == 2 || n == 3 || n == 5) return true;
	else if(n % 2 == 0 || n % 3 == 0 || n % 5 == 0) return false;
	else{
		for(ll i=7;i * i <= n;i+=2){
			if(n % i == 0) return false;
		}
	}
	return false;
}

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

	set<ll> s;

	for(ll a=2;a * a <= n;a++){
		ll x = a*a;
		while(x <= n){
			s.insert(x);
			x *= a;
		}
	}

	ll ans = n - s.size();

	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

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

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __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; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;
int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string x; cin >> x;
    int n = x.length();
    ll m; cin >> m;
    if (x.length() == 1) {
        if ((int)(x[0] - '0') <= m) cout << 1 << "\n";
        else cout << 0 << "\n";
    } else {
        int soko = (int)(*max_element(all(x)) - '0') + 1;
        ll ok = soko - 1, ng = m + 1;
        eprintf("%lld %lld\n", ok, ng);
        while (abs(ok - ng) > 1) {
            __int128_t med = (ok + ng) / 2;
            __int128_t y = 0, m_tmp = m;
            eprintf("%lld\n", (ll)med);
            reprev (i, n) {
                m_tmp -= x[i] - '0';
                if (m_tmp < 0) {
                    ng = med;
                    goto end_loop;
                }
                m_tmp /= med;
            }
            ok = med;
            end_loop:;
        }
        cout << ok - soko + 1 << "\n";
    }

    return 0;
}


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

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

using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
	if (b==0)return a;
	return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
ll pow(ll a, ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans;
}
const int N=1e6;
vec g[N];
ll vis[N],valid[N],a[N];
multiset<ll> m;
void dfs(int u)
{
    vis[u]=1;
    if(m.find(a[u])==m.end())valid[u]=1;
    m.insert(a[u]);
    trav(it,g[u])
    {
        if(!vis[it])
        {
            dfs(it);
        }
    }
    m.erase(m.find(a[u]));
}

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

	fast
    ll n;
    cin>>n;
    FOR(i,1,n)cin>>a[i];
    FOR(i,1,n-1)
    {
        ll x,y;cin>>x>>y;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(1);
    FOR(i,1,n)
    if(valid[i])cout<<i<<nl;

}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*

*/
vector<vector<ll>> G(100010);
bool used[100010];
ll C[100010];
ll D[100010];
vector<ll> ans;

void dfs(ll v){
  used[v] = true;
  ll col = C[v];
  if (!D[col]) ans.push_back(v+1);
  D[col]++;
  for(auto nv: G[v]){
    if (used[nv]) continue;
    dfs(nv);
  }
  D[col]--;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  rep(i, N) cin >> C[i];
  rep(i, N-1){
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(0);
  sort(all(ans));
  for(auto v: ans)
    cout << v << endl;
}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void){
    int n, sum = 0;
    cin >> n;
    vector< int > a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        sum += a[i];
    }
    sort(a.begin(), a.end());

    vector< bool > dp(sum+1, false);
    dp[0] = true;
    for(int i = 0; i < n; i++){
        for(int j = sum; j >= a[i]; j--){
            dp[j] = (dp[j] || dp[j-a[i]]);
        }
    }
    int ans = sum;
    for(int j = 0; j <= sum; j++){
        if(dp[j]) ans = min(ans, max(sum-j, j));
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define ld long double
#define rep(i,n) for (ll i = 0;i<(n);++i)
#define all(v) v.begin(),v.end()

template <typename T>bool chmax(T &a, const T& b) {if (a < b) {a = b;return true;}return false;}
template <typename T>bool chmin(T &a, const T& b) {if (a > b) {a = b;return true;}return false;}
vector<long long> divisor(long long n){vector<long long> res;long long i = 1;while (i*i<=n){if(n%i==0){res.push_back(i);}i++;}if(res.size()==0)return res;for(long long i = res.size()-1-(res.back()*res.back()==n);i>=0;--i){res.push_back(n/res[i]);}return res;}
long long safe_mod(long long x,long long m){x%=m;if(x<0){x+=m;}return x;}
long long modpow(long long x,long long n,long long mod){long long ans=1;while(n>0){if(n&1){ans*=x;ans%=mod;}n>>=1;x*=x;x%=mod;}return ans;}
long long intpow(long long x,long long n){long long ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;}
//template<typename T>T intpow(T x,T n){T ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;}
vector<pair<long long,long long>> factor_lst(long long n){vector<pair<long long,long long>> factor_lst;long long d=2;while(d*d<=n){if(n%d==0){long long num=0;while(n%d==0){num+=1;n/=d;}factor_lst.push_back({d,num});}d+=1;}if(n!=1){factor_lst.push_back({n,1});}return factor_lst;}
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))

int msb(int x){return x==0 ? -1:32-__builtin_clz(x);}//1-indexed
int lsb(int x){return x==0 ? 32:__builtin_ctz(x)+1;}//1-indexed
int popcnt(int x){return __builtin_popcount(x);}
int popcnt(long long x){return __builtin_popcount(x);}

bool ingrid(int i,int j,int H,int W){
    return 0<=i&&i<H&&0<=j&&j<W;
}

const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
template<typename T> void print(vector<T> &v){for(int i=0;i<v.size();++i){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
template<typename T> void print(T* v,int size){for(int i=0;i<size;++i){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
template<typename T,typename S>void print(pair<T,S>&p){cout<<p.first<<" "<<p.second<<endl;}
const ll LINF=4*1e18;
const ll MINF=5*1e15;
const int INF=1e9+5;
const ld PI=acosl(-1);
const ld DINF=INF;

void Main();
int main(){cout<<fixed<<setprecision(15);Main();}

void Main(){

int N;cin>>N;
vector<int> T(N);
int sum=0;
rep(i,N){
    cin>>T[i];
    sum+=T[i];
}
vector<bool> dp(sum+1,false);
dp[0]=true;
rep(i,N){
    for(int j=sum;j>0;--j){
        if(j-T[i]>=0&&dp[j-T[i]])dp[j]=true;
    }
}
int half=sum/2;
if(sum%2==1)half++;
for(int i=half;i<=sum;++i){
    if(dp[i]){
        cout<<i<<endl;
        return;
    }
}

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<(n); ++i)
#define rep1(i,n) for(ll i=1; i<=(n); ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 62) -1;
#define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T> bool updmax(T& a, T b) { if (b > a) { a = b; return true;} return false;}
template <class T> bool updmin(T& a, T b) { if (b < a) { a = b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;

int main() {
    fastio;
    ll ans=0;
    ll a,b,c,d;
    cin >> a >> b >> c >> d;
  	ans = a*d-b*c;
    cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<(n);i++)
#define rep2(i,n,m) for(ll i=n;i<(m);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
void YES(int a){
  if(a) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}
void Yes(int a){
  if(a) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}

//#include <atcoder/all>

//using namespace atcoder;
//typedef modint998244353 mint;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin>>N;
  VI A(N),P(N),X(N);
  rep(i,N) cin>>A[i]>>P[i]>>X[i];
  int ans=1001001001;
  rep(i,N){
    if(A[i]<X[i]) ans=min(ans,P[i]);
  }
  if(ans==1001001001) cout<<-1<<endl;
  else cout<<ans<<endl;
}

#include<iostream>
#include<cstring>
#include<cassert>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<time.h>
#include<bitset>
#include<cstdio>
#include<algorithm>
using namespace std;
#define REP(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) REP(i,1,n)
#define rep0(i,n) REP(i,0,n-1)
#define repG(i,x) for(int i=pos[x];~i;i=e[i].next)
#define ll long long
#define db double
const int N=1e4+7;
const int INF=1e9+7;
int n,m,num=0,cnt=0;
int pos[N],X[N],Y[N],D[N*2],c[N],vis[N];
struct edge{int v,next,c;}e[N*2];
void add(int x,int y,int c){e[num]=(edge){y,pos[x],c}; pos[x]=num++;}
void dfs(int x,int f){
	vis[x]=++cnt;
	repG(i,x){
		if(e[i].v==f)continue;
		if(vis[e[i].v]){
			if(vis[e[i].v]<vis[x])D[e[i].c]=1;
		}
		else{
			dfs(e[i].v,x);
			D[e[i].c]=1;
		}
	}
}
int main(){
	memset(pos,-1,sizeof(pos));
	scanf("%d%d",&n,&m);
	rep(i,m)scanf("%d%d",&X[i],&Y[i]);
	rep(i,n)scanf("%d",&c[i]);
	rep(i,m){
		if(c[X[i]]!=c[Y[i]]){
			if(c[X[i]]>c[Y[i]])D[i*2-1]=1;
			else D[i*2]=1;
		}
		else {
			add(X[i],Y[i],i*2-1);
			add(Y[i],X[i],i*2);
		}
	}
	rep(i,n)if(!vis[i])dfs(i,0);
	rep(i,m){
		if(D[i*2-1])puts("->");
		else puts("<-");
	}
	return 0;
}