#include <bits/stdc++.h>

typedef long long int ll;
typedef long double ld;
using namespace std;
#define P 1000000007
#define rep(i,n) for(i=0;i<n;++i)
#define re(i,a,n) for(i=a;i<=n;++i)
#define repr(i,a,n) for(i=a;i>=n;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define mset(a,val) memset(a,val,sizeof(a));
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
#define PI 3.14159265
#define block_size ((ll)sqrt(3e5) + 1)
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
// Important header files
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;*/

const int maxn = 1<<18 ;
ll dp[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   ll t,i,j,l,r,f,e,x,y,mid,z,w,k,d,c,n,a,b,m;
   ll t1,t2,t3,ans=0,tc ;
   t=1;
   cin >> n >> m ;
   vector<pair<ll,pair<ll,ll>>> co(m);
   rep(i,m)
   {
      cin >> co[i].fi >> co[i].se.fi >> co[i].se.se ;
   }
   sortv(co);
   x=1<<n;

   mset(dp,0);
   dp[0]=1;

   re(i,0,x-2)
   {

      ll mx=0;
      //cout << i << endl ;
      f=0;
      rep(j,m)
      {
          t1=0;
          t2=__builtin_popcount(i);
          if(t2>=co[j].fi) continue;
          rep(k,n)
          {
            y=1<<k;
            if((y&i)!=0)
            {
              //cout << k << " " ;
              if(k<co[j].se.fi) t1++;
            }
          }
          if(t1==co[j].se.se) mx=max(mx,co[j].se.fi);
          if(t1>co[j].se.se) f=1;
      }

      //cout << endl ;
      //cout << i << " " ;
      //cout << mx << endl ;
      if(f) continue;
      for(j=mx;j<n;++j)
      {
        y=1<<j;
        //cout << j << endl ;
        if(y&i) continue;
        dp[i|y]+=dp[i];

      }

   }
   //rep(i,x) cout << dp[i] << " " ;

   cout << dp[x-1] << endl ;

  return 0;

}

#include "bits/stdc++.h"
#include<sstream>
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define NIL = -1;
#define all(x) x.begin(),x.end()
const ll INF = 1e9;
const long long inf = 1e18;
const ll INFL = 1e18;
const ll MOD = 1e9 + 7;

int digit(ll x) {
    int digits = 0;

    while(x > 0){
        x /= 10;
        digits++;
    }

    return digits;
}

ll gcd(long long a,long long b) {
    if (a < b) swap(a,b);
    if (b == 0) return a;

    return gcd(b,a%b);
}

bool is_prime(long long N){
    if (N == 1) return false;
    for (long long i = 2;i * i <= N;i++){
        if (N % i == 0) return false;
    }
    return true;
}

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

double DegreeToRadian(double degree){
    return degree * M_PI / 180.0;
}

long long modpow(long long a, long long b, long long m){
    long long ans = 1;
    while(b > 0){
        if (b % 2 == 1){
            ans *= a;
            ans %= m;
        }
        a *= a;
        a %= m;
        b /= 2;
    }
    return ans;
}

long long comb(long long x, long long y){
    int z;
    if (y == 0) return 1;
    else {
        z = modpow(x, y/2, MOD)*modpow(x, y/2, MOD)%MOD;
        if (y % 2 == 1) z = z*x%MOD;
        return z;
    }
}

vector<pair<long long, long long>> prime_fact(long long x){
    vector<pair<long long, long long>> res;
    for(int i = 2;i*i <= x;i++){
        if (x % i == 0){
            long long cnt = 0;
            while(x % i == 0){
                cnt++;
                x /= i;
            }
            res.push_back({i, cnt});
        }
    }
    if (x != 1){
        res.push_back({x, 1});
    }
    return res;
}

int64_t mod_inv(int64_t a, int64_t m){
    int64_t b = m, u = 1, v = 0;

    while(b){
        int64_t t = a/b;
        a -= t*b;
        swap(a, b);
        u -= t*v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;

    return u;
}

// {g, x, y}: ax + by = g
tuple<long long, long long, long long> extgcd(long long a, long long b){
    if (b == 0) return {a, 1, 0};
    long long g, x, y;
    tie(g, x, y) = extgcd(b, a % b);
    return {g, y, x - a/b*y};
}

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

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

int main(){
    int n,k;
    cin >> n >> k;
    for(int i = 0;i < k;i++){
        string tmp = to_string(n);
        sort(tmp.begin(), tmp.end());
        int mini = stoi(tmp);
        sort(tmp.begin(), tmp.end(), greater<>());
        int mx = stoi(tmp);
        n = mx - mini;
    }
    cout << n << endl;
}


#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define cat(x) cerr << #x << " = " << x << endl

using ll = long long;
using namespace std;

const int MOD = 1e9 + 7;

ll bin_pow(ll a, ll b) {
	ll c = 1;
	while (b) {
		if (b & 1) c = c * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return c;
}

int n, m, a, s;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 0; i < n; ++i) {
		cin >> a;
		s += a;
	}
	int x = n + m - 1 + 1;
	int y = s + n - 1 + 1;
	//~ cout << x << " " << y << endl;
	if (x < y) {
		cout << 0 << endl;
	}
	else {
		ll res = 1;
		for (int i = 1; i <= y; ++i)
			res = res * bin_pow(i, MOD - 2) % MOD * (x - i + 1) % MOD;
		cout << res << endl;
	}
	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>

using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll modq=998244353;
const ll INF=1e12;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}

ll modpow(ll a,ll n) {
    ll res = 1;
    while (n>0) {
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}

// a^{-1} mod を計算する
ll modinv(ll a) {
    return modpow(a, mod - 2);
}

ll nCk(ll n,ll k){
    ll nck=1;
    rep(i,k){
        nck=(nck*(n-i))%mod;
        nck=(nck*modinv(k-i))%mod;
    }
    return nck;
}

int main(void){
    ll N,M,Asum=0;
    cin>>N>>M;
    rep(i,N){
        ll A;
        cin>>A;
        Asum+=A;
    }
    if(M<Asum){
        cout<<0<<endl;
        return 0;
    }
    cout<<nCk(M+N,N+Asum)<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define real long double
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pi M_PI
#define elif else if
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const int INF = 2e18, MOD = 998244353, RANDOM = chrono::steady_clock::now().time_since_epoch().count();
const real EPS = 1e-12;
mt19937 rng(RANDOM);
struct chash {

    int operator() (int x) const { return (x ^ RANDOM) % MOD; }
};

int n;
vector<vector<int>> people;
vector<int> ind;
vector<pair<int, int>> answer;

int search(){
    int cnt = 0;
    for (int i=0; i<n - 1; ++i){
        if (people[i][2] == people[i][3]) continue;
        if (people[i][1] >= people[i][0]) return -1;
        int j = ind[people[i][3]];
        if (people[j][1] >= people[j][0]) return -1;
        ind[people[i][2]] = j;
        ind[people[i][3]] = people[i][3];
        swap(people[i][1], people[j][1]);
        swap(people[i][2], people[j][2]);
        answer.push_back({people[i][3], people[j][3]});
        ++cnt;
    }
    return cnt;
}

void debug(){
    cerr << "\n";

    cerr << "\n";
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // cout.precision(6);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    cin >> n;
    people.assign(n, vector<int>(4, 0));
    ind.assign(n, 0);
    vector<int> b(n);
    for (int i=0; i<n; ++i) cin >> people[i][0];
    for (int i=0; i<n; ++i) cin >> b[i];
    for (int i=0; i<n; ++i){
        cin >> people[i][2];
        --people[i][2];
        people[i][1] = b[people[i][2]];
        people[i][3] = i;
    }
    sort(all(people));
    for (int i=0; i<n; ++i) ind[people[i][2]] = i;
    int res = search();
    cout << res << "\n";
    if (res != -1) for (auto i: answer) cout << i.f + 1 << " " << i.s + 1 << "\n";
}

#include<bits/stdc++.h>
using namespace std;
int n,a[200100],b[200100],p[200100],q[200100];
pair<int,int> pa[200100];
vector<pair<int,int> > vec;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],pa[i]=make_pair(a[i],i);
	for(int i=1;i<=n;i++) cin>>b[i];
	for(int i=1;i<=n;i++) cin>>p[i],q[p[i]]=i;
	for(int i=1;i<=n;i++) if(a[i]<=b[p[i]]&&p[i]!=i){
		cout<<-1<<endl;
		return 0;
	}
	sort(pa+1,pa+n+1);
	int res=0;
	for(int ii=1;ii<=n;ii++){
		int i=pa[ii].second;
		if(p[i]==i) continue;
		res++;
		int j=q[i];
		q[p[i]]=j;q[i]=i;
		p[j]=p[i];p[i]=i;
		vec.push_back(make_pair(i,j));
	}
	cout<<res<<endl;
	for(int i=0;i<res;i++) cout<<vec[i].first<<' '<<vec[i].second<<endl;
	return 0;
}


#include <math.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using Graph = vector<vector<int>>;

using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;

int main() {
  int N;
  cin >> N;
  if (N >= 0) {
    cout << N << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}

/*
* @author : ashnove
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define nl cout<<endl;
#define pb push_back
#define f(i,a,b) for(ll i=a;i<b;i++)
#define Knucklehead ios_base::sync_with_stdio(false);cin.tie(NULL);
//#define mod 100000007
#define mat vector<vector<ll>>
mat cn(ll n, ll m){return vector< vector<ll> >(n, vector<ll>(m));}
bool compare(char &s1,char &s2){return s1 > s2;}
bool sortmahstyle(const pair<ll,ll> &a,  const pair<ll,ll> &b)
{
    if(a.first < b.first )
        return true;
    if( a.first==b.first && a.S != b.second )
       return true;
    return false;
}
//me
ll const mod = 998244353;
ll const inf = 1e18;
ll const maxn = 1e5+1;

void solve()
{
      ll x; cin >> x;

      cout << (x>=0?x:0);
}

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

          ll t;
          /**/t=1;
          /*/cin>>t;/**/
          f(testcases,0,t){
               //cout << "Case #" <<testcases+1<<": ";
               solve();
          }

    return 0;
}


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

using namespace std;

const int N = 200000;

int aa[N << 1], bb[N], ii[N << 1];
long long dd[N << 1];

int main() {
	int n; cin >> n;
	long long b_; cin >> b_;
	for (int i = 0; i < n; i++)
		cin >> aa[i << 1 | 0] >> aa[i << 1 | 1] >> bb[i], aa[i << 1 | 1]++;
	for (int h = 0; h < n << 1; h++)
		ii[h] = h;
	sort(ii, ii + (n << 1), [] (int i, int j) { return aa[i] < aa[j]; });
	map<int, int> mp;
	int a_ = 0;
	for (int h = 0; h < n << 1; h++) {
		int a = aa[ii[h]];
		mp[a] = h + 1 == n << 1 || aa[ii[h + 1]] != a ? a_++ : a_;
	}
	for (int i = 0; i < n; i++)
		dd[mp[aa[i << 1 | 0]]] += bb[i], dd[mp[aa[i << 1 | 1]]] -= bb[i];
	for (int a = 1; a < a_; a++)
		dd[a] += dd[a - 1];
	set<int> s;
	for (int i = 0; i < n << 1; i++)
		s.insert(aa[i]);
	long long ans = 0;
	while (s.size() >= 2) {
		int a = *s.begin(); s.erase(s.begin());
		int b = *s.begin();
		ans += min(dd[mp[a]], b_) * (b - a);
	}
	ans += min(dd[mp[*s.begin()]], b_);
	cout << ans << '\n';
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int a[200005], b[200005];
pair <int, int> masA[200005], masB[200005];
int perm[200005], fen[200005];

int f(int x)
{
 	return (x & (-x));
}

int get_sum(int pos, int n)
{
 	int s = 0;
 	for (int i = pos; i <= n; i += f(i)) s += fen[i];
 	return s;
}

void update(int pos, int n)
{
 	for (int i = pos; i > 0; i -= f(i)) fen[i]++;
 	return;
}

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int n;
 	cin >> n;
 	for (int i = 1; i <= n; i++)
 	{
 		cin >> a[i];
 		masA[i] = mp(a[i] + i, i);
   	}
 	for (int i = 1; i <= n; i++)
 	{
 		cin >> b[i];
 		masB[i] = mp(b[i] + i, i);
 	}
	sort(masA + 1, masA + n + 1);
	sort(masB + 1, masB + n + 1);

	bool fl = true;
	for (int i = 1; i <= n; i++)
		if (masA[i].x != masB[i].x) fl = false;

	if (!fl) cout << "-1\n", exit(0);

	for (int i = 1; i <= n; i++) perm[masA[i].y] = masB[i].y;
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	{
	 	ans += get_sum(perm[i], n);
	 	update(perm[i], n);
	}
	cout << ans << '\n';
 	return 0;
}


/*** keep hungry and calm CoolGuang!  ***/
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define d(x) printf("%lld\n",x);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF= 1e18;
const ll maxn = 4e5+700;
const int mod= 1e9+7;
const int up = 1e9;
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
ll n,m,p;
ll dp[1005][1005];
ll a[maxn],b[maxn];
int main(){
    read(n);read(m);
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=1;i<=m;i++) read(b[i]);
    for(int i=1;i<=n;i++)
        for(int k=1;k<=m;k++)
            dp[i][k] = INF;
    dp[0][0] = 0;
    for(int i=1;i<=n;i++) dp[i][0] = i;
    for(int k=1;k<=m;k++) dp[0][k] = k;
    for(int i=1;i<=n;i++){
        for(int k=1;k<=m;k++){
            dp[i][k] = min(dp[i-1][k]+1,dp[i][k-1]+1);
            dp[i][k] = min(dp[i][k],dp[i-1][k-1]+(a[i]!=b[k]));
        }
    }
    /*for(int i=1;i<=n;i++){
        for(int k=1;k<=m;k++){
            printf("%lld ",dp[i][k]);
        }
        printf("\n");
    }*/
    ll ans = INF;
    for(int i=0;i<=n;i++){
        for(int k=0;k<=m;k++){
            ans = min(ans,dp[i][k] + n-i + m-k);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
/***
2 4
2 3
1 1 2 3
***/

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

using namespace std;
using ll = long long;

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

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

vector<vector<ll>> dp;

int main() {
	const int INF = 1001001001;
	int n, m;
	cin >> n >> m;
	vector<int> a(n), b(m);
	rep(i, n) cin >> a[i];
	rep(j, m) cin >> b[j];
	dp = vector<vector<ll>>(n + 1, vector<ll>(m + 1, INF));
	dp[0][0] = 0;
	rep(i, n + 1) rep(j, m + 1) {
		if (i < n) chmin(dp[i + 1][j], dp[i][j] + 1);
		if (j < m) chmin(dp[i][j + 1], dp[i][j] + 1);
		if (i < n && j < m) {
			int co = 0;
			if (a[i] != b[j]) co = 1;
			chmin(dp[i + 1][j + 1], dp[i][j] + co);
		}
	}
	cout << dp[n][m] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, s[N], e[N], tim = 1;
vector<int> adj[N];
vector<int> lvl[N];
int cnt(int idx, int l, int r)
{
    return upper_bound(lvl[idx].begin(), lvl[idx].end(), r) - lower_bound(lvl[idx].begin(), lvl[idx].end(), l);
}
void dfs(int u = 1, int l = 0)
{
    s[u] = tim;
    lvl[l].push_back(s[u]);
    for (int v: adj[u])
    {
        tim ++;
        dfs(v, l + 1);
    }
    e[u] = tim;
}
int main(int argc, char const *argv[])
{
    cin >> n;
    for (int i = 2; i <= n; ++i)
    {
        int p;
        cin >> p;
        adj[p].push_back(i);
    }
    dfs();
    int q;
    cin >> q;
    while (q --)
    {
        int u, v;
        cin >> u >> v;
        cout << cnt(v, s[u], e[u]) << '\n';
    }
    return 0;
}

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

/*
https://atcoder.jp/contests/abc202/tasks/abc202_e

## 關鍵詞：

## 題意：
一個由N <= 2e5個節點組成的樹。樹根爲頂點1。
給出以下Q <= 2e5個問題：
給定U_i, D_i，求所有深度爲D_i且到達根節點的最短路徑包含U_i的節點數量。

## 思路：
本來的思路：用一個集合記錄每個節點的最短路徑上的頂點，但這樣一定會超時……

https://atcoder.jp/contests/abc202/editorial/1864
用DFS從根節點遍歷整棵樹，同時記錄每個頂點進入和退出的順序，in_i和out_i。
「x到root最短路徑包含y」 a.k.a 「x是y的子孫」的充分必要條件是：
「in_y <= in_x <= out_x <= out_y」，實際上只需要關注in_x即可。
用一個數組按照深度保存節點，並且按照in的順序從小到大排序。
實際求數量只需做兩次二分。

## 雜多：
1. 學到了如何用O(N)的預處理來在O(1)時間內解「x是否是y的祖先」這個問題。
2. 根據題解的思路，
*/

void solve(){
    int n;
    #define maxn 200005
    int in[maxn], out[maxn], depth[maxn]={0};
    ivector son[maxn];
    ivector dvector[maxn]; dvector[0] = {1};

    // 讀取
    cin>>n;
    range(i,2,n-1) {
        int p; cin>>p; son[p].push_back(i);
    }

    // dfs遍歷
    auto dfs = [&](auto self, int ith, int &cnt) -> void {
        in[ith] = cnt++;
        foreach(v,son[ith]) {
            depth[v] = depth[ith] + 1;
            dvector[depth[v]].push_back(v);
            self(self, v, cnt);
        }
        out[ith] = cnt++;
    };
    int cnt = 0;
    dfs(dfs, 1, cnt);

    // 對所有dvector[i]按照in的順序排序
    range(i,0,maxn) {
        sort(ALL(dvector[i]), [&](const int &a, const int &b) {
            return in[a] < in[b];
        });
    }

    // 在[l,r)範圍內，尋找是check成立的最小下標
    auto lower = [&](int l, int r, auto check) -> int {
        while(l<r) {
            int m = l + (r-l) / 2;
            if(!check(m)) l = m+1;
            else r = m;
        }
        return l;
    };

    // 在[l,r)範圍內，尋找是check不成立的最小下標
    auto upper = [&](int l, int r, auto check) -> int {
        while(l<r) {
            int m = l + (r-l) / 2;
            if(check(m)) l = m+1;
            else r = m;
        }
        return l;
    };

    int q; cin>>q;
    while(q--) {
        int u,d; cin>>u>>d;
        if(depth[u]>d) {cout<<0<<endl; continue;}

        // 在x \in dvector[d]當中，尋找所有滿足 in[u] <= in[x] <= out[u]的節點
        auto pl = lower(0, dvector[d].size(), [&](int ith) {
            return in[dvector[d][ith]] >= in[u];
        });
        auto pr = upper(0, dvector[d].size(), [&](int ith) {
            return in[dvector[d][ith]] <= out[u];
        });
        cout<<pr-pl<<endl;
    }
}

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


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

bool check(string iStr,string jStr){

   bool retval = false;
   for(int i=0;i<iStr.length();i++){
           //std::cout << iStr[i] << "  " << jStr[i] << endl;

       if(iStr[i] != jStr[i])
         retval = ! retval;
   }

   return retval;
}

int main(void)
{
    int64_t N;
    cin >> N;
    int64_t M;
    cin >> M;
    vector<string> S(N);
    for(int i = 0; i < N; i++){
         cin >> S[i];
    }

    vector<bool> checkVal(N);
    int64_t checkCount = 0;
    for(int i=1;i<N;i++){
        if(check(S[0],S[i])){
            checkVal[i] = true;
            checkCount++;
        }
        else{
            checkVal[i] = false;
        }
    }

    // for(int i=1;i<N;i++){
    // std::cout << "check :" << checkVal[i] << endl;
    // }

    int64_t ans = 0;
    ans += checkCount;
    for(int i=1;i<N;i++){
        if(checkVal[i]== false){
           ans += checkCount;
        }else{
            checkCount--;
            int tmp = N-i-1-checkCount;
            ans += tmp;
    //std::cout << " tmp :" << tmp << " N :" << N << " i :" << i << " checkCount :" << checkCount<< endl;
        }
    //std::cout << ans << endl;
    }

    std::cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
#define N 2010
#define pp pair<int,int>
#define ppp pair<pp,int>
#define M 26
#define fi first
#define si second
using namespace std;
int n, m, x, y, u, v, l;
char dp[N][N];
bool vis[N][N];
queue<ppp>q;
vector<pp>g[M];
ppp tmp;
int s[4][2] = {{ -1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool isValid(int x, int y) {
	return (x >= 0 && y >= 0 && x < n && y < m && dp[x][y] != '#');
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout.precision(10);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			cin >> dp[i][j];
			if (dp[i][j] == 'S') {
				vis[i][j] = true;
				q.push(make_pair(make_pair(i, j), 0));
			}
			if (dp[i][j] >= 'a' && dp[i][j] <= 'z') {
				g[dp[i][j] - 'a'].push_back(make_pair(i, j));
			}
		}
	while (!q.empty()) {
		tmp = q.front();
		q.pop();
		l = tmp.si;
		u = tmp.fi.fi;
		v = tmp.fi.si;
		if (dp[u][v] == 'G') {
			cout << l << endl;
			return 0;
		}
		if (dp[u][v] >= 'a' && dp[u][v] <= 'z') {
			for (int i = 0; i < g[dp[u][v] - 'a'].size(); i++) {
				x = g[dp[u][v] - 'a'][i].fi;
				y = g[dp[u][v] - 'a'][i].si;
				if (!vis[x][y]) {
					if (dp[x][y] == 'G') {
						cout << l + 1 << endl;
						return 0;
					}
					q.push(make_pair(make_pair(x, y), l + 1));
					vis[x][y] = true;
				}
			}
		}
		for (int i = 0; i < 4; i++) {
			x = s[i][0] + u;
			y = s[i][1] + v;
			if (isValid(x, y) && !vis[x][y]) {
				if (dp[x][y] == 'G') {
					cout << l + 1 << endl;
					return 0;
				}
				q.push(make_pair(make_pair(x, y), l + 1));
				vis[x][y] = true;
			}
		}
	}
	cout << -1 << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <initializer_list>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <time.h>
#include <limits.h>
#define rep(unt) for (int i = 0; i < unt; ++i)
#define each(var, st) for (auto var : st)
#define all(x) (x).begin(), (x).end()
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pb push_back
#define ins insert
#define db double
#define ldb long double
#define pll pair<long long, long long>
#define ci const int
#define ll long long
#define INF 1000000007
#define LLINF 1000000000000000007
#define rt return
using namespace std;

mt19937 rng(time(0));
uniform_int_distribution<int> uid(-INF, INF);
#define int long long
struct v3 {
    int x=0,y=0,z=0;
    v3(int x, int y, int z): x(x), y(y), z(z) {}
};

void solve(bool debug=false) {

    int res = 0;
    int n, k; cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= k; ++j) {
            res += i * 100 + j;
        }
    }
    cout << res;

}

template <size_t SZ, class T>inline void clear1dArray(T(&st)[SZ],T fillValue)
{rep(SZ)st[i]=fillValue;}
template<size_t SZn,size_t SZm,class T>inline void clear2dArray(T(&st)[SZn][SZm],T fillValue)
{rep(SZn)for(int j=0;j<SZm;++j)st[i][j]=fillValue;}
void clear() {
}
signed main(signed argc,char* argv[]) {
    if (argc==2&&strcmp(argv[1],"__DEBUG__")==0){
        int t=0;cin>>t;
        string ph;
        getline(cin, ph);
        for(int i=1; i<=t;++i){
            cout << "Case #" << i << ":" << "\n";
            solve(true);
            cout << "\n";
            clear();
        }
    }
    else solve();
    return 0;
}

#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int N,M;
    cin >> N >> M;

    int sum = 0;
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= M; j++){
            sum += i*100 + j;
        }
    }

    cout << sum << endl;
}


/*
ID:ACwisher
LANG:C++
TASK:
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200010;
int n,x[N],y[N],fa[N<<1],e[N<<1],sum[N<<1],sumn[N<<1];
bool vis[N<<1];
int find(int x){ return (x==fa[x])?x:fa[x]=find(fa[x]); }
void merge(int x,int y){ x=find(x); y=find(y); if(x!=y) fa[x]=y; }
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdiout);
	scanf("%d",&n);
	for(int i=1;i<=400000;i++) fa[i]=i;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&x[i],&y[i]);
		merge(x[i],y[i]); vis[x[i]]=vis[y[i]]=1;
	}
	for(int i=1;i<=400000;i++)
		e[i]=find(i),sumn[e[i]]++;
	for(int i=1;i<=n;i++) sum[e[x[i]]]++;
	int ans=0;
	for(int i=1;i<=400000;i++)
		if(vis[i]&&e[i]==i)
			ans+=min(sum[i],sumn[i]);
	printf("%d\n",ans);
	return 0;
}

#include<bits/stdc++.h>

#define IOS ios::sync_with_stdio(false);cin.tie(nullptr)
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;

const ll LINF=1e18;
const ll MOD=1e9+7;
const int INF=0x3f3f3f3f;
const int MAXN=400050;

int a[MAXN],b[MAXN],fa[MAXN],cyc[MAXN];
int find(int x){
	return fa[x]==x?x:(fa[x]=find(fa[x]));
}
void merge(int x,int y){
	int fax=find(x),fay=find(y);
	fa[fay]=fax;
}
void solve(int T){
	int n;
	scanf("%d",&n);
	for(int i=1;i<MAXN;i++){
		fa[i]=i;
		cyc[i]=0;
	}
	int u,v,fau,fav;
	int ans=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&u,&v);
		fau=find(u),fav=find(v);
		if(fau==fav){
			if(cyc[fau]==0){
				cyc[fau]=1;
				ans++;
			}
		}else if(cyc[fau]==0||cyc[fav]==0){
			ans++;
			fa[fav]=fau;
			cyc[fau]|=cyc[fav];
		}
	}
	printf("%d\n",ans);
}
signed main(){
	//IOS;
	int t=1;
	//scanf("%d",&t);
	for(int i=1;i<=t;i++){
		solve(i);
	}
}


/**
 *    author:  Anchord
 *    created: 11.12.2020
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long ans = 1;
  for(long long i = 2; i <= n; ++i){
   ans = (ans*i) / __gcd(ans, i);
  }
  ans++;
  cout << ans << '\n';
  return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
using ll = long long ;
#define rep(i,n) for (int i=0; i < (n); ++i)

const ll lim = 1e18 ;

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

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

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

    ll ans ;
    ans = 2 ;

    for (ll i = 3; i <= n; i++)
    {
        ans = lcm(i,ans) ;
    }
    cout << ans + 1 << endl ;
 return 0 ;
}


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

vector<vector<ll>> calc_ncr(const int n){
    vector<vector<ll>> ret(n+1, vector<ll>(n+1, 0));
    for(int i=0; i<=n; ++i){
        ret[i][0] = 1;
        for(int j=1; j<i; ++j){
            ret[i][j] = ret[i-1][j] + ret[i-1][j-1];
        }
        ret[i][i] = 1;
    }
    return ret;
}

ll solve_naive(const int n, const int m, const vector<int> &a){
    const auto ncr = calc_ncr(m);
    if(n==3){
        ll ret = 0;
        for(int b0=a[0]; b0<=m-2; ++b0){
            for(int b1=a[1]; b0+b1<=m-1; ++b1){
                for(int b2=a[2]; b0+b1+b2<=m; ++b2){
                    ret += ncr[b0][a[0]] * ncr[b1][a[1]] * ncr[b2][a[2]];
                }
            }
        }
        return ret;
    }else{
        return 0;
    }
}

ll inv(ll a, ll b = MOD){
    ll x0 = 1, y0 = 0, x1 = 0, y1 = 1;
    while (b != 0) {
        const ll q = a / b;
        ll tmp = a % b;
        a = b;
        b = tmp;
        tmp = x0 - q * x1;
        x0 = x1;
        x1 = tmp;
        tmp = y0 - q * y1;
        y0 = y1;
        y1 = tmp;
    }
    return (x0 + MOD) % MOD;
}

vector<ll> calc_ncr_n(const ll n, const ll r_max){
    vector<ll> ret(r_max+1);
    ret[0] = 1;
    for(ll r=1; r<=r_max; ++r){
        ret[r] = ((ret[r-1] * (n - r + 1)) % MOD * inv(r)) % MOD;
    }
    return ret;
}

ll solve(const int n, const int m, const vector<int> &a){
    ll sum_a = 0;
    for(int i=0; i<n; ++i)sum_a += a[i];
    const auto ncr = calc_ncr_n(m+n, sum_a+n+1);
    return ncr[sum_a+n];
}

int main()
{
    ll n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i=0; i<n; ++i)cin >> a[i];
    //cout << solve_naive(n, m, a) << endl;
    cout << solve(n, m, a) << endl;
}

#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define endl "\n"
using namespace std;

const int Mod = 1e9+7;

int power(int a,int b){
	if(b==0) return 1;
	int temp = power(a,b/2)%Mod;
	if(b%2==0){
		return (temp*temp)%Mod;
	}else{
		return (a*((temp*temp)%Mod))%Mod;
	}
}

int nCr(int n,int r){
	if(r>n) return 0;
	int num=1;
	for(int i=n;i>=(n-r+1);i--){
		num=(num*i)%Mod;
	}
	int den=1;
	for(int i=1;i<=r;i++){
		den=(den*i)%Mod;
	}
	return (num*power(den,Mod-2))%Mod;
}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n,m;
	cin>>n>>m;
	int a[n];
	int sum=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	if(sum>m){
		cout<<0<<endl;
		return 0;
	}

	int ans=1;
	int options=1;
	for(int i=0;i<n;i++){
		if(a[i]==0){
			options++;
			continue;
		}
		for(int j=1;j<=a[i];j++){
			if(j==a[i]){
				options+=2;
			}else{
				options+=1;
			}
		}
	}
	ans = nCr(m-sum+options-1,options-1);
	cout<<ans<<endl;

}


#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

//n=1ならsecond
//n odd 先手はxorを0にしたい
//n even 先手はxor!=0にしたい

void solve(){
  ll n;scanf("%lld", &n);
  vector<ll> a(n);
  re(i, n) scanf("%lld", &a[i]);
  if(n%2==0){
    sort(all(a));
    bool f = true;
    for(int i=0;i<n;i+=2){
      if(a[i]!=a[i+1]){
        f = false;
        break;
      }
    }
    //全要素偶数個
    if(f){
      std::cout << "Second" << '\n';
      return;
    }else{
      std::cout << "First" << '\n';
      return;
    }
  }else{
    std::cout << "Second" << '\n';
    return;
  }
}
int main(){
  ll T;std::cin >> T;
  re(i, T) solve();
}

// dont stop until you reach there
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define rep(i,j,k) for(int i=j;i<k;i+=1)
#define rrep(i,j,k) for(int i=n-1;i>=0;i-=1)
#define sorti(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<int>())
#define mm map<int,int>
#define vv vector<int>
#define tmm map<int,map<int,int>>
#define trav(a, x) for(auto& a : x)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define pb push_back
#define alot(x,y) memset(x,y,sizeof(x))
#define pii pair<int,int>
#define code ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD=1e9+7;
const double pi=acos(-1.0);
//memset(array_name,value_to_be_intialized,sizeof(array_name));
// __builtin_popcount(n) count no of set bit in number n
//next_permutation(s.begin(),s.end());
/*int change(string g){
     int p=10;
     int ans=g[g.length()-1]-'0';
     for(int i=g.length()-2;i>=0;i--){
         int j=g[i]-'0';
         ans=p*ans+j;
     }
     return ans;
}
int binpow(int a,int b){
    int res=1;
    while(b>0){
        if(b&1)res*=a;
        a*=a;
        b>>=1;

    }
    return res;
}
// lower_bound==> if the element is present it return its first occurence else 1st element which is greater than
// upper_bound==> it will always return iterator to element which is greater than searched element
*/
int32_t main ()
{
     code;
    int tt;
    //cin>>tt;
    tt=1;
    while(tt--){
        int n,m;
        cin>>n>>m;
        vv a;
        vv b;
        rep(i,0,n){
            int p;
            cin>>p;
            a.pb(p);

        }
        rep(i,0,n){
            int p;
            cin>>p;
            b.pb(p);
        }
        int sum1=0,sum2=0;
        set<int>st;
        rep(i,0,m){
            int p,q;
            cin>>p>>q;
            p--;
            q--;
            st.insert(p);
            st.insert(q);
        }
        trav(x,st){
            sum1+=a[x];
            sum2+=b[x];
        }
        if(m==0){
            if(a==b)cout<<"Yes\n";
            else cout<<"No\n";
        }
        else if(sum1==sum2)cout<<"Yes\n";
        else cout<<"No\n";

    }
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define fastIO std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0)
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A>
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 100 + 10;

int n, a[N];

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

	int ans = 0, num = 0;

	rep (i,2,1001) {
		int cnt = 0;
		rep (j,0,n) {
			if (a[j] % i == 0) cnt++;
		}
		if (cnt > num) {
			num = cnt;
			ans = i;
		}
	}

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll,ll> P;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define EFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,I) for(const auto &(i):(I))
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ll(x.size())
#define INF32 2147483647
#define INF64 9223372036854775807
#define MOD 1000000007
#define PI acos(-1)
#define endl '\n'
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    ll n; cin >> n;
    ll i = 1, ans = 0;
    while(n > 0){
        n -= i;
        i++;
        ans++;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define ll long long
using namespace std;

#define MOD 1000000007
#define INF 100000000000000000

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

    int n;
    cin >> n;

    bool a[n];
    memset(a, false, sizeof(a));

    bool can = true;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        x--;
        if (a[x]) can = false;
        a[x] = true;
    }

    for (int i = 0; i < n; i++) {
        if (!a[i]) can = false;
    }

    if (can) cout << "Yes\n";
    else cout << "No\n";

}

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

#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a	b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")

int main()
{
int T;
	cin >> T;
	fx(T) {
		string S;
		cin >> S;
		bool a = true;
		fy(S.size()) {
			if (S[y] != 'a') { a = false; }
		}
		if (a) {
			ce(-1)
		}
		else if (S>"atcoder") {
			ce(0)
		}
		else {
			int ans = 0;
			while (S[ans] == 'a') { ans++; }
			if (ans>0&&S[ans] > 't') { ans--; }
			ce(ans)
		}

	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 1007, inf = 1e9;

char s[sz][sz];

int main() {
   int h, w;
   cin >> h >> w;
   int x, y;
   cin >> x >> y;
   x--, y--;

   for(int i=0; i<h; i++) scanf("%s", s[i]);

   if(s[x][y] == '#') {
      puts("0");
      return 0;
   }

   int ans = 1;
   for(int i=x-1; i>=0; i--) {
      if(s[i][y] == '#') break;
      ans++;
   }
   for(int i=x+1; i<h; i++) {
      if(s[i][y] == '#') break;
      ans++;
   }
   for(int i=y-1; i>=0; i--) {
      if(s[x][i] == '#') break;
      ans++;
   }
   for(int i=y+1; i<w; i++) {
      if(s[x][i] == '#') break;
      ans++;
   }
   printf("%d\n", ans);
}

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

int main(){
  int H,W,X,Y;
  cin >>H>>W>>X>>Y;
  vector<vector<char>> data(H,vector<char>(W));
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin >> data.at(i).at(j);
    }
  }
  int ans=0;
  for(int i=0;Y-1+i<W;i++){
    if(data.at(X-1).at(Y-1+i)=='#') break;
    ans++;
  }
  for(int i=0;Y-1-i>=0;i++){
    if(data.at(X-1).at(Y-1-i)=='#') break;
    ans++;
  }
  for(int i=0;X-1+i<H;i++){
    if(data.at(X-1+i).at(Y-1)=='#') break;
    ans++;
  }
  for(int i=0;X-1-i>=0;i++){
    if(data.at(X-1-i).at(Y-1)=='#') break;
    ans++;
  }

  cout << ans-3;

}


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
//    int x=0;
//    char ch=getchar();
//    while(ch<'0'||ch>'9'){
//        ch=getchar();
//    }
//    while(ch>='0'&&ch<='9'){
//        x=(x<<1)+(x<<3)+(ch^48);
//        ch=getchar();
//    }
//    return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int main(){
	cin>>n;
	string s,t;
	cin>>s>>t;
	LL rest=0;
	stack<int> sta;
	rl(i,n-1,0){
		if(s[i]=='1'){
			sta.push(i);
		}
		if(t[i]=='1'){
			if(sta.empty()){
				cout<<-1<<endl;;
				return 0;
			}
			if(sta.size()%2==0){
				int t=sta.top();
				sta.pop();
				rest+=sta.top()-i;
				sta.pop();
				sta.push(t);
			}
			else{
				rest+=sta.top()-i;
				sta.pop();
			}
		}
	}
	if(sta.size()&1){
		cout<<-1<<endl;
	}
	else{
		while(!sta.empty()){
			int a,b;
			a=sta.top();sta.pop();
			b=sta.top();	sta.pop();
			rest+=b-a;
		}
		cout<<rest<<endl;
	}
	return 0;
}
/** 程序框架：
  *
  *
  *
  *
  **/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = 1; i < (int)(n+1); ++i)
const int MAX = 510000;
const long long INF = 1LL<<60;
const int MOD = 10007;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
//二項係数ライブラリ
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数計算
long long COM(ll n, ll k) {
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    if (n == k) {
        return 1;
    }
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int gcd(ll N){
    ll ans=1;
    for(ll i=sqrt(N)+1; 0<i; i--){
        if(N%i==0){
            ans=i;
          	break;
        }
    }
    return ans;
}
//桁和
int digsum(ll N){
    bool a=true;
    ll ans=0;
    while(a==true){
        if(0<N){
            ans+=N%10;
            N-=N%10;
            N/=10;
        }
        else{
            a=false;
        }
    }
    return ans;
}
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 UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

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

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

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
// vector<vector<int>> A(3, vector<int>(4));
int main() {
    ll N;
    cin>>N;
    string T;
    cin>>T;
    ll e=3;
    rep(i,10){
        e*=10;
    }
    ll a=(e-N+1)/3;
    ll b=a;
    ll c=a;
    if((e-N+1)%3==1){
        a++;
    }
    else if((e-N+1)%3==2){
        a++;
        b++;
    }
    ll ans=0;
    vector<char>A(N);
    vector<char>B(N);
    vector<char>C(N);
    rep(i,N){
        if(i%3==0){
            A[i]='1';
            B[i]='1';
            C[i]='0';
        }
        else if(i%3==1){
            A[i]='1';
            B[i]='0';
            C[i]='1';
        }
        else{
            A[i]='0';
            B[i]='1';
            C[i]='1';
        }
    }
    bool d=true;
    bool G=true;
    bool f=true;
    rep(i,N){
        if(T[i]!=A[i]){
            d=false;
        }
        if(T[i]!=B[i]){
            G=false;
        }
        if(T[i]!=C[i]){
            f=false;
        }
    }
    if(d==true){
        ans+=a;
    }
    if(G==true){
        ans+=b;
    }
    if(f==true){
        ans+=c;
    }
    cout<<ans<<endl;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define V vector
#define debug(v) {for(auto i: v) cout << i << " "; cout << endl;}
#define vcin(v) rep(i, v.size()) cin >> v[i]
#define wcin(v, w) rep(i, v.size()) cin >>v[i] >>w[i]
ll MOD=1000000007;

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

    if(s[0]!=s[n-1]){
        cout <<1;
        return 0;
    }
    char c=s[0];

    for(ll i=1;i<n-1;i++){
        if(s[i]!=c&&s[i+1]!=c){
            cout <<2;
            return 0;
        }
    }

    cout <<-1;
}

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

typedef long long ll;

typedef   vector<ll>  vi;
typedef   vector<pair<ll, ll> > vii;
typedef   pair<ll,ll> pii;
typedef   multiset<pair<ll, ll> > msii;
typedef   multiset<ll> msi;
typedef   set<pair<ll, ll> > sii;
typedef   set<ll> si;
typedef   map<ll, ll> mii;

#define boost 	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define for1(x, a, b)  for(x=a; x<b; x++)
#define for2(x, a, b)  for(x=a, x>=b; x--)
//#define endl 	"\n"
#define all(x)  x.begin(), x.end()
#define p       pair
#define mp      make_pair
#define read    freopen("input.txt","r",stdin)
#define write   freopen("output.txt","w",stdout)
#define pb      push_back
#define ff      first
#define ss      second
#define bb      begin
#define arr_ub(arr, n, x) upper_bound(arr, arr+n, x)-arr
#define arr_lb(arr, n, x) lower_bound(arr, arr+n, x)-arr
#define u_p(v, x) upper_bound(v.begin(), v.end(), x)-v.begin()
#define l_b(v, x) lower_bound(v.begin(), v.end(), x)-v.begin()

const ll sz=1e6+123;
#define INF 1000000000000000007
#define MOD 1000000007
#define stringLen 18446744073709551620
#define pi 3.1415926536

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int n;
string s;

void dfs(){

}

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

    if(s[0]!=s[n-1]){
        cout << 1;
        return 0;
    }

    vector<int> st;
    for(int i=0; i<n-1; i++){
        if(s[i]==s[0]){
            if(s[i+1]!=s[i] ){
                st.pb(s[i]);
            }
        }else{
            st.pb(s[i]);
        }
    }
    st.pb(s[n-1]);

    bool ok = true;
    int nt = st.size();

    if(nt==1){
        cout<<-1;
        return 0;
    }

    if(nt%2 ==1){
        for(int i=2; i<nt; i+=2){
            if(st[i]!=st[0]){
                ok = false;
                break;
            }
        }
        if(ok){
            cout << -1;
            return 0;
        }
    }

    cout << 2;

    return 0;

    // vector<vector<bool>> Fs(n, vector<bool>(n,false));
    // int first = 0;
    // int init_length=1;
    // int last = first+init_length;
    // // while(init_length<n-2){
    //     int ans = 0;
    //     while(last<n){
    //         if (s[first]!= s[last] && !Fs[last][init_length]){
    //             ans++;
    //             Fs[last][init_length] = true;
    //             first = last+1;
    //             if(s[first]!=s[n-1]){
    //                 cout<<ans+1;
    //                 return 0;
    //             }
    //             last = first+1;
    //         }else{
    //             last++;
    //         }
    //     }
    //     if(first==n){
    //         cout << ans;
    //         return 0;
    //     }

        // init_length++;
        // first = 0;
        // last=first+init_length;
    //}
    // cout << -1;

    // return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#define N 1000005
using namespace std;

int n, s, ans, a[N], b[N], v[N];
vector<int> num;
int f(int p) {return p > 0 ? p : -p;}
int main()
{
    int i, j, k;
    cin >> n;
    for (i = 0, j = 1; i < n; i++, j *= 2);
    for (i = 1; i <= j; i++) {
        scanf ("%d", &a[i]);
    }
    for (i = 1; i <= n; i++) {
        num.clear();
        for (k = 1; k <= j; k++) {
            if (v[k]) continue;
            num.push_back(k);
            if (num.size() == 2) {
                if (a[num[0]] > a[num[1]]) {v[num[1]] = 1; ans = num[1];}
                else {v[num[0]] = 1; ans = num[0];}
                num.clear();
            }
        }
    }
    cout << ans;
    return 0;
}

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

int main()
{
    int n;
    cin>> n;
    int iter = pow(2,n) + .000001;
    //cout<< iter;
    vector<int>vec;
    for(int i = 1; i <= iter; i++)
    {
        int input;
        cin>> input;
        vec.push_back(input);
    }
    if(n == 1)
    {
        if(vec[0] > vec[1])
            cout<< 2;
        else
            cout<< 1;

    }
    else
    {

    vector<int>temp;
    temp = vec;
    int res;
    while(1)
    {
        //vector<int>temp;
        //temp = vec;
        vector<int>vec_tmp;
        for(int i = 0; i < temp.size(); i+=2)
        {
            vec_tmp.push_back(max(temp[i],temp[i+1]));
        }
        temp = vec_tmp;
        if(vec_tmp.size() == 2)
        {
            res = min(vec_tmp[0],vec_tmp[1]);
            //cout<< vec_tmp[0] << " " << vec_tmp[1] << "\n";
            break;
        }
    }

    for(int i = 0; i < vec.size(); i++)
    {
        if(vec[i] == res)
        {
            cout<< i+1;
            break;
        }
    }

    }

    return 0;
}


/*
	Auth: Loxilante
	Time: 2020/11/15
	Prog: A
	Lang: cpp
*/
#include <bits/extc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...) 97
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; }
signed main(void)
{
	clock_t Begin = clock();

	#ifdef JOEON
//		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
//		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
	#endif

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

	int n = next();
	cout<<(n < 0 ? 0 : n)<<endl;

	clock_t End = clock();

	D((double)(End-Begin)/CLOCKS_PER_SEC);

	return 0;
}
/*

 */

/**
 *    author:  Anchord
 *    created: 15.01.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long N; cin >> N;
  long long A[N], B[N];
  for(int i = 0; i < N; ++i){
   cin >> A[i];
  }
  for(int i = 0; i < N; ++i){
   cin >> B[i];
  }
  long long ans = 0, m = 0;
  for(int i = 0; i < N; ++i){
   m = max(m, A[i]);
   ans = max(ans, m*B[i]);
   cout << ans << '\n';
  }
  return 0;
}


#include <bits/stdc++.h>
#define pb(x) push_back(x)
using namespace std;
using ll=long long;
const int N=300005;
const ll M=998244353;
int a[N],b[N],c[N];
map<int,int> g;
vector<int> f[N];
int gt=0,n;
void upd(int x){for(;x<=n;x+=x&-x)c[x]++;}
int q(int x){int res=0;for(;x;x-=x&-x)res+=c[x];return res;}
void f1(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]+=i;}
	for(int i=1;i<=n;i++){
		scanf("%d",&b[i]);b[i]+=i;
		if(!g.count(b[i])){g[b[i]]=++gt;f[gt].pb(0);}
		f[g[b[i]]].pb(i);
	}
	for(int i=1;i<=n;i++){
		if(!g.count(a[i])){printf("-1");return ;}
		int na=g[a[i]];
		++f[na][0];
		if(f[na][0]>f[na].size()-1){printf("-1");return ;}
		a[i]=f[na][f[na][0]];

	}
	ll ans=0;
	for(int i=n;i>=1;i--){
		ans+=q(a[i]-1);
		upd(a[i]);
	}
	printf("%lld",ans);
}
int main(){
	f1();
	return 0;
}

/*

*/

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

#include <map>
#include <deque>

#include <functional>
template<typename T>
class SegmentTreeOneAll {
    using Func = function<T(T, T)>;

public:
    vector<T> data;
    int n;
    T init;
    Func update_func;
    Func query_func;
    SegmentTreeOneAll(int _n, T _init, Func up, Func qu) {
        init = _init;
        update_func = up;
        query_func = qu;
        for (n = 1; n < _n; n *= 2)
            ;
        data.resize(2 * n - 1, init);
    }
    void update(int pos, T val) {
        pos += n - 1;
        data[pos] = update_func(data[pos], val);
        while (pos > 0) {
            pos = (pos - 1) / 2;
            data[pos] = query_func(data[2 * pos + 1], data[2 * pos + 2]);
        }
    }
    T query(int l, int r) {
        T resL = init, resR = init;
        for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
            if (!(l & 1)) {
                resL = query_func(resL, data[l]);
            }
            if (!(r & 1)) {
                resR = query_func(data[r - 1], resR);
            }
        }
        return query_func(resL, resR);
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    input(n);
    vector<int> a(n), b(n);
    map<int, deque<int>> m;
    rep(i, 0, n) {
        input(a[i]);
        a[i] += i;
        m[a[i]].push_back(i);
    }
    vector<int> idx(n, -1);
    rep(i, 0, n) {
        input(b[i]);
        b[i] += i;
        if (m[b[i]].size()) {
            int index = m[b[i]].front();
            m[b[i]].pop_front();
            idx[index] = i;
        } else {
            print(-1);
            return 0;
        }
    }
    rep(i, 0, n) {
        if (idx[i] == -1) {
            print(-1);
            return 0;
        }
    }

    auto SUM = [](ll s, ll t) { return s + t; };
    SegmentTreeOneAll<ll> seg(n, 0LL, SUM, SUM);
    ll ans = 0;
    rep(i, 0, n) {
        int val = idx[i];
        ans += seg.query(val, n);
        seg.update(val, 1);
    }
    print(ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

//#include <atcoder/all>

//using namespace atcoder;
//typedef modint1000000007 mint;

int main(){
  int N;
  cin>>N;
  VL A(N),B(N);
  rep(i,N) cin>>A[i];
  rep(i,N) cin>>B[i];
  ll ans=0;
  rep(i,N) ans+=B[i];
  VVL X(2,VL(N/2));
  rep(i,N) X[i%2][i/2]=A[i]-B[i];
  rep(i,2) sort(all(X[i]));
  rep(i,N/2){
    ll tmp=X[0][N/2-1-i]+X[1][N/2-1-i];
    if(tmp>0) ans+=tmp;
  }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>

const int N = 400000;

bool tag[N + 5]; int fa[N + 5], s[N + 5];
int find(int x) {return (fa[x] == x ? x : fa[x] = find(fa[x]));}
void unite(int x, int y) {
	int fx = find(x), fy = find(y);
	if( fx != fy ) fa[fx] = fy, tag[fy] |= tag[fx], s[fy] += s[fx];
	else tag[fx] = true;
}

int main() {
	int n; scanf("%d", &n);
	for(int i=1;i<=N;i++) fa[i] = i, s[i] = 1;
	for(int i=1,a,b;i<=n;i++) scanf("%d%d", &a, &b), unite(a, b);

	int ans = 0;
	for(int i=1;i<=N;i++) if( find(i) == i )
		ans += s[i] - (!tag[i]);
	printf("%d\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using llint = long long;
#define rep(a,n) for(int i=a;i<(n);++i)

int main() {
	int N;
	cin >> N;
	int judge = N * 1.08;
	if(judge > 206){
		cout << ":(" << endl;
	}else if(judge == 206){
		cout << "so-so" << endl;
	}else{
		cout << "Yay!" << endl;
	}
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define vll vector<long long>
#define ll long long
#define pii pair<int,int>
const ll mod=1e9+7;

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

    string s;
    cin>>s;
    for(int i=1;i<s.size();i++){
        cout<<s[i];
    }
    cout<<s[0];
    return 0;
}


#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define read(x) for(auto& qw : (x)) cin >> qw;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MAX = 2e3 + 10;

/*
row: i
col: N + j
*/
vector<int> g[MAX];
vector<int> vis(MAX, 0);
int N,M;
char A[MAX][MAX];

void print(int v){
	if(v < N){
		cout << "row " << v;
	}
	else cout << "col " << v-N;
}

void dfs(int v){
	vis[v] = 1;
	for(int u : g[v]){
		if(!vis[u]){
			/*print(v);
			cout << " ";
			print(u);
			cout << endl;*/

			dfs(u);
		}
	}

}

void test_case(){

	cin >> N >> M;

	for(int i = 0; i < N; i++){
		for(int j = 0; j < M; j++){
			cin >> A[i][j];

			if(A[i][j] == '#'){
				g[i].pb(N+j);
				g[N+j].pb(i);
			}
		}
	}

	int ans = 0;
	dfs(0);
	dfs(N-1);
	dfs(N);
	dfs(N+M-1);

	for(int i = 1; i < N; i++){
		if(!vis[i]){
			ans++;
			dfs(i);
		}
	}

	int ans1 = 0;
	vis.assign(MAX,0);

	dfs(0);
	dfs(N-1);
	dfs(N);
	dfs(N+M-1);

	for(int i = N+1; i < N+M; i++){
		if(!vis[i]){
			ans1++;
			dfs(i);
		}
	}

	cout << min(ans, ans1) << endl;
}

int main(){ _

	int tt = 1;
//	cin >> tt;

	while(tt--){
		test_case();
	}

	return 0;
}

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

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

typedef long long ll;

using namespace std;
//using namespace atcoder;

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

class UnionFind {
    public:
      vector<int> parent;
      vector<int> size;
//      size[i] = number of nodes in tree rooted at i
// Note: not necessarily correct if i is not a root node
      set<int> roots;

    UnionFind(int n) : parent(n), size(n) {
        forr(i,0,n) { roots.insert(i); }
        forr(i,0,n) {
            parent[i] = i; size[i] = 1;
        }
    }

    int Find(int p) {
        int root = p;
        while (root != parent[root]) {  root = parent[root]; }
        while (p != root) {
            int newp = parent[p];
            parent[p] = root;
            p = newp;
        }
        return root;
    }

    void Union(int p, int q) {
        int rootP = Find(p);
        int rootQ = Find(q);
        if (rootP == rootQ) return;

        if (size[rootP] < size[rootQ]) {
            parent[rootP] = rootQ;
            size[rootQ] += size[rootP];
            roots.erase(rootP);
        }
        else {
            parent[rootQ] = rootP;
            size[rootP] += size[rootQ];
            roots.erase(rootQ);
        }
    }

};

int main(int argc, char *argv[])   {

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

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

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

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(h); readint(w);
        vector<string> grid;
        forr(i,0,h) {
            readstring(s);
            grid.push_back(s);
        }

        UnionFind uf(h+w);
        uf.Union(0, h+0);
        uf.Union(h-1, h+0);
        uf.Union(0, h+w-1);
        uf.Union(h-1, h+w-1);
        forr(i,0,h) {
            forr(j,0,w) {
                if (grid[i][j]=='#') {
                    uf.Union(i, h+j);
                }
            }
        }
        UnionFind ufh(h);
        forr(i,0,h) {
            forr(j,0,h) {
                if (uf.Find(i)==uf.Find(j)) {
                    ufh.Union(i,j);
                }
            }
        }
        UnionFind ufv(w);
        forr(i,0,w) {
            forr(j,0,w) {
                if (uf.Find(h+i)==uf.Find(h+j)) {
                    ufv.Union(i,j);
                }
            }
        }

        cout << min(ufv.roots.size()-1, ufh.roots.size()-1) << endl;

    }

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


#include <bits/stdc++.h>
#define PRINT(x) cerr<<#x<<'='<<x<<endl
#define NL(x) " \n"[(x)]
#define sz(x) int((x).size())
#define all(x) begin(x),end(x)
#define mid (l+r)/2
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define lld long long
#define pii pair<int,int>
#define pli pair<lld,int>
#define pil pair<int,lld>
#define pll pair<lld,lld>
#define INF 1000000000
#define LINF 1000000000000000000LL
#define EPS 1e-9
using namespace std;
#define MAXN 200010
map<lld, lld> e;
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    lld N, C; cin >> N >> C;
    for(int i = 1; i <= N; i++) {
        lld a, b, c; cin >> a >> b >> c;
        e[a] += c;
        e[b+1] -= c;
    }
    lld cur = 0, pretx = 0;
    lld rez = 0;
    for(auto x : e) {
        lld idx = x.fi, val = x.se;
        rez += min(cur, C)*(idx-pretx);

        cur += val;
        pretx = idx;
    }
    cout << rez;
    return 0;
}

#include<bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i, 0, n)
#define all(x) (x).begin(),(x).end()
const int INF = 1e9+1;
using namespace std;
using ll = long long;
using p = pair<int, int>;

int main() {
  ll N, C;
  cin >> N >> C;
  vector<pair<ll, ll>> D;
  REP(i, N) {
    int a, b, c;
    cin >> a >> b >> c;
    D.push_back(make_pair(a-1, c));
    D.push_back(make_pair(b, -c));
  }
  sort(all(D));
  ll ans = 0;
  ll fee = 0;
  ll t = 0;
  for (auto [x, y] : D) {
    if (x != t) {
      ans += min(C, fee)*(x-t);
      t = x;
    }
    fee += y;
  }
  cout << ans << endl;
  return 0;
}


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

void solve(long long N, long long K){
    for (int i = 0; i < K; i++)
    {
        if (N % 200 == 0){
            N = N / 200;
        }else{
            N = N * 1000;
            N = N + 200;
        }
    }
    cout << N << endl;
}

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

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(__int128_t &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(__int128_t x){
  int s=0;
  int m=0;
  char f[40];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int main(){
  int Lj4PdHRW;
  __int128_t N;
  rd(N);
  int KL2GvlyY = rd_int();
  for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){
    if(N%200==0){
      N /= 200;
      continue;
    }
    N = N * 1000 + 200;
  }
  wt_L(N);
  wt_L('\n');
  return 0;
}
// cLay version 20210508-1 [beta]

// --- original code ---
// {
//   __int128_t @N;
//   REP(rd_int()){
//     if(N%200==0) N /= 200, continue;
//     N = N * 1000 + 200;
//   }
//   wt(N);
// }


#include<bits/stdc++.h>
using namespace std;
long long pow(int k){
    if (k==0) return 1;
    else return 2*pow(k-1);
}
int main(){
    long long N;
    cin >> N;
    int k = 0;
    priority_queue<long long , vector<long long> , greater<long long>> pq;
    while(N>=pow(k)){
        long long a,b;
        a = N/pow(k);
        b = N - a * pow(k);
        long long sum = a + b + k;
        pq.push(sum);
        k++;
    }

    cout << pq.top()  << endl;
}

#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

int main() {
    uint64_t N;
    cin >> N;

    uint64_t a, b, c;
    uint64_t ans = N;

    for (b = 0; b < 100; b++){
        if (1LL<<b > N) break;
        for (int bb = b; bb < 100; bb++){
            if (1LL<<bb > N) break;
            c = N % (1LL<<bb);
            a = (N - c) / (1LL<<b);
            ans = min(ans, a+b+c);
        }
        // c = N % (1<<b);
        // a = (N - c) / (1<<b);
        // ans = min(ans, a+b+c);
        // cout << b << " " << a  << " " << c  << " " << ans <<endl;
    }
    cout << ans <<endl;
   return 0;
}


#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define mydeb(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
//#define mydeb(fmt, ...)

int main()
{
    int64_t t,n;
    cin >> t >> n;
    cout << (n*100+(t-1))/t*(100+t)/100 -1 << endl;
}

#include<bits/stdc++.h>
#pragma GCC optimize("Ofast","unroll-loops")
#define INF 2147483647
#define ll long long
#define ft first
#define sec second
#define pb push_back
#define pi pair<ll,int>
using namespace std;
ll t,n,a,b;
int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> a >> b;
	int x=min(a,b),y=max(a,b);
	cout << (x+3>y?"Yes":"No");
	return 0;
}


#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_    }
#define MY_DEBUG(s)      s
#define MY_IS_DEBUG      (true)
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#define MY_IS_DEBUG      (false)
#endif

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

constexpr int IMIN = numeric_limits<int>::min();
constexpr int IMAX = numeric_limits<int>::max();
constexpr ll LLMIN = numeric_limits<ll>::min();
constexpr ll LLMAX = numeric_limits<ll>::max();

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

MY_NAMESPACE(testbed)
void dfs_route(int this_v, vector<vector<int>> const& edges, vector<int>& route, vector<int>& visited)
{
	route.push_back(this_v);
	visited[this_v] = route.size() - 1;

	for (int i = 0; i < (int)edges[this_v].size(); i++)
	{
		int next_v = edges[this_v][i];
		if (visited[next_v] != IMAX)
			continue;
		dfs_route(next_v, edges, route, visited);
	}
}

ll dfs(int this_i, vector<vector<int>> const& edges, vector<int> const& route, vector<int>& colours)
{
	int this_v = route[this_i];
	int this_colour = colours[this_v];

	for (int i = 0; i < (int)edges[this_v].size(); i++)
	{
		int test_v = edges[this_v][i];
		int test_colour = colours[test_v];
		if (test_colour == this_colour)
			return 0;
	}

	int next_i = this_i + 1;
	if (next_i == route.size())
		return 1;
	int next_v = route[next_i];

	ll ans = 0;
	colours[next_v] = 0;
	ans += dfs(next_i, edges, route, colours);
	colours[next_v] = 1;
	ans += dfs(next_i, edges, route, colours);
	colours[next_v] = 2;
	ans += dfs(next_i, edges, route, colours);
	colours[next_v] = IMAX;

	return ans;
}

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

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

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

	ll ans = 1;
	vector<int> visited(n, IMAX);
	vector<int> colours(n, IMAX);
	for (int v = 0; v < n; v++)
	{
		if (visited[v] == IMAX)
		{
			vector<int> route;
			dfs_route(v, edges, route, visited);
			colours[v] = 0;
			ll this_ans = dfs(0, edges, route, colours);
			this_ans *= 3;
			ans *= this_ans;
		}
	}
	cout << ans << "\n";
	return 0;
}
MY_NAMESPACE_

#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

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

const int N = 20;

int G[N][N];
int n;
int col[N];

int bit(int mask, int x) {
  return (mask >> x) & 1;
}

bool singlecol(int mask) {
  rep(i,0,n) if(bit(mask, i)) {
    rep(j,i+1,n) if(bit(mask, j)) {
      if(G[i][j]) return false;
    }
  }
  return true;
}

bool dfs(int c, int mask, int cc) {
  if(col[c] != -1) {
    return col[c] == cc;
  }
  col[c] = cc;
  bool res = true;
  rep(i,0,n) if(bit(mask, i) && G[c][i]) {
    res &= dfs(i, mask, cc ^ 1);
  }
  return res;
}

LL twocol(int mask) {
  memset(col, -1, sizeof(col));
  LL res = 1;
  rep(i,0,n) if(bit(mask, i) && col[i] == -1) {
    if(!dfs(i, mask, 0)) {
      res = 0;
    } else {
      res *= 2;
    }
  }
  return res;
}

int main() {
  int m;
  S2(n,m);
  rep(i,0,m) {
    int x,y;
    S2(x,y);
    x--; y--;
    G[x][y] = G[y][x] = 1;
  }
  int mx = (1 << n) - 1;
  LL ans = 0;
  rep(i,0,1<<n) {
    if(!(i & 1)) continue;
    if(singlecol(i)) {
      ans += twocol((~i) & mx);
      // printf("%d %lld\n",i,ans);
    }
  }
  ans *= 3;
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10;
map<string,int>f,fff;
string x[N];
int main(){

	int n; cin>>n;
	for(int i=0;i<n;i++){
		cin>>x[i];
		if(fff[x[i]]) continue;
		fff[x[i]]=1;
		int len=x[i].size();
		//for(int j=1;j<=len;j++){
			int ff=(x[i][0]=='!');
			f[x[i].substr(ff)]++;
//			for(int k=ff;k+j-1<len;k++){
//				f[x[i].substr(k,j)]++;
//			}
		//}
		//cout<<"check is "<<x[i].substr(0)<<endl;
	}
	int res=0;
	for(pair<string,int>i :f){
        if(i.second>1){
			res=1;
			cout<<i.first<<endl;
			break;
        }
	}
	if(!res){
		 cout<<"satisfiable"<<endl;
	}

    return 0;
}

#include <bits/stdc++.h>

#include <algorithm>
#include <iterator>
#include <vector>
// #include <atcoder/all>
// using namespace atcoder;

using namespace std;
using ll = long long;

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

    int N;
    cin >> N;

    vector<string> n(N);
    vector<string> e(N);
    for (int i = 0; i < N; i++) {
        string s;
        cin >> s;
        if (s[0] == '!') {
            e.push_back(s.erase(0, 1));
        } else {
            n.push_back(s);
        }
    }

    sort(n.begin(), n.end());
    sort(e.begin(), e.end());

    vector<string> inter;
    set_intersection(n.begin(), n.end(), e.begin(), e.end(),
                     back_inserter(inter));
    string output = "satisfiable";
    for (int i = 0; i < inter.size(); i++) {
        if (inter[i] != "") output = inter[i];
    }
    cout << output << endl;

    return 0;
}


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

typedef pair <int, int> pii;

const int N = 4e5 + 10;

int n, ans[N], p[N];

set <int> s[2];

set <int> :: iterator it;

pii a[N];

int main() {
	scanf("%d", &n), n *= 2;

	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i].first);
		a[i].second = i;
	}

	sort(a + 1, a + n + 1);

	for (int i = 1; i <= n/2; i++) {
		s[0].insert(a[i].second), s[1].insert(a[i + n/2].second);
		p[a[i + n/2].second] = 1;
	}

//	puts("OK");

	for (int i = 1; i < n; i++)
		if (!ans[i]) {
			int x = p[i];
			s[x].erase(i), ans[i] = 1;
			it = s[x^1].upper_bound(i);
			ans[*it] = 2, s[x^1].erase(it);
		}

	for (int i = 1; i <= n; i++)
		printf("%c", ans[i] == 1 ? '(' : ')');
}


// Problem Link -
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define inf 1e18
#define endl "\n"
#define pb push_back
#define pi pair<ll, ll>
#define vi vector<ll>
#define vpi vector<pi>
#define vvi vector<vi>
#define vvpi vector<vector<pi>>
#define vs vector<string>
#define ump unordered_map
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll, vi, greater<ll>>
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define mid(l, r) (l + (r - l) / 2)
#define loop(i, a, b) for (ll i = (a); i <= (b); i++)
#define looprev(i, a, b) for (ll i = (a); i >= (b); i--)
#define log(args...)                             \
	{                                            \
		string _s = #args;                       \
		replace(_s.begin(), _s.end(), ',', ' '); \
		stringstream _ss(_s);                    \
		istream_iterator<string> _it(_ss);       \
		err(_it, args);                          \
	}
#define logarr(arr, a, b)            \
	for (int z = (a); z <= (b); z++) \
		cout << (arr[z]) << " ";     \
	cout << endl;
#define token(str, ch) (std::istringstream var((str)); vs v; string t; while (getline((var), t, (ch))) { v.pb(t); } return v;)
vs tokenizer(string str, char ch)
{
	std::istringstream var((str));
	vs v;
	string t;
	while (getline((var), t, (ch)))
	{
		v.pb(t);
	}
	return v;
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
	cout << *it << " = " << a << endl;
	err(++it, args...);
}
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void file_i_o()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main(int argc, char const *argv[])
{
	file_i_o();

	ll n=3;
	// cin>>n;

	vi a(n);
	loop(i,0,n-1)
	{
		cin>>a[i];
	}

sort(all(a));
cout<<a[1]+a[2];

	return 0;
}

#include<bits/stdc++.h>
#define SORT(v) sort(v.rbegin() , v.rend())
#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 sz(x) (int)x.size()
#define PB push_back
#define fst first
#define scn second
#define DBG(x) cerr << #x << " = " << (x) << endl
#define N_MAX 1000010
#define M 1000000007
#define EPS 1e-9

using namespace std;

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

int main(){
	int a, b, c;
	sii(a, b);
	si(c);
	int ans = max(b+c, max(a+b, a+c));
	printf("%d\n", ans);
  return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <fstream>
#include <bitset>
#include <cassert>
#include <functional>
#include <cstring>
#include <ctime>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#define sec second
#define fir first
#define pb push_back
#define mp make_pair
#define REP(i , n) for (int i = 0 ; i < (n) ; i++)
#define BACK(i , n) for (int i = (n) - 1 ; i >= 0 ; i--)
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int , int>;
using ll = long long;
using ld = long double;
const int MAX = 1e9 , MIN = - MAX , MOD = 1e9 + 7;
const ll MAX_LL = 1e18 , MIN_LL = -MAX_LL;
int N , T , ans = 0 , arr[40];
vector<int> vec = {0};
void gener(int tot , int idx){
    if (tot > T)
        return;
    if (idx == N)
        vec.push_back(tot);
    else if (idx == N / 2){
        int l = 0 , r = vec.size() - 1;
        while(l < r){
            int mid = (l + r >> 1) + (l + r & 1);
            if (vec[mid] + tot > T)
                r = mid - 1;
            else
                l = mid;
        }
        ans = max(ans , vec[l] + tot);
    }
    else{
        gener(tot + arr[idx] , idx + 1);
        gener(tot , idx + 1);
    }
}
int main(){
    cin >> N >> T;
    REP(i , N)
        cin >> arr[i];
    if (N == 1)
        return 0 * printf("%d\n" , arr[0] <= T ? arr[0] : 0);
    gener(arr[N / 2] , N / 2 + 1);
    gener(0 , N / 2 + 1);
    sort(begin(vec) , end(vec));
    gener(0 , 0);
    cout << ans << endl;
}

//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll int
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);

void solve()
{
    int n,t;
    cin>>n>>t;
    int mid=n/2;
    vector<int> a(n);
    fi(0,n)
    {
        cin>>a[i];
    }
    set<int> s;
    fi(0,(1<<mid))
    {
        int sum=0;
        fj(0,mid)
        {
            if(i&(1LL<<j))
                sum+=a[j];
        }
        if(sum<t)
        {
            s.insert(sum);
        }
    }
    mid=n-mid;
    s.insert(0);
    int ans=0;
    fi(0,(1<<mid))
    {
        int sum=0;
        fj(0,mid)
        {
            if(i&(1LL<<j))
                sum+=a[j+n/2];
        }
        if(sum>t) continue;
        auto it=s.upper_bound(t-sum);
        it--;
        ans=max(ans,sum+*it);
    }
    cout<<ans<<'\n';

}

signed main()
{
    FAST;
    int tt=1;
    //cin>>tt;
    while(tt--)
    {
        solve();
    }
}

//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1)..{because we solve this by introducing log ..do same for general cases}
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)


#include <bits/stdc++.h>

using namespace std;

#define rep(i,n)      for(long long(i)=0;(i)<(n);(i)++)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

#define GCD(a,b)      __gcd((a),(b))
#define LCM(a,b)      (a)/GCD((a),(b))*(b)

int main(){

    long n,a,b,c;

    cin >> n;

    long ans = LONG_MAX;

    rep(i,n)
    {
        cin >> a >> b >> c;
        long rem = c - a;
        if(rem > 0)
        {
            ans = min(ans, b);
        }
    }

    if(ans == LLONG_MAX) ans = -1;
    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define ALL(v) v.begin(),v.end()
#define sz(v) (ll)v.size()
const ll INF = 1e18;
const ll MOD = 998244353;

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n), p(n), x(n);
    for(ll i = 0; i < n; i++) cin >> a[i] >> p[i] >> x[i];
    ll ans = INF;
    for(ll i = 0; i < n; i++) {
        if(x[i]-a[i] > 0){
            chmin(ans, p[i]);
        }
    }
    cout << (ans==INF ? -1 : ans) << endl;

    return 0;
}


//
// Created by Tetsuya Shiota on 2019-04-13.
//
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define REP(i, b, n) for (Int i = b; i < Int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(e, o) for (auto &&e : o)
#ifdef LOCAL
#define pp(x) cerr << __LINE__ << ' ' << #x << " = " << (x) << endl
#define dump(x)                                                                \
  cerr << __LINE__ << ' ' << #x << " : ";                                      \
  FOR(e, x) cerr << e << ' ';                                                  \
  cerr << endl;
#else
#define pp(x)
#define dump(x)
#endif
using namespace std;
using Int = long long;
using vi = vector<Int>;
using vvi = vector<vi>;
using pii = pair<Int, Int>;
const Int dy[] = {-1, 0, 1, 0};
const Int dx[] = {0, 1, 0, -1};
template <typename T> void reverse(vector<T> &v) {
  reverse(v.begin(), v.end());
}
template <typename T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <typename T> void rsort(vector<T> &v) { sort(v.rbegin(), v.rend()); }
template <typename T> T sum(const vector<T> &v) {
  return accumulate(v.begin(), v.end(), T(0));
}
template <class T> void update_max(T &a, const T &b) { a = max(a, b); }
template <class T> void update_min(T &a, const T &b) { a = min(a, b); }
vector<pair<char, Int>> merge_cont_char(const string &s) {
  vector<pair<char, Int>> ret;
  if (s.empty())
    return ret;
  ret.push_back(make_pair(s[0], 0));
  FOR(c, s) {
    if (ret.back().first == c)
      ret.back().second++;
    else
      ret.push_back(make_pair(c, 1));
  }
  return ret;
}
class Shakutori {
public:
  // 全体の範囲
  // [0, end)
  // 注目している範囲
  // [tail, head)
  Int head, tail, end = 0;
  bool moveHead() {
    if (head == end) {
      return false;
    }
    head++;
    return true;
  }
  void moveTail() { tail++; }
  bool valid() {
    if (head == tail)
      return true;
    // [tail, head)
    // が解として適当か検証する
    return true;
  }
  void solve() {
    head = tail = 0;
    Int ans = 0;
    while (moveHead()) {
      while (!valid()) {
        moveTail();
      }
      // ここで解を更新する
    }
  }
};
class Solver {
public:
  Int ans = 0, N;
  vi A, B;
  Solver() {}
  void input() {
    cin >> N;
    A = B = vi(N);
    FOR(a, A)cin >> a;
    FOR(b, B)cin >> b;
  }
  void solve() {
    rep(i, N){
      ans += A[i] * B[i];
    }
  }
  void output() {
    cout << (ans == 0) <<endl;
  }
};
void initilization() {
  struct BoolName : numpunct<char> {
    string t, f;
    BoolName(string t = "Yes", string f = "No") : t(t), f(f) {}
    string do_truename() const { return t; }
    string do_falsename() const { return f; }
  };
  cin.tie(nullptr);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(15) << boolalpha;
  cout.imbue(locale(cout.getloc(), new BoolName));
}
int main() {
  initilization();
#ifdef LOCAL
  while (1) {
    Solver s;
    s.input();
    if (cin.eof())
      break;
    s.solve();
    s.output();
  }
#else
  Solver s;
  s.input();
  s.solve();
  s.output();
#endif
  return 0;
}

#include <iostream>

using namespace std;

int main() {
  int N;
  int A[100000], B[100000];
  int i = 0;
  cin >> N;

  //Aの読み込み
  while (i < N) {
    cin >> A[i];
    i++;
  }

  //Bの読み込み
  i = 0;
  while (i < N) {
    cin >> B[i];
    i++;
  }

  //内積の計算
  int sum = 0;
  i = 0;
  while (i < N) {
    sum += A[i] * B[i];
    i++;
  }

  //内積の判定
  if (sum == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG
#define LOG(...) cerr << "[" << #__VA_ARGS__ << "]: " << repr(__VA_ARGS__) << endl;
#define MSG(args) cerr << args << "\n";
#define debug(x) x
#else
#define LOG(...)
#define MSG(args)
#define debug(x)
#endif

#define mp make_pair
#define pb push_back
#define sz(x) (int)((x).size())
#define ms(x, v) memset((x), v, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define REP(x, n) for(int x = 0; x < n; x++)
#define REPV(x, v, n) for(int x = v; x < n; x++)
#define REVE(x, n) for(int x = n; x >= 0; x--)

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vvi = vector<vi>;

class UnionFind
{
    int N, cnt;
    vector<int> parent, size;

public:
    UnionFind() { N = 0; }
    UnionFind(int N) :N(N) { clear(); }

    void clear() { cnt = N; size.assign(N + 1, 1); parent.assign(N + 1, 0); for(int i = 1; i <= N; i++) parent[i] = i; }
    void reset(int sz) { N = sz; clear(); }
    inline int count() const { assert(N != 0); return cnt; }
    int getSetSize(int i) { assert(N != 0 && i >= 1 && i <= N); return size[findSet(i)]; }
    int findSet(int i) { assert(N != 0 && i >= 1 && i <= N); return (parent[i] == i) ? i : (parent[i] = findSet(parent[i])); }
    bool isSameSet(int i, int j) { assert(N != 0 && i >= 1 && i <= N && j >= 1 && j <= N); return findSet(i) == findSet(j); }

    void unite(int i, int j)
    {
        assert(N != 0 && i >= 1 && i <= N && j >= 1 && j <= N);
        int x = findSet(i), y = findSet(j);
        if(x == y)
            return;
        if(size[x] < size[y])
            swap(x, y);
        parent[y] = x;
        size[x] += size[y];
        cnt--;
    }
};

const ld eps = 1e-7;

int N;
pll nails[111];

bool can(ld R)
{
    UnionFind uf(N + 2);
    int up = N + 1, down = N + 2;
    REPV(i, 1, N + 1)
    {
	if(100 - nails[i].second <= 2 * R) uf.unite(i, up);
	if(100 + nails[i].second <= 2 * R) uf.unite(i, down);
    }
    REPV(i, 1, N + 1)
	REPV(j, i + 1, N + 1)
	    if(pow(nails[i].first - nails[j].first, 2) + pow(nails[i].second - nails[j].second, 2) <= 4 * R * R)
		uf.unite(i, j);
    return !uf.isSameSet(up, down);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N;
    REPV(i, 1, N + 1) cin >> nails[i].first >> nails[i].second;
    ld lo = 0.0, hi = 500.0;
    while(hi - lo > eps)
    {
	ld mid = (hi + lo) / 2.0;
	if(can(mid)) lo = mid;
	else hi = mid;
    }
    cout.precision(9);
    cout << fixed << lo << "\n";
    return 0;
}

// Standard I/O
#include <iostream>
#include <sstream>
#include <cstdio>
// Standard Library
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
// Template Class
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
// Container Control
#include <algorithm>

using namespace std;

#define rep( i, n ) for( int i = 0; i < n; ++i )
#define irep( i, n ) for( int i = n-1; i >= 0; --i )
#define reep( i, s, n ) for ( int i = s; i < n; ++i )
#define ireep( i, n, s ) for ( int i = n-1; i >= s; --i )
#define foreach(itr, x) for( typeof(x.begin()) itr = x.begin(); itr != x.end(); ++itr)

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all( v ) v.begin(), v.end()
#define fs first
#define sc second
#define vc vector

// for visualizer.html
double SCALE = 1.0;
double OFFSET_X = 0.0;
double OFFSET_Y = 0.0;
#define LINE(x,y,a,b) cerr << "line(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(a) + OFFSET_X << ","										\
	<< SCALE*(b) + OFFSET_Y << ")" << endl;
#define CIRCLE(x,y,r) cerr << "circle(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(r) << ")" << endl;

typedef long long ll;
typedef complex<double> Point;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector< vector<int> > vii;
typedef vector< vector<double> > vdd;

typedef vector<int>::iterator vi_itr;

const int IINF = 1 << 28;
const int MOD = 1e9+7;
const double INF = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1.0);

// Direction : L U R D
const int dx[] = { -1, 0, 1, 0};
const int dy[] = { 0, -1, 0, 1 };

struct Node {
    vector<int> edge;
    vector<int> weight;
};

struct Graph {
    vector<Node> node;
};

bool is_reachable_graph ( const Graph& g, const int beg_node, const int end_node ) {
    queue<int> que;

    que.push(beg_node);
    vector<bool> exist(g.node.size(), false);
    while ( !que.empty() ) {
        int n = que.front(); que.pop();
        if ( exist[n] ) continue;
        exist[n] = true;

        rep(i, g.node[n].edge.size() ) {
            que.push(g.node[n].edge[i]);
        }
    }

    return exist[end_node];
}

int main()
{
    vector<Point> p;
    int n;
    cin >> n;

    double dist[120][120];
    p.resize(n);
    rep(i, n) {
        double x, y;
        cin >> x >> y;
        p[i] = Point(x, y);
    }

    rep(i, n) rep(j, n) dist[i][j] = sqrt(norm(p[i] - p[j]));
    rep(i, n) dist[n][i] = dist[i][n] = abs(p[i].imag() - 100);
    rep(i, n) dist[n+1][i] = dist[i][n+1] = abs(p[i].imag() + 100);
    dist[n][n+1] = dist[n+1][n] = 200;

    vector<tuple<double, int, int>> v;
    rep(i, n) reep(j, i+1, n) v.emplace_back(dist[i][j], i, j);
    rep(i, n) v.emplace_back(abs(p[i].imag() - 100), i, -1);
    rep(i, n) v.emplace_back(abs(p[i].imag() + 100), i, -2);

    sort(all(v));

    rep(i, v.size()) {
        double r = get<0>(v[i]);

        Graph g;
        g.node.resize(n+2);

        rep(i, n+2) reep(j, i+1, n+2) {
            if ( dist[i][j] < r + EPS ) {
                g.node[i].edge.emplace_back(j);
                g.node[j].edge.emplace_back(i);
            }
        }

        if ( is_reachable_graph(g, n, n+1) ) {
            printf("%23.16E\n", r / 2.0);
            break;
        }
    }
}


#include <iostream>

int n, k, m;
int dp[105][500005], tmp[500005];

int Add(int x, int y)
{
	return x + y >= m ? x + y - m : x + y;
}

int Sub(int x, int y)
{
	return x - y < 0 ? x - y + m : x - y;
}

int Mul(int x, int y)
{
	return 1ll * x * y % m;
}

int main()
{
	std::cin >> n >> k >> m;
	dp[0][0] = 1;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 0; j <= 500000; j++)
		{
			if(j < i)
			{
				tmp[j] = dp[i - 1][j];
			}
			else
			{
				tmp[j] = Add(tmp[j - i], dp[i - 1][j]);
			}
		}
		for(int j = 0; j <= 500000; j++)
		{
			int r = j - i * (k + 1);
			dp[i][j] = Sub(tmp[j], r < 0 ? 0 : tmp[r]);
		}
	}
	for(int i = 1; i <= n; i++)
	{
		int l = 1 - i, r = n - i;
		int ans = 0;
		for(int j = 1; j <= 500000; j++)
		{
			if(l != 0 and r != 0)
			{
				ans = Add(ans, Mul(dp[-l][j], dp[r][j]));
			}
		}
		std::cout << Add(Mul(ans, k + 1), k) << '\n';
	}
}

#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <stdio.h>

constexpr int HeighestDim = 505000;

int n, m, k;
long long int res[101];
int dim_a = 0, dim_b = 0;
int A[HeighestDim + 1], B[HeighestDim];

void mult_a(int v){
    for(int i = dim_a, j = i - v; j >= 0; --i, --j){ A[i] -= A[j]; if(A[i] < 0) A[i] += m; }
}
void div_a(int v){
    for(int i = v, j = 0; i <= dim_a; ++i, ++j){
        A[i] += A[j]; if(A[i] >= m) A[i] -= m;
    }
}

void mult_b(int v){
    dim_b += v;
    for(int i = dim_b, j = i - v; j >= 0; --i, --j){
        B[i] -= B[j]; if(B[i] < 0) B[i] += m;
    }
}
void div_b(int v){
    for(int i = v, j = 0; i <= dim_b; ++i, ++j){
        B[i] += B[j]; if(B[i] >= m) B[i] -= m;
    }
}

int len = 0;
char Buffer[20];
char Output[1500];

int main(void){
    A[0] = 1; B[0] = 1;
    scanf("%d%d%d", &n, &k, &m);
    if(n == 1){ printf("%d\n", k); return 0; }
    else if(n == 2){ printf("%d\n%d\n", k, k); return 0; }
    else if(n == 3){
        printf("%d\n%d\n%d\n", k, k * static_cast<long long int>(k + 2) % m, k);
        return 0;
    }
    res[1] = k; res[n] = k; ++k;
    const int hd = (n >> 1) + (n & 1);
    const int nmt = n - 2;
    {
        int i, j;
        for(i = 1, j = k; i <= hd; ++i, j += k){ dim_a += j; mult_a(j); div_a(i); }
        for(; i <= nmt; ++i, j += k){ mult_a(j); div_a(i); }
        mult_b(k); div_b(1);
    }
    for(int i = 2, j = n - 1, I = 2 * k, J = k * (n - 2); ; I += k, J -= k){
        long long int res_v = 1;
        for(int d = 1; d <= dim_b; ++d){
            res_v += static_cast<long long int>(A[d]) * B[d];
            if(res_v > 7'000'000'000'000'000'000) res_v %= m;
        }
        res_v %= m; if(res_v) res_v = (k * res_v - 1) % m; else res_v = m - 1;
        res[i] = res_v; res[j--] = res_v;
        if(i + 1 > j) break;
        mult_a(j); div_a(J);
        mult_b(I); div_b(i++);
    }

    for(int i = 1; i <= n; ++i){
        if(res[i]){
            int l = 0;
            do { Buffer[l++] = res[i] % 10; res[i] /= 10; } while(res[i]);
            while(l--) Output[len++] = Buffer[l] ^ '0';
        }
        else Output[len++] = '0';
        Output[len++] = '\n';
    }
    fwrite(Output, 1, len, stdout);
    return 0;
}