#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;

int main(){
  int n; cin >> n; ll a[n];
  rep(i, n){
    cin >> a[i];
  }
  if(a[0]!=0){
    cout << -1 << endl; return 0;
  }
  rep(i, n-1){
    if(a[i+1]>a[i]+1){
      cout << -1 << endl; return 0;
    }
  }
  ll ans = 0;
  for(int i=1; i<n; i++){
    if(a[i]==a[i-1]+1) ans++;
    else ans+=a[i];
  }
  cout << ans << endl;
}

#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, vector<ll>> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    rep(ti,t){
        int n;
        cin >> n;
        vector<ll> a(n);
        rep(i,n) cin >> a[i];
        string s;
        cin >> s;
        if(s.back() == 1){
            cout << 1 << endl;
            continue;
        }
        vector<P> v;
        v.push_back(P(0, {}));
        for(int i = n-1; i >= 0; --i){
            if(v.back().first == (s[i] - '0')){
                v.back().second.push_back(a[i]);
            }
            else{
                int nex = 1 - v.back().first;
                v.push_back(P(nex, {a[i]}));
            }
        }
        for(auto &p : v){
            sort(p.second.begin(), p.second.end());
        }
        int sz = v.size();
        vector<ll> buf;
        bool f = true;
        rep(i,sz){
            if(v[i].first == 0){
                for(ll x : v[i].second) buf.push_back(x);
            }
            else{
                auto memo = buf;
                int sz2 = memo.size();
                int rank = 0;
                for(int j = 59; j >= 0; --j){
                    int pivot = -1;
                    for(int k = rank; k < sz2; ++k){
                        if(memo[k] >> j & 1){
                            pivot = k;
                            break;
                        }
                    }
                    if(pivot == -1) continue;
                    swap(memo[pivot], memo[rank]);
                    for(int k = 0; k < sz2; ++k){
                        if(k != rank && (memo[k] >> j & 1)) memo[k] ^= memo[rank];
                    }
                    for(ll &x : v[i].second){
                        if(x >> j & 1) x ^= memo[rank];
                    }
                    ++rank;
                }
                for(ll x : v[i].second){
                    if(x != 0){
                        f = false;
                        break;
                    }
                }
                if(!f) break;
            }
        }
        cout << !f << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)

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

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

vector<int> m(8, -1);
vector<int> ans(8, -1);

bool valid() {
	bool board[8][8] = {0};
	rep(i, 8) {
		if(m[i] == -1) continue;
		board[i][m[i]] = true;
	}
	const int dr[4] = {1, 1, 0, -1};
	const int dc[4] = {0, 1, 1, 1};
	rep(i, 8) {
		if(m[i] == -1) continue;
		rep(j, 4) {
			int q_r = i+dr[j], q_c = m[i]+dc[j];
			while(0<=q_r && q_r<8
					&&0<=q_c && q_c<8) {
				if(board[q_r][q_c]) return false;
				q_r += dr[j], q_c += dc[j];
			}
		}
	}
	return true;
}

void dfs(int r) {
	if(r == 8) {
		ans = m;
		return;
	}

	if(m[r] != -1) {
		dfs(r+1);
		return;
	}

	rep(i, 8) {
		m[r] = i;
		if(valid()) dfs(r+1);
		m[r] = -1;
	}
}

int main(void){
	int k; cin >> k;
	rep(i, k) {
		int r, c; cin >> r >> c;
		m[r] = c;
	}
	dfs(0);
	// rep(i, 8) {
	// 	cerr << ans[i] << " ";
	// }
	char board[8][8] = {0};
	rep(i, 8) rep(j, 8) board[i][j] = '.';
	rep(i, 8) {
		board[i][ans[i]] = 'Q';
	}
	rep(i, 8) {
		rep(j, 8) {
			cout << board[i][j];
		}
		cout << endl;
	}

	return 0;
}

#include <iostream>
#include <vector>
//---------------------------------------------------------------------------

enum TSquareState
{
	ssPlaceable,
	ssAttacked,
	ssQueen
};

typedef std::vector<std::vector<TSquareState> > TBoard;
//---------------------------------------------------------------------------

TBoard PutQueen(const TBoard& PreBoard, int Row, int Col)
{
	TBoard Board = PreBoard;

	for(int i = 0; i < 8; ++i){
		Board[Row][i] = ssAttacked;
	}
	for(int i = 0; i < 8; ++i){
		Board[i][Col] = ssAttacked;
	}
	for(int i = Row, j = Col; i < 8 && j < 8; ++i, ++j){
		Board[i][j] = ssAttacked;
	}
	for(int i = Row, j = Col; i >= 0 && j >= 0; --i, --j){
		Board[i][j] = ssAttacked;
	}
	for(int i = Row, j = Col; i < 8 && j >= 0; ++i, --j){
		Board[i][j] = ssAttacked;
	}
	for(int i = Row, j = Col; i >= 0 && j < 8; --i, ++j){
		Board[i][j] = ssAttacked;
	}
	Board[Row][Col] = ssQueen;
	return Board;
}
//---------------------------------------------------------------------------

bool PutQueens(TBoard& Board, int QueenCount)
{
	if(QueenCount == 0) return true;
	for(int i = 0; i < 8; ++i){
		for(int j = 0; j < 8; ++j){
			TBoard TempBoard = PutQueen(Board, i, j);
			if(Board[i][j] == ssPlaceable && PutQueens(TempBoard, QueenCount - 1)){
				Board = TempBoard;
				return true;
			}
		}
	}
	return false;
}
//---------------------------------------------------------------------------

void OutputEightQueens(const TBoard& Board, std::ostream& Stream)
{
	for(int i = 0; i < 8; ++i){
		for(int j = 0; j < 8; ++j){
			char Square = Board[i][j] == ssQueen ? 'Q' : '.';
			Stream << Square;
		}
		Stream << std::endl;
	}
}
//---------------------------------------------------------------------------

int main()
{
	int Count;
	std::cin >> Count;

	TBoard Board(8, std::vector<TSquareState>(8, ssPlaceable));
	for(int i = 0; i < Count; ++i){
		int Row, Col;
		std::cin >> Row >> Col;
		Board = PutQueen(Board, Row, Col);
	}
	PutQueens(Board, 8 - Count);
	OutputEightQueens(Board, std::cout);
	return 0;
}


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

int main()
{
  string s;
  getline(cin, s);
  while (getline(cin, s)) {
    string::size_type b = 0, e;
    do {
      e = s.find("Hoshino", b);
      if (e != string::npos) {
	s[e+6] = 'a';
      }
      b = e + 1;
    } while (e != string::npos);
    cout << s << endl;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a;
    string str,Hoshino="Hoshino";
    cin>>n;
    getline(cin,str);
    for(int i=0;i<n;i++){
        getline(cin,str);
        for(int j=0;str[j]!='\0';j++){
            if(str[j]=='H'){
                for(int k=j;k<7+j;k++){
                    if(str[k]!=Hoshino[k-j])break;
                    if(k==j+6)str[k]='a';
                }
            }
        }
        cout<<str<<endl;
    }
}


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

# define ll long long
# define VI vector<int>
# define VLL vector<ll>
# define PII pair<int,int>
#define mp make_pair
#define pb push_back
# define PI acos(-1)
# define ld long double
const int MxN = 200001 ;
# define FOR(i,a,b) for(int i = a ; i < b ; ++i)
# define RFOR(i,a,b) for(int i = a ; i >= b ; --i)

/* VARIABLES */
int n , k ;
VI a(MxN) ;

/* END OF VARIABLES */

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

	cin >> n >> k ;
	FOR(i,0,n)
		cin >> a[i] ;
	if(k>=3*log2((ld)n))
	{
		FOR(i,0,n)
			cout<<n<<' ';
		return 0;
	}
	FOR(i,0,k) {
		VI tmp(n+1) ;
		FOR(j,0,n) {
			int l = max(0,j-a[j])  ;
			int r = min(n-1,j+a[j]) ;
			tmp[l]++;
			if(r+1 < n)
				tmp[r+1]--;
		}
		FOR(j,1,n) {
			tmp[j] += tmp[j-1] ;
		}
		FOR(j,0,n){
			a[j] = tmp[j] ;
		}
	}

	FOR(i,0,n)
	cout << a[i] << " " ;

	return 0 ;
}


#include <bits/stdc++.h>
#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 namespace std;
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>>;

int main() {
    ll x; cin >> x;
    ll ans = x/11*2;
    ll rest = x%11;
    if(rest >= 7) ans += 2;
    else if(rest > 0) ans++;
    cout << ans << endl;
}

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

int main()
{
    int a, b;
    cin >> a >> b;
    b*=2, b-=a;
    cout << b;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 1000000 + 1000;

int n;
char s[MAXN], t[MAXN];

void no()
{
	cout << -1 << endl;
	exit(0);
}

vector<int> sp, ri;

void init()
{
	int p = n;
	for(int i = n; i >= 1; i--)
		if(i == n || t[i] != t[i + 1] || i == p)
		{
			ri.push_back(i);
			while(p >= 1 && s[p] != t[i])
				p--;
			if(p <= 0) no();
			sp.push_back(p);
			p = p - 1;
			// cout << i << ' ' << p << endl;
		}
	reverse(sp.begin(), sp.end());
	reverse(ri.begin(), ri.end());
	sp.push_back(n + 1), ri.push_back(n + 1);
	// for(int i = 0; i < sp.size(); i++)
	// 	cout << sp[i] << ' ' << ri[i] << endl;
}

void solve()
{
	if(strcmp(s + 1, t + 1) == 0)
	{
		cout << 0 << endl;
		exit(0);
	}
	int ans = 0;
	int pos = 0, cnt = 0;
	for(int i = 0; i < sp.size() - 1; i++)
	{
		if(cnt > 0) cnt--;
		while(i + cnt < sp.size() && sp[i + cnt] - cnt < ri[i])
			cnt++;
		ans = max(ans, cnt);
		// cout << cnt << endl;
	}
	cout << ans << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	// freopen("game.in", "r", stdin);
	// freopen("game.out", "w", stdout);

	cin >> n;
	cin >> (s + 1);
	cin >> (t + 1);

	init();
	solve();

	return 0;
}
/*
12
abxxcdefghij
aabbbbccdeee
*/
/*
8
bbaabbxx
bbbbbbba
*/
/*
10
cydjgcbckr
cccdggggcc
*/
/*
10
aaaaaaaa
aaaaaaaa
*/

#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1001000
#define pb push_back
int n,ans,pos[N],sum[N];
char s[N],p[N];
vector<int>ve[30];
vector<int>::iterator it;
int p1[N],p2[N],m;
bool check(int x)
{
	for(int i=1;i+x<=m;++i)
		if(p1[i+x]-x<p2[i])
			return false;
	return true;
}
int main()
{
	scanf("%d%s%s",&n,s+1,p+1);
	int i,last=n+1,flag=0,r;
	for(i=1;i<=n;++i) if(s[i]!=p[i]) flag=1;
	if(!flag) puts("0");
	else
	{
		flag=0;
		for(i=1;i<=n;++i) ve[s[i]-'a'].pb(i);
		for(r=i=n;i>=1;--i)
		{
			if(i==1||p[i-1]!=p[i])
			{
				last=min(last,i+1);
				it=lower_bound(ve[p[i]-'a'].begin(),ve[p[i]-'a'].end(),last);
				if(it==ve[p[i]-'a'].begin()) {flag=1; break;}
				--it;
				last=*it;
				++m;
				p1[m]=*it; p2[m]=r;
				r=i-1;
			}
		}
		if(flag) puts("-1");
		else
		{
			for(i=1;i<=m;++i) if(i<m+1-i) swap(p1[i],p1[m+1-i]),swap(p2[i],p2[m+1-i]);
			int l=0,mid; r=m;
			while(l<r)
			{
				mid=(l+r)>>1;
				if(check(mid)) r=mid;
				else l=mid+1;
			}
			printf("%d",r);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<ll,ll,ll> T;
const long long INF = 1LL<<60;
const int MOD = 1000000000+7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
// cout << fixed << setprecision(10) << ans << endl;
// *min_element(c + l, c + r) *max_element(c + l, c + r)
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// struct Edge {
//   int to, id;
//   Edge(int to, int id): to(to), id(id) {}
// };

int main() {
  int N,u,v;cin>>N>>u>>v;
  u--;v--;
  vector<vector<int>> tree(N,vector<int>());
  rep(i,N-1) {
    int a,b;cin>>a>>b;
    a--;b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  vector<bool> u_done(N,false),v_done(N,false);
  vector<int> u_dis(N,0),v_dis(N,0);
  u_done[u] = true;
  v_done[v] = true;
  queue<int> uq,vq;
  uq.push(u);
  vq.push(v);
  while(!uq.empty()) {
    int now = uq.front();
    uq.pop();
    for(auto nxt : tree[now]) {
      if(!u_done[nxt]) {
	u_done[nxt]= true;
	u_dis[nxt] = u_dis[now] + 1;
	uq.push(nxt);
      }
    }
  }
  // rep(i,N) {
  //   cout<<u_dis[i]<<endl;
  // }
  vector<int> leaf;
  while(!vq.empty()) {
    int now = vq.front();
    vq.pop();
    int chi_siz = 0;
    for(auto nxt : tree[now]) {
      if(!v_done[nxt]) {
	chi_siz++;
	v_done[nxt]= true;
	v_dis[nxt] = v_dis[now] + 1;
	vq.push(nxt);
      }
    }
    // cout<<now<<" "<<chi_siz<<endl;
    if(chi_siz == 0) leaf.push_back(now);
  }
  int ans = 0;
  for(auto lf : leaf) {
    // cout<<lf<<endl;
    if(u_dis[lf] < v_dis[lf]) {
      ans = max(ans,v_dis[lf]-1);
    }
  }
  cout<<ans<<endl;
}

//In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define Mp make_pair

typedef pair < int , int > pii;

const int N = 2e5 + 10;

int mark[N], cmp[2][N];
vector < int > adj[2][N];
map < pii , int > mp;

void dfs(int v, int t, int c){
    mark[v] = 1;
    cmp[t][v] = c;
    for (int u : adj[t][v])
        if (!mark[u])
            dfs(u, t, c);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m1, m2;
    cin >> n >> m1 >> m2;
    for (int i = 0; i < m1; i ++){
        int u, v;
        cin >> u >> v;
        adj[0][u].pb(v);
        adj[0][v].pb(u);
    }
    for (int i = 0; i < m2; i ++){
        int u, v;
        cin >> u >> v;
        adj[1][u].pb(v);
        adj[1][v].pb(u);
    }
    int c = 0;
    for (int i = 1; i <= n; i ++)
        if (!mark[i])
            dfs(i, 0, c ++);
    fill(mark, mark + N, 0);
    c = 0;
    for (int i = 1; i <= n; i ++)
        if (!mark[i])
            dfs(i, 1, c ++);
    for (int i = 1; i <= n; i ++)
        mp[Mp(cmp[0][i], cmp[1][i])] ++;
    for (int i = 1; i <= n; i ++)
        cout << mp[Mp(cmp[0][i], cmp[1][i])] << ' ';
    return 0;
}


#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mpcy(a, b) memcpy(a, b, sizeof(a))
#define rg register
using namespace std;
typedef long long LL;
const int MAXN = 100005;

template <typename T> inline void read(T &AKNOI) {
    T x = 0, flag = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') flag = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    AKNOI = flag * x;
}

template <typename T> inline void cmin(T &x, T y) {
    if (x > y) x = y;
}

template <typename T> inline void cmax(T &x, T y) {
    if (x < y) x = y;
}

int n, a[MAXN];

void output(int win) {
	puts(win? "First" : "Second");
	exit(0);
}

int gcd(int x, int y) {
	return (y == 0)? x : gcd(y, x % y);
}

void play(int player) {
	int even = 0;
	for (int i=1;i<=n;++i) {
		even += !(a[i] & 1);
	}
	if (even & 1) output(1 ^ player);
	if (n - even > 1) output(0 ^ player);
	int d = 0;
	for (int i=1;i<=n;++i) {
		if (a[i] == 1) output(0 ^ player);
		d = gcd(d, a[i] - (a[i] & 1));
	}
	for (int i=1;i<=n;++i) {
		a[i] /= d;
	}
	play(player ^ 1);
}

void solve() {
	read(n);
	for (int i=1;i<=n;++i) {
		read(a[i]);
	}
	play(0);
}

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

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

int main(){
	int num_battle;
	int a, b;
	int point_a = 0;
	int point_b = 0;
	string cards;

	cin >> num_battle;

	for(; num_battle != 0;){
		while(num_battle > 0){
			cin >> a >> b;
			if(a == b){
				point_a += a;
				point_b += b;
			}else if(a > b){
				point_a += a + b;
			}else{
				point_b += a + b;
			}
			num_battle--;
		}
		cout << point_a << " " << point_b << endl;
		// initialize
		point_a = 0;
		point_b = 0;
		cin >> num_battle;
	}

	return 0;
}


#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB push_back
#define PF push_front
#define FILL(a,b) memset(a,b,sizeof(a))
using namespace std;
int n,a[200005],b[200005],p[600005],dp[600005];
int main(){
	ios::sync_with_stdio(0);
    cin.tie(0);
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<n;i++)cin>>b[i];
	for(int i=0;i<n;i++){
		p[i]=b[i]^b[(i+1)%n];
		p[i+n]=a[i]^a[(i+1)%n];p[i+n*2]=p[i+n];
	}
	int l=0;
	FOR(i,1,n*3-1){
		if(i+dp[i-l]<dp[l]+l){
			dp[i]=dp[i-l];
		}else{
			int t=max(0,l+dp[l]-i);
			while(t+i<3*n&&p[t]==p[t+i])t++;
			dp[i]=t;
			l=i;
		}
	}
	FOR(i,n,2*n-1){
		if(dp[i]>=n){
			cout<<i-n<<' '<<(b[0]^a[i-n])<<'\n';
		}
	}
	RE 0;
}

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

int main(){
  ll n;
  cin>>n;
  ll a[n],b[n];
  for(int i=0;i<n;i++)cin>>a[i];
  for(int i=0;i<n;i++)cin>>b[i];
  reverse(b,b+n);
  ll j=0;
  for(int i=0;i<n;i++){
    if(a[i]==b[i]){
      for(;j<n;j++){
        if(a[i]!=b[j] && a[j]!=b[i]){
          swap(b[i],b[j]);
          break;
        }
      }
      if(a[i]==b[i]){cout<<"No"<<endl;return 0;}
    }
  }
  cout<<"Yes"<<endl;
  for(int i=0;i<n;i++)cout<<b[i]<<" ";
  cout<<endl;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int a;
	cin>>a;
	a=a+pow(a,2)+pow(a,3);
	cout<<a;
	}

#include <bits/stdc++.h>
#define ms(a) memset (a , 0 , sizeof a)
#define rr(i, a, b) for(register int i=a;i<=b;++i)
#define re(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
typedef long long ll ;

#define v(int) vector <int>
#define pb push_back
#define pp push
#define fi first
#define se second
using namespace std ;

const int N = 100 ;
int qwq () {
	char ch = getchar () ;
	int x = 0 , f = 1 ;
	while (ch < '0' || ch > '9') {if (ch == '-') f = -1 ; ch = getchar() ;}
	while (ch >= '0' && ch <= '9') {x = x *10 + ch - '0' ; ch = getchar() ;}
	return x * f ;
}
#define q qwq ()

int main () {
	int a ;
	cin >> a ;
	cout << (ll)(a + a * a + a * a * a) << endl;

	return 0 ;
}


#include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

void no_cost_graphmake(ll E,mat &G){
  rep(i,E){
    ll a,b;
    cin >> a >> b;
    G.at(a-1).push_back(b-1);
    G.at(b-1).push_back(a-1);//有向なら消す
  }
}
ll W=0,B=0;
bool nibugraph(mat &G,ll n){
  vec A(n,0);
  rep(j,n){
    if(A.at(j)==0){
      A.at(j)=1;
      W++;
      queue<ll> Q;
      Q.push(j);
      while(!Q.empty()){
        ll a=Q.front();
        Q.pop();
        rep(i,(ll)G.at(a).size()){
          if(A.at(G.at(a).at(i))==0){
            A.at(G.at(a).at(i))=3-A.at(a);
            if(A.at(G.at(a).at(i))==1){
              W++;
            }else{
              B++;
            }
            Q.push(G.at(a).at(i));
          }else if(A.at(G.at(a).at(i))==A.at(a)){
            return false;
          }
        }
      }
    }
  }
  return true;
}

int main(){
  ll N,M;
  cin >> N >> M;
  mat G(N);
  no_cost_graphmake(M,G);
  if(nibugraph(G,N)){
    cout << W*B-M << endl;
  }else{
    cout << (N*(N-1))/2-M << endl;
  }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, n) for(int i = 0; i < n; i++)
#define REP(i, n) for(int i = 1; i <= n; i++)
template<class T> inline int chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline int chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1<<29;
const ll LINF = 1LL<<58;
const int mod = 1000000007;

int used[2][100010] = {0};
int color[2] = {0};
vector<int> G[100010];

void dfs(int v, int c){
    used[c][v]++;
    color[c]++;
    for(auto x: G[v]){
        if(used[c ^ 1][x]) continue;
        dfs(x, c^1);
    }
    return;
}

int main(){
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0, 0);
    ll ans = 0;

    if(used[0][0] * used[1][0]) {
        cout << (ll)n * (n - 1) / 2 - m << endl;
        return 0;
    }

    for (int i = 0; i < n; i++) {
        int b = used[0][i], w = used[1][i];
        ans += b * (color[1] - G[i].size()) + w * (color[0] - G[i].size());
    }
    ans /= 2;

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


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,sum;
     cin>>a>>b>>c>>d;
     if(a>b && c>d)
        sum=b+d;
     else if(a>=b && c<=d)
        sum=b+c;
        else if(a<=b && c<=d)
        sum=a+c;
        else if(a<=b && c>=d)
        sum=a+d;
         cout<<sum<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    double n, x, t;
    cin>>n>>x>>t;
    double t1 = ceil(double(n/x));
    //cout<<n/x<<endl;
    //cout<<t1<<endl;
    ll t11 = ll(t1)*ll(t);
    cout<<t11<<endl;

}


#include <iostream>
using namespace std;

int main() {
    int n;

    while(1){
    cin >> n;
    if(n == 0) break;
    int num[10000][2] = {};
    int score[2] = {};

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < 2; ++j){
            cin >> num[i][j];
        }
        if (num[i][0] > num[i][1])
        {
            score[0] += num[i][0] + num[i][1];
        } else if (num[i][0] == num[i][1])
        {
            score[0] += num[i][0];
            score[1] += num[i][1];
        } else{
            score[1] += num[i][0] + num[i][1];
        }
    }

    cout << score[0] << " " << score[1] << endl;
    }

    return 0;
}

#include <iostream>
#include <string>
#include <stack>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

int main()
{
	int n;
	int a[10000], b[10000];
	while (cin >> n,n) {
		int A = 0;
		int B = 0;
		for (int i = 0; i < n; i++) {
			cin >> a[i] >> b[i];
		}
		for (int i = 0; i < n; i++) {
			if (a[i] > b[i]) A += a[i] + b[i];
			else if (a[i] < b[i]) B += a[i] + b[i];
			else {
				A += a[i];
				B += b[i];
			}
		}
		cout << A << " " << B << endl;
	}
}


#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
ll min(int a, ll b) {return a < b ? a : b;}
ll min(ll a, int b) {return a < b ? a : b;}
ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
ll max(int a, ll b) {return a > b ? a : b;}
ll max(ll a, int b) {return a > b ? a : b;}
ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

const int N = 333333;
const int MOD = 998244353;

char sumb = '#';

int n, m;
string s[5000];

int d[2100][2100];

bool bd(int i, int j)
{
    int cnt = 0;
    cnt += s[i - 1][j] == sumb;
    cnt += s[i - 1][j + 1] == sumb;
    cnt += s[i][j] == sumb;
    cnt += s[i][j + 1] == sumb;
    return cnt % 2;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int j = 0; j < m - 1; j++)
        d[n][j] = n;
    for (int i = n - 1; i >= 0; i--)
    {
        for (int j = 0; j < m - 1; j++)
        {
            d[i][j] = d[i + 1][j];
            if (i && bd(i, j)) d[i][j] = i;
        }
    }
    int ans = max(n, m);
    for (int i = 0; i < n; i++)
    {
        vector<int> r(m);
        vector<int> l(m);
        for (int j = 0; j < m; j++) r[j] = j, l[j] = j;
        vector<vector<pair<int, int> > > v(n + 2);
        for (int j = 0; j + 1 < m; j++)
        {
            int x = d[i + 1][j];
            v[x].push_back({j, j + 1});
        }
        int s = 1;
        for (int j = n; j > i; j--)
        {
            for (int k = 0; k < v[j].size(); k++)
            {
                int R = v[j][k].first;
                int L = v[j][k].second;
                int RR = r[L];
                int LL = l[R];
                int len = RR - LL + 1;
                s = max(s, len);
                l[RR] = LL;
                r[LL] = RR;
            }
            //cout << i << " " << j << " " << s * (j - i) << "\n";
            ans = max(ans, s * (j - i));
        }
    }
    cout << ans;
}

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define int long long
using namespace std;
const ll INF = 1e18;
const ll MOD = 998244353;

int basis[100];
int cnt = 0;

void insert(int mask) {
	for(int i = 61; i >= 0; i--) {
		if(!(mask&(1ll << i)))	continue;
		if(basis[i] == 0)	{
			basis[i] = mask;
			cnt++;
			return;
		}
		mask ^= basis[i];
	}
}

bool check(int mask) {
	for(int i = 61; i >= 0; i--) {
		if(!(mask&(1ll << i)))	continue;
		if(!basis[i]) return false;
		mask ^= basis[i];
	}
	return (mask == 0);
}

int32_t main() {
	ios::sync_with_stdio(!cin.tie(NULL));

	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		memset(basis, 0, sizeof(basis));
		cnt = 0;
		int arr[n];
		for(int i = 0; i < n; i++)	cin >> arr[i];
		string s;
		cin >> s;
		bool flag = true;

		for(int i = n - 1; i >=0; i--) {
			if(s[i] == '0') {
				insert(arr[i]);
			}
			else {
				if(!check(arr[i])) {
					cout << 1 << "\n";
					flag = false;
					break;
				}
			}
		}
		if(flag)
		cout << 0 << "\n";
	}
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<ll,ll>
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;

struct BIT
{
	int N;
	vector<int> bit;

	void init(int n)
	{
		N = n;
		bit.assign(n + 1, 0);
	}

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

	void updateMax(int idx, int val)
	{
		while(idx <= N)
		{
			bit[idx] = max(bit[idx], val);
			idx += idx & -idx;
		}
	}

	int pref(int idx)
	{
		int ans = 0;
		while(idx > 0)
		{
			ans += bit[idx];
			idx -= idx & -idx;
		}
		return ans;
	}

	int rsum(int l, int r)
	{
		return pref(r) - pref(l - 1);
	}

	int prefMax(int idx)
	{
		int ans = -2e9;
		while(idx > 0)
		{
			ans = max(ans, bit[idx]);
			idx -= idx & -idx;
		}
		return ans;
	}
};

const int M=502;
vector<int>vec[M];
int ses[100009];
vector<pi>yo[M];
vector<pi>abr_yo[M];
BIT Magic;
 main()

{
    fast
    int n,m,q;
    cin>>n>>m>>q;
    Magic.init(503);
    f(i,1,m)
    {
        int l,r;
        cin>>l>>r;
        vec[l].pb(r);
    }
    f(i,1,q)
    {
        int l,r;
        cin>>l>>r;
        yo[l].pb(mp(r,i));
        abr_yo[r].pb(mp(l,i));
    }
    f(i,1,500)
    {
       for(auto x:yo[i])
       {
           int idx=x.si;
           int rgt=x.fi;
           int lft=i;
           ses[idx]=-Magic.rsum(lft,rgt);
       }
       for(auto x:vec[i])
       {
           Magic.update(x,1);
       }
    }
    Magic.init(503);
    f(i,1,500)
    {
        for(auto x:vec[i])
       {
           Magic.update(x,1);
       }
       for(auto x:abr_yo[i])
       {
           int idx=x.si;
           int lft=x.fi;
           int rgt=i;
           ses[idx]+=Magic.rsum(lft,rgt);
       }

    }
    f(i,1,q)
    {
        cout<<ses[i]<<endl;
    }
     cout<<endl;
    return 0;

}

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

int main(void){
    int n,m,q;cin >> n >> m >> q;
    vector<vector<int>> s(n+2,vector<int>(n+2,0));

    int num[575][770] = {};
    rep(i,m){
        int l,r;cin >> l >> r;
        num[max(l,r)-1][min(r,l)-1]++;
    }
    for(int i = 1;i < n+1;i++){
        for(int j = 1;j < n+1;j++){
            s[i][j] = s[i][j-1] + s[i-1][j] - s[i-1][j-1] + num[i-1][j-1];
        }
    }

    rep(i,q){
        int alina,gray;cin >> alina >> gray;
        int l = max(alina,gray),r = min(alina,gray);

        int sum = s[l][l] - s[r-1][l] - s[l][r-1] + s[r-1][r-1];

        cout << sum << endl;
    }
}


#include<bits/stdc++.h>
#define N 200005
#define reg register
#define LL long long
using namespace std;

int val,cnt;
int n,a[N],b[N];
vector<int> p,q;

inline int qr()
{
	int x=0,w=1;char a=0;
	while(a<'0'||a>'9'){if(a=='-')w=-1;a=getchar();}
	while(a<='9'&&a>='0'){x=(x<<3)+(x<<1)+(a^48);a=getchar();}
	return x*w;
}

inline LL qrl()
{
	LL x=0,w=1;char a=0;
	while(a<'0'||a>'9'){if(a=='-')w=-1;a=getchar();}
	while(a<='9'&&a>='0'){x=(x<<3)+(x<<1)+(a^48);a=getchar();}
	return x*w;
}

bool mycmp(int a,int b)
{
	return a>b;
}

int main()
{
	n=qr();
	for(register int i=1;i<=n;i++)
		a[i]=qr();
	for(register int i=1;i<=n;i++)
		b[i]=qr();
	sort(b+1,b+n+1,mycmp);
	for(register int i=1;i<=n;i++)
	{
		if(a[i]!=b[i])
			continue;
		cnt++;
		p.push_back(i);
		val=b[i];
	}
	for(register int i=1;i<=n;i++)
	{
		if(!cnt)
			break;
		if(a[i]==b[i])
			continue;
		if(a[i]!=val&&b[i]!=val)
		{
			cnt--;
			q.push_back(i);
		}
	}
	if(cnt)
	{
		printf("No\n");
		return 0;
	}
	for(int i=0;i<q.size();i++)
		swap(b[p[i]],b[q[i]]);
	printf("Yes\n");
	for(register int i=1;i<=n;i++)
		printf("%d ",b[i]);
	printf("\n");
	return 0;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> a(n), b(n), aa(n+1,0), bb(n+1,0);
  rep(i,n) {
    cin >> a[i];
    aa[a[i]]++;
  }
  rep(i,n) {
    cin >> b[i];
    bb[b[i]]++;
  }

  rep(i,n){
    if(bb[i+1]+aa[i+1] > n){
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  rep(i,n){
    aa[i+1] += aa[i];
    bb[i+1] += bb[i];
  }

  int x = 0;
  rep(i,n){
    x = max(x,aa[i+1]-bb[i]);
  }
  rep(i,n){
    cout << b[(i-x+n)%n] << " ";
  }
  cout << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define fr first
#define sc second
#define pii pair < int, int >
#define pb push_back
#define ll long long
#define int ll

const int inf = (int)1e18 + 7;

pii ar[5234];
int dp[5234];
int x;

bool comp (const pii &a, const pii &b) {
    return a.fr + a.sc < b.fr + b.sc;
}

main () {
    int n; scanf ("%lld", &n);
    for (int i = 1; i <= n; i++) {
        int a, b; scanf ("%lld %lld", &a, &b);
        ar[i] = {a, b};
    }
    for (int i = 0; i <= n; i++) {
        dp[i] = inf;
    }
    sort(ar + 1, ar + n + 1, comp);
    dp[0] = x;
    for (int i = 1; i <= n; i++) {
        for (int j = i; j >= 1; j--) {
            if (ar[i].fr >= dp[j - 1]) {
                dp[j] = min(dp[j], dp[j - 1] + ar[i].sc);
            }
        }
    }
    vector < int > ans;
    while (dp[n] == inf) n--;
    printf ("%lld\n", n);
    return 0;
    while (n > 1) {
        for (int i = n - 1; i >= 1; i--) {
            if (dp[n] == dp[i] + ar[i].sc) {
                cout << i << ' ';
                n = i;
                break;
            }
        }
    }
    for (int to : ans) {
        printf ("%d ", to);
    }
}

#include<bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
#define mod 1000000007
#define mod2 998244353
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define setbits(x)  __builtin_popcount(x)
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(),(x).end()
#define scf(x) scanf("%lld",&x)
#define rep(i,n) for(ll i=0;i<n;i++)
const int INF = (int)1e9;
const ll IINF=1LL<<62;
const int maxn=10000005;

//int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/

/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
/*int find(int a){
     if(a==par[a])return a;
     return par[a]=find(par[a]);
}
void unite(int a,int b){
     a=find(a),b=find(b);
     if(a==b)return ;
     par[b]=a;
}
ll modexpo(ll a,ll b)
{
     ll ans=1LL;
     while(b>0)
     {
          if(b&1)ans=(ans*a)%mod;
          a=(a*a)%mod;
          b>>=1;
     }
     return ans;
}
*/
vector<int> adj[100005];
int indeg[100005];
int par[100005];
int main(){
     int n,m;
     cin>>n>>m;
     for(int i=1;i<=n+m-1;i++){
          int a,b;cin>>a>>b;
          adj[a].pb(b);indeg[b]++;
     }
     queue<int> q;
     for(int i=1;i<=n;i++)if(indeg[i]==0){q.push(i);break;}
     while(!q.empty()){
          int x=q.front();q.pop();
          for(int child:adj[x]){
               indeg[child]--;
               if(indeg[child]==0){
                    q.push(child);par[child]=x;
               }
          }
     }
     for(int i=1;i<=n;i++)cout<<par[i]<<"\n";

}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int dp[41];

int main(){
    memset(dp,0,sizeof(dp));
    dp[0] = 1;
    for(int j=0;j<30;j++){
	for(int i=1;i<=3;i++){
	    dp[i+j] += dp[j];
	}
    }
    while(true){
	int n;
	cin >> n;
	if(n == 0) break;
	int fact = dp[n]/10 + ((dp[n]%365==0) ? 0 : 1) ;
	cout << fact/365 + ((fact%365==0) ? 0 : 1) << endl;
    }
    return 0;
}

#include<iostream>
using namespace std;

#define MEMO_MAX 100
typedef unsigned long long ULL;

bool Flag_memo_g = true;	//?????¢??????????????????

class Answer
{
public:
	Answer()
	{
		day = 10; year = 365;
		for(int i = 0; i < MEMO_MAX; i++)
		{
			memo[i] = 0;
			Flag_memo[i] = false;
		}
	}

	int N;	//??\???
	int day;
	int year;

	ULL memo[MEMO_MAX];
	bool Flag_memo[MEMO_MAX];

	ULL count(int cnt);	//??¨??¢?´¢(??±???)
	ULL count_memo(int cnt);	//??¨??¢?´¢(??±???)(?????¢???ver.)
	void set();	//??\???
	void OutPut();	//???????????????

	bool empty();	//???????????¶?????????
	bool ans();	//?§£???
};

ULL Answer::count(int cnt)
{
	if( cnt < 0 )
		return 0;

	else if( cnt == 0 )
		return 1;

	return count(cnt - 1) + count(cnt - 2) + count(cnt - 3);
}

ULL Answer::count_memo(int cnt)
{
	if( cnt < 0 )return 0;
	else if( cnt == 0 )return 1;

	if( !(Flag_memo[cnt]) )
	{
		memo[cnt] = count_memo(cnt - 1) + count_memo(cnt - 2) + count_memo(cnt - 3);
		Flag_memo[cnt] = true;
	}

	return memo[cnt];
}

void Answer::OutPut()
{
	if( N == 0 )
		return;
	int ans_ = 1;	//??????????????´???
	if( Flag_memo_g )
		ans_ += count_memo( N ) / (day * year);

	else if( !Flag_memo_g )
		ans_ += (count( N )) / (day * year);	//??\???

	cout << ans_ << endl;
}

void Answer::set()
{
	cin >> N;
}

bool Answer::empty()
{
	if( N == 0 )return false;
	else return true;
}

bool Answer::ans()
{
	set();
	OutPut();
	return empty();
}

int main()
{
	Answer ans;

	while( ans.ans() );
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

int main()
{
    int n;
    cin >> n;
    vector<int> x(n), y(n), per(n);
    rep(i, n) cin >> x[i] >> y[i];

    rep(i, n) per[i] = i;
    double ans = 0;
    int cnt = 0;
    do
    {
        cnt++;
        rep(i, n - 1)
        {
            ans += sqrt(pow(x[per[i + 1]] - x[per[i]], 2.0) + pow(y[per[i + 1]] - y[per[i]], 2.0));
        }

    } while (next_permutation(all(per)));

    printf("%6lf\n", ans / cnt);
    return 0;
}

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

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

typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//定数
#define INF 1000000000 //10^9:極めて大きい値,∞
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素

int main(){
    int N;
    cin>>N;
    vector<double> x(N),y(N);
    rep(i,N){
        cin>>x[i]>>y[i];
    }
    double sum=0;
    rep(i,N-1){
        for(int j=i;j<N;j++){
            sum+=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
    }
    double ans=sum*2/N;
    std::cout << std::fixed<<std::setprecision(9);
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int di[]={-1, 0, 1, 0}, dj[]={0, -1, 0, 1};

bool is_valid(const vector<size_t> &q, size_t r) {
    for (size_t j=0; j<8; ++j)
        if (j!=r && q[j]==q[r])
            return false;

    for (size_t i=0; i<r; ++i) {
        if (q[i]-i == q[r]-r) return false;
        if (q[i]+i == q[r]+r) return false;
    }

    return true;
}

bool solve_equeen(vector<size_t> &q, size_t r=0) {
    if (r == 8) return true;

    if (!~q[r]) {
        for (size_t c=0; c<8; ++c) {
            q[r] = c;
            if (is_valid(q, r))
                if (solve_equeen(q, r+1))
                    return true;
        }
        q[r] = -1;
    } else {
        if (is_valid(q, r))
            if (solve_equeen(q, r+1))
                return true;
    }

    return false;
}

int main() {
    size_t k;
    scanf("%zu", &k);

    vector<size_t> q(8, -1);
    for (size_t i=0; i<k; ++i) {
        size_t r, c;
        scanf("%zu %zu", &r, &c);

        if (~q[r]) return 1;
        q[r] = c;
    }

    solve_equeen(q);

    for (size_t i=0; i<8; ++i)
        for (size_t j=0; j<8; ++j)
            printf("%c%s", q[i]==j? 'Q':'.', j<7? "":"\n");

    return 0;
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
vector<int> part_ans(0);
vector<int> t_ans;
bool fin=false;
int last=-1;

vector<int> topo_sort1(vector<vector<int>> e)
{
  int n=e.size();
  vector<int> ans(0);
  vector<int> ins(n,0);
  int i,j;
  rep(i,n)
  {
    rep(j,e[i].size())
    {
      ins[e[i][j]]++;
    }
  }
  queue<int> s;
  rep(i,n) if(ins[i]==0)s.push(i);
  while(!s.empty())
  {
    int now=s.front();
    s.pop();
    ans.emplace_back(now);
    rep(i,e[now].size())
    {
      ins[e[now][i]]--;
      if(ins[e[now][i]]==0)
      {
        s.push(e[now][i]);
      }
    }
  }
  return ans;
}

void dfs(int now,vector<bool> &visited,vector<vector<int>> e,int count)
{
  if(fin)return;
  int i,j;
  visited[now]=true;
  part_ans.emplace_back(now);
  int pas=part_ans.size();
  rep(i,e[now].size())
  {
    if(!visited[e[now][i]])
    {
      dfs(e[now][i],visited,e,count+1);
      if(fin)
      {
        return;
      }
    }
    else
    {
      last=e[now][i];
      bool ap=false;
      rep(j,part_ans.size())
      {
        if(!ap)
        {
          if(part_ans[j]==last)
          {
            ap=true;
            t_ans.emplace_back(part_ans[j]);
          }
        }
        else
        {
          t_ans.emplace_back(part_ans[j]);
        }
      }
      fin=true;
      break;
    }
  }
  visited[now]=false;
  part_ans.erase(part_ans.end()-1,part_ans.end());
}

int main()
{
  int n,m;cin>>n>>m;
  vector<int> a(m);
  vector<int> b(m);
  int i,j;
  rep(i,m)cin>>a[i]>>b[i];
  rep(i,m)
  {
    a[i]--;
    b[i]--;
  }
  vector<vector<int>> e(n);
  rep(i,m)
  {
    e[a[i]].emplace_back(b[i]);
  }
  vector<int> ans=topo_sort1(e);
  if(ans.size()==n)
  {
    cout<<-1<<endl;
  }
  else
  {
    rep(i,n)
    {
      vector<bool> visited(n,false);
      dfs(i,visited,e,0);
      if(fin)break;
    }
    while(true)
    {
      vector<bool> inside(n,false);
      rep(i,t_ans.size())
      {
        inside[t_ans[i]]=true;
      }
      vector<int> ins(n,0);
      vector<int> outs(n,0);
      bool ok=true;
      vector<vector<int>> mine(n);
      rep(i,m)
      {
        if(inside[a[i]]&&inside[b[i]])
        {
          ins[b[i]]++;
          outs[a[i]]++;
          mine[a[i]].emplace_back(b[i]);
        }
      }
      int insum=0;
      int outsum=0;
      rep(i,n)
      {
        if(inside[i])
        {
          if(ins[i]!=1||outs[i]!=1)
          {
            ok=false;
          }
        }
      }
      if(ok)break;
      int stat=-1;
      rep(i,t_ans.size())
      {
        if(outs[t_ans[i]]>1)
        {
          stat=i;
          break;
        }
      }
      vector<bool> use(n,true);
      rep(i,outs[t_ans[stat]])
      {
        vector<bool> vis(n,false);
        int now=t_ans[stat];
        vis[now]=true;
        now=mine[now][i];
        while(true)
        {
          vis[now]=true;
          now=mine[now][0];
          if(vis[now])break;
        }
        rep(j,n)
        {
          if(!vis[j])use[j]=false;
        }
      }
      t_ans.erase(all(t_ans));
      rep(i,n)if(use[i])t_ans.emplace_back(i);
    }
    cout<<t_ans.size()<<endl;
    rep(i,t_ans.size())
    {
      cout<<t_ans[i]+1<<endl;
    }
  }
}


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

int main() {
    double n, k;
    cin >> n >> k;

    vector<ll> arr(n);
    for (int i = 0; i < n; i++) {
        ll x; cin >> x;
        arr[i] = (x + 1.0) / 2.0;
    }

    ll ans = 0;
    for (int i = 0; i < k; i++) ans += arr[i];
    ll temp = ans;

    for (int i = k; i < n; i++) {
        temp = temp + arr[i] - arr[i-k];
        ans = max(ans, temp);
    }

    printf("%0.7Lf\n", ans);
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, k, ans = 0, prt[N];
vector<int> g[N];

int dfs(int u, int fa) {
  int res = 0;
  for (auto v : g[u]) {
    res = max(res, dfs(v, u));
  }
  if (res == k - 1 && fa != 1) {
    res = -1;
    ans++;
  }
  return res + 1;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &prt[i]);
    if (i > 1) {
      g[prt[i]].push_back(i);
    }
  }
  if (prt[1] != 1) {
    ans ++;
    prt[1] = 1;
  }
  dfs(1, 1);
  printf("%d\n", ans);
  return 0;
}


#include<bits/stdc++.h>
#define REP(i, n) for(int i=0; i<n; i++)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;

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

int main() {
    int N;
    cin >> N;
    int A[N];
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
    }
  	sort(A, A+N);
    string ans = "YES";
    for (int i = 1; i < N; i++)
    {
        if(A[i-1]==A[i]) ans = "NO";
    }
    cout << ans << endl;
}


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

typedef long long ll;

const ll MOD = 1000000007;
const int MAX_N = 2005;

ll sil[MAX_N * MAX_N], odw[MAX_N * MAX_N], DP[MAX_N][MAX_N];

ll pot(ll a, ll b) {
    ll result = 1;

    while(b > 0) {
        if(b % 2 == 1) {
            result = (result * a) % MOD;
        }
        a = (a * a) % MOD;
        b /= 2;
    }

    return result;
}

ll binom(int n, int k) {
    ll result = sil[n];
    result = (result * odw[k]) % MOD;
    result = (result * odw[n - k]) % MOD;
    return result;
}

int main() {
    sil[0] = 1;
    for(int i = 1; i < MAX_N * MAX_N; i++) {
        sil[i] = (sil[i - 1] * i) % MOD;
    }
    odw[MAX_N * MAX_N - 1] = pot(sil[MAX_N * MAX_N - 1], MOD - 2);
    for(int i = MAX_N * MAX_N - 2; i >= 1; i--) {
        odw[i] = (odw[i + 1] * ll(i + 1)) % MOD;
    }
    odw[0] = odw[1];

    int n, k;

    cin >> n >> k;

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

    for(int i = 0; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            if(i == 0) {
                if(j == 0) {
                    DP[i][j] = 1;
                } else {
                    DP[i][j] = (DP[i][j - 1] * binom(j * (k - 1) - 1, k - 2)) % MOD;
                }
                continue;
            }
            DP[i][j] = DP[i - 1][j];
            if(j > i) {
                DP[i][j] += DP[i][j - 1] * binom(j * (k - 1) + i - 1, k - 2);
                DP[i][j] %= MOD;
            }
        }
    }

    for(int i = 1; i <= n; i++) {
        DP[n][n] = (DP[n][n] * i) % MOD;
    }

    cout << DP[n][n];

    return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int N = 4e6 + 10, M = 2e3 + 10, MOD = 1e9 + 7;
int fac[N], inv[N], dp[M][M], ps[M][M];

int sum(int a, int b) {return (a + b) % MOD;}
int mul(int a, int b) {return (a * b) % MOD;}
int Pow(int a, int b)
{
    int res = 1;
    for (; b; b>>=1, a = mul(a, a)) if (b & 1) res = mul(res, a);
    return res;
}

int C(int n, int k)
{
    if (k < 0 || n < 0 || k > n) return 0;
    return mul(fac[n], mul(inv[k], inv[n - k]));
}

int32_t main()
{
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i);
    inv[N - 1] = Pow(fac[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1);
    int n, k; cin >> n >> k;
    if (k == 1) return cout << "1\n", 0;
    ps[1][1] = dp[1][1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i][1] = mul(ps[i - 1][1], C(i * k - 2, k - 2));
        for (int j = 2; j <= i; j++) {
            dp[i][j] = mul(ps[i - 1][j - 1], C(i * k - j - 1, k - 2));
        }
        for (int j = i; j >= 1; j--) ps[i][j] = sum(ps[i][j + 1], dp[i][j]);
    }
    cout << mul(ps[n][1], fac[n]) << "\n";
}


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

int main() {
    string s;
    cin >> s;
    int ans=0;
    for(int i=0;i<s.size()-1;i++){
        if(s.at(i)=='A'&&s.at(i+1)=='C') ans++;
    }
    if(ans>=1) cout << "Yes";
    else cout << "No";
}

#include <bits/stdc++.h>		//万能头文件
using namespace std;

int main()
{
	string a;
	int pos=0;		//记录位置
	cin>>a;
	a=a+"D";		//为防止最后一个字母是"A",添加一个不是"C"的字母
	while(1)
	{
		pos=a.find("A",pos);		//find()函数查找位置
		if(pos<0)		//如果没找到，返回-1
		{
			cout<<"No"<<endl;		//endl养成好习惯
			break;		//跳出循环
		}
		if(a[pos+1]=='C')		//判断"A"下一个是不是"C"
		{
			cout<<"Yes"<<endl;
			break;		//跳出循环
		}
		pos++;		//下一次从pos+1找起
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int main() {
	while(cin >> n >> m, n) {
		vector<vector<int> > G(n);
		for(int i = 0; i < m; i++) {
			cin >> a >> b; a--, b--;
			G[a].push_back(b);
			G[b].push_back(a);
		}
		vector<int> d(n, -1); d[0] = 0;
		queue<int> que; que.push(0);
		while(!que.empty()) {
			int u = que.front(); que.pop();
			for(int i : G[u]) {
				if(d[i] == -1) {
					d[i] = d[u] + 1;
					que.push(i);
				}
			}
		}
		int cnt = -1;
		for(int i = 0; i < n; i++) {
			if(d[i] <= 2) cnt++;
		}
		cout << cnt << endl;
	}
}


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

using namespace std;

#define int long long

const int INF = 1e18;

long long d;
unsigned long long ans;
unsigned long long p[50];
map<int, int> kek[2];

void check(long long d, int L, int R, unsigned long long cnt)
{
    if (L >= R)
    {
        ans += cnt * (d == 0) * (L == R ? 10 : 1);
        return;
    }
    long long c = p[R] - p[L];
    vector<long long> xs;
    for (int digit = -9; digit <= 9; digit++)
    {
        if (digit < 0 && L == 0) continue;
        long long x = d - digit * c;
        xs.push_back(x);
    }
    sort(xs.begin(), xs.end());
    for (int i = -1; i < (int)xs.size(); i++)
    {
        if ((i == -1 || xs[i] < 0) && (i + 1 == xs.size() || xs[i + 1] >= 0))
        {
            if (i > -1)
            {
                int digit = (d - xs[i]) / c;
                check(xs[i], L + 1, R - 1, cnt * kek[L == 0][digit]);
            }
            if (i + 1 < xs.size())
            {
                int digit = (d - xs[i + 1]) / c;
                check(xs[i + 1], L + 1, R - 1, cnt * kek[L == 0][digit]);
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            kek[0][i - j]++;
        }
    }
    for (int i = 1; i < 10; i++)
    {
        for (int j = 1; j < 10; j++)
        {
            kek[1][i - j]++;
        }
    }
    cin >> d;
    p[0] = 1;
    for (int i = 1; i <= 19; i++) p[i] = p[i - 1] * 10;
    for (int len = 1; len <= 19; len++)
    {
        check(d, 0, len - 1, 1);
    }
    cout << ans;
}

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

int main()
{
   int N,Y,x,y,z;
   cin >> N >> Y;
   int check = 0;
   for(x=0;x<=N;x++){
      for(y = 0;y <=N;y++){
         z = N-x-y;
         if(z >= 0 && Y == 10000*x+5000*y+1000*z){
            check = 1;
            break;
         }
      }
      if(check)break;
   }
   if(check){
      cout << x << " " << y << " " << z << endl;
   }else{
      cout << -1 << " " << -1 << " " << -1 << endl;
   }

   return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repq(i,a,n) for(int i=a; i<=n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

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

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

// Binary Indexed Tree (BIT)
// Verified: AOJ DSL_2_B: Range Sum Query (int??????)

template <typename T>
struct BIT{
private:
    vector<T> array;
    const int n;

public:
    // ?????????
    BIT(int _n) : array(_n + 1, 0), n(_n) {}

    // 1???????????? i???????????§????´???????????±???????
    T sum(int i) {
        T s = 0;
        while(i > 0) {
            s += array[i];
            i -= i & -i;      // LSB ??????
        }
        return s;
    }

    // [i, j] ???????´??????????
    T sum(int i, int j) {
        T ret_i = sum(i-1);
        T ret_j = sum(j);
        return ret_j - ret_i;
    }

    // i ????????? ????´? x ?????????
    void add(int i, T x) {
        while(i <= n) {
            array[i] += x;
            i += i & -i;      // LSB ??????
        }
    }
};

signed main() {
    int n; cin >> n;
    BIT<int> b(n);
    vector<int> a(n);
    map<int, int> m;
    rep(i,0,n) {
        cin >> a[i];
        m[a[i]];
    }

    int newnum = 1;
    for(auto &x : m) x.second = newnum++;
    rep(i,0,n) a[i] = m[a[i]];

    int ans = 0;
    rep(j,0,n) {
        ans += j - b.sum(a[j]);
        b.add(a[j], 1);
    }
    cout << ans << endl;
    return 0;
}

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef vector<pii> VP;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;

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

	int N; cin >> N;
	VP A(N);
	REP(i, N) {
		cin >> A[i].first >> A[i].second;
		A[i].second++;
	}
	SORT(A);
	int s1, s2, t1, t2;
	int ans = -1;
	s2 = A[N - 1].first;

	VI mi(N, INF), mii(N, INF);

	mi[N - 1] = A[N - 1].second;
	for (int i = N - 2; i >= 0; i--)mi[i] = min(A[i].second, mi[i + 1]);

	mii[0] = A[0].second;
	FOR(i, 1, N)mii[i] = min(mii[i - 1], A[i].second);

	REP(i, N - 1) {

		int cnt=-1;
		s1 = A[i].first;
		t2 = mi[i + 1];
		t1 = mii[i];

		chmax(cnt, max((t1 - s1), 0LL) + max((t2 - s2), 0LL));
		chmax(ans, cnt);

		if (i == 0)continue;

		t1 = A[i].second;
		t2 = min(mii[i - 1], mi[i + 1]);

		chmax(cnt, max((t1 - s1), 0LL) + max((t2 - s2), 0LL));

		chmax(ans, cnt);
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

#define int long long

struct edge {
    int to;
    int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define delim(i, n) cout << (i == n-1 ? "\n" : " ");

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

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 100000;
int n, m;
vi a(maxn);
vi table(maxn);

vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};

int lds() {
    table.resize(n);
    REP(i, n) {
        table[i] = -1;
    }
    REP(i, n) {
        auto iter = upper_bound(table.begin(), table.end(), a[i], [](const int& a, const int& b) { return a > b; });
        *iter = a[i];
    }
    auto iter = lower_bound(table.begin(), table.end(), -1, [](const int& a, const int& b) { return a > b; });
    return (int) (iter - table.begin());
}

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

    // https://onlinejudge.u-aizu.ac.jp/problems/DPL_1_D
    cin >> n;
    REP(i, n) {
        cin >> a[i];
    }

    int ret = lds();
    cout << std::fixed << std::setprecision(10) << ret << "\n";
}

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

int main(){
  int N;
  cin >> N;
  for(int i=0;i<9;i++){
    int A=100*(i+1)+10*(i+1)+i+1;
    if(N<=A){
      cout << A << endl;
      break;
    }
  }
}


#include <stdio.h>
#include <algorithm>
using namespace std;
using ll = long long;

int n, a[500010], ca[500010];
ll czero[500010], dp[1 << 20][2], last[1 << 20], mod = 1e9+7;
void zero() {
	for (int i = 0; i < (1 << 20); i++)
		dp[i][0] = 1, last[i] = 0;
	for (int i = 1; i <= n; i++) {
		czero[i] = czero[i - 1];
		if (ca[i] != 0) {
			dp[ca[i]][0] += dp[ca[i]][1] * (czero[i] - czero[last[ca[i]]]);
			dp[ca[i]][0] %= mod;

			dp[ca[i]][1] += dp[ca[i]][0];
			dp[ca[i]][1] %= mod;

			last[ca[i]] = i;
		}
		else
			czero[i]++;
	}
	ll sum = 0;
	for (int i = 1; i < (1 << 20); i++)
		sum += dp[i][1];
	ll pow2 = 1;
	for (int i = 0; i < czero[n] - 1; i++)
		pow2 = (pow2 * 2) % mod;
	sum += pow2;
	sum = ((sum % mod) + mod) % mod;
	printf("%lld\n", sum);
}
void nonzero() {
	for (int i = 0; i < (1 << 20); i++)
		dp[i][0] = 1, last[i] = 0;
	for (int i = 1; i <= n; i++) {
		czero[i] = czero[i - 1];
		if (ca[i] != 0) {
			dp[ca[i]][0] += dp[ca[i]][1] * (czero[i] - czero[last[ca[i]]]);
			dp[ca[i]][0] %= mod;

			dp[ca[i]][1] += dp[ca[i]][0];
			dp[ca[i]][1] %= mod;

			last[ca[i]] = i;
		}
		else
			czero[i]++;
	}
	printf("%lld\n", dp[ca[n]][0]);
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
		ca[i] ^= ca[i - 1] ^ a[i];
	}

	if (ca[n] == 0)
		zero();
	else
		nonzero();
}

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

typedef long long lli;

lli rec(int l, int r, vector<int> &A) {
  if(r-l <= 1) return 0;
  int m = (l+r)/2;
  lli res = rec(l, m, A) + rec(m, r, A);
  queue<int> a, b;
  for(int i = l; i < m; ++i) a.push(A[i]);
  for(int i = m; i < r; ++i) b.push(A[i]);
  int k = l;
  while(a.size() && b.size()) {
    if(a.front() <= b.front()) {
      A[k++] = a.front();
      a.pop();
    } else {
      A[k++] = b.front();
      b.pop();
      res += a.size();
    }
  }
  while(a.size()) {
    A[k++] = a.front();
    a.pop();
  }
  while(b.size()) {
    A[k++] = b.front();
    b.pop();
  }
  return res;
}

int main() {
  int n;
  vector<int> A;
  cin >> n;
  A.resize(n);
  for(int i = 0; i < n; ++i) cin >> A[i];
  cout << rec(0, n, A) << endl;
  return 0;
}


#include <iostream>
int main(){
    int a;
    std::cin >> a;
    a=a+a*a+a*a*a;
    std::cout << a << std::endl;
}

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define MAX 1000001
ll prime[MAX];
#define mod 1000000007

ll rec(vector<vector<ll> >& dp,vector<ll>& arr,int n,int i,int k,int w)
{
  ll ans=0;
  if(i>=n)
    return 0;

  if(dp[i][k]!=-1)
    return dp[i][k];

  if(k!=0)
  ans=max(rec(dp,arr,n,i+1,w,w),arr[i]+rec(dp,arr,n,i+1,k-1,w));
  else
  ans=max(rec(dp,arr,n,i+1,k,w),rec(dp,arr,n,i+1,w,w));

  return dp[i][k]=ans;

}

ll t[400008];
void build(vector<ll>& a, int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = max(t[v*2] ,t[v*2+1]);
    }
}

ll maxi(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return max(maxi(v*2, tl, tm, l, min(r, tm)) ,maxi(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void update(int v, int tl, int tr, int pos, ll new_val) {
    if (tl == tr) {
        t[v] = new_val;
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        t[v] = max(t[v*2] ,t[v*2+1]);
    }
}

int main() {

int a;
cin>>a;
cout<<a+a*a+a*a*a;
return 0;
}


#include<stdio.h>

int main(void)
{
  int i,num[100],a,j;

  scanf("%d",&a);

  for(i=0;i<a;i++){
    scanf("%d",&num[i]);
  }
  for(j=a-1;j>=0;j--){
    if(j>0&&j<=a-1){
      printf("%d ",num[j]);
    }
    else{
      printf("%d\n",num[0]);
    }
  }
  return 0;
}

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

int input()
{
    int value;
    std::cin>>value;
    return value;
}

int main()
{
    std::vector<int> vec;
    int n;
    std::cin >> n;
    for(int i = 0;i<n;++i)
        vec.push_back(input());
    std::reverse(vec.begin(),vec.end());
    for(auto i = 0;i < n;++i)
    {
        std::cout << vec[i] << (i==n-1?"":" ");
    }
    std::cout << std::endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll r = 0, g = 0, b = 0;
  for (const auto& c : s) {
    if (c == 'R')
      r++;
    else if (c == 'G')
      g++;
    else
      b++;
  }
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int k = 2 * j - i;
      if (k <= j || k > n - 1) continue;
      if (s[i] != s[j] && s[j] != s[k] && s[k] != s[i]) {
        cnt++;
      }
    }
  }
  ll ans = r * g * b - cnt;
  cout << ans << '\n';
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(I,X,Y) for(long long (I)=(X);(I)<(Y);(I)++)
#define REP(I,X,Y) for(long long (I)=(Y)-1;(I)>=(X);(I)--)
#define ALL(X) (X).begin(),(X).end()
#define pb push_back
#define COUNT(V,X) upper_bound((V).begin(),(V).end(),X)-lower_bound((V).begin(),(V).end(),X)
#define debug(x) cerr<<#x<<':'<<x<<endl;
#define DEBUG(v) cerr<<#v<<':';for(auto x:v)cerr<<x<<' ';cerr<<endl;
#define INF 1000000007
#define LINF 1000000000000000007
#define EPS 1e-9
typedef long long ll;

ll levenshtein(string s1,string s2){
  long long n = s1.size();
  long long m = s2.size();
  long long DP[n+1][m+1]={0};
  for(long long i = 0;i < n+1;i++)DP[i][0] = i;
  for(long long j = 0;j < m+1;j++)DP[0][j] = j;
  for(long long i = 1;i < n+1;i++){
    for(long long j = 1;j < m+1;j++){
      long long cost = (s1[i-1]==s2[j-1] ? 0 :1);
      DP[i][j] = min({DP[i-1][j]+1,DP[i][j-1]+1,DP[i-1][j-1]+cost});
    }
  }
  return DP[n][m];
}

signed main(){
  string s1,s2;
  cin >> s1 >> s2;
  cout << levenshtein(s1,s2) << endl;
}


#include<iostream>
using namespace std;

int main(){
	int h, w;

	cin >> h >> w;
	while(h != 0 || w != 0){
		int ans[2],min=100000;;
		ans[0]=1000;
		ans[1]=1000;
		int current=h*h+w*w;
		int anstmp=current;

		for(int i=1; i<10000; i++){
			for(int j=i+1; j<10000-i; j++){
				int tmp=i*i+j*j;
				if(tmp > current || (tmp == current && i>h) ){
					if(tmp < min || (tmp == min && ans[0] > i) ){
						min = tmp;
						ans[0]=i;
						ans[1]=j;
					}
				}
			}
		}
		cout << ans[0] << ' ' << ans[1] << endl;
		cin >> h >> w;
	}
}

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node{int key; Node *p,*l,*r;};
Node *null,*root;

void insert(int k){
Node *x,*y,*z;
	y=null;
	x=root;
	z=(Node*)malloc(sizeof(Node));
	z->key=k;
	z->l=null;
	z->r=null;
	while(x!=null){
	    y=x;
		if(z->key<x->key)
		x=x->l;
		else
		x=x->r;
	}
	z->p=y;
	if(y==null)
	root=z;
	else if(z->key>y->key)
	y->r=z;
	else
	y->l=z;
}
void in_p(Node *u){
	if(u==null)
	return;
	in_p(u->l);
	printf(" %d",u->key);
	in_p(u->r);
}
void pre_p(Node *u){
	if(u==null)
	return ;
	printf(" %d",u->key);
	pre_p(u->l);
	pre_p(u->r);
}
void print(){
	in_p(root);
	printf("\n");
	pre_p(root);
	printf("\n");
}
bool find(Node *a,int k){
	if(a==null)
	return false;
	    else if(a->key==k)
		return true;
		else if(a->key>k)
	     find(a->l,k);
	    else
	    find(a->r,k);
	}
	Node* getmin(Node *x){
	while(x->l!=null)
	x=x->l;
	return x;
}
Node* getS(Node *x){
	if(x->r!=null)
	return getmin(x->r);
	Node *y;
	y=x->p;
	while(y!=null&&x==y->r){
	x=y;
		y=y->p;
	}
	return y;
}

void Delete(int k){
Node *y,*z,*x;
	x=null;
	z=root;
	while(z->key!=k&&z!=null){
		if(k>z->key)
		z=z->r;
		else
		z=z->l;
	}
	if(z->l==null || z->r==null)
	y=z;
else
	y=getS(z);
	if(y->l!=null)
	x=y->l;
	else
	x=y->r;
	if(x!=null)
	x->p=y->p;
	if(y->p==null)
	root=x;
	else if(y==y->p->l)
	y->p->l=x;
	else
	y->p->r=x;
	if(y!=z)
	z->key=y->key;
	free(y);
}

main(){
	int i,n,key;
	string com;
	scanf("%d",&n);

	for(i=0;i<n;i++){
		cin>>com;
		if(com=="insert"){
			scanf("%d",&key);
			insert(key);
		}
		else if(com=="find"){
			scanf("%d",&key);
			if(find(root,key))
			printf("yes\n");
			else
			printf("no\n");
		}
		else if(com=="delete"){
			scanf("%d",&key);
			Delete(key);
		}
		else
		print();
	}

	return 0;
}


#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <map>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <string>

class Direct_Graph {
    public:
    struct Edge {
        int to; int cost;
    };
    std::vector<std::vector<Edge>> G;
    int node_num;
    Direct_Graph() {}
    Direct_Graph(const int node_num_in) {
        node_num = node_num_in;
        G.resize(node_num);
    }
    void add_edge(const int from, const int to, const int cost) {
        Edge edge; edge.to = to; edge.cost = cost;
        G[from].push_back(edge);
    }
    void add_edge_both(const int node1, const int node2, const int cost) {
        add_edge(node1, node2, cost);
        add_edge(node2, node1, cost);
    }

    std::vector<int> Dijkstra(int s, const int dist_inf) {
        std::vector<int> dist; dist.resize(node_num);
        for (int i = 0; i < dist.size(); i++) {
            dist[i] = dist_inf;
        }
        typedef std::pair<int, int> Pair;

        std::priority_queue <Pair, std::vector<Pair>, std::greater<Pair>> que;
        dist[s] = 0;
        que.push(Pair(0, s));

        while (!que.empty()) {
            Pair pair = que.top(); que.pop();
            int v = pair.second;
            if (dist[v] < pair.first) { continue; }
            for (Edge edge : G[v]) {
                if (dist[v] + edge.cost < dist[edge.to]) {
                    dist[edge.to] = dist[v] + edge.cost;
                    que.push(Pair(dist[edge.to], edge.to));
                }
            }
        }

        return dist;
    }
};

const int MAX_N = 100050;
const int MAX_M = 200050;

int N, M;
int p[MAX_M], q[MAX_M], c[MAX_M];

Direct_Graph graph = Direct_Graph(MAX_N + MAX_M * 2);

std::map<std::pair<int, int>, int> mp;

int main(int argc, char **argv) {
    std::cin >> N >> M;
    for (int i = 1; i <= M; i++) {
        std::cin >> p[i] >> q[i] >> c[i];
    }

    int idx = N + 1;
    for (int i = 1; i <= M; i++) {
        if (mp.find(std::pair<int, int>({p[i], c[i]})) == mp.end()) {
            mp[std::pair<int, int>({p[i], c[i]})] = idx;
            graph.add_edge(p[i], idx, 1);
            graph.add_edge(idx, p[i], 0);
            idx++;
        }
        if (mp.find(std::pair<int, int>({q[i], c[i]})) == mp.end()) {
            mp[std::pair<int, int>({q[i], c[i]})] = idx;
            graph.add_edge(q[i], idx, 1);
            graph.add_edge(idx, q[i], 0);
            idx++;
        }
        int node_p = mp[std::pair<int, int>({p[i], c[i]})];
        int node_q = mp[std::pair<int, int>({q[i], c[i]})];

        //graph.add_edge_both(p[i], q[i], 1);
        //graph.add_edge_both(p[i], node_q, 1);
        //graph.add_edge_both(node_p, q[i], 1);
        graph.add_edge_both(node_p, node_q, 0);
    }

    std::vector<int> dist = graph.Dijkstra(1, MAX_M * 2);
    int ret = dist[N];

    /*
    for (int i = 1; i <= M; i++) {
        if (p[i] == N) {
            ret = std::min(ret, dist[mp[std::pair<int, int>({p[i], c[i]})]]);
        } else if (q[i] == N) {
            ret = std::min(ret, dist[mp[std::pair<int, int>({q[i], c[i]})]]);
        }
    }
    */

    if (ret < MAX_M * 2) {
        std::cout << ret << std::endl;
    } else {
        std::cout << -1 << std::endl;
    }

    return 0;
}

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int maxn=4e6+10;
int n,m,head[maxn*2],cnt,vis[maxn*2],dis[maxn*2],tot=200010;
struct edge
{
    int next,to,quan;
} e[maxn*2];
priority_queue<pair<int,int> >q;
map<pair<int,int>,int>mp;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
	if(ch=='-')
	    f=-1;
	ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
	x=(x<<1)+(x<<3)+(ch^48);
	ch=getchar();
    }
    return x*f;
}
inline void add(int from,int to,int quan)
{
    e[++cnt].next=head[from];
    e[cnt].quan=quan;
    e[cnt].to=to;
    head[from]=cnt;
}
int bianhao(int x,int y)
{
    if(mp.find(make_pair(x,y))!=mp.end())
	return mp[make_pair(x,y)];
    return mp[make_pair(x,y)]=++tot;
}
int main()
{
    n=read(),m=read();
    memset(dis,0x3f,sizeof(dis));
    for(int i=1; i<=m; i++)
    {
	int p=read(),q=read(),c=read();
	int t1=bianhao(p,c),t2=bianhao(q,c);
	add(p,t1,1),add(t1,t2,0),add(t2,q,1);
	add(t1,p,1),add(t2,t1,0),add(q,t2,1);
    }
    dis[1]=0;
    q.push(make_pair(0,1));
    while(!q.empty())
    {
	int now=q.top().second;
	q.pop();
	if(vis[now])
	    continue;
	vis[now]=1;
	for(int i=head[now]; i; i=e[i].next)
	{
	    int to=e[i].to;
	    if(dis[to]>dis[now]+e[i].quan)
	    {
		dis[to]=dis[now]+e[i].quan;
		q.push(make_pair(-dis[to],to));
	    }
	}
    }
    if(dis[n]>1e9+10)dis[n]=-2;
    printf("%d",dis[n]/2);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
const int maxn=2e5+7;
const ll modn=1e9+7;
const int inf=2e9+7;
const double PI = acos(-1.0);
const double eps=1e-8;
inline int lowbit(int x){return x&-x;}
inline ll lowbit(ll x) {return x&-x;}
inline int CountOneBinary(ll num){int ans=0;while(num){if(num&1) ans+=1;num>>=1;}return ans;}
inline void read(int &x){char c;x=0;for(c=getchar();c>'9'||c<'0';c=getchar());for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';}
inline void read(ll &x){char c;x=0;for(c=getchar();c>'9'||c<'0';c=getchar());for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';}
inline ll qpow(ll x,ll y){ll ans=1;while(y){if(y&1) ans=ans*x%modn;x=x*x%modn;y>>=1;}return ans;}
inline double qpow(double x,ll y){double ans=1;while(y){if(y&1) ans=ans*x;x=x*x;y>>=1;}return ans;}
ll inv[maxn],h,w,a,b;
inline ll C(ll x,ll y)
{
    x=min(x,y-x);
    ll ans=1;
    for(ll up=y,down=1;down<=x;down++,up--) ans=ans*up%modn*inv[down]%modn;
    return ans;
}
int main()
{
    for(ll i=1;i<maxn;i++) inv[i]=qpow(i,modn-2);
    while(scanf("%lld%lld%lld%lld",&h,&w,&a,&b)==4)
    {
        ll ans=C(h-1,w+h-2);
        ll left=C(w-b-1,a+w-b-2),right=C(b-1,h-a+b-1);
        while(a)
        {
            ans=((ans-left*right%modn)%modn+modn)%modn;
            left=left*(a-1)%modn*inv[a+w-b-2]%modn;
            right=right*(h-a+b)%modn*inv[h-a+1]%modn;
            a--;
        }
        printf("%lld\n",ans);
    }
}

#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using base = complex<double>;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M 1000000007
#define M2 998244353
#define ll long long
#define pll pair<long, long>
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define ff first
#define ss second
#define pb push_back
#define db pop_back
#define mp make_pair
#define mt make_tuple
#define g(a, b) get<a>(b)
#define INF (ll)1e18 + 100
#define vl vector<ll>
#define vi vector<int>
#define vll vector<pair<ll, ll>>
#define vii vector<pair<int, int>>
#define all(v) v.begin(), v.end()
#define bset(a, p) ((a) | (1ll << (p)))
#define bchk(a, p) ((a) & (1ll << (p)))
#define bxor(a, p) ((a) ^ (1ll << (p)));
#define brem(a, p) (bchk(a, p) ? (bxor(a, p)) : (a))
#define INT_SIZE 32
/*SOME BITMASK KNOWLEDGE
1)x & (x - 1):sets the last one bit of x to zero
power of two exactly when x & (x − 1) = 0.
2)x & -x:sets all the one bits to zero, except last one bit
3)x | (x - 1):inverts all the bits after the last one bit*/

#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
ll powM(ll x, ll y, ll m)
{
    ll ans = 1, r = 1;
    x %= m;
    while (r > 0 && r <= y)
    {
        if (r & y)
        {
            ans *= x;
            ans %= m;
        }
        r <<= 1;
        x *= x;
        x %= m;
    }
    return ans;
}
ll modI(ll a, ll m)
{
    ll m0 = m, y = 0, x = 1;
    if (m == 1)
        return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m;
        a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
        x += m0;
    return x;
}

ll logint(ll x, ll y)
{
    ll ans = 0;
    ll a = 1;
    for (ll i = 0; i <= x; i++)
    {
        if (x < a)
        {
            return ans;
        }
        ans++;
        a *= y;
    }
    return -1;
}
ll extended(ll a, ll b, ll &x, ll &y)
{
    if (a == 0)
    {
        x = 0;
        y = 1;
        return b;
    }
    ll x1, y1;
    ll d = extended(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}
ll lcm(ll x, ll y) {
    return (x * y) / (__gcd(x, y));
}

void solve() {
    ll sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    // int c = 0;
    REP(j, sx, tx) {
        cout << 'R';
        // c++;
    }
    REP(j, sy, ty) {
        cout << 'U';
        // c++;
    }
    REP(j, sx, tx) {
        cout << 'L';
        // c++;
    }
    REP(j, sy, ty) {
        cout << 'D';
        // c++;
    }
    cout << 'D';
    // c++;
    REP(j, sx, tx) {
        cout << 'R';
        // c++;
    }
    // c++;
    cout << 'R';
    REP(j, sy, ty) {
        // c++;

        cout << 'U';
    }
    cout << 'U';
    cout << 'L';
    cout << 'U';
    // c++;
    // c++;
    // c++;

    REP(j, sx, tx) {
        // c++;
        cout << 'L';
    }
    // c++;
    cout << 'L';
    REP(j, sy, ty) {
        cout << 'D';
    }
    cout << 'D';
    cout << 'R';
    // string temp = "UURRURRDDDLLDLLULUUURRURRDDDLLDL";
    // cout << temp.size() << " " << c;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen("b1.in", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ll ntc;
    // cin >> ntc;
    ntc = 1;
    REP(tc, 1, ntc + 1)
    {
        // cout << "Case #" << tc << ": ";
        solve();
    }
}


#include <vector>
#include <iostream>
#include <valarray>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>
typedef unsigned long long ull;
typedef long long ll;
#define fst first
#define snd second
#define rep(i,n) for(ull i = 0;i<(n);i++)
#define rep1(i,n) for(ull i = 1;i<=(n);i++)
#define revrep(i,n) for(ull i = (n-1);i>=0;i--)
#define all(n) (n).begin(),(n).end()
#define show(x) cout << "debug " << #x << ":" << x << endl;
#define showvec(x) cout << endl << "debug " << #x << ";" << endl; \
    for(ull __i = 0;__i < x.size();__i++){ \
        for(ull __j = 0;__j<x[0].size();__j++){ \
            cout << x[__i][__j] << " "; \
        } \
        cout << endl; \
    } \
    cout << endl;
using namespace std;

int f(int x){
	return x*x;
}

int main(){
	int d;
	while(~scanf("%d\n",&d)){
		ll sum = 0;
		for(int x = d;x <= 600 - d; x += d){
			sum += d * f(x);
		}
		cout << sum << endl;
	}
}

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

int main(){
    int n;
    cin >> n;
    string v = "Vacant";

    vector<string> s(n);
    int l = 0;
    int r = n-1;
    cout << l << endl;
    cin >> s[l];
    if(s[l] == v) return 0;
    cout << r << endl;
    cin >> s[r];
    if(s[r] == v) return 0;

    while(r-l > 0){
        int mid = (l+r) / 2;
        cout << mid << endl;
        cin >> s[mid];
        if(s[mid] == v) return 0;

        if(s[mid] == s[l]){
            if((mid-l)%2 == 0) l = mid;
            else r = mid;
        }
        else{
            if((mid-l)%2 == 0) r = mid;
            else l = mid;
        }
    }

    return 0;

}


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

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<double> VD;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ITER(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9 + 47;
const LL LINF = INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mod = 998244353;

inline int add(int x, int y)
{
	if (x + y < mod)
		return x + y;
	return x + y - mod;
}

inline int sub(int x, int y)
{
	if (x >= y)
		return x - y;
	return x - y + mod;
}

inline int mult(int x, int y)
{
	return x * (LL) y % mod;
}

inline int power(int x, int y)
{
	int r = 1;
	while(y)
	{
		if (y & 1)
			r = mult(r, x);
		x = mult(x, x);
		y >>= 1;
	}

	return r;
}

inline int inverse(int x)
{
	return power(x, mod - 2);
}

const int N = 1 << 22;
int f[N];
int inv[N];

inline int C(int n, int m)
{
	int res = f[n];
	res = mult(res, inv[m]);
	res = mult(res, inv[n - m]);
	return res;
}

inline int count(int n, int m)
{
	return C(n - 1 + m, m);
}

int jebu(int n, int odd, int m)
{
	return count(n, (3 * m - odd) / 2);
}

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("In.txt", "r", stdin);
	//freopen("In.txt", "w", stdout);

	int n, m;
	cin >> n >> m;
	f[0] = 1;
	FOR(i, 1, N)
		f[i] = mult(f[i - 1], i);
	FOR(i, 0, N)
		inv[i] = inverse(f[i]);

	int ans = count(n, 3 * m);
	int tut = 0;
	FOR(i, 2 * m + 1, 3 * m + 1)
		tut = add(tut, count(n - 1, 3 * m - i));

	tut = mult(tut, n);
	ans = sub(ans, tut);

	int odd = 0;
	for(int cnt = m + 2; cnt <= n && cnt <= 3 * m; cnt += 2)
	{
		int coef = C(n, cnt);
		int here = jebu(n, cnt, m);
		odd = add(odd, mult(coef, here));
	}

	ans = sub(ans, odd);
	cout << ans << endl;

	cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
	return 0;
}

#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define vec(s) vector<s>
#define vvec(s) vector<vector<s>>
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli,lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes(x) cout<<(x?"Yes":"No")<<endl;
#define YES(x) cout<<(x?"YES":"NO")<<endl;
#define out(s) cout<<s<<endl;
#define pb(s) push_back(s);
#define sp " "
#define INF 10000000000
#define LINF 9000000000000000000
#define all(s) s.begin(),s.end()
void vout(vi v){
    for(lli i=0;i<v.size();i++)
    cout<<v.at(i)<<endl;
}
int main(){
    cout << std::fixed << std::setprecision(10);
    lli n, m;
    cin>>n>>m;
    lli l=0,r=INF;
    rep(i,m){
        lli x,y;
        cin>>x>>y;
        l=max(l, x);
        r=min(y, r);
    }
    out((r-l+1<0?0:r-l+1));

}


#include<iostream>
using namespace std;

int main(){

	int n, num[10000];
	int max = -1000000, min = 1000000;
	long long sum = 0;

	cin >> n;

	for(int i = 0; i < n; i++){
		cin >> num[i];

		if(min > num[i]) min = num[i];
		if(max < num[i]) max = num[i];
		sum = sum + num[i];
	}

	cout << min << " " << max << " " << sum << endl;

	return 0;
}

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;n>i;++i)
using namespace std;
using ll=int64_t;
using vi=vector<int>;
using VI=vector<ll>;
using vis=vector<string>;
using vvi=vector<vi>;
int main(){
    string s;
    cin>>s;
    string hitachi="";
    for(int i=0;5>i;++i){
        hitachi+="hi";
        if(hitachi==s){
            cout<<"Yes"<<endl;
            return 0;
        }
    }
    cout<<"No"<<endl;
}


#include <bits/stdc++.h>

#define gc( ) std::getchar( )
#define pc( i ) std::putchar( i )

template < typename T >
inline T read( )
{
	register T x = 0;
	register char ch = gc( );
	register bool f = 0;
	while( !std::isdigit( ch ) )
	{
		f = ( ch == '-' );
		ch = gc( );
	}
	while( std::isdigit( ch ) )
	{
		x = x * 10 + ( ch - '0' );
		ch = gc( );
	}
	return f ? -x : x;
}

template < typename T >
void print( T x )
{
	if( x < 0 )
	{
		x = -x;
		pc( '-' );
	}
	if( x < 10 )
	{
		pc( x + 48 );
		return;
	}
	print < T > ( x / 10 );
	pc( x % 10 + 48 );
	return ;
}

namespace Solution
{
#define IOS std::ios::sync_with_stdio( false ), std::cin.tie( 0 ), std::cout.tie( 0 )
#define Rep( i, j, k ) for( int i = j; i >= k; --i )
#define rdi( ) read < int > ( )
#define rdl( ) read < long long > ( )
#define pti( i ) print < int > ( i ), putchar( ' ' )
#define ptl( i ) print < long long > ( i ), putchar( ' ' )
#define For( i, j, k ) for( int i = j; i <= k; ++i )
	using std::cin;
	using std::cout;
	using std::endl;
	using std::vector;
	using std::map;
	using std::queue;
	using std::deque;
	using std::set;
	using std::pair;
	const int Maxn = 100001;
	int n, bol, L, Q, bl[ Maxn ], x[ Maxn ], to[ Maxn ], nxt[ Maxn ], out[ Maxn ];
	int find( int u )
	{
		int l = u + 1, r = n, ans = u;
		while( l <= r )
		{
			int mid = l + r >> 1;
			if( x[ mid ] - x[ u ] <= L )
				ans = mid, l = mid + 1;
			else
				r = mid - 1;
		}
		return ans;
	}
	int solve( int l, int r )
	{
		int res = 0;
		while( bl[ l ] < bl[ r ] )
		{
			res += nxt[ l ];
			l = out[ l ];
		}
		while( l < r )
		{
			res++;
			l = to[ l ];
		}
		return res;
	}
	void fakemain( )
	{
		IOS;
		cin >> n;
		bol = sqrt( n );
		For( i, 1, n )
			cin >> x[ i ],
			bl[ i ] = ( i - 1 ) / bol + 1;
		cin >> L;
		For( i, 1, n )
			to[ i ] = find( i );
		Rep( i, n - 1, 1 )
		{
			if( to[ i ] == n )
			{
				out[ i ] = n;
				nxt[ i ] = 1;
			}
			else
			{
				if( to[ i ] > bol * bl[ i ] )
				{
					out[ i ] = to[ i ];
					nxt[ i ] = 1;
				}
				else
				{
					out[ i ] = out[ to[ i ] ];
					nxt[ i ] = nxt[ to[ i ] ] + 1;
				}
			}
		}
		cin >> Q;
		int l, r;
		while( Q-- )
		{
			cin >> l >> r;
			if( l > r )
				std::swap( l, r );
			cout << solve( l, r ) << '\n';
		}
		return;
	}

} // namespace Solution

int main( int argc, char* argv[] )
{
	Solution::fakemain( );
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;
const ll MAX = 50;

ll N;
ll X[100005];
ll D[100005][MAX];

ll solve(ll a, ll b) {
    if (a > b)
        swap(a, b);
    ll ret = 0;
    for (ll x = MAX - 1; x >= 0; --x) {
        ll nxt = D[a][x];
        if (nxt < 0 || nxt > b)
            continue;
        ret |= (1LL << x);
        a = nxt;
    }
    if (a < b)
        ret++;
    return ret;
}

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

    cin >> N;
    REP(i, N) {
        cin >> X[i];
    }
    ll L;
    cin >> L;

    memset(D, -1, sizeof(D));
    ll n = 0;
    REP(i, N - 1) {
        while (n < N && X[n + 1] - X[i] <= L)
            n++;
        D[i][0] = n;
    }

    FOR(x, MAX, 1) {
        REP(i, N - 1) {
            if (D[i][x - 1] < 0)
                continue;
            D[i][x] = D[D[i][x - 1]][x - 1];
        }
    }

    ll Q;
    cin >> Q;

    REP(_, Q) {
        ll a, b;
        cin >> a >> b;
        cout << solve(a - 1, b - 1) << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int A,B,C,D;
  cin >> A >> B  >> C >> D;
  if(A+B > C+D)
    cout << "Left" << endl;
  if(A+B == C+D)
    cout << "Balanced" << endl;
  if(A+B < C+D)
    cout << "Right" <<endl;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define re register
#define il inline
#define inf 0x3f3f3f

#define maxn 1000005

int n, pos, ans, head, tail;
int q[maxn];
char s[maxn], t[maxn];

int main() {
  scanf("%d%s%s", &n, s + 1, t + 1);
  if (strcmp(s + 1, t + 1) == 0) {
    putchar('0');
    return 0;
  }
  pos = n;
  head = 1;
  tail = 0;
  for (int i = n; i >= 1; i--) {
    if (t[i] == t[i - 1])
      continue;
    pos = min(pos, i);
    while (pos && t[i] != s[pos])
      pos--;
    if (pos == 0) {
      putchar('-');
      putchar('1');
      return 0;
    }
    while (head <= tail) {
      if (q[head] - (tail - head + 1) + 1 > i)
        head++;
      else
        break;
    }
    q[++tail] = pos;
    if (i != pos)
      ans = max(ans, tail - head + 1);
  }
  printf("%d\n", ans + 1);
  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

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

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...);}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif

#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fr(i,x,n) for(int i=x;i>=n;i--)
struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _;

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

int main() {

	int n, l; cin >> n >> l;
	int t = (n * (2 * l + (n - 1))) / 2;
	debug(t);
	int i = min(n, max(1, 1 - l));
	int q1 = (l + max(1, (i - 1)) - 1);
	int q2 = (l + i - 1);
	int q3 = (l + min(n, (i + 1)) - 1);

	debug(i, q2, q2, q3);

	int min_diff = min({abs(q1), abs(q2), abs(q3)});

	if (abs(q1) == min_diff) cout << t - q1;
	else if (abs(q2) == min_diff) cout << t - q2;
	else cout << t - q3;
	return 0;
}

#include <bits/stdc++.h>

typedef long long LL;

const int N = 2e3, M = 4e6, MOD = 1e9 + 7;

int n, k, f[N + 5][N + 5], fac[M + 5], ifac[M + 5];

inline void add(int &x, const int &y) {
	(x += y) >= MOD && (x -= MOD);
}
inline int pow(int x, int k) {
	int ans = 1;
	for (; k > 0; k >>= 1, x = (LL)x * x % MOD) {
		if (k & 1) {
			ans = (LL)ans * x % MOD;
		}
	}
	return ans;
}
inline int inv(int x) {
	return pow(x, MOD - 2);
}
void init(int n) {
	fac[0] = 1;
	for (int i = 1; i <= n; i++) {
		fac[i] = (LL)fac[i - 1] * i % MOD;
	}
	ifac[n] = inv(fac[n]);
	for (int i = n; i >= 1; i--) {
		ifac[i - 1] = (LL)ifac[i] * i % MOD;
	}
}
inline int binom(int n, int m) {
	return (LL)fac[n] * ifac[m] % MOD * ifac[n - m] % MOD;
}
int main() {
	scanf("%d%d", &n, &k);
	if (k == 1) {
		printf("1\n");
		return 0;
	}
	init(n * k);
	f[0][0] = 1;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n && j <= i; j++) {
			if (i < n) {
				add(f[i + 1][j], f[i][j]);
			}
			if (j < n) {
				add(f[i][j + 1], (LL)f[i][j] * (n - j) % MOD * binom(n * k - i - j * (k - 1) - 1, k - 2) % MOD);
			}
		}
	}
	printf("%d\n", f[n][n]);
	return 0;
}


#include <algorithm>
#include <iostream>

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

#define ALL(x) begin(x), end(x)
#define REP(i, n) for (size_t i = 0, i##_len = (n); i < i##_len; ++i)

void solve(ll A, ll B, ll C, ll D) {
  cout <<   min(A,B) + min(C,D) << endl;
}

int main() {
  ll A;
  cin >> A;
  ll B;
  cin >> B;
  ll C;
  cin >> C;
  ll D;
  cin >> D;
  solve(A, B, C, D);
  return 0;
}

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

int main(void){
  int AB[2], CD[2], value;
  for(int i = 0; i < 2; i++)
    cin >> AB[i];
  for(int i = 0; i < 2; i++)
    cin >> CD[i];
  sort(begin(AB), end(AB));
  sort(begin(CD), end(CD));
  value = AB[0] + CD[0];
  cout << value << "\n";
  return 0;
}


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

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

#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);

int main() {
    ll h, w, ans = 0; cin >> h >> w;
    vector<string> s(h);
    rep(i, h) cin >> s[i];

    vector<vector<bool>> used(h, vector<bool>(w, false));

    rep(i, h) {
        rep(j, w) {
            // スタート地点
            if(s[i][j] == '.' && !used[i][j]) {
                ll wh = 0, bl = 0; // 白と黒
                wh++;
                queue<P> que;
                que.push(make_pair(i, j));
                used[i][j] = true;
                // 幅優先
                while(!que.empty()) {
                    P t = que.front();
                    que.pop();
                    ll x = t.first, y = t.second;
                    if(x > 0) if(!used[x-1][y] && s[x][y] != s[x-1][y]) {
                        used[x-1][y] = true;
                        que.push(make_pair(x-1, y));
                        if(s[x-1][y] == '.') wh++;
                        else bl++;
                    }
                    if(x < h-1) if(!used[x+1][y] && s[x][y] != s[x+1][y]) {
                        used[x+1][y] = true;
                        que.push(make_pair(x+1, y));
                        if(s[x+1][y] == '.') wh++;
                        else bl++;
                    }
                    if(y > 0) if(!used[x][y-1] && s[x][y] != s[x][y-1]) {
                        used[x][y-1] = true;
                        que.push(make_pair(x, y-1));
                        if(s[x][y-1] == '.') wh++;
                        else bl++;
                    }
                    if(y < w-1) if(!used[x][y+1] && s[x][y] != s[x][y+1]) {
                        used[x][y+1] = true;
                        que.push(make_pair(x, y+1));
                        if(s[x][y+1] == '.') wh++;
                        else bl++;
                    }
                }
                ans += bl*wh;
            }
        }
    }
    cout << ans << endl;

    return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) {
    if(n == 0) return 1;

    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count));
}

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

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
int main(){
	int h,w;
	char c[400][400];
	cin>>h>>w;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>c[i][j];
		}
	}
	int dx[4]={-1,0,1,0};
	int dy[4]={0,-1,0,1};
	int cnt=0;
	int id[400][400];
	bool used[400][400]={};
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(used[i][j]){
				continue;
			}
			id[i][j]=cnt;
			used[i][j]=1;
			queue<pair<int,int> > que;
			que.push(make_pair(i,j));
			while(!que.empty()){
				int x=que.front().first;
				int y=que.front().second;
				que.pop();
				for(int i=0;i<4;i++){
					int nx=x+dx[i];
					int ny=y+dy[i];
					if(0<=nx&&nx<h&&0<=ny&&ny<w&&!used[nx][ny]){
						if(c[nx][ny]!=c[x][y]){
							used[nx][ny]=1;
							id[nx][ny]=cnt;
							que.push(make_pair(nx,ny));
						}
					}
				}
			}
			cnt++;
		}
    }
	long long sum[160001][2]={};
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(c[i][j]=='#'){
				sum[id[i][j]][0]++;
			}else{
				sum[id[i][j]][1]++;
			}
		}
	}
	long long ans=0;
	for(int i=0;i<cnt;i++){
		ans+=sum[i][0]*sum[i][1];
	}
	cout<<ans<<endl;
	return 0;
}