#include<bits/stdc++.h>
#define inf 2000000000
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define fastio ios::sync_with_stdio(false);cin.tie(NULL)
#define rng_23 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef set<int> si;
typedef multiset<int> msi;
typedef map<int,int> mii;
int grid[1001][1001],reps[1001][1001];
pii parent[1001][1001];
pii rep(pii a)
{
    if(parent[a.fi][a.se]==a)
        return a;
    return  parent[a.fi][a.se]=rep(parent[a.fi][a.se]);
}
int main()
{
    int i,j,k;
    int n,m;
    string str;
    cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>str;
        for(j=1;j<=m;j++){
            grid[i][j]=str[j-1];
            parent[i][j]=make_pair(i,j);
        }

    }
    grid[1][1]='#';
    grid[1][m]='#';
    grid[n][1]='#';
    grid[n][m]='#';
    int r=0,c=0;
    for(i=1;i<=n;i++){
        int sig=1;
        for(j=1;j<=m;j++)
            if(grid[i][j]=='#')
                sig=0;
        r+=sig;
        if(sig)
            grid[i][1]='*';
    }
    for(j=1;j<=m;j++){
        int sig=1;
        for(i=1;i<=n;i++)
            if(grid[i][j]=='#')
                sig=0;
        c+=sig;
        if(sig)
            grid[1][j]='*';
    }
    //cout<<r<<" "<<c<<endl;
    if(r<c){
        for(j=1;j<=m;j++)
            if(grid[1][j]=='*')
                grid[1][j]='.';

    }
    else {
        for(i=1;i<=n;i++)
            if(grid[i][1]=='*')
                grid[i][1]='.';
    }
    for(i=1;i<=n;i++){
        vii vec;
        for(j=1;j<=m;j++)
            if(grid[i][j]=='#' || grid[i][j]=='*')
                vec.pb(make_pair(i,j));
        pii lrp;
        if(!vec.empty())
            lrp=rep(vec.front());
        for(j=1;j<vec.size();j++){
            pii rp=rep(vec[j]);
            parent[rp.fi][rp.se]=lrp;
        }
    }
    for(j=1;j<=m;j++){
        vii vec;
        for(i=1;i<=n;i++)
            if(grid[i][j]=='#' || grid[i][j]=='*')
                vec.pb(make_pair(i,j));
        pii lrp;
        if(!vec.empty())
            lrp=rep(vec.front());
        for(i=1;i<vec.size();i++){
            pii rp=rep(vec[i]);
            parent[rp.fi][rp.se]=lrp;
        }
    }
    //cout<<"hola"<<endl;
    set<pii> cnt;
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
            if(grid[i][j]=='#' || grid[i][j]=='*')
                cnt.insert(rep(make_pair(i,j)));
    cout<<cnt.size()-1+min(r,c)<<endl;
    //cout<<rep(make_pair(7,2)).fi<<" "<<rep(make_pair(7,2)).se<<endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
#include<unordered_map>

using namespace std;

#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()

using ll = long long;
using ii = pair<int,int>;

struct DSU {
    int n;
    // p[i] := rep of the component of i.
    vector<int> p;
    // rank[i] := size of the component of i.
    vector<int> rank;

    void init(int sz) {
        n = sz;
        p.resize(n);
        iota(p.begin(), p.end(), 0);
        rank.assign(n,1);
    }

    int getRep(int i) {
        if(p[i] == i) {
            return i;
        }
        return p[i] = getRep(p[i]);
    }

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

    bool merge(int x, int y) {
        x = getRep(x);
        y = getRep(y);
        if(x == y) {
            return false;
        }
        if(rank[x] < rank[y]) {
            swap(x, y);
        }
        rank[x] += rank[y];
        p[y] = x;
        return true;
    }
};

int main()
{
    int h, w;
    cin >> h >> w;
    // Consider bipartite graph: vertices represent rows 0,...h-1
    // and cols h,...h+w-1; and edges between rows and columns.
    vector<ii> edges;

    vector<string> s(h);
    REP(i, 0, h) {
        cin >> s[i];
        REP(j, 0, w) {
            if(s[i][j] == '#') {
                edges.emplace_back(i, j+h);
            }
        }
    }
    // row 0 <-> col 0 (indexed as vertex h)
    edges.emplace_back(0, h);
    // row 0 <-> col w-1 (last col)
    edges.emplace_back(0, h+w-1);
    // row h-1 (last row) <-> col 0
    edges.emplace_back(h-1, h);
    // row h-1 <-> col w-1
    edges.emplace_back(h-1, h+w-1);

    DSU dsu;
    dsu.init(h+w);
    for(auto e: edges) {
        dsu.merge(e.first, e.second);
    }

    unordered_map<int, bool> comp;
    int cc = 0;
    REP(i, 0, h) {
        int c = dsu.getRep(i);
        if(!comp[c]) {
            ++cc;
            comp[c] = true;
        }
    }
    int ans = cc-1;

    comp.clear();
    cc = 0;
    REP(i, h, h+w) {
        int c = dsu.getRep(i);
        if(!comp[c]) {
            ++cc;
            comp[c] = true;
        }
    }
    ans = min(ans, cc-1);

    cout << ans << endl;

    return 0;
}


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

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<vector<int>> graph(N);
  for (int i = 0; i < M; i++) {
    int X, Y;
    cin >> X >> Y;
    X--;
    Y--;
    graph[X].push_back(Y);
  }

  vector<long long> minimum(N, 1e10);
  minimum[0] = A[0];
  for (int i = 0; i < N; i++) {
    for (auto g : graph[i]) {
      minimum[g] = min({minimum[g], minimum[i], A[i]});
    }
  }

  long long ans = -1e10;
  for (int i = 1; i < N; i++) {
    ans = max(A[i] - minimum[i], ans);
  }

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

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
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);

vector<vector<int>> adj;
vector<int> a;
int n,m;
vector<ar<int,3>> dp;
vector<bool> vis;

// 0-->dp value (profit)
// 1-->max number till here
// 2-->min number till here
vector<int> maxi,mini;

void dfs(int node)
{
	maxi[node]=max(maxi[node],a[node]);
	mini[node]=min(mini[node],a[node]);
	vis[node]=true;
	for(auto i:adj[node])
	{
		mini[i]=min(mini[i],mini[node]);
		if(!vis[i])
		{
			dfs(i);
		}
		maxi[node]=max(maxi[i],maxi[node]);
	}
}

void solve()
{
	cin>>n>>m;
	a=vector<int> (n);
	fi(0,n) cin>>a[i];
	adj=vector<vector<int>> (n);
	// dp=vector<ar<int,3>> (n);
	const int INF=1e13;
	maxi=vector<int> (n);
	mini=vector<int> (n,INF);
	vis=vector<bool> (n);
	fi(0,m)
	{
		int x,y;
		cin>>x>>y;
		--x,--y;
		adj[x].pb(y);
	}
	fi(0,n)
	{
		if(!vis[i])
		{
			dfs(i);
		}
	}
	int ans=-INF;
	fi(0,n)
	{
		for(auto x:adj[i])
		{
			ans=max(ans,maxi[x]-mini[i]);
		}
	}
	cout<<ans<<'\n';
}

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


#include <bits/stdc++.h>

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

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

#define rand __rand
    mt19937 generator(chrono::system_clock::now().time_since_epoch().count());
    template <class T = int>
    T rand(T range = numeric_limits <T> :: max()){
        return (T) (generator() % range);
    }
}
using namespace DEBUG;

template <class T,class U>
void maximize(T &lhs,U rhs){
    if(lhs < rhs)lhs = rhs;
}
template <class T,class U>
void minimize(T &lhs,U rhs){
    if(lhs > rhs)lhs = rhs;
}
template <class T>
T tabs(T x){
    return (x < (T)0 ? -x : x);
}

#define len(c) (int)((c).size())
#define llong int64_t
#define MASK(i) ((1LL) << (i))
#define BIT(x,i) (((x) >> (i)) & 1)
#define file_name "test"    /// FILE NAME HERE!!!

/* ================================= END OF TEMPLATE ================================= */

namespace Task{

    const llong INF = 1e18;
    class Solver{
        public:

            int n;
            llong X;
            vector < int > a;

            void solve(){
                cin>>n>>X;
                a.resize(n + 2,0);
                for(int i = 1;i <= n;++i){
                    cin>>a[i];
                }
                auto compute = [&](const int k) -> llong{
                    vector < vector < vector < llong > > > dp(n + 2,vector < vector < llong > > (k + 2,vector < llong > (k + 2,-INF)));
                    dp[0][0][0] = 0;
                    for(int i = 1;i <= n;++i){
                      for(int j = 1;j <= k;++j){
                        for(int m = 0;m < k;++m){
                            maximize(dp[i][j][m],dp[i - 1][j][m]);
                            int rm = ((m % k) - (a[i] % k) + k) % k;
                            maximize(dp[i][j][m],dp[i - 1][j - 1][rm] + (llong)a[i]);
                        }
                      }
                    }
                    return dp[n][k][X % k];
                };
                llong ans = INF;
                for(int i = 1;i <= n;++i){
                    llong S = compute(i);
                    if(S >= 0){
                        ans = min(ans,(X - S) / i);
                    }
                }
                cout<<ans<<'\n';
            }

            ~Solver(){}
    };

    void solve(){
        int ntest = 1;
#ifndef  ONLINE_JUDGE
        ntest = 2; // single test to multitest.
#endif
        //cin>>ntest;  // multitest
        while(ntest--){
            Solver().solve();
        }
    }
}

int main(int argc, char** argv){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen(file_name".inp","r",stdin);
    //freopen(file_name".out","w",stdout);
    #endif // ONLINE_JUDGE
    Task::solve();
}

#include<bits/stdc++.h>
#define forn(i,s,t) for(register int i=(s);i<=(t);++i)
#define form(i,s,t) for(register int i=(s);i>=(t);--i)
using namespace std;
typedef long long LL;
const int N = 1e2+3;
const LL INF = 2e18;
template<typename T>inline T Max(T A, T B) {return A>B?A:B;}
template<typename T>inline T Min(T A, T B) {return A<B?A:B;}
LL a[N], f[N][N], X, Ans; int n;
int main() {
	scanf("%d%lld", &n, &X);
	forn(i,1,n) scanf("%lld", a+i);
	Ans = INF;
	forn(i,1,n) {
		forn(j,1,n) form(k,i-1,0) f[j][k] = -1;
		f[0][0] = 0;
		forn(j,1,n) form(k,i,0) forn(l,0,i-1) if(f[k][l] != -1) {
			LL tmp = f[k][l] + a[j];
			f[k+1][tmp % i] = Max(f[k+1][tmp % i], tmp);
		}
		if(f[i][X % i] != -1) Ans = Min(Ans, (X - f[i][X % i]) / i);
	}
	printf("%lld\n", Ans);
	return 0;
}


#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200010,mod=1e9+7;
double f[N][2];
bool vis[N];
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= k;i++)
    {
        int x;
        cin >> x;
        vis[x] = true;
    }
    double p = 0, q = 0;
    for (int i = n - 1; i >= 0;i--)
    {
        if(i + m <= n - 1)
        {
            p -= f[i + m + 1][0];
            q -= f[i + m + 1][1];
        }
        if(!vis[i])
        {
            f[i][0] = p / m;
            f[i][1] = q / m + 1;
        }
        else
        {
            f[i][0] = 1;
            f[i][1] = 0;
        }
        p += f[i][0];
        q += f[i][1];
    }
    if(fabs(1.00000-f[0][0])<=1e-12)
        puts("-1");
        else
            printf("%.6lf\n", f[0][1] / (1 - f[0][0]));
        return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define dup(x,y) (((x)+(y)-1)/(y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
template<typename T> bool chmax(T &a, T b) {if(a<b){a=b; return 1; } return 0; }
template<typename T> bool chmin(T &a, T b) {if(a>b){a=b; return 1; } return 0; }
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
ll mpow(ll x, ll p) {
	if (p==0) return 1;
	ll res=mpow(x,p/2); res=res*res%MOD;
	return p%2?res*x%MOD:res;
}
ll lpow(ll x, ll p) {
	if (p==0) return 1;
	ll res=lpow(x,p/2); res*=res;
	return p%2?res*x:res;
}

#ifndef fenwick_tree_hpp
#define fenwick_tree_hpp

#include<vector>

template<typename T> struct Fenwick_Tree {
	private:
		int n;
		std::vector<T> bit;
	public:
		Fenwick_Tree(int _n): n(_n), bit(_n, 0) {}
		// [0, r)の要素の総和
		T sum(int i) {
			T s = 0;
			while (i > 0) {
				s += bit[i-1];
				i -= i & -i;
			}
			return s;
		}
		// [l, r)の要素の総和
		T sum(int l, int r) {
			return sum(r) - sum(l);
		}
		void add(int i, T x) {
			i += 1;
			while (i <= n) {
				bit[i-1] += x;
				i += i & -i;
			}
		}
};

#endif // fenwick_tree_hpp

const int M = 100000;

int main() {
	int n, m, k;
	cin >> n >> m >> k;
	vector<int> batsu(n+1, 0);
	Fenwick_Tree<double> fw_a(M+M+5), fw_b(M+M+5);
	rep(i,k) {
		int x; cin >> x;
		batsu[x]++;
		fw_a.add(x, 1);
	}
	for (int i = n-1; i >= 0; --i) {
		if (batsu[i]) continue;
		fw_a.add(i, fw_a.sum(i+1, i+m+1)/m);
		fw_b.add(i, fw_b.sum(i+1, i+m+1)/m + 1);
	}
	double a = fw_a.sum(0, 1);
	double b = fw_b.sum(0, 1);
	if (1 - a < EPS) cout << -1 << endl;
	else {
		double ans = b / (1 - a);
		cout << fixed << setprecision(10) << ans << endl;
	}
}


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

struct S {
    ll l, v;

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

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

};

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

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

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

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

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

    return 0;
}

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

#ifdef LOCAL
#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
    ~printer(){cerr<<endl;}
    Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
    Rics(string x){cerr<<x;return *this;}
    Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
    Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
    Gene c >Rics(rge<c> x){
        *this,"["; for(auto it = x.b; it != x.e; ++it)
            *this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
#define tham getchar()
#endif

#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define eq(x, y) (fabs((x)-(y))<error)
#define bt(i) (1LL<<(i))

mt19937_64 rng((unsigned)chrono::system_clock::now().time_since_epoch().count());
//const ll mod = ;
//const ld error = ;
const ld PI = acosl(-1);

//inline ll MOD(ll x, ll m = mod){
//    ll y = x % m;
//    return (y >= 0) ? y: y+m;
//}

//const int/ll inf = ;
//const int nmax = ;
///===========================================  template  =======================================================

ll req[8][8];
ll memo[8];

int main(){
    FASTIO;

    int n, m;
    cin>>n>>m;
    vector<ll> input(n);
    vector<pair<ll, ll>> v(m);
    ll tot = 0;

    ll mx = 0;
    for(int i = 0; i<n; i++){
        cin>>input[i];
        mx = max(input[i], mx);
    }

    bool good = true;
    for(int i = 0; i<m; i++){
        cin>>v[i].second>>v[i].first;
        tot += v[i].second;
        if(v[i].first < mx)
            good = false;
    }
    if(!good){
        cout<<-1;
        return 0;
    }
    v.emplace_back(0ll, 0ll);

    sort(input.begin(), input.end());
    sort(v.begin(), v.end());

    for(int i = 1; i<=m; i++){
        v[i].second = max(v[i].second, v[i-1].second);
    }

    ll ans = 1e18;
    do{
        for(int i = 0; i<n; i++){
            ll w = 0;
            for(int j = i; j<n; j++){
                w += input[j];
                req[i][j] = (lower_bound(v.begin(), v.end(), make_pair(w, -1ll))-1)->second;
            }
        }

        bool ok = true;
        for(int i = 0; i<n; i++){
            if(req[i][i] > 0)
                ok = false;
            memo[i] = 0;
        }
        if(!ok) continue;

        for(int i = 0; i<n; i++){
            for(int j = i+1; j<n; j++){
                memo[j] = max(memo[j], memo[i] + req[i][j]);
            }
        }

        ans = min(ans, memo[n-1]);

    }while(next_permutation(input.begin(), input.end()));

    if(ans == ll(1e18))
        ans = -1;
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
    ll N;
    cin >> N;
    vl A(2 * N), F(2 * N);
    rep(i, 2 * N) cin >> A[i];

    vc<pair<ll, ll>> P(2 * N);
    rep(i, 2 * N) P[i] = {A[i], i};
    sort(all(P));

    rep(i, N) F[P[i].second] = 1;

    ll cnt0 = 0, cnt1 = 0;
    string ans;
    rep(i, 2 * N){
        if(F[i] == 0){
            if(cnt1 > 0){
                cnt1--;
                ans += ')';
            }else{
                cnt0++;
                ans += '(';
            }
        }else{
            if(cnt0 > 0){
                cnt0--;
                ans += ')';
            }else{
                cnt1++;
                ans += '(';
            }
        }
    }

    cout << ans << endl;
}

#include <iostream>
#include <cmath>
#include <utility>
#include <algorithm>
#include <vector>

using namespace std;

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

int arr[400005];
pii a[400004];
vector<int> st;
int col[400005];
char s[400004];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=2*n;i++){
		cin>>arr[i];
		a[i]=mp(arr[i],i);
	}
	sort(a+1,a+2*n+1);
	for(int i=1;i<=n;i++){
		col[a[i].y]=1;
	}
	for(int i=1;i<=2*n;i++){
		if(st.empty() or col[st.back()]==col[i]) st.push_back(i);
		else{
			s[st.back()]='(';
			s[i]=')';
			st.pop_back();
		}
	}
	for(int i=1;i<=2*n;i++) cout<<s[i];

return 0;
}


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

const ll inf = 1LL<<60;

int main(){
    int n;  cin >> n;
    vector<pll> vec(n+1, make_pair(inf, -1*inf));
    rep(i, n)
    {
        ll x, c; cin >> x >> c;
        vec[c].first = min(vec[c].first, x);
        vec[c].second = max(vec[c].second, x);
    }
    vector<pll> dp(n+1, make_pair(0, 0));
    ll l_l = 0, l_r = 0;
    rep(i, n)
    {
        ll l = vec[i+1].first, r = vec[i+1].second;
        if(l == inf){
            dp[i+1].first = dp[i].first;
            dp[i+1].second = dp[i].second;
        }else{
            ll ff,fs,sf,ss;
            if(l_l < r)   ff = (r - l_l) + (r - l);
            else    ff = l_l - l;
            if(l_r < r)   sf = (r - l_r) + (r - l);
            else    sf = l_r - l;
            dp[i+1].first = min(dp[i].first + ff, dp[i].second + sf);
            if(l < l_l)   fs = (l_l - l) + (r - l);
            else    fs = r - l_l;
            if(l < l_r)   ss = (l_r-l) + (r-l);
            else    ss = r - l_r;
            dp[i+1].second = min(dp[i].first + fs, dp[i].second + ss);
            l_l = l;
            l_r = r;
            //cout << ff  << ':' << sf << ':' << fs << ':' << ss << endl;
            //cout << dp[i+1].first << ':' << dp[i+1].second << endl;
        }
    }
    ll ans = min(dp[n].first + abs(l_l), dp[n].second + abs(l_r));
    cout << ans << endl;
    return 0;
}

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

const int INF = 2e9;

vector<int> divisor(int x) {
	vector<int> ret;
	for (int i = 1; i * i <= x; i++) {
		if (x % i == 0) {
			ret.emplace_back(i);
			if (i * i != x) ret.emplace_back(x / i);
		}
	}
	return ret;
}

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

    int n;
	cin >> n;
	int mini = INF;
	unordered_map<int, int> mp;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		for (int d : divisor(x)) {
			mp[d] = __gcd(mp[d], x);
		}
		mini = min(mini, x);
	}
	int ans = 0;
	for (auto& e : mp) {
		if (e.first > mini) continue;
		ans += (e.first == e.second);
	}
	cout << ans << '\n';

    return 0;
}


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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

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

using namespace std;

typedef pair<int, int> P;

class AHealthMDeath {
public:
	void solve(std::istream& cin, std::ostream& cout) {
        int m,h;
        cin >> m >> h;
        cout << (h % m == 0 ? "Yes" : "No") << endl;
	}
};

signed main() {
	AHealthMDeath solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
int main(){
  int m,h;
  cin>>m>>h;

  cout<<(h%m==0?"Yes" : "No")<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define memf(a) memset(a,false,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define DEC(x) fixed<<setprecision(x)
#define FAST ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
const int M=1000000007;
const int MM=998244353;
const long double PI = acos(-1);
const long long INF=2e18;

template<typename T,typename T1>void amax(T &a,T1 b){if(b>a)a=b;}
template<typename T,typename T1>void amin(T &a,T1 b){if(b<a)a=b;}

ll power( ll b, ll e)
{
    if(e==0) return 1;
    if(e&1) return b*power(b*b,e/2);
    return power(b*b,e/2);
}
ll power(ll b,ll e,ll m)
{
    if(e==0) return 1;
    if(e&1) return b*power(b*b%m,e/2,m)%m;
    return power(b*b%m,e/2,m);
}
ll modinv(ll a,ll m)
{
    return power(a,m-2,m);
}

// #define _NCR_

int TLE_TERROR()
{
 	ll n,c,l,r,k,j,z,zz,ans=0;
 	cin>>n>>c;
 	map<ll,ll>m;
 	for(ll i=0;i<n;i++)
 	{
 		cin>>l>>r>>k;
 		m[l]+=k;
 		m[r+1]-=k;
 	}
 	for(auto it=m.begin();it!=m.end();it++)
 	{
 		if(it==m.begin())
 		{
 			z=it->S;
 			continue;
 		}
 		it->S+=z;
 		z=it->S;
 	}
 	// for(auto it=m.begin();it!=m.end();it++)
 	// {
 	// 	cout<<it->F<<" "<<it->S<<endl;
 	// }
 	for(auto it=m.begin();it!=m.end();it++)
 	{
 		if(it==m.begin())
 		{
 			z=it->F;
 			zz=it->S;
 			if(zz<c)
 				ans+=zz;
 			else
 				ans+=c;
 			continue;
 		}
 		// cout<<ans<<endl;
 		ll y=it->F-z;
 		y--;
 		if(zz<c)
 			ans+=y*(zz);
 		else
 			ans+=y*c;
 		if(it->S<c)
 			ans+=it->S;
 		else
 			ans+=c;
 		z=it->F;
 		zz=it->S;
 	}
 	cout<<ans<<endl;

 	return 0;
}

int main()
{
    FAST
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    ll int TESTS=1;
    // cin>>TESTS;
    #ifdef _NCR_
        initialvalues();
    #endif
    while(TESTS--)
        TLE_TERROR();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

void solve() {
    int N, C;
    cin >> N >> C;
    vector<pair<int, int>> event;
    REP(i, N) {
        int a, b, c;
        cin >> a >> b >> c;
        event.emplace_back(a - 1, c);
        event.emplace_back(b, -c);
    }
    sort(ALL(event));
    int ans = 0, fee = 0, t = 0;
    for (auto [x, y] : event) {
        if (x != t) {
            ans += min(C, fee) * (x - t);
            t = x;
        }
        fee += y;
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    solve();
}


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

int main() {
  long long s = 0, a, n;
  map<long long, long long> m;

  cin >> n;
  m[0] += 1;

  for (int i = 0; i < n; i++) {
    cin >> a;
    if (i % 2 == 0) s += a;
    else s -= a;

    m[s] += 1;
  }

  long long count = 0;
  for (auto i : m) {
    count += i.second * (i.second - 1) / 2;
  }

  cout << count << endl;

  return 0;
}

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
   #include <atcoder/all>
   using namespace atcoder;
#endif
#define all(v) v.begin(), v.end()
using in = long long;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i,a,b) for(in i=(in)(a);i<(in)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const in INF=(in)1e18+7;
const in inf=max(in(1e18)+7,in(1e9)+7);
using P=pair<in,in>;
using T=tuple<in,in,in>;
vector<in> dx={0,1,-1,0};
vector<in> dy={1,0,0,-1};
template <typename Typ>
bool chmin(Typ &a, const Typ& b) {if (a > b) {a = b; return true;} return false;}
template <typename Typ>
bool chmax(Typ &a, const Typ& b) {if (a < b) {a = b; return true;} return false;}
template <typename Typ>
Typ ceil(Typ x, Typ y){return (x+y-1)/y;}
#ifdef  _DEBUG
    #define debug(x) cout<<"debug:"<<x<<endl
    #define vdebug(x) cout<<"debug:";\
                      for(auto asdf:x) cout<<asdf<<" ";\
                      cout<<endl
#else
    #define debug(x)
    #define vdebug(x)
#endif
#define CASE(x) cout<<"Case #"<<x<<": "

int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
//cout << fixed << setprecision(12);
    in n;  cin>>n;
    vector<in> a(n);
        rep(i,n) cin>>a[i];
    vector<in> b(n);
    map<in,in> mp0,mp1;
    set<in> st0,st1;
    rep(i,n){
        if(i==0) b[i]=a[i];
        else{
            b[i] = a[i]-b[i-1];
            if(i%2==0) mp0[b[i]] ++;
            else mp1[b[i]] ++;
        }
    }

    in cnt=0,sum=0;
    for(in i=0;i<n-1;i++){
        cnt += mp0[sum]+mp1[-sum];
        if((i+1)%2==0) mp0[b[i+1]] --;
        else mp1[b[i+1]] --;
        if(i%2==0) sum += a[i];
        else sum -= a[i];
    }
    cout<<cnt<<"\n";

}/*
./problem.exe
*/


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int>people_weight(n);
    vector<int>bag_weight(n);
    vector<int>has_bag(n);
    set<pair<int,int>>people_priority;
    vector<int>has_my_bag(n);
    for(int i = 0; i < n; ++i){
        cin >> people_weight[i];
        people_priority.insert({people_weight[i],i});
    }
    for(int j = 0; j < n; ++j){
        cin >> bag_weight[j];
    }
    for(int i = 0; i < n; ++i){
        cin >> has_bag[i];
        has_bag[i]--;
        has_my_bag[has_bag[i]] = i;
    }
    bool works = true;
    vector<pair<int,int>>ans;
    while(people_priority.size()){
        auto nxt = *people_priority.begin();
        int me = nxt.second;
        people_priority.erase(people_priority.begin());
        if(has_bag[me] == me){
            continue;
        }
        else if(bag_weight[has_bag[me]] >= people_weight[me]){
            //cerr << me << " has a weight of "<< people_weight[me]
            //<<  " he is carrying a bag of weight of "<<
             // bag_weight[has_bag[me]] << "\n";
            works = false;
            break;
        }
        else{
            int who = has_my_bag[me];
            if(bag_weight[has_bag[who]] >= people_weight[who]){
                works = false;
                break;
            }
            has_my_bag[has_bag[me]] = who;
            has_bag[who] = has_bag[me];
            has_bag[me] = me;
            ans.emplace_back(me,who);
            //cerr << me << " and "<< who << " are exchanging bags\n";
        }
    }
    if(works){
        cout << ans.size() << "\n";
        for(auto a : ans){
            cout << a.first+1 << " " << a.second+1 << "\n";
        }

    }
    else {
        cout << "-1\n";
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> A(n), B(n), P(n);
    copy_n(istream_iterator<int>(cin), n, A.begin());
    copy_n(istream_iterator<int>(cin), n, B.begin());
    copy_n(istream_iterator<int>(cin), n, P.begin());

    for (auto &p: P)
        p--;

    for (int i = 0; i < n; i++){
        if (i != P[i] and A[i] <= B[P[i]]){
            puts("-1");
            return 0;
        }
    }

    vector<array<int, 2>> AI(n);
    for (int i = 0; i < n; i++){
        AI[i][0] = A[i];
        AI[i][1] = i;
    }

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

    vector<int> Q(n);
    for (int i = 0; i < n; i++)
        Q[P[i]] = i;

    vector<array<int, 2>> ans;
    for (const auto &[a, i]: AI){
        if (i == P[i])
            continue;
        const int j = Q[i];
        ans.push_back({i, j});
        Q[P[i]] = j;
        Q[i] = i;
        swap(P[i], P[j]);
    }

    cout << ans.size() << endl;
    for (const auto &[i, j]: ans)
        cout << i + 1 << " " << j + 1 << "\n";
}


#include <bits/stdc++.h>

using namespace std;

const int N = 15;

char str[N][N];

int n, m, ans;

struct Res {
  int x, y;
};

bool cmp1(Res a, Res b) {
  return a.x != b.x ? a.x < b.x : a.y < b.y;
}

bool cmp2(Res a, Res b) {
  return a.y != b.y ? a.y < b.y : a.x < b.x;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> str[i] + 1;
  }
  vector<Res> vt;
  vt.clear();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (str[i][j] == '#' && str[i - 1][j] == '.') {
        vt.push_back({i, j});
      }
    }
  }
  sort(vt.begin(), vt.end(), cmp1);
  for (int i = 1; i < vt.size(); i++) {
    if (vt[i].x == vt[i - 1].x && vt[i].y == vt[i - 1].y + 1) {
      continue;
    }
    ans++;
  }
  ans++;
  vt.clear();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (str[i][j] == '#' && str[i + 1][j] == '.') {
        vt.push_back({i, j});
      }
    }
  }
  sort(vt.begin(), vt.end(), cmp1);
  for (int i = 1; i < vt.size(); i++) {
    if (vt[i].x == vt[i - 1].x && vt[i].y == vt[i - 1].y + 1) {
      continue;
    }
    ans++;
  }
  ans++;
  vt.clear();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (str[i][j] == '#' && str[i][j + 1] == '.') {
        vt.push_back({i, j});
      }
    }
  }
  sort(vt.begin(), vt.end(), cmp2);
  for (int i = 1; i < vt.size(); i++) {
    if (vt[i].y == vt[i - 1].y && vt[i].x == vt[i - 1].x + 1) {
      continue;
    }
    ans++;
  }
  ans++;
  vt.clear();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (str[i][j] == '#' && str[i][j - 1] == '.') {
        vt.push_back({i, j});
      }
    }
  }
  sort(vt.begin(), vt.end(), cmp2);
  for (int i = 1; i < vt.size(); i++) {
    if (vt[i].y == vt[i - 1].y && vt[i].x == vt[i - 1].x + 1) {
      continue;
    }
    ans++;
  }
  ans++;
  cout << ans << "\n";
  return 0;
}

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

int main(){
  int n, a;
  cin >> n;
  string s, t;
  vector<int> p, q;
  cin >> s >> t;
  for(int i = 0; i < n; i++){
    if(s[i] == '0')
      p.push_back(i);
    if(t[i] == '0')
      q.push_back(i);
  }
  a = 0;
  if(p.size() != q.size()){
    a = -1;
  }
  else{
    for(int i = 0; i < p.size(); i++){
      if(p[i] != q[i])
        a++;
    }
  }
  cout << a << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e+7;
const ld PI = acos(-1);
const ld EPS = 1e-8;
const ll LINF = 1LL<<60;
const int INF = 1LL<<17;
#define rep(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define rrep(i, a, b) for(ll i=b-1; a<=i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
ll ceil(ll a, ll b) {if(a==0) return 0; return (a-1)/b+1;}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
using mint = Fp<1000000007>;

int main(){
    ll n;   cin >> n;
    if(n%2) cout << "Black" << endl;
    else cout << "White" << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define N 107
#define ldb long double
char s[N][N];
int g[N][N],cnt[N],n;
bool vis[N];
ldb f[N],cof[N];
void dfs(int v){
	cnt[v]++; vis[v]=1;
	for(int i=1;i<=n;i++)
		if(g[v][i]&&!vis[i])
			dfs(i);
}
int main(){
	//freopen("cpp.in","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%s",s[i]+1);
		for(int j=1;j<=n;j++)
			g[i][j]=s[i][j]-'0';
	}
	for(int i=1;i<=n;i++){
		memset(vis,0,sizeof(vis));
		dfs(i);
	}
	for(int i=1;i<=n;i++)cof[i]=(1.0-1.0*cnt[i]/n),f[i]=1;
	ldb ans=0;
	for(int o=1;o<=100000;o++){
		for(int i=1;i<=n;i++)ans+=f[i];
		for(int i=1;i<=n;i++)f[i]=f[i]*cof[i];
	}
	ans/=n;
	printf("%.10Lf\n",ans);
	return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;
//struct edge{lint to,num;};
//vector<int> bucket[MAX_N/1000];

int main(void){
    int N;
    cin >> N;
    string S,T;
    cin >> S >> T;
    int scnt=0,tcnt=0;
    rep(i,N){
        if(S[i]=='1') scnt++;
        if(T[i]=='1') tcnt++;
    }
    if(scnt<tcnt){
        cout << -1 << endl;
        return 0;
    }
    if((scnt-tcnt)%2){
        cout << -1 << endl;
        return 0;
    }
    lint ans=0;
    lint rem=scnt-tcnt;
    set<int> ones;
    rep(i,N) if(S[i]=='1') ones.insert(i);
    rep(i,N){
        if(S[i]==T[i]){
            if(S[i]=='1') ones.erase(ones.begin());
            continue;
        }
        if(S[i]=='1'){
            if(rem==0){
                cout << -1 << endl;
                return 0;
            }
            auto itr=ones.begin();
            itr++;
            int plc=*itr;
            ans+=plc-i;
            S[plc]='0';
            ones.erase(itr);
            ones.erase(i);
            rem-=2;
            if(rem<0){
                cout << -1 << endl;
                return 0;
            }
        }
        else if(T[i]=='1'){
            auto itr=ones.begin();
            int plc=*itr;
            ans+=plc-i;
            S[plc]='0';
            ones.erase(itr);
        }
    }
    cout << ans << endl;
}

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

using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) -> iterator of kth element (in sorted fashion : low - high)
// order_of_key(x) -> #numbers strictly less than x

#pragma GCC optimize("Ofast","unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")

#define sync ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define fi first
#define se second
#define endl '\n'

using pii = pair<int , int>;
using ll = long long;
using ld = long double;

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

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

    sync

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

    auto get = [&](vector<ll>& x, vector<ll>& y){
      set<ll> hi(all(x));
      set<ll, greater<ll>> lo(all(x));
      ll mx = 1e18;
      for (const ll& v : y){
        auto nx = hi.lower_bound(v);
        if (nx != hi.end()) mx = min(mx , *nx - v);
        auto fx = lo.lower_bound(v);
        if (fx != lo.end()) mx = min(mx , v - *fx);
      }
      return mx;
    };

    while(t--){
      int n;
      cin >> n;
      n = 2 * n;
      vector<ll> r, g, b;
      for (ll bu, i = 0; i < n; i++){
        char c;
        cin >> bu >> c;
        if (c == 'R') r.push_back(bu);
        else if (c == 'G') g.push_back(bu);
        else b.push_back(bu);
      }
      if ((r.size() & 1) or (b.size() & 1)){
        vector<ll> x, y, z;
        if (r.size() & 1){
          x = r;
          if (g.size() & 1){
            y = g; z = b;
          }
          else{
            y = b; z = g;
          }
        }
        else{
          x = g; y = b; z = r;
        }
        assert(x.size() & 1);
        assert(y.size() & 1);
        ll mx = get(x , y);
        if (z.size()){
          vector<array<ll , 2>> up, dwn;

          set<ll> hi(all(x));
          set<ll, greater<ll>> lo(all(x));
          for (int i = 0; i < z.size(); i++){
            ll nw = 1e18;
            auto nx = hi.lower_bound(z[i]);
            if (nx != hi.end()) nw = min(nw , *nx - z[i]);
            auto fx = lo.lower_bound(z[i]);
            if (fx != lo.end()) nw = min(nw , z[i] - *fx);
            up.push_back({nw , i});
          }

          hi = set<ll>(all(y));
          lo = set<ll, greater<ll>>(all(y));
          for (int i = 0; i < z.size(); i++){
            ll nw = 1e18;
            auto nx = hi.lower_bound(z[i]);
            if (nx != hi.end()) nw = min(nw , *nx - z[i]);
            auto fx = lo.lower_bound(z[i]);
            if (fx != lo.end()) nw = min(nw , z[i] - *fx);
            dwn.push_back({nw , i});
          }
          sort(all(up)); sort(all(dwn));
          for (int i = 0; i < up.size(); i++){
            int ix = -1;
            for (int j = 0; j < dwn.size(); j++){
              if (up[i][1] == dwn[j][1]) continue;
              ix = j;
              break;
            }
            assert(ix != -1);
            mx = min(mx , up[i][0] + dwn[ix][0]);
          }
        }
        cout << mx << endl;
      }
      else{
        cout << 0 << endl;
      }
    }
    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";
    return 0;
}


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

using ld = long double;
const ld et=1e-9;

int32_t main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m,k;
    cin >> n >> m >> k;
    vector <ld> p(n+1),e(n+1);
    int cnt[n+1]={};
    p[n]=e[n]=cnt[n]=0;
    for(int i=0;i<k;i++){
    	int x;
    	cin >> x;
    	cnt[x]=1;
    }
    for(int i=n-1;i>=0;i--){
    	if(!cnt[i]){
    		p[i]=(p[i+1]-p[min(i+m+1,n)])/m;
    		e[i]=(e[i+1]-e[min(i+m+1,n)])/m+1;
    	}
    	else e[i]=0,p[i]=1;
    	e[i]+=e[i+1],p[i]+=p[i+1];
    	cerr << i << '\n';
    }
    p[0]-=p[1],e[0]-=e[1];
    if(p[0]>=1-et)cout << "-1";
    else{
    	cout << setprecision(10) << e[0]/(1-p[0]);
    }
}

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

#define rep(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; (i) >= (int)(a); (i)--)
#define all(v) v.begin(), v.end()

typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = vector<V<T>>;

/* 提出時これをコメントアウトする */
#define LOCAL true

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif

typedef vector<ll> vec;
typedef vector<vec> mat;

ll M = 1e9+7;

mat mul(mat &A, mat &B) {
    mat C(A.size(), vec(B[0].size()));
    for (int i = 0; i < A.size(); i++) {
        for (int k = 0; k < B.size(); k++) {
            for (int j = 0; j < B[0].size(); j++) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
            }
        }
    }
    return C;
}

mat pow(mat A, ll n) {
    mat B(A.size(), vec(A.size()));
    for (int i = 0; i < A.size(); i++) {
        B[i][i] = 1;
    }
    while (n > 0) {
        if (n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

ll mod = 1e9+7;
long long mp(long long a, long long b) {
    long long res = 1;
    for (; b; b >>= 1) {
        if (b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
    }
    return res;
}

ll rev2, revm;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    constexpr char endl = '\n';

    rev2 = mp(2,mod-2);

    ll n,m,k;
    cin >> n >> m >> k;
    revm = mp(m, mod-2);

    V<ll> a(n);
    rep(i,0,n) cin >> a[i];
    V<ll> x(m), y(m);

    mat maze(n, vec(n));

    rep(i,0,m) {
        cin >> x[i] >> y[i];
        x[i]--;
        y[i]--;
        maze[x[i]][y[i]] = 1;
        maze[y[i]][x[i]] = 1;
    }

    V<ll> cnt(n);
    rep(i,0,m) {
        cnt[x[i]]++;
        cnt[y[i]]++;
    }
    mat A(n, vec(n,0));
    rep(i,0,n) {
        rep(j,0,n) {
            if (maze[i][j] == 1) {
                A[i][j] = rev2 * revm % mod;
            }
        }
    }
    rep(i,0,n) {
        // cnt/2m
        A[i][i] = ((cnt[i] * revm) % mod * rev2) % mod;
        A[i][i] %= mod;
        // (m-cnt)/m
        A[i][i] += ((m-cnt[i]) * revm) % mod;
        A[i][i] %= mod;
    }

    // rep(i,0,n) {
    //     rep(j,0,n) {
    //         cout << setw(10) << A[i][j];
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    mat B = pow(A, k);
    vec ret(n);

    rep(i,0,n) {
        rep(j,0,n) {
            ret[i] += B[i][j] * a[j] % mod;
            ret[i] %= mod;
        }
    }
    rep(i,0,n) {
        cout << ret[i] << endl;
    }
    return 0;
}


#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int>
#define vivi vector <vi>
#define v(s) vector<s>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine

const int max_n = 1005;

using namespace std;
int h,w;
bool usd[max_n][max_n],hv[4][max_n],a[max_n][max_n];
int next[4][max_n][max_n];

void read() {
	cin >> h >> w;

	rep(i,h){
		string s;
		cin >> s;
		rep(j,sz(s)) {
			if(s[j]=='#') {
				a[i][j]=1;
			}
		}
	}
}

void dfs(int x,int y,int type) {
	if(usd[x][y]) return;
	usd[x][y]=1;

	rep(i,h) {
		if(!a[i][y] or usd[i][y])continue;
		dfs(i,y,type);
		break;
	}

	rep(i,w) {
		if(!a[x][i] or usd[x][i])continue;
		dfs(x,i,type);
		break;
	}

	hv[0][y]=1;
	hv[1][x]=1;

}

int slv(int type = 0) {
	rep(i,h){
		rep(j,w)usd[i][j]=0;
	}

	rep(i,max(h,w)){rep(t,2)hv[t][i]=0;}

	rep(i,w) {
		if(a[0][i])dfs(0,i,type);
		if(a[h-1][i])dfs(h-1,i,type);
	}

	rep(i,h) {
		if(a[i][0])dfs(i,0,type);
		if(a[i][w-1])dfs(i,w-1,type);
	}

	int c=0;

	rep(i,h) {
		rep(j,w) {
			if(a[i][j] == 1 and usd[i][j]==0) {
				dfs(i,j,type);
				c++;
			}
		}
	}

	int e=0,r=0;
	crep(i,1,h-1) {
		if(hv[1][i]==0) e++;
	}
	crep(i,1,w-1) {
		if(hv[0][i]==0) r++;
	}
	return c+min(e,r);
}
int main(){
fcin;
	read();

	int ans = slv();

	cout << ans << "\n";

/*

*/

    return 0;
}

//#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x)                             \
    if (dbg) {                              \
        cerr << #x << " = " << (x) << endl; \
    }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    } else
        return 0;
}
void Yes(bool flag = true) {
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
void No(bool flag = true) {
    Yes(!flag);
}
void YES(bool flag = true) {
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
void NO(bool flag = true) {
    YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
}  // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans

//UnionFind
//How to use
//unite(x,y) : 集合 x と y を併合する. 併合済のとき false, 未併合のとき true が返される
//find(k) : 要素 k が属する集合を求める
//size(k) : 要素 k が属する集合の要素の数を求める
struct UnionFind {
    vector<int> data;

    UnionFind(int sz) {
        data.assign(sz, -1);
    }

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

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

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

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

int H, W;
string S[1010];
//vvi V[1010];

void solve() {
    UnionFind uf(H + W);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == 1) {
                uf.unite(i, j + H);
            }
        }
    }
    uf.unite(0, H - 1);
    uf.unite(0, H);
    uf.unite(0, H + W - 1);

    set<int> set_r, set_c;
    for (int i = 0; i < H; i++) {
        set_r.insert(uf.find(i));
    }
    for (int j = 0; j < W; j++) {
        set_c.insert(uf.find(j + H));
    }

    cout << min(set_r.size() - 1, set_c.size() - 1) << endl;
}

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

    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        cin >> S[i];
        for (auto& c : S[i]) {
            if (c == '.')
                c = 0;
            else
                c = 1;
        }
    }

    solve();
    return 0;
}


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

int main(){
  ll n;
  cin>>n;
  ll x=1000;
  ll ans=0;
  while(n>=x){
    ans+=n-x+1;
    x*=1000;
  }
  cout<<ans<<endl;
}

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

int main() {
    string x;
    cin >> x;
    int n = x.find('.');
    cout << x.substr(0,n) << endl;
    return 0;
}


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

using ll=long long;
using vpii=vector<pair<int,int>>;
using vpll=vector<pair<ll,ll>>;
using Graph_1=vector<vector<int>>;

const ll INF=1e18;
const int inf=1e9;
const double eps=1e-10;
const double pi=acos(-1);
const ll mod=1e9+7;
const ll MOD=998244353;

//数学関連
int num_order(ll N){
  int res=0;

  while(N>0){
    N/=10;
    res++;
  }

  return res;
}
template<typename T>
T GCD(T A,T B){
  if(B==0) return A;
  else return GCD(B,A%B);
}
template<typename T>
ll LCM(T A,T B){
  return A/GCD(A,B)*B;
}
template<typename T>
vpll prime_factorize(T N){
  vpll res;

  for(ll i=2;i*i<=N;i++){
    if(N%i!=0) continue;

    ll ex=0;
    while(N%i==0){
      ex++;
      N/=i;
    }

    res.push_back({i,ex});
  }

  if(N!=1) res.push_back({N,1});

  return res;
}
template<typename S,typename T,typename U>
ll modpow(S x,T p,U y){
  ll res=1;

  while(p>0){
    if(p&1) res=res*x%y;
    x=x*x%y;
    p>>=1;
  }

  return res;
}
template<typename S,typename T>
ll small_nCk(S n,T k){
  ll res=1,fact=1;

  for(int i=0;i<k;i++){
    res=res*(n-i)/fact;
    fact++;
  }

  return res;
}

//データ構造
struct UnionFind {
    //自身が親であれば、その集合に属する頂点数に-1を掛けたもの
    //そうでなければ親のid
    vector<int> r;

    UnionFind(int N){
        r=vector<int>(N,-1);
    }

    int root(int x){
        if (r[x]<0) return x;
        return r[x]=root(r[x]);
    }

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

    int size(int x){
        return -r[root(x)];
    }
};

struct Egde{
  int to;
  ll W;
  Egde(int to,ll W) : to(to),W(W) {}
};

using Graph_2=vector<vector<Egde>>;

int main(){

  string S;
  cin>>S;

  int ans=0;

  for(int i=0;i<S.size()-3;i++){
    if(S[i]=='Z'&&S[i+1]=='O'&&S[i+2]=='N'&&S[i+3]=='e') ans++;
  }

  cout<<ans<<endl;
}

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

int main() {
    string s;
    cin >> s;
    int t = s.size();
    for (int i = 0; i < t; i++) {
        if (i%2 == 0 && isupper(s[i])) {cout << "No" << endl; return 0;}
        if (i%2 == 1 && islower(s[i])) {cout << "No" << endl; return 0;}
    }
    cout << "Yes" << endl;
    return 0;
}


#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

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

ll modinv(ll a, ll mod){
  ll b=mod, u=1, v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a, b);
    u-=t*v; swap(u, v);
  }
  u%=mod;
  if(u<0)u+=mod;
  return u;
}

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

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}
/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N;
  cin >> N;
  llvec A(N);
  rep(i, N)cin >> A[i];
  vector<P> v(N+1, {0, 0});
  rep(i, N){
    v[i+1] = {v[i].fi+A[i], max(v[i].se, v[i].fi+A[i])};
    //cerr << v[i+1].fi << " " << v[i+1].se << endl;
  }

  ll cur=0;
  ll ans = 0;
  rep(i, N){
    ans = max(cur + v[i+1].se, ans);
    cur += v[i+1].fi;
  }
  cout << ans << endl;
  return 0;
}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <chrono>
#include <random>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(),a.end()
#define pb push_back
#define ti(a) ((int)(a-'0'))
#define tc(a) ((char)(a+'0'))
#define mod ((int)1e9+7)
using namespace std;
const int mxn = 3e5+10;
void solve(){
	int n ;
	cin>>n; vector<int> a(n) ;
	for(int& x:a)cin>>x ;
	vector<int> b=a;
	for(int i=1;i<n;i++)b[i]+=b[i-1];
	int coor= 0 , mx = 0 ,ans = 0 ;
	for(int i=0;i<n;i++){
		mx =max(mx,b[i]) ;
		ans = max(mx+coor,ans) ;
		coor+=b[i];
	}
	cout << ans ;
	return ;
}
signed main(){
	#ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin) ;
		freopen("out.txt","w",stdout) ;
	#endif
	int t=1 ;
//	cin>>t ;
	//cout << t << endl ;
	while(t--){
		solve() ;
		//cout << endl ;
	}
	return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
const int MN = 1e3 + 5, MOD = 1e9 + 7;
int dp[MN][2];
char o(char c) {return c == 'A' ? 'B' : 'A';}
int fpow (int base, int exp) {
	int res = 1; exp = max(exp,0);
	while (exp) {
		if (exp & 1) res = (long long)res * base % MOD;
		base = (long long)base * base % MOD;
		exp >>= 1;
	}
	return res;
}
int main () {
	int n; char caa, cab, cba, cbb;
	scanf ("%d %c %c %c %c",&n,&caa,&cab,&cba,&cbb);
	if (cab == 'B') {
		char ccc = caa;
		caa = o(cbb);
		cbb = o(ccc);
		cab = o(cab);
		cba = o(cba);
	}
	if (caa == 'A') printf ("1\n");
	else if (cba == 'A') {
		dp[1][0] = 1;
		for (int i = 2; i <= n; i++) {
			dp[i][0] = dp[i-1][0] + dp[i-1][1];
			if (dp[i][0] >= MOD) dp[i][0] -= MOD;
			dp[i][1] = dp[i-1][0];
		}
		printf ("%d\n",dp[n][1]);
	} else printf ("%d\n",fpow(2,n-3));
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
#define rep1(i,n) for(ll i=0;i<n;i++)
#define rep2(i,x,n) for(ll i=x;i<n;i++)
int main(){
    ll N;cin>>N;
    ll counter=0;
    vector<ll>V;
    for(ll i=2;i<=1e5;i++){
        for(ll j=2;j<=34;j++){
            ll number=pow(i,j);
            if(number<=N){
                ll flag=1;
                for(auto x:V){
                    if(number==x){
                        flag=0;
                        break;
                    }
                }
                if(flag==1){
                    V.push_back(number);
                }
            }else{
                break;
            }
        }
    }
    cout<<N-V.size()<<endl;
}

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

//#define int long long
typedef long long ll;
//const int INF = 2e9;
//const ll INF = 9e18;

void solve(){
    ll N;
    cin >> N;
    set<ll> st;
    ll ans = 0;
    for (ll i=2;i*i<=N;i++){
        if (st.find(i)!=st.end()) continue;
        for (ll j=i*i;j<=N;j*=i){
            if (st.find(j)==st.end()) {
                ans++;
                st.insert(j);
            }
        }
    }
    cout << N - ans << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(9);
    solve();
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
//typedef gp_hash_table<int, int> hashmap;
//typedef tree<pii, null_type, std::less<pii>, splay_tree_tag, tree_order_statistics_node_update> splaytree;
//typedef tree<pii, null_type, std::less<pii>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::binary_heap_tag> binheap;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::pairing_heap_tag> pairingheap;

template <typename T>
inline void read(T &x)
{
    T data = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        data = (data << 3) + (data << 1) + ch - '0';
        ch = getchar();
    }
    x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args)
{
    read(t);
    read(args...);
}

//mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 9;
const ll mod = 998244353;
ll a[maxn], b[maxn];
priority_queue<ll> pq;
signed main()
{
    //freopen("in.txt","r",stdin);
    //freopen("data.txt","w",stdout);
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(0);
    //std::cout.tie(0);
    int n;
    read(n);
    for (int i = 1; i <= n; ++i)
        read(a[i]);
    for (int i = 1; i <= n; ++i)
        read(b[i]);
    ll ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        pq.push(a[i]);
        ans = max(ans, pq.top() * b[i]);
        printf("%lld\n", ans);
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <numeric>
#define IINF 2147483647
#define INF 3223372036854775807
#define MOD 1000000007
#define mod 998244353
#define INT_MAX_ 2147483647
#define EPS (1e-10)
//#define REP(i, a, n) fo-r (ll i = a; i < (ll)(n); i++)
//#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
//#define REP(i,x,y) for(register int i=(x); i<(y); i++)
//#define REPE(i,x,y) for(register int i=(x); i<=(y); i++)
//#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define pi acos(-1.0)
#define me memset
// /#define bit(n,k) ((n>>k)&1)
#define lg length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<Pll,ll> a,pair<Pll,ll> b){
        return a.fi.se>b.fi.se;
}
ll cmp2(pair<ll,ll> a,pair<ll,ll> b){
    if(a.fi!=b.fi)
        return  a.fi>b.fi;
    else
        return a.se<b.se;
}
//----------------------------------------------------------------------
ll sum[200020];
ll maxs[200020];
//----------------------------------------------------------------------
int main(int argc, char * argv[]){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    //ll begin_t=clock();
    //freopen("big.txt", "r", stdin);
    //freopen("out3.txt", "w", stdout);
    //------------------------------
    ll n;cin>>n;
    V<ll>a(n);

    for(ll i=0;i<n;i++){
        cin>>a[i];
        if(i>0){
            sum[i]=a[i];
            sum[i]+=sum[i-1];
            //cout<<sum[i]<<Endl;
        }
        else sum[0]=a[0];
    }
    ll now = 0;
    for(ll i=0;i<n;i++)maxs[i]=-INF;
    for(ll i=0;i<n;i++){
        now+=a[i];
        chmax(maxs[i],now);
        if(i>0)chmax(maxs[i],maxs[i-1]);
    }
    ll ans = 0;
    now = 0;
    for(ll i=0;i<n;i++){
        //cout<<now<<" "<<maxs[i]<<Endl;
        chmax(ans,now+maxs[i]);
        now+=sum[i];
    }
    cout<<ans<<endl;
    //------------------------------
    //fclose(stdin);
    //fclose(stdout);
    //ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll a,b,x,y;
	cin>>a>>b>>x>>y;
	if(a==b)
	{
		cout<<x<<"\n";
	}
	else if(a<b)
	{
		int d=b-a;
		int ans=min(d*y+x,d*2*x+x);
		cout<<ans<<"\n";
	}
	else
	{
		int d=a-b;
		int ans=min({(d*y)+x,(d-1)*2*x+x,(d-1)*y+x});
		cout<<ans<<"\n";
	}
}

#include<bits/stdc++.h>
using namespace std;
#define ll         long long int
#define co         cout
#define ci         cin
#define sf1(n)     scanf("%lld",&n)
#define sf2(n,m)   scanf("%lld %lld",&n,&m);
#define sf3(n,m,p) scanf("%lld %lld %lld",&n,&m,&p);
#define pf1(n)     printf("%lld\n",n);
#define mem(a,b)   memset(a,b,sizeof(a))
#define en         cout<<endl;
#define pb         push_back
#define gcd(n,m)   __gcd(n,m)
#define lcm(n,m)   ((n)/__gcd(n,m))*(m)
#define fi         first
#define se         second
#define coy        cout<< "YES"<<endl;
#define con        cout<< "NO"<<endl;
#define f(i,a,b)     for(ll i=a;i<b;i++)
#define all(x)     (x).begin(),(x).end()
#define allr(x)    (x).rbegin(),(x).rend()
#define pi         pair<ll,ll>
#define br         break;
#define i64        long long
#define PI         2*acos(0.0)
#define MAXN       3*100000
ll n,m,k,g,mx,mn,res,c,x,y,z,w,p,q,r,l,s,t;

void solve()
{
    ll a,b;
    ci>>a>>b>>x>>y;
    if(a<b)
    {
        res=(((b-a)*2)+1)*x;
        res=min(res,x+(y*(b-a)));
        co<<res<<endl;
    }
    else if(a>b)
    {
        res=(((a-b)*2)-1)*x;
        res=min(res,x+(y*(a-b)));
        if(a-b>1) res=min(res,x+(y*(a-b-1)));
        co<<res<<endl;
    }
    else
    {
        co<<x<<endl;
    }

}

int main()
{

//#ifdef LOCAL
        //freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
        auto start_time = clock();
        cerr << setprecision(3) << fixed;
//#endif
        cout << setprecision(10) << fixed;
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        //ci>>t;
        t=1;
        for(ll ca=0;ca<t;ca++)
        {
            solve();
        }
//#ifdef LOCAL
        auto end_time = clock();
        cerr<< "Execution time: "<<(end_time-start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
//#endif
}


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

int main()
{
    int t;
    t=1;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> v;
        for(int i=0;i<n;i++)
        {
            int a;
            cin>>a;
            v.push_back(a);
        }
        sort(v.begin(),v.end());
        bool ans=true;
        for(int i=0;i<n;i++)
            if(v[i]!=i+1)
                ans=false;
        if(ans)
            cout<<"Yes";
        else
            cout<<"No";
    }
    return 0;
}

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

int main(){
  int N,X;  cin >> N >> X;
  vector<int> a(N);
  for( int i=0;i<N;i++ ){
    cin >> a[i];
  }
  vector<int> ans;
  int element=0;
  for(int i=0;i<N;i++){
    if(a[i]!=X) ans.push_back(a[i]);
    element++;
  }
  if(element==0) cout << endl;
  for(int i=0;i<ans.size();i++){
    //int kkk; cin >> kkk;
    cout << ans[i] << "";
    if(i==ans.size()-1){
      cout << endl;
      return 0;
    }
    cout << " ";
  }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define f(i,n) for(int i=0; i<n; ++i)
#define fi(i,st,n) for(int i=st; i<=n; ++i)
#define veci vector<int>
#define vecp vector<pair<int,int> >
#define vecl vector<ll>
int prime[100000+10];

ll lcm(ll a, ll b) {
	return a*b/__gcd(a,b);
}

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

ll sum(int arr[], int n) {
	ll res = 0;
	f(i,n) {
		res += arr[i];
	}
	return res;
}

void seive() {
	prime[1] = 0;
	for(int i=2; i<=100000; i++) {
		prime[i] = 1;
	}
	for(ll i=2; i<=100000; i++) {
		if(prime[i]) {
			for(ll j=i*i; j<=100000; j+=i) {
				prime[j] = 0;
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t = 1;
//	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll arr[n];
		vector<int> g[200000+10];
		f(i,n) {
			cin>>arr[i];
			g[arr[i]].pb(i);
		}
		int ans = 0;
		for(int i=0; i<n/2; i++) {
			if(arr[i] != arr[n-i-1]) {
				ans++;
				if(g[arr[i]].size() < g[arr[n-i-1]].size() ) {
					for(auto it : g[arr[i]]) {
						arr[it] = arr[n-i-1];
						g[arr[n-i-1]].pb(it);
					}
				}
				else {
					for(auto it : g[arr[n-i-1]]) {
						arr[it] = arr[i];
						g[arr[i]].pb(it);
					}
				}
			}
		}
//		for(int i=0; i<n; i++) cout<<arr[i]<<" ";
		cout<<ans;
	}
 	return 0;
}

#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(0), cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl '\n'
#define mkp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long  ull;

constexpr int INF = INT_MAX;

using namespace std;

int main(){
    fastIO;
    int N;
    cin >> N;
    ll sum = 0;
    while (N--)
    {
        ll a, b;
        cin >> a >> b;
        sum += ((b * (b + 1)) / 2) - ((a * (a + 1)) / 2) + a;
    }
    cout << sum;
    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")

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

	int n, i, j, k, s = 0, ans;
	cin >> n;
	vector<int> t(n);
	for (i = 0; i < n; i++) {
		cin >> t[i];
		s += t[i];
	}

	vector<bool> used(s + 1, false);
	used[0] = true;
	for (i = 0; i < n; i++) {
		for (j = s; j >= 0; j--) {
			if (used[j]) used[j + t[i]] = true;
		}
	}

	ans = s;
	for (i = 0; i <= s; i++) {
		if (used[i]) ans = min(ans, max(i, s - i));
	}
	cout << ans << "\n";

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n)  for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif

// $ cp-batch Cooking | diff Cooking.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address Cooking.cpp && ./a.out

/*

 6/6/2021

 14:31-14:46

 */

const int MAX_T=1e5+10;
const int MAX_N=1e2+1;
int A[MAX_N];
int N;

int dp[MAX_N][MAX_T];
void solve() {
  dp[0][0]=1;
  REP(i,N)REP(t,MAX_T) if(dp[i][t]) {
    dp[i+1][t]=1;
    dp[i+1][t+A[i]]=1;
  }
  int tot=accumulate(A,A+N,0);

  int res=MAX_T;
  REP(t,MAX_T) if(dp[N][t]) SMIN(res,max(t,tot-t));
  cout<<res<<endl;
}

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

  cin>>N;
  REP(i,N) cin>>A[i];
  solve();

  return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define endl '\n'
#define mod 1000000007
//#define mod 998244353
#define inf 1e18
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define clr(x,n) x.clear();x.resize(n,0)
#define precise(x) fixed<<setprecision(x)
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> pbds;

/**
 *  author: Ashutosh Nayak (@nayakashutosh9)
 *	created: [2020-11-08 17:28]
 *	"Compete against yourself"
**/

const int N = 3e5 + 5;
string s; int ans, len;
void check(int i, int val, int cur) {
	if (i == len) {
		if (val % 3 == 0) ans = min(ans, len - cur);
		return;
	}
	check(i + 1, val, cur);
	int x = (int)(s[i] - '0');
	val = val * 10 + x; cur++;
	check(i + 1, val, cur);
}
void solve() {
	int n; cin >> n;
	s = "";
	int p = n;
	while (p) {
		s = (char)((p % 10) + '0') + s;
		p /= 10;
	}
	ans = inf; len = (int)s.length();
	check(0, 0, 0);
	if (ans == inf || ans == len) cout << "-1\n";
	else cout << ans << endl;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int T = 1;
	//cin >> T; cin.ignore();
	for (int i = 1; i <= T; i++) {
		// cout << "Case #" << i << ": ";
		// clock_t start = clock();
		solve();
		// clock_t end = clock();
		// cout << (end-start) << endl;
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n;
const int N = 25;
typedef long long ll;
ll ans = 1e18;
ll a[N];
void dfs(int p,ll s){
    if(p == n){
        ans = min(ans,s);
        return;
    }
    ll sum = 0;
    for(int i=p;i<n;i++){
        sum |= a[i];
        dfs(i+1,s^sum);
    }
}
int main(){
    cin >> n;
    for(int i=0;i<n;i++) cin >> a[i];
    dfs(0,0);
    cout << ans << endl;
    return 0;
}


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

using namespace std;
const int maxn=1e3+5;
int a[maxn],b[maxn];
int book1[maxn],book2[maxn];
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		book1[a[i]]=1;
	}

	for(int i=1;i<=m;i++)
	{
		scanf("%d",&b[i]);
		book2[b[i]]=1;
	}
	for(int i=1;i<=1000;i++)
	{
		if((book1[i]==1 && book2[i]!=1) || (book1[i]!=1 && book2[i]==1))
			printf("%d ",i);
	}
	printf("\n");
}

//#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<set>
#include<unordered_map>
#include<stack>
#include<time.h>
using namespace std;
const int mod = 1e9+7;
const int N = 2e3+5;
const int maxn = 1e5+5;
const long long INF =1e18;
#define REP(i,n) for(int i = 1; i <= n; ++i)
#define REPA(i,j,n) for(ll i = j; i <= n; ++i)
#define RREP(i,n) for(ll i = n; i >= 1; --i)
#define REPR(i,j,n) for(ll i = n; i >= j; --i)
#define lll __int128
#define Accpect return 0;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,ll> pdi;
int t;
ll n, m;
ll a[N];
inline int read(){
    char c=getchar();
    int x=0,f=1;
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*f;
}
const int MOD = 998244353;
char g[503][503];
ll fast(ll a,ll b){
    ll res = 1;
    while(b){
        if(b & 1)res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res % MOD;
}
void solve(){
    ll w, h;
    cin>>h>>w;
    ll ans = 0;
    bool f = 1;
    for(int i = 1; i <= h; ++i)cin>>g[i]+1;
    for(int i  = 2 ; f && i <= w; ++i){
        int x = 1, y = i;
        int c1 = 0, c2 = 0;
        while(x <= h && y >= 1){
            if(g[x][y] == 'R')++c1;
            else if(g[x][y] == 'B')++c2;
            if(c1 && c2){
                f = 0;break;
            }
            ++x, --y;
        }
        if(!c1 && !c2) ++ans;
    }
    for(int i = 2; f && i < h ; ++i){
        int x = i , y = w;
        int c1 = 0, c2 = 0;
        while(y >= 1 && x <= h){
            if(g[x][y] == 'R')++c1;
            else if(g[x][y] == 'B')++c2;
            if(c1 && c2){
                f = 0;break;
            }
            --y,++x;
        }
        if(!c1 && !c2)++ans;
    }
    if(!f) cout<<0<<'\n';
    else {
        if(g[1][1] == '.')++ans;
        if(g[h][w] == '.')++ans;
        cout<<fast(2,ans)<<'\n';
    }
}

signed main(){
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //.cout.tie(0);
#ifdef ACM
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    t = 1;

    //srand((unsigned)time(NULL));
//    cin>>t;

    while (t--){

        solve();

    }

    fclose(stdin);
    fclose(stdout);
    //-------------
    Accpect;
    //-------------
}


#include<bits/stdc++.h>
using namespace std;
bool check(int num){
	int num1 = num, num2 = num;
	while(num1 != 0){
		int x = num1 % 10;
		num1 /= 10;
		if(x == 7)
		    return 0;
	}
	while(num2 != 0){
		int x = num2 % 8;
		num2 /= 8;
		if(x == 7)
		    return 0;
	}
	return 1;
}
int main(){
	int N = 0, ans = 0;
	cin >> N;
	for(int i = 1 ; i <= N ; i ++)
	    if(check(i))
	        ans ++;
	cout << ans;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
int n, res = 0;

void f(int i, int x, int cnt = 0) {
	if (i == n) {
		if (x % 3 == 0) {
			res = max(res, cnt);
		}
		return;
	}
	f(i + 1, x + s[i] - '0', cnt + 1);
	f(i + 1, x, cnt);
}

void solve() {
	cin >> s;
	n = s.size();
	f(0, 0);
	cout << (n - res == n ? -1 : n - res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	for (int i = 1; i <= t; i++) {
		solve();
	}
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <string>

#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()

typedef long long ll;

using namespace std;

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    int temp;
    ll sum=0;
    rep(i,N) cin >> A[i];
    rep(i,N){
        cin >> temp;
        sum += A[i]*temp;
    }
    if(sum==0) cout << "Yes" << endl;
    else cout << "No" << endl;
}

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T>
bool chmin(T& a, const T& b) { return (b < a) ? (a = b, true) : false; }
template<class T>
bool chmax(T& a, const T& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}

long long gcdsolve(long long a, long long b, long long &x, long long &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    long long d = gcdsolve(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        i64 ans = LLONG_MAX / 2;
        i64 x, y, p, q;
        cin >> x >> y >> p >> q;
        rep(i, y)
        {
            rep(j, q)
            {
                i64 n, m;
                i64 c = p - x - i + j;
                i64 a = 2 * (x + y);
                i64 b = -(p + q);
                auto d = gcdsolve(a, b, n, m);
                if(c % d) continue;
                a /= d;
                b /= d;
                m *= (c / d);
                auto u = m % a;
                if (u < 0)
                {
                    if(a < 0) u -= a;
                    else u += a;
                }
                i64 v = ((p + q) * u + p + j);
                chmin(ans, v);
            }
        }
        cout << (ans != (LLONG_MAX / 2)? to_string(ans): "infinity") << endl;
    }
}


/*
 * Author: $%U%$
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) ((x)&(-x))
#define sz(x) ((int)x.size())
#define fr(x) freopen(x,'r',stdin)
#define fw(x) freopen(x,'w',stdout)
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define rep(i,a,b) for(int i = (a);i<=(b); ++i)
#define per(i,a,b) for(int i = (a);i>=(b); --i)
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VB;
typedef double db;

template <class T> void clear(T& a) { T().swap(a);}
template <class T,class S>
inline bool upmin(T& a,const S&b){ return a>b ? a=b,1:0;}
template <class T,class S>
inline bool upmax(T&a, const S&b){ return a<b? a=b,1:0;}

//fast read(improved by fread)
char buf[1<<21],*p1=buf,*p2=buf;
inline int getc(){
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
}
inline int redi() {
    int ret = 0,f = 0;char ch = getc();
    while (!isdigit (ch)) {
        if (ch == '-') f = 1;
        ch = getc();
    }
    while (isdigit (ch)) {
        ret = ret * 10 + ch - 48;
        ch = getc();
    }
    return f?-ret:ret;
}

//global variable
const int N = 1;
const int mod = 1e9+7;
const double eps = -1e-12;
int n;
long double a,b,m;
//

//code from here! Come on! Have a pleasant experience~
int main()
{
    IO;
    cin>>n>>m;

    long double res = m;

    rep(i,1,n){
        cin>>a>>b;
        res -= (a * b/100.0);
        // cout<<res<<endl;
        if(res<eps){
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int INF=1e18;
void chmin(int&a,int b){if(a>b)a=b;}
signed main(){
  int N; cin>>N;
  string S; cin>>S;
  vector<vector<int>> dp(N,vector<int>(26,INF));
  dp[0][S[0]-'a']=0;
  int ans=INF;
  for(int i=0;i<N;i++){
    for(int j=0;j<26;j++)if(S[i]!='a'+j){
      if(i==N-1)chmin(ans,dp[i][j]);
      else chmin(dp[i+1][S[i+1]-'a'],dp[i][j]+1);
    }
    if(i!=N-1){
      for(int j=0;j<26;j++)chmin(dp[i+1][j],dp[i][j]);
    }
  }
  cout<<(ans==INF?-1:ans+1)<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < (n); ++i)
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;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}

ll INF = 1LL<<60;

int main() {
    int N; cin >> N;
    int M; cin >> M;
    vecl H(N), W(M);
    REP(i,N) cin >> H[i];
    REP(i,M) cin >> W[i];

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

    vecl L(200010), R(200010);
    for(int i = 2; i < N; i++) {
        L[i] = L[i-2] + H[i-1] - H[i-2];
        R[i] = R[i-2] + H[N-i+1] - H[N-i];
    }

    ll ans = INF;

    for(ll w : W) {
        int i = lower_bound(H.begin(),H.end(),w) - H.begin();
        if(i%2 == 0) chmin(ans,L[i]+R[N-i-1] + H[i]-w);
        else chmin(ans,L[i-1] + R[N-i] + w-H[i-1]);
    }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf

int main(){
    int H,W;
    cin >> H >> W;
    vector<int > A(H*W);
    int amin=10000;
    for(int i=0; i<H*W; i++){
        cin >> A.at(i);
        amin = min(amin,A.at(i));
    }
    int ans=0;
    for(int i=0; i<H*W; i++){
        ans += A.at(i) - amin;
    }

    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ull> vull;
typedef pair<int, int> pii;

#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORDOWN(i, a, b) for (int i = a; i >= b; --i)
#define ff first
#define ss second
#define pb push_back

#define debug(x) cout << #x << " = " << x << '\n';

ull func(vector<ll> &v) {
    ull ans = 0;
    int n = v.size();
    for (int i = 0; i < n - 1; ++i) {
        for (int j = i + 1; j < n; ++j) {
            ans += abs(v[i] - v[j]);
        }
    }
    return ans;
}
/*
 * 3
10002 8668 92833
*/
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    vector<ll> v(n);
//    vector<ll> v1(n);

    for (int i = 0; i < n; ++i) {
        cin >> v[i];
//        v1[i] = v[i];
    }

    sort(v.rbegin(), v.rend());
    vector<ll> pref(n + 1);
    pref[0] = v[0];

    for (int i = 1; i < n; ++i) {
        pref[i] = pref[i - 1] + v[i];
    }

    ull ans = 0;
    for (ll i = 0; i < n - 1; ++i) {
        ans += abs(((ll)n - i - 1) * v[i] - (pref[n - 1] - pref[i]));
    }

    cout << ans;

    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 ll IINFLL = 1ll << 61;
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 };

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

    ll c_a = 0, c_b = -IINFLL, c_c = IINFLL;
    rep(i, n) {
        ll a, t;
        cin >> a >> t;

        if ( t == 1 ) {
            c_a = c_a + a;
            c_b = a + c_b;
            c_c = a + c_c;
        } else if ( t == 2 ) {
            c_b = max(a, c_b);
            c_c = max(a, c_c);
        } else {
            c_b = min(a, c_b);
            c_c = min(a, c_c);
        }
    }

    int q;
    cin >> q;
    rep(i, q) {
        ll x;

        cin >> x;
        cout << min(max(x + c_a, c_b), c_c) << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,k,n)      for(int i=k;i<n;i++)
#define all(x)          x.begin(),x.end()
#define sz(x)           (int) x.size()
#define pii             pair<int,int>
#define debug(x)        cerr << #x << " : " << x << "\n";
#define int             ll
/*############################ !!SOLVE!! ################################*/

struct interval{
    int t;
    int x;
    int y;
};

void solve(){

  int n;
  cin >> n;

  vector <interval> seg(n);

  rep(i,0,n){
      cin >> seg[i].t >> seg[i].x >> seg[i].y;
  }
  int intersections = 0;
  for(int i = 0 ;i < n ; i++){
      interval one = seg[i];
      for(int j = i+1;j<n;j++){
          interval two = seg[j];

          if(one.x < two.x){
              if(two.x < one.y){
                  intersections++;
              }

              if(one.y == two.x){
                  if((one.t == 1 || one.t == 3) and (two.t == 1 || two.t == 2))
                    intersections++;
              }

          }

          else{
              if(one.x < two.y){
                  intersections++;
              }

              if(one.x == two.y){
                  if((two.t == 1 || two.t == 3) and (one.t == 2 || one.t == 1))
                    intersections++;
              }
          }
      }

  }

  cout << intersections << "\n";

}

/*######################## !!TESTCASES!! ################################*/
signed main(){
    ios ::sync_with_stdio(0);
    cin.tie(0);
    int testcases = 1;

    while(testcases -->0){
        solve();
    }
}

#include <bits/stdc++.h>
#include <set>
#define ll long long
#include <string>
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323846
#define mod 1000000007
#define INF 1e18
// typedef pair<ll, ll> pi;
#define rep(i,n) for(i=0;i<n;i++)
#define repk(i,k,n) for(i=k;i<n;i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define oset tree <pair<ll int ,ll int>, null_type, less<pair<ll int,ll int> >, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define see(args...)                             \
        {                                            \
            cerr << "LINE " << __LINE__;             \
            string _s = #args;                       \
            replace(_s.begin(), _s.end(), ',', ' '); \
            stringstream _ss(_s);                    \
            istream_iterator<string> _it(_ss);       \
            err(_it, args);                          \
            cerr << endl;                            \
        }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << ' ' << *it << " = " << a;
    err(++it, args...);
}
void fastio()
{
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

}
void solve()
{
    string s; cin >> s;
    string s2 = "ZONe";
    ll int i, ans = 0;
    rep(i, 12)
    {
        if (i + 3 < 12)
        {
            if (s.substr(i, 4) == s2)ans++;
        }
    }
    cout << ans << endl;

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

}