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

using ll = long long;

using namespace std;

// xorの掃き出し法をするためのクラス
// matに基底たちが入る
// Tはbitsetは無理(minしないといけないから)
template <typename T>
class BitLinearSpace {
    vector<T> bases; // この中身をsortしちゃうと壊れる

public:
    BitLinearSpace() = default;
    BitLinearSpace(vector<T> &matrix) {
        for (auto &mi:matrix) {
            add_val(mi, false);
        }
    }

    // valが張れるように基底を追加する
    // 追加した場合true, しなかった場合false
    // O(N) (ソートするとNlogN)
    // 追加後にソートするかを選ぶ
    bool add_val(T val) {
        if ((val = elementary_opperation(val))) {
            bases.push_back(val);
            return true;
        } else {
            return false;
        }
    }

    // 今のベクトル空間にvalが含まれるか(=basesの要素のxorでvalが作れるかをチェック)
    // O(n)
    bool includes(T val) {
        return elementary_opperation(val) == 0;
    }

    // 行列にvalを追加するていで行基本変形をvalに施す
    // 実際には追加しない
    // O(n)
    T elementary_opperation(T val) {
        for (auto &b:bases) {
            val = min(val, val ^ b);
        }
        return val;
    }

    T &operator[](int i) { return bases[(size_t)i]; }
    const T &operator[](int i) const { return bases[(size_t)i]; }

    auto begin() { return bases.begin(); }
    auto end() { return bases.end(); }
    auto begin() const { return bases.begin(); }
    auto end() const { return bases.end(); }

    int size() { return (int)bases.size(); }
};

int main() {
    int n;
    cin >> n;
    BitLinearSpace<ll> bls;
    ll sum = 0;
    for (int i=0; i<n; i++) {
        ll ai; cin >> ai;
        sum ^= ai;
        bls.add_val(ai);
    }
    vector<ll> bases(bls.begin(), bls.end());
    ll blue = 0, red = sum;
    for (int i=59; i>=0; i--) {
        if ((blue & (1ll << i)) == (red & (1ll << i))) {
            ll used = -1;
            for (auto &bi:bases) {
                if (bi & (1ll << i)) {
                    if (used < 0) {
                        if ((blue & (1ll << i)) == 0)
                            blue ^= bi;
                        used = bi;
                    }
                    bi ^= used;
                }
            }
        }
        red = blue ^ sum;
    }
    cout << blue + red << '\n';
    return 0;
}

#include <iostream>
#include <algorithm>

std::vector<size_t> gaps = {1};

template <typename T>
size_t insertion_sort(T a[], size_t n, size_t gap = 1){
  size_t count = 0;
  for(size_t i = gap; i < n; i++){
    T v = a[i];
    int j = i - gap;
    while(j >= 0 && a[j] > v){
      a[j+gap] = a[j];
      j -= gap;
      count++;
    }
    a[j+gap] = v;
  }
  return count;
}

void make_gap_table(size_t n){
  size_t h = 4;
  while(h < n){
    gaps.push_back(h);
    h = 3*h + 1;
  }
}

template <typename T>
void shell_sort(T a[], size_t n){
  make_gap_table(n);
  size_t count = 0;
  for(auto it = gaps.rbegin(); it != gaps.rend(); it++){
    count += insertion_sort(a,n,*it);
  }

  std::cout << gaps.size() << '\n';

  for(auto it = gaps.rbegin(); it != std::prev(gaps.rend()); it++){
    std::cout << *it << ' ';
  }
  std::cout << *(std::prev(gaps.rend())) << '\n';

  std::cout << count << '\n';
}

int main(){
  size_t n;
  size_t buf;
  std::vector<size_t> a;
  std::cin >> n;
  for(size_t i = 0; i < n; i++){
    std::cin >> buf;
    a.push_back(buf);
  }

  shell_sort(a.data(),a.size());
  for(auto &v : a){
    std::cout << v << '\n';
  }
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1001001001
#define LINF 1001001001001001001
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define bit(n) (1LL<<(n))
using namespace std;

typedef pair<int, int> P;
typedef pair<long long, long long> LLP;
typedef long long ll;

class Dijkstra {
    private:
        vector< vector<LLP> > G;
        vector<long long> cost;
        vector<long long> par;
        priority_queue<LLP, vector<LLP>, greater<LLP> > que;

    public:
        Dijkstra(long long n, vector< vector<long long> > g, long long s);
        long long getCost(long long target);
        vector< vector<long long> > getPath(long long target);
};

// n: |V|
// g: [point_from, point_to, cost] (directed graph)
// s: start point
Dijkstra::Dijkstra(long long n, vector< vector<long long> > g, long long s) {
    for (long long i = 0; i < n; i++) {
        G.push_back(vector<LLP>());
        cost.push_back(-1);
        par.push_back(-1);
    }

    // create adj-Graph
    for (long long i = 0; i < g.size(); i++) {
        G[g[i][0]].push_back(make_pair(g[i][1], g[i][2]));
    }

    // initialize
    cost[s] = 0;
    par[s] = s;
    que.push(make_pair(cost[s], s));

    while (!que.empty()) {
        LLP p = que.top();
        que.pop();

        if (cost[p.second] < p.first) continue;
        for (int i = 0; i < G[p.second].size(); i++) {
            if (cost[G[p.second][i].first] < 0 || cost[G[p.second][i].first] > cost[p.second] + G[p.second][i].second) {
                cost[G[p.second][i].first] = cost[p.second] + G[p.second][i].second;
                par[G[p.second][i].first] = p.second;
                que.push(make_pair(cost[G[p.second][i].first], G[p.second][i].first));
            }
        }
    }
}

// returns the minimum cost from the start point to the target.
// (if there is no path to the target, this returns -1.)
// (if the target is the start point, this returns 0.)
long long Dijkstra::getCost(long long target) {
    return cost[target];
}

// returns a list of path: Array of [point_from, point_to, cost].
// (if there is no path to the target or the target is the start point, this returns an empty vector.)
vector< vector<long long> > Dijkstra::getPath(long long target) {

    vector< vector<long long> > reversePath = vector< vector<long long> >(0);

    if (cost[target] <= 0) return reversePath;

    long long now_p = target;
    while (par[now_p] != now_p) {
        vector<long long> temp(3);
        temp[0] = par[now_p];
        temp[1] = now_p;
        temp[2] = cost[now_p] - cost[par[now_p]];
        reversePath.push_back(temp);
        now_p = par[now_p];
    }

    vector< vector<long long> > path = vector< vector<long long> >(0);
    for (long long i = reversePath.size() - 1; i >= 0; i--) path.push_back(reversePath[i]);

    return path;
}

int main() {

    int N, Q, K;
    cin >> N;
    vector<vector<ll>> g;
    rep(i,N-1) {
        vector<ll> e(3);
        cin >> e[0] >> e[1] >> e[2];
        e[0]--;
        e[1]--;
        g.push_back(e);
        swap(e[0], e[1]);
        g.push_back(e);
    }

    cin >> Q >> K;
    K--;
    Dijkstra tree(N, g, K);

    rep(i,Q) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        cout << tree.getCost(x) + tree.getCost(y) << endl;
    }

    return 0;
}

#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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; }

int dp[2][6005];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, t;
	cin >> n >> t;
	vector<P> v(n);
	rep(i,n) cin >> v[i].first >> v[i].second;
	sort(v.begin(), v.end());
	rep(i,n){
		int a = v[i].first;
		int b = v[i].second;
		rep(j,6001){
			chmax(dp[(i+1)%2][j], dp[i%2][j]);
			if(j < t) chmax(dp[(i+1)%2][j+a], dp[i%2][j] + b);
		}
	}
	int ans = 0;
	rep(j,6001) chmax(ans, dp[n%2][j]);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <cstdlib>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) {
	return max(max(a, b), c);
}
ll Min(ll(a), ll(b), ll(c)) {
	return min(min(a, b), c);
}
ll gcd(ll(a), ll(b)) {
	ll c = a;
	while (a % b != 0) {
		c = a % b;
		a = b;
		b = c;
	}
	return b;
}

int main() {
	ll N,K,G,M;
	cin >> N >> K;
	ll A;
	cin >> A;
	G = A;
	M = A;
	string an = "POSSIBLE";
	rep(i, N-1) {
		cin >> A;
		G = gcd(G, A);
		M = max(M, A);
	}
	if (K % G != 0 || K > M) {
		an = "IM" + an;
	}
	cout <<an << endl;
}

#include <bits/stdc++.h>

///    ____ ____ ____ ____ ____
///   ||a |||t |||o |||d |||o ||
///   ||__|||__|||__|||__|||__||
///   |/__\|/__\|/__\|/__\|/__\|
///

using namespace std;

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

int n, k;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    int g = 0;
    int mi = INT_MAX, mx = INT_MIN;
    for(int i = 1; i <= n; i++)
    {
        int val;
        cin >> val;
        g = gcd(g, val);
        mi = min(mi, val);
        mx = max(mx, val);
    }
    if(k % g == 0 && mi <= k && k <= mx)
        cout << "POSSIBLE\n";
    else
        cout << "IMPOSSIBLE\n";
    return 0;
}


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

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

const ll Inf = 1000000000000000000ll;
const int Maxn = 200005;
const int Maxm = 1048576;

struct node {
    ll add;
    lli mn;
};

int n, D;
int a[2][Maxn];
node st[2][Maxm];
map <int, ll> M[2];
ll res;

void downOn(int s, int v, ll fl)
{
    st[s][v].add += fl;
    st[s][v].mn.first += fl;
}

void Down(int s, int v)
{
    if (st[s][v].add) {
        downOn(s, 2 * v, st[s][v].add);
        downOn(s, 2 * v + 1, st[s][v].add);
        st[s][v].add = 0;
    }
}

void Union(int s, int v)
{
    st[s][v].mn = min(st[s][2 * v].mn, st[s][2 * v + 1].mn);
}

void Create(int s, int v, int l, int r)
{
    st[s][v].add = 0;
    if (l == r) {
        st[s][v].mn.first = a[s][l] - ll(l) * D;
        st[s][v].mn.second = l;
    } else {
        int m = l + r >> 1;
        Create(s, 2 * v, l, m);
        Create(s, 2 * v + 1, m + 1, r);
        Union(s, v);
    }
}

void Update(int s, int v, int l, int r, int a, int b, ll add)
{
    if (l == a && r == b) downOn(s, v, add);
    else {
        Down(s, v);
        int m = l + r >> 1;
        if (a <= m) Update(s, 2 * v, l, m, a, min(m, b), add);
        if (m + 1 <= b) Update(s, 2 * v + 1, m + 1, r, max(m + 1, a), b, add);
        Union(s, v);
    }
}

void Insert(int s, int x, ll val)
{
    map <int, ll>::iterator it = M[s].upper_bound(x);
    if (it->second <= val) return;
    ll oldval = it->second;
    int cur = x;
    it--;
    Update(s, 1, 0, n - 1, it->first + 1, cur, val - oldval);
    while (val <= it->second) {
        cur = it->first;
        oldval = it->second;
        M[s].erase(it--);
        Update(s, 1, 0, n - 1, it->first + 1, cur, val - oldval);
    }
    M[s][x] = val;
}

void Init()
{
    for (int s = 0; s < 2; s++) {
        Create(s, 1, 0, n - 1);
        M[s][-1] = -Inf; M[s][n] = Inf;
        Update(s, 1, 0, n - 1, 0, n - 1, Inf);
    }
}

int main()
{
    scanf("%d %d", &n, &D);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[0][i]);
        a[1][n - 1 - i] = a[0][i];
    }
    Init();
    int v = 0;
    for (int i = 0; i < n - 1; i++) {
        int ov = n - 1 - v;
        Update(0, 1, 0, n - 1, v, v, Inf);
        Update(1, 1, 0, n - 1, ov, ov, Inf);
        ll myval = a[0][v] + ll(v) * D;
        Insert(0, v, myval);
        ll omyval = a[1][ov] + ll(ov) * D;
        Insert(1, ov, omyval);
        lli A = st[0][1].mn;
        lli B = st[1][1].mn;
        if (A <= B) { res += A.first; v = A.second; }
        else { res += B.first; v = n - 1 - B.second; }
    }
    cout << res << endl;
    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;int n,d,a[220001],b[220001];main(){cin>>n>>d;for(int i=1;i<=n;i++)cin>>a[i],b[i]=a[i];for(int i=2;i<=n;i++)b[i]=min(b[i],b[i-1]+d);for(int i=n-1;i;i--)b[i]=min(b[i],b[i+1]+d);int pos=min_element(a+1,a+1+n)-a,ans=0;for(int i=pos-1;i;i--)ans+=a[i]+b[i+1]+d;for(int i=pos+1;i<=n;i++)ans+=a[i]+b[i-1]+d;cout<<ans<<'\n';}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
#define N 1100005
struct node
{
	int st,ed,cnt;
	char c;
}a[N];
int c[N];
int n,z;
char ch[N],cc[N];
queue<int>q;
int main()
{
	//freopen("copy.in","r",stdin);
	//freopen("copy.out","w",stdout);
	scanf("%d%s%s",&n,ch+1,cc+1);
	bool flag=true;
	for(int i=1;i<=n;i++)
	{
		if(ch[i]!=cc[i])
		{
			flag=false;
			break;
		}
	}
	if(ch[1]!=cc[1])
	{
		puts("-1");return 0;
	}
	if(flag)
	{
		puts("0");return 0;
	}
	for(int i=1;i<=n;i++)
	{
		int j=i;
		while(j<=n&&cc[j]==cc[i])j++;
		a[++z].st=i;a[z].ed=j;
		if(cc[j]!=cc[i])a[z].ed--;
		a[z].c=cc[i];
		i=a[z].ed;
	}
	int la=n,ans=0;
	for(int i=z;i;i--)
	{
		la=min(la,a[i].st);
		while(la&&a[i].c!=ch[la])la--;
		if(la==0)
		{
			puts("-1");return 0;
		}
		while(!q.empty()&&q.front()-q.size()>=a[i].ed)q.pop();
		q.push(la);
		int te=q.size();
		ans=max(ans,te);
	}
	printf("%d\n",ans);
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9;
using namespace std;
using ll = long long;
int main() {
    int x , y;
    cin >> x >> y;
    int t = abs(abs(x)-abs(y));
    if((x > 0 && y < 0) || (x < 0 && y > 0)){
        t++;
    }
    else if((x == 0 || y == 0) && y < x){
        t++;
    }
    else if (y < x){
        t+=2;
    }
    cout << t << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
#define Priq priority_queue<int,vi,greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll<<60)
signed main(){
  int N; cin>>N;
  vi A(N);
  rep(i,0,N) cin>>A[i];
  vi dp(N+1);
  rep(i,0,N) dp[i+1]=dp[i]+A[i];
  int ans=INF;
  REP(i,2,N){
    int X=disup(dp,dp[i]/2),Y=disup(dp,(dp[i]+dp[N])/2);
    rep(j,0,(1ll<<2)){
      bitset<2> B(j);
      int P=X,Q=Y;
      if(B.test(0)) P--;
      if(B.test(1)) Q--;
      vi C(4);
      C[0]=dp[P];
      C[1]=dp[i]-dp[P];
      C[2]=dp[Q]-dp[i];
      C[3]=dp[N]-dp[Q];
      sort(ALL(C));
      if(C[0]>0) ans=min(ans,C[3]-C[0]);
    }
  }
  cout<<ans<<endl;
  }


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

int main(){

    int l,r;
    cin >> l >> r;
    const int num = 2019;
    int comp = INT_MAX;
    long long ans = 0;
    for(long long i=l; i<=r; i++){
        for(long long j=l; j<i; j++){
            ans = ((i%num) * (j%num))%num;
            if(ans < comp){
                comp = ans;
            }
            else if(ans == 0){
                cout << ans << endl;
                return 0;
            }
        }
    }
    cout << comp << endl;
}

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

int main() {
  ll L, R;
  cin >> L >> R;

  if (R - L >= 2019) {
    cout << 0 << endl;
  } else {
    ll ans = 2019;
    for (ll i = L; i < R; i++) {
      for (ll j = i + 1; j <= R; j++) {
        ans = min(ans, (i * j) % 2019);
      }
    }
    cout << ans << endl;
  }
}


#include <cstdio>
using namespace std;
int main()
{
    int n;
    bool prime[10001];
    for (int i=0; i<=10000; i++) prime[i]=true;
    prime[0]=false;
    prime[1]=false;
    for (int i=2; i<=10000; i++) {
        if (!prime[i]) continue;
        for (int j=i+i;j<=10000; j+=i) prime[j]=false;
    }
    while (scanf("%d",&n)&&n!=0) {
        int p1=0, p2=0;
        for (int i=2; i+2<=n; i++) {
            if (!prime[i]) continue;
            if (prime[i]&&prime[i+2]) {
                p1=i;
                p2=i+2;
            }
        }
        printf("%d %d\n",p1,p2);
    }
}

#include <cstdio>
#include <cmath>
#define buf 10000
using namespace std;
int k=0;
int B[buf];
void Answer(int n);
int PrimeNumber(int n);

int main(void){
  int i,n;
  char str[buf];

  for(i=2;i<buf;i++)
    PrimeNumber(i);
  for( ;fgets(str,sizeof(str),stdin)!=NULL; ){
    sscanf(str,"%d",&n);
    if(n==0) break;
    Answer(n);
  }
  return 0;
}

void Answer(int n){
  int i,j;
  for(i=0;i<k;i++){
    if(B[i]>n)
        break;
  }
  for(j=i-1;j>=0;j--){
    if(B[j]-B[j-1]==2){
      printf("%d %d\n",B[j-1],B[j]);
      break;
    }
  }
}

int PrimeNumber(int n){
  int i,c;
  if(n==2){
    B[k]=n;
    k++; return 0;
  }
  if(n<2 || n%2==0)
    return 0;
  c=sqrt(n);
  for(i=3;i<=c;i+=2){
    if(n%i==0)
      return 0;
  }
  B[k]=n;
  k++; return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1e9+7;
void add(ll &a, ll b)
{
    a = (a + b) % MOD;
}
int main()
{
    ll n, m;
    cin >> n >> m;
    vector<ll> x(n);
    vector<ll> y(m);
    for(int i = 0; i < n; i++)
    {
        cin >> x[i];
    }
    for(int i = 0; i < m; i++)
    {
        cin >> y[i];
    }

    ll xsum = 0;
    ll ex = 0;
    for(int i = 1; i < n; i++)
    {
        ll cand = x[i] - x[i-1];
        add(ex, i * cand);
        add(xsum, ex);
    }
    ll ysum = 0;
    ll ey = 0;
    for(int i = 1; i < m; i++)
    {
        ll cand = y[i] - y[i-1];
        add(ey, i * cand);
        add(ysum, ey);
    }

    cout << xsum * ysum % MOD << endl;
}

const int LG = 21;
const int FN = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
string Yes[2] = {"No", "Yes"};
string YES[2] = {"NO", "YES"};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(FN);
    __ufact.resize(FN);
    __rev.resize(FN);
    __rev[1] = 1;
    for (int i = 2; i < FN; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < FN; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (k < 0 || k > n) return 0;
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int Root(int x, vector<int> &root)
{
    if (x == root[x]) return x;
    return root[x] = Root(root[x], root);
}

void Merge(int v, int u, vector<int> &root, vector<int> &sz)
{
    v = Root(v, root), u = Root(u, root);
    if (v == u) return;
    if (sz[v] < sz[u])
    {
        sz[u] += sz[v];
        root[v] = u;
    }
    else
    {
        sz[v] += sz[u];
        root[u] = v;
    }
}

int ok(int x, int n)
{
    return 0 <= x && x < n;
}

void bfs(int v, vi &dist, vector<vi> &graph)
{
    fill(all(dist), -1);
    dist[v] = 0;
    vi q = {v};
    for (int i = 0; i < q.size(); i++)
    {
        for (auto u : graph[q[i]])
        {
            if (dist[u] == -1)
            {
                dist[u] = dist[q[i]] + 1;
                q.push_back(u);
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int k, n;
    cin >> k >> n;
    vi a(n);
    forn(i, n) cin >> a[i];
    int x = *max_element(all(a));
    for (int bd = 0; bd <= 30000; bd++)
    {
        int nx = x - bd;
        int ny = k - bd;
        if (nx <= (ny + 1) / 2) cout << bd, exit(0);
    }
}

/* Note:
Check constants at the beginning of the code.
    N is set to 4e5 but be careful in problems with large constant factor.
    Setting N in every problem is more effective.
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/


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

int main() {
  ll N,K;
  cin>>N>>K;
  vector<ll> vec(N);
  for(ll i=0;i<N;i++) {
cin>>vec[i];
  }
  ll ans=0;

  for(ll i=1;i<=min(N,K);i++) {

    for(ll j=0;j<=i;j++) {
      ll sum=0;
      priority_queue<ll> S;
      ll count=0;
      while(count<j) {
        sum+=vec[count];
        if(vec[count]<0) {
S.push(vec[count]*-1);
        }
        count++;
      }

      for(ll h=0;h<i-j;h++) {
sum+=vec[N-1-h];
        if(vec[N-1-h]<0) {
S.push(vec[N-1-h]*-1);
        }
      }

      ll s=i;
      while(s<K) {
        if(S.empty()) {
break;
        }
sum+=S.top();
        S.pop();

        s++;
      }

      ans=max(ans,sum);
    }
  }

    cout<<ans<<endl;
  }

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

//#include <atcoder/all>

using namespace std;
//using namespace atcoder;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i > 0; i--)
#define FOR(i, j, n) for (int i = j; i < n; i++)

typedef long long ll;
const ll MOD = 1000000007; // 10^9+7
const ll INF = 10000000000;

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

    ll r=0,g=0,b=0;
    rep(i,n){
        if(s[i] == 'R') r++;
        if(s[i] == 'G') g++;
        if(s[i] == 'B') b++;
    }

    ll cnt = 0;
    for(int i = 0; i < n; i++){
        for(int j = i+1; j < n; j++){
            if(2*j-i >= n) continue;
            if(s[i] != s[j] && s[j] != s[2*j-i] && s[2*j-i] != s[i]) cnt++;
        }
    }

    cout << r*g*b - cnt << endl;
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+5;
int n,s,x[N];long long p[N],ans;
int main(){
	scanf("%d%d",&n,&s);
	for(int i=1;i<=n;++i)scanf("%d%lld",&x[i],&p[i]);
	for(int l=1,r=n,las=0;;){
		if(s<x[l]){ans+=x[r]-s;break;}
		if(x[r]<s){ans+=s-x[l];break;}
		if(p[l]>=p[r]){
			if(las!=1)las=1,ans+=x[r]-x[l];
			p[l]+=p[r];--r;
		}else{
			if(las!=2)las=2,ans+=x[r]-x[l];
			p[r]+=p[l];++l;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

//Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

template <typename T> void print(T t) { cout<<t<<endl; }
template<typename T, typename... Args> void print(T t, Args... args) { cout<<t<<" "; print(args...); }

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define int long long
#define double long double

void start() {
	#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
	#endif
}

int n,t;
vector< pair<int,int> >v;
int cache[3004][3004];

int dp(int pos,int time) {
    if(pos == n || time >= t) {
        return 0;
    }

    int &ans = cache[pos][time];
    if(ans != -1) {
        return ans;
    }

    ans = dp(pos+1, time);
    ans = max(ans, v[pos].second + dp(pos+1, time + v[pos].first));
    return ans;

}

int32_t main() {
    IOS;
    start();

    cin >> n >> t;
    for(int i = 0; i < n; i++) {
        int a,b;
        cin >> a >> b;
        v.push_back(make_pair(a,b));
    }
    sort(v.begin(), v.end());
    memset(cache, -1, sizeof(cache));
    print(dp(0,0));
}


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

int main(void){
    string str;
    cin >> str;
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
        string op, p = "";
        int a, b;
        cin >> op;
        if(op == "print"){
            cin >> a >> b;
            cout << str.substr(a, b - a + 1) << endl;
        } else if (op == "reverse"){
            cin >> a >> b;
            for(int i = b; i >= a; i--) p += str[i];
            str.replace(a, b - a + 1, p);
        } else {
            cin >> a >> b >> p;
            str.replace(a, b - a + 1, p);
        }
    }

    return 0;
}

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

void print(int a, int b, char str[]);
void rev(int a, int b, char str[]);
void rep(int a, int b, char str[], char repstr[]);

int main()
{
	char str[1000];
	int orderlen;
	cin >> str;
	cin >> orderlen;
	cin.clear();
	cin.ignore();
	for(int i=0; i<orderlen; i++)
	{
		char orderstr[1000], buf[1100],
			pripar[3][8]={
				"print",
				"reverse",
				"replace"
		};
		int a, b, j=0;
		enum __ORDER{PRI, REV, REP, NONE} ORDER;
		cin.getline(buf, sizeof(buf));
		while(true){
			if(buf[j]==' '){
				bool print=true, replace=true, reverse=true;
				for(int k=0; k<j; k++){
					if(print) if(pripar[0][k] != buf[k]) print = false;
					if(reverse) if(pripar[1][k] != buf[k]) reverse = false;
					if(replace) if(pripar[2][k] != buf[k]) replace = false;
					if(!print && !reverse && !replace) break;
				}
				if(print){
					ORDER = PRI;
				} else if(reverse){
					ORDER = REV;
				} else if(replace){
					ORDER = REP;
				} else {
					ORDER = NONE;
				}
				j++;
				{
					int num=0;
					char numbuf[4]="   ";
					while(true){
						numbuf[num] = buf[j];
						num++;
						j++;
						if(buf[j]== ' ' || buf[j]=='\0') break;
					}
					a = atoi(numbuf);
					j++;
					num = 0;
					while(true){
						numbuf[num] = buf[j];
						num++;
						j++;
						if(buf[j]== ' ' || buf[j]=='\0') break;
					}
					b = atoi(numbuf);
					j++;
				}
				break;
			}
			j++;
		}
		if(ORDER == REP){
			int num = 0;
			while(true){
				if(buf[j]=='\0') break;
				orderstr[num] = buf[j];
				num++;
				j++;
			}
		}
		switch(ORDER){
		case PRI:
			print(a, b, str);
			break;
		case REV:
			rev(a, b, str);
			break;
		case REP:
			rep(a, b, str, orderstr);
			break;
		default:
			break;
		}
	}
	return 0;
}

void print(int a, int b, char str[])
{
	for(int i=a; i<=b; i++){
		cout << str[i];
	}
	cout << endl;
}
void rev(int a, int b, char str[])
{
	char tmp;
	for(int i=a, j=b; i<j; i++){
		tmp = str[i];
		str[i] =str[j];
		str[j] = tmp;
		j--;
	}
}
void rep(int a, int b, char str[], char repstr[])
{
	int i=0;
	for(int j=a; j<=b; j++){
		str[j] = repstr[i];
		i++;
	}
}


#include"bits/stdc++.h"
using namespace std;
const int N=5e3+100;
typedef long long LL;
struct node{
	LL h,p;
}a[N];
LL dp[N];
bool cmp(node a,node b){
	if(a.h+a.p==b.h+b.p)return a.h<b.h;
	return a.h+a.p<b.h+b.p;
}

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<=5050;i++)dp[i]=0x3f3f3f3f3f3f3f3f;
	for(int i=1;i<=n;i++)
		scanf("%d %d",&a[i].h,&a[i].p);
	sort(a+1,a+n+1,cmp);
	dp[0]=0;
	for(int i=1;i<=n;i++){
		for(int j=i-1;j>=0;j--){
			if(a[i].h>=dp[j]){
				dp[j+1]=min(dp[j+1],dp[j]+a[i].p);
			}
		}
	}
	int ans=1;
	for(int i=2;i<=n;i++)
		if(dp[i]!=0x3f3f3f3f3f3f3f3f)ans=i;
	printf("%d\n",ans);
}

#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;
typedef tuple<ll,ll,ll> T;
const long long INF = 1LL<<60;
const int MOD = 1000000000+7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
// cout << fixed << setprecision(10) << ans << endl; 有効桁数指定
// *min_element(c + l, c + r) *max_element(c + l, c + r) 配列の中のmin-max
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// ~ は、-1の時だけfalse

int main() {
    int n,a[16],x[16][16],y[16][16];
    cin>> n;
    rep(i,n) {
        cin>>a[i];
        rep(j,a[i]) {
            cin>>x[i][j]>>y[i][j];
        }
    }
    int ans = 0;
    for (int bit = 0;bit<(1<<n);bit++) {
        int count=0;
        bool end = false,last=false;
        for (int i = 0;i<n;i++) {
            if((1<<i) & bit) {
                rep(j,a[i]) {
                    if(y[i][j] != ((bit>>(x[i][j]-1))&1)) {
                        end = true;
                        break;
                    }
                }
                if(end) {
                    last=true;
                    break;
                } else {
                    count++;
                }
            }
        }
        if(!last) ans = max(ans,count);
    }

    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;

const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector <int> a(n);
    vector <vector<pii> > b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b[i].resize(a[i]);
        for (auto &j : b[i]) {
            cin >> j.y >> j.x;
            --j.y;
        }
    }
    int ans = 0;
    for (int i = 1; i < (1 << n); i++) {
        int res = 0;
        vector <int> used(n);
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                ++res;
                used[j] = 1;
            }
        }
        if (ans >= res) continue;
        bool ok = true;
        for (int j = 0; j < n; j++) {
            if (!used[j]) continue;
            for (auto &k : b[j]) {
                if (k.x) {
                    if (!used[k.y]) {
                        ok = false;
                    }
                }
                else {
                    if (used[k.y]) {
                        ok = false;
                    }
                }
            }
        }
        if (ok) ans = res;
    }
    cout << ans << '\n';
}

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main () {
    int n;
    cin >> n;
    int a[n];
    vector<pair<int, int> > vec[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int j = 0; j < a[i]; j++) {
            int x, y;
            cin >> x >> y;
            vec[i].push_back(make_pair(x - 1, y));
        }
    }
    int max = 0;
    for (int i = 0; i < (1 << n); i++) {
        int count = 0;
        int honest[n];
        for (int j = 0; j < n; j++) {
            honest[j] = (i >> j) & 1;
            count += (i >> j) & 1;
        }
        bool ok = true;
        for (int k = 0; k < n; k++) {
            if (honest[k]) {
                for (int l = 0; l < a[k]; l++) {
                    if (honest[vec[k][l].first] != vec[k][l].second) {
                        ok = false;
                    }
                }
            }
        }
        if (ok) {
            if (max < count) max = count;
        }
    }
    cout << max << endl;
    return 0;
}


#include <bits/stdc++.h>
#define newl "\n"
using namespace std;
using lli = long long int;
using vvint = vector <vector <int>>;
const int mod = 1e9 + 7;

const int N = 2e5 + 1;
lli pow1(lli a,lli b){
	lli out = 1;
	for(int i = 0;i < b;++i) out = out * a;
	return out;
}

void solve(){
	lli n;
	cin >> n;
	n = n - 1;
	vector <lli> a;
	for(int i = 0;i <= 11;++i) a.push_back(26ll * (pow1(26ll,i) - 1) / 25);
	int len = upper_bound(a.begin(),a.end(),n) - a.begin();
	n = n - a[len - 1];
	string out = "";
	for(int i = 1;i <= len;++i){
		lli k = pow1(26ll,len - i);
		out += 'a' + n / k;
		n = n % k;
	}
	//reverse(out.begin(),out.end());
	cout << out << newl;
	return;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(10);
	#ifdef EXECUTE_LOCALLY
		   freopen("in.txt","r",stdin);
	#endif
	int t = 1;
	//cin >> t;
	while(t--) solve();
	#ifdef EXECUTE_LOCALLY
    	cout << setprecision(3) << "program time: " << (double)clock() / CLOCKS_PER_SEC << newl;
	#endif
	return 0;
}

#include<iostream>
#include<string>
#include<stdio.h>
#include<cctype>
#include<algorithm>
#include<climits>
#include<cmath>
#include<map>
#include<vector>
#define INF INT_MAX
using namespace std;

long long N;
string name;

int main(){
	cin >> N;
	while (N != 0){
		N--;
		name += ('a' + (N % 26));
		N /= 26;
	}

	reverse(name.begin(), name.end());
	cout << name << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  long long N,x;
  cin>>N>>x;
  long long A[N];
  for(long long i=0;i<N;i++){
    cin>>A[i];
  }
  long long ans=0;
  if(A[0]>x){
    ans+=A[0]-x;
    A[0]=x;
  }
  long long temp=0;
  for(long long i=1;i<N;i++){
    if(A[i-1]+A[i]>x){
      temp=A[i-1]+A[i]-x;
      ans+=temp;
      A[i]-=temp;
    }
  }
  cout<<setprecision(18)<<ans<<endl;
  return 0;
}

#include <iostream>
using namespace std;
typedef long long int LLI;
int main()
{
	int n;
	LLI a, b, x, diff, sum = 0 ;
	cin >> n >> x;
	cin >> a >> b;
	if( a + b > x )
	{
		diff = a + b - x;
		if( diff <= b )
	    	b -= diff;
	    else
	    	b = 0;
		sum += diff;
	}
	a = b;
	for(int i = 2; i < n; i++)
	{
	    cin >> b;
	    if( a + b > x )
		{
			diff = a + b - x;
			if( diff <= b )
		    	b -= diff;
		    else
		    	b = 0;
			sum += diff;
		}
		a = b;
	}
	cout << sum;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(),in.end()

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

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main(){
    int n; cin >> n;
    string s; cin >> s;
    int q; cin >> q;
    while(q--){
        int k; cin >> k;
        ll d = 0, m = 0, dm = 0, dmc = 0;
        rep(i,n){
            if(i >= k){
                if(s[i-k] == 'D'){ d--; dm -= m; }
                else if(s[i-k] == 'M') m--;
            }
            if(s[i] == 'D') d++;
            else if(s[i] == 'M'){ dm += d; m++; }
            else if(s[i] == 'C') dmc += dm;
        }
        cout << dmc << endl;
    }
    return 0;
}

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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

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

void func(long long N, std::string S, long long Q, std::vector<long long> k){

    lli d=0,m=0,dm=0;

    REP(j,0,Q){
        lli ans = 0;
        d=0,m=0,dm=0;
        REP(i,0,N){
            if(S[i]=='D')d++;
            if(S[i]=='M'){
                m++;
                dm+= d;
            }
            if(S[i]=='C')ans += dm;

            if(i<k[j]-1)continue;
            if(S[i-k[j]+1]=='D'){
                    dm -= m;
                    d--;
            }
            if(S[i-k[j]+1]=='M'){
                    m--;
            }
        }
        cout<<ans<<endl;
    }

}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    std::string S;
    std::cin >> S;
    long long Q;
    scanf("%lld",&Q);
    std::vector<long long> k(Q-1-0+1);
    for(int i = 0 ; i < Q-1-0+1 ; i++){
        scanf("%lld",&k[i]);
    }
    func(N, S, Q, std::move(k));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 5e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
    init();
    string S;
    cin >> S;
    if(S.find("AC")==string::npos)
        cout << "No" << endl;
    else
        cout << "Yes" << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ll N; cin >> N;
    ll res = to_string(N).size();
    ll A,B;
    // A <= B としてA,Bを求める． A <= B より Bの桁数の方が大きいことに注意．
    for (ll A=1; A<=sqrt(N); A++){
        if (N%A != 0) continue;
        B = N/A;
        // res = min(ll(to_string(B).size()),res);
        res = min((ll)log10(B)+1, res);
    }
    cout << res << endl;
    return 0;
}


//#define _USE_MATH_DEFINES
//#include <cmath>
#include <iostream>
//#include <stdio.h>
//#include <stdlib.h>
//#include <iomanip>
#include <vector>
#include <string>
//#include <algorithm>
//#include <functional>
//#include <time.h>
#include <sstream>

using namespace std;

bool isthe(string str){
	//?????????????????????????????? the this that?????¢??? ????????°ture

	int d1, d2, d3;

	d1 = (str[1] - str[0] + 26) % 26;

	if (d1 != 14){
		return false;
	}
	else{
		d2 = (str[2] - str[0] + 26) % 26;
		if (d2 != 11 && d2 != 15 && d2 != 7){
			return false;
		}
		else{
			if (d2 == 11 && str.size() == 3){
				return true;
			}
			else{
				d3 = (str[3] - str[0] + 26) % 26;
				if (d2 == 15 && d3 == 25) return true;
				if (d2 == 7 && d3 == 0) return true;
			}
		}
	}

	return false;

}

int main(){

	string s,sc, buf;
	stringstream ss;
	vector <string> as;
	int dif = 0;

	while (getline(cin, s)){
		dif = 0;
		buf = "";
		sc = "";
		ss.str("");
		ss.clear();

		sc += s;
		for (int i = 0; i < sc.size(); i++){
			if (sc[i] == '.')sc[i] = ' ';
		}
		ss << sc;

		while (ss >> buf){
			//the this that???????´¢
			if (buf.size() == 4){
				if (isthe(buf)){
					dif = (buf[0] - 't' + 26)%26;
					break;
				}
			}
			if (buf.size() == 3){
				if (isthe(buf)){
					dif = (buf[0] - 't' + 26) % 26;
					//break;
				}
			}
		}

		for (int i = 0; i < s.size(); i++){
			if (s[i] >= 'a' && s[i] <= 'z') {
				s[i] -= dif;
				if (s[i] < 'a') s[i] += 26;
				if (s[i] > 'z') s[i] -= 26;
			}
		}

		cout << s << endl;
	}

	return 0;
}

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

string Caesar(string a) {
	for (int b = 0; b < a.length(); b++) {
		if (a[b] == 'z')a[b] = 'a';
		else if (a[b] >= 'a'&&a[b] < 'z')a[b] = a[b] + 1;
	}
	return a;
}
int main()
{
	string a;
	while (getline(cin, a)) {
		for (int b = 0; b < 26; b++) {
		    a=Caesar(a);
			for (int c = 0; c < a.length() - 4; c++) {
				if (a[c] == 't'&&a[c + 1] == 'h') {
					if (a[c + 2] == 'e') { cout << a << endl; goto l; }
					else	if (a[c + 2] == 'a'&&a[c + 3] == 't') { cout << a << endl; goto l; }
					else	if (a[c + 2] == 'i'&&a[c + 3] == 's') { cout << a << endl; goto l; }
				}
			}
			if(a[a.length()-1]=='t'&&a[a.length() - 2] == 'h'&&a[a.length() - 3] == 'e') { cout << a << endl; break; }
		l:;
		}
	}
}


#include<iostream>
using namespace std;

int main(){
	int n=1;
	int a = 0;
	int f = 0;
	int quest[10000] = { 1 };
	double ans[10000];
	int checkc = 0;
	int i = 4;
	double check[3] = { 1, 2, 4 };

	do{
		cin >> quest[a];
		a++;
	} while (quest[a-1] != 0);
	while (a > f+1){
		switch (quest[f]){
		case 1:
			ans[f] = 1;
			break;
		case 2:
			ans[f] = 2;
			break;
		case 3:
			ans[f] = 4;
			break;
		default:
			while (quest[f] >= i){
				ans[f] = check[0] + check[1] + check[2];
				i++;
				check[0] = check[1];
				check[1] = check[2];
				check[2] = ans[f];
			}
		}

		ans[f] = ans[f] / 3650;
		checkc = (int)(ans[f]);

		if (ans[f] > (double)(checkc)){
			checkc++;
		}
		if (ans[f] != 0){
			cout << checkc << endl;
		}
		else{
			cout << endl;
		}
		f++;
	}
	return 0;
}


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

int main()
{
    int n;
    cin >> n;
    int ans = (n + 110) / 111 * 111;
    cout << ans << endl;
    return 0;
}

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

int main() {
  string s;
  cin >> s;
  for (int i = 0; i < s.size() - 8; i++) {
    cout << s.at(i);
  }
  cout << endl;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
#include<cmath>
#include<climits>
#include<queue>
#include<stack>
#include<numeric>
#include<set>
#include<iomanip>
#include<map>
#include<type_traits>
#include<tuple>
#include<deque>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
const int mod = 1000000007;
const int MAXH = 100;
const int MAXW = 100;

int main() {

  int a, b;
  cin >> a >> b;
  vector<string> s(MAXH);
  rep(i, MAXH / 2) {
    s[i] = string(MAXW, '#');
  }
  for (int i = MAXH / 2; i < MAXH; ++i) {
    s[i] = string(MAXW, '.');
  }
  a--; b--;

  int cur = 0;
  rep(i, a) {
    if(cur % 200 == 100) cur += 100;
    s[cur / MAXW][cur % MAXW] = '.';
    cur += 2;

  }
  cur = 50 * 100 + 100;
  rep(i, b) {
    if(cur % 200 == 100) cur += 100;
    s[cur / MAXW][cur % MAXW] = '#';
    cur += 2;

  }
  cout << MAXH << " " << MAXW << endl;
  rep(i, MAXH) cout << s[i] << endl;
}


#include <bits/stdc++.h>
using namespace std;
bool a[1000005],b[1000005];
const int mod=1e9+7;
int main()
{
	int n,m;
	cin>>n>>m;
	long long sum=1;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		if(a[x])
		{
			cout<<"0";
			return 0;
		}
		a[x]=1;
	}
	for(int i=0;i<m;i++)
	{
		int y;
		cin>>y;
		if(b[y])
		{
			cout<<"0";
			return 0;
		}
		b[y]=1;
	}
	int k1=0,t1=0;
	for(int i=n*m;i>0;i--)
	{
		int k2=k1,t2=t1;
		if(b[i])
		{
			k2=1;
			k1++;
		}
		if(a[i])
		{
			t2=1;
			t1++;
		}
		if(a[i]||b[i])
			sum*=(k2*t2)%mod;
		else
			sum*=(k2*t2-(n*m-i))%mod;
		sum%=mod;
	}
	cout<<sum%mod;

	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include<string>
#include <set>
#include <map>
#include <queue>
#include <limits.h>
#include <bitset>
#include <cmath>

using namespace std;

#define rip(i, n, s) for (int i = (s);i < ( int )( n ); i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int,int> P;
struct modint {
	long long num;
	long long p;
	modint() {
		num = 0;
		p = 1000000007;
	}
	modint(int x) {
		p = 1000000007;
		num = (long long)x%p;
	}
	modint(long long x) {
		p = 1000000007;
		num = x % p;
	}
	modint operator*(const long long &other) {
		modint ret;
		ret.p = p;
		ret.num = (num*(other%p)) % p;
		return ret;
	}
	void operator*=(const long long &other) {
		num = (num*other) % p;
	}
};

int main(){
    int n,m;
    cin >> n >> m;
    vector<P> da(n+m);
    rip(i,n,0){
        cin >> da[i].first;
        da[i].second=1;
    }
    rip(i,m,0){
        cin >> da[i+n].first;
        da[i+n].second=0;
    }
	set<int> ch;
	rip(i,n,0){
		if(ch.count(da[i].first)){
			cout << 0 << endl;
			return 0;
		}
		else{
			ch.insert(da[i].first);
		}
	}
	ch.clear();
	rip(i,m,0){
		if(ch.count(da[i+n].first)){
			cout << 0 << endl;
			return 0;
		}
		else{
			ch.insert(da[i+n].first);
		}
	}
    modint ans(1);
    int now=n*m,h=0,w=0,cr=0;
    sort(all(da));
    reverse(all(da));
    int co=0;
	while(co<n+m){
		//printf("%d %d %d %d %d %lld\n",now,co,h,w,cr,ans.num);
		if(now<=0){
			cout << 0 << endl;
			return 0;
		}
		if(da[co].first==now){
			if(co<n*m-1){
				if(da[co+1].first==now){
					h++;
					w++;
					cr++;
					co++;
				}
				else{
					if(da[co].second){
						ans*=w;
						h++;
					}
					else{
						ans*=h;
						w++;
					}
					cr++;
				}
			}
			else{
				if(da[co].second){
					ans*=w;
					h++;
				}
				else{
					ans*=h;
					w++;
				}
			}
			co++;
		}
		else{
			if((h*w)-cr>0){
			ans*=(h*w)-cr;
			cr++;
			}
			else{
				cout << 0 << endl;
				return 0;
			}
		}
		now--;
	}
	rip(i,now+1,1){
		ans*=i;
	}
	cout << ans.num << endl;
}


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

const int64_t MOD = 1000000007;
typedef int64_t ll;
typedef uint64_t ull;

#define FOR(i, start, end) for(uint64_t i=start; i<end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0){
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) {
  return (m*n)/gcd(m,n);
}

// vector<vector<uint64_t> > v(n+1, vector<uint64_t>(n+1, 0))
// v[n][k]に組み合わせ数が入る。
void comb(vector<vector <uint64_t> > &v){
  for(uint64_t i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(uint64_t k = 1;k <v.size();k++){
    for(uint64_t j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

// 掛け算オーバーフロー判定
bool is_product_overflow(uint64_t a, uint64_t b) {

	uint64_t prod = a * b;

	return (prod / b != a);
}

//素因数分解
void primeFactorization(uint64_t a, list<uint64_t> &factors){ //素因数分解を出力するプログラム
  long i,sq;
  if(a%2==0){ //偶数の場合
    factors.push_back(2);
    primeFactorization(a/2,factors); //2で割った値で再帰
    return;
  }
  sq = sqrt(a);
  for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合
	if(a%i==0){
	  factors.push_back(i);
      primeFactorization(a/i,factors); //割れた値で再帰
      return;
	}
  }
  //偶数でも3以上√a以下の奇数の場合でも割り切れない場合
  if(a!=1){ //aが1でないなら、a自身は素数
    factors.push_back(a);
  }
}

// フェルマーの小定理
// mod. m での a の逆元 a^{-1} を計算する
// (a/b)%m = a*movinv(b,m)%m
int64_t modinv(int64_t a, int64_t m) {
    int64_t b = m, u = 1, v = 0;
    while (b) {
        int64_t t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

// 円周率
// M_PI

// #include <iomanip> // setprecisionを使用するのに必要
// cout << std::fixed << std::setprecision(15) << y << endl;

// 昇順
// priority_queue<int, vector<int>, greater<int> > queue;

ll pCount(int x){
  if(x==0) return 1;
  return pCount(x-1)*2 + 1;
}

ll allCount(int x){
  if(x==0) return 1;
  return allCount(x-1)*2 + 3;
}

signed main() {
  ull n,m;
  cin >> n >> m;

  ll ans = 1;
  for(ll i=1;i*i<=m;i++){
    if(m%i!=0)continue;
    ll j = m/i;
    if(i*n<=m) ans = max(ans,i);
    if(j*n<=m) ans = max(ans,j);
  }
  cout << ans << 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(){
  int K;
  string S;

   cin >> K >> S;

  if(S.size()<=K) {
    cout << S << endl;
    return 0;
  }
  else{
    rep(i,K){
     cout << S.at(i);
    }
     cout << "..."<< endl;
     return 0;
  }

}


#include<iostream>

using namespace std;
int main() {
    int d;
    while(cin >> d) {
        int ans = 0;
        int x = d;
        while (x <= 600 - d) {
            ans += x * x * d;
            x += d;
        }
        cout << ans << endl;
    }
}

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

int main(){
	int d, ans, n;
	while(cin >> d){
		ans = pow(d, 3);
		n = 600 / d - 1;
		ans *= n*(n+1)*(2*n+1) / 6;
		cout << ans << endl;
	}
	return 0;
}


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

#define int unsigned long long
#define fi first
#define se second
#define pb push_back
#define ins insert
#define er erase

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int oo = 1e18 + 7, mod = 1e9 + 7, mul = (1 << 30);

int n, pw[200005], a[200005], b[200005], hash1[200005], hash2[200005], c[200005], d[200005];
vector<ii> answers;

int hash11(int l, int r){
    return (hash1[r] - (hash1[l - 1] * pw[r - l + 1]) + mod * mod) % mod;
}

int hash22(int l, int r){
    return (hash2[r] - (hash2[l - 1] * pw[r - l + 1]) + mod * mod) % mod;
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) cin >> b[i];
	for(int i = 1; i < n; i++) c[i] = (a[i] ^ a[i + 1]);
	c[n] = (a[n] ^ a[1]);
	for(int i = 1; i < n; i++) d[i] = (b[i] ^ b[i + 1]);
	d[n] = (b[n] ^ b[1]);
	//for(int i = 1; i <= n; i++) cout << c[i] << " ";
	//cout << "\n";
	//for(int i = 1; i <= n; i++) cout << d[i] << " ";
	//cout << "\n";
	for(int i = 1; i <= n; i++) hash1[i] = (hash1[i - 1] * mul + c[i]) % mod;
	for(int i = 1; i <= n; i++) hash2[i] = (hash2[i - 1] * mul + d[i]) % mod;
	pw[0] = 1;
	for(int i = 1; i <= n; i++) pw[i] = (pw[i - 1] * mul) % mod;
	for(int i = 1; i <= n; i++){
	    //cout << hash11(1, n - i + 1) << " " << hash22(i, n) << "\n";
	    if(hash11(1, (n - i + 1)) != hash22(i, n) || ((i != 1) && (hash11(n - i + 2, n) != hash22(1, i - 1)))) continue;
	    answers.pb({(n - i + 1) % n, b[i] ^ a[1]});
	}
	sort(answers.begin(), answers.end());
	for(int i = 0; i < answers.size(); i++) cout << answers[i].fi << " " << answers[i].se << "\n";
}

/*
TEST HERE
INPUT #1

OUTPUT #1

INPUT #2

OUTPUT #2

INPUT #3

OUTPUT #3

*/

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,1,-1,1,-1,-1,0};
int dy[]={0,1,1,-1,-1,1,0,-1};

int main(){
    int n;cin >> n;
    vector<int> a(n),b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    vector<int> aa(3*n);
    for (int i = 0; i < n; i++) {
        aa[i] = b[i] ^ b[(i+1)%n];
        aa[i + n] = a[i] ^ a[(i+1)%n];
        aa[i + 2 * n]  = aa[i + n];
    }
    vector< int > prefix(aa.size());
    for(int i = 1, j = 0; i < aa.size(); i++) {
        if(i + prefix[i - j] < j + prefix[j]) {
            prefix[i] = prefix[i - j];
        } else {
            int k = max(0, j + prefix[j] - i);
            while(i + k < aa.size() && aa[k] == aa[i + k]) ++k;
            prefix[i] = k;
            j = i;
        }
    }
    for (int i = n; i < 2 * n; i++) {
        if(prefix[i] >= n){
            int k = i - n;
            cout << k << " " << (a[k] ^ b[0]) << endl;
        }
    }
    return 0;
}


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

using namespace std;
#include <vector>
#define rep(i,n) for (ll i = 0; i < (n) ; i++)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000      //1e4
#define base10_5 100000     //1e5
#define base10_6 1000000    //1e6
#define base10_7 10000000   //1e7
#define base10_8 100000000  //1e8
#define base10_9 1000000000 //1e9

#define MOD 1000000007
#define pb push_back
#define ll long long
#define ld long double
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

//#include <stack>
//#include <queue>

// #include <iomanip>
//  cout << fixed << setprecision(15) << y << endl;

string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

vll A;
vll B;
ll C;
ll N;
ll L;
ll K;

ll ltmp;
string stmp;
double dtmp;

int main(){

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

    cin >> N;
    cin >> L;

    ll sumA=0;
    rep(ni,N){
        ltmp = L+(ni+1)-1;
        A.push_back(ltmp);
        sumA+=ltmp;
    }

    ll ans = sumA;
    if(A[0]>0) ans-=A[0];
    else if(A[N-1]<0) ans-=A[N-1];

    cout << ans << endl;

}

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int N, L;
  cin>>N>>L;
  int ans = 0;
  if(L*(L+N-1)<=0) {
    for(int i=L;i<=L+N-1;++i) {
      ans += i;
    }
  }
  else if(L+N-1<0) {
    for(int i=L;i<L+N-1;++i) ans += i;
  }
  else {
    for(int i=L+1;i<=L+N-1;++i) ans += i;
  }
  cout<<ans<<endl;
}


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

// varibable settings
#define int long long
const int INF=1e18;

// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(a, b, c) make_tuple(a, b, c)
#define divceil(a,b) ((a)+(b)-1)/(b)
#define is_in(x, a, b) ((a)<=(x) && (x)<(b))
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
#define posl(A, x) (lower_bound(all(A), x)-A.begin())
#define posu(A, x) (upper_bound(all(A),x)-A.begin())
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;

template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

#if defined(PCM) || defined(LOCAL)
  #include "lib/dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
  #define cerrendl 42
#endif
//}}}

//%snippet.set('kmp')%

// Morris-Pratt
template<typename T> struct KMP {
  int n;
  T pattern;
  vector<int> a;
  KMP() {}/*{{{*/
  KMP(const T& pattern): pattern(pattern) {
    n = pattern.size();
    a = vector<int>(n+1);
    a[0] = -1;
    int j = -1;
    for (int i = 0; i < n; ++i) {
      while (j != -1 && pattern[j] != pattern[i]) j = a[j];
      j++;
      a[i+1] = j;
    }
  }/*}}}*/
  int operator[](int i) { return a[i];}
  vector<int> findAll(const T& s) {/*{{{*/
    vector<int> res;
    int j = 0;
    for (int i = 0; i < sz(s); ++i) {
      while (j != -1 && pattern[j] != s[i]) j = a[j];
      j++;
      if (j == n) {
        res.push_back(i-j+1);
        j = a[j];
      }
    }
    return res;
  }/*}}}*/
};

// KMP<vector<int>> kmp({1,2,3});
// KMP<string> kmp("abc");

//%snippet.end()%

vector<int> f(vector<int>& a) {
  int n = a.size();
  vector<int> res(n);
  rep(i,n) res[i] = a[i]^a[(i+1)%n];
  return res;
}

signed main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];
  vector<int> xa = f(a);
  vector<int> xb = f(b);
  dump(xa);
  dump(xb);

  KMP<vector<int>> kmp(xa);
  dump("hello");
  vi wxb;
  wxb.insert(wxb.end(), xb.begin(), xb.end());
  wxb.insert(wxb.end(), xb.begin(), xb.end());

  vector<int> res = kmp.findAll(wxb);
  vector<int> ks;
  for (int p : res) ks.push_back(n-p);
  sort(ks.begin(), ks.end());
  for (int k : ks) {
    if (k >= n) continue;
    int x = a[k]^b[0];
    cout << k << " " << x << endl;
  }
  return 0;
}

#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>

typedef long long ll;
using namespace std;

bool is_1pair(int* cards){
	sort(cards,cards+5);
	map<int,int> freq;
	for(int i=0;i<5;i++){
		freq[cards[i]]++;
	}

	int count = 0;
	for(map<int,int>::iterator it = freq.begin(); it != freq.end(); it++){
		if(it->second == 2){
			count++;
		}
		if(it->second == 3){
			return false;
		}
	}

	return count == 1;
}

bool is_2pair(int* cards){
	sort(cards,cards+5);
	map<int,int> freq;
	for(int i=0;i<5;i++){
		freq[cards[i]]++;
	}

	int count = 0;
	for(map<int,int>::iterator it = freq.begin(); it != freq.end(); it++){
		if(it->second == 2){
			count++;
		}
	}

	return count == 2;
}

bool is_3card(int* cards){
	sort(cards,cards+5);
	map<int,int> freq;
	for(int i=0;i<5;i++){
		freq[cards[i]]++;
	}

	int count = 0;
	for(map<int,int>::iterator it = freq.begin(); it != freq.end(); it++){
		if(it->second == 3){
			count++;
		}
		if(it->second == 2){
			return false;
		}
	}

	return count == 1;
}

bool is_straight(int* cards){
	sort(cards,cards+5);
	map<int,int> freq;
	for(int i=0;i<5;i++){
		freq[cards[i]]++;
	}

	int count = 0;
	for(map<int,int>::iterator it = freq.begin(); it != freq.end(); it++){
		if(it->second > 1){
			return false;
		}
	}

	if(freq[1]==1){
		int ans[][5] = {{1,2,3,4,5},{1,10,11,12,13}};

		bool isok1 = true;
		for(int i=0;i<5;i++){
			if(cards[i] != ans[0][i]) isok1 = false;
		}

		bool isok2 = true;
		for(int i=0;i<5;i++){
			if(cards[i] != ans[1][i]) isok2 = false;
		}

		return (isok1 || isok2);
	}

	else{
		for(int i=0;i+1<5;i++){
			if(cards[i+1] - cards[i] != 1) return false;
		}
	}

	return true;
}

bool is_fullhouse(int* cards){
	sort(cards,cards+5);
	map<int,int> freq;
	for(int i=0;i<5;i++){
		freq[cards[i]]++;
	}

	int count = 0;
	bool isok1=false,isok2=false;
	for(map<int,int>::iterator it = freq.begin(); it != freq.end(); it++){
		if(it->second == 3){
			isok1 = true;
		}
		else if(it->second == 2){
			isok2 = true;
		}
	}

	return isok1 && isok2;
}

bool is_4card(int* cards){
	sort(cards,cards+5);
	map<int,int> freq;
	for(int i=0;i<5;i++){
		freq[cards[i]]++;
	}
	int count = 0;
	for(map<int,int>::iterator it = freq.begin(); it != freq.end(); it++){
		if(it->second == 4){
			return true;
		}
	}

	return false;
}
int main()
{
	int cards[5];
	while(~scanf("%d,%d,%d,%d,%d",cards,cards+1,cards+2,cards+3,cards+4)){
		if(is_1pair(cards)) cout << "one pair" << endl;
		else if(is_2pair(cards)) cout << "two pair" << endl;
		else if(is_3card(cards)) cout << "three card" << endl;
		else if(is_straight(cards)) cout << "straight" << endl;
		else if(is_fullhouse(cards)) cout << "full house" << endl;
		else if(is_4card(cards)) cout << "four card" << endl;
		else cout << "null" << endl;

	}
}


#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

void dfs(vector<vector<int>>& G, int u, vector<int>& dist) {
  for (auto v : G[u]) {
    if (dist[v] != -1) continue;
    dist[v] = dist[u] + 1;
    dfs(G, v, dist);
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  int N, u, v;
  cin >> N >> u >> v;
  --u, --v;

  vector<vector<int>> G(N);
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  vector<int> distT(N, -1);
  distT[u] = 0;
  dfs(G, u, distT);

  vector<int> distA(N, -1);
  distA[v] = 0;
  dfs(G, v, distA);

  int answer = 0;
  rep(i, N) {
    if (distT[i] < distA[i]) {
      answer = max(answer, distA[i] - 1);
    }
  }
  cout << answer << endl;
  return 0;
}

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

int main() {
    int n, u, v; cin >> n >> u >> v;
    vector<vector<int>> g(n+1);
    rep(i, n-1){
        int a, b; cin >> a >> b;
        g.at(a).push_back(b);
        g.at(b).push_back(a);
    }

    vector<int> distU(n+1, -1);
    vector<int> distV(n+1, -1);
    queue<int> qu, qv;
    qu.push(u);
    qv.push(v);

    distU.at(u) = 0;
    distV.at(v) = 0;

    while(!qu.empty()){
        int t = qu.front();
        qu.pop();

        for(auto a:g.at(t)){
            if(distU.at(a)!=-1) continue;
            distU.at(a) = distU.at(t) + 1;
            qu.push(a);
        }
    }

    while(!qv.empty()){
        int t = qv.front();
        qv.pop();

        for(auto a:g.at(t)){
            if(distV.at(a)!=-1) continue;
            distV.at(a) = distV.at(t) + 1;
            qv.push(a);
        }
    }

    int fart = v;
    ll dist = 0;
    for(int i=1; i<=n; i++){
        if(distU.at(i)<distV.at(i)){
            if(dist<distV.at(i)){
                dist = distV.at(i);
                fart = i;
            }
        }
    }

    dump(fart);
    if(fart==v){
        cout << 0 << endl;
    }
    else{
        cout << distV.at(fart) - 1 << endl;
    }

    return 0;
}


#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int N,M;

long long int H,W,A[210005]={},Q,sum[210005]={},res=INF;

int main()
{
    cout << fixed << setprecision(18);
   cin>>N;
   for(long long int i = 1; i <= N; i++){
       cin>>A[i];
   }
   for(long long int i = 1; i <= N; i++){
       sum[i]=sum[i-1]+A[i];
   }
   for(long long int i = 2; i <= N-2; i++){
       long long int p=1,q=i,pq;
       while(q-p>1){
           pq=(p+q)/2;
           if(sum[pq]*2>=sum[i]){
               q=pq;
           }else{
               p=pq;
           }
       }
       long long int P=sum[p],Q=sum[i]-sum[p];
        if(q!=i&&abs(sum[i]-2*sum[p+1])<=Q-P){
            P=sum[p+1];
            Q=sum[i]-sum[p+1];
        }
       long long int r=i+1,s=N,rs;
       while(s-r>1){
           rs=(r+s)/2;
           if((sum[rs]-sum[i])*2>=sum[N]-sum[i]){
               s=rs;
           }else{
               r=rs;
           }
       }
        long long int R=sum[r]-sum[i],S=sum[N]-sum[r];
        //cout<<R<<" "<<S<<endl;
        if(s!=N&&abs(sum[N]-2*sum[r+1]+sum[i])<=S-R){
            R=sum[r+1]-sum[i];
            S=sum[N]-sum[r+1];
        }
        //cout<<i<<" "<<P<<" "<<Q<<" "<<R<<" "<<S<<endl;
        res=min(res,max({P,Q,R,S})-min({P,Q,R,S}));
   }
   cout<<res<<endl;
}

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


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<string>
#define rep(i,n,m) for(int i=(n);i<(int)(m);i++)
#define reps(i,n,m) for(int i=(n);i<=(int)(m);i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a,x) lb(all(a), x) - a.begin()
#define UB(a,x) ub(all(a), x) - a.begin()
#define printfdouble(x) printf("%.12f\n",(x))
#define chartoint(c) (int)((c) - '0')
#define chartoll(c) (long long)((c) - '0')
#define MOD 1000000007
#define itn int
#define enld endl
#define ednl endl
#define icn cin
#define stirng string
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
using Graph = vector<vector<int>>;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b){if(b<a){a=b; return 1;} return 0;}
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int N; cin >> N;
    pair<int,int> red[105];
    pair<int,int> blue[105];
    rep(i,0,N){
        int a,b; cin >> a >> b;
        red[i] = {a,b};
    }
    rep(i,0,N){
        int c,d; cin >> c >> d;
        blue[i] = {c,d};
    }
    sort(red,red+N);
    sort(blue,blue+N);
    int ptr = 0;
    int ans = 0;
    bool used[105];
    Fill(used,false);
    rep(i,0,N){
        int cnt = -1;
        int maxy = -1;
        rep(j,0,N){
            if(blue[i].first > red[j].first && blue[i].second > red[j].second && used[j] == false){
                if(maxy < red[j].second){
                    maxy = red[j].second;
                    cnt = j;
                }
            }
        }
        if(cnt != -1){
            used[cnt] = true;
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

//
// Created by tac on 2016/10/16.
//

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

namespace {

void print(const std::string &str,
           std::string::size_type a,
           std::string::size_type b) {
    std::cout << str.substr(a, b - a + 1) << std::endl;
    return;
}

const std::string reverse(const std::string &str,
                          std::string::size_type a,
                          std::string::size_type b) {
    std::string ret = str;
    std::reverse(ret.begin() + a, ret.begin() + b + 1);
    return ret;
}

const std::string replace(const std::string &str,
                          std::string::size_type a,
                          std::string::size_type b,
                          const std::string &rep) {
    std::string ret = str;
    ret.replace(ret.begin() + a, ret.begin() + (b + 1), rep);
    return ret;
}

}

int main() {
    std::string str;
    int n;
    std::string op, rep;
    int a, b;
    std::cin >> str;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
        std::cin >> op;
        std::cin >> a >> b;
        if (op == "print") {
            print(str, a, b);
        } else if (op == "reverse") {
            str = reverse(str, a, b);
        } else if (op == "replace") {
            std::cin >> rep;
            str = replace(str, a, b, rep);
        } else {
            throw std::runtime_error("");
        }
    }
    return EXIT_SUCCESS;
}


#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	int a,b;cin>>a>>b;
	vector<vector<char>> v(100,vector<char>(100,'#'));
	for(int i = 50; i < 100; i++) {
		for(int j = 0; j < 100; j++) {
			v[i][j] = '.';
		}
	}
	int h = 0,w = 0;
	a--;b--;
	while(a > 0){
		v[h][w] = '.';
		if (w + 2 < 100){
			w += 2;
		}
		else{
			w = 0;
			h += 2;
		}
		a--;
	}
	h = 51,w = 0;
	while(b > 0){
		v[h][w] = '#';
		if (w + 2 < 100){
			w += 2;
		}
		else{
			w = 0;
			h += 2;
		}
		b--;
	}
	cout << 100 << " " << 100 << endl;
	for(int i = 0; i < 100; i++) {
		for(int j = 0; j < 100; j++) {
			cout << v[i][j];
		}
		cout << endl;
	}

	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define rrep(i,n) for(int i=1;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int h, w;
	cin >> h >> w;
	vector<vector<int>> mp(h, vector<int>(w));
	rep(i, h) rep(j, w) cin >> mp[i][j];
	vector<tuple<int, int, int, int>> ans;
	rep(i, h) {
		if (i % 2 == 0) {
			for (int j = 0; j < w; ++j) {
				if (mp[i][j] % 2 == 1) {
					if (j + 1 < w) {
						mp[i][j + 1]++;
						ans.emplace_back(i + 1, j + 1, i + 1, j + 2);
					}
					else if (i + 1 < h) {
						mp[i + 1][j]++;
						ans.emplace_back(i + 1, j + 1, i + 2, j + 1);
					}
				}
			}
		}
		else {
			for (int j = w - 1; j >= 0; --j) {
				if (mp[i][j] % 2 == 1) {
					if (j - 1 >= 0) {
						mp[i][j - 1]++;
						ans.emplace_back(i + 1, j + 1, i + 1, j);
					}
					else if (i + 1 < h) {
						mp[i + 1][j]++;
						ans.emplace_back(i + 1, j + 1, i + 2, j + 1);
					}
				}
			}
		}
	}
	int sz = ans.size();
	cout << sz << endl;
	for (auto c : ans) printf("%d %d %d %d\n", get<0>(c), get<1>(c), get<2>(c), get<3>(c));
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
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; }
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int d, g;
    cin >> d >> g;
    V<> p(d), c(d);
    rep(i, d) cin >> p[i] >> c[i];
    int ans = INF;
    rep(bit, 1 << d) {
        int score = 0;
        int num = 0;
        rep(i, d) if(1 & bit >> i) score += c[i] + (i+1) * 100 * p[i], num += p[i];
        int rest = g - score;
        if(rest > 0) {
            drep(i, d) if(!(1 & bit >> i)) {
                int now_score = (i+1) * 100;
                if(now_score * (p[i] - 1) < rest) rest -= now_score*(p[i]-1), num += (p[i]-1);
                else {
                    num += (rest+now_score-100)/now_score;
                    score += (rest+now_score-100)/now_score * now_score;
                    break;
                }
            }
        }
        if(score < g) continue;
        else chmin(ans, num);
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
#ifdef i_am_noob
#define debug(a) print2(#a,a)
#else
#define debug(a)
#endif
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
using namespace __gnu_pbds;

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod;

inline int add(int a, int b){return (a+b>=MOD?a+b-MOD:a+b);}
inline int sub(int a, int b){return (a+MOD-b>=MOD?a-b:a+MOD-b);}
int Pow(int a, int b){
	if(!b) return 1;
	int tmp=Pow(a*a%MOD,b>>1);
	return (b&1?tmp*a%MOD:tmp);
}
inline int inverse(int n){return Pow(n,MOD-2);}

const int maxn=100005;
//i_am_noob
int n,goal,a[10][2],cnt,x,ans=4e18;
bool flag;

signed main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n >> goal;
    rep(n) rep1(j,2) cin >> a[i][j];
    rep(pow2(n)){
        cnt=x=0;
        rep1(j,n) if(i&pow2(j)){
            x+=a[j][0];
            cnt+=a[j][0]*(j+1)*100+a[j][1];
        }
        if(cnt>=goal){
            ans=min(ans,x);
            continue;
        }
        flag=false;
        rep3(j,n-1,0) if((i&pow2(j))==0){
            rep1(k,a[j][0]){
                x++;
                cnt+=(j+1)*100;
                if(cnt>=goal){
                    ans=min(ans,x);
                    flag=true;
                    break;
                }
            }
            if(flag) break;
        }
    }
    print1(ans);
	return 0;
}


#include<iostream>
using namespace std;
int main(){
  int a,b,c;
  while(cin>>a>>b>>c){
    if(a==0&&b==0&&c==0) break;
  int ans[a+b+c+1];
  for(int i=1;i<=a+b+c;i++) ans[i] = 2;
  int n;
  cin >> n;
  int ag[n],bg[n],cg[n],r[n];
  for(int i=0;i<n;i++){
    cin >> ag[i] >> bg[i] >> cg[i] >> r[i];
    if(r[i]==1) ans[ag[i]] = ans[bg[i]] = ans[cg[i]] = 1;
  }
  for(int i=0;i<n;i++){
    if(r[i]==0){
      if(ans[ag[i]]==2&&ans[bg[i]]==1&&ans[cg[i]]==1) ans[ag[i]] = 0;
      if(ans[ag[i]]==1&&ans[bg[i]]==2&&ans[cg[i]]==1) ans[bg[i]] = 0;
      if(ans[ag[i]]==1&&ans[bg[i]]==1&&ans[cg[i]]==2) ans[cg[i]] = 0;
    }
  }
  for(int i=1;i<=a+b+c;i++) cout << ans[i] << endl;
  }
  return 0;
}

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

#define MP make_pair
#define PB push_back

int main(int argc,char* argv[]){
  multimap<int,vector<int> > tab;
  vector<int> vec;
  int a,b,c,x,i,j,count,n;
  int flag[400];

  while(cin>>a>>b>>c){
    if(a==0 && b==0 && c==0) break;
    for(i=0;i<400;i++) flag[i]=2;
    tab.clear();

    cin>>n;
    for(i=0;i<n;i++){
      vec.clear();
      for(j=0;j<3;j++){
	cin>>x;
	vec.PB(x);
      }
      cin>>x;
      if(x==1) tab.insert(MP(0,vec));
      else tab.insert(MP(1,vec));
    }

    multimap<int,vector<int> >::iterator itr;
    for(itr=tab.begin();itr!=tab.end();itr++){
      if((*itr).first==0){
	vector<int>::iterator it;
	for(it=(*itr).second.begin();it!=(*itr).second.end();it++){
	  flag[(*it)]=1;
	}
      }
      else{
	vec.clear();
	count=0;
	vector<int>::iterator it;
	for(it=(*itr).second.begin();it!=(*itr).second.end();it++){
	  if(flag[(*it)]==1) count++;
	  else{
	    vec.PB((*it));
	  }
	}
	if(count==2){
	  flag[vec.back()]=0;
	}
      }
    }
    for(i=1;i<=a+b+c;i++){
      cout<<flag[i]<<endl;
    }
  }
  return 0;
}


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

typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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

const ll INF = 1e18;
const ll MOD = 998244353;
const ll MX = 1000001;

string N;

void solve() {
	cin >> N;
	int a[N.size()], inv[N.size()];
	ll ans = 0;
	for (int i = 0; i < (int)N.size(); i++) {
		a[i] = (int)(N[i] - '0');
		inv[i] = -1;
		// ans += dp[i];
	}

	// Figure out what segments are better to inverse
	for (int i = N.size() - 1; i >= 0; --i) {
		if (a[i] > 5) {
			inv[i] = i;
			if (i + 1 < (int)N.size() && inv[i + 1] != -1) inv[i] = inv[i + 1];
		} else if (a[i] == 5) {
			if (i + 1 < (int)N.size() && inv[i + 1] != -1) inv[i] = inv[i + 1];
			else if (i > 0 && a[i - 1] >= 5) inv[i] = i;
		} else if (a[i] == 4) {
			if (i > 0 && i < (int)N.size() - 1) {
				if (a[i - 1] >= 5 && inv[i + 1] != -1) {
					inv[i] = inv[i + 1];
				}
			}

		}
	}

	int end = -1;
	for (int i = 0; i < (int)N.size(); i++) {
		if (end == -1) {
			if (inv[i] == -1) ans += a[i];
			else {
				ans += 2;
				end = inv[i];
				ans += 9 - a[i];
			}
		} else {
			ans += 9 - a[i];
		}
		if (i == end) end = -1;
	}
	cout << ans;
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
    ll T = 1; // cin >> T;
    while (T--) solve();
    return 0;
    // You should actually read the stuff at the bottom
}

/* Stuff to Look For
 * -----------------
 * Int overflow, array bounds
 * Initializing all variables, avoid weird behavior
 * Edge cases(n = 0, n = 1)
 * Just return 0 after result
 */

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int dp[1000005][2];

signed main()
{

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

#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

    string a;
    cin>>a;
    int n=a.length();
    reverse(a.begin(),a.end());
    a="#"+a;
    int ans=0;
    fill_n(&dp[0][0],1000005*2,1e17);
    dp[0][0]=0;
    for(int i=1;i<=n;i++)
    {
        int z=a[i]-'0';
        dp[i][0]=min(dp[i][0],dp[i-1][0]+z);
        dp[i][1]=min(dp[i][1],dp[i-1][0]+(10-z));
        z++;
        dp[i][0]=min(dp[i][0],dp[i-1][1]+z);
        dp[i][1]=min(dp[i][1],dp[i-1][1]+(10-z));
    }
    int mn=min(dp[n][0],dp[n][1]+1);
    cout<<mn;
}


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

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

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

int n, m;
int Fact[200005];
map <int, int> M;

int BinPow(int x, int n)
{
	if(n == 0)
	{
		return 1;
	}
	int temp = BinPow(x, n >> 1);
	if(n & 1)
	{
		return 1ll * temp * temp % mod * x % mod;
	}
	else
	{
		return 1ll * temp * temp % mod;
	}
}

int C(int k, int n)
{
	return 1ll * Fact[n] * BinPow(Fact[k], mod - 2) % mod * BinPow(Fact[n - k], mod - 2) % mod;
}

signed main()
{
//	Fastio();
	cin >> n >> m;
	Fact[0] = 1;
	for(int i = 1; i <= 2e5; i++)
	{
		Fact[i] = 1ll * Fact[i - 1] * i % mod;
	}
	for(int i = 2; i <= sqrt(m); i++)
	{
		while(m % i == 0)
		{
			M[i]++;
			m /= i;
		}
	}
	if(m > 1)
	{
		M[m]++;
	}
	int ans = 1;
	for(auto x : M)
	{
		ans = 1ll * ans * C(n - 1, x.se + n - 1) % mod;
	}
	cout << ans;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;

#define SIZE 4000001

ll kai[SIZE];
ll mokai[SIZE];

ll mod_pow (ll x, ll y) {
    ll ret = 1;
    while (y) {
        if (y&1) ret = ret*x%MOD;
        x = x*x%MOD;
        y /= 2;
    }
    return ret;
}

void init () {
    kai[0] = 1;
    for (int i = 1; i < SIZE; i++) kai[i] = kai[i-1]*i%MOD;
    for (int i = 0; i < SIZE; i++) mokai[i] = mod_pow(kai[i],MOD-2);
}

ll conb (ll x, ll y) {
    ll z = x-y;
    ll ret = kai[x]*mokai[y]%MOD;
    ret = ret*mokai[z]%MOD;
    return ret;
}

ll n, k;
ll dp[2001][2001];

inline ll mmul (ll x, ll y) {
    return x*y%MOD;
}

int main() {
    cin >> n >> k;
    init();
    if (k == 1) {
        cout << 1 << endl;
        return 0;
    }
    dp[0][0] = 1;
    for (ll i = 1; i <= n; i++) {
        dp[i][0] = mmul(dp[i-1][0], conb(i*(k-1)-1,k-2));
    }
    for (ll j = 1; j <= n; j++) {
        for (ll i = j; i <= n; i++) {
            if (i == j) {
                dp[i][j] = dp[i][j-1];
            } else {
                dp[i][j] = (mmul(dp[i-1][j], conb(i*k-(i-j)-1, k-2)) + dp[i][j-1]) % MOD;
            }
        }
    }
    cout << (dp[n][n] * kai[n]) % MOD;
}


#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
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; }

struct UnionFind {
    vector<int> par;
    vector<int> rank;

    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n); rank.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        return true;
    }
};

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

    ll h, w;
    cin>>h>>w;
    vector<string> grid(h);
    cin>>grid;

    UnionFind ds(h * w);
    rep(i, h) rep(j, w) {
        if (i - 1 >= 0 && grid[i][j] != grid[i - 1][j]) ds.merge(i * w + j, (i - 1) * w + j);
        if (i + 1 < h && grid[i][j] != grid[i + 1][j]) ds.merge(i * w + j, (i + 1) * w + j);
        if (j + 1 < w && grid[i][j] != grid[i][j + 1]) ds.merge(i * w + j, i * w + j + 1);
        if (j - 1 >= 0 && grid[i][j] != grid[i][j - 1]) ds.merge(i * w + j, i * w + j - 1);
    }

    vl bs(h * w);
    vl ws(h * w);
    rep(i, h) rep(j, w) {
        if (grid[i][j] == '#') bs[ds.root(i * w + j)]++;
        else ws[ds.root(i * w + j)]++;
    }
    ll ans = 0;
    rep(i, h * w) {
        ans += bs[i] * ws[i];
    }
    cout<<ans<<endl;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)((x).size())
#define jonathan ios_base::sync_with_stdio(0)
#define livingston cin.tie(0)
using namespace std;

void yes() {cout << "Yes" << endl;}
void no() {cout << "No" << endl;}

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

const int INF = 1e9 + 9;
const long long LINF = 1e18 + 8;
const double EPS = 1e-9;
const long long MOD = 1e9 + 7; //998244353
const double PI = acos(-1);

vector<int> g[100010];
bool used[100010];

void dfs(int v) {
    used[v] = true;
    for (auto to : g[v]) {
        if (!used[to]) {
            dfs(to);
        }
    }
}

void solve() {
    int n, k; cin >> n >> k;
    k = min(n, k);
    vector<int> a(n + 1);
    vector<int> d(n + 1, INF);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int res = (a[1] != 1);
    a[1] = 1;
    for (int i = 1; i <= n; i++) {
        g[a[i]].push_back(i);
    }
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < sz(g[v]); i++) {
            int to = g[v][i];
            if (d[to] > d[v] + 1) {
                d[to] = d[v] + 1;
                q.push(to);
            }
        }
    }

    vector<int> s(n);
    for (int i = 0; i < n; i++) {
        s[i] = i + 1;
    }
    sort(all(s), [&](int x, int y) {
        return d[x] > d[y];
    });

    for (int i = 0; i < n; i++) {
        int v = s[i];
        if (used[v]) continue;
        for (int j = 0; j < k - 1; j++) {
            v = a[v];
        }
        dfs(v);
        if (a[v] != 1) {
            res++;
        }
    }

    cout << res << endl;
}

int main() {
    jonathan;
    livingston;
    solve();
    return 0;
}


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

int main(void){
    int N,X;
    cin >> N >> X;
    vector<long long> A(N);
    for(int i = 0; i < N;i++)cin >> A[i];
    long long ans = 0;
    long long b = 0;
    for(int i =1; i < N;i++){
       b = max(0LL,A[i-1]+A[i]-X);
       ans += b;
       A[i]= max(0LL,A[i]-b);
    }
    cout << ans << endl;

}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF=(ll)1e9;
const ll MOD=(ll)1e9+7;
vector<int> dx={1,0,-1,0},dy={0,1,0,-1};

int main(){
  ll N,A,B,ans=0;
  cin>>N>>A>>B;
  ll x[N];
  for(ll i=0;i<N;i++) cin>>x[i];
  for(ll i=0;i<N-1;i++){
    if((x[i+1]-x[i])*A>B) ans+=B;
    else ans+=(x[i+1]-x[i])*A;
  }
  cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define INF 1e9
#define endl '\n'
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define FORE(i, a, b) for (int i = (a), e = (b); i < e; ++i)
#define RFORE(i, a, b) for (int i = (b)-1, e = (a); i >= e; --i)
#define ALL(x) (x).begin(), (x).end()
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).begin(), (c).end(), greater<int>())
using namespace std;
long long digits(long long num) {
    long long digit = num;
    int       d_sum = 0;
    while (digit > 0) {
        ++d_sum;
        digit /= 10;
    }
    return d_sum;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long N;
    cin >> N;
    long long ans = INF;
    for (long long A = 1; A < sqrt(N) + 1; ++A) {
        if (N % A == 0) { ans = min(ans, max(digits(A), digits(N / A))); }
    }
    cout << ans << endl;
}

#include<stdio.h>
#define INF (1<<30)-1
int main(){
  int n;
  scanf("%d",&n);
  int s[n];
  int i,j,c=INF;
  for(i=0;i<n;i++){
    scanf("%d",&s[i]);
    if(s[i]<c) c = s[i];
  }
  for(i=1;i<=c;i++){
    int ok = 0;
    for(j=0;j<n;j++) if(s[j]%i!=0) ok = 1;
    if(ok == 0) printf("%d\n",i);
  }
}


#include <iostream>
using namespace std;

static int num =0;
void swap(int &a,int &b){
    int tmp;
    tmp =a;
    a = b;
    b = tmp;
    num++;
}

void bubbleSort(int a[],int n){
    int flag = 1;
    while(flag){
        flag =0;
        for(int i=n-1;i>0;i--){
            if(a[i]<a[i-1]){
                swap(a[i],a[i-1]);
                flag =1;
            }
        }
    }
}

int main(){
    int n; cin >>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    bubbleSort(a,n);
    for(int i=0;i<n-1;i++){
        cout << a[i] << " ";
    }
    cout << a[n-1] << endl;
    cout << num << endl;

}

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

int main(){
  int n;
  cin>>n;
  int elem[n];
  int cnt=0;
  for(int i=0;i<n;i++){
    cin>>elem[i];
  }
  for(int i=0;i<n-1;++i){
    for(int j=0;j<n-1-i;++j){
      if(elem[j]>elem[j+1]){
	++cnt;
	elem[j]^=elem[j+1];
	elem[j+1]^=elem[j];
	elem[j]^=elem[j+1];
      }
    }
  }
  for(int i=0;i<n;++i){
    cout<<elem[i];
    if(i!=n-1)cout<<" ";
    else cout<<endl;
  }
  cout<<cnt<<endl;
}