#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

int N;
vector<int> A, sum;

signed main() {
  cin >> N;

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

  sum.resize(N+1, 0);
  REP(i, N) sum[i+1] += sum[i]+A[i];

  auto f = [&](int l, int m, int r){
    return abs((sum[m]-sum[l])-(sum[r+1]-sum[m]));
  };

  int ans = INF;
  int l = 1, r = 2;
  FOR(i, 1, N-1){
    while(l+1 <= i && f(0, l, i) > f(0, l+1, i)) l++;
    while(r+1 <= N && f(i+1, r, N-1) > f(i+1, r+1, N-1)) r++;

    int P = sum[l];
    int Q = sum[i+1]-sum[l];
    int R = sum[r]-sum[i+1];
    int S = sum[N]-sum[r];

    int MAX = max(max(P, Q), max(R, S));
    int MIN = min(min(P, Q), min(R, S));

    chmin(ans, MAX-MIN);
  }

  cout << ans << "\n";

  return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

Int n, d;
Int a[216000];
Int sums[216000];
Int difs[216000];
Int res;
int main(){
  cin >> n >> d;
  for(int i = 0;i < n;i++){
    cin >> a[i];
    sums[i] = a[i] + i * d;
    difs[i] = a[i] - i * d;
    res += 2 * a[i];
  }
  res += (n-1)*d;
  res -= a[0] + a[n-1];
  for(int i = 1;i < n;i++)difs[i] = min(difs[i], difs[i-1]);
  for(int i = n-2;i >= 0;i--)sums[i] = min(sums[i], sums[i+1]);
  for(int i = 1;i < n-1;i++){
    //    cout << difs[i] << " " << sums[i] << endl;
    Int ltmp = -a[i] + i*d + difs[i];
    Int rtmp = -a[i] - i*d + sums[i];
    //    cout << ltmp << " " << rtmp << endl;
    res += min(ltmp, rtmp);
  }

  cout << res << endl;
  return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define pb push_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
// #define int ll
using namespace std;
const ll INF = (1 << 30 ) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] ={1,0,-1,0} , dy[4] ={0,1,0,-1};

struct Timer{
    chrono::system_clock::time_point start, end;
    Timer(){ start = chrono::system_clock::now(); }
    ~Timer(){
        end = chrono::system_clock::now();
        auto msec = chrono::duration_cast<chrono::milliseconds>(end - start).count();
        cerr<<"time : "<<msec<<" ms"<<endl;
    }
};

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Timer TM;

    ll n;
    ll ans=0;
    string s;
    // unordered_map<string,ll> mp;
    map<pair<string,string>,ll> mp;
    cin>>n>>s;
    rep(bit,(1<<n)){
        string u="",t="";
        rep(i,n){
            if(bit&(1<<i)){
                u+=s[i];
            }
            else t+=s[i];
        }
        ++mp[make_pair(u,t)];
        // cout<<u+'0'+t<<endl;
    }
    rep(bit,(1<<n)){
        string u="",t="";
        rep(i,n){
            if(bit&(1<<i)){
                u=s[i+n]+u;
            }
            else t=s[i+n]+t;
        }
        ans+=mp[make_pair(t,u)];
    }
    cout<<ans<<endl;

    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long

using namespace std;

long long MOD = 1000000007;

void all_enum(string R, string B, string &S, map<string,int> &m){
    int i=(int)R.size()+B.size();
    if(i==(int)S.size()){
        string key_=R+","+B;
        //cout << key_ << endl;
        if(m.count(key_)){
            m[key_]++;
        }else{
            m[key_]=1;
        }
        return;
    }

    all_enum(R+S[i],B,S,m);
    all_enum(R,B+S[i],S,m);
}

ll count_pattern(string R, string B, string &T, map<string,int> &m){
    int i=(int)R.size()+B.size();
    if(i==(int)T.size()){
        string key_=R+","+B;
        //cout << key_ << endl;
        if(m.count(key_)){
            return m[key_];
        }else{
            return 0;
        }
    }

    ll cnt=0;
    cnt+=count_pattern(R+T[i],B,T,m);
    cnt+=count_pattern(R,B+T[i],T,m);
    return cnt;
}

int main(){
    int N;
    cin >> N;
    string S;
    cin >> S;
    string T=S.substr(N,N);
    reverse(T.begin(),T.end());
    S=S.substr(0,N);

    map<string,int> m;

    all_enum("","",S,m);
    //cout << "_______" << endl;
    cout << count_pattern("","",T,m) << endl;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int N; cin>>N;
  vector<bool>A(55556,true);
  for(int i=2;i<55556;i++){
  if(!A.at(i))
    continue;
    else{
    for(int j=2;i*j<=55555;j++)
      A.at(i*j)=false;
    }
   }int a=0;
  vector<int>B;
  for(int i=2;i<55556;i++){
  if(A.at(i)){if(i%5==1){B.push_back(i);a++;}}
  if(a==N)break;
  }for(int i=0;i<N-1;i++)
    cout<<B.at(i)<<" ";
  cout<<B.at(N-1)<<endl;
  return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define AfterDecimal(n) cout << fixed << setprecision(n);
#define ll long long int
#define ld long double
#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bits(x) __builtin_popcount(x)

using namespace __gnu_pbds;
using namespace std;

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

//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;

#ifdef LOCAL
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << "# "<< name << " = " << arg1 << '\n';
}
template<typename Arg1, typename... Args>
void __f(const char* names, Arg1 && arg1, Args &&... args){
        const char* comma = strchr(names + 1 ,',');
        cout << "# ";
        cout.write(names, comma - names)<< " = " << arg1 << " | ";
        __f(comma + 1, args...);
}
#else
#define debug(...) 36
#endif

std::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int a, int b) {
    return uniform_int_distribution<int>(a, b)(rng);
}

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

const ll INF = (ll)(8e18);
const ll MOD = (ll)(1e9 + 7);
const ll HMOD = (ll)4222234727;
const ll Hashprime = (ll)31;

const int maxn = 100100;

int32_t main(){
    IOS;
    #ifndef ONLINE_JUDGE
    //  freopen("input.txt", "r", stdin);
    //  freopen("output.txt", "w", stdout);
    #endif
    int TestCases = 1; // cin >> TestCases;
    while(TestCases--){
       int n,r; cin >> n >> r;
       if(n >= 10){
          cout << r << '\n';
       }else{
          cout << r + (100 * (10 - n)) << '\n';
       }
    }
    #ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}
//g++ -DLOCAL -std=c++17 -Wshadow -Wall -o "c" "comp.cpp" -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -g

#include<bits/stdc++.h>
using namespace std;
int main(){
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    for(int i = 0; i < N; i++){
        cin >> A.at(i);
    }
    sort(A.begin(), A.end());
    int ans = 0;
    for(int i = 0; i < K; i++){
        ans += A.at(N - 1 - i);
    }
    cout << ans << endl;
    return 0;
}


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

typedef long long ll;
#define pii pair<int , int>
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MAXX 1005

int n , m , X , Y;
int x[MAXX];
int y[MAXX];

int main()
{
    _FastIO;
    int a , b;
    cin >> n >> m >> X >> Y;
    for(int i = 0; i < n; i++){
        cin >> x[i];
        if(i) a = max(a , x[i]);
        else    a = x[0];
    }
    for(int i = 0; i < m; i++){
        cin >> y[i];
        if(i)   b = min(b , y[i]);
        else    b = y[0];
    }
    int tp = max(X + 1 , a + 1);
    int yp = min(Y , b);
    if(tp > yp){
        cout << "War" << endl;
        return 0;
    }
    cout << "No War" << endl;
    return 0;
}

#include <cctype>
#include <string>
#include <iostream>

using namespace std;

int main()
{
	string S; int C = 0;

	while (getline(cin, S))
	{
		for (int i = 0; i < 26; i++)
		{
			if (!(S.find("this") == string::npos && S.find("that") == string::npos && S.find("the") == string::npos))
			{
				cout << S << endl;

				goto Exit;
			}

			for (string::size_type j = 0; j < S.size(); j++)
			{
				if (isalpha(static_cast<int>(S[j])))
				{
					if (S[j] == 'z')
					{
						S[j] = 'a';
					}
					else
					{
						S[j]++;
					}
				}
			}
		}

	Exit:;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,N) for(int i = 0; i < (N); i++)
#define erep(i,N) for(int i = N - 1; i >= 0; i--)
const ll INF = 1000000000;
//const ll MOD = 1000000007;
const ld PI = (acos(-1));
using Graph = vector<vector<int>>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true;} return false;}
typedef pair<int, int> P;
typedef pair<ll,ll> PLL;
double rad(double a) {return a * 180 / PI;}

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

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

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

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

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

//dpTable
int dp[100050];

int main(){
    ll N;
    cin >> N;
    int sqrtN = sqrt(N);
    int A,B;
    for (int i = sqrtN; i >= 1; i--) {
        if (N % i == 0) {
            A = i;
            B = N / i;
            break;
        }
    }
    int cntA = 0,cntB = 0;
    while (A != 0) {
        A /= 10;
        cntA++;
    }
    while (B != 0) {
        B /= 10;
        cntB++;
    }
    cout << max(cntA,cntB) << endl;
    return 0;
}

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

int main() {
	long long N;
	cin >> N;
	long long total = 0;
	for (long long i = 1; i * i <= N; i++) {
		if (N % i == 0) {
			total = i;
		}
	}
	long long result = N / total;
	long long count = 0;
	while (result != 0) {
		result = result / 10;
		count++;
	}
	cout << count << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define very_slow       ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vec             vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define inf             1e18
#define MAX             1e9
#define MIN            -1e9
#define no_of_test(x)        int x; cin>>x; while(x--)
const double pi=3.14159265359;
//Actual Code Is At The Top
const int mod = 1e9 + 6;
int gcd(int a, int b)
{
	if (b == 0) return a;
	return gcd(b, a % b);
}
int binary_exponentation(int a, int b)
{
	int ret = 1;
	while (b)
	{
		if (b & 1) ret = (ret * a) % mod;
		b >>= 1;
		a = (a * a) % mod;
	}
	return ret;
}
int main()
{
	very_slow
//	no_of_test(x)
	{
       double r;
	   cin>>r;
	   double k=(2.0)*pi*r;
        cout<<fixed<<setprecision(16)<<k<<'\n';
	}
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(long long i=0; i<n; i++)
#define Rep(i,n) for(long long i=1; i<n; i++)
#define ll long long
#include <math.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)

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

  cout<<2*N*PI<<endl;
  return 0;
}


#include <bits/stdc++.h>
#define FAST_INPUT ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long

using namespace std;

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

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

const int mod = 1e9+7;

void add_self(int& a, int b) {
	a+=b;
	if (a>mod) {
		a-=mod;
	}
}

void solve() {
	string K;
	cin >> K;
	int D;
	cin >> D;

	// 544 -> 499
	vector<vector<int>> dp(D, vector<int>(2));
	dp[0][false] = 1;
	for (int idx=0; idx<(int) K.size(); idx++) {
		vector<vector<int>> new_dp(D, vector<int>(2));
		for (int sum=0; sum<D; sum++) {
			for (bool smaller_already: {false, true}) {
				for(int digit=0; digit<10; digit++) {
					if (digit>K[idx]-'0' && !smaller_already) {
						break;
					}
					add_self(new_dp[(sum+digit)%D][smaller_already||(digit<K[idx]-'0')], dp[sum][smaller_already]);
				}
			}
		}
		dp = new_dp;
	}

	int res=(dp[0][true]+dp[0][false])%mod;
	res--;
	if (res==-1){
		res=mod-1;
	}

	cout << res << endl;
}

int main() {
    FAST_INPUT;

    solve();

    return 0;
}


#include <bits/stdc++.h>

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

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

  return 0;
}

#include <iostream>

int main()
{
    int64_t x;
    std::cin >> x;
    // 5と6を交互に出す
    // どちらから始めてもいいので、最後を6にできる
    const auto r = x % 11;
    std::cout << x / 11 * 2 + (r + 5) / 6 << std::endl;
    return 0;
}


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

#define int unsigned long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define er erase

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int oo = 1e18 + 7, mod = 1e9 + 7, mul = (1 << 30);

int n, pw[200005], a[200005], b[200005], hash1[200005], hash2[200005], c[200005], d[200005];
vector<ii> answers;

int hash11(int l, int r){
    return (hash1[r] - (hash1[l - 1] * pw[r - l + 1]) + mod * mod) % mod;
}

int hash22(int l, int r){
    return (hash2[r] - (hash2[l - 1] * pw[r - l + 1]) + mod * mod) % mod;
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) cin >> b[i];
	for(int i = 1; i < n; i++) c[i] = (a[i] ^ a[i + 1]);
	c[n] = (a[n] ^ a[1]);
	for(int i = 1; i < n; i++) d[i] = (b[i] ^ b[i + 1]);
	d[n] = (b[n] ^ b[1]);
	//for(int i = 1; i <= n; i++) cout << c[i] << " ";
	//cout << "\n";
	//for(int i = 1; i <= n; i++) cout << d[i] << " ";
	//cout << "\n";
	for(int i = 1; i <= n; i++) hash1[i] = (hash1[i - 1] * mul + c[i]) % mod;
	for(int i = 1; i <= n; i++) hash2[i] = (hash2[i - 1] * mul + d[i]) % mod;
	pw[0] = 1;
	for(int i = 1; i <= n; i++) pw[i] = (pw[i - 1] * mul) % mod;
	for(int i = 1; i <= n; i++){
	    //cout << hash11(1, n - i + 1) << " " << hash22(i, n) << "\n";
	    if(hash11(1, (n - i + 1)) != hash22(i, n) || ((i != 1) && (hash11(n - i + 2, n) != hash22(1, i - 1)))) continue;
	    answers.pb({(n - i + 1) % n, b[i] ^ a[1]});
	}
	sort(answers.begin(), answers.end());
	for(int i = 0; i < answers.size(); i++) cout << answers[i].fi << " " << answers[i].se << "\n";
}

/*
TEST HERE
INPUT #1

OUTPUT #1

INPUT #2

OUTPUT #2

INPUT #3

OUTPUT #3

*/

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<list>
#include<set>
#include<cmath>

#define REP(i,n) for(int (i) = 0;(i) < (n);(i)++)
using namespace std;
int C[10001];

int partition(int *A,int p,int r){
   int x = A[r];
   int i = p-1;
   for (int j = p ;j < r ;j++){
     if (A[j] <= x){
       i = i+1;
       swap(A[i],A[j]);
     }
   }
   swap(A[i+1],A[r]);
   return i+1;
}

int main(){
	int n,A[100000];
	cin >> n;
	REP(i,n){
		cin >> A[i];
	}
	int part;
	part = partition(A,0,n-1);
	cout << A[0];
	REP(i,n-1){
		cout << " ";
		if(part==i+1) cout << "[";
		cout << A[i+1];
		if(part==i+1) cout << "]";
	}
	cout << endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7, two = (mod+1)/2;
int n, m, k;
ll fac[N], inv[N];
ll powmod(ll a,ll b)
{
	a %= mod;
	ll ans = 1;
	while(b)
	{
		if(b&1) ans = ans*a%mod;
		a = a*a%mod;
		b >>= 1;
	}
	return ans;
}
void init(int n)
{
	fac[0] = 1;
	for(int i=1;i<=n;i++) fac[i] = fac[i-1]*i%mod;
	inv[n] = powmod(fac[n],mod-2);
	for(int i=n-1;i>=0;i--) inv[i] = inv[i+1]*(i+1)%mod;
}
ll comp(ll a,ll b)
{
	if(a<b||b<0||a<0) return 0;
	return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
int main()
{
 	ios::sync_with_stdio(false);
 	cin.tie(0);
 	ll ans = 0;
 	cin >> n >> m >> k;
 	init(n*m);
 	for(int i=1; i<n; i++)
 	{
		ll cur = 1ll*(n-i)*(n-i+1)%mod*m%mod*two%mod*m%mod;
		cur = cur*comp(n*m-2, k-2)%mod;
		ans = (ans + cur)%mod;
 	}
 	for(int i=1; i<m; i++)
 	{
		ll cur = 1ll*(m-i)*(m-i+1)%mod*n%mod*two%mod*n%mod;
		cur = cur*comp(n*m-2, k-2)%mod;
		ans = (ans + cur)%mod;
 	}
 	ans = (ans + mod)%mod;
 	cout << ans;
	return 0;
}

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

int main() {
    int ans = 0;
    int H, W, N;
    cin >> H >> W >> N;
    int MAX = max(H, W);
    ans = (N + MAX - 1) / MAX;
    cout << ans << endl;
}


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

#define vt          vector
#define sz(x)       int((x).size())
#define all(x)      (x).begin(), (x).end()
#define rall(x)     (x).rbegin(), (x).rend()
#define fi          first
#define se          second

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

void solve() {
    map<string, int> m{
        {"SUN", 7},
        {"MON", 6},
        {"TUE", 5},
        {"WED", 4},
        {"THU", 3},
        {"FRI", 2},
        {"SAT", 1}
    };

    string s;
    cin >> s;
    cout << m[s];
}

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

    int tcs = 1;
    for (int tc = 1; tc <= tcs; tc++) {
        // cout << "Case " << tc << ": ";
        solve();
    }
}

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

int main() {
  string S;
  cin>>S;
  int M=S.size();
  int N=0,A=0,B=0;
  for(int i=0;i<M;i++){
    if(S[i]=='S'){
      B++;
    }
    else{
      A++;
    }
    N=max(N,A-B);
  }
  cout<<N*2<<endl;

}


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

#define int long long

int32_t main(){
	int n;
	cin >> n;
	int a[n] ;
	for(int i = 0 ; i < n ; i++) cin >> a[i] ;
	int ans = 0 ;
	int mx = 0 ;
	for(int i = 0 ; i < n ; i++){
		mx = max(mx , a[i]) ;
		ans += mx - a[i] ;
	}
	cout << ans ;
	return 0 ;
}

#include<stdio.h>

int s[500000],n;
long long count;

void Merge(int left,int mid,int right){
    int n1 = mid - left;
    int n2 = right - mid;
    int L[n1 + 1],R[n2 + 1];
    for(int i = 0;i < n1;i++)
        L[i] = s[left + i];
    for(int i = 0;i < n2;i++)
        R[i] = s[mid + i];
    L[n1] = 2000000000;
    R[n2] = 2000000000;
    int i = 0;
    int j = 0;
    for(int k = left;k < right;k++){
        if(L[i] <= R[j])
            s[k] = L[i++];
        else{
            s[k] = R[j++];
            count += n1 - i;
        }
    }
}

void MergeSort(int left,int right){
    if(left + 1< right){
        int mid = (left + right)/2;
        MergeSort(left,mid);
        MergeSort(mid,right);
        Merge(left,mid,right);
    }
}

int main(void){
    scanf("%d",&n);
    for(int i = 0;i < n;i++){
        scanf("%d",&s[i]);
    }

    MergeSort(0,n);
    printf("%lld\n",count);
    return 0;
}


#include<iostream>
using namespace std;

void checknum(int &i, int &n);

void endchecknum(int i, int n)
{
	i++;
	if (i <= n)
	{
		checknum(i, n);
	}
	else
	{
		cout << endl;
	}
}

void include3(int &x, int &i, int &n)
{
	if (x % 10 == 3)
	{
		cout << " " << i;
		endchecknum(i, n);
	}
	else
	{
		x /= 10;
		if (x)
		{
			include3(x, i, n);
		}
		else
		{
			endchecknum(i, n);
		}
	}
}

void checknum(int &i, int &n)
{
	int x = i;

	if (x % 3 == 0)
	{
		cout << " " << i;
		endchecknum(i, n);
	}
	else
	{
		include3(x, i, n);
	}
}

void call(int &n)
{
	int i = 1;

	checknum(i, n);
}

int main()
{
	int n;

	cin >> n;
	call(n);
	return 0;
}

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

const int N = 1e5 + 5, mod = 1e9 + 7, inf = INT_MAX;

int main()
{
    //ios::sync_with_stdio(false);cin.tie(nullptr);ios_base::sync_with_stdio(false);
    int ans = inf, n, x, c;
    cin >> n;
    while (n--)
    {
        c = 0;
        cin >> x;
        while (x % 2 == 0)
            c++,
            x /= 2;
        ans = min(ans, c);
    }
    cout << ans;
    return 0;
}


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

class DICE
{
private:
	const int dice_tem[5][6] = { { 3,2,6,1,5,4 },{ 2,6,3,4,1,5 },
	{ 5,1,3,4,6,2 },{ 4,2,1,6,5,3 },{1,2,3,4,5,6} }; //{0}:W {1}:N {2}:S {3}:E {4}:defalut
	const string dice_rl = "AWWWNWWWNWWWWNWWWSWWWSWWW";
	const int n = 6;
	int dice_i[6] = { 0 };
	int dice_j[6] = { 0 };
	int a = 4;

	void setup() {
		for (int i = 0; i < n; i++) {
			cin >> dice[i];
			dice_i[i] = dice[i];
		}
	}

public:
	int dice[6];
	int num = 0;
	const int dice_suffle_num = dice_rl.length();
	DICE() {

	}
	void roll_dice() {
		switch (dice_rl[num])
		{
		case 78: //N
			a = 1;
			break;
		case 83: //S
			a = 2;
			break;
		case 87: //W
			a = 0;
			break;
		case 69: //E
			break;
			a = 3;
		default:
			a = 4;
			break;
		}

		for (int i = 0; i < n; i++) dice_j[i] = dice_i[dice_tem[a][i] - 1];
		for (int i = 0; i < n; i++) dice_i[i] = dice_j[i];
		num++;
		if (num > dice_suffle_num) num = 0;
	}
	int check(int i) {
		return dice_i[i];
	}
	void set()
	{
		setup();
	}
};

int main() {
	const int n = 6;
	bool flag = true;
	int num;
	cin >> num;
	const int con_num = num;
	DICE dice[100];
	for (int i = 0; i < num ; i++)dice[i].set();
	for (int i = 0; i < num - 1; i++) {
		for (int j = i+1; j < num; j++) {
			for (int k = 0; k < dice[0].dice_suffle_num;k++) {
				dice[j].roll_dice();
				for (int l = 0; l < n; l++) {
					if (dice[i].dice[l] != dice[j].check(l)) {
						flag = true;
						break;
					}
					flag = false;
				}
				if (flag == false)break;
			}
			if (flag == false)break;
		}
		if (flag == false)break;
	}

	if (flag == true) {
		cout << "Yes" << endl;
	}
	else if (flag == false) {
		cout << "No" << endl;
	}
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <queue>
#define rep(i,n) for (int i=0;i<(n);i++)

using namespace std;

int main() {
	int n;
	cin >> n;
	int min = n;
	int ans = 0;
	vector<int> p(n);
	for (int i = 0;i < n;i++) {
		cin >> p[i];
		if (min >= p[i]) {
			ans++;
			min = p[i];
		}
	}

	cout << ans << endl;

	return 0;
}

//BBBBBBBBBBBBBBBBB
//int main() {
//	int a, b;
//	cin >> a >> b;
//	if (a > b) {
//		for (int i = 0;i < a;i++) {
//			printf("%d", b);
//		}
//	}
//	else {
//		for (int i = 0;i < b;i++) {
//			printf("%d", a);
//		}
//	}
//
//	return 0;
//}

//AAAAAAAAAAAAAAAAA
//int main() {
//	int n, m;
//	cin >> n >> m;
//
//	if (n == m) {
//		cout << "Yes" << endl;
//	}
//	else {
//		cout << "No" << endl;
//	}
//
//	return 0;
//}


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

#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a	b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define set0(k,n) for(int nn=0;nn<(n);nn++){ (k).push_back(0); }
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define vecsize(b,size) vector<int> (b)((size));
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsl(s) vector<pair<ll,ll>> (s);
#define pairss(s) vector<pair<string,string>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairspl(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairsps(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairsREV(s) (s).push_back(make_pair(bb,aa));
#define pairslREV(s) (s).push_back(make_pair(bb,aa));
#define pairssREV(s) (s).push_back(make_pair(bb,aa));
#define MOD 1000000007

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

	ce(s.substr(0,s.size()-8))

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
string str;
signed main()
{
    cin>>str;
    for(int i=0;i<str.size()-8;i++) cout<<str[i];
    return 0;
}


#include <iostream>
using namespace std;
int main(){
  int N;
  cin>>N;
  long long answer=0;
  long long before=-1;
  int judge=0;
  for(int i=0;i<N;i++){
    long long A;
    cin>>A;
    if(A>before+1){
      cout<<-1<<endl;
      judge++;
      break;
    }
    else if(A<=before) answer+=before;
    if(i==N-1) answer+=A;
    before=A;
  }
  if(judge==0) cout<<answer<<endl;

  return 0;
}

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec){
    ll n = vec.size();
    rep(i,n) cout << "i" << " " << vec.at(i) << "\n";
}

void pr2d(vector<vector<ll>> vvec){
    ll h = vvec.size();
    ll w = vvec.at(0).size();
    rep(i,h){
        rep(j,w){
            cout << vvec.at(i).at(j) << " ";
        }
        cout << "\n";
    }
}

int main(){
    ll n ; cin >> n;
    ll prev ; cin >> prev;
    bool NG = false;
    if(prev!=0) NG = true;
    ll ans = 0;
    rep(i,n-1){
        ll x ; cin >> x;
        if(x == prev + 1){
            ans++;
        }else if(x>prev+1){
            NG = true;
        }else{
            ans+=x;
        }
        prev = x;
    }
    if(NG){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) // rep(i, 1, 5) --> {1,2,3,4} // rep(i, 5, 1) --> {4,3,2,1} // rep(it, end(v), begin(v)) --> *it

using namespace std;

int main()
{
    ll n;
    cin >> n;
    ll q = (n-1) / 11 + 1;
    ll r = n % 11;
    if (r == 0)
        r = 7;
    ll res = q * 2;
    if (r <= 6)
        cout << (ll)res - 1;
    else
        cout << (ll)res;
    return 0;
}

#include<iostream>
using namespace std;
typedef long long ll;
int main(){
    ll n;
    while(cin>>n){
        ll ans,x;
        ans=n/11<<1;
        x=n%11;
        if(x>6)
            ans+=2;
        else if(x)
            ans++;
        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;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

void dfs(vector<vector<int>> &g, vector<bool> &used, int v, vector<int> &ans) {
    used[v] = true;
    for(int nv: g[v]) {
        if(!used[nv])  {
            dfs(g, used, nv, ans);
        }
    }
    ans.push_back(v);
}
int main(){
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vector<vector<int>> to(n);
    vector<vector<int>> from(n);
    vector<int> ind(n,0);
    rep(i,n-1+m) {
        int a,b;
        cin >> a >> b;
        a--;b--;
        to[a].push_back(b);
        from[b].push_back(a);
        ind[b]++;
    }
    vector<bool> used(n, false);
    vector<int> topo_sort(0);
    //dfs
    /*
    rep(v,n) {
        if(!used[v]) dfs(to, used, v, topo_sort);
    }
    reverse(topo_sort.begin(), topo_sort.end());
    */
    //bfs
    queue<int> q;
    rep(i,n) if(ind[i] == 0) q.emplace(i);
    while(!q.empty()) {
        int now = q.front();
        topo_sort.push_back(now);
        q.pop();
        for(auto e: to[now]) {
            ind[e]--;
            if(ind[e] == 0) {
                q.emplace(e);
            }
        }
    }

    vector<int> inv_topo(n);
    rep(i,n) {
        inv_topo[topo_sort[i]] = i;
    }
    rep(i,n) {
        if(from[i].size() == 0) {
            cout << 0 << endl;
            continue;
        }
        int ans = from[i][0];
        for(int v :from[i]) {
            if(inv_topo[v] > inv_topo[ans]) {
                ans = v;
            }
        }
        cout << ans+1 << endl;
    }
}

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int v, e;
const int MAX_N = 100010;
// 隣接リスト
vector<int> g[MAX_N];
bool used[MAX_N];
// トポロジカルソートされた数列
vector<int> ans;
bool head[MAX_N];
void dfs(int u) {
  if(used[u]) return;
  used[u] = true;
  for(auto& i: g[u]) dfs(i);
  // 帰りがけ順で追加
  ans.push_back(u);
}

void tsort() {
  for(int i=0; i<v; ++i) dfs(i);
  reverse(ans.begin(), ans.end());
}

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> v >> e;
    e += v - 1;
    rep(i, v) head[i] = true;
    for(int i=0; i<e; ++i) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        g[s].push_back(t);
    }
    tsort();
    //for(int i: ans) cout << i + 1 << endl;
    vector<pair<int, int>> p;
    rep(i, v) p.push_back({i, ans[i]});
    rep(i, v) ans[i] = -1;
    sort(p.begin(), p.end());
    for(auto& e: p) {
        int u = e.second;
        //cerr << "*" << e.first << " " << e.second << endl;
        for(auto to: g[u]) {
            ans[to] = u;
        }
    }
    for(int i: ans) cout << i + 1 << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()

int w,h;
int sx,sy;
int gx,gy;
int n;

vector<vector<int>> field;
int de[] = {1, 0, -1, 0};

void dfs(int x ,int y, int c) {
  if(x < 1 || x > w || y < 1 || y > h || field[x][y] != c) return;
  field[x][y] = -1;
  dfs(x + 1, y, c);
  dfs(x - 1, y, c);
  dfs(x, y + 1, c);
  dfs(x, y - 1, c);
}

int main() {
  while(cin >> w >> h, w && h) {
    field.assign(101, vector<int>(101, 0));
    cin >> sx >> sy;
    cin >> gx >> gy;
    cin >> n;
    int dd[] = {4, 2};
    for(int i = 0;i < n;i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      for(int xx = x; xx < x + dd[d];xx++) {
        for(int yy = y;yy < y + dd[d ^ 1];yy++) {
          field[xx][yy] = c;
        }
      }
    }
    if(field[sx][sy] == 0) {
      cout << "NG" << endl;
      continue;
    }
    dfs(sx, sy, field[sx][sy]);
    if(field[gx][gy] == -1) {
      cout << "OK" << endl;
    }
    else {
      cout << "NG" << endl;
    }
  }
}

#include<iostream>
#include<queue>
using namespace std;
int main() {
	int w, h, sx, sy, gx, gy, n;
	while (cin >> w >> h, w) {
		cin >> sx >> sy >> gx >> gy >> n;
		int map[110][110] = {};
		for (int i = 0; i < n; i++) {
			int c, d, x, y; cin >> c >> d >> x >> y;
			map[x][y] = c;
			map[x][y + 1] = c;
			map[x + 1][y] = c;
			map[x + 1][y + 1] = c;
			if (d == 0) {
				map[x + 2][y] = c;
				map[x + 2][y + 1] = c;
				map[x + 3][y] = c;
				map[x + 3][y + 1] = c;
			}
			else {
				map[x][y + 2] = c;
				map[x + 1][y + 2] = c;
				map[x][y + 3] = c;
				map[x + 1][y + 3] = c;
			}
		}
		queue<pair<int, int> >q;
		int p = map[sx][sy];
		if (p == 0)goto museum;
		q.push(make_pair(sx, sy));
		while (q.size()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			map[x][y] = 0;
			if (x == gx&&y == gy)goto heaven;
			if (map[x][y - 1] == p)q.push(make_pair(x, y - 1));
			if (map[x][y + 1] == p)q.push(make_pair(x, y + 1));
			if (map[x - 1][y] == p)q.push(make_pair(x - 1, y));
			if (map[x + 1][y] == p)q.push(make_pair(x + 1, y));
		}
		museum:cout << "NG\n"; goto park;
	heaven:cout << "OK\n";
	park:;
	}
}


#include <iostream>

using namespace std;

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

int field[20][20];

int dfs(int W, int H, int x, int y, int step, int limit){
	if(++step > limit){ return limit; }
	for(int i = 0; i < 4; ++i){
		int fx = x + dx[i], fy = y + dy[i];
		if(fx < 0 || fx >= W || fy < 0 || fy >= H){ continue; }
		while(!(fx < 0 || fx >= W || fy < 0 || fy >= H)){
			if(field[fy][fx] < 0){ return step; }
			if(field[fy][fx] > 0){
				fx -= dx[i]; fy -= dy[i];
				break;
			}
			fx += dx[i]; fy += dy[i];
		}
		if(fx < 0 || fx >= W || fy < 0 || fy >= H){ continue; }
		if(fx == x && fy == y){ continue; }
		field[fy + dy[i]][fx + dx[i]] = 0;
		limit = min(limit, dfs(W, H, fx, fy, step, limit));
		field[fy + dy[i]][fx + dx[i]] = 1;
	}
	return limit;
}

int main(){
	while(true){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0){ break; }
		int x = 0, y = 0;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				cin >> field[i][j];
				if(field[i][j] == 2){
					x = j; y = i;
					field[i][j] = 0;
				}else if(field[i][j] == 3){
					field[i][j] = -1;
				}
			}
		}
		int answer = dfs(w, h, x, y, 0, 11);
		if(answer >= 11){ answer = -1; }
		cout << answer << endl;
	}
	return 0;
}

/* 駄実装の典型 皆さんはstackではなくてn進法を使いましょう */
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <deque>
#include <algorithm>

using namespace std;

int main() {
    while (1) {
        int n;
        int k;
        vector<string> element_strings;
        set<string> generated_strings;
        deque<int> stack;

        scanf("%d", &n);
        scanf("%d", &k);

        if (n == 0 && k == 0) return 0;

        for (int i=0; i<n; i++) {
            char number[3];
            scanf("%s", number);
            element_strings.push_back(string(number));
        }

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

        while (1) {
            char has_appeared[11];
            char not_count = 0;
            string new_string = "";
            int next_ind;

            while (stack.size() != k) {
                stack.push_back(0);
            }

            memset(has_appeared, 0, sizeof(has_appeared));

            for (deque<int>::iterator itr=stack.begin(); itr != stack.end(); itr++) {
                if (has_appeared[*itr]) {
                    not_count = 1;
                    break;
                }
                has_appeared[*itr] = 1;
                new_string += element_strings[*itr];
            }

            if (!not_count && generated_strings.count(new_string) == 0) {
                generated_strings.insert(new_string);
            }

            while (!stack.empty() && stack.back() == element_strings.size()-1) {
                stack.pop_back();
            }

            if (stack.empty()) {
                break;
            }

            next_ind = stack.back() + 1;
            stack.pop_back();
            stack.push_back(next_ind);
        }

        printf("%lu\n", generated_strings.size());
    }
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int,int>;

int main()
{
  int H,W;
  cin >> H >> W;
  vector<vector<int>> A(H,vector<int>(W));
  REP(i, H){
    REP(j, W){
      cin >> A.at(i).at(j);
    }
  }

  vector<vector<int>> ans(4,vector<int> ());
  int flag = 0;
  REP(i,H){
    if(i%2==0){
      REP(j,W){
        if(A.at(i).at(j)%2==1&&flag==0){
          flag=1;
          continue;
        }
        if(flag==1){
          ans.at(2).push_back(i);
          ans.at(3).push_back(j);
          if(j==0){
            ans.at(0).push_back(i-1);
            ans.at(1).push_back(j);
          }else{
            ans.at(0).push_back(i);
            ans.at(1).push_back(j-1);
          }
        }
        if(A.at(i).at(j)%2==1&&flag==1){
          flag=0;
        }
      }
    }else{
      REPR(j,W-1){
        if(A.at(i).at(j)%2==1&&flag==0){
          flag=1;
          continue;
        }
        if(flag==1){
          ans.at(2).push_back(i);
          ans.at(3).push_back(j);
          if(j==W-1){
            ans.at(0).push_back(i-1);
            ans.at(1).push_back(j);
          }else{
            ans.at(0).push_back(i);
            ans.at(1).push_back(j+1);
          }
        }
        if(A.at(i).at(j)%2==1&&flag==1){
          flag=0;
        }
      }
    }
  }

  cout << ans.at(0).size() << endl;
  REP(i,ans.at(0).size()){
    REP(j,3){
      cout << ++ans.at(j).at(i) << " ";
    }
    cout << ++ans.at(3).at(i) << endl;
  }
}

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <queue>
#include <bitset>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)
#define RPA(i,s,e) for(int i=s; i<=e; i++)
#define RPD(i,s,e) for(int i=s; i>=e; i--)
#define PB(a) push_back(a)
#define MP(i,s) make_pair(i,s)
#define ALL(a) (a).begin(), (a).end()
#define PRT(a) cout << (a) << endl

typedef pair<int,int> P;

const int INF = 1000000009;
const int MAX_V = 101;
struct edge { int to, cost; };
vector<edge> G[MAX_V];

int N,K,L;
int f,t,c;
int dist[MAX_V];

void add_edge(int from, int to, int cost) {
	edge e1 = { to, cost };
	edge e2 = { from, cost };
	G[from].PB(e1);
	G[to].PB(e2);
}

int dijkstra(int from, int to) {
	fill(dist, dist+MAX_V, INF);
	priority_queue<P, vector<P>, greater<P> > que;
	dist[from] = 0;
	que.push(MP(0, from));
	while(!que.empty()) {
		P p = que.top();
		que.pop();

		int v = p.second;
		if(dist[v] < p.first) continue;

		REP(i,G[v].size()) {
			edge& e = G[v][i];
			if(dist[e.to] > dist[v] + e.cost) {
				dist[e.to] = dist[v] + e.cost;
				que.push(MP(dist[e.to], e.to));
			}
		}
	}

	return dist[to] == INF ? -1 : dist[to];
}

void coding() {
	while(scanf("%d%d", &N, &K) != EOF) {
		REP(i,N) G[i].clear();
		REP(i,K) {
			scanf("%d", &L);
			if(L == 0) {
				scanf("%d%d", &f, &t);
				f--; t--;
				printf("%d\n", dijkstra(f, t));
			} else if(L == 1) {
				scanf("%d%d%d", &f, &t, &c);
				f--; t--;
				add_edge(f, t, c);
			} else {
				PRT("unknown");
			}
		}
	}
}

// #define _LOCAL_TEST

int main() {
#ifdef _LOCAL_TEST
	clock_t startTime = clock();
	freopen("a.in", "r", stdin);
#endif

	coding();

#ifdef _LOCAL_TEST
	clock_t elapsedTime = clock() - startTime;
	cout << endl;
	cout << (elapsedTime / 1000.0) << " sec elapsed." << endl;
	cout << "This is local test" << endl;
	cout << "Do not forget to comment out _LOCAL_TEST" << endl << endl;
#endif
}


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

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

int main(){
    int h,w;
    cin>>h>>w;
    int ans=0;
    vector<string>s(h);
    rep(i,h)cin>>s[i];
    rep(i,h)rep(j,w)if(s[i][j]=='.')ans++;
    vector<vector<int>>dist(h,vector<int>(w,INF));
    queue<P>q;
    q.push(P(0,0));
    dist[0][0]=1;
    while(!q.empty()){
        int y=q.front().first;
        int x=q.front().second;
        q.pop();
        rep(dir,4){
            int vy=y+dy[dir];
            int vx=x+dx[dir];
            if(vy<0||vy>=h||vx<0||vx>=w)continue;
            if(s[vy][vx]=='#'||dist[vy][vx]!=INF)continue;
            dist[vy][vx]=dist[y][x]+1;
            q.push(P(vy,vx));
        }
    }
    ans-=dist[h-1][w-1];
    if(dist[h-1][w-1]!=INF)cout<<ans<<endl;
    else cout<<-1<<endl;
}

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

int main() {
  int H, W; cin >> H >> W;
  int N = H*W;
  int A[N]; string str;
  int white_num = 0;
  for (int i = 0; i < H; i++) {
    cin >> str;
    for (int j = 0; j < W; j++) {
      white_num += A[i*W+j] = (str[j] == '.');
    }
  }
  queue<pair<int, int>> Q; Q.push(make_pair(0, 1));
  bool visited[N]; for (int i = 0; i < N; i++) visited[i] = false;
  visited[0] = true;
  int score = -1;
  while (!Q.empty()) {
    auto front = Q.front(); Q.pop();
    int u = front.first, d = front.second;
    if (u == H*W-1) { score = white_num-d; break; }
    vector<int> e;
    if (u >= W) e.push_back(u-W);
    if (u % W > 0) e.push_back(u-1);
    if (u % W < W-1) e.push_back(u+1);
    if (u / W < H-1) e.push_back(u+W);
    for (int v : e) {
      if (A[v] && !visited[v]) {
        visited[v] = true;
        Q.push(make_pair(v, d+1));
      }
    }
  }
  cout << score << endl;
}


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

int read(){
	int x=0; char c=getchar(); int flag=1;
	while(!isdigit(c)) { if(c=='-') flag=-1; c=getchar(); }
	while(isdigit(c)) { x=((x+(x<<2))<<1)+(c^48); c=getchar(); }
	return x*flag;
}

const int N=1e5+50;

int n,k;

struct Edge{
    int to,next;
}edge[N<<1];
int head[N];
int tmp=0;
void add(int x,int y){
    ++tmp;
    edge[tmp].to=y; edge[tmp].next=head[x];
    head[x]=tmp;
}

int a[N];
int ans;

int dfs(int nod,int dep){
    int ret=dep;
    for(int i=head[nod];i!=-1;i=edge[i].next){
	    ret=max(ret,dfs(edge[i].to,dep+1));
	}
	if(a[nod]!=1&&ret-dep==k-1) { ++ans; return 0; }
	return ret;
}

signed main(){
	memset(head,-1,sizeof(head));

    n=read(),k=read();
    for(int i=1;i<=n;i++) a[i]=read();

    if(a[1]!=1) ans=1,a[1]=1;

    for(int i=2;i<=n;i++) add(a[i],i);

    dfs(1,0);

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

#include <bits/stdc++.h>

using namespace std;

#define N 100000
#define pb push_back

int n, k, len, a[N + 5], d[N + 5], vis[N + 5], ans;
vector<int> G[N + 5];

struct Cmp {
	bool operator () (const int &a, const int &b) const {
		return d[a] < d[b];
	}
};
priority_queue<int, vector<int>, Cmp> pq;

void dfs1(int u) {
	for (int i = 0, v; i < G[u].size(); ++i) {
		v = G[u][i];
		d[v] = d[u] + 1;
		dfs1(v);
	}
}

void dfs2(int u) {
	len++;
	if (u == 1)
		return ;
	dfs2(a[u]);
}

void mark(int u) {
	vis[u] = 1;
	for (int i = 0, v; i < G[u].size(); ++i) {
		v = G[u][i];
		if (vis[v])
			continue;
		mark(v);
	}
}

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	dfs2(a[1]);
	for (int i = 2; i <= n; ++i)
		G[a[i]].pb(i);
	dfs1(1);
	bool flag = 1;
	for (int i = 1; i <= n; ++i)
		if (d[i] > k || (k - d[i]) % len) {
			flag = 0;
			break;
		}
	if (flag)
		ans = 0;
	else {
		if (a[1] != 1) ans = 1;
		else ans = 0;
		a[1] = 1;
		for (int i = 2; i <= n; ++i)
			pq.push(i);
		while (!pq.empty()) {
			int u = pq.top(), v; pq.pop();
			if (d[u] <= k || vis[u])
				continue;
			ans++;
			v = u;
			for (int i = 1; i <= k - 1; ++i)
				v = a[v];
			a[v] = 1;
//			printf("u:%d v:%d\n", u, v);
			mark(v);
		}
	}
	printf("%d\n", ans);
//	for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
//	printf("\n");
	return 0;
}


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

int cnt = 0;

void PrintfArray(int a[], int n)
{
    for(int i = 0; i < n; i++)
    {
        if( i )
            cout << endl;
        cout << a[i];
    }
    cout << endl;
}

void insertionSort(int a[], int n, int g)
{
    for(int i = g; i < n; i++)
    {
        int temp = a[i];
        int j = i - g;
        while(j >= 0 && temp < a[j]){
            a[j+g] = a[j];
            j -= g;
            cnt ++;
        }
        a[j+g] = temp;
    }
}

void shellSort(int a[], int n)
{
    cnt = 0;
    vector<int> G;
    int g = 1;
    do{
        G.push_back(g);
        g = 3 * g + 1;
    }while(g<n);

    cout << G.size() << endl;

    for(int i=G.size()-1; i>=0; i--)
    {
        if(G.size()-i-1)
            cout << " ";
        cout << G[i];
    }
    cout << endl;

    for(int i=G.size()-1; i>=0; i--)
    {
        insertionSort(a, n, G[i]);
    }
}

int main() {
	int n;
	cin >> n;
	int* a = new int[n];
	for(int i = 0; i < n; i++)
	{
	    cin >> a[i];
	}
	shellSort(a, n);

	cout << cnt << endl;
	PrintfArray(a, n);

	return 0;
}

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

using namespace std;

void insertion_sort(vector<int> &list, int interval, int &count) {
  for (int i = interval; i < list.size(); ++i) {
    int key = list[i];
    int j = i - interval;
    while (j >= 0 && list[j] > key) {
      list[j+interval] = list[j];
      j = j - interval;
      count++;
    }
    list[j+interval] = key;
  }
}

void shell_sort(vector<int> &list, int &count) {
  int index = max(0, (int)(log10(list.size())/log10(2)) - 1);
  vector<int> intervals;
  while (index >= 0) {
    intervals.push_back(pow(2, index));
    index--;
  }
  cout << intervals.size() << endl;
  for (auto iter = intervals.begin(); iter != intervals.end() - 1; ++iter) {
    cout << *iter << " ";
  }
  cout << intervals.back() << endl;
  for (int interval : intervals) {
    insertion_sort(list, interval, count);
  }
}

int main() {
  int N;
  cin >> N;
  string str;
  vector<int> list;
  for (int i = 0; i < N; ++i) {
    cin >> str;
    list.push_back(stoi(str));
  }

  int count = 0;
  shell_sort(list, count);
  cout << count << endl;
  for (auto iter = list.begin(); iter != list.end(); ++iter) {
    cout << *iter << endl;
  }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000009LL;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
    int n; cin >> n;
    vector<int> memo{0, 0, 0};
    ll res = 1;
    rep(i, n){
        int a; cin >> a;
        int cnt = 0;
        rep(i, 3)if(memo[i] == a) cnt++;
        if(cnt == 0){res = 0; break;}
        res = res * cnt % MOD;
        rep(i, 3)if(memo[i] == a){memo[i]++; break;}
    }
    cout << res << ln;
}

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

int main(){
    int n; cin >> n; int64_t ans = 0;
    vector<int64_t> a(n), b(n);
    priority_queue<pair<int64_t, int>> que;
    for (auto i = 0; i < n; i++) {
        cin >> a[i] >> b[i]; que.push({{a[i] + b[i]}, i});
    }
    for (auto i = 0; i < n; i++) {
        auto p = que.top(); que.pop();
        if(i % 2 == 0) ans += a[p.second];
        else ans -= b[p.second];
    }
    cout << ans << endl;
    return 0;
}


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

const int INF = 1000000007;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int mx = -INF, mi = INF;
    int n;
    cin >> n;
    vector<int> l(n), r(n);
    rep(i,n){
        cin >> l[i] >> r[i];
        --l[i];
        chmax(mx, l[i]);
        chmin(mi, r[i]);
    }
    int ans = 0;
    rep(i,n) chmax(ans, max(mi - mx, 0) + r[i] - l[i]);
    vector<P> v;
    rep(i,n){
        int a = max(mi - l[i], 0);
        int b = max(r[i] - mx, 0);
        v.emplace_back(b, -a);
    }
    sort(v.begin(), v.end());
    int left = INF;
    rep(i,n){
        if(i > 0) chmax(ans, left + v[i].first);
        chmin(left, -v[i].second);
    }
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const double pi=acos(-1.);
int x[233],y[233],n,sz;
double ans,st[666];

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d%d",x+i,y+i);
	for(int i=1;i<=n;i++){
		sz=0;
		for(int j=1;j<=n;j++)
			if(i^j)
				st[++sz]=atan2(x[j]-x[i],y[j]-y[i]);
		sort(st+1,st+sz+1);
		st[0]=st[sz]-2.*pi;
		ans=0;
		for(int j=1;j<=n;j++)
			ans=max(ans,st[j]-st[j-1]-pi);
		printf("%.8f\n",ans/2./pi);
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int n = s.length();
    if(n%2!=0) {
        cout<<"No\n";
        return 0;
    }
    for(int i =0;i<n;i+=2)
    {
       if(s[i]=='h' && s[i+1]=='i') continue;
       else{
           cout<<"No\n";
           return 0;
       }
    }
    cout<<"Yes\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  string s;
  cin >> s;
  rep(i, 1 << 3) {
    ll n = s[0] - '0';
    rep(j, 3) {
      if(i >> j & 1)
        n += s[j + 1] - '0';
      else
        n -= s[j + 1] - '0';
    }
    if(n != 7) continue;
    cout << s[0];
    rep(j, 3) cout << (i >> j & 1 ? '+' : '-') << s[j + 1];
    cout << "=7" << endl;
    return 0;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll mo = 998244353;
const int MAX = 510000;
const int MOD = 998244353;
long long fac[MAX], finv[MAX], inv[MAX];
ll mypow(ll a, ll b){
  ll res = 1;
  a %= mo;
  while (b)
  {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return res;
}
void COMinit(){
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++)
  {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k){
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main()
{
ll k , n ;
cin >> k >> n ;
vector<ll> ans(2*k-1);
COMinit();
for (ll i = 2; i <= 2*k; i++)
{
  ll kk;
  if(i<k+1)kk=i-1;
  else kk=2*k+1-i;
  if(i%2){
    rep(j,kk/2+1){
      ll te=COM(kk/2,j)*mypow(2,j);
      te%=mo;
      te*=COM(n-j+j+k-kk-1,j+k-kk-1);
      te%=mo;
      ans[i-2]+=te;
      ans[i-2]%=mo;
    }
  }
  else{
    rep(j,kk/2+1){
      ll te=COM(kk/2,j)*mypow(2,j);
      te%=mo;
      te*=COM(n-j+j+k-kk-1,j+k-kk-1);
      te%=mo;
      ans[i-2]+=te;
      ans[i-2]%=mo;
    }
    rep(j,kk/2+1){
      ll te=COM(kk/2,j)*mypow(2,j);
      te%=mo;
      te*=COM(n-j+j+k-kk-1-1,j+k-kk-1);
      te%=mo;
      ans[i-2]+=te;
      ans[i-2]%=mo;
    }
  }
}

for(auto a:ans){
  cout << a << endl;

}
return 0;
}

//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>

using namespace std;

const long long N = 2e3 + 5;
const long long mod = 1e9 + 7;
long long dp[N][N];
long long fact[N * N];
long long n, k;
//dp[i][j] = the number of ways when i zero has come and j colorful ball has come

void pre()
{
	fact[0] = 1;
	for (long long i = 1; i < N * N; i++)
	{
		fact[i] = fact[i - 1] * i;
		fact[i] = fact[i] % mod;
	}
}

long long power(long long a, long long b)
{
	if (!b)
	{
		return 1;
	}
	long long ans = power(a, b / 2);
	ans = ans * ans;
	ans = ans % mod;
	if (b % 2)
	{
		ans = ans * a;
		ans = ans % mod;
	}
	return ans % mod;
}

long long choose(long long a, long long b)
{
	if (a > b)
	{
		return 0;
	}
	long long ans = fact[b];
	ans = ans * power(fact[a], mod - 2);
	ans = ans % mod;
	ans = ans * power(fact[b - a], mod - 2);
	ans = ans % mod;
	return ans;
}

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	pre();
	cin >> n >> k;
	dp[0][0] = 1;
	for (int i = 0; i <= n; i++)
	{
		for (int j = 0; j <= n; j++)
		{
			// the i + j * (k - 1) + 1th ball
			if (j > i)
			{
				continue;
			}
			if (i == j) // this ball only can be zero
			{
				dp[i + 1][j] += dp[i][j];
				dp[i + 1][j] %= mod;
				continue;
			}
			else
			{
				//this ball is zero
				dp[i + 1][j] += dp[i][j];
				dp[i + 1][j] %= mod;
				// this ball is colorful
				dp[i][j + 1] += dp[i][j] * choose(k - 2, n * k - i - j * (k - 1) - 1);
				dp[i][j + 1] %= mod;
			}
		}
	}
	if (k > 1)
	{
		cout << (dp[n][n] * fact[n]) % mod << endl;
	}
	else
	{
		cout << 1 << endl;
	}

}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
using namespace std;
int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    int n,x,t;
    int ans;
    scanf("%d%d%d",&n,&x,&t);
    if(n%x==0)
        ans = n/x*t;
    else
        ans = n/x*t + t;
    printf("%d\n",ans);
    return 0;
}

#include<iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main()
{
	int x,y,z;
  cin>>x>>y;
  z =x*y;
  cout <<z<<endl;
	return 0;
}//どうして・・・(´･_･`)


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

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

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

ll l_max = 0, r_min = 1e9+5;

struct problem {
    ll l;
    ll r;
    ll a;
    ll b;

    bool operator<(const problem& other) const {
        if (a < other.a ) return true;
        else if(a > other.a ) return false;
        else return b > other.b;
    }
};

int main() {
    int N, l_arg, r_arg;
    cin >> N;

    vector<ll> L(N), R(N);
    vector<problem> problems(N);
    REP(i, N) {
        ll l, r;
        cin >> l >> r;
        L[i] = l;
        R[i] = r+1;
        if(l_max < l) {
            l_max = l;
            l_arg = i;
        }
        if(r_min > r + 1) {
            r_min = r + 1;
            r_arg = i;
        }
    }

    ll length_max = 0;
    REP(i, N) {
        problems[i].l = L[i];
        problems[i].r = R[i];
        problems[i].a = max(R[i] - l_max, ll(0));
        problems[i].b = max(r_min - L[i], ll(0));
        if(R[i] - L[i] > length_max) length_max = R[i] - L[i];
    }

    sort(ALL(problems));

    set<ll> tset;
    ll ans = 0, min1, min2;
    min1 = problems[0].a;
    min2 = 0;
    ans = min1 + min2;

    FOR(i, 1, N-1) {
        min1 = problems[i].a;
        tset.insert(problems[i-1].b);
        min2 = *tset.begin();
        if(ans < min1 + min2) ans = min1 + min2;
    }

    tset.insert(problems[N-1].b);
    if(ans < *tset.begin()) {
        ans = *tset.begin();
    }

    if(ans < length_max + max(min(R[l_arg], R[r_arg]) - max(L[l_arg], L[r_arg]), ll(0)))
        ans = length_max + max(min(R[l_arg], R[r_arg]) - max(L[l_arg], L[r_arg]), ll(0));

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

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

typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9+7;

#define pi pair<int,int>
#define pl pair<lld,lld>

#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl

#define mem0(x) memset(x,0,sizeof(x))
#define fillnum(x,n) fill(begin(x),end(x),n)
#define asort(x) sort(x.begin(),x.end())
#define dsort(x,t) sort(x.begin(),x.end(),greater<t>())
#define vuniq(x) x.erase(unique(x.begin(), x.end()), x.end())

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)

int main() {
    ll N;
    cin >> N;
    vector<ll> A;
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        ll a; cin >> a;
        A.pb(a);
        sum += a;
    }
    long double avg = (long double)((long double)sum / (long double)N);
    ll num = 0;
    for (int i = 1; i < N; i++) {
        if(abs(avg-A[num]) > abs(avg-A[i])){
            num = i;
        }
    }
    cout << num << endl;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

ll DP[200005][2];

int main() {
  for(int i=0;i<200005;++i) {
    for(int j=0;j<2;++j) {
      DP[i][j] = -big;
    }
  }
  int N;
  cin>>N;
  ll A[N];
  for(int i=0;i<N;++i) cin>>A[i];
  if(N==2) {
    cout<<max(A[0], A[1])<<endl;
    return 0;
  }
  DP[0][1] = A[0];
  DP[1][0] = A[1];
  DP[2][0] = A[2];
  DP[2][1] = DP[0][1]+A[2];
  for(int i=3;i<N;++i) {
    if(i%2==0) {
      DP[i][0] = max(DP[i-4][1], max(DP[i-3][0], DP[i-2][0]))+A[i];
      DP[i][1] = DP[i-2][1]+A[i];
    }
    else {
      DP[i][0] = max(DP[i-3][1], DP[i-2][0])+A[i];
    }
  }
  if(N%2==0) {
    cout<<max(DP[N-1][0], DP[N-2][1])<<endl;
  }
  else {
    cout<<max(DP[N-1][0], max(DP[N-2][0], DP[N-3][1]))<<endl;
  }
}

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int month[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int main(){
int m,d;
while(scanf("%d %d",&m,&d),(m!=0)){
for(int i=0;i<m;i++){
d=d+month[i];
}
d=d%7;
switch(d){
case 0:
cout<<"Wednesday"<<endl;
break;
case 1:
cout<<"Thursday"<<endl;
break;
case 2:
cout<<"Friday"<<endl;
break;
case 3:
cout<<"Saturday"<<endl;
break;
case 4:
cout<<"Sunday"<<endl;
break;
case 5:
cout<<"Monday"<<endl;
break;
case 6:
cout<<"Tuesday"<<endl;
}
}
return 0;
}


#include <iostream>

using namespace std;

typedef long long ll;

const ll mod = 998244353;

int n;
int a[305];
ll dp[305][90005];
ll dp2[305][90005];

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

int main() {
  cin >> n;
  int sum = 0;
  int maxa = 0;
  for(int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    maxa = max(maxa,a[i]);
  }

  dp[0][0] = 1;

  for(int i = 0; i < n; i++) {
    for(int j = 0; j <= i * maxa; j++) {
      if(dp[i][j] == 0) continue;
      dp[i+1][j] = (dp[i+1][j] + dp[i][j] * 2) % mod;
      dp[i+1][j+a[i]] = (dp[i+1][j+a[i]] + dp[i][j]) % mod;

    }
  }
  dp2[0][0] = 1;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j <= i * maxa; j++) {
       if(dp2[i][j] == 0) continue;
      dp2[i+1][j] = (dp2[i+1][j] + dp2[i][j]) % mod;
      dp2[i+1][j+a[i]] = (dp2[i+1][j+a[i]] + dp2[i][j]) % mod;
    }
  }

  ll ans = p(3,n);
  for(int i = 0; i <= n * maxa; i++) {
    if(i >= sum - i) {
      ans = (ans + mod - dp[n][i] * 3 % mod) % mod;
    }
    if(i == sum - i) {
      ans = (ans + dp2[n][i] * 3 % mod) % mod;
    }
  }
  if(ans < 0 ) cout << ans + mod << endl;
  else
  cout << ans << endl;
}

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 998244353;

void add(ll& a, ll b) { a = (a+b) % MOD; }
void mul(ll& a, ll b) { a = (a*b) % MOD; }

int main() {
    int N, A[300];
    cin >> N;
    int sm = 0;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        sm += A[i];
    }

    const int MX = 300 * 300;
    static ll dp[301][MX + 301], sub[301][MX + 301];
    dp[0][0] = 1;
    sub[0][0] = 1;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= MX; j++) {
            add(dp[i + 1][j + A[i]], dp[i][j]);
            add(dp[i + 1][j], 2 * dp[i][j]);
            add(sub[i + 1][j + A[i]], sub[i][j]);
            add(sub[i + 1][j], sub[i][j]);
        }
    }

    ll ng = 0;
    for (int j = (sm + 1) / 2; j <= MX; j++) add(ng, dp[N][j]);
    if (sm % 2 == 0) add(ng, MOD - sub[N][sm / 2]);
    mul(ng, 3);

    ll ans = 1;
    for (int i = 0; i < N; i++) mul(ans, 3);
    add(ans, MOD - ng);

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


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

int main()
{
	//コンテスト

	int W[10], K[10];
	for (int i = 0; i < 10; i++) {
		cin >> W[i];
	}
	for (int i = 0; i < 10; i++) {
		cin >> K[i];
	}
	sort(W, W + 10);
	reverse(W, W + 10);
	sort(K, K + 10);
	reverse(K, K + 10);
	cout << W[0] + W[1] + W[2] << " " << K[0] + K[1] + K[2] << endl;

	//ポイントカード
	/*
	int N, M;
	cin >> N >> M;
	int a;
	int A[5000];
	int b = 0;
	int x = 0;
	for (int i = 0; i <  M; i++) {
		cin >> a;
		if (a < N) {
			A[x]=a;
			x++;
		}
		else
		{
			b++;
		}
		cin >> a;
	}
	int sum = 0;
	if (b < M - 1) {
		sort(A, A+x);
		reverse(A, A+x);
		for (int i = 0; i < M - 1 - b; i++) {
			sum += N - A[i];
		}
	}
	cout << sum << endl;
	*/

	//合計時間
	/*
	int sum=0, n;
	for (int i = 0; i < 4; i++) {
	cin >> n;
	sum += n;
	}
	cout << sum / 60 << endl;
	cout << sum % 60 << endl;
	//平均点
	/*
	int P[5];
	int sum=0;
	for (int i = 0; i < 5; i++) {
	cin >> P[i];
	if (P[i] < 40) P[i] = 40;
	sum += P[i];
	}
	cout << sum / 5 << endl;
	*/
	//投票
	/*
	int N, M;
	cin >> N >> M;
	int A[1000], vote[1000];
	for (int i = 0; i < N; i++) {
	cin >> A[i];
	vote[i] = 0;
	}
	int b;
	for (int i = 0; i < M; i++) {
	cin >> b;
	for (int j = 0; j < N; j++) {
	if (A[j] <= b) {
	vote[j]++;
	break;
	}
	}
	}
	int temps = 0, ans;
	for (int i = 0; i < N; i++) {
	if (vote[i] > temps) {
	temps = vote[i];
	ans = i;
	}
	}
	cout << ans + 1 << endl;
	*/

	//2.すごろく
	/*
	int n;
	cin >> n;
	int A, _A = 1;
	int k = 0;
	int A_count[100];
	A_count[0] = 1;
	for (int i = 0; i < n; i++)
	{
	cin >> A;
	if (A == 1)
	{
	if (A == _A)
	{
	A_count[k]++;
	}
	else
	{
	k++;
	A_count[k] = 2;
	}
	}
	_A = A;
	}
	sort(A_count, A_count + k + 1);
	cout << A_count[k] << endl;
	*/

	//タイムカード
	/*
	int t[2][3];
	int x[3][3];
	for (int n=0; n < 3; n++)
	{
	for (int i=0; i < 2; i++)
	for (int k=0; k < 3; k++)
	cin >> t[i][k];

	for (int m = 2; m >= 0; m--)
	{
	x[n][m] = t[1][m] - t[0][m];
	if (t[0][m] > t[1][m])
	{
	x[n][m] += 60;
	t[1][m - 1]--;
	}
	}
	}
	for (int n= 0; n < 3; n++)
	{
	cout << x[n][0] << ' ' << x[n][1] <<' '<< x[n][2] << endl;
	}
	*/

	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main() {
	vector<int> W, K;
	int dommy;
	for (int i = 0; i < 10; ++i) {
		cin >> dommy;
		W.push_back(dommy);
	}
	sort(W.begin(), W.end(), greater<int>());
	for (int i = 0; i < 10; ++i) {
		cin >> dommy;
		K.push_back(dommy);
	}
	sort(K.begin(), K.end(), greater<int>());
	int Wpoint = 0, Kpoint = 0;
	for (int i = 0; i < 3; ++i) {
		Wpoint += W[i];
	}
	for (int i = 0; i < 3; ++i) {
		Kpoint += K[i];
	}
	cout << Wpoint << ' ' << Kpoint << endl;
	return 0;
}


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

int main() {
  string a;
  cin>>a;
  if(a.at(0)=='7'||a.at(1)=='7'||a.at(2)=='7'){
    cout<<"Yes";
  }else{
    cout<<"No";
  }
}

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int> v={0};
  for (int i = 0; i < (int)S.size(); i++) {
    if (S[i] == 'S') {
      v.push_back(0);
    } else {
      v[(int)v.size()-1]++;
    }
  }
  cout << *max_element(v.begin(), v.end()) << endl;
  return 0;
}


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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 9e18
#define ceill(a, b) (a+b-1)/b
#define ok cout << "ok" << endl;
#define sp << " " <<
ll mod = 1e9+7;
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(b<a){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

class debug
{
public:
    template<typename T> void vdeb(T vec);
    template<typename T1, typename T2> void adeb(T1 *arr, T2 n);
    template<typename T> void mdeb(T mp);
};

#endif //INCLUDE_DEBUG_HPP

template<typename T> void debug::vdeb(T vec)
{
    cout << "#vector set debug" << endl;
    for(auto vv : vec) cout << vv << " ";
    cout << endl;
}

template<typename T1, typename T2> void debug::adeb(T1 *arr, T2 n)
{
    cout << "#adebug" << endl;
    for(int i=0; i<=n; i++) cout << arr[i] << " ";
    cout << endl;
}

template<typename T> void debug::mdeb(T mp)
{
    cout << "#map pair debug" << endl;
    for(auto const& m : mp) cout << m.first sp m.second << endl;
}
debug dbg;

class BAcrostic {
public:
	void solve(std::istream& in, std::ostream& out)
	{
	    string s;
	    int w;
	    in >> s >> w;

	    string res = "";
	    for(int i=0; i<s.length(); i++){
            if(i%w == 0) res += s[i];
	    }

	    out << res << endl;
	}
};

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

#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back

int main(){

    // 1. 入力情報.
    char c[2020];
    int w;
    scanf("%s %d", c, &w);

    // 2. 文字を連結.
    string ans = "";
    int l = strlen(c);
    rep(i, l) if(i % w == 0) ans.pb(c[i]);

    // 3. 出力.
    printf("%s\n", ans.c_str());
    return 0;

}


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

int main()
{
    int n;
    cin>>n;
    while(n){
        string action;
        int base=0,outCount=0,point=0;
        while(outCount!=3){
            cin>>action;
            if(action=="OUT"){
                outCount++;
            }
            else if(action=="HIT"){
                base++;
                if(base==4){
                    point++;
                    base--;
                }
            }
            else if(action=="HOMERUN"){
                point += base+1;
                base=0;
            }
        }
        cout<<point<<endl;
        n--;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),v).end()

#define PB(a) push_back(a)
#define MOD 1000000007

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

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

  int n;cin>>n;
  for(int i=0;i<n;i++){
      int f=0,s=0,t=0,out=0,point=0;
      string st;
      while(cin>>st){
          if(st=="HIT"){
              if(t) point++;
              if(s) t=1;
              if(f) s=1;
              f=1;
          }
          else if(st=="HOMERUN"){
              point+=(f+s+t+1);
              f=0;s=0;t=0;
          }
          else{
              out++;
              if(out==3){
                  cout<<point<<endl;
                  break;
              }
          }
      }
  }

}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln =  '\n';

const int mx=100010;
const ll mod=1e9+7;

int main(){
  int n,t;
  cin >> n >> t;
  vector<P> a(n);
  rep(i,n){
    cin >> a[i].F >> a[i].S;
  }
  sort(a.begin(),a.end());

  vector<vector<ll>> dp(3010, vector<ll>(6010,0));
  rep(i,n)rep(j,6005){
    dp[i+1][j] = dp[i][j];
    if(j-a[i].F>=0 && j-a[i].F<t){
      dp[i+1][j] = max(dp[i+1][j], dp[i][j-a[i].F] + a[i].S);
    }
  }
  ll ans = 0;
  rep(i,6005){
    ans = max(ans, dp[n][i]);
  }
  cout << ans << ln;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie();
    int n, m;
    cin>>n>>m;
    if(n == 0 || m == 0){
        cout<<"0 0";
        return 0;
    }
    bool accepted[n];
    unordered_map<int, int> ma;
    for(int i = 0; i < n; i++){
        accepted[i] = false;
    }
    for(int i = 0; i < m; i++){
        int p; string s;
        cin>>p>>s;
        p-=1;
        if(s == "AC"){
            accepted[p] = true;
        }else{
            if(!accepted[p]){
                if(ma.find(p) == ma.end()){
                    ma[p] = 1;
                }else
                {
                    ma[p]++;
                }

            }
        }
    }
    int correct = 0, penalties = 0;
    for(int i = 0; i < n; i++){
        if(accepted[i]){
            correct++;
            penalties += ma[i];
        }
    }
    cout<<correct<<" "<<penalties;
    return 0;
}


#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define mx 1000008

#define pii pair<int,int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
///adjacent move
int fx[4]={0,+1,0,-1};
int fy[4]={+1,0,-1,0};
///knight
ll prime[mx];
void seive()
{
    for(int i=2;i<mx;i+=2) prime[i]=2;
    for(int i=3;i<mx;i+=2) prime[i]=i;
    ll sq=sqrt(mx);
    for(int i=3;i<=sq;i+=2){
        if(prime[i]==i){
            for(int j=i*i;j<mx;j+=i){
                if(prime[j]==j) prime[j]=i;
            }
        }
    }
}
int a[1000004];
ll f[2000004];
ll po[2000004];
int main()
{
    //seive();
    int t=1;
    //cin>>t;

    int c=0;
    vector<ll>vec;
    for(int i=2;i<mx;i++){
        if(prime[i]==i){
            vec.push_back(i);
        }
    }
    while(t--){
        ll n,m;

        cin>>n;

        ll a[n*2+7];
        for(int i=1;i<=2*n;i++) cin>>a[i];
        ll sum=0;
        sort(a+1,a+2*n+1);
        for(int i=1;i<=2*n;i++){
            if(i&1) sum+=a[i];
        }
        cout<<sum<<endl;
    }
}

/*MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWO:,'..'xXWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .:kOo.,kNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMO. .lNWWo..lXMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MNOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMMMMWKxoloxKWNc  .:xxd;  ,dxxxkXMXkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMM
MKc''''''''''''''''.  ..'''''.   .''''.  .''MMMWx..,:. .xXo''.  .''.  ..'';kMk;'''''.  .'.''.  .''''''.  ..''.   .''MM
MWNXNNNNNNNNNNNNNNNO' ;KNNNNNKo. cKNNNd. lXNMMMK; ,0Wx. :NWXXl .dNNO' ;KNNNWMWNNNNNXc .kNNNNx. cXNNNNNO' ;KNN0; 'ONNMM
MMMMMMMMMMMMMMMMMWM0' :NMMMMWMX: .OMWWx. oWMMMMWx. ';. .dNMWWo .xMM0' :NWWMMMMMMMMWWl .OMMMWk. lWWMMWM0' ;XMMX: ,KMMMM
MMMMMMWMWkccccccccc;. :NMWNNNKd. :XMMWx. oWMMMWKo. .',. .,ldx; .dMM0' :NMMMMMMMNNWMWl .OMMMWk. lWMMMMM0' ;XMMX: ,0MMMM
MMWXKNWMWxcc,. .;cc;. :XMk,''..'lKWMMWx. oWMNx:. .lONNKxl.     .xMM0' :NMMMMMM0:,oKO' ,KMMMMk. lWWNkoc,. .;::;. ,KMMMM
MMXc.lXMMWWWNd..xWM0' :NM0;  ;kXWMMMWNl  oWMK:.;xXWWMWXx:. ,c' .dMM0' :NMMMMMMKc. ...;kWMMMMk. lWWc .:;. .clc:. ,KMMMM
MWWx. lXMWMMMK, lWM0' :NMMNx'.,ldxxoc'   oWMWNKNWWWN0o' .:kNWo .dMM0' :NMMMMMMMNO;  ,OWWWMMMk. lWM' lXx. lNMMX: ,KMMMM
MMMWk'.,xKNX0c..kWM0' :NMMMMNkl;,'',:l:  oWMMMMMWMWO' 'l0WMWWo .xMM0' :NMMMMMMMMMNd. .oXWMMMk. lWMd.....;0WMMX: ,KMMMM
MMMMWKo'...'..:kWMM0' :NMMMMMMMWWNNWWWx. oWMMMMMMMMWOxXWMWMWWo .xMM0' :NMMMMMMMMMMMKl. ,kNMMk. lWMWKkxx0NMWMMX: ,KMMMM
MMMMMMMN0kxk0XNWMMWKl;xNMMMMMMMMMMMMWM0c;kWMMMMMMMMMMMWWMMMMWk;:OMMXl,dNMMMMMMMMMMMMWO; .dNM0c;kWWMMMMMMMMMMMNd,oXMMMM
MMMMMMMMWWMWMMMMMMMMMWWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWWMMWWMMWMMMMMMMMMMMMMMNkxKWMMWWMMMMMMMMMMMMMMWMMWMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM*/

//             Author: Udit "luctivud" Gupta @ (https://www.linkedin.com/in/udit-gupta-1b7863135/)                  //

#include <bits/stdc++.h>
#pragma GCC optimize "trapv"

using namespace std;

typedef long long int lld;
typedef unsigned long long int llu;

#define         TESTCASES()    cin >> (T3X0); T353 = T3X0; while(T3X0--)
#define         _input(V3C)    for(auto &V3C_I7 : (V3C)) cin >> (V3C_I7)
#define   mems(A77AY, V4LU)    memset((A77AY), (V4LU), sizeof((A77AY)))
#define    CH3K(I7, E4, S7)    (((S7)<0) ? (I7)>(E4) : (I7)<(E4))
#define   for4(I7,S4,E4,S7)    for(auto I7=(S4); CH3K(I7,E4,S7); (I7)+=(S7))

void solveEachTest(lld T35TC453N = 1) {
	int n; cin >> n;
	int k; cin >> k;
	vector<bool> arr(10, false);
	for4(i, 0, k, 1) {
		int temp; cin >> temp;
		arr[temp] = true;
	}
	while (true) {
		int temp = n;
		bool ok = true;
		while (temp) {
			if (arr[temp % 10]) {ok = false; break;}
			temp /= 10;
		}
		if (ok) {
			break;
		}
		n++;
	}

	cout << n;
	return;
}

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

	lld T3X0 = 0, T353 = 1;

	// TESTCASES()
	solveEachTest(T353 - T3X0);
	return 0;
}
// Random Thought :  null


// #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=998244353;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-9;
const long double INF=1e+10;
const long double PI=acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
int main(){
	int a,b;scanf("%d%d",&a,&b);
	if(a>b)swap(a,b);
	init_C(1100000);
	long long ret=0;
	long long cur=Comb(a+b-1,b-1);
	long long S=0;
	for(int i=1;i<=a+b;i++){
		S=(S+cur)%mod;
		ret=(ret+S*inv[i])%mod;
		if(i%2){
			if(a-(i+1)/2>=0)cur=(cur+mod-Comb(a-(i+1)/2+b-(i+1)/2,a-(i+1)/2)*Comb((i+1)/2+(i+1)/2-2,(i+1)/2-1)%mod)%mod;
		}else{
			cur=(cur+Comb(a-i/2+b-i/2-1,a-i/2)*Comb(i/2+i/2-1,i/2))%mod;
		}
		// printf("%d: %lld %lld %lld\n",i,S,ret,cur);
	}
	// printf("%lld\n",ret*12%mod);
	cur=Comb(a+b-1,a-1);
	S=0;
	for(int i=1;i<=a+b;i++){
		S=(S+cur)%mod;
		ret=(ret+S*inv[i])%mod;
		if(i%2){
			if(b-(i+1)/2>=0)cur=(cur+mod-Comb(a-(i+1)/2+b-(i+1)/2,b-(i+1)/2)*Comb((i+1)/2+(i+1)/2-2,(i+1)/2-1)%mod)%mod;
		}else{
			cur=(cur+Comb(b-i/2+a-i/2-1,b-i/2)*Comb(i/2+i/2-1,i/2))%mod;
		}
	}
	ret=ret*fact[a]%mod*fact[b]%mod*finv[a+b]%mod;
	printf("%lld\n",ret);
}

#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main(){
    ll n;
    ll m;
    cin >> n >> m;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector<ll> sum(n+1);
    sum[0] = 0;
    map<ll, ll> mp;
    mp[0] = 1;
    rep(i, n) {
        sum[i+1] = (sum[i]+a[i]) % m;
        ++mp[sum[i+1]];
    }
    ll ans = 0;
    for ( auto p : mp ) {
        ans += p.second * (p.second - 1 ) / 2;
    }
    cout << ans << endl;
    return 0;
}


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

int main(){
	int n,m;

	while(std::cin >> n >> m){

		if(n == 0 && m == 0) return 0;

		std::vector<int> a(n);

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

		std::sort(a.begin(), a.end(), std::greater<int>());//降順

		int cost = 0;

		for(int i = 0; i < n; ++i){
			if((i+1) % m != 0) cost += a[i];//mの倍数番目に高い野菜を無料にする
		}

		std::cout << cost << std::endl;
	}
}

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
int main() {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        std::cin >> a[i];
    }

    std::sort(a.begin(), a.end(), std::greater<int>());
    int ans = 0;
    for (int i = 0; i < k; i++)
    {
        ans += a[i];
    }

    std::cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
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;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

bool dfs(int now, int pre, vector<int> &d, vector<vector<int>> &g) {
    if (now) d[now] = d[pre] + 1;

    bool ret = false;
    for (int nxt : g[now]) {
        if (d[nxt] != -1) {
            if (d[nxt] % 2 != d[now] % 2)
                continue;
            else
                return true;
        }
        ret |= dfs(nxt, now, d, g);
    }
    return ret;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vector<int> d(n, -1);
    d[0] = 0;
    if (dfs(0, -1, d, g)) {
        cout << (ll)n * (n - 1) / 2 - (ll)m << endl;
        return 0;
    }

    ll odd = 0, even = 0;
    rep(i, n) {
        (d[i] % 2 == 0 ? even : odd)++;
    }
    cout << odd * even - (ll)m << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;

const int N = 1e5 + 10;
VI adj[N];
int col[N];
bool bip = true;
int f[3];

void dfs(int u, int c) {
  if (col[u] != 0) {
    if (c != col[u]) bip = false;
    return;
  }
  col[u] = c;
  f[c]++;

  for (int v: adj[u]) {
    dfs(v, 3 - c);
  }
}

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif

  int n; cin >> n;
  int m; cin >> m;
  for (int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 1);
  ll ans = 0;
  if (!bip) {
    ans = (n * 1ll * (n - 1)) / 2 - m;
  } else {
    ans = f[1] * 1ll * f[2] - m;
  }

  cout << ans << endl;

  return 0;
}


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

int main() {
    int x, y;
    cin >> x >> y;
    vector<int> m(206, 0);
    m[0] = 300000;
    m[1] = 200000;
    m[2] = 100000;
    cout << m[x - 1] + m[y - 1] + (x == 1 && y == 1 ? 400000 : 0) << endl;
    return 0;
}

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

}


  /********************************************************
  **░██████╗██╗░░░░░░█████╗░██╗░░░██╗██╗░█████╗░░██████╗░**
  **██╔════╝██║░░░░░██╔══██╗██║░░░██║██║██╔══██╗██╔════╝░**
  **╚█████╗░██║░░░░░███████║╚██╗░██╔╝██║██║░░╚═╝██║░░██╗░**
  **░╚═══██╗██║░░░░░██╔══██║░╚████╔╝░██║██║░░██╗██║░░╚██╗**
  **██████╔╝███████╗██║░░██║░░╚██╔╝░░██║╚█████╔╝╚██████╔╝**
  **╚═════╝░╚══════╝╚═╝░░╚═╝░░░╚═╝░░░╚═╝░╚════╝░░╚═════╝░**
  *********************************************************/

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

#define ll                          long long
#define ld                          long double

#define forn(i,n)                   for(int i=0;i<n;++i)
#define all(v)                      v.begin(), v.end()
#define rall(v)                     v.rbegin(),v.rend();
#define isSubstring(s1,s2)          s1.find(s2) != std::string::npos

#define pb                          push_back
#define pf                          push_front
#define mp                          make_pair
#define fr                          first
#define sc                          second

#define vpi                         <vector<pair<int,int>>
#define vi                          vector<int>
#define sz(a)                       (int)a.size()

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

#define MN                           INT_MIN
#define MX                           INT_MAX

bool isPrime(ll n){if(n<2)return false;for(ll x = 2;x*x <=n;x++){if(n%x == 0)return false;}return true;}
bool isPerfectSquare(long double x){long double sr=sqrt(x);return((sr-floor(sr))==0);}
int gcd(int a, int b){if(a==0)return b;if(b==0) return a;if(a==b)return a;if(a>b)return gcd(a-b,b);return gcd(a,b-a);}
int32_t main()
{
	map<string,int> m;
	m["SUN"] = 7;
	m["MON"] = 6;
	m["TUE"] = 5;
	m["WED"] = 4;
	m["THU"] = 3;
	m["FRI"] = 2;
	m["SAT"] = 1;
	string s;
	cin >> s;
	cout << m[s] << endl;
}

#include "bits/stdc++.h"

using namespace std;

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

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int main(){
	int n;
	cin>>n;
	rep(loop,n){
		int rui[5]={};
		string s;
		int o=0;
		int p=0;
		while(cin>>s){
			if(s=="HIT"){
				if(rui[3]==1)p++;
				rui[3]=rui[2];
				rui[2]=rui[1];
				rui[1]=1;
			}
			if(s=="OUT"){
				o++;
				if(o>=3)break;
			}
			if(s=="HOMERUN"){
				p+=1+rui[3]+rui[2]+rui[1];
				rui[3]=0;
				rui[2]=0;
				rui[1]=0;
			}
		}
		cout<<p<<endl;
	}
}


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

using namespace std;

template <class Type> Type GCD(Type a, Type b)
{
	if (a % b == 0) { return b; }
	if (b % a == 0) { return a; }

	if (a > b) { return GCD(b, a % b); }
	if (a < b) { return GCD(a, b % a); }

	return (Type)(-1);
}

template <class Type> Type GCD(vector<Type> v)
{
	Type ret = v[0];

	for (int i = 1; i < v.size(); i++)
	{
		ret = GCD(ret, v[i]);
	}

	return ret;
}

template <class Type> vector<Type> Divisors(Type n)
{
	vector<int> c;

	for (Type i = 1; i * i <= n; i++)
	{
		if (n % i == 0) { c.push_back(i); if (i * i != n) { c.push_back(n / i); } }
	}

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

	return c;
}

int main()
{
	int n;

	cin >> n;

	vector<int> v(n);

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

	vector<int> c = Divisors(GCD(v));

	for (int i = 0; i < c.size(); i++)
	{
		cout << c[i] << endl;
	}

	return 0;
}

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

int main() {
  ll n; cin >> n;
  vector<ll> t(5);
  rep(i,5) cin >> t[i];
  ll bn = *min_element(t.begin(),t.end());
  ll ans = 5;
  if (n>bn) {
    ans = n/bn + 4;
    if (n > (n/bn) * bn) ans++;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <math.h>
#include <cmath>
#include <ctime>
#include <stdlib.h>

using namespace std;

#define int long long
#define endl "\n"
#define all(v)  v.begin(),v.end()
#define fir first
#define sec second
#define m_p make_pair
#define m_t make_tuple
#define rep(i,n)  for(int i=0; i<(int) (n); i++)
typedef pair<int, int> P;

const double pai = 3.1415926535897;
const int mod = 1000000007;
const int INF = 1000000021;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];

//x未満の要素数を返す二分探索関数
int b_s(vector<int>& vec, int xx) {
	return lower_bound(all(vec), xx) - vec.begin();
}
template<typename T>void vecout(vector<T>& vec) { for (T t : vec) cout << t << " "; cout << endl; }
template<typename TT>void vecin(vector<TT>& vec) {
	for (int i = 0; i < vec.size(); i++) {
		cin >> vec[i];
	}
}

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

//n個を1個以上のx組のグループに分ける重複組み合わせはcom(n-1,x-1)
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
long long modpow(int a, int n) {
	int res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

bool chmax(int& xx, int yy) {
	if (xx < yy) {
		xx = yy;
		return true;
	}
	return false;
}
bool chmin(int& xx, int yy) {
	if (xx > yy) {
		xx = yy;
		return true;
	}
	return false;
}
int gcd(int xx, int yy) {
	int p = xx;
	int q = yy;
	if (q > p)swap(p, q);
	while (p % q != 0) {
		p %= q;
		swap(p, q);
	}
	return q;
}
int lcm(int xx, int yy) {
	return xx * yy / gcd(xx, yy);
}
bool prime(int xx) {
	if (xx <= 1) {
		return 0;
	}
	for (int i = 2; i * i <= xx; i++) {
		if (xx % i == 0) {
			return 0;
		}
	}
	return 1;
}

signed main() {
	int n, y,a,b,c;
	cin >> n >> y;
	for (int i = 0; i <= y / 10000; i++) {
		for (int j = 0; j <= (y - i * 10000) / 5000; j++) {
			int a = (y - i * 10000 - j * 5000) / 1000;
			if (a + i + j == n) {
				cout << i << " " << j << " " << a << endl;
				return 0;
			}
		}
	}
	cout << "-1 -1 -1" << endl;
}

#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

template<typename T>
T power(T x, long long n){
    if(n == 0) return 1;
    if(n == 1) return x;
    T tmp = power(x, n/2);
    if(n%2 == 0) return tmp * tmp;
    else return tmp * tmp * x;
}

ll N, M, D;
double ans = 0;
double f(double N, double D){
    if(D == 0) return N;
    else return (N-D)*2;
}

signed main(){
    cin >> N >> M >> D;
    ans = (double)(M-1)*f(N,D)/N/N;
    cout << fixed << setprecision(16) << ans << endl;
}


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

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

  bool ok = false;
  char c;

 for (c = 'a' ; c <= 'z'; c++ ) {
   ok = false;
  for (int i = 0; i < S.size(); i++) {
    if(S.at(i) == c){
      ok = true;
      break;
    }
  }
    if(ok == false){
    break;
    }
 }

  if(ok == false){
  	cout << c << endl;
  }else {
    cout << "None" << endl;
  }

}

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

int main(void){
    vector<int>X(26,0);
    string S;cin>>S;
    for(long long i=0;i<S.size();i++){
        X[(int)S[i]-97]++;
    }
    for(long long i=0;i<26;i++){
        if(X[i]==0){
            int ans=i+97;
            cout<<(char)ans<<endl;
            return 0;
        }
    }
    cout<<"None"<<endl;
    return 0;
}