#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
int bit[55][55];
void add(int x, int y){
	for(int i=x;i>0;i-=(i&-i)){
		for(int j=y;j>0;j-=(j&-j)){
			bit[i][j]++;
		}
	}
}
ll get(int x, int y){
	ll ret =0;
	for(int i=x;i<55;i+=(i&-i)){
		for(int j=y;j<55;j+=(j&-j)){
			ret+=bit[i][j];
		}
	}
	return ret;
}
ll get2(ll x){
	ll ret = 1;
	while(x%2==0){
		ret++;
		x/=2;
	}
	return ret;
}
ll get5(ll x){
	ll ret = 1;
	while(x%5==0){
		ret++;
		x/=5;
	}
	return ret;
}
void solve(){
	int n;
	cin>>n;
	ll ans = 0;
	for(int i=1;i<=n;i++){
		ld inp;
		cin>>inp;
		ll trt = llround(inp*1e9);
		ll x = get2(trt);
		ll y = get5(trt);
		ll xp = max(1ll,20-x);
		ll yp = max(1ll,20-y);
		ans+=get(xp,yp);
		add(x,y);
	}
	cout<<ans<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <math.h>

unsigned int factorial(unsigned int n){
    if (n==0) return 1;
    else return n*factorial(n-1);
}

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

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

    vector<pair<int,int>> xy(n);
    vector<int> a(n);
    for(int i=0;i<n;i++) {
        cin>>xy[i].first>>xy[i].second;
        a[i]=i;
    }

    double l_sum=0;

    do{
        for(int i=0;i<n-1;i++){
            l_sum+=sqrt(pow(xy[a[i]].first-xy[a[i+1]].first,2)+pow(xy[a[i]].second-xy[a[i+1]].second,2));
        }
    }while(next_permutation(a.begin(),a.end()));

    cout<<fixed<<setprecision(10)<<l_sum/(double) factorial(n)<<endl;

    return 0;
}


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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}
struct edge {
  ll to, cost;
};

int main() {
  int A, B, C; cin >> A >> B >> C;
  if(A <= C && C <= B) cout << "Yes" << endl;
  else if(B <= C && C <= A) cout << "Yes" << endl;
  else cout << "No" << endl;
}

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

int main() {
    int A,B,K;
    cin >> A >> B >> K;

    rep(i,K)
        if(i%2) {
            if(B%2)
                --B;
            B /= 2;
            A += B;
        } else {
            if(A%2)
                --A;
            A /= 2;
            B += A;
        }

    cout << A << " " << B << endl;
}


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

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

long long MOD = 10000000000 + 7;

int main(){
  cout << setprecision(10);
  int N,K; cin >> N >> K;
  vector<ll> v(N+1,0);

  for(int i = 0; i < N; i++){
    ll tmp; cin >> tmp;
    v[i+1] = tmp;
  }

  for(int i = 1; i <= N; i++){
    v[i] = v[i] + v[i-1];
  }

  ll max_val = 0;
  for(int i = N; i - K >= 0; i--){
    max_val = max(max_val, v[i] - v[i - K]);
  }
  cout << (double)(max_val+K) / 2.0 << endl;

}

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x) \
	for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T>
inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return 1;
	}
	return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return 1;
	}
	return 0;
}
const long long INFLL = 1LL << 60;
const int INF         = 1 << 30;
const double PI       = acos(-1);
int main() {
	int n;
	int k;
	cin >> n >> k;
	vector<double> p(n);
	double ans = 0.0, cnt = 0.0;

	rep(i, n) {
		cin >> p[i];
		p[i] = (p[i]*(p[i]+1.0)/2.0) / p[i];
		clog << p[i] << endl;
		cnt += p[i];
		if (i >= k) {
			cnt -= p[i - k];
		}
		chmax(ans, cnt);
		// clog << cnt << endl;
	}

	printf("%.9lf\n", ans);
}


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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int M = 998244353;
const int N = 305;

int n,m,k;
vector<int> v,u;
vector<vector<int>> ans;
int dp[N][N][N];

int rec(int id,int carry,int can)
{
    if(id == -1)
    {
        //if(!carry){ans.pb(u);}
        return (carry == 0);
    }
    if(dp[id][carry][can] != -1)return dp[id][carry][can];
    int has = v[id] + carry;
    int ans = 0;
    for(int give=0;give<=min(has,k);give++)
    {
        int left = has - give;
        //u[id] = left;
        int now = max(0LL,left-v[id]);
        if(now > can)break;
        ans += rec(id-1,give,can-now),ans %= M;
    }
    dp[id][carry][can] = ans;
    return ans;
}

signed main()
{
    FAST
    int tc=1;
    //cin>>tc;
    for(int ti=1;ti<=tc;ti++)
    {
        for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int ii=0;ii<N;ii++)dp[i][j][ii]=-1;
        string s;
        cin>>s>>k;
        n = s.length();
        k = min(n,k);
        int curr = 0;
        for(int i=0;i<n;i++)
        {
            if(s[i] == '0')
            {
                v.pb(curr);
                curr = 0;
            }
            else curr++;
        }
        if(curr)v.pb(curr);
        m = v.size();
        //u = v;
        cout<<rec(m-1,0,k)<<endl;
        //sort(all(ans));
        //for(auto u:ans){for(auto i:u)cout<<i<<" ";cout<<endl;}
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N_MAX = 302;

const int MOD = 998244353;

int n, k;

string s;

int m;

int a[N_MAX];
int spa[N_MAX];

int dp[N_MAX][N_MAX][N_MAX];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> s >> k;
    n = s.size();
    s = " " + s;
    for(int i = 1; i <= n; i++)
        if(s[i] == '1')
            a[i - (++m) + 1]++;
    for(int i = 1; i <= n; i++)
        spa[i] = spa[i - 1] + a[i];
    k = min(k, n);
    dp[0][0][0] = 1;
    for(int i = 1; i <= n; i++)
        for(int sp = spa[i]; sp <= spa[n]; sp++)
            for(int sum = 0; sum <= k; sum++)
            {
                for(int x = 1; x <= sum && (a[i] - x) <= sp && x <= a[i]; x++)
                {
                    dp[i][sp][sum] += dp[i - 1][sp - (a[i] - x)][sum - x];
                    if(dp[i][sp][sum] >= MOD)
                        dp[i][sp][sum] -= MOD;
                }
                for(int x = 0; (a[i] + x) <= sp; x++)
                {
                    dp[i][sp][sum] += dp[i - 1][sp - (a[i] + x)][sum];
                    if(dp[i][sp][sum] >= MOD)
                        dp[i][sp][sum] -= MOD;
                }
            }
    int ans = 0;
    for(int i = 0; i <= k; i++)
    {
        ans += dp[n][spa[n]][i];
        if(ans >= MOD)
            ans -= MOD;
    }
    cout << ans << "\n";
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

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

using namespace std;

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

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

	string S;cin>>S;

	vector<int> T(60); //T[j]:=0がjビット目を0にできるときに使用する数。掃き出し法を用いて整理する。
	for(int i=N-1;i>=0;i--){
		int e=A[i];
		for(int j=59;j>=0;j--){
			if(T[j]==0)continue;
			if((e&T[j])==0)continue;
			if(topbit(e&T[j])==topbit(T[j]))e^=T[j]; //0がA[i]を0にできるかどうかを調べる
		}
		if(S[i]=='1'){
			if(e){
				cout<<1<<endl;
				return;
			}
		}
		else{
			if(e){
				rep(j,60){
					if(T[j]==0)continue;
					if((e&T[j])==0)continue;
					if(topbit(e&T[j])==topbit(e))T[j]^=e;
				}
				T[topbit(e)]=e;
			}
		}
	}
	cout<<0<<endl;
}

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

#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7)    //0.0000001 the value
#define PI (acos(-1))
#define MAXN (300006)
#define MAXK 26
#define MAXX 15000006
#define ll long long int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = ss; ii < ee; ++ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL<<(x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
ll n, A[MAXN], dp[302*302], dp2[302*302];
const ll MOD=998244353;
ll qexp(ll x, ll e) {
	if(e==0) return 1;
	ll half = qexp(x,e/2);
	half *= half;
	half %= MOD;
	if(e%2) half *= x;
	half %= MOD;
	return half;
}
int main()
{
	cin>>n; FOR(i,0,n) cin>>A[i];
	ll sum=accumulate(A,A+n,0);
	dp[0]=1;dp2[0]=1;
	for(ll i=0;i<n;i++) {
		for(ll j=sum;j>=0;j--) {
			// dp[j] = dp[j] + (dp[j]*2)%MOD; dp[j] %= MOD;
			dp[j] *= 2; dp[j] %= MOD;
			if(j>=A[i])dp[j] += dp[j-A[i]]; dp[j] %= MOD;
			if(j>=A[i])dp2[j] += dp2[j-A[i]]; dp2[j] %= MOD;
		}
	}
	ll ans = qexp(3,n-1);
	for(ll i=(sum+1)/2;i<=sum;++i) {
		ans -= (dp[i]);
		ans += MOD;
		ans %= MOD;
	}
	if(sum%2==0) {
		ans += dp2[sum/2];
		ans %= MOD;
	}
	cout<<(ans*3LL)%MOD<<'\n';
}


#include <iostream>

int main (){
    std::string rightName = "Hoshina";
    std::string wrongName = "Hoshino";

    //num of data n
    int n = 0;
    std::cin >> n;
    //skip "\n"
    std::cin.ignore();
    for(int i = 0; i < n; ++i){
        std::string line;
        //input string
        getline(std::cin, line);
        //find exist of wrongName
        std::string::size_type pos = line.find(wrongName);
        while(pos != std::string::npos){
            //rename to rightName
            line.replace(pos, wrongName.size(), rightName);
            pos = line.find(wrongName, pos + rightName.size());
        }

        std::cout << line << std::endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define all(x) x.begin(), x.end()
#define watch(x) cout<<#x<<" : "<<x<<endl
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define testCases int tt;cin>>tt; while(tt--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef vector<string> vs;

int main() {
    fastio;
    int n;
    cin >> n;
    set<int> s;
    for (int i = 0; i < n; i++) {
        int temp;
        cin >> temp;
        s.insert(temp);
    }
    if (s.size() == n) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using cd = complex <double>;

typedef pair <int, int> pii;
const int N = 3e3 + 5;
const long long INF = 1e18;
const int mod = 998244353;//786433;//998244353;
const double Pi = acos(-1);

void Fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

int n, m;
queue <pair <int, int> > BFS;
int Dist[1005][1005];
char a[1005][1005];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			Dist[i][j] = 1000000;
			cin >> a[i][j];
			if(a[i][j] == '#')
			{
				Dist[i][j] = 0;
				BFS.push(mp(i, j));
			}
		}
	}
	while(BFS.empty() == false)
	{
		int x = BFS.front().fi, y = BFS.front().se;
		BFS.pop();
		for(int i = 0; i < 4; i++)
		{
			if(Dist[x + dx[i]][y + dy[i]] > Dist[x][y] + 1)
			{
				Dist[x + dx[i]][y + dy[i]] = Dist[x][y] + 1;
				BFS.push(mp(x + dx[i], y + dy[i]));
			}
		}
	}
	int t = 0;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			t = max(t, Dist[i][j]);
		}
	}
	cout << t;
}

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

int main(){
    int h, w;
    char t;
    cin >> h >> w;
    vector<vector<bool>> c(h, vector<bool>(w, false));
    queue<pair<int, int>> que1, que2;
    rep(i, h) rep(j, w){
        cin >> t;
        if(t == '#'){
            que2.push(make_pair(i, j));
            c[i][j] = true;
        }
    }
    pair<int, int> p;
    vector<int> dy ={1,0,-1,0}, dx={0,1,0,-1};
    int tx, ty, result = 0;
    while(!que2.empty()){
        ++result;
        que2.swap(que1);
        while(!que1.empty()){
            p = que1.front();
            que1.pop();
            rep(i, 4){
                ty = p.first + dy[i];
                tx = p.second + dx[i];
                if(ty==-1||tx==-1||ty==h||tx==w||c[ty][tx]) continue;
                que2.push(make_pair(ty, tx));
                c[ty][tx] = true;
            }
        }
    }
    cout(result-1);
}


#include <iostream>
using namespace std;

int main() {
    int n,g,m;
    cin>>n>>g;
    m=(g*2)-n;
    cout<<m<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
	int a,b;
	cin >> a >> b;
	int d = 2;
	int sum = 0;
	while(d--){
		if(a>b){
			sum+=a;
			a=a-1;
		}
		else if(a<b){
			sum+=b;
			b=b-1;
		}
		else if(a==b && d==2){
			sum = a+b;
		}
		else{
			sum+=a;
		}
	}
	cout << sum << endl;
	return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class BIT {
    int n;
    vector<ll> node;

public:
    BIT(int N) : n(N), node(N + 1) {}
    void add(int i, ll x) {
        for (++i; i <= n; i += i & -i) { node[i] += x; }
    }
    ll sum(int i) {
        ll s = 0;
        for (; i > 0; i -= i & -i) { s += node[i]; }
        return s;
    }
};
class RSQ {
    int n;
    BIT a, b;

public:
    RSQ(int N) : n(N), a(N), b(N) {}
    void add(int l, int r, ll x) {
        int t = r - l;
        a.add(l, -l * x);
        a.add(r, r * x);
        b.add(l, x);
        b.add(r, -x);
    }
    ll sum(int i) { return a.sum(i) + i * b.sum(i); }
};

class Main {
    int N, K;
    vector<int> A;
    void input() {
        cin >> N >> K;
        A.resize(N);
        REP(i, N) { cin >> A[i]; }
    }
    void output() {
        REP(_, K) {
            vector<ll> imos(N + 2);
            REP(i, N) {
                imos[max(0, i - A[i]) + 1] += 1;
                imos[min(N, i + A[i] + 1) + 1] -= 1;
            }
            REP(i, N) {
                imos[i + 1] += imos[i];
                A[i] = imos[i + 1];
            }
            if (*min_element(A.begin(), A.end()) == N) break;
        }
        REP(i, N) { cout << A[i] << " "; }
        cout << endl;
    }

public:
    Main() {
        input();
        output();
    }
};

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

    Main();

    return 0;
}

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

void Main()
{
    int n, m;
    cin >> n >> m;
    int a[m], b[m];
    int j = 0;

    int sp = 0;
    for (int i = 0; i < m/2; i++)
    {
        a[i] = j;
        j++;
        sp++;
    }
    int sp2 = sp;
    if(m % 2 == 1){
        a[sp] = j;
        j++;
        b[sp] = j;
        j++;
        sp++;
    }
    for (int i = 0; i < m/2; i++)
    {
        b[sp2-i-1] = j;
        j++;
    }

    for (int i = 0; i < m/2; i++)
    {
        a[i+sp] = j;
        j++;
    }
    j++;
    for (int i = 0; i < m/2; i++)
    {
        b[sp2-i-1+sp] = j;
        j++;
    }
    for (int i = 0; i < m; i++){
        cout << a[i]+1 << " " << b[i]+1 << endl;
    }

}

int main(int argc, char **argv)
{
    Main();
    return 0;
}


#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = int_fast64_t;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }

void answer() {
  int n; cin>>n;
  const ll INF = 1e18;
  vector<ll> A(n);
  rep(i,n) cin>>A[i];
  vector<vll> dp(n+1, vll(5, -INF));
  dp[0][0] = 0;
  int k = n%2+1;
  rep(i,n) repc(j,k) {
    chmax(dp[i+1][j+1], dp[i][j]);
    if((i+j)%2==0) chmax(dp[i+1][j], dp[i][j] + A[i]);
    else chmax(dp[i+1][j], dp[i][j]);
  }
  cout << dp[n][k] << "\n";
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
bool prime[MAX];
int prime_numbers[9999999],pos;
void sieve()
{
    prime[0]=1;
    prime[1]=1;
    for(int i=4; i<=MAX; i+=2)
        prime[i]=1;
    int root = sqrt(MAX);
    for(int i=3; i<=root; i+=2)
    {
        if(prime[i]==0)
        {
            for(int j=i*i; j<=MAX; j+=i)
            {
                prime[j]=1;
            }
        }
    }
    prime_numbers[pos++]=2;
    for(int i=3; i<MAX; i+=2)
        if(prime[i]==0)
            prime_numbers[pos++]=i;
}
map<int,int>mp;
void recur(int pos,int sum,int taken,vector<int>v)
{
    if(pos>=20)
        return;
    if(taken==5)
    {
        if(sum==67)
        {
           for(int i=0;i<5;i++)
            cout<<v[i]<<" ";
           cout<<endl;
        }

        mp[sum]++;
        return ;
    }
    v.push_back(prime_numbers[pos]);
    recur(pos+1,sum+prime_numbers[pos],taken+1,v);
    v.pop_back();
    recur(pos+1,sum,taken,v);
}
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    sieve();
 //   for(int i=0; prime_numbers[i]<55555; i++)
    //    cout<<prime_numbers[i]<<" "<<mp[prime_numbers[i]]<<endl;
    int n;
    cin>>n;
    for(int i=0;i<pos;i++){
        if(prime_numbers[i]%10==7){
            cout<<prime_numbers[i]<<" ";
         n--;
        }
        if(n==0)
            break;
    }

    return 0;
}


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

int main() {
    int n;
    long long int ans = 3;
    cin >> n;
    int a[n], rgb[3] = {0};
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }
  	if(a[0] != 0) {
    	cout << 0 << '\n';
      	return 0;
    }
    rgb[0] = 1;
    for(int i = 1; i < n; ++i) {
        int sum = 0;
        for(int j = 0; j < 3; ++j) {
            if(rgb[j] == a[i]) sum++;
        }
        ans *= sum;
        ans %= MOD;
        for(int j = 0; j < 3; ++j) {
            if(rgb[j] == a[i]) {
                rgb[j]++;
                break;
            }
        }
    }
    cout << ans << '\n';

    return 0;
}

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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;

int n;
int a[maxn];

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

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

    multiset<int> ms;
    ms.insert(0);ms.insert(0);ms.insert(0);

    ll res = 1;

    for (int i=0; i<n; i++) {
	res=res*ms.count(a[i])%mod;
	if (res==0) out(0);
	ms.erase(ms.find(a[i]));
	ms.insert(a[i]+1);
    }
    cout<<res<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define Nmax 210
#define M 63
using namespace std;
typedef long long ll;
string s;
int N, n, T;
bitset<M> A[Nmax];
ll a[Nmax];

bool gauss() { //true if linearly dep
  int i=0,j=0;

  while(i<N && j<M) {
    int k;
    for(k=i;k<N;++k) if(A[k][j]) break;
    if(k==N) { ++j; continue; }
    if(k!=i) swap(A[i], A[k]);

    for(int u=i+1;u<N;++u) {
      for(int q=j+1;q<M;++q) A[u][q] = (A[u][q] ^ (A[u][j]*A[i][q]));
      A[u][j]=0;
    }
    ++i; ++j;
  }

  return (A[N-1] == 0);
}

int main() {

  cin>>T;
  while(T--) {
    cin>>n;
    N = 0;
    for(int i=0;i<n;++i) cin>>a[i];

    cin>>s;
    if(s[N-1] == '1') {
      cout<<"1\n";
      continue;
    }
    bool ok = true;

    for(int i=n-1;i>=0;--i) {
      A[N++] = a[i];
      if(s[i] == '1') {
        if(!gauss()) {
          ok = false;
          break;
        }
        --N;
      } else {
        if(gauss()) {
          --N;
        }
      }
    }

    if(!ok) {
      cout<<"1\n";
    } else {
      cout<<"0\n";
    }
  }
  return 0;
}

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
#define endl '\n'
#define ld long double
#define all(a) a.begin(),a.end()
#define int long long
#define pb push_back
#define pii pair <int, int>
#define ff first
#define ss second
#define sz(v) (int)v.size()
#define UB upper_bound
#define LB lower_bound
#define BP(x) __builtin_popcountll(x)
#define OST tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

const int INF = 1e18 + 0;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int MAXLOG = 62;

struct basis {
  int base[MAXLOG];

  void clear() {
   for(int i = MAXLOG - 1; i >= 0; i--) base[i] = 0;
  }
  void prepare(int val) {
    for(int i = MAXLOG - 1; i >= 0; i--) {
      if((val >> i) & 1) {
      if(!base[i]) { base[i] = val; return; }
      else val ^= base[i];
    }
  }
 }
 int size() {
   int c = 0;
   for(int i = 0; i < MAXLOG; i++) c += (bool)(base[i]);
   return c;
 }
 int max_xor() {
  int res = 0;
  for(int i = MAXLOG - 1; i >= 0; i--)
    if(!((res >> i) & 1) && base[i]) res ^= base[i];
  return res;
 }
 bool possible(int val) {
   for(int i = MAXLOG - 1; i >= 0; i--)
     if(((val >> i) & 1) && base[i]) val ^= base[i];
   return (val == 0);
  }
};

void solve() {
  int n; cin >> n;
  int data[n+5];
  for(int i = 0; i < n; i++) cin >> data[i];
  basis b; b.clear();
  string s; cin >> s;
  for(int i = n-1; i >= 0; i--) {
     if(s[i] == '1') {
       if(!b.possible(data[i])) {
        cout << "1\n"; return;
       }
     } else {
      b.prepare(data[i]);
     }
  }
  cout << "0\n";
}

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

  #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
  #endif
   int t = 1;  cin >> t;
   for(int i = 1; i <= t; i++) {
     solve();
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)

using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

int n, a, b, x[100000];

int main(){
	//IO("input.txt", "output.txt");
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> a >> b >> x[0];
	long long cnt = 0;
	for(int i = 1; i < n; i++){
		cin >> x[i];
		cnt += min((long long)b, a * (long long)(x[i] - x[i - 1]));
	}
	cout << cnt << "\n";
	return 0;
}

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
#define REP(i,n) for(ll i = 0;i < (ll)n;i++)
#define REPD(i,n) for(ll i = (ll)n - 1;i >= 0;i--)
#define ALL(x) (x).begin(),(x).end()
#define FILLl(n,first_dimension_size,value) fill((ll*)n,(ll*)(n+first_dimension_size),value)
#define FILL(n,first_dimension_size,value) fill((int*)n,(int*)(n+first_dimension_size),value)
#define INF (1 << 30)
#define MOD 1000000007
//int dy[4] = {-1,0,0,1};
//int dx[4] = {0,1,-1,0};

int main(){

  int n,a,b;
  cin >> n >> a >> b;
  vector<ll> v(n);
  REP(i,n)cin >> v[i];
  vector<ll> dist(n);
  REP(i,n-1)dist[i] = v[i+1]-v[i];
  ll res = 0;
  REP(i,n-1)dist[i]*a <= b ? res += dist[i]*a : res += b;
  cout << res << endl;

  return 0;
}


#include <iostream>
using namespace std;
int Rates(int A){
     if (A>=10){
          return 0;
     }else{
          return 100*(10-A);
     }
}
int main(){
     int N,R;
     cin >>N>>R;
     cout <<R+Rates(N)<<endl;
}

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

#define ll long long

int main()
{
    int N, R;
    std::cin >> N >> R;

    if(N >= 10) {
        std::cout << R << std::endl;
    }
    else {
        std::cout << R + (10-N)*100 << std::endl;
    }
}


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

vector<ll> pw2(51,1);vector<ll> pw10(19,1);
ll high_order_bit(ll n){
    ll tmp=1,i=-1;
    while(tmp<=n){
        tmp*=2;
        i++;
    }
    if(n==0) return 0;
    else return i;
}
vector<ll> binary_expansion(ll n){
    vector<ll> B(high_order_bit(n)+1);
    ll tmp=n;
    for(ll i=high_order_bit(n);i>=0;i--){
        B[i]=tmp/pw2[i];
        tmp%=pw2[i];
    }
    return B;
}
vector<ll> divisor(ll x){
    vector<ll> div;
    for(int i=1;i*i<=x;i++){
        if(i*i!=x && x%i==0){
            div.pb(i);
            div.pb(x/i);
        }
        else if(x%i==0) div.pb(i);
    }
    return div;
}
vector<ll> vectorprime(ll x){
    vector<bool> is_prime(x+1);
    vector<ll> prime;
    rep(i,x+1) is_prime[i]=true;
    is_prime[0]=is_prime[1]=false;
    for(int i=2;i<=x;i++){
        if(is_prime[i]){
            prime.pb(i);
        }
        for(int j=i*2;j<=x;j+=i) is_prime[j]=false;
    }
    return prime;
}

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

    ll N; cin>>N;
    ll count=0;
    vector<ll> V=vectorprime(55555);
    rep(i,sz(V)){
        if(V[i]%5==2){
            cout<<V[i]<<" ";
            count++;
        }
        if(count==N) break;
    }
}

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

int main() {
    int N;
    cin >> N ;
    string str = to_string(N);
    int length= str.size();
    int div=0;

    for(int i=0; i<length; i++){
        div+=(int)(str[i] - '0');
    }
    if(N%div == 0){
        cout<< "Yes" <<endl;
    }else{
        cout<< "No" <<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
//#define fr(i,j,k) for(int i=j;i<k;i++)
//#define f(n) fr(i,0,n)
//#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const ll mod = 1e11 + 7;
const int maxn = 2e5+5;
#include<atcoder/all>
using namespace atcoder;
void solve() {
    int n, m;
    cin >> n >> m;
    fenwick_tree<ll>b(n);
    for(int i = 0 ; i < n ; i++) {
        ll x;
        cin >> x;
        b.add(i,x);
    }
    while (m--) {
        int op;
        cin >> op;
        if (!op) {
            int p, x;
            cin >> p >> x;
            b.add(p,x);
        }
        else {
            int l, r;
            cin >> l >> r;
            cout << b.sum(l, r) << '\n';
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        solve();
    }

}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define LOL cout<<'\n';
#define MOD 1000000007
#define sz(x) ((int)x.size())
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define deb(x) cout << "[" << (#x) << "=" << x << "]" << '\n';
#define deb2(x,y) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "]" << '\n';
#define deb3(x,y,z) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "] [" << (#z) << "=" << z << "]" << '\n';

const int N=1e5+7;
int n,m,k,q,l,r,x,y,z;
int a[N],b[N],c[N];
string s,t;
int ans=0,cnt=0;

void solve(){
	cin>>n>>m;
	cout<<n*m;LOL
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int _=1; //cin>>_;
  for(int i=1 ; i<=_ ; i++) solve();
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(),(c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

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

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 1e4 + 10;

int dp[N][2][100];

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

	string k;
	int d;
	cin >> k >> d;
	int n = k.length();
	dp[0][1][0] = 1;
	for(int i = 1; i <= n; i++) {
		int cur = k[i - 1] - '0';
		for(int j = 0; j <= 9; j++) {
			for(int rem = 0; rem < d; rem++) {
				dp[i][0][(rem + j) % d] = (dp[i][0][(rem + j) % d] + dp[i - 1][0][rem]) % mod;
				if(j < cur) {
					dp[i][0][(rem + j) % d] = (dp[i][0][(rem + j) % d] + dp[i - 1][1][rem]) % mod;
				}
				if(j == cur) {
					dp[i][1][(rem + j) % d] = (dp[i][1][(rem + j) % d] + dp[i - 1][1][rem]) % mod;
				}
			}
		}
	}
	cout << (dp[n][0][0] + dp[n][1][0] - 1 + mod) % mod;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
static const int64_t mod=1000000007;
int64_t f(int64_t x,int64_t y){
if(y==0)
  return 1;
  else if(y%2==0)
    return (f(x,y/2)*f(x,y/2))%mod;
  else
    return (x*((f(x,y/2)*f(x,y/2))%mod))%mod;
}
int64_t comb(int64_t n,int64_t k){
int64_t a=1;
  for(int64_t i=1;i<=k;i++){
  a=(a*(n+1-i))%mod;
    a=(a*f(i,mod-2))%mod;
  }return a;

}
int main(){
int64_t N,M,K;
  cin>>N>>M>>K;
  int64_t ans=0;
  for(int64_t i=1;i<M;i++)
    ans=(ans+i*(M-i))%mod;
  ans=(((ans*N)%mod)*N)%mod;
  int64_t ans1=0;
  for(int64_t i=1;i<N;i++)
    ans1=(ans1+i*(N-i))%mod;
  ans1=(((ans1*M)%mod)*M)%mod;
  ans=(ans+ans1)%mod;
  ans=(ans*comb(N*M-2,K-2))%mod;
  cout<<ans<<endl;
    return 0;
}


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

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

    set<int>st;
    int input;
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        cin>> input;

        st.insert(input);

        if(input == *st.begin())
            ans++;
    }

    cout<< ans;

    return 0;
}


#include <stdio.h>

int main(void)
{
	int day[13][32];
	int i, j;
	int n = 4;
	int month;
	int da;

	for (i = 1; i <= 12; i++){
		 if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
			for (j = 1; j <= 31; j++){
				day[i][j] = n;
				n++;
				if (n == 8){
					n = 1;
				}
			}
		}
		else if (i == 4 || i == 6 || i == 9 || i == 11){
			for (j = 1; j <= 30; j++){
				day[i][j] = n;
				n++;
				if (n == 8){
					n = 1;
				}
			}
		}
		else if (i == 2){
			for (j = 1; j <= 29; j++){
				day[i][j] = n;
				n++;
				if (n == 8){
					n = 1;
				}
			}
		}
	}

	while (1){

		scanf("%d %d", &month, &da);
		if (month == 0){
			return (0);
		}

		//printf("%d\n", day[month][da]);

		switch (day[month][da]){
			case 1:
			printf("Monday\n");
			break;

			case 2:
			printf("Tuesday\n");
			break;

			case 3:
			printf("Wednesday\n");
			break;

			case 4:
			printf("Thursday\n");
			break;

			case 5:
			printf("Friday\n");
			break;

			case 6:
			printf("Saturday\n");
			break;

			case 7:
			printf("Sunday\n");
			break;
		}
	}
}

#include <iostream>
#include <vector>
#include <string>

int main()
{
	std::vector<std::string> day_of_week{
		"Thursday",
		"Friday",
		"Saturday",
		"Sunday",
		"Monday",
		"Tuesday",
		"Wednesday"
	};

	std::vector<int> day_num_of_month{
		31,
		29,
		31,
		30,
		31,
		30,
		31,
		31,
		30,
		31,
		30,
		31
	};

	int month = 0;
	int day = 0;

	while (std::cin >> month >> day)
	{
		if (0 == month) { break; }

		int days_elapsed = 0;
		for (int m = 1; m < month; ++m)
		{
			days_elapsed += day_num_of_month[m - 1];
		}
		days_elapsed += day;

		std::cout << day_of_week[(days_elapsed - 1) % 7] << std::endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll n,m,k,ans=0,tmp=1;
ll power(ll a,ll b) {return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
int main()
{
    cin>>n>>m>>k;
    for(ll i=0;i<=k;i++){
        ans=(ans+(tmp*m%mod)*power(m-1,n-i-1))%mod;
        tmp=tmp*(n-i-1)%mod*power(i+1,mod-2)%mod;
    }
    cout<<ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define MAX 100001
#define MOD 1000000007
#define ll long long
#define ld long double
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
int main() {
	fast;
	int t = 1;
	// cin >> t;
    while(t--) {
		int n, x, t;
		cin >> n >> x >> t;
		int ans = (n / x) * t;
		if(n % x != 0) ans += t;
		cout << ans << endl;
    }
}


#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>

// ===============================================================

using namespace std;
using ll = long long;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;

// ===============================================================

ll gcd(ll a, ll b)
{
	if (a % b == 0)
	{
		return(b);
	}
	else
	{
		return(gcd(b, a % b));
	}
}
//最大公約数
ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
//最小公倍数
ll box(double a)
{
	ll b = a;
	return b;
}
//切り捨て
ll fff(double a)
{
	ll b = a + 0.5;
	return b;
}
//四捨五入
ll sum(ll n) {
	return (1 + n) * n / 2;
}
//1から整数nまでの合計
bool prime(ll a)//素数判定、primeならtrue,違うならfalse
{
	if (a < 2) return false;
	else if (a == 2) return true;
	else if (a % 2 == 0) return false;
	double sqrtNum = sqrt(a);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (a % i == 0)
		{
			return false;
		}
	}

	// 素数である
	return true;
}
//素数判定

// ===============================================================

int main() {
	ll n;
	cin >> n;
	string a;
	cin >> a;
	vl x(n, 0);
	vl y(n, 0);
	for (int i = 0; i < n; i++) {
		if (a[i] == 'E') {
			if (i == 0) {
				x[i] = 1;
				y[i] = 0;
			}
			else {
				x[i] = x[i - 1] + 1;
				y[i] = y[i - 1];
			}
		}
		if (a[i] == 'W') {
			if (i == 0) {
				y[i] = 1;
				x[i] = 0;
			}
			else {
				y[i] = y[i - 1] + 1;
				x[i] = x[i - 1];
			}
		}
	}//x=E y=W
	ll count = 0;
	ll ans = 100000000;
	for (int i = 0; i < n; i++) {
		if (i != 0) {
			count = (x[n - 1] - x[i]) + y[i - 1];
		}
		else {
			count = x[n - 1] - x[i];
		}
		if (min(ans, count) == count) {
			ans = count;
		}
		count = 0;
	}
	cout << ans << endl;
}

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

typedef long long ll;
typedef vector<int> vi;

int N; string S; vi l,r;
void input()
{
    cin>>N>>S;
}

void solve()
{
    int count=0;
    for(int i=0;i<N;++i) {
        if(S[i]=='W') ++count;
        l.emplace_back(count);
    }
    count=0;
    for(int i=0;i<N;++i) {
        if(S[N-1-i]=='E') ++count;
        r.emplace_back(count);
    }
    int MIN=100000000;
    for(int i=0;i<N;++i) {
        int tmp=0;
        if(i>0) tmp+=l[i-1];
        if(i<N-1) tmp+=r[N-2-i];
        MIN=min(MIN,tmp);
    }
    cout<<MIN<<endl;
}

int main()
{
    cin.tie();
    ios::sync_with_stdio(false);
    input();
    solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl/*, greater<ll>*/> pql; //降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for(ll i = 1; i <= ll(n); ++i)
//#define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
//#define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';

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

  ll a, b, c;
  cin >> a >> b >> c;

  cout << ((a-c)*(c-b)>=0 ? "Yes" : "No") << newl;
  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define dunk(a) cout << (a) << endl
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int a, b, c;
	cin >> a >> b >> c;

	if ((a < c && b > c) || (a > c&& b < c)) puts("Yes");
	else puts("No");

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
using Graph = vector<vector<int>>;
using Graphl = vector<vector<ll>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a)  (a).begin(),(a).end()
#define rALL(a)  (a).rbegin(),(a).rend()
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; }
template<class T> void PrintVector(const vector<T> &vec) {for (auto val : vec) cout << val << " "; cout << endl;}
const long long INF = 1LL << 60;
const long long minusINF = -(1LL << 40);
const int MOD = 1000000007;
const double PI = acos(-1); //3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

vector<ll> divisor(ll n)
{
    vector<ll> ret;
    for (ll i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end());
    return ret;
}

template<class T> int former(const vector<T> &v, T x) {
    return upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
}

int main()
{
    ll n, m; cin >> n >> m;
    vector<ll> a = divisor(m);
    cout << a[former(a, m/n)] << endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <cstdio>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
# define ll int64_t
# define str string
# define rep(i,n) for(ll i=0;i<n;i++)
# define rrep(i,n) for(ll i=1;i<=n;i++)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define mod 1000000007
# define PI 3.141592653589793
# define vec vector
#define dump(x) cerr<<#x<<"="<<x<<endl
using namespace std;

#define INF 2000000000
#define MAX_V 10
const int MAX_N =10000000;

bool compare_by_b(pair<string,ll> a,pair<string,ll> b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}

bool my_compare(pair<ll,ll> a,pair<ll,ll> b){
    if(a.first != b.first) return a.first<b.first;
    if(a.second != b.second) return a.second>b.second;
    else return true;
}

ll modpow(ll a,ll n,ll mod1) {
    ll res=1;
    while(n>0){
        if(n&1) res=res*a%mod1;
        a = a*a%mod1;
        n >>= 1;
    }
    return res;
}

ll factorial(ll n){
    ll x=1;
    rrep(i,n) x*=i;
    return x;
}

ll gcd(ll a,ll b) { return b ? gcd(b, a%b) : a; }

ll func(ll a,ll b,ll& ans_x,ll& ans_y){
    if(b == 0){
        ans_x = 1;
        ans_y = 0;
        return a;
    }else{
        ll ret = func(b,(a%b),ans_y,ans_x);
        ans_y -= (a/b)*ans_x;
        return ret;
    }
}

ll par[MAX_N],ranking[MAX_N];

void init(ll n){
    rep(i,n){
        par[i]=i;
        ranking[i]=0;
    }
}

ll find(ll x){
    if(par[x]==x) return x;
    else return par[x]=find(par[x]);
}

void unite(ll x,ll y){
    x=find(x);
    y=find(y);
    if(x==y) return;

    if(ranking[x]<ranking[y]) par[x]=y;
    else{
        par[y]=x;
        if(ranking[x]==ranking[y]) ranking[x]++;
    }
}

bool same(ll x,ll y){
    return find(x)==find(y);
}

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

    ll n,m;
    cin>>n>>m;
    map<ll,ll> mp;
    for(ll i=1;i*i<=m;i++){
        if(m%i==0){
            if(i==m/i) mp[i]+=m/i;
            else {
                mp[m/i]+=i;
                mp[i]+=m/i;
            }
        }
    }
    ll maxi=1;
    for(auto p:mp){
        if(p.second>=n || p.first<=m/n) maxi=max(p.first,maxi);
    }
    cout<<maxi<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;

vector< pair<int,int> > V;
vector< pair< pair<int,int>,int> > E;
vector<pair<int,int> > G[500005];
int dist[500005];

map<pair<int,int>,int> mp;
int main(){
    int N,M;
    cin>>N>>M;
    for(int i = 0;i < M;i++){
        int p,q,c;
        cin>>p>>q>>c;
        E.push_back(make_pair(make_pair(p,q),c));
        V.push_back(make_pair(p,c));
        V.push_back(make_pair(q,c));
    }
    for(int i = 1;i <= N;i++){V.push_back(make_pair(i,0));}
    for(int i = 0;i<500005;i++)dist[i]=-1;
    sort(V.begin(),V.end());
    V.resize(unique(V.begin(),V.end()) -V.begin());
    int vsize = V.size();
    for(int i = 0;i < vsize;i++){
        mp[V[i]]=i;
    }
    for(int i = 0;i < E.size();i++){
        int p,q,c;
        p=E[i].first.first;
        q=E[i].first.second;
        c=E[i].second;
        G[mp[make_pair(p,c)]].push_back(make_pair(0,mp[make_pair(q,c)]));
        G[mp[make_pair(q,c)]].push_back(make_pair(0,mp[make_pair(p,c)]));
        G[mp[make_pair(p,c)]].push_back(make_pair(0,mp[make_pair(p,0)]));
        G[mp[make_pair(p,0)]].push_back(make_pair(1,mp[make_pair(p,c)]));
        G[mp[make_pair(q,c)]].push_back(make_pair(0,mp[make_pair(q,0)]));
        G[mp[make_pair(q,0)]].push_back(make_pair(1,mp[make_pair(q,c)]));
    }
    priority_queue<pair<int,int> ,vector<pair<int,int> > ,greater<pair<int,int> > > pq;
    pq.push(make_pair(0,mp[make_pair(1,0)]));
    while(!pq.empty()){
        pair<int,int> top = pq.top();
        pq.pop();
        int d = top.first;
        int v = top.second;
        //cout<<"a: "<<d<<" "<<V[v].first<<" "<<V[v].second<<" "<<endl;
        if(dist[v]==-1||dist[v] > d){
            dist[v] = d;
            for(int i = 0; i < G[v].size(); i++){
                pq.push(make_pair(G[v][i].first + d, G[v][i].second));
            }
        }
    }
    cout<<dist[mp[make_pair(N,0)]]<<endl;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, m;
    cin >> n >> m;
    vector<int>v(m + 3), v2, stick(12), val(12, -1), vis(12, 0);
    stick[1] = 2, stick[2] = 5, stick[3] = 5, stick[4] = 4, stick[5] = 5, stick[6] = 6, stick[7] = 3, stick[8] = 7, stick[9] = 6;
    for(int i = 0; i < m; i++){
        cin >> v[i];
        if(vis[stick[v[i]]] == 0){
            vis[stick[v[i]]] = 1;
            v2.push_back(stick[v[i]]);
        }
        val[stick[v[i]]] = max(val[stick[v[i]]], v[i]);
    }
    int dp[n + 3];
    for(int i = 0; i <= n; i++){
        dp[i] = -1;
    }
    dp[0] = 0;
    for(int i = 0; i < v2.size(); i++){
        for(int j = v2[i]; j <= n; j++){
            if(dp[j - v2[i]] != -1){
                dp[j] = max(dp[j], dp[j - v2[i]] + 1);
            }
        }
    }
    vector<pair<int, int> >vec;
    for(int i = 0; i < v2.size(); i++){
        vec.push_back(make_pair(val[v2[i]], v2[i]));
    }
    sort(vec.rbegin(), vec.rend());
    string s;
    int cnt = 0;
    while(n > 0){
        cnt++;
        for(int j = 0; j < vec.size(); j++){
            if(n - vec[j].second >= 0 && dp[n] == dp[n - vec[j].second] + 1){
                s.push_back('0' + vec[j].first);
                n = n - vec[j].second;
                break;
            }
        }
    }
    cout << s;
    return 0;
}


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

using namespace std;

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

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

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

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

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

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

signed main() {
	int n,mi=INF,ans=1;
	cin >> n;
	for (int i = 1; i*i <= n; i++) {
		if (n % i == 0) {
			chmin(mi,n / i);
		}
	}
	if (mi == INF)mi = n;
	while (mi >= 10) {
		mi /= 10;
		ans++;
	}
	cout << ans << endl;
}

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

int main() {
  ll n;
  cin >> n;
  ll m = (ll)sqrt((double)n);

  auto d = [](ll x) {
    ll res = 0;
    while (x) {
      x /= 10;
      res++;
    }
    return res;
  };

  rrep(i, 1, m + 1) {
    if (n % i == 0) {
      cout << max(d(i), d(n / i)) << '\n';
      return 0;
    }
  }
  return 0;
}


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

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

using namespace std;

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

  int minv = 1000;
  for (int i = 0; i < s.size() - 2; ++i) {
    minv = min(minv, abs(stoi(s.substr(i, 3)) - 753));
  }

  std::cout << minv << std::endl;
  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()
typedef long long ll;

int main(){
    int n;
    cin >> n;
    vector <int> s(4);
    rep(i,4){
        s[3-i] = n%10;
        n /= 10;
    }

    vector <char> ansi(3);
    for(int i=0; i<(1<<3); i++){
        bitset<3> a(i);
        int tmp = s[0];
        for(int j=0; j<3; j++){
            if(a.test(j)){
                tmp += s[j+1];
                ansi[j] = '+';
            }
            else{
                tmp -= s[j+1];
                ansi[j] = '-';
            }
        }
        if(tmp == 7) break;
    }

    rep(i,3){
        cout << s[i] << ansi[i];
    }
    cout << s[3];
    cout << "=7" << endl;

}


#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

class Graph{
	private:
	int V;
	public:
	vvi Pro;
	Graph(int v){
		V=v;
		Pro=vvi(v,vi(v,inf));
		for(int i=0;i<v;i++) Pro[i][i]=0;
	}
	void add_edge(int x,int y,int c){
		Pro[x][y]=c;
		Pro[y][x]=c;
	}
	void Warshall(){
		for(int k=0;k<V;k++){
			for(int i=0;i<V;i++){
				for(int j=0;j<V;j++){
					Pro[i][j]=min(Pro[i][j],Pro[i][k]+Pro[k][j]);
				}
			}
		}
	}
};

int V,E,N;

int main(){
	while(1){
		cin>>E>>V;
		if(!V&&!E) break;
		Graph cost(V),time(V);
		int s,t,c,l;
		for(int i=0;i<E;i++){
			cin>>s>>t>>c>>l;
			s--;
			t--;
			cost.add_edge(s,t,c);
			time.add_edge(s,t,l);
		}
		cost.Warshall();
		time.Warshall();
		cin>>N;
		bool r;
		for(int i=0;i<N;i++){
			cin>>s>>t>>r;
			s--;
			t--;
			if(r) cout<<time.Pro[s][t]<<endl;
			else cout<<cost.Pro[s][t]<<endl;
		}
	}
}

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
	int n;
	int ary[100][100];

while(1)
	{
		cin >> n;

		int sumx = 0;
		int sumy = 0;
		int sumxy = 0;

		if(n == 0)
		{
			break;
		}

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

			for(int i = 0; i < n; i++){
				sumx = 0;
				for(int j = 0; j < n; j++)
				{
					sumx += ary[i][j];
					sumxy += ary[i][j];

					printf("%5d",ary[i][j] );
					//ary[i][j] << " ";//????????´???
				}
				printf("%5d", sumx);
				cout << endl;
			}

			for(int i = 0; i < n; i++){
				sumy = 0;
				for(int j = 0; j < n; j++)
			{
					sumy += ary[j][i];
				}
				printf("%5d", sumy);
			}
			printf("%5d\n", sumxy);
	}
}


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

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}

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

    ll a[n];
    int flag = 0;
    ll max = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(max < a[i]){
            max = a[i];
        }
        if(a[i] == k){
            flag = 1;
        }
    }

    if(flag == 0){
        ll b = a[0];
        for(int i = 1; i < n; i++){
            b = gcd(b, a[i]);

            if(b == 1){
                break;
            }
        }

        if(k % b == 0 && k <= max){
            flag = 1;
        }
    }

    if(flag == 0){
        cout << "IMPOSSIBLE" << endl;
    }else{
        cout << "POSSIBLE" << endl;
    }
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    int N = 0;
    cin >> N;
    int K;
    cin >> K;
    vector<int> A(N + 1,0);
    for(int i = 0; i < N; i++) {
        cin >> A.at(i + 1);
    }
    for(int i = 1; i < N; i++) {
        A.at(i + 1) += A.at(i);
    }
    int ans = 0;
    for(int i = 0; i < N - K + 1; i++) {
        ans = max(ans, A.at(i + K) - A.at(i));
    }
    cout << fixed << setprecision(15) << (double)(ans + K)/2 << endl;

    return 0;
}


#if 0
2019.12.30

朴素 dp 设 f[i][p][q] 表示前 i 次操作后当前棋子为 (p, q) 的最小代价。
事实上由于第 i 次操作后一定有一个棋子是 x[i] ，因此可以优化一维：
f[i][j] 表示前 i 次操作后当前棋子为 (j, x[i]) 的最小代价。

转移有两种：
1) f[i - 1][j] + |x[i - 1] - x[i]| -> f[i][j]
2) min(f[i - 1][k] + |x[i] - k|) -> f[i][x[i - 1]]
用线段树维护 f[~][j] ，第一种转移就是全局加，第二种是单点取 min 。
第二种转移可以把左边的绝对值拆开：
f[i - 1][k] - k + x[i] (k < x[i])
f[i - 1][k] (k = x[i])
f[i - 1][k] + k - x[i] (k > x[i])
线段树分别维护 f[~][j] - j, f[~][j], f[~][j] + j 即可。
#endif
#include <cstdio>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
	inline operator ll () { ll x; return scanf("%lld", &x), x; }
	template<class T> inline void operator () (T &x) { x = *this; }
	template<class T, class ...A> inline void operator () (T &x, A &...a)
	{ x = *this; this -> operator ()(a...); }
} read;

const int maxn = 200005;
ll f[maxn << 2], fa[maxn << 2], fb[maxn << 2], tag;

void modify(int now, int L, int R, int p, ll x) {
	f[now] = std::min(f[now], x);
	fa[now] = std::min(fa[now], x + p);
	fb[now] = std::min(fb[now], x - p);
	if(L == R) return;
	int M = (L + R) >> 1;
	if(p <= M) modify(now << 1, L, M, p, x);
	else modify(now << 1 | 1, M + 1, R, p, x);
}

void build(int now, int L, int R) {
	f[now] = fa[now] = fb[now] = 1000000000000000000;
	if(L == R) return;
	int M = (L + R) >> 1;
	build(now << 1, L, M);
	build(now << 1 | 1, M + 1, R);
}

ll query(int now, int L, int R, int p) {
	if(R < p) return p + fb[now];
	if(L > p) return fa[now] - p;
	if(L == R) return f[now];
	int M = (L + R) >> 1;
	return std::min(query(now << 1, L, M, p), query(now << 1 | 1, M + 1, R, p));
}

int main() {
	int n = read, q = read, a = read, b = read;

	build(1, 1, n);
	modify(1, 1, n, a, 0);
	/* modify(1, 1, n, b, 0); */

	int last = b;
	while(q --) {
		int x = read;
		ll qu = query(1, 1, n, x) + tag;
		/* debug("qu = %lld\n", qu); */
		tag += std::abs(x - last);
		modify(1, 1, n, last, qu - tag);
		last = x;
		/* debug("tag=%lld\n", tag); */
	}

	printf("%lld\n", f[1] + tag);
}

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll n, x;
  cin >> n >> x;
  vector<ll> vec(n);
  for (auto &v : vec)
    cin >> v;

  ll ans = 0;
  for (ll i = 1; i< n; ++i) {
    ll vsum = vec[i] + vec[i-1];
    if (vsum <= x)
      continue;
    else if (vsum - x <= vec[i])
      vec[i] -= vsum - x;
    else
      vec[i] = 0;
    ans += vsum - x;
  }

  cout << ans << endl;
}


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

#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1

#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;

int main() {

    string s;
    cin>>s;
    if(s=="hi"||s=="hihi"||s=="hihihi"||s=="hihihihi"||s=="hihihihihi"){
        puts("Yes");
    }else{
        puts("No");
    }

    return 0;
}

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

int main() {
string s;
cin >> s;
bool ans = true;
rep(i,s.size()){
  if(s.size()%2!=0){
    ans = false;
    break;
  }
  if(s.at(i)!='h' || s.at(i+1)!='i') ans = false;
  i++;

}
cout << (ans?"Yes":"No") << endl;
}


#include<iostream>
#include<iomanip>
#include<functional>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
#include<tuple>
#include<map>
#include<set>
#include<queue>
#include<cmath>

using namespace std;
#define LL long long

int n,a[111111]={},sum=0;
int main() {
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum+=abs(a[i]-a[i-1]);
	}
	sum+=abs(a[n]);
	for(int i=1;i<=n;i++){
		int tmp=abs(a[i-1]-a[i+1])-abs(a[i]-a[i-1])-abs(a[i+1]-a[i]);
		cout<<sum+tmp<<endl;
	}
	return 0;
}

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
  int n;
  scanf("%d", &n);
  int S[n];
  for (int i = 0; i < n; i++)
  {
    scanf("%d", &S[i]);
  }
  int q;
  scanf("%d", &q);
  int T[q];
  for (int i = 0; i < q; i++)
  {
    scanf("%d", &T[i]);
  }
  int cnt = 0;
  for (int i = 0; i < q; i++)
  {
    for (int j = 0; j < n; j++)
    {
      if (S[j] == T[i])
      {
        cnt++;
        break;
      }
    }
  }

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int maxn = 4000 + 10;
int n, k;
ll inv[maxn], fac[maxn], facinv[maxn], ans[maxn];
void init()
{
	inv[0] = inv[1] = 1;
	fac[0] = facinv[0] = 1;
	for(int i = 1; i < maxn; ++i)
		fac[i] = fac[i - 1] * i % mod;
	for(int i = 2; i < maxn; ++i)
		inv[i] = (mod - mod / i) * inv[mod % i] % mod;
	for(int i = 1; i < maxn; ++i)
		facinv[i] = facinv[i - 1] * inv[i] % mod;
}
ll C(ll m, ll n)
{
	return fac[m] * facinv[m - n] % mod * facinv[n] % mod;
}
ll norm(ll x)
{
	while(x > 0) x -= mod;
	while(x < 0) x += mod;
	return x;
}
int main()
{
	init();
	scanf("%d%d", &k, &n);
	for(int j = 2; j <= k + 1; ++j)
	{
		ll ret = 0;
		int t = j >> 1;
		for(int i = 0; i <= t && n - 2 * i >= 0; ++i)
		{
			ll tmp = C(t, i) * C(n - 2 * i + k - 1, k - 1) % mod;
			if(i & 1)
				ret = norm(ret - tmp);
			else
				ret = norm(ret + tmp);
 		}
 		ans[j] = ans[2 * k - j + 2] = ret;
 	}
 	for(int j = 2; j <= 2 * k; ++j)
 		printf("%lld\n", ans[j]);
 	return 0;
}

// ※※※ 解答不能 ※※※
// 解き直し.
// scott_wu氏.
// https://atcoder.jp/contests/arc102/submissions/3114772
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL MOD = 998244353;
const int MAXN = 4100;

int K, N;
LL pascal[MAXN][MAXN];

int main(){

    pascal[0][0] = 1;
    for(int i = 1; i < MAXN; i++){
        for (int j = 0; j < MAXN; j++){
            pascal[i][j] = pascal[i - 1][j];
            if(j) pascal[i][j] = (pascal[i][j] + pascal[i - 1][j - 1]) % MOD;
        }
    }

    scanf("%d %d", &K, &N);
    for(int i = 2; i <= 2 * K; i++){
        int n2 = 0, n1 = 0;
        for(int j = 1; j <= K; j++){
            if(j * 2 == i) continue;
            if((i - j) <= K && (i - j) >= 1) n2++;
            else                             n1++;
        }
        n2 /= 2;
        LL res = 0;
        LL p2 = 1;
        for(int j = 0; j <= n2; j++){
            if(j) p2 = (p2 * 2) % MOD;
            if(j > N) continue;
            if(j == 0 && n1 == 0) continue;
            res = (res + pascal[n2][j] * (p2 * pascal[N + n1 - 1][j + n1 - 1] % MOD)) % MOD;
            if(i % 2 == 0) res = (res + pascal[n2][j] * (p2 * pascal[N+n1-2][j+n1-1] % MOD)) % MOD;
        }
        printf("%lld\n", res);
    }

    return 0;

}


#include<iostream>
using namespace std;

class dice {
private:
	int n[7];

public:
	dice(int, int, int, int, int, int);
	int getcur() { return n[1]; }
	void rotate(char);
};

dice::dice(int n1, int n2, int n3, int n4, int n5, int n6) {
	n[1] = n1;
	n[2] = n2;
	n[3] = n3;
	n[4] = n4;
	n[5] = n5;
	n[6] = n6;
}

void dice::rotate(char op) {
	dice copy(n[1], n[2], n[3], n[4], n[5], n[6]);
	switch (op) {
	case'W':
		n[1] = copy.n[3];
		n[3] = copy.n[6];
		n[6] = copy.n[4];
		n[4] = copy.n[1];
		break;
	case'E':
		n[1] = copy.n[4];
		n[3] = copy.n[1];
		n[6] = copy.n[3];
		n[4] = copy.n[6];
		break;
	case'S':
		n[1] = copy.n[5];
		n[2] = copy.n[1];
		n[6] = copy.n[2];
		n[5] = copy.n[6];
		break;
	case'N':
		n[1] = copy.n[2];
		n[2] = copy.n[6];
		n[6] = copy.n[5];
		n[5] = copy.n[1];
		break;
	}

}

int main() {
	int n[7];
	for (int i = 1; i <= 6; i++) {
		cin >> n[i];
	}
	dice a(n[1], n[2], n[3], n[4], n[5], n[6]);

	char op;
	while (cin >> op) {
		a.rotate(op);
		//cerr << a.getcur() << endl;
	}
	cout << a.getcur() << endl;
	return 0;
}

#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<utility>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;

using ll = long long;

class Stream{
public:
    Stream(){

    }
    //配列以外の入力を受け取る
    void read(){

    }
    template<typename First, typename... Rest>
    void read(First& first, Rest&... rest){
        cin >> first;
        read(rest...);
    }
    template<typename T>
    vector<T> read_vec(size_t n){
        vector<T> tmp(n);
        for(size_t i = 0; i < n; i++){
            cin >> tmp[i];
        }
        return tmp;
    }
    //配列を区切って出力する
    template<typename T>
    void write_vec(vector<T>& v, char divider){
        for(size_t i = 0; i < v.size(); i++){
            cout << v[i] << divider;
        }
    }
};

int main(){
    Stream io;

    int N;
    io.read(N);
    int ans;
    for(ans = 1; (ans + 1) * (ans + 1) <= N; ans++);
    cout << ans * ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int ll

#define rep(i, a) for (int i = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int i = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int i = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define RS resize
#define CINV(v, N) do {\
	v.RS(N);\
	rep(i, N) cin >> v[i];\
} while (0);
#define RCINV(v, N) do {\
	v.RS(N);\
	rrep(i, N) cin >> v[i];\
} while (0);

#define MOD 998244353

template<class T>
inline T GET() {
	T x;
	cin >> x;
	return x;
}

void init();
void solve();

signed main()
{
	init();
	solve();
}

ll dp[2020][2020];
ll sum;
int K, N;

void init()
{
	cin >> K >> N;
	rep(i, K) {
		dp[N-1][i] = 1;
	}
	rrep(i, N-1) {
		ll a = 0;
		rrep(j, K) {
			a += dp[i+1][j];
			a %= MOD;
			dp[i][j] = a;
		}
	}
	rep(i, K) {
		sum += dp[0][i];
		sum %= MOD;
	}
}

struct comb {
#define MOD_COMB MOD
	vector<ll> fact;
	vector<ll> facti;
	comb(int n) {
		init(n);
	}
	void init(int n) {
		fact.resize(n);
		facti.resize(n);
		fact[0] = 1;
		for (int i = 1; i < n; i++) {
			fact[i] = fact[i-1] * i % MOD_COMB;
		}
		facti[n-1] = po(fact[n-1], MOD_COMB - 2);
		for (int i = n-2; i >= 0; i--) {
			facti[i] = facti[i+1] * (i + 1) % MOD_COMB;
		}
	}
	ll nCr(int a, int b) {
		return (fact[a] * facti[b] % MOD_COMB) * facti[a-b] % MOD_COMB;
	}
private:
	ll po(ll next, int cnt) {
		ll res = 1;
		if (cnt == 0) return 1;
		if (cnt & 1) res = res * next % MOD_COMB;
		return res * po(next * next % MOD_COMB, cnt >> 1) % MOD_COMB;
	}
};

void solve()
{
	vector<ll> res;
	comb co(2020);
	if (K == 1) {
		cout << 0 << endl;
		return;
	}
	int i = 0;
	reps(cnt, 3, K+3) {
		ll hugo = 1;
		ll ans = 0;
		rep(j, i+1) {
			ans += hugo * co.nCr(i+1, j+1) * dp[j*2+1][0];
			ans %= MOD;
			hugo *= -1;
		}
		res.PB(ans);
		res.PB(ans);
		cnt++;
		i++;
	}
	if (K%2) res.pop_back();
	rep(i, res.size()) {
		cout << (sum - res[i] + MOD) % MOD << endl;
	}
	rrep(i, res.size()-1) {
		cout << (sum - res[i] + MOD) % MOD << endl;
	}
}

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

const int MOD = 998244353;

int put(int a, int b)
{
	int ans = 1;
	while (b) {
		if (b & 1)
			ans = 1LL * ans * a % MOD;
		a = 1LL * a * a % MOD;
		b /= 2;
	}
	return ans;
}

const int NMAX = 2e6;
int fact[NMAX], invfact[NMAX];

void precalc()
{
	fact[0] = 1;
	for (int i = 1; i < NMAX; i++)
		fact[i] = 1LL * i * fact[i - 1] % MOD;
	invfact[NMAX - 1] = put(fact[NMAX - 1], MOD - 2);
	for (int i = NMAX - 1; i >= 1; i--)
		invfact[i - 1] = 1LL * invfact[i] * i % MOD;
}

int comb(int a, int b)
{
	if (a < b || b < 0)
		return 0;
	return 1LL * fact[a] * invfact[b] % MOD * invfact[a - b] % MOD;
}

int main()
{
	ios_base::sync_with_stdio(0);
	precalc();

	int N, M;

	cin >> N >> M;

	int ans = 0;

	for (int nri = 0; nri <= min(N, M); nri++) {
		/// sunt nri impare
		int left = 3 * M - nri;
		if (left % 2 == 1)
			continue;
		left /= 2;
		int modurii = comb(N, nri);
		int modurip = comb(N + left - 1, N - 1);
		ans = (ans + 1LL * modurii * modurip) % MOD;
	}

	for (int toomuch = 2 * M + 1; toomuch <= 3 * M; toomuch++) {
		int moduri = comb(N + 3 * M - toomuch - 2, N - 2);
		ans = (ans - 1LL * moduri * N) % MOD;
	}

	if (ans < 0)
		ans += MOD;

	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define nl '\n'
#define INF 1e9 + 5
#define f(i, n) for (int i = 0; i < n; ++i)

using namespace std;
pair<int, int> get(vector<int>& a, bool even, int skip) {
    int n = a.size();
    unordered_map<int, int> book;
    pair<int, int> res = {0, 0};
    if (even) {
        for (int i = 0; i < n; ++i) {
            if (i & 1 || a[i] == skip) continue;
            if (book.count(a[i]))
                book[a[i]]++;
            else
                book[a[i]] = 1;
            if (res.second < book[a[i]]) {
                res.first = a[i];
                res.second = book[a[i]];
            }
        }
    } else {
        for (int i = 0; i < n; ++i) {
            if (!(i & 1) || a[i] == skip) continue;
            if (book.count(a[i]))
                book[a[i]]++;
            else
                book[a[i]] = 1;
            if (res.second < book[a[i]]) {
                res.first = a[i];
                res.second = book[a[i]];
            }
        }
    }
    return res;
}

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    f(i, n) cin >> a[i];
    int m = n / 2;
    pair<int, int> one = get(a, true, INF);
    pair<int, int> two = get(a, false, one.first);
    pair<int, int> three = get(a, false, INF);
    pair<int, int> four = get(a, true, one.first);
    int ans = min({(m - one.second) + (m - two.second),
                   (m - three.second) + (m - four.second)});
    cout << ans << nl;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
}

#include<iostream>
using namespace std;

int m[101];

int main(){
  int n,q;
  int i,j;
  int d,x;
  int max,a,f;

  while(1){
    f=0;
    max=-1;
    for(i=0;i<101;i++){
      m[i]=0;
    }
    cin >> n >> q;
    if(n==0 && q==0)break;
    q--;
    for(i=0;i<n;i++){
      cin >> d;
      if(d==0)f=1;
      for(j=0;j<d;j++){
	cin >> x;
	m[x]++;
      }
    }
    a=0;
    for(i=0;i<100;i++){
      if(q<m[i]){
	q=m[i];
	a=i;
      }
      m[i]=0;
    }
    cout << a << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
int main(void)
{
	int a, b;
	cin >> a >> b;
	int maxv = max(a, b);
	int minv = min(a, b);
	int c = minv - 1;
	int d = maxv - c;

	int count = 0;
	bool da[100][100];
	fill(da[0], da[0]+100*100, false);
	for (int i = 0; i < 25; i++) {
		for (int j = 0; j < 25; j++) {
			if (!(count < c))
				goto end;
			int ox = j*4;
			int oy = i*4;
			da[oy+0][ox+0] = true;
			da[oy+0][ox+1] = true;
			da[oy+0][ox+2] = true;
			da[oy+1][ox+0] = true;
			da[oy+1][ox+2] = true;
			da[oy+2][ox+0] = true;
			da[oy+2][ox+1] = true;
			da[oy+2][ox+2] = true;
			count++;
		}
	}
end:

	count = 0;
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 50; j++) {
			if (!(count < d))
				goto end2;
			int oy = 80+i*2;
			int ox = j*2;
			da[oy][ox] = true;
			count++;
		}
	}
end2:

  	printf("100 100\n");
	for (int y = 0; y < 100; y++) {
		for (int x = 0; x < 100; x++) {
			if (minv == maxv) {
				if (da[y][x])
					putchar('#');
				else
					putchar('.');
			} else {
				if (maxv == b) {
					if (da[y][x])
						putchar('#');
					else
						putchar('.');
				} else {
					if (da[y][x])
						putchar('.');
					else
						putchar('#');
				}
			}
		}
		cout << endl;
	}

	return 0;
}

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// 最大公約数
ll gcd(ll a,ll b){
   if (a%b == 0) return(b);
   else return(gcd(b, a%b));
}

// 最小公倍数
ll lcm(ll a, ll b){
    return a*b/gcd(a, b);
}

const int MAX = 1000000;
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;
}

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

int main()
{
    cout << fixed << setprecision(15);
    ll N, M;
    cin >> N >> M;

    // for(auto p : prime_factor(N)) {
    //     while(p.second--) cout << " " << p.first;
    // }
    // cout << endl;

    map<ll, ll> PR;
    PR = prime_factor(M);

    // 前処理
    COMinit();

    ll ans = 1;
    for(auto p:PR){
        // cout << p.second << endl;
        ans = (ans * COM(p.second + N - 1, p.second)) % MOD;
        ans = ans % MOD;
    }
    cout << ans << endl;
    return 0;
}


%:pragma GCC optimize "-O3"
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("fast-math")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC target("sse3","sse2","sse")
#include<bits/stdc++.h>
#define ll long long
#define heap priority_queue
using namespace std;
const ll maxn=15+1;
const ll inf=9e17;
ll ans,sum,dp[maxn][1<<maxn],dis[1<<maxn],g[maxn][maxn],tmp,lim,n,m;
inline ll lowbit(ll x){
	return x&-x;
}
int main()
{
	cin>>n>>m;
	ll u,v,w;
	for(int i=1;i<=m;i++)
	{
		cin>>u>>v>>w;
		--u;
		--v;
		g[u][v]=g[v][u]=w;
		sum+=w;
	}
	lim=(1<<n)-1;
	for(int i=1;i<=lim;i++)
	{
		tmp=log2(lowbit(i));
		dis[i]=dis[i^(1<<tmp)];
		for(int j=0;j<n;j++)
			if(g[tmp][j]&&(i&(1<<j)))
				dis[i]+=g[tmp][j];
	}
	memset(dp,0xc0,sizeof(dp));
	dp[0][1]=0;
	ans=-inf;
	for(int i=1;i<=lim;i++)
	{
		if(!(i&1))
			continue;
		for(int j=0;j<n;j++)
			if(i&(1<<j))
			{
				for(int k=i;k;k=(k-1)&i)
					if(k&(1<<j))
						dp[j][i]=max(dp[j][i],dp[j][i^k^(1<<j)]+dis[k]);
				for(int k=lim^i;k;k-=lowbit(k))
				{
					tmp=log2(lowbit(k));
					if(g[tmp][j])
						dp[tmp][i^(1<<tmp)]=max(dp[tmp][i^(1<<tmp)],dp[j][i]+g[tmp][j]);
				}
			}
		if(i&(1<<(n-1)))
			ans=max(ans,dp[n-1][i]);
	}
	cout<<sum-ans<<"\n";
	return 0;
}


#include "bits/stdc++.h"
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define mFOR(i, a, b) for (int i = a; i > b; i--)
#define MP make_pair
#define PB push_back
#define ALL(v) v.begin(), v.end()
#define N 100007
#define INF 1000000007
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll fceil(ll a, ll b) { return (a % b == 0 ? a / b : a / b + 1); }
int main()
{
   int n, k;
   cin >> n >> k;
   cout << (n % k ? 1 : 0) << endl;
   return 0;
}

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

void solve(long long N, long long K) {
    if (N % K == 0) {
        cout << 0 << endl;
    } else {
        cout << 1 << endl;
    }
}

int main() {
    long long N;
    scanf("%lld", &N);
    long long K;
    scanf("%lld", &K);
    solve(N, K);
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;

vector<long long> dijkstra(const Graph &g, int s) {
    vector<long long> dist(g.size(), INF);
    using Pi = pair<long long, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
    while (!que.empty()) {
        long long cost; int u; tie(cost, u) = que.top(); que.pop();
        if (dist[u] < cost) continue;
        for (auto &e: g[u]) {
            int v; long long nc; tie(v, nc) = e;
            if (chmin(dist[v], dist[u] + nc)) que.emplace(dist[v], v);
        }
    }
    return dist;
}

int main() {
    int n, k;
    while (cin >> n >> k, n) {
        Graph g(n);
        while (k--) {
            int com; cin >> com;
            if (com == 0) {
                int a, b; cin >> a >> b; a--, b--;
                auto res = dijkstra(g, a)[b];
                cout << (res == INF ? -1 : res) << endl;
            } else if (com == 1) {
                int c, d, e; cin >> c >> d >> e; c--, d--;
                g[c].emplace_back(d, e);
                g[d].emplace_back(c, e);
            }
        }
    }
    return 0;
}

#include<stdio.h>
#define INF 1000000
void warshall_floyd();
int min(int,int);
int graph[101][101],n,c,d;

int main(){
  int k,i,j,x,e,a,b;

  while(scanf("%d %d",&n,&k),n,k){

    for(i=0;i<=n;i++)
      for(j=0;j<=n;j++)
	graph[i][j]=i==j?0:INF;

    for(i=0;i<k;i++){

      if(scanf("%d",&x),x){
	scanf("%d %d %d",&c,&d,&e);

	if(graph[c][d]>e){
	graph[c][d]=graph[d][c]=e;
	warshall_floyd();
	}
      }else{
	scanf("%d %d",&a,&b);
	printf(graph[a][b]>=INF?"-1\n":"%d\n",graph[a][b]);
      }
    }
  }
  return 0;
}

void warshall_floyd(){
  int i,j;
  for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
      graph[i][j]=graph[j][i]=min(graph[i][j],graph[i][c]+graph[c][d]+graph[d][j]);
}

int min(int x,int y){
  return x<y?x:y;
}


#include<bits/stdc++.h>
using namespace std;
int main(void){
  int n; cin>>n;
  vector<int> a(n);
  for(auto& i:a) cin>>i;
  unordered_map<int,int> m;
  for(auto& i:a){ ++m[i]; if(m[i]>1){ cout<<"NO"; return 0;}}
  cout<<"YES";
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()

int main() {
  ll N;
  cin>>N;
 set<ll> S;
  for(ll i=0;i<N;i++) {
ll A;
    cin>>A;
    if(S.count(A)) {
cout<<"NO"<<endl;
      return 0;
    }
    S.insert(A);
  }

  cout<<"YES"<<endl;
}


#include <iostream>
using namespace std;

int main()
{
	int date[13][32];
	for(int i = 1;i<=31;i++)date[1][i] = i;
	for(int i = 1;i<=28;i++)date[2][i] = i+3;
	for(int i = 1;i<=31;i++)date[3][i] = i+4;
	for(int i = 1;i<=31;i++)date[4][i] = i;
	for(int i = 1;i<=31;i++)date[5][i] = i+2;
	for(int i = 1;i<=31;i++)date[6][i] = i+5;
	for(int i = 1;i<=31;i++)date[7][i] = i;
	for(int i = 1;i<=31;i++)date[8][i] = i+3;
	for(int i = 1;i<=31;i++)date[9][i] = i+6;
	for(int i = 1;i<=31;i++)date[10][i] = i+1;
	for(int i = 1;i<=31;i++)date[11][i] = i+4;
	for(int i = 1;i<=31;i++)date[12][i] = i+6;

	int month,day;
	while(true)
	{
		cin >> month;
		cin >> day;
		if(month==0&&day==0)break;
		switch (date[month][day] % 7)
		{
			case 0:
			cout << "Wednesday" << endl;
			break;
			case 1:
			cout << "Thursday" << endl;
			break;
			case 2:
			cout << "Friday" << endl;
			break;
			case 3:
			cout << "Saturday" << endl;
			break;
			case 4:
			cout << "Sunday" << endl;
			break;
			case 5:
			cout << "Monday" << endl;
			break;
			case 6:
			cout << "Tuesday" << endl;
			break;
		}

	}

	return 0;
}

#include <iostream>
#include <string>
using namespace std;
int main( void )
{
	int days[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	string d[7] = { "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday" };

	while (1)
	{
		int month;
		int day;
		cin >> month >> day;
		if ( month == 0 && day == 0 )
			break;

		for ( int i = 1; i < month; i++ )
		{
			day += days[i];
		}

		day--;
		int index = day % 7;
		cout << d[index] << endl;

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int N;
	cin >> N;
	long long int P = 998244353;
	int A[310] = {};
	int S = 0;
	for(int i = 1; i <= N; i++){
		cin >> A[i];
		S += A[i];
	}
	long long int dp[310][90010] = {}; // はじめi枚まででRがjになるようなRGBの組の個数
	dp[0][0] = 1;
	for(int i = 1; i <= N; i++){
		for(int j = 0; j <= 300 * i; j++){
			if(j >= A[i]) dp[i][j] = (dp[i-1][j-A[i]] + dp[i-1][j] * 2) % P;
			else dp[i][j] = (dp[i-1][j] * 2) % P;
		}
    }
	long long int exceed = 0;
	for(int i = (S + 1)/2; i <= S; i++){
		exceed += dp[N][i];
	}
	exceed = exceed % P;
	dp[0][0] = 1; // はじめi枚まででRがjになるようなRの組の個を同じ配列で再計算
	for(int i = 1; i <= N; i++){
		for(int j = 0; j <= 300 * i; j++){
			if(j >= A[i]) dp[i][j] = (dp[i-1][j-A[i]] + dp[i-1][j]) % P;
			else dp[i][j] = dp[i-1][j];
		}
    }
	long long int pow3[314] = {};
	pow3[0] = 1;
	for(int i = 1; i <= 310; i++){
		pow3[i] = (pow3[i-1] * 3) % P;
	}
	if(S % 2 == 1) cout << (3 * P + pow3[N] - exceed * 3) % P << endl;
	else cout << (3 * P + pow3[N] + dp[N][S / 2] * 3- exceed * 3) % P << endl;
}

#include <stdio.h>
#include <stdlib.h>

#define MAX_N (300L)
#define MAX_A (300L)
#define NUM_MOD (998244353L)

long dp0ss[MAX_N + 1][2 * MAX_N * MAX_A + 1],
    dp1ss[MAX_N + 1][2 * MAX_N * MAX_A + 1];

long mod(long n, long k) { return n % k >= 0 ? n % k : n % k + k; }

int main(int argc, char *argv[]) {
  // read inputs
  long N, as[MAX_N];
  scanf("%ld", &N);
  for (long i = 0; i < N; i++) {
    scanf("%ld", &as[i]);
  }

  // solve dp
  // init
  dp0ss[0][0 + MAX_N * MAX_A] = 1;
  dp1ss[0][0 + MAX_N * MAX_A] = 1;
  // update
  for (long i = 0; i < N; i++) {
    const long a = as[i];
    for (long d = -MAX_N * MAX_A; d <= MAX_N * MAX_A; d++) {
      if (d - a >= -MAX_N * MAX_A) {
        dp0ss[i + 1][d - a + MAX_N * MAX_A] =
            mod(dp0ss[i + 1][d - a + MAX_N * MAX_A] +
                    2 * dp0ss[i][d + MAX_N * MAX_A],
                NUM_MOD);
        dp1ss[i + 1][d - a + MAX_N * MAX_A] = mod(
            dp1ss[i + 1][d - a + MAX_N * MAX_A] + dp1ss[i][d + MAX_N * MAX_A],
            NUM_MOD);
      }
      if (d + a <= MAX_N * MAX_A) {
        dp0ss[i + 1][d + a + MAX_N * MAX_A] = mod(
            dp0ss[i + 1][d + a + MAX_N * MAX_A] + dp0ss[i][d + MAX_N * MAX_A],
            NUM_MOD);
        dp1ss[i + 1][d + a + MAX_N * MAX_A] = mod(
            dp1ss[i + 1][d + a + MAX_N * MAX_A] + dp1ss[i][d + MAX_N * MAX_A],
            NUM_MOD);
      }
    }
  }
  // for (long i = 0; i < N; i++) {
  //   long s = 0;
  //   for (long d = -MAX_N * MAX_A; d <= MAX_N * MAX_A; d++) {
  //     s += dp0ss[i + 1][d + MAX_N * MAX_A];
  //   }
  //   printf("i = %ld, s = %ld\n", i, s);
  // }

  // subtract from 3^N to get answer
  long ans = 1;
  for (long i = 0; i < N; i++) {
    ans = mod(ans * 3, NUM_MOD);
  }
  for (long d = 0; d <= MAX_N * MAX_A; d++) {
    ans = mod(ans - 3 * dp0ss[N][d + MAX_N * MAX_A], NUM_MOD);
  }
  ans = mod(ans + 3 * dp1ss[N][0 + MAX_N * MAX_A], NUM_MOD);
  printf("%ld\n", ans);

  return 0;
}


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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int main(){
    int n;
    cin >> n;
    V<int> a(n);
    rep(i,n) cin >> a[i];
    V<ll> sums(n+1);
    sums[1] = a[0];
    for(int i = 2; i <= n; i++) {
        sums[i] = a[i-1] + sums[i-1];
    }
    ll ans = infll;
    for(int i = 2; i <= n-2; i++) {

        auto func = [&](int L, int R) {
            int l = L; int r = R;
            while(abs(r-l) > 1) {
                int mid = (r+l)/2;
                ll p = sums[mid]-sums[L];
                ll q = sums[R]-sums[mid];
                if(p < q) l = mid;
                else r = mid;
            }
            Pl res;
            if(abs((sums[l]-sums[L])-(sums[R]-sums[l])) > abs((sums[r]-sums[L])-(sums[R]-sums[r])))
                res = make_pair(sums[r]-sums[L], sums[R]-sums[r]);
            else
                res = make_pair(sums[l]-sums[L], sums[R]-sums[l]);
            return res;
        };

        Pl a = func(0,i);
        Pl b = func(i,n);
        ll tmp = max({a.fi, a.se, b.fi, b.se}) - min({a.fi, a.se, b.fi, b.se});
        chmin(ans, tmp);
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
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;}
template <class T, class U>ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << "(" << p.first << "," << p.second << ")";
	return os;
}
template <class T>ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "{";
	rep(i, v.size()) {
		if (i) os << ",";
		os << v[i];
	}
	os << "}";
	return os;
}
template <class T, class U>istream& operator>>(istream& is, pair<T, U>& p) {
	is >> p.first >>p.second ;
	return is;
}
template <class T>istream& operator>>(istream& is,  vector<T>& v) {
	rep(i, v.size()) {
		is >> v[i];
	}
	return is;
}
ll mod                = 1e9 + 7;
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
int main(){
	int n;
	ll ans=1LL;
	cin >>n;
	Vi a(n);
	cin >> a;
	map<int, ll> dis;
	dis[-1] = 3LL;
	rep(i, n) {
		ans *= dis[a[i] - 1LL];
		ans %= mod;
		dis[a[i]]++;
		dis[a[i] - 1]--;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>

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

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

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 88
#define MAXS 13000

int dp[MAXN][MAXN][MAXS];

int main(){
  int h,w;cin>>h>>w;
  vector<vector<int>> scores(h);
  rep(i, h)rep(j, w){
    int a;cin>>a;
    scores[i].push_back(a);
  }
  rep(i, h)rep(j, w){
    int a;cin>>a;
    scores[i][j] -= a;
    if(scores[i][j] < 0)scores[i][j] *= -1;
  }

  dp[0][0][scores[0][0]] = 1;
  rep(i, h)rep(j, w)rep(k, MAXS){
    int s = scores[i][j];
    int mpre = k-s;
    if(mpre<0)mpre *= -1;
    if(i && dp[i-1][j][mpre]==1)dp[i][j][k]=1;
    if(j && dp[i][j-1][mpre]==1)dp[i][j][k]=1;

    int ppre = k+s;
    if(ppre<=7000){
      if(i && dp[i-1][j][ppre]==1)dp[i][j][k]=1;
      if(j && dp[i][j-1][ppre]==1)dp[i][j][k]=1;
    }
  }

  int result = 0;
  rrep(i, MAXS)if(dp[h-1][w-1][i])result = i;

  cout<<result<<endl;

  return 0;

  rep(i, h){
    rep(j, w)cout<<scores[i][j]<<' ';cout<<endl;
  }
cout<<"###"<<endl;

rep(k, 15){
cout<<k<<endl;
  rep(i, h){
    rep(j, w)cout<<dp[i][j][k]<<' ';cout<<endl;
  }
  cout<<"---"<<endl;
}

  return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

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

  using D = tuple<int, int, int>;
  vector<D> data;
  rep(i, N) {
    int x, y;
    char d;
    cin >> x >> y >> d;

    int dir = 0;
    if (d == 'D' || d == 'U') {
      dir |= 2;
    }
    if (d == 'D' || d == 'L') {
      dir |= 1;
    }
    data.push_back(D(x, y, dir));
  }

  constexpr int INF = numeric_limits<int>::max();
  int result = INF;

  constexpr int MAX = 200'000;
  vector<vector<vector<int>>> tbl(4, vector<vector<int>>(MAX + 1));
  vector<vector<vector<int>>> cross(4, vector<vector<int>>(2 * MAX + 1)),
      cross2(4, vector<vector<int>>(2 * MAX + 1));
  rep(i, N) {
    int x, y, d;
    tie(x, y, d) = data[i];

    tbl[d][((d & 2) ? x : y)].push_back((d & 2) ? y : x);
    cross[d][x + y].push_back(x - y + MAX);
    cross2[d][x - y + MAX].push_back(x + y);
  }
  rep(i, 4) irep(j, MAX) { sort(tbl[i][j].begin(), tbl[i][j].end()); }

  rep(i, 4) irep(j, 2 * MAX) {
    sort(cross[i][j].begin(), cross[i][j].end());
    sort(cross2[i][j].begin(), cross2[i][j].end());
  }

  rep(i, 2) {
    irep(j, MAX) {
      for (int v : tbl[i << 1][j]) {
        const auto &tmp = tbl[(i << 1) ^ 1][j];
        auto iter = lower_bound(tmp.begin(), tmp.end(), v);
        if (iter != tmp.end()) {
          // cerr << i << " " << j << " " << v << " " << *iter << "\n";
          chmin(result, 5 * (*iter - v));
        }
      }
    }
  }

  rep(i, 2) {
    irep(j, 2 * MAX) {
      for (int v : cross[i][j]) {
        const auto &tmp = cross[i ^ 2][j];
        auto iter = lower_bound(tmp.begin(), tmp.end(), v);
        if (i == 0) {
          if (iter != tmp.end()) {
            cerr << i << " " << j << " " << v << " " << *iter << "\n";
            chmin(result, 5 * (*iter - v));
          }
        } else {
          if (iter != tmp.begin()) {
            chmin(result, 5 * (v - *prev(iter)));
          }
        }
      }
    }
  }

  rep(i, 2) {
    irep(j, 2 * MAX) {
      for (int v : cross2[i][j]) {
        const auto &tmp = cross2[i ^ 3][j];
        auto iter = lower_bound(tmp.begin(), tmp.end(), v);
        if (i == 0) {
          if (iter != tmp.end()) {
            chmin(result, 5 * (*iter - v));
          }
        } else {
          if (iter != tmp.begin()) {
            chmin(result, 5 * (v - *prev(iter)));
          }
        }
      }
    }
  }

  if (result == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << result << endl;
  }
  return 0;
}

#include<bits/stdc++.h>
#define tmp(x) std::cout<<"& "<<(x)<<" &\n"
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
typedef long long ll;
#define  pii pair<int,int>
#define  pll pair<ll,ll>
#define  vi vector<int>

using namespace std;
const int maxn=3e5+100;
const int inf=(int)1e9+7;
int n;

vector<pair<int,char> > x[maxn],y[maxn],le[2*maxn],ri[2*maxn];
int ans=inf;
void work(vi v1,vi v2,int aa){

    if(!v2.empty()&&!v1.empty()){
        vector<int>::iterator it1,it2;
        sort(v2.begin(),v2.end());
        sort(v1.begin(),v1.end());
        it2=v2.begin(),it1=v1.begin();
        for(auto p1:v1){
            for(auto p2:v2){
                if(p2<p1)continue;
                else {
                    ans=min(ans,abs(p1-p2)*10/aa);
                    break;
                }
            }
        }

        /*while(*it1!=*v1.end()&&*it2!=*v2.end()){
            while(*it2!=*v2.end()&&*it2<*it1)++it2;
            if(*it2==*v2.end())break;
            ans=min(ans,abs(*it1-*it2)*10/aa);
            ++it1;
        }*/
    }
}
int main(){
//	freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int xx,yy;char c;
    rep(i,1,n){
        scanf("%d%d %c",&xx,&yy,&c);

        x[xx].push_back(make_pair(yy,c));
        y[yy].push_back(make_pair(xx,c));
        le[xx-yy+200000].push_back(make_pair(xx,c));
        ri[xx+yy].push_back(make_pair(xx,c));
    }
    rep(i,0,200000){
        if(!x[i].empty()){
            vector<int>up,down;
            for(auto p:x[i]){
                if(p.second=='U')up.push_back(p.first);
                if(p.second=='D')down.push_back(p.first);
            }
            work(up,down,2);
        }
        if(!y[i].empty()){
            vector<int>right,left;
            for(auto p:y[i]){
                if(p.second=='R')right.push_back(p.first);
                if(p.second=='L')left.push_back(p.first);
            }
            work(right,left,2);
        }
    }

    rep(i,0,400000){
        if(!le[i].empty()){
            vector<int> right,down,left,up;
            for(auto p:le[i]){
                if(p.second=='D')down.push_back(p.first);
                else if(p.second=='R')right.push_back(p.first);
                else if(p.second=='U')up.push_back(p.first);
                else if(p.second=='L')left.push_back(p.first);
            }
            work(right,down,1);
            work(up,left,1);
        }
        if(!ri[i].empty()) {
            vector<int> right, up,left, down;
            for (auto p:ri[i]) {
                if (p.second == 'U')up.push_back(p.first);
                else if (p.second == 'R')right.push_back(p.first);
                else if (p.second == 'D')down.push_back(p.first);
                else if (p.second == 'L')left.push_back(p.first);
            }
            work(right, up,1);
            work(down, left,1);
        }
    }
    if(ans==inf)printf("SAFE");
    else  printf("%d",ans);

    return 0;
}