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

int main(){
    double n,m,d,l;cin>>n>>m>>d;
    l=2*(n-d);
    if(d==0)l/=2;
    cout<<fixed<<setprecision(15)<<l*(m-1)/n/n<<endl;
}

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

ll memo[15];

const ll MOD = 1e9+7;

ll fib(ll n){
    if(n <= 0) return 1LL;

    if(memo[n] > 0) return memo[n];
    else return memo[n] = (fib(n-1) + fib(n-2))%MOD;
}

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

    int h,w,k; cin >> h >> w >> k;
    if(w == 1){
        cout << 1 << endl;
        return 0;
    }
    --k;
    vector<ll> bef(w+1, 0LL), now(w+1, 0LL);

    // fib前計算
    fib(w);

    bef[0] = 1LL;
    rep(i,h){
        rep(j, w){
            if(j == 0){
                now[0] = bef[1]*fib(w-3)%MOD + bef[0]*fib(w-2)%MOD;
                now[0] %= MOD;
                continue;
            }
            if(j == w-1){
                now[w-1] = bef[w-2]*fib(w-3)%MOD + bef[w-1]*fib(w-2)%MOD;
                now[w-1] %= MOD;
                continue;
            }
            // 1
            now[j] = (( (fib(w-j-2)*fib(j-2))%MOD )*bef[j-1]) % MOD;
            now[j] %= MOD;
            // 2
            now[j] += (( (fib(w-j-3)*fib(j-1))%MOD )*bef[j+1]) % MOD;
            now[j] %= MOD;
            // 3
            now[j] += (( (fib(w-j-2)*fib(j-1))%MOD )*bef[j]) % MOD;
            now[j] %= MOD;
        }

        rep(j,w){
            bef[j] = now[j];
            now[j] = 0LL;
        }
    }

    cout << bef[k] << endl;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
	int n;
	long minimum = 1000000, maximum = -1000000, sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++){
		long a;
		cin >> a;
		minimum = min(a, minimum);
		maximum = max(a, maximum);
		sum += a;
	}
	cout << minimum << " " << maximum << " " << sum << endl;
	return 0;
}

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), i##_max = (b); i < i##_max; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;

constexpr int INF = 1 << 30;
constexpr int MOD = 1000000007;

static vector<int> G;
static int cnt = 0;

void insert_sort(vector<int> &A, int g) {
  FOR(i, g, A.size()) {
    auto v = A[i];
    auto j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j + g] = v;
  }
}

void shell_sort(vector<int> &A) {
  for(int h = 1;;) {
    if (h > A.size()) break;
    G.push_back(h);
    h = 3*h + 1;
  }
  reverse(ALL(G));
  REP(i, G.size()) insert_sort(A, G[i]);
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  shell_sort(A);
  cout << G.size() << endl;
  REP(i, G.size()) {
    if (i) cout << " ";
    cout << G[i];
  }
  cout << endl << cnt << endl;
  for (auto a : A) cout << a << endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
char s[1000001],ss[1000000];
int suu[1000000],cnt[1000000];
int main(void)
{
	int i,j,kazu,max=0,len;
	scanf("%s",s);
	kazu=0;
	cnt[kazu]=1;
	ss[kazu]=s[kazu];
	len=strlen(s);
	for(i=1;i<len;i++) {
		if(ss[kazu]==s[i]) cnt[kazu]++;
		else {
			kazu++;
			cnt[kazu]=1;
			ss[kazu]=s[i];
		}
	}
	for(i=0;i<=kazu;i++) {
		if(ss[i]=='O') {
			if(i-1>=0 && i+1<=kazu) {
				if(ss[i-1]=='J' && cnt[i-1]>=cnt[i]) {
					if(ss[i+1]=='I' && cnt[i+1]>=cnt[i]) {
						if(max<cnt[i]) max=cnt[i];
					}
				}
			}
		}
	}
	printf("%d\n",max);
	return 0;
}

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,a;
    cin>>n;
    for(int i=n;i>=1;i--)
    {
        a=sqrt(i);
        if(i%a==0&&i/a==a)
        {
            cout<<i<<endl;
            return 0;
        }
    }

	return 0;
}


#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

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

    string s;
    cin >> s;

    int64_t m = s.size();

    vector a(1, 0L);
    int64_t t = 0;
    for (auto c : s) {
        if (c == '0') {
            a.push_back(t);
        } else {
            ++t;
        }
    }
    a.push_back(t);

    int64_t k;
    cin >> k;
    k = min(k, a.back());

    int64_t n = a.size();

    constexpr auto M = 998244353L;

    vector dp(vector(k + 1, vector(a.back() + 1, 0L)));
    int64_t mk = 0;
    dp[0][0] = 1;
    for (auto i : irange(0L, n - 1)) {
        for (auto j : irange(mk, -1L, -1L)) {
            auto r = min(k - j, a[i + 1] - a[i]);
            for (auto d : irange(1L, r + 1L)) {
                auto w = j + d;
                for (auto c : irange(0L, a.back() - a[i + 1] + 1)) {
                    auto b = c + d;
                    dp[w][c] += dp[j][b];
                    if (dp[w][c] >= M) {
                        dp[w][c] -= M;
                    }
                }
            }

            for (auto c : irange(1L, a.back() - a[i + 1] + 1)) {
                dp[j][c] += dp[j][c - 1];
                if (dp[j][c] >= M) {
                    dp[j][c] -= M;
                }
            }
        }
        mk = min(k, mk + a[i + 1] - a[i]);
    }

    int64_t ans = 0;
    for (auto i : irange(0L, mk + 1)) {
        ans += dp[i][0];
    }

    cout << ans % M << "\n";
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
typedef long double ld;

typedef pair<int,int> pii;
#define x first
#define y second

const int N=310,MOD=998244353;

int dp[N][N][N];
int xxm[N][N][N];
int xmp[N][N][N];

#define get(arr)                    \
int get_##arr(int pos,int mov,int dif) \
{                                    \
    if(pos>=0&&mov>=0&&dif>=0)       \
        return arr[pos][mov][dif];   \
    else                             \
        return 0;                    \
}
get(dp)
get(xxm)
get(xmp)

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

    string s;
    int k;
    cin>>s>>k;
    k=min<int>(k,s.size());
    vector<int> v{0};
    int cur=0;
    for(char c:s)
        if(c=='0')
            v.push_back(cur),
            cur=0;
        else
            cur++;
    v.push_back(cur);
    int n=v.size()-1;
//    cout<<n<<"\n";
//    for(int i=1;i<=n;i++)
//        cout<<v[i]<<" ";
//    cout<<"\n";
    dp[0][0][0]=1;
    xxm[0][0][0]=1;
    xmp[0][0][0]=1;
    for(int pos=0;pos<=n;pos++)
        for(int mov=0;mov<=k;mov++)
            for(int dif=0;dif<=k;dif++)
            {
                dp[pos][mov][dif]=(get_dp(pos,mov,dif)+
                                   get_xxm(pos-1,mov,dif-1)+
                                   get_xmp(pos-1,mov-1,dif+1)-
                                   get_xmp(pos-1,mov-1-v[pos],dif+1+v[pos])+
                                   get_dp(pos-1,mov,dif))%MOD;
                xxm[pos][mov][dif]=(dp[pos][mov][dif]+get_xxm(pos,mov,dif-1))%MOD;
                xmp[pos][mov][dif]=(dp[pos][mov][dif]+get_xmp(pos,mov-1,dif+1))%MOD;
//                cout<<pos<<" "<<mov<<" "<<dif<<" -> "<<dp[pos][mov][dif]<<"\n";
            }
    int ans=0;
    for(int mov=0;mov<=k;mov++)
        ans=(ans+dp[n][mov][0])%MOD;
    cout<<(ans+MOD)%MOD;
}


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

int main(){
  long n,m,p[1001];
  while(cin>>n>>m,n||m){
    for(int i=1;i<=n;++i) cin>>p[i];
    p[0]=0;
    vector<long> s(0),t(0);
    for(int i=0;i<=n;++i){
      for(int j=0;j<=n;++j) s.push_back(p[i]+p[j]);
    }
    sort(s.begin(),s.end());
    long a=0;
    for(vector<long>::iterator it=s.begin();it!=s.end();++it){
      if(*it*2>m) break;
      a=max(a,*it+*(upper_bound(s.begin(),s.end(),m-*it)-1));
    }
    cout<<a<<endl;
  }
  return 0;
}

#include<iostream>
using namespace std;

int getDate(int q,int array[]){
	int max=0,key=0;
	for(int i=0;i<100000;i++){
		if(max<array[i]){
			max = array[i];
			key = i;
		}
	}
	return (max >= q) ? key : 0;
}

int main(){
	int n,q;
	int array[100000];
	while(1){
		cin >> n >> q;
		if(n+q==0) break;
		for(int i=0;i<100000;i++)
			array[i]=0;
		for(int i=0;i<n;i++){
			int m, date;
			cin >> m;
			for(int j=0;j<m;j++){
				cin >> date;
				array[date]++;
			}
		}
		cout << getDate(q,array) << endl;
	}

}


#include <iostream>
using namespace std;
int main(void){
	string s;
	cin>>s;
	int k=0;
	for(int i=0;i<s.size();++i){
		if(s[i]=='J'){
			int JOI[3]={1,0,0};
			while(s[i]=='J'){++JOI[0];++i;}
			while(s[i]=='O'){++JOI[1];++i;}
			while(s[i]=='I'){++JOI[2];++i;}
			if(JOI[0]>=JOI[1] && JOI[2]>=JOI[1]){
				if(k<JOI[1]) k=JOI[1];
			}
		}
	}
	cout<<k<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int res=0;
    char c;
    bool zf=0;
    while(((c=getchar())<'0'||c>'9')&&c!= '-');
    if(c=='-')zf=1;
    else res=c-'0';
    while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
    if(zf)return -res;
    return res;
}
int tot[26];
signed main(){
	string ss;cin>>ss;int n=ss.size(),s=0;
	for(register int i=0;i<n;i++)tot[ss[i]-'a']++,s++;
	long long ans=1ll*s*(s-1)/2;for(register int i=0;i<26;i++)ans-=1ll*tot[i]*(tot[i]-1)/2;printf("%lld\n",ans+1);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<vector<int> > Array2;
typedef vector<int> LArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

int itiv;
inline int getInt() { return (cin >> itiv, itiv); }
void readAll(Array& vec, int n) { for (int i = 0; i < n; i++) cin >> vec[i]; }
inline bool between(int first, int last, int n) { return first <= n && n <= last; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int m, n;
int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int main(void)
{
	while(cin >> n >> m && n)
	{
		Array kyougi(n), sinsa(m);
		Array hyou(n);
		readAll(kyougi, n); readAll(sinsa, m);
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if(kyougi[j] <= sinsa[i])
				{
					hyou[j]++;
					break;
				}
			}
		}
		cout << (max_element(ALL(hyou)) - hyou.begin()) + 1 << endl;
	}
	return 0;
}


#include <stdio.h>
int main ()
{
	int H, W, N;
	scanf ("%d", &H);
	scanf ("%d", &W);
	scanf ("%d", &N);
	if (H < W)
	{
		H=W;
	}
	if (N % H == 0){
		printf ("%d", N/H);
	}
	else if (N % H != 0){
		printf ("%d", (N/H) + 1);
	}
return 0;
}

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

int main(){
    int N, K;
    double ans = 0;
    cin >> N >> K;
    vector<double> p(N), E(N), sumE(N, 0);
    for(int i = 0; i < N; ++i)cin >> p[i];
    double bufe = 0;
    for(int i = 0; i < N; ++i){
        E[i] = (1/p[i])*(p[i]*(p[i]+1)/2);
    }
    for(int i = 0; i < N; ++i){
        if(i == 0){
            for(int i = 0; i < K; ++i) sumE[0] += E[i];
        }
        else if(i+K-1 < N)sumE[i] = sumE[i-1] - E[i-1] + E[i+K-1];
        else break;
    }
    sort(sumE.begin(), sumE.end(), greater<double>());
    cout << setprecision(16) << sumE[0] << endl;
    return 0;
}


#if 1
  #include <bits/stdc++.h>
#else
  #include "../../bits/stdc++.h"
#endif

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

using namespace std;

typedef pair<int, int> P;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second

int m, nin, nax;
int p[202];

bool input(){
  cin>>m>>nin>>nax;
  REP(i, m)cin>>p[i];
  return m;
}

void solve(){
  int s=0, b=0;
  FOR(i, nin-1, nax) {
    if(p[i]-p[i+1]>=s){
      b=i+1;
      s=p[i]-p[i+1];
    }
  }
  cout<<b<<endl;
}

int main(){
  cin.tie(0);

  while (input()){
      solve();
  }

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int m, n1, n2;
    while(cin >> m >> n1 >> n2, m || n1 || n2){
	int P[201];
	for(int i = 0; i < m; i++) cin >> P[i];
	int ans = n1;
	for(int i = n1; i <= n2; i++){
	    if(P[ans-1]-P[ans] <= P[i-1]-P[i]) ans = i;
	}
	cout << ans << endl;
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <climits>
#include <algorithm>
#include <utility>

using namespace std;
using Int = long long int;
using UInt = unsigned long long int;
using VI = vector<long long int>;

// 基本的な演算
namespace BasicMath {
    // 二つの値の最大値を求める関数
    static Int Max(const Int a, const Int b);
    // 三つの値の最大値を求める関数
    static Int Max(const Int a, const Int b, const Int c);
    // 二つの値の最大値を求める関数
    Int Max(const Int a, const Int b) {
        if (a >= b) {
            return a;
        } else {
            return b;
        }
    }
    // 三つの値の最大値を求める関数
    Int Max(const Int a, const Int b, const Int c) {
        return Max(a, Max(b, c));
    }
}
using namespace BasicMath;

int main(void) {
    Int a, b, c;
    cin >> a >> b >> c;
    cout  << a + b + c - Max(a, b, c) << endl;
    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <iomanip>
#define INF 2147483647

using namespace std;

int main()
{
	double x1, y1, x2, y2;

	cin >> x1 >> y1 >> x2 >> y2;

	cout << fixed << setprecision(8) << sqrt(pow(x2 - x1, 2.0) + pow(y2 - y1, 2.0))  <<  endl;

	return (0);
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
using Graph = vector<vector<int>>;
const ll mod=1000000007;
const int MAX_N = 1000;         // n の最大値

double nCk(int n, int k) {
double res=1.0;
for(int i=0; i<n; i++){
res*=0.5;}
for(int i=0; i<k; i++){
res*=(double)(n-i);
res/=(double)(k-i);
}
return res;}

int main() {
ll n; cin >> n;
string a, b, c; cin >> a >> b >> c;
ll ca=0;
rep(i,n){
if(a.at(i)==b.at(i)&&b.at(i)==c.at(i)){
continue;}
else if(a.at(i)==b.at(i)||b.at(i)==c.at(i)||c.at(i)==a.at(i)){
ca+=1;}
else ca+=2;}
cout << ca << endl;}

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

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
	int N;
	cin >> N;
	string a, b, c;
	cin >> a >> b >> c;

	int ans = 0;
	for (int ii = 0; ii < N; ++ii){
		map<char, int> m;
		m[a[ii]]++;
		m[b[ii]]++;
		m[c[ii]]++;

		int maxVal = max(m[a[ii]], max(m[b[ii]], m[c[ii]]));
		ans += 3 - maxVal;
	}

		cout << ans << "\n";

	return 0;
}


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

int main()
{
    int n;

    cin >> n;
    vector<pair<int, int>> red;
    vector<pair<int, int>> blue;
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        red.emplace_back(b, a);
    }
    for (int i = 0; i < n; i++) {
        int c, d;
        cin >> c >> d;
        blue.emplace_back(c, d);
    }

    sort(red.begin(), red.end(), greater<pair<int, int>>());
    sort(blue.begin(), blue.end());
    int ans = 0;

    for (auto bit = blue.begin(); bit != blue.end(); bit++) {
        auto it = red.begin();
        while (it != red.end()) {
            if (it->first < bit->second && it->second < bit->first) {
                ans++;
                red.erase(it);
                break;
            }
            it++;
        }
    }

    cout << ans << 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);
}

struct BipartiteMatching {
  vector< vector< int > > graph;
  vector< int > match, alive, used;
  int timestamp;

  BipartiteMatching(int n) : graph(n), alive(n, 1), used(n, 0), match(n, -1), timestamp(0) {}

  void add_edge(int u, int v) {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  bool dfs(int idx) {
    used[idx] = timestamp;
    for(auto &to : graph[idx]) {
      int to_match = match[to];
      if(alive[to] == 0) continue;
      if(to_match == -1 || (used[to_match] != timestamp && dfs(to_match))) {
        match[idx] = to;
        match[to] = idx;
        return true;
      }
    }
    return false;
  }

  int bipartite_matching() {
    int ret = 0;
    for(int i = 0; i < graph.size(); i++) {
      if(alive[i] == 0) continue;
      if(match[i] == -1) {
        ++timestamp;
        ret += dfs(i);
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      if(i < match[i]) {
        cout << i << "-" << match[i] << endl;
      }
    }
  }
};

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

    vector<ll> A(N);
    vector<ll> B(N);
    vector<ll> C(N);
    vector<ll> D(N);

    REP(i, N){
        cin >> A[i] >> B[i];
    }
    REP(i, N){
        cin >> C[i] >> D[i];
    }

    BipartiteMatching bm(N + N);

    REP(i, N){
        REP(j, N){
            // cout << i << " " << j << endl;
            if((A[i] < C[j]) && (B[i] < D[j])){
                bm.add_edge(i, N + j);
                // cout << i << " " << j << endl;
            }
        }
    }

    // BipartiteMatching bm(X + Y);
    // for(int i = 0; i < E; i++) {
    //     int a, b;
    //     cin >> a >> b;
    //     bm.add_edge(a, X + b);
    // }

    cout << bm.bipartite_matching() << endl;
    return 0;
}


//https://atcoder.jp/contests/abc160/tasks/abc160_b
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll x;
	cin>>x;
	ll a;
	a=x/500;
	ll fin1;
	fin1=a*1000;
	ll b;
	b=x%500;
	ll c;
	c=b/5;
	ll fin2;
	fin2=c*5;
	ll sum=0;
	sum=fin1+fin2;
	cout<<sum<<"\n";
	return 0;
}

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

int main() {
 ll X;
  cin>>X;
  ll a=X/500;
  X-=500*a;
  cout<<1000*a+5*(X/5)<<endl;
}


#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll N,A,B;

int main(){

	scanf("%lld %lld %lld",&N,&A,&B);

	ll pre,current;
	scanf("%lld",&pre);

	ll ans = 0;
	for(int i = 1; i < N; i++){
		scanf("%lld",&current);
		if(A*(current-pre) < B){

			ans += A*(current-pre);
		}else{

			ans += B;
		}
		pre = current;
	}

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

	return 0;
}

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

typedef long long int ll;
#define all(x) (x).begin(), (x).end()

using namespace std;

int nxt() {
    int x;
    cin >> x;
    return x;
}

ll nxtl(){
    ll x;
    cin>>x;
    return x;
}

void SieveOfEratosthenes(int n,vector <int> &primes)
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    bool prime[n+1];
    memset(prime, true, sizeof(prime));

    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }

    // Print all prime numbers
    for (int p=2; p<=n; p++)
       if (prime[p])
          primes.push_back(p);
}

ll max(ll a,ll b)
{
    if(a>b)
        return a;
    return b;
}

ll power(ll x, ll y,ll mod)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2,mod);
    if (y%2 == 0)
        return (temp*temp)%mod;
    else
        return (((x*temp)%mod)*temp)%mod;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n=nxtl(),a=nxtl(),b=nxtl();
    vector <int> v(n);
    generate(all(v),nxt);
    ll test=0;
    for(int i=0;i<n-1;i++)
    {
        test+=min(b,a*(v[i+1]-v[i]));
    }
    cout<<test<<endl;

    return 0;
}


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

int H, W, N;

signed main(){
	cin >> H >> W >> N;
	cout << min((N+H-1)/H, (N+W-1)/W) << endl;
}

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

int H, W, N;

int main() {
	read(H), read(W), read(N);
	print((N - 1) / std::max(H, W) + 1);
}


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

int main() {
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;

  string ans;
  auto move = [&](int h, int w) {
    rep(i, abs(h)) ans += (h > 0 ? 'U' : 'D');
    rep(i, abs(w)) ans += (w > 0 ? 'R' : 'L');
  };

  int h1 = ty - sy, w1 = tx - sx;
  move(h1, w1);

  int h2 = sy - ty, w2 = sx - tx;
  move(h2, w2);

  int h3 = ty - sy, w3 = tx - sx;
  ans += (w3 > 0 ? 'L' : 'R');
  h3 += (h3 > 0 ? 1 : -1);
  w3 += (w3 > 0 ? 1 : -1);
  move(h3, w3);
  ans += (h3 > 0 ? 'D' : 'U');

  int h4 = sy - ty, w4 = sx - tx;
  ans += (w4 > 0 ? 'L' : 'R');
  h4 += (h4 > 0 ? 1 : -1);
  w4 += (w4 > 0 ? 1 : -1);
  move(h4, w4);
  ans += (h4 > 0 ? 'D' : 'U');

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

#include <bits/stdc++.h>
//#include <atcoder/all>
#define ll long long int
#define MOD 1000000007
#define P pair<ll,ll>
#define INF 1000000000000000000

//using namespace atcoder;
using namespace std;

string transform(string t, ll x, ll y){
  for (ll i = 0; i < t.size(); i++){
    if (t[i] == 'R' && x < 0){
      t[i] = 'L';
    }
    else if (t[i] == 'L' && x < 0){
      t[i] = 'R';
    }
    else if (t[i] == 'U' && y < 0){
      t[i] = 'D';
    }
    else if (t[i] == 'D' && y < 0){
      t[i] = 'U';
    }
  }
  return t;
}

int main(void){
  ll sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;

  tx -= sx;
  ty -= sy;

  sx = tx;
  sy = ty;

  tx = abs(tx);
  ty = abs(ty);

  string t = "";
  // 1周目
  for (ll i = 0; i < ty; i++){
    t += "U";
  }
  for (ll i = 0; i < tx; i++){
    t += "R";
  }
  for (ll i = 0; i < ty; i++){
    t += "D";
  }
  for (ll i = 0; i < tx; i++){
    t += "L";
  }

  // 1周目
  t += "L";
  for (ll i = 0; i < ty + 1; i++){
    t += "U";
  }
  for (ll i = 0; i < tx + 1; i++){
    t += "R";
  }
  t += "D";
  t += "R";
  for (ll i = 0; i < ty + 1; i++){
    t += "D";
  }
  for (ll i = 0; i < tx + 1; i++){
    t += "L";
  }
  t += "U";

  cout << transform(t, sx, sy) << endl;
  for (ll i = 0; i < ty; i++){
    t += "D";
  }
  for (ll i = 0; i < tx; i++){
    t += "L";
  }

  return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;
typedef pair<int, int> Pii;

struct st{
	string name;
	double a;

	st(){}
	st(string n, double a) : name(n), a(a) {}
	bool operator< (const st &s)const{
		return (this->a == s.a) ? this->name < s.name : this->a > s.a;
	}
};

bool solve(int n){
	vector<st> vst;
	for(int i=0;i<n;i++){
		string l;
		int p, a, b, c, d, e, f, s, m;
		cin>> l>> p>> a>> b>> c>> d>> e>> f>> s>> m;
		int t, money;
		t = a + b + c + (d + e) * m;
		money = m * f * s - p;
		st tmp(l, (double)money/t);
		vst.pb(tmp);
	}
	sort(vst.begin(),vst.end());
	for(int i=0;i<vst.size();i++){
		cout<< vst[i].name<<  endl;
	}
	cout<< "#"<< endl;

	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);

	int n;
	while(cin>>n, n){
		solve(n);
	}
	return 0;
}

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

int main()
{
    int n, a, b;
    bool p[10000] = {false};

    for( int i = 2; i < 10000; i++ )
    {
        bool judge = true;
        for( int j = 2; j <= sqrt(i); j++ )
            if( i % j == 0 )
                judge = false;
        if( judge == true )
            p[i] = true;
    }

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

        for( int i = 2; i <= n - 2; i++ )
        {
            if( p[i] == true && p[i+2] == true )
            {
                a = i;
                b = i + 2;
            }
        }
        cout << a << " " << b << endl;
    }
    return 0;
}


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

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main()
{
  cout << fixed << setprecision(10);
  int N,K;
  cin >> N >> K;
  vector<int> A(N);
  bool ok = false;
  int M = 0;
  int g = 0;
  rep(i,N)
  {
    cin >> A[i];
    if(A[i]==K)
    {
      ok = true;
    }
    M = max(M,A[i]);
    if(i==0) g = A[0];
    g = gcd(A[i],g);
  }
  if(g==1)
  {
    ok = true;
  }
  else
  {
    if(gcd(g,K)!=g)
    {
      ok = false;
    }
    else
    {
      ok = true;
    }
  }
  if(M<K)
  {
    ok = false;
  }
  if(ok)
  {
    cout << "POSSIBLE" << endl;
  }
  else cout << "IMPOSSIBLE" << endl;
  return 0;
}

//Author: AnandRaj anand873
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define prin(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "

const int MOD = 998244353,MAX = 1e6+5;
/////////////////FastExp///////////////////
ll powN(ll a,ll p)
{
	if(p==0) return 1;
	ll z=powN(a,p/2);
	z=(z*z)%MOD;
	if(p%2) z=(z*a)%MOD;
	return z;
}
/////////////////FastExp///////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX, true);
vector<int> MinDiv(MAX);
void Sieve()
{
	is_prime[0] = is_prime[1] = false;
	int i,j;
	for (i = 2; i*i <= MAX; i++)
	{
    	if (is_prime[i])
    	{
    		MinDiv[i]=i;
        	for (j = i * i; j <= MAX; j += i)
            {
				is_prime[j] = false;
				MinDiv[j]=i;
    		}
    	}
	}
	for(int i=2;i<=MAX;i++) if(is_prime[i]) MinDiv[i]=i;
}
//////////////////Sieve////////////////////

int main()
{
	int n,k;
	cin>>n>>k;
	vi A(n);
	take(A,0,n);
	sort(all(A));
	int gcd = 0;
	for(int i=0;i<n;i++) gcd = __gcd(gcd,A[i]);

	bool can=false;
	for(int i=n-1;i>=0;i--)
	{
		if(A[i]>=k&&(A[i]-k)%gcd==0) can=true;
	}
	if(can) cout<<"POSSIBLE"<<endl;
	else cout<<"IMPOSSIBLE"<<endl;
}


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

int main() {
  int A, B, M;
  cin >> A >> B >> M;
  vector<int> a(A);
  for (int i = 0; i < A; i++) cin >> a.at(i);
  vector<int> b(B);
  for (int i = 0; i < B; i++) cin >> b.at(i);

  int ans = *min_element(a.begin(), a.end()) + *min_element(b.begin(), b.end());

  for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    ans = min(ans, a.at(x - 1) + b.at(y - 1) - c);
  }

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

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    string str;
    int q;
    cin >> str >> q;

    for (int i = 0; i < q; i++) {
        string inst;
        unsigned int a,b;
        cin >> inst >> a >> b;

        if (inst == "print") {
            cout << str.substr(a,b-a+1) << endl;
        } else if (inst == "reverse") {
            string target = str.substr(a,b-a+1);
            reverse(target.begin(), target.end());
            str = str.substr(0,a) + target + str.substr(b+1);
        } else if (inst == "replace") {
            string rep;
            cin >> rep;
            str = str.substr(0,a) + rep + str.substr(b+1);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
#define fr(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=a; i>=b; i--)
typedef std::vector<long long > vi ;
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
#define endl '\n'
#define show(a) for(auto el:a)cout<<el<<" "
const ll INF=LLONG_MAX/2;
const ll N=2e5+1;
#define yesno(f) yes(f);no(f);
#define yes(f) if(f)cout<<"YES\n";
#define no(f) if(!f)cout<<"NO\n";
using  namespace std;
ll pow1(ll n,ll p)
{
	if(p==0)	return 1;
	ll x=pow1(n, p/2);
	x=(x*x);
	if(p%2==0)
		return x;
	else
		return (x*n);
}
int main()
{
    fast;
   ll t=1;
    //std::cin >> t;
   while(t--)
   {
       ll n,w,x=0,sum=0,a,b,c;
       std::cin >> a>>b>>c;
       if(a==b&&b!=c)
       {
           cout<<"Yes\n";
       }
       else if(a==c&&a!=b)
       {
           cout<<"Yes\n";
       }
       else if(b==c&&a!=b)
       {
           cout<<"Yes\n";
       }
       else
       cout<<"No\n";
   }
}

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

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

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

char toLowerCase(char c){
  if(isLowerCase(c)){
    return c;
  }else{
    return c+'a'-'A';
  }
}
char toUpperCase(char c){
  if(isLowerCase(c)){
    return c-('a'-'A');
  }else{
    return c;
  }
}
ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
void func(vector<int>& l, int depth, int idx){
  ;
}
int main() {
  ll a[2],k;cin>>a[0]>>a[1]>>k;
  for(int i=0;i<k;i++){
    ll half=a[i%2]/2;
    if(a[i%2]%2!=0){
      a[i%2]--;
    }
    a[i%2]-=half;
    a[(i+1)%2]+=half;
  }
  cout<<a[0]<<" "<<a[1]<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define LAR(a, b) ((a)=max((a),(b)))
#define SML(a, b) ((a)=min((a),(b)))
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
#define PB push_back
#define EB emplace_back
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define MOD 1'000'000'007

#define N 512345
int n;
int a[N];
int b[N]={};
map<int, vector<int>> mp;
vi v;
ll dp[N][2];

ll chk(int x, int y){
	v.clear();
	vi u = mp[x];
	REP(i, (int)u.size()-1){
		v.EB(b[u[i+1]]-b[u[i]]);
	}
	dp[v.size()][0]=0;
	dp[v.size()][1]=1;
	for(int i=v.size()-1; i>=0; i--){
		dp[i][0] = dp[i+1][0] + dp[i+1][1]*v[i];
		dp[i][0] %= MOD;
		dp[i][1] = y + dp[i+1][0] + dp[i+1][1]*(v[i]+1);
		dp[i][1] %= MOD;
	}
	DEBUG("chk(%d)\n", x);
	REP(i, (int)v.size()+1) DEBUG("%2d ", i); DEBUG("\n");
	REP(i, (int)v.size())   DEBUG("%2d ", v[i]); DEBUG("\n");
	REP(i, (int)v.size()+1) DEBUG("%2lld ", dp[i][0]); DEBUG("\n");
	REP(i, (int)v.size()+1) DEBUG("%2lld ", dp[i][1]); DEBUG("\n");
	return dp[0][1];
}

int main(){
	scanf("%d", &n);
	REP(i, n) scanf("%d", a+i);
	FOR(i, 1, n) a[i]^=a[i-1];
	REP(i, n) b[i+1]=b[i]+(a[i]==0?1:0);
	REP(i, n) if(a[i]) mp[a[i]].EB(i);

	REP(i, n) DEBUG("%2d ", i); DEBUG("\n");
	REP(i, n) DEBUG("%2d ", a[i]); DEBUG("\n");

	ll ans = 0;
	if(a[n-1]){
		ans+=chk(a[n-1], 0);
		ans%=MOD;
	}else{
		for(auto p:mp){
			ans+=chk(p.first, 1);
			ans%=MOD;
		}
		ll pw = 1;
		REP(i, b[n]-1) pw*=2, pw%=MOD;
		ans+=pw;
		ans%=MOD;
	}
	printf("%lld\n", ans);
}


#include<bits/stdc++.h>
#define Rint register int
using namespace std;
typedef unsigned long long LL;
const int N = 100003;
int n;
LL a[N], b[61], sum, res;
inline void insert(LL x){
	for(Rint i = 60;~i;-- i)
		if((x >> i) & 1)
			if(!b[i]){
				b[i] = x; return;
			} else x ^= b[i];
}
int main(){
	scanf("%d", &n);
	for(Rint i = 1;i <= n;i ++) scanf("%llu", a + i), sum ^= a[i];
	for(Rint i = 1;i <= n;i ++) insert(a[i] &= ~sum);
	for(Rint i = 60;~i;-- i) res = max(res, res ^ b[i]);
	printf("%llu\n", res * 2 + sum);
}

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

typedef long long LL;

namespace INPUT{
	const int L=1<<15;
	char _buf[L],*S,*T,c;
	char _gc(){
		if(S==T){
			T=(S=_buf)+fread(_buf,1,L,stdin);
			if(S==T) return EOF;
		}
		return *S++;
	}
	void readc(char &c){
		for(c=_gc();c<'a'||c>'z';c=_gc());
	}
	void readi(int &X){
		for(c=_gc();c<'0'||c>'9';c=_gc());X=c&15;
		for(c=_gc();c>='0'&&c<='9';X=X*10+(c&15),c=_gc());
	}
}
#define readc INPUT::readc
#define readi INPUT::readi

const int Maxn=1E6+5;

int N;
char S[Maxn];
char T[Maxn];
int a[Maxn],b[Maxn];

int tap[Maxn];
/*int P[Maxn];
int lnk[Maxn];
int f[Maxn];
*/

bool Check(int X){
	for(int i=1;i+X<=tap[N];++i)
		if(a[i+X]-X<b[i]) return false;
	return true;
}

int main(){
	readi(N); bool flag=true;
	for(int i=1;i<=N;++i) readc(S[i]);
	for(int i=1;i<=N;++i) readc(T[i]);
	for(int i=1;i<=N;++i) flag&=(S[i]==T[i]);
	if(flag){puts("0"); return 0;}

	tap[1]=1;
	for(int i=2;i<=N;++i)
		if(T[i]==T[i-1]) tap[i]=tap[i-1],b[tap[i]]=i;
		else tap[i]=tap[i-1]+1;
	/*for(int i=N,j=N;i>=1;--i){
		j=min(j,i);
		while(S[j]!=T[i]&&j>0) --j;
		if(j==0){puts("-1"); return 0;}
		lnk[j]=tap[i];
	}*/
	for(int i=N,j=N;i>=1;--i){
		j=min(j,i);
		while(S[j]!=T[i]&&j>0) --j;
		if(j==0){printf("-1");return 0;}
		a[tap[i]]=j;
	}

	int stt,mid,lst;
	stt=0,lst=N+1;
	while(stt+1<lst){
		mid=(stt+lst)>>1;
		if(Check(mid)) lst=mid;
		else stt=mid;
	}
	cout<<lst;

	return 0;
}


/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include<bits/stdc++.h>
#define ALL(x) (x).begin(),(x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#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 show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int A[maxn],B[maxn];
struct seg_tree{
    ll tr[maxn<<2],lazy[maxn<<2];

    void push_up(int id)
    {
        tr[id] = min(tr[id<<1], tr[id<<1|1]);
    }
    void push_down(int id)
    {
        if(lazy[id]!=0){
            tr[id<<1] += lazy[id],tr[id<<1|1] += lazy[id];
            lazy[id<<1] += lazy[id],lazy[id<<1|1] += lazy[id];
            lazy[id] = 0;
        }
    }
    void update(int id,int stl,int str,int l,int r,ll k)
    {
        if(stl==l && str==r){
            tr[id] += k;
            lazy[id] += k;
            return ;
        }
        push_down(id);
        int mid = (stl+str)>>1;
        if(r<=mid) update(id<<1,stl,mid,l,r,k);
        else if(l>mid) update(id<<1|1,mid+1,str,l,r,k);
        else update(id<<1,stl,mid,l,mid,k),update(id<<1|1,mid+1,str,mid+1,r,k);
        push_up(id);
    }
    ll query(int id,int stl,int str,int l,int r)
    {
        if(stl==l && str==r) return tr[id];
        push_down(id);
        int mid = (stl+str)>>1;
        if(r<=mid) return query(id<<1,stl,mid,l,r);
        else if (l>mid) return query(id<<1|1,mid+1,str,l,r);
        else return min(query(id<<1,stl,mid,l,mid), query(id<<1|1,mid+1,str,mid+1,r));
    }
}dp,rec;
int main()
{
    fastio();
    int n,m; cin>>n>>m;
    rep(i,1,n) cin>>A[i]>>B[i];
    rep(i,1,m) rec.update(1,1,m,i,i,-i);
    rep(i,1,n){
        int R = B[i] + 1;
        if(R<=m){
            ll mi = rec.query(1,1,m,1,B[i]);
            ll now = dp.query(1,1,m,R,R);
            if(now > mi + R){
                dp.update(1,1,m,R,R,mi+R-now);
                ll tmp = rec.query(1,1,m,R,R);
                rec.update(1,1,m,R,R,mi-tmp);
            }
        }
        dp.update(1,1,m,A[i],B[i],inf);
        rec.update(1,1,m,A[i],B[i],inf);
        dp.update(1,1,m,1,m,1);
        rec.update(1,1,m,1,m,1);
        ll ans = dp.tr[1];
        if(ans >= inf) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<int, int>;
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

int n, u, v;
vector< vector<int> > graphlist(100100, vector<int>());
vector<int> takahashi(100100, -1);
vector<int> aoki(100100, -1);
struct posdata {
    int p; int dist;
};

int main() {
    cin >> n >> u >> v;
    u--; v--;
    takahashi.at(u) = 0; aoki.at(v) = 0;
    for(int i = 0; i < n-1; ++i) {
        int aa, bb; cin >> aa >> bb;
        aa--; bb--;
        graphlist.at(aa).emplace_back(bb);
        graphlist.at(bb).emplace_back(aa);
    }

    queue<posdata> tque;
    posdata tfirst;
    tfirst.p = u; tfirst.dist = 0;
    tque.push(tfirst);
    while(!tque.empty()) {
        posdata now = tque.front();
        tque.pop();
        for(int i = 0; i < (int)graphlist.at(now.p).size(); ++i) {
            int np = graphlist.at(now.p).at(i);
            int ndist = now.dist+1;
            if(takahashi.at(np) == -1) {
                takahashi.at(np) = ndist;
                posdata next;
                next.p = np; next.dist = ndist;
                tque.push(next);
            }
        }
    }

    queue<posdata> aque;
    posdata afirst;
    afirst.p = v; afirst.dist = 0;
    aque.push(afirst);
    while(!aque.empty()) {
        posdata now = aque.front();
        aque.pop();
        for(int i = 0; i < (int)graphlist.at(now.p).size(); ++i) {
            int np = graphlist.at(now.p).at(i);
            int ndist = now.dist+1;
            if(aoki.at(np) == -1) {
                aoki.at(np) = ndist;
                posdata next;
                next.p = np; next.dist = ndist;
                aque.push(next);
            }
        }
    }

    int amax = 0;
    for(int i = 0; i < n; ++i) {
        if(aoki.at(i) > takahashi.at(i)) {
            chmax(amax, aoki.at(i));
        }
    }

    cout << amax-1 << endl;
}


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

using namespace std;

#define Rep(i,a,b) for(ll i = a; i < b; ++i)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

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

    void Cout(long long x, const char* end = "\n") { std::cout << x << end; }
    template <typename T> void Cout(const T& x, const char* end = "\n") { std::cout << x << end; }
    template <typename T> void Cout(const std::vector<T>& x, const char* sep = " ", const char* end = "\n") { for (std::size_t i = 0, sz = x.size(); i < sz; ++i) { std::cout << x[i] << (i == sz - 1 ? end : sep); } }

    // 標準入出力
    struct inp {
        std::size_t szi, szj;
        inp(std::size_t _szi = 1, std::size_t _szj = 1) : szi(_szi), szj(_szj) {}
        template <typename T> operator T () const { T a; std::cin >> a; return a; }
        template <typename T> operator std::vector<T>() const { std::vector<T> a(szi); for (std::size_t i = 0; i < szi; ++i) std::cin >> a[i]; return a; }
        template <typename T> operator std::vector<std::vector<T>>() const {
            std::vector<std::vector<T>> a(szi, std::vector<T>(szj));
            for (std::size_t i = 0; i < szi; ++i) for (std::size_t j = 0; j < szj; ++j) cin >> a[i][j]; return a;
        }
    };
    inp inp1;
}

int main() {

    string s = inp1;
    int n = s.length();

    // 文字の距離をとるだけでよい
    vector<vector<int>> pos(26, vector<int>());
    rep(i, n) {
        pos[(s[i] - 'a')].emplace_back(i);
    }

    int a, b;
    a = b = -1;
    rep(i, 26) {
        int sz = pos[i].size();
        rep(j, sz - 1) {
            if (pos[i][j + 1] - pos[i][j] <= 2) {
                a = pos[i][j] + 1;
                b = pos[i][j + 1] + 1;
                i = 26;
                break;
            }
        }
    }

    cout << a << " " << b << endl;

    return 0;
}

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  for (int i=0; i<n-1; i++) {
    if (s[i] == s[i+1]) {
      cout << i+1 << ' ' << i+2 << endl;
      return 0;
    }
  }
  for (int i=0; i<n-2; i++) {
    if (s[i] == s[i+2]) {
      cout << i+1 << ' ' << i+3 << endl;
      return 0;
    }
  }
  cout << -1 << ' ' << -1 << endl;
  return 0;
}


#include <stdio.h>

int main(void){
	int n, a[100], i, j;
	char c, s[53] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
	while(1){
		scanf("%d",&n);
		if(!n) break;
		for(i = 0;i < n;i++)
			scanf("%d",&a[i]);
		getchar();
		for(i = 0;(c = getchar()) != '\n';i++){
			for(j = 0;j < 52;j++){
				if(s[j] == c)
					printf("%c",s[(52 + j - a[i % n]) % 52]);
			}
		}
		printf("\n");
	}
	return 0;
}

#include<iostream>

int main(){
	int e;
	while(1){
		std::cin >> e;
		if(!e)break;
		int m = 1000001;
		for(int i=0;i*i*i<=e;i++){
			for(int ii=0;i*i*i + ii*ii<=e;ii++){
				int x = e-i*i*i-ii*ii;
				int tmp = i+ii+x;
				if(tmp < m) m = tmp;
			}
		}
		std::cout << m << std::endl;
	}
	return 0;
}


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

#define     fast()              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define     ll                  long long int
#define     test()              ll t; cin>>t; while(t--)
#define     lp0(i,begin,end)    for(ll i=begin;i<end;i++)
#define     lp1(i,begin,end)    for(ll i=begin;i<=end;i++)
#define     rlp(i,begin,end)    for(ll i=end;i>=begin;i--)
#define     prec(n)             fixed<<setprecision(n)
#define     initial(a,i)        memset(a,i,sizeof(a))
#define     pb                  push_back
#define     PI                  3.1415926535897932384626433832795
#define     MOD                 1000000007
#define     F                   first
#define     S                   second
#define     all(a)              (a).begin(),(a).end()
#define     BPC(x)              __builtin_popcountll(x)
#define     gcd(a,b)            __gcd(a,b)

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

ll power(ll x,ll n)
{
    ll result = 1;
    while (n)
    {
        if (n & 1)
            result = result * x;
        n = n / 2;
        x = x * x;
    }
    return result;
}

// Solution Function
void solution(ll compte)
{
    // cout<<compte<<"\n";
    ll n,m;
    cin>>n>>m;
    ll x =10;
    cout<<(m+(100*(10-min(x,n))) )<<"\n";
}
// Driver Function
int main()
{
    ll compte = 1;
    //test()
    //{
        solution(compte);
        compte++;
    //}
    return 0;
}

#include <bits/stdc++.h>
const int INF=1e9;
//const int MOD=1e9+7;
const int MOD=998244353;
const long long LINF=1e18;
using namespace std;
#define int long long
//template
std::vector<int> Factorial(5234567),Finverse(5234567);
int pw(int n,int k){
  if(k<0)return pw(n,k+MOD-1);
  int res=1;
  while(k){
    if(k&1)res*=n;res%=MOD;
    n*=n;n%=MOD;
    k>>=1;
  }
  return res;
}
void Cinit(){
  Factorial[0]=1;
  for(int i=1;i<5234567;i++)Factorial[i]=Factorial[i-1]*i%MOD;
  for(int i=0;i<5234567;i++)Finverse[i]=pw(Factorial[i],MOD-2);
}
int nCk(int n,int k){
  if(n<k)return 1;if(k<0)return 0;
  int res=Factorial[n];
  res*=Finverse[k];res%=MOD;
  res*=Finverse[n-k];res%=MOD;
  return res;
}
//main
signed main(){
  Cinit();
  int N,M;cin>>N>>M;
  int ans=nCk(3*M+(N-1),N-1);//cout<<ans<<endl;
  int mi=N*nCk(M-1+N-1,N-1)%MOD;
  ans-=mi;
  ans+=MOD;ans%=MOD;
  //cout<<ans<<endl;
  for(int i=M+1;i<=min(N,3*M);i++){
    if((i&1)!=((3*M)&1))continue;
    int p=(3*M-i)/2;
    int q=nCk(p+N-1,N-1);
    int s=nCk(N,i);
    int a=q*s%MOD;
    ans-=a;
    ans+=MOD;ans%=MOD;
  }
  cout<<ans<<endl;
}


// C++ 14
#include <bits/stdc++.h>
using namespace std;
template <typename T>  ostream& operator<<(ostream& os, const vector<T>& v)  {  os << "[";  for (int i = 0; i < v.size(); ++i) {  os << v[i];  if (i != v.size() - 1)  os << ", ";  }  os << "]";  return os;  }
template <typename T> void print(T v, string s = "\n") { cout << v << s; }
template <typename T> void in(T& v) { cin >> v; }
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)

int main() {
  string s; in(s);
  int n = s.size();
  loop(len,2,4) {
    if (len > n) break;
    loop(l,0,n-len+1) {
      map<char, int> M;
      int max_ = 0;
      loop(i,l,l+len) {
        M[s[i]]++;
        max_ = max(max_, M[s[i]]);
      }
      if (max_ > len/2) {
        cout << l+1 << " " << l+len << endl;
        return 0;
      }
    }
  }
  cout << "-1 -1" << endl;
  return 0;
}

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

ll pow[19];

ll dfs(int k, int i, ll dif, ll dif_org){
    // if(k == 4) cout << dif << endl;
    if(i < k-i+1) return dif == 0 ? 1:0;
    if(i == k-i+1){
        if(dif == 0){
            if(k != i){
                return 10;
            }else{
                return 9;
            }
        }else{
            return 0;
        }
    }
    ll d = pow[i-1]-pow[k-i];
    ll r = dif%d;
    ll m = dif/d;
    ll sum = 0;
    if(r == 0){
        if(abs(m) < 10) {
            if(k != i){
                sum += (10-abs(m))*dfs(k, i-1, r, dif_org);
            }else{
                sum += (10-abs(m)-1)*dfs(k, i-1, r, dif_org);
            }
        }
    }else{
        if(abs(m) < 10) {
            if(k != i){
                sum += (10-abs(m))*dfs(k, i-1, r, dif_org);
            }else{
                sum += (10-abs(m)-1)*dfs(k, i-1, r, dif_org);
            }
        }
        if(dif > 0){
            if(abs(m)+1 < 10) {
                if(k != i){
                    sum += (10-abs(m)-1)*dfs(k, i-1, r-d, dif_org);
                }else{
                    sum += (10-abs(m)-2)*dfs(k, i-1, r-d, dif_org);
                }
            }
        }else if(dif < 0){
            if(abs(m)+1 < 10) {
                if(k != i){
                    sum += (10-abs(m)-1)*dfs(k, i-1, r+d, dif_org);
                }else{
                    sum += (10-abs(m)-2)*dfs(k, i-1, r+d, dif_org);
                }
            }
        }
    }
    return sum;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    pow[0] = 1;
    for(int i = 1; i < 19; i++) pow[i] = pow[i-1]*10;
    ll D;
    cin >> D;
    ll ans = 0;
    for(int i = 1; i <= 19; i++){
        ans += dfs(i, i, D, D);
        // cout << i << ' ' << ans << endl;
    }
    cout << ans << endl;
}


#include <iostream>
#define rep(S, L) for (int i = S; i < L; ++i)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int ans = 0;
  if (n % k > 0) {
    ans = 1;
  }
  cout << ans << endl;
  return 0;
}

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

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  solve(N, K);
  return 0;
}


//----------------------------templates
    #pragma GCC optimize ("Ofast")
    #pragma GCC target ("tune=native")
    #pragma GCC target ("avx")
    //----------------------------
    #include <bits/stdc++.h>
    using namespace std;

    typedef long long ll;
    typedef unsigned long long ull;
    #define int ll

    #define FOR(i,j,n) for (int i=(int)(j);i<(n);i++)
    #define REP(i,n) for (int i=0;i<(int)(n);i++)
    #define REPS(i,n) for (int i=1;i<=(int)(n);i++)
    #define REPN(i,n) for (int i=(int)(n)-1;i>=0;i--)
    #define REPNS(i,n) for (int i=(int)(n);i>0;i--)

    #define I(n) scanf("%lld", &(n))
    #define LL(n) scanf("%lld", &(n))
    #define pb(n) push_back((n))
    #define mp(i,j) make_pair((i),(j))
    #define eb(i,j) emplace_back((i),(j))
    #define y0 y3487465
    #define y1 y8687969
    #define j0 j1347829
    #define j1 j234892
    #define uniq(v) v.erase( unique(v.begin(), v.end()), v.end() )

    #define all(x) (x).begin(),(x).end()
    #define sz(x) ((int)(x).size())

    typedef vector<int> vi;
    typedef pair<int,int> pi;
    typedef vector<pi> vpi;
    typedef vector<vi> vvi;
    typedef vector<vpi> vvpi;
    typedef vector<vvi> vvvi;

    const int mod = 1000000007;

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

int n,x;
vi s,prim;
int ret;
int facto[201];

ll powll(ll base,ll power){
    ll ans = 1;
    while (power){
        if (power&1) ans = (base*ans)%mod;
        base = (base*base)%mod;
        power >>= 1;
    }
    return ans;
}
int invs[201];

int dp[201][100010];

signed main(){
    facto[0] = 1;
    REPS(i,200) facto[i] = (facto[i-1] * i) % mod;
    REPS(i,200) invs[i] = powll(i,mod-2);

    I(n); I(x);
    s.resize(n);
    REP(i,n) I(s[i]);
    sort(all(s), greater<int>());

    dp[0][x] = 1;
    REP(i,n){
        REP(j,x+1){
            int prob = invs[n-i];
            dp[i+1][j] = (dp[i+1][j] + dp[i][j] * (1 + mod - prob)) % mod;
            dp[i+1][j % s[i]] = (dp[i+1][j % s[i]] + dp[i][j] * prob) % mod;
        }
    }
    int ret = 0;
    REP(j,s[n-1]) ret = ((dp[n][j] * j) + ret) % mod;
    cout << (ret * facto[n]) % mod << endl;
}

#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 4e5 + 7;

int a[N], b[N], ok[N], okk[N], c[N], d[N], fail[N];
int n;

void goo() {
	for(int i=1,j=fail[0]=-1;i<n;i++) {
		while(j >= 0 && d[j+1] != d[i])
			j = fail[j];
		if(d[j+1] == d[i])
			j++;
		fail[i] = j;
	}
	for(int i=0,j=-1;i<2*n;i++) {
		while(j >= 0 && (j == n - 1 || d[j+1] != c[i]))
			j = fail[j];
		if(d[j+1] == c[i])
			j++;
		if(j == n - 1) okk[i-n+1] = 1;
	}
}

void go(int bit) {
	for(int i=0;i<2*n;i++)
		c[i] = a[i] >> bit & 1;
	for(int i=0;i<n;i++)
		d[i] = b[i] >> bit & 1;
	for(int i=0;i<n;i++) okk[i] = 0;
	goo();
	for(int i=0;i<n;i++)
		d[i] ^= 1;
	goo();
	for(int i=0;i<n;i++) ok[i] &= okk[i];
}

signed main()
{
	IO_OP;

	cin >> n;
	for(int i=0;i<n;i++) {
		cin >> a[i];
		a[i+n] = a[i];
		ok[i] = 1;
	}
	for(int i=0;i<n;i++)
		cin >> b[i];
	for(int i=0;i<30;i++)
		go(i);
	for(int i=0;i<n;i++)
		if(ok[i]) cout << i << " " << (a[i] ^ b[0]) << endl;
}


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

#define w cout
#define e '\n'
#define int long long
#define sz(x) (int)x.size()
template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; }

int mod = 998244353;

void solve() {
	int n ; cin >> n;
	int v[n + 1] = {};
	for(int i = 1 ; i <= n ;i ++) cin >> v[i];
	int dd[n + 1][30];

	memset(dd , 0, sizeof dd);

	int l , q; cin >> l >> q;

	for(int i = 1; i <= n ;i ++){
		int can = v[i] - l;
		dd[i][0] = max(1ll , (int)(lower_bound(v + 1 , v + n + 1 , can) - v));
	}
	for(int i = 1; i <= n ;i ++) {
		for(int j = 1; j <= 17 ;j ++) {
			dd[i][j] = dd[dd[i][j-1]][j-1];
		}
	}

	while(q--) {
		int a ,b ; cin >> a >> b;
		if(a > b) swap(a , b);
		int ans = 0;

		for(int j = 17 ; j >= 0; j --) {
			if(dd[b][j] > a) {
				ans += pow(2 , j);
				b = dd[b][j];
			}
		}
		w << ans + 1 << e;
	}
}

signed main() {
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t = 1;
	//~ cin >> t;
	while(t--) {
		solve();
	}
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
signed main(){
    ll n;cin>>n;
    vector<ll>x(n);
    rep(i,n)cin>>x[i];
    ll l,q;cin>>l>>q;
    vector<ll>nxt(n);
    vector<pll>b(0);
    for(int i=n-1;i>=0;i--){
        if(i==n-1){
            b.pb(mp(-x[i],i));
            nxt[i]=i;
            continue;
        }
        pll k=*lower_bound(all(b),mp(-x[i]-l,-1LL));
        nxt[i]=k.second;
        b.pb(mp(-x[i],i));
    }
    vector<vector<ll> >dp(n,vector<ll>(32));
    rep(i,n)dp[i][0]=nxt[i];
    REP(i,1,32){
        rep(j,n){
            dp[j][i]=dp[dp[j][i-1]][i-1];
        }
    }
    rep(i,32){
        //cout<<dp[0][i]<<endl;
    }
    while(q){
        q--;
        ll a,b;cin>>a>>b;
        a--;b--;
        if(a>b){
            swap(a,b);
        }
        ll l=0,r=n;
        while(r-l>1){
            ll mid=(r+l)/2;
            ll x=a;
            rep(j,32){
                if(mid&(1LL<<j)){
                    x=dp[x][j];
                }
            }
            if(q==3){
                //cout<<mid<<' '<<a<<' '<<b<<endl;
            }
            if(x>=b)r=mid;
            else l=mid;
        }
        cout<<r<<endl;
    }
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;

const int mod = 1e9 + 7;

int main() {
	int n, m; cin >> n >> m;
	vector<ll>x(n), y(m); for (auto&& xx : x)cin >> xx;for (auto&& yy : y)cin >> yy;
	sort(x.begin(), x.end());sort(y.begin(), y.end());
	ll a = 0, b = 0;
	for (int i = 0; i < n; i++)a -= x[i] * (n - 1 - i - i);
	for (int i = 0; i < m; i++)b -= y[i] * (m - 1 - i - i);
	cout << (a % mod) * (b % mod) % mod << endl;
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>
#include <numeric>
#include<stdio.h>
#include <queue>
#include <iomanip>
#include <float.h>
#include <set>
#include<functional>
#include <stack>
#include <time.h>
#include <climits>
#include <bitset>
using namespace std;
int  gcd(int a, int b) {
	if (a < b) gcd(b, a);
	int r;
	while ((r = a%b)) {
		a = b;
		b = r;
	}
	return b;
}
long long lcm(long long a, long long b) {
	return a / gcd(a, b) * b;
}
long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t*b; swap(a, b);
		u -= t*v; swap(u, v);
	}
	u %= mod;
	if (u < 0) u += mod;
	return u;
}
namespace NTT {
	const int MOD = 998244353;  // to be set appropriately
	const long long PR = 3;     // to be set appropriately

	void trans(vector<long long> &v, bool inv = false) {
		int n = (int)v.size();
		for (int i = 0, j = 1; j < n - 1; j++) {
			for (int k = n >> 1; k >(i ^= k); k >>= 1);
			if (i > j) swap(v[i], v[j]);
		}
		for (int t = 2; t <= n; t <<= 1) {
			long long bw = modpow(PR, (MOD - 1) / t, MOD);
			if (inv) bw = modinv(bw, MOD);
			for (int i = 0; i < n; i += t) {
				long long w = 1;
				for (int j = 0; j < t / 2; ++j) {
					int j1 = i + j, j2 = i + j + t / 2;
					long long c1 = v[j1], c2 = v[j2] * w % MOD;
					v[j1] = c1 + c2;
					v[j2] = c1 - c2 + MOD;
					while (v[j1] >= MOD) v[j1] -= MOD;
					while (v[j2] >= MOD) v[j2] -= MOD;
					w = w * bw % MOD;
				}
			}
		}
		if (inv) {
			long long inv_n = modinv(n, MOD);
			for (int i = 0; i < n; ++i) v[i] = v[i] * inv_n % MOD;
		}
	}

	// C is A*B
	vector<long long> mult(vector<long long> A, vector<long long> B) {
		int size_a = 1; while (size_a < A.size()) size_a <<= 1;
		int size_b = 1; while (size_b < B.size()) size_b <<= 1;
		int size_fft = max(size_a, size_b) << 1;

		vector<long long> cA(size_fft, 0), cB(size_fft, 0), cC(size_fft, 0);
		for (int i = 0; i < A.size(); ++i) cA[i] = A[i];
		for (int i = 0; i < B.size(); ++i) cB[i] = B[i];

		trans(cA); trans(cB);
		for (int i = 0; i < size_fft; ++i) cC[i] = cA[i] * cB[i] % MOD;
		trans(cC, true);

		vector<long long> res((int)A.size() + (int)B.size() - 1);
		for (int i = 0; i < res.size(); ++i) res[i] = cC[i];
		return res;
	}
};
template<class V, int ME> class BIT {
public:
	V bit[1 << ME];
	V operator()(int e) { if (e<0) return 0; V s = 0; e++; while (e) s += bit[e - 1], e -= e&-e; return s; }
	void add(int e, V v) { e++; while (e <= 1 << ME) bit[e - 1] += v, e += e&-e; }
};
typedef pair<int, int>p;
vector<int>graph[100005];
int nyuuji[100005] = { 0 };
int parent[100005];
void dfs(int now) {
	vector<int>child;
	for (int i = 0; i < graph[now].size(); i++) {
		nyuuji[graph[now][i]] -= 1;
		if (nyuuji[graph[now][i]] == 0) {
			child.push_back(graph[now][i]);
			parent[graph[now][i]] = now;
		}
	}
	for (int i = 0; i < child.size(); i++) {
		dfs(child[i]);
	}
}
int doubutu[100005];
int next( int now,int prev,int ans) {
	if (doubutu[now]) {
		ans = 1 - ans;
	}
	if (ans) {
		return doubutu[prev];
	}
	else return 1 - doubutu[prev];
}
long long mod = 1e9 + 7;
signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	long long vsum = 0;
	long long hsum = 0;
	long long tmp1, tmp2;
	cin >> tmp1;
	for (int i = 2; i <= n; i++) {
		cin >> tmp2;
		long long nagasa = tmp2 - tmp1;
		vsum += ((nagasa*(i - 1)) % mod)*(n - i + 1);
		vsum %= mod;
		tmp1 = tmp2;
	}
	cin >> tmp1;
	for (int i = 2; i <= m; i++) {
		cin >> tmp2;
		long long nagasa = tmp2 - tmp1;
		hsum += ((nagasa*(i - 1)) % mod)*(m - i + 1);
		hsum %= mod;
		tmp1 = tmp2;
	}
	cout << (hsum*vsum) % mod;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
	string s;
	int q;
	cin >> s >> q;
	cin.ignore();

	string buf;
	while (q)
	{
		getline(cin, buf);

		//cout << "DEBUG:" << buf << endl;

		if (buf.find("replace ") != string::npos)
		{
			char cmd[10] = {0};
			int a1, a2;
			char a3[1001] = {0};
			sscanf(buf.c_str(), "%s %d %d %s", cmd, &a1, &a2, a3);
			s.replace(a1, (a2+1)-a1, string(a3));
			//cout << "DEBUG:replaced:" << s << endl;
		}
		else if (buf.find("reverse ") != string::npos)
		{
			char cmd[10] = {0};
			int a1, a2;
			sscanf(buf.c_str(), "%s %d %d", cmd, &a1, &a2);
			string sub = s.substr(a1, (a2+1)-a1);
			//cout << "DEBUG:sub( ):" << sub << endl;
			reverse(sub.begin(), sub.end());
			//cout << "DEBUG:sub(r):" << sub << endl;
			size_t subsize = sub.size();
			for (int i = a1, j = 0; i < a1 + subsize; i++, j++) {
				s[i] = sub[j];
			}
			//cout << "DEBUG:reversed:" << s << endl;
		}
		else if (buf.find("print ") != string::npos)
		{
			char cmd[10] = {0};
			int a1, a2;
			sscanf(buf.c_str(), "%s %d %d", cmd, &a1, &a2);
			cout << s.substr(a1, (a2+1)-a1) << endl;
		}
		q--;
	}

	return 0;
}

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

string reverse_partial(string str, int s, int e) {
    string top = s != 0 ? str.substr(0, s - 1) : "";
    string head = str.substr(s, e - s + 1);
    string tail = str.substr(e + 1, str.size());
    string reverse = "";
    for (int i=head.size()-1; i >= 0; i--) {
        reverse += head[i];
    }
    return (top + reverse + tail);
}

int main () {
    string str;
    cin >> str;
    int q;
    cin >> q;

    for (int i=0; i<q; i++) {
        string order, p;
        int a, b;
        cin >> order >> a >> b;
        if (order == "print") {
            cout << str.substr(a, b - a + 1) << "\n";
        }
        else if (order == "replace") {
            cin >> p;
            str = str.replace(a, b - a + 1, p);
        }
        else if (order == "reverse") {
            string sub = str.substr(a, b - a + 1);
            for (int i=0; i < sub.size(); i++) {
                str[i + a] = sub[sub.size() - i - 1];
            }
        }
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, f, n) for (int i = (f); i < (int)(n); i++)
#define repe(i, f, n) for (int i = (f); i <= (int)(n); i++)
using namespace std;
using ll = long long;
using GraphI = vector<vector<int>>;
using GraphC = vector<vector<char>>;
using pint = pair<int, int>;

const int INF = 1001001001;

int main() {
  string S;
  cin >> S;
  int ans = INF;
  rep(i, 0, S.size() - 2) {
    int x = stoi(S.substr(i, 3));
    ans = min(ans, abs(753 - x));
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
int INF = 1e9+7;
signed main() {
    int N;
    cin >> N;
    vector<int>cnt(N+1,1);
    for(int i = 2; i <= N; i++) {
        int X = i;
        for(int j = 2; j*j <= i; j++) {
            int Y = j;
            if(X%Y == 0) {
                while(X%Y == 0) {
                    cnt[Y]++;
                    X/=Y;
                }
            }
        }
        if(X != 1) {
            cnt[X]++;
        }
    }
    int ans = 1;
    for(int i = 2; i <= N; i++) {
        ans*=cnt[i];
        ans = ans%INF;
    }
    cout << ans << endl;
}


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

int main() {
  int A,B;
  string S;

  cin >> A >> B >> S;
  for(int i=0;i<S.size();++i){
    if(i == A){
      if(S.at(i) == '-'){
        continue;
      }else{
        cout << "No" << endl;
        return 0;
      }
    }else{
      if(S.at(i) == '-'){
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

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

int main() {
  int A,B;
  cin >> A >> B;
  for(int i=0;i<A+B+1;i++){
    char a;
    cin >> a;
    if((i==A&&a!='-')||(i!=A&&a=='-')){
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}


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

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

int dp[3][100010];

int main(){

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

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll mod = 1000000007;
const int MAX = 1000100;

int main(){
    int n; cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    ll ans = 1;
    vector<ll> num(n);
    for(int i = 0; i < n; i++){
        if(a[i] == 0){
            ans *= 3 - num[0];
            num[0]++;
        }
        else{
            ans *= num[a[i] - 1] - num[a[i]];
            num[a[i]]++;
        }
        ans %= mod;
    }
    cout << ans << endl;
}


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

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

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

  double sum_d = 0;
  int k = 0;
  do {
    ++k;
    rep(i, n-1) sum_d += sqrt(pow(x[perm[i+1]] - x[perm[i]], 2) + pow(y[perm[i+1]] - y[perm[i]], 2));
  } while (next_permutation(perm.begin(), perm.end()));
  double ave_d = sum_d / k;
  printf("%.7f\n", ave_d);
  return 0;
}

#include <iostream>
#include <cstdio>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#define MAX 200000
#define INFTY 1<<30
using namespace std;
typedef long long llong;

template <typename T>
class Array
{
private:
  llong merge(int l, int m, int r)
  {
    llong cnt = 0;
    int n1 = m - l;
    int n2 = r - m;
    vector<T> L, R;
    L.resize(n1+1);
    R.resize(n2+1);
    for(int i = 0; i < n1; i++) L[i] = A[l+i];
    for(int i = 0; i < n2; i++) R[i] = A[m+i];
    L[n1] = INFTY;
    R[n2] = INFTY;
    int a = 0;
    int b = 0;
    for(int i = l; i < r; i++)
    {
      if(L[a] <= R[b])
      {
        A[i] = L[a];
        a++;
      }
      else
      {
        A[i] = R[b];
        b++;
        cnt += n1 - a;  // mid + a - b - 1
      }
    }
    //cerr << cnt << endl;
    return cnt;
  }
  int partition(int p, int r)
  {
    T x = A[r];
    int i = p-1;
    T tmp;
    for(int j = p; j < r; j++)
    {
      if(A[j] <= x)
      {
        i++;
        tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
      }
    }
    tmp = A[i+1];
    A[i+1] = A[r];
    A[r] = tmp;
    return i+1;
  }
public:
  Array();
  Array(vector<T> &arr){A = arr;}
  vector<T> A;
  T operator [] (int x)
  {
    return A[x];
  }
  void dispArr()
  {
    for(int i = 0; i < A.size()-1 ; i++) cout << A[i] << " ";
    cout << A[A.size()-1] << endl;
  }
  void swap(int i, int j)
  {
    T tmp = A[i];
    A[i] = A[j];
    A[j] = tmp;
  }
  /*
  l : ?????????index
  r : (?????????index + 1) or (?????° + ?????????index)
  */
  llong mergeSort(int l, int r)
  {
    llong v1, v2, v3;
    if(l + 1 < r){
      int m = (l + r) / 2;
      v1 = mergeSort(l, m);
      v2 = mergeSort(m, r);
      v3 = merge(l, m ,r);
      return v1 + v2 + v3;
    }
    else return 0;
  }
  /*
  l : ?????????index
  r : ?????????index
  */
  void quicksort(int p, int r)
  {
    if(p < r)
    {
      int q = partition(p,r);
      quicksort(p, q-1);
      quicksort(q+1, r);
    }
  }
};

int main()
{
  int n; cin >> n;
  vector<int> v;
  v.resize(n);
  for(int i = 0; i < n; i++) cin >> v[i];
  Array<int> T(v);
  cout << T.mergeSort(0, n) << endl;
  //T.dispArr();
  return 0;
}


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

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

    rep(i, b)
    {
        rep(j, a) cout << 0;
        for (int j = a; j < w; j++)
            cout << 1;
        cout << endl;
    }
    for (int i = b; i < h; i++)
    {
        rep(j, a) cout << 1;
        for (int j = a; j < w; j++)
            cout << 0;
        cout << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
int main(void){

    /*
    int a, b;
    cin >> a >> b ;
    //*/

    //*
    vector<int> vec(3);
    cin >> vec.at(0) >> vec.at(1) >> vec.at(2) ;

    sort(vec.begin(), vec.end());
    //*/

    /*
    if( a < 6 ){
        cout << 0 << endl;
    } else if (a < 13){
        cout << b / 2 << endl;
    } else {
        cout << b << endl;
    }
    //*/

    cout << vec.at(0) + vec.at(1) << endl;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

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

  ll ans = INFL;
  auto d = [](ll x) {
    ll res = 0;
    while (x) {
      res++;
      x /= 10;
    }
    return res;
  };
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ll a = d(i);
      ll b = d(n / i);
      ll f = max(a, b);
      ans = min(ans, f);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main(){
#ifdef LOCAL
	freopen("inp.txt", "r", stdin );
	freopen("ans.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	string s;
	cin >> s;
	if(s=="SUN") cout << 7 << '\n';
	if(s=="MON") cout << 6 << '\n';
	if(s=="TUE") cout << 5 << '\n';
	if(s=="WED") cout << 4 << '\n';
	if(s=="THU") cout << 3 << '\n';
	if(s=="FRI") cout << 2 << '\n';
	if(s=="SAT") cout << 1 << '\n';
}


#include <bits/stdc++.h>

int main() {
    unsigned int firstNumber;
    unsigned int lastNumber;
    std::cin >> firstNumber >> lastNumber;

    unsigned int numOfPalindrome = 0U;
    for(unsigned int i = firstNumber; i <= lastNumber; ++i) {
        std::string forwardNumber = std::to_string(i);
        std::string reverseNumber = forwardNumber;
        std::reverse(reverseNumber.begin(), reverseNumber.end());

        if(forwardNumber == reverseNumber) {
            ++numOfPalindrome;
        }
    }
    std::cout << numOfPalindrome << std::endl;
}

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast <t> (x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef double db ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int maxn = 1e6 + 100 ;
const int inf = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const ll mod = 1e9 + 7 ;
const double eps = 1e-5 ;

ll n, s;

ll x[maxn], p[maxn];

ll ans[maxn], tim;

signed main()
{
	scanf("%lld %lld", &n, &s);
	rep(i, 1, n) scanf("%lld %lld", &x[i], &p[i]);
	ll l = 1, r = n;
	rep(i, 1, n)
	{
		if(x[r] < s) ans[i] = x[l], p[r] += p[l ++];
		else if(x[l] > s) ans[i] = x[r], p[l] += p[r --];
		else
		{
			if(p[l] < p[r]) ans[i] = x[l], p[r] += p[l ++];
			else ans[i] = x[r], p[l] += p[r --];
		}
	}
	per(i, n, 1) tim += abs(s - ans[i]), s = ans[i];
	printf("%lld\n", tim);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  int s,i,a,b,c;
  while(cin>>s,s){
  while(s--){
    cin>>a>>b>>c;
    if(a==100||b==100||c==100||b+c>179||a+b+c>239)cout<<'A'<<endl;
    else if(a+b+c>209||(a+b+c>149&&(a>79||c>79)))cout<<'B'<<endl;
    else cout<<'C'<<endl;
  }}
}

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5+5;

void solve() {
    int res = N*100;
    int n; cin >> n;
    vector<int> X(n), Y(n);
    string d(n,'?');
    for (int i = 0; i < n; i++) {
        cin >> X[i] >> Y[i] >> d[i];
    }
    auto rotate = [&](){
        for (int i = 0; i < n; i++) {
            if (d[i] == 'R') d[i] = 'U';
            else if (d[i] == 'U') d[i] = 'L';
            else if (d[i] == 'L') d[i] = 'D';
            else d[i] = 'R';

            swap(X[i], Y[i]);
            X[i] = N - X[i];
        }
    };
    // -> <-
    auto ck0 = [&](){
        vector<vector<int>> evs(N+1);
        for (int i = 0; i < n; i++) {
            if (d[i]=='L' || d[i]=='R') {
                evs[Y[i]].push_back(i);
            }
        }
        for (auto& a: evs) {
            sort(a.begin(), a.end(), [&](auto& u, auto& v){
                return X[u] < X[v];
            });
            int n = a.size();
            for (int i = 1; i < n; i++) {
                if (d[a[i-1]] == 'R' && d[a[i]] == 'L') {
                    res = min(res, (X[a[i]] - X[a[i-1]]) * 5);
                }
            }
        }
    };
    // ->
    //   ^
    auto ck1 = [&](){
        vector<vector<int>> evs(N*2);
        for (int i = 0; i < n; i++) {
            if (d[i] == 'R' || d[i] == 'U') {
                evs[X[i]+Y[i]].push_back(i);
            }
        }
        for (auto& a: evs) {
            sort(a.begin(), a.end(), [&](auto& u, auto& v){
                return X[u] < X[v];
            });
            int n = a.size();
            for (int i = 1; i < n; i++) {
                if (d[a[i-1]] == 'R' && d[a[i]] == 'U') {
                    res = min(res, (X[a[i]] - X[a[i-1]]) * 10);
                }
            }
        }
    };
    for (int _ = 0; _ < 4; _++) {
        ck0();ck1();rotate();
    }
    if (res == N*100) cout << "SAFE";
    else cout << res;
}

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


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

typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,ll> PLL;
typedef pair<P,P> PP;
typedef vector<ll> VECLL;
typedef vector<P> VECP;
typedef priority_queue<P,VECP,greater<P> > PQP;
typedef priority_queue<ll, VECLL, greater<ll> > PQll;

#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue;
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second

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

int main(){
    ll n,k;
    cin >> n >> k;
    ll a[n] = {};
    rep(i,0,n) cin >> a[i];

    ll ans = 0;
    rep(i,0,n-1){
        if(a[i] + a[i+1] > k){
            if(a[i] >= k){
                ans += a[i+1] + a[i] - k;
                a[i] = k;
                a[i+1] = 0;
            }
            else{
                ans += a[i]+a[i+1]-k;
                a[i+1] -= a[i]+a[i+1]-k;
            }
        }
    }
    cout << ans << endl;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
set<pair<long long, long long>> st;
long long sum = 0;
int main() {
	long long n, m;
	cin >> n >> m;
	long long ans = 0;
	st.insert(make_pair(0, 1));
	for (int i = 0; i < n; i++) {
		long long a;
		cin >> a;
		sum = (sum + a) % m;
		pair<long long, long long> pa;
		if (st.lower_bound(make_pair(sum, 0)) != st.end()) {
			pa = *st.lower_bound(make_pair(sum, 0));
			if (sum == pa.first) {
				ans += pa.second;
				st.erase(*st.lower_bound(make_pair(sum, 0)));
				st.insert(make_pair(sum, pa.second + 1));
			}
			else {
				st.insert(make_pair(sum, 1));
			}
		}
		else {
			st.insert(make_pair(sum, 1));
		}
	}
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<tuple>
#include<vector>
#include<cstdlib>
#include<cstdint>
#include<stdio.h>
#include<cmath>
#include<limits>
#include<iomanip>
#include<ctime>
#include<climits>
#include<random>
#include<queue>
#include<map>

using namespace std;

template <class T> using V = vector<T>;

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

const long long INF = 1LL << 60;
const double pi=acos(-1);

using ll = long long;
using db = long double;
using st = string;
using ch = char;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;
using graph = V<V<int>>;
using pq = priority_queue<ll>;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define bgn begin()
#define en end()
#define SORT(a) sort((a).bgn,(a).en)
#define REV(a) reverse((a).bgn,(a).en)
#define fi first
#define se second
#define sz size()
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a);
#define ALL(a)  (a).begin(),(a).end()

ll Sum(ll n) {
ll m=0;
  while(n){
    m+=n%10;
    n/=10;
  }
  return m;
}

const int MAX = 510000;
// change
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void Comuse() {
    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;
    }
}

#define comuse Comuse()

ll combi(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;
}

ll perm(int n,int k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] % MOD) % MOD;
}

ll modpow(ll a,ll n,ll mod){
  ll ans=1;
  while(n>0){
    if(n&1){
      ans=ans*a%mod;
    }

    a=a*a%mod;

    n>>=1;
  }

  return ans;
}

ll modinv(ll a, ll mod) {
    return modpow(a, mod - 2, mod);
}

ll modcombi(int n,int k,int mod){
  ll ans=1;
  for(ll i=n;i>n-k;i--){
    ans*=i;
    ans%=mod;
  }
  for(ll i=1;i<=k;i++){
    ans*=modinv(i,mod);
    ans%=mod;
  }

  return ans;
}

ll lcm(ll a,ll b){
  ll n;
  n=a/gcd(a,b)*b;
  return n;
}

vll div(ll n){
  vll 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);
  return (ret);
}

vector<bool> isprime(MAX+100,true);

void primeuse(){
  isprime[0]=false;
  isprime[1]=false;
  for(int i=2;i<MAX+50;i++){
    int up=sqrt(i)+1;
    for(int j=2;j<up;j++){
      if(i%j==0){
        isprime[i]=false;
      }
    }
  }
}

void bf(ll n,string s){
  for(ll i=0;i<n;i++){
    cout<<s;
  }
  cout<<"\n";

}

void Solve();

const int MAX_N = 131072;
//segment tree
int NN;
int seg[MAX_N*2-1];
void seguse(){
  for(int i=0;i<2*NN-1;i++){
    seg[i]=INT_MAX;
  }
}

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

/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  |
\****************************************/
//segtreeの葉の先頭の添え字はN-1
void Solve(){
  string s;
    cin>>s;
  ll n=s.size();
  FOR(i,0,n-1){
    if(s[i]==s[i+1]){
      cout<<i+1<<" "<<i+2<<"\n";
      return;
    }
  }

  FOR(i,0,n-2){
    if(s[i]==s[i+2]){
      cout<<i+1<<" "<<i+3<<"\n";
      return;
    }
  }

  cout<<"-1 -1\n";
}

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
  string s;cin>>s;
  string a[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
  for(int i=0;i<7;i++){
  	if( s!="SUN" && a[i]==s){
  		cout<<(6-i);
  	}
  	else if(s=="SUN"){
  		cout<<7;
  		break;
  }
}
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>;

const ll MOD = (ll)(1e9 + 7);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const double PI = acos(-1.0);

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;

vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

int main()
{

  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(50) << setiosflags(ios::fixed);

  int n, m;
  cin >> n >> m;
  int a[100010], b[100010];
  vector<int> E[100010];
  int in_cnt[100010] = {};
  rep(i, n + m - 1)
  {
    cin >> a[i] >> b[i];
    E[a[i]].push_back(b[i]);
    in_cnt[b[i]]++;
  }
  int root;
  for (int i = 1; i <= n; ++i)
  {
    if (in_cnt[i] == 0)
    {
      root = i;
      break;
    }
  }

  queue<int> que;
  int d[100010];
  int check_cnt[100010] = {};
  d[root] = 0;
  que.push(root);
  while (!que.empty())
  {
    int q = que.front();
    que.pop();
    for (auto to : E[q])
    {
      d[to] = d[q] + 1;
      check_cnt[to]++;
      if (check_cnt[to] == in_cnt[to])
        que.push(to);
    }
  }

  int par[100010];
  for (int i = 1; i <= n; ++i)
    par[i] = -1;
  par[root] = 0;
  que.push(root);
  while (!que.empty())
  {
    int q = que.front();
    que.pop();
    for (auto to : E[q])
    {
      if (par[to] != -1)
        continue;
      if (d[to] == d[q] + 1)
      {
        par[to] = q;
        que.push(to);
      }
    }
  }

  for (int i = 1; i <= n; ++i)
    cout << par[i] << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define pb push_back
using ll = long long;
using D = double;
using LD = long double;
using P = pair<int, int>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main(){
    int h,w; cin >> h >> w;
    vector<int> a(h), b(h);
    rep(i,h) {
        cin >> a[i] >> b[i];
        a[i]--;
    }

    map<int, int> mp;
    multiset<int> cand;
    rep(i,w) mp[i] = i;
    rep(i,w) cand.insert(0);

    rep(i,h){
        int l = a[i], r = b[i];
        auto it = mp.lower_bound(l);
        int rb = -1;
        while(it != mp.end() && it -> first <= r){
            chmax(rb, it -> second);
            cand.erase(cand.find(it -> first - it -> second));
            mp.erase(it++);
        }

        if(rb != -1 && r < w){
            cand.insert(r - rb);
            mp[r] = rb;
        }

        int ans = -1;
        if(cand.size()) ans = *cand.begin() + i+1;
        cout << ans << endl;
    }
}


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

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

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

  string s,t;
  cin >> s >> t;

  rep(i,n){
    int same=n-i;

    bool check=true;
    rep(j,same){
      if(s.at(n-same+j)!=t.at(j)){check=false; break;}
    }

    if(check){cout<<n*2-same; return 0;}
  }

  cout << n*2;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;

string res;

int main()
{
    ll a, b;
    string s;
    cin >> a >> b >> s;

    res = "Yes";
    for (int i = 0; i < s.length(); i++)
    {
        if (i == a)
        {
            if (s[i] != '-')
            {
                res = "No";
            }
        }
        else
        {
            if (!('0' <= s[i] && s[i] <= '9'))
            {
                res = "No";
            }
        }
    }
    println(res);
}


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

int main(){
  int m,n_min,n_max,gap,temp=0;
  while(1){
    cin >> m >> n_min >> n_max;
    if(m==0 && n_min==0 && n_max==0){break;}
    int P[m+1];
    P[0]=-1;
    for(int i=1;i<=m;i++){
      cin >> P[i];
    }
    gap=0;
    for(int i=n_min;i<=n_max;i++){
      if(P[i]-P[i+1]>=gap){
        gap=P[i]-P[i+1];
        temp=i;
      }
    }
    cout << temp << endl;
  }
}

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, Q; string S;
	cin >> N >> S >> Q;
	vector<int> dsum(N + 1), msum(N + 1);
	for (int i = 0; i < N; ++i) {
		dsum[i + 1] = dsum[i] + (S[i] == 'D' ? 1 : 0);
		msum[i + 1] = msum[i] + (S[i] == 'M' ? 1 : 0);
	}
	for (int i = 0; i < Q; ++i) {
		int K;
		cin >> K;
		long long cur = 0, ans = 0;
		for (int i = 0; i < N; ++i) {
			if (S[i] == 'C') {
				ans += cur;
			}
			if (S[i] == 'M') {
				cur += dsum[i] - dsum[max(i - K + 1, 0)];
			}
			if (i >= K - 1 && S[i - K + 1] == 'D') {
				cur -= msum[i + 1] - msum[i - K + 2];
			}
		}
		cout << ans << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(a)  (a).begin(),(a).end()

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

int main() {
    int N;
    cin >> N;
    vector<double> a(N);
    REP(i,N) cin >> a[i];

    double av = accumulate(ALL(a),double(0))/N;
    int ans  = 0;
    double shortest = INF;
    REP(i,N) {
        if (chmin(shortest,abs(av-a[i]))) {
            ans = i;
        }
    }

    cout << ans << endl;

    return 0;
}

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

int main(){
    int N;
    cin >> N;
    int sum = 0;
    double ave = 0;
    double mini = 100;
    double aa = 0;
    int ans = 0;
    vector<int> a(N);
    for(int i = 0;i < N;i++){
        cin >> a.at(i);
        sum += a.at(i);
    }
    ave =  (double)sum/(double)N;
    /*cout << ave << endl;*/

    for(int i = 0;i < N;i++){
        aa = abs(ave - a.at(i));
        if(mini > aa){
            mini = aa;

            ans = i;
        }
    }
    cout << ans << endl;
}


#include <cstdio>
#include <bitset>
#include <cmath>
#include <complex>
#include <vector>
#include <algorithm>
#include <stack>
#include <iostream>
#include <sstream>
#define EPS 1e-6

using namespace std;
typedef complex<double> coor;

double cross(coor a, coor b)
{
  return a.real() * b.imag() - a.imag() * b.real();
}

double dot(coor a, coor b)
{
  return a.real() * b.real() + a.imag() * b.imag();
}

int main()
{
  string str;
  while (getline(cin, str)) {
    istringstream is(str);
    string token;
    int n;
    while (is >> token) {
      for (int i = 0; i < 26; i++) {
	string tmp;
	for (int j = 0; j < token.size(); j++) {
	  tmp += (token[j] - 'a' + i) % 26 + 'a';
	}
	//cout << "i = " << i << "tmp = " << tmp << endl;
	if (tmp == "the" || tmp == "this" || tmp == "that") {
	  n = i;
	  goto next;
	}
      }
    }
  next:
    //printf("n = %d\n", n);
    for (int i = 0; i < str.size(); i++) {
      printf("%c", ('a' <= str[i] && str[i] <= 'z') ?(str[i] - 'a' + n) % 26 + 'a' : str[i]);
    }
    printf("\n");
  }
}

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

string tar[] = { "the", "this", "that" };

int main()
{
	string in;
	while (getline(cin, in))
	{
		string ans;
		for (int i=0; i<26; i++)
		{
			ans = in;
			for (int k=0; k<ans.size(); k++)
			{
				if (islower(ans[k]))
				{
					int r = i - ('z' - ans[k]);
					if (r > 0)
						ans[k] = (char) ((int) 'a' + r - 1);
					else
						ans[k] = (char) ((int) ans[k] + i);
				}
			}
			if (	   ans.find(tar[0], 0) != string::npos
				|| ans.find(tar[1], 0) != string::npos
				|| ans.find(tar[2], 0) != string::npos)
				break;
		}

		cout << ans << endl;
	}
}


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

#define Abigail inline void
typedef long long LL;

const int N=15,INF=(1<<30)-1;

int n,m,e[N+9][N+9],sum;
int a[(1<<N)+9];

void Get_a(){
  for (int g=1;g<1<<n;++g)
    for (int i=0;i<n;++i){
    	if (g>>i&1^1) continue;
    	for (int j=i+1;j<n;++j)
    	  if (g>>j&1) a[g]+=e[i][j];
		}
}

int b[N+9][(1<<N)+9];

void Get_b(){
  for (int g=1;g<1<<n;++g)
  	for (int i=0;i<n;++i){
  		if (g>>i&1) continue;
  	  b[i][g]=a[g];
  		for (int j=0;j<n;++j)
  		  if (g>>j&1) b[i][g]+=e[i][j];
		}
}

int dp[N+9][(1<<N)+9];

void Get_dp(){
  for (int i=0;i<n;++i)
    for (int g=0;g<1<<n;++g) dp[i][g]=-INF;
  dp[0][1]=0;
  for (int g1=1;g1<1<<n;++g1)
  	for (int i=0;i<n;++i){
  		if (dp[i][g1]==-INF) continue;
  		int t=~g1&(1<<n)-1;
  		for (int g2=t;g2;g2=g2-1&t)
  			dp[i][g1|g2]=max(dp[i][g1|g2],dp[i][g1]+b[i][g2]);
  		for (int j=0;j<n;++j)
  		  if (g1>>j&1^1&&e[i][j]){
				  dp[j][g1|1<<j]=max(dp[j][g1|1<<j],dp[i][g1]+e[i][j]);
				}
		}
}

Abigail into(){
  scanf("%d%d",&n,&m);
  for (int i=1;i<=m;++i){
  	int x,y,v;
  	scanf("%d%d%d",&x,&y,&v);--x;--y;
  	e[x][y]=e[y][x]=v;
  	sum+=v;
	}
}

Abigail work(){
  Get_a();
  Get_b();
  Get_dp();
}

Abigail outo(){
  printf("%d\n",sum-dp[n-1][(1<<n)-1]);
}

int main(){
  into();
  work();
  outo();
  return 0;
}


#include <stdio.h>

int main(){
	int H, W, N, res;
	scanf("%d", &H);
	scanf("%d", &W);
	scanf("%d", &N);
	if(H>W){
		res = N/H;
		N%H > 0? res+=1 : res=res;
	}
	else{
		res = N/W;
		N%W > 0? res+=1 : res=res;
	}
	printf("%d\n", res);

	return 0;
}

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

const int INF = 1001001001;

int main(){
    ll n, x; cin >> n >> x;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    ll ans = 0;
    for(int i = 0; i < n-1; i++){
        if(a[i] > x){
            ll tmp = a[i] - x;
            ans += tmp + a[i+1];
            a[i+1] = 0;
        }
        else{
            if(a[i+1] + a[i] <= x) continue;
            else{
                ll tmp = a[i+1] + a[i] - x;
                ans += tmp;
                a[i+1] -= tmp;
                if(i + 1 == n - 1 && a[i+1] > x) ans += a[i+1] - x;
            }
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

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

    int ans = 0;
    bool ok = true;
    rep(i, 3)
    {
        if (s[i] == 'R')
        {
            if (ok)
            {
                ans++;
            }
            ok = true;
        }
        else
        {
            if (ans > 0)
            {
                ok = false;
            }
        }
    }

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

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

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

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

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