#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1000000007;
signed main(){
  int N,ans=1;
  cin>>N;
  vector<int> A(3,-1);
  while(N--){
    int a;
    cin>>a;
    vector<int> B(0);
    for(int i=0;i<3;i++)
      if(A[i]+1==a)
        B.push_back(i);
    if(B.size()==0){
      puts("0");
      return 0;
    }
    ans=ans*((int)B.size())%mod;
    A[B[0]]++;
  }
  cout<<ans<<endl;
}

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
#define sz(x) int(x.size())
using namespace std;
template <class T>
void setmax(T &a, T const &b) {
    if (a < b) a = b;
}

template <class T>
void setmin(T &a, T const &b) {
    if (a > b) a = b;
}

template <typename T, typename X>
auto vectors(T a, X x) { return vector<T>(x, a); }

template <typename T, typename X, typename Y, typename... Zs>
auto vectors(T a, X x, Y y, Zs... zs) {
    auto cont = vectors(a, y, zs...);
    return vector<decltype(cont)>(x, cont);
}

// 高橋が料理i選んだらスコアはAi + Bi増え
// 青木が料理i選んだらスコアは-(Ai + Bi)増える

#define NDEBUG

int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    vector<int> ab(n);
    repeat(i, n) {
        cin >> a[i] >> b[i];
        ab[i] = a[i] + b[i];
    }

    sort(ab.rbegin(), ab.rend());
    long long ans = 0;
    repeat(i, n) ans -= b[i];
    repeat(i, n) {
        if (i % 2 == 0) ans += ab[i];
    }

    cout << ans << endl;

    return 0;
}


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

int	main(void)
{
	int a, b;
	cin >> a >> b;
	cout << a * b << endl;
	return (0);
}

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

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

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

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

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

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

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
    bool comp(const pii& a, const pii& b) {
        int t1 = a.fr * a.fr + a.sc * a.sc;
        int t2 = b.fr * b.fr + b.sc * b.sc;
        return (t1 != t2) ? t1 < t2 : a.fr < b.fr;
    }
}

signed main() {
    int h, w;
    vector<pii> v;
    repq(i,1,150) repq(j,i+1,150) {
        v.pb(pii(i,j));
    }
    sort(v.begin(), v.end(), comp);
    while(cin >> h >> w, h || w) {
        int k = 0;
        rep(i,0,v.size())  {
            if(v[i].fr == h && v[i].sc == w) {
                k = i;
                break;
            }
        }
        printf("%lld %lld\n", v[k+1].fr, v[k+1].sc);
    }
    return 0;
}


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

int ks(int n){
  int ans=1;
  for(int i=0;i<n;i++){
    ans*=10;
  }
  return ans;
}

int ki(int n){
  if(n==1){
    return 0;
  }
  int ans=1;
  for(int i=0;i<n-1;i++){
    ans*=10;
  }
  return ans;
}

bool hantei(int N,int n,int s,int c){
  int ss=ks(N-s+1);
  int si=ks(N-s+1)/10;
  if(n%ss/si==c){
    return true;
  }
  return false;
}

int main(){
  int N,M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for(int i=0;i<M;i++){
    cin >> s[i] >> c[i];
  }
  for(int i=ki(N);i<ks(N);i++){
    int flag=0;
    for(int j=0;j<M;j++){
      if(!hantei(N,i,s[j],c[j])){
        flag=1;
      }
    }
    if(flag==1){
      continue;
    }
    else{
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

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

int prime_factors(long long x){
    set<int>s;
    if(x%2 == 0){
        s.insert(2);
        while(x%2 == 0){
            x /= 2;
        }
    }
    for(int i = 3; i <= sqrt(x); i++){
        if(x%i == 0){
            s.insert(i);
            while(x%i == 0){
                x/= i;
            }
        }
    }
    if(x > 1){
        s.insert(x);
    }

    return s.size();

}

int main(){

    long long a, b;
    cin >> a >> b;
    long long g = __gcd(a, b);

    long long factors = prime_factors(g);

    cout << factors + 1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007

ll a[50];
int main() {
	for(ll i=0;i<50;i++){
		a[i]=49-i;
	}
	ll k;
	cin>>k;
	for(ll i=0;i<50;i++){
		a[i]+=k/50;
	}
	for(ll i=0;i<k%50;i++){
		a[i]++;
	}
	cout << 50<<endl;
	for(ll i=0;i<50;i++){
		cout << a[i]<<" ";
	}
	// your code goes here
	return 0;
}

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

int digit(ll x) {
    if (x / 10 == 0) return 1;
    return digit(x / 10) + 1;
}

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

    return gcd(b,a%b);
}

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

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

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

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

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

int main(){
    map<long long, int> mp;
    int n;
    cin >> n;
    for (int i = 0;i < n;i++){
        int a;
        cin >> a;
        mp[a]++;
    }

    int cnt = 0;
    for (auto v : mp){
        if (v.second % 2 != 0){
            cnt++;
        }
    }

    cout << cnt << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = M_PI;
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

int main() {
  ll n;
  cin >> n;
  vector<vll> a(n, vll(2));
  long double avg = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i][0];
    avg += a[i][0];
    a[i][1] = i;
  }
  avg /= n;
  ll m = -1;
  sort(all(a));
  for (ll i = 0; i < n; i++) {
    if (a[i][0] == avg) {
      cout << a[i][1] << endl;
      return 0;
    }
    if (a[i][0] > avg) {
      while (i > 0 && a[i][0] == a[i - 1][0]) i--;
      m = i;
      break;
    }
  }
  if (m == 0) {
    cout << a[m][1] << endl;
    return 0;
  }
  ll x = m - 1;
  while (x > 0 && a[x][0] == a[x - 1][0]) x--;
  //cout << a[m][0] - avg << " " << avg - a[x][0] << endl;
  if (a[m][0] - avg > avg - a[x][0]) cout << a[x][1] << endl;
  else if (a[m][0] - avg < avg - a[x][0]) cout << a[m][1] << endl;
  else {
    cout << min(a[x][1], a[m][1]) << endl;
  }
}

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define REV(x) reverse(ALL(x))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ld = long double;
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using Pll_ll = pair<ll, ll>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;
const double EPS = 1E-10;

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

int main(){
    int N, sum(0); cin >> N;
    VI a(N);
    REP(i, N){
        cin >> a[i];
        sum += a[i];
    }
    int ans = -1, dif(INF);
    REP(i, N){
        if(dif > abs(N*a[i] - sum)){
            dif = abs(N*a[i] - sum);
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <iostream>

using namespace std;

string s;

string Replace( string s1, string s2, string s3 )
{
    string :: size_type  pos( s1.find( s2 ) );
    while( pos != string :: npos )
    {
        s1.replace( pos, s2.size(), s3 );
        pos = s1.find( s2, pos + s3.size() );
    }
    return s1;
}

signed main()
{
    int n = 0;
    scanf( "%d\n", &n );
    while( 0 < n )
    {
        getline( cin, s );
        s = Replace( s, "Hoshino", "Hoshina" );
        cout << s << endl;
        n--;
    }
    return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin>>N;
    vi A(N),B(N),C(N);
    rep(i,N) cin>>A[i];
    rep(i,N) cin>>B[i];
    rep(i,N) cin>>C[i];
    sort(all(A));
    sort(all(B));
    sort(all(C));
    ll ans=0;
    rep(i,N){
        int a = lower_bound(all(A),B[i])-A.begin();
        int c = C.end()-upper_bound(all(C),B[i]);
        ans += (ll)a*c;
    }
    cout<<ans<<"\n";
    return 0;
}


#include<iostream>
#include<string.h>

using namespace std;

int main(){
//	int input = 0;
	char input[10];
	cin >> input;
//	int angka1 = input%10;
//	int angka2 = input-100-angka1;
//	angka2 = angka2/10;
//	int angka3 = input-angka2-angka1;
//	angka3 = angka3/100;
	if(input[0] == '7' || input[1] == '7' || input[2] == '7'){
		cout << "Yes" <<endl;
		}
	else {
		cout << "No" << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
//#include<tr1/unordered_map>
//#include"Bignum/bignum.h"
//#define lll bignum
#define ls(x) ( x << 1 )
#define rs(x) ( x << 1 | 1 )
//#define mid ( ( l + r ) >> 1 )
#define lowbit(x) ( x & -x )
#define debug(x) (cout << "#x = " << x << endl)
#define re register
#define For(i, j, k) for(re int i = (j); i <= (k); i++)
#define foR(i, j, k) for(re int i = (j); i >= (k); i--)
#define Cross(i, j, k) for(re int i = (j); i; i = (k))
using namespace std;
typedef long long ll;
const ll N = 100011;
const ll inf = 0x3f3f3f3f3f3f;

ll n, m, k, x, a[N], b[N], c[N], B[N];
double res = 0;

namespace IO {

	#define dd ch = getchar()
	inline ll read() {
		ll x = 0; bool f = 0; char dd;
		for (; !isdigit (ch); dd) f ^= (ch == '-');
		for (; isdigit (ch); dd)  x = (x << 3) + (x << 1) + (ch ^ 48);
		return f? -x: x;
	}
	#undef dd

	inline void write( ll x ) {
		if ( x < 0 ) putchar ( '-' ), x = -x;
		if ( x > 9 ) write ( x / 10 ); putchar ( x % 10 | 48 );
	}

	inline void wrn ( ll x ) { write (x); putchar ( ' ' ); }

	inline void wln ( ll x ) { write (x); putchar ( '\n' ); }

	inline void wlnn ( ll x, ll y ) { wrn (x), wln (y); }

}

using namespace IO;

inline void Redouble ( ll x ) {
	for (; x; x >>= 1) {
		if (x & 1) {
			For ( i, 1, n ) B[i] = a[b[i]];
			For ( i, 1, n ) a[i] = B[i];
		}
		For ( i, 1, n ) B[i] = b[b[i]];
		For ( i, 1, n ) b[i] = B[i];
	}
}

int main()
{
	n = read();
	For ( i, 1, n ) a[i] = read(), b[i] = i;
	foR ( i, n, 1 ) a[i] = a[i] - a[i - 1];
	m = read(), k = read();
	For ( i, 1, m ) x = read(), swap ( b[x], b[x + 1] );
	Redouble (k);
	For ( i, 1, n )
		res += a[i], printf ("%.1lf\n", res);
	return 0;
}

/*

*/

#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<stack>
#include<unordered_map>
#include<bitset>
#include<limits>
#include<complex>
#include<array>
#include<numeric>
#include<functional>

using namespace std;
#define ll long long
#define ull unsigned long long
#define rrep(i,m,n) for(ll (i)=(ll)(m);(i)>=(ll)(n);(i)--)
#define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++)
#define REP(i,n) rep(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(hoge) (hoge).begin(),(hoge).end()
typedef pair<ll, ll> P;
constexpr long double m_pi = 3.1415926535897932L;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1LL << 61;
constexpr long double EPS = 1e-10;
template<typename T> using vector2 = vector<vector<T>>;
template<typename T> using vector3 = vector<vector2<T>>;
typedef vector<ll> Array;

ll dp[65][101010];

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

	ll n;
	cin >> n;
	Array x(n);
	REP(i, n)cin >> x[i];
	ll m, k;
	cin >> m >> k;
	Array a(m);
	REP(i, m)cin >> a[i], a[i]--;
	Array d(n - 1);
	REP(i, n - 1)d[i] = x[i + 1] - x[i];
	Array p(n - 1);
	REP(i, n - 1)p[i] = i;
	REP(i, m)swap(p[a[i]], p[a[i] - 1]);
	REP(i, n - 1) dp[0][i] = p[i];
	REP(i, 63)REP(j, n - 1)dp[i + 1][j] = dp[i][dp[i][j]];
	cout << x[0] << "\n";
	REP(i, n - 1) {
		ll now = i;
		REP(j, 61)if (k & (1LL << j))now = dp[j][now];
		x[i + 1] = x[i] + d[now];
		cout << x[i + 1] << "\n";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod =(1e9)+7;
using P = pair<ll,ll>;

int main(){
    string s;cin >>s;
    ll n=s.size();
    bool flg=true;
    if(n%2==1)flg=false;
    else {
        for (int i = 0; i < n; i += 2) {
            if (s.substr(i, 2) != "hi")flg = false;
        }
    }
    if(flg)puts("Yes");
    else puts("No");
    return 0;
}

#include<iostream>

int main()
{
	int x = 0;
	int n = 0;
	std::cin >> x;

	n = x;
	for (int i = 1; i < 3; i++)
		n *= x;

	std::cout << n << std::endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <functional>
#include <math.h>
#include <utility>
using namespace std;
typedef pair<int,int> P;
int n,rnk[101];
P p[100];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++){
		p[i].first=0;
		p[i].second=i+1;
	}
	for(int i=0;i<n*(n-1)/2;i++){
		int a,b,c,d;cin>>a>>b>>c>>d;a--;b--;
		if(c!=d){
			p[c>d?a:b].first+=3;
		}else{
			p[a].first+=1;
			p[b].first+=1;
		}
	}
	sort(p,p+n,greater<P>());
	for(int i=1;i<=n;i++){
		int r=i;
		if(i!=1&&p[i-1].first==p[i-2].first)r=rnk[p[i-2].second];
		rnk[p[i-1].second]=r;
	}
	for(int i=1;i<=n;i++){
		cout<<rnk[i]<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long s[200005],sum[200005],a,b,c,d,maxx,minn,ans=1e18;
long long get(int l,int r){
    return sum[r]-sum[l-1];
}
int n;
int main(){
	scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&s[i]);
        sum[i]=s[i]+sum[i-1];
    }
    int l=1,r=3;
    for(int i=2;i<n-1;i++){
        while(l<=(i-2)&&abs(get(1,l)-get(l+1,i))>=abs(get(1,l+1)-get(l+2,i)))l++;
        while(r<=(n-2)&&abs(get(i+1,r)-get(r+1,n))>=abs(get(i+1,r+1)-get(r+2,n)))r++;
        a=get(1,l);
        b=get(l+1,i);
        c=get(i+1,r);
        d=get(r+1,n);
        maxx=max(max(a,b),max(c,d));
        minn=min(min(a,b),min(c,d));
        ans=min(ans,maxx-minn);
    }
    printf("%lld\n",ans);
}

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<cstdint>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

int64_t const INF = 1e18;

int main() {
  std::cin.tie(0); std::ios::sync_with_stdio(false);
  int N; std::cin >> N;
  std::vector<int64_t> A(N), S(N+1, 0);
  for(int i = 0; i < N; ++i) std::cin >> A[i];
  for(int i = 0; i < N; ++i) S[i+1] = S[i] + A[i];
  auto split = [&](auto b, auto e)-> std::pair<int64_t,int64_t> {
    int64_t V = (*e + *b)/2;
    auto m = std::upper_bound(b, e, V);
    int64_t mi = INF;
    std::pair<int64_t, int64_t> res;
    for(auto i: {m-1, m, m+1}) if(b < i && i < e) {
      int64_t cur = std::abs((*i-*b) - (*e-*i));
      if(cur < mi) { mi = cur; res = std::make_pair(*i-*b, *e-*i); }
    }
    return res;
  };
  auto isOK = [&](int64_t k)->bool {
    int64_t vmi = (S[N]+1)/2 - k, vma = S[N]/2 + k;
    auto lb = std::lower_bound(S.begin()+2, S.end()-3, vmi);
    auto ub = std::upper_bound(S.begin()+2, S.end()-2, vma);
    for(auto it = lb; it != ub; ++it) {
      auto L = split(S.begin(), it);
      if(std::abs(L.first - L.second) > k) continue;
      auto R = split(it, S.end()-1);
      if(std::abs(R.first - R.second) > k) continue;
      if(std::abs(L.first - R.first) > k) continue;
      if(std::abs(L.first - R.second) > k) continue;
      if(std::abs(L.second - R.first) > k) continue;
      if(std::abs(L.second - R.second) > k) continue;
      return true;
    }
    return false;
  };

  int64_t ng = -1, tmp[6];
  tmp[0] = std::abs(S[N] - S[3] - A[0]);
  tmp[1] = std::abs(S[N] - S[3] - A[1]);
  tmp[2] = std::abs(S[N] - S[3] - A[2]);
  tmp[3] = std::abs(A[0] - A[1]);
  tmp[4] = std::abs(A[0] - A[2]);
  tmp[5] = std::abs(A[1] - A[2]);
  int64_t ok = *std::max_element(tmp, tmp+6);
  while(ok - ng > 1) {
    int64_t mid = (ok+ng)/2;
    if(isOK(mid)) ok = mid; else ng = mid;
  }
  fin(ok);
  return 0;
}


#include <iostream>
using namespace std;

class Data{
private:
	int a_point, b_point;
public:
	Data(){ a_point=0; b_point=0;}
	void Add(int a, int b);
	void Show();
};

struct DataList{
	Data dataset;
	struct DataList* next;
};

void Data::Add(int a, int b){
	a_point += a;
	b_point += b;
}

void Data::Show(){
	cout << a_point << " " << b_point << endl;
}

int main(){
	int n, a, b;
	struct DataList *datalist, *p;

	datalist = new DataList;
	p = datalist;

	datalist->next = NULL;

	while(1){
		cin >> n;
		if(n == 0) break;

		p->next = new DataList;
		p->next->next = NULL;
		p = p->next;

		while(n > 0){
			cin >> a >> b;
			if(a > b) p->dataset.Add(a+b,0);
			else if(a < b) p->dataset.Add(0,a+b);
			else p->dataset.Add(a,b);
			n -= 1;
		}
	}

	p = datalist;

	while(p->next != NULL){
		p = p->next;
		p->dataset.Show();
	}

	return 0;
}

#include<iostream>

using namespace std;

int main() {
	int p1[10000], p2[10000], n, point1, point2, i;
	while (true) {
		point1 = 0;
		point2 = 0;
		cin >> n;
		if (n == 0) { break; }
		for (i = 0; i < n; i++) {
			cin >> p1[i] >> p2[i];
		}
		for (i = 0; i < n; i++) {
			if (p1[i] == p2[i]) {
				point1 += p1[i];
				point2 += p2[i];
			}
			else if (p1[i] > p2[i]) {
				point1 += p1[i] + p2[i];
			}
			else {
				point2 += p1[i] + p2[i];
			}
		}
		cout << point1 << ' ' << point2 << endl;
	}
	return 0;
}


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

int main() {

  string week[7] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
  };
  int end_day[13] = {-1,31,29,31,30,31,30,31,31,30,31,30,31};
  int month,day;
  while(cin >> month >> day) {
    if(month == 0) break;
    int today = 3; // because, 1/1 is Thursday
    for(int m = 1;m <= month;m++) {
      for(int d = 1;d <= end_day[m];d++) {
        if(m == month && d == day) break;
        today = (today + 1) % 7;
      }
    }
    cout << week[today] << endl;
  }
}

#include <iostream>
#include <stdio.h>
int main(void){
    int month,day,chek;
    while(std::cin >> month >> day,month){ int year = 2004;
    if(month == 1 || month == 2){year--;month+=12;}
    chek = ((year+ year/4-year/100+year/400+(13*month+8)/5+day)%7);
    switch (chek) {case 0:puts("Sunday");break; case 1:puts("Monday");break; case 2:puts("Tuesday");break; case 3:puts("Wednesday");break;
        case 4:puts("Thursday");break; case 5:puts("Friday");break; case 6:puts("Saturday");break; }}return 0;}


#pragma GCC optimize ("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
#define trav(i,a) for(auto &i:a)
inline int in(){int x;scanf("%lld",&x);return x;}
int cnt[100][100];
int32_t main()
{
	int n=in();
	int ans=0;
	for(int j=0;j<n;j++)
	{
		double x;
		cin>>x;
		int i=llround(x*1e9);
		int t=0,f=0;
		//cout<<i<<"\n";
		while(i%2==0)t++,i/=2;
		while(i%5==0)f++,i/=5;
		for(int x=max(18-t,0LL);x<70;x++)
		for(int y=max(18-f,0LL);y<70;y++)
		ans+=cnt[x][y];

		cnt[t][f]++;
	}
	cout<<ans;

}


#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <chrono>
#include <numeric>

using namespace std;

class cLSearch
{
	int mn, mq;
	string mS, mT;
	vector<int> vs, vt;
	int ans;

	void decode();
public:
	cLSearch(const int &n, const int &q, const string &s, const string &t)
	{
		mn = n;
		mq = q;
		mS = s;
		mT = t;

		decode();
	}

	void search();
	void output();
};

void cLSearch::search()
{
	sort(vs.begin(), vs.end());
	sort(vt.begin(), vt.end());
	int count = 0;
	vector<int>::iterator vis = vs.begin();
	vector<int>::iterator vit = vt.begin();

	int ts, tt;
	while (vis != vs.end() && vit != vt.end())
	{
		if (*vis == *vit)
		{
			++count;
			ts = *vis;
			tt = *vit;
			while (vis != vs.end() && ts == *vis)
				++vis;
			while (vit != vt.end() && tt == *vit)
				++vit;
		}
		else
		{
			if (vis != vs.end() && vit != vt.end() && *vis > *vit)
				++vit;
			else
				++vis;
		}
	}

	ans = count;
}

void cLSearch::output()
{
	cout << ans << endl;
}
void cLSearch::decode()
{
	istringstream iss;
	iss.str(mS);
	int ts;
	for (int i = 0; i < mn; ++i)
	{
		iss >> ts;
		vs.push_back(ts);
	}
	iss.str(mT);
	iss.seekg(0, iss.beg);
	for (int i = 0; i < mq; ++i)
	{
		iss >> ts;
		vt.push_back(ts);
	}
}

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

	int n, q;
	string s, t;
	cin >> n;
	cin.ignore();
	getline(cin, s);
	cin >> q;
	cin.ignore();
	getline(cin, t);

	cLSearch cls(n, q, s, t);
	cls.search();
	cls.output();

	return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define pb push_back

const int N = 16;

int a[N + 1][N + 1];
ll value[1 << N];
ll f[1 << N];

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

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

    for(int i = 1; i < (1 << n); ++i) {
        vector<int> vec;
        for(int j = 0; j < n; ++j) {
            if((i >> j)&1) vec.pb(j);
        }

        value[i] = 0;
        for(int x : vec) {
            for(int y : vec) {
                value[i] += a[x][y];
            }
        }
        value[i] /= 2;
    }

    for(int i = 0; i < (1 << n); ++i) {
        f[i] = 0;
        for(int j = i; j > 0; j = (j - 1)&i) {
            f[i] = max(f[i], value[j] + f[i^j]);
        }
    }

    cout << f[(1 << n) - 1] << endl;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int scores[2] = {0, 0};
    int points[2][10];
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 10; ++j) {
            cin >> points[i][j];
        }
        sort(points[i], points[i]+10);
        for (int j = 0; j < 3; ++j) {
            scores[i] += points[i][9 - j];
        }
    }
    cout << scores[0] << ' ' << scores[1] << endl;
    return 0;
}

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0533

int main() {
	int a[10], b[10];
	for (int i = 0; i < 10; i++) cin >> a[i];
	for (int i = 0; i < 10; i++) cin >> b[i];
	sort(a, a + 10);
	sort(b, b + 10);
	cout << (a[7] + a[8] + a[9]);
	cout << " ";
	cout << (b[7] + b[8] + b[9]) << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;
#define SIZE 12
void init_v (vector <vector <bool> >& v )
{
	int size = v.size();
	int i, j;

	for (i = 0; i < size; ++i){
		v[i][0] = true;
		v[i][size - 1] = true;
	} // end for

	for (j = 0; j < size; ++j){
		v[0][j] = true;
		v[size - 1][j] = true;
	} // end for

}

void dfs (vector <vector <bool> >& v, vector <vector <int> > m, int row, int col )
{
	const int dir[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1}, { -1, 0 } }; // ツ右ツ可コツ債カツ湘」
	int k;

	v[row][col] = true;

	for (k = 0; k < 4; ++k){
		if ( m[ row + dir[k][0] ][ col + dir[k][1] ] == 1
		&&  !v[ row + dir[k][0] ][ col + dir[k][1] ] ){
			dfs (v, m, row + dir[k][0], col + dir[k][1] );
		} // end if
	} // end for
}

void disp_map (vector <vector <int> > m )
{
	int size = m.size();
	int len  = m[0].size();
	int i, j;

	for (i = 0; i < size; ++i){
		for (j = 0; j < len; ++j){
			cout << setw(2) << m[i][j] << ' ';
		} // end for
		cout << endl;
	} // end for
}

void disp_v (vector <vector <bool> > m )
{
	int size = m.size();
	int len  = m[0].size();
	int i, j;

	for (i = 0; i < size; ++i){
		for (j = 0; j < len; ++j){
			cout << setw(2) << m[i][j] << ' ';
		} // end for
		cout << endl;
	} // end for
}

int main()
{
//	cut here before submit
//	freopen("testcase.island", "r", stdin);
	string str = "";
	int i = 1, j;
	vector <vector<int> > Map (SIZE + 2, vector <int> (SIZE + 2, 0 ) );

	while (getline (cin, str ) ){
		if (str.empty() ){
//			Map.clear();
			i = 1;
			continue;
		} // end if

		stringstream ss(str);
		ss >> str;
		for (j = 1; j <= SIZE; ++j){
			Map[i][j] = str[j-1] - '0';
		} // end for
		++i;

		if (i <= SIZE){
			continue;
		} // end if

//		cout << "i: " << i << endl;
//		disp_map (Map);

		int res = 0;
		vector <vector<bool> > visit (SIZE + 2, vector<bool> (SIZE + 2, false ) );
		init_v (visit );
		int row, col;

		for (row = 1; row < SIZE + 2; ++row){
			for (col = 1; col < SIZE + 2; ++col){
				if ( Map[row][col] == 1 && !visit[row][col]){
					++res;
					dfs (visit, Map, row , col );
//					disp_v (visit );
//					cout << endl;
				} // end if
				if (!visit[row][col]){
					visit[row][col] = true;
				} // end if
			} // end for
		} // end for
		cout << res << endl;
	} // end loop

//	cout << res << endl;

	return 0;
}


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

int main(){
  int r;
  cin >> r;
  cout << fixed << setprecision(20) << 2*r*M_PI << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define PER(i,n,a) for(ll i=n;i>a;i--)
#define REP1(i,a,n) for(ll i=a;i<=n;i++)
#define forit(it,x) for(auto it=(x).begin();it!=(x).end();++it)
#define ull unsigned long long int
#define ld long double
#define prec(n) fixed<<setprecision(n)
#define test ll t; cin>>t; while(t--)
#define re return 0
#define pb push_back
#define PI 3.1415926535897932384626
#define MOD (ll)1000000007
#define fi first
#define se second
#define eb emplace_back
#define vll vector<ll>
#define vi  vector<int>
#define mpll map<ll,ll>
#define pll pair<ll,ll>
#define mp(x,y) make_pair(x,y)
const ll MAXI = (ll)1e9+7;
int main(){
    IOS;
/*    test{
        int n;
        cin>>n;
        int ar[n];
        REP(i,a,n){
           cin>>ar[i];
        }
    }
    */
    string str;
    cin>>str;
    if(str=="SUN"){
        cout<<"7"<<"\n";
    }else if(str=="MON"){
        cout<<"6"<<"\n";
    }else if(str=="TUE"){
        cout<<"5"<<"\n";
    }else if(str=="WED"){
        cout<<"4"<<"\n";
    }else if(str=="THU"){
        cout<<"3"<<"\n";
    }else if(str=="FRI"){
        cout<<"2"<<"\n";
    }else if(str=="SAT"){
        cout<<"1"<<"\n";
    }
re;
}


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

signed main () {
    int n;
    cin >> n;
    if (n == 0) {
        cout << 0 << '\n';
        return 0;
    }
    string ans;
    while (n) {
        if (n % (-2) == 0)
            ans += '0';
        else
            --n, ans += '1';
        n /= -2;
    }
    reverse(ans.begin(), ans.end());
    cout << ans << '\n';
}

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

int main() {
    int N, sum, digit;
    cin >> N;
    digit = log10(N);
    sum = 0;
    for (int i = 0; i <= digit; i++) {
        sum += N % (int (pow(10, i + 1))) / (int (pow(10, i)));
    }
    if (N % sum == 0) {
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
const int N=110;
int h[N][N],u[N][N];
int f[N][N][N],g[N][N][N];
inline void upd(int &x,int y) { x<y?x=y:0; }
int main()
{
	int n=gi(),m=gi(),i,j,x,y,lx,rx,ly,ry,t;
	char c;
	for (i=1;i<=n;i++)
		for (j=1;j<=m;j++) {
			h[i][j]=h[i][j-1];u[i][j]=u[i-1][j];
			while ((c=getchar())!='.'&&c!='E'&&c!='o');
			if (c=='E') x=i,y=j;
			else if (c=='o') h[i][j]++,u[i][j]++;
		}
	for (lx=0;lx<x;lx++) {
		memcpy(g,f,sizeof(g));
		memset(f,0,sizeof(f));
		for (rx=0;rx<=n-x;rx++)
			for (ly=0;ly<y;ly++)
				for (ry=0;ry<=m-y;ry++) {
					t=g[rx][ly][ry];
					upd(f[rx][ly][ry],t+(x-lx-1>rx?h[x-lx-1][min(y+ry,m-ly)]-h[x-lx-1][max(y-ly-1,ry)]:0));
					upd(g[rx+1][ly][ry],t+(x+rx+1<=n-lx?h[x+rx+1][min(y+ry,m-ly)]-h[x+rx+1][max(y-ly-1,ry)]:0));
					upd(g[rx][ly+1][ry],t+(y-ly-1>ry?u[min(x+rx,n-lx)][y-ly-1]-u[max(x-lx-1,rx)][y-ly-1]:0));
					upd(g[rx][ly][ry+1],t+(y+ry+1<=m-ly?u[min(x+rx,n-lx)][y+ry+1]-u[max(x-lx-1,rx)][y+ry+1]:0));
				}
	}
	printf("%d\n",g[n-x][y-1][m-y]);
	return 0;
}

#include<bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;
using vii=vector<vector<int>>;

const ll LINF=1LL<<60;
const int INF=1<<29;
const int MOD=1e9+7;

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

const int MAX_N=1005;
const int MAX_V=MAX_N*(MAX_N-1)/2;
vii G(MAX_V);
int id[MAX_N][MAX_N];
bool vis[MAX_V];
bool fin[MAX_V];
int dp[MAX_V];

int dfs(int v){
    if(vis[v]){
        if(!fin[v]) return -1;
        else return dp[v];
    }
    vis[v]=true;
    dp[v]=1;
    for(auto &nv : G[v]){
        int res=dfs(nv);
        if(res == -1) return -1;
        dp[v]=max(dp[v], res+1);
    }
    fin[v]=true;
    return dp[v];
}

int main(){
    int n; cin >> n;
    vii a(n, vi(n-1));
    for(int i=0; i<n; i++){
        for(int j=0; j<n-1; j++){
            cin >> a[i][j];
            a[i][j]--;
        }
    }
    int num=0;
    for(int i=0; i<n-1; i++) for(int j=i+1; j<n; j++) id[i][j]=num++;
    for(int i=0; i<n; i++){
        for(int j=0; j<n-1; j++){
            if(i > a[i][j]) a[i][j]=id[a[i][j]][i];
            else a[i][j]=id[i][a[i][j]];
        }
        for(int j=0; j<n-2; j++){
            G[a[i][j]].push_back(a[i][j+1]);
        }
    }
    int ans=0;
    for(int i=0; i<num; i++){
        int res=dfs(i);
        if(res == -1){
            ans=-1;
            break;
        }
        chmax(ans, res);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
  ll n,a,b;
  cin >> n >> a >> b;
  vector<ll> x(n);
  rep(i,n) cin >> x[i];

  ll ans = 0;
  rep(i,n-1) ans += min((x[i+1]-x[i])*a,b);
  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int N,A,B,n;
  cin>>N>>A>>B>>n;
  vector<int> C(N);
  C[0]=n;
  int ans=0;
  for(int i=1;i<N;i++){
    cin>>C[i];
    ans+=min((C[i]-C[i-1])*A,B);
  }
  cout<<ans<<endl;
}


#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int aoi=2038,mod=1e9+7;
ll dp[aoi],inv[aoi];

ll kasumi(ll a,ll b=mod-2) {
ll s=1;
for (;b;b>>=1,a=a*a%mod) if (b&1) s=s*a%mod;
return s;
}

int main() {
int n,a,b,c,d,i,j,k;
read(n),read(a),read(b),read(c),read(d);
for (dp[n]=i=1;i<=n;++i) dp[n]=dp[n]*i%mod;
inv[n]=kasumi(dp[n]);
for (i=n;i;--i) inv[i-1]=inv[i]*i%mod;
for (i=a;i<=b;++i)
  for (j=0;j<=n;++j)
    for (k=c;k<=d&&j+i*k<=n;++k)
      dp[j]=(dp[j]+dp[j+i*k]*kasumi(inv[i],k)%mod*inv[k]%mod)%mod;
cout<<*dp<<"\n";
}

#include <bits/stdc++.h>

using namespace std;
inline void prep ()
{
  cin.tie (0);
  cin.sync_with_stdio (0);
};
const double pi = acos(-1.);
//printf(%.12lf\n,mid);
// __builtin_popcount(int) count 1's in binary
// get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

// 32 mil = ~1 sec
long long mymod(long long num){
  if (num< 0 && !(num%mod == 0)){
    if (mod == 1){
      return 0;
    }
    return mod-abs(num)%mod;
  }else{
    return num%mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/

double eps = .000000001;

long long modpow(long long x, long long n) {
  if (n == 0) return 1%mod;
  long long u = modpow(x,n/2);
  u = (u*u)%mod;
  if (n%2 == 1) u = (u*x)%mod;
  return u;
}
long long n, a, b, c, d;
bool memoo[1001][1001];
long long memo[1001][1001];
long long shortmod(long long num){
    if (num >= mod){
        num -= mod;
    }
    return num;
}
void EE(int a, int b, int& x, int& y)
{
    if(a%b == 0)
    {
        x=0;
        y=1;
        return;
    }
    EE(b,a%b,x,y);
    int temp = x;
    x = y;
    y = temp - y*(a/b);
}
int inverse(int a)
{
    int x,y;
    EE(a,mod,x,y);
    if(x<0) x += mod;
    return x;

}
long long facts[1001];
long long invfacts[1001];
long long inverses[1001];
long long func(int left, int grpsze){

    if (left == 0 && grpsze > b){
        return 1;
    }
    if (left < 0 || grpsze > b){
        return 0;
    }

    if (memoo[left][grpsze]){
        return memo[left][grpsze];
    }
    long long res = 0;

    long long choose = 1;
    int templeft = left;

    templeft -= grpsze*(c-1);
    choose *= modpow(invfacts[grpsze], c-1);
    choose %= mod;
    choose *= invfacts[c-1];
    choose %= mod;
    for (int i=c; i <= d; i++){
        if (templeft <= 0){
            break;
        }
        choose *= invfacts[grpsze];
        choose %= mod;
        choose *= inverses[i];
        choose %= mod;
        templeft -= grpsze;
        res += (choose*func(templeft, grpsze+1))%mod;
        res = shortmod(res);

    }
    res += func(left, grpsze+1);
    res = shortmod(res);
    memoo[left][grpsze] = true;
    memo[left][grpsze] = res;
    return res;
}
int main ()
{
  prep();

  cin >> n >> a >> b >> c >> d;

  long long cur = 1;
  for (int i=1; i<=1000; i++){
      cur *= i;
      cur %= mod;
      facts[i] = cur;
  }
  facts[0] = 1;
  invfacts[0] = 1;
  inverses[0] = 1;
  cur = 1;
  for (int i=1; i<=1000; i++){
      cur *= inverse(i);
      cur %= mod;
      invfacts[i] = cur;
  }
  for (int i=1; i<=1000; i++){
      inverses[i] = inverse(i);
  }
  cout << (facts[n]*func(n, a))%mod;

  return 0;
}


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

int main(){
  int N,count=0;
  cin >> N;
  vector<int> L(N);
  rep(i,N) cin >> L.at(i);

  for(int i=0; i<N-2; i++){
    for(int j=i+1;j<N-1;j++){
       for(int k=j+1;k<N;k++){
         int a=L.at(i),b=L.at(j),c=L.at(k);

         if(a+b>c&&a+c>b&&b+c>a&&a!=b&&b!=c&&a!=c) count++;
       }
    }
  }

  cout << count << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) int((a).size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
template<class T, class U> inline void add_self(T &a, U b){a += b;if (a >= mod) a -= mod;if (a < 0) a += mod;}
template<class T, class U> inline void min_self(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void max_self(T &x, U y) { if (y > x) x = y; }

#define _deb(x) cout<<x;
void _print() {cerr << "]\n";} template <typename T, typename... V>void _print(T t, V... v) {_deb(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
template <class T, class U> void print_m(const map<T,U> &m, int w=3){if(m.empty()){cout<<"Empty"<<endl; return;}for(auto x: m)cout<<"("<<x.first<<": "<<x.second<<"),"<<endl;cout<<endl;}
template<class T, class U>void debp(const pair<T, U> &pr, bool end_line=1){cout<<"{"<<pr.first<<" "<<pr.second<<"}"; cout<<(end_line ? "\n" : ", ");}
template <class T> void print_vp(const T &vp, int sep_line=0){if(vp.empty()){cout<<"Empty"<<endl; return;}if(!sep_line) cout<<"{ ";for(auto x: vp) debp(x,sep_line);if(!sep_line) cout<<"}\n";cout<<endl;}
template <typename T>void print(const T &v, bool show_index = false){int w = 2;if(show_index){for(int i=0; i<sz(v); i++)cout<<setw(w)<<i<<" ";cout<<endl;}for(auto &el: v) cout<<setw(w)<<el<<" ";cout<<endl;}
template <typename T>void print_vv(const T &vv){if(sz(vv)==0) {cout<<"Empty"<<endl; return;} int w = 3;cout<<setw(w)<<" ";for(int j=0; j<sz(*vv.begin()); j++)cout<<setw(w)<<j<<" ";cout<<endl;int i = 0;for(auto &v: vv){cout<<i++<<" {";for(auto &el: v) cout<<setw(w)<<el<<" ";cout<<"},\n";}cout<<endl;}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    while(cin>>n){
        vl a(n);
        forn(i,n) cin>>a[i];
        sort(all(a));
        ll ans = 0;
        fore(i,0,n){
            fore(j,i+1,n){
                fore(k,j+1,n){
                    if(a[i]<a[j]+a[k] && a[j]<a[i]+a[k] && a[k]<a[i]+a[j] && a[i]!=a[j] && a[j]!=a[k] && a[i]!=a[k]){
                        ++ans;
                    }
                }
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}


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

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define rep(i,n) for(int i = 0; i < n; i++)
#define REP(i, k , n) for(int i = k; i <= n; i++)
typedef long long ll;

const char nl = '\n' ;
const ll MOD = 1e9 + 7;

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

   int a[3], k;
   cin >> a[0] >> a[1] >> a[2] >> k;
   sort(a, a+3);

   cout << a[2]*pow(2,k) + a[1] + a[0]  << nl;
   return 0;
}

#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

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

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;

const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    cout << n + n * n + n * n * n << '\n';
}


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

int main(){
    vector<int> a(2);
    int k;
    cin >> a[0] >> a[1] >> k;

    int times = 0;
    int side = 0;
    while(1){
        if(a[side] % 2 == 1) a[side] -= 1;
        a[side] /= 2;
        a[(side+1)%2] += a[side];
        side += 1;
        side %= 2;
        times += 1;
        if(times == k) break;
    }

    cout << a[0] << " " << a[1] << endl;
    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <numeric>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <map>
#include <utility>
#include<functional>
#include <iomanip>
#include <unordered_map>
#include <set>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<ll,ll> prll;
#define MOD 1000000007           //10億　= 10^9になってる
#define juu 100000               //10万 10e5
#define hyaku 1000000            //100万　10e6
#define mINF -922337200085470000 //llのmax-1桁の小さい方
#define pINF 922337203600070000  //llのmax-1桁のデカい方

#define uINF 1844674407399900000 //ullのmax-1桁してる
#define pi 3.1415926535897932384
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll factor[300];
ll memory[300];
/*素朴法で計算
factorは必ずmemset(factor,0,sizeof(factor));
グローバルであるmemoryに個数を書き込む
memory配列の走査にはfactorの要素を使う*/
void primefac(ll num)
{
    int i = 0, kosuu = 0;
    while (*(factor + i) != 0)
    {
        while (num % *(factor + i) == 0)
        {
            num /= *(factor + i);
            memory[*(factor + i)] += 1;
        }
        if (num == 1)
            break;
        i++;
    }
}
/*グローバルにあるfactorに素数書き込む.個数を返すので配列は-1しろforは<*/
int elast(ll number)
{
    ll tmp = 0;
    int flag = 0;
    *factor = 2;
    for (int i = 3; i <= number; i++)
    {
        flag = 0;
        for (int j = 0; j <= tmp; j++)
        {
            if (i % (*(factor + j)) == 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag)
            continue;
        tmp += 1;
        *(factor + tmp) = i;
    }
    return tmp + 2;
}

/*intで収まる範囲であってくれ*/
ll nCr(ll n, ll r)
{
    ll val = 1;
    ll i;
    if (r == 0)
        return 1;
    for (i = 0; i < n - r; i++)
    {
        val *= (n - i);
        val /= (i + 1);
    }
    return val;
}
/*intで収まる範囲であってくれ*/
ll nPr(ll n, ll r)
{
    ll val = 1;
    ll i;
    for (i = 0; i < r; i++)
    {
        val *= (n - i);
    }
    return val;
}
template <typename T>
bool chmin(T &a, const T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &a, const T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

/*ソートして被りを無くす*/
template <typename T>
void eraseSame(T &a)
{
    sort(all(a));
    a.erase(unique(all(a)), a.end());
}

/*aとbで大きい方を後ろにする。変更なしならtrue、ありならfalse */
template <typename T>
bool swapmax(T &a, T &b)
{
    T temp;
    temp = a;
    if (a > b)
    {
        a = b;
        b = temp;
        return false;
    }
    return true;
}
ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

/*一行に入力が何個あるかを1,ぶち込むvectorを2*/
template <typename T>
void splitIn(ll N, vector<T> &array)
{
    T temp;
    int i = 0;
    if(N == 0){
        return ;
    }
    array.resize(N);
    while (cin >> temp)
    {
        array[i] = temp;
        i++;
        if (i >= N)
        {
            break;
        }
    }
    return;
}
int main()
{
    ll n,k,m;
    ll ans = 0;
    ll tm1,tm2;
    string s;
    vector<ll> v;
    cin >> n >>  m >> k;
    rep(i,k){
        if(i%2 == 1){
        m/=2;
        tm2 = m;
        n += tm2; }
        else{
        n/= 2;
        tm1 = n;
        m += tm1;

        }

    }

    cout << n <<" " << m;
    return 0;
}


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

int main(){
	ll n;
	cin>>n;
	vector<vector<pair<ll,ll>>>ro(n+1);
	for(int i=0;i<n-1;i++){
		ll a,b,c;
		cin>>a>>b>>c;
		ro[a].push_back({b,c});
		ro[b].push_back({a,c});
	}
	queue<ll>que;
	ll q,k;
	cin>>q>>k;
	ll dis[114514];
	for(int i=1;i<=n;i++)dis[i]=-1;
	que.push(k);
	dis[k]=0;
	while(!que.empty()){
		ll x=que.front();
		que.pop();
		for(int i=0;i<ro[x].size();i++){
			if(dis[ro[x][i].first]!=-1)continue;
			dis[ro[x][i].first]=dis[x]+ro[x][i].second;
			que.push(ro[x][i].first);
		}
	}
	for(int i=0;i<q;i++){
		ll a,b;
		cin>>a>>b;
		cout<<dis[a]+dis[b]<<endl;
	}
	return 0;
}

#include <iostream>
#include<iomanip>
#include <cmath>
#include <climits>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <tuple>
#include <map>
#include <list>
#include <string>
#include <numeric>
#include <utility>
#include <cfloat>
#include <set>

using namespace std;

int sqrtN = 512;
struct SqrtDecomposition{
    int N, K;
    vector <long long> data;
    vector <long long> bucketUpdatedLazy;

    SqrtDecomposition(int n){
        N = n;
        K = (N + sqrtN - 1) / sqrtN;
        data.assign(N, ((long long) 1 << 31) - 1);
        bucketUpdatedLazy.assign(K, -1);
    }

    void propagateLazy(int k){
        if(bucketUpdatedLazy[k] < 0){
            return;
        }
        for(int i = 0; i < sqrtN; i++){
            data[k * sqrtN + i] = bucketUpdatedLazy[k];
        }
        bucketUpdatedLazy[k] = -1;
    }

    long long get(int x){
        propagateLazy(x / sqrtN);
        return data[x];
    }

    void update(int x, int y, int a){
        if(y - x < sqrtN){
            propagateLazy(x / sqrtN);
            propagateLazy((y - 1) / sqrtN);
            for(int i = x; i < y; i++){
                data[i] = a;
            }
            return;
        }
        for(int i = x / sqrtN + 1; i < y / sqrtN; i++){
            bucketUpdatedLazy[i] = a;
            //if(bucketUpdatedLazy[i] < 0 && bucketUpdatedLazy[i] != -1){
            //    cerr << "Warning" << endl;
            //}
        }

        propagateLazy(x / sqrtN);
        for(int i = x; i < (x / sqrtN + 1) * sqrtN; i++){
            data[i] = a;
        }
        propagateLazy((y - 1)/ sqrtN);
        for(int i = y / sqrtN * sqrtN; i < y; i++){
            data[i] = a;
        }
    }

};

int main(){
    int n;
    int q;
    cin >> n >> q;
    SqrtDecomposition sq(n);
    for(int i = 0; i < q; i++){
        int com;
        int s;
        int t;
        int x;
        cin >> com;
        if(com == 0){
            cin >> s >> t >> x;
            sq.update(s, t + 1, x);
        } else {
            cin >> x;
            cout << sq.get(x) << endl;
        }
    }
    return 0;
}


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

using namespace std;

long long INF = 1e18;

int main() {
    int N;
    cin >> N;
    vector<long long> A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    deque<long long> dp(N, INF);
    for(int i = 0; i < N; i++) {
        long long p = lower_bound(dp.begin(), dp.end(), A[i]) - dp.begin();
        if(p == 0) {
            dp.push_front(A[i]);
        } else {
            dp[p - 1] = A[i];
        }
    }
    cout << lower_bound(dp.begin(), dp.end(), INF) - dp.begin() << endl;
}

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

#define bit(n,k) ((n>>k)&1) //*n no k bit me 1 or 0*/

vector<vector<ll>> a(20, vector<ll>(20,0));
vector<ll> dp((1<<18),-longinf);
int n;

ll rec(ll x){
  if(dp[x]!=-longinf) return dp[x];
  ll cur = 0;
  rep(i,n)rep(j,i)if(bit(x,i) && bit(x,j)){ cur += a[i][j]; }
  for(ll i=(1<<n)-1; i>=0; i--){
    i &= x; //x bubun syugo //https://primenumber.hatenadiary.jp/entry/2016/12/01/000000
    if(i==x || i==0) continue;
    cur = max(cur, rec(i) + rec(i^x));
  }
  return dp[x] = cur;
}

int main(){
  cin >> n;
  rep(i,n)rep(j,n){
    cin >> a[i][j];
  }
  ll ans = rec((1<<n)-1);
  cout << ans << ln;
  return 0;
}


#include <iostream>
using namespace std;

static int bubbleSort(int *A, int N){
	int temp, count=0;
	bool flag;
	do{
		flag = false;
		for (int j = N - 1; j >= 1; j--){
			if (A[j] < A[j - 1]){
				temp = A[j];
				A[j] = A[j - 1];
				A[j - 1] = temp;
				count++;
				flag = true;
			}
		}
	} while (flag);

	return count;
}

int main(void){
	int N, *A, count;

	cin >> N;

	A = new int[N];

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

	count = bubbleSort(A, N);

	for (int i = 0; i < N; i++){
		cout << A[i];
		if (i != N - 1){
			cout << " ";
		}
	}

	cout << "\n" << count <<"\n";

	return 0;
}

#include <iostream>
#include <complex>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;

void rotate(int a[], string str)
{
    for(auto itr = str.begin(); itr != str.end(); ++itr)
    {
        int tmp;
        switch (*itr) {
            case 'N':
                tmp = a[0];
                a[0] = a[1];
                a[1] = a[5];
                a[5] = a[4];
                a[4] = tmp;
                break;
            case 'S':
                tmp = a[0];
                a[0] = a[4];
                a[4] = a[5];
                a[5] = a[1];
                a[1] = tmp;
                break;
            case 'W':
                tmp = a[3];
                a[3] = a[0];
                a[0] = a[2];
                a[2] = a[5];
                a[5] = tmp;
                break;
            case 'E':
                tmp = a[2];
                a[2] = a[0];
                a[0] = a[3];
                a[3] = a[5];
                a[5] = tmp;
                break;
        }
    }
}

int isSame(int a[], int b[])
{
    bool flag;
    for (int i = 0; i < 6; ++i) {
        flag = false;
        for (int j = 0; j < 6; ++j) {
            if (a[i] == b[j]) {
                flag = true;
                break;
            }
        }
        if (flag==false)
            break;
    }

    if (flag == false) {
        return 0;
    }

    int t = b[0], f = b[1];

    if (t == a[5])
            rotate(a, "NN");
    else if (t == a[1])
            rotate(a, "N");
    else if (t == a[3])
            rotate(a, "E");
    else if (t == a[2])
            rotate(a, "W");
    else if (t == a[4])
            rotate(a, "S");

    if (f == a[3])
            rotate(a, "ESW");
    else if (f == a[2])
            rotate(a, "WSE");
    else if (f == a[4])
                rotate(a, "SENWSE");

    for (int i = 0; i < 6; ++i) {
        if (a[i] != b[i]) {
            return 0;
        }
    }
    return 1;
}

int main(void)
{
    int a[100][6], n;

    cin >> n;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < 6; ++j)
            cin >> a[i][j];

    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (i == j)
                continue;
            if (isSame(a[j], a[i]) == 1) {
                cout << "No" << endl;
                return 0;
            }
        }
    }

    cout << "Yes" << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define ST string
#define BS bitset
#define VE vector
#define VL vector<LL>
#define VP vector<pair<LL,LL>>
#define VVL vector<vector<LL>>
#define PQ priority_queue<LL>
#define PQS priority_queue<LL, vector<LL>, greater<LL>>
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define FOR(i,a,n) for(i=a;i<n;i++)
#define rep(i,n) FOR(i,0,n)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a,n) *upper_bound(ALL(a),n)
#define LB(a,n) *lower_bound(ALL(a),n)
#define INF 1145141919810364364
#define PI 3.14159265358979
//#define MOD 1000000007
#define MOD 998244353
#define ERR 0.00000000000001
#define NUM 200010

void Yn(LL a){if(a)printf("Yes\n");else printf("No\n");}
void YN(LL a){if(a)printf("YES\n");else printf("NO\n");}
LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;}
LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
LL LCM(LL a,LL b){return a*b/GCD(a,b);}
LL mod(LL a,LL m){if(a<0)return a%m+m;else return a%m;}
LL DIV(LL a,LL d){LL m=MOD,x=1,y=0,k;while(m){k=d/m;d-=k*m;swap(m,d);x-=k*y;swap(x,y);}return mod(a*mod(x,MOD),MOD);}
LL FAC(LL a){LL i,ans=1;FOR(i,1,a+1){ans*=i;if(MOD>0&&ans>MOD)ans%=MOD;}return ans;}
LL POW(LL a,LL n){LL ans=1;while(n>0){if(n&1)ans=ans*a%MOD;a=a*a%MOD;n>>=1;}return ans;}
LL fact[NUM],finv[NUM],inv[NUM];
void comi(){LL i;fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[1]=1;FOR(i,2,NUM){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;}
bool cmps(pair<LL,LL> a,pair<LL,LL> b){if(a.second!=b.second)return a.second<b.second;return a.first<b.first;}

struct UF{
private:
  VL par;
public:
  UF(LL n){par.resize(n,-1);}
  LL root(LL x){if(par[x]<0)return x;else return par[x]=root(par[x]);}
  LL size(LL x){return -par[root(x)];}
  bool same(LL x,LL y){return root(x)==root(y);}
  void unite(LL x,LL y){x=root(x);y=root(y);if(x!=y){if(size(x)<size(y))swap(x,y);par[x]+=par[y];par[y]=x;}}
};
typedef struct{LL s,e,c,n;}Edge;
bool cmpc(Edge a,Edge b){return a.c<b.c;}
struct Kruscal{
private:UF *uf;VE<Edge> ed;
public:
  VE<Edge> edge;
  Kruscal(LL N){uf=new UF(N);}
  void add(LL s,LL e,LL c,LL n=0){Edge f;f.s=s,f.e=e,f.c=c,f.n=n;ed.PB(f);}
  void run(){sort(ALL(ed),cmpc);for(Edge a:ed){if(uf->same(a.s,a.e))continue;else{edge.PB(a);uf->unite(a.s,a.e);}}}
};

typedef struct{LL x,y,n;}Node;
bool cmpx(Node a,Node b){return a.x<b.x;}
bool cmpy(Node a,Node b){return a.y<b.y;}

int main(){
  LL i,N,ans=0;
  cin>>N;
  VE<Node> v(N);
  Kruscal k(2*N-2);
  rep(i,N){
    cin>>v[i].x>>v[i].y;
    v[i].n=i;
  }
  sort(ALL(v),cmpx);
  rep(i,N-1)k.add(v[i].n,v[i+1].n,v[i+1].x-v[i].x);
  sort(ALL(v),cmpy);
  rep(i,N-1)k.add(v[i].n,v[i+1].n,v[i+1].y-v[i].y);
  k.run();
  for(Edge a:k.edge)ans+=a.c;
  cout<<ans<<endl;
}

#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MAX 2000005
#define MOD 1000000007
#define MOD2 998244353
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define vi vector<ll>
#define vp vector<pii>
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
#define trace(x) cerr<<#x<<": "<<x<<'\n';
typedef long long ll;
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

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

const int N = 100005;

bool cmp1(pair<int,pair<int,int>>a, pair<int,pair<int,int>>b){
    return a.S.F<b.S.F;
}

bool cmp2(pair<int,pair<int,int>>a, pair<int,pair<int,int>>b){
    return a.S.S<b.S.S;
}

pair<int,pair<int,int>>c[N];
int par[N],siz[N];

int find(int x){
    while(par[x]!=x){
        x=par[x];
    }
    return x;
}

void un(int a,int b){
    a=find(a);
    b=find(b);
    if(siz[a]<siz[b]){
        par[a]=par[b];
        siz[b]+=siz[a];
    }
    else{
        par[b]=par[a];
        siz[a]+=siz[b];
    }
}

int main(){
    IOS

    int n;
    cin>>n;
    FOR(i,1,n+1){
        par[i]=i;
        siz[i]=1;
        c[i].F=i;
        cin>>c[i].S.F>>c[i].S.S;
    }

    vector<pair<int,pair<int,int>>>v;

    sort(c+1,c+n+1,cmp1);

    FOR(i,1,n){
        v.pb(mp(c[i+1].S.F-c[i].S.F,mp(c[i].F,c[i+1].F)));
    }
    sort(c+1,c+n+1,cmp2);
    FOR(i,1,n){
        v.pb(mp(c[i+1].S.S-c[i].S.S,mp(c[i].F,c[i+1].F)));
    }
    sort(all(v));

    int cnt=0;
    int i=0;
    ll ans=0;
    while(cnt<n-1){
        int x=find(v[i].S.F),y=find(v[i].S.S);
        if(x==y){
            i++;
            continue;
        }
        un(x,y);
        ans+=v[i].F;
        i++;
        cnt++;
    }
    cout<<ans;

    cerr<<"Time: "<<1.0/CLOCKS_PER_SEC<<" ms"<<endl;
    return 0;
}


#include <iostream>

using ll = long long int;

constexpr ll mod = (1e9)+7;
constexpr int MAX = 2*1e5+10;

ll fac[MAX];

void init() {
	fac[0]=1;
	for(int i=1;i<MAX;++i)
		fac[i]=(fac[i-1]*i)%mod;
}

ll inv(ll x){
	ll n = mod-2;
	ll y = 1;
	ll z = 1;
	for(int i=0;i<40;++i){
		if(n&y) z = (z*x)%mod;
		x=(x*x)%mod;
		y*=2;
	}
	return z;
}

ll func(ll x, ll y) {
	return ((x*(x+1)/2%mod)*y%mod + (y*(y+1)/2%mod)*x%mod)%mod;
}

ll g(ll x){return x*(x+1)/2%mod;}

int main(){
	init();
	int n, m, k;
	std::cin>>n>>m>>k;

	ll comb = (fac[n*m-2] * inv(fac[k-2]) % mod) * inv(fac[n*m-k]) % mod;

	ll sum = 0;

	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			sum+=func(i-1,j-1)+func(i-1,m-j)+func(n-i,j-1)+func(n-i,m-j)+g(i-1)+g(j-1)+g(m-j)+g(n-i);
			sum%=mod;
		}
	}

	std::cout<<((comb*sum)%mod)*inv(2)%mod<<std::endl;

	return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int mod = 1e9 + 7;

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

ll inv (ll a) {
  return pwr (a, mod - 2);
}

ll nCr (ll n, ll r) {
  ll ans = 1;
  for (int i = 0; i < r; i++) ans = (ans * (n - i)) % mod;
  for (int i = 1; i <= r; i++) ans = (ans * inv (i)) % mod;

  return ans;
}

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

  int n, m;
  ll k;
  cin >> n >> m >> k;

  ll ans = 0;
  for (int i = 1; i <= m; i++) {
    ans += (((ll)(m - i) * (m - i + 1)) / 2);
    ans %= mod;
  }
  ll a = (((ll)n * (n - 1)) + n) % mod;
  ans = (ans * a) % mod;
  ll tmp_ans = 0;
  for (int i = 1; i <= n; i++) {
    tmp_ans += (((ll)(n - i) * (n - i + 1)) / 2);
    tmp_ans %= mod;
  }
  ll b = (((ll)m * (m - 1)) + m) % mod;
  tmp_ans = (tmp_ans * b) % mod;

  ans = (ans + tmp_ans) % mod;

  ans = (ans * nCr ((ll)n * m - 2, k - 2)) % mod;

  cout << ans << '\n';
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define mod 998244353
#define inv2 499122177
#define M 1000020
using namespace std;
namespace IO{
	int Top=0; char SS[20];
	void write(int x){
		if(!x){putchar('0');return;} if(x<0) x=-x,putchar('-');
		while(x) SS[++Top]=x%10,x/=10;
		while(Top) putchar(SS[Top]+'0'),--Top;
	}
	int read(){
		int nm=0,fh=1; char cw=getchar();
		for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
		for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
		return nm*fh;
	}
}using namespace IO;
int mul(int x,int y){return (LL)x*(LL)y%mod;}
int add(int x,int y){return (x+y>=mod)?x+y-mod:x+y;}
int mus(int x,int y){return (x-y<0)?x-y+mod:x-y;}
int qpow(int x,int sq){
	int res=1;
	for(;sq;sq>>=1,x=mul(x,x)) if(sq&1) res=mul(res,x);
	return res;
}
int n,m,fac[M],ifac[M],ans;
int C(int tot,int tk){return mul(fac[tot],mul(ifac[tot-tk],ifac[tk]));}
int main(){
	n=read(),m=read(),fac[0]=1; if(n>m) swap(n,m);
	for(int i=1;i<=n+m;i++) fac[i]=mul(fac[i-1],i); ifac[n+m]=qpow(fac[n+m],mod-2);
	for(int i=n+m;i;i--) ifac[i-1]=mul(ifac[i],i);
	for(int i=1;i<=n;i++) ans=add(ans,mul(C(i<<1,i),C(n+m-(i<<1),n-i)));
	write(add(mul(ans,mul(inv2,qpow(C(n+m,m),mod-2))),m)),putchar('\n'); return 0;
}

#include <stdio.h>

using ll = long long;
const int MX = 1000005, MM = 998244353;

ll F[MX], I[MX], IF[MX];

ll C(int x,int y){return F[x+y]*IF[x]%MM*IF[y]% MM;}

int main()
{
	int N, M, i;
	I[1]=F[0]=IF[0]=F[1]=IF[1]=1;
	for(i = 2; i < MX; i++){
		I[i] = (MM - MM/i) * I[MM%i] % MM;
		F[i] = F[i-1] * i % MM;
		IF[i] = IF[i-1] * I[i] % MM;
	}

	scanf("%d%d", &N, &M);
	if( N > M ) N^=M^=N^=M;
	ll ans = M, tot = IF[N+M] * F[N] % MM * F[M] % MM;
	for(i = 1; i <= N; i++){
		ans = (ans + tot * C(N-i, M-i) % MM * C(i, i) % MM * I[2]) % MM;
	}
	printf("%lld\n", ans);
}


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

int main(){
  long n;
  cin >> n;
  string ans = "";
  while(n){
    if(n%2==0){
      ans.push_back('0');
    }else{
      ans.push_back('1');
      n -= 1;
    }
    n /= (-2);
  }
  if(int(ans.size())==0)ans.push_back('0');
  rep(i,int(ans.size())){
    cout << ans[int(ans.size())-1-i];
  }
  cout << endl;
  return 0;
}

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1.0)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    ll n;
    string ans;
    cin >> n;
    ll i = 1;
    while(n != 0){
        ans += (n % (i*(-2)) ? "1" : "0");
        if(n % (i*(-2)))n -= i;
        i *= (-2);
    }
    reverse(all(ans));
    cout << (ans.size() ? ans : "0") << endl;
}


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

int max_gcd(int n, int m) {
    if (n == 1) return m;
    for (int gcd = m / n; gcd > 0; --gcd) {
        if (m % (gcd * n) % gcd == 0) return gcd;
    }
    return 1;
}

int main() {
    int N, M;
    std::cin >> N >> M;
    std::cout << max_gcd(N, M) << '\n';
    return 0;
}

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

int main() {
    int N;
    ll M;
    cin >> N >> M;
    // int M2 = M;

    // map<int, int> mp;  // 素因数、個数
    // for (int i = 2; i * i <= 1000000000; i++) {
    //     while (M % i == 0) {
    //         mp[i]++;
    //         M /= i;
    //     }
    // }
    // if (M != 1) mp[M]++;

    // int res = 1;
    // for (auto p : mp) {
    //     int prime = p.first;
    //     int num = p.second;
    //     printf("prime, num = %d, %d\n", prime, num);
    //     // while (num >= N) {
    //     //     res *= prime;
    //     //     num -= N;
    //     // }
    //     for (int i = 0; i < num; i++) {
    //         int now = res * prime;
    //         if ((M2 / now) >= N) {
    //             res *= prime;
    //         }
    //         else {
    //             break;
    //         }
    //     }
    // }

    vector<ll> div;
    div.push_back(1);
    for (int i = 2; i * i <= M; i++) {
        if (M % i == 0) {
            div.push_back(i);
            if (i != (M/i)) {
                div.push_back(M/i);
            }
        }
    }
    div.push_back(M);

    sort(div.rbegin(), div.rend());

    ll res = 0;
    // for (int i = div.size() - 1; i >= 0; i--) {
    rep(i, div.size()) {
        if ((M / div[i]) >= N) {
            res = div[i];
            break;
        }
    }

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


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

bool linearSearch(const vector<int>& ary, int key) {
    for (auto i = 0; i < ary.size(); i++) {
        if (ary[i] == key) {
            return true;
        }
    }
    return false;
}

int main(void) {
    int n;
    cin >> n;
    vector<int> S(n);
    for (int i = 0; i < n; i++) {
        cin >> S[i];
    }
    int q;
    cin >> q;

    int cnt = 0;
    for (int i = 0; i < q; i++) {
        int key;
        cin >> key;

        if (linearSearch(S, key)) cnt++;
    }

    cout << cnt << endl;
    return 0;
}

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int,int>
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define endl '\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

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

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

constexpr ll MOD = 1000000007;

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

  vi d(3);
  rep(i, 3) cin >> d[i];

  int k; cin >> k;
  sort(all(d));

  rep(i, k) d[2] *= 2;

  cout << accumulate(all(d),0) << endl;

  return (0);
}


/*#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
#include<bits/stdc++.h>
#define ll long long
#define inf 1000000005
#define put putchar('\n')
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,t) for (int i=head[t];i;i=Next[i])
#define sqr(x) ((x)*(x))
#define re register
#define mp make_pair
#define fi first
#define se second
#define pa pair<int,int>
#define pb push_back
#define be begin()
#define en end()
#define ret return puts("-1"),0;
#define mod 1000000007
#define N 500055
#define int ll
using namespace std;
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){char c=getchar();int tot=1;while ((c<'0'|| c>'9')&&c!='-') c=getchar();if (c=='-'){tot=-1;c=getchar();}
int sum=0;while (c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}return sum*tot;}
inline void wr(int x){if (x<0) {putchar('-');wr(-x);return;}if(x>=10)wr(x/10);putchar(x%10+'0');}
inline void wrn(int x){wr(x);put;}inline void wri(int x){wr(x);putchar(' ');}
inline void wrn(int x,int y){wri(x);wrn(y);}inline void wrn(int a,int b,int c){wri(a);wrn(b,c);}
int n,m,vis[N],co[N],I,P,Q;
int nedge,Next[N*2],head[N],to[N*2];
#define V to[i]
void add(int a,int b){
	Next[++nedge]=head[a];head[a]=nedge;to[nedge]=b;
}
void add_ne(int a,int b){add(a,b);add(b,a);}
bool dfs(int x){
	vis[x]=1;
	bool num=1;
	go(i,x){
		if (vis[V]) num&=(co[x]!=co[V]);
		else co[V]=co[x]^1,num&=dfs(V);
	}
	return num;
}
signed main(){
	n=read();m=read();
	F(i,1,m){
		add_ne(read(),read());
	}
	F(i,1,n){
		if (!vis[i]){
			if (head[i]==0) I++;
			else if (dfs(i)) Q++;
			else P++;
		}
	}
	wrn(I*I+I*(n-I)*2+P*Q*2+P*P+Q*Q*2);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
ll N, S, X[100500], P[100500];

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> S;
    for(int i = 1; i <= N; i++) cin >> X[i] >> P[i];
    vector<l_l> before;
    vector<l_l> after;
    for(int i = 1; i <= N; i++) {
        if(X[i] >= S) break;
        before.push_back({X[i], P[i]});
    }
    for(int i = N; i >= 1; i--) {
        if(X[i] <= S) break;
        after.push_back({X[i], P[i]});
    }
    ll sum = 0;
    int cheer = 1;
    vector<l_l> que;
    int after_index = 0;
    int before_index = 0;
    for(int time = 1; time <= N; time++) {
        if(after_index >= after.size()) {
            que.push_back(before[before_index]);
            before_index++;
            continue;
        }
        if(before_index >= before.size()) {
            que.push_back(after[after_index]);
            after_index++;
            continue;
        }
        ll left = before[before_index].second;
        ll right = after[after_index].second;
        if(cheer == 1) {
            left += sum;
        } else {
            right += sum;
        }
        if(right > left) {
            que.push_back(before[before_index]);
            sum += before[before_index].second;
            before_index++;
            cheer = 0;
        } else {
            que.push_back(after[after_index]);
            sum += after[after_index].second;
            after_index++;
            cheer = 1;
        }
    }
    ll ans = 0;
    ll now = S;
    for(int i = que.size() - 1; i >= 0; i--) {
        ans += abs(que[i].first - now);
        now = que[i].first;
    }
    cout << ans << endl;
    return 0;
}


#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;
const double EPS = 1E-10;

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

int main(){
    int N; cin >> N;
    cout << (N - 2) * 180 << endl;
    return 0;
}

#include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include<vector>
#include<iomanip>
#include<map>
#include <queue>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<set>
#include <bits/stdc++.h>

using namespace std;
using ll = long long int;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++) //REP(i, 5) cout<<i;
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define MOD 1000000007 //10^9+7
//#define MOD 1

ll gcd(ll a, ll b) {
  ll tmp;
  if ( b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b !=0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

const int MAX = 1000;

ll table[MAX][MAX];

// テーブルを作る前処理
void COMinit() {
    table[0][0] = table[0][1] = 1;
    FOR(i, 1, MAX-1){
      FOR(j, 0, i+1){
        if(j==0) table[i][j] = 1;
        else if(j == i+1) table[i][j] = 1;
        else table[i][j] = table[i-1][j-1]+table[i-1][j];
      }
    }
}

// 二項係数計算
ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return table[n-1][k];
}

void sosuu(ll n, vector<ll> &v){

  vector<ll> num(n, 0);

  FOR(i, 1, sqrt(n)){
    if(num[i]==0){
      FOR(j, 0, n/i){
        num[i+(j*i)] = 1;
      }
    }
  }
  REP(i, n){
    if(num[i]==0) v.push_back(i+1);
  }
}

int main(void){

   double n;
   cin>>n;

    cout<<(n-2)*180.0<<endl;
  return 0;
}


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

int main() {

    int N;
    cin >> N;
    vector<int> A(N);
    rep(i,N){
        cin >> A[i];
    }

    map<int, int> K;
    for(int i = 0; i < N; i++){
        K[A[i]]++;
    }

    int ans = 0;
    for(auto m:K){
        if(m.second % 2 != 0){
            ans++;
        }
    }
    cout << ans << endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
signed main(){
  int n,ans=0;
  string a,b,c;
  cin >> n;
  cin >> a >> b >> c;
  for (int i = 0; i < n; i++){
    if (a[i] == b[i]){
      if (b[i] != c[i]){
        ans++;
      }
    }else if (b[i] == c[i]){
      ans++;
    }else if (a[i] == c[i]){
      ans++;
    }else{
      ans += 2;
    }
  }
  cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<double> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<int>> vvint;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<int, vector<int>, greater<int>> spqint; //小さい順に取り出し
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;     //小さい順に取り出し
typedef priority_queue<int, vector<int>, less<int>> bpqint;    //大きい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;        //大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define TS to_string
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

int INF = 1e9;
int NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
double EPS = 1e-9;
double PI = M_PI;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     //最小公倍数

void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}
//-----------------------------------------

//-----------------------------------------
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::chrono::system_clock::time_point start,end;
    start = std::chrono::system_clock::now();

    ll n, k ;
    cin >> n >> k ;
    vll pa ;
    vll ma ;
    pa.PB(0) ;
    ma.PB(0) ;
    REP(i,n){
        ll a ;
        cin >> a ;
        if(a==0){
            k-- ;
        }
        else if(a>0){
            pa.PB(a) ;
        }
        else{
            ma.PB(-a) ;
        }
    }
    sort(ALL(ma)) ;
    sort(ALL(pa)) ;
    ll sp = SZ(pa) ;
    ll sm = SZ(ma) ;

    ll ans = LINF ;

    for(ll i=0;i<=k;i++){
        ll tmp ;
        if(k-i<sm && i<sp){
            tmp = 2*min(pa[i],ma[k-i]) + max(pa[i],ma[k-i]) ;
            ans = min(ans,tmp) ;
        }
    }

    cout << ans <<endl;

    end = std::chrono::system_clock::now();
    auto elapsed = std::chrono::duration_cast< std::chrono::milliseconds >(end - start).count();
    //std::cout << elapsed <<"ms"<< std::endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
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=1001001001;
vector<pair<int64_t,int64_t>>prime_factorize(int64_t x){
  vector<pair<int64_t,int64_t>>p;
  for(int64_t i=2;i*i<=x;i++){
    int cnt=0;
    if(x%i==0){
    while(x%i==0){cnt++;x/=i;}
    p.push_back(make_pair(i,cnt));
    }
  }
  if(x!=1){p.push_back(make_pair(x,1));}
  return p;
}
int main() {
  int N,K;
  cin>>N>>K;
  vector<int>x(N);
  for(int i=0;i<N;i++){
    cin>>x[i];
  }
  vector<int>dis;
  for(int i=0;i<N-1;i++){
    dis.push_back(x[i+1]-x[i]);
  }
  vector<int64_t>table(N+2);
  for(int i=0;i<N-1;i++){
    table[i+1]=table[i]+dis[i];
  }
  int64_t ans=999999999999999;
  for(int i=0;i<N;i++){
    if(N-K>=i){
    int64_t sum=0;
    int start=min(abs(x[i]),abs(x[i+K-1]));
    sum=start+table[i+K-1]-table[i];
    //cout<<sum<<endl;
    ans=min(ans,sum);
    }
  }
  cout<<ans<<endl;
  return 0;
}


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

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

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// const value
// const ll MOD = 1e9 + 7;
// const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
// const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline ll toLL(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int l = a + b;
  int r = c + d;
  if(l > r) {
    cout << "Left" << endl;
  } else if(r > l) {
    cout << "Right" << endl;
  } else {
    cout << "Balanced" << endl;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long i,j,k,l,m,n,o,p,q;
    while(cin>>m>>n>>o>>p)
    {
        if(m+n>o+p)
            cout<<"Left"<<endl;
        else if(m+n<o+p)
            cout<<"Right"<<endl;
        else
            cout<<"Balanced"<<endl;
    }
    return 0;
}


#include"bits/stdc++.h"

#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(ll x = (l);x<=(u);x++)
#define RREP(x,l,u) for(ll x = (l);x>=(u);x--)
#define mst(x,a) memset(x,a,sizeof(x))
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define se second
#define fi first
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define cl(x) x.clear()

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

const int maxn = 100010;
const int mod = 1e9+7;
const int MAX = 1000000010;
const double eps = 1e-6;
const double PI = acos(-1);

template<typename T> inline void read(T &x){
x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

template<typename A,typename B> inline void read(A&x,B&y){read(x);read(y);}
template<typename A,typename B,typename C> inline void read(A&x,B&y,C&z){read(x);read(y);read(z);}
template<typename A,typename B,typename C,typename D> inline void read(A&x,B&y,C&z,D&w){read(x);read(y);read(z);read(w);}
template<typename A,typename B> inline A fexp(A x,B p){A ans=1;for(;p;p>>=1,x=1LL*x*x%mod)if(p&1)ans=1LL*ans*x%mod;return ans;}
template<typename A,typename B> inline A fexp(A x,B p,A mo){A ans=1;for(;p;p>>=1,x=1LL*x*x%mo)if(p&1)ans=1LL*ans*x%mo;return ans;}

int n,m,TOT;

int G[16][16];

int f[(1<<15)+5][16];

int All[(1<<15)+5];

void Work(){
	REP(sta,0,(1<<n)-1)REP(i,1,n)f[sta][i]=-1000000000;f[1][1]=0;
	REP(sta,0,(1<<n)-1)
		REP(i,1,n)if((sta>>i-1)&1){
			REP(j,1,n)if(j!=i&&((sta>>j-1)&1)&&G[i][j])f[sta][i]=max(f[sta][i],f[sta^(1<<i-1)][j]+G[i][j]);
			for(int s=sta;s;s=(s-1)&sta)if(!((s>>i-1)&1))f[sta][i]=max(f[sta][i],f[sta^s][i]+All[s^(1<<i-1)]);
		}
	iout(TOT-f[(1<<n)-1][n]);
}

void Init(){
	read(n,m);
	REP(i,1,m){
		int u,v,w;
		read(u,v,w);
		G[u][v]=G[v][u]=w;
		TOT+=w;
	}
	REP(sta,0,(1<<n)-1){
		REP(i,1,n)if((sta>>i-1)&1)
			REP(j,i+1,n)if((sta>>j-1)&1)All[sta]+=G[i][j];
	}
}

int main(){
	Init();
	Work();
	return 0;
}

//  main.cpp
//  ABC174F2
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <tuple>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
// Binary Indexed Tree (Fenwick Tree)
//    使い方 :    BIT<int>bit
//                BIT<int> bt(n + 1); BITを初期化して
//                 bt.add(x, 1); xに1を加算
//                bt.sum(x); 0～xの累積和
template<typename T> // T: type of cost
struct BIT {
private:vector<T> array;
    const int n;
public:BIT(int _n):array(_n+1,0),n(_n) {}
    // iまでの累積和
    T sum(int i) {
        T x = 0;
        while(i>0){
            x += array[i];
            i-=i&-i; // LSBからの減算
        }
        return x;
    }
    // iからjの区間和
    T sum(int i,int j){
        T ret_i=sum(i-1);
        T ret_j=sum(j);
        return ret_j-ret_i;
    }
    // i番目にxを追加
    void add(int i, T x) {
        while(i<=n) {
            array[i] += x;
            i+=i&-i; // LSBへの加算
        }
    }
};

int main(int argc, const char * argv[]) {
    int n, q;
    cin >> n >> q;
    vector<int>c(n + 1);
    vector<int>p(n + 1, -1); // -1で初期化しておく
    vector<int> st(n + 1,0);
    // st(s)=t  左がsで右がtの矢印
    // ２回目の登場以降ならiをtとして登録
    for (int i = 1; i <= n; i++) {
        cin >> c.at(i);
        if (p.at(c.at(i)) != -1) {
            st.at(p.at(c.at(i)))=i;
        }
            p.at(c.at(i)) = i;
    }
    /*rep(i,n){
        cout<<i<<" "<<st.at(i)<<endl;
    }*/
    // クエリを整理する
    // q(l)=(r,i) q(左端)=(右端、読み込みの順番)
    int l, r;
    vector<int>len(q);
    vector<vector<P>> query(n+1,vector<P>()); // lの値によってクエリを整理する
    rep(i, q) {
        cin >> l >> r;
        query.at(l).emplace_back(r,i);
        len.at(i)=r-l+1;
    }
    /*for(int i=n-1;i>=0;i--){
        for(auto& x:query.at(i)){
            cout<<i<<" "<<x.first<<" "<<x.second<<endl;
        }
    }*/
 // BITを使う
    vector<int> cnt(q);
    BIT<int> bt(n + 1); // BITを初期化
    for (int s = n; s >= 1; s--) {
        if (st.at(s) > 0) {
            bt.add(st.at(s), 1);
        }
        for(auto& x:query.at(s)){
            cnt.at(x.second)=bt.sum(x.first);
        }
    }
    rep(i, q) {
        cout << len.at(i)-cnt.at(i) << endl;
    }
    return 0;
}


#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 998244353
#define INIT -1

int main()
{
   string n;
   cin >> n;
   // result[i][0] : i - 1桁めまで払ってる　繰り下がりはない
   // result[i][1] : i - 1桁目まで払ってる　繰り下がりあり
   int result[1000010][2] = {};
   result[0][0] = 0;
   result[0][1] = 1;
   for (int i = 1; i <= n.length(); i++)
   {
      result[i][0] = min(result[i - 1][0] + (n[i - 1] - '0'), result[i - 1][1] + (10 - (n[i - 1] - '0')));
      result[i][1] = min(result[i - 1][1] + (10 - (n[i - 1] - '0') - 1), result[i - 1][0] + (n[i - 1] - '0' + 1));
   }
   cout << result[n.length()][0] << endl;
}

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

#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, n, m) for(int (i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
//constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9+7;

int main(){
  string S;
  cin >> S;
  int N = S.size();
  int ans = 0;;
  int c = 0;
  REPR(i, N-1){
    int x = S[i] - '0';
    if((x+c) > 5){
      ans += 10 - (x+c);
      c = 1;
    }
    else if((x+c) == 5){
      if(i > 0 && S[i-1] >= '5'){
        ans += 10 -(x+c);
        c= 1;
      }
      else{
        ans += (x+c);
        c = 0;
      }
    }
    else{
      ans+= x+c;
      c = 0;
    }
  }
  ans += c;
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>

#define REP(i, n) for(int (i)=0;(i)<(n);++(i))
#define REPV(iter, v) for(auto (iter)=(v).begin(); (iter)!=(v).end();++(iter))
#define ALL(v) (v).begin(),(v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

int main()
{
    ll H, W, A, B;
    cin >> H >> W >>A >> B;

    vector<string> ansS(H);
    vector<ll> countH(W, 0);
    REP(i, H)
    {
        REP(j, W)
        {
            if (i < B)
            {
                if (j < A)
                {
                    ansS[i].push_back('0');
                }
                else
                {
                    ansS[i].push_back('1');
                }
            }
            else
            {
                if (j < A)
                {
                    ansS[i].push_back('1');
                }
                else
                {
                    ansS[i].push_back('0');
                }
            }
        }
    }

    REP(i, H)
    {
        cout << ansS[i] << endl;
    }
}

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

int main() {
	long long n,i,p,q;
	while(cin>>n,n){
		vector<int> ids;
		map<int,long long> m;
		while(n--){
			cin>>i>>p>>q;
			if(!m.count(i))ids.push_back(i);
			m[i]+=p*q;
		}
		int na=1;
		for (int k=0;k<(int)ids.size();++k) {
			if(1000000<=m[ids[k]]){
				cout<<ids[k]<<endl;
				na=0;
			}
		}
		if(na) cout<<"NA\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(void){
  int n;
  cin>>n;
  vector<vector<int>> x(n);
  vector<vector<int>> y(n);
  for(int i=0;i<n;i++){
    int a;
    cin>>a;
    for(int j=0;j<a;j++){
      int b,c;
      cin>>b>>c;
      x.at(i).push_back(b);
      y.at(i).push_back(c);
    }
  }
  int count=0;
  bool check=true;
  for(int tmp=0;tmp<1<<15;tmp++) {
    if (tmp>=(1<<n)) continue;
    bitset<15> s(tmp);
    for(int i=0;i<n;i++) {
      if (s[i]==0) continue;
      int a=x.at(i).size();
      for(int j=0;j<a;j++) {
        if (s[x.at(i).at(j)-1]!=y.at(i).at(j)) {
          check=false;
          break;
        }
      }
    }
    int count1=s.count();
    if(check)count=max(count1,count);
    check=true;
  }
  cout<<count<<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 <functional>
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<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; }

int main() {
    int n;
    cin >> n;
    VVI x(n), y(n);
    REP(i,n){
        int a = in();
        REP(j,a){
            x[i].push_back(in() - 1);
            y[i].push_back(in());
        }
    }
    int ans = 0;
    REP(mask,1<<n){
        bool f = true;
        int cnt = 0;
        REP(i,n) if ((mask >> i) & 1) cnt++;

        REP(i,n){
            if (((mask >> i) & 1) == 0) continue;
            REP(j,x[i].size()){
                if (((mask >> x[i][j]) & 1) != y[i][j]) f = false;
            }
        }
        if (f) chmax(ans, cnt);
    }
    cout << ans << endl;
    return 0;
}


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

#define M 1000000007
#define gb(co, k) ((co)>>(k)&1)

int main(){
   int n;
   cin>>n;
   vector<ll> a(n);
   rep(i, n) cin>>a[i];

   vector<vector<int>> sb(n);
   rep(i, n) sb[i]=vector<int>(60);

   rep(i, n){
      rep(j, 60) sb[i][j]=((i==0)?0:sb[i-1][j]) + gb(a[i], j);
   }
   int ans=0;
   for(int i=1; i<n; i++) rep(j, 60){
      int c;
      if(gb(a[i], j))
         c=((ll)(i-sb[i-1][j]) * (((ll)1<<j)%M))%M;
      else
         c=((ll)sb[i-1][j] * (((ll)1<<j)%M))%M;
      ans=(ans+c)%M;
   }

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

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

using namespace std;

const int MaxN = 100005;
const int Mod = 1000000007;

int n, D;
string s;
long long dp[MaxN], tmp[MaxN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    cin >> D;
    n = s.size();
    for (int i = s[0] - '0' - 1; i >= 0; i--)
    {
        dp[i % D]++;
    }
    int Sums = (s[0] - '0') % D;
    for (int i = 1; i < n; i++)
    {
        int x = s[i] - '0';
        for (int k = 0; k < D; k++)
        {
            tmp[k] = tmp[k] + dp[k];
            tmp[k + 10] = tmp[k + 10] - dp[k] + Mod;
        }
        tmp[Sums]++;
        tmp[Sums + x]--;
        Sums = (Sums + x) % D;
        dp[0] = tmp[0];
        tmp[0] = 0;
        for (int k = 1; k < D + 10; k++)
        {
            dp[k] = (tmp[k] + dp[k - 1]) % Mod;
            tmp[k] = 0;
        }
        for (int k = D; k < D + 10; k++)
        {
            dp[k % D] = (dp[k % D] + dp[k]) % Mod;
        }
    }
    long long ans = (dp[0] + (Sums % D == 0) - 1 + Mod) % Mod;
    cout << ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=205,up=60;
int n;
ll a[N],p[N];
char s[N];
void add(ll x)
{
    for(int i=up;i>=0;i--)
        if(x>>i&1)
        {
            if(!p[i]) {p[i]=x;break;}
            else x=x^p[i];
        }
}
bool query(ll x)
{
    for(int i=up;i>=0;i--)
        if(x>>i&1)
    {
        if(!p[i]) return true;
        else x=x^p[i];
    }
    return false;
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        scanf("%s",s+1);
        if(s[n]=='1')
        {
            printf("1\n");continue;
        }
        memset(p,0,sizeof(p));
        int ans=0;
        for(int i=n;i>=1;i--)
        if(s[i]=='0') add(a[i]);
        else if(query(a[i]))
        {
            ans=1;break;
        }
        printf("%d\n",ans);
    }
}

#include<bits/stdc++.h>
using namespace std;
int b[100000];
int main()
{
    int n;
    cin>>n;
    long long int a[n+5];
    for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
    sort(a,a+n);
    int j=0;
    for(int i=1;i<n;i++)
     {
         if(a[i]==a[i-1])
            {
                b[j]=a[i];
                i++;
                j++;
            }
     }
    if(j>=2)
    {
    long long int x=b[j-1],y=b[j-2];
    cout<<x*y<<endl;
    }
    else
    cout<<0<<endl;
}