#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v),end(v)
#define debug(x) std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"

using ll=long long;
using vi=std::vector<int>;
using vvi=std::vector<vi>;
using vll=std::vector<ll>;
using vvll=std::vector<vll>;
template<typename T> using vvec=std::vector<std::vector<T>>;
template<typename T>
auto make_v(size_t sz){return std::vector<T>(sz);}
template<typename T,typename... Ts>
auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));}
template<typename T>
void fill_v(T&var,const T&x){var=x;}
template<typename V,typename T>
void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}}

template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){
  s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;}

inline void scan(int&a){scanf("%d",&a);}
inline void scan(ll&a){scanf("%lld",&a);}
inline void scan(char&a){scanf(" %c",&a);}
inline void scan(double&a){scanf("%lf",&a);}
template<typename T>
inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}}
template<typename First,typename...Args>
inline void scan(First&f,Args&...args){scan(f);scan(args...);}
inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);}
inline void print(int a){printf("%d\n",a);}
inline void print(ll a){printf("%lld\n",a);}
inline void print(double a){printf("%.12f\n",a);}
inline void print(std::string s){std::cout<<s<<"\n";}

using namespace std;

int mod = 1000000007;

bool is_wafu(vi& x, int m) {
  return ((m >> (x[2] - 1)) & 1)
      && ((m >> (x[2] + x[1] - 1)) & 1)
      && ((m >> (x[2] + x[1] + x[0] - 1)) & 1);
}

int main() {
  int n;
  scan(n);
  vi x(3);
  scan(x);
  int sumd = x[0] + x[1] + x[2];

  ll bitdpmax = (1LL << sumd);
  vector<bool> wafu(bitdpmax << 1);
  rep (i, bitdpmax << 1) {
    wafu[i] = is_wafu(x, i);
  }
  vll dp(bitdpmax, 0);
  dp[0] = 1;
  vll dp_;
  rep (i, n) {
    dp_.assign(bitdpmax, 0);
    rep (j, bitdpmax) {
      if (dp[j] == 0) { continue; }
      if (wafu[j]) { continue; }
      FOR (k, 1, 11) {
        int nj = (j << k) + (1 << (k-1));
        nj &= ((bitdpmax << 1) - 1);
        if (wafu[nj]) { continue; }
        nj &= (bitdpmax - 1);
        dp_[nj] = (dp_[nj] + dp[j]) % mod;
      }
    }
    swap(dp, dp_);
  }

  ll ans = 1;
  rep (i, n) {
    ans = ans * 10 % mod;
  }
  rep (j, bitdpmax) {
    ans = (ans + mod - dp[j]) % mod;
  }
  print(ans);

  return 0;
}

#include<bits/stdc++.h>
#define int long long
#define rg register
#define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
const int mod=1e9+7;
int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
    return f*x;
}
int f[41][262143+10];
int ksm(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
main(){
    //file("");
	int n=read(),x=read(),y=read(),z=read(),len=(1<<(x+y+z))-1,res=1<<(x+y+z-1),ans=0;
    res=res|(1<<(y+z-1)),res=res|(1<<(z-1)),f[0][0]=1;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=len;j++){
			if(f[i-1][j]==0) continue;
			for(int k=1;k<=10;k++){
				int z=(j<<k)|(1<<(k-1));
				z&=len;
				if((z&res)==res) continue;
				f[i][z]=(f[i-1][j]+f[i][z])%mod;
			}
		}
	for(int i=0;i<=len;i++)
		ans=(ans+f[n][i])%mod;
	cout<<(ksm(10,n)-ans+mod)%mod;
    return 0;
}


#include<stdio.h>

int main (){
	int number, A,B,C;
	scanf("%d", &number );
	A = number / 100;
	B = number % 100 / 10;
	C = number % 10;
	if (A == 7 || B == 7 || C == 7)
	{
		printf("Yes\n");
	}
	else
	{
		printf("No\n");
	}

	return 0;
}

#include<iostream>

using namespace std;

int f(int x){
	return x*x;
}

int main(){
	int x;
	while(cin>>x){
		int ans=0;
		for(int i=1;i<600/x;i++)ans+=f(i*x)*x;
		cout<<ans<<endl;
	}
}


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

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

  if (N.size() == 3) reverse(N.begin(), N.end());
  cout << N << endl;
}

#include <iostream>
#include <string>

using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    getline(cin, s);
    for (int i = 6; i < s.length(); i++)
      if (s[i-6] == 'H' &&
          s[i-5] == 'o' &&
          s[i-4] == 's' &&
          s[i-3] == 'h' &&
          s[i-2] == 'i' &&
          s[i-1] == 'n' &&
          s[i] == 'o')
        s[i] = 'a';
    cout << s << endl;
  }
  return 0;
}


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

int const M = 1e9 + 7;
vector<int> memo(5500000, -1);
string K; int D;

int count(size_t i, int total, bool upto) {
	if (i == K.size()) return total == 0;

	// 14 bits for index, 7 bits for total, 1 bit for upto
	int key = (i << 9) + (total << 1) + upto;
	if (memo[key] >= 0) return memo[key];

	int sum = 0, leading = K[i] - '0';
	for (int digit = 0; digit <= (upto ? leading : 9); ++digit) {
		if (digit == leading)
			sum += count(i+1, (total+digit) % D, upto);
		else
			sum += count(i+1, (total+digit) % D, false);
		sum %= M;
	}

	return memo[key] = sum;
}

int main() {
	ios_base::sync_with_stdio(false);

	getline(cin, K);
	cin >> D;

	int answer = count(0, 0, true) - 1;
	if (answer < 0) answer += M;
	cout << answer << endl;
}

#include <bits/stdc++.h>

#define name "vd"
#define ini freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define oo 1e9 + 7;
#define PII pair<int, int>
#define long long long
using namespace std;

const int NMAX = 1e4 + 5;
const int MOD = 1e9 + 7;
string s;
int d, n, nxt;
long dp[NMAX][2][105];

int main()
{
    //ini;
    fastio;
    cin >> s >> d;
    n = s.size();
    s = "0" + s;

    memset(dp, 0, sizeof dp);

    dp[0][0][0] = 1;
    for (int i = 1; i <= n; i++){
        for (int lss = 0; lss <= 1; lss++){
            for (int j = 0; j < d; j++){
                for (int dg = 0; dg <= 9; dg++){
                    if (!dp[i - 1][lss][j]) continue;
                    if (!lss && dg > (s[i] - '0')) continue;

                    if (!lss && dg == (s[i] - '0')) nxt = 0;
                    else nxt = 1;

                    dp[i][nxt][(j + dg) % d] += dp[i - 1][lss][j] % MOD;
                }
            }
        }
    }

    cout << ((dp[n][0][0] + dp[n][1][0] - 1) % MOD + MOD) % MOD;
    return 0;
}


#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
const int maxn=1000000+10;
char a[maxn],b[maxn],sta[maxn];
int dl[maxn*3][2];
int i,j,k,l,t,n,m,tot,head,tail,ans,mi,cnt,num;
bool czy;
int main(){
    scanf("%d",&n);
    scanf("%s",a+1);
    scanf("%s",b+1);
    fo(i,1,n+1)
        if (i>n||a[i]!=b[i]) break;
    if (i>n){
        printf("0\n");
        return 0;
    }
    czy=1;
    mi=n+1;
    i=n;k=n+1;
    head=n+1;tail=n;
    while (i){
        j=i;
        while (j>1&&b[j-1]==b[i]) j--;
        k=min(k,j);
        while (k&&a[k]!=b[i]) k--;
        if (!k){
            czy=0;
            break;
        }
        while (head<=tail){
            if (dl[tail][0]-num>i) tail--;
            else break;
        }
        cnt++;
        num++;
        head--;
        dl[head][0]=k+num;
        dl[head][1]=1-cnt;
        ans=max(ans,dl[tail][1]+cnt);
        i=j-1;
    }
    if (!czy) printf("-1\n");
    else printf("%d\n",ans);
}

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

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

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

int H,W,sy,sx,gy,gx;
int N;

int C[100][100];
bool vis[100][100];

signed main(){
    while(cin>>W>>H,W||H){
        cin>>sx>>sy>>gx>>gy;
        cin>>N;
        sy--;sx--;gy--;gx--;
        memset(C,0,sizeof(C));
        rep(i,N){
            int c,d,x,y;
            cin>>c>>d>>x>>y;
            x--;y--;
            for(int dy=0;dy<2;dy++){
                for(int dx=0;dx<4;dx++){
                    int ny,nx;
                    if(!d)ny=y+dy,nx=x+dx;
                    else ny=y+dx,nx=x+dy;
                    C[ny][nx]=c;
                }
            }
        }
        if(!C[sy][sx]){
            cout<<"NG"<<endl;
            continue;
        }

        memset(vis,0,sizeof(vis));
        queue<pint>que;
        que.push(pint(sy,sx));
        vis[sy][sx]=true;
        while(que.size()){
            int y,x;
            tie(y,x)=que.front();
            que.pop();
            rep(i,4){
                int ny=y+dy[i],nx=x+dx[i];
                if(ny<0||ny>=H||nx<0||nx>=W||C[y][x]!=C[ny][nx]||vis[ny][nx])continue;
                vis[ny][nx]=true;
                que.push(pint(ny,nx));
            }
        }
        if(vis[gy][gx])cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define str string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

#define vc vector<char>
#define vvc vector<vc>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvvll vector<vvll>
#define vs vector<str>
#define vvs vector<vs>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vpll vector<pll>
#define vvpll vector<vpll>
#define vb vector<bool>
#define vvb vector<vb>
#define rep(i, a, b) for (int i = (a); i < int(b); i++)
#define repi(i, a, b) for (int i = (a); i <= int(b); i++)

using namespace std;
ll INF = LONG_LONG_MAX;
ll mod = 1000000000 + 7;

template <typename T, typename L>
void readVector(vector<T> & _data, L & _size, bool _shift) {
    _data.resize(_size + (ll)_shift);
    for (ll i = (ll)_shift; i < _size + (ll)_shift; i++)
        cin >> _data[i];
}

template <typename T, typename L>
void readMatrix(vector<vector<T>> & _data, L & _rows, L & _cols, bool _shiftRows, bool _shiftCols) {
    _data.resize(_rows + (ll)_shiftRows);
    for (ll i = 0; i < _rows + (ll)_shiftRows; i++)
        _data[i].resize(_cols + (ll)_shiftCols);
    for (ll i = (ll)_shiftRows; i < _rows + (ll)_shiftRows; i++)
        for (ll j = (ll)_shiftCols; j < _cols + (ll)_shiftCols; j++)
            cin >> _data[i][j];
}

//TODO: SOLUTION

ll n;
vll a, pre, f, g;

void computeG() {
    ll j = n - 2;
    g[n - 1] = n;

    while (j >= 1) {
        int k = g[j + 1];
        while (abs(pre[n] - (2*pre[k - 1]) + pre[j - 1]) > abs(pre[n] - (2*pre[k - 2]) + pre[j - 1]))
            k--;
        g[j] = k;
        j--;
    }
}

void computeF() {
    ll j = 3;
    f[2] = 1;

    while (j <= n) {
        int k = f[j - 1];
        while (abs(pre[j] - (2*pre[k])) > abs(pre[j] - (2*pre[k + 1])))
            k++;
        f[j] = k;
        j++;
    }
}

void solve() {
    cin >> n;
    readVector(a, n, 1);
    pre.resize(n + 1, 0);

    repi(i, 1, n) {
        pre[i] = pre[i - 1] + a[i];
    }

    f.resize(n + 1);
    g.resize(n + 1);

    computeF();
    computeG();

    ll ans = INF;
    repi(i, 2, n - 2) {
        ans = min(ans, max(max(max(pre[f[i]], pre[i] - pre[f[i]]), pre[n] - pre[g[i + 1] - 1]), pre[g[i + 1] - 1] - pre[i])
                      -min(min(min(pre[f[i]], pre[i] - pre[f[i]]), pre[n] - pre[g[i + 1] - 1]), pre[g[i + 1] - 1] - pre[i]));
    }

    cout << ans << endl;
}

int main() {

    // TODO: Set value of this variable manually
    bool _multipleTestCases = false;

    if (_multipleTestCases) {
        ll t; cin >> t;
        while (t--)
            solve();
    }
    else {
        solve();
    }

    return 0;
}

#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define FOR(i,n,m) for(int i=n; i<(int)m; i++)
#define ROF(i,n,m) for(int i=n; i>(int)m; i--)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define ri(a) scanf("%d",&a)
#define rii(a,b) ri(a),ri(b)
#define riii(a,b,c) rii(a,b),ri(c)
#define lri(a) scanf("%lld",&a)
#define lrii(a,b) lri(a),lri(b)
#define ms(obj,val) memset(obj,val,sizeof(obj))
#define all(x) x.begin(),x.end()
#define debug true
#define dprintf debug && printf

const int INF = 0x3f3f3f3f;
const ll INFLL = 1e18;
const int MOD = 1e9+7;
const int MAXN = 2e5+5;

ll pref[MAXN];
int n;

ll sum(int l, int r){
    if(l > r) return 0;
    return pref[r] - (l == 0 ? 0 : pref[l-1]);
}

ll dif(int l, int r, int m){
    return abs(sum(l, m-1) - sum(m, r));
}

void mxze(ll &a, ll b){
    a = max(a,b);
}
void mnze(ll &a, ll b){
    a = min(a,b);
}

ll result(int m1, int m, int m2){
    ll mx = sum(0,m1-1);
    mxze(mx, sum(m1,m-1));
    mxze(mx, sum(m,m2-1));
    mxze(mx, sum(m2,n-1));

    ll mn = sum(0,m1-1);
    mnze(mn, sum(m1,m-1));
    mnze(mn, sum(m,m2-1));
    mnze(mn, sum(m2,n-1));

    return mx - mn;
}

bool check(int l, int r, int m){
    return dif(l, r, m) > dif(l,r,m+1);
}

int findBest(int l, int r){
    int lo = l + 1, hi = r;
    while(lo < hi){
        int mid = (lo + hi)>>1;
        if(check(l, r, mid)) lo = mid + 1;
        else hi = mid;
    }
    return hi;
}

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

    ri(n);
    FOR(i,0,n) lri(pref[i]);
    FOR(i,1,n) pref[i] += pref[i-1];

    ll ans = INFLL;
    FOR(m,2,n-1){
        mnze(ans, result(findBest(0,m-1), m, findBest(m, n-1)));
    }

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

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define MAX_V 100010

using namespace std;

typedef pair<int,int> ii;
typedef vector<ii>   vii;
typedef vector<int>   vi;
const int DFS_WHITE = -1; // unvisited
const int DFS_BLACK =  1; // visited

int V,E;
int dfsNumberCounter,rootChildren,dfsRoot;
vi dfs_num,dfs_low,dfs_parent,articulation_vertex;
vii AdjList[MAX_V];// ii(neighbor,weight)

void articulationPointAndBridge(int u)
{
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;
  for(int j=0;j<(int)AdjList[u].size();j++)
    {
      ii v = AdjList[u][j];
      if(dfs_num[v.first] == DFS_WHITE)
	{
	  dfs_parent[v.first] = u;
	  if(u == dfsRoot)rootChildren++;

	  articulationPointAndBridge(v.first);

	  if(dfs_low[v.first] >= dfs_num[u])
	    articulation_vertex[u] = true;
	  //if(dfs_low[v.first] >  dfs_num[u])
	    //printf("Edge (%d,%d) is a bridge\n",u,v.first);
	  dfs_low[u] = min(dfs_low[u],dfs_low[v.first]);

	}
      else if(v.first != dfs_parent[u])
	dfs_low[u] = min(dfs_low[u],dfs_num[v.first]);
    }
}

void compute()
{
  dfsNumberCounter = 0; dfs_num.assign(V,DFS_WHITE); dfs_low.assign(V,0);
  dfs_parent.assign(V,0); articulation_vertex.assign(V,0);
  //puts("Bridges:");
  for(int i = 0; i < V; i++)
    if(dfs_num[i] == DFS_WHITE)
      {
	dfsRoot = i; rootChildren = 0;
	articulationPointAndBridge(i);
	articulation_vertex[dfsRoot] = (rootChildren > 1);// special case
      }
  //puts("Articulation Points");
  for(int i = 0; i < V; i++)
    if(articulation_vertex[i])
      printf("%d\n",i);
      //printf("Vertex %d\n",i);
}

int main()
{
  scanf("%d%d",&V,&E);
  rep(i,V)AdjList[i].clear();
  rep(i,E)
    {
      int s,t;
      scanf("%d%d",&s,&t);
      AdjList[s].push_back(ii(t,1));
      AdjList[t].push_back(ii(s,1));
    }

  compute();

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
void display()
{
    int a, b, ar[3];
    cin>>a>>b;
    ar[0] = a+ b, ar[1] = a - b , ar[2] = a * b;
    sort(ar, ar + 3);
    cout<<ar[2]<<endl;
}
int main(void)
{
    display();
}


#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
#define REP(i,n) for (int i = 0; i <(n); ++i)
//debug
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

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

  if(c > b) swap(b,c);
  if(b > a) swap(a,b);
  if(c > b) swap(b,c);

  // cout << a << b << c;
  REP(i,k){
    a *= 2;
  }

  int ans = a + b + c;
  cout << ans << endl;

  return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int main()
{
    fastio();
    int a,b,c;
    cin>>a>>b>>c;
    cout<<min(min(a+b,b+c),a+c)<<endl;
    return 0;
}


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

#include <limits.h>

using namespace std;

typedef long long ll;

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

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

vector<int> problems;
vector<int> bonus;

int solve(const int bits, const int D, const ll G) {
    ll score = 0;
    int ans = 0;
    priority_queue<int> que;
    for (int i = 0; i < D; i++) {
        if (bits & (1 << i)) {
            score += (i + 1) * 100 * problems[i];
            score += bonus[i];
            ans += problems[i];
        } else {
            for (int j = 0; j < max(0, problems[i] - 1); j++) {
                que.push((i + 1) * 100);
            }
        }
    }

    while (!que.empty() && score < G) {
        auto v = que.top();
        que.pop();
        score += v;
        ans++;
    }

    if (score < G) {
        return -1;
    } else {
        return ans;
    }
}

int main(void) {
    int D;
    ll G;
    cin >> D >> G;

    problems.resize(D);
    bonus.resize(D);
    for (int i = 0; i < D; i++) {
        cin >> problems[i] >> bonus[i];
    }

    int ans = INT_MAX / 2;

    for (int bits = 0; bits < (1 << D); bits++) {
        int cand = solve(bits, D, G);
        if (cand >= 0) {
            // printf("bits: %x, required: %d\n", bits, cand);
            chmin(ans, cand);
        }
    }

    cout << ans << endl;

    return 0;
}

#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    char buf[1<<10];
    size_t q;
    scanf("%s %zu", buf, &q);
    string str(buf);

    for (size_t i=0; i<q; ++i) {
        size_t a, b;
        scanf("%s %zu %zu", buf, &a, &b);
        string instr(buf);

        if (instr == "print") {
            printf("%s\n", str.substr(a, b-a+1).c_str());
        } else if (instr == "reverse") {
            reverse(str.begin()+a, str.begin()+b+1);
        } else if (instr == "replace") {
            scanf("%s", buf);
            string p(buf);
            str.replace(a, b-a+1, p);
        }
    }

    return 0;
}


#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool int_ceil(T a, T b) { T res = a / b; if(a % b != 0) res++; return res; }
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif

void answer(bool ok){
	if(ok) cout << "POSSIBLE" << endl;
	else cout << "IMPOSSIBLE" << endl;
}

vector<ll> dividedNum(ll n){
    //n == 2 && n == 1の時エラー
    vector<ll> res;
	ll lim = sqrt(n)+1;
    for(ll i=1; i<lim; i++){
        if(n%i==0){
            res.push_back(i);
            if(i != n / i && lim <= n/i){
                res.push_back(n/i);
            }
        }
    }
    return res;
}

int main(int argc, const char * argv[]) {
	std::cout << std::fixed << std::setprecision(15);

	ll N, K; cin >> N >> K;
	vector<ll> v(N);
	rep(i, N) cin >> v[i];
	sort(ALL(v));

	if(K > v[N-1]){
		answer(false);
		return 0;
	}
	if(N == 1){
		answer(v[0] == K);
		return 0;
	}
	if(v[0] == 1) {
		answer(true);
		return 0;
	}

	bool hasTwoBaisu = true;
	auto divs = dividedNum(v[0]);
	ll res = 1;
	for(ll x : divs){
		// print(x);
		if(x == 1) continue;
		bool ok = true;
		rep(i, N){
			if(v[i] % x != 0) ok = false;
		}
		if(ok) {
			chmax(res, x);
			hasTwoBaisu = false;
		}
	}
	// print(hasTwoBaisu);
	// print(res);

	if(!hasTwoBaisu){
		answer(K % res == 0);
	}
	else{
		answer(true);
	}

	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main(){
  int n, k;
  cin >> n >> k;
  int g;
  bool f = false;
  rep(i, n){
    int in;
    cin >> in;
    if(in >= k) f = true;
    if(i == 0) g = in;
    else g = __gcd(in, g);
  }
  if(f == true && k % g == 0) cout << "POSSIBLE" << endl;
  else cout << "IMPOSSIBLE" << endl;
  return 0;
}


#include <iostream>
#include <vector>

using namespace std;

class BIT {
public:
	BIT(int n) : a(n) {}

	void add(int p, long long num)
	{
		while (p < a.size()) {
			a[p] += num;
			p |= p + 1;
		}
	}

	/* i から j までの和 */
	long long sum(int i, int j)
	{
		long long s = 0, t = 0;
		i--;
		while (i >= 0) {
			s += a[i];
			i = (i & (i + 1)) - 1;
		}
		while (j >= 0) {
			t += a[j];
			j = (j & (j + 1)) - 1;
		}
		return t - s;
	}

private:
	vector<long long> a;
};

int main()
{
	int n, q;
	cin >> n >> q;
	BIT b(n);
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		b.add(i, a);
	}
	for (int i = 0; i < q; i++) {
		int p, q, r;
		cin >> p >> q >> r;
		if (p == 1) {
			cout << b.sum(q, r - 1) << endl;
		} else {
			b.add(q, r);
		}
	}

	return 0;
}

#include<atcoder/all>
using namespace atcoder;

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

int main() {
    int N, Q; scanf("%d%d", &N, &Q);
    fenwick_tree<LL> G(N);
    rep(i, N) { int a; scanf("%d", &a); G.add(i, a); }

    rep(q, Q) {
        int c; scanf("%d", &c);
        if (c == 0) {
            int p, x; scanf("%d%d", &p, &x);
            G.add(p, x);
        }
        if (c == 1) {
            int l, r; scanf("%d%d", &l, &r);
            printf("%lld\n", G.sum(l,r));
        }
    }

    return 0;
}


/*
 * Project: AtCoder Beginners Contest 105 A - AtCoder Crackers
 * Author: toms74209200 <https://github.com/toms74209200>
 *
 * Created on 2020/06/16
 * Copyright (c) 2020 toms74209200
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
#include <cstdio>
using namespace std;

int n, k;

void solve() {

    int rem = n % k;
    if (rem == 0)
        printf("0\n");
    else
        printf("1\n");

}

int main() {

    scanf("%d", &n);
    scanf("%d", &k);

    solve();

}

#include <iostream>
using namespace std;
int main(){
  int n,k;
  cin >> n >> k;
  int c = 1;
  int i;
  for(i=0;i<n;i++){
    if(c<=k) c*=2;
    else c+=k;
  }
  cout << c << "\n";
  return 0;
}


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

int dp[105][10];
int mod = 1000000007;

int main() {
    int H, W, K; cin >> H >> W >> K;
    dp[0][0] = 1;
    for (int i = 0; i < H; i++){
        for (int j = 0; j < 1<<(W-1); j++){
            bool ok = true;
            for (int k = 0; k < (W-2); k++){
                if(j>>k&1 == 1 && j>>(k+1)&1 == 1){
                    ok = false;
                }
            }
            if(!ok)continue;
            for (int k = 0; k < W; k++){
                int t = k;
                if(j>>(k-1)&1 == 1)t = k-1;
                if(j>>(k)&1 == 1)t = k+1;
                dp[i+1][t] += dp[i][k];
                dp[i+1][t] %= mod;
            }

        }
    }

    cout << dp[H][K-1] << endl;

    return 0;
}

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

using ll = long long;
using ld  = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF=1e9+7;
const ll LINF=9223372036854775807;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld EPS = 1e-10; //微調整用（EPSより小さいと0と判定など）

int ii() { int x; if (scanf("%d", &x)==1) return x; else return 0; }
long long il() { long long x; if (scanf("%lld", &x)==1) return x; else return 0; }
string is() { string x; cin >> x; return x; }
char ic() { char x; cin >> x; return x; }
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od_nosp(double x) { printf("%.15f", x); } // 古い問題用
void od(double x) { printf("%.15f ", x); }
// long doubleで受け取り、fをLfなどに変えて出力すると、変な数値が出る
// それをなんとかするには独自の出力を作らなければならなそう
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
#define o_map(v){cerr << #v << endl; for(const auto& xxx: v){cout << xxx.first << " " << xxx.second << "\n";}} //動作未確認
void br() { putchar('\n'); }

// #define gcd __gcd //llは受け取らない C++17~のgcdと違うので注意
// int lcm(int a, int b){return a / gcd(a, b) * b;}
#define begin_end(a) a.begin(),a.end() //sort(begin_end(vec));
#define REP(i,m,n) for(ll i=(ll)(m) ; i < (ll)(n) ; i++ )
#define DREP(i,m,n) for(ll i=(ll)(m) ; i > (ll)(n) ; i-- )
#define rep(i,n) REP(i,0,n)
#define m_p(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define p_b push_back
#define SZ(x) ((int)(x).size) //size()がunsignedなのでエラー避けに

// coutによるpairの出力（空白区切り）
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << " " << p.second << ")";}
// coutによるvectorの出力（空白区切り）
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << " "; //"\t"に変えるとTabで見やすく区切る
  }
  return s;
}
// coutによる多次元vectorの出力（空白区切り）
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

//最大値、最小値の更新。更新したor等しければtrueを返す
template<typename T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<typename T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}

//4近傍（上下左右） rep(i, 2) にすると右・下だけに進む
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};

// -------- template end - //

// - library ------------- //

// --------- library end - //

int main(){

  ll n, m, d;
  cin >> n >> m >> d;

  ld ans;
  if (d==0){
    ans = ld(m-1) / n;
  }
  else if (n - d - d <= 0){
    ans = ld(m-1) / n; // d==0 のときと結果は同じ
  }
  else{
    // n-2d >= 1
    ans = ld((n-d-d)*2 + (d+d)) / n * (m-1) / n;
  }

  od_nosp(ans);

}


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

const ll INF = 1LL << 60;

ll H, W;
ll A[85][85], B[85][85];
ll AB[85][85];

const ll D = 160 * 80 + 10; // あり得る長さ
const ll D2 = D * 2; // あり得る値の範囲
typedef bitset<D2> bs;

// [r][c] にきたときに、この値はあり得るか。0 or 1
bs dp[85][85];

int main() {
    cin >> H >> W;
    rep(i, H)rep(j, W) cin >> A[i][j];
    rep(i, H)rep(j, W) cin >> B[i][j];

    rep(i, H)rep(j, W) AB[i][j] = abs(A[i][j] - B[i][j]);

    dp[0][0][D - AB[0][0]] = 1;
    dp[0][0][D + AB[0][0]] = 1;

    rep(i, H)rep(j, W) {
        if (i) {
            // たす
            dp[i][j] |= dp[i - 1][j] << AB[i][j];
            // 引く
            dp[i][j] |= dp[i - 1][j] >> AB[i][j];
        }

        if (j) {
            // たす
            dp[i][j] |= dp[i][j - 1] << AB[i][j];
            // 引く
            dp[i][j] |= dp[i][j - 1] >> AB[i][j];
        }
    }

    // 答えを求める D の分を戻す
    ll ans = INF;
    rep(i, D2) {
        if (dp[H - 1][W - 1][i]) ans = min(ans, abs(i - D));
    }
    cout << ans << endl;
}

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

int main()
{
    int H, W;
    cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(W));
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            cin >> A.at(i).at(j);
        }
    }
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            cin >> B.at(i).at(j);
        }
    }

    vector<vector<int>> d(H, vector<int>(W));
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            d.at(i).at(j) = abs(A.at(i).at(j) - B.at(i).at(j));
        }
    }

    auto to_id = [&](int i, int j) {
        return i * W + j;
    };
    vector<vector<bool>> dp(H * W, vector<bool>(80 * 160 + 1));
    dp.at(to_id(0, 0)).at(d.at(0).at(0)) = true;
    for (int i = 1; i < H; i++)
    {
        for (int k = 0; k < 80 * 160 + 1; k++)
        {
            if (dp.at(to_id(i - 1, 0)).at(k))
            {
                dp.at(to_id(i, 0)).at(abs(k + d.at(i).at(0))) = true;
                dp.at(to_id(i, 0)).at(abs(k - d.at(i).at(0))) = true;
            }
        }
    }
    for (int j = 1; j < W; j++)
    {
        for (int k = 0; k < 80 * 160 + 1; k++)
        {
            if (dp.at(to_id(0, j - 1)).at(k))
            {
                dp.at(to_id(0, j)).at(abs(k + d.at(0).at(j))) = true;
                dp.at(to_id(0, j)).at(abs(k - d.at(0).at(j))) = true;
            }
        }
    }
    for (int i = 1; i < H; i++)
    {
        for (int j = 1; j < W; j++)
        {
            for (int k = 0; k < 80 * 160 + 1; k++)
            {
                if (dp.at(to_id(i - 1, j)).at(k))
                {
                    dp.at(to_id(i, j)).at(abs(k + d.at(i).at(j))) = true;
                    dp.at(to_id(i, j)).at(abs(k - d.at(i).at(j))) = true;
                }
                if (dp.at(to_id(i, j - 1)).at(k))
                {
                    dp.at(to_id(i, j)).at(abs(k + d.at(i).at(j))) = true;
                    dp.at(to_id(i, j)).at(abs(k - d.at(i).at(j))) = true;
                }
            }
        }
    }
    for (int k = 0; k < 80 * 160 + 1; k++)
    {
        if (dp.at(to_id(H - 1, W - 1)).at(k))
        {
            cout << k << endl;
            break;
        }
    }
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int n, a, b, count = 0, pointX = 0, pointY = 0;
	cin >> n;
	do {
		cin >> a >> b;
		if (a > b) {
			pointX += (a + b);
		}
		else if (a < b) {
			pointY += (a + b);
		}
		else {
			pointX += a;
			pointY += b;
		}
		count++;
		if (count == n) {
			cout << pointX << " " << pointY << endl;
			pointX = pointY = count = 0;
			cin >> n;
		}
	} while (n != 0);
	return 0;
}

#include <iostream>

int main(){
	std::ios::sync_with_stdio(false);
    while(true){
    int n, a_score = 0, b_score = 0;
    std::cin >> n;

    if(n == 0)
        break;

    for(int i = 0; i < n; ++i){
        int a, b;
        std::cin >> a >> b;
        if(a > b)
            a_score += a + b;
        else if(a < b)
            b_score += a + b;
        else
            a_score += a, b_score += b;
    }

    std::cout << a_score << " " << b_score << std::endl;
    }
    return 0;
}


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

int main(){
    int number;
    int sum = 0;

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

        int array[number][number];
        int lastColumn[number];
        int lastRow[number];
        int total=0;

        for(int i=0; i<number; i++){
            sum = 0;
            for(int j=0; j<number; j++){
                cin >> array[i][j];
                sum += array[i][j];
            }
            lastColumn[i] = sum;
        }

        for(int j=0; j<number; j++){
            sum = 0;
            for(int i=0; i<number; i++){
                sum += array[i][j];
            }
            lastRow[j] = sum;
            total +=sum;
        }

        for(int i=0; i<number; i++){
            for(int j=0; j<number; j++){
                printf("%5d", array[i][j]);
            }
            printf("%5d\n", lastColumn[i]);
        }

        for(int i=0; i<number; i++){
            printf("%5d", lastRow[i]);
         }
        printf("%5d\n", total);

    }

    return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define PB push_back

using namespace std;

class cond{
public:
	cond(int x, int y, int z, int t){a=x;b=y;c=z;r=t;chked=false;}
	int a,b,c,r;
	bool chked;
};

int main(void){
	int a,b,c;

	while(cin >> a >> b >> c){
		if((a|b|c) == 0) break;
		vector<cond> v;
		bool changed = true;
		int state[a+b+c+1];
		int x,y,z,r;

		//at first, every state are unknown
		fill(state,state+a+b+c+1,2);

		//read data
		int n; //# of conditions
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> x >> y >> z >> r;
			v.PB(cond(x,y,z,r));
		}
		while(changed){
			changed = false;
			//for all conditions
			for(int i=0;i<n;i++){
				if(v[i].chked) continue; //do not check which is determined
				if(v[i].r == 1){          //in case work well
					state[v[i].a] = 1;
					state[v[i].b] = 1;
					state[v[i].c] = 1;
					v[i].chked = true;
					changed = true;
				}else{
					if(state[v[i].a] == 1 && state[v[i].b] == 1){
						state[v[i].c] = 0;
						changed = true;
						v[i].chked = true;
					}else if(state[v[i].a] == 1 && state[v[i].c] == 1){
						state[v[i].b] = 0;
						changed = true;
						v[i].chked = true;
					}else if(state[v[i].b] == 1 && state[v[i].c] == 1){
						state[v[i].a] = 0;
						changed = true;
						v[i].chked = true;
					}
				}
			}
		}
		for(int i=1;i<=a+b+c;i++){
			cout << state[i] << endl;
		}
	}
}


// ARC100
// D - Equal Cut

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

const int MAX_N = 200000;
const ll INF = 1e18;

int N;
int A[MAX_N];
ll s[MAX_N + 1];

int search(int a, int b) {
    int l = a + 1, r = b - 1;
    while (r - l > 1) {
        int m = (l + r) / 2;
        if (s[m] - s[a] <= s[b] - s[m]) {
            l = m;
        } else {
            r = m;
        }
    }
    return l;
}

ll solve() {
    s[0] = 0;
    for (int i = 1; i <= N; i++) {
        s[i] = s[i - 1] + A[i - 1];
    }

    ll ans = INF;
    for (int i = 1; i < N; i++) {
        int pq = search(0, i);
        int rs = search(i, N);
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                ll pqrs[4] = {
                    s[pq + j] - s[0],
                    s[i] - s[pq + j],
                    s[rs + k] - s[i],
                    s[N] - s[rs + k],
                };
                sort(pqrs, pqrs + 4);
                ans = min(ans, pqrs[3] - pqrs[0]);
            }
        }
    }
    return ans;
}

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

    cout << solve() << endl;

    return 0;
}

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=200000+100;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

namespace My_Math{
	#define N 100000

	int fac[N+100],invfac[N+100];

	int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;}
	int dec(int x,int y) {return x<y?x-y+maxd:x-y;}
	int mul(int x,int y) {return 1ll*x*y%maxd;}
	ll qpow(ll x,int y)
	{
		ll ans=1;
		while (y)
		{
			if (y&1) ans=mul(ans,x);
			x=mul(x,x);y>>=1;
		}
		return ans;
	}
	int getinv(int x) {return qpow(x,maxd-2);}

	int C(int n,int m)
	{
		if ((n<m) || (n<0) || (m<0)) return 0;
		return mul(mul(fac[n],invfac[m]),invfac[n-m]);
	}

	void math_init()
	{
		fac[0]=invfac[0]=1;
		rep(i,1,N) fac[i]=mul(fac[i-1],i);
		invfac[N]=getinv(fac[N]);
		per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1);
	}
	#undef N
}
using namespace My_Math;

int n,a[N];
ll sum[N];

ll calc(int l,int r) {return sum[r]-sum[l-1];}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	rep(i,1,n) sum[i]=sum[i-1]+a[i];
	int l=1,r=3;
	ll ans=1e18;
	rep(i,2,n-2)
	{
		while ((l+2<=i) && (abs(calc(1,l)-calc(l+1,i))>abs(calc(1,l+1)-calc(l+2,i)))) l++;
		while ((r+2<=n) && (abs(calc(i+1,r)-calc(r+1,n))>abs(calc(i+1,r+1)-calc(r+2,n)))) r++;
		ll mx=0,mn=1e18,x;
		x=calc(1,l);mx=max(mx,x);mn=min(mn,x);
		x=calc(l+1,i);mx=max(mx,x);mn=min(mn,x);
		x=calc(i+1,r);mx=max(mx,x);mn=min(mn,x);
		x=calc(r+1,n);mx=max(mx,x);mn=min(mn,x);
		ans=min(ans,mx-mn);
	}
	printf("%lld\n",ans);
	return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
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);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

const int N = 310;

//head

int n;
int a[N], cnt[4];
ld _[N][N][N];

ld dfs(int i, int j, int k) {
	if(_[i][j][k] > -0.5) return _[i][j][k];
	ld &res = _[i][j][k] = 0;
	int n_ = i+j+k;
	if(i) res += dfs(i-1, j, k) * i/n_;
	if(j) res += dfs(i+1, j-1, k) * j/n_;
	if(k) res += dfs(i, j+1, k-1) * k/n_;
	if(n_) res += (ld)n/n_;
	return res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	rep(i, n) cin >> a[i];
	rep(i, n) cnt[a[i]]++;
	rep(i, n+1) rep(j, n+1) rep(k, n+1) _[i][j][k] = -1;
	cout << fixed << setprecision(20) << dfs(cnt[1], cnt[2], cnt[3]) << endl;
}

#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

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

	int N=in(),M=in();
	ConnectNodeInfo connect(N);
	vector<int> input(N,0);
	REP(i,N-1+M){
		int u=in()-1,v=in()-1;
		connect.AddDirectionalConnection(u,v,1);
		input[v]++;
	}

	stack<int> vertex;
	vector<int> sorted;
	REP(i,N){
		if(input[i]==0){
			vertex.push(i);
			break;
		}
	}
	while(!vertex.empty()){
		auto v=vertex.top();
		vertex.pop();
		sorted.push_back(v);
		for(auto& x:connect[v]){
			input[x.first]--;
			if(!input[x.first]) vertex.push(x.first);
		}
	}
	vector<int> parent(N,-1);
	REP(i,N){
		for(auto& x:connect[sorted[i]]){
			parent[x.first] = sorted[i];
		}
	}

	for(auto& x:parent){
		out(x+1);
	}
	return 0;
}


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

int main() {
	int d, g;
	cin >> d >> g;
	vector<pair<int,int>>pc(d);
	for (int i = 0; i < d; i++)cin >> pc.at(i).first >> pc.at(i).second;
	int ans = 1e10;
	for (int bit = 0; bit < (1 << d); bit++) {
		vector<pair<int, int>>pcc = pc;
		vector<int> l(0);
		for (int i = 0; i < d; i++) if (bit & (1 << i))l.push_back(i);
		int res = 0;
		int point = 0;
		for (int i = 0; i < l.size(); i++) {
			point += pcc.at(l.at(i)).second;
			res += pcc.at(l.at(i)).first;
			point += pcc.at(l.at(i)).first * (l.at(i) + 1) * 100;
			pcc.at(l.at(i)).first = 0;
		}
		int ct = d - 1;
		while (true) {
			if (ct == -1)break;
			if (point >= g)break;
			if (pcc.at(ct).first == 0) {
				ct--;
			}
			else {
				pcc.at(ct).first--;
				res++;
				point += (ct + 1) * 100;
			}
		}
		ans = min(ans, res);
	}
	cout << ans << endl;
}

#include <string>
#include <memory>
#include <vector>
#include <iostream>
#include <set>
#include <algorithm>
#include <array>

using namespace std;

vector<vector<int>> graph;
vector<int> colors;
vector<bool> used;

bool Dfs(int v, int col, int* sz) {
  used[v] = true;
  colors[v] = col;
  (*sz)++;
  bool ans = false;
  for (const auto& to : graph[v]) {
    if (!used[to]) {
      if (Dfs(to, 1 - col, sz)) {
        ans = true;
      }
    } else {
      if (colors[to] == col) {
        ans = true;
      }
    }
  }
  return ans;
}

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

  int n, m;
  cin >> n >> m;
  graph.resize(n);
  colors.resize(n);
  used.resize(n);

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  std::array<int64_t, 2> arr{};
  int64_t one = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int sz = 0;
      if (Dfs(i, 0, &sz)) {
        arr[0]++;
      } else if (sz == 1) {
        one++;
      } else {
        arr[1]++;
      }
    }
  }
  int64_t ans = one * 1ll * n * 2 - one * one;
  cout << arr[0] * arr[0] + 2 * arr[0] * arr[1] + 2 * arr[1] * arr[1] + ans  << endl;
}


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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

enum{
  t = 0,
  f ,
  r ,
  b ,
  l ,
  d,
};

array<int,6> dice = {1,3,5,4,2,6};

void Spin(array<int,6> &D,int dir){
  if(dir == l){//
    swap(D[t],D[r]);
    swap(D[r],D[d]);
    swap(D[d],D[l]);
  }
  else if(dir == r){
    swap(D[t],D[l]);
    swap(D[l],D[d]);
    swap(D[d],D[r]);
  }
  else if(dir == f){
    swap(D[t],D[b]);
    swap(D[b],D[d]);
    swap(D[d],D[f]);
  }
  else if(dir == b){
    swap(D[t],D[f]);
    swap(D[f],D[d]);
    swap(D[d],D[b]);
  }
  else if(dir == d){//leftspin
    swap(D[f],D[r]);
    swap(D[r],D[b]);
    swap(D[b],D[l]);
  }
  else if(dir == t){
    swap(D[f],D[l]);
    swap(D[l],D[b]);
    swap(D[b],D[r]);
  }
}

void ViewDice(array<int,6> D){
  rep(i,6){
    cout << D[i]<< " ";
  }
  cout << endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  array<int,6> di;
  cin >> di[t] >> di[f] >> di[r] >> di[l] >> di[b] >> di[d];
  string s;
  cin >> s;
  rep(i,s.size()){
    if(s[i] == 'S'){
      Spin(di,f);
    }
    if(s[i] == 'E'){
      Spin(di,r);
    }
    if(s[i] == 'W'){
      Spin(di,l);
    }
    if(s[i] == 'N'){
      Spin(di,b);
    }
  }
  cout << di[t]<<endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define  ft  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define  ll  long long
#define  pb  push_back
#define  F   first
#define  S   second
const int N=1e5+5;
ll a[N];
int main()
{
    ft
    ll t,i,j,n,m,l,r,p,q,s=0;
    cin>>n>>l>>r;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=1;i<n;i++)
    {
        p=(a[i]-a[i-1])*l;
        s+=min(p,r);
    }
    cout<<s<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, h, w;
  cin >> n >> h >> w;
  cout << (n-h+1)*(n-w+1) << endl;
  return 0;
}

#include <iostream>

using namespace std;

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    a=a+b;
    c=c+d;
    if(a>c)
    {
        cout<<"Left";
    }
    else if(a==c)
        cout<<"Balanced";
    else
        cout<<"Right";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m; cin>>n>>m;
  int mx=INT_MIN, mn=INT_MAX;
  for(int i=0;i<m;i++){
    int l,r; cin>>l>>r;
    mx = max(mx, l);
    mn = min(mn,r);
  }
  cout<<max(0,mn-mx+1)<<"\n";
}

#include<bits/stdc++.h>

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

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

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main(){
  ll m;cin>>m;
  ll result = 0;
  ll digits_counter=-1;
  ll sum=0;
  rep(i, m){
    ll d,c;cin>>d>>c;
    digits_counter+=c;
    sum+= d*c;
  }

  if(sum%9==0)cout << sum/9+digits_counter-1 << endl;
  else cout << sum/9+digits_counter << endl;

//  cout << (sum-1)/9+digits_counter << endl;

  return 0;
}


#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size()){cout << a[i];cout << (i == a.size() - 1 ? "\n":" ");}
#define mp make_pair
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const dou pi = 3.14159265358;
const ll mod = 1000000007LL;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a / gcd(a,b) * b;
}

//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < cmax; 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;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    string S;
    cin >> S;
    ll s = S.size();
    if(s != 26){
        map<char,ll> ma;
        rep(i,s){
            ma[S[i]]++;
        }
        for(char c = 'a';c <= 'z';c++){
            if(!ma[c]){
                S += c;
                break;
            }
        }
        cout << S << endl;
    }
    else{
        if(S == "zyxwvutsrqponmlkjihgfedcba"){
            cout << "-1\n";
            return 0;
        }
        string T = S;
        next_permutation(all(S));
        rep(i,s){
            if(T[i] == S[i])cout << S[i];
            else{
                cout << S[i] << "\n";
                return 0;
            }
        }
    }
}

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

string s;

int main(void){
	cin >> s;
	lli n = s.size();
	if(s.size() == 26){
		if(s == "zyxwvutsrqponmlkjihgfedcba") cout << -1 << endl;
		else{
			string b = s;
			next_permutation(s.begin(), s.end());
			rep(i, 26){
				if(s[i] == b[i]) cout << s[i];
				else{
					cout << s[i] << endl;
					return 0;
				}
			}
		}

		return 0;
	}

	vector<bool> used(26);
	rep(i, n) used[s[i]-'a'] = true;
	rep(i, 26){
		if(used[i] == false){
			cout << s+char('a'+i) << endl;
			return 0;
		}
	}
	cout << "error" << endl;
	return 0;
}


#include<iostream>
using namespace std;

#define N 10	// üHÌ

int main()
{
	double l[N], v[2];
	double sum[N];
	double position;
	int i;

	while (scanf("%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",&l[0],&l[1],&l[2],&l[3],&l[4],&l[5],&l[6],&l[7],&l[8],&l[9],&v[0],&v[1]) != EOF) {
		sum[0] = l[0];
		for (i = 1; i < N; i++)
			sum[i] = sum[i-1]+l[i];
		position = v[0] * sum[9] / (v[0]+v[1]);
		for (i = 0; i < N; i++)
			if (sum[i]>=position)
				break;
		i++;

		cout<<i<<endl;
	}

	return 0;
}

#include <iostream>

using namespace std;

int main()
{
	double l[10], sum, v1, v2;
	char c;

	while (1) {
		for (int i = 0; i < 10; i++) {
			cin >> l[i] >> c;
		}
		if (cin.eof())	break;

		cin >> v1 >> c >> v2;

		sum = 0;
		for (int i = 0; i < 10; i++) {
			sum += l[i];
		}

		sum *= v1 / (v1 + v2);

		for (int i = 0; i < 10; i++) {
			sum -= (double)l[i];

			if (sum <= 0) {
				cout << i + 1 << endl;
				break;
			}
		}
	}

	return 0;
}


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

using namespace std;

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

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

		decode();
	}

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

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

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

	ans = count;
}

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

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

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

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

	return 0;
}

#include <iostream>
using namespace std;

const int NOT_FOUND=-1;

int linerSearch(int key, int A[],int n){
	int i=0;
	A[n]=key;
	while(A[i]!=key)
		i++;

	if(i==n)
		return NOT_FOUND;
	else
		return i;

}

int main(){
	int n,q;

	cin >>n;
	int S[n+1];
	for(int i=0; i<n; i++)
		cin>>S[i];

	cin >>q;
	int T[q+1];
	for(int i=0; i<q; i++)
		cin>>T[i];

	int C=0;
	for(int i=0; i<q; i++){
		if(linerSearch(T[i], S,n)!=NOT_FOUND)
			C++;
	}

	cout<<C<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long int power(int a,int b)
{
    if(b==0)
        return 1;
    long long int k=power(a,b/2);
    if(b%2==0)
        return ((k%M)*(k%M))%M;
    else
        return ((k%M)*(k%M)*(a%M))%M;
}
long long int fact(long long int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return ((n%M)*(fact(n-1)%M))%M;

}
int div(int n)
{  int c=0;
    for(int i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
          if(n/i==i)
            c++;
          else
            c+=2;
        }

    }

    return  c;
}
int func(int n)
{
    return (n*(n+1))/2;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     #ifndef ONLINE_JUDGE

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

   #endif

   string str;
   cin>>str;
   set<char> s;
   for(int i=0;i<str.length();i++)
   {
      s.insert(str[i]);
   }
   if(s.size()==26)
    cout<<"None";
  else
  { int i=0;

    for(auto it=s.begin();it!=s.end();it++,i++)
    {

        if(*it!=(97+i))
        {

            cout<<(char)(97+i);
            return 0;
        }
    }
    if(s.size()<26)
    {
        cout<<char(97+s.size());
    }
  }

 return 0;
}

#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

int main(){
    string n;
    cin >> n;
    int len = n.size();
    vector<ll> tmp(2);
    vector<vector<ll>> bill(len, tmp);
    bill[0][0] = n[len-1] - '0';
    bill[0][1] = 10 - (n[len-1] - '0');
    rep2(i, 1, len){
      int x = n[len - 1 - i] - '0';
      bill[i][0] = min(bill[i - 1][0] + x, bill[i-1][1] + x + 1);
      bill[i][1] = min(bill[i-1][0] + 10 - x, bill[i-1][1] + 9 - x);
    }
    cout << min(bill[len-1][0], bill[len-1][1]+1) << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

ll cnt[26];
int main()
{
	string S; cin >> S;
	cnt[S[0] - 'a']++;
	ll ans = 1;
	for (int i = 1; i < S.size(); i++) {
		ans += i - cnt[S[i] - 'a'];
		cnt[S[i] - 'a']++;
	}
	cout << ans << endl;
}

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main(void) {
    int n, i, j, l[30] = {};
    ll ans = 1;
    string a;

    cin >> a;

    n = a.size();

    for (i = 0; i < n; i++) {
        l[a[i] - 'a']++;
    }

    for (i = 0; i < 26; i++) for (j = i + 1; j < 26; j++) {
        ans += (ll)l[i] * l[j];
    }

    cout << ans << endl;

    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int n, A[1010000], B[1010000], cnt;
vector<int>E[26];
char p[1010000], q[1010000];
int main(){
    int i, pv, ck = 0, res = 0;
    scanf("%d",&n);
    scanf("%s",p+1);
    scanf("%s",q+1);
    for(i=1;i<=n;i++){
        E[p[i]-'a'].push_back(i);
        if(p[i]!=q[i])ck = 1;
    }
    if(!ck){
        printf("0\n");
        return 0;
    }
    pv = n;
    for(i=n;i>=1;i--){
        if(q[i]!=q[i-1]){
            pv = min(pv, i);
            int x = q[i]-'a';
            int t = lower_bound(E[x].begin(), E[x].end(), pv + 1) - E[x].begin();
            if(!t){
                printf("-1\n");
                return 0;
            }
            B[++cnt] = i;
            A[cnt] = E[x][t-1];
            pv = E[x][t-1] - 1;
        }
    }
    for(i=1;i<=cnt/2;i++)swap(A[i],A[cnt+1-i]),swap(B[i],B[cnt+1-i]);
    cnt++;
    A[cnt]=B[cnt]=n+1;
    for(i=1;i<=cnt;i++) A[i]-=i, B[i]-=i;
    for(i=1;i<=cnt;i++){
        int t = lower_bound(A+1,A+cnt+1,B[i]) - A;
        res = max(res, t - i);
    }
    printf("%d\n",res+1);
}

#include<stdio.h>

int main()
{
    int A, B;
    scanf("%d %d", &A, &B);
    int size = A + B + 1;

    char str[100];
    scanf("%s", str);

    int i, count = 0;

    if (str[A] == '-')
    {
        for (i = 0; i < size; i++)
        {
            if (str[i] >= '0' && str[i] <= '9')
            {
                count++;
            }
        }

        if (count == size - 1)
        {
            printf("Yes\n");
        }

        else
        {
            printf("No\n");
        }
    }

    else
    {
        printf("No\n");
    }

    return 0;
}


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

#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff              first
#define ss              second
//#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x)           (int)x.size()
#define db(x)           cout<<x<<"\n";

const int N = 2e5 + 5;

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

//////////////////////////////code//////////////////////////////

string s;
int n;

int dp[10001][100][2][2]; // digitnum, digitval , modval , sameask , greatrthanzero

int sol(int i,int k,int l,int m)
{
   // deb(i,j,k,l,m);

    if(i==sz(s))
    {
        if(m && k==0)
        {
        return 1;
        }
        else
        return 0;
    }

    if(dp[i][k][l][m]!=-1)
    return dp[i][k][l][m];

    int ans = 0;

    if(l)
    {
        for(int digit=0;digit<10;digit++)
        {
            int now = s[i] - '0';

            if(now > digit)
            {
            if(digit > 0)
            ans  = (ans +  sol(i+1,(k+digit)%n,0,1))%mod;
            else
            ans = (ans + sol(i+1,(k+digit)%n,0,m))%mod;
            }
            else if(now == digit)
            {
                if(now == 0)
                ans = (ans + sol(i+1,(k+digit)%n,1,m))%mod;
                else
                ans  = (ans + sol(i+1,(k+digit)%n,1,1))%mod;
            }

          //  deb(i,j,k,l,m,ans,digit);
        }
    }
    else
    {
        for(int digit=0;digit<10;digit++)
        {
            if(digit==0)
            ans  = (ans + sol(i+1,(k+digit)%n,0,m))%mod;
            else
            ans = (ans + sol(i+1,(k+digit)%n,0,1))%mod;

           //  deb(i,j,k,l,m,ans,digit);
        }
    }

    return dp[i][k][l][m]=ans;
}

void solve()
{
  //  cout << "-------------------" << "\n";

    cin>>s>>n;

    memset(dp,-1,sizeof(dp));

    cout<<sol(0,0,1,0);

   // cout << "-------------------" << "\n";

}

int32_t main()
{

    LETS_GET_SCHWIFTY;

    int t = 1;
    //cin >> t;

    while (t--)
        solve();

}

// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values

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

using namespace std;

typedef pair<int, int> p;
typedef long long ll;
const int inf = 1000000007;

int main() {
  ll l, r;
  cin >> l >> r;
  if (r - l + 1 >= 2019) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> a(r - l + 1);
  rep(i, r - l + 1) { a[i] = (l + i) % 2019; }
  ll ans = inf;
  rep(i, a.size() - 1) {
    for (int j = i + 1; j < a.size(); j++) {
      ll tmp = a[i] * a[j] % 2019;
      if (ans > tmp) ans = tmp;
    }
  }
  cout << ans << endl;

  return 0;
}


#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

const long long int MOD = 1000000007;
//const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R;

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

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	cin >> M >> K;
	vector<int>p(N - 1);
	for (int i = 0; i < N - 1; i++)p[i] = i;
	for (int i = 0; i < M; i++) {
		cin >> L;
		swap(p[L - 2], p[L - 1]);
	}
	vector<vector<int>>to(62, vector<int>(N - 1));
	for (int i = 0; i < N - 1; i++)to[0][i] = p[i];
	for (int i = 1; i < 62; i++) {
		for (int j = 0; j < N - 1; j++) {
			to[i][j] = to[i - 1][to[i - 1][j]];
		}
	}
	for (int i = 0; i < N - 1; i++) {
		long long int box = K;
		int cnt = i;
		for (int j = 0; j < 62; j++) {
			if ((box >> j) & 1) {
				cnt = to[j][cnt];
			}
		}
		p[i] = cnt;
	}
	long long int num = v[0];
	cout << num << endl;
	for (auto i : p) {
		//cout << i << endl;
		num += v[i+1]-v[i];
		cout << num << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define DEBUG(a) cerr<<#a<<"="<<a<<endl
#define DEBUG_ARRAY(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(int i = a;i < b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(int i = b-1;i >= a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> P;
typedef pair<ll,P> PP;
typedef vector<ll> vll;

const ll INF=(1LL<<50);

vector<ll> d;
vector<ll> powv(vector<ll> &a){
    vector<ll> ret(a.size());
    REP(a.size()){
        ret[i]=a[a[i]];
    }
    return ret;
}
vector<ll> addv(vector<ll> &a,vector<ll> &b){
    vector<ll> ret(a.size());
    REP(a.size()){
        ret[i]=b[a[i]];
    }
    return ret;
}
void swapv(vector<ll> &a,ll b,ll c){
    swap(a[b],a[c]);
}
void solve(long long N, std::vector<long long> x, long long M, long long K, std::vector<long long> a){
    REP(N-1){
        d.push_back(x[i+1]-x[i]);
    }
    vector<ll> h(N-1);
    iota(h.begin(),h.end(),0);
    REP(M){
        swapv(h,a[i],a[i]-1);
    }
    vector<ll> ans(N-1);
    iota(ans.begin(),ans.end(),0);
    while(K>0){
        if((K&1)>0){
            ans=addv(ans,h);
        }
        K>>=1;
        h=powv(h);
    }
    cout<<x[0]<<endl;
    REP(x.size()-1){
        x[i+1]=x[i]+d[ans[i]];
        cout<<x[i+1]<<endl;
    }
}

int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> x(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&x[i]);
    }
    long long M;
    scanf("%lld",&M);
    long long K;
    scanf("%lld",&K);
    std::vector<long long> a(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&a[i]);
        a[i]--;
    }
    solve(N, std::move(x), M, K, std::move(a));
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstring>
using namespace std;
typedef long long ll;
int a[100100];
int c[100100];
bool check(int x,int i)
{
    if(c[x]<a[i])
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
int bs(int l, int r,int i)
{
    while (l < r)
    {
        int mid = l + r >> 1;
        if (check(mid,i)) r = mid;    // check()判断mid是否满足性质
        else l = mid + 1;
    }
    return l;
}
int main()
{
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    int cnt=1;
    c[1] = a[1];
    for(int i=2;i<=n;i++)
    {
        int pos = bs(1,cnt,i);
        if(c[pos]<a[i])
        {
            c[pos] = a[i];
        }
        else
        {
            cnt++;
            c[cnt] = a[i];
        }

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

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

//PointBlank's code (⌐■_■)

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

ll power(ll x, ll y);
#define repp(I, A, B) for(int I = A; I <= B; I++)
#define rep(I, A, B) for(int I = A; I < B; I++)
#define rrep(I, B, A) for(int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(this) cerr<<"> "<<#this<<" : "<<this<<"\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B), v[B].pb(A);}
#define in_edges2(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B);}
#define in_edges3(M) repp(I, 1, M){int A, B, C; cin >> A >> B >> C; v[A].pb(B, C), v[B].pb(A, C);}
#define endl "\n"

const ll MOD = 1000000007;

int main() //PointBlank's code ¯\_(ツ)_/¯
{
    int n;
    cin >> n;

    multiset<int> st;
    int res = 0;
    int inp;

    rep(i, 0, n){
        cin >> inp;
        auto it = st.UB(-inp);
        if(it == st.end()){
            res++;
            st.insert(-inp);
        }else{
            st.erase(it);
            st.insert(-inp);
        }
    }

    cout << res;
}

ll power(ll x, ll y)
{
    ll res = 1; x %= MOD;
    while(y > 0){if(y & 1) res = (res*x) % MOD; y = y>>1, x = (x*x) % MOD;}
    return res;
}


#include <iostream>
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#include <algorithm>
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define l long
#define mm(arr) memset(arr, 0, sizeof(arr))
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i]}
using namespace std;
int main(){
    FAST
    int n; cin >> n;
    int x;
    int result=0;
    while(n!=0){
        x=n%10;
        if(x==7){
            result=1;
        }
        n=n/10;
    }
    if(result==1){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }

    return 0;
}

#include <iostream>

using namespace std;

int main()
{
string N;
getline(cin,N);

if(N[0]=='7'||N[1]=='7'||N[2]=='7')
{
    cout<<"Yes";

}
else
{
    cout<<"No";
}
return 0;
}


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

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

int gcd(int a, int b) {
  if (a % b == 0) {
    return(b);
  } else {
    return(gcd(b, a % b));
  }
}
int main() {
  ll N, K; cin >> N >> K;
  vector<ll> A(N); REP(i, N) cin >> A[i];
  ll g = A[0]; ll max_n = *max_element(all(A));
  for(int i = 1; i < N; ++i) {
    g = gcd(A[i], g);
  }
  if(K % g == 0 && max_n >= K) cout << "POSSIBLE" << endl;
  else cout << "IMPOSSIBLE" << endl;
}

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

int main() {
  int N,K;
  cin >> N >> K;
  if(N % K == 0)
    cout << 0;
  else
    cout << 1;
}


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

inline namespace atcoder {
    using i32 = int32_t; using u32 = uint32_t;
    using i64 = int64_t; using u64 = uint64_t;
    using f32 = float; using f64 = double;

    template <typename T, size_t Depth>
    struct vector_helper { using type = vector<typename vector_helper<T, Depth - 1>::type>; };
    template <typename T>
    struct vector_helper<T, 0> { using type = T; };
    template <typename T, size_t Depth>
    using vector_t = typename vector_helper<T, Depth>::type;

    template <typename T> using vec = vector_t<T, 1>;
    template <typename T> using vvec = vector_t<T, 2>;
    template <typename T> using vvvec = vector_t<T, 3>;
}

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

    int expect[] = { -1, -1, -1 };
    while (m--) {
        int s, c; cin >> s >> c; --s;
        if (expect[s] != -1 && expect[s] != c) {
            cout << -1 << endl;
            return 0;
        }
        expect[s] = c;
    }

    if (n != 1 && expect[0] == 0) {
        cout << -1 << endl;
        return 0;
    }
    if (n != 1 && expect[0] == -1) {
        expect[0] = 1;
    }
    for (auto &e : expect) e = max(e, 0);

    int ans = 0;
    for (int i = n - 1; 0 <= i; --i) {
        ans += expect[i] * pow(10, n - 1 - i);
    }

    cout << ans << endl;
}

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

int main(){
  vector<int> abcde(5);
  int k;
  for(int i = 0;i < 5;i++) cin >> abcde.at(i);
  cin >> k;

  for(int i = 0;i < 5;i++){
    for(int j = i + 1;j < 5;j++){
      if(k < abcde.at(j) - abcde.at(i)){
        cout << ":(" << endl;
        return 0;
      }
    }
  }
  cout << "Yay!" << endl;

  return 0;
}


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

static const int MOD = 998244353;

long long Inv(long long a) {
        long long res = 1;
        long long n = MOD - 2;
        while (n > 0) {
                if (n & 1) res = res * a % MOD;
                a = a * a % MOD;
                n >>= 1;
        }
        return res;
}
const int N = 5000001;
long long fact[N];
long long invfact[N];
void init() {
        fact[0] = 1;
        for (int i = 1; i < N; i ++) fact[i] = fact[i - 1] * i % MOD;
        for (int i = 0; i < N; i ++) invfact[i] = Inv(fact[i]);
}
long long C(long long n, long long r) {
        if (n < 0 || r < 0 || n < r) return 0;
        return fact[n] * invfact[n - r] % MOD * invfact[r] % MOD;
}

int main() {
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        if (n < m) swap(n, m);
        long long ans = 0;
        for (int i = 1; i <= m; i ++) {
                long long mul = C(2 * i, i) % MOD;
                mul = mul * C(n + m - 2 * i, n - i) % MOD;
                ans = (ans + mul) % MOD;
        }
        ans = ans * Inv(2 * C(n + m, n)) % MOD;
        ans = (ans + n) % MOD;
        printf("%lld\n", ans);
        return 0;
}

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


#include<bits/stdc++.h>
using namespace std;
#define num 1000000007
//#define num 998244353
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define inarr(i,a,n,A) for(ll i=a;i<n;i++) { cin>>A[i]; }
#define vll vector<long long>
#define pii pair <int,int>
#define pll pair<long long, long long>
#define vpii vector< pair <int,int>>
#define vpll vector< pair<long long, long long>>
//#define max(a,b) ((a>b)?a:b)
//#define min(a,b) ((a>b)?b:a)
#define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c)
#define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c)
#define avg(a,b) ((a)+(b))/2
#define pb push_back
#define pob pop_back
#define sf int flag=0     //Remember semicolon
// #define assmin(a,b) if(b<=a) { a=b; }
// #define assmax(a,b) if(b>=a) { a=b; }
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.00000000001)
#define side 1e9
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
ll mod(ll a, ll b)
{
	if(a%b<0)
	{
		return a%b + b;
	}
	return a%b;
}

ll mod_exp(ll a, ll b, ll c)
{
	ll res=1;
	a=a%c;
	while(b>0)
	{
		if(b%2==1)
		{
			res=(res*a)%c;
		}
		b/=2;
		a=(a*a)%c;
	}
	return res;
}
ll mymod(ll a,ll b)
{
    return ((a%b)+b)%b;
}
bool prime[200001];
void SieveOfEratosthenes()
{
    memset(prime, true, sizeof(prime));
    prime[1]=false;
    for (int p=2; p*p<=200000; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=200000; i += p)
                prime[i] = false;
        }
    }
    return;
}
ll powe[200005];
void power()
{
    powe[0]=1;
    REP(i,1,200005)
    {
        powe[i]=mymod(2*powe[i-1],num);
    }
}
ll gcdExtended(ll,ll,ll *,ll *);
ll modInverse(ll a, ll m)
{
    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);
        ll res = (x%m + m) % m;
        return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }
    ll x1, y1;
    ll gcd = gcdExtended(b%a, a, &x1, &y1);
    *x = y1 - (b/a) * x1;
    *y = x1;
    return gcd;
}
struct node
{
	vll adj;
    ll indegree;
};

ll find(ll n, ll wt, ll nm)
{
    ll st = 1;
    ll en = nm;
    ll mid = (st) + (en - st)/2;
    while(st<=en)
    {
        if(wt+3*(nm+mid)>=n && wt+2*(nm+mid)<=n)
        {
            return mid;
        }
        else if(wt+3*(nm+mid)<n)
        {
            st = mid+1;
        }
        else
        {
            en = mid-1;
        }
        mid = (st) + (en - st)/2;
    }
    if(wt+3*(nm+mid)>=n && wt+2*(nm+mid)<=n)
    {
            return mid;
     }
    return 0;
}
bool my(pair<ll, char> p1, pair<ll, char> p2)
{
    return p1.first > p2.first;
}
struct segment
{
    ll l,r,v;
};
bool my(segment a, segment b)
{
    return a.l<b.l;
}
ll convert(string s)
{
    ll n = 0;
    REP(i,0,s.size())
    {
        if(!(n>>(s[i]-97)&1))
        {
            n+=(1<<(s[i]-97));
        }
    }
    return n;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    cin>>s;
    ll n = s.size();
    ll ans = (n*(n+1))/2;
    ll count[26]={0};
    for(ll i=n-1; i>=0; i--)
    {
        count[s[i]-97]++;
        ans-=count[s[i]-97];
    }
    cout<<ans+1<<"\n";
    return 0;
}

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

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

  vector<ll> cnt(26, 0);
  ll n = s.size();
  ll res = n * (n - 1) / 2;
  rep(i,n) cnt[s[i] - 'a']++;
  rep(i,26) res -= cnt[i] * (cnt[i] - 1) / 2;
  cout << res + 1 << endl;
  return 0;
}


#include <bits/stdc++.h>
#include <limits>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define fi(i, n) for( int i=0 ; i<n ; i++ )
#define f(i, a, b) for( int i=a ; i<b ; i++ )
#define vi vector<int>
#define pb push_back
#define MOD 1000000007
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll fact(ll n)
{
    ll ans = 1e14;
    for(ll i = 1; i<=sqrt(n); i++)
    {
        if(n%i==0)
        {
            ll sol = (i - 1) + (n/i - 1);
            ans = min(ans,sol);
        }
    }
    return ans;
}
int visited[200000] = {0};
vector<vector<int> > v(200000);
int dfs(int x)
{
    int ans = 0;
    if(visited[x]==0)
    {
        visited[x] = 1;
        ans++;
        int a = v[x].size();
        for(int j = 0; j<a; j++)
        {
            int data = v[x][j];
            ans = ans + dfs(data);
        }
    }
    return ans;
}
void primefactors(int n)
{
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";

}
double fib(int n)
{
    double sol1,sol2;
    sol1 = (1 + sqrt(5))/2;
    sol2 = (1 - sqrt(5))/2;
    sol1 = pow(sol1,n);
    sol2 = pow(sol2,n);
    double ans = sol1 - sol2;
    ans = ans/sqrt(5);
    //ans = int(ans);
    return ans;
}

ll digits(ll n)
{
    return floor(log10(double(n))) + 1;
}

int main()
{

    string s,t;
    cin>>s>>t;
    cout<<t + s;
}

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>

using namespace std;

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

	for (int i = 0; i < S.length(); i++) {
		if (i % w == 0) {
			cout << S[i];
		}
	}
	cout << endl;

}


//
//  nomura_a.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
ll mod = 998244353;
ll INF = 1000000007;
#define PI 3.14159265

pair<int, int> dijsktra(int x, char des, int n, vector<pair<int, char> > *adj) {
  priority_queue<pair<pair<int, char>, int> >  q; // (-d, x) current distance to node x is d, using -d to find minimum distance
  bool processed[n];
  pair<int, char> distance[n];
  for (int i=0; i<n; i++) {
    distance[i].first = INF;
    distance[i].second = '0';
    processed[i] = false;
  }
  // find from x
  distance[x] = make_pair(0, '0');
  q.push({distance[x], x});

  while (!q.empty()) {
    int a = q.top().second;
    q.pop();
    if (processed[a]) continue;;
    processed[a] = true;
    for (auto u: adj[a]) {
      int b = u.first; // node b
      char label = u.second;
      int w = 1; // distance from a to b is w
      if (distance[b].first > distance[a].first + w) {
        distance[b] = make_pair(distance[a].first + w, label);
        q.push({{-distance[b].first, label}, b});
      }
    }
  }

  int l = INF;
  int end = 0;
  // std::cout << "distance from " << x << std::endl;
  for (int i = 0; i < n; i++) {
    if (distance[i].first==INF) continue;
    if (distance[i].second == des && distance[i].first < l) {
      l = distance[i].first;
      end = i;
    }
    // std::cout << " distance to " << i << ": " << distance[i] << std::endl;
  }

  return make_pair(l, end);
}

int main() {
  int h1, m1, h2, m2, k;
  cin >> h1 >> m1 >> h2 >> m2 >> k;

  std::cout << h2*60+m2 - (h1*60+m1) - k << std::endl;

  return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <stdio.h>
#include <unistd.h>
using namespace std;
typedef long long int ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define drep(i, n) for(int i = n - 1; i >= 0; i--)
#define itrep(itr, base) for (auto itr = base.begin(); itr != base.end(); itr++)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl
#define PI 3.14159265359

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

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 h[2], m[2], k;
  cin >>  h[0] >> m[0] >> h[1] >> m[1] >> k;
  int up   = h[0] * 60 + m[0];
  int down = h[1] * 60 + m[1];
  if (down - up - k < 0) {
    P(0);
  } else {
    P(down - up - k);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod=1000000007;
const int INF=1001001001;

vector<pair<int64_t,int64_t>>prime_factorize(int64_t x){
  vector<pair<int64_t,int64_t>>p;
  for(int64_t i=2;i*i<=x;i++){
    int cnt=0;
    if(x%i==0){
    while(x%i==0){cnt++;x/=i;}
    p.push_back(make_pair(i,cnt));
    }
  }
  if(x!=1){p.push_back(make_pair(x,1));}
  return p;
}

int main() {
  int A,B,C,D;
  cin>>A>>B>>C>>D;
  if(A+B==C+D){cout<<"Balanced"<<endl;}
  else if(A+B<C+D){cout<<"Right"<<endl;}
  else{cout<<"Left"<<endl;}
  return 0;
}

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

int main() {
int a,b,c,d;
  cin >> a >> b >> c >> d;
  int L=a+b;
  int R=c+d;
  if(L>R){
    cout << "Left" << endl;
  }
    if(L<R){
      cout << "Right" << endl;
    }
  if(L==R){
    cout << "Balanced" << endl;
  }
}


/*
      author : nishi5451
      created: 11.08.2020 00:36:47
*/

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

int main(){
    string a,b,c;
    cin >> a >> b >> c;
    if(*a.rbegin()==*b.begin() && *b.rbegin()==*c.begin()){
        cout << "YES" << endl;
    }
    else cout << "NO" << endl;
    return 0;
}

#include <stdio.h>

int main () {
	char a[12];
	char b[12];
	char c[12];
	int lenA = 0, lenB = 0;
	scanf("%s", a);
	scanf("%s", b);
	scanf("%s", c);

	for(int i = 0; a[i] != '\0'; i++) {
		lenA++;
	}
	for(int i = 0; b[i] != '\0'; i++) {
		lenB++;
	}

	if (a[lenA-1] == b [0] && b[lenB-1] == c[0]) {
		printf("YES\n");
	}
	else {
		printf("NO\n");
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <queue>
using namespace std;
using P=pair<int,int>;
using PP=pair<P,int>;
const int INF=INT_MAX;

/* RMQ：[0,n-1] について、区間ごとの最小値を管理する構造体
    update(i,x): i 番目の要素を x に更新。O(log(n))
    query(a,b): [a,b) での最小の要素を取得。O(log(n))
*/
template <typename T>
struct RMQ {
    const T INF = numeric_limits<T>::max();
    int n;         // 葉の数
    vector<T> dat; // 完全二分木の配列
    RMQ(int n_) : n(), dat(n_ * 4, INF) { // 葉の数は 2^x の形
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
    void update(int i, T x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;  // parent
            dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
    // the minimum element of [a,b)
    T find(int a, int b) { return query_sub(a, b, 0, 0, n); }
    T query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return INF;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return min(vl, vr);
        }
    }
};
signed main(){
    int n,q;
    cin>>n>>q;
    priority_queue<pair<int,P>,vector<pair<int,P>>,greater<>> c;
    for(int i=0;i<n;i++){
        int s,t,x;
        cin>>s>>t>>x;
        c.push(make_pair(s-x,P(x,i)));
        c.push(make_pair(t-x,P(x,i)));
    }
    vector<int> d(q);
    for(int i=0;i<q;i++){
        cin>>d[i];
    }
    RMQ<int> r(n);
    for(int i=0;i<q;i++){
        queue<pair<int,P>> task;
        while(!c.empty()&&c.top().first<=d[i]){
            int p=c.top().second.first,idx=c.top().second.second;
            //cout<<c.top().first<<" "<<p<<" "<<idx<<endl;
            if(r.find(idx,idx+1)==INF){
                r.update(idx,p);
            }else r.update(idx,INF);
            c.pop();
        }
        if(r.find(0,n)==INF)cout<<-1<<endl;
        else cout<<r.find(0,n)<<endl;
    }
}

#include <iostream>

char jukuClass(int, int, int);
int main()
{
    while (true) {
        int n;
        std::cin >> n;
        if (n == 0) {
            break;
        }
        for (int i = 0; i < n; ++i) {
            int pm, pe, pj;
            std::cin >> pm >> pe >> pj;
            std::cout << jukuClass(pm, pe, pj) << std::endl;
        }
    }
    return 0;
}
char jukuClass(int pm, int pe, int pj)
{
    if (pm == 100 || pe == 100 || pj == 100) {
        return 'A';
    } else if ((pm + pe) / 2.0 >= 90) {
        return 'A';
    } else if ((pm + pe + pj) / 3.0 >= 80) {
        return 'A';
    } else if ((pm + pe + pj) / 3.0 >= 70) {
        return 'B';
    } else if (((pm + pe + pj) / 3.0 >= 50) && (pm >= 80 || pe >= 80)) {
        return 'B';
    } else {
        return 'C';
    }
}


#include<iostream>
#include<algorithm>
#include<functional>
#define MAX 1000

int main(){
	int n,m,p[MAX];
	while(1){
		int f=0,m1=0;
		std::cin>>n>>m;
		if(n==0&&m==0)
			break;
		for(int i=0;i<n;++i){
			std::cin>>p[i];
		    m1+=p[i];
		}
		std::sort(p,p+n,std::greater<int>());
		int c=n%m;
		for(int i=0;i<n-c;++i){
			if((i+1)%m==0)
				f+=p[i];
		}
		std::cout<<m1-f<<std::endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repr(i,a,n) for(int i=a;i>=n;i--)
#define INF 999999999
#define INF_M 2147483647
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int main() {
    string s;
    while(getline(cin,s)) {
		string t;
		rep(j,0,26) {
			t.clear();
			rep(i,0,s.length()) {
				if('a' <= s[i] && s[i] <= 'z') t += (s[i] + j > 'z') ? (s[i] + j - 26) : (s[i] + j);
				else t += s[i];
			}
			if(t.find("this") != string::npos) break;
			else if(t.find("the") != string::npos) break;
			else if(t.find("that") != string::npos) break;
		}
		cout << t << endl;
	}
    return 0;
}


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

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> wa(N + 5, 0);
    for (int i = 0; i < M; i++) {
        int p;
        string s;
        cin >> p >> s;
        if (wa[p] >= 0 && s == "WA") wa[p]++;
        else if (wa[p] >= 0 && s == "AC") wa[p] = wa[p] * (-1) - 1;
    }
    int ac = 0, penalty = 0;
    for (auto a: wa){
        if (a < 0) {
            penalty += (a + 1) * (-1);
            ac++;
        }
    }
    cout << ac << " " << penalty << endl;
    return 0;
}

#include <bits/stdc++.h>

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

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> wa(n), al(n);
  int ac = 0, pe = 0;
  while (m--) {
    int p;
    string s;
    cin >> p >> s;
    p--;
    if (s == "WA") {
      if (al[p] == 0) wa[p]++;
    } else {
      if (al[p] == 0) {
        pe += wa[p];
        ac++;
      }
      al[p] = 1;
    }
  }
  cout << ac << ' ' << pe;
  return 0;
}


//
//  main.cpp
//  Atcoder1
//
//  Created by Hamske on 2020/09/17.
//  Copyright © 2020 Author. All rights reserved.
//

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

using namespace std;

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

typedef long long ll;

#define rept(_i,iv,n,type) for(type _i=(type)(iv);_i<(type)(n);++_i)
//#define repi(_i,iv,n) rept(_i,iv,n,int)
//#define repl(_i,iv,n) rept(_i,iv,n,ll)

#define rept0(_i,n,type) rept(_i,0,n,type)
#define repi0(_i,n) rept(_i,0,n,int)
#define repl0(_i,n) rept(_i,0,n,ll)

//#define repst(_i,iv,n,type) for(type _i=iv;_i<=(type)(n);++_i)
//#define repsi1(_i,n) repst(_i,1,n,int)

#define REPT(_i,iv,_l,n,type) for(type _i=(type)(iv),_l=(type)(n);_i<_l;++_i)
//#define REPI(_i,iv,_l,n) REPT(_i,iv,_l,n,int)
//#define REPL(_i,iv,_l,n) REPT(_i,iv,_l,n,ll)
//
#define REPT0(_i,_l,n,type) REPT(_i,0,_l,n,type)
#define REPI0(_i,_l,n) REPT(_i,0,_l,n,int)
#define REPL0(_i,_l,n) REPT(_i,0,_l,n,ll)

//#define rrept(_i,x,e,type) for(type _i=((type)(x)-1);_i>=e;--_i)
//
//#define rrept0(_i,x,type) rrept(_i,x,0,type)
//
//#define RREPT(_i,x,_e,e,type) for(type _i=((type)(x)-1),_e=(type)(e);_i>=_e;--_i)
//
//#define RREPT0(_i,x,_e,type) RREPT(_i,x,_e,0,type)
//
//#define RREPS(i,x) for(int i=((int)(x));i>0;i--)

//#define all(x) (x).begin(),(x).end()
#define REPV(v,_itr,_end) for(auto _itr=(v).begin(),_end=(v).end();_itr!=_end;++_itr)
#define REPVC(v,_itr,_end) for(auto _itr=(v).cbegin(),_end=(v).cend();_itr!=_end;++_itr)

#define td typedef
//#define tds(stl,type,name) typedef stl<type > name;
#define tdv(type,name) typedef vector<type > name;

//typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<vb > vvb;
typedef vector<int> vi;
typedef vector<vi > vvi;
//typedef vector<ll> vll;
//typedef vector<vll > vvll;

#define MOD7 1'000'000'007
#define MOD9 1'000'000'009

int main(int argc, const char * argv[]) {
    int N;
    cin>>N;
    vi A(N);
    REPV(A, itr, end){
        cin>>(*itr);
    }
    ll sum=0;
    int bh=0;
    repi0(i, N){
        if(bh<=A[i]){
            bh=A[i];
        }else{
            sum+=bh-A[i];
        }
    }
    cout<<sum<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,s,n) for (int i=s;i<n;++i)
#define drep(i,s,n) for (int i=n;i>=s;--i)
#define all(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define print(x) cout<<(x)<<endl
using namespace std;
typedef long long ll;

ll gcd(ll a,ll b)
{if (a%b == 0) return(b);
 else          return(gcd(b,a%b));}
ll lcm(ll a,ll b)
{return a/gcd(a,b)*b;}
void printv(vector<auto> v) {cout<<'[';rep(i,0,v.size()){cout<<v[i]<<',';}cout<<']'<<endl;}
void printvv(vector<vector<auto>> vv)
{cout<<'['<<endl;
  rep(q,0,vv.size()){cout<<'[';rep(p,0,vv.at(q).size())cout<<vv.at(q).at(p)<<',';cout <<']'<<endl;}
  cout<<']'<<endl;}
void solve(); int main(){cin.tie(0);ios::sync_with_stdio(0);solve();}
int step_x[] = {-1,0,0,1};
int step_y[] = {0,-1,1,0};
ll INF=1e18;
//------------------------------------------------------------
int N;
int nums[1100];
ll ans=1;

void solve() {
  cin>>N;

  rep(i,1,N)
  {
    int num = i+1;
    for(int j=2*num;j<=N;j+=num) nums[j]=1;
  }

  rep(i,1,N)
  {
    int num = i+1;
    if(nums[num]==0)
    {
      ll tmp=0;
      for(int j=num; j<=N; j*=num) tmp+=N/j;
      // tmp+=N/num;
      ans = ans*(tmp+1)%(int)(1e9+7);
    }
  }

  print(ans);
  return;
}


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

int main(){
  long long N,a,c=1,A[100002]={};A[0]=3;
  cin>>N;
  for(int i=0;i<N;i++){
    scanf("%d",&a);
    c*=(A[a]-A[a+1]++);
    c%=1000000007;
  }
  cout<<c<<endl;
}

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

int main() {
  int MOD=1000000007;
  int N;cin>>N;
  vector<int> A(N);
  for(int i=0;i<N;i++)cin>>A[i];
  int a=0,b=0,c=0;
  long ans=1;
  for(int i=0;i<N;i++){
    int m=0;
    if(A[i]==a)m++;
    if(A[i]==b)m++;
    if(A[i]==c)m++;
    ans=(ans*m)%MOD;
    if(A[i]==a)a++;
    else if(A[i]==b)b++;
    else if(A[i]==c)c++;
  }
  cout<<ans<<endl;

}


/**
*    created: 03.07.2020 16:00:37
**/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b; return 1;} return 0;}
const long double pi = acos(-1.0);
const int INF = 1LL << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

signed main() {

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

    int a, b;
    cin >> a >> b;
    cout << 2 * b - a << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define fi first
#define se second
#define pb push_back

const int N = 202;
int a[N];

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, l;
    scanf("%d %d", &n, &l);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
		sum += l + i - 1;
	}
	int res, mn = 1e9, tot = sum;
	for (int i = 1; i <= n; i++) {
		tot -= l + i - 1;
		if (abs(tot - sum) < mn) {
			mn = abs(tot - sum);
			res = tot;
		}
		tot += l + i - 1;
	}
	printf("%d", res);
    return 0;
}