#include<bits/stdc++.h>
#include<math.h>
#define rep(i,k,n) for(int i=k;i<n;i++)
#define MOD 1000000007LL
#define ROD 1000000000
#define INF 12345678
#define MIN(a,b) (a>b?b:a)
using namespace std;
#include<bits/stdc++.h>
#include<math.h>
#define rep(i,k,n) for(int i=k;i<n;i++)
#define ll long long
#define mp make_pair
#define  piii pair<pair<ll,ll>,pair <ll,ll>>
#define pii pair <ll,ll>
#define fi first
#define se second
ll ch(ll);
ll n,cr[100005],tot,x;
piii br[100005];
int main()
{
	cin>>n>>x;
	rep(i,0,n)
	{
		cin>>br[i].fi.se>>br[i].se.fi>>br[i].se.se;
		br[i].fi.fi=(br[i].se.se)*(x-br[i].fi.se)+br[i].fi.se*br[i].se.fi;
		tot+=br[i].fi.se*br[i].se.fi;
	}
	//cout<<tot<<"\n";
	sort(br,br+n,greater <piii> ());
	rep(i,0,n)
	{
		cr[i]=br[i].fi.fi;
		if(i-1>=0)
		cr[i]+=cr[i-1];
	}
	cr[n]=cr[n-1];
	// rep(i,0,n+1)
	// cout<<cr[i]<<" ";
	// rep(i,0,n)
	// cout<<br[i].fi.se<<" "<<br[i].se.fi<<" "<<br[i].se.se<<" "<<cr[i]<<"\n";
	ch(2497);
	ll low=0,high=n*x,mid,ans;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(ch(mid))
		{
			ans=mid;
			high=mid-1;
		}
		else
			low=mid+1;
	}
	//cout<<ch(400)<<" ";
	cout<<ans;
}
ll ch(ll mid)
{
	ll q=mid/x,r=mid-x*q;
	if(mid==n*x)
		return 1;
	//cout<<r<<" ";
	rep(i,0,n)
	{
		ll temp=0;
		if(i<q)
		{
			if(q>=0)
			temp+=cr[q]-br[i].fi.fi;
			//temp+=(r>br[i].fi.se?r*br[i].se.se:r*br[i].se.fi);
			temp+=(r>br[i].fi.se?(r-br[i].fi.se)*br[i].se.se+br[i].fi.se*br[i].se.fi:r*br[i].se.fi);
		}
		else
		{
			if(q-1>=0)
			temp+=cr[q-1];
			temp+=(r>br[i].fi.se?(r-br[i].fi.se)*br[i].se.se+br[i].fi.se*br[i].se.fi:r*br[i].se.fi);
			//cout<<"HERE"<<br[i].fi.se<<" "<<r<<" "<<r-br[i].fi.se<<" "<<br[i].se.se<<" ";
		}
		// ll qw=r>br[i].fi.se?(r-br[i].fi.se)*br[i].se.se+br[i].fi.se*br[i].se.fi:r*br[i].se.fi;
		// cout<<mid<<" "<<tot<<" "<<qw<<" "<<r<<" "<<" "<<temp<<"\n";
		if(temp>=tot)
			return 1;
	}
	return 0;
}

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
template<typename T>
struct BIT{
    //1-index
    //0-indexで取ると無限ループする
    private:
    V<T> arr;
    int n;

    public:
    BIT(int siz){
        n=siz;
        arr.assign(n+1,0);
    }
    //一点更新 aにvを加える
    void add(T a,T v){
        //x+=(x&-x) 初項a,それ以降a<=2^Nが列挙
        for(T x=a;x<=n;x+=(x&-x)){
            arr[x]+=v;
        }
    }
    T sum(T a){
        // 1からaまでの区間和
        T ret=0;
        for(T x=a;x>0;x-=(x&-x))ret+=arr[x];
        return ret;
    }
    T get(T x){//x番目に大きい数　// 区間和がw以上となる最小のindを返す
        if(x<=0) return 0;
        T N=1;
        while(N*2<=n)N*=2;
        T v=0;
        for(T i=N;i>0;i/=2){
            if(v+i<=n&&arr[v+i]<x){
                x-=arr[v+i];
                v+=i;
            }
        }
        return v+1;
    }
    T query(int l,int r){
        return sum(r)-sum(l);
    }
};
int main(){
    int n,q;
    cin>>n>>q;
    BIT<ll> bit(n+5);
    ll a;
    for(int i=0;i<n;i++){
        cin>>a;
        bit.add(i+1,a);
    }
    ll t,l,r;
    while(q--){
        cin>>t>>l>>r;
        if(t){
            cout<<bit.query(l,r)<<"\n";
        }else{
            bit.add(l+1,r);
        }
    }
}


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

int main() {
    uint N;
    long long x;
    cin >> N >> x;

    vector<long long> a(N, 0);
    long long sum = 0;
    for (auto &c: a) {
        cin >> c;
        sum += c;
    }

    if (sum < x) {
        cout << a.size() - 1 << endl;
    }
    else if (sum == x) {
        cout << a.size() << endl;
    }
    // sum > x
    else {
        sort(a.begin(), a.end());
        int numHappy = 0;
        for (size_t i=0; i<a.size(); ++i) {
            x -= a[i];
            if (x >= 0)  ++numHappy;
            else break;
        }
        cout << numHappy << endl;
    }
}

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie(0);
	int n, x;
	cin >> n >> x;
	vector<int> a;
	int i, count = 0;
	for(i=0; i<n; i++){
		int p;
		cin >> p;
		a.push_back(p);
	}
	sort(a.begin(), a.end());
	for(i=0; i<n; i++){
		if(a[i]>x){
			x = 0;
			break;
		}
		else{
			count++;
			x = x-a[i];
		}
	}
	if(x) count = count - 1;
	cout << count << endl;
	return 0;
}


#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
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;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000009;
const double pi = acos(-1);

int dist_sum(int N) {
    int ans = 0;
    while(N > 0) {
        ans += N%10;
        N /= 10;
    }
    return ans;
}

int main() {
    int N; cin >> N;
    cout << (N%dist_sum(N) == 0 ? "Yes" : "No") << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main (){
  int N,K,sum,s;
  cin>>N;
  K=1;
  sum=0;
  for (int i=0;i<8;i++){
    for(int j=0;j<i;j++){
      K*=10;
    }
    s=(N-N%K)%(K*10)/K;
    sum+=s;
    K=1;
  }
  int m;
  m=N%sum;
  if (m==0){
    cout<<"Yes"<<endl;
  }
  else{
    cout<<"No"<<endl;
  }
}


#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	unsigned long long a;
	double b;
	cin>> a >>b;
	unsigned long long c = 1000*b;
	unsigned long long d = a*c;
	unsigned long long e = d/1000;
	cout<< e <<endl;
}

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

int main (){
    int A, B;
    cin >> A >> B;
    int ans = A*B;
    cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef vector <int> VI;
typedef set <int> SI;

int get_digit( int n )
{
	int count = 0;
	while ( n >= 10 )
	{
		n /= 10;
		count++;
	}
	return count + 1;
}

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

int main( void )
{
	int n, k;
	while ( cin >> n >> k && n && k )
	{
		VI cards(n);
		for ( int i = 0; i < n; i++ ) cin >> cards[i];
		sort( cards.begin(), cards.end() );

		SI h;
		for ( int i = 0; i < 1<<n; i++ )
		{
			int cnt = 0;
			for ( int j = 0; j < n; j++ )
			{
				if ( i & 1 << j ) cnt++;
			}
			if ( cnt != k ) continue;

			VI C;
			for ( int j = 0; j < n; j++ )
			{
				if ( i & 1 << j ) C.push_back( cards[j] );
			}

			do {
				int m = 0;
				for ( VI::iterator it = C.begin(); it != C.end(); it++ )
				{
					if ( m == 0 ) m += *it;
					else
					{
						m *= ten_pow( get_digit( *it ) );
						m += *it;
					}
				}
				h.insert( m );
			} while ( next_permutation( C.begin(), C.end() ) );
		}

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

	}
	return 0;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;

int n, k;
string str[10];

int main(){
	while(scanf("%d", &n),n){
		set<string> s;
		scanf("%d", &k);
		rep(i,n) cin >> str[i];
		if(k == 2){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j) continue;
					s.insert(str[i]+str[j]);
				}
			}
		} else if(k == 3){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(j == i) continue;
					for(int u = 0; u < n; u++){
						if(u == i || u == j) continue;
						s.insert(str[i]+str[j]+str[u]);
					}
				}
			}
		} else{
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j) continue;
					for(int u = 0; u < n; u++){
						if(u == i || u == j) continue;
						for(int v = 0; v < n; v++){
							if(v == i || v == j || v == u) continue;
							s.insert(str[i]+str[j]+str[u]+str[v]);
						}
					}
				}
			}
		}
		cout << s.size() << endl;
	}
}


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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    for(int T = 0; T < t ; T++){
        ll n, x;
       cin >> n >> x;
       ll a[n], ans = 0;
       for(int i = 0; i < n; i++)
            cin >> a[i];
       if(a[0] > x){
           ans += a[0] - x;
           a[0] = x;
       }
       for(int i = 1; i < n; i++){
           if(a[i] + a[i - 1] > x){
                ans += a[i] + a[i -1] - x;
               a[i] = x - a[i - 1];
           }
       }
       std::cout << ans  << std::endl;
    }
    return 0;
}

#include <algorithm>
#include <boost/optional.hpp>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for(int i = 0, i##_MACRO = (n); i < i##_MACRO; i++)
#define RANGE(i, a, b) for(int i = (a), i##_MACRO = (b); i < i##_MACRO; i++)
#define EACH(e, a) for(auto&& e : a)
#define ALL(a) std::begin(a), std::end(a)
#define RALL(a) std::rbegin(a), std::rend(a)
#define FILL(a, n) memset((a), n, sizeof(a))
#define FILLZ(a) FILL(a, 0)
#define INT(x) (static_cast<int>(x))
#define PRECISION(x) std::fixed << std::setprecision(x)

using namespace std;

using ll = long long;
using VI = std::vector<int>;
using VI2D = std::vector<vector<int>>;
using VLL = std::vector<long long>;
using VLL2D = std::vector<vector<long long>>;

constexpr int INF = 2e9;
constexpr double EPS = 1e-10;
constexpr double PI = acos(-1.0);

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

template <typename T>
constexpr int sign(T x) {
	return x < 0 ? -1 : x > 0 ? 1 : 0;
}

template <>
constexpr int sign(double x) {
	return x < -EPS ? -1 : x > EPS ? 1 : 0;
}

template <typename T, typename U>
constexpr void chmax(T& m, U x) {
	m = max(m, x);
}

template <typename T, typename U>
constexpr void chmin(T& m, U x) {
	m = min(m, x);
}

template <typename T>
constexpr T square(T x) {
	return x * x;
}

int main() {
	int n, x;
	cin >> n >> x;
	VI a(n);
	REP(i, n) { cin >> a[i]; }

	ll result = 0;
	REP(i, n - 1) {
		if(a[i] + a[i + 1] > x) {
			int d = a[i] + a[i + 1] - x;
			if(a[i + 1] < d) {
				a[i] -= d - a[i + 1];
				a[i + 1] = 0;
			} else {
				a[i + 1] -= d;
			}
			result += d;
		}
	}

	cout << result << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

void solve(int m, int nmi, int nma) {
    VI P(m);
    REP(i, m) cin >> P[i];
    RSORT(P);
    P.EB(0);
    PII ans = MP(abs(P[nmi] - P[nmi - 1]), nmi);
    FOR(i, nmi, nma) {
        int dif = abs(P[i] - P[i + 1]);
        if (dif >= ans.first) {
            ans = MP(dif, i + 1);
        }
    }
    cout << ans.second << endl;
}

signed main(void) {
    int n, m, p, a, b, c, x, y, z, q;
    string s;
    while (cin >> m >> a >> b, m) {
        solve(m, a, b);
        //return 0;
    }
}

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char convNext(char ch) {
	if('a' <= ch && ch <= 'y') return ch+1;
	if(ch=='z')return 'a';
	return ch;
}
int main() {
	char str[90];
	while(true) {
		if(!fgets(str, 90, stdin))break;
		for(int i = 0; i < 26; i++) {
			if(strstr(str,"this") || strstr(str,"that") || strstr(str,"the"))break;
			for(int j = 0; str[j]; j++) {
				str[j]=convNext(str[j]);
			}
		}
		fputs(str, stdout);
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const LL mod=1e9+7;
LL f[1010][1010],n,a,b,c,d,C[1010][1010],inv[1010][1010],fac[1010],finv[1010];
void pre()
{
	inv[0][1]=inv[1][1]=1;fac[0]=fac[1]=1;
	for(LL i=2;i<=1000;i++) inv[i][1]=(mod-mod/i)*inv[mod%i][1]%mod,fac[i]=fac[i-1]*i%mod;
	for(LL i=2;i<=1000;i++) inv[i][1]=inv[i-1][1]*inv[i][1]%mod;
	for(LL i=1;i<=1000;i++)
		for(LL j=2;j<=1000;j++) inv[i][j]=inv[i][1]*inv[i][j-1]%mod;
	for(LL i=1;i<=1000;i++)
		for(LL j=1;j<=i;j++) C[i][j]=fac[i]*inv[i-j][1]%mod;
}
LL get(LL x,LL y,LL i) {return C[x][i*y]*inv[y][i]%mod*inv[i][1]%mod;}
LL dfs(LL x,LL y)
{
	if(f[x][y]!=-1) return f[x][y];
	if(x==0) return 1;
	if(y<a) return 0;
	f[x][y]=0;
	for(LL i=c;i<=d;i++)
	{
		LL num=i*y;
		if(num<=x) (f[x][y]+=dfs(x-num,y-1)*get(x,y,i)%mod)%=mod;
		else break;
	}
	(f[x][y]+=dfs(x,y-1))%=mod;
	return f[x][y];
}
int main()
{
	pre();
	scanf("%lld %lld %lld %lld %lld",&n,&a,&b,&c,&d);
	memset(f,-1,sizeof(f));
	printf("%lld",dfs(n,b));
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<iomanip>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define DEBUG(x) cout<<#x<<"="<<x<<endl
#define DEBUG2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl
typedef long long ll;
// #define LOCAL
const ll MOD=998244353;
ll _gcd(ll a,ll b){if(b==0)return a;return _gcd(b,a%b);}
ll gcd(ll a,ll b){a=abs(a),b=abs(b);if(a<b)swap(a,b);return _gcd(a,b);}
ll qpow(ll a,ll n){ll rt=1;while(n){if(n&1)rt=(rt*a)%MOD;a=a*a%MOD;n>>=1;}return rt;}
int N,M,K;
const int MAXN=2e5+10;
ll factor[MAXN];
void init()
{
    factor[0]=1;
    for(int u=1;u<MAXN;u++){
        factor[u]=(factor[u-1]*u)%MOD;
    }
}
ll C(ll n,ll k)
{
    return factor[n]*qpow(factor[n-k],MOD-2)%MOD*qpow(factor[k],MOD-2)%MOD;
}
void solve()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>N>>M>>K;
    init();
    ll ans=0;
    for(int u=0;u<=K;u++){
        ans=(ans+M*C(N-1,u)%MOD*qpow(M-1,N-1-u)%MOD)%MOD;
    }
    cout<<ans;
}
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    solve();
}


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

vector<bool> digit(10, false);

bool check(int N) {
  bool res = true;
  while(N > 0) {
    int x = N % 10;

    if (digit[x]) {
      res = false;
      break;
    }
    N /= 10;
  }

  return res;

}

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

  for (int i = 0; i < K; i++) {
    int x;
    cin >> x;
    digit[x] = true;
  }

  while(true) {
    if (check(N)) {
      cout << N << endl;
      return 0;
    }

    N++;
  }

}

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> ng_d;
  int dd;
  vector<int> d;
  for (int i = 0; i < k; ++i) {
    cin >> dd;
    ng_d.push_back(dd);
  }
  for (int i = 0; i < 10; ++i) {
    bool ng = false;
    for (int j = 0; j < ng_d.size(); ++j) {
      if (ng_d[j] == i) {
        ng = true;
        break;
      }
    }
    if (!ng) {
      d.push_back(i);
    }
  }
  sort(d.begin(), d.end());

  int ans = d[0];
  bool ok = false;

  for (int i1 = 0; i1 < d.size(); ++i1) {
    int tmp1 = d[i1];
    if (tmp1 >= n) {
      ans = tmp1;
      goto end;
    }
  }

  for (int i1 = 0; i1 < d.size(); ++i1) {
    int tmp1 = d[i1];

    for (int i2 = 0; i2 < d.size(); ++i2) {
      int tmp2 = tmp1 * 10 + d[i2];
      if (tmp2 >= n) {
        ans = tmp2;
        goto end;
      }
    }
  }
  for (int i1 = 0; i1 < d.size(); ++i1) {
    int tmp1 = d[i1];

    for (int i2 = 0; i2 < d.size(); ++i2) {
      int tmp2 = tmp1 * 10 + d[i2];

      for (int i3 = 0; i3 < d.size(); ++i3) {
        int tmp3 = tmp2 * 10 + d[i3];
        if (tmp3 >= n) {
          ans = tmp3;
          goto end;
        }
      }
    }
  }
  for (int i1 = 0; i1 < d.size(); ++i1) {
    int tmp1 = d[i1];

    for (int i2 = 0; i2 < d.size(); ++i2) {
      int tmp2 = tmp1 * 10 + d[i2];

      for (int i3 = 0; i3 < d.size(); ++i3) {
        int tmp3 = tmp2 * 10 + d[i3];

        for (int i4 = 0; i4 < d.size(); ++i4) {
          int tmp4 = tmp3 * 10 + d[i4];

          if (tmp4 >= n) {
            ans = tmp4;
            goto end;
          }
        }
      }
    }
  }
  for (int i1 = 0; i1 < d.size(); ++i1) {
    int tmp1 = d[i1];

    for (int i2 = 0; i2 < d.size(); ++i2) {
      int tmp2 = tmp1 * 10 + d[i2];

      for (int i3 = 0; i3 < d.size(); ++i3) {
        int tmp3 = tmp2 * 10 + d[i3];

        for (int i4 = 0; i4 < d.size(); ++i4) {
          int tmp4 = tmp3 * 10 + d[i4];

          for (int i5 = 0; i5 < d.size(); ++i5) {
            int tmp5 = tmp4 * 10 + d[i5];
            if (tmp5 >= n) {
              ans = tmp5;
              goto end;
            }
          }
        }
      }
    }
  }

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


#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int H, W;

bool check(int i, int j, int dx, int dy, vector<vector<int>> &field) {
    int x = i + dx;
    int y = j + dy;
    return (x >= 0 && x < H && y >= 0 && y < W && (field[x][y] == -1));
}

int main() {
    cin >> H >> W;
    vector<vector<int>> field(H, vector<int>(W));
    int white = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            char c; cin >> c;
            if (c == '.') {
                field[i][j] = -1;
                white++;
            }
            else field[i][j] = -2;
        }
    }
    vector<int> dx = {-1, 1, 0, 0};
    vector<int> dy = {0, 0, -1, 1};
    field[0][0] = 0;
    queue<pii> que;
    que.push({0, 0});
    while (!que.empty()) {
        int i = que.front().first;
        int j = que.front().second;
        que.pop();
        if (i == (H - 1) && j == (W - 1)) {
            cout << white - field[H-1][W-1] - 1 << endl;
            return 0;
        }
        for (int k = 0; k < 4; k++) {
            if (check(i, j, dx[k], dy[k], field)) {
                que.push({i + dx[k], j + dy[k]});
                field[i + dx[k]][j + dy[k]] = field[i][j] + 1;
            }
        }
    }
    cout << -1 << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef vector<bool> vbool;
typedef vector<int> vint;

#define inf 1e+9
#define endl "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep_rev(i, n) for (int i = n-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {a = b;return 1;} return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {a = b;return 1;} return 0;
}
template <class T>
void line(vector<T> &x){for (auto&& xi : x) cin >> xi;}

inline void IN(void){ return;}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return;}

#define INT(...) int __VA_ARGS__; IN (__VA_ARGS__)
#define LINT(...) lint __VA_ARGS__; IN (__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
    sort(all(x)); x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end){
    for (auto i = first; i < end - 1; i++) cout << *i << " ";
    cout << *(end-1) << endl;
}

template<class... T>
void debug_print(T... args){
    vector<lint> tmp = initializer_list<lint>{args...};
    print_vec(all(tmp));
}
template<class T>
void print(T a){ cout << a << endl;}

vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

int ord(char x){ return (int)(x - 'a');}
char chr(lint x){ return (char)(x + (lint)('a'));}
lint mod = 1e9+7;
// lint mod =

lint sum(vlint a){lint ret = 0; for(auto&& v:a) ret += v; return ret;}
lint vmini(vlint a, lint &index){
    lint ret = LLONG_MAX;
    rep(i, Size(a)){ if (chmin(ret, a[i])) index = i;}
    return ret;
}
lint vmaxi(vlint a, lint &index){
    lint ret = -LLONG_MAX;
    rep(i, Size(a)){ if (chmax(ret, a[i])) index = i;}
    return ret;
}

lint vmin(vlint a){
    lint ret = LLONG_MAX; for (auto && v : a) chmin(ret, v); return ret;
}
lint vmax(vlint a){
    lint ret = -LLONG_MAX; for (auto && v : a) chmax(ret, v); return ret;
}

vlint base_3(int k){
    vlint ret; while (k > 0){ ret.pb(k % 3); k /= 3;} reverse(all(ret)); return ret;
}
map<lint, lint> trial_division(lint n){
    map<lint, lint> factor;
    lint tmp = (lint)(sqrt(n)) + 1;
    for (lint i = 2; i < tmp; ++i){
        while (n % i == 0){
            n /= i;
            factor[i] ++;
        }
    }
    if(n != 1) factor[n] ++;
    return factor;
}

lint dist(lint x1, lint y1, lint x2, lint y2){
    return (x1-x2) * (x1-x2) + (y1-y2) * (y1 - y2);
}

int main(){
    INT(h, w);
    char o;
    vector<vint> grid(h+2, vint(w+2, 0));
    int cnt = 0;
    rep1(i, h+1){
        rep1(j, w+1){
            cin >> o;
            if (o == '.'){
                grid[i][j] = 1;
                cnt ++;
            }
        }
    }
    queue<pair<int, int>> q;
    q.push({1, 1});
    vector<vint> dist(h+2, vint(w+2, -1));
    dist[1][1] = 0;
    int y, x, ny, nx;
    vint dx = {-1, 0, 0, 1};
    vint dy = {0, -1, 1, 0};
    while (!q.empty()){
        y = q.front().first;
        x = q.front().second; q.pop();
        rep(i, 4){
            ny = y + dy[i];
            nx = x + dx[i];
            if (dist[ny][nx] != -1 || grid[ny][nx] == 0){
                continue;
            }
            q.push({ny, nx});
            dist[ny][nx] = dist[y][x] + 1;
        }
    }
    if (dist[h][w] == -1){
        print(-1);
    }
    else{
        print(cnt - dist[h][w]-1);
    }

}


#include <iostream>
using namespace std;
int main(){
  int n;
  cin >> n;
  int i=1;
  while(i*i<=n) i++;
  cout << (i-1)*(i-1) << "\n";
  return 0;
}

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {
    int n;
    cin >> n;
    n = pow(n, 0.5);
    cout << n * n << endl;
}


#define DIN freopen("input.txt","r",stdin);
#define DOUT freopen("output.txt","w",stdout);
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=(a);i<=(int)(b);i++)
#define REP_(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL,LL> P;
int read()
{
    int x=0,flag=1; char c=getchar();
    while((c>'9' || c<'0') && c!='-') c=getchar();
    if(c=='-') flag=0,c=getchar();
    while(c<='9' && c>='0') {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return flag?x:-x;
}

const int maxn=1e6+5,v=5e5,N=1e6;
struct node {int x,y,dir;} a[maxn];
VI x[maxn],y[maxn],p[maxn],q[maxn];
int n,ans=1e9;
char s[10];
bool cmp1(int i,int j) {return a[i].x==a[j].x?a[i].y<a[j].y:a[i].x<a[j].x;}
bool cmp2(int i,int j) {return a[i].y==a[j].y?a[i].x<a[j].x:a[i].y<a[j].y;}

int main()
{
    n=read();
    REP(i,1,n)
    {
        a[i].x=read();
        a[i].y=read();
        scanf("%s",s);
        a[i].dir=s[0];
    }
    sort(a+1,a+n+1,[&](node a,node b){return a.x==b.x?a.y<b.y:a.x<b.x;});
    REP(i,2,n) if(a[i].x==a[i-1].x && a[i].y==a[i-1].y) return puts("0"),0;
    REP(i,1,n)
    {
        x[a[i].x+v].pb(i);
        y[a[i].y+v].pb(i);
        p[a[i].y-a[i].x+v].pb(i);
        q[a[i].y+a[i].x+v].pb(i);
    }

    REP(i,1,N)
    {
        sort(x[i].begin(),x[i].end(),cmp1);
        sort(y[i].begin(),y[i].end(),cmp2);
        sort(p[i].begin(),p[i].end(),cmp1);
        sort(q[i].begin(),q[i].end(),cmp1);
    }

    REP(i,1,N) if(x[i].size()>1)
    {
        int yy=-1;
        REP(j,0,x[i].size()-1)
        {
            int id=x[i][j];
            if(yy>=0 && a[id].dir=='D') ans=min(ans,abs(a[id].y-yy)*5);
            if(a[id].dir=='U') yy=a[id].y;
        }
    }
    REP(i,1,N) if(y[i].size()>1)
    {
        int xx=-1;
        REP(j,0,y[i].size()-1)
        {
            int id=y[i][j];
            if(xx>=0 && a[id].dir=='L') ans=min(ans,abs(a[id].x-xx)*5);
            if(a[id].dir=='R') xx=a[id].x;
        }
    }
    REP(i,1,N) if(p[i].size()>1)
    {
        int xx1=-1,xx2=-1;
        REP(j,0,p[i].size()-1)
        {
            int id=p[i][j];
            if(xx1>=0 && a[id].dir=='L') ans=min(ans,abs(a[id].x-xx1)*10);
            if(xx2>=0 && a[id].dir=='D') ans=min(ans,abs(a[id].x-xx2)*10);
            if(a[id].dir=='U') xx1=a[id].x;
            if(a[id].dir=='R') xx2=a[id].x;
        }
    }
    REP(i,1,N) if(q[i].size()>1)
    {
        int xx1=-1,xx2=-1;
        REP(j,0,q[i].size()-1)
        {
            int id=q[i][j];
            if(xx1>=0 && a[id].dir=='U') ans=min(ans,abs(a[id].x-xx1)*10);
            if(xx2>=0 && a[id].dir=='L') ans=min(ans,abs(a[id].x-xx2)*10);
            if(a[id].dir=='R') xx1=a[id].x;
            if(a[id].dir=='D') xx2=a[id].x;
        }
    }
    if(ans>=(1e9)-1) puts("SAFE");
    else printf("%d\n",ans);

    return 0;
}

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

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll llINF = 1LL << 60;
const int iINF = 1e8;

//collision time
int colltime(vector<P> yu){
    int res = iINF;
    sort(yu.begin(),yu.end());
    rep(i,yu.size()-1){
        if( yu[i].second == 0 && yu[i+1].second != 0 ){
            chmin(res, (yu[i+1].first - yu[i].first));
        }
    }
    return res;
}

int min_colltime(map<int,vector<P>>& UD){
    int res = iINF;
    for(auto udi : UD) chmin(res,colltime(udi.second));
    return res;
}

//---main---------------------------------------------
int main(){
    //main
    int N;
    cin >> N;
    vector<int> X(N),Y(N),U(N);
    char c;
    rep(i,N){
        cin >> X[i] >> Y[i] >> c;
        if(c=='U') U[i] = 0;
        if(c=='R') U[i] = 1;
        if(c=='D') U[i] = 2;
        if(c=='L') U[i] = 3;
    }

    int ans = iINF;
    rep(d,4){
        //同じx座標でU,Dのものをまとめる
        map<int,vector<P>> UD;
        rep(i,N) if(U[i]==0 || U[i]==2) UD[X[i]].emplace_back(Y[i],U[i]);
        chmin(ans, min_colltime(UD)*5);

        //同じ直線上でU,Rのものをまとめる
        map<int,vector<P>> UR;
        rep(i,N) if(U[i]==0 || U[i]==1) UR[X[i]+Y[i]].emplace_back(Y[i],U[i]);
        chmin(ans, min_colltime(UR)*10);

        //rotate
        int xi;
        rep(i,N){
            xi = X[i];
            X[i] = Y[i];
            Y[i] = -xi;
            U[i] = (U[i]+1)%4;
        }
    }

    if(ans == iINF) cout << "SAFE" << endl;
    else cout << ans << endl;

    return 0;
}


#include<iostream>
#include<cctype>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cctype>
#include<cstring>
#include<utility>
const int inf=0x7fffffff;
typedef long long int ll;
using namespace std;
const int N=100+5;
char s[100+10];
char ans[][10]={
	"No\n",
	"Yes\n"
};
struct DICE{
	int u,s,e,w,n,d;
	DICE (int u=0,int s=0,int e=0,int w=0,int n=0,int d=0):u(u),s(s),e(e),w(w),n(n),d(d){}
	DICE fW(){
		DICE tmp;
		tmp.s=this->s;
		tmp.n=this->n;
		tmp.e=this->d;
		tmp.w=this->u;
		tmp.u=this->e;
		tmp.d=this->w;
		return tmp;
	}
	DICE fS(){
		DICE tmp;
		tmp.s=this->u;
		tmp.n=this->d;
		tmp.e=this->e;
		tmp.w=this->w;
		tmp.d=this->s;
		tmp.u=this->n;
		return tmp;
	}
	DICE fE(){
		DICE tmp;
		tmp.s=this->s;
		tmp.n=this->n;
		tmp.e=this->u;
		tmp.w=this->d;
		tmp.d=this->e;
		tmp.u=this->w;
		return tmp;
	}
	DICE fN(){
		DICE tmp;
		tmp.s=this->d;
		tmp.n=this->u;
		tmp.e=this->e;
		tmp.w=this->w;
		tmp.d=this->n;
		tmp.u=this->s;
		return tmp;
	}
	int right(int t,int f){
		if(t==this->u)
		{
			if(f==this->s)return this->e;
			else if(f==this->e)return this->n;
			else if(f==this->n)return this->w;
			else if(f==this->w)return this->s;
			else return -1;
		}
		else if(t==this->s){
			DICE tmp=(*this).fN();
			return tmp.right(t,f);
		}
		else if(t==this->e){
			DICE tmp=(*this).fW();
			return tmp.right(t,f);
		}
		else if(t==this->n){
			DICE tmp=(*this).fS();
			return tmp.right(t,f);
		}
		else if(t==this->w){
			DICE tmp=(*this).fE();
			return tmp.right(t,f);
		}
		else if(t==this->d){
			DICE tmp=(*this).fE().fE();
			return tmp.right(t,f);
		}
		else return -1;
		}
};
DICE A[N];
bool check(DICE x,DICE y){
	int ax[]={
		x.s,x.e,x.n,x.w
	};
	int ay[]={
		y.s,y.e,y.n,y.w
	};
	if(x.u==y.u){
		if(x.d==y.d){
			int cnt=0;
			while(cnt<=4){
				while(ax[cnt]!=ay[0]&&cnt<=4)
					cnt++;
				int i=0;
				while(ax[(i+cnt)%4]==ay[i]&&i<=4)
					i++;
				if(i==4)return 1;
				cnt++;
			}

		}
	}
	return 0;
}
bool sovle(DICE x,DICE y){
	if(check(x,y))return 1;
	if(check(x.fE(),y))return 1;
	if(check(x.fW(),y))return 1;
	if(check(x.fN(),y))return 1;
	if(check(x.fS(),y))return 1;
	if(check(x.fS().fS(),y))return 1;
	return 0;
}
int main()
{
//#ifdef DEBUG
//	freopen("in", "r", stdin);
////	freopen("out", "w", stdout);
//#endif
	int n;scanf("%d", &n);
	int t=0;
	int a[6];
	bool flag=1;
	for(int i=0;i<6;i++){
		scanf("%d", &a[i]);
	}
	A[0]=DICE(a[0],a[1],a[2],a[3],a[4],a[5]);
	t++;
	while(t<n){
	for(int i=0;i<6;i++){
		scanf("%d", &a[i]);
	}
	A[t]=DICE (a[0],a[1],a[2],a[3],a[4],a[5]);
	for(int i=0;i<t;i++)
	{
		if(sovle(A[i],A[t])){flag=0;
//			cout<<i<<' '<<t<<endl;
		}
	}
	t++;
	}
	printf("%s", ans[flag]);
//	printf("%s", ans[solve(dice,dice2)]);
//	DICE I
//	scanf("%s", s);
//	int len=strlen(s);
//	for(int i=0;i<len;i++){
//		switch(s[i]){
//			case'S':dice=dice.fS();break;
//			case'E':dice=dice.fE();break;
//			case'W':dice=dice.fW();break;
//			case'N':dice=dice.fN();break;
//		}
//	}
//	printf("%d\n", dice.u);
//
	return 0;
}
//aoj1_11_a.cc

//AC


#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define FOE(x, a) for(auto x : a)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)

using namespace std;

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

const int N = 2e5 + 10;
const ll INF = 1e18;

struct segmentTree{
    ll d[N * 4], lazy[N * 4];
    void doLazy(int id, int l, int r){
        d[id] += lazy[id];
        if (l < r){
            lazy[id << 1] += lazy[id];
            lazy[id << 1 | 1] += lazy[id];
        }
        lazy[id] = 0;
    }
    void update(int id, int l, int r, int u, int v, ll val){
        doLazy(id, l, r);
        if (v < l || r < u)
            return;
        if (u <= l && r <= v){
            lazy[id] += val;
            doLazy(id, l, r);
            return;
        }
        int mid = (l + r) >> 1;
        update(id << 1, l, mid, u, v, val);
        update(id << 1 | 1, mid + 1, r, u, v, val);
        d[id] = min(d[id << 1], d[id << 1 | 1]);
    }
    ll getMin(int id, int l, int r, int u, int v){
        doLazy(id, l, r);
        if (v < l || r < u)
            return INF;
        if (u <= l && r <= v)
            return d[id];
        int mid = (l + r) >> 1;
        return min(getMin(id << 1, l, mid, u, v), getMin(id << 1 | 1, mid + 1, r, u, v));
    }
} ST[2];

int n, q, a, b;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> q >> a >> b;

    FOR(i, 1, n){
        ST[0].update(1, 1, n, i, i, abs(b - i) - i);
        ST[1].update(1, 1, n, i, i, abs(b - i) + i);
    }

    while (q--){
        int x;
        cin >> x;

        ll cur = ST[0].getMin(1, 1, n, a, a) + a;
        ll minVal = min(ST[0].getMin(1, 1, n, 1, x) + x, ST[1].getMin(1, 1, n, x, n) - x);
        minVal = min(minVal, cur + abs(x - a));

        ST[0].update(1, 1, n, a, a, minVal - cur);
        ST[1].update(1, 1, n, a, a, minVal - cur);

        if (a > 1){
            ST[0].update(1, 1, n, 1, a - 1, abs(x - a));
            ST[1].update(1, 1, n, 1, a - 1, abs(x - a));
        }

        if (a < n){
            ST[0].update(1, 1, n, a + 1, n, abs(x - a));
            ST[1].update(1, 1, n, a + 1, n, abs(x - a));
        }
        a = x;
    }

    ll ans = INF;
    FOR(i, 1, n)
        ans = min(ans, ST[0].getMin(1, 1, n, i, i) + i);

    cout << ans;

    return 0;
}

//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

int main() {
	struct Test {
		ll b;
		ll l;
		ll u;
	};
	ll N, X;
	vector<Test> tests;
	cin >> N >> X;
	for (int i = 0; i < N; i++) {
		int bi, li, ui;
		cin >> bi >> li >> ui;
		tests.push_back({bi, li, ui});
	}
	auto full_score = [X](Test test) {
		return (X - test.b) * test.u - (0 - test.b) * test.l;
	};
	sort(tests.begin(), tests.end(), [&full_score](Test lhs, Test rhs) {
		return full_score(lhs) > full_score(rhs);
	});
	vector<ll> full_score_sum;
	{
		ll sum = 0;
        full_score_sum.push_back(sum);
		for (Test test : tests) {
			sum += full_score(test);
			full_score_sum.push_back(sum);
		}
	}
	ll zero_score = 0;
	for (Test test : tests) {
		zero_score += (0 - test.b) * test.l;
	}
	ll lower = -1;
	ll upper = N * X;
	while (lower + 1 < upper) {
		ll time = (lower + upper) / 2;
		ll full_cnt = time / X;
		ll residue = time % X;
		ll score_max = numeric_limits<ll>::min();
		if (residue == 0) {
			ll score = 0;
            score = full_score_sum[full_cnt];
			score_max = max(score_max, score);
		} else {
			for (int i = 0; i < tests.size(); i++) {
				ll score = 0;
				if (residue < tests[i].b) {
					score = (residue - tests[i].b) * tests[i].l
						- (0 - tests[i].b) * tests[i].l;
				} else {
					score = (residue - tests[i].b) * tests[i].u
						- (0 - tests[i].b) * tests[i].l;
				}
                if (i >= full_cnt) {
                    score += full_score_sum[full_cnt];
                } else {
                    score += full_score_sum[i];
                    score += full_score_sum[full_cnt + 1] - full_score_sum[i + 1];
                }
				score_max = max(score_max, score);
			}
		}
        if (score_max + zero_score >= 0) {
            upper = time;
        } else {
            lower = time;
        }
	}
	cout << upper << endl;
}


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

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;
typedef priority_queue< int, vector< int >, greater< int > >  minHeap;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define bug(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\n"

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

inline int addmod(lli x,lli y)
{
    return (x%mod + y%mod)%mod;
}
inline int submod(lli x,lli y)
{
    return (x%mod - y%mod + mod)%mod;
}
inline int mulmod(lli x,lli y)
{
    return (x%mod * y%mod)%mod;
}

inline int nextSubMask(int i, int mask)
{
    return (i-1)&mask;   /// returns next  submask
}

template<typename T>
void we_r_done(T mssg)
{
    cout<<mssg;
    exit(0);
}

int main()
{
    ///freopen("output.txt","w",stdout);
  // freopen("input.txt","r",stdin);
    FastIO;

    string s;
    while(cin>>s){
        int n = s.size();
        lli ans = 1 + n*1LL*(n-1)/2;
        vector<int>cnt(30,0);
        for(int i=0; i<n; i++){
            cnt[s[i]-'a']++;
        }
        for(int i=0; i<26; i++) ans -= cnt[i]*1LL*(cnt[i]-1)/2;

        cout<<ans<<endl;
    }

    return 0;
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)
///using bs = bitset<MX>; // how many distinct number can be form?
///sort(atoz(v), [](const data x, const data y){return (x.a==y.a?x.b>y.b : x.a<y.a);});

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

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


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

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

  if (N == 1)
    cout << "Hello World" << endl;
  else {
    int A, B;
    cin >> A >> B;
    cout << A + B << endl;
  }
  return 0;
}

#include <iostream>
#include <string>

using namespace std;

int const NMAX = 100;
char v[1 + NMAX][1 + NMAX];

int main() {

  int n, m;
  cin >> n >> m;
  cout << max(n * m, max(n - m, n + m));
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
int main() {
    ll N, m = LONG_LONG_MAX;
    cin >> N;
    rep(i, 5) {
        ll x;
        cin >> x;
        m = min(m, x);
    }
    ll ans = 4 + (ll)ceil(N * 1.0 / m);
    cout << ans << endl;
}

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

int main() {
  ll n,a,b,c,d,e;
  cin >> n >> a >> b >> c >> d >> e;
  ll mn= min(min(min(a,b),c),min(d,e));
  ll ans = (n-0.5)/mn +5;
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n; cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> b(n);
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    vector<int> c(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        auto low = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
        auto up = c.end() - upper_bound(c.begin(), c.end(), b[i]);
        ans += low * up;
    }
    cout << ans;
    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++)

int main() {
    int n; cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    double average = (double)accumulate(a.begin(), a.end(), 0)/n;
    int ans = 0;
    double minv=100100100;
    rep(i, n) {
        if (abs(average-(double)a[i])< minv) {
            ans = i;
            minv = abs(average-(double)a[i]);
        }
    }
    cout << ans << endl;
    //cout << a[ans] << endl;
}


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

//1 <= N <= 10^10なので
//1回のfor文すら回せない
//N = A * B なので√Nまで探索すればok
int findDigit(ll x, ll y)
{
    ll s, t;
    s = log10(x) + 1;
    t = log10(y) + 1;
    return max(s, t);
}

int main()
{
    cin >> N;
    ll s = sqrt(N);

    ll res = N;
    for (int i = 1; i <= s; i++)
    {
        if (N % i == 0)
        {
            int F = findDigit(i, N / i);
            if (F < res)
                res = F;
        }
    }
    cout << res << endl;
}

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

int main(){
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int N = S.length();
  const long long INF = 1e18;
  vector<long long> dp(2,INF);
  dp[0] = 0;
  for(auto c : S){
    vector<long long> dp_(2,INF);
    for(int i = 0; i < 2; ++i){
      int d = c-'0'+i;
      dp_[1] = min(dp_[1],dp[i]+10-d);
      dp_[d/10] = min(dp_[d/10],dp[i]+d%10);
    }
    swap(dp,dp_);
  }
  ++dp[1];
  cout << min(dp[0],dp[1]) << endl;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000000LL;
#define all(x) (x).begin(),(x).end()

vector<pll> prime_factorize(ll N) {
    vector<pll> res;
    for (ll a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        ll ex = 0;
        while (N % a == 0) {
            ++ex;
            N /= a;
        }
        res.push_back({a, ex});
    }
    if (N != 1) res.push_back({N, 1});
    return res;
}
const int MAX = 210000;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
        finv[i] = finv[i-1] * inv[i] % MOD;
    }
}
long long COM(int n, int k){
    if(n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

int main() {
  ll n, m;
  COMinit();
  cin >> n >> m;
  vector<pll> vc = prime_factorize(m);
  ll ans = 1;
  for(int i=0; i<vc.size(); i++){
    ll num = COM(vc[i].second + n - 1, n-1);
    //cout << "num:" << num << " vc[i]:" << vc[i].first << " " << vc[i].second << ln;
    ans = (ans * num)% MOD;
  }
  cout << ans % MOD << ln;
}

#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 305;
const long long mod = 998244353ll;

int n, K, num[N], pre[N], cnt = 0;
char str[N];
long long f[N][N << 1], g[N][N << 1], tmp[N][N << 1];

int main () {
	scanf("%s%d", str, &K), n = strlen(str);
	int pos = -1;
	for (int i = 0; str[i]; i++) {
		if (str[i] == '0') {
			num[++cnt] = i - pos - 1;
			pos = i;
		}
	}
	num[++cnt] = n - 1 - pos;
	pre[0] = 0;
	for (int i = 1; i <= cnt; i++) pre[i] = pre[i - 1] + num[i];

	for (int i = 0; i <= pre[cnt]; i++) {
		for (int j = 0; j <= (pre[cnt] << 1); j++) f[i][j] = 0ll;
	}
	f[0][0] = 1ll;
	for (int i = 1; i <= cnt; i++) {
		for (int j = 0; j <= (pre[cnt] << 1); j++) {
			tmp[0][j] = f[0][j];
			for (int k = 1; k <= pre[cnt]; k++) tmp[k][j] = (tmp[k - 1][j] + f[k][j]) % mod;
		}
		for (int j = 0; j <= pre[cnt]; j++) {
			for (int k = 0; k <= (pre[cnt] << 1); k++) {
				g[j][k] = 0ll;
				if (j >= pre[i]) {
					g[j][k] = j > num[i] ? tmp[j - num[i] - 1][k] : 0ll;
					for (int l = 0; l <= min(num[i], j); l++) {
						if (k + l >= num[i]) g[j][k] = (g[j][k] + f[j - l][k + l - num[i]]) % mod;
					}
				}
			}
		}
		for (int j = 0; j <= pre[cnt]; j++) {
			for (int k = 0; k <= (pre[cnt] << 1); k++) f[j][k] = g[j][k];
		}
	}

	long long ans = 0ll;
	for (int i = 0; i <= min(pre[cnt] << 1, K); i++) ans = (ans + f[pre[cnt]][i]) % mod;
	printf("%lld\n", ans);
	return 0;
}


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

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

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

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

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

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

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VLL a(n);
  rep(i, n) cin >> a[i];
  int mcnt = 0;
  VLL absA;
  ll ans = 0;
  rep(i, n) {
    absA.push_back(abs(a[i]));
    ans += abs(a[i]);
    if(a[i] < 0)
      mcnt++;
  }
  sort(all(absA));
  // rep(i, n) cout << absA[i] << " ";
  // cout << endl;
  // cout << ans << " " << absA[0] << endl;
  if(mcnt % 2) {
    cout << ans - 2LL * absA[0] << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}


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

int H, W;
vector<string> fi;

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

int main() {
    cin >> H >> W;
    fi.resize(H);
    for (int i = 0; i < H; ++i) cin >> fi[i];

    // 多点をスタートとして扱う
    vector<vector<int> > dist(H, vector<int>(W, -1));
    using pint = pair<int,int>;
    queue<pint> que;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            if (fi[i][j] == '#') {
                dist[i][j] = 0;
                que.push(pint(i, j));
            }
        }
    }

    // BFS
    while (!que.empty()) {
        auto cur = que.front();
        que.pop();
        for (int dir = 0; dir < 4; ++dir) {
            int nx = cur.first + dx[dir];
            int ny = cur.second + dy[dir];
            if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if (dist[nx][ny] == -1) {
                dist[nx][ny] = dist[cur.first][cur.second] + 1;
                que.push(pint(nx, ny));
            }
        }
    }
    int res = 0;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            res = max(res, dist[i][j]);
        }
    }
    cout << res << endl;
}

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

int H, W, x, y, ans = 0, flag = 0;
queue<int> Qx, Qx_next; queue<int> Qy, Qy_next;

bool can_go(int x, int y){
    return (1 <= x && x <= H && 1 <= y && y<= W);
}

void f(vector<vector<int>>& A, int x, int y){
    if(can_go(x,y)){
        if(A[x][y] == 1){
            flag = 1;
            A[x][y] = -1;
            Qx_next.push(x);
            Qy_next.push(y);
        }
    }
}

void visit(vector<vector<int>>& A, int x, int y){
    f(A, x-1, y);
    f(A, x+1, y);
    f(A, x, y-1);
    f(A, x, y+1);
}

int main(){
    cin >> H >> W;
    vector<vector<int>> A(H+1, vector<int>(W+1, 1));
    char buf;
    for(int i = 1; i <= H; ++i){
        for(int j = 1; j <= W; ++j){
            cin >> buf;
            if(buf == '#'){
                A[i][j] = -1; //黒色ならfalse, 白色ならtrue
                Qx.push(i);
                Qy.push(j);
            }
        }
    }
    while(!Qx.empty()){
        x = Qx.front();
        y = Qy.front();
        Qx.pop();
        Qy.pop();
        visit(A, x, y);
        if(Qx.empty()){
            while(!Qx_next.empty()){
                Qx.push(Qx_next.front()); Qx_next.pop();
                Qy.push(Qy_next.front()); Qy_next.pop();
            }
            if(flag == 1)++ans;
            flag = 0;
        }
    }
    cout << ans << endl;
    return 0;
}


/*
 * src.cpp
 *
 *  Created on: 2016/08/04
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

const int MAX_N=1e4;

int n;
int p[3];

char out[MAX_N+5];

int main(){
	do{
		cin>>n;
		if (n){
			REP(i,n){
				int ave=0;
				bool f=false;
				REP(j,3){
					cin>>p[j];
					ave+=p[j];
					if (p[j]==100){
						f=true;
					}
				}
				if (f){
					out[i]='A';
				}else if (p[0]+p[1]>=180){
					out[i]='A';
				}else if (ave>=80*3){
					out[i]='A';
				}else if (ave>=70*3){
					out[i]='B';
				}else if (ave>=50*3 && (p[0]>=80 || p[1]>=80)){
					out[i]='B';
				}else{
					out[i]='C';
				}
			}
			REP(i,n){
				cout<<out[i]<<endl;
			}
		}
	}while (n);
	return 0;
}

#include <stdio.h>

void grade(int students)
{
    while(students != 0) {
        for(int i = 0; i < students; ++i) {
            int math, english, japanese;
            scanf("%d %d %d", &math, &english, &japanese);
            int mean = (math + english + japanese) / 3;
            if ((math == 100) || (english == 100) || (japanese == 100)) {
                printf("A\n");
            } else {
                if (((math + english) / 2 >= 90) || mean >= 80) {
                    printf("A\n");
                } else if ((mean >= 70) || ((mean >= 50) && ((math >= 80) || (english >= 80)))) {
                    printf("B\n");
                } else {
                    printf("C\n");
                }
            }
        }
        scanf("%d", &students);
    }
}

int main(void)
{
    int students;
    scanf("%d", &students);
    grade(students);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
vector<ll> z_algorithmll(const vector<ll> &s)
{
  vector<ll> prefix(s.size());
  for (int i = 1, j = 0; i < s.size(); i++)
  {
    if (i + prefix[i - j] < j + prefix[j])
    {
      prefix[i] = prefix[i - j];
    }
    else
    {
      int k = max(0LL, j + prefix[j] - i);
      while (i + k < s.size() && s[k] == s[i + k])
        ++k;
      prefix[i] = k;
      j = i;
    }
  }
  prefix[0] = (int)s.size();
  return prefix;
}
int main()
{
ll n ;
cin >> n ;
vector<ll> a(n);
rep(i, n){
  cin >> a[i];
}
vector<ll> b(n);
rep(i, n){
  cin >> b[i];
}
vector<vector<ll>> v,vv;
rep(j,30){
  vector<ll> a1;
  vector<ll> a2;
  vector<ll> bb;
  rep(i,n){
    if(a[i]>>j & 1LL){a1.push_back(1);a2.push_back(0);}
    else {a1.push_back(0);a2.push_back(1);}
    if(b[i]>>j & 1LL)bb.push_back(1);
    else bb.push_back(0);
  }
  auto r=bb; r.push_back(-1);
  r.insert(r.end(), a1.begin(), a1.end());
  r.insert(r.end(), a1.begin(), a1.end());
  v.push_back(z_algorithmll(r));
  r=bb; r.push_back(-1);
  r.insert(r.end(), a2.begin(), a2.end());
  r.insert(r.end(), a2.begin(), a2.end());
  vv.push_back(z_algorithmll(r));
}
rep(i,n){
  bool ok=1;
  ll ans=0;
  rep(j,30){
    if(v[j][i+n+1]!=n&&vv[j][i+n+1]!=n){
      ok=0;
      break;
    }
    if(vv[j][i+n+1]==n)ans+=1LL<<j;
  }
  if(ok)cout << i << ' ' << ans <<endl;
}
return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define vi vector<int>
template <class t, class u>
bool chmax(t &a, u b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class t, class u>
bool chmin(t &a, u b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}

//ll mod = 1000000007;
ll mod = 998244353;

signed main()
{
    int n;
    cin >> n;
    vector<ll> a(n);
    bool is_minus_even = true;
    bool is_zero_exist = false;
    rep(i, n)
    {
        cin >> a[i];
        if (a[i] < 0)
        {
            is_minus_even = !is_minus_even;
        }
        if (a[i] == 0)
        {
            is_zero_exist = true;
        }
    }
    ll ans = 0;
    if (is_minus_even == true || (!is_minus_even && is_zero_exist))
    {
        rep(i, n)
        {
            ans += abs(a[i]);
        }
    }
    else
    {
        rep(i, n)
        {
            a[i] = abs(a[i]);
        }
        sort(all(a));
        ans = accumulate(a.begin() + 1, a.end(), 0LL) - a[0];
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) printf("%lld ",i);puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 1<<20|3, mod = 1e9+7;
int n,maxn,a[N],pre[N],s[N],s2[N],f[N];
poly v[N];

inline int Mod(int x){
	return x>=mod?x-mod:x;
}

int main(){
	n=read(),pre[0]=1;
	For(i,1,n){
		a[i]=read(),a[i]=a[i-1]^a[i],v[a[i]].pb(i);
		pre[i]=pre[i-1]+(a[i]==0);
	}
	int ans=0;
	if (a[n]==0){
		int x=1;
		For(i,1,pre[n]-2) x=x*2%mod;
		ans=Mod(ans+x);
	}
	FOR(i,1,1<<20) if (!v[i].empty()){
		f[0]=1,s[0]=1,s2[0]=pre[v[i][0]];
		FOR(j,1,siz(v[i])){
			f[j]=(1ll*s[j-1]*pre[v[i][j]]+mod-s2[j-1]+1)%mod;
			s[j]=Mod(s[j-1]+f[j]),s2[j]=(s2[j-1]+1ll*f[j]*pre[v[i][j]])%mod;
		}
		if (a[n]==0) ans=Mod(ans+s[siz(v[i])-1]);
		else if (a[n]==i) ans=Mod(ans+f[siz(v[i])-1]);
	}
	printf("%d\n",ans);
}

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <float.h>
#include <random>

#define repeat(i,n) for (int i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
#define debugArrayP(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge].first<< " " << x[hoge].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = LLONG_MAX/10;
const ll MOD = 1e9+7;

ll mod_pow(ll x,int n){
  return n? n&1? x*mod_pow(x,n-1)%MOD:mod_pow(x*x%MOD,n/2):1;
}

int N;
ll dp[(1<<20)+10],dpsum[(1<<20)+10],dpsum2[(1<<20)+10];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>N;
  vint A(N);
  repeat(i,N)cin>>A[i];
  int c=0;
  int xorsum=0;
  repeat(i,N){
    c++;
    int j=i+1;
    xorsum=A[i];
    while(j<N&&xorsum!=0){
      dp[xorsum] = (1+dpsum[xorsum]*c-dpsum2[xorsum]+MOD)%MOD;
      dpsum[xorsum] = (dpsum[xorsum]+dp[xorsum])%MOD;
      dpsum2[xorsum] = (dpsum2[xorsum]+dp[xorsum]*c)%MOD;
      xorsum ^= A[j];
      j++;
    }
    i=j-1;
  }
  ll ans = 0;
  if(xorsum){
    ans = (1+dpsum[xorsum]*c-dpsum2[xorsum]+MOD)%MOD;
  }else{
    ans = mod_pow(2,c-1);
    repeat(i,1<<20){
      ans = (ans+dpsum[i])%MOD;
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <bits/stdc++.h>

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

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

#include <bits/stdc++.h>  // ver1.8
#define int long long
#define ALL(v) (v).begin(),(v).end()
#define Vi vector<int>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define INF 9000000000000000000  //  9.0 * 10^18
#define MOD 1000000007  //  10^9 + 7
#define PAI 3.14159265358979
using namespace std;

int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm

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


# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
typedef long long ll;
int arr[100005];
int n,k,x;
int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);

}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    bool u=false;
    for(int i=1;i<=n;i++){
        cin>>arr[i];
        if(i==1) x=arr[1];
        else x=gcd(x,arr[i]);
        if(arr[i]==k){
            cout<<"POSSIBLE"<<endl;
            return 0;
        }
        if(arr[i]>k) u=true;
    }
    if(!u){
        cout<<"IMPOSSIBLE"<<endl;
        return 0;
    }
    if(k%x==0){
        cout<<"POSSIBLE"<<endl;
    }
    else cout<<"IMPOSSIBLE"<<endl;

}

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
using namespace std;
int main(){
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int a[10];
  int k,b;
  for(int i=0;i<5;i++){
    cin>>a[i];
  }
  cin>>k;
  sort(a,a+5);
  b=a[4]-a[0];
  if(k<b){
    cout<<":("<<"\n";
  }
  else{
    cout<<"Yay!"<<"\n";
  }
  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

int n;
int field[21][21];

void hantei(void){
	/*
	rep(i, 21){
		rep(j, 21){
			printf("%d", field[i][j]);
		}
		printf("\n");
	}
	*/
	int sum = 0;
	rep(i, 21) rep(j, 21) sum += field[i][j];
	//printf("summm  %d\n", sum);
	if (sum == 0) printf("Yes\n");
	else printf("No\n");
	return;
}

void slove(int n){
	int x = 10, y = 10; //?????¨???????????????????????°
	rep(i, 21) rep(j, 21) field[i][j] = 0;
	rep(i, n){
		int pointx, pointy;
		cin >> pointx >> pointy;
		field[pointy][pointx] = 1;//????????????????????¨??????????????????
	}

	int meireikaisu;
	cin >> meireikaisu;
	rep(i, meireikaisu){
		string meirei;
		int kyori;
		cin >> meirei >> kyori;
		if (meirei[0] == 'N'){
			reps(j, 1, kyori + 1){
				if (field[y + j][x] == 1) field[y + j][x]--;//??¨??£????????????
			}
			y += kyori;
		}else if(meirei[0] == 'E'){
			reps(j, 1, kyori + 1){
				if (field[y][x + j] == 1) field[y][x + j]--;
			}
			x += kyori;
		}else if(meirei[0] == 'S'){
			reps(j, 1, kyori + 1){
				if (field[y - j][x] == 1) field[y - j][x]--;
			}
			y -= kyori;
		}else if(meirei[0] == 'W'){
			reps(j, 1, kyori + 1){
				if (field[y][x - j] == 1) field[y][x - j]--;
			}
			x -= kyori;
		}
	}
	hantei();
}

int main(void){
	rep(i, 1000){
		cin >> n;
		if (n == 0) break;
		slove(n);
	}
	return 0;
}

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

int main(){
    int N,x,y,M,l;
    char d;
    map<char,int> D;
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    D['E']=0; D['W']=1; D['N']=2; D['S']=3;
    while(cin>>N,N){
        bool a[21][21]={};
        rep(i,N){
            cin>>x>>y;
            a[x][y]=true;
        }
        x=y=10;
        cin>>M;
        rep(i,M){
            cin>>d>>l;
            while(l--){
                if(a[x+=dx[D[d]]][y+=dy[D[d]]]){
                    a[x][y]=false;
                    N--;
                }
            }
        }
        cout<<(N?"No":"Yes")<<endl;
    }
    return 0;
}


/**
*    author:  yuya1234
*    created: 25.06.2020 13:29:29
**/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define SORT(s) sort((s).begin(),(s).end())
#define SORTD(s) sort((s).rbegin(),(s).rend())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

    ll n,x,tmp;
    vector<ll> v;

    cin>>n>>x;

    REP(i,n)
    {
        cin>>tmp;
        v.push_back(tmp);
    }

    SORT(v);

    ll cnt=0;

    REP(i,n)
    {
        if((x>=v[i] && i!=n-1) || x==v[i])
        {
            x-=v[i];
            cnt++;
        }
    }

    cout<<cnt<<endl;

    return 0;
}

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

int main(){
    long int n,x;
    cin >> n >> x;
    vector<long int> a(n);
    long int sum=0;
    int i;

    for(i=0; i<n; i++){
        cin >> a.at(i);
        sum+=a.at(i);
    }
    sort(a.begin(),a.end());

    if(x>sum) cout << n-1 << endl;
    else if(x==sum) cout << n << endl;
    else{
        for(i=0; i<n; i++){
            sum-=a.at(n-1-i);
            if(x>=sum) break;
        }
        cout << n-1-i << endl;
    }
}


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

using namespace std;

int main() {
    long sum;
    cin >> sum;
    cout << int(pow(sum, 3)) << endl;
}

#include <iostream>
using namespace std;
int main(void){
 int x,y;
 x=y=0;
 cin>>x;
 y=x*x*x;
 cout<<y<<endl;
 return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define flash ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
ll dp[2][2][100];
ll mod=1e9+7;ll d;
ll mo(ll x)
{
    if(x>=0)
    return(x%d);
    else
    return(x%d+d)%d;
}
int main()
{
    flash
    string s;
    cin>>s;ll n=s.size();ll a[n];
    for(ll i=0;i<n;i++)
    {
        a[i]=s[i]-'0';
    }
cin>>d;
  dp[1][0][0] = 1;dp[1][1][0] = 1;

    for(ll i = n-1; i >=0 ; i--) {
        for(ll state = 0; state < 2 ; state++) {
            for(ll sum = 0; sum < d; sum++) {
                if(state) {
                    for(ll x = 0; x <= a[i] ; x++) {
                        dp[0][1][sum] += (x== a[i]) ? dp[1][1][mo(sum-x)]%mod:dp[1][0][mo(sum-x)]%mod;
                    }
                }
                else {
                    for(ll x = 0; x < 10 ; x++) {
                        dp[0][0][sum] += dp[1][0][mo(sum-x)]%mod;
                    }
                }
            }
          }
          if(i!=0)
          {
         for(ll sum=0;sum<d;sum++)
            {
                dp[1][1][sum]=dp[0][1][sum];
                dp[1][0][sum]=dp[0][0][sum];
                dp[0][1][sum]=0;
                dp[0][0][sum]=0;
            }
          }
    }
    dp[0][1][0]--;
    dp[0][1][0]=dp[0][1][0]%mod;
    cout<<dp[0][1][0]<<endl;
}

#include<iostream>
#include<string>
using namespace std;
int alignment(string s,string t){

	int a[1001][1001];
	int n=s.size();
	int m=t.size();
	a[0][0]=0;
	for(int i=0;i<n;i++){
		a[i+1][0]=a[i][0]+1;
	}
	for(int i=0;i<m;i++){
		a[0][i+1]=a[0][i]+1;
	}

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(s[i]==t[j]){
				a[i+1][j+1]=min(a[i][j],min(a[i+1][j]+1,a[i][j+1]+1));
			}else{
					a[i+1][j+1]=min(a[i][j]+1,min(a[i+1][j]+1,a[i][j+1]+1));
			}
		}
	}
	return a[n][m];
}

int main(void){

	string s;
	string t;
	cin >> s >> t;
	cout << alignment(s,t) <<endl;

}


#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

	int N;
	cin >> N;

	ll dp[2][3];
	dp[0][0] = 0;
	dp[0][1] = -1e18;
	dp[0][2] = -1e18;
	dp[1][0] = -1e18;
	dp[1][1] = -1e18;
	dp[1][2] = -1e18;

	int n = N / 2;
	if (N % 2) {
		rep(i, n) {
			int a, b;
			cin >> a >> b;
			chmax(dp[1][1], dp[0][0]);
			dp[0][0] += a;
			dp[0][2] += a;
			chmax(dp[0][2], dp[1][1]);
			dp[1][1] += b;
		}
		int a;
		cin >> a;
		chmax(dp[1][1], dp[0][0]);
		dp[0][2] += a;
		printf("%lld\n", max(dp[0][2], dp[1][1]));
	}
	else {
		rep(i, n) {
			int a, b;
			cin >> a >> b;
			chmax(dp[1][1], dp[0][0]);
			dp[0][0] += a;
			dp[1][1] += b;
		}
		printf("%lld\n", max(dp[1][1], dp[0][0]));
	}

	Would you please return 0;
}


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

int main()
{

	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int cnt=0;
	string s;
	cin>>s;

	int m =0;

	for(int i =0 ;i<3;i++)
	{
		if(s[i]=='R')m++;
		else
		{
			cnt = max(m,cnt);
			m =0;
		}
	}
	cnt = max(m,cnt);

	cout<<cnt;
    return 0;

}

#include<bits/stdc++.h>
#define io ios_base::sync_with_stdio(false); cin.tie(NULL);

#define N 4e7
#define nl "\n"
#define f first
#define s second
#define pi acos(-1.0)
#define MOD 1000000007
#define pb push_back
#define vector<int> VI;
#define vector<long long> VII;

typedef long long ll;
typedef double db;

using namespace std;

int main()
{
    io;
    //freopen("in.txt" , "r" , stdin);
    //freopen("out.txt" , "w" , stdout);

    ///Let the showdown begin

    string str;
    int cnt;

    cin >> str;

    if(str[0] =='R' && str[1] == 'R' && str[2] == 'R')
        cnt = 3;
    else if((str[0] =='R' && str[1] == 'R' && str[2] == 'S') || (str[0] =='S' && str[1] == 'R' && str[2] == 'R'))
        cnt = 2;
    else if((str[0] =='S' && str[1] == 'S' && str[2] == 'S'))
        cnt = 0;
    else
        cnt = 1;

    cout << cnt << nl;

    ///Happily DONE
    return 0;
}


#include <iostream>
using namespace std;
const int MAX = 500;

int main() {
	int n, m, Q;
	cin >> n >> m >> Q;
	int S[MAX+1][MAX+1] = {};
	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> l >> r;
		S[l][r]++;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			S[i][j] += S[i][j-1];
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			S[i][j] += S[i-1][j];
		}
	}
	auto sum_rect = [&](int x0, int y0, int x1, int y1) {
		return S[x1][y1] - S[x0-1][y1] - S[x1][y0-1] + S[x0-1][y0-1];
	};
	for (int query = 0; query < Q; query++) {
		int p, q;
		cin >> p >> q;
		int ans = sum_rect(p, p, q, q);
		cout << ans << endl;
	}
	return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mem(a,b) memset(a, b, sizeof(a) )
#define all(x) (x).begin(),(x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

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 main(){
    int n,m,q;cin>>n>>m>>q;
    vvi g(n);
    rep(i,m){
        int l,r;
        cin>>l>>r;
        l--;r--;
        g[l].PB(r);
    }
    rep(i,n){
        sort(all(g[i]));
    }
    rep(i,q){
        int P,Q;
        cin>>P>>Q;
        P--;Q--;
        int cnt=0;
        for(int j=P;j<=Q;++j){
            cnt+=upper_bound(all(g[j]),Q)-g[j].begin();
        }
        cout<<cnt<<endl;
    }
}


#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << endl;
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

void chmax(ll& a, ll b) {
    if (a < b) a = b;
}

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

    ll N;
    cin >> N;

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

    vector<vll> dp(N, vll(3, -INF));
    dp[0][0] = 0;
    dp[0][1] = 0;
    dp[0][2] = A[0];
    REP(i, N - 1) {
        auto &nxt = dp[i+1], &cur = dp[i];
        if (N & 1) {
            if (i & 1) {
                chmax(nxt[0], cur[1]);
                chmax(nxt[0], cur[2]);
                chmax(nxt[1], cur[0] + A[i+1]);
                chmax(nxt[2], cur[2] + A[i+1]);
            } else {
                chmax(nxt[0], cur[0]);
                chmax(nxt[0], cur[1]);
                chmax(nxt[1], cur[0] + A[i+1]);
                chmax(nxt[2], cur[2]);
            }
        } else {
            if (i & 1) {
                chmax(nxt[0], cur[0]);
                chmax(nxt[0], cur[2]);
                chmax(nxt[2], cur[0] + A[i+1]);
            } else {
                chmax(nxt[0], cur[2]);
                chmax(nxt[2], cur[0] + A[i+1]);
            }
        }
    }

    if (N&1) print(max(dp[N-1][0], dp[N-1][1]));
    else print(max(dp[N-1][0], dp[N-1][2]));

}

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

struct RangeUpdateQuery{
    int n, now;
    std::vector<int> dat, time;

    void init(int n_){
        n = 1;
        now = 0;
        while(n < n_) n *= 2;
        dat.resize(2 * n - 1, std::numeric_limits<int>::max());
        time.resize(2 * n - 1, 0);
    }

    void update(int a, int b, int k, int l, int r, int x){
        if(r <= a || b <= l) return;
        if(a <= l && r <= b){
            dat[k] = x;
            time[k] = now;
        }
        else{
            update(a, b, k * 2 + 1, l, (l + r) / 2, x);
            update(a, b, k * 2 + 2, (l + r) / 2, r, x);
        }
    }

    void update(int a, int b, int x){
        now++;
        update(a, b, 0, 0, n, x);
    }

    int query(int k){
        k += n - 1;
        int res = dat[k], recent = time[k];
        while(k > 0) {
            k = (k - 1) / 2;
            if(recent < time[k]){
                recent = time[k];
                res = dat[k];
            }
        }
        return res;
    }
};

using namespace std;

int main(){
    int n, q;
    cin >> n >> q;
    RangeUpdateQuery ruq;
    ruq.init(n);
    while(q--){
        int com;
        cin >> com;
        if(com){
            int i;
            cin >> i;
            cout << ruq.query(i) << "\n";
        }else{
            int s, t, x;
            cin >> s >> t >> x;
            ruq.update(s, t + 1, x);
        }
    }
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int stage[12][12] = {{0}};
    int n;
    int j = 0;
    int x = 0,y = 0,xx = 0;
    for(;;){
        cin >> n;
        if(n == 0)break;
        for(int i = 0;i < n; i++){
            for(j = 0;j < n; j++){
                cin >> stage[i][j];
                x += stage[i][j];
                if(j == n-1){
                    stage[i][n] = x;
                    xx += stage[i][n];
                    x = 0;
                }
            }
        }
        for(int i = 0;i < n; i++){
            for(j = 0;j < n; j++){
                y += stage[j][i];
                if(j == n-1){
                    stage[n][i] = y;
                    y = 0;
                }
            }
        }
        stage[n][n] = xx;
        for(int i = 0;i <= n; i++){
            for(int j = 0;j <= n; j++){
                printf("%5d",stage[i][j]);
            }
            printf("\n");
        }
        x = 0,y = 0,xx = 0;
    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#define SIZE 1002
int main(){

  int i,j;
  int na,nb,nc,n;
  int a[SIZE],b[SIZE],c[SIZE],d[SIZE];
  int data[SIZE];

  while(1){
  scanf("%d %d %d",&na,&nb,&nc);
  if(na==0 && nb==0 && nc==0)break;
  scanf("%d",&n);

  for(i=1;i<=na+nb+nc;i++)data[i]=2;
  for(i=0;i<n;i++){
    scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
    if(d[i]==1){
      data[a[i]]=data[b[i]]=data[c[i]]=1;
    }
  }

  for(i=0;i<n;i++){
    if(d[i]==0){
      if(data[a[i]]==1 && data[b[i]]==1)data[c[i]]=0;
      else if(data[a[i]]==1 && data[c[i]]==1)data[b[i]]=0;
      else if(data[b[i]]==1 && data[c[i]]==1)data[a[i]]=0;
    }
  }

  for(i=1;i<=na+nb+nc;i++){
    printf("%d\n",data[i]);
  }
  }

  return 0;
}


// A - Rated for Me
#include <bits/stdc++.h>
using namespace std;

int main(){
	int r; cin>>r;
	string ans = "ARC";
	if(r < 1200) ans = "ABC";
	if(2800 <= r) ans = "AGC";
	cout<< ans <<endl;
}

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
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;}

#define ABC 001
#define ARC 010
#define AGC 100

int main(void){
    int r;
    cin >> r;
    int frag = AGC;
    if(r < 1200){
        frag |= ABC;
        frag |= ARC;
    }else if( r < 2800){
        frag |= ARC;
    }
    if(frag & ABC)cout << "ABC" << endl;
    else if(frag & ARC)cout << "ARC" << endl;
    else cout << "AGC" << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, a, b, c, d, e;
  cin >> n >> a >> b >> c >> d >> e;
  ll ans = 0;
  chmax(ans, (n+a-1)/a+4);
  chmax(ans, (n+b-1)/b+4);
  chmax(ans, (n+c-1)/c+4);
  chmax(ans, (n+d-1)/d+4);
  chmax(ans, (n+e-1)/e+4);
  cout << ans << endk;
  return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
    int m;
    cin >> m;
    ll res = 0, sum = 0;
    for (int i=0; i<m; ++i) {
        ll d, c;
        cin >> d >> c;
        res += c;
        sum += d * c;
    }
    cout << res - 1 + (sum - 1) / 9 << '\n';
}

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

    solve();

    return 0;
}


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

int map[21][21];

void search(int a[][21], string d, int i, int *x, int *y) {

	if (d == "N") {
		for (int j = 1; j <= i; j++) a[*y + j][*x] = 2;
		*y += i;
	} else if (d == "S") {
		for (int j = 1; j <= i; j++) a[*y - j][*x] = 2;
		*y -= i;
	} else if (d == "E") {
		for (int j = 1; j <= i; j++) a[*y][*x + j] = 2;
		*x += i;
	} else if (d == "W") {
		for (int j = 1; j <= i; j++) a[*y][*x - j] = 2;
		*x -= i;
	}

}

int judge (int a[][21]) {
	for (int i = 0; i < 21; i++) {
		for (int j = 0; j < 21; j++) {
			if (a[i][j] == 1) return 0;
		}
	}
	return 1;
}

void print (int a[][21]) {
	for (int i = 20; i >= 0; i--) {
		for (int j = 0; j < 21; j++) {
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}
}

int main(void) {
	int N, x, y, M, i, nowx, nowy;
	string d;

	cin.tie(0);
	//ios::sync_with_stdio(false);

	while (cin >> N, N) {
		nowx = nowy = 10;
		for (int k = 0; k < 21; k++) {
			for (int l = 0; l < 21; l++) {
				map[k][l] = 0;
			}
		}

		//print(map);

		for (int j = 0; j < N; j++) {
			cin >> x >> y;
			map[y][x] = 1;
		}

		map[nowy][nowx] = 2;

		//print(map);

		cin >> M;

		for (int k = 0; k < M; k++) {
			cin >> d >> i;
			search(map, d, i, &nowx, &nowy);
			//print(map);
		}
		cout << (judge(map) == 1 ? "Yes" : "No") << "\n";

	}

	return 0;
}

#include <iostream>
using namespace std;
int main()
{
	int a,b,c; int CASE[1000][3];
	while(1)
	{
		cin >> a >> b >> c ; int total=a+b+c;
		if(a==0 && b==0 && c==0) break;
		int N,i,j,k,r; int pnt=0;
		cin >> N ;
		int* test=new int[total]; for(int n=0;n<total;n++) test[n]=2;
		for(int n=0;n<N;n++)
		{
			cin >> i >> j >> k >> r ;
			if(r==1) test[i-1]=test[j-1]=test[k-1]=1;
			else CASE[pnt][0]=i-1,CASE[pnt][1]=j-1,CASE[pnt++][2]=k-1;
		}
		for(int n=0;n<pnt;n++)
		{
			if(test[CASE[n][0]]==1 && test[CASE[n][1]]==1 && test[CASE[n][2]]==2) test[CASE[n][2]]=0;
			if(test[CASE[n][0]]==1 && test[CASE[n][2]]==1 && test[CASE[n][1]]==2) test[CASE[n][1]]=0;
			if(test[CASE[n][1]]==1 && test[CASE[n][2]]==1 && test[CASE[n][0]]==2) test[CASE[n][0]]=0;
		}
		for(int n=0;n<total;n++)
		cout << test[n] << '\n' ;
	}
}


#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a)/(b)+(((a)%(b))?1:0)
#define ln '\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

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

vector<LL> Div(LL n) {
    vector<LL> ret;
    for(LL i = 1; i * i <= n; ++i) {
        if(n % i == 0) {
            ret.pb(i);
            if(i * i != n) ret.pb(n / i);
        }
    }
    sort(all(ret));
    return ret;
}

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

    int N;
    cin >> N;
    vector<int> v(N);
    for(int i = 0; i < N; ++i) {
        cin >> v[i];
    }
    sort(all(v));

    LL a, b;
    a = 0, b = 0;
    for(int i = 0; i < N - 1; ++i) {
        if(v[i] == v[i + 1]) {
            b = a;
            a = v[i];
            i++;
        }
    }
    cout << a * b << endl;

    return 0;
}

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

int main() {
    int n;
    cin >> n;
    map<ll, int, greater<ll>> m;
    rep(i, n) {
        ll a;
        cin >> a;
        m[a]++;
    }

    int cnt = 0;
    ll ans = 0;
    for (auto e : m) {
        if (e.second >= 4 && cnt == 0) {
            ans = e.first * e.first;
            cnt += 2;
            break;
        } else if (e.second >= 2) {
            if (cnt == 0)
                ans = e.first, cnt++;
            else if (cnt == 1) {
                ans *= e.first, cnt++;
                break;
            }
        }
    }

    if (cnt == 1) ans = 0;
    cout << ans << endl;
    return 0;
}


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

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int h1, m1, h2, m2, k; cin >> h1 >> m1 >> h2 >> m2 >> k;
	int a = h1 * 60 + m1, b = h2 * 60 + m2;
	int ans = b - a;
	if (ans < 0) ans += 24 * 60;
	ans -= k;
	cout << ans << "\n";

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ll h1,m1,h2,m2,k;
	cin>>h1>>m1>>h2>>m2>>k;
	ll t=((h2-h1)*60)-m1+m2;
	ll d=t-k;
	if(d<=0){
		cout<<"0"<<"\n";
	}
	else{
		cout<<d<<"\n";
	}
	return 0;
}


#include <iostream>

using namespace std;

typedef long long ll;

const ll mod = 998244353;

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

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

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

  dp[0][0] = 1;

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

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

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define MOD 998244353
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

inline int fastpow(int bas, int ex){
	register int res = 1;
	for(; ex; ex >>= 1, bas = bas * (ll)bas % MOD) if(ex & 1) res = res * (ll)bas % MOD;
	return res;
}

inline int kazu(int a, const int &b) {return (a += b) >= MOD ? a - MOD : a;}
inline int hiku(int a, const int &b) {return (a -= b) < 0 ? a + MOD : a;}

int N, sum = 0, f[90005], g[90005];

int main(){
	getint(N), f[0] = g[0] = 1;
	for(register int i = 1, a; i <= N; i++){
		getint(a), sum += a;
		for(register int j = sum; j >= 0; j--){
			f[j] = f[j] * 2LL % MOD;
			if(j >= a) f[j] = kazu(f[j], f[j - a]), g[j] = kazu(g[j], g[j - a]);
		}
	}
	int ans = fastpow(3, N);
	for(register int i = sum + 1 >> 1; i <= sum; i++) ans = hiku(ans, f[i] * 3LL % MOD);
	if(!(sum & 1)) ans = kazu(ans, g[sum >> 1] * 3LL % MOD);
	return printf("%d\n", ans), 0;
}


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

double len(int x1,int x2, int y1,int y2){
    double res;
    double x=x1-x2;
    double y=y1-y2;
    res=pow(x*x+y*y,0.5);
    return res;
}

int main(){
    int n;
    cin>>n;
    int x[n],y[n];
    double ans=0;
    for(int i=0;i<n;i++)cin>>x[i]>>y[i];
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            ans+=len(x[i],x[j],y[i],y[j])*2.0/n;
        }
    }
    cout<<fixed<<setprecision(12)<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 1e5 + 5;
int x[8], y[8];

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int N;
    cin >> N;
    double sum = 0;
    int cnt = 0;
    for (int i = 0; i < N; i++) cin >> x[i] >> y[i];
    vector<int> v(N);
    iota(all(v), 0);
    do {
        double cur = 0;
        for (int i = 1; i < N; i++) {
            int dx = x[v[i]]-x[v[i-1]], dy = y[v[i]]-y[v[i-1]];
            cur += sqrt(dx*dx+dy*dy);
        }
        sum += cur;
        cnt++;
    } while (next_permutation(all(v)));
    cout << fixed << setprecision(7) << sum / cnt << '\n';
    return 0;
}


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

int N,M,Q,m;
const int maxn=600000+5;
int siz;
int C[maxn],a[maxn],ret[maxn];
struct node{
	int l,r,id;
	bool operator<(const node& other)const{
		return other.r > r;
	}
}x[maxn],y[maxn];

void add(int x,int v){
	for(int i=x;i<=m;i+=i&-i) C[i]+=v;
}

int sum(int x){
	int ret=0;
	for(int i=x;i;i-=i&-i) ret+=C[i];
	return ret;
}

int main(){
	cin>>N>>M>>Q;
	for(int i=0;i<M;i++){
		cin>>x[i].l>>x[i].r;
		a[siz++]=x[i].l;
		a[siz++]=x[i].r;
	}
	for(int i=0;i<Q;i++){
		cin>>y[i].l>>y[i].r;
		y[i].id=i;
		a[siz++]=y[i].l;
		a[siz++]=y[i].r;
	}
	sort(a,a+siz);
	m=unique(a,a+siz)-a;
	for(int i=0;i<M;i++){
		x[i].l=lower_bound(a,a+m,x[i].l)-a+1;
		x[i].r=lower_bound(a,a+m,x[i].r)-a+1;
	}
	for(int i=0;i<Q;i++){
		y[i].l=lower_bound(a,a+m,y[i].l)-a+1;
		y[i].r=lower_bound(a,a+m,y[i].r)-a+1;
	}
	sort(x,x+M);
	sort(y,y+Q);
	int j=0;
	for(int i=0;i<Q;i++){
		while(j<M&&x[j].r<=y[i].r) add(x[j++].l,1);
		ret[y[i].id]=j-sum(y[i].l-1);
	}
	for(int i=0;i<Q;i++) cout<<ret[i]<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> cnt(n+1, vector<int>(n+1, 0));
    rep(i, m){
        int l, r;
        cin >> l >> r;
        cnt[l][r]++;
    }
    rep(i, n)rep(j, n){
        cnt[i+1][j+1] += cnt[i][j+1] + cnt[i+1][j] - cnt[i][j];
    }
    rep(i, q){
        int p, q;
        cin >> p >> q;
        int ans = cnt[q][q] - cnt[q][p-1] - cnt[p-1][q] + cnt[p-1][p-1];
        cout << ans << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,char> Q;

ll N;
vector<Q> v;
vector<vector<P>> V,H;
set<P> S;
vector<vector<ll>> LU,LD,RU,RD;

int main(){
    cin >> N;
    V.resize(200010);
    H.resize(200010);
    LU.resize(400010);
    LD.resize(400010);
    RU.resize(400010);
    RD.resize(400010);
    ll offset = 200000;
    for(int i=0;i<N;i++){
        ll x,y;
        char u;
        cin >> x >> y >> u;
        v.push_back({{x,y},u});
    }
    for(auto &e : v){
        ll x = e.first.first;
        ll y = e.first.second;
        if(S.find({x,y}) != S.end()){
            cout << 0 << endl;
            return 0;
        }
        S.insert({x,y});
    }
    for(auto &e : v){
        ll x = e.first.first;
        ll y = e.first.second;
        if(e.second == 'U'){
            V[x].push_back({y,0});
            RU[x+y].push_back(x);
            LU[x-y+offset].push_back(x);
        }
        if(e.second == 'D'){
            V[x].push_back({y,1});
            LD[x+y].push_back(x);
            RD[x-y+offset].push_back(x);
        }
        if(e.second == 'R'){
            H[y].push_back({x,0});
        }
        if(e.second == 'L'){
            H[y].push_back({x,1});
        }
    }
    ll ans = 1e18;
    for(int i=0;i<=200000;i++) sort(V[i].begin(),V[i].end());
    for(int i=0;i<=200000;i++) sort(H[i].begin(),H[i].end());
    for(int i=0;i<=200000;i++){
        ll tmp = -1;
        for(auto &e : V[i]){
            ll x = e.first;
            ll d = e.second;
            if(d == 0) tmp = x;
            else{
                if(tmp == -1) continue;
                ans = min(ans, (x-tmp) * 5);
            }
        }
    }
    for(int i=0;i<=200000;i++){
        ll tmp = -1;
        for(auto &e : H[i]){
            ll y = e.first;
            ll d = e.second;
            if(d == 0) tmp = y;
            else{
                if(tmp == -1) continue;
                ans = min(ans, (y-tmp) * 5);
            }
        }
    }
    for(int i=0;i<=400000;i++) sort(LU[i].begin(),LU[i].end());
    for(int i=0;i<=400000;i++) sort(LD[i].begin(),LD[i].end());
    for(int i=0;i<=400000;i++) sort(RU[i].begin(),RU[i].end());
    for(int i=0;i<=400000;i++) sort(RD[i].begin(),RD[i].end());
    for(auto &e : v){
        ll x = e.first.first;
        ll y = e.first.second;
        if(e.second == 'U'){
            continue;
        }
        if(e.second == 'D'){
            continue;
        }
        if(e.second == 'R'){
            auto itr_d = upper_bound(RD[x-y+offset].begin(),RD[x-y+offset].end(),x);
            if(itr_d != RD[x-y+offset].end()) ans = min(ans,((*itr_d) - x)*10);
            auto itr_u = upper_bound(RU[x+y].begin(),RU[x+y].end(),x);
            if(itr_u != RU[x+y].end()) ans = min(ans,((*itr_u) - x)*10);
        }
        if(e.second == 'L'){
            auto itr_d = upper_bound(LD[x+y].begin(),LD[x+y].end(),x);
            if(itr_d != LD[x+y].begin() && LD[x+y].size() != 0) ans = min(ans,(x - *(itr_d-1))*10);
            auto itr_u = upper_bound(LU[x-y+offset].begin(),LU[x-y+offset].end(),x);
            if(itr_u != LU[x-y+offset].begin() && LU[x-y+offset].size() != 0) ans = min(ans,(x - *(itr_u-1))*10);
        }
    }
    if(ans == 1e18) cout << "SAFE" << endl;
    else cout << ans << endl;
    return 0;
}

#include<cstdio>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const static int MOD = 1e9+7;

int main(){
	int N;
	scanf("%d\n", &N);
	bool is_black[2*N];
	for (int i = 0; i < 2*N; ++i){
		char tmp;
		scanf("%c", &tmp);
		is_black[i] = tmp == 'B';
	}
	bool is_left_end[2*N];
	if(!is_black[0]){
		printf("0\n");
		return 0;
	}
	is_left_end[0] = true;
	int n_right_end = 0;
	ull ans = 1;
	for (int i = 1; i < 2*N; ++i){
		is_left_end[i] = (is_black[i] ^ is_black[i-1]) ? is_left_end[i-1] : !is_left_end[i-1];
		if(!is_left_end[i]){
			//これを含めてこれより左側にある左端の数はi-n_right_end、右端の数はn_right_end+1
			if(i - n_right_end < n_right_end + 1){
				printf("0\n");
				return 0;
			}
			(ans *= i - n_right_end*2) %= MOD;
			n_right_end++;
		}
	}
	if(n_right_end != N){
		printf("0\n");
		return 0;
	}
	for (int i = 2; i <= N; ++i) (ans *= i) %= MOD;
	printf("%llu\n", ans);
	return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

using namespace std;
LL p = 1000000007ll;

int main(void)
{
  int n;
  cin >> n;
  vector<int> a(n);
  for(auto &x: a) cin >> x;
  vector<vector<int> > cnt0(1<<20);
  int zeros = 0;
  int part = 0;
  for(auto x:a) {
    part ^= x;
    if(part) cnt0[part].push_back(zeros);
    else ++zeros;
  }
  if(part) {
    LL pre = 0;
    LL j = 1;
    LL k = 0;
    for(auto c:cnt0[part]) {
      LL tmp = c-pre;
      j += k*tmp;
      j %= p;
      k += j;
      k %= p;
      pre = c;
    }
    cout << j << endl;
    return 0;
  } else {
    LL answer = 1;
    REP(i,zeros-1) {
      answer *= 2;
      answer %= p;
    }
    FOR(i,1,1<<20) {
      LL pre = 0;
      LL j = 1;
      LL k = 0;
      for(auto c:cnt0[i]) {
        LL tmp = c-pre;
        j += k*tmp;
        j %= p;
        k += j;
        k %= p;
        pre = c;
      }
      answer += k;
      answer %= p;
    }
    cout << answer << endl;
    return 0;
  }
}

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

const int N = 550000;
const int MOD = 1e9 + 7;

int a[N], sum[N], cnt[N];
vector<int> pos[1 << 21];
int chk[1 << 21];

inline int modExp(int a, int n, int mod) {
	int ret = 1;
	while (n) {
		if (n & 1) ret = (long long)ret * a % mod;
		a = (long long)a * a % mod;
		n >>= 1;
	}
	return ret;
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] ^ a[i], cnt[i] = cnt[i - 1] + (sum[i] == 0);
	for (int i = 1; i <= n; i++) pos[sum[i]].push_back(i);
	if (sum[n] == 0) {
		int ans = 0;
		for (int i = 1; i <= n; i++) if (sum[i] && !chk[sum[i]]) {
			chk[sum[i]] = 1;
			int cnt0 = 1, cntg = 0;
			for (int j = pos[sum[i]].size() - 1; j >= 0; j--) {
				(cntg += cnt0) >= MOD && (cntg -= MOD);
				if (j) (cnt0 += (long long)cntg * (cnt[pos[sum[i]][j]] - cnt[pos[sum[i]][j - 1]]) % MOD) >= MOD && (cnt0 -= MOD);
			}
			(ans += cntg) >= MOD && (ans -= MOD);
		}
		(ans += modExp(2, cnt[n] - 1, MOD)) >= MOD && (ans -= MOD);
		cout << ans << "\n";
		return 0;
	} else {
		int cnt0 = 1, cntg = 0;
		for (int j = 0; j < pos[sum[n]].size(); j++) {
			(cntg += cnt0) >= MOD && (cntg -= MOD);
			if (j + 1 < pos[sum[n]].size()) (cnt0 += (long long)cntg * (cnt[pos[sum[n]][j + 1]] - cnt[pos[sum[n]][j]]) % MOD) >= MOD && (cnt0 -= MOD);
		}
		cout << cnt0 << "\n";
	}

	return 0;
}


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

int main() {
  // Grid Repainting 2
  int h, w; cin >> h >> w;
  vector<string> s(h);
  rep(i,h) cin >> s.at(i);

  vector<int> di = {1, 0, -1, 0};
  vector<int> dj = {0, 1, 0, -1};

  rep(i,h) {
    rep(j,w) {
      if (s.at(i).at(j) == '#') {
        int cnt = 0;
        rep(k,4) {
          int ni = i + di[k];
          int nj = j + dj[k];
          if (0 <= ni && ni < h && 0 <= nj && nj < w) {
            if (s.at(ni).at(nj) == '.') cnt++;
          }
        }
        if (cnt == 4) {
          cout << "No" << endl;
          return 0;
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

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

int main(){
    string s;
    cin>>s;
    if(s=="zyxwvutsrqponmlkjihgfedcba"){
        out(-1);
        return 0;
    }
    if(s.length()<26){
    cout<<s;
    char c='a';
    rep(i,26){
        if(count(all(s),c)==0){
            cout<<c<<endl;
            return 0;
        }
        c++;
    }
    }else if(s.length()==26){
        int n=25,d=0;
        while(1){
            s[n]++;
            if(s[n]>'z'){
                s[n-1]++;
                s.erase(s.begin()+n);
                n--;
            }
            if(s[n]>='a'&&s[n]<='z'&&count(all(s),s[n])==1)break;
        }
        cout<<s<<endl;
    }
}