#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void assert_NO(T condition){ if(!condition){ cout << "NO" << endl; exit(0); } }
template<class T> inline void assert_No(T condition){ if(!condition){ cout << "No" << endl; exit(0); } }
template<class T> inline void assert_minus_1(T condition){ if(!condition){ cout << -1 << endl; exit(0); } }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd_calc(T a, T b){ if(b){ return gcd_calc(b, a % b); }else{ return a; }}
template<class T> T gcd(T a, T b){ if(a < b) swap(a, b); return gcd_calc(a, b); }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
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; }
inline int at(int x, int k){ return (x >> k) & 1; }
random_device rnd;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep2(i, l, n) for(int i = int(l); i < (n); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)

int main(){
    string a;
    cin >> a;
    lint sz = a.size(), ans = sz * (sz + 1) / 2;
    for(char i = 'a'; i <= 'z'; i++){
        sz = count(all(a), i);
        ans -= sz * (sz + 1) / 2;
    }
    cout << ans + 1 << endl;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;

int main(void){
    ll n;
    string s;
    cin>>n;
    cin>>s;
    ll ans = 1;
    ll num = 0;
    for(ll i=0;i<2*n;i++){
        if((2*n-1-i)%2==1){
            if(s[i]=='B')num++;
            else{
                ans = ans*num;
                ans = ans%N;
                num--;
            }
        }
        else{
            if(s[i]=='W')num++;
            else{
                ans = ans*num;
                ans = ans%N;
                num--;
            }
        }
    }
    if(num!=0){
        cout<<0<<endl;
        return 0;
    }
    for(ll i=1;i<=n;i++){
        ans = ans*i;
        ans = ans%N;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int n,m;
  while(cin>>n>>m, n+m) {
    int p[n];
    REP(i,n)
      cin >> p[i];
    vector<int> v(n*n);
    REP(i,n)
      REP(j,n)
      v[i*n + j] = p[i] + p[j];
    v.push_back(0);
    REP(i,n)
      v.push_back(p[i]);
    sort(ALL(v));
    // FOR(it, v)
    //   cout << *it << " ";
    // cout << endl;
    int res = 0;
    REP(i,v.size()) {
      vector<int>::iterator it = upper_bound(ALL(v), m-v[i]);
      int tmp;
      if (it == v.begin()) {
        break;
      } else {
        it--;
        tmp = v[i] + *it;
      }
      res = max(res, tmp);
    }
    cout << res << endl;
  }
}

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

int main() {
	while(true){
		int n, k, x, a, b, fee[101][101];
		cin >> n >> k;

		if(n == 0 && k == 0){
			break;
		}

		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				fee[i][j] = 1000000000;
			}
		}

		for(int i = 0; i < k; i++){
			cin >> x >> a >> b;

			if(x){
				cin >> fee[a][b];
				fee[b][a] = fee[a][b] = min(fee[a][b], fee[b][a]);

				for(int j = 1; j <= n; j++){
					for(int l = 1; l <= n; l++){
						fee[j][l] = min(fee[j][l], fee[j][a] + fee[a][l]);
						fee[l][j] = fee[j][l] = min(fee[j][l], fee[j][b] + fee[b][l]);
					}
				}
			} else{
				if(fee[a][b] != 1000000000){
					cout << fee[a][b] << endl;
				} else{
					cout << -1 << endl;
				}
			}
		}
	}
	return 0;
}


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

int main() {
  int n = 0, tmp0 = 0, tmp1 = 0;
  int tmp2 = 0, tmp3, out1 = 0, out2 = 0;
  int a[100];
  scanf("%d", &n);
  while (n) {
    for (tmp3=0;tmp3<100;tmp3++) a[tmp3] = 0;
    tmp0 = 0, out2 = 0;
    while (tmp0 < n) {
      tmp1 = 0, out1 = 0;
      while (tmp1 < n) {
        scanf("%d", &tmp2);
        a[tmp1] += tmp2;
        printf("%5d", tmp2);
        out1 += tmp2;
        tmp1++;
      }
      printf("%5d\n", out1);
      a[tmp1] += out1;
      tmp0++;
    }
    tmp0 = 0;
    while (tmp0 < n) {
      printf("%5d", a[tmp0]);
      tmp0++;
    }
    printf("%5d\n", a[tmp0]);
    scanf("%d", &n);
  }
  return 0;
}

#include <stack>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <utility>
#include <math.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

int main() {

    int n;
    while(cin >> n && n != 0)
    {
        int a[11][11];
        for(int i = 0;i < n;i++)
        {
            for(int j = 0; j<n;j++)
            {
                cin >> a[i][j];
            }
        }
        for(int i = 0; i<n;i++)
        {
            a[i][n] = 0;
            a[n][i] = 0;
            for(int j = 0;j < n;j++)
            {
                a[i][n] += a[i][j];
                a[n][i] += a[j][i];
            }
        }
        a[n][n] = 0;
        for(int i = 0;i<n;i++)
        {
            a[n][n] += a[i][n];
        }
        for(int i = 0; i< n+1;i++)
        {
            for(int j = 0; j<n+1;j++)
            {
                cout << setw(5) << right << a[i][j];
            }
            cout <<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl
#define show2(a, n, m) rep(i, n) { rep(j, m) {cout << a[i][j] << ' ';} cout << endl;}
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const int INF = 1 << 30;
const long long INFL = 1LL << 60;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

int main() {
//	cout << fixed << setprecision(15);
	ll m;
	cin >> m;
	vll d(m), c(m);
	rep(i, m) cin >> d[i] >> c[i];

	ll sumc = 0;
	ll sumd = 0;
	rep(i, m) sumc += c[i];
	rep(i, m) sumd += c[i] * d[i];

	cout << sumc - 1 + (sumd - 1) / 9 << endl;
}

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

int solve(int n, vector<int> & dims){
    int dp[100][101] = {{0}};
    for (int span = 2; span != n + 1; ++span){
        for (int first = 0; first + span != n + 1; ++first){
            for (int last = first + span; last != n + 1; ++last){
                int record = 2000000000;
                for (int mid = first + 1; mid != last; ++mid){
                    record = min(record, dp[first][mid] + dp[mid][last] + dims[first] * dims[last] * dims[mid]);
                }
                dp[first][last] = record;
            }
        }
    }
    return dp[0][n];
}

int main()
{
    int n = 0;
    cin >> n;
    vector<int> dims(n + 1);
    int r = 0;
    int c = 0;
    for (int i = 0; i != n; ++i){
        cin >> r >> c;
        dims[i] = r;
    }
    dims[n] = c;
    cout << solve(n, dims) << endl;
    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int data[7];
void N(){int x=data[5];data[5]=data[1];data[1]=data[2];data[2]=data[6];data[6]=x;}
void S(){int x=data[5];data[5]=data[6];data[6]=data[2];data[2]=data[1];data[1]=x;}
void E(){int x=data[3];data[3]=data[1];data[1]=data[4];data[4]=data[6];data[6]=x;}
void W(){int x=data[4];data[4]=data[1];data[1]=data[3];data[3]=data[6];data[6]=x;}
int main(){
  string com;
  for(int i=1;i<=6;++i)cin>>data[i];
  getline(cin,com);
  getline(cin,com);
  for(auto ch:com){
    if(ch=='N')N();
    if(ch=='S')S();
    if(ch=='E')E();
    if(ch=='W')W();
  }
  cout<<data[1]<<endl;
  return 0;
}

#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include <ctype.h>
#include <algorithm>
#include <cmath>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

class dice{
    private:
    int s[6];
    int top;
    int bottom;
    int r_side;
    int l_side;
    int face;
    int rev;

    public:
    dice(){
        top = 0;
        bottom = 5;
        r_side = 2;
        l_side = 3;
        face = 1;
        rev = 4;
        s[0] = 1;
        s[1] = 2;
        s[2] = 3;
        s[3] = 4;
        s[4] = 5;
        s[5] = 6;
    }

    dice(int a[5]){
        top = 0;
        bottom = 5;
        r_side = 2;
        l_side = 3;
        face = 1;
        rev = 4;
        REP(i, 6) s[i] = a[i];
    }

    dice(int x, int y, int z, int u, int v, int w){
        top = 0;
        bottom = 5;
        r_side = 2;
        l_side = 3;
        face = 1;
        rev = 4;
        s[0] = x;
        s[1] = y;
        s[2] = z;
        s[3] = u;
        s[4] = v;
        s[5] = w;
    }

    int get_top() {
        return s[top];
    }

    int get_bottom(){
        return s[bottom];
    }

    int get_r_side(){
        return s[r_side];
    }

    int get_l_side(){
        return s[l_side];
    }

    int get_face(){
        return s[face];
    }

    int get_rev(){
        return s[rev];
    }

    int set_top(int i) {
        top = i;
        bottom = 5 - top;
        r_side = (i+1)%5;
        l_side = 5 - r_side;
        face = (i+2)%5;
        rev = 5 - face;
        return s[top];
    }

    int rotate_r(int i) {
        while(i<0)
            i+=4;
        i %= 4;
        REP(j, i){
            int temp = face;
            face = r_side;
            r_side = rev;
            rev = l_side;
            l_side = temp;
        }
        return s[top];
    }

    int roll_r(int i) {
        while(i<0)
            i+=4;
        i %= 4;
        REP(j, i){
            int temp = top;
            top = l_side;
            l_side = bottom;
            bottom = r_side;
            r_side = temp;
        }
        return s[top];
    }

    int roll_f(int i){
        while(i<0)
            i+=4;
        i %= 4;
        REP(j, i){
            int temp = top;
            top = face;
            face = bottom;
            bottom = rev;
            rev = temp;
        }
        return s[top];
    }

};

int main() {
    int in[6];
    REP(i, 6) cin >> in[i];
    dice d = dice(in);
    string roll;
    cin >> roll;
    while(roll.size() != 0){
        string order = roll.substr(0, 1);
        if(order == "E")
            d.roll_r(1);
        else if(order == "S")
            d.roll_f(-1);
        else if(order == "N")
            d.roll_f(1);
        else if(order == "W")
            d.roll_r(-1);
        roll = roll.substr(1);
    }
    cout << d.get_top() << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define INF (1<<29)//INF
#define F first
#define S second
#define VS vector<string>
#define VI vector<int>
#define REPI(i,s,n) for(int i=s;i<n;i++)//sからn未満の間iを回す
#define REPJ(j,s,n) for(int j=s;j<n;j++)//sからn未満の間iを回す
#define RREPI(i,s,n) for(int i=s;i>=n;i--)//sからn以下の間iを回す
#define RREPJ(j,s,n) for(int j=s;j>=n;j--)//sからn以下の間jを回す
#define ALL(a) a.begin(),a.end()//ソートとかに使える sort(ALL(vec))のように
#define SET(a,c) memset(a,c,sizeof(a));//aをc（0or1）で初期化する
#define DEBUGONE(x) cout<<#x<<" : "<<x<<endl;//デバックで一つの変数を表示
#define DEBUGTWO(x,y) cout<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl;//デバックで二つの変数を表示
#define DEBUGTHREE(x,y,z) cout<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl;//デバックで三つの変数を表示
#define OUTONE(x) cout<<x<<endl;//一つの変数を表示
#define OUTTWO(x,y) cout<<x<<" "<<y<<endl;//二つの変数を表示
#define OUTTHREE(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;//三つの変数を表示
typedef long long int64;
typedef pair<string,int> Psi;
typedef pair<int,int> Pii;
map<Psi,int64> mem;
string num[15];
int n,k;
int64 ans=0;
int64 bitdfs(int now,string nows,int bits);
main(){
  while(cin>>n>>k,n||k){
    ans=0;
    mem.clear();
    REPI(i,0,n){
      cin>>num[i];
    }
    cout<<bitdfs(0,"",0)<<endl;
  }
}
int64 bitdfs(int now,string nows,int bits){
  if(now>=k){
    if(mem[Psi(nows,now)]==0){
      mem[Psi(nows,now)]=-1;
      return 1;
    }
    else{
      return 0;
    }
  }
  else{
    int64 cnt=0;
    int bitst=1;
    REPI(i,0,n){
      int b=bitst&bits;
      if(b==0){
	cnt+=bitdfs(now+1,nows+num[i],bits|bitst);
      }
      bitst=bitst<<1;
    }
    return mem[Psi(nows,now)]=cnt;
  }
}

#include <bits/stdc++.h>

using namespace std;

#define INF (1<<29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str)
{
    if (K == 0) {
        st.insert(str);
        return;
    }

    int N = s.size();
    for (int i = 0; i < N; i++) {
        if (S >> i & 1) continue;
        solve(K-1, S|(1<<i), s, str + s[i]);
    }
}

int main()
{
    int N, K;
    while (cin >> N, N) {
        vector<string> s(N);
        cin >> K;
        for (int i = 0; i < N; i++) {
            cin >> s[i];
        }
        st.clear();
        solve(K, 0, s, "");
        cout << st.size() << endl;
    }
    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif

#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef long double f80;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }
ll nxt_C(ll x) { ll c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }

vector<int> get_bits(int mask) {
	vector<int> bb;
	while(mask) { int b = bt(mask); bb.pb(b); mask ^= (1 << b); }
	reverse(all(bb));
	return bb;
}

int get_mask(vector<int> v) {
	int mask = 0;
	for(int x : v) { mask ^= (1 << x); }
	return mask;
}

template<typename T>
void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r){
	uniform_int_distribution<ll> uid(l, r);
	return uid(rng);
}

void sc() {}

template <typename Head, typename... Tail>
void sc(Head &H, Tail &... T) { cin >> H; sc(T...); }

#ifdef LOCAL
#define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// #ifndef LOCAL
// string to_string(__int128 x) {
// 	string s = "";
// 	bool neg = 0;
// 	if(x < 0) { s += "-"; neg = 1; x = -x; }
// 	if(!x) s += '0';
// 	while(x) {
// 		int rem = x % 10;
// 		s += to_string(rem);
// 		x /= 10;
// 	}
// 	reverse(s.begin() + neg, s.end());
// 	return s;
// }
// #endif

const int mod = 1e9 + 7; // 998244353;

int pwr(int a,ll b) {
	int ans = 1;
	while(b) {
		if(b & 1) ans = (ans * 1LL * a) % mod;
		a = (a * 1LL * a) % mod;
		b >>= 1;
	}
	return ans;
}

/*
	Lookout for overflows!!
	Check array sizes!!
	Clear before test cases!!
	Use the correct modulo!!
	Check for corner cases!!
	Are you forgetting something?!
	Read problem statement carefully!!!
*/

const int N = 1e5 + 5;
int dsu[N], sz[N];
bool odd[N], par[N];

int root(int u) {
    while(u != dsu[u]) u = dsu[u];
    return u;
}

int get_par(int u) {
    int p = 0;
    while(u != dsu[u]) {
        p ^= par[u];
        u = dsu[u];
    }
    return p;
}

void uni(int u,int v) {
    int uu = root(u), vv = root(v);
    if(uu == vv) {
        if(get_par(u) == get_par(v))
            odd[uu] = 1;
        return;
    }
    if(sz[uu] > sz[vv]) {
        swap(uu, vv), swap(u, v);
    }
    dsu[uu] = vv;
    sz[vv] += sz[uu];
    odd[vv] |= odd[uu];
    par[uu] = get_par(u) ^ get_par(v) ^ 1;
}

void solve() {
	int n, m;
    sc(n, m);
    fr(i, 1, n) {
        sz[i] = 1;
        dsu[i] = i;
        odd[i] = 0;
        par[i] = 0;
    }
    fr(i, 1, m) {
        int u, v;
        sc(u, v);
        uni(u, v);
    }
    int c1 = 0, c2 = 0, c3 = 0;
    fr(i, 1, n) {
        if(i == dsu[i]) {
            if(sz[i] == 1) {
                c1++;
            }
            else if(odd[i]) {
                c2++;
            }
            else {
                c3++;
            }
        }
    }
    ll ans = 0;
    while(c1) {
        ans += 2 * n - 1;
        n--;
        c1--;
    }
    while(c2) {
        ans += 2 * (c2 + c3) - 1;
        c2--;
    }
    ans += (c3 * 1LL * (c3 - 1)) * 2 + 2LL * c3;
    cout << ans;
}

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

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define se second
#define fi first
#define _ std::ios_base::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1e9+7;
const int LOG = 17;
const int N = 5e5+123;
const ll INF = (ll)1e18 + 7;

int dp[10010][110];

int main()
{
    string s;
    cin >> s;
    int d;
    cin >> d;
    dp[0][0] = 1;
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < d; j++) {
            for (int k = 0; k <= 9; k++) {
                dp[i + 1][(j + k) % d] += dp[i][j];
                dp[i + 1][(j + k) % d] %= mod;
            }
        }
    }
    int sum = 0;
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < s[i] - '0'; j++) {
            ans += dp[(int)s.size() - i - 1][(d - (sum + j) % d) % d];
            ans %= mod;
        }
        sum += s[i] - '0';
    }
    if (sum % d == 0) {
        ans++;
    }
    ans += mod - 1;
    ans %= mod;
    cout << ans;
    return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
 int N,X,T;
 cin >> N >> X >> T;
 int ans = N/X;
 double mod = N%X;
 if(mod > 0){
 ans = ans + 1;
 }
  ans = ans * T;
 cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int mod=1e9+7;
const int N=1e6+10;
void read(int &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
void read(ll &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
int main()
{
    int n,x,t;
    read(n),read(x),read(t);
    if(n%x==0) printf("%d\n",n/x*t);
    else printf("%d\n",(n/x+1)*t);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
using pii = pair<int,int>;
#define ALL(x) (x).begin(),(x).end()

int main(){
	ll K;
	cin >> K;
	ll N = 50;
	vector<ll> A(N);
	// K/N周回す
	for(int i=0;i<N;i++) {
		A[i] = (N-1) + K/N;
	}
	// あまりの処理
	ll d = K % N;
	for(int i=0;i<d;i++) {
		A[i] += N;
		for(int j=0;j<N;j++) {
			if(i == j) continue;
			A[j]--;
		}
	}

	cout << N << endl;
	for(int i=0;i<N;i++) cout << A[i] << ((i == N-1) ? "\n" : " ");
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cassert>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define mp make_pair
#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

    ll k;
    cin >> k;

    ll n = (k + 49) / 50;
    ll m = (50 - (k % 50)) % 50;

    cout << 50 << endl;

    ll a[50];

    REP(i, 50)
    a[i] = 49 + n;

    REP(i, m)
    {
        sort(a, a + 50);

        REP(j, 49)
        a[j]++;

        a[49] -= 50;
    }

    REP(i, 50)
    cout << a[i] << endl;

    return 0;
}


#include <stdio.h>

int main(){

	int H1,H2,M1,M2,K;

	scanf("%d %d %d %d %d",&H1,&M1,&H2,&M2,&K);

	int sum1 = (H1*60) + M1;
	int sum2 = (H2*60) + M2;

	int sum3 = sum2 - sum1;

	printf("%d", sum3 - K);

	return 0;
}

//
//  nomura_a.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
ll mod = 998244353;
ll INF = 1000000007;
#define PI 3.14159265

pair<int, int> dijsktra(int x, char des, int n, vector<pair<int, char> > *adj) {
  priority_queue<pair<pair<int, char>, int> >  q; // (-d, x) current distance to node x is d, using -d to find minimum distance
  bool processed[n];
  pair<int, char> distance[n];
  for (int i=0; i<n; i++) {
    distance[i].first = INF;
    distance[i].second = '0';
    processed[i] = false;
  }
  // find from x
  distance[x] = make_pair(0, '0');
  q.push({distance[x], x});

  while (!q.empty()) {
    int a = q.top().second;
    q.pop();
    if (processed[a]) continue;;
    processed[a] = true;
    for (auto u: adj[a]) {
      int b = u.first; // node b
      char label = u.second;
      int w = 1; // distance from a to b is w
      if (distance[b].first > distance[a].first + w) {
        distance[b] = make_pair(distance[a].first + w, label);
        q.push({{-distance[b].first, label}, b});
      }
    }
  }

  int l = INF;
  int end = 0;
  // std::cout << "distance from " << x << std::endl;
  for (int i = 0; i < n; i++) {
    if (distance[i].first==INF) continue;
    if (distance[i].second == des && distance[i].first < l) {
      l = distance[i].first;
      end = i;
    }
    // std::cout << " distance to " << i << ": " << distance[i] << std::endl;
  }

  return make_pair(l, end);
}

int main() {
  int h1, m1, h2, m2, k;
  cin >> h1 >> m1 >> h2 >> m2 >> k;

  std::cout << h2*60+m2 - (h1*60+m1) - k << std::endl;

  return 0;
}


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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

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

    vector<int> AC(N,0);
    FOR(i,1,N){
        if(S[i-1]=='A' && S[i]=='C'){
            AC[i] += 1;
        }
        AC[i] += AC[i-1];
    }

    REP(i,Q){
        int l,r; cin >> l >> r;
        l--;
        r--;
        cout << AC[r] - AC[l] << endl;
    }
    return 0;
}

#include<iostream>
// Need start
#include<vector>
#include<algorithm>
#include<functional>
// Need end
#include<cmath>
#include<iomanip>
using namespace std;

// Copy start
template<typename T>
class Polygon2D {
    private:
        T x_, y_;
        int idx_;
    public:
        Polygon2D() {}
        Polygon2D(T x, T y):x_(x), y_(y) {}
        Polygon2D(T x, T y, int idx):x_(x), y_(y), idx_(idx) {}  // for convex-full
        static T dot(const Polygon2D<T>& p1, const Polygon2D<T>& p2) {return p1.x_ * p2.x_ + p1.y_ * p2.y_;}
        static T det(const Polygon2D<T>& p1, const Polygon2D<T>& p2) {return p1.x_ * p2.y_ - p1.y_ * p2.x_;}
        static T pow(const Polygon2D<T>& p) {return dot(p, p);}
        const Polygon2D operator=(const Polygon2D<T>& p) {x_ = p.x_; y_ = p.y_; idx_ = p.idx_; return *this;}
        const Polygon2D operator+(const Polygon2D<T>& p) const {Polygon2D<T> res = *this; res.x_ += p.x_; res.y_ += p.y_; return res;}
        const Polygon2D operator-(const Polygon2D<T>& p) const {Polygon2D<T> res = *this; res.x_ -= p.x_; res.y_ -= p.y_; return res;}
        T getX() const {return x_;}
        T getY() const {return y_;}
        int getIdx() const {return idx_;}
        friend ostream& operator<<(ostream& stream, const Polygon2D& p) {stream << '(' << p.x_ << ',' << p.y_ << ')'; return stream;}
};

// Graham's scan
template<typename T>
vector<Polygon2D<T> > convex_hull(vector<Polygon2D<T> > ps, int vertex_num) {
    sort(ps.begin(), ps.end(), [](const Polygon2D<T>& p, const Polygon2D<T>& q) {
        if (p.getX() != q.getX()) return p.getX() < q.getX();
        return p.getY() < q.getY();
    });
    int k = 0;
    vector<Polygon2D<T> > qs(vertex_num * 2);
    // bottom
    for (int i = 0; i < vertex_num; i++) {
        while (k > 1 && Polygon2D<T>::det(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0) k--;
        qs[k++] = ps[i];
    }
    // up
    for (int i = vertex_num - 2, t = k; i >= 0; i--) {
        while (k > t && Polygon2D<T>::det(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0) k--;
        qs[k++] = ps[i];
    }
    qs.resize(k - 1);
    return qs;
}
// Copy end

const double PI = 3.14159265358979323846264338327950288;

int main() {
    // This sample is AtCoder Grand Contest 21 B
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<Polygon2D<double>> ps(N);
    double x, y;
    for (int i = 0; i < N; ++i) {
        cin >> x >> y;
        ps[i] = Polygon2D<double>(x, y, i);
    }
    if (N == 2) {
        cout << fixed << setprecision(7);
        cout << 0.5 << endl;
        cout << 0.5 << endl;
        return 0;
    }
    vector<Polygon2D<double> > qs = convex_hull(ps, N);
    qs.push_back(qs[0]);
    qs.push_back(qs[1]);
    vector<double> ans(N, 0.0);
    for (int i = 0; i < (int)qs.size() - 2; i++) {
        Polygon2D<double> pr = qs[i] - qs[i + 1], pq = qs[i + 2] - qs[i + 1];
        double len_pr = Polygon2D<double>::pow(pr);
        double len_pq = Polygon2D<double>::pow(pq);
        ans[qs[i + 1].getIdx()] = (PI - acos(Polygon2D<double>::dot(pr, pq) / sqrt(len_pr * len_pq))) / (2 * PI);
    }
    cout << fixed << setprecision(7);
    for (int i = 0; i < N; i++) {
        cout << ans[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <limits.h>

using namespace std;

int m;

int dijkstra( const int data[101][101], int start, int end )
{
	int i,j,next,min;
	bool visited[101];
	int distance[101];

	for( i = 1;i <= 100;i++ ){
		visited[i] = false;
		distance[i] = INT_MAX;
	}

	distance[start] = 0; next = start;

	do{
		i = next; visited[i] = true; min = INT_MAX;
		for(j = 1;j <= m;j++){
			if(visited[j]) continue;
			if(data[i][j] < INT_MAX && distance[i] + data[i][j] < distance[j]) {
				distance[j] = distance[i] + data[i][j];
			}
			if( distance[j] < min ){
				min = distance[j]; next = j;
			}
		}
	} while( min < INT_MAX );

	return distance[end];
}

int main(int argc, char const* argv[])
{
	int n,weight[101][101][2];

	while( cin >> n >> m && n != 0 && m != 0 ){
		int a,b,c,d;
		for( int i = 0;i < 101;i++ ){
			for( int j = 0;j < 101;j++ ){
				weight[i][j][0] = INT_MAX;
				weight[i][j][1] = INT_MAX;
			}
		}

		for( int i = 0;i < n;i++ ){
			cin >> a >> b >> c >> d;
			weight[a][b][0] = c;
			weight[b][a][0] = c;
			weight[a][b][1] = d;
			weight[b][a][1] = d;
		}

		cin >> n;

		for( int i = 0;i < n;i++ ){
			int data[101][101];
			int p,q,r;
			cin >> p >> q >> r;
			for( int j = 1;j < 101;j++ ){
				for( int k = 1;k <101;k++ ) data[j][k] = weight[j][k][r];
			}
			cout << dijkstra( data, p, q ) << endl;
		}
	}

	return 0;

}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long INTEGER;
typedef double FLOAT;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

const int MM = 100;
int cost[MM][MM];
int toki[MM][MM];

int main() {

entry:;
  int n, m; cin >> m >> n;
  if (n + m == 0) return 0;

  rep (i, n) {
    rep (j, n) {
      cost[i][j] = INF;
      toki[i][j] = INF;
    }
    cost[i][i] = 0;
    toki[i][i] = 0;
  }

  rep (i, m) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    --a; --b;
    cost[a][b] = cost[b][a] = c;
    toki[a][b] = toki[b][a] = t;
  }

  rep (k, n) {
    rep (i, n) {
      rep (j, n) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        toki[i][j] = min(toki[i][j], toki[i][k] + toki[k][j]);
      }
    }
  }

  //rep (i,n) { rep(j,n) { cout << cost[i][j] << ' '; } cout << endl; }

  int k; cin >> k;
  rep (i, k) {
    int a, b, t;
    cin >> a >> b >> t;
    --a; --b;
    if (t) {
      cout << toki[a][b] << endl;
    } else {
      cout << cost[a][b] << endl;
    }
  }

  goto entry;

  return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll n,q,m=0;
    cin>>n>>q;
    vector<ll> a(n+1);
    string s;
    cin>>s;
    for(int i=1;i<n;i++)
       {
        if(s[i-1]=='A'&& s[i]=='C')
        {
            a[i]=1;
        }
       }
    for(int i=1;i<=n;i++)
      a[i]+=a[i-1];
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        if(r==l) cout<<0<<endl;
        else
        { r--;
         cout<<a[r]-a[l-1]<<endl;
        }
    }
    return 0;
}

/*
 * @Author: RBQRBQ
 * @Date: 2020-04-05 15:08:15
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2020-04-05 19:16:50
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll,ll> pii;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;ll f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
        x=x*f;
    }
}
using namespace IO;
vector<pii> pp;
vector<pii> T;
ll mxL,minR,mxR,minL;

int main()
{
    int N;
    cin>>N;
    mxL=mxR=-1;
    minL=minR=10000000000ll;
    int P,Q;
    for(int i=1;i<=N;i++)
    {
        ll x,y;
        read(x),read(y);
        y++;
        pp.push_back(make_pair(x,y));
        if(x>mxL)
        {
            mxL=x;
            P=i;
        }
        if(minR>y)
        {
            minR=y;
            Q=i;
        }
    }
    ll ans1=0,ans2=0;
    minL=0;
    for(int i=1;i<=N;i++)
    {
            minL=max(minL,pp[i-1].second-pp[i-1].first);
    }

    ans1=max((minL),0ll)+max((minR-mxL),0ll);

    int ca=1;
    for(auto p:pp)
    {
        if(ca!=P&&ca!=Q)
        T.push_back(make_pair(max(p.second-mxL,0ll),max(minR-p.first,0ll)));
        ca++;
    }
    sort(T.begin(),T.end());
    ll P1=pp[P-1].second-pp[P-1].first;
    ll Q1=pp[Q-1].second-pp[Q-1].first;
    ll x1=0,x2=Q1;
    // for(auto p:T)
    // {
    //     cout<<p.first<<" "<<p.second<<endl;
    // }
    for(int i=0;i<T.size();i++)
    {

        x1=min(T[i].first,P1);
        ans2=max(ans2,x1+x2);
        x2=min(T[i].second,x2);
    }
    ans2=max(ans2,P1+x2);
    cout<<max(ans1,ans2);

}


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

int main() {
	vector<int> vec(4); int his, geo, ans;
	for (int i = 0; i < 4; i++) cin >> vec[i];
	cin >> his >> geo;
	sort(vec.begin(), vec.end(), greater<int>());
	ans = vec[0] + vec[1] + vec[2] + (his > geo ? his : geo);
	cout << ans << endl;
	return 0;
}


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

int main() {
  vector<long long> A(3);
  for (int i = 0; i < 3; ++i) cin >> A[i];
  long long K;
  cin >> K;
  sort(A.begin(), A.end());
  for (int i = 0; i < K; ++i) A.back() *= 2;
  cout << A[0] + A[1] + A[2] << endl;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
#include<stdio.h>
#include<string.h>
#include <bitset>
// #include<assert.h>
#include<random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 100010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

//https://www.youtube.com/watch?v=RDaGj_ztNz4&feature=youtu.be

//-----CODE------//
void solve(void)
{
  int n;
  cin>>n;
  vector<int> a (n,0);
  for (int i = 0; i < n; i++)
  {
     cin>>a[i];
  }
  vector<vector<int> > dp (n+1,vector<int>(4,-1*INF));
  dp[0][0] = 0;
  int k = 1 +n%2;

  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < k+1; j++)
    {
      chmax(dp[i+1][j+1],dp[i][j]);
      int now = dp[i][j];
      if((i+j)%2 == 0) now += a[i];
      chmax(dp[i+1][j],now);
    }

  }
  cout<<dp[n][k]<<endl;

  return;
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


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

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

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long i = 2; i * i <= N; ++i) {
        if (N % i != 0) {
            continue;
        }
        long long ex = 0; // 指数
        // 割れるだけ割る
        while (N % i == 0) {
            ++ex;
            N /= i;
        }
        // 結果を push
        res.push_back({i, ex});
    }

    // 残った数はそのまま push
    if (N != 1) {
        res.push_back({N, 1});
    } else {
        res.push_back({1, 1});
    }

    return res;
}

int main() {
    long long N, target = 1, ans = 1, num = 1000000007;
    cin >> N;

    vector<long long> ex(N+1, 0); //指数情報を格納していく
    for (int i = 1; i <= N; ++i) {
        const auto &res = prime_factorize(i);
        for (auto p : res) {
            ex[p.first] += p.second;
        }
    }

    for (long long i = 2; i <= N; ++i) {
        ans *= ex[i] + 1 ;
        ans %= num;
    }

    cout << ans << endl;

    return 0;
}

// C - Factors of Factorial
#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
#define int long long
#define MOD 1000000007
typedef vector<int> vi;
typedef pair<int,int> pi;
#define inf 1e18
#define For(i,a,b) for (int i = a; i < b; i++)
#define Rep(i,a,b) for (int i = a; i <= b; i++)
#define ps(x,y) fixed << setprecision(y) << x
#define pb push_back
#define mp make_pair

int sieve[1005];
vi primes;

int32_t main()
{
    fast;
    int n, ans = 1; cin >> n;
    for(int x = 2; x <= n; x++) {
        if(sieve[x]) continue;
        for(int u = 2*x; u <= n; u += x) {
            sieve[u] = x;
        }
    }

    Rep(i, 2, n) {
        int cnt = 0;
        if(sieve[i] == 0) {
            int exp = 1;
            while(pow(i, exp) <= n) {
                int x = pow(i, exp);
                cnt += (n/x);
                exp++;
            }
            ans *= (cnt+1);
            ans %= MOD;
        }
    }

    cout << ans;

    return 0;
}


#include<iostream>
using namespace std;
int main(){
    int a,b,c,sum,maxim;
    cin>>a>>b>>c;
    sum=a+b+c;
    maxim=max(a,b);
    maxim=max(maxim,c);
    cout<<sum-maxim;
}

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

signed main() {
    int a[3];

    for ( int i = 0; i < 3; i++ ) {
        cin >> a[i];
    }

    sort(a, a+3);
    cout << a[0] + a[1] << endl;

    return (0);
}


#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<ll,ll> P;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define EFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i, n) FOR(i,0,n)
#define IINF 1e9

int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
// memo:BFS
int main(void){
    int h,w,ans = 0;
    cin >> h >> w;
    vector<vector<char>> s(h,vector<char>(w));
    vector<vector<int>> dist(h,vector<int>(w,-2));
    queue<P> q;
    int c_hash = 0;
    REP(i,h)REP(j,w){
        cin >> s[i][j];
        if(s[i][j] == '#') dist[i][j] = -1,c_hash++;
    }

    q.push(P(0,0));
    dist[0][0] = 0;
    while(!q.empty()){
        P v = q.front();
        q.pop();
        REP(i,4){
            int vx = v.first + dx[i];
            int vy = v.second + dy[i];
            if(vx < 0 || vx >= h || vy < 0 || vy >= w) continue;
            if(dist[vx][vy] == -2){
            dist[vx][vy] = dist[v.first][v.second] + 1;
            q.push(P(vx,vy));
            }

        }
    }

    if(dist[h-1][w-1] > 0 ) ans = h*w - c_hash - dist[h-1][w-1] - 1;
    else ans = -1;
    cout << ans << endl;
    return 0;
}

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

using namespace std;

#define FOR(i,a,b) for (ll i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

typedef long long ll;

int a[1000];
int b[1000];
int c[1000];

int main(){
	int n, m;

	cin >> n >> m;

	REP(i,n){
		scanf("%d", &a[i]);
	}

	REP(i,m){
		scanf("%d", &b[i]);
	}

	REP(i,m){
		REP(j,n){
			if (a[j] <= b[i]){
				c[j]++;
				break;
			}
		}
	}

	int ma = 0;
	int ans;

	REP(i,n){
		if (c[i] > ma){
			ma = c[i];
			ans = i+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

int main(){

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

    int aux = 1;
    int res = 0;

    while(true){
        if(aux >= n){break;}
        --aux;
        aux += m;
        res++;
    }

    cout << res <<"\n";

    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int main(){
  int k,t;
  cin >> k >> t;
  vector<int> n(t);
  for(int i = 0;i < t;i++)cin >> n[i];
  sort(n.begin(),n.end());
  cout << max(0,n[t - 1] - (k - n[t - 1]) - 1) << endl;
}


#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) printf("%lld ",i);puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 2e5+10;
int n;

inline PA Get(int d,ll c){
	if (c==1) return {d,0};
	PA tmp=Get(d,c/2);
	tmp.se=(tmp.se*2+1+(tmp.fi*2>=10));
	if (tmp.fi*2>=10) tmp.fi=(tmp.fi*2)/10+(tmp.fi*2)%10;
		else tmp.fi*=2;
	if (c&1){
		tmp.se=tmp.se+1+(tmp.fi+d>=10);
		tmp.fi=(tmp.fi+d)/10+(tmp.fi+d)%10;
	}
	return tmp;
}

int main(){
	n=read();
	ll x=0,ans=0;
	For(i,1,n){
		ll d=read(),c=read();
		PA tmp=Get(d,c);
		ans+=tmp.se;
		if (i!=1) ans++;
		ans+=(x+tmp.fi>=10);
		x=(x+tmp.fi)/10+(x+tmp.fi)%10;
		//printf(" %lld %lld\n",x,ans);
	}
	printf("%lld\n",ans);
}

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int mod=998244353,N=305;
int nm,m,n,f[N][N][N],ans,sum[N],a[N];
char s[N];

signed main(){
	scanf("%s",s+1);read(m);
	n=strlen(s+1);s[++n]='0';
	for(int i=1;i<=n;i++){
		if(s[i]=='0') nm++;
		else a[nm+1]++;
	}
	for(int i=1;i<=nm;i++) sum[i]=sum[i-1]+a[i];
	m=min(m,sum[nm]);
	f[0][0][0]=1;
	for(int i=1;i<=nm;i++) 	for(int j=sum[i];j<=sum[nm];j++){
		int lim=min(j,sum[nm]-sum[i-1]);
		for(int k=0;k<=m;k++) for(int bi=0;bi<=lim;bi++) if(max(0,bi-a[i])<=k)
			f[i][j][k]=(f[i][j][k]+f[i-1][j-bi][k-max(0,bi-a[i])])%mod;
	}
	for(int i=0;i<=m;i++) ans=(ans+f[nm][sum[nm]][i])%mod;
	write(ans);
}


#include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

signed main(void){
  int i, j, k;
  for(int testcase = 0;testcase >= 0;testcase++){
    int2(n, m);
    int a[n], cnt[n];
    scn(n, a);
    rep(i, n)cnt[i] = 0;

    rep(i, m){
      int(b);
      rep(j, n)if(b >= a[j]){
        cnt[j]++; break;
      }
    }

    int ma = 0;
    rep1(i, n)if(cnt[ma] < cnt[i])ma = i;
    pri(ma + 1);

/*/

//*/ break;
  }
  return 0;
}

#include<iostream>
using namespace std;
int main(void)
{
    int n,m,mo[1001],bo[1001],ans[1001],A=0,i2;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        ans[i]=0;
    }
    for(int i=0;i<n;i++){
        cin>>mo[i];
    }
    for(int i=0;i<m;i++){
        cin>>bo[i];
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(mo[j]<=bo[i]){
                i2=j;
                break;
            }

        }
        ans[i2]+=1;
    }
    for(int i=0;i<n;i++){
        if(A<ans[i]){
            A=ans[i];
            i2=i;
        }
    }
    cout<<i2+1<<"\n";
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=1e9;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};

int main(){
  double n;
  cin>>n;
  vector<double> x(n),y(n);
  rep(i,n) cin>>x[i]>>y[i];
  vector<int> t(n);
  rep(i,n) t[i]=i;
  double sum=0;
  int cnt=0;
  auto dis= [&](int i,int j){
    double dx=x[i]-x[j];
    double dy=y[i]-y[j];
    return sqrt(dx*dx+dy*dy);
  };
  do{
    for(int i=1;i<n;i++){
      sum+=dis(t[i],t[i-1]);
    }
    cnt++;
  }while(next_permutation(all(t)));
  printf("%.10f\n",sum/cnt);
}

//最少全域木
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[103][103];
int V;
int NG[103][103];//新しくできるほう
int used[103];
int main(){
	int ans=0;
	cin>>V;
	for(int i=0;i<V;i++){
		for(int j=0;j<V;j++){
		cin>>G[i][j];
		}
	}
	for(int i=0;i<103;i++)for(int j=0;j<103;j++)NG[i][j]=-1;
	used[0]=1;
	for(int i=0;i<V;i++){
		int u=-1,a,b;
		for(int j=0;j<V;j++){
		if(used[j]==0)u=i;
		}
		if(u==-1)break;
		int now=99999999;
		for(int j=0;j<V;j++){
			for(int k=0;k<V;k++){
			if(now>G[j][k]&&G[j][k]!=-1&&used[j]==1&&used[k]==0){
			now=G[j][k];
			a=j,b=k;
			}
			}
		}
		ans+=G[a][b];
		used[b]=1;
		NG[a][b]=G[a][b];
	}
	cout<<ans<<endl;
	return 0;
}


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

int n;

bool trust(int flag, int index) {
    bool trust = (flag >> index) & 1;
    return trust;
}

int count (int flag) {
    int dst = 0;
    rep(i, n) if (trust(flag, i)) dst++;
    return dst;
}

int main() {
    cin >> n;
    vector<vector<P>> evidence(n);

    rep(i, n) {
        int a;
        cin >> a;
        evidence.at(i).resize(a);
        rep(j, a) {
            cin >> evidence.at(i).at(j).first >> evidence.at(i).at(j).second;
            evidence.at(i).at(j).first--;
        }
    }
    int ans = 0;
    for (int flag = 0; flag < (1<<n); flag++) {
        bool ok = true;
        rep(i, n) {
            if (trust(flag, i)) {
                for (P p : evidence.at(i)) {
                    if (trust(flag, p.first) ^ (p.second == 1)) {
                        ok = false;
                        break;
                    }
                }
            }
        }
        if (ok) ans = max(ans, count(flag));
    }
    cout << ans << endl;
    return 0;
}

#include<iostream>
using namespace std;
int main(){
	int n;
	string A,B,C;
	cin>>n;
	int count=0;
	cin>>A>>B>>C;
	for(int i=0;i<n;i++){
		if(A[i]==B[i]){
			if(B[i]!=C[i])
			count++;
		}
		else if(B[i]==C[i]){
			count++;
		}
		else if(A[i]==C[i]){
			count++;
		}
		else{
			count+=2;
		}
	}
	cout<<count;
}


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

const ll MMM = -1e18;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	int a[n];
	for (int i=0; i<n; i++) cin >> a[i];
	int m = n/2;
	if (n&1) {
		ll ans = MMM;
		vector<ll> suff(m+1), pref(m+1), best_suff(m+1);
		// both end case
		for (int i=0; i<m; i++) {
			pref[i+1] = pref[i] + a[2*i];
		}
		for (int i=m-1; i>=0; i--) {
			suff[i] = suff[i+1] + a[2*i+2];
		}
		// single 3-jump
		for (int i=0; i<=m; i++) {
			ans = max(ans, pref[i] + suff[i]);
		}

		// double 2-jump
		for (int i=m-1; i>=0; i--) {
//			best_suff[i] = max(best_suff[i+1] + a[i*2+1], suff[i+1] + a[i*2+1]);
			best_suff[i] = max(best_suff[i+1], suff[i+1]) + a[i*2+1];
		}

		bool dbg = false;
		if (dbg) {
			cout << "PREF "; for (ll x : pref) cout << x << ' '; cout << '\n';
			cout << "SUFF "; for (ll x : suff) cout << x << ' '; cout << '\n';
			cout << "BEST "; for (ll x : best_suff) cout << x << ' '; cout << '\n';
		}

		ll best_pref = 0;
		ans = max(ans, best_suff[0]);
		for (int i=0; i<m; i++) {
			best_pref = max(best_pref, pref[i]) + a[i*2+1];
			ans = max(ans, best_pref + best_suff[i+1]);
		}
//		ans = max(ans, max(best_pref, pref[

		// one end case
		// should have covered by double 2-jump

		// neither end case
		ll tmp = 0;
		for (int i=0, idx=1; i<m; i++, idx+=2) tmp += a[idx];
		ans = max(ans, tmp);
		cout << ans;
	}
	else {
		vector<ll> suff(m+1);
		for (int i=m-1; i>=0; i--) {
			suff[i] = suff[i+1] + a[2*i+1];
		}
		ll ans = suff[0];
		ll pref = 0;
		for (int i=0; i<m; i++) {
			pref += a[i*2];
			ans = max(ans, pref + suff[i+1]);
		}
		cout << ans;
	}
}

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

template <typename T> void print(T t) { cout<<t<<endl; }
template<typename T, typename... Args> void print(T t, Args... args) { cout<<t<<" "; print(args...); }

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define int long long
#define double long double

int arr[200005];
int cache[200005][3];
int vis[200005][3];
int n;
int dp(int pos,int skip)
{
    if(skip>n%2+1)
        return -1e18;
    if(pos>=n)
    {
        if(!skip && n%2)
            return -1e18;
        return 0;
    }
    if(vis[pos][skip])
        return cache[pos][skip];
    vis[pos][skip]=1;
    int ans=max(dp(pos+1,skip+1),arr[pos]+dp(pos+2,skip));
    cache[pos][skip]=ans;
    return ans;
}
int32_t main()
{
    IOS;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>arr[i];
    print(dp(0,0));
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s)    (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
#define x first
#define y second
#define r 0
#define l 1
#define u 2
#define d 3
const int N = 2e5 + 9;
vi dia[N + N + 9][2][4];
vi arr[N][4];
int gt(char ch) {
	if (ch == 'R') return r;
	if (ch == 'L') return l;
	if (ch == 'U') return u;
	if (ch == 'D') return d;
}
int main() {
	file();
	fast();
	int n; cin >> n;
	for (int i = 0; i < n; i++) {
		char ch; int xx, yy;
		cin >> xx >> yy >> ch;
		int g = gt(ch);
		dia[yy - xx + N][l][g].push_back(yy);
		dia[yy + xx][r][g].push_back(yy);
		if (g == l || g == r) arr[yy][g].push_back(xx);
		else arr[xx][g].push_back(yy);
	}

	for (int i = 0; i < N + N; i++)
		for (int j = 0; j < 4; j++)
			sort(all(dia[i][l][j])), sort(all(dia[i][r][j]));
	for (int i = 0; i < N; i++)
		for (int j = 0; j < 4; j++)
			sort(all(arr[i][j]));

	int ans = INT_MAX;
	// on the same horizonatl
	for (int i = 0; i < N; i++) {
		for (auto& it : arr[i][r]) {
			auto bs = lower_bound(all(arr[i][l]), it);
			if (bs != arr[i][l].end())
				ans = min(ans, (*bs - it) * 5);
		}
	}
	// on the same vertically
	for (int i = 0; i < N; i++) {
		for (auto& it : arr[i][u]) {
			auto bs = lower_bound(all(arr[i][d]), it);
			if (bs != arr[i][d].end())
				ans = min(ans, (*bs - it) * 5);
		}
	}

	// from r or l to d
	for (int i = 0; i < N + N; i++) {
		for (auto& it : dia[i][l][r]) {
			auto bs = lower_bound(all(dia[i][l][d]), it);
			if (bs != dia[i][l][d].end())
				ans = min(ans, (*bs - it) * 10);
		}
		for (auto& it : dia[i][r][l]) {
			auto bs = lower_bound(all(dia[i][r][d]), it);
			if (bs != dia[i][r][d].end())
				ans = min(ans, (*bs - it) * 10);
		}
	}
	// from u to r or l
	for (int i = 0; i < N + N; i++) {
		for (auto& it : dia[i][l][u]) {
			auto bs = lower_bound(all(dia[i][l][l]), it);
			if (bs != dia[i][l][l].end())
				ans = min(ans, (*bs - it) * 10);
		}
		for (auto& it : dia[i][r][u]) {
			auto bs = lower_bound(all(dia[i][r][r]), it);
			if (bs != dia[i][r][r].end())
				ans = min(ans, (*bs - it) * 10);
		}
	}
	if (ans == INT_MAX)
		RT("SAFE\n");
	cout << ans << endl;
	return 0;
}

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define IL inline
#define reg register
#define LL long long
#define N 200010
#define INF 0x3f3f3f3f
using namespace std;

int n,i,j;
int ans=INF;
set <int> S[N*4];

struct Data{
	int x,y;
	char c;
}a[N];

IL int Abs(int x){return (x<0)?-x:x;}
IL void Swap(int &a,int &b){a^=b^=a^=b;}
IL int Min(int a,int b){return (a<b)?a:b;}
IL int Max(int a,int b){return (a>b)?a:b;}

IL int read(){
	int p=0,f=1;	char	c=getchar();
	while (c<48||c>57)	{if (c=='-')	f=-1;	c=getchar();}
	while (c>=48&&c<=57)	p=(p<<1)+(p<<3)+c-48,c=getchar();
	return p*f;
}

int main(){
	#ifdef __Marvolo
	freopen("zht.in","r",stdin);
	freopen("zht.out","w",stdout);
	#endif
	n=read();
	for (i=1;i<=n;i++){
		a[i].x=read();	a[i].y=read();	a[i].c=getchar();
	}
	if (n==1){
		puts("SAFE");
		return 0;
	}

	for (i=1;i<=n;i++)
		if (a[i].c=='U')	S[a[i].x+a[i].y].insert(a[i].x);

	for (i=1;i<=n;i++){
		if (a[i].c=='R'){
			if (!S[a[i].x+a[i].y].size())	continue;
			j=*(--S[a[i].x+a[i].y].end());
			if (j<a[i].x)	continue;
			j=*S[a[i].x+a[i].y].upper_bound(a[i].x);
			ans=Min(ans,j-a[i].x);
		}
	}
	for (i=0;i<4*N;i++)	S[i].clear();
	//U--R

	for (i=1;i<=n;i++)
		if (a[i].c=='D')	S[a[i].x+a[i].y].insert(a[i].x);

	for (i=1;i<=n;i++){
		if (a[i].c=='L'){
			if (!S[a[i].x+a[i].y].size())	continue;
			j=*S[a[i].x+a[i].y].begin();
			if (j>a[i].x)	continue;
			j=*(--S[a[i].x+a[i].y].upper_bound(a[i].x));
			ans=Min(ans,a[i].x-j);
		}
	}
	for (i=0;i<4*N;i++)	S[i].clear();
	//D--L

	for (i=1;i<=n;i++)
		if (a[i].c=='R')	S[a[i].y-a[i].x+N].insert(a[i].x);

	for (i=1;i<=n;i++){
		if (a[i].c=='D'){
			if (!S[a[i].y-a[i].x+N].size())	continue;
			j=*S[a[i].y-a[i].x+N].begin();
			if (j>a[i].x)	continue;
			j=*(--S[a[i].y-a[i].x+N].upper_bound(a[i].x));
			ans=Min(ans,a[i].x-j);
		}
	}
	for (i=0;i<4*N;i++)	S[i].clear();
	//D--R

	for (i=1;i<=n;i++)
		if (a[i].c=='L')	S[a[i].y-a[i].x+N].insert(a[i].x);

	for (i=1;i<=n;i++){
		if (a[i].c=='U'){
			if (!S[a[i].y-a[i].x+N].size())	continue;
			j=*(--S[a[i].y-a[i].x+N].end());
			if (j<a[i].x)	continue;
			j=*S[a[i].y-a[i].x+N].upper_bound(a[i].x);
			ans=Min(ans,j-a[i].x);
		}
	}
	for (i=0;i<4*N;i++)	S[i].clear();
	//D--R
	if (ans!=INF)	ans*=2;

	for (i=1;i<=n;i++)
		if (a[i].c=='R')	S[a[i].y].insert(a[i].x);

	for (i=1;i<=n;i++){
		if (a[i].c=='L'){
			if (!S[a[i].y].size())	continue;
			j=*S[a[i].y].begin();
			if (j>a[i].x)	continue;
			j=*(--S[a[i].y].upper_bound(a[i].x));
			ans=Min(ans,a[i].x-j);
		}
	}
	for (i=0;i<4*N;i++)	S[i].clear();
	//L--R

	for (i=1;i<=n;i++)
		if (a[i].c=='U')	S[a[i].x].insert(a[i].y);

	for (i=1;i<=n;i++){
		if (a[i].c=='D'){
			if (!S[a[i].x].size())	continue;
			j=*S[a[i].x].begin();
			if (j>a[i].y)	continue;
			j=*(--S[a[i].x].upper_bound(a[i].y));
			ans=Min(ans,a[i].y-j);
		}
	}
	for (i=0;i<4*N;i++)	S[i].clear();
	//U--D

	if (ans==INF)	puts("SAFE");
	else cout<<ans*5<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define maxn 505050

using namespace std;
typedef long long ll;
struct edge{
    ll x,y,z;
}e[maxn*4];
ll ans,pre[maxn],fa[maxn],n,a[maxn],b[maxn],c[maxn],d,p,q;
int l[maxn],r[maxn];

int ffind(int x){
    if (pre[x]==x) return x;
    pre[x]=ffind(pre[x]); return pre[x];
}

void union_(int u,int v){
    pre[v]=u;
    b[u]=min(b[u],b[v]);
    c[u]=min(c[u],c[v]);
}

bool cmp(edge x,edge y){
    return x.z<y.z;
}

ll cost(int u,int v){
    return 1ll*abs(u-v)*d+a[u]+a[v];
}
ll f[maxn],g[maxn];

int main(){
    cin >> n >> d; int cnt=0;
    for (int i=1;i<=n;i++) cin >> a[i];
    for (int i=1;i<=n;i++) fa[i]=pre[i]=i,b[i]=a[i]-d*i,c[i]=a[i]+d*i;
    int tot=n;
    while (tot>1){
        int p=1; ll res=b[fa[1]];
        for (int i=2;i<=tot;i++){
            ll tmp=c[fa[i]];
            f[i]=tmp+res,g[i]=p;
            if (b[fa[i]]<res) res=b[fa[i]],p=i;
        }
        int q=n; res=c[fa[tot]];
        for (int i=tot-1;i;i--){
            ll tmp=b[fa[i]];
            if (i==1||tmp+res<f[i]) f[i]=tmp+res,g[i]=q;
            if (c[fa[i]]<res) res=c[fa[i]],q=i;
        }
        for (int i=1;i<=tot;i++){
            int u=ffind(fa[i]),v=ffind(fa[g[i]]);
            if (u!=v) union_(u,v),ans+=f[i];
        }
        tot=0;
        for (int i=1;i<=n;i++){
            fa[++tot]=ffind(i);
            while (i<n&&ffind(i+1)==fa[tot]) i++;
        }
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,i,l=0;
  cin>>n;
  vector<int> a(n+2);
  for(i=1;i<=n;i++) cin>>a.at(i);
  for(i=0;i<n+1;i++) l+=abs(a.at(i+1)-a.at(i));
  for(i=1;i<n+1;i++){
    if((a.at(i+1)-a.at(i))*(a.at(i)-a.at(i-1))>=0) cout<<l<<endl;
    else cout<<l-2*min(abs(a.at(i+1)-a.at(i)),abs(a.at(i)-a.at(i-1)))<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 400005
#define MOD 998244353
#define LL long long int
LL ans,res,C[MAXN],fac[MAXN],revfac[MAXN];
int n,m,k;
LL qpow(LL a,int b)
{
    LL tmp=1;
    while(b)
    {
        if(b&1)
        tmp=tmp*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return tmp%MOD;
}
void facrev()
{
    C[0]=1;
    C[1]=(n-1);
    fac[1]=1;
    fac[0]=1;
    revfac[0]=1;
    revfac[1]=1;
    for(int i=2;i<=n;i++)
    {
        fac[i]=fac[i-1]*i%MOD;
        C[i]=C[i-1]*(n-i)%MOD;
    }
    revfac[n]=qpow(fac[n],MOD-2);
    for(int i=n-1;i>=1;i--)
    {
        revfac[i]=revfac[i+1]*(i+1)%MOD;
        C[i]=C[i]*revfac[i]%MOD;
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    facrev();
    int p=n-k;
    res=m;
    for(int i=1;i<=p-1;i++)
    {
        res=res*(m-1)%MOD;
    }
    ans=res*C[p-1]%MOD;
    for(int j=p+1;j<=n;j++)
    {
        res=res*(m-1)%MOD;
        ans=(ans+res*C[j-1]%MOD)%MOD;
    }
    printf("%lld\n",ans);
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define ALL(vec) vec.begin(), vec.end()
#define rALL(vec) vec.rbegin(), vec.rend()
using namespace std;
using ll = long long;
// ------------------------------

int main() {
    int N;
    string s, t;
    cin >> N >> s >> t;
    int cnt = 0;
    rep(i, N) {
        string left = s.substr(N - i - 1, i + 1);
        string right = t.substr(0, i + 1);
        if (right == left) cnt = i + 1;
    }
    cout << 2*N - cnt << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ll h1,m1,h2,m2,k;
	cin>>h1>>m1>>h2>>m2>>k;
	ll t=((h2-h1)*60)-m1+m2;
	ll d=t-k;
	if(d<=0){
		cout<<"0"<<"\n";
	}
	else{
		cout<<d<<"\n";
	}
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

// Acknowledgement: Special thanks to kyomukyomupurin, who developed this
// template.
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  int n = 0;
  for (auto e : vec) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
  int n = 0;
  for (auto e : st) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
  int n = 0;
  for (auto e : mp) os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}

#ifdef LOCAL
#define debug(...) \
  std::cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0) std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

using namespace std;
using int64 = long long;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> a(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    cin >> a[i];
  }
  int n = 0;
  string ans = "";
  vector<int> od_row_idx;
  for (int i = 0; i < H; i++) {
    vector<int> od_idx;
    for (int j = 0; j < W; j++) {
      if (a[i][j] % 2 == 1) {
        od_idx.push_back(j);
      }
    }
    debug(od_idx);
    if ((int)od_idx.size() == 0) continue;
    for (int j = 0; j < (int)od_idx.size() - 1; j += 2) {
      for (int k = od_idx[j]; k < od_idx[j + 1]; k++) {
        ans += to_string(i + 1) + ' ' + to_string(k + 1) + ' ' +
               to_string(i + 1) + ' ' + to_string(k + 2) + '\n';
        n++;
      }
    }
    if ((int)od_idx.size() % 2 == 1) {
      for (int k = od_idx.back(); k < W - 1; k++) {
        ans += to_string(i + 1) + ' ' + to_string(k + 1) + ' ' +
               to_string(i + 1) + ' ' + to_string(k + 2) + '\n';
        n++;
      }
      od_row_idx.push_back(i);
    }
  }
  if ((int)od_row_idx.size() > 1) {
    for (int j = 0; j < (int)od_row_idx.size() - 1; j += 2) {
      for (int k = od_row_idx[j]; k < od_row_idx[j + 1]; k++) {
        ans += to_string(k + 1) + ' ' + to_string(W) + ' ' + to_string(k + 2) +
               ' ' + to_string(W) + '\n';
        n++;
      }
    }
  }
  cout << n << endl;
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"

using namespace std;
using ll = long long;

const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;

void solve()
{
  int h, w;
  cin >> h >> w;
  vector< vector<int> > a( h, vector<int>(w) );
  for(int i = 0; i < h; ++i)
  {
    for(int j = 0; j < w; ++j)
    {
      cin >> a[i][j];
    }
  }

  vector<tuple<int, int, int, int>> ans;
  for(int i = 0; i < h; ++i)
  {
    for(int j = 0; j < w; ++j)
    {
      if(a[i][j] & 1)
      {
        if (j < w - 1)
        {
          a[i][j + 1]++;
          ans.emplace_back(make_tuple(i, j, i, j + 1));
        }
        else if (i < h - 1)
        {
          a[i + 1][j]++;
          ans.emplace_back(make_tuple(i, j, i + 1, j));
        }
      }
    }
  }

  cout << ans.size() << endl;
  for(const auto& t : ans)
  {
    int x, y, ax, ay;
    tie(x, y, ax, ay) = t;
    cout << x + 1 << " " << y + 1 << " " << ax + 1 << " " << ay + 1 << endl;
  }
}

int main()
{
  fastio;
  solve();

  return 0;
}


#include<cstdio>
#include<cstring>
#include<stack>
struct p{
	int x;
	int y;
};
using namespace std;
int main(){
	int w,h,sx,sy,gx,gy,n,c,d,x,y,i,j,k,o;
	int map[100][100];
	int used[100][100];
	int mx[4]={-1,0,1,0};
	int my[4]={0,-1,0,1};
	p v,u;
	stack<p> s;
	while(1){
		scanf("%d %d",&w,&h);
		if(w==0&&h==0){
			break;
		}
		memset(map,0,sizeof(map));
		memset(used,0,sizeof(used));
		scanf("%d %d",&sx,&sy);
		sx--;
		sy--;
		scanf("%d %d",&gx,&gy);
		gx--;
		gy--;
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			x--;
			y--;
			if(!d){
				for(j=y;j<y+2;j++){
					for(k=x;k<x+4;k++){
						map[j][k]=c;
					}
				}
			}
			else{
				for(j=y;j<y+4;j++){
					for(k=x;k<x+2;k++){
						map[j][k]=c;
					}
				}
			}
		}
		if(map[sy][sx]!=map[gy][gx]){
			printf("NG\n");
			continue;
		}
		v.x=sx;
		v.y=sy;
		s.push(v);
		o=0;
		while(!s.empty()){
			v=s.top();
			s.pop();
			if(used[v.y][v.x]){
				continue;
			}
			else{
				used[v.y][v.x]=1;
			}
			if(v.x==gx&&v.y==gy){
				o=1;
				while(!s.empty()){
					s.pop();
				}
				break;
			}
			for(i=0;i<4;i++){
				u=v;
				u.x+=mx[i];
				u.y+=my[i];
				if(u.x>=0&&u.x<w&&u.y>=0&&u.y<h&&map[u.y][u.x]==map[sy][sx]){
					s.push(u);
				}
			}
		}
		if(o){
			printf("OK\n");
		}
		else{
			printf("NG\n");
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

//const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;

#define REP(i,n) for (int i = 0; i < (n); i++)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll n,a,b,c,d;
ll dp[1010][1010];
ll K[1010];
ll cc[1010][1010];
ll inv[1010];

ll mul(ll _a, ll _b) {
  if (_b == 0) return 1LL;
  if (_b == 1) return _a;
  ll ans = mul(_a, _b/2);
  ans *= ans;
  ans %= MOD;
  if (_b&1) ans *= _a;
  return ans % MOD;
}

ll C(ll _n, ll _k) {
  if (cc[_n][_k] != 0LL) return cc[_n][_k];
  if (_n == _k) return cc[_n][_k] = 1;
  return cc[_n][_k] = (K[_n] * ((inv[_k] * inv[_n-_k]) % MOD)) % MOD;
}

ll get(ll groups, ll mem, ll from) {
  ll ans = 1;

  REP(i,groups) {
    ans *= C(from, mem);
    ans %= MOD;
    from -= mem;
  }

  return (ans * inv[groups]) % MOD;
}

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

  cin >> n >> a >> b >> c >> d;
  dp[0][0] = 1;

  K[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i <= n; i++) {
    K[i] = K[i-1]*i;
    K[i] %= MOD;
    inv[i] = mul(K[i], MOD-2);
  }

  for (ll i = 1; i <= b; i++) {
    if (i < a) {
      dp[i][0] += dp[i-1][0];
      dp[i][0] %= MOD;
      continue;
    }
    REP(j,n+1) {
      dp[i][j] += dp[i-1][j];
      dp[i][j] %= MOD;
      ll k = c;
      while (k <= d && j + k * i <= n) {
        dp[i][j+k*i] += dp[i-1][j] * get(k, i, n-j);
        dp[i][j+k*i] %= MOD;
        k++;
      }
    }
  }
  cout << dp[b][n] << endl;
  return 0;
}

#include <bits/stdc++.h>
#include <string>
#include <cmath>
#include <sstream>
//#include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for(int i = (a-1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
typedef long long int ll;
using namespace std;
ll mod=1000000007;
//ll mod=67280421310721;
//ll mod=998244353;
ll INF=1e18;
ll po(ll a,ll b)
{
	ll re=1;
	while(b)
	{
		if(b%2==1)
		{
			re=(re*a);
			re%=mod;
		}
		a=(a*a);
		a%=mod;
		b>>=1;
	}
	return re;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int test=1;
//	cin>>test;
	while(test--)
	{
		int n,m;
		cin>>n>>m;
		ll x[n],y[m],l=0,b=0;
		cin>>x[0];
		FOR(i,1,n)
		{
			cin>>x[i];
			l=(l+(i*(((n-i)*(x[i]-x[i-1]))%mod)))%mod;
		}
		cin>>y[0];
		FOR(i,1,m)
		{
			cin>>y[i];
			b=(b+(i*(((m-i)*(y[i]-y[i-1]))%mod)))%mod;
		}
		cout<<(l*b)%mod;
	}
	return 0;
}


#include <bitset>
#include <tuple>
#include <string>
#include <array>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cmath>
#ifdef LOCAL
  #include <iostream>
#endif
using namespace std;

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

typedef long long ll;

#ifdef LOCAL
  #define debug_echo(e) cout << "L" << __LINE__ << ": " << e << "\n"
  #define debug_repr(e) cout << "L" << __LINE__ << ": " << #e << " -> " << e << "\n"
#else
  #define debug_echo(e)
  #define debug_repr(e)
#endif

#define defread(T, fmt) \
  inline T read_ ## T() { \
    T result; \
    scanf(fmt, &result); \
    return result; \
  }
defread(int, "%d")
defread(ll, "%lld")
defread(float, "%f")
defread(double, "%lf")

int gcd(int m, int n) {
  if ((0 == m) || (0 == n)) return 0;
  while (m != n) {
    if (m > n) m -= n;
    else       n -= m;
  }
  return m;
}
int lcm(int m, int n) {
  if ((0 == m) || (0 == n)) return 0;
  return (m / gcd(m, n)) * n;
}

int main() {
  vector<int> a(4);
  vector<int> b(2);
  a[0] = read_int();
  a[1] = read_int();
  a[2] = read_int();
  a[3] = read_int();
  b[0] = read_int();
  b[1] = read_int();

  sort(all(a));
  sort(all(b));

  printf("%d\n", a[1] + a[2] + a[3] + b[1]);
}

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

int main(){
	int s=0;
int a=0,b=INT_MAX;
int c;
for(int d=0;d<4;d++){
	scanf("%d",&c);
	a+=c;
	b=min(b,c);
}
a-=b;
b=INT_MAX;
for(int d=0;d<2;d++){
	scanf("%d",&c);
	a+=c;
	b=min(b,c);
}
printf("%d\n",a-b);
}


#include<stdio.h>

int main()
{
	int n, A[10001];
	scanf("%d", &n);
	int i;
	for (i = 0; i <= n - 1; i++)
	{
		scanf("%d ", &A[i]);
	}
	long int min = 1000001, max = -1000001, total = 0;
	for (i = 0; i <= n - 1; i++)
	{
		if (min > A[i]) min = A[i];
		if (max < A[i]) max = A[i];
		total = total + A[i];
	}
	printf("%ld %ld %ld\n", min, max, total);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// --------------------------------------------------------
#define FOR(i,l,r) for (int i = (l); i < (r); ++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define SZ(c) ((int)(c).size())
#define debug(x) cerr << #x << " = " << (x) << '\n';
using P = pair<int,int>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const int INF = 1 << 30;
// const ll INF = 1LL << 62;
// --------------------------------------------------------

int binary_search_A(vector<int> &a, int key) {
    // Meguru type binary search
    int ok = -1;
    int ng = a.size();

    auto is_ok = [&] (int idx) {
        if (a[idx] < key) {
            return true;
        } else {
            return false;
        }
    };

    int mid;
    while (abs(ok - ng) > 1) {
        mid = (ok + ng) / 2;
        if (is_ok(mid)) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    return ok;
}

int binary_search_C(vector<int> &a, int key) {
    // Meguru type binary search
    int ng = -1;
    int ok = a.size();

    auto is_ok = [&] (int idx) {
        if (key < a[idx]) {
            return true;
        } else {
            return false;
        }
    };

    int mid;
    while (abs(ok - ng) > 1) {
        mid = (ok + ng) / 2;
        if (is_ok(mid)) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    return ok;
}

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

    int N; cin >> N;
    VI A(N); REP(i,N) cin >> A[i];
    VI B(N); REP(i,N) cin >> B[i];
    VI C(N); REP(i,N) cin >> C[i];

    SORT(A);
    SORT(B);
    SORT(C);

    ll ans = 0;
    for (auto b : B) {
        int ia = binary_search_A(A, b);
        int ic = binary_search_C(C, b);
        ans += (ll)(ia + 1) * (N - ic);
    }
    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=998244353,MAX=103,INF=1<<30;

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

int N;

bool DFS(vector<ll> A){

    vector<ll> left(N+2),right(N+2);
    ll sum=0;
    for(int i=0;i<N;i++){
        sum+=A[i];
    }

    if((sum-N)%2){
        return 1;
    }

    for(int i=1;i<=N;i++){
        left[i]=gcd(left[i-1],A[i-1]);
    }

    for(int i=N;i>=1;i--){
        right[i]=gcd(right[i+1],A[i-1]);
    }

    bool res=false;

    for(int i=1;i<=N;i++){
        if(A[i-1]==1) continue;
        ll g=gcd(gcd(left[i-1],A[i-1]-1),right[i+1]);
        if(g==1) continue;

        if(((sum-1)/g-N)%2==0){
            vector<ll> to(N);
            to=A;
            for(int j=0;j<N;j++){
                if(j==i-1) to[j]--;
                to[j]/=g;
            }
            res|=!DFS(to);
        }
    }

    return res;
}

int main(){

    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin>>N;
    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }

    if(DFS(A)) cout<<"First"<<endl;
    else cout<<"Second"<<endl;

}

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

const int N = 1000 * 100 + 10;
int n, a[N], tt = 0;
string ans[2] = {"First\n", "Second\n"};

void solve() {
	long long sum = 0;
	bool one = false;
	int cnt[2] = {0, 0};
	for (int i = 0; i < n; i++) {
		 one |= a[i] == 1;
		sum += a[i] - 1;
		cnt[a[i] & 1]++;
	}
	if (one) {
		cout << ans[(sum + 1 + tt) & 1];
		exit(0);
	}
	if (n == 1) {
		cout << ans[(sum + 1 + tt) & 1];
		exit(0);
	}
	if (cnt[0] & 1) {
		cout << ans[tt & 1];
		exit(0);
	}
	if (cnt[1] >= 2) {
		cout << ans[(1 + tt) & 1];
		exit(0);
	}
	for (int i = 0; i < n; i++)
		if (a[i] & 1) {
			a[i]--;
			break;
		}
	long long g = a[0];
	for (int i = 1; i < n; i++)
		g = __gcd(1LL * a[i], g);
	for (int i = 0; i < n; i++)
		a[i] /= g;
	tt++;
	solve();
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	solve();
	return 0;
}


#include <bits/stdc++.h>
#include <cmath>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string> > PQ_ASK;

#include <iostream>
#include <vector>

using namespace std;

const int mod = 1000000007;

class UnionFind {
public:
    // 親の番号を格納する。親だった場合-size
    vector<int> parents;

    UnionFind(int n) {
        parents = vector<int>(n, -1);
    }

    // aがどのグループに属しているか
    int root(int a) {
        if (parents[a] < 0) {
            return a;
        }
        return parents[a] = root(parents[a]);
    }

    int size(int a) {
        return -parents[root(a)];
    }

    // aとbをくっつける
    bool connect(int a, int b) {
        int ra = root(a);
        int rb = root(b);
        if (ra == rb) {
            return false;
        }
        // 大きいほうにA
        if (size(ra) < size(rb)) {
            swap(ra, rb);
        }
        parents[ra] += parents[rb];
        parents[rb] = ra;
        return true;
    }

    bool is_union(int a, int b) {
        int ra = root(a);
        int rb = root(b);
        return ra == rb;
    }
};

int main() {
    int h, w;
    cin >> h >> w;

    vector<vector<char>> grid(h, vector<char>(w));
    rep(y, h) rep(x, w) cin >> grid[y][x];

    auto to_id = [&](int y, int x) {
        return y * w + x;
    };

    UnionFind uf(h * w);

    struct Direction {
        int x, y;
    };

    vector<Direction> directions = {
            {0,  1},
            {1,  0},
            {0,  -1},
            {-1, 0},
    };

    rep(y, h) {
        rep(x, w) {
            for (Direction d : directions) {
                int ny = y + d.y;
                int nx = x + d.x;
                if (ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
                if (grid[y][x] != grid[ny][nx]) uf.connect(to_id(y, x), to_id(ny, nx));
            }
        }
    }

    UnionFind uf2(h * w);

    rep(y, h) {
        rep(x, w) {
            if (grid[y][x] != '#') continue;
            for (Direction d : directions) {
                int ny = y + d.y;
                int nx = x + d.x;
                if (ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
                if (grid[ny][nx] != '.')continue;

                for (Direction e : directions) {
                    int nny = ny + e.y;
                    int nnx = nx + e.x;
                    if (nny < 0 || h <= nny || nnx < 0 || w <= nnx) continue;
                    if (grid[nny][nnx] != '#')continue;

                    uf2.connect(to_id(y, x), to_id(nny, nnx));
                }
            }
        }
    }

    ll ans = 0;
    rep(y, h) {
        rep(x, w) {
            if (grid[y][x] != '#') continue;
            ll now = uf.size(to_id(y, x)) - uf2.size(to_id(y, x));
//            cout << uf.size(to_id(y, x)) << ' ' << uf2.size(to_id(y, x)) << ' ' << now << endl;
            ans += now;

        }
    }
    cout << ans << endl;
}

#include <iostream>

using namespace std;

int main() {
	int n, m;
	int a[1010][2] = {0};
	int b;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i][0];
	}
	for (int i = 0; i < m; i++) {
		cin >> b;
		for (int j = 0; j < n; j++) {
			if (a[j][0] <= b) {
				a[j][1]++;
				break;
			}
		}
	}
	int max[2] = {0};
	for (int i = 0; i < n; i++) {
		if (max[1] < a[i][1]) {
			max[1] = a[i][1];
			max[0] = i+1;
		}
	}
	cout << max[0] << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
typedef pair<int,int> pii;
typedef pair <long long,long long> pll;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9+7;

ll dp[10001][101][2];
ll solve( string & k, int& D, int pos , int d , bool tight)
{	if(dp[pos][d][tight] != -1)
		return dp[pos][d][tight];

	int ub = (tight) ? (k[pos]-'0') : 9 ;
	if(pos == k.length() - 1){
		ll ans = 0;
		for(int x = 0; x <= ub ; x++)
			if(x%D == d)
				ans++;
		return ans;
	}
	else{
	ll ans = 0;
	for(int x = 0; x <= ub; x++)
		ans = (ans + solve( k , D, pos+1, (D + d - x%D)%D , tight&&(x == ub)))%mod;
	return dp[pos][d][tight] = ans;
	}
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
	int d;
	string k;
	cin >> k >> d;
	memset(dp, -1, sizeof(dp));
	cout << (mod+solve(k, d, 0, 0, 1)-1)%mod;
	return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#define mod 998244353
#define ll long long
#define N 1000005
#define inv 499122177

using namespace std;

int n, m;
ll fac[N], ifac[N];
ll ans;

void init();
ll quick_pow(ll ,int);
ll C(int ,int);

int main(){
	cin >> n >> m;init();
	if(n < m) swap(n, m);
	for(int i = 1; i <= m; ++i){
		int x = n - i, y = m - i;
		(ans += C(i, 2 * i) * C(x, x + y) % mod) %= mod;
	}
	(ans *= quick_pow(C(n, n + m), mod - 2) * inv % mod) %= mod;
	(ans += n) %= mod;
	cout << ans << endl;
}

void init(){
	fac[0] = 1;ifac[0] = 1;
	for(int i = 1; i <= 1e6; ++i)
		fac[i] = fac[i - 1] * i % mod, ifac[i] = quick_pow(fac[i], mod - 2);
	return ;
}

ll C(int x,int y){
	return fac[y] * ifac[y - x] % mod * ifac[x] % mod;
}

ll quick_pow(ll x,int y){
	ll sum = 1, a = x;
	while(y){
		if(y & 1) (sum *= a) %= mod;
		(a *= a) %= mod;
		y >>= 1;
	}
	return sum;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <tuple>
using namespace std;
using ll = long long;
#define rep(i, j, n) for(int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for(int i = (int)n - 1; j <= i; --i)

constexpr ll MOD = 0x3b9aca07;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

vector<int> par;

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

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

void merge(int x, int y) {
    if (size(x) < size(y)) swap(x, y);
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    par[rx] += par[ry];
    par[ry] = rx;
}

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

    ll n, m; cin >> n >> m;

    par.assign(n * 2, -1);

    rep(i, 0, m) {
        int a, b; cin >> a >> b;
        --a, --b;
		merge(a, b + n);
		merge(b, a + n);
    }

    if (root(0) != root(n)) {
        // 二部グラフの左側*右側
        ll cnt = 0;
        rep(i, 0, n)
            if (root(0) == root(i)) ++cnt;
        cout << cnt * (n - cnt) - m << endl;
    }
    else {
        cout << n * (n - 1) / 2 - m << endl;
    }

	return 0;
}

#include<bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mod 1000000007ll
const ll maxn = 9e18;
using namespace std;
const ll maxsize = 100000009;

vector<bool> mark(100005, 0);
int wa[100005];
void solve() {
	int n, k;
	cin >> n >> k;
	map<string,ll> m;
	m["AC"] = 0;
	int ac_cnt = 0, wa_cnt = 0;
	m["WA"] = 0;
	int t = 0;
	for(int i = 0; i < k; ++i) {
		int x;
		string s;
		cin >> x >> s;
		if(mark[x]) continue;
		if(s == "AC") {
			mark[x] = 1;
			ac_cnt++;
			wa_cnt += wa[x];
			}
			else wa[x]++;
		}
		cout << ac_cnt <<" "<< wa_cnt << endl;
		cout << endl;
}

int main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cout.precision(35);
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
#define dame { puts("-1"); return 0;}
#define yn {puts("YES");}else{puts("NO");}

int main() {
  int N;
  cin >> N;
  int H,W;
  cin >> H >> W;
  cout << (N-H+1) * (N-W+1) << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //GCC5.4.1で配列の範囲外アクセスをエラーにする

//cmdで g++ test.cpp とするとコンパイルできる
//制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい
// https://github.com/atcoder/live_library
// cppdbg: test.exeのterminalに標準入力を打てる

#define ALL(x) x.begin(),x.end()
#define get_tp(t,i) get<i>(t)
#define len(x) (ll)(x.size())
#define ub(a,x) (upper_bound(ALL(a),x)-a.begin())
#define lb(a,x) (lower_bound(ALL(a),x)-a.begin())
typedef long long ll; //long long
typedef pair<int,int> pii; //pair<int,int>
typedef pair<long long,long long> pllll; //pair<ll,ll>
typedef vector<int> vi; //vector<int>
typedef vector<vector<int>> vvi; //vector<vector<int>>
typedef vector<ll> vll; //vector<long long>
typedef vector<vector<ll>> vvll; //vector<vector<long long>>
typedef priority_queue<ll, vector<ll>, greater<ll> > gpq; //逆順priority_queue
typedef priority_queue<ll> pq;

//const int MOD=998244353;
const int MOD=1000000007; //10^9+7
const long double PI=3.1415926535897932;

int main(){
    string S;cin>>S;
    S[3]='8';
    cout<<S<<endl;
}


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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
#define se cout << "test ok" << endl;
#define sp << " " <<
using namespace std;
using ll = long long;
using ld = long double;
ll mod = ll(1e9)+7;
ll inf = ll(5e18);
template<typename T1, typename T2> ll ceill(T1 a, T2 b){ return (a+b-1)/b; }
void ans(bool b, ostream& out=cout){ out << (b ? "Yes" : "No") << endl; }
void ans2(bool b, ostream& out=cout){ out << (b ? "YES" : "NO") << endl; }
template<typename T> inline bool chmax(T &a, T b){ if(a<b){a=b; return true;} return false; }
template<typename T> inline bool chmin(T &a, T b){ if(a>b){a=b; return true;} return false; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ fill((T*)array, (T*)(array+N), val); }
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

#ifndef INCLUDE_DEBUG_HPP
#define INCLUDE_DEBUG_HPP

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

namespace dbg {
    using ll = long long;

    class Debug {
    public:
        Debug(){}
        template<typename T>
        void vd(vector<T> vec);
        template<typename T>
        void vd2(vector<vector<T>> vec);
        template<typename T>
        void ad(T *arr, ll n);
        template<typename T, size_t N>
        void ad2(T (&arr)[N], ll n);
        template<typename T>
        void md(T mp);
    };

    template<typename T>
    void Debug::vd(vector<T> vec) {
        for (auto vv : vec) cout << vv << " ";
        cout << endl;
    }

    template<typename T>
    void Debug::vd2(vector<vector<T>> vec) {
        for (auto vv : vec) for (auto vvv : vv) cout << vvv << " ";
        cout << endl;
    }

    template<typename T>
    void Debug::ad(T *arr, ll n) {
        for (int i = 0; i <= n; i++) cout << arr[i] << " ";
        cout << endl;
    }

    template<typename T, size_t N>
    void Debug::ad2(T (&arr)[N], ll n) {
        for (int i = 0; i <= N; i++) for (int j = 0; j <= n; j++) cout << arr[i][j] << " ";
        cout << endl;
    }

    template<typename T>
    void Debug::md(T mp) {
        cout << "key : value" << endl;
        for (auto const &m : mp) cout << m.first << " : " << m.second << endl;
        cout << endl;
    }

}

#endif //INCLUDE_DEBUG_HPP

using dbg::Debug;
Debug debug;

class DGridComponents {
public:
	void solve(std::istream& in, std::ostream& out) {
	    int a, b;
	    in >> a >> b;

	    vector<vector<char>> g(100, vector<char>(100, '#'));
	    for(int i=0; i<100; i++){
	        if(i >= 50) break;
	        for(int j=0; j<100; j++){
                g[i][j] = '.';
	        }
	    }

	    int cnt = 0;
	    for(int i=0; i<50; i++){
	        if(i%2 == 1) continue;
	        for(int j=0; j<100; j++){
                if(cnt == b-1) break;
                if(i%2 == j%2) {
                    g[i][j] = '#';
                    cnt++;
                }
	        }
	    }
	    cnt = 0;
	    for(int i=99; i>=50; i--){
            if(i%2 == 0) continue;
	        for(int j=0; j<100; j++){
	            if(cnt == a-1) break;
	            if(i%2 == j%2){
	                g[i][j] = '.';
	                cnt++;
	            }
	        }
	    }

	    out << 100 sp 100 << endl;
	    for(int i=0; i<100; i++){
	        for(int j=0; j<100; j++){
	            out << g[i][j];
	        }
	        out << endl;
	    }
	}
};

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define lowbit(i) i&-i
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
	ull ans=0; For(i,0,5)ans=ans<<15^rand(); return ans;
}
ll ycl[20],f[10],ans,d,L;
ll xqz(ll x,ll y){
	if(x>=0)return x/y;
	x=-x;
	x--; return -x/y-1;
}
void dfs(int p,ll xs,ll d){
	if(p==L/2){
		if(d==0)ans+=xs*((L&1)?10:1); return;
	}
	ll cz=ycl[L-1-p]-ycl[p],t=xqz(d,cz);
	//cout<<d<<" "<<cz<<" "<<t<<" "<<L/2<<endl;
	if(abs(t)<=9){
		dfs(p+1,xs*(f[abs(t)]-(p==0&&t>=0)),d-t*cz);
	}
	t++;
	if(abs(t)<=9){
		dfs(p+1,xs*(f[abs(t)]-(p==0&&t>=0)),d-t*cz);
	}
}
void get(int len,ll d){
	L=len;
	dfs(0,1,d);
}
int main(){
	For(i,0,9)For(j,0,i)f[i-j]++;
	d=read();
	int ws=1;
	For(i,ycl[0]=1,18){ycl[i]=ycl[i-1]*10; if(d>=ycl[i])ws++;}
	//get(2,d); cerr<<ans<<endl; return 0;
	For(i,max(2,ws),2*ws){
		get(i,d);
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 10000
#define MAXD 100

ll dp[MAXN+1][MAXD][2];

int main(){
  string s;cin>>s;
  int n = s.size();
  int d;cin>>d;

  dp[0][0][1]=1;
  rep(i, n)rep(j, d)rep(k, 10){
    int cd = s[i]-'0';
    int nj = (j+k)%d;
    if(k>cd){
      dp[i+1][nj][0] += dp[i][j][0];
    }else if(k==cd){
      dp[i+1][nj][1] += dp[i][j][1];
      dp[i+1][nj][0] += dp[i][j][0];
    }else{
      dp[i+1][nj][0] += dp[i][j][1];
      dp[i+1][nj][0] += dp[i][j][0];
    }
    dp[i+1][nj][0] %= MOD;
    dp[i+1][nj][0] %= MOD;
  }

  cout<<(dp[n][0][0]+dp[n][0][1]-1+MOD)%MOD<<endl;

  return 0;
}

#include <bits/stdc++.h>
#define mod 998244353
#define int long long
using namespace std;
const int N=3*1e6+100;
int n,m,fac[N],inv[N],a,b;
inline int m_pow(int a,int b)
{
	int ans=1;
	while (b)
	{
		if (b&1) ans=(ans*a)%mod;
		b>>=1;
		a=(a*a)%mod;
	}
	return ans;
}
inline int c(int n,int m)
{
	if (m>n) return 0;
	return (fac[n]*(inv[m]*inv[n-m]%mod)%mod)%mod;
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	fac[0]=1;
	for (int i=1;i<=n+3*m;i++) fac[i]=(fac[i-1]*i)%mod;
	inv[n+3*m]=m_pow(fac[n+3*m],mod-2);
	for (int i=n+3*m-1;i>=0;i--) inv[i]=(inv[i+1]*(i+1))%mod;
	for (int i=0;i<=m;i++)
	{
		if ((3*m-i)%2!=0) continue;
		a=(a+(c(n,i)*c(n+(3*m-i)/2-1,n-1))%mod)%mod;
	}
	for (int i=2*m+1;i<=3*m;i++) b=(b+c(n+3*m-i-2,n-2))%mod;
	b=(b*n)%mod;
	printf("%lld\n",(a-b+mod)%mod);
}


#include<iostream>

using namespace std;

int main(){
	int a[10][10];
	int d, i, j, k, temp,sum=0;

	while (1){
		cin >> d;
		if (d == 0){
			return 0;
		}

		for (i = 0, j = 0; i < d; i++){
			for (j = 0; j < d; j++){
				cin >> a[i][j];
			}
		}

		for (i = 0, j = 0; i < d; i++){
			for (j = 0; j < d; j++){
				cout.width(5);
				cout << a[i][j];
			}
			temp = 0;
			for (k = 0; k < d; k++){
				temp += a[i][k];
			}
			cout.width(5);
			cout << temp << endl;
			sum += temp;
		}

		for (j = 0; j < d; j++){
			temp = 0;
			for (k = 0; k < d; k++){
				temp += a[k][j];
			}
			cout.width(5);
			cout << temp;
			sum += temp;

		}

		cout.width(5);
		cout << sum/2 << endl;
		sum = 0;
	}
}

#include <bits/stdc++.h>

#define loop(v, f, l) for(int v = (f), v##_ = (l); v < v##_; ++v)

using namespace std;

typedef long long int lint;
static inline int in(){ int x; scanf("%d", &x); return (x); }
static inline lint inl(){ lint x; scanf("%lld", &x); return (x); }

int matrix[11][11];

int main()
{
	int n;

	while( (n = in()) != 0 )
	{
		loop(i, 0, 11)
		{
			loop(j, 0, 11)
			{
				matrix[i][j] = 0;
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][j] = in();
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][n] += matrix[i][j];
				matrix[n][i] += matrix[j][i];
			}
			matrix[n][n] += matrix[i][n];
		}

		loop(i, 0, n + 1)
		{
			loop(j, 0, n + 1)
			{
				printf("%5d", matrix[i][j]);
			}
			printf("\n");
		}

	}

	return (0);
}


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

int main(){
    int n; cin>>n;
    int x[n],y[n];
    for(int i=0;i<n;i++){
        cin>>x[i]>>y[i];
    }
    int p[10]={0,1,2,3,4,5,6,7,8,9};

    double sum=0;
    int divide=0;

    do{
        for(int i=0;i<n-1;i++) sum+=sqrt(pow(x[p[i+1]]-x[p[i]],2)+pow(y[p[i+1]]-y[p[i]],2));
        divide++;
    }while(next_permutation(p,p+n));

    cout<<fixed<<setprecision(7)<<sum/divide<<endl;
}

#include"bits/stdc++.h"
using namespace std;
#define ll long long
#define mod 1000000007
#define prec(n) fixed<<setprecision(n)
int N;
int x[10], y[10];

double dist(int i, int j) {
    double dx = x[i] - x[j];
    double dy = y[i] - y[j];
    return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
    cin >> N;
    for(int i = 1; i <= N; i++) cin >> x[i] >> y[i];

    double dsum = 0.0;

	for (int i = 1 ;i<=N; ++i) {
		for (int j = i+1; j<=N;++j) {
			dsum += dist(i, j) * 2 / N;
		}
	}

	cout << prec(10) << dsum << "\n";

    return;
}

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


# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	while (cin >> n&&n) {
		long long member[4001] = {};
		bool flag[4001] = {};
		for (int i = 1; i <= 4000; i++) {
			member[i] = 0;
			flag[i] = false;
		}
		long long a, b, c;
		int count = 0;
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> c;
			member[a] += b*c;
			if (member[a] >= 1000000&&!flag[a]) {
				count++;
				flag[a] = true;
				cout << a << endl;
			}
		}
		if (count == 0)cout << "NA" << endl;
	}
}

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

int main()
{
	int a, b, c;
	while (cin >> a >> b >> c, a | b | c) {
		int N;
		cin >> N;
		vector<int> res(a + b + c, 2);
		vector<vector<int>> no;
		for (int i = 0; i < N; i++) {
			int x, y, z, t;
			cin >> x >> y >> z >> t; x--, y--, z--;
			if (t) {
				res[x] = res[y] = res[z] = 1;
			}
			else {
				no.push_back(vector<int>({ x, y, z }));
			}
		}
		for (auto t : no) {
			int x = t[0], y = t[1], z = t[2];
			if (res[x] == 1 && res[y] == 1) {
				res[z] = 0;
			}
			else if (res[y] == 1 && res[z] == 1) {
				res[x] = 0;
			}
			else if (res[z] == 1 && res[x] == 1) {
				res[y] = 0;
			}
		}
		for (int i = 0; i < a + b + c; i++) {
			cout << res[i] << endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#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--)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい
#define SIZE(x) ll(x.size()) //sizeをsize_tからllに直しておく
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//略記
#define PB emplace_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define Umap unordered_map
#define Uset unordered_set

int main(){
    ll a, b;
    cin >> a >> b;
    cout << max(a+b, max(a, b)*2-1);
    return 0;
}

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
using namespace std;
int main(){
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int a[5];
  int ans;
  for(int i=0;i<2;i++){
    cin>>a[i];
  }
  sort(a,a+2);
  ans=a[1];
  a[1]--;
  sort(a,a+2);
  ans+=a[1];
  cout<<ans<<"\n";
  return 0;
}