#include <bits/stdc++.h>

#define MAXN 100005
#define lowbit(x) x & -x

using namespace std;

int n, N;
int a[MAXN][4], b[MAXN], c[MAXN], t[2][MAXN * 3], sum[2];

int read() {
    char c = getchar();
    int x = 0;
    while (!isdigit(c))
        c = getchar();
    while (isdigit(c)) {
        x = (x << 3) + (x << 1) + c - '0';
        c = getchar();
    }
    return x;
}

void add(int c, int x, int v) {
    while (x <= N) {
        t[c][x] += v;
        x += lowbit(x);
    }
}

int query(int c,int x) {
    int res = 0;
    while (x) {
        res += t[c][x];
        x -= lowbit(x);
    }
    return res;
}

int main() {
    n = read();
    N = n * 3;
    for (int j = 1; j <= 3; ++j)
        for (int i = 1; i <= n; ++i)
            a[i][j] = read();
    for (int i = 1; i <= n; ++i) {
        if (a[i][1] > a[i][2]) {
            sum[i & 1] ^= 1;
            swap(a[i][1], a[i][3]);
        }
        b[i] = a[i][3];
        if (a[i][1] != a[i][2] - 1 || a[i][2] != a[i][3] - 1 || b[i] % 3 != 0 || ((b[i] / 3) & 1) != (i & 1)) {
            puts("No");
            return 0;
        }
    }
    for (int i = n; i >= 1; --i) {
        sum[(i & 1) ^ 1] ^= query(i & 1, b[i] / 3) & 1;
        add(i & 1, b[i] / 3, 1);
    }
    if ((sum[0] & 1) || (sum[1] & 1))
        puts("No");
    else
        puts("Yes");
}

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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

struct FenwickTree{
	vector<ll> data;
	FenwickTree(int n):data(n+1){}
	void Add(int i,int x){
		for(i++;i<data.size();i+=i&-i)
			data[i]+=x;
	}
	ll Sum(int i){
		ll res=0;
		for(;i;i-=i&-i)
			res+=data[i];
		return res;
	}
	ll Sum(int i,int j){
		return Sum(j)-Sum(i);
	}
};

ll InversionNumber(const vi& a)
{
	FenwickTree ft(a.size());
	ll res=0;
	rep(i,a.size()){
		res+=ft.Sum(a[i],a.size());
		ft.Add(a[i],1);
	}
	return res;
}

bool test(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int n=src.size();
	rep(i,n){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):mt(z,y,x);
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):mt(z,y,x);
	}
	sort(all(src)); sort(all(dst));
	return src==dst;
}

tuple<bool,bool> calc(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int m=src.size(),flip=0;
	rep(i,m){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
	}
	map<tuple<int,int,int>,int> f;
	rep(i,m) f[dst[i]]=i;
	vi a(m);
	rep(i,m) a[i]=f[src[i]];
	return mt(InversionNumber(a)&1,flip&1);
}

bool solve(int n,vvi grid)
{
	array<bool,2> inv,flip;
	rep(i,2){
		vector<tuple<int,int,int>> src,dst;
		rep(j,n) if(j%2==i){
			if(j/2%2==0){
				src.emplace_back(mt(3*j+1,3*j+2,3*j+3));
				dst.emplace_back(mt(grid[0][j],grid[1][j],grid[2][j]));
			}
			else{
				src.emplace_back(mt(3*j+3,3*j+2,3*j+1));
				dst.emplace_back(mt(grid[2][j],grid[1][j],grid[0][j]));
			}
		}
		if(!test(src,dst)) return false;
		tie(inv[i],flip[i])=calc(src,dst);
	}
	return inv[0]==flip[1]&&inv[1]==flip[0];
}

int main()
{
	for(int n;cin>>n&&n;){
		vvi grid(3,vi(n));
		rep(i,3) rep(j,n) cin>>grid[i][j];
		cout<<(solve(n,grid)?"Yes":"No")<<endl;
	}
}


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

int main() {
    int N; cin >> N;
    cout << 0 << endl;
    string pres,s;
    int l,r,prei;
    l = N-1; r=0;
    cin >> pres;
    if(pres == "Vacant") return 0;
    else cout << N-1 << endl;
    cin >> s;
    if(s == "Vacant") return 0;
    else{
        cout << (l+r)/2 << endl;
    }
    prei = 0;
    for (int i = 0; i < 30; i++)
    {
        cin >> s;
        if(s == "Vacant") return 0;
        else if(s == pres){
            if(abs((l+r)/2-prei)%2 == 0){
                if(prei == r){
                    prei = (l+r)/2;
                    r = (l+r)/2;
                }else{
                    prei = (l+r)/2;
                    l = (l+r)/2;
                }
            }
            else{
                if(prei == r){
                    prei = (l+r)/2;
                    l = (l+r)/2;
                }else{
                    prei = (l+r)/2;
                    r = (l+r)/2;
                }
            }
        }
        else{
            if(abs((l+r)/2-prei)%2 == 0){
                if(prei == r){
                    prei = (l+r)/2;
                    l = (l+r)/2;
                }else{
                    prei = (l+r)/2;
                    r = (l+r)/2;
                }
            }
            else{
                if(prei == r){
                    prei = (l+r)/2;
                    r = (l+r)/2;
                }else{
                    prei = (l+r)/2;
                    l = (l+r)/2;
                }
            }
        }
        pres = s;
        cout << (l+r)/2 << endl;
    }
    return -1;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.141592653589793
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rrep(i, n, k) for (int i = (n); i >= (k); i--);
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}
const int MOD = 1e9 + 7;
const int INF = 2e18;

signed main() {
    int N;
    cin >> N;
    vector<int> A(N);
    cin >> A;
    {
        bool check = (A[0] == 0);
        for (int i = 1; i < N - 1; i++) {
            if (A[i] > i) check = false;
        }
        if (!check) {
            cout << -1 << endl;
            return 0;
        }
    }
    int ans = 0;
    for (int i = N - 1; i >= 1;) {
        ans += A[i];
        int count = 0;
        for (int j = i - 1; j >= 0; j--) {
            if (A[j] != A[i] - ++count) {
                if (A[j] < A[i] - count) {
                    cout << -1 << endl;
                    return 0;
                }
                i = j;
                break;
            }
            if (j == 0) i = 0;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define fastio cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(0)
typedef long long ll;
#define int ll
#define swap(x, y)((x)^=(y)^=(x)^=(y))
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> matrix;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(c) c.begin(),c.end()
#define mp make_pair
#define pb push_back
#define f first
#define tcase int _; cin>> _; for( ; _; --_)
#define s second
#define endl '\n'
const int MAX = 1e6+1;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
const double DEG = 180/PI;
const int MOD = 1e9+7;
int mod(int x, int m){ if(x >= 0) return x % m; if(-x < m) return m-(-x); return mod(x%m, m);}
int gcd (int a, int b) {while(b){a = mod(a, b); swap(a, b);} return a;}
int lcm (int a, int b) {return a / gcd(a, b) * b;}
int fexp(int base, int exp, int m){
	int total = 1;
	while(exp > 0){
		if(exp % 2 == 1){
			total = mod(((mod(total, m)) * (mod(base, m))), m);
			exp--;
		}
		base= mod((mod(base, m) * mod(base, m)), m);
		exp /= 2;
	}
	return mod(total, m);
}

int n;

void solve(){
	cin>> n;

	vector<ll> cnt(60, 0);

	for(int i = 0; i<n; ++i){
		ll x;
		cin>> x;

		for(int j = 1, k = 0; j <= x; j<<=1LL, ++k) if(j & x) cnt[k]++;
	}

	ll ans = 0LL;

	for(int i = 0; i<60; ++i){
		ll temp = mod(cnt[i] * (n - cnt[i]), MOD);
		ans = mod(ans + mod(temp * mod(1LL << i, MOD), MOD), MOD);
	}
	cout<< ans << endl;
}

int32_t main(){
	fastio;
	solve();
	return 0;
}


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

long long ara[3][4007];

int main()
{

        long long n,m,a,b,c,d,e,f,g,h,p,q,r,fr,sc,tr,sz,tz,i,j,k,mx=LLONG_MIN,mn=LLONG_MAX;
        long long x,y=0,z=0,cnt=0,res=0,ttl=0,ans=0,sum=0; long long flg=0,flag=1,na=0,as=1;
        vector<long long>u,v,w; vector< pair<long long,long long> >vct; vector<string>vst;
        set<long long>st,nt,tt; map<long long,long long>mp,nq,qr; string str,ttr,ntr;

        scanf("%lld",&n);
        cin>>str;

        if(n<=3)printf("0\n");
        else
        {

            for(i=0;i<n;i++)
            {
                if(str[i]=='R')cnt++;
                else if(str[i]=='G')y++;
                else{  z++; }

                ara[0][i+1]=cnt; ara[1][i+1]=y; ara[2][i+1]=z;
            }

            char ch,th,rh;

            for(i=0;i<n-2;i++)
            {
                ch=str[i];

                for(j=i+1;j<n-1;j++)
                {
                    if(str[j]!=ch)
                    {
                        th=str[j];

                        if(ch!='R' && th!='R')x=0;
                        else if(ch!='G' && th!='G')x=1;
                        else{ x=2;  }

                        fr=ara[x][n]-ara[x][j+1-1];

                        p=j-i; h=j+p;

                        if(h<=n-1)
                        {
                            if(x==2)rh='B';
                            else if(x==1)rh='G';
                            else{ rh='R';  }

                            if(str[h]==rh)fr--;

                        }

                        ans=ans+fr;

                    }

                }

            }

            printf("%lld\n",ans);

        }

    return 0;

}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

void solve() {
    int n;
    cin >> n;
    ll a[n];
    rep(i, n) {
        cin >> a[i];
    }
    string s;
    cin >> s;
    reverse(a, a + n);
    reverse(ALL(s));
    vector<ll> bs;
    rep(i, n) {
        for (auto b : bs) {
            a[i] = min(a[i], a[i] ^ b);
        }
        if (s[i] == '0') {
            if (a[i] != 0) bs.emplace_back(a[i]);
        } else {
            if (a[i]) {
                cout << 1 << endl;
                return;
            }
        }
    }
    cout << 0 << endl;
}

signed main() {
    int tt;
    cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

const int N = 2010, M = 4e6+10;
int n, k, dp[N][N];
int fac[M], ifac[M];
int C(int n, int m) {
	if (n<m) return 0;
	return (ll)fac[n]*ifac[m]%P*ifac[n-m]%P;
}
int main() {
	fac[0]=1;
	REP(i,1,M-1) fac[i]=(ll)fac[i-1]*i%P;
	ifac[M-1]=inv(fac[M-1]);
	PER(i,0,M-2) ifac[i]=(ll)ifac[i+1]*(i+1)%P;
	scanf("%d%d", &n, &k);
	if (k==1) return puts("1"),0;
	dp[0][0] = 1;
	REP(i,0,n) REP(j,0,i) {
		dp[i+1][j] = (dp[i+1][j]+dp[i][j])%P;
		dp[i][j+1] = (dp[i][j+1]+(ll)C(n*k-j*(k-1)-i-1,k-2)*dp[i][j])%P;
	}
	int ans = (ll)dp[n][n]*fac[n]%P;
	printf("%d\n", ans);
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_X 200005

const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long FINV(int n){
    if (n < 0) return 0;
    return finv[n];
}

ll dp[MAX_X];

int main() {
    int n, x;
    cin >> n >> x;
    int a[n];
    rep(i,n)cin >> a[i];
    sort(a,a+n);

    COMinit();

    int ite = lower_bound(a,a+n,x+1) - a;
    ite = n - ite;

    dp[x] = COM(n, ite) * fac[ite] % MOD;

    drep(i,MAX_X){
        if(dp[i] == 0)continue;
        if(i < a[0])break;
        drep(j,n){
            if(a[j] > i)continue;
            int after = i % a[j];
            int nn = 0;
            int kk = 0;
            rep(k,n){
                if(a[k] <= i){
                    nn++;
                    if(after < a[k])kk++;
                }
            }
            nn--;
            kk--;
            // cout << i << ' ' << a[j] << ' ' << after << ' ' << kk << ' ' << dp[i] * COM(nn, kk) << endl;
            dp[after] += dp[i] * COM(nn, kk) % MOD * fac[kk];
            dp[after] %= MOD;
        }
    }

    ll ans = 0;
    rep(i,a[0]){
        ans += dp[i] * i;
        ans %= MOD;
    }

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


#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>
using namespace std;

int main(){
	long long e;
	while ((cin >> e), e){
		long long  x,y, z, min = 1000000;
		for (z = 0; z*z*z <= e; z++){
			for (y = 0; y*y <= e; y++){
				x = e - z*z*z - y*y;
				if (x >= 0 && min > x + y + z)min = x + y + z;
			}
		}
		cout << min << endl;
	}
}


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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 1; i < n; ++i) {
    if (s[i - 1] == 'A' && s[i] == 'C') {
      cout << "Yes" << '\n';
      return 0;
    }
  }
  puts("No");
  return 0;
}

#include <iostream>
using namespace std;

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

	for (int i=1; i<=n; i=i+1) {
		int x = i;
		if (x % 3 == 0) {
			cout << " " << i;
	 	} else {
			while (x % 10 != 3 && x > 0) {
				x = x / 10;
			}
			if (x % 10 == 3)
				cout << " " << i;
		}
	}
	cout << endl;

	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int d,ans,p[25];
int Abs(int x)
{
	return x<0?-x:x;
}
signed main()
{
	cin>>d;
	p[0]=1LL;
	for(int i=1;i<=20;i++) p[i]=p[i-1]*10LL;
	for(int i=2;i<=20;i++)
	{
		int k=i/2;
		for(int s=0;s<(1<<k);s++)
		{
			int w,x=d;
			if(i&1) w=10;
			else w=1;
			for(int j=0;j<k;j++)
			{
				int d2=Abs(x)%10LL;
				if(x<0) d2=(10-d2)%10;
				if(s&(1<<j))
				{
					int n=10LL-d2;
					if(j==0) n--;
					w*=n;
					x+=d2*p[i-2*j-1];
					x-=d2;
				}
				else
				{
					int n=d2;
					if(j==0) n--;
					if(n<0)
					{
						w=0;
						break;
					}
					w*=n;
					x-=(10LL-d2)*p[i-2*j-1];
					x+=10LL-d2;
				}
				x/=10LL;
			}
			if(!x) ans+=w;
		}
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define INF 1 << 30
#define LLINF 1LL << 60
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define DOWN(i,b,a) for(int i=b;i>=a;i--)
#define SET(a,c) memset(a,c,sizeof a)
#define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FOREACH(i,c) for(auto (i) : (c))
#define BIT(i,j) ((i)>>(j))&1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
#define SQ(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll,ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i]<<((i==a.size()-1)?"\n":" ");}}
ll gcd(ll a,ll b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
ll lcm(ll a,ll b){return a==0||b==0?0:a*b/gcd(a,b);}

int a[144];

void flip(int x, int y)
{
  if(x>11||y>11||x<0||y<0) return;
  if(a[x+y*12])
  {
    a[x+y*12] = 0;
    flip(x+1,y);
    flip(x-1,y);
    flip(x,y+1);
    flip(x,y-1);
  }
}

void solve()
{
  int cnt = 0;
  REP(i,144) if(a[i])
  {
    cnt++;
    flip(i%12,i/12);
  }
  cout << cnt << endl;
}

int main()
{
  string s;
  int i = 0;
  while(cin >> s)
  {
    REP(j,12) a[i*12+j] = s[j]-'0';
    i=(++i)%12;
    if(i==0) solve();
  }
  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

const long long MOD = 1000000007;
long long N;
long long a[100000] = {0};

int main() {
  cin >> N;
  long long ans = 1;
  rep(i, N) {
    long long A;
    cin >> A;
    long long temp;
    if(A == 0) {
      temp = 3 - a[A];
    } else {
      temp = a[A-1] - a[A];
    }
    ans =  ans * temp % MOD;
    a[A]++;
  }
  if(ans <= 0) cout << 0 << endl;
  else cout << ans << endl;
  return 0;
}

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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;

int main(){
   int n;
   cin >> n;
   vector<ll>v(100000);
   vector<int>a(n);
   rep(i, 0, n)cin >> a[i];
   ll ans = 1;
   rep(i, 0, n){
       ll p = a[i];
       if(p==0){
           v[p]++;
           ans *= (4-v[p]);
           ans %= mod;
       }else{
           v[p]++;
           ans *= min(v[p-1],v[p-1]+1-v[p]);
           ans %= mod;
       }
       //cout << ans << endl;
   }
   cout << ans << endl;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ss(x) (int) x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, l, r) for (int i = r; l <= i; --i)

using ll = long long;
using namespace std;

string s;
int cnt[26];

int main() {
	cin >> s;
	for (auto x : s)
		cnt[x - 'a']++;
	ll res = 0;
	rep(i, 0, 25)
		rep(j, i + 1, 25)
			res += 1LL * cnt[i] * cnt[j];
	cout << res + 1 << endl;
	return 0;
}

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

int main() {
  string S;
  cin>>S;
  vector<int64_t> A(26,0);
  int64_t a=-97,b=S.size();
  rep(i,b){
    int64_t c=S.at(i);
    A.at(a+c)++;
  }
  int64_t ans=b*(b-1)/2;
  rep(i,26){
    int64_t d=A.at(i);
    if(d>1) ans-=d*(d-1)/2;
  }
  cout<<ans+1;
}


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

int main(){
  char s[3];
  cin>>s[0]>>s[1]>>s[2];
  int max=0, locmax=0;
  for(int i=0;i<3;i++){
    if(s[i] == 'R'){
      locmax++;
    }
    else{
      locmax=0;
    }
    if(locmax>max){
      max=locmax;
    }
  }
  cout<<max;
}

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int a, b, c, k, x, y, z;
    cin >> a >> b >> c >> k;
    if(a >= b && a >= c)
    {x = a; y = b; z = c;}
    if(b > a && b >= c)
    {x = b; y = c; z = a;}
    else if(c > a && c > b)
    {x = c; y = a; z = b;}
    while(k > 0)
    {
        k--;
        x = x * 2;
    }
    cout << y + z + x;
    return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll stoi9(string s) {
    // 10 ^ 9倍した整数を返す
    int digit = 0;
    for (int i = sz(s)-1; i >= 0; i--) {
        if (s[i] == '.') {
            s.replace(i, 1, "");
            break;
        }
        digit++;
        if (i == 0) digit = 0;
    }
    ll x = stoll(s);
    rep(i, 9-digit) x *= 10;
    return x;
}

int table[20][20];
ll cum[20][20];

int main() {
    int N;
    cin >> N;
    ll ans = 0;
    rep(i, N) {
        string S;
        cin >> S;
        ll x = stoi9(S);
        int two = 0, five = 0; // 2と5で何回割れるか
        while (x%2 == 0 && two < 18) {
            two++;
            x /= 2;
        }
        while (x%5 == 0 && five < 18) {
            five++;
            x /= 5;
        }
        table[two][five]++;
        if (two >= 9 && five >= 9) ans--;
    }
    for (int i = 18; i >= 0; i--) {
        for (int j = 18; j >= 0; j--) {
            cum[i][j] = table[i][j] + cum[i+1][j] + cum[i][j+1] - cum[i+1][j+1];
        }
    }
    for (int i = 0; i < 19; i++) {
        for (int j = 0; j < 19; j++) {
            ans += table[i][j] * cum[18-i][18-j];
        }
    }
    ans /= 2;
    cout << ans << '\n';
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int n;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  vector<long long> a(n);
  const int b = 200;
  for(int i = 0; i < n; i++) {
    double p;
    cin >> p;
    a[i] = p * 1000000000 + 0.5;
  }
  vector<vector<int>> cnt(b, vector<int>(b));
  long long res = 0;
  for(int i = 0; i < n; i++) {
    int two = 0, five = 0;
    while(a[i] % 2 == 0) {
      two++;
      a[i] /= 2;
    }
    while(a[i] % 5 == 0) {
      five++;
      a[i] /= 5;
    }
    res += cnt[18 + b / 2 - two][18 + b / 2 - five];
    for(int u = -30; u <= two; u++) {
      for(int v = -30; v <= five; v++) {
        cnt[b / 2 + u][b / 2 + v]++;
      }
    }
  }
  cout << res;
  return 0;
}


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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n;
  cin >> n;
  vector<int64> a(n);
  REP(i,n)cin >> a[i];
  int64 s = 0;
  REP(i,n)s ^= a[i];
  REP(i,n)a[i] &= ~s;
  // REP(i,n){
  //   cout << bitset<60>(a[i]) << endl;
  // }
  int32 r = 0;
  FORR(d,61,0){
    int32 i;
    for(i = r; i < n; ++i){
      if((a[i]>>d)&1){
        swap(a[r],a[i]);
        break;
      }
    }
    if(i == n)continue;
    REP(j,n){
      if(j == r)continue;
      if((a[j]>>d)&1)a[j] ^= a[r];
    }
    r++;
  }
  // REP(i,n){
  //   cout << bitset<60>(a[i]) << endl;
  // }
  int64 ad = 0;
  REP(i,n)ad ^= a[i];
  ANS(s + (ad << 1));
  return 0;
}

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}

int main(){
    INT(h,w);
    vector2d(int,table,h,w);
    vector<tuple<int,int,int,int>> ope;
    in(table);
    rep(y,h){
        rep(x,w){
            table[y][x]%=2;
            if(y<h-1&&table[y][x]==1){
                table[y+1][x]++;
                ope.push_back(make_tuple(y+1,x+1,y+2,x+1));
            }
            if(y==h-1&&x+1<w&&table[y][x]==1){
                table[y][x+1]++;
                ope.push_back(make_tuple(y+1,x+1,y+1,x+2));
            }
        }
    }
    out(ope.size());
    rep(i,ope.size())out(get<0>(ope[i]),get<1>(ope[i]),get<2>(ope[i]),get<3>(ope[i]));
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector< vector<int> > VVI;
typedef vector< vector<ll> > VVL;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main(){
  int N,M;
  cin>>N>>M;
  VI A(N-1+M),B(N-1+M);
  rep(i,N-1+M) cin>>A[i]>>B[i];
  VVI X(N,VI(0));
  VI I(N);
  rep(i,N-1+M){
    X[A[i]-1].pb(B[i]-1);
    I[B[i]-1]++;
  }
  int S=0;
  rep(i,N) if(I[i]==0) S=i;
  VI T(0);
  T.pb(S);
  queue<int> Q;
  Q.push(S);
  VI F(N);
  F[S]++;
  while(!Q.empty()){
    int now=Q.front();
    Q.pop();
    rep(i,X[now].size()){
      int nxt=X[now][i];
      if(!F[nxt]){
        I[nxt]--;
        if(!I[nxt]){
          Q.push(nxt);
          T.pb(nxt);
        }
      }
    }
  }
  VI Z(N);
  rep(i,N){
    int now=T[i];
    if(!Z[now]) Z[now]=1;
    rep(j,X[now].size()){
      int nxt=X[now][j];
      Z[nxt]=max(Z[nxt],Z[now]+1);
    }
  }
  VI ans(N);
  rep(i,N){
    rep(j,X[i].size()){
      int nxt=X[i][j];
      if(Z[i]+1==Z[nxt]) ans[nxt]=i+1;
    }
  }
  ans[S]=0;
  rep(i,N) cout<<ans[i]<<endl;
}

#include <bits/stdc++.h>
using namespace std;
int main(){//priority_queue?
	int n, m;
	cin >> n >> m;
	vector<vector<int>> elist(n), rlist(n);
	vector<int> indeg(n);
	for(int i=0; i<n-1+m; ++i){
		int s, t;
		cin >> s >> t;
        --s; --t;
		elist[s].push_back(t);
		rlist[t].push_back(s);
		++indeg[t];
	}
	queue<int> zeroq;
	for(int i=0; i<n; ++i){
		if(indeg[i]==0) zeroq.push(i);
	}
	vector<int> result, par(n,0);
    while(!zeroq.empty()){
		int u=zeroq.front();
		zeroq.pop();

		result.push_back(u);
        for(int i : elist[u]){
			--indeg[i];
			if(indeg[i]==0){
				zeroq.push(i);
				par[i]=u+1;
			}
		}
	}

	for(int i : par) cout << i << endl;
}


#include<iostream>
#include<set>

using namespace std;

int main(){
    int K, N, i=0;
    int input, output;
    cin >> N >> K;

    set<int> num;

    while (i < K){
        cin >> input;
        num.insert(input);
        i++;
    }

    int a1, a2=10, a3=10, a4=10, a5=10;
    output = N;
    while (1){
        if (output >= 10000){a5 = output / 10000;}
        if (output >= 1000){a4 = (output % 10000) / 1000;}
        if (output >= 100){a3 = (output % 1000) / 100;}
        if (output >= 10){a2 = (output % 100) / 10;}
        a1 = (output % 10);
        if (num.find(a5) == num.end() && num.find(a4) == num.end() && num.find(a3) == num.end() && num.find(a2) == num.end() && num.find(a1) == num.end()){
            break;
        }
        output += 1;
    }

    cout << output << endl;
    return 0;
}

#include <bits/stdc++.h>
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1000000000
using namespace std;
typedef long long ll;

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

    vector<int> X(N);
    int res = 0;
    REP(i, N){
        if (S[i] == 'W'){
            res++;
        }
        X[i] += res;
    }
    res = 0;
    REPD(i, N){
        if (S[i] == 'E'){
            res++;
        }
        X[i] += res;
    }

    int ans = MIN(X);

    cout << ans-1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int MAXN = 1e4 + 10;
ll dp[MAXN][128], num[MAXN];
char s[MAXN];
int len, D;

ll dfs(int pos, int tot, bool lim) {
	if (pos == len + 1) return dp[pos][tot] = (tot == 0);
	if (!lim && dp[pos][tot] != -1) return dp[pos][tot];
	int up = lim ? num[pos] : 9;
	ll ret = 0;
	for (int i = 0; i <= up; i++) {
		ret += dfs(pos + 1, (tot + i) % D, lim && (i == up));
		ret %= mod;
	}
	if (!lim) dp[pos][tot] = ret;
	return ret;
}

int main() {
	memset(dp, -1, sizeof(dp));
	scanf("%s%d", s + 1, &D);
	len = strlen(s + 1);
	for (int i = 1; i <= len; i++) num[i] = s[i] - '0';
	ll ans = (dfs(1, 0, 1) - 1 + mod) % mod;
// minus one to deduct the zero
	cout << ans << endl;
	return 0;
}

#include<bits/stdc++.h>

const int MOD = 1e9 + 7;

using namespace std;

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

  int D;
  cin >> D;

  vector<vector<vector<long long>>> dp(K.size() + 1, vector<vector<long long>>(D, vector<long long>(2, 0)));
  dp[0][0][1] = 1;
  for(int i = 0; i < (int)K.size(); i++){
    for(int j = 0; j < D; j++){
      for(int k = 0; k < 10; k++){
        dp[i + 1][(j + k) % D][0] += dp[i][j][0];
        if(k < K[i] - '0'){
          dp[i + 1][(j + k) % D][0] += dp[i][j][1];
        }else if(k == K[i] - '0'){
          dp[i + 1][(j + k) % D][1] += dp[i][j][1];
        }
        dp[i + 1][(j + k) % D][0] %= MOD;
        dp[i + 1][(j + k) % D][1] %= MOD;
      }
    }
  }

  cout << ((dp[K.size()][0][0] + dp[K.size()][0][1] - 1) % MOD + MOD) % MOD << endl;

  return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;

LL n,d;
LL col[2][500005] = {0};
LL v[4][500005],cnt[4] = {0};

LL id(LL x,LL y){ return x * n + y + 1; } // (x,y) -> id
LL getx(LL x){ return (x - 1) / n; } // id(x,y) -> x
LL gety(LL y){ return (y - 1) % n; } // id(x,y) -> y

void dfs(LL u,LL idd){
	LL tx = getx(u),ty = gety(u);
	for(LL dy,dx = 0;dx <= n;dx ++){
		if(dx * dx > d) continue;
		dy = sqrt(d - dx * dx);
		if(dx * dx + dy * dy != d) continue;

		if(tx + dx < n && ty + dy < n)
			if(col[idd][id(tx + dx,ty + dy)] == -1) { col[idd][id(tx + dx,ty + dy)] = 1 - col[idd][u]; dfs(id(tx + dx,ty + dy),idd); }
		if(tx + dx < n && ty - dy >= 0)
			if(col[idd][id(tx + dx,ty - dy)] == -1) { col[idd][id(tx + dx,ty - dy)] = 1 - col[idd][u]; dfs(id(tx + dx,ty - dy),idd); }
		if(tx - dx >= 0 && ty + dy < n)
			if(col[idd][id(tx - dx,ty + dy)] == -1) { col[idd][id(tx - dx,ty + dy)] = 1 - col[idd][u]; dfs(id(tx - dx,ty + dy),idd); }
		if(tx - dx >= 0 && ty - dy >= 0)
			if(col[idd][id(tx - dx,ty - dy)] == -1) { col[idd][id(tx - dx,ty - dy)] = 1 - col[idd][u]; dfs(id(tx - dx,ty - dy),idd); }
	}
}

int main(){
	memset(col,-1,sizeof(col));
	cin >> n; n <<= 1;
	cin >> d;
	for(LL i = 0;i < n;i ++)
		for(LL j = 0;j < n;j ++)
			if(col[0][id(i,j)] == -1){ col[0][id(i,j)] = 1; dfs(id(i,j),0); }
	cin >> d;
	for(LL i = 0;i < n;i ++)
		for(LL j = 0;j < n;j ++)
			if(col[1][id(i,j)] == -1){ col[1][id(i,j)] = 1; dfs(id(i,j),1); }
	/*
	for(LL i = 0;i < n;i ++)
		for(LL j = 0;j < n;j ++)
			cout << col[0][id(i,j)] << (j == n - 1 ? '\n' : ' ');
	for(LL i = 0;i < n;i ++)
		for(LL j = 0;j < n;j ++)
			cout << col[1][id(i,j)] << (j == n - 1 ? '\n' : ' ');
	*/
	for(LL i = 1;i <= id(n - 1,n - 1);i ++){
		LL co = col[0][i] + col[1][i] * 2;
		v[co][++ cnt[co]] = i;
	}
	for(LL i = 0;i < 4;i ++){
		if(cnt[i] >= n * n / 4){
			for(LL j = 1;j <= n * n / 4;j ++) cout << getx(v[i][j]) << ' ' << gety(v[i][j]) << '\n';
			break;
		}
	}
	return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

#define int long long
typedef long double ld;

const ld PI = acos(-1);

struct Point {
	int x, y;

	Point() {}

	Point(int x_, int y_) {
		x = x_;
		y = y_;
	}

	Point operator-(const Point &other) const {
		return Point(x - other.x, y - other.y);
	}

	bool operator<(const Point &other) const {
		return make_pair(x, y) < make_pair(other.x, other.y);
	}

	int operator*(const Point &other) const {
		return x * other.y - y * other.x;
	}

	int operator%(const Point &other) const {
		return x * other.x + y * other.y;
	}

	int dist(const Point &other) const {
		int dx = x - other.x;
		int dy = y - other.y;
		return dx * dx + dy * dy;
	}
};

ld ag(const Point &a, const Point &b, const Point &c) {
	Point f = (a - b);
	Point s = (c - b);
	ld ans = atan2(f * s, f % s);
	if (ans < 0) ans += PI;
	return ans;
}

vector<Point> convex_hull(vector<Point> a) {
	int mn = min_element(a.begin(), a.end()) - a.begin();
	Point to = a[mn];
	a.erase(a.begin() + mn);
	auto cmp = [&](const Point &f, const Point &s) {
		Point ff = (f - to);
		Point ss = (s - to);
		if (ff * ss != 0) {
			return ff * ss > 0;
		} else {
			return to.dist(f) < to.dist(s);
		}
	};
	auto ok_ang = [&](const Point &f, const Point &s, const Point &t) {
		Point ff = (s - f);
		Point ss = (t - s);
		return ff * ss > 0;
	};
	sort(a.begin(), a.end(), cmp);
	vector<Point> st = {to};
	for (auto t : a) {
		while (st.size() > 1 && !ok_ang(st.rbegin()[1], st.rbegin()[0], t)) {
			st.pop_back();
		}
		st.push_back(t);
	}
	return st;
}

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

	int n;
	cin >> n;
	vector<Point> a(n);
	vector<ld> ans(n);
	map<Point, int> ind;
	for (int i = 0; i < n; i++) {
		cin >> a[i].x >> a[i].y;
		ind[a[i]] = i;
	}
	a = convex_hull(a);
	n = (int)a.size();
	for (int i = 0; i < n; i++) {
		ld c = ag(a[(i - 1 + n) % n], a[i], a[(i + 1) % n]);
		if (n == 2) c = PI;
		ans[ind[a[i]]] = c / (2 * PI);
	}
	cout << fixed << setprecision(16);
	for (auto t : ans) {
		cout << t << '\n';
	}
}


#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define put(x) cout << x << endl;
using namespace std;
int main()
{
    ll x;
    cin >> x;
    put(1000 * (x / 500) + 5 * ((x % 500) / 5))
    return 0;
}

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

int main() {
    string s;
    cin>>s;
    int minv=1000000005;
    size_t l=s.length();
    rep(i,l-2){
        string ss=s.substr(i,3);
        int num=stoi(ss);
        minv=min(minv,abs(num-753));
    }
    cout<<minv<<"\n";
    return 0;
}


#include <map>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
typedef long long ll;

int n;
char s[40];
std::map<std::pair<std::string,std::string>,int> C;

int main()
{
    scanf("%d%s",&n,s);
    std::reverse(s+n,s+(n<<1));
    for(int i=0;i<(1<<n);++i)
    {
        std::string a,b;
        for(int j=0;j<n;++j)
            if(i>>j&1)a+=s[j];
            else b+=s[j];
        ++C[std::make_pair(a,b)];
    }
    ll Ans=0;
    for(int i=0;i<(1<<n);++i)
    {
        std::string a,b;
        for(int j=0;j<n;++j)
            if(i>>j&1)a+=s[n+j];
            else b+=s[n+j];
        Ans+=C[std::make_pair(a,b)];
    }
    printf("%lld\n",Ans);
    return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#include <functional>
using namespace std;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define MOD (long long int)(1e15+7)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

ll mpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
  else return mpow(a,b-1) * a % MOD;
}
ll lpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;}
  else return lpow(a,b-1) * a;
}
ll gcd(ll a, ll b){
  if(b==0) return a;
  else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
  if(kaijo_memo.size() > n) return kaijo_memo[n];
  if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
  while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n){
  if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n];
  if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1);
  while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r){
  if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く？不安.
  if(n < r || r < 0) return 0;
  ll ret = 1;
  if(n <= 1e7){
    ret *= kaijo(n); ret %= MOD;
    ret *= gyaku_kaijo(r); ret %= MOD;
    ret *= gyaku_kaijo(n-r); ret %= MOD;
  }else{
    rep(i,r){
      ret *= n-i; ret %= MOD;
      ret *= mpow(r-i, MOD-2); ret %= MOD;
    }
  }
  return ret;
}

int main(void){
  fast_io
  cout<<fixed<<setprecision(15);

  ll n;cin>>n;
  string s="", t="", inp; cin>>inp;
  rep(i,n){
    s += inp[i];
    t += inp[2*n-1-i];
  }

  ll ans = 0;
  rep(bit, 1<<n){
    bitset<18> bs(bit);
    string x = "", y = "";
    rep(i,n){
      if(bs[i]){
        x += s[i];
      }else{
        y += s[i];
      }
    }
    rep(i,n){
      x += "X";
      y += "X";
    }
    ll dp[20][20] = {};
    dp[0][0] = 1;
    rep(i,n){
      rep(j,n+1){
        if(x[j] == t[i]){
          dp[i+1][j+1] += dp[i][j];
          dp[i+1][j+1] %= MOD;
        }
        if(i-j < 0) continue;
        if(y[(i-j)] == t[i]){
          dp[i+1][j] += dp[i][j];
          dp[i+1][j] %= MOD;
        }
      }
    }
    ans += dp[n][bs.count()];
    /*if(dp[n][bs.count()] > 0 bit == 9){
      rep(i,n){
        cout<<bs[i];
      }
      cout<<endl;
      cout<<x<<" "<<y<<endl;

      cout<<dp[2][0]<<" "<<x[0]<<" "<<t[2]<<endl;
    }*/
    ans %= MOD;
  }
  cout<<ans<<endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
pair<ll,int> p[100005];
int main(){
	ll n,x; cin >> n >> x;
	vector<ll> b(n),l(n),u(n);
	for(int i=0;i<n;i++){
		cin >> b[i] >> l[i] >> u[i];
	}
	ll sum=0;
	for(int i=0;i<n;i++){
		p[i]=pair<ll,int>((x-b[i])*u[i]+b[i]*l[i],i);
		sum-=b[i]*l[i];
	}
	sort(p,p+n);
	ll ret=0;//満点を取る教科を数える
	int last=0;
	for(int i=n-1;i>=0;i--){
		if(sum+p[i].first>=0){
			ret+=(ll)(n-i-1)*(ll)x;//満点を取る教科はn-i-1個である
			last=i+1;
			break;
		}
		sum+=p[i].first;
	}
	ll Min=x;//中途半端に解く科目の得点の最小値
	sum=-sum;
	for(int i=0;i<last;i++){
		int v=p[i].second;
		Min=min(Min,(sum+l[v]-1)/l[v]);//中途半端に解く科目に最小の重みをつけた場合
		ll nsum=sum+b[v]*(u[v]-l[v]);//最大の重みをつける場合には更にディスアドバンテージが広がる
		Min=min(Min,(nsum+u[v]-1)/u[v]);
	}
	//貪欲に見ると満点を取る科目になる科目のうち中途半端に解くものを探索する場合、
	//満点を取る科目としてi=last-1も追加される
	for(int i=last;i<n;i++){
		int v=p[i].second;
		ll nsum=sum+p[i].first-p[last-1].first;
		Min=min(Min,(nsum+l[v]-1)/l[v]);
		ll nsum2=nsum+b[v]*(u[v]-l[v]);
		Min=min(Min,(nsum2+u[v]-1)/u[v]);
	}
	cout << ret+Min << endl;
}

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

#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long N, std::vector<long long> X, std::vector<long long> Y, std::vector<int> U){
    const int MAX_LEN = 2e5;
    const int OFFSET = 2e5;
    int answer = INT_MAX;
    rep(u,0,4) {
        vector<set<int>> lineL(MAX_LEN+1);  // lineL[y when x is 0] = all x whose direction is u+2
        vector<set<int>> lineD(2*MAX_LEN+1); // lineD[y when x is 0] = all x whose direction is u+1
        rep(i,0,N) {
            if (U[i] == (u+2) % 4) {
                lineL[Y[i]].insert(X[i]);
            } else if (U[i] == (u+1) % 4) {
                lineD[OFFSET + Y[i]-X[i]].insert(X[i]);
            }
        }
        rep(i,0,N) {
            if (U[i] != u) continue;
            // lineL
            auto itr = lineL[Y[i]].lower_bound(X[i]);
            if (itr != lineL[Y[i]].end()) {
                answer = min<int>(answer, (*itr - X[i]) * 5);
            }
            // lineD
            itr = lineD[OFFSET + Y[i]-X[i]].lower_bound(X[i]);
            if (itr != lineD[OFFSET + Y[i]-X[i]].end()) {
                answer = min<int>(answer, (*itr - X[i]) * 10);
            }
        }
        // rotate to left
        rep(i,0,N) {
            ll x = MAX_LEN - Y[i], y = X[i];
            X[i] = x;
            Y[i] = y;
        }
    }
    cout << (answer == INT_MAX ? "SAFE" : to_string(answer)) << endl;
}

signed main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    long long N;
    scanf("%lld",&N);
    std::vector<long long> X(N);
    std::vector<long long> Y(N);
    std::vector<int> U(N);
    unordered_map<char, int> direction = {{'R', 0}, {'D', 1}, {'L', 2}, {'U', 3}};
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&X[i]);
        scanf("%lld",&Y[i]);
        char c;
        std::cin >> c;
        U[i] = direction[c];
    }
    solve(N, std::move(X), std::move(Y), std::move(U));
    return 0;
}


#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/integer/common_factor.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using std::string;
using std::vector;
using std::set;
using std::multiset;
using std::unordered_set;
using std::map;
using std::multimap;
using std::unordered_map;
using std::pair;
using std::cin;
using std::cout;

using boost::multiprecision::cpp_int;
using cpp_dec_float_1000 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000, boost::int128_type>>;

typedef uintmax_t    ull;
typedef intmax_t     ll;
typedef uint64_t     ul;
typedef uint32_t     ui;
typedef uint8_t      uc;

constexpr char CRLF  = '\n';
constexpr char SPACE = ' ';
constexpr char VECTOR_COUT_SEPARATOR = SPACE;
constexpr ll INF = 1000'000'007;
constexpr int MOD = 1000'000'007;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& vc) { for(auto it = vc.begin(); it != vc.end(); ++it) { if (std::next(it) == vc.end()) os << *it; else os << *it << VECTOR_COUT_SEPARATOR; } return os; }
template<class T1, class T2> inline std::ostream & operator<< (std::ostream & os, const std::pair<T1, T2> & p) { return os << p.first << ' ' << p.second; }
template<class T> T modinv(T a, T m) { T b = m, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; boost::swap(a, b); u -= t * v; boost::swap(u, v); } u %= m; if (u < 0) u += m; return u; }
template<class T> inline bool isSosuu(const T& n) { if (n == T(1)) { return false; } for (T i = 2; i*i <= n; ++i) { if (n % i == 0) { return false; } } return true; }
template<class T> vector<vector<T>> split (const vector<T>& S, T delim) { vector<vector<T>> ret{}; vector<T> buff{}; for (auto c : S) { if (c == delim) { if (!buff.empty()) ret.push_back(buff); buff.clear(); } else { buff.push_back(c); } } if (!buff.empty()) ret.push_back(buff); return ret; }
template<class T> inline void printYesNo(const T& t) { cout << (t ? "Yes" : "No") << CRLF; }
template<class T = int64_t> T modpow(T N, T P, T M) { T ret{1}; N %= M; while (P) { if (P & 0x01) { ret *= N; ret %= M; } P >>= 1; N *= N; N %= M; } return ret; }

/** code for assert debug begin */
int gErr = 0;
#define ASSERT(p) if(!(p)){ gErr = 1; }else{ ; }
/** code for assert debug end */

struct Tmp
{
public:
  int N;
  Tmp(int N)
  : N(N)
  {}
};

void solve(void)
{
  int N; cin >> N;
  int d; cin >> d;
  if (d) {
    cout << 0 << CRLF;
    return;
  }
  vector<int> D(N+1);
  int max = 0;
  int min = N;
  for (int _ = 0; _ < N-1; ++_) {
    int d; cin >> d;
    ++D[d];
    chmax(max, d);
    chmin(min, d);
  }
  if (min < 1) {
    cout << 0 << CRLF;
    return;
  }
  if (max < 1) {
    cout << 0 << CRLF;
  }
  ll res = 1;
  D[0] = 1;
  constexpr ll mod = 998244353;
  for (int i = 1; i <= max; ++i) {
    ll pre = D[i-1];
    ll cur = D[i];
    ll now = modpow(pre, cur, mod);
    res *= now;
    res %= mod;
  }
  cout << res << CRLF;
  return;
}

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

  solve();

  return 0;
}

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

int main() {
	int n; cin >> n;
	vector<long long> v(n, 0);
	bool fail = false;
	int tmp;
	cin >> tmp;
	if (tmp != 0) fail = true;
	for (int i = 1; i < n; i++) {
		cin >> tmp;
		if (tmp == 0) fail = true;
		v[tmp]++;
	}
	if (fail) cout << 0 << endl;
	else {
		long long ans = 1;
		for (int i = 1; i < n-1; i++) {
			for (int j = 0; j < v[i+1]; j++) {
				ans *= v[i];
				ans %= 998244353;
			}
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
short S[105][105];
struct Rect {
	int x1, x2, y1, y2;
	inline bool valid() {
		return x2 >= x1 && y2 >= y1;
	}
	inline short gain() {
		return S[y2][x2] - S[y1 - 1][x2] - S[y2][x1 - 1] + S[y1 - 1][x1 - 1];
	}
};
Rect inter(const Rect& a, const Rect& b) {
	return {max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2)};
}
char G[105][105];
int H, W;
int ex, ey;
short dp[101][101][101][101];
short f(int x1, int x2, int y1, int y2) {
	short& ret = dp[x1][x2][y1][y2];
	if (ret != -1) return ret;
	int l = x2 - ex + 1;
	int r = x1 + (W - ex);
	int u = y2 - ey + 1;
	int d = y1 + (H - ey);
	ret = 0;
	Rect allow{l, r, u, d};
	Rect slice;
	slice = inter({x1 - 1, x1 - 1, y1, y2}, allow);
	if (slice.valid()) {
		ret = max((int)ret, f(x1 - 1, x2, y1, y2) + slice.gain());
	}
	slice = inter({x2 + 1, x2 + 1, y1, y2}, allow);
	if (slice.valid()) {
		ret = max((int)ret, f(x1, x2 + 1, y1, y2) + slice.gain());
	}
	slice = inter({x1, x2, y1 - 1, y1 - 1}, allow);
	if (slice.valid()) {
		ret = max((int)ret, f(x1, x2, y1 - 1, y2) + slice.gain());
	}
	slice = inter({x1, x2, y2 + 1, y2 + 1}, allow);
	if (slice.valid()) {
		ret = max((int)ret, f(x1, x2, y1, y2 + 1) + slice.gain());
	}
	return ret;
}
int main() {
	scanf("%d%d", &H, &W);
	for (int i = 0; i < H; ++i) {
		scanf("%s", G[i]);
		for (int j = 0; j < W; ++j) {
			S[i + 1][j + 1] = S[i + 1][j] + S[i][j + 1] - S[i][j] + (G[i][j] == 'o');
			if (G[i][j] == 'E') {
				ex = j + 1;
				ey = i + 1;
			}
		}
	}
	memset(dp, -1, sizeof(dp));
	int ans = f(ex, ex, ey, ey);
	printf("%d\n", ans);
}

#include<stdio.h>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n,k,i,a,b,c,f[1000],sum_max=-999999999,sum;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&f[i]);
	}
	for(a=0;a<=k;a++)
	{
		for(b=0;b<=k;b++)
		{
				for(c=0;c<=k;c++)
				{
					priority_queue<int>q;
					if(a+b+c<=k&&a+b>=c&&a+b<=n)
					{
						for(i=0;i<a;i++)
						{
					       q.push(f[i]);
						}
						for(i=0;i<b;i++)
						{
					       q.push(f[n-i-1]);
						}
						int sum=0;
						for(i=0;i<a+b-c;i++)
						{
							sum=sum+q.top();
							q.pop();
						}
						sum_max=max(sum_max,sum);
					}
				}

		}
	}
	printf("%d",sum_max);
	return 0;
}


#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
using namespace std;
int main(){
  char x[52];
  for(int i='a';i<='z';i++){
    x[i-'a']=i;
  }
  for(int i='A';i<='Z';i++){
    x[26+i-'A']=i;
  }

int n;
  while(cin>>n,n!=0){
    int k[n];
    string s;
    for(int i=0;i<n;i++){
      cin>>k[i];
    }
    cin>>s;
    for(int i=0;i<s.size();i++){
      int b=distance(x,find(x,x+52,s[i]));
      int c=(b-k[i%n]+52)%52;
      cout<<x[c];
    }
    cout<<endl;
  }
  return 0;
}

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

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

int dp[3][100010];

int main(){

    rep(i,3){
        rep(j,100010){
            dp[i][j] = 0;
        }
    }

    int n;
    int tmp ;
    ll ans = 1;
    ll mod = 1e9+7;

    cin >> n;
    vector<int> vec(n);
    rep(j,n){
        cin >> tmp;
        vec[j] = tmp;
        rep(i,3){
            dp[i][j+1] = dp[i][j];
        }
        rep(i,3){
            if(dp[i][j+1] == tmp) {
                dp[i][j+1] = tmp+1;
                break;
            }
        }
    }

    rep(j,n){
        ll cnt = 0;
        rep(i, 3){
            if(dp[i][j] == vec[j]){
                cnt++;
            }
        }
        ans = (ans*cnt) % mod;
    }

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


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

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

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

struct Edge {
    ll to, rev;
    long double cap;
    Edge(ll _to, long double _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll n, w, h;
    cin >> n >> h >> w;
    cout << (n - w + 1) * (n - h + 1) << en;
}

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

    solve();
    /*
    ll t;
    cin >> t;
    REP(i, t)
    solve();
    */

    return 0;
}

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

int main(){
    int a,b,c,p,q,r;
	cin >> a >> b >> c;
     p = a+b;
     q = a+c;
     r = b+c;
    if(p>=q){
        if(q >= r){
            cout << r;
        }else{
            cout << q;
        }
    }else{
        if(p >= r){
            cout << r;
        }else{
            cout << p;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;
using Graph = vector<vector<ll>>;

int main()
{
    ll n, m;
    cin >> n >> m;
    ll maL = -1;
    ll miR = n + 10;
    rep(i, m)
    {
        ll l, r;
        cin >> l >> r;
        maL = max(maL, l);
        miR = min(miR, r);
    }

    ll ans = max((ll)0, miR - maL + 1);
    cout << ans << endl;
}

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

template<typename t1, class t2>
void input(t1 count,t2 array) {
	for(t1 i = 0;i < count;i++) {
		cin >> array[i];
	}
}

template<typename t1, class t2>
void output(t1 count,t2 array) {
	for(t1 i = 0;i < count;i++) {
		cout << array[i];
		if(i != count - 1) cout << " ";
	}
	cout << endl;
}

int n,s[10000],t[500],q,c;

bool exists(int x) {
	for(int i = 0;i < n;i++) if(s[i] == x) return true;
	return false;
}

int main(int argc,char** argv) {
	cin >> n;
	input(n,s);
	cin >> q;
	input(q,t);
	for(int i = 0;i < q;i++) {
		if(exists(t[i]))c++;
	}
	cout << c << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;
using Graph = vector<vector<ll>>;

Graph G;
//2部グラフ判定
vector<ll> color; //1と-1に塗り分ける
bool ok = true;
int dfs(ll v, ll c)
{
    color[v] = c;
    rep(i, G[v].size())
    {
        if (color[G[v][i]] == 0)
            dfs(G[v][i], -c);
        else if (color[G[v][i]] == c)
            ok = false;
    }
    return 0;
}

int main()
{
    ll n, m;
    cin >> n >> m;
    G.resize(n);
    color.resize(n);
    rep(i, m)
    {
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    dfs(0, 1);
    ll b = 0;
    rep(i, n) if (color[i] == 1) b++;
    if (ok)
        cout << b * (n - b) - m << endl;
    else
        cout << n * (n - 1) / 2 - m << endl;
}

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vl vector
#define ff first
#define ss second
using namespace std;
int main()
{
    ll a,b,i,j,n,t,c,m;
    cin>>n>>m;
    ll l,r;
    ll arr[n+1]={0};
    for(i=0;i<m;i++)
    {
        cin>>l>>r;
        l--;
        arr[l]++;
        arr[r]--;
    }
    for(i=1;i<n+1;i++) arr[i]+=arr[i-1];
    ll count=0;
    for(i=0;i<n+1;i++) if(arr[i]==m) count++;
    cout<<count;
}


#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <complex>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
const double PI = 3.141592653589793238462643383279502884L;
const int LARGE_PRIME = 1000000007;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++)
#define rtr(c, i) for(auto i = (c).rbegin(); i != (c).rend(); i++)
#define repfn(i,a,cont,next) \
  for(auto i = (a); [=](){return(cont);}(); i = [=](){return(next);}())
#define repby(i,a,b,s) repfn(i,a,i<b,i+s)
#define repab(i,a,b) repby(i,a,b,1)
#define rep(i,b) repab(i,0,b)
#define pb push_back
#define sz(c) int((c).size())

char rot13(char c, int n)
{
  return 'a' + (c + n) % 26;
}

string rot13(string &s, int n)
{
  n %= 26;
  string res;
  tr (s, p) {
    if (islower(*p))
      res.pb(rot13(*p, n));
    else
      res.pb(*p);
  }
  return res;
}

int main(int argc, char **argv)
{
  string enc;

  while (!getline(cin, enc).eof()) {
    for (int i = 0; i < 26; i++) {
      string s = rot13(enc, i);
      if ((int)s.find("the ") >= 0 || (int)s.find("this ") >= 0 || (int)s.find("that ") >= 0) {
        cout << s << endl;
        break;
      }
    }
  }
  return 0;
}


//Author:xht37
#include <bits/stdc++.h>

#define ui unsigned int
#define ll long long
#define ul unsigned ll
#define ld long double

#define pi pair <int, int>
#define fi first
#define se second
#define mp make_pair

#define ls (p << 1)
#define rs (ls | 1)
#define md ((t[p].l + t[p].r) >> 1)

#define vi vector <int>
#define pb push_back
#define pq priority_queue

#define dbg(x) cerr << #x" = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)

#define fl(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)

using namespace std;

namespace io {
	const int SI = 1 << 21 | 1;
	char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
	int f, t;
	#define gc() (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)
	inline void flush() {
		fwrite(OB, 1, OS - OB, stdout), OS = OB;
	}
	inline void pc(char x) {
		*OS++ = x;
		if (OS == OT) flush();
	}

	template <class I>
	inline void rd(I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15), c = gc());
		x *= f;
	}
	template <class I>
	inline void rd(I &x, I &y) {
		rd(x), rd(y);
	}
	template <class I>
	inline void rd(I &x, I &y, I &z) {
		rd(x), rd(y), rd(z);
	}
	template <class I>
	inline void rda(I *a, int n) {
		for (int i = 1; i <= n; i++) rd(a[i]);
	}
	inline void rdc(char &c) {
		for (c = gc(); c < 33 || c > 126; c = gc());
	}
	inline void rds(char *s, int &n) {
		for (c = gc(); c < 33 || c > 126; c = gc());
		for (n = 0; c >= 33 && c <= 126; s[++n] = c, c = gc());
		s[n+1] = '\0';
	}
	inline void rds(string &s) {
		for (c = gc(); c < 33 || c > 126; c = gc());
		for (s.clear(); c >= 33 && c <= 126; s.pb(c), c = gc());
	}

	template <class I>
	inline void print(I x, char k = '\n') {
		if (!x) pc('0');
		if (x < 0) pc('-'), x = -x;
		while (x) ch[++t] = x % 10 + '0', x /= 10;
		while (t) pc(ch[t--]);
		pc(k);
	}
	template <class I>
	inline void print(I x, I y) {
		print(x, ' '), print(y);
	}
	template <class I>
	inline void print(I x, I y, I z) {
		print(x, ' '), print(y, ' '), print(z);
	}
	template <class I>
	inline void printa(I *a, int n) {
		for (int i = 1; i <= n; i++) print(a[i], " \n"[i==n]);
	}
	inline void printc(char c) {
		pc(c);
	}
	inline void prints(char *s, int n) {
		for (int i = 1; i <= n; i++) pc(s[i]);
		pc('\n');
	}
	inline void prints(string s) {
		int n = s.length();
		while (t < n) pc(s[t++]);
		pc('\n'), t = 0;
	}
	struct Flush {
		~Flush() {
			flush();
		}
	} flusher;
}
using io::rd;
using io::rda;
using io::rdc;
using io::rds;
using io::print;
using io::printa;
using io::printc;
using io::prints;

const int N = 1e5 + 7;
int n;
ll s, x[N], p[N];

ll f(int l, int r, int o) {
	if (s < x[l] || x[r] < s) return abs(s - x[o]);
	if (p[l] >= p[r]) return p[l] += p[r], f(l, r - 1, l) + abs(x[l] - x[o]);
	return p[r] += p[l], f(l + 1, r, r) + abs(x[r] - x[o]);
}

int main() {
	rd(n), rd(s);
	for (int i = 1; i <= n; i++) rd(x[i], p[i]);
	print(f(1, n, s < x[1] ? n : x[n] < s ? 1 : p[1] >= p[n] ? n : 1));
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11;
#define LL long long
int n;
LL ans, s, x[N];
LL a[N];
LL sol(int l, int r){
	//printf("l=%d r=%d al=%lld ar=%lld\n", l, r, a[l], a[r]);
	if(x[l] > s){
		ans += x[r] - s;
		return x[r];
	}
	if(x[r] < s){
		ans += s - x[l];
		return x[l];
	}
	if(a[l] >= a[r]){
		a[l] += a[r];
		ans += abs(x[r] - sol(l, r - 1));
		return x[r];
	}
	else{
		a[r] += a[l];
		ans += abs(x[l] - sol(l + 1, r));
		return x[l];
	}
}
int main(){
	cin>>n>>s;
	for(int i = 1;i <= n; i++){
		scanf("%lld%lld", &x[i], &a[i]);
	}
	sol(1, n);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <atcoder/fenwicktree>

using namespace atcoder;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<double,double> pdd;
#define SQ(i) ((i)*(i))
#define MEM(a, b) memset(a, (b), sizeof(a))
#define SZ(i) int(i.size())
#define FOR(i, j, k, in) for (int i=j ; i<(k) ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=(k) ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define REP1(i,j) FOR(i, 1, j+1, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define ALL(_a) _a.begin(),_a.end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define X first
#define Y second
#ifdef jayinnn
#define TIME(i) Timer i(#i)
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,deque<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
class Timer {
private:
    string scope_name;
    chrono::high_resolution_clock::time_point start_time;
public:
    Timer (string name) : scope_name(name) {
        start_time = chrono::high_resolution_clock::now();
    }
    ~Timer () {
        auto stop_time = chrono::high_resolution_clock::now();
        auto length = chrono::duration_cast<chrono::microseconds>(stop_time - start_time).count();
        double mlength = double(length) * 0.001;
        debug(scope_name, mlength);
    }
};
#else
#define TIME(i)
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
#endif

const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int iNF = 0x3f3f3f3f;
const ll MAXN = 100005;

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

/********** Good Luck :) **********/
int main () {
    TIME(main);
    IOS();
    int n, q;
    cin >> n >> q;
    fenwick_tree<ll> bit(n);
    for(int i=0;i<n;i++){
        int p;
        cin >> p;
        bit.add(i, p);
    }

    while(q--){
        int t;
        cin >> t;
        if(t == 0){
            int p, x;
            cin >> p >> x;
            bit.add(p, x);
        } else {
            int l, r;
            cin >> l >> r;
            cout << bit.sum(l, r) << endl;
        }
    }

    return 0;
}

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

int bfs(vector<string> A,int H,int W)
{
	queue<pair<int,int>> que;
	vector<vector<int>> cost(H,vector<int>(W,-1));
	int lp=H*W;
	rep(i,H)
	{
		rep(j,W)
		{
			if(A[i][j]=='#')
			{
				que.push(make_pair(j,i));
				cost[i][j]=0;
				lp--;
			}
		}
	}
	int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
	int ret=0;
	while(lp!=0)
	{
		int n=que.size();
		rep(i,n)
		{
			pair<int,int> p=que.front();que.pop();
			for(auto d : dir)
			{
				int x=d[0]+p.first;
				int y=d[1]+p.second;
				if(0>x||x>=W||0>y||y>=H||cost[y][x]!=-1) continue;
				cost[y][x]=0;
				que.push(make_pair(x,y));
			}
		}
		lp-=que.size();
		ret++;
	}
	return ret;
}
int main()
{
	int H,W;
	cin>>H>>W;
	vector<string> A(H);
	rep(i,H)
	{
		cin>>A[i];
	}
	cout<<bfs(A,H,W)<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
        int N;
        int out=0;
        cin>>N;
        N*=3;
        string str;
        int ans=0;
        int run=0;
        while(out<N){
                cin>>str;
                if(str=="HIT"){
                        run++;
                        if(run>=4){
                                ans++;
                                run--;
                        }
                }
                else if(str=="OUT"){
                        out++;
                        if(out%3==0){
                                cout<<ans<<endl;
                                ans=0;
                                run=0;
                        }
                }
                else if(str=="HOMERUN"){
                        run++;
                        ans+=run;
                        run=0;
                }
        }

return 0;
}

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

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int score = 0, out = 0;
        int bases[3] = {};
        while (true) {
            char event[8];
            cin >> event;
            if (strcmp(event, "OUT") == 0) {
                out++;
                if (out == 3) {
                    break;
                }
            } else if (strcmp(event, "HIT") == 0) {
                score += bases[2];
                bases[2] = bases[1];
                bases[1] = bases[0];
                bases[0] = 1;
            } else if (strcmp(event, "HOMERUN") == 0) {
                score += bases[0] + bases[1] + bases[2] + 1;
                bases[0] = bases[1] = bases[2] = 0;
            }
        }
        cout << score << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Rep(i, N) for(int i = 0; i < N; i++)
#define Reps(i, x, N) for(int i = x; i < N; i++)

const int LLINF = 1LL << 60;

signed main()
{
  int N, P;

  while(cin >> N >> P, N || P) {

    int w = P;
    int have[55] = {0};
    int i = 0;
    while(true) {
      if(w) have[i]++, w--;
      else w += have[i], have[i] = 0;
      if(have[i] == P) { cout << i << endl; break; }
      i++; if(i == N) i = 0;
    }

  }

  return 0;
}

/*** ?¬?????????? ***/

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

int main(){
	int n, p;
	int i;
	int count, size;
	int max, num;

	cin >> n >> p;
	while(n!=0 || p!=0){
		vector<int> v;
		for(i=0; i<n; ++i) v.push_back(0);
		count=0;
		size=p;

		while(1){
			for(i=size; 0<i; --i){
				++v[count];
				count=(count+1)%n;
			}

			max=0;
			num=-1;
			for(i=0; i<n; ++i){
				if(max<v[i]){
					max=v[i];
					num=i;
				}
			}
			if(max==p) break;

			while(v[count]==0) count=(count+1)%n;
			size=v[count];
			v[count]=0;
			count=(count+1)%n;
		}
		cout << num << endl;

		cin >> n >> p;
	}
	return 0;
}


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

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, int(n) + 1)
#define RREP(i, n) RFOR(i, 0, n)
#define RREPS(i, n) RFOR(i, 1, int(n) + 1)
#define ALL(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define debug(x) cout << #x << ": " << x << endl

#define pb push_back
#define mp make_pair
#define F first
#define S second

#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev
#define INF (1L << 30)
#define MOD (1000000007)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

template <class T>
void pv(T a, T b)
{
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T>
void pvp(T a, T b)
{
  for (T i = a; i != b; ++i)
    cout << "(" << i->first << ", " << i->second << ") ";
  cout << endl;
}
template <class T>
bool chmax(T &a, const T &b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
  if (b < a)
  {
    a = b;
    return 1;
  }
  return 0;
}
int in()
{
  int x;
  scanf("%d", &x);
  return x;
}

using ll = long long;
#define int ll

//**********

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

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

  // A[i]: member i が j 番目に好きなスポーツ
  vector<int> A[301];

  REP(i, n)
  {
    REP(j, m)
    {
      int sp;
      cin >> sp;
      A[i].pb(sp - 1);
    }
  }

  int best_members = 999;

  if (m == 1)
  {
    best_members = n;
  }

  REP(ii, m - 1)
  {
    int popular[301];
    MEMSET(popular, 0);
    REP(i, n)
    { // popular[m]はm番目のスポーツの参加人数
      popular[A[i][0]] += 1;
    }
    int max_sp = 0;
    int max_members = 0;
    REP(sp, m)
    {
      if (max_members < popular[sp])
      {
        max_sp = sp;
        max_members = popular[sp];
      }
    }

    // debug(max_sp + 1);
    // debug(max_members);

    REP(i, n)
    { // 最大人気スポーツを除外
      A[i].erase(remove(ALL(A[i]), max_sp), A[i].end());
    }

    if (best_members > max_members)
    { // もし最大人数が、これまでで最小だったら更新
      best_members = max_members;
    }
  }

  cout << best_members << endl;

  return 0;
};

#define C
typedef long long readtype;

/* Header {{{ */
#ifdef C
#include <stdio.h>
#include <math.h>
#include <string.h>
#define isdigit(a) ((a) >= '0' && (a) <= '9' ? true : false)
template<typename type> type abs(type a) { return a < 0 ? -a : a; }
template<typename type> type min(type a, type b) { return a < b ? a : b; }
template<typename type> type beMin(type &a, type b) { return a = (a < b ? a : b); }
template<typename type> type max(type a, type b) { return a > b ? a : b; }
template<typename type> type beMax(type &a, type b) { return a = (a > b ? a : b); }
template<typename type> void swap(type &a, type &b) { type c; c = a; a = b; b = c; }
#endif

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

typedef long long var;

readtype read() {
  readtype a = 0, c = getchar(), s = 0;
  while (!isdigit(c)) s |= c == '-', c = getchar();
  while (isdigit(c)) a = a * 10 + c - 48, c = getchar();
  return s ? -a : a;
}
/* }}} */

const int N = 311;

int n, m;
int a[N][N], have[N], pos[N];
bool ban[N];

bool check(int t);

int main() {
  /*
#ifndef ONLINE_JUDGE
  freopen("source.in", "r", stdin);
  freopen("source.out", "w", stdout);
#endif
*/
  n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) a[i][j] = read();
  }
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (!check(mid)) l = mid + 1;
    else r = mid;
  }
  printf("%d\n", l);
  return 0;
}

bool check(int t) {
  memset(ban, false, sizeof(ban));
  memset(have, 0, sizeof(have));
  memset(pos, 0, sizeof(pos));
  for (int i = 1; i <= n; ++i) have[a[i][++pos[i]]]++;
  while (true) {
    bool ok = true;
    for (int i = 1; i <= m; ++i) {
      if (have[i] <= t) continue;
      ok = false;
      ban[i] = true;
    }
    if (ok) break;
    for (int i = 1; i <= n; ++i) {
      while (pos[i] <= m && ban[a[i][pos[i]]]) {
        have[a[i][pos[i]]]--;
        have[a[i][++pos[i]]]++;
      }
      if (pos[i] > m) return false;
    }
  }
  return true;
}


#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vector<int>>;
using vb=vector<bool>;
using pii=pair<int,int>;
using vpii=vector<pair<int,int>>;

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

//pow(llpow,modpow)
long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)
//試験導入
#define irep(i, end_i, begin_i) for (ll i = (ll)begin_i-1; i >= (ll)end_i; i--)

long long INF = 1LL<<60;
int main( ){
    int n;
    cin>>n;
    vpii rxy(n),byx(n);
    rep(i,0,n){
        int x,y;
        cin>>x>>y;
        pii p=make_pair(x,y);
        rxy[i]=p;
    }
    rep(i,0,n){
        int x,y;
        cin>>x>>y;
        pii p=make_pair(y,x);
        byx[i]=p;
    }
    vb fixedr(n,false);
    int cnt=0;
    sort(rxy.begin(),rxy.end());
    sort(byx.begin(),byx.end());
    rep(bi,0,n){
        int  bx=byx[bi].second;
        int  by=byx[bi].first;
        int rfi=-1,vrf=0;
        rep(ri,0,n){
            if(fixedr[ri])continue;
            int rx,ry;
            rx=rxy[ri].first;
            ry=rxy[ri].second;
            if(rx<bx&&ry<by){
                rfi=ri;
            }
        }
        if(rfi!=-1){
            cnt++;
            fixedr[rfi]=true;
        }
    }
    cout<<cnt<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define Fst first
#define Snd second
#define RG register
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int UI;
typedef unsigned long long ULL;
template<typename T> inline void read(T& x) {
	char c = getchar();
	bool f = false;
	for (x = 0; !isdigit(c); c = getchar()) {
		if (c == '-') {
			f = true;
		}
	}
	for (; isdigit(c); c = getchar()) {
		x = x * 10 + c - '0';
	}
	if (f) {
		x = -x;
	}
}
template<typename T, typename... U> inline void read(T& x, U& ... y) {
	read(x), read(y...);
}
const int N=1e5+10;
int n,m,p;
int head[N],vis[N],In[N];
bool Flag;
struct Edge {
  int to,last;
  Edge () {}
  Edge (int a,int b) :to(a),last(b) {}
}edge[N<<2];
void ADD(int a,int b) {
  edge[++p]=Edge(b,head[a]); head[a]=p;
  edge[++p]=Edge(a,head[b]); head[b]=p;
}
void DFS(int u,int c) {
  if(vis[u]) {
    if(vis[u]!=c) Flag=true;
    return;
  }
  vis[u]=c;
  for(int i=head[u];i;i=edge[i].last) {
    int v=edge[i].to;
    DFS(v,c^1);
  }
}
//#define rua
int main() {
//	ios::sync_with_stdio(false);
#ifdef rua
	freopen("GG.in","r",stdin);
#endif
  read(n,m);
  for(int i=1;i<=m;++i) {
    int u,v; read(u,v);
    ADD(u,v);
    ++In[u]; ++In[v];
  }
  LL A=0,B=0,C=0;
  for(int i=1;i<=n;++i) if(!vis[i]) {
    if(!In[i]) ++A;
    else {
      Flag=false;
      DFS(i,2);
      if(Flag) ++C;
      else ++B;
    }
  }
  printf("%lld\n",A*n+A*(n-A)+B*B*2+C*C+C*B*2);
  return 0;
}


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

int main(int argc, char const *argv[])
{
    string str,str1,str2;
    int a=0, b=0, n;

    cin >> str;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> str1;
        cin >> a >> b;
        if(str1 == "print"){
            str2 = str.substr(a,b-a+1);
            cout << str2 << endl;
        }
        if(str1 == "reverse"){
            str2 = str.substr(a,b-a+1);
            for (int i = 0; i < str2.size(); ++i)
            {
                str[a+i] = str2[str2.size()-i-1];
            }
        }
        if(str1 == "replace"){
            cin >> str2;
            for (int i = 0; i < str2.size(); ++i)
            {
                str[a+i] = str2[i];
            }
        }
    }

    return 0;
}

#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define getbit(x, i) (((x) >> (i)) & 1)
using namespace std;
typedef pair<int, int> pii;
#define hashset unordered_set
#define hashmap unordered_map
#define newline fast_writechar('\n')
#define unify(arr) arr.resize(unique(arr.begin(), arr.end()) - arr.begin())
#define getbit(x, i) (((x) >> (i)) & 1)

template <typename T>
vector<T> readvector(size_t sz) {
    vector<T> res(sz);
    for (size_t i = 0; i < sz; ++i) {
        cin >> res[i];
    }
    return res;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, " "));
    return out;
}

int mylog(int n) {
    return 63 - __builtin_clzll(n);
}

inline int binPow(int x, int deg, int mod) {
    int ans = 1;
    for (int i = sizeof(deg) * CHAR_BIT - 1; i >= 0; i--) {
        ans *= ans;
        ans %= mod;
        if (getbit(deg, i)) ans *= x;
        ans %= mod;
    }
    return ans;
}

/** Interface */

inline int readInt();
inline int readUInt();
inline void readWord(char *s);
inline int fast_readchar();  // you may use readchar() instead of it

inline void writeInt(int x);
inline void fast_writechar(int x);  // you may use putchar() instead of it
inline void writeWord(const char *s);
inline void fast_flush();

// ====================== END ======================
const int MAXN = 2e6 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e18;

void solve() {
    int n, k;
    cin >> n >> k;
    auto arr =readvector<int>(n);
    arr.insert(arr.begin(), 0);
    for (int i = 1; i <= n; i++) arr[i] += arr[i - 1];
    int ans = 0;
    for (int i = 0; i + k <= n; i++) {
        ans = max(ans, arr[i + k] - arr[i] + k);
    }
    cout << fixed << setprecision(1) << ans / 2. << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // t = readInt();
    // cin >> t;
    for (int i = 1; i <= t; ++i) {
        // cout << "Case #" << i << ": ";
        solve();
    }
    // fast_flush();
    return 0;
}

/** Read */

static const int buf_size = 4096;

inline int fast_readchar() {
    static char buf[buf_size];
    static int len = 0, pos = 0;
    if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);
    if (pos == len) return -1;
    return buf[pos++];
}

inline int readUInt() {
    int c = fast_readchar(), x = 0;
    while (c <= 32) c = fast_readchar();
    while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = fast_readchar();
    return x;
}

inline int readInt() {
    int s = 1, c = fast_readchar();
    int x = 0;
    while (c <= 32) c = fast_readchar();
    if (c == '-') s = -1, c = fast_readchar();
    while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = fast_readchar();
    return x * s;
}

inline void readWord(char *s) {
    int c = fast_readchar();
    while (c <= 32) c = fast_readchar();
    while (c > 32) *s++ = c, c = fast_readchar();
    *s = 0;
}

/** Write */

static int write_pos = 0;
static char write_buf[buf_size];

inline void fast_writechar(int x) {
    if (write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
    write_buf[write_pos++] = x;
}

inline void fast_flush() {
    if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}

inline void writeInt(int x) {
    if (x < 0) fast_writechar('-'), x = -x;

    char s[24];
    int n = 0;
    while (x || !n) s[n++] = '0' + x % 10, x /= 10;
    while (n--) fast_writechar(s[n]);
}

inline void writeWord(const char *s) {
    while (*s) fast_writechar(*s++);
}


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

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.141592653589793238462643383279;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

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

    VI nd(N,0), nm(N,0), nc(N,0);
    REP(i,N){
        if(S[i]=='D') nd[i] = 1;
        else if(S[i]=='M') nm[i] = 1;
        else if(S[i]=='C') nc[i] = 1;
    }
    REP(i,N-1) nd[i+1] += nd[i]; REP(i,N-1) nm[i+1] += nm[i]; REP(i,N-1) nc[i+1] += nc[i];
    ll ndmc = 0;
    REP(i,N) if(S[i]=='M') ndmc += (ll)nd[i]*(nc[N-1]-nc[i]);
    VL sc(nc[N-1]+1);
    REP(i,N) if(S[i]=='C') sc[nc[i]] = nm[i];
    REP(i,nc[N-1]) sc[i+1] += sc[i];

    int Q; cin >> Q;
    REP(i,Q){
        int k; cin >> k;
        ll ans = 0;
        REP(i,N-k){
            if(S[i] != 'D') continue;
            ll M = nm[i];
            ll t = nc.back() - nc[i+k-1];
            ans += sc.back() - sc[nc.back()-t] - M*t;
        }
        cout << ndmc - ans << en;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,q,k;
char s[1000005];
int main(){
	scanf("%lld%s%lld",&n,s,&q);
	while(q--){
		scanf("%lld",&k);
		ll D=0,M=0,DM=0,DMC=0;
		for(ll i=0;i<n;i++){
			if(i>=k){
				if(s[i-k]=='D'){
					DM-=M;
					D--;
				}
				if(s[i-k]=='M')
					M--;
			}
			if(s[i]=='D')
				D++;
			if(s[i]=='M'){
				M++;
				DM+=D;
			}
			if(s[i]=='C')
				DMC+=DM;
		}
		printf("%lld\n",DMC);
	}
}


#include <bits/stdc++.h>

using namespace std;

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

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define DEBUG_ARR(v,n) cout<<#v<<":";REP(i,n)cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
void DEBUG_DP(T* viewdp, int ilen, int jlen){
    REP(i,ilen){
        REP(j,jlen){
            if(viewdp[i][j] != -1)
                printf("[% 4d]", viewdp[i][j]);
            else
                printf("[    ]");
        }
        puts("");
    }
}
const int N=81;
const int C=6400;
const int K=C*2+1;
int h,w;
int a[N][N], b[N][N];
bitset<K> dp[N][N];
int ans = INT_MAX;

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

    REP(i,N){
        fill(a[i], a[i]+N, -1);
        fill(b[i], b[i]+N, -1);
    }

    cin>>h>>w;

    REP(i,h) REP(j,w) cin>>a[i][j];
    REP(i,h) REP(j,w) cin>>b[i][j];

    //int sx=0,sy=0, gx=w-1, gy=h-1;
    dp[0][0][abs(a[0][0]-b[0][0]) + 6400]=true;

    REP(i,h) REP(j,w){
        int ab =abs(a[i+1][j]-b[i+1][j]);

        dp[i+1][j] |= dp[i][j] << ab;
        dp[i+1][j] |= dp[i][j] >> ab;

        ab =abs(a[i][j+1]-b[i][j+1]);
        dp[i][j+1] |= dp[i][j] << ab;
        dp[i][j+1] |= dp[i][j] >> ab;
    }

    REP(i,K){
        if(dp[h-1][w-1][i]) chmin(ans, abs(6400-i));
    }

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

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}}
ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;}
vector<ll> pw2(62,1);vector<ll> pw10(19,1);

int main(){
    {rep1(i,61) pw2[i]=2*pw2[i-1];}
    {rep1(i,18) pw10[i]=10*pw10[i-1];}

    ll K,T; cin>>K>>T;
    vector<ll> A(T,0);
    rep(i,T){
        cin>>A[i];
    }
    sort(all(A));
    ll L=A[sz(A)-1];
    rep(i,sz(A)-1){
        L-=A[i];
    }
    cout<<max(L-1,0ll)<<endl;
}


#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include <iomanip>

std::vector<int> parse_line(std::string const& line) {
    using namespace std;
    vector<int> result;
    istringstream s(line);
    string element;
    while (getline(s, element, ' ')) {
        stringstream buf;
        int value = 0;
        buf << element;
        buf >> value;
        result.push_back(value);
    }
    return std::move(result);
}
int main(int argc, char* argv[])
{
    using namespace std;
    while (true) {
        string line;
        getline(std::cin, line);
        auto elements = parse_line(line);
        auto length = elements[0];
        if (length == 0) break;
        vector<int> total(length + 1);
        size_t width = length + 1;
        for (size_t i = 0; i < length; ++i) {
            getline(cin, line);
            elements = parse_line(line);
            int sum = 0;
            for (size_t n = 0; n < length; ++n) {
                int val = elements[n];
                sum += val;
                total[n] += val;
                cout << setw(5) << val;
            }
            cout << setw(5) << sum << endl;
            total[length] += sum;
        }
        for (size_t i = 0; i < width; ++i) {
            cout << setw(5) << total[i];
        }
        cout << endl;
    }

    return 0;
}

#include <cstdio>

int matrix[12][12];
int n;

int main() {
  while(scanf("%d", &n) != EOF, n) {
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
	scanf("%d", &matrix[i][j]);
      }
    }

    // output
    for (int i=0; i<n; i++) {
      int sum = 0;
      for (int j=0; j<n; j++) {
	sum += matrix[i][j];
	printf("%5d", matrix[i][j]);
      }
      matrix[i][n] = sum;
      printf("%5d\n", sum);
    }

    for (int j=0; j<=n; j++) {
      int sum = 0;
      for (int k=0; k<n; k++) {
	sum += matrix[k][j];
      }
      printf("%5d", sum);
    }
    printf("\n");
  }
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b, c;
  while(cin>>a>>b>>c,a||b||c) {
    int N; cin >> N;
    int info_[4][N];
    FOR(i,0,N) FOR(j,0,4) cin >> info_[j][i];
    int info[a+b+c+1];
    FOR(i,0,a+b+c+1) info[i] = 2;
    // ???????????????????????¨?????????
    FOR(i,0,N) if(info_[3][i] == 1) FOR(j,0,3) info[info_[j][i]] = 1;

    // ?????????????????¨?????????
    FOR(i,0,N) {
      if(info_[3][i] == 0) {
        int cnt = 0;
        FOR(j,0,3) cnt += info[info_[j][i]];
        if(cnt == 4) {
          FOR(j,0,3) if(info[info_[j][i]] == 2) info[info_[j][i]] = 1000;
        }
      }
    }
    FOR(i,1,a+b+c+1) cout << (info[i] == 1000 ? 0 : info[i]) << '\n';
  }
  return 0;
}


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

int main()
{
    string s;
    char c[3]={};
    int k,ans=999;
    cin>>s;
    for(int i=0;i<s.size()-2;i++)
    {
        c[0]=s[i];
        c[1]=s[i+1];
        c[2]=s[i+2];
        k=atoi(c);
       // cout<<"k = "<<k<<'\n';
        ans=min(abs(k-753),ans);
    }
    cout<<ans;
    return 0;
}

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

int main(void)
{
    vector<ll> cost(5);
    ll N;
    cin >> N;
    for (int i = 0; i < 5; i++)
    {
        cin >> cost[i];
    }

    ll result = (ll) ceil((double) N / *min_element(cost.begin(), cost.end())) + 4;
    cout << result << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  long long N,M;
  cin>>N>>M;
  vector<long long> l(M);
  vector<long long> r(M);
  for(long long i=0;i<M;i++)
    cin>>l.at(i)>>r.at(i);
  sort(l.begin(),l.end());
  sort(r.begin(),r.end());
  long long a=r.at(0)-l.at(M-1)+1;
  if(a>0)
  cout<<a<<endl;
  else
    cout<<0<<endl;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)
#define rrep(i,x) for(int i=(int)x-1;i>=0;--i)
#define rrep1(i,x) for(int i=(int)x;i>=1;--i)

using ll=long long;
using ld=long double;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int, int>;
using vpii=vector<pii>;

template<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>p){os<<"("<<p.fst<<", "<<p.scd<<")";return os;}
template<class T>ostream&operator<<(ostream&os,const vector<T>v){rep(i,v.size()){if(i)os<<", ";os<<v[i];}return os;}
template<typename T>T&max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T&min(T&a,T&b){if(a < b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a < b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a > b){a=b;return true;}return false;}
constexpr ll TEN(ll n){return n==0?1:10ll*TEN(n-1);}

constexpr ll inf = TEN(9)+5;
constexpr ll linf = 3*TEN(18);

signed main()
{
    int N, M;
    cin >> N >> M;

    vector<int> imos(N + 1);

    rep(i, M) {
        int l, r; cin >> l >> r;
        --l, --r;
        imos[l]++, imos[r + 1]--;
    }

    rep(i, N) imos[i + 1] += imos[i];

    int ans = 0;
    rep(i, N) if (imos[i] == M) ans++;

    cout << ans << endl;
}


#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long li;

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

    li n;
    cin >> n;

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

    li xorsum = 0;
    for (int i = 0; i < n; ++i) {
        xorsum ^= xs[i];
    }

    li ans = 0;
    for (unsigned int pos = 0; pos < 60; ++pos) {
        if ((xorsum & (1ULL << pos)) != 0) {
            ans += 1ULL << pos;
            for (int i = 0; i < n; ++i) {
                xs[i] &= ~(1ULL << pos);
            }
        }
    }

    vector<bool> used(n);
    for (unsigned int pos = 59; pos <= 59; --pos) {
        for (int pivot = 0; pivot < n; ++pivot) {
            if ((xs[pivot] & (1ULL << pos)) != 0 && !used[pivot]) {
                used[pivot] = true;
                for (int i = 0; i < n; ++i) {
                    if (i == pivot) {
                        continue;
                    }
                    if ((xs[i] & (1ULL << pos)) == 0) {
                        continue;
                    }
                    xs[i] ^= xs[pivot];
                }
            }
        }
    }

    li dupsum = 0;
    for (int i = 0; i < n; ++i) {
        dupsum ^= xs[i];
    }

    cout << ans + dupsum * 2 << endl;

    return 0;
}

#include<vector>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include<queue>
#include <sstream>
#include <set>
#include<stack>

const long long MOD = 1000000007;
using namespace std;
typedef long long llong;

//int isalpha(char ch): ch がアルファベットなら true を返す
//int isdigit(char ch): ch が数字なら true を返す
//int islower(char ch): ch が小文字なら true を返す
//int isupper(char ch): ch が大文字なら true を返す
//int tolower(char ch): ch の小文字を返す
//int toupper(char ch): ch の大文字を返す

//string型
//size()	文字数を返す
//Insert()	（指定した場所に）文字・文字列を挿入する
//erase()	（指定した場所の）文字・文字列を削除する
//clear()	すべての文字を削除する
//substr()	文字列の（指定した）部分文字列を返す
//replace()	（指定した）部分文字列を新しい文字列に置換する
//c_str()変換
//文字列の比較は、<=や==などを使え
//replace関数を使い、簡単に文字列を置換
//リバース関数：reverse(str.begin(), str.end());
//map<type, type> dict;で宣言
//グラフ理論用変数
//vector<vector<llong> > graph(N);

//ソート
//降順sort(v.begin(), v.end(), std::greater<Type>());

//大文字から小文字へんかん
//w[i] = w[i]-'A'+'a';

//vector
//assignメソッド　引数：サイズ、値
//与えられたサイズと値でvectorを初期化する

//queueクラス
//find()次に取り出す値の表示をする。
//pop()値を取り出す。戻り値はなし
//push()キューに値をプッシュする

//priority_queueクラス

//切り上げ
//ceil
//floor

llong eval(char *s){
    llong total;
    total = strtol(s, &s, 10);
    while(*s){
        if(*s=='+'){
            s++;
            total = total + strtol(s, &s, 10);
            continue;
        }
        if(*s=='-'){
            s++;
            total = total - strtol(s, &s, 10);
            continue;
        }
    }
    return total;
}

int main(){
    string S;
    cin >> S;
    llong N=S.size()-1;
    llong total = 0;
    string s;
    for(int i=0; i<pow(2, N); i++){
        string temp;
        for(int j=0; j<N; j++){
            if((i>>j)&1){
                temp.push_back('+');
                continue;
            }
            temp.push_back('-');
        }
        s = "";
        for(int i=0; i<N; i++){
            s = s + S[i] + temp[i];
        }
        s = s + S[S.size()-1];
        if(eval((char*)s.c_str())==7){
            cout << s << "=7" << endl;
            exit(0);
        };
    }
    return 0;
}