#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<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
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> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
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 h1, m1, h2, m2;
    cin >> h1 >> m1 >> h2 >> m2;
    while(h2 > h1) {
        m2 += 60;
        h2--;
    }
    int k;
    cin >> k;
    cout << (m2 - m1) - k << '\n';
}


#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a;
    cin >> a;
    cout << a / 3;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
int counter = 0;
cin >>  N;
for(int i = 0;i < N/3 + 1;i++){
if((N - 3*(i + 1) >= 0)){
counter ++;}
else  cout << counter << endl;}
}


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

int main(){
    int x,y; cin >> x >> y;
    int ans = 0;
    if(x>0){
        if(y>0){
            if(x>y){
                ans++; // x -> -x
                ans += abs(x-y); // x -> -y
                ans++; // x-> y
            }else if(x < y){
                ans+= y-x; // x->y
            }
        }else if(y<0){
            if(abs(x) > abs(y)){
                ans++; // x->-x
                ans+= abs(x) - abs(y); // x->-y
            }else if(abs(x) < abs(y)){
                ans+= abs(y) - abs(x); // x->y
                ans++;   // x -> -y
            }else{
                ans++;
            }
        }else{
            ans++;
            ans+= x;
        }
    }else if(x<0){
        if(y>0){
            if(abs(x)>abs(y)){
                ans += abs(x) -abs(y);
                ans++;
            }else if(abs(x)<abs(y)){
                ans++;
                ans += abs(y) - abs(x);
            }else{
                ans++;
            }
        }else if(y<0){
            if(abs(x)>abs(y)){
                ans += abs(x) -abs(y);
            }else if(abs(x)<abs(y)){
                ans++;
                ans += abs(y) - abs(x);
                ans++;
            }
        }else{
            ans+=abs(x);
        }
    }else{
        if(y>0) ans+=y;
        else if(y<0) ans+= abs(y) + 1;
    }
    cout << ans << endl;
    return 0;
}

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

int main() {
  double current, goal;

  cin >> current >> goal;
  cout << 2 * goal - current << endl; // Solve for x: (current + x) / 2 = goal
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 35000
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);
using ll=long long int  ;
using dl =double;
const int N = 2e5 + 10;
ll aarray[300000+10];
ll magic[101][101];
vector<ll>primes;
bool prime[1000001];
ll chkbit(ll n,ll k)
{
    return n&(1LL<<k);
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll a,b,c,d,e,f,x,y,k,t,A=0,mod,B=0,L,j,i,l,r,m,n,C=0,ans=0,sum=0,sum1=0;
    vector<ll>v,v1;
    queue<ll>qu;
    cin>>n;
    for(i=0; i<n; i++)
        cin>>aarray[i];
    for(i=0; i<64; i++)
    {
        ll cnt=0;
        for(j=0; j<n; j++)
        {
            if(chkbit(aarray[j],i))
                cnt++;
        }
        ll ad=((cnt*(n-cnt))%MOD)*((1LL<<i)%MOD);
        ans=(ans+ad)%MOD;

    }
    cout<<ans<<endl;

    return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

const long long MOD = 1000000007;
long long N;
long long a[100000] = {0};

int main() {
  cin >> N;
  long long ans = 1;
  rep(i, N) {
    long long A;
    cin >> A;
    long long temp;
    if(A == 0) {
      temp = 3 - a[A];
    } else {
      temp = a[A-1] - a[A];
    }
    ans =  ans * temp % MOD;
    a[A]++;
  }
  if(ans <= 0) cout << 0 << endl;
  else cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;cin>>s;
  int n=s.size();
  if(n%2!=0)cout<<"No";
  else
  {int f=0;
    for(int i=0;i<n;i+=2)
  {
   if(s[i]=='h' && s[i+1]=='i')continue;
    else{f++;break;}
  }
   if(f==0)cout<<"Yes";
   else cout<<"No";
  }
  return 0;
}

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

int main() {
    string S;
    cin >> S;
    int cur = 0;
    while (cur < S.size()) {
        if (cur + 1 < S.size() && S[cur] == 'h' && S[cur + 1] == 'i') {
            cur += 2;
        } else {
            break;
        }
    }
    if (cur < S.size()) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

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

    vector<int> A(N);
    int count = 0;
    const int INF = 2000000000;

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

    // 最長減少(等しい場合も含む)部分列の数列の長さがi+1のときの最終要素の最大値dp[i]?
    vector<int> dp;

    // 初期条件
    dp.push_back(A[0]);

    // dp開始
    for(int i = 1; i < N; i++) {
        // dp[i]の右から走査
        auto iter = lower_bound(dp.rbegin(), dp.rend(), A[i]);

        // A[i]が右端の値以下の場合
        if(iter == dp.rbegin()) {
            dp.push_back(A[i]);

        // A[i]より小さい値がdp[i]に存在する場合
        } else {
            *(iter-1) = A[i];
        }
    }

    // dp[i]の大きさが答え
    cout << dp.size() << endl;
	return 0;
}

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int N,M;
    int a[1002]={0},vt[1002]={0};
    cin>>N>>M;
    for(int i=1;i<=N;i++)cin>>a[i];
    for(int i=0;i<M;i++){
        int b;
        cin>>b;
        for(int j=1;j<=N;j++){
            if(a[j]<=b){
                vt[j]++;
                break;
            }
        }
    }
    int ans=0,ansvt=0;
    for(int i=1;i<=N;i++)if(ansvt<vt[i])ans=i,ansvt=vt[i];
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long,long long>
#define ii pair<int,int>
#define mod 1000000007
#define endl '\n'
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);

ll power(ll a,ll b){
	ll ans=1;
	while(b>0){
		if(b%2!=0){
			ans=(ans*a)%mod;
		}
		a=((a%mod)*(a%mod))%mod;
		b>>=1;
	}
	return ans;
}

void sieve(int n){
	bool prime[n+1];
	memset(prime,true,sizeof(prime));
	for(int p=2;p*p<=n;p++){
		if(prime[p]){
			for(int i=p*p;i<=n;i+=p)
				prime[i]=false;
		}
	}
	for(int p=2;p<=n;p++){
		if(prime[p])
			cout<<p<<" ";
	}
}

int ans(int index,int time,int t,vector<pair<int,int>>& v,vector<vector<int>>& value){
	if(index>=v.size())
		return 0;
	if(time>=t)
		return 0;
	if(value[index][time]!=-1)
		return value[index][time];
	int returnVal=0;
	returnVal = v[index].second + ans(index+1,time+v[index].first,t,v,value);
	returnVal = max(returnVal,ans(index+1,time,t,v,value));
	value[index][time]=returnVal;
	return returnVal;
}

int main() {
	IOS;
	int n,t;
	cin>>n>>t;
	vector<pair<int,int>> v;
	for(int i=0;i<n;i++){
		int p,q;
		cin>>p>>q;
		v.push_back({p,q});
	}
	sort(v.begin(),v.end());
	std::vector<vector<int>> va(n+1,vector<int> (t+1,-1));
	cout<<ans(0,0,t,v,va);

	return 0;
}

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <deque>
#include <utility>

int dp1[3004][3004];
int dp2[3004][3004];

int main(){
  int N, T;
  scanf("%d", &N);
  scanf("%d", &T);
  std::vector<int > A(N), B(N);
  int max=-1;
  int maxindex=-1;
  for (int i=0; i<N; i++){
    scanf("%d", &A[i]);
    scanf("%d", &B[i]);
  }
  for (int i=0; i<3004; i++){
    for (int j=0; j<3004; j++){
      dp1[i][j]=-1;
      dp2[i][j]=-1;
    }
  }
  dp1[0][0]=0;
  dp2[N][0]=0;
  for (int i=0; i<N; i++){
    //printf("i1=%d\n", i);
    for (int j=0; j<=T; j++){
      if (j>0) dp1[i+1][j]=std::max(dp1[i+1][j-1], dp1[i+1][j]);
      if (dp1[i][j]!=-1){
	if (j+A[i]<=T) dp1[i+1][j+A[i]]=std::max(dp1[i+1][j+A[i]], dp1[i][j]+B[i]);
	dp1[i+1][j] = std::max(dp1[i+1][j], dp1[i][j]);
      }
    }
  }
  for (int i=N-1; i>=0; i--){
    //printf("i2=%d\n", i);
    for (int j=0; j<=T; j++){
      if (j>0) dp2[i+1][j]=std::max(dp2[i+1][j-1], dp2[i+1][j]);
      if (dp1[i+1][j]!=-1){
	if (j+A[i]<=T) dp2[i][j+A[i]]=std::max(dp2[i][j+A[i]], dp2[i+1][j]+B[i]);
	dp2[i][j] = std::max(dp2[i][j], dp2[i+1][j]);
      }
    }
  }
  int ans = -1;
  for (int k=0; k<N; k++){
    //printf("k=%d\n", k);
    int cand = -1;
    for (int j=1; j<T-1; j++){
      int val1 = std::max(dp1[k][j], 0);
      int val2 = std::max(dp2[k+1][T-j-1], 0);
      cand = std::max(cand, val1+val2);
      //if (val1+val2+B[k]>55) printf("k=%d, j=%d, %d + %d + B[%d](=%d)\n", k, j, dp1[k][j], dp2[k+1][T-j],k, B[k]);
    }
    ans = std::max(ans, cand+B[k]);
  }
  std::cout << ans << std::endl;
  return 0;
}


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

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using pii=pair<int,int>;
using vs=vector<string>;
using vpii=vector<pii>;
using si=set<int>;

#define REP(i,a) for(int i=0;i<a;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rREP(i,a) for(int i=(a)-1;i>=0;i--)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define out(S) cout<<(S)<<endl;
#define beginend(v) (v).begin(),(v).end()
#define IfOut(condition,text) if(condition){out(text);return 0;}
#define IfOutElse(condition,if_text,else_text) if(condition){out(if_text);return 0;}else{out(else_text);return 0;}
#define mod(s) ((s)%(ll)(1e9+7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a*modpow((ll)b,(ll)(1e9+5))))
#define pb(i) push_back(i)
#define fst first
#define snd second
#define Foreach(item,collection) for(auto item:collection)
#define INF 1e10
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
ll sum(vi v) {ll sum=0;Foreach(i,v){sum+=i;}return sum;}
#define ShowAll(v) Foreach(i,v) out(i);

int shouhi(int v,int x,int y){
    double motone=(double)v*100.0/(100.0+x);
    return motone*(100.0+y)/100.0;
}

int main(){
    vi ans;
    int m,nmin,nmax;
    while (cin>>m>>nmin>>nmax,(m||nmin||nmax))
    {
        vi P(m);
        REP(i,m)cin>>P[i];
        int n=nmin-1,pmax=0;
        FOR(i,nmin-1,nmax){
            if(pmax<=P[i]-P[i+1]){
                pmax=P[i]-P[i+1];
                n=i;
            }
        }
        ans.pb(n+1);
    }

    ShowAll(ans);
    return 0;
}

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define MOD(x) (x%(mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int main(){
  int m,mini,maxi,gap,ans;
  int data[200];
  while(cin>>m>>mini>>maxi){
    if(m==0&&mini==0&&maxi==0)break;
    rep(i,m)cin>>data[i];
    ans=mini;gap=0;
    for(int i=mini-1;i<=maxi-1;i++){
      if(abs(data[i]-data[i+1])>=gap){
	gap=abs(data[i]-data[i+1]);
	ans=i+1;
      }
    }
    cout<<ans<<endl;
  }
}


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

class Dice{
public:
	long top, front, right, left, back, bottom;
	Dice(long top, long front, long right, long left, long back, long bottom);
	void north();
	void south();
	void west();
	void east();
	void rightTurn();
	void ChangeTop(long top);
};

Dice::Dice(long top, long front, long right, long left, long back, long bottom){
	this->top = top;
	this->front = front;
	this->right = right;
	this->left = left;
	this->back = back;
	this->bottom = bottom;
}

void Dice::north(){
	long sv[6];
	sv[0] = this->front;
	sv[1] = this->bottom;
	sv[2] = this->right;
	sv[3] = this->left;
	sv[4] = this->top;
	sv[5] = this->back;

	this->top = sv[0];
	this->front = sv[1];
	this->right = sv[2];
	this->left = sv[3];
	this->back = sv[4];
	this->bottom = sv[5];
}

void Dice::south(){
	long sv[6];
	sv[0] = this->back;
	sv[1] = this->top;
	sv[2] = this->right;
	sv[3] = this->left;
	sv[4] = this->bottom;
	sv[5] = this->front;

	this->top = sv[0];
	this->front = sv[1];
	this->right = sv[2];
	this->left = sv[3];
	this->back = sv[4];
	this->bottom = sv[5];
}

void Dice::west(){
	long sv[6];
	sv[0] = this->right;
	sv[1] = this->front;
	sv[2] = this->bottom;
	sv[3] = this->top;
	sv[4] = this->back;
	sv[5] = this->left;

	this->top = sv[0];
	this->front = sv[1];
	this->right = sv[2];
	this->left = sv[3];
	this->back = sv[4];
	this->bottom = sv[5];
}

void Dice::east(){
	long sv[6];
	sv[0] = this->left;
	sv[1] = this->front;
	sv[2] = this->top;
	sv[3] = this->bottom;
	sv[4] = this->back;
	sv[5] = this->right;

	this->top = sv[0];
	this->front = sv[1];
	this->right = sv[2];
	this->left = sv[3];
	this->back = sv[4];
	this->bottom = sv[5];
}

void Dice::rightTurn(){
	long vs[4];
	vs[0] = this->left;
	vs[1] = this->right;
	vs[2] = this->front;
	vs[3] = this->back;
	this->left = vs[2];
	this->right = vs[3];
	this->front = vs[1];
	this->back = vs[0];
}

void Dice::ChangeTop(long top){
	if (top == this->top) return;
	else if (top == this->back) Dice::south();
	else if (top == this->front) Dice::north();
	else if (top == this->left) Dice::east();
	else if (top == this->right) Dice::west();
	else for (long i = 0; i < 2; i++) Dice::north();
	return;
}

int main(){
	bool judge = 1;
	long n;
	cin >> n;
	//n = 4;
	vector< vector<long> > roll(n);
	for (long i = 0; i < n; i++){
		roll[i].resize(6);
		cin >> roll[i][0] >> roll[i][1] >> roll[i][2] >> roll[i][3] >> roll[i][4] >> roll[i][5];
	}
	/*
	roll[0][0] = 1;
	roll[0][1] = 2;
	roll[0][2] = 3;
	roll[0][3] = 4;
	roll[0][4] = 5;
	roll[0][5] = 6;
	roll[1][0] = 1;
	roll[1][1] = 2;
	roll[1][2] = 3;
	roll[1][3] = 4;
	roll[1][4] = 5;
	roll[1][5] = 7;
	roll[2][0] = 6;
	roll[2][1] = 2;
	roll[2][2] = 3;
	roll[2][3] = 4;
	roll[2][4] = 5;
	roll[2][5] = 1;
	roll[3][0] = 2;
	roll[3][1] = 4;
	roll[3][2] = 5;
	roll[3][3] = 6;
	roll[3][4] = 3;
	roll[3][5] = 1;
	*/
	for (long i = 0; i < n; i++){
		for (long j = i + 1; j < n; j++){
			long isum = 0, jsum = 0;
			for (long k = 0; k < 6; k++){
				isum += roll[i][k];
				jsum += roll[j][k];
			}
			//cout << isum << " " << jsum << endl;
			if (isum != jsum) continue;
			Dice *di, *dj;
			di = new Dice(roll[i][0], roll[i][1], roll[i][2], roll[i][3], roll[i][4], roll[i][5]);
			dj = new Dice(roll[j][0], roll[j][1], roll[j][2], roll[j][3], roll[j][4], roll[j][5]);
			di->ChangeTop(roll[0][0]);
			dj->ChangeTop(roll[0][0]);
			//cout << di->top << " " << dj->top << endl;
			for (long k = 0; k < 3; k++) if (di->front != dj->front) dj->rightTurn();
			//cout << di->front << " " << dj->front << endl;
			if (di->top == dj->top && di->front == dj->front && di->right == dj->right && di->left == dj->left && di->back == dj->back && di->bottom == dj->bottom){
				//cout << i << " " << j << endl;
				judge = 0;
				break;
			}
			delete di;
			delete dj;
		}
		if (judge == 0) break;
	}
	if (judge == 1) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>

using namespace std;

int main() {
  int n, m, cost;
  int p[1000];
  while (true) {
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    for (int i = 0; i < n; ++i) {
      cin >> p[i];
    }
    sort(p, p + n);
    cost = accumulate(p, p + n, 0);
    for (int i = n - m; i >= 0; i -= m) {
      cost -= p[i];
    }
    cout << cost << endl;
  }
}

#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <vector>
#include <list>

using namespace std;
using namespace boost;

int main(int argc, char* argv[])
{
	using sprtr = char_separator<char>;
	using tknzr = tokenizer<sprtr>;
	sprtr sep(" ", "", keep_empty_tokens);

	int f, mf, mo;

	string line;
	while(1) {

		int n, m;
		{
			getline(cin, line);
			tknzr tkns(line, sep);
			auto it = tkns.begin();
			n = lexical_cast<int>(*it++);
			m = lexical_cast<int>(*it);
		}

		if( n == 0 && m == 0 ) {
			break;
		}

		list<int> ps;
		{
			getline(cin, line);
			tknzr tkns(line, sep);
			for( auto p : tkns ) {
				//cout << p << endl;
				ps.push_back(lexical_cast<int>(p));
			}
		}

		ps.sort(greater<int>());

		int div = n / m;
		int mod = n % m;

		int sum = 0;
		auto it = ps.begin();
		for( int i = 0; i < div; i++ ) {
			for( int j = 0; j < m; j++ ) {
				auto p = *it++;
				if( j == m-1 ) continue;
				sum += p;
			}
		}

		if( mod ) {
			for( int i = 0; i < mod; i++ ) {
				auto p = *it++;
				sum += p;
			}
		}

		cout << sum << endl;

		ps.clear();

	}

    return 0;
}


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> divs = divisor(M);
  ll ans = 1;
  for (int i = 0; i < divs.size(); i++) {
    if (M / divs[i] >= N) ans = max(ans, divs[i]);
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>

#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FILL0(x) memset(x,0,sizeof(x))

using namespace std;

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

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

bool cmp(P a, P b)
{
    return a.first < b.first;
}

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

    int N;
    ll M;

    cin >> N >> M;

    if(N==1)
    {
        cout  << M << endl;
        return 0;
    }

    ll ans=0;
    vector<P> div;

    for(ll i=2; i*i<=M; i++)
    {
        ll cnt = 0;
        if(M%i==0)
        {
            cnt=M/i;
            div.push_back(P(i, cnt));
            div.push_back(P(cnt, i));
        }
    }

    sort(div.begin(), div.end(), cmp);
    reverse(div.begin(), div.end());

    rep(i, div.size())
    {
        if(div[i].second>=N)
        {
            cout << div[i].first << endl;
            return 0;
        }
    }

    cout << 1 << endl;

    return 0;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;

int n,t;
ll a[105],dp[105][105];

ll solve(int i,int j) {
	if (i==j) return 0;
	if (dp[i][j]) return dp[i][j];
	dp[i][j]=INF;
	for (int k=i+1;k<=j;k++)
		dp[i][j]=min(dp[i][j],solve(i,k-1)+solve(k,j)+a[i]*a[k]*a[j+1]);
	return dp[i][j];
}

int main() {
	cin>>n;
	for (int i=1;i<=n;i++) if (i==n) cin>>a[i]>>a[i+1];
	else cin>>a[i]>>t;
	solve(1,n);
	cout<<dp[1][n]<<'\n';
}

#include <bits/stdc++.h>
#define pb push_back
#define vii vector<int>
#define task "KMULT"
#define pll pair<ll, ll>
#define pii pair< ll, pair< ll, ll > >
#define fi first
#define se second

using ll = long long;
using ld = long double;
using ull = unsigned long long;
const ll mod = 998244353;
using namespace std;
const int N = 6e5+5;
ll m, n, k, t, T, ans, u, v, tong;
ll a[N], b[N], st[N*4], lazy[N*4];
string s;
vector<ll> kq;
struct dang
{
    ll l, r, pos;
} p[N];
bool cmp(const dang& X, const dang& Y)
{
    return (X.r < Y.r) || (X.r == Y.r && X.l < Y.l);
}
void sol()
{
    cin >> n;
    pll q;
    for(int i = 1; i <= n; i ++)
    {
        cin >> p[i].l >> p[i].r;
        if(p[i].l > q.fi)
        {
            q.fi = p[i].l;
            q.se = p[i].r;
        }
        else if(p[i].l == q.fi && p[i].r > q.se)q.se = p[i].r;
    }
    sort(p+1, p+1+n, cmp);
    ans = p[1].r - p[1].l + 1;
    for(int i = 1; i <= n; i ++)a[i] = max(a[i-1], p[i].l);
    for(int i = 2; i <= n; i ++)
    {
        if(p[i].r <= q.se)
        {
            ans = max(ans, max((ll)0, p[i].r - q.fi + 1) + max((ll)0, p[1].r - a[i-1] +1 ) );
        }
        if(p[i].l != q.fi || p[i].r != q.se)ans = max(ans, max(p[1].r - q.fi + 1, (ll)0) + p[i].r - p[i].l + 1);
    }
    cout << ans;

}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(fopen(task".INP", "r"))
    {
		freopen(task".INP","r",stdin);
		freopen(task".OUT","w",stdout);
    }
    sol();
    //cout << chap(4, 3);
}


#include <stdio.h>

int main(){

  int x;
  int y;

  scanf("%d", &x);

  y = x*x*x;

  printf("%d\n", y);

}

#include <stdio.h>
#include <string.h>

int main() {

	int angka1=0,angka2=0;
	scanf("%d %d",&angka1,&angka2);
	int hitung = 0;
	hitung = angka1*angka2;
	printf("%d\n",hitung);

	return 0;
}


#include<cstdio>

int main(){
	char str[20];
	char c;
	int i = -1;
	scanf("%c", &c);
	while(c - 10){
		i++;
		str[i] = c;
		scanf("%c", &c);
	}
	for(; i >= 0; i--){
		printf("%c", str[i]);
	}
	printf("\n");
	return 0;
}

#include<iostream>
using namespace std;

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

	i = a;
	a = b;
	b = i;

	j = a;
	a = c;
	c = j;

	cout << a << " " << b << " " << c;
	return 0;
}


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

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

  string S;
  map<int, int> mp;
  int total_acs = 0;
  rep(i,0,N-1) {
    char c;
    cin >> c;
    if (S.back() == 'A' && c == 'C') {
      total_acs++;
    }
    mp[i] = total_acs;
    S.push_back(c);
  }

  vector<int> answers;
  rep(i,0,Q-1) {
    int l, r;
    cin >> l >> r;
    int answer = mp[r-1] - mp[l-1];
    answers.push_back(answer);
  }

  for (int answer: answers)
    cout << answer << endl;

  return 0;
}

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
using namespace std;

int main()
{
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    int a[N];
    a[0] = 1;
    for (int i = 1; i < n; ++i)
    {
        if (s[i] == 'C' && s[i - 1] == 'A')
        {
            a[i] = a[i - 1] + 1;
        }
        else
        {
            a[i] = a[i - 1];
        }
    }
    int l, r;
    rep(i, q)
    {
        cin >> l >> r;
        cout << a[r - 1] - a[l - 1] << endl;
    }
    return 0;
}


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

int main(void){
  int AB[2], CD[2], value;
  for(int i = 0; i < 2; i++)
    cin >> AB[i];
  for(int i = 0; i < 2; i++)
    cin >> CD[i];
  sort(begin(AB), end(AB));
  sort(begin(CD), end(CD));
  value = AB[0] + CD[0];
  cout << value << "\n";
  return 0;
}

#include <iostream>

using namespace std;

int main()
{
    int A , B , C , D;
    cin >> A >> B >> C >> D;

    if      ((A >= B) && (C >= D)) {cout << B + D;}
    else if ((A <= B) && (C >= D)) {cout << A + D;}
    else if ((A >= B) && (C <= D)) {cout << B + C;}
    else if ((A <= B) && (C <= D)) {cout << A + C;}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

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

  int e;
  while(cin >>e,e){
    int ans=1000000;
    for(int y=0;y*y<=e;y++){
      for(int z=0;z*z*z<=e-y*y;z++){
        int x=e-z*z*z-y*y;
        ans = min(ans,x+y+z);
      }
    }
    cout << ans << endl;
  }
}

#include <bits/stdc++.h>

using namespace std;

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

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T In(){ T x; cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

int main()
{
  while (int e = in()){
    int m = 1e8;
    for (int z = 0; z <= 100; ++z){
      for (int y = 0; y <= 1000; ++y){
        int x = e - (sq(z) * z + sq(y));
        if (x < 0) continue;
        m = min(m, z + y + x);
      }
    }
    printf("%d\n", m);
  }
  return (0);
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#include <iterator>
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<int,int> pii;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 2e5+50;
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define rep0(i,l,r) for(int i=l;i<r;++i
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }

int solve(){
    int n;
    cin>>n;
    vector<int> vec(n);
    for(int i=0;i<n;++i)
         cin>>vec[i];
    set<int> st;
    for(int i=0;i<vec.size();++i){
         if(st.find(vec[i])==st.end()){
             st.insert(vec[i]);
         }
         else
            return 1;

    }
    return 0;

}
int main(){

   ios::sync_with_stdio(false);
  cin.tie(0);
     if(solve()==1)
        cout<<"NO"<<endl;
     else
        cout<<"YES"<<endl;
}

#include<bits/stdc++.h>
using namespace std;
int counter[200005];
bool balls[200005];
bool picked[200005];
void solve(int k){
	for(int i=1; i*i<=k; i++){
		if(k%i==0){
			counter[i]++;
			if(i!=k/i) counter[k/i]++;
		}
	}
}
int main(){
	int n;
	cin>>n;
	int ret = 0;
	for(int i=1; i<=n; i++) cin>>balls[i];
	for(int i=n; i>=1; i--){
		if(balls[i] && counter[i]%2) continue;
		if(!balls[i] && counter[i]%2==0) continue;
		picked[i] = true;
		ret++;
		solve(i);
	}
	cout<<ret<<'\n';
	for(int i=1; i<=n; i++){
		if(picked[i]) cout<<i<<" ";
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
static const long long INFTY = (1LL << 32);
int main()
{
	int n, m, p, q, r,i,j,k,a,b,cost,time;
	long long cost_d[101][101], time_d[101][101];
	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0)break;
		for (i = 1; i <= m; i++) {
			for (j = 1; j <= m; j++) {
				cost_d[i][j] = ((i == j) ? 0 : INFTY);
				time_d[i][j] = ((i == j) ? 0 : INFTY);
			}
		}
		for (i = 0; i < n; i++) {
			cin >> a >> b >> cost >> time;
			cost_d[a][b] = cost;
			cost_d[b][a] = cost;
			time_d[a][b] = time;
			time_d[b][a] = time;
		}
		for (k = 1; k <= m; k++) {
			for (i = 1; i <= m; i++) {
				if (cost_d[i][k] == INFTY)continue;
				for (j = 1; j <= m; j++) {
					if (cost_d[k][j] == INFTY)continue;
					cost_d[i][j] = min(cost_d[i][j], cost_d[i][k] + cost_d[k][j]);
					time_d[i][j] = min(time_d[i][j], time_d[i][k] + time_d[k][j]);
				}
			}
		}
		cin >> k;
		for (i = 0; i < k; i++) {
			cin >> p >> q >> r;
			if (r == 0) {
				cout << cost_d[p][q]<<endl;
			}
			else {
				cout << time_d[p][q] << endl;
			}
		}
	}
	return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	string s, t, r;
	int n, a, b;
	cin >> s >> n;
	while (n--) {
		cin >> t >> a >> b;
		if (t == "print")
			cout << s.substr(a, b - a + 1) << endl;
		else if (t == "reverse")
			reverse(s.begin() + a, s.begin() + b + 1);
		else {
			cin >> r;
			s.replace(a, b - a + 1, r);
		}
	}
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int main()
{
    int m; cin >> m;
    vector<ll> d(m), c(m);
    for(int i = 0; i < m; i++)cin >> d[i] >> c[i];
    ll res = 0, ans = -1;
    for(int i = 0; i < m; i++) {
        res += c[i] * d[i];
        ans += c[i];
    }
    while(res > 9) {
        ans += res / 10;
        res = res / 10 + (res % 10);
    }
    cout << ans << endl;
}

#pragma once
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int t[101];
int pri[26];
int res = 0;
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (px < py) {
		pr[py] = px;
	}
	else {
		pr[px] = py;
	}
	return true;
}
void solv() {

	int n;
	cin >> n;
	uini(n);
	pair<ll, int> xr[100010];
	pair<ll, int> yr[100010];
	for (size_t i = 1; i <= n; i++)
	{
		ll x, y;
		cin >> x >> y;
		xr[i] = pair<ll, int>(x, i);
		yr[i] = pair<ll, int>(y, i);
	}

	sort(xr, xr + n + 1);
	sort(yr, yr + n + 1);
	map<ll, vector<pair<int, int>>> mx;
	for (int i = 2; i <= n; i++)
	{
		ll rem = abs( xr[i].first - xr[i - 1].first);
		mx[rem].push_back(pii(xr[i].second, xr[i - 1].second));

		ll remy = abs(yr[i].first - yr[i - 1].first);
		mx[remy].push_back(pii(yr[i].second, yr[i - 1].second));
	}
	ll res = 0;
	for (map<ll, vector<pii>>::iterator i = mx.begin(); i != mx.end(); i++)
	{
		ll val = i->first;
		vector<pii> v = i->second;
		for (int j = 0; j < v.size(); j++)
		{
			if (unit(v[j].first, v[j].second)) {
				res += val;
			}
		}
	}
	cout << res << endl;
}

int main() {
	solv();

	return 0;
}


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<cstring>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<tuple>
#include<list>
#include<unordered_map>
#include<random>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) v.erase(unique(v.begin(),v.end()),v.end())
// #define int ll
using namespace std;
const ll INF = (1 << 30 ) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 1100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[4] ={1,0,-1,0} , dy[4] ={0,1,0,-1};

mat mul(mat &A, mat &B){
    mat C(A.size(), vll(B[0].size()));
    rep(i,A.size()){
        rep(k,B.size()){
            rep(j,B[0].size()){
                C[i][j]=(C[i][j]^(A[i][k]&B[k][j]));
            }
        }
    }
    return C;
}

mat pow(mat A,ll n){
    mat B(A.size(), vll(A.size()));
    rep(i,A.size()){
        B[i][i]=(1ll<<32)-1;
    }
    while(n>0){
        if(n&1) B=mul(B,A);
        A=mul(A,A);
        n>>=1;
    }
    return B;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    string s;
    cin>>s;
    rep(i,s.size()-8){
        cout<<s[i];
    }
    cout<<endl;
    return 0;
}

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

int main(){
    string s; cin >> s;
    int len = s.length();
    for(int i = 0; i < len-8; i++){
        cout << s[i];
    }
    cout << endl;
    return 0;
}


///FUCKOUT
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

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

#define pb push_back
#define F first
#define S second
#define f(i,a,b)  for(int i = a; i < b; i++)
//#define endl '\n'

using ll = long long;
using db = long double;
using row = vector<int>;
using ii = pair<ll, ll>;

#define int ll

const int N = 2e5+5, M = 1e6 + 5, LG = 17, MOD = 1e6 + 3;
const int BLOCK = 55;
const int BLOCKN = N / BLOCK + 1;
const long double EPS = 1e-7;
using ull = unsigned long long;
ll t[N<<2];
ll tA[N<<2], tB[N<<2];
void upd(int node, int s, int e, int idx, ll v){
  t[node] = min(t[node], v);
  tA[node] = min(tA[node], v + idx);
  tB[node] = min(tB[node], v - idx);
  if(s==e)return;
  int md = (s+e)>>1;
  if(idx<=md)
    upd(node<<1,s,md,idx,v);
  else
    upd(node<<1|1,md+1,e,idx,v);
}
ll qry(int node, int s, int e, int idx){
  if(e < idx)return idx + tB[node];
  if(s > idx)return tA[node] - idx;
  if(s==e)return t[node];
  int md = (s+e)>>1;
  return min(qry(node<<1,s,md,idx),qry(node<<1|1,md+1,e,idx));
}
int32_t main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif // ONLINE_JUDGE
  memset(t,63, sizeof t);
  memset(tA,63, sizeof tA);
  memset(tB,63, sizeof tB);
  int n, q, A, B;
  cin >> n >> q >> A >> B;

  upd(1, 1, n, A, 0);

  int prv = B;
  ll ans = 0;
  while(q--){
    int x;
    cin >> x;
    ll val = qry(1, 1, n, x) + ans;
    ans += abs(x - prv);
    upd(1, 1, n, prv, val - ans);
    prv = x;
  }

  cout << ans+t[1]<<'\n';
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define int long long
int hmt() {int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';if(n) x=-x;return x;}
#define in hmt()
#define ins ({string x;char c=getchar();for(;c==' '||c=='\n';c=getchar());for(;c!=' '&&c!='\n';c=getchar()) x+=c;x;})
#define forinc(i,a,b) for(int i=a,_b=b;i<=_b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define forb(i,BS) for(int i=BS._Find_first();i< BS.size();i = BS._Find_next(i))
#define forv(a,b) for(auto &a:b)
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define reset(f,x) memset(f,x,sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define onbit(x,i) (x|(1<<(i-1)))
#define offbit(x,i) (x&~(1<<(i-1)))
const int N=200010;
int n,Q,A,B,a[N],oo,s[N],f[N],ans;
struct bit
{
	int t[N];
	void rs()
	{
	    reset(t,127);oo=t[0];
	}
	void upd(int i,int x)
	{
		for(;i<=n;i+=i&-i) t[i]=min(t[i],x);
	}
	int get(int i)
	{
		int g=oo;
		for(;i;i-=i&-i) g=min(g,t[i]);
		return g;
	}
}T[2];
void add(int i,int x)
{
	T[0].upd(i,x-i);
	T[1].upd(n-i+1,i+x);
}
main()
{
    //freopen("C.inp","r",stdin);
	n=in,Q=in,A=in,B=in;
	forinc(i,1,Q) a[i]=in;
	forinc(i,2,Q) s[i]=s[i-1]+abs(a[i]-a[i-1]);
	T[0].rs();T[1].rs();
	add(B,abs(A-a[1]));
	add(A,abs(B-a[1]));
	ans=s[Q]+min(abs(A-a[1]),abs(B-a[1]));
	forinc(i,2,Q)
	{
		f[i]=min(T[0].get(a[i])+a[i],T[1].get(n-a[i]+1)-a[i])+s[i-1];
		add(a[i-1],f[i]-s[i]);
		ans=min(ans,f[i]+s[Q]-s[i]);
	}
	cout<<ans<<"\n";
}


#include<iostream>
using namespace std;
int main(void){
    long long n,a[10000],min=1000000,max=-1000000,sum=0;

    cin>>n;
    for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i]<min){min=a[i];}
    if(a[i]>max){max=a[i];}
    sum=a[i]+sum;
    }
    cout<<min<<" "<<max<<" "<<sum<<endl;

}

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

using namespace std;

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 INF=1e9+7;

int main(){
    ll n;
    cin >> n ;
    vector<string> a(3);
    rep(i,3){
        cin >> a[i];
    }
    ll ans=0;
    rep(i,n){
        set<char> s;
        rep(j,3){
            s.insert(a[j][i]);
        }
        ans+=s.size()-1;
    }
    cout << ans << endl;
}


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

#define MAXN 200001

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    vector<int64_t> a(n), dp(n), os(n);
    for (auto &i: a) cin >> i;
    for (int i = 0; i < n; ++i) {
        os[i] = (i ? os[i-1] : 0) + (i % 2 ? 0 : a[i]);
    }
    for (int i = 0; i < n; ++i) {
        if (i) dp[i] = a[i] + (i > 1 ? dp[i-2] : 0);
        if (i % 2) {
            dp[i] = max(dp[i], os[i-1]);
        } else if (i){
            dp[i] = max(dp[i], dp[i-1]);
        }
    }
    cout << dp[n-1];
}

#include <bits/stdc++.h>
#define sp ' '
#define nyan "(=^・ω・^=)"
#define mkp make_pair
#define intmax 2147483647
#define llmax 9223372036854775807
#define lP pair<ll,ll>
#define iP pair<int,int>
typedef long long ll;
using namespace std;
const int mod = 1000000007;

int N, A, B, C, D;
ll DP[1001], E869120[1001];

ll pw(ll x, int y) {
	ll a = 1;
	while (y) {
		if (y & 1)
			a = a * x%mod;
		x = x * x%mod;
		y >>= 1;
	}
	return a;
}
ll modinv(ll x) {
	return pw(x, mod - 2);
}

int main() {
	cin >> N >> A >> B >> C >> D;
	DP[0] = 1;
	E869120[0] = 1;
	for (int i = 1; i != N + 1; ++i)
		E869120[i] = E869120[i - 1] * i%mod;
	for (int i = A; i != B + 1; ++i) {
		for (int j = N; j != -1; --j) {
			for (int k = C; k != D + 1 && j + i * k <= N; ++k) {
				DP[j + i * k] = (DP[j + i * k] + DP[j] * E869120[N - j] % mod*modinv(E869120[i*k] * E869120[N - j - i * k] % mod*pw(E869120[i], k) % mod*E869120[k] % mod) % mod*E869120[i*k]) % mod;
			}
		}
	}
	cout << DP[N] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define __lcm(a, b) a * b / __gcd(a, b)
// #define multicase
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 2e5 + 5;

int x;

void solve() {
    int T = 1;
  #ifdef multicase
    cin >> T;
  #endif
    while(T--) {
        cin >> x;
        cout << (x < 1200 ? "ABC\n" : x < 2800 ? "ARC\n" : "AGC\n");
    }
}

int main() {

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

    solve();

}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;

int main(){

  int r; cin>>r;
  if(r<1200) cout<<"ABC"<<endl;
  else if(r<2800)cout<<"ARC"<<endl;
  else cout<<"AGC"<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pi acos(0.0)
#define ll long long
#define f first
#define s second
#define gap ' '
#define pb push_back
#define mod 1000000007
int main()
{
    ll n,m;
    cin>>n>>m;
    ll x[n+5],y[m+5];
    for(int i=0;i<n;i++)
    {
        cin>>x[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>y[i];
    }
    sort(x,x+n);
    sort(y,y+m);
    ll xx=0;
    ll yy=0;
    for(int i=0;i<n;i++)
    {
        xx+=(i-(n-1))*x[i]+i*x[i];
        //cout<<xx<<endl;
    }

    for(int i=0;i<m;i++)
    {
        yy+=(i-(m-1))*y[i]+i*y[i];
    }

    ll ans=((xx%mod)*(yy%mod))%mod;
    cout<<ans;

}

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
double PI = 3.1415926535897932;
long mod = 1000000007;
const long INF = 2000000000;

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

  long sum[n];
  sum[n-1] = a[n-1];
  for(int i = n-2; i >= 0; i--)
    sum[i] = sum[i+1] + a[i];

  long ans = 0;
  for(int i = 0; i < n-1; i++)
    ans += (sum[i+1] % mod * a[i]) % mod;

  cout << ans % mod << endl;
}


#include <bits/stdc++.h>
#define len(x) ((int)(x).size())

using namespace std;

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

  int N, M;
  cin >> N >> M;

  // [
  //   (count_WA_before_passed, is_passed)
  // ]
  vector<pair<int, bool>> contest_result(105000, make_pair(0, false));

  int WA_count = 0;
  int AC_count = 0;

  while (M--) {
    int p;
    string S;
    cin >> p >> S;
    if (contest_result[p].second) continue;

    if (S == "AC") {
      contest_result[p].second = true;
      AC_count++;
      WA_count += contest_result[p].first;
    } else if (S == "WA") {
      contest_result[p].first++;
    }
  }

  cout << AC_count << " " << WA_count << endl;
}

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

int main(){
    while(true){
        int n, q, m, d;
        int mxd=0;
        set<int> s[50];
        int date=0;
        cin >> n >> q;
        if(!(n||q)) break;
        --q;
        for(int i=0;i<n;++i){
            cin >> m;
            for(int j=0;j<m;++j){
                cin >> d;
                s[i].insert(d);
                mxd = max(mxd, d);
            }
        }
        for(int i=1;i<=mxd;++i){
            int cnt=0;
            for(int j=0;j<n;++j){
                if(s[j].find(i)!=s[j].end()) ++cnt;
            }
            if(q<cnt){
                q = cnt;
                date = i;
            }
        }
        cout << date << endl;
    }
}


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

int main()
{
    int n, x, t;
    cin >> n >> x >> t;
    cout << (n+(x-1))/x*t << endl;
    return 0;
}

/**
 * ABC042 C
 *
 * @date 2020.05.03
 * @author 焼き鮭 <yaki-shake@up-tri.me>
 */

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

int target;
bool *disallow;

bool is_ok(int target)
{
  int remain_size = std::to_string(target).length();
  while (remain_size != 0)
  {
    int d = target % 10;
    if (disallow[d])
    {
      return false;
    }

    target = target / 10;
    remain_size -= 1;
  }
  return true;
}

int main(int argc, char const *argv[])
{
  int k;

  std::cin >> target;
  std::cin >> k;
  disallow = (bool *)malloc(k * sizeof(bool));

  for (int i = 0; i < k; i++)
  {
    int tmp;
    std::cin >> tmp;
    disallow[tmp] = true;
  }

  int ans = target;
  while (true)
  {
    if (is_ok(ans))
    {
      break;
    }

    ans++;
  }

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


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
    ll n, m, i, j, k;
    cin >> n >> m;
    vector<ll> x(n), y(m);
    for(i = 0; i < n; i++) {
        cin >> x[i];
    }
    for(i = 0; i < m; i++) {
        cin >> y[i];
    }
    ll retx = 0;
    for(k=0;k<n;k++){
        retx = (retx + k*x[k] - (n-1-k)*x[k])%MOD;
    }
    ll rety=0;
    for(k = 0; k < m; k++) {
        rety = (rety + k * y[k] - (m - 1 - k) * y[k]) % MOD;
    }

    cout << (rety*retx)%MOD << endl;
    return 0;
}

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <assert.h>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <math.h>
#include <string>
#include <string.h>
#include <queue>
#include <complex>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
ll mod=1e9+7;
double eps=1e-7;
ll exp(ll x,ll y){if(y<0) return 0; ll ret=1;for(;y;y>>=1,x=(x*x)%mod){if(y&1)ret=(ret*x)%mod;}return ret;}
ull pexp(ull x,ull y){if(y<0) return 0; ull ret=1; for(;y;y>>=1,x=(x*x)){if(y&1)ret=(ret*x);}return ret;}
ll gcd(ll x,ll y){if(!x||!y) return x+y; return x%y==0?y:gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll bsum(ll u,ll b){ll ret=0;if(u<b)return u;while(u){ret+=u%b;u/=b;}return ret;}
ll prival(ll u,ll p){ll cn=0;while(u%p==0){cn++;u=u/p;}return cn;}
ll minv(ll a,ll b){return 1<a?b-minv(b%a,a)*b/a:1;}
ll extm(ll a,ll b){ll ret=0;while(a!=0){if(a%2==1){ret+=b;ret%=mod;}a>>=1;b=(2*b)%mod;}return ret;}
ll eaphi(ll x){ll t=x,ret=x,i;for(i=2;i*i<=x;i++){if(t%i==0){ret-=ret/i;while(t%i==0) t/=i;}}if(t!=1) ret-=ret/t;return ret;}
ll eadivc(ll x){ll ret=0;ll i;for(i=1;i*i<=x;i++){if(x%i==0 && i*i!=x) ret+=2;if(x%i==0 && i*i==x) ret+=1;}return ret;}
ll eadivs(ll x){ll ret=0;ll i;for(i=1;i*i<=x;i++){if(x%i==0 && i*i!=x) ret+=i+x/i;if(x%i==0 && i*i==x) ret+=i;}return ret;}
ll ndig(ll x, ll b){ll ret=0;while(x){x/=b; ret++;}return ret;}
ll rev(ll n, ll b){ll ret=0;while(n){ret=b*ret+n%b; n/=b;}return ret;}
ll sq(ll x){ll t=(ll)sqrt(x); for(ll i=t-2 ; i<=t+2 ; i++) if(i*i==x) return abs(i); return -1;}
ll extexp(ll x,ll y){if(y<0) return 0; ll ret=1;for(;y;y>>=1,x=extm(x,x)){if(y&1)ret=extm(ret,x);}return ret;}
bool isprime(ll x){if(x<=1) return false; for(ll i=2;i*i<=x;i++){if(x%i==0){return false;}}return true;}
ll ans, x, y, n, m;
ll u[111111], v[111111];

int main(void)
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin>>n>>m; int i;
	for(i=1 ; i<=n ; i++) cin>>u[i];
	for(i=1 ; i<=m ; i++) cin>>v[i];
	for(i=1 ; i<=n ; i++) x+=((mod+2LL*i-n-1)*(mod+u[i]))%mod;
	for(i=1 ; i<=m ; i++) y+=((mod+2LL*i-m-1)*(mod+v[i]))%mod;
	x%=mod; y%=mod; cout<<(x*y)%mod; return 0;
}

// it may be easier than you think
// it may be harder than you think
// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);


#include<bits/stdc++.h>
#include<tr1/unordered_map>
#define ri register int
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> poly;
#define pb push_back
const int rlen=1<<18|1,inf=0x3f3f3f3f;
const ll Inf=1e18;
char buf[rlen],*ib=buf,*ob=buf;
#define gc() (((ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin)),ib==ob)?-1:*ib++)
inline int read() {
  int ans=0;
  bool f=1;
  char ch=gc();
  while(!isdigit(ch)) f^=ch=='-',ch=gc();
  while(isdigit(ch)) ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
  return f?ans:-ans;
}
inline ll readl() {
  ll ans=0;
  bool f=1;
  char ch=gc();
  while(!isdigit(ch)) f^=ch=='-',ch=gc();
  while(isdigit(ch)) ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
  return f?ans:-ans;
}
inline int Read(char*s) {
  int tp=0;
  char ch=gc();
  while(!isdigit(ch)&&!isalpha(ch)) ch=gc();
  while(isdigit(ch)||isalpha(ch)) s[++tp]=ch,ch=gc();
  return tp;
}
namespace modular {
  const int mod=1e9+7;
  inline int add(int a,int b) { return a+b<mod?a+b:a+b-mod; }
  inline int dec(int a,int b) { return a<b?a-b+mod:a-b; }
  inline int mul(int a,int b) { return (ll)a*b%mod; }
  inline void Add(int&a,int b) { a=a+b<mod?a+b:a+b-mod; }
  inline void Dec(int&a,int b) { a=a<b?a-b+mod:a-b; }
  inline void Mul(int&a,int b) { a=(ll)a*b%mod; }
  inline int ksm(int a,int p) { int ret=1;for(;p;p>>=1,Mul(a,a)) (p&1)&&(Mul(ret,a),1);return ret; }
  inline int Inv(int a) { return ksm(a,mod-2); }
  inline int sqr(int a) { return mul(a,a); }
  inline int cub(int a) { return (ll)a*a%mod*a%mod; }
}
using namespace modular;
template<typename T> inline void ckmax(T&a,T b) { a<b?a=b:0; }
template<typename T> inline void ckmin(T&a,T b) { a>b?a=b:0; }
template<typename T> inline T gcd(T a,T b) { T t;while(b)t=a,a=b,b=t-t/a*a;return a; }
template<typename T> inline T Abs(T x) { return x<0?-x:x; }
inline int rd() { return rand()|(rand()<<15); }
const int N=5e5+5;
int n,pre[N],suf[N];
pii a[N];
int main() {
  #ifdef ldxcaicai
  freopen("lx.in","r",stdin);
  #endif
  n=read();
  for(ri i=1;i<=n;++i) a[i].fi=read(),a[i].se=read();
  sort(a+1,a+n+1);
  pre[0]=0x3f3f3f3f,suf[n]=0x3f3f3f3f;
  for(ri i=1;i<n;++i) pre[i]=min(pre[i-1],a[i].se);
  for(ri i=n-1;i;--i) suf[i]=min(suf[i+1],a[i].se);
  int res=0;
  for(ri mn,r1,r2,t,i=1;i<n;++i) {
    r1=a[i].se,r2=min(max(suf[i+1],a[n].fi-1),a[n].se);
    t=(r1-a[i].fi+1)+(r2-a[n].fi+1);
    mn=pre[i-1];
    ckmax(res,t-max(0,min(r1-max(a[i].fi-1,mn),r2-max(a[n].fi-1,mn))));
  }
  cout<<res;
  return 0;
}

/*
 * @Author: RBQRBQ
 * @Date: 2020-04-05 15:08:15
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2020-04-05 19:16:50
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll,ll> pii;
namespace IO{
    template<typename T>inline void read(T &x){
        x=0;ll f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
        x=x*f;
    }
}
using namespace IO;
vector<pii> pp;
vector<pii> T;
ll mxL,minR,mxR,minL;

int main()
{
    int N;
    cin>>N;
    mxL=mxR=-1;
    minL=minR=10000000000ll;
    int P,Q;
    for(int i=1;i<=N;i++)
    {
        ll x,y;
        read(x),read(y);
        y++;
        pp.push_back(make_pair(x,y));
        if(x>mxL)
        {
            mxL=x;
            P=i;
        }
        if(minR>y)
        {
            minR=y;
            Q=i;
        }
    }
    ll ans1=0,ans2=0;
    minL=0;
    for(int i=1;i<=N;i++)
    {
            minL=max(minL,pp[i-1].second-pp[i-1].first);
    }

    ans1=max((minL),0ll)+max((minR-mxL),0ll);

    int ca=1;
    for(auto p:pp)
    {
        if(ca!=P&&ca!=Q)
        T.push_back(make_pair(max(p.second-mxL,0ll),max(minR-p.first,0ll)));
        ca++;
    }
    sort(T.begin(),T.end());
    ll P1=pp[P-1].second-pp[P-1].first;
    ll Q1=pp[Q-1].second-pp[Q-1].first;
    ll x1=0,x2=Q1;
    // for(auto p:T)
    // {
    //     cout<<p.first<<" "<<p.second<<endl;
    // }
    for(int i=0;i<T.size();i++)
    {

        x1=min(T[i].first,P1);
        ans2=max(ans2,x1+x2);
        x2=min(T[i].second,x2);
    }
    ans2=max(ans2,P1+x2);
    cout<<max(ans1,ans2);

}


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

int main(void) {
	int N;
	scanf("%d", &N);
	map<int, int> m;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		int A;
		scanf("%d", &A);
		if (m[A] == 1) {
			m[A] = 0;
			ans--;
		} else {
			m[A] = 1;
			ans++;
		}
	}
	printf("%d\n", ans);
	return 0;
}

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

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

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll prev = 0;
    vector<ll> A(5);
    ll k;
    rep(i, 5) cin >> A[i];
    cin >> k;
    rep(i, 4) {
        rep2(j, i + 1, 5) {
            if(A[j] - A[i] > k) {
                cout << ":(" << endl;
                return 0;
            }
        }
    }
    cout << "Yay!" << endl;
    return 0;
}


#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
	int count = 1000000;
	for (int i = 0; i < count; i++) {
		int e = 0;
		cin >> e;
		if (e == 0){
			return 0;
		}
		int min = e;
		for (int z = 0; z*z*z <= e; z++) {
			for (int y = 0; y*y <= e - z*z*z; y++) {
				int x = e - y*y - z*z*z;
				int m = x + y + z;
				if (m < min){
					min = m;
				}
			}
		} //
		cout << min << endl;
	} //
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <initializer_list>

using namespace std;

int main()
{
    int m, e, y, z;
    int min;
    while (cin >> e, e) {
        m = min = 1000000;
        for (z = 0; z <= 100; z++) {
            if (z*z*z > e)
                break;
            for (y = 0; y <= 1000; y++) {
                if (y*y + z*z*z > e) {
                    y--;
                    m = y + z + (e-y*y-z*z*z);
                    min = m < min ? m : min;
                    break;
                }
            }
        }
        cout << min << endl;
    }

    return 0;
}


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

int main()
{
    ll n;
    cin >> n;
    ll a[n], b[n], c[n];
    for(int i=0; i<n; i++) cin >> a[i];
    for(int i=0; i<n; i++) cin >> b[i];
    for(int i=0; i<n; i++) cin >> c[i];

    sort(a, a+n);
    sort(b, b+n);
    sort(c, c+n);

    ll ans = 0;

    for(ll i=0; i<n; i++){
        ll u = upper_bound(a, a+n, b[i]-1) - a;
        ll l = lower_bound(c, c+n, b[i]+1) - c;

        ans += u * (n-l);
    }
    cout << ans << endl;

}

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

int N;
int A[3][100001];
LL dp[3][100001] = {};

int main() {
    scanf("%d", &N);
    rep(j, 3) { rep(i, N) scanf("%d", &A[j][i]); A[j][N] = 1000000001; }
    rep(j, 3) sort(A[j], A[j] + N);
    rep(i, N + 1) dp[0][i] = i;
    rep(j, 2) {
        int p = 0;
        rep(i, N) {
            while (A[j][p] < A[j + 1][i]) p++;
            dp[j + 1][i + 1] += dp[j + 1][i] + dp[j][p];
        }
    }
    printf("%lld\n", dp[2][N]);
    return 0;
}


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

class dice{
public:
	int init_label[6];
    int label[6];
    int tmp1, tmp2;
    int order[4] = {1,2,4,3};
	void setnumber(){
		for(int i=0; i<6; i++)
			cin >> init_label[i];
    }

    int check(int over, int front, int right, int left, int back, int bottom){
    	for(int i=0; i<4; i++){
    		if(over == label[0] && front == label[order[i]] && right == label[order[(i+1)%4]] && back == label[order[(i+2)%4]] && left == label[order[(i+3)%4]] && bottom == label[5]){
            cout << "No" << endl;
    		return 1;
    		}
    	}
    	return 0;
    }

    void rotation(string sign){
            tmp1 = label[0];
            tmp2 = label[5];
            if(sign == "N"){
                label[0] = label[1];
                label[5] = label[4];
                label[1] = tmp2;
                label[4] = tmp1;
            }
            if(sign == "S"){
                label[0] = label[4];
                label[5] = label[1];
                label[4] = tmp2;
                label[1] = tmp1;
            }
            if(sign == "W"){
                label[0] = label[2];
                label[5] = label[3];
                label[2] = tmp2;
                label[3] = tmp1;
            }
            if(sign == "E"){
                label[0] = label[3];
                label[5] = label[2];
                label[3] = tmp2;
                label[2] = tmp1;
            }
            if(sign == "NN"){
            	label[0] = tmp2;
            	label[5] = tmp1;
            	int tmp3 = label[4];
            	label[4] = label[1];
            	label[1] = tmp3;
            }
        }

    void initialization(void){
    	for(int i=0; i<6; i++){
    		label[i] = init_label[i];
    	}
    }
};

int main(){
    int n; cin >> n;
	vector<dice> d(n);
	for(int i=0; i<n; i++){
        d[i].setnumber();
    }
	string rot_info[6] = {"","N","W","E","S","NN"};
	int flag=0;
        for(int i=0; i<n; i++){
            d[i].initialization();
            for(int k=i+1; k<n; k++){
    		      for(int j=0; j<6; j++){
                        d[k].initialization();
                        d[k].rotation(rot_info[j]);
                        flag = d[k].check(d[i].label[0],d[i].label[1],d[i].label[2],d[i].label[3],d[i].label[4],d[i].label[5]);
                        if(flag == 1)
				            break;
                }
                if(flag == 1)
                    break;
            }
            if(flag == 1)
                break;
        }
    if(flag == 0)
        cout << "Yes" << endl;

    return 0;
}

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

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

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll N;
    map<ll, ll> verts;
    pair<ll, ll> prev = {-1, 0};
    ll ans = 1;
    cin >> N;
    rep(i, N) {
        ll temp;
        cin >> temp;
        if(i == 0 && temp != 0) {
            cout << 0 << endl;
            return 0;
        }
        verts[temp]++;
    }
    if(verts[0] > 1) {
        cout << 0 << endl;
        return 0;
    }
    iter(i, verts) {
        if(i.first - prev.first != 1) {
            cout << 0 << endl;
            return 0;
        }
        if(i.first != 0) {
            rep(j, i.second) (ans *= prev.second) %= 998244353LL;
        }
        prev = i;
    }
    cout << ans << endl;
    return 0;
}


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

long long nexta[18][100001];

long long arr(long long from, long long days){
    long long shisu=0;
    while(days>0){
        if(days%2==1){
            from=nexta[shisu][from];
        }
        days/=2;
        shisu++;
    }
    return from;
}

int main(){
    //input
    long long N;
    cin >> N;
    long long x[N+1];
    long long i,j;
    for(i=1; i<=N; i++){
        cin >> x[i];
    }
    long long L;
    cin >> L;
    long long Q;
    cin >> Q;
    long long a[Q+1], b[Q+1];
    for(i=1; i<=Q; i++){
        cin >> a[i] >> b[i];
        if(a[i]>b[i]) swap(a[i],b[i]);
    }

    //calc
    for(i=1; i<=N; i++){
        j=lower_bound(x+1,x+N+1,x[i]+L)-x;
        if(j>N){
            nexta[0][i]=N;
            continue;
        }
        if(x[i]+L<x[j]){
            j--;
        }
        nexta[0][i]=j;
    }
    for(i=1; i<18; i++){
        for(j=1; j<=N; j++){
            nexta[i][j]=nexta[i-1][nexta[i-1][j]];
        }
    }
    long long ans[Q+1];
    long long lef, rig, mid;
    long long arrarr, arrarrarr;
    for(i=1; i<=Q; i++){
        lef=0;
        rig=N;
        while(lef<rig-1){
            mid=(lef+rig)/2;
            arrarr=arr(a[i],mid-1);
            if(arrarr>=b[i]){
                rig=mid;
            }else{
                lef=mid;
            }
        }
        ans[i]=lef;
    }

    //output
    for(i=1; i<=Q; i++){
        cout << ans[i] << endl;
    }
    system("pause");
    return 0;
}

/*
 * Author : Andrew J
*/
#include <bits/stdc++.h>
using namespace std;
#define rall(x) x.rbegin() , x.rend()
#define all(x) x.begin() , x.end()
#define fi first
#define se second
#define pb push_back
using ll = long long;
using ii = pair <int , int>;
void in(int &x);
const int N = 1050;
#define singleCase
// #define multipleCase
int n,m,a,b;
string grid[N];

void work() {
   cin >> n >> m >> a >> b;
   string s = "";
   for (int i = 1 ; i <= a ; i++) {
      s += "1";
   }
   for (int i = 1 ; i <= m - a ; i++) {
      s += "0";
   }
   for (int i = 1 ; i <= b ; i++) {
      grid[i] = s;
   }
   for (int i = 0 ; i < m ; i++) {
      s[i] = s[i] == '0' ? '1' : '0';
   }
   for (int i = b + 1 ; i <= n ; i++) {
      grid[i] = s;
   }
   for (int i = 1 ; i <= n ; i++) {
      for (int j = 0 ; j < m ; j++) {
         printf("%c" , grid[i][j]);
      } puts("");
   }
   return;
}

int main() {
   #ifdef multipleCase
      int nQ; in(nQ);
      while (nQ--)
         work();
   #endif
   #ifdef singleCase
      work();
   #endif
   return 0;
}

void in(int &x) {
	bool neg = false;
	register int c;
	x = 0;
	c = getchar();
	if(c == '-') {
		neg = true;
		c = getchar();
	}
	for (; c > 47 && c < 58; c = getchar())
	  x = (x << 1) + (x << 3) + c - 48;
	if (neg) x *= -1;
}


#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
	int f=1;char k=getchar();x=0;
	for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
	for(;k>='0'&&k<='9';k=getchar()) x=x*10+k-'0';
	x*=f;
}
template<class T>il _print(T x){
	if(x>=10) _print(x/10);
	putchar(x%10+'0');
}
template<class T>il print(T x){
	if(x<0) putchar('-'),x=-x;
	_print(x);
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int k,int mod){
	int res=1,bas=x;
	while(k){
		if(k&1) res=1ll*res*bas%mod;
		bas=1ll*bas*bas%mod,k>>=1;
	}
	return res;
}
const int N = 2e5+5,mod = 1e9+7;
it add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
it mul(int x,int y){return 1ll*x*y%mod;}
il inc(int &x,int y){x=add(x,y);}
int n,m,k,ans,fac[N],ifac[N];
it C(int n,int m){return mul(mul(ifac[n-m],ifac[m]),fac[n]);}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	read(n),read(m),read(k),fac[0]=1;
	for(ri i=1;i<=n*m;++i) fac[i]=mul(fac[i-1],i);
	ifac[n*m]=qpow(fac[n*m],mod-2,mod);
	for(ri i=n*m-1;i>=0;--i) ifac[i]=mul(ifac[i+1],i+1);
	for(ri i=1;i<=n-1;++i) inc(ans,mul(mul(mul(m,m),n-i),i));
	for(ri i=1;i<=m-1;++i) inc(ans,mul(mul(mul(n,n),m-i),i));
	print(mul(ans,C(n*m-2,k-2)));
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
char s[maxn];
typedef long long ll;
const ll mod = 1e9 + 7;
ll fac(int x) { ll ret = 1; for(int i = 1; i <= x; ++i) ret = ret * i % mod; return ret;}
ll f(int i, int j)
{
	if(i == 0) return j == 0;
	if(s[i] == 'B')
	{
		if(j & 1)
			return f(i - 1, j - 1);
		else
			return f(i - 1, j + 1) * (j + 1) % mod;
	}
	else
	{
		if(j & 1)
			return f(i - 1, j + 1) * (j + 1) % mod;
		else if(j > 0)
			return f(i - 1, j - 1);
		else
			return 0;
	}
}
int main()
{
	scanf("%d", &n);
	scanf("%s", s + 1);
	cout << f(2 * n, 0) * fac(n) % mod << endl;
	return 0;
}


#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <cstdio>
#include <cctype>
#include <queue>
#include <complex>
#include <climits>

typedef long long ll;

using namespace std;

char field[12][12];

void dfs(int x, int y){
	field[x][y] = '0';
	for(int dx=-1; dx<=1; ++dx){
		for(int dy=-1; dy<=1; ++dy){
			if(dx != 0 && dy != 0) continue;
			int nx = x+dx, ny = y+dy;
			if(0 <= nx && nx < 12 && 0 <= ny && ny < 12 && field[nx][ny] == '1'){
				dfs(nx,ny);
			}
		}
	}
}

int main(void){
	while(1){
		if(cin >> field[0][0] == 0) break;
		for(int i=0; i<12; ++i){
			for(int j=0; j<12; ++j){
				if(i==0&&j==0)continue;
				cin >> field[i][j];
			}
		}

		int count = 0;

		for(int i=0; i<12; ++i){
			for(int j=0; j<12; ++j){
				if(field[i][j] == '1'){
					dfs(i, j);
					count++;
				}
			}
		}

		cout << count << endl;

	}
	return 0;
}

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
typedef long long ll;

#define INF 1000000000

int main(){
    int m,mi,mx;
    while(cin>>m>>mi>>mx, m){
        vector<int> p(m);
        rep(i,m) cin>>p[i];

        sort(all(p), greater<int>());

        int n, gap=0;

        for(int i=mi-1; i<mx; i++){
            if(p[i]-p[i+1] >= gap){
                n=i+1;
                gap = p[i]-p[i+1];
            }
        }
        cout<<n<<endl;
    }

  return 0;
}


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

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

int main() {
  ll N, M; cin >> N >> M;
  vector<pair<int, int>> ans(N + 1); // 提出数と0, 1の正解か不正解か
  REP(i, M) {
    int p; string S; cin >> p >> S;
    if(S == "WA" && ans[p].second == 0) ans[p].first++;
    else if(S == "AC") ans[p].second = 1;
  }
  ll res = 0; ll pena = 0;
  REP(i, N + 1) {
    if(ans[i].second == 1) {
      res++;
      pena += ans[i].first;
    }
  }
  cout << res << " " << pena << endl;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, int>;

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

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

    int size(int x) {
        return -data[find(x)];
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, K, L;
    cin >> n >> K >> L;
    UnionFind uf1(n), uf2(n);
    for (int i = 0; i < K; i++) {
        int p, q;
        cin >> p >> q;
        p--; q--;
        uf1.unite(p, q);
    }
    for (int i = 0; i < L; i++) {
        int p, q;
        cin >> p >> q;
        p--; q--;
        uf2.unite(p, q);
    }

    map<P, int> memo;
    vector<P> v;
    for (int i = 0; i < n; i++) {
        P tar(uf1.find(i), uf2.find(i));
        v.push_back(tar);
        memo[tar]++;
    }

    for (int i = 0; i < n; i++) {
        cout << memo[v[i]] << " \n"[i + 1 == n];
    }
    return 0;
}


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

struct person{
	long long h;
	long long p;
};

int main(){
	int n;
	int ans=0;
	std::vector<person> list;
	std::vector<long long> dpList;

	std::cin>>n;
	list.resize(n);
	dpList.resize(n+1, -1);
	dpList[0]= 0;

	for(int i=0; i<n; i++)
		std::cin>>list[i].h>>list[i].p;

	std::stable_sort(list.begin(), list.end(), [](const person& x, const person& y){return x.p+ x.h< y.p + y.h;});

	for(int i=0; i<n; i++){
		for(int j=i; j>=0; j--){
			if(dpList[j]==-1)
				continue;

			if(dpList[j]>list[i].h)
				continue;

			if(dpList[j+1]==-1)
				dpList[j+1]=dpList[j]+list[i].p;
			else
				dpList[j+1]=std::min(dpList[j]+list[i].p, dpList[j+1]);
		}
	}

	for(int i=n; i>=0; i--){
		if(dpList[i]!=-1){
			ans= i;
			break;
		}

	}

	std::cout<<ans<<std::endl;

	return 0;

}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << x << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

bool srt(pi a, pi b){
    return a.e1+a.e2 < b.e1+b.e2;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    sc(n);
    pi a[n];
    FORR(i,a)
        sc(i.e1,i.e2);
    sort(a,a+n,srt);
    ll dp[n+1];
    ll LINF = 1e18;
    FORR(i,dp)
        i = LINF;
    dp[0] = 0;
    FORR(i,a){
        /* whatis(i) */
        for(int x = n-1; x >= 0; --x){
            /* whatis(x) */
            /* whatis(dp[x]) */
            if(dp[x] == LINF) continue;
            if(dp[x] > i.e1) continue;
            /* if(dp[ */
            dp[x+1] = min(dp[x+1],dp[x]+i.e2);
        }
    }
    for(int i = n+1; --i;)
        if(dp[i] != LINF) REE(i)
}


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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
    int N; cin >> N;
    while(N%111!=0){
        N++;
    }
    cout << N << endl;
    return 0;
}

#include <algorithm>

#include <bitset>

#include <tuple>

#include <cstdint>

#include <cstdio>

#include <cctype>

#include <assert.h>

#include <stdlib.h>

#include <stdio.h>

#include <cassert>

#include <cfloat>

#include <climits>

#include <cmath>

#include <complex>

#include <ctime>

#include <deque>

#include <fstream>

#include <functional>

#include <iomanip>

#include <iostream>

#include <iterator>

#include <list>

#include <limits>

#include <map>

#include <memory>

#include <queue>

#include <random>

#include <set>

#include <stack>

#include <string>

#include <unordered_map>

#include <unordered_set>

#include <utility>

#include <vector>

#include <math.h>
using namespace std;

int main() {
	int N;
	cin >> N;
	int hundred = N / 100;
	int ten = N / 10 % 10;
	int one = N % 100 % 10;
	if (hundred < ten) {
		cout << (hundred + 1) * 111 << endl;
	}
	else if (hundred >= ten && hundred >= one) {
		cout << hundred * 111 << endl;
	}
	else {
		cout << (hundred + 1) * 111 << endl;
	}
}


#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
const long double eps = 1e-12;
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144*2
#define REP(a,b) for(long long a = 0;a < b;++a)
long long bad[700][700] = {};
vector<pair<int,int>> vertexs[2][700][700];
int visited[2][700][700];
int coloring[2][700][700];
int main() {
	long long n, d[2];
	cin >> n;
	REP(q, 2) {
		cin >> d[q];
	}
	REP(itr, 2) {
		for (long long a = 0; a * a <= d[itr]; ++a) {
			long long b = sqrt(d[itr] - a * a);
			if (b * b + a * a != d[itr]) continue;
			for (int q = 0; q < 2 * n; ++q) {
				for (int j = 0; j < 2 * n; ++j) {
					if (bad[q][j] == 1) continue;
					int xe[4] = { 1,1,-1,-1 };
					int ye[4] = { 1,-1,1,-1 };
					REP(t, 4) {
						int now_x = q + xe[t] * a;
						int now_y = j + ye[t] * b;
						if (now_x >= 0 && now_x < 2 * n && now_y >= 0 && now_y < 2 * n) {
							if (bad[now_x][now_y] == 1) continue;
							vertexs[itr][q][j].push_back(make_pair(now_x, now_y));
							vertexs[itr][now_x][now_y].push_back(make_pair(q, j));
						}
					}
				}
			}
		}
		for (int q = 0; q < 2 * n; ++q) {
			for (int j = 0; j < 2 * n; ++j) {
				if (visited[itr][q][j] == 0 && bad[q][j] == 0) {
					queue<pair<int, int>> next;
					next.push(make_pair(q, j));
					visited[itr][q][j] = 1;
					vector<pair<int, int>> used;
					int cnt[2] = {};
					while (next.empty() == false) {
						pair<int, int> now = next.front();
						next.pop();
						used.push_back(now);
						REP(i, vertexs[itr][now.first][now.second].size()) {
							pair<int, int> going = vertexs[itr][now.first][now.second][i];
							if (visited[itr][going.first][going.second] == 0) {
								visited[itr][going.first][going.second] = 1;
								coloring[itr][going.first][going.second] = (coloring[itr][now.first][now.second] + 1) % 2;
								cnt[coloring[itr][going.first][going.second]]++;
								next.push(going);
							}
						}
					}
					int yusen = 1;
					if (cnt[0] < cnt[1]) {
						yusen = 0;
					}
					REP(q, used.size()) {
						if (coloring[itr][used[q].first][used[q].second] == yusen) {
							bad[used[q].first][used[q].second] = 1;
						}
					}
				}
			}
		}
	}
	int cnt = 0;
	REP(q, 2 * n) {
		REP(j, 2 * n) {
			if (bad[q][j] == 0) {
				cnt++;
				if (cnt <= n * n) {
					cout << q << " " << j << endl;
				}
			}
		}
	}
}

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
using namespace std;
inline LL read()
{
	LL f=1,x=0;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;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=305;
int col[2][2*MAXN][2*MAXN],n,D1,D2;
vector<pii> fac[2];
bool in(int x,int y){return x>=0&&x<2*n&&y>=0&&y<2*n;}
void DFS(int x,int y,int o)
{
	for(auto p:fac[o])for(int u=-1;u<=1;u++)if(u)for(int v=-1;v<=1;v++)if(v)
		if(in(x+u*p.first,y+v*p.second))
		{
			int ux=x+u*p.first,uy=y+v*p.second;
			if(col[o][ux][uy]!=-1)continue;
			col[o][ux][uy]=col[o][x][y]^1;DFS(ux,uy,o);
		}
}
void color(int o)
{
	memset(col[o],-1,sizeof(col[o]));
	for(int i=0;i<2*n;i++)for(int j=0;j<2*n;j++)if(col[o][i][j]==-1)
		col[o][i][j]=0,DFS(i,j,o);
}
int sqr(int x){return x*x;}
int dis(pii u,pii v){return sqr(u.first-v.first)+sqr(u.second-v.second);}
vector<pii> ans[2][2];
int main()
{
	n=read();D1=read();D2=read();
	for(int i=0;i*i<=D1;i++)if(floor(sqrt(D1-i*i))==ceil(sqrt(D1-i*i)))fac[0].emplace_back(mp(i,sqrt(D1-i*i)));
	for(int i=0;i*i<=D2;i++)if(floor(sqrt(D2-i*i))==ceil(sqrt(D2-i*i)))fac[1].emplace_back(mp(i,sqrt(D2-i*i)));
	color(0);color(1);
	for(int i=0;i<2*n;i++)for(int j=0;j<2*n;j++)
		ans[col[0][i][j]][col[1][i][j]].emplace_back(mp(i,j));
	for(int i=0;i<=1;i++)for(int j=0;j<=1;j++)if(SZ(ans[i][j])>=n*n)
	{
//		for(auto p:ans[i][j])for(auto q:ans[i][j])
//			assert(dis(p,q)!=D1&&dis(p,q)!=D2);
		int cnt=0;
		for(auto p:ans[i][j])
		{
			pr1(p.first),pr2(p.second);
			++cnt;if(cnt==n*n)break;
		}
		return 0;
	}
	return 0;
}


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

int main() {
  int a;

  cin >> a;
  int x = a;
  int z = std::pow(x, 2);
  int y = std::pow(x, 3);
  int ans = x+y+z;
  cout << ans << endl;
  return 0;
 }

/**
 * auther: moririn_cocoa
 */
// #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second

int main(int argc, char* argv[]) {

  int R;

  cin >> R;

  if ( R < 1200 ) {

    cout << "ABC" << endl;
  }

  else if ( R >= 1200 && R < 2800 ) {

    cout << "ARC" << endl;
  }

  else {

    cout << "AGC" << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
#define sz(arr) (int)(arr).size()
#define rng(arr) arr.begin(), arr.end()
#define show(x) cout << #x << " = " << x << endl;

using namespace std;
typedef long long int ll;

void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...) \
  T __VA_ARGS__;    \
  _cin(__VA_ARGS__)

#define Cins(T, n, xs) \
  vector<T> xs(n);     \
  for (int i = 0; i < n; ++i) cin >> xs[i]

void Main();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

void Main() {
  Cin(int, n);
  Cins(int, n, a);

  int sum = 0;
  for (int i = 0; i < n; ++i) sum += a[i];

  double mean = (double)sum / n;

  double mi = 1001001001;
  int mii = -1;
  for (int i = 0; i < n; ++i) {
    double sa = abs(a[i] - mean);
    if (mi > sa) {
      mi = sa;
      mii = i;
    }
  }

  cout << mii << endl;
}

#include<stdio.h>
int main(){
    long long input;
    scanf("%lld",&input);
    int i = 0;
    char ans[100];
    while(input > 0){
        int temp = input % 26;
        if(temp == 0){
            ans[i++] = 'z';
            input = (input/26) - 1;
        }
        else{
            ans[i++] = (temp - 1) + 'a';
            input = input/26;
        }
    }
    ans[i] = '\0';
    for(int j = i - 1; j >= 0; j--){
        printf("%c",ans[j]);
    }
    return 0;
}


//#pragma warning(disable:4996)
//#include <Windows.h>
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;
template<class T>
using pqueue = priority_queue<T, vector<T>, function<bool(T, T)>>;

template<class T>
inline void chmin(T& a, T b) {
	a = min(a, b);
}

template<class T>
inline void chmax(T& a, T b) {
	a = max(a, b);
}

void input();
void solve();

void daminput();
void naive();

void outputinput();

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	input();
	//daminput();
	solve();
	//naive();
	//outputinput();
	return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

int N, Q;
VLL A;
VLL query;

void input() {
	cin >> N >> Q;
	A.resize(N);
	for (int n = 0; n < N; n++) {
		cin >> A[n];
	}
	query.resize(3 * Q);
	for (int q = 0; q < 3 * Q; q++) {
		cin >> query[q];
	}
}

void daminput() {
}

//TT(T,T):=モノイドの乗算
//require Monoid
template<class T>
class Segtree {
private:
	vector<T> seg;
	LL RN;
	typedef function<T(T, T)> TT;
	TT f;
	T te;
public:
	Segtree(LL N, TT _f, T e) :te(e) {
		RN = 1;
		while (RN < N)RN *= 2;
		seg.resize(2 * RN, te);
		f = _f;
	}
	Segtree(vector<T>& V, TT _f, T e) :te(e) {
		LL N = V.size();
		RN = 1;
		while (RN < N)RN *= 2;
		seg.resize(2 * RN, te);
		f = _f;
		for (LL n = 0; n < N; n++) seg[RN + n] = V[n];
		for (LL k = RN - 1; k >= 1; k--) {
			seg[k] = f(seg[2 * k], seg[2 * k + 1]);
		}
	}
	//set n-th as x(0 index!!!!!)
	void set(LL n, T x) {
		seg[RN + n] = x;
		n = (RN + n) / 2;
		while (n >= 1) {
			seg[n] = f(seg[2 * n], seg[2 * n + 1]);
			n /= 2;
		}
	}
	//change n-th number p to x*p(0 index!!!!!)
	void addl(LL n, T x) {
		seg[RN + n] = f(x, seg[RN + n]);
		n = (RN + n) / 2;
		while (n >= 1) {
			seg[n] = f(seg[2 * n], seg[2 * n + 1]);
			n /= 2;
		}
	}
	//change n-th number p to p*x(0 index!!!!!)
	void addr(LL n, T x) {
		seg[RN + n] = f(seg[RN + n], x);
		n = (RN + n) / 2;
		while (n >= 1) {
			seg[n] = f(seg[2 * n], seg[2 * n + 1]);
			n /= 2;
		}
	}
	//get [l,r] (0 index!!!!!)
	T get(LL l, LL r) {
		T ansl = te, ansr = te;
		r++;
		l += RN;
		r += RN;
		while (l < r) {
			if (l & 1) {
				ansl = f(ansl, seg[l]);
				l++;
			}
			if (r & 1) {
				r--;
				ansr = f(seg[r], ansr);
			}
			l >>= 1;
			r >>= 1;
		}
		return f(ansl, ansr);
	}
	//get n-th number(0 index!!!!!)
	T get(LL n) {
		return seg[RN + n];
	}
	T operator[](LL n) {
		return seg[RN + n];
	}
};

void solve() {
	Segtree<LL> seg(A, [](LL a, LL b) {
		return a + b;
	}, 0);
	for (int q = 0; q < Q; q++) {
		int t = query[3 * q];
		if (t == 0) {
			int p = query[3 * q + 1];
			int x = query[3 * q + 2];
			seg.addl(p, x);
		}
		else {
			int l = query[3 * q + 1];
			int r = query[3 * q + 2];
			cout << seg.get(l, r - 1) << "\n";
		}
	}
}

void naive() {
}

void outputinput() {
}

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

int main(){
  int N, Q; cin >> N >> Q;
  fenwick_tree<long long> fw(N);
  rep(i, N) {
    int a; cin >> a;
    fw.add(i, a);
  }
  rep(query, Q) {
    int a, b, c; cin >> a >> b >> c;
    if (a == 0) fw.add(b, c);
    else cout << fw.sum(b, c) << endl;
  }
}


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

int main()
{
	string game[3];
	cin >> game[0] >> game[1] >> game[2];
	int turn = 0;
	int length[] = {game[0].length(), game[1].length(), game[2].length()};
	int indices[] = {0, 0, 0};
	while(true) {
		int curr = indices[turn]++;
		if(indices[turn] > length[turn]) break;
		turn = game[turn][curr] - 'a';
	}
	cout << char(turn + 'A');
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define FOR(i,start,end) for(int i=start;i<=end;i++)
const int INF = 1001001001;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
typedef long long ll;
const ll MOD=1000000007;
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T>auto MAX(const T& a) { return *max_element(a.begin(),a.end()); }
template<class T>auto MIN(const T& a) { return *min_element(a.begin(),a.end()); }
template<class T, class U>U SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); }
template<class T, class U>U COUNT(const T& a, const U& v) { return count(a.begin(),a.end(), v); }
template<class T, class U>int LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); }
template<class T, class U>int UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); }
int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }
int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; }
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<vll> vvll;
typedef map<int, int> mii;
typedef set<int> si;
//---------------------------------------------------------------------------------------------------

int main(void){
    // Your code here!
    string A,B,C; cin >> A >> B >> C;
    queue<char> Sa,Sb,Sc;
    rep(i,A.size()) Sa.push(A[i]);
    rep(i,B.size()) Sb.push(B[i]);
    rep(i,C.size()) Sc.push(C[i]);
    char now ;
    rep(i,A.size()+B.size()+C.size()+2) {
        if (now == 'a' && Sa.empty()){
            cout << 'A' << endl;
            return 0;
        }
        if (now == 'b' &&Sb.empty()){
            cout << 'B' << endl;
            return 0;
        }
        if (now == 'c' &&Sc.empty()){
            cout << 'C' << endl;
            return 0;
        }
        if (i == 0) {
            now = 'a';
            continue;
        }
        if (now == 'a') {
            now = Sa.front();
            Sa.pop();
            continue;
        }
        if (now == 'b') {
            now = Sb.front();
            Sb.pop();
            continue;
        }
        if (now == 'c') {
            now = Sc.front();
            Sc.pop();
            continue;
        }
    }
}


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

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;

int N,M;
int nor, bip, sing, done[MX];
ll ans;
vi adj[MX];
bool ok = 1;

ll sq(ll x) { return x*x; };

void dfs(int x, int y) {
    if (done[x]) {
        if (done[x] != y) ok = 0;
        return;
    }
    // cout << "HUH " << x << "\n";
    done[x] = y;
    for (int i: adj[x]) dfs(i,y^3);
}

void solve(int x) {
    if (sz(adj[x]) == 0) {
        sing ++;
        return;
    }
    ok = 1;
    dfs(x,1); // cout << "OOPS " << x << "\n";
    if (ok) bip ++;
    else nor ++;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> M;
    F0R(i,M) {
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }
    FOR(i,1,N+1) if (!done[i]) {
        //cout << i << "\n";
        solve(i);
    }
    //cout << nor << " " << bip << " " << sing << "\n";
    F0R(i,sing) {
        ans += 2*N-1; N --;
    }
    //cout << ans << "\n";
    ans += sq(bip);
    ans += sq(nor+bip);
    cout << ans;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS

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

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;}
template<class T> inline bool chmax(T& a,T b){ if(a<b){a=b;return 1;}return 0; }
template<class T> inline bool chmin(T& a,T b){ if(a>b){a=b;return 1;}return 0; }
//template end

int main(){
   int n,m; scanf("%d%d",&n,&m);
   int a[310][310];
   rep(i,0,n)rep(j,0,m){
      scanf("%d",&a[i][j]);
      a[i][j]--;
   }
   int ans=n;
   int cnt[310],idx[310]; bool used[310];
   rep(i,0,n)cnt[a[i][0]]++;
   rep(i,0,m-1){
      int ma=0,idx2=-1;
      rep(j,0,m)if(chmax(ma,cnt[j]))idx2=j;
      chmin(ans,ma); used[idx2]=1;
      rep(j,0,m)cnt[j]=0;
      rep(j,0,n){
         while(idx[j]<m&&used[a[j][idx[j]]])idx[j]++;
         cnt[a[j][idx[j]]]++;
      }
   } printf("%d\n",ans);
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;

constexpr int64_t mpow(int64_t a, int64_t b, int64_t mod = 1000000007) {
    if (b <= 0) { return 1; } if (b == 1) { return a % mod; } if (b == 2) { return (a * a) % mod; }
    return (max(a * (b & 1), (int64_t)1) * mpow(mpow(a, b / 2, mod), 2, mod)) % mod;
}
constexpr int64_t mdiv(int64_t a, int64_t b, int64_t mod = 1000000007) {
    return (a * mpow(b, mod - 2, mod)) % mod;
}

int main() {
    int n, l; cin >> n >> l;
    int all = n * (n - 1) / 2 + l * n;
    if (0 <= l) {
        cout << all - l << endl;
        return 0;
    }
    if (abs(l) < n) {
        cout << all << endl;
        return 0;
    }
    cout << all - (l + n - 1) << endl;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

//#define int long long
//signed main(){
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,r;
    cin >> n >> r;
    if(n<10) cout << r+(10-n)*100 << "\n";
    else cout << r << "\n";

    return 0;
}