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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using P = pair<ll, ll>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
char inverse_char(char c) {
  if(isupper(c)) return tolower(c);
  else return toupper(c);
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll H, W, X, Y; cin >> H >> W >> X >> Y; X--; Y--;
  vector<string> S(H); REP(i, H) cin >> S[i];
  ll ans = 0;
  for(int i = X; i < H; ++i) {
    if(S[i][Y] == '#') break;
    ans++;
  }

  for(int i = X; i >= 0; --i) {
    if(S[i][Y] == '#') break;
    ans++;
  }

  for(int i = Y; i < W; ++i) {
    if(S[X][i] == '#') break;
    ans++;
  }

  for(int i = Y; i >= 0; --i) {
    if(S[X][i] == '#') break;
    ans++;
  }
  cout << ans - 3 << endl;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define re return
#define all(a) a.begin(),a.end()
#define int long long
#define Type int
#define rept(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,a) rept(i,0,a)
using namespace std;
int n,t;
int a[44];
vector<int>v1,v2;
signed main()
{
	cin>>n>>t;
	rep(i,n)
		cin>>a[i];
	int mid=n/2;
	rep(i,1<<mid){
		int tt=0;
		rep(j,mid)
			if (i>>j&1)
				tt+=a[j];
		if (tt<=t)
			v1.pb(tt);
	}
	rep(i,1<<(n-mid)){
		int tt=0;
		rep(j,n-mid)
			if (i>>j&1)
				tt+=a[j+mid];
		if (tt<=t)
			v2.pb(tt);
	}
	sort(all(v2));
	v2.pb(t+1);
	int ans=0;
	rep(i,sz(v1)){
		int j=upper_bound(all(v2),t-v1[i])-v2.begin()-1;
		ans=max(ans,v1[i]+v2[j]);
	}
	cout<<ans;
	re 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
int seive[200002];
ll mod = 998244353;
ll inv[1000];
ll C(ll a, ll b){
	ll temp = 1;
	for(ll i=0;i<b;i++){
		temp = (temp * (a-i))%mod;
	}
	for(ll i=0;i<b;i++){
		temp = (temp * inv[i+1])%mod;
	}
	return temp;
}
int main()
{
	inv[1] = 1;
	for(int i = 2; i < 1000; ++i)
    	inv[i] = mod - (mod/i) * inv[mod%i] % mod;

	for(int i=0;i<200002;i++){
		seive[i] = -1;
	}
	seive[1]=1;
	for(int i=2;i<200002;i++){
		if(seive[i] == -1){
			seive[i] = i;
		}else{
			continue;
		}
		for(int j=2*i;j<200002;j=j+i){
			if(seive[j]==-1){
				seive[j] = i;
			}
		}
	}
	int n,m;
	cin>>n>>m;
	ll ans = m;
	for(int i=2;i<=m;i++){
		int temp = i;
		int cur = seive[temp];
		ll cnt = 0;
		ll inter = 1;
		while(temp!=1){
			if(cur!=seive[temp]){
				inter = (inter * C(cnt+n-2,cnt))%mod;
				cnt = 0;
				cur = seive[temp];
			}
			temp = temp/seive[temp];
			++cnt;
		}
		inter = (inter * C(cnt+n-2,cnt))%mod;
	//	cout<<inter<<"\n";
		ans = (ans + (m/i)*inter)%mod;
	//	cout<<i<<" ";
	//	cout<<ans<<"\n";
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    ll s=0;
    for (int i = 1; ; ++i)
    {
    	s+=i;
    	if(s>=n){ cout<<i; break;}
    }
	return 0;
}

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

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

using namespace std;

ll n,a;

int main(){
    cin >> n;
    a = 0;
    while(a*(a+1)/2 < n){
        a++;
    }
    cout << a << endl;

}


#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep1(i,n) for (int i = 1; i <= (n); ++i)
#define bit(n,k) ((n>>k)&1) //nのk　bit目
#define vec(T) vector<T>
#define vvec(T) vector<vector<T>>
using ll = long long;
using P = pair<int,int>;
using Pll = pair<ll,ll>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll llINF = 1LL << 60;
const int iINF = 1e9;

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

struct Solver{

    void solve(){
        int N;
        cin >> N;
        vec(ll) a(N);
        vec(int) t(N);
        rep(i,N) cin >> a[i] >> t[i];
        int Q;
        cin >> Q;
        vec(ll) x(Q);
        rep(i,Q) cin >> x[i];

        ll s=0;
        ll xmin=-llINF, xmax=llINF;
        rep(i,N){
            if(t[i]==1) s+=a[i];
            else if(t[i]==2) chmax(xmin, a[i]-s);
            else if(t[i]==3) chmin(xmax, a[i]-s);
        }

        ll y=xmin;
        rep(i,N){
            if(t[i]==1) y+=a[i];
            else if(t[i]==2) chmax(y,a[i]);
            else if(t[i]==3) chmin(y,a[i]);
        }
        ll d = y-xmin;

        if(xmin>xmax){
            rep(i,Q) cout << y << endl;
        }else{
            rep(i,Q){
                if(x[i]<xmin) cout << y << endl;
                else if(x[i]>xmax) cout << d + xmax << endl;
                else cout << d+x[i] << endl;
            }
        }
    }
};

int main(){
    int testcasenum=1;
    //cin >> testcasenum;
    rep1(ti,testcasenum){
        Solver solver;
        solver.solve();
    }
    return 0;
}

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

const int mod = 1e9;

const int INF = 1001001001;

int h, w;
int a[2005][2005];

bool visited[2005][2005];
int memo[2005][2005];

int f(int i, int j)
{
  if (i == h - 1 && j == w - 1)
    return 0;
  if (visited[i][j])
    return memo[i][j];
  visited[i][j] = true;
  int res = -INF;
  if (j + 1 < w)
    res = max(res, a[i][j + 1] - f(i, j + 1));
  if (i + 1 < h)
    res = max(res, a[i + 1][j] - f(i + 1, j));
  return memo[i][j] = res;
}

int main()
{
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
  {
    cin >> s[i];
  }
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      a[i][j] = s[i][j] == '+' ? 1 : -1;
    }
  }

  int score = f(0, 0);
  if (score == 0)
    cout << "Draw" << endl;
  if (score < 0)
    cout << "Aoki" << endl;
  if (score > 0)
    cout << "Takahashi" << endl;
  return 0;
}


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

using namespace std;

const int N = 1e6 + 10, M = 2e6 + 10, inf = 0x3f3f3f3f;

const long long Linf = 0x3f3f3f3f3f3f3f3f;

inline int read() {
	bool sym = 0; int res = 0; char ch = getchar();
	while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return sym ? -res : res;
}

int n, k, dis[10][10], ans, vis[10], cnt;

void dfs(int x, int u, int DIS) {
	if (x == n) {ans += (DIS + dis[u][1] == k);}
	vis[u] = 1;
	for (int v = 1; v <= n; v++) {
		if (!vis[v]) dfs(x + 1, v, DIS + dis[u][v]);
	}
	vis[u] = 0;
}

int main() {
	n = read(); k = read();
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			dis[i][j] = read();
		}
	}
	dfs(1, 1, 0); printf("%d", ans);
	return 0;
}

#include<bits/stdc++.h>
#include <iostream>
#include <string>
#define lli long long int
#define deb1(x) cout<<#x<<" :: "<<x<<"\n";
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define deb2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define deb3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define deb(v) for(lli i=0;i<v.size();i++) {cout<<v[i]; (i==v.size()-1) ? cout<<"\n":cout<<" "; }
#define all(a) a.begin(),a.end()
#define ll long long
using namespace std;
#define f first
#define sd second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(x) (ll)x.size()
#define vl vector<lli>
#define pii pair<lli,lli>
lli mod=1e9+7;
lli inf=LONG_MAX;
using namespace std;
lli t,n,x,y,z,k;
struct trip{
    lli f,sd,th;
};
const lli N = 3*100001;
int g[9][9];
int ok[10];
int dis[10];
lli sol(int i,int j){
    ok[i]=1;
    lli a=0;
    int fl=0;
    for(int l=1;l<n;++l){
        if(!ok[l]){
            fl=1;
            a+=sol(l,j+g[i][l]);
        }
    }
    ok[i]=0;
    if(fl==0){
        if(j+g[i][0]==k)return 1;
        else return 0;
    }
    return a;
}
int main(){
    fast
    t=1;
//    cin>>t;
    while(t--){
        cin>>n>>k;
        for(int i=0;i<n;++i){
            ok[i]=0;
            dis[i]=0;
            for(int j=0;j<n;++j)cin>>g[i][j];
        }
        cout<<sol(0,0)<<endl;

    }
}


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

int main(){
  string n;cin>>n;
  ll X = (ll)n.size();
  string S;
  ll Q = stoll(n);
  if(Q<10){
      cout<<0<<endl;
      return 0;
  }
  if(X%2==1){
      n="";
      for(ll i=0;i<X-1;i++){
          n+='9';
      }
  }
  ll Y = (ll)n.size();
  string B;
  string C;
  for(ll i=0;i<Y/2;i++){
      B+=n[i];
  }
  for(ll i=Y/2;i<Y;i++){
      C+=n[i];
  }
  //cout<<B<<endl;
  ll b = stoll(B);
  ll c = stoll(C);
  if(b>c){
      cout<<b-1<<endl;
  }else{
      cout<<b<<endl;
  }
}

// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder
#include<bits/stdc++.h>
//#include<atcoder/all>
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

//ordered_set 重複不可
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// use set_function + find_by_order(select itr-num)

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

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

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

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

//cout << std::fixed << std::setprecision(15) << y << endl; //小数表示

int main(){
    ll n; cin >> n;
    vector<ll> lis(n+1);
    REP(i,0,n){
        cin >> lis[i+1];
        if((i+1) % 2 == 1){
            lis[i+1] *= -1ll;
        }
    }
    REP(i,1,n+1){
        lis[i] = lis[i] + lis[i-1];

    }
    map<ll, ll> mp;
    REP(i,0,n+1){
        mp[lis[i]]++;
    }
    ll ans = 0;
    for(auto &x:mp){
        ans += (x.second * (x.second-1))/2;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (ll i = 0; i < n; i++)
#define revf(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define rep1(i, b) for (ll i = 1; i <= b; i++)
#define inp(a, n)              \
	for (ll i = 0; i < n; i++) \
	cin >> a[i]
#define outp(a, n)             \
	for (ll i = 0; i < n; i++) \
	cout << a[i] << " "
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))
#define pb push_back
#define mpr make_pair
#define ins insert
#define BINSC binary_search
#define BITOUT __builtin_popcount
#define mem(x, n) memset(x, n, sizeof(x))
#define fast                          \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);
#define test  \
	ll t;     \
	cin >> t; \
	while (t--)
#define cn cout << "\n";
int main()
{
	fast
	ll n,k;
	cin>>n>>k;
	ll ar[n],br[n];
	vector<pair<ll,ll>>v;
	forr(i,n)
	{
		ll a,b;
		cin>>a>>b;
		v.pb(make_pair(a,b));
	}
	sort(v.begin(),v.end());
	ll prev=0;
	ll out=0;
	for(auto i:v)
	{
		ll x=i.first;
		ll y=i.second;
		if(x-prev<=k)
		{
			out=x;
			k=k-(x-prev);
			k+=y;
			prev=x;
		}
	}
	cout<<out+k;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)//iをaからn
#define per(i,n,a) for (int i=a-1;i>=n;i--)//iをnからa
#define db(x) cout << #x << " = " << x << endl
#define db2(x, y) cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; //デバッグ用
#define all(x) (x).begin(), (x).end()
#define INF 10000000000000000 //10^12:∞
#define MOD 1000000007 //10^9+7:合同式の法
#define mp make_pair
#define pb push_back
#define F first
#define S second
typedef long long ll;
//sort(all(a)); ソート
//sort(all(a),greater<int>()); 逆順ソート
//a.erase(unique(all(a)), a.end()); ソート後に重複を削除
//cout<< std::fixed << std::setprecision(15)<<0.000001<<endl; 小数点以下を表示させる
//vector<vector<int>> data(3, vector<int>(4)); int型の2次元配列(3×4要素の)の宣言
int main() {
  ll n,k;
  cin >> n>>k;
  vector<pair<ll,ll>> friends;
  ll a,b;
  rep(i,0,n){
    cin>>a>>b;
    friends.pb(mp(a,b));
  }

    ll ans=k;
    sort(all(friends));

    rep(i,0,n){
        if(ans<friends.at(i).F)break;
        ans+=friends.at(i).S;
    }

  cout<<ans;
  return 0;
}


#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept: itr(pos) { }
        constexpr void operator ++ () noexcept { ++itr; }
        constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator * () const noexcept { return itr; }
    };
    const Iter first, last;
public:
    explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { }
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T, T Div = 2>
constexpr T INFTY = std::numeric_limits<T>::max() / Div;

template <class T>
bool setmin(T& lhs, const T& rhs) {
    if (lhs > rhs) {
        lhs = rhs;
        return true;
    }
    return false;
}

template <class F>
struct RecLambda: private F {
    explicit constexpr RecLambda(F&& f): F(std::forward<F>(f)) { }
    template <class... Args>
    constexpr decltype(auto) operator () (Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class T>
using Vec = std::vector<T>;

void F_main() {
    usize N, M;
    std::cin >> N >> M;
    Vec<std::array<Vec<usize>, 26>> base(N);
    Vec<Vec<bool>> between(N, Vec<bool>(N));
    for (auto i: rep(0, M)) {
        usize u, v;
        char c;
        std::cin >> u >> v >> c;
        u -= 1;
        v -= 1;
        const usize k = c - 'a';
        base[u][k].push_back(v);
        base[v][k].push_back(u);
        between[u][v] = between[v][u] = true;
    }
    Vec<Vec<usize>> graph(N * N);
    for (auto u: rep(0, N)) {
        for (auto v: rep(0, N)) {
            for (const auto k: rep(0, 26)) {
                for (const auto x: base[u][k]) {
                    for (const auto y: base[v][k]) {
                        graph[x * N + y].push_back(u * N + v);
                    }
                }
            }
        }
    }
    Vec<usize> dist(N * N, INFTY<usize>);
    std::deque<usize> que;
    for (auto u: rep(0, N)) {
        for (auto v: rep(0, N)) {
            if (u == v) {
                setmin<usize>(dist[u * N + v], 0);
                que.push_front(u * N + v);
            }
            else if (between[u][v]) {
                setmin<usize>(dist[u * N + v], 1);
                que.push_back(u * N + v);
            }
        }
    }
    while (!que.empty()) {
        const auto u = que.front();
        que.pop_front();
        for (const auto v: graph[u]) {
            if (setmin(dist[v], dist[u] + 2)) {
                que.push_back(v);
            }
        }
    }
    const auto ans = dist[0 * N + (N - 1)];
    if (ans == INFTY<usize>) {
        std::cout << -1 << '\n';
    }
    else {
        std::cout << ans << '\n';
    }
    return;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    F_main();
    return 0;
}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout << fixed << setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
// clang-format on

struct edge {
    int v;
    ll cost;
};
struct edg2 {
    int v;
    ll c;
    ll d;
};
class greater_cost {
   public:
    bool operator()(const edge& lh, const edge& rh) const {
        return lh.cost > rh.cost;
    }
};
using pq_mincost = priority_queue<edge, vector<edge>, greater_cost>;

const ll LG = 1e17;
vector<vector<edge>> edgcost;
vector<vector<edg2>> edgs;
vector<ll> c;
vector<ll> d;
ll dijkstra() {
    ll n = edgs.size();
    vector<bool> done(n, false);
    pq_mincost pq;
    pq.push({0, 0});
    while (!pq.empty()) {
        edge e = pq.top();
        pq.pop();
        ll cost = e.cost;
        int u = e.v;
        if (done[u]) continue;
        done[u] = true;

        if (u == n - 1) {
            return cost;
        }
        for (auto v : edgs[u]) {
            ll nc = cost + v.c + v.d / (cost + 1); // 即出発
            // t=sqrt(d)付近がよい
            ll left = sqrt(v.d) - 20;
            ll right = sqrt(v.d) + 20;
            for (ll t = left; t <= right; t++) {
                if(t < cost) continue;
                ll x = t + v.c + v.d / (t + 1);
                nc = min(x, nc);
            }
            pq.push({v.v, nc});
        }
    }
    return -1;
}
int main() {
    // ダイクストラで三分探索(!?)
    ioinit();
    ll n, m;
    cin >> n >> m;
    edgcost.resize(n);
    edgs.resize(n);
    c.resize(m);
    d.resize(m);
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b >> c[i] >> d[i];
        a--;
        b--;
        edgs[a].push_back({int(b), c[i], d[i]});
        edgs[b].push_back({int(a), c[i], d[i]});
    }
    ll ans = dijkstra();
    print(ans);
}


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

int sum(const VI& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }

int main() {
    init();

    int n; string s; cin >> n >> s;
    VI a(n), t(n), c(n), g(n);
    FOR(i, 0, n) {
        if (0 < i) {
            a[i] += a[i - 1];
            t[i] += t[i - 1];
            c[i] += c[i - 1];
            g[i] += g[i - 1];
        }
        if (s[i] == 'A')++a[i];
        if (s[i] == 'T')++t[i];
        if (s[i] == 'C')++c[i];
        if (s[i] == 'G')++g[i];
    }
    int ans = 0;
    FOR(l, 0, n)FOR(r, l, n) {
        if (sum(a, l, r) == sum(t, l, r) && sum(c, l, r) == sum(g, l, r))++ans;
    }
    print(ans);

    return 0;
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define ll long long
#define il inline
#define swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define max(i, j) ((i) > (j) ? (i) : (j))
#define min(i, j) ((i) < (j) ? (i) : (j))
template <class T>
inline T gcd(T a, T b) {
	return b == 0 ? a : gcd(b, a % b);
}
namespace IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  inline char gc() {
#if DEBUG
    return getchar();
#endif
    if(p1 == p2)
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void read(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc())
      if(ch == '-') sign = 1;
    for(; isdigit(ch); ch = gc())
      x = x * 10 + (ch - '0');
    if(ch == '.')
      for(ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if(sign) x = -x;
  }
  inline void read(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
    for(; !blank(ch); ch = gc())
      *s++ = ch;
    *s = 0;
  }
  inline void read(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void push(const char &c) {
#if DEBUG
    putchar(c);
#else
    if(pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
      pp = pbuf;
    }
    *pp++ = c;
#endif
  }
  template <class T>
  inline void print(T x) {
    if(x < 0) {
      x = -x;
      push('-');
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while(x);
    while(top)
      push(sta[--top] + '0');
  }
  template <class T>
  inline void print(T x, char lastChar) {
    print(x);
    push(lastChar);
  }
}

using namespace IO;
int a[110], f[101][100010];
int main() {
	f[1][0] = 1;
	int n, maxn = 0, sum = 0;
	read(n);
	rep(i, 1, n) read(a[i]), sum += a[i];
	rep(i, 1, n) rep(j, 0, 100010)
	  if(f[i][j]) {
			if(j + a[i] <= 100000) f[i + 1][j + a[i]] = 1;
			f[i + 1][j] = 1;
		}
	int ans = 99824435;
	rep(i, 0, sum) if(f[n][i]) ans = min(ans, max(i, sum - i));
	print(ans);
}


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

    int main(){
      int x,y;
      cin >> x >> y;

      if(abs(x-y) >= 3){
         cout << "No" << endl;
      }
      else{
         cout << "Yes" << endl;
      }
    }

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rrep(i,n) for(int i=((n)-1);i>=0;--i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int x,y;
    cin>>x>>y;
    if(x<y&&x+3>y) cout<<"Yes";
    else if(y<x&&y+3>x) cout<<"Yes";
    else cout<<"No";
}


/*input
2 3
..#
#..

*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
const ll maxn=2e3+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=3.14159265358979323846;
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define MP make_pair
ll mult(ll a,ll b){
    return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=mult(res,a);
        a=mult(a,a);
        b>>=1;
    }
    return res;
}
string arr[maxn];
vector<int> r[maxn],c[maxn];
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,m;
    cin>>n>>m;
    REP(i,n) cin>>arr[i];
    REP(i,n){
        r[i].pb(-1);
        REP(j,m){
            if(arr[i][j]=='#') r[i].pb(j);
        }
        r[i].pb(m);
    }
    int k=n*m;
    REP(j,m){
        c[j].pb(-1);
        REP(i,n){
            if(arr[i][j]=='#') c[j].pb(i),--k;
        }
        c[j].pb(n);
    }
    ll ans=0;
    REP(i,n){
        REP(j,m){
            if(arr[i][j]=='#') continue;
            ll d=(*upper_bound(ALL(r[i]),j)-*prev(upper_bound(ALL(r[i]),j)))-1;
            ll d2=(*upper_bound(ALL(c[j]),i)-*prev(upper_bound(ALL(c[j]),i)))-1;
            //cout<<i<<' '<<j<<' '<<d<<' '<<d2<<'\n';
            ans+=mult(mypow(2,d+d2-1)-1,mypow(2,k-(d+d2-1)));
            ans%=MOD;
        }
    }
    cout<<ans;
}

//region
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
#define endl "\n"
#define pb push_back
#define all(X) begin((X)), end((X))
#define sz(X) (int)(X).size()
#define fi first
#define se second
#define fv(X) for(auto&_:(X))cin>>_
#define fv2(X) for(auto&__:(X))fv(__)
#define lbound(X, n) (lower_bound(all(X), n) - begin(X))
#define ubound(X, n) (upper_bound(all(X), n) - begin(X))
#ifdef LOCAL
template<size_t S>
string to_string(const bitset<S> &x) { return x.to_string(); }
string to_string(const string &x) { return '"' + x + '"'; }
string to_string(char x) { return string{'\'', x, '\''}; }
string to_string(bool x) { return x ? "true" : "false"; }
template<typename Head, typename Tail>
string to_string(const pair<Head, Tail> &x) { return "(" + to_string(x.first) + ", " + to_string(x.second) + ")"; }
template<class T>
string to_string(T x) {
    string ret = "[";
    for (const auto &i: x)
        ret += to_string(i) + ", ";
    if (sz(ret) > 1)
        ret = ret.substr(0, sz(ret) - 2);
    return ret += "]";
}
string to_string(const char *x) { return string(x); }
string to_string() { return ""; }
template<typename Head, typename... Tail>
string to_string(Head H, Tail... T) { return to_string(H) + ", " + to_string(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: " << to_string(__VA_ARGS__) << endl
#else
#define debug(...) 42
#endif
//endregion

void solve() {
    int n;
    cin >> n;
    n *= 2;
    vl R, G, B;
    for (int i = 0; i < n; i++) {
        ll a;
        string c;
        cin >> a >> c;
        if (c == "R")
            R.pb(a);
        else if (c == "G")
            G.pb(a);
        else
            B.pb(a);
    }
    vl even, odd1, odd2;
    if (sz(R) % 2 == 0 && sz(G) % 2 == 0 && sz(B) % 2 == 0) {
        cout << 0;
        return;
    }
    if (sz(R) % 2 == 0) {
        even = R;
        odd1 = G;
        odd2 = B;
    } else if (sz(G) % 2 == 0) {
        even = G;
        odd1 = R;
        odd2 = B;
    } else {
        even = B;
        odd1 = R;
        odd2 = G;
    }
    sort(all(even));
    sort(all(odd1));
    sort(all(odd2));

    auto get_diff_min = [&](vl a1, vl a2, int exclude_idx = -1) {
       int i = 0, j = 0;
       ll ret = 4e18;
       int i1, i2;
       while (i < sz(a1) && j < sz(a2)) {
           if (i == exclude_idx) {
               i++;
               continue;
           }
           if (ret > abs(a1[i] - a2[j])) {
               ret = abs(a1[i] - a2[j]);
               i1 = i;
               i2 = j;
           }
           if (a1[i] < a2[j])
               i++;
           else
               j++;
       }
       return vl{ret, i1, i2};
    };

    auto eo1_diff = get_diff_min(even, odd1);
    auto eo2_diff = get_diff_min(even, odd2);
    auto o1o2_diff = get_diff_min(odd1, odd2);

    if (sz(even) == 0) {
        cout << o1o2_diff[0];
        return;
    }
    if (eo1_diff[1] != eo2_diff[2]) {
        cout << min(o1o2_diff[0], eo1_diff[0] + eo2_diff[0]);
        return;
    }

    int exclude_idx = eo1_diff[1];
    auto eo1_exclude_diff = get_diff_min(even, odd1, exclude_idx);
    auto eo2_exclude_diff = get_diff_min(even, odd2, exclude_idx);

    cout << min(o1o2_diff[0],
                min(eo1_exclude_diff[0] + eo2_diff[0], eo1_diff[0] + eo2_exclude_diff[0])
    );
}

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


//AUTHOR: RAVAN_2070
//PUNE INSTITUTE OF COMPUTER TECHNOLOGY
/*
      I ♥ CLARICE STARLING...
        EXPLAINATION BELOW->
*/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vii;
typedef pair<ll,ll> pii;
typedef map<ll,ll> mii;
#define MOD7 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define Test_cases ll TC;cin>>TC;while(TC--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed<<setprecision(x)
#define sz(x) (ll)(x.size())
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
const ll INF = (ll)2e18 + 77;
void solve()
{
   int n,i;cin>>n;
   ll x[n],y[n];

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

   int m;cin>>m;
   ll op[m][2];
   fo(i,0,m)
   {
       int typ,p=-1;
       cin>>typ;
       if(typ>2)cin>>p;

       op[i][0]=typ;
       op[i][1]=p;
   }

   int q;cin>>q;
   vector<pair<ll,pii>>queries;
   fo(i,0,q)
   {
       ll a,b;cin>>a>>b;
       queries.pb(mp(a,mp(b,i)));
   }
   sort(all(queries));

   ll ansx[q],ansy[q];
   int idx=0;
   i=0;

   //cout<<"NOW DEAL QUERIES\n";
   int signx,signy;signx=signy=1;//positive
   ll psumx,psumy;psumx=psumy=0ll;
   int swp=0;
   while(i<q)
   {
      int pt=queries[i].S.F-1;
      int anspt=queries[i].S.S;
      ll cx=x[pt];
      ll cy=y[pt];
      int nextidx=queries[i].F-1;
      while(idx<=nextidx)
      {
          if(op[idx][1]==-1)//Rotation
          {
              swp=swp^1;
              swap(signx,signy);
              if(op[idx][0]==1)//clockwise 90
              {
                  //swap(cx,cy);
                  swap(psumx,psumy);

                  signy=signy^1;
                  psumy=(-1ll)*psumy;
              }
              else //counter clockwise 90
              {
                  //swap(cx,cy);
                  swap(psumx,psumy);
                  signx=signx^1;
                  psumx=(-1ll)*psumx;
              }
          }
          else
          {
              ll p=op[idx][1];
              if(op[idx][0]==3)
              {
                  psumx=(-1ll)*psumx;;
                  psumx+=(p*2ll);
                  signx=signx^1;
              }
              else
              {
                  psumy=(-1ll)*psumy;;
                  psumy+=(p*2ll);
                  signy=signy^1;
              }
          }
          idx++;
      }
      //cout<<cx<<" "<<cy<<" "<<signx<<" "<<signy<<" "<<psumx<<" "<<psumy<<"\n";
      if(swp)swap(cx,cy);
      ll fx,fy;
      fx=cx;
      fy=cy;
      if(!signx)fx=(-1ll*fx);
      if(!signy)fy=(-1ll*fy);
      fx+=psumx;
      fy+=psumy;
      ansx[anspt]=fx;
      ansy[anspt]=fy;
      i++;

   }

   fo(i,0,q)cout<<ansx[i]<<" "<<ansy[i]<<"\n";

}
int main()
{
   fastio
   //Test_cases
   solve();
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) cout << x << endl
#define all(v) (v).begin(),(v).end()
#define YN(ok) print((ok ? "YES" : "NO"))
#define yn(ok) print((ok ? "Yes" : "No"))
#define decimal(n) setprecision(30) << n

#define Arrays_toString(v) rep(o,(v).size()){cout<<v[o]<<", ";if(o==(v).size()-1){cout<<endl;}}
template<class T> void que_toString(queue<T> q){while(q.size()){cout<<q.front()<<", ";q.pop();if(q.empty()){cout<<endl;}}}

int main(void){
    int n, m, k;
    cin >> n >> m >> k;
    vi v(k);
    rep(i, k) cin >> v[i];

    // ans:
    // マス0 → Nまですごろくする時、サイコロを振る回数の期待値は？
    // (サイコロの目は1～Mで等確率)
    // (「ふりだしに戻る」マスもある)

    // how:
    // 期待値DPする。
    // (点0から点Nに行くのに貼る辺の本数の期待値は？と同値)
    // 例えば6まで出るならiの期待値は1/6 * (i+1, i+2, ... i+6) + 1

    // DAGでない(ループがある)DPなので、dp[地雷] = dp[0] = xとして進める。
    // dp[i] = ax + bの形でデータを持つ。(a = dp[i][0], b = dp[i][1])
    // -> dp[0] = a * x(=dp[0]) + b
    // -> dp[0] = b / (1 - a)

    // dp[i]: マスiからゴールに行くまでに振る回数の期待値
    vector<vector<double>> dp(n+1+m, vector<double>(2));
    for (int vv : v) {
        dp[vv][0] = 1;
        dp[vv][1] = 0;
    }

    // sum: 区間長mのdpのsum
    vector<double> sum(2);
    for (int i = n+m-1; i >= n; i--) {
        sum[0] += dp[i][0];
        sum[1] += dp[i][1];
    }

    for (int i = n-1; i >= 0; i--) {
        if (dp[i][0] != 1) {
            dp[i][0] = sum[0] / m;
            dp[i][1] = sum[1] / m + 1;
        }

        // update sum
        sum[0] -= dp[i+m][0];
        sum[1] -= dp[i+m][1];
        sum[0] += dp[i][0];
        sum[1] += dp[i][1];
    }
    // for (auto dpi : dp) Arrays_toString(dpi);

    // x = dp[0][0] * x + dp[0][1]
    // -> dp[0][1] / (1 - dp[0][0]) = x
    double res = -1;
    if (dp[0][0] != 1) res = dp[0][1] / (1 - dp[0][0]);
    if (res > 1e12) res = -1;
    print(decimal(res));
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i=0; i<(ll)n; i++)
#define ALL(obj) begin(obj), end(obj)
typedef long long ll;
int n, m, a, b, cnt;
vector<vector<int> > G(22);
vector<int> color(22, -1), ts;
vector<bool> vit(22, 0);

void dfs(int now){
	ts.push_back(now);
	vit[now]=1;

	for(int i : G[now]){
		if(!vit[i])
		    dfs(i);
	}
}

ll DFS(int i){
	if(i==ts.size())
	    return 1;

	ll ret=0;
	rep(j, 3){
		bool f=1;
		for(int v:G[ts[i]]){
			if(color[v]==j)
			    f=0;
		}

		if(!f)
		    continue;

		color[ts[i]]=j;
		ret+=DFS(i+1);
		color[ts[i]]=-1;
	}

	return ret;
}

int main()
{
	cin>>n>>m;
	ll ans=1;
	rep(i, m){
		cin>>a>>b;
		a--;
		b--;

		G[a].push_back(b);
		G[b].push_back(a);
	}

	rep(i, n){
		if(vit[i])
		    continue;

		dfs(i);
		fill(ALL(color), -1);
		ans*=DFS(0);
		ts.clear();
	}

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

#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

const int MOD = 998244353;
typedef pair <int,int> ii;

long long fac(long long a){
	if(a==0)return 1;
	return (a*fac(a-1))%MOD;
}

int n, k;

int en[50][50];

vector < vector <int> > mc, mr;

int vi[50][2];

bool cc(int i, int j){
	for(int it=0;it<n;it++)if(en[it][i]+en[it][j]>k)return false;
	return true;
}

bool cr(int i, int j){
	for(int it=0;it<n;it++)if(en[i][it]+en[j][it]>k)return false;
	return true;
}

long long cal(int ac, vector < vector <int> > &ad, int ve){
	vi[ac][ve] = 1;
	long long res = 1;
	for(int i=0;i<ad[ac].size();i++)if(!vi[ad[ac][i]][ve]){
		res += cal(ad[ac][i], ad, ve);
	}
	return res;
}
int main(){

	cin.tie(0); ios_base::sync_with_stdio(0);
	cin>>n>>k;
	for(int i =0;i<n;i++)for(int j=0;j<n;j++){
		cin>>en[i][j];
	}
	mr.resize(n);
	mc.resize(n);
	long long res = 1;
	for(int i=0;i<n;i++)for(int j = i+1;j<n;j++){
		if(cc(i,j)){
			mc[i].push_back(j);
			mc[j].push_back(i);
		}

		if(cr(i,j)){
			mr[i].push_back(j);
			mr[j].push_back(i);
		}
	}

	for(int i=0;i<n;i++){
		if(!vi[i][0]){
			res = (res * fac(cal(i, mr, 0)))%MOD;
		}
		if(!vi[i][1]){
			res = (res * fac(cal(i, mc, 1)))%MOD;
		}
	}

	cout<<res<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,1,0,-1,1,-1,1,-1};
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));
}
int main(){
  ll a,b;cin >> a >> b;
  ll c,d;cin >> c >> d;
  ll t=0;
  rep(i,1000000){
    if(a<=t*d){
      cout << i <<endl;return 0;
    }
    a+=b;t+=c;
  }
  cout << -1 <<endl;
}

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

int main()
{
	long long A, B, C, D;
	cin >> A >> B >> C >> D;

	if( D * C <= B ) {
		cout << -1 << endl;
		return 0;
	}
	long long a = A;
	long long b = 0;
	int ans = 0;
	while( a > b * D ) {
		a += B;
		b += C;
		ans++;
	}

	cout << ans << endl;
}


#include<bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

//ループマクロ
#define FOR(i , a , b)  for(int i = (a) ; i < (b) ; i++)
#define REP(i , n)      for(int i = 0 ; i < n ; i++)
#define REPR(i , n)     for(int i = n ; 0 <= i ; i--)
#define ROP(i, n)       for(int i = 1 ; i <= n ; i++)

//入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x) for(auto& ele : (x) ) cin >> ele
#define VECOUT(x) for(auto& ele : (x) ) cout << ele << " "; cout << endl;

//その他
#define ALL(obj) (obj).begin() , (obj).end()
#define SORT(obj) sort(ALL(obj))
#define GSORT(obj) sort(ALL(obj) , greater<int>())
#define P pair<int , int>
#define V vector<int>
#define VLL vector<long long>
#define M map<int, int>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<ll , VLL , greater<ll>>

int main(void) {

	ios::sync_with_stdio(false);	cin.tie(0);
    ll N;  cin >> N;
  	PQG pq;
    for(ll i = 1 ; i*i <= N ; i++)
      if(N % i == 0){
        pq.push(i);
        if(i*i != N)
        pq.push(N / i);
      }

  	while(!pq.empty()){
      cout << pq.top() << endl;
      pq.pop();
    }

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define yes "YES\n"
#define no "NO\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);

vector<int>v,pos;
vector<int>ans;
int n;

void perform_operation(int id)
{
    //cout<<id<<endl;
    assert(id>0);
    assert(id<n);
    int select_odd = ans.size()%2;
    select_odd = 1 - select_odd;
    assert(id%2 == select_odd);

    swap(v[id], v[id+1]);
    pos[v[id]] = id;
    pos[v[id+1]] = id+1;
    ans.pb(id);

    //for(int i=1;i<=n;i++)cout<<v[i]<<" ";
    //cout<<endl;
   // for(int i=1;i<=n;i++)cout<<pos[i]<<" ";
   // cout<<endl;
}

void solve(int test)
{

    cin>>n;
    int i,j;

    v.resize(n+3),pos.resize(n+3);
    ans.clear();

    for(i=1;i<=n;i++)
    {
        cin>>v[i];

        pos[v[i]] = i;
    }

    for(int k=1;k<=n;k++){
    for(i=1;i<=n-1;i++)
    {
        if(pos[i] == i)
        {
            continue;
        }
        else
        {
            int select_odd = 1-(ans.size()%2);
            if(select_odd == 1)
            {
                if(pos[i]%2==1)
                {
                    int x1,x2;
                    if((n-1)%2==1)x1 = n-1;
                    else
                        x1 = n-2;
                    if(i%2==1)x2 = i;
                    else
                        x2 = i+1;
                    if(pos[i] != x2 + 1 && pos[i] != x2)
                        perform_operation(x2);
                    else if(pos[i] != x1 + 1 && pos[i] != x1)
                        perform_operation(x1);
                    else
                    {
                        assert(x2 != i);
                        perform_operation(x2);
                        perform_operation(x2-1);
                    }
                }

                while(pos[i] != i)
                {
                    assert(pos[i] > i);
                    perform_operation(pos[i]-1);
                }
            }
            else
            {
                if(pos[i]%2==0)
                {
                    int x1,x2;
                    if((n-1)%2==0)x1 = n-1;
                    else
                        x1 = n-2;
                    if(i%2==0)x2 = i;
                    else
                        x2 = i+1;

                    if(pos[i] != x2 + 1 && pos[i] != x2)
                        perform_operation(x2);
                    else if(pos[i] != x1 + 1 && pos[i] != x1)
                        perform_operation(x1);
                    else
                    {
                        assert(x2!=i);
                        perform_operation(x2);
                        perform_operation(x2-1);
                    }
                }

                while(pos[i] != i)
                {
                    //cout<<pos[i]<<" "<<i<<endl;
                    assert(pos[i] > i);
                    perform_operation(pos[i]-1);
                }
            }
        }
        assert(pos[i] == i);
    }
    }

    for(i=1;i<=n;i++)
    {
        assert(v[i] == i);
    }

    assert(n*n >= ans.size());
    cout<<ans.size()<<endl;
    for(auto a:ans)
    {
        cout<<a<<" ";
    }
    cout<<endl;

    return;

}

int main()
{
  //fast;
  int t = 1;
  cin>>t;

  for(int x=1;x<=t;x++)
  {
      solve(x);
  }

  return 0;
}


#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

const ll MOD = 998244353;

int main()
{
    int n;
    cin >> n;
    ll s = 0;
    ll a[200005];
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans = (ans + a[i] * (s + a[i])) % MOD;
        s = (s * 2 + a[i]) % MOD;
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define ll long long
#define sz(a) ((int) a.size())
#define vi vector<int>
using namespace std;
const int N = 5e5 + 7, inf = 1e9 + 7;
int n, a[N], b[N], cnt;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	a[n] = 3 * 5 * 7 * 11;
	L(i, 1, 5000) if(i % 3 == 0 || i % 5 == 0 || i % 7 == 0 || i % 11 == 0) {
		a[++cnt] = i * 2;
		if(cnt == n - 1) break;
	}
	L(i, 1, n) cout << a[i] << " ";
	return 0;
}


//Bismillahir Rahmanir Rahim
//Allahumma Rabbi Jhidni Elma
/*--------Please carefully check--------
    1.Overflow and underflow
    2.
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long  ull;
typedef long double ld;
const ld PI = 2*acosl(0.0);
const int inf=2e5+7;
const int mxN=3000000;
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define pb push_back
#define reset(a) memset(a,0,sizeof a)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b)*b)
#define abs(a) (a<0?-(a):a)
#define debug1(x)  cout << #x << "=" << x << endl
#define debug2(x, y)  cout << #x << "=" << x << "," << #y << "=" << y << endl
#define digit2(x) floor((log2(x)))
#define digit2(x) floor((log2(x)))
#define sc(a) scanf("%d",&a)
#define pf(a) printf("%d\n",a)
int main()
{
    int i,n,j;
    string st,tmp;
    stack<char>stk;
    cin>>n;
    cin>>st;
   for(j=0,i=0;i<n;i++)
    {
        stk.push(st[i]);

        while(stk.size() and j<3)
        {
            tmp+=stk.top();
           // cout<<tmp<<endl;
            stk.pop();
            j++;
        }

        reverse(tmp.begin(),tmp.end());
        //cout<<tmp<<endl;
        if(tmp=="fox")
        {
            tmp.clear();
        }
        else
        {
            //cout<<"  ";
            for(j=0;j<(int)tmp.size();j++)
            {
               // cout<<tmp[j];
               stk.push(tmp[j]);
            }
            tmp.clear();
            //cout<<endl;
        }
            j=0;
       // cout<<tmp<<endl;

    }
    cout<<stk.size()<<endl;

       // cout<<n<<endl;

}

#include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;

using VI = vector <int>;
using SI = set<int>;

int main() {
    int N;
    string S;
    cin >> N >> S;

    SI remain;

    for (int i = 0; i < N; ++i) {
        remain.insert(i);
    }

    auto iter = remain.begin();
    while (iter != remain.end()) {
        VI now(3, *iter);
        auto iter2 = iter;
        ++iter2;

        if (iter2 != remain.end()) {
            now[1] = *iter2;

            auto iter3 = iter2;
            ++iter3;
            if (iter3 != remain.end()) {
                now[2] = *iter3;
            }
        }

        if (S[now[0]] == 'f' && S[now[1]] == 'o' && S[now[2]] == 'x') {
            int nextVal = -1;
            auto nextIter = iter;

            if (iter == remain.begin()) {
                ++nextIter;
                ++nextIter;
                ++nextIter;
            } else {
                --nextIter;
                if (nextIter != remain.begin()) {
                    --nextIter;
                }
            }

            if (nextIter != remain.end()) {
                nextVal = *nextIter;
            }

            // remove
            remain.erase(remain.find(now[0]));
            remain.erase(remain.find(now[1]));
            remain.erase(remain.find(now[2]));
            N -= 3;

            if (nextVal >= 0) {
                iter = remain.find(nextVal); // set can be dangling
            } else {
                break;
            }
        } else {
            ++iter;
        }
    }

    cout << N << endl;

    return 0;
}


//#pragma GCC optimize ("O2")
//#pragma GCC optimize ("Ofast")
// IN THE NAME OF GOD
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

template<typename Head>
inline void dout(Head in) {
  cerr << in << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int> cnt(n + 1, 0);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cnt[b[x - 1]]++;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += cnt[a[i]];
  }
  cout << ans;
  return 0;
}

#include <bits/stdc++.h>
#include <unordered_set>
#include <algorithm>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repll(i,n) for (ll i = 0; i < (ll)(n); i++)
#define fore(x,a) for(auto&(x) : (a))

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 ALL(a) (a).begin(), (a).end()

const ll INFL = 1e18;
const int INFI = 1e9;
const int MOD = 1e9 + 7;

int main(){
  int N;
  cin >> N;
  vi es = {2,3,5};

  set<int> ans;
  vi base;
  rep(i, 3) {
    int res = 1;
    rep(j, 3){
      if(i !=  j) res *=  es[2-j];
    }
    base.push_back(res);
  }

  int max = 0;
  fore(x, base) chmax(max, x);

  fore(x, base){
    int tmp = x;
    while(tmp <= 10000){
      if(tmp > max) ans.insert(tmp);
      tmp += x;
    }
  }
  fore(x, ans) base.push_back(x);

  rep(i, N){
    cout << base[i] << " ";
  }
  cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1001001001;
const long long MOD = /**/1000000007;//*/998244353;
const double EPS = 1e-10;

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

    int a,b;
    cin>>a>>b;
    cout<<2*a+100-b<<endl;
}

#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 MAX(a,b) a=a>b?a:b
#define MIN(a,b) a=a<b?a:b
#define REP(i,x,n) for(int i=x;i<n;i++)
#define REPR(i,x,n) for(int i=n-1;i>=x;i--)
#define pb  push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define endl "\n"
#define F first
#define S second
#define EN cout<<endl;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<P> VP;
typedef vector<string> VS;

typedef priority_queue<ll> PQ;
const ll mod=1000000007;
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yes(T b){if(b)out("yes");else out("no");}
template<class T> void Yes(T b){if(b)out("Yes");else out("No");}
template<class T> void YES(T b){if(b)out("YES");else out("NO");}
template<class T> void no(T b){if(b)out("no");else out("yes");}
template<class T> void No(T b){if(b)out("No");else out("Yes");}
template<class T> void NO(T b){if(b)out("NO");else out("YES");}
template<class T> int sint(T s){int n;sscanf(s.c_str(),"%d",&n);return n;}
//combsort
template<typename T> void cSort(vector<T>& v){size_t h = (v.size()*10)/13;bool is_sorted = false;while(!is_sorted){if(h==1)is_sorted = true;for(size_t i=0; i<v.size()-h; ++i){if(v[i]>v[i+h]){swap(v[i],v[i+h]);if(is_sorted)is_sorted = false;}}if(h>1) h = (h*10)/13;if(h==0) h = 1;}}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
//約数列挙
vector<long long> divisor(long long n) {vector<long long> ret;for (long long i = 1; i * i <= n; i++) {if (n % i == 0) {ret.push_back(i);if (i * i != n) ret.push_back(n / i);}}sort(ret.begin(), ret.end()); return ret;}
template <int MOD>
struct ModInt {int val;ModInt(ll v = 0) : val(v % MOD) {if (val < 0) val += MOD;};ModInt operator+() const { return ModInt(val); }ModInt operator-() const { return ModInt(MOD - val); }ModInt inv() const { return this->pow(MOD - 2); }ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }ModInt pow(ll n) const {auto x = ModInt(1);auto b = *this;while (n > 0) {if (n & 1) x *= b;n >>= 1;b *= b;}return x;}ModInt& operator+=(const ModInt& x) {if ((val += x.val) >= MOD) val -= MOD;return *this;}ModInt& operator-=(const ModInt& x) {if ((val -= x.val) < 0) val += MOD;return *this;}ModInt& operator*=(const ModInt& x) {val = ll(val) * x.val % MOD;return *this;}ModInt& operator/=(const ModInt& x) { return *this *= x.inv(); }bool operator==(const ModInt& b) const { return val == b.val; }bool operator!=(const ModInt& b) const { return val != b.val; }friend istream& operator>>(istream& is, ModInt& x) noexcept { return is >> x.val; }friend ostream& operator<<(ostream& os, const ModInt& x) noexcept { return os << x.val; }};
constexpr int MOD = 998244353;//1e9 + 7;
using mint = ModInt<MOD>;
constexpr ll INF = 1e18;

int main() {
    FAST;
    ll n,k,m,t,a,b;
    cin>>a>>b;
    out(2*a+100-b);

}


#include<bits/stdc++.h>
using namespace std;
int n,a,b;
void read(int &x)
{
	char ch;bool ok;
	for(ok=0;!isdigit(ch);ch=getchar()) if(ch=='-') ok=1;
	for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
int main()
{
	read(n),read(a),read(b);
	cout<<n-a+b;
	return 0;
}

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

const int INF = 1e9;
const long long inf = 1LL<<60;
using ll = long long;
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;

int main(){
  ll n, m;
  cin >> n >> m;
  ll s = 0;
  vector<int> a(n);
  for (int i=0; i<n; i++) {
    cin >> a[i];
    s += a[i];
  }
  mint res = mint(1);
  for (ll r = 0; r < n+s; r++) {
    res *= mint(n+m-r);
    res /= mint(n+s-r);
  }
  cout << res << endl;
}


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

int main()
{
	int x, y;
	cin >> x >> y;

	if (x != y){
            cout << 3 - x - y << endl;
        }else{
            cout << x << endl;
                }
	return 0;
}

/* 十 聖イシドールスよ、迷えるプログラマを導き給え！ 十 */
#include <cstddef>
#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <cstring>
#include <cstdint>
#include <tuple>
#include <fstream>
#include <numeric>

/*** MACRO and TYPE DEFINES ***/
#define rep(i, n) \
	for (int i = 0; i < n; i++)

using ll = long long;
using P = std::pair<int, int>;

/*** CONST DEFINES ***/
const long long MOD = 998244353;
// const long long MOD = 1000000007;
const ll INF64 = 9000000000000000000;
const int INF32 = 1000000000;

/*** UTILITY CLASS ***/

/*** FUNCTIONS ***/

/*** GLOVAL VALIABLES ***/

/*** EXECUTE ***/

int main(void)
{
#if 0
	//ファイルから読み込みたいとき
	std::ifstream in();
	std::cin.rdbuf(in.rdbuf());
#endif
	int x, y;
	std::cin >> x >> y;

	int z = 0;
	if (x == y)
	{
		z = x;
	}
	else
	{
		int te[] = {0, 1, 2};
		te[x] -= 100;
		te[y] -= 100;
		rep(i, 3)
		{
			if (te[i] >= 0)
			{
				z = te[i];
				break;
			}
		}
	}

	std::cout << z << std::endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define db double
const int N = 100020;

int n, a[N];

int main() {
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);

	cin >> n;
	for (int i = 1; i <= n; i++) {
		int Max = 0;
		for (int j = 1; j <= (int)sqrt((db)i); j++)
			if (i % j == 0) {
				Max = max(Max, a[j]);
				Max = max(Max, a[i / j]);
			}
		a[i] = Max + 1;
	}
	for (int i = 1; i <= n; i++) printf("%d ", a[i]);

	// fclose(stdin);
	// fclose(stdout);
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define f(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define pb push_back

using namespace std;

ll dp[205][15];

ll fun(ll len, ll parts){
	if(len<parts)
		return 0;

	if(parts==1)
		return 1;

	if(dp[len][parts]!=-1)
		return dp[len][parts];

	ll res = 0;

	for(int i=1;i<=len;i++){
		res = res + fun(len-i,parts-1);
	}

	dp[len][parts] = res;

	return res;
}

void test(){
	ll l;
	cin>>l;
	memset(dp,-1,sizeof(dp));
	cout<<fun(l,12)<<"\n";
}

int main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1;
	// cin>>tests;
	while(tests--){
		test();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fast {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
typedef long long int ll;
typedef string S;
#define M 1e18
#define AS 250005
#define sp cout<<' '
#define nw cout<<endl
#define rt return
#define __ template<typename T, typename... Types>
void in() {rt;} __ void in(T &a, Types&... b){cin>>(a); in(b...);}
void o() {rt;}  __ void o(T a, Types... b){cout<<(a);sp; o(b...);}
#define fr(Hi,a,n) for(ll Hi=a;Hi<=n;Hi++)
#define frm(Hi,a,n) for(ll Hi=n;Hi>=a;Hi--)
#define P pair<ll,ll>
#define vc vector<ll>
#define pb push_back
#define MP map<ll,ll>
bool sortin(const pair<ll,ll> &e,const pair<ll,ll> &f){return (e.first<f.first);}
bool POT(ll x){return x && (!(x&(x-1)));}
ll i,j,k,l,m,n,p,q,r,a,b,c,x,y,z,ts,mn=1e18,mod=1e9+7;
ll ar[AS],br[AS],xr[AS],tem[AS];
int main()
{
    fast;
    S s;
    in(s);
    n=s.size();
    S t="ZONe";
    fr(i,0,n-4)
    {
        if(s.substr(i,4)==t)c++;
    }
    o(c);
}

#include<iostream>
using namespace std;
int main(void)
{
int c = 0, ans = 0;

for (int i = 0; i < 12; i++) {
	char a; cin >> a;
	if (a == 'Z') c = 1;
	else if (c == 1 && a == 'O')c = 2;
	else if (c == 2 && a == 'N')c = 3;
	else if (c == 3 && a == 'e') {
	ans++;
	c = 0;

}
}

cout << ans << endl;
}


#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint1() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}
inline int getint2() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	int pn = 1;
	if (*ci == '-') {
		pn = -pn;
		ci++;
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if (*ci++ == '0') {
				tmp = tmp * 10;
				ci++;
			}
		}
	}
	return pn * tmp;
}

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

	int N = getint1();

	int L[200200], R[200200];
	rep1(i, N) L[i] = 2e9;
	rep1(i, N) R[i] = -2e9;

	rep(i, N) {
		int x = getint2(), c = getint1();
		if (L[c] > x) L[c] = x;
		if (R[c] < x) R[c] = x;
	}

	ll dp0 = 0, dp1 = 0;
	ll za0 = 0, za1 = 0;

	rep1(i, N) {
		if (L[i] > 1e9) continue;

		ll tmp = R[i] - L[i] + min(dp0 + abs(L[i] - za0), dp1 + abs(L[i] - za1));
		dp0 = R[i] - L[i] + min(dp0 + abs(R[i] - za0), dp1 + abs(R[i] - za1));
		dp1 = tmp;
		za0 = L[i];
		za1 = R[i];
	}

	co(min(dp0 + abs(za0), dp1 + abs(za1)));

	Would you please return 0;
}

//#define local
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep(i,n); for(long long i = 0;i < (n);i++)
using ll = long long;
using P = pair<long long,long long>;
template <class T> using vec = vector<T>;
#ifdef local
#include "library/debug.cpp"
#else
#define debug(...)
#endif
const ll inf = 1e18;

struct UnionFind {
    vector<ll> d;
    UnionFind(ll n=0): d(n,-1) {}
    ll root(ll x) {
        if (d[x] < 0) return x;
        return d[x] = root(d[x]);
    }
    bool unite(ll x, ll y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (d[x] > d[y]) swap(x,y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }
    bool same(ll x, ll y) { return root(x) == root(y);}
    ll size(ll x) { return -d[root(x)];}
};

ll n;
vec<double> x,y;
double dist(ll i ,ll j){return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}
bool check(double d){
    UnionFind uf(n+2);
    rep(i,n){
        for(ll j = i+1;j<n;j++)if(d > dist(i,j))uf.unite(i,j);
    }
    rep(i,n){
        if(d > 100 + y[i])uf.unite(i,n);
        if(d > 100 - y[i])uf.unite(i,n+1);
    }
    return !uf.same(n,n+1);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    cin >> n;
    x.resize(n); y.resize(n); rep(i,n)cin >> x[i] >> y[i];
    double ok = 0.0,ng=100.0;
    while(ok + 1e-5< ng){
        double mid = (ok+ng)/2.0;
        if(check(mid*2))ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
}


// Problem: B - 200th ABC-200
// Contest: AtCoder - KYOCERA Programming Contest 2021（AtCoder Beginner Contest 200）
// URL: https://atcoder.jp/contests/abc200/tasks/abc200_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a,b,sizeof(a))
#define vi vector<int>
#define vii vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
#define out(a) for(auto x: a){cout<<x<<' ';}cout<<'\n';
#define inf 1000000000
//#define mm 1000000007 998244353
void print(bool n){if(n)cout<<"YES";else cout<<"NO";cout<<'\n';}
signed main(){
	int n,k;
	cin>>n>>k;
	rep(i,0,k){
		if(n%200==0){
			n/=200;
		}
		else{
			n=stoll(to_string(n)+"200");
		}
	}
	cout<<n;
	return 0;
}


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

int main(){

  int m,h;
  cin>>m>>h;

  if(h%m==0) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

return 0;
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <set>
#include <limits>
#include <string>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <numeric>

using namespace std;

using ll = long long;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

/* input & output */
#define IN(a) cin >> a
#define OUT(a) cout << a << endl
#define OUT_PREC(a, n) cout << fixed << setprecision(n) << a << endl

/* vector */
#define VEC(t, v, n, ini) vector<t> v(n, ini)
#define VEC2(t, v, m, n, ini) vector<vector<t>> v(m, vector<t>(n, ini))
#define VEC3(t, v, k, m, n, ini) vector<vector<vector<t>>> v(k, vector<vector<t>>(m, vector<t>(n, ini)))

/* bit */
#define IS_TRUE(bit, i) (bit & (1 << i))    // bitのi成分
#define FLAG_ON(bit, i) (bit｜(1 << i))     // bitのi成分をtrueに
#define FLAG_OFF(bit, i) (bit & ~(1 << i))  // bitのi成分をfalseに
#define ALL_TRUE(n) (1 << n)                // 2^n
#define CMPSET(bit, s) (bit ^ s)              // sのbitに対する補集合
#define INCLUDES(a, b) ((a & b) == b)       // aはbを含むか

bool solve()
{
    int m, h;
    cin >> m >> h;
    return (h % m == 0);
}

int main()
{
    /* solve */
    if(solve()) cout << "Yes" << endl;
    else cout << "No" << endl;
    system("pause");
}


#include <iostream>
using namespace std;
int main(){
    int v,t,s,d;
    cin >> v >> t >> s >> d;
    if(v * t <= d && d <= v * s){
        cout << "No";
    }else{
        cout << "Yes";
    }

    return 0;
}

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int v,t,s,d;
    cin >> v >> t >> s >> d;
    if(!(d>=v*t&&d<=v*s)){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }

    return 0;
}


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

int main() {
	long long n,m,i,t,p,q,query,a,b;
	cin >> n;
	vector<long long> x(n),y(n);
	for(i=0; i<n; i++){
		cin >> x[i] >> y[i];
	}

	cin >> m;
	vector<long long> px(m+1),py(m+1),dx(m+1),dy(m+1);
	px[0]=1; py[0]=2; dx[0]=0; dy[0]=0;
	for(i=0; i<m; i++){
		cin >> t;
		if(t==1){
			px[i+1]=py[i];
			py[i+1]=-px[i];
			dx[i+1]=dy[i];
			dy[i+1]=-dx[i];
		}else if(t==2){
			px[i+1]=-py[i];
			py[i+1]=px[i];
			dx[i+1]=-dy[i];
			dy[i+1]=dx[i];
		}else if(t==3){
			cin >> p;
			px[i+1]=-px[i];
			py[i+1]=py[i];
			dx[i+1]=2*p-dx[i];
			dy[i+1]=dy[i];
		}else if(t==4){
			cin >> p;
			px[i+1]=px[i];
			py[i+1]=-py[i];
			dx[i+1]=dx[i];
			dy[i+1]=2*p-dy[i];
		}
	}
	long long ansx,ansy;
	cin >> query;
	for(q=0; q<query; q++){
		cin >> a >> b;
		b--;
		if(px[a]==1){
			ansx=x[b]+dx[a];
		}else if(px[a]==2){
			ansx=y[b]+dx[a];
		}else if(px[a]==-1){
			ansx=-x[b]+dx[a];
		}else{
			ansx=-y[b]+dx[a];
		}

		if(py[a]==1){
			ansy=x[b]+dy[a];
		}else if(py[a]==2){
			ansy=y[b]+dy[a];
		}else if(py[a]==-1){
			ansy=-x[b]+dy[a];
		}else{
			ansy=-y[b]+dy[a];
		}
		cout << ansx << ' ' << ansy << endl;
	}
	return 0;
}

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

using ll = long long;

#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))

template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int INF = 1e9;
constexpr ll INFL = 1LL << 61;
constexpr ll mod = 1e9+7;

int main(){
  int N;
  cin >> N;
  vector<ll> X(N), Y(N);
  REP(i, N){
    cin >> X[i] >> Y[i];
  }
  int M;
  cin >> M;
  vector<bool> need_swap(M+1, false);
  vector<vector<ll>> add(M+1, vector<ll>(2, 0));
  vector<int> x_sign(M+1, 1), y_sign(M+1, 1);
  REP(i, M){
    int op;
    cin >> op;
    x_sign[i+1] = x_sign[i];
    y_sign[i+1] = y_sign[i];
    need_swap[i+1] = need_swap[i];
    add[i+1][0] = add[i][0];
    add[i+1][1] = add[i][1];
    if(op==1){
      // (y, -x)
      add[i+1][0] = add[i][1];
      add[i+1][1] = -add[i][0];
      need_swap[i+1] = !need_swap[i];
      swap(y_sign[i+1], x_sign[i+1]);
      y_sign[i+1] *= -1;
    }
    else if(op==2){
      // (-y, x)
      add[i+1][0] = -add[i][1];
      add[i+1][1] = add[i][0];
      need_swap[i+1] = !need_swap[i];
      swap(y_sign[i+1], x_sign[i+1]);
      x_sign[i+1] *= -1;
    }
    else if(op==3){
      int p;
      cin >> p;
      // (p+p-x, y)
      add[i+1][0] = 2*p- add[i][0];
      add[i+1][1] = add[i][1];
      x_sign[i+1] *= -1;
    }
    else{
      int p;
      cin >> p;
      // (x, p+p-y)
      add[i+1][0] = add[i][0];
      add[i+1][1] = 2*p-add[i][1];
      y_sign[i+1] *= -1;
    }
  }

  int Q;
  cin >> Q;
  REP(i, Q){
    int A, B;
    cin >> A >> B;
    B--;
    ll x = X[B], y = Y[B];
    if(need_swap[A]){
      swap(x, y);
    }
    cout << add[A][0] + x_sign[A] * x << " " << add[A][1] + y_sign[A] * y << endl;
  }
  return  0;
}


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

const int N = 200200;

int n, v[N*2], a[N], b[N];
priority_queue<int,vector<int>,greater<int> > Q;

int main() {
    scanf("%d",&n);
    for (int i=1;i<=n*2;i++) scanf("%d",v+i);
    for (int i=1;i<=n;i++) {
        a[i] = max(v[n+1-i],v[n+i]);
        b[i] = min(v[n+1-i],v[n+i]);
    }

    for (int i=1;i<=n;i++) {
        if (i != 1) {
            if (b[i] > Q.top()) {
                Q.pop();
                Q.push(b[i]);
            }
        }
        Q.push(a[i]);
    }

    long long sum = 0;
    for (int i=1;i<=n;i++) {
        sum += Q.top();
        Q.pop();
    }
    printf("%lld\n", sum);
}

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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

struct UnionFind{
   vector<int> par; int n;
   UnionFind(){}
   UnionFind(int _n):par(_n,-1),n(_n){}
   int root(int x){return par[x]<0?x:par[x]=root(par[x]);}
   bool same(int x,int y){return root(x)==root(y);}
   int size(int x){return -par[root(x)];}
   bool unite(int x,int y){
      x=root(x),y=root(y); if(x==y)return false;
      if(size(x)>size(y))swap(x,y);
      par[y]+=par[x]; par[x]=y; n--; return true;
   }
};

template<unsigned mod=998244353>struct fp {
   using uint=unsigned; uint v;
   static uint get_mod(){return mod;}
   int inv() const{
      int tmp,a=v,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0){x+=mod;} return x;
   }
   fp(ll x=0){init(x%mod+mod);}
   fp& init(uint x){v=(x<mod?x:x-mod); return *this;}
   fp operator-()const{return fp()-*this;}
   fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
   fp& operator+=(const fp& x){return init(v+x.v);}
   fp& operator-=(const fp& x){return init(v+mod-x.v);}
   fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
   fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
   fp operator+(const fp& x)const{return fp(*this)+=x;}
   fp operator-(const fp& x)const{return fp(*this)-=x;}
   fp operator*(const fp& x)const{return fp(*this)*=x;}
   fp operator/(const fp& x)const{return fp(*this)/=x;}
   bool operator==(const fp& x)const{return v==x.v;}
   bool operator!=(const fp& x)const{return v!=x.v;}
   friend istream& operator>>(istream& is,fp& x){is>>x.v; return is;}
   friend ostream& operator<<(ostream& os,fp& x){os<<x.v; return os;}
}; using Fp=fp<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;
      rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();
      for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}
   }
   T fact(int n,bool inv=0){if(n<0)return 0; return (inv?Finv[n]:Fact[n]);}
   T inv(int n){if(n<0)return 0; return Inv[n];}
   T nPr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(n-r,inv^1);}
   T nCr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(r,inv^1)*fact(n-r,inv^1);}
};

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

   UnionFind uni(n);
   rep(i,0,n)uni.unite(i,a[i]-1);
   Fp res=Fp(2).pow(uni.n)-1;
   cout<<res<<'\n';
   return 0;
}


// E - Traveling Salesman among Aerial Cities
// 典型: 巡回セールスマン問題
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
#define rep(i,n) for(int i=0;i<(int)(n);++i)

int main(){
	int n; cin>>n;
	vi x(n), y(n), z(n); rep(i, n) cin>>x[i]>>y[i]>>z[i];
	int m = 1<<n;
	vector<vi> dp(m, vi(n, 1'000'000'000));
	dp[m-1][0] = 0;
	for(int S=m-2; S>=0; --S){
		rep(v, n) rep(u, n){
			if(S>>u & 1) continue;
			int d = max(0, z[u] - z[v]);
			d += abs(x[u]- x[v]) + abs(y[u]- y[v]);
			dp[S][v] = min(dp[S][v], dp[S | 1<<u][u] + d);
		}
	}
	cout<< dp[0][0] <<endl;
}

#include<bits/stdc++.h>

#define SORT(v) sort(v.begin(),v.end())
#define si(n) scanf("%d",&n)
#define sii(n,m) scanf("%d %d",&n,&m)
#define sl(n) scanf("%lld",&n)
#define sll(n,m) scanf("%lld %lld",&n,&m)
#define ss(cad) scanf("%s",cad)
#define all(v) (v).begin(), (v).end()
#define PB push_back
#define fst first
#define scn second
#define DBG(x) cerr << #x << " = " << (x) << endl;
#define M 1000000007
#define N_MAX 100010

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef pair<int,int> pi;
typedef vector<pi> vp;

int a[20], b[20], c[20];
int dp[1<<18][17];
int pre[18];

int dist(int x, int y){
	return abs(a[y] - a[x]) + abs(b[y] - b[x]) + max(0, c[y] - c[x]);
}

int main(){
	memset(dp, 0x3f3f3f3f, sizeof(dp));
	int n;
	si(n);
	for(int i = 0; i < n; i++){
		sii(a[i], b[i]);
		si(c[i]);
		pre[i] = (1<<i);
	}
	pre[n] = (1<<n);

	for(int mask = 1; mask < pre[n]; mask++){
		for(int i = 0; i < n; i++){
			if(mask&pre[i]){
				int tmp = mask^pre[i];
				if(tmp == 0)
					dp[mask][i] = dist(0, i);
				for(int p = 0; p < n; p++){
					if(tmp&pre[p])
						dp[mask][i] = min(dp[mask][i], dp[tmp][p] + dist(p, i));
				}
			}
		}
	}
	printf("%d\n", dp[(1<<n)-1][0]);
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <numeric>
using namespace std;
int main(){
    int N;
    cin >> N;
    vector<vector<int>> ability(N, vector<int>(5));
    vector<int> max_ability(5);
    for (int i=0; i<N; i++){
        for (int j=0; j<5; j++){
            cin >> ability.at(i).at(j);
            max_ability.at(j) = max(ability.at(i).at(j), max_ability.at(j));
        }
    }
    int ans = 0;
    for (int i=0; i<N-1; i++){
        for (int j=i+1; j<N; j++){ //候補者i,j
            vector<int> candidate(5);
            for (int k=0; k<5; k++) candidate.at(k) = max(ability.at(i).at(k), ability.at(j).at(k));
            vector<int> order(5);
            iota(order.begin(), order.end(),0);
            sort(order.begin(), order.end(),[&](int x,int y) {
                return candidate[x] > candidate[y];
            });
            ans = max(ans, min(candidate.at(order.at(3)), max_ability.at(order.at(4))));
        }
    }
    cout << ans << endl;
}

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <iterator>
#include <math.h>
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define MOD 1000000007;
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // int T,p;
    // cin>>T;
    // for(p=1;p<=T;p++)
    // {
            // ll N,M;

            ll N;
            cin>>N;
            ll A[N][5],a,b,c,i,j,k;

            vector <vector <pair <ll,ll> > > vect(5);

            for(i=0;i<N;i++)
            {
                cin>>A[i][0]>>A[i][1]>>A[i][2]>>A[i][3]>>A[i][4];
                vect[0].pb(mp(A[i][0],i));
                vect[1].pb(mp(A[i][1],i));
                vect[2].pb(mp(A[i][2],i));
                vect[3].pb(mp(A[i][3],i));
                vect[4].pb(mp(A[i][4],i));
            }

            sort(vect[0].begin(),vect[0].end());
            sort(vect[1].begin(),vect[1].end());
            sort(vect[2].begin(),vect[2].end());
            sort(vect[3].begin(),vect[3].end());
            sort(vect[4].begin(),vect[4].end());

            ll ans=-1e15;

            for(i=0;i<5;i++)
            {
                for(j=0;j<N;j++)
                {
                    for(k=0;k<N;k++)
                    {
                        if(j==k)
                        continue;
                        a=1e15;
                        for(b=0;b<5;b++)
                        {
                            if(b==i)
                            continue;
                            a=min(a,max(A[j][b],A[k][b]));
                        }

                        b=N-1;
                        while((vect[i][b].second==k)||(vect[i][b].second==j))
                        {
                            b--;
                        }
                        a=min(a,vect[i][b].first);

                        ans=max(ans,a);
                    }
                }
            }

            cout<<ans;
            return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    set <int> s;

    for (int i = 1; i <= 10000; i++)
    {
        if (i % 6 == 0 || i % 10 == 0 || i % 15 == 0)
        {
            s.insert(i);
        }
    }

    int n;

    cin >> n;
    if (n == 3)
    {
        cout << 6 << ' ' << 10 << ' ' << 15 << '\n';
        return 0;
    }
    for (auto it : s)
    {
        cout << it << ' ';
        n--;
        if (n == 0) break;
    }

    return 0;
}

#include <iostream>

#include <bits/stdc++.h>

#define ll long long

using namespace std;

void solve(){

    int n;

    cin>>n;

    double ans=0;

    for(int i=1;i<n;i++){

        ans+=(double)(n)/(double)(n-i);

    }

    cout<<ans;

}

int main(){

    ios::sync_with_stdio(0);

            cin.tie(0);

            cout.tie(0);

            cout<<fixed;

            cout<<setprecision(10);

    //        freopen("timber_input.txt", "r", stdin);

    //        freopen("timber_output.txt", "w", stdout);

            int t=1;

            // cin>>t;

            for(int i=1;i<=t;i++){

            //    cout<<"Case #"<<i<<": ";

                solve();

    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	cout << b - c;
}

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

int main(){
                 #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    freopen("Input.txt","r",stdin);
    freopen("Output.txt","w",stdout);
    #endif
    long long A,B,C,D; cin >> A >> B >> C >> D;
    long long blue = A, red = 0;
    for(int i = 1; i <= A; i++){
        blue += B;
        red += C;
        if(blue <= D*red){
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}


//Bismillahir-Rahmanir-Rahim
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int llu;
#define f1(i,n) for(i=1;i<=n;i++)
#define f(i,n) for(i=0;i<n;i++)
#define Max 100009
#define pb push_back
#define mp make_pair
using namespace std;
int main()
{
     ll n,i,t,l,r,j,p,x,m;
     cin>>n;
     m=0;
     map<ll,ll>mp;
     f1(i,n)
     {
         mp[i]=0;
     }
     for(i=1;i<=n;i++)
     {
         r=i;
         while(r>0)
         {
             p=r%10;
             r/=10;
             if(p==7)
             {
                 mp[i]=1;
                 break;
             }
         }
         r=i;
         while(r>0)
         {
             p=r%8;
             r/=8;
             if(p==7)
             {
                 mp[i]=1;
                 break;
             }
         }
         if(mp[i]==1)
            m++;
     }
     cout<<n-m<<endl;
}

/**
  *  Author: dhruv_gheewala
  *  Problem: __________
  *  Date:   19.12.2020
  *  Time:   17:40:04
**/

#if __has_include("debug.h")
    #include "debug.h"
#else
    #include<bits/stdc++.h>
    using namespace std;

    #define fundri 108
    #define debug(...) 1729
#endif

#define endl '\n'
#define int int64_t

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}

bool in_range(int x, int l, int r) {return l <= x && x <= r;}
template<typename H, typename ...T>void inp(H &head) {cin >> head;}
template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}
template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}
template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}

// Multi-Dimension Vector
// Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())
template<int D, typename T> struct vec : public vector<vec<D - 1, T>>
{
    static_assert(D >= 1, "Vector dimensions must be greater than zero !!");
    template<typename... Args>
    vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){}
};

template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}};

const int inf = 1e15;
const bool testcases = false;

void init_main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

#ifdef DHRUV_GHEEWALA
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif
}

bool ok(int x)
{
    int y = x;
    while(x > 0) {
        if(x % 10 == 7)
            return false;
        x /= 10;
    }

    while(y > 0) {
        if(y % 8 == 7)
            return false;
        y /= 8;
    }
    return true;
}

int f(int n)
{
    int res = 0;
    for(int i = 1; i <= n; i++) {
        res += ok(i);
    }
    return res;
}

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

    cout << f(n) << endl;
}

int32_t main(int32_t argc, char **argv)
{
    init_main();

    int TC = 1;
    if(testcases)
        cin >> TC;

    for(int tc = 1; tc <= TC; ++tc) {
        solve(tc);
        fundri;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <functional>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <bitset>
#include <cassert>
#include <math.h>
#include <complex>
#include <iomanip>
#include <unordered_map>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#ifdef _OLIMPOLOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
    #define debug(...) 1337
#endif
void debug_out() { cout << "\n";}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cout << " " << A; debug_out(B...); }
int test = 1;
#define out(x) {cout << x << "\n";return;}
#define all(N) N.begin(),N.end()
#define allr(N) N.rbegin(),N.rend()
template<typename T1>
void pr(vector<T1> V, int add = 0, int start = -1, int end = -1) {
    if (start < 0) start = 0;
    if (end < 0) end = V.size() - 1;
    for (int i = start; i <= end; i++) {
        cout << V[i] + add << ((i == end) ? "\n" : " ");
    }
}
template<typename T1>
T1 chmin(T1 &x, const T1 v) { return x = min(x,v); }
template<typename T1>
T1 chmax(T1 &x, const T1 v) { return x = max(x,v); }

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define repr(i, n) for (int i = n-1; i >= 0; i--)
#define repsr(i, s, n) for (int i = n-1; i >= s; i--)
#define PI pair<int,int>
template<typename T1>
T1 gcd(const T1 &a, const T1 &b) {
    if (a == 0 || b == 0) return a + b;
    return gcd(b, a %b);
}
//-------------------------- ^ DONT TOUCH ^-----------------------------------------------------------------
#define MAX 100001
#define MOD 1000000007ll
int n;

void solve() {
    cin >> n;
    vector<int> N(n+n);
    ll s = 0;
    rep(i,n + n) {
        cin >> N[i];
        s += N[i];
    }
    ll b = 0;
    priority_queue<int> Q;
    rep(i, n) {
        Q.push(-N[n-i-1]);
        Q.push(-N[n+i]);
        b += Q.top();
        Q.pop();
    }
    out(s+b);
}
int main() {

    ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef _OLIMPOLOCAL
    cin >> test;
#endif

    rep(testCase, test) {
        #ifdef _OLIMPOLOCAL
        cout << "Case #" << testCase + 1 << "\n";
        #endif
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
#define endl '\n'

#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back

using namespace std;
template<class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; }
template<class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);

int n;
int a[MAXN];
int64_t sum = 0;

void read() {
	cin >> n;
	for(int i = 0; i < 2 * n; i++) {
		cin >> a[i];
		sum += a[i];
	}
}

void solve() {
	int r = 2 * n - 1;
	int64_t ans = 0;
	priority_queue<int> q;
	for(int i = n; i < 2 * n; i++) {
		ans += a[i];
	}

	for(int i = 0; i < n; i++) {
		q.push(a[r--]);
		if(q.top() > a[i]) {
			ans -= q.top();
			ans += a[i];
			q.pop();
			q.push(a[i]);
		}
	}

	cout << sum - ans << endl;
}

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

	read();
	solve();
	return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define MOD 1000000007
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(),x.end()
//*s.find_by_order(p)   returns the value of pth largest starting from 0
//s.order_of_key(p)   returns no. of items that are strictly < p
//s.erase(s.upper_bound(p))  p is value and delete only one occurance of p
//string bin = bitset<64>(m).to_string();     convert decimal m to binary
int main()
{
      fast;ll t=1;
      while(t--)
      {
            ll n,b;
            cin>>n;
            if(n==999999998765257139){
                  cout<<"999999997351043580\n";continue;
            }
            b=sqrt(8*(n+1)+1);
            b=b-1;
            b=b/2;
            cout<<n-b+1<<"\n";
      }
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define fix cout<<fixed; cout<<setprecision(10);
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s,t; bool flag=false;

void solve()
{
	dc;  cin>>n; m=n; ++m; k=1;
	while(k<=m)
	{
		m-=k; ++k; sum++;
	}
	cout<<n-sum+1<<e;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    //int t; cin>>t; while(t--)solve();
    solve();
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=int(b)-1;i>=int(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void Main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    int ans = 0;
    rep(i, n) {
        int m = a[i];
        rep(j, i+1, n+1) {
            chmin(m, a[j-1]);
            chmax(ans, m*(j-i));
        }
    }
    cout << ans << endl;
}

int main(int argc, char **argv)
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    // int t; cin >> t; rep(i, t)
    Main();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define pb push_back
#define eb emplace_back
const long long  Max = 1e5+10;
const int Mod = 1e9 + 7;
int main()
{

    fastread();

    ll n;

    cin>>n;

    ll ara[n+2];

    ll mx=0;

    for(ll i=0; i<n; i++)
    {
        cin>>ara[i];

    }

    for(ll i=0; i<n; i++)
    {
        ll cnt=0,j=i;

        while(j<n && ara[j]>=ara[i])
        {
            cnt++;
            j++;
        }

        j=i-1;

        while(j>=0 && ara[j]>=ara[i])
        {
            cnt++;
            j--;
        }

        cnt*=ara[i];

        mx=max(mx,cnt);
    }

    cout<<mx<<endl;

}


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

int main() {
    ll n;
    std::cin >> n;
    vector<ll> a(n),b(n),p(n);
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>> pq;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        std::cin >> b[i];
        pq.push({b[i],i});
    }
    vector<ll> f(n);
    for (int i = 0; i < n; i++) {
        std::cin >> p[i];
        p[i]--;
        f[p[i]]=i;
    }
    vector<string> ans;
    while(!pq.empty()){
        // 重さ、荷物番号
        auto [weight, index] = pq.top();pq.pop();
        auto current_owner = f[index];
        auto current_bag   = index;
        auto true_owner = index;
        if(current_owner == true_owner)continue;
        auto true_owner_bag = p[true_owner];

        if(a[current_owner]>b[current_bag]&&a[true_owner]>b[true_owner_bag]){
            ans.push_back(to_string(current_owner+1)+" "+to_string(true_owner+1));
            f[true_owner_bag] = current_owner;
            f[current_bag] = true_owner;
            p[current_owner] = true_owner_bag;
            p[true_owner] = current_bag;
        }else{
            std::cout << -1 << std::endl;
            return 0;
        }
    }
    std::cout << ans.size() << std::endl;
    for (auto e : ans) {
        std::cout << e << std::endl;
    }
}

#include <bits/stdc++.h>
#define fir first
#define sec second
#define PB push_back
#define MP make_pair

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i < (int)(n); ++i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)

template <class T> void chkmax(T& x, T y) { x = (x > y ? x : y); }
template <class T> void chkmin(T& x, T y) { x = (x < y ? x : y); }

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

const int N = 2e5 + 5;

int n, a[N], b[N];
int p[N], pos[N];
bool vis[N];

vi d[N];
int kd;

void dfs(int now){
    vis[now] = 1;
    d[kd].PB(now);
    if (!vis[p[now]]) dfs(p[now]);
}

bool cmp(int x, int y){
    return a[x] < a[y];
}

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d", &a[i]);
    rep(i, n) scanf("%d", &b[i]);
    rep(i, n){
        scanf("%d", &p[i]), p[i]--;
        pos[p[i]] = i;
    }
    rep(i, n) if (i != p[i] && a[i] <= b[p[i]]){
        printf("-1\n");
        return 0;
    }

    int cnt = 0;
    rep(i, n) if (!vis[i]){
        dfs(i);
        if (d[kd].size() > 1) kd++;
        else d[kd].clear(), cnt++;
    }

    rep(i, kd) sort(d[i].begin(), d[i].end(), cmp);

    printf("%d\n", n - cnt - kd);
    rep(i, kd){
        rep(j, d[i].size() - 1){
            printf("%d %d\n", d[i][j] + 1, pos[d[i][j]] + 1);
            pos[p[d[i][j]]] = pos[d[i][j]];
            p[pos[d[i][j]]] = p[d[i][j]];
        }
    }
    return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
const int maxn=20;
typedef long long ll;
ll f[1<<maxn][maxn],dis[maxn][maxn];
int n,x[maxn],y[maxn],z[maxn],mmax;
int main(){
	memset(f,0x3f,sizeof(f));
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&x[i],&y[i],&z[i]);
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			dis[i][j]=std::abs(x[i]-x[j])+std::abs(y[i]-y[j])+std::max(z[j]-z[i],0);
		}
		f[1|(1<<(i-1))][i]=dis[1][i]+dis[i][1];
	}
	mmax=(1<<n)-1;
	for(int i=1;i<=mmax;i++){
		for(int j=1;j<=n;j++){
			if(i&(1<<(j-1))){
				for(int k=1;k<=n;k++){
					if(i&(1<<(k-1))) continue;
					f[i|(1<<(k-1))][k]=std::min(f[i^(1<<(k-1))][k],f[i][j]+dis[j][k]);
					f[i|(1<<(k-1))][k]=std::min(f[i^(1<<(k-1))][k],f[i][j]+dis[1][k]+dis[j][1]);
				}
			}
		}
	}
	long long ans=0x3f3f3f3f3f3f3f3f;
	for(int i=1;i<=n;i++){
		ans=std::min(ans,f[mmax][i]+dis[i][1]);
	}
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}

int main(){
    ll n;cin>>n;
    vi x(n),y(n),z(n);
    rep(i,n)cin>>x[i]>>y[i]>>z[i];
    vvi dp(n,vi(1<<n,inf));
    dp[0][0]=0;
    SPQ(PP) pq;
    pq.push(PP(0,0,0));
    while(!pq.empty()){
        auto t=pq.top();pq.pop();
        ll cost,i,bit;tie(cost,i,bit)=t;
        if(cost!=dp[i][bit])continue;
        rep(j,n)if(chmin(dp[j][bit|(1<<j)],cost+abs(x[i]-x[j])+abs(y[i]-y[j])+max(0ll,z[j]-z[i])))pq.push(PP(dp[j][bit|(1<<j)],j,bit|(1<<j)));
    }
    out(dp[0][(1<<n)-1]);
}