#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
using namespace std;
/// khodaya komak kon
/// ya navid navid
const int N=2e5+100;
ll seg[4*N];
void shift(ll nod){
    if (seg[nod]==1){
        seg[nod*2]=1;
        seg[nod*2+1]=1;
    }
}
void upd(ll nod,ll l,ll r,ll L,ll R){
    if (l>=R || L>=r){
        return ;
    }
    if (l>=L && r<=R){
        seg[nod]=1;
        return ;
    }
    shift(nod);
    ll mid=(r+l)/2;
    upd(nod*2,l,mid,L,R);
    upd(nod*2+1,mid,r,L,R);
}
ll get(ll nod,ll l,ll r,ll id){
    if (r-l==1){
        return seg[nod];
    }
    shift(nod);
    ll mid=(r+l)/2;
    if (mid>id){
        return get(nod*2,l,mid,id);
    }
    else{
        return get(nod*2+1,mid,r,id);
    }
}
ll k;
vector <pii> h;
ll st[N];
ll fn[N];
vector <int> g[N];
ll cnt=0;
ll par[N];
vector <int> t;
ll par1[N];
ll dfs(ll v,ll p,ll hi){
    par1[v]=p;
    t.pb(v);
    ll z=t.size();
    z-=k;
    z=max((ll)0,z);
    par[v]=t[z];
    h.pb({hi,v});
    st[v]=cnt;
    for (int i=0;i<g[v].size();i++){
        ll u=g[v][i];
        if (u==p){
            continue;
        }
        cnt++;
        dfs(u,v,hi+1);
    }
    t.pop_back();
    fn[v]=cnt;
}
int32_t main(){
    sync;
    ll n;
    cin >> n >> k;
    ll ans=0;
    ll x;
    cin >> x;
    if (x!=1){
        ans++;
    }
    for (int i=2;i<=n;i++){
        cin >> x;
        g[x].pb(i);
        g[i].pb(x);
    }
    upd(1,0,N,0,1);
    dfs(1,1,0);
    sort(h.begin(),h.end());
    reverse(h.begin(),h.end());
    for (int i=0;i<n-1;i++){
        ll v=h[i].S;
        ll u=par[v];
        //cout << v << " " << u << endl;
        ll z=get(1,0,N,st[v]);
        if (z==1 || par1[u]==1){
            continue;
        }
        ans++;
        upd(1,0,N,st[u],fn[u]+1);
    }
    cout << ans;
}

#include <bits/stdc++.h>
#define repr(i,from,to) for(int (i)=(from);(i)<(to);(i)++)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
#define vprint(x) for(auto a:(x)) cout << x << endl;
#define all(x) begin((x)), end((x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
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;
}

vector<ll> a;
ll n,m,v,p;
bool chk(int idx) {
    if (a[idx]+m<a[n-p]) {
        return false;
    }
    ll cnt = 0;
    rep(i,n) {
        if (i>n-p||i<=idx) {
            cnt += m;
        }
        else {
            cnt += max(0LL, a[idx]+m-a[i]);
        }
    }
    return cnt >= m*v;
}

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin>>n>>m>>v>>p;
    a.assign(n,0);
    rep(i,n) cin>>a[i];
    sort(all(a));
    ll l=0,r=n-1;
    while(r>l) {
        ll mid = (l+r)/2;
        if (chk(mid)) {
            r = mid;
        } else {
            l = mid;
        }
        if (l+1==r) {
            if (chk(l)) {
                r = l;
            } else {
                l = r;
            }
        }
    }
    cout << n-l << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
template <typename TYPE> inline void chkmax(TYPE &x,TYPE y){x<y?x=y:0;}
template <typename TYPE> inline void chkmin(TYPE &x,TYPE y){y<x?x=y:0;}
template <typename TYPE> void readint(TYPE &x)
{
    x=0;int f=1;char c;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    x*=f;
}
const int MAXN=300005;

int n,s,a[MAXN];
ll ans=0,b[MAXN];
int dir=-1;
void work(int l,int r)
{
	if(a[l]>s)ans+=a[r]-s;
	else if(a[r]<s)ans+=s-a[l];
	else if(b[l]>=b[r])
	{
		if(dir!=0)ans+=a[r]-a[l],dir=0;
		b[l]+=b[r];
		work(l,r-1);
	}
	else
	{
		if(dir!=1)ans+=a[r]-a[l],dir=1;
		b[r]+=b[l];
		work(l+1,r);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("code.in","r",stdin);
//	freopen("code.out","w",stdout);
	#endif
	readint(n),readint(s);
	for(int i=1;i<=n;++i)readint(a[i]),readint(b[i]);
	work(1,n);
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ll << (b))
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define rsort(v) sort(all(v)); reverse(all(v))

using namespace std;

using i32 = int;
using i64 = long long;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() { cout << "Yes" << '\n'; exit(0); }
inline void no() { cout << "No" << '\n'; exit(0); }
inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); }
inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }
template<typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template<typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template<typename T> inline void amax(T &x, T y) { if (x < y) x = y; }
template<typename T> inline void amin(T &x, T y) { if (x > y) x = y; }
template<typename T> inline T power(T x, i64 n) { T r = 1; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template<typename T> istream& operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }

void solve() {
  int N, X; cin >> N >> X;
  vi64 S(N);
  vector<tuple<i64, i64, i64>> t;
  i64 M = 0;
  i64 D = 0;
  i64 ok = 0, ng = -1;
  rep(i, N) {
    i64 b, l, u; cin >> b >> l >> u;
    t.push_back(make_tuple(b, l, u));
    S[i] = b * l + (X - b) * u;
    M += S[i];
    ok += b;
    D += -l * b;
  }
  vi32 ord(N);
  iota(all(ord), 0);
  sort(all(ord), [&](int i, int j) {
    return S[i] > S[j];
  });
  vi64 sums(N + 1);
  rep(i, N) sums[i + 1] = sums[i] + S[ord[i]];
  while (ok - ng > 1) {
    i64 k = (ok + ng) / 2;
    i64 ma = -1e18;
    i64 p = k / X, q = k - p * X;
    rep(i, N) {
      i64 MM;
      if (p > i) MM = sums[p + 1] - S[ord[i]];
      else MM = sums[p];
      i64 b, l, u;
      tie(b, l, u) = t[ord[i]];
      i64 T;
      if (q <= b) T = q * l;
      else T = b * l + (q - b) * u;
      amax(ma, MM + T);
    }
    if (ma + D >= 0) ok = k;
    else ng = k;
  }
  cout << ok << '\n';
}


/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using Pair = pair<int64_t, int64_t>;

// std::cout << std::setprecision(20) << 1.1 << endl;

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

  int64_t K, T;
  cin >> K >> T;
  vector<int64_t> A(T);
  for (int64_t i = 0; i < T; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  cout << max(A[0] - (K - A[0] + 1), 0L) << endl;

  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define inf 2,147,483,647 //int値の最大値
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define cint(n) int n;cin>>n;
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 gcd(int a, int b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
int main() {
    int n,x;cin>>n>>x;
    vector<int> a(n);
    rep(i,n){
        cin>>a[i];
    }
    sort(ALL(a));
    int j=0;
    ll sum=0;
    while(sum<x && j<n){
        sum+=a[j];
        j++;
    }
    if(sum==x){
        cout<<j;
    }else{
        cout<<j-1;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ft  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define  ll  long long
string s,p;
int main()
{
    ft
    ll t,i,j,n,m=0,l,r;
    cin>>s;
    n=s.size();
    for(i=0;i<n;i++)
    {
        if((i+1)<n && s[i]==s[i+1])
        {
            cout<<(i+1)<<" "<<(i+2)<<"\n";
            return 0;
        }
        else if(((i+2)<n && s[i]==s[i+2]))
        {
            cout<<(i+1)<<" "<<(i+3)<<"\n";
            return 0;
        }
    }
    cout<<"-1 -1\n";
    return 0;
}

/* Simplicity and Goodness */

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

using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B) { cout << ' ' << A; my_dbg(B...); }
#define dbg(...)  cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\n", n)
#define rep(i,a,b) for(int i=a; i<(int)b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9+7;
const int maxN = 1e6+2;

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

   int l = -2, r = -2;

   for(int i = 0; i + 1 < (int) s.size(); i ++) {
      if(s[i] == s[i + 1]) {
         l = i, r = i + 1;
         break;
      }
   }

   for(int i = 0; i + 2 < (int) s.size(); i ++) {
      set<char> unique;
      for(int j = i; j < i + 3; j ++) {
         unique.insert(s[j]);
      }
      if((int) unique.size() <= 2) {
         l = i, r = i + 2;
         break;
      }
   }

   printf("%d %d\n", l + 1, r + 1);
}


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

const int INF = 99999999;

int main(void) {

  int e, t;
  vector<int> y2, z3;
  for (t = 0; t*t <= 1000000; t++) y2.push_back(t*t);
  for (t = 0; t*t*t <= 1000000; t++) z3.push_back(t*t*t);

  while (cin >> e, e) {
    int m = INF;
    for (int i = 0; i < y2.size(); i++) {
      for (int j = 0; j < z3.size(); j++) {
        if (y2[i]+z3[j] <= e) {
          m = min(m, (e-y2[i]-z3[j])+i+j);
        }
      }
    }
    cout << m << endl;
  }

  return 0;
}

#include <iostream>
using namespace std;

int main() {
  int e;
  while (scanf("%d", &e) != EOF && e) {
    int m = 1000;
    for (int z=100; z>=0; z--) {
      if (e < z*z*z) {
	continue;
      }
      for (int y=0; y<m; y++) {
	if (e < y*y + z*z*z) {
	  break;
	}
	int x = e - y*y - z*z*z;
	if (x + y + z < m) {
	  m = x + y + z;
	}
      }
    }
    printf("%d\n", m);
  }
}


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

int main()
{
    int N, M, point = 0, max = 0;
    cin >> N;
    vector<string> blue(N);
    set<string> ch;
    for (int i = 0; i < N; ++i)
    {
        cin >> blue.at(i);
        ch.insert(blue.at(i));
    }
    cin >> M;
    vector<string> red(M);
    for (int i = 0; i < M; ++i)
    {
        cin >> red.at(i);
        ch.insert(red.at(i));
    }

    for (auto x : ch)
    {
        point = 0; //重要
        for (int i = 0; i < N; ++i)
        {
            if (blue.at(i) == x)
            {
                ++point;
            }
        }
        for (int i = 0; i < M; ++i)
        {
            if (red.at(i) == x)
            {
                --point;
            }
        }
        max = max < point ? point : max;
    }
    cout << max << endl;
}

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

int main()
{
  int n, m, ans = 0;
  cin >> n;
  map<string, int> s;
  map<string, int> t;
  for (int i = 0; i < n; i++)
  {
    string blue;
    cin >> blue;
    s[blue]++;
  }
  cin >> m;
  for (int i = 0; i < m; i++)
  {
    string red;
    cin >> red;
    t[red]++;
  }

  for (auto i = s.begin(); i != s.end(); i++)
  {
    ans = max(ans, i->second - t[i->first]);
  }
  cout << ans << endl;
}


#include<stdio.h>
int main()
{
	int N,R,A;
	scanf("%d %d", &N, &R);
	A =100*(10-N);
	if(N>=10){
		printf("%d", R);
	}
	else{
		printf("%d", R + A);
	}
return 0;
}

#include<iostream>
using namespace std;
long long int MOD = 1000000007;
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);

int main() {

	//A
	int n, r;
	cin >> n >> r;
	if (n >= 10) {
		cout << r << endl;
	}
	else {
		cout << r + 100 * (10 - n) << endl;
	}
	return 0;

	//B
	/*int n;
	cin >> n;
	int k;
	cin >> k;
	int i = 0;
	while (n > 0) {
		n = (int)(n / k);
		i++;
	}
	cout << i << endl;
	return 0;*/
	//C
	/*int n;
	cin >> n;
	int x[100];
	int i;
	for (i = 0; i < n; i++) {
		cin >> x[i];
	}
	int sum, j, summin = 100000000;
	for (i = 0; i <= 100; i++) {
		sum = 0;
		for (j = 0; j < n; j++) {
			sum = sum + (x[j] - i) * (x[j] - i);
		}
		if (sum < summin)summin = sum;
	}
	cout << summin << endl;
	return 0;
	*/
	//D
	/*
	long long int n;
	cin >> n;
	long long int a, b;
	cin >> a >> b;
	if (n == 2) {
		cout << 0 << endl;
		return 0;
	}
	long long int sum = 1;
	int i;
	long long int x = 2, m = n;
	//2^nを計算
	while (m > 0) {
		if (m % 2 == 1) {
			sum = (sum * x) % MOD;
		}
		m = (int)(m / 2);
		x = (x * x) % MOD;
	}

	//cout << 1 << endl;

	long long int sum1 = 1;
	for (i = 1; i <= a; i++) {
		sum1 = (sum1 * (n + 1 - i)) % MOD;
	}
	for (i = 1; i <= a; i++) {
		sum1 = (sum1 * modinv(i, MOD)) % MOD;
	}
	sum = (sum - sum1 + MOD) % MOD;
	//cout << 2 << endl;

	sum1 = 1;
	for (i = 1; i <= b; i++) {
		sum1 = (sum1 * (n + 1 - i)) % MOD;
	}
	for (i = 1; i <= b; i++) {
		sum1 = (sum1 * modinv(i, MOD)) % MOD;
	}
	sum = (sum - sum1 + MOD) % MOD;
	sum = (sum + MOD - 1) % MOD;
	cout << sum << endl;
	return 0;
	*/
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
	return modpow(a, mod - 2, mod);
}


#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() {
    ll N, x;
    cin >> N >> x;
    vector<ll> a(N);
    rep(i,N) cin >> a.at(i);
    ll ans = 0;
    rep(i,N) {
        if (i == 0) continue;
        if(a.at(i) + a.at(i-1) > x) {
            ll diff = a.at(i) + a.at(i-1) - x;
            ans += diff;
            if (diff <= a.at(i)) a.at(i) -= diff;
            else a.at(i) = 0;
        }
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
#include<cctype>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define all(v) (v).begin(),(v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int,int>

int main(){
  ll N; cin >> N;
  ll a, b, c, d, e; cin >> a >> b >> c >> d >> e;
  ll m = min({a,b,c,d,e});
  ll retval = (N+m-1ll)/m + 4ll;
  cout << retval << endl;

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

  return 0;
}


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

#define ll long long
#define mtv(kit) cout<<#kit<<" - "<<kit<<"\n";
#define ff first
#define ss second
#define pb push_back
#define rep(i,a,b) for(i=a;i<b;i++)
#define pii pair<ll , ll>
#define all(x) x.begin(),x.end()
#define nl "\n"
#define ump unordered_map

void doit(){
    ll n,i,k,j;
    cin >> n;
    vector< ll >v(n);
    for(i = 0; i < n; i++)cin>>v[i];
    vector<pii>answer(61,{0,0});
    rep(i,0,n){
        rep(j,0,61){
            if(v[i] & (1LL << j) )answer[j].ff++;
            else answer[j].ss++;
        }
    }
    ll ans = 0,mod = 1e9+7;
    rep(i,0,61){
        ll k1 = (answer[i].ff * answer[i].ss)%mod ;
        ll k2 = (1LL << i)%mod;
        k1 = (k1*k2)%mod;
        ans = ((ans % mod) + (k1 % mod)) % mod;
    }
    cout << ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin >> t;
    for(ll i = 1; i <= t; i++){
        doit();
    }
}

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(), is_sorted(),

int n;
int v[300005];
int suf[64][300005][2];

signed main(){
    FASTER;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>v[i];

    for(int i=0;i<64;i++){
        for(int j=n;j>=1;j--){
            suf[i][j][1]=suf[i][j+1][1]+((v[j]>>i)&1);
            suf[i][j][0]=suf[i][j+1][0]+(1-((v[j]>>i)&1));
        }
    }
    int ans=0;
    for(int i=0;i<64;i++){
        for(int j=n;j>=1;j--){
            int z=suf[i][j+1][0];
            int o=suf[i][j+1][1];
            if((v[j]>>i)&1){
                ans+=(1LL<<i)%MOD*z%MOD;
            }else{
                ans+=(1LL<<i)%MOD*o%MOD;
            }
            ans%=MOD;
        }
    }
    cout<<ans;
}


#include <map>
#include <iostream>

using namespace std;

/** Problem0039 : Roman Figure **/
int main()
{
	char num[110]; int now, old, ans;
	map<char, int> list;
	list.insert(map<char, int>::value_type('I', 1));
	list.insert(map<char, int>::value_type('V', 5));
	list.insert(map<char, int>::value_type('X', 10));
	list.insert(map<char, int>::value_type('L', 50));
	list.insert(map<char, int>::value_type('C', 100));
	list.insert(map<char, int>::value_type('D', 500));
	list.insert(map<char, int>::value_type('M', 1000));

	while (cin >> num) {
		ans = 0;
		old = list[num[0]];
		for (int i=1; i<110 && num[i]!='\0'; i++) {
			now = list[num[i]];
			if (old == 0) {
				old = now;
			} else if (old<now) {
				ans += (now-old);
				old = 0;
			} else {
				ans += old;
				old = now;
			}
		}
		ans += old;
		cout << ans << endl;
	}

	return 0;
}

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

int main() {
	for (;;) {
		int e; cin >> e;
		if (e == 0) break;
		int m = INT_MAX;
		for (int z = 0; z * z * z <= e; z++) {
			int _e = e - z * z * z;
			int y = sqrt((double)_e), x = _e - y * y;
			m = min(m, x + y + z);
		}
		cout << m << endl;
	}
}


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

int main(void){
   long long A,B,K;
   cin>>A>>B>>K;
   for(int i=0;i<K;i++){
      if(i%2==0){
         if(A%2!=0){
            A-=1;
         }
         B+=A/2;
         A/=2;
      }else{
         if(B%2!=0){
            B-=1;
         }
         A+=B/2;
         B/=2;
      }
   }
   cout<<A<<" "<<B<<endl;
   return 0;
}

#include <iostream>
#include <string>

typedef long long ll;

void print()
{
  std::cout << std::endl;
}

template<class T>
void print(T param)
{
  std::cout << param << std::endl;
  return;
}

template<class T, class... S>
void print(T param1, S... param2)
{
  std::cout << param1 << " "; print(std::forward<S>(param2)...);
}

int main()
{
  ll A, B, K;
  std::cin >> A >> B >> K;

  for (ll i = 0; i < K; i++) {
    A /= 2;
    B += A;
    std::swap(A, B);
  }

  if (K % 2 == 1) {
    std::swap(A, B);
  }

  print(A, B);
  return 0;
}


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

const int needs[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

bool compare(vector<int>& a, vector<int>& b, int size) {
    int cnta = 0, cntb = 0;
    for (int i = 0; i < size; i++) {
        cnta += a[i];
        cntb += b[i];
    }
    if (cnta != cntb) {
        return cnta > cntb;
    } else {
        return a > b;
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> A(M);
    for (int i = 0; i < M; i++) {
        cin >> A[i];
    }
    sort(A.begin(), A.end());
    reverse(A.begin(), A.end());

    vector<vector<int>> dp(N + 1, vector<int>(M, -1));
    for (int i = 0; i < M; i++) {
        dp[0][i] = 0;
    }
    for (int i = 0; i < N; i++) {
        for (int m = 0; m < M; m++) {
            if (dp[i][0] == -1) {
                continue;
            }
            vector<int> tmp = dp[i];
            tmp[m]++;
            if (i + needs[A[m]] <= N && compare(tmp, dp[i + needs[A[m]]], M)) {
                dp[i + needs[A[m]]] = tmp;
            }
        }
    }
    // for (int n = 0; n <= N; n++) {
    //     cout << "n = " << n << endl;
    //     for (int i = 0; i < M; i++) {
    //         cout << A[i] << " " << dp[n][i] << endl;
    //     }
    // }

    string ans = "";
    for (int i = 0; i < M; i++) {
        ans += string(dp[N][i], A[i] + '0');
    }
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
}

#include<iostream>
#include<cstdio>
using namespace std;
int n,S,X[202020],st[202020],top;
long long P[202020];
void solve(int l,int r)
{
	if(S<=X[l]) return (void)(st[++top]=r);
	if(S>=X[r]) return (void)(st[++top]=l);
	if(P[l]<P[r]) return st[++top]=l,P[r]+=P[l],solve(l+1,r);
	else return st[++top]=r,P[l]+=P[r],solve(l,r-1);
}
int aabs(int x){ return x<0?-x:x;}
int main()
{
	scanf("%d%d",&n,&S);
	for(int i=1;i<=n;i++)
	scanf("%d%lld",&X[i],&P[i]);
	solve(1,n);
	long long ans=0;
	while(top) ans+=aabs(S-X[st[top]]),S=X[st[top--]];
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
typedef long long ll;

int main() {

    ll x,y;
    cin >> x >> y;
    ll ans;
    if (x * y > 0) {
        if (y >= x) ans = y - x;
        else ans = x - y + 2;
    } else if (x * y < 0) {
        ans = abs(abs(x)-abs(y)) + 1;
    } else {
        if (y > x) ans = y - x;
        else ans = x - y + 1;

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

#include <stdio.h>

int main ()
{
	char x[100];
	char y[100];

	scanf("%s%s\n", &x,&y);

	printf("%s%s\n", y, x);

	return 0;
}


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

int main(){
    int a, b;
    cin >> a >> b;
    string s;
    cin >> s;
    string ans = "Yes";
    for(int i = 0; i < a; i++){
        if(!(s[i] >= '0' && s[i] <= '9')) ans = "No";
    }
    if(s[a] != '-') ans = "No";
    for(int i = a+1; i < a+b+1; i++){
        if(!(s[i] >= '0' && s[i] <= '9')) ans = "No";
    }
    cout << ans << endl;
    return 0;
}

#include<stdio.h>

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

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

    int i, count = 0;

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

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

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

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

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
//#define INF (1e9+1)
#define INF (1LL<<59)

#define int ll

signed main(){
    int v,e;
    cin>>v>>e;
    int d[101][101];
    rep(i,101)rep(j,101)d[i][j] = INF;
    rep(i,101)d[i][i] = 0;

    rep(i,e){
        int s,t,c;
        cin>>s>>t>>c;
        d[s][t] = c;
    }

    rep(k,v){
        rep(i,v){
            rep(j,v){
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }

    rep(i,v){
        if(d[i][i]<0){
            cout<<"NEGATIVE CYCLE"<<endl;
            return 0;
        }
    }

    rep(i,v){
        rep(j,v){
            if(j)cout<<" ";
            int res=d[i][j];
            if(res>=INF/2)cout<<"INF";
            else cout<<res;
        }
        cout<<endl;
    }
}

#include<bits/stdc++.h>
#define MAXN 1000005
#define INF int(16843009)
using namespace std;

int N,M;

struct edge{
	int u,v;
	edge(int u=0, int v=0):u(u), v(v){}
};

vector<edge> adjc[MAXN];
vector<int> adj1[MAXN];

void addE(int u, int v){
	cerr<<"addE: "<<u<<" "<<v<<endl;
	adj1[u].push_back(v);
	adj1[v].push_back(u);
}

int d[MAXN];
void bfs(){
	memset(d, 1, sizeof(d));
	d[1] = 0;
	queue<int> q;
	q.push(1);

	int u,v;
	while(!q.empty()){
		u = q.front(); q.pop();
		if(u==N) return;

		for(int k=0;k<adj1[u].size();k++){
			v = adj1[u][k];
			if(d[u] + 1 < d[v]){
				d[v] = d[u] + 1;
				q.push(v);
			}
		}
	}
}

int fa[MAXN];
int p[MAXN];
int findR(int x){
	if(p[x]==x) return x;
	else return p[x] = findR(p[x]);
}

int main(){

	scanf("%d%d", &N, &M);

	int u,v,c;
	for(int i=1;i<=M;i++){
		scanf("%d%d%d", &u, &v, &c);
		adjc[c].push_back(edge(u,v));
	}

	int top = N;
	for(int i=1;i<=1e6;i++){
		//cout<<i<<endl;
		if(adjc[i].size()==0) continue;
		//
		for(int k=0;k<adjc[i].size();k++){
			u = adjc[i][k].u; v = adjc[i][k].v;
			p[u] = u; p[v] = v;
			fa[u] = fa[v] = 0;
		}

		int ru,rv;
		for(int k=0;k<adjc[i].size();k++){
			u = adjc[i][k].u; v = adjc[i][k].v;
			ru = findR(u); rv = findR(v);
			p[ru] = rv;
		}

		for(int k=0;k<adjc[i].size();k++){
			u = adjc[i][k].u; v = adjc[i][k].v;
			ru = findR(u); rv = findR(v);
			//
			if(fa[ru]) addE(u,fa[ru]);
			else{
				fa[ru] = ++top;
				addE(u,top);
			}

			//
			if(fa[rv]) addE(v,fa[rv]);
			else{
				fa[rv] = ++top;
				addE(v,top);
			}
		}
	}
	//cout<<"pass"<<endl;
	bfs();

	if(d[N]==INF) cout<<"-1"<<endl;
	else cout<<d[N]/2<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
const int M = 1'000'000'007;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<ll> x(n), y(m);
    cin >> x >> y;
    adjacent_difference(x.begin(), x.end(), x.begin());
    adjacent_difference(y.begin(), y.end(), y.begin());
    ll ysum = 0;
    for(int j = 1; j < m; j++) {
        ysum += y[j] * (m - j) % M * j % M;
        ysum %= M;
    }
    ll xsum = 0;
    for(int i = 1; i < n; i++) {
        xsum += x[i] * (n - i) % M * i % M;
        xsum %= M;
    }
    cout << xsum * ysum % M << endl;
    return 0;
}

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

const int mod = 1e9 + 7;

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


#include <bits/stdc++.h>
using namespace std;
using Graph= vector<vector<int>>;
const int TBC= -1;

void DFS(const Graph &tree, int parent, int root, vector<int> *parents, vector<int> *pre_timestamp, vector<int> *post_timestamp, vector<int> *depth){
  if(parents->at(root)==TBC){
    parents->at(root)= parent;
    pre_timestamp->at(root)= depth->size();
  }
  int depth_root= (!depth->empty()) ? depth->back()+ 1
                                    : 0;
  depth->emplace_back(depth_root);
  for(auto child:tree.at(root)){
    if(child != parent){
      DFS(tree, root, child, parents, pre_timestamp, post_timestamp, depth);
      depth->emplace_back(depth_root);
    }
  }
  post_timestamp->at(root)= depth->size();
}

int main(){
  int N,v0_runner,v0_trailer;
  cin >> N >> v0_runner >> v0_trailer;
  const int M= N-1;
  v0_runner--; v0_trailer--; // 1-indexed -> 0-indexed

  Graph tree(N);

  int v1,v2;
  for(int i=0; i<M; i++){
    cin >> v1 >> v2;
    v1--; v2--; // 1-indexed -> 0-indexed
    tree.at(v1).emplace_back(v2);
    tree.at(v2).emplace_back(v1);
  }

  vector<int> parents(N, TBC);
  vector<int> pre_timestamp(N, TBC);
  vector<int> post_timestamp(N, TBC);
  vector<int> depth;

  DFS(tree, -1, v0_trailer, &parents, &pre_timestamp, &post_timestamp, &depth);

  int depth0_runner= depth.at(pre_timestamp.at(v0_runner));
  int cnt_up= (depth0_runner- 1)/ 2;
  int pos_runner= v0_runner;
  for(int i=0; i<cnt_up; i++){
    pos_runner= parents.at(pos_runner);
  }

  int depth1_runner= depth.at(pre_timestamp.at(pos_runner));
  int left = pre_timestamp.at(pos_runner);
  int right= post_timestamp.at(pos_runner);
  int depth_tree= *max_element(depth.begin()+ left, depth.begin()+ right);

  int ans= (depth0_runner/ 2)+ (depth_tree- depth1_runner);

  cout << ans << endl;
}

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define fi first
#define sc second
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << endl
#define RETURN(x)    \
  cout << x << endl; \
  return 0
clock_t CLOCK;
#define START_TIMER CLOCK = clock()
#define SHOW_TIMER cerr << "time: " << (ld)(clock() - CLOCK) / 1000000 << endl
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(i, v.size()) cerr << " " << v[i]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

signed main() {
  init();
  ll N, u, v;
  cin >> N >> u >> v;
  u--;
  v--;

  vvll to(N);
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].pb(b);
    to[b].pb(a);
  }

  queue<P> q1;
  q1.push({u, 1});
  vll d1(N);
  while (!q1.empty()) {
    ll index = q1.front().first;
    ll distance = q1.front().second;
    q1.pop();
    if (d1[index] != 0) continue;
    d1[index] = distance;
    rep(i, to[index].size()) {
      q1.push({to[index][i], distance + 1});
    }
  }

  debugV(d1);

  queue<P> q2;
  q2.push({v, 1});
  vll d2(N);
  while (!q2.empty()) {
    ll index = q2.front().first;
    ll distance = q2.front().second;
    q2.pop();
    if (d2[index] != 0) continue;
    d2[index] = distance;
    rep(i, to[index].size()) {
      q2.push({to[index][i], distance + 1});
    }
  }

  debugV(d2);

  ll ans = 0;
  rep(i, N) {
    if (d1[i] < d2[i]) ans = max(ans, d2[i]);
  }
  ans--;
  ans--;
  COUT(ans);
  return 0;
}


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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int main() {
	string L = "";
	for_(i,0,26) L += char('a' + i);
	for_(i,0,26) L += char('A' + i);

	int n;
	while (cin >> n, n) {
		vector< int > k(n);
		for_(i,0,n) cin >> k[i];

		string s;
		cin >> s;

		int m = s.size();
		for_(i,0,m) {
			int pos = L.find(s[i]);
			s[i] = L[(pos - k[i%n] + 52) % 52];
		}
		cout << s << endl;
	}
}

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define perf(i, n) for(ll i=((ll)(n)); i>0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

int main(){
    Init();
    ll n, t, sum = 0; cin >> n >> t;
    ll pt; cin >> pt;
    rep(i, n-1){
        ll np; cin >> np;
        if(np-pt >= t) sum += t;
        else sum += np-pt;
        pt = np;
    }
    sum += t;
    cout << sum << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2000+5,base=2001*3;
int f[N*3][N*6],mod;
void add(int &x,int y){
	x=(x+y)%mod;
}
signed main(){
	int n;
	cin>>n>>mod;
	n*=3;
	f[0][base]=1;
	for(int i=0;i<n;++i)
		for(int j=-i;j<=i;++j)
			if(f[i][j+base]){
				add(f[i+1][j+1+base],f[i][j+base]);
				add(f[i+2][j-1+base],1ll*f[i][j+base]*(i+1)%mod);
				add(f[i+3][j+base],1ll*f[i][j+base]*(i+1)%mod*(i+2)%mod);
			}
	int ans=0;
	for(int j=0;j<=n;++j)
		add(ans,f[n][j+base]);
	cout<<ans;
	return 0;
}

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

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

    string s;
    cin >> s;

    vector<int> pref(n, 0), suff(n, 0);

    for (int i = 1; i < n; i++) {
        pref[i] = pref[i-1] + (s[i-1] == 'W');
    }
    for (int i = n-2; i >= 0; i--) {
        suff[i] = suff[i+1] + (s[i+1] == 'E');
    }

    int ans = 1e8;
    for (int i = 0; i < n; i++) {
        ans = min(ans, pref[i] + suff[i]);
    }

    cout << ans << endl;
}


/*!enjoy karo yaar!*/

//CF,CC,AtC,SPOJ: hp1999
//HE: hemant269
//HR: hemant2132

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

/*-----<Defines>-----*/
#define     int             long long int
#define     fast()          ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define     all(x)          begin(x),end(x)
#define     rz(x)           resize(x)
#define     asn(x,y)        assign(x,y)
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     sz(x)           ((int)size(x))
#define     eb              emplace_back
#define     pb              push_back
#define     pf              push_front
#define     pob             pop_back
#define     pof             pop_front
#define     ins             insert
#define     vi              vector<int>
#define     pii             pair<int,int>
#define     mii             map<int,int>
#define     F               first
#define     S               second
#define     remax(a,b)      a=max(a,b)
#define     remin(a,b)      a=min(a,b)
#define     bitcount(x)     __builtin_popcountll(x)
#define     iceil(n,x)      (((n)-1)/(x)+1)
#define     flush           fflush(stdout)

using ull=unsigned long long;
using ll=long long;
using ld=long double;
/*-----</Defines>-----*/

/*-----<Debugging Stuff>-----*/
string to_string(char s)
{   string res="'''";res[1]=s;return res; }

string to_string(string s)
{   return '"'+s+'"'; }

string to_string(const char* s)
{   return to_string((string)s); }

string to_string(bool b)
{   return (b?"true":"false"); }

template<typename A, typename B>
string to_string(pair<A, B> p)
{   return "("+to_string(p.F)+", "+to_string(p.S)+")"; }

template<typename A>
string to_string(A v)
{
    bool first=1; string res="{";
    for(const auto &x:v)
    {
        if(!first)  res+=", ";
        first=0;
        res+=to_string(x);
    }
    res+="}";
    return res;
}

void debug() { cout<<"\n"; }
template<typename Head, typename... Tail>
void debug(Head H, Tail... T)
{
    cout<<" "<<to_string(H);
    debug(T...);
}

#define show(...) cout << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
/*-----</Debugging Stuff>-----*/

/*-----<Mod Operations>-----*/
const int M=1e9+7;
inline int add(int a,int b,int p=M){ a=a+b; if(a>=p) a-=p; return a; }
inline int sub(int a,int b,int p=M){ a=a-b; if(a<0) a+=p; return a; }
inline int mul(int a,int b,int p=M){ a=(a*1ll*b)%p; return a; }
/*-----</Mod Operations>-----*/

/*-----<Constants>-----*/
const ld pi=acos(-1),eps=1e-9;
const ll inf=1e18;
const int N=1;
/*-----</Constants>-----*/

void solve()
{
    int n,m;
    cin>>n>>m;

    if(n%2)
    {
        for(int cur=1;m;--m,++cur)
        {
            cout<<cur<<" "<<n-cur+1<<"\n";
        }

        return;
    }

    int mx=(n-1)/2;
    int split;
    if(mx%2)
        split=mx-1;
    else
        split=mx;

    int l=1,r=split+1;
    for(int i=r-l;i>=2;i-=2)
    {
        if(i<=m)
            cout<<l<<" "<<r<<"\n";

        ++l,--r;
    }

    l=split+2;
    r=l+split+(mx%2?1:-1);
    for(int i=r-l;i>=1;i-=2)
    {
        if(i<=m)
            cout<<l<<" "<<r<<"\n";

        ++l,--r;
    }
}

int32_t main()
{
    fast();

    int t=1;
    //cin>>t;
    for(int z=1;z<=t;++z)
    {
        solve();
        //cout<<"\n";
    }

    return 0;
}

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

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> num(N, -1);
    for (int i=0; i<M; i++) {
        int si, ci;
        cin >> si >> ci;
        si--;
        if ((num[si] != -1) && (num[si] != ci)){
            cout << -1;
            return 0;
        }
        num[si] = ci;
    }
    if (N == 1) {
        if (num[0] == -1) {
            cout << 0;
            return 0;
        } else {
            cout << num[0];
            return 0;
        }
    } else {
        for (int j=0; j<N; j++){
            if (j == 0 && num[j] == 0) {
                cout << -1;
                return 0;
            }
            if (j == 0 && (num[j] == -1)){
                num[j] = 1;
            }

            if (num[j] == -1) {
                num[j] = 0;
            }
        }
    }

    // // bool first_val = true;
    // for (int j=0; j<N; j++) {
    //     if ((j == 0) && (num[j] == 0) && (N != 1)){
    //         cout << -1;
    //         return 0;
    //     } else if ((j == 0) && (num[j]==-1)) {
    //         num[j] = 1;
    //     }

    //     // if (num[j] != -1) first_val = false;
    //     // if ((j==0) && (num[j] == -1)) num[j] = 1;
    //     if (num[j] == -1){
    //         num[j] = 0;
    //     }
    // }

    int ans = 0;
    for (int k=0; k<N;k++) {
        ans = ans * 10 + num[k];
    }
    cout << ans << endl;
}


#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,ans=0x3f3f3f3f;
int a[100000+5];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n-m+1;i++){
		int j=m+i-1;
		ans=min(ans,min(abs(a[i])+abs(a[j]-a[i]),abs(a[j])+abs(a[j]-a[i])));
	}
	if(ans==0x3f3f3f3f) cout<<0<<endl;
	cout<<ans<<endl;
}

#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <numeric>
#include <queue>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <map>
#include <functional>
#include <limits>
#include <set>
#include <stack>
#include <cctype>
#define rep(i,a,b) for((i)=a;i<(int)(b);i++)

#define AAA (1000000007)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pairII;

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

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll powD(ll a, ll b) {
	ll ans = 1;
	a %= AAA;
	for (int i = 0; i < b; i++)
	{
		ans *= a;
		ans %= AAA;
	}
	return ans;
}

int main() {
	int i, j, k;

	int N, M, K;

	vector<int> X;
	cin >> N >> K;
	rep(i, 0, N) {
		int x;
		cin >> x;
		X.push_back(x);
	}
	ll ans = abs(X[0])*2 + abs(X[N - 1])*2;

	rep(i, 0, N-K+1) {
		ll a = min(abs(X[i]), abs(X[i + K-1]));
		ll b = max(abs(X[i]), abs(X[i + K-1]));
		if (X[i] < 0 && X[i + K - 1]>0) {
			ans = min(ans, a * 2 + b);
		}
		else {
			ans = min(ans,b);
		}

	}
	cout << ans;
	return 0;
}


#include <stdio.h>
int main(){
	int n,now=-1,temp,a[100000];
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		if(a[i]<=a[n-1]){
			now++;
			temp=a[i];
			a[i]=a[now];
			a[now]=temp;
		}
	}
	for(int i=0;i<now;i++)printf("%d ",a[i]);
	printf("[%d]",a[now]);
	for(int i=now+1;i<n;i++)printf(" %d",a[i]);
	printf("\n");
}

#include<iostream>
#include<map>
#include<string>
#include<vector>
using namespace std;
int inf = 10000000;
int n;
vector<int> a;
int partition(int p, int r){
  int x = a[r-1];
  int i = p-1;
  for(int j=p; j<r; j++){
    if( a[j] <= x ){
      // i = i + 1;
      swap(a[++i], a[j]);
    }
  }
  // swap(a[i+1], a[r]);
  return i;
}

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

  int p = partition(0, n);

  for(int i=0; i<n; i++){
    if( i == p ) cout << "[" << a[i] << "]";
    else cout << a[i];
    if( i == n-1 ) cout << endl;
    else cout << " ";
  }

  return 0;
}


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

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

#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
//ll mod = 998244353;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    string s;
    cin >> s;
    vector<bool> flg(26, false);
    int n = s.size();
    rep(i, n) flg[s[i] - 'a'] = true;
    rep(i, 26) {
        if (!flg[i]) {
            cout << (char)(i + 'a') << endl;
            return 0;
        }
    }
    cout << "None" << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007
#define int long long int
#define pii pair<int,int>
#define vpii vector<pii>
#define vi vector<int>
#define fi first
#define se second
#define psb push_back
#define ppb pop_back
#define fio ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define endl "\n"

int ar[300];

int32_t main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>ar[i];
		string s;
		cin>>s;
		int ans=0;
		set<int,greater<int>> st;
		for(int i=n-1;i>=0;i--)
		{
			for(int e:st)
				ar[i]=min(ar[i],ar[i]^e);
			if(ar[i]==0)
				continue;
			if(s[i]=='0')
				st.insert(ar[i]);
			else
			{
				ans=1;
				break;
			}
		}
		cout<<ans<<endl;
	}
}

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

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

namespace SCC{
    void visit(const vector<vector<int>>&G,vector<int>&vs,vector<int>&used,int v){
        used[v]=true;
        for(auto u:G[v]){
            if(!used[u])visit(G,vs,used,u);
        }
        vs.push_back(v);
    }

    void visit2(const vector<vector<int>>&T,vector<int>&used,vector<int>&comp,int k,int v){
        comp[v]=k;
        used[v]=true;
        for(auto u:T[v]){
            if(!used[u])visit2(T,used,comp,k,u);
        }
    }

    void decompose(const vector<vector<int>>&G,vector<vector<int>>&H,vector<int>&comp){
        vector<vector<int>>T(G.size());
        for(int i=0;i<G.size();i++){
            for(auto v:G[i]){
                T[v].push_back(i);
            }
        }
        comp.resize(G.size());

        vector<int>vs(G.size());
        vector<int>used(G.size());
        for(int i=0;i<G.size();i++){
            if(!used[i])visit(G,vs,used,i);
        }
        reverse(vs.begin(),vs.end());
        fill(used.begin(),used.end(),0);

        int K=0;
        for(auto v:vs){
            if(!used[v]){
                visit2(T,used,comp,K++,v);
            }
        }

        H.resize(K);
        for(int i=0;i<G.size();i++){
            for(auto v:G[i]){
                H[comp[i]].push_back(comp[v]);
            }
        }
        for(int i=0;i<K;i++){
            sort(H[i].begin(),H[i].end());
            H[i].erase(unique(H[i].begin(),H[i].end()),H[i].end());
        }
    }
}

signed main(){
    int N,M;
    scanf("%lld%lld",&N,&M);
    vector<vector<int>>G(N);
    rep(i,M){
        int a,b;
        scanf("%lld%lld",&a,&b);
        G[a].push_back(b);
    }

    vector<vector<int>>H;
    vector<int>comp;

    SCC::decompose(G,H,comp);

    int Q;
    scanf("%lld",&Q);
    while(Q--){
        int a,b;
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",(int)(comp[a]==comp[b]));
    }
    return 0;
}


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

int main() {
  string s, ans;
  int w, i = 0;
  cin >> s >> w;
  while(i < (int)s.size()) {
    ans += s[i];
    i += w;
  }
  cout << ans << "\n";
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int A,B,ans=0; cin >> A >> B;
  if(A>B) ans += 2*A -1;
  else if(A<B) ans += 2*B -1;
  else if(A==B) ans += 2*A;

  cout << ans << endl;
}


#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
#define pvec(vec) {for (auto v: vec) cout << v << ' '; cout << endl;}
#define pivec(vec) {rep(i, 0, vec.size()) cout << i << ':' << vec[i] << ' '; cout << endl;}

using namespace std;
using ll = long long;

// Binary Indexed Tree (Fenwick Tree)
template<typename T>
struct BIT {
  int n;
  vector<T> d;
  BIT(int n=0):n(n),d(n+1) {}
  void add(int i, T x=1) {
    for (i++; i <= n; i += i&-i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i&-i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) {
    return sum(r-1) - sum(l-1);
  }
};

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

    // read input as range
    vector<pair<int, int>> clr_rng;
    {
        unordered_map<int, int> prepos;
        rep(i, 1, N+1)
        {
            int c;
            cin >> c;
            clr_rng.emplace_back(prepos[c], i);
            prepos[c] = i;
        }
    }
    vector<tuple<int, int, int>> qry;
    rep(i, 0, Q)
    {
        int l, r;
        cin >> l >> r;
        qry.emplace_back(l, r, i);
    }

    // sort by start pos
    sort(clr_rng.rbegin(), clr_rng.rend());
    sort(qry.rbegin(), qry.rend());

    // countint with bit
    BIT<ll> bit(N+1);
    vector<int> ans(Q);
    int pos_c = 0;
    int pos_q = 0;
    rrep(pos, N+1, 1)
    {
        // add range
        while (pos_c < (int) clr_rng.size())
        {
            int frm, to;
            tie(frm, to) = clr_rng[pos_c];
            if (frm < pos) break;

            // printf("from: %d, to: %d\n", frm, to);

            bit.add(to, 1);
            ++pos_c;
        }

        // process query
        while (pos_q < (int) qry.size())
        {
            int frm, to, i;
            tie(frm, to, i) = qry[pos_q];
            if (frm < pos) break;

            int num = to - frm + 1;
            num -= bit.sum(to);

            // printf("from: %d, to: %d, i: %d, bit: %lld\n", frm, to, i, bit.sum(to));

            ans[i] = num;
            ++pos_q;
        }

    }

    // output answer
    rep(i, 0, Q) printf("%d\n", ans[i]);

    return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
#include<stdio.h>
#include<string.h>
#include <bitset>
// #include<assert.h>
#include<random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 100010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

//https://www.youtube.com/watch?v=RDaGj_ztNz4&feature=youtu.be

//-----CODE------//

int dfs(int v,int c,vector<vector<int> > & GRAPH,vector<int> & color)
{

  color[v] = c;
  // DEBUG(color);
  for (auto &&u : GRAPH[v])
  {
    if(color[u] == c)
    {
      return false;
    }

    if(color[u] ==0 && !dfs(u,-1*c,GRAPH,color))
    {
    return false;
    }
  }
  return true;

}
void solve(void)
{
  int n,m;
  cin>>n>>m;
  vector<vector<int> > GRAPH (n);
  for (int i = 0; i < m; i++)
  {
    int A,B;
    cin>>A>>B;
    A--;B--;
    GRAPH[A].push_back(B);
    GRAPH[B].push_back(A);
  }
  int b = 0,w = 0;
  vector<int> color(n,0);
  if(dfs(0,1,GRAPH,color))
  {
    for (int i = 0; i < n; i++)
    {
      if(color[i] == 1)
      {
        b++;
      }
      else if(color[i] == -1)
      {
        w++;
      }
    }
    cout<<b*w - m <<endl;

  }
  else
  {
    cout<<n*(n-1)/2 - m<<endl;
  }

  return;
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


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

int main()
{
	int n, m; scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i)
		if (2 * i < m + 2) printf("%d %d\n", i, m + 2 - i);
	for (int i = 1; i <= m; ++i)
		if (2 * i < m + 1) printf("%d %d\n", m + 1 + i, 2 * m + 2 - i);
	return 0;
}

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

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

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

void solve() {
    string s;
    int w;
    cin >> s;
    cin >> w;
    REP(i,s.size()) if(i%w == 0) cout << s[i];
    cout << endl;
}

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


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i=0;i<(n);i++)
#define init(a,i) for(int k=0;k<(i);k++)(a)[k]=0
#define in(a,i) for(int k=0;k<(i);k++)cin>>(a)[k]
int main(){
	ll n;
	cin>>n;
	stack<char> st;
	while(n!=0){
		n--;
		st.push('a'+n%26);
		n/=26;
	}
	while(!st.empty()){
		cout<<st.top();
		st.pop();
	}
	return 0;
}

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

using namespace std;

string convert(string str) {
    int idx = 0;
    for (;;) {
        idx = str.find("Hoshino", idx);
        if (idx == string::npos) break;
        str.replace(idx, 7, "Hoshina");
        idx += 7;
    }
    return str;
}

int main() {
    int n;
    string str;
    getline(cin, str);
    n = atoi(str.c_str());
    for (int i = 0; i < n; i++) {
        getline(cin, str);
        cout << convert(str) << endl;
    }
    return 0;
}


#include <stdio.h>

int main(){
    char c;
    char s[21]={'\0'};
    int i = 0,j;
    c = getchar();
    while(c != '\n'){
        for(j=i;j>=0;j--){
            s[j+1] = s[j];
        }
        i++;
        s[0] = c;
        c = getchar();
    }
    printf("%s\n",s);
}

#include <bits/stdc++.h>
#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 INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int,int>;

vector<ll> vec;
vector<ll> vec2;

void init(){
  ll x = 1;
  ll x2 = 1;
  while(x<10000000000){
    vec.push_back(x);
    x2*=4;
    x+=x2;
  }
  ll y = 0;
  ll y2 = 2;
  while(y>-10000000000){
    vec2.push_back(y);
    y-=y2;
    y2*=4;
  }
}

vector<ll> ans(33,0);

void solve(ll N){
  if(N==1){
    ans.at(0)=1;
  }else if(N==0){
    ans.at(0)=0;
  }else if(N>1){
    REP(i,vec.size()){
      if(N<=vec.at(i)){
        ans.at(i*2)=1;
        solve(N-pow(2,i*2));
        break;
      }
    }
  }else if(N<0){
    REP(i,vec2.size()){
      if(vec2.at(i)<=N){
        ans.at(i*2-1)=1;
        solve(N+pow(2,i*2-1));
        break;
      }
    }
  }
}

int main()
{
  ll N;
  cin >> N;
  init();
  solve(N);

  int flag = 0;

  REPR(i,32){
    if(ans.at(i)==1&&flag==0){
      flag=1;
    }
    if(flag==1){
      cout << ans.at(i);
    }
  }
  if(flag==0){
    cout << 0;
  }
  cout << endl;
}


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

int main() {
  int k,x;
  cin >> k >> x;
  for(int i=-k+1;i<k;i++){
    cout << x+i << " ";
  }
}

#include <bits/stdc++.h>
//登録しよう！→ https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
using namespace std;
using ll = long long;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}//a,bの最大公約数(gcd)を求める
ll lcm(ll a,ll b){return abs(a*b)/gcd(a,b);}//a,bの最小公倍数(lcm)を求める
vector<ll> enum_div(ll n){vector<ll> ret;for(int i=1 ; i*i<=n ; i++){if(n%i == 0){ret.push_back(i);if(i!=1 && i*i!=n)ret.push_back(n/i);}}ret.push_back(n);return ret;}
//↑nの約数を求める
vector<bool> IsPrime; void sieve(size_t max){if(max+1 > IsPrime.size())IsPrime.resize(max+1,true);IsPrime[0] = false;IsPrime[1] = false;for(size_t i=2; i*i<=max; ++i)if(IsPrime[i])for(size_t j=2; i*j<=max; ++j)IsPrime[i*j] = false;}
//↑エラトステネスの篩で素数を求める
#define roundup(divisor,dividend) (divisor + (dividend - 1)) / dividend //切り上げ割り算
#define all(x) (x).begin(),(x).end() //xの初めから終わりまでのポインタ
#define size_t ll //size_tは自動でllに変換される
#define pb(x) push_back(x)
#define gre greater //sortを降順にする
#define pri_queue priority_queue //優先度付きキュー
#define syo(x) fixed << setprecision(x) //iostreamで小数をx桁表示

void solve(std::string S, long long w){
  string ans ="";
  int i = 0;
  while(i < S.length()){
    ans += S[i];
    i += w;
  }
  cout << ans << endl;
}

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


#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define int long long int
constexpr int INF = std::numeric_limits<int>::max();
constexpr int NINF = std::numeric_limits<int>::min();
using namespace std;

void solve() {
  int M;
  cin >> M;
  int ans = 0;
  int sum = 0;

  while (M--) {
    int c, d; cin >> d >> c;
    ans += c;
    sum += d*c;
  }

  ans -= 1;
  ans += (sum - 1)/9;

  cout << ans << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll M;
deque<ll> d;
deque<ll> c;
deque<ll> e;

int main(){
    cin >> M;
    for(int i=0;i<M;i++){
        ll a,b;
        cin >> a >> b;
        d.push_back(a);
        c.push_back(b);
    }
    ll count = 0;
    while(d.size() > 0){
        if(c[0] == 1){
            e.push_back(d[0]);
            c.pop_front();
            d.pop_front();
            continue;
        }
        ll dd = d[0];
        ll cc = c[0];
        c.pop_front();
        d.pop_front();
        if(dd < 5){
            if(cc % 2 == 1){
                c.push_front(1);
                d.push_front(dd);
            }
            c.push_front(cc/2);
            d.push_front(dd*2);
            count += cc/2;
            continue;
        }
        if(cc % 2 == 1){
            c.push_front(1);
            d.push_front(dd);
        }
        dd = (dd*2)/10 + (dd*2)%10;
        c.push_front(cc/2);
        d.push_front(dd);
        count += cc/2 * 2;
    }
    while(e.size() > 1){
        ll a = e[0];
        ll b = e[1];
        e.pop_front();
        e.pop_front();
        if(a+b < 10){
            count++;
            e.push_front(a+b);
        }else{
            count += 2;
            e.push_front((a+b)/10 + (a+b)%10);
        }
    }
    cout << count << endl;
    return 0;
}


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

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair

long long max_box_area_in_histogram(vector<int>& a) {
    int N = a.size();

    vector<int> L(N);
    stack<int> st;

    for (int i=0; i<N; ++i) {
        while (!st.empty() && a[st.top()] >= a[i]) {
            st.pop();
        }
        L[i] = (st.empty() ? -1 : st.top())+1;
        st.push(i);
    }

    while (!st.empty()) st.pop();

    vector<int> R(N);
    for (int i=N-1; i>=0; --i) {
        while (!st.empty() && a[st.top()] >= a[i]) {
            st.pop();
        }
        R[i] = (st.empty() ? N : st.top())-1;
        st.push(i);
    }

    long long smax = 0;
    for (int i=0; i<N; ++i) {
        int h = a[i], w = R[i] - L[i] + 1;
        // ll s = (long long)h * w;
        ll s = (long long)(h + 1)*(w + 1);
        smax = max(smax, s);
    }

    return smax;
}

ll solve(int H, int W, vector<string>& s) {
    vvi a(H-1, vi(W-1, 0));
    rep(r, H-1) {
        rep(c, W-1) {
            a[r][c] = (s[r][c] == '#') ^ (s[r][c+1] == '#')
                    ^ (s[r+1][c] == '#') ^ (s[r+1][c+1] == '#');
        }
    }

    ll best = max(W, H);

    vi histo(W-1, 0);
    rep(r, H-1){
        rep(c, W-1) {
            if (a[r][c]) histo[c] = 0;
            else         histo[c]++;
        }
        best = max(best, max_box_area_in_histogram(histo));
    }

    return best;
}

int main() {
    int H, W; cin >> H >> W;
    vector<string> s(H);
    rep(i, H) cin >> s[i];
    cout << solve(H,W,s) << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w))
#define dump(...)
const signed INF_=1001001001; const ll INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct{template<class T> operator T(){T x;cin>>x;return x;}} IN;
struct before_main_function {
    before_main_function() {
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

int H,W;
vector<vector<int>> b;
int calc(int y) {
    int ret=0;
    stack<pair<int,int>> st;
    REP(j,W) {
        if(st.empty() || st.top().first<b[y][j]) {
            st.push({b[y][j],j});
        } else if(st.top().first>b[y][j]) {
            int idx=0;
            while(!st.empty() && st.top().first>b[y][j]) {
                auto tp=st.top();
                idx=tp.second;
                st.pop();
                chmax(ret,(j-tp.second+1)*tp.first);
            }
            st.push({b[y][j],idx});
        }
    }
    return ret;
}
signed main() {
    cin>>H>>W;
    vector<string> S(H);
    cin>>S;
    b=vector<vector<int>>(H,vector<int>(W,0));
    REP(j,W-1) {
        b[0][j]=1;
        REPF(i,1,H) {
            if((S[i][j]==S[i][j+1])==(S[i-1][j]==S[i-1][j+1])) {
                b[i][j]=b[i-1][j]+1;
            } else {
                b[i][j]=1;
            }
        }
    }
    int ans=max(H,W);
    REP(i,H) {
        chmax(ans,calc(i));
    }
    cout<<ans<<endl;
    return 0;
}


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

template< typename T >
size_t longest_increasing_subsequence(const vector< T > &a, bool strict) {
  vector< T > lis;
  for(auto &p : a) {
    typename vector< T >::iterator it;
    if(strict) it = lower_bound(begin(lis), end(lis), p);
    else it = upper_bound(begin(lis), end(lis), p);
    if(end(lis) == it) lis.emplace_back(p);
    else *it = p;
  }
  return lis.size();
}

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

  cout<<longest_increasing_subsequence(A,false)<<endl;
  return 0;
}

#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7)    //0.0000001 the value
#define PI (acos(-1))
#define MAXN (300006)
#define MAXK 26
#define MAXX 15000006
#define ll long long int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = ss; ii < ee; ++ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll(x)
#define p2(x) (1LL<<(x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
ll n, A[MAXN], dp[302*302], dp2[302*302];
const ll MOD=998244353;
ll qexp(ll x, ll e) {
	if(e==0) return 1;
	ll half = qexp(x,e/2);
	half *= half;
	half %= MOD;
	if(e%2) half *= x;
	half %= MOD;
	return half;
}
int main()
{
	cin>>n; FOR(i,0,n) cin>>A[i];
	ll sum=accumulate(A,A+n,0);
	dp[0]=1;dp2[0]=1;
	for(ll i=0;i<n;i++) {
		for(ll j=sum;j>=0;j--) {
			// dp[j] = dp[j] + (dp[j]*2)%MOD; dp[j] %= MOD;
			dp[j] *= 2; dp[j] %= MOD;
			if(j>=A[i])dp[j] += dp[j-A[i]]; dp[j] %= MOD;
			if(j>=A[i])dp2[j] += dp2[j-A[i]]; dp2[j] %= MOD;
		}
	}
	ll ans = qexp(3,n-1);
	for(ll i=(sum+1)/2;i<=sum;++i) {
		ans -= (dp[i]);
		ans += MOD;
		ans %= MOD;
	}
	if(sum%2==0) {
		ans += dp2[sum/2];
		ans %= MOD;
	}
	cout<<(ans*3LL)%MOD<<'\n';
}


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

bool check(string s){
  if(s == "Vacant") return true;
  else return false;
}

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

  int n;
  cin >> n;
  cout << 0 << endl;
  vector<string> s(n);
  cin >> s[0];
  if(check(s[0])) return 0;
  cout << n-1 << endl;
  cin >> s[n-1];
  if(check(s[n-1])) return 0;
  int lb =0;
  int ub = n-1;
  while(ub - lb>1){
    int mid = (ub+lb)/2;
    cout << mid << endl;
    cin >> s[mid];
    if(check(s[mid])) return 0;
    if((mid - lb)%2==0){
      if(s[mid]==s[lb]){
        lb = mid;
      }
      else{
        ub = mid;
      }
    }
    else{
      if(s[mid]!=s[lb]){
        lb = mid;
      }
      else{
        ub = mid;
      }
    }
  }
  cout << ub << endl;
  cin >> s[ub];
  if(check(s[ub])) return 0;
  cout << lb << endl;
  cin >> s[lb];
  return 0;

}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<ll,ll>
#define poly vector<ll>
#define mem(a) memset((a),0,sizeof(a))
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x.size()))
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
ll rnd(){
	ull ans=0;
	For(i,0,4)ans=ans<<15^rand();
	return ans%((ull)1<<63);
}
const int N=10005;
int a[N],b[N];
void GG(){
	puts("No"); exit(0);
}
char ask(int t){
	cout<<t<<endl;
	string s;
	cin>>s;
	if(s[0]=='V')exit(0);
	return s[0];
}
void solve(int l,int r,char L){
	int mid=(l+r)>>1;
	char t=ask(mid);
	if((t==L)^(l%2)^(mid%2))solve(mid,r,t); else solve(l,mid,L);
}
int main(){
	#ifdef Brollan
		//freopen("1.in","r",stdin);
	#endif
	int n=read();
	ask(n-1);
	solve(0,n-1,ask(0));
}


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

int W , H , sx , sy ;
int ans ;
char p ;
int dx[] = {-1,0,1,0} ;
int dy[] = {0,1,0,-1} ;
int table[23][23] ;

void DFS( int x , int y , int cnt ){
	if( cnt+1 >= ans ) return ;

	for( int i=0 ; i<4 ; i++ ){
		int nx = x + dx[i] , ny = y + dy[i] ;

		if( table[ny][nx] == 1 ){ continue ; }
		else { while( table[ny][nx] != 4 ){

			if( table[ny][nx] == 1 ){
				table[ny][nx] = 0 ;
				DFS( nx-dx[i] , ny-dy[i] , cnt+1 ) ;
				table[ny][nx] = 1 ;
				break ;
			}
			else if( table[ny][nx] == 3 ){
				ans = min( ans , cnt+1 ) ;
				return ;
			}

			nx += dx[i] ;
			ny += dy[i] ;
		}}
	}

	return ;
}

int main(){

	while( cin >> W >> H , W ){
		for( int i=0 ; i<=H+1 ; i++ ){
			for( int j=0 ; j<=W+1 ; j++ ){
				if( i==0 || j==0 || i==H+1 || j==W+1 ) table[i][j] = 4 ;
				else{
					cin >> table[i][j] ;
					if( table[i][j] == 2 ){
						sx = j ;
						sy = i ;
					}
				}
			}
		}

		ans = 11 ;
		DFS( sx , sy , 0 ) ;
		if( ans == 11 ) ans = -1 ;
		cout << ans << endl ;
	}

return 0;
}

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

int a[5];

bool isStraight(){
	vector<int> n;
	for(int i=0 ; i<5 ; i++){
		n.push_back(a[i]);
	}
	sort( n.begin(), n.end() );
	if( n[0]+1 == n[1] && n[1]+1 == n[2] && n[2]+1 == n[3] && n[3]+1 == n[4]){
		return true;
	}
	if( n[0]==1 && n[1]==10 && n[2]==11 && n[3]==12 && n[4]==13){
		return true;
	}
	return false;
}

string checkCard(){
	int n, count, c[5];
	int c2=0, c3=0, c4=0;

	for(int j=0 ; j<5 ; j++){
		count = 0;
		n = a[j];
		for(int i=0 ; i<5 ; i++){
			if(a[i]==n)count++;
		}
		c[j] = count;
	}
	for(int i=0 ; i<5 ; i++){
		if(c[i]==2)c2++;
		if(c[i]==3)c3++;
		if(c[i]==4)c4++;
	}
	if(c4==4) return "four card";
	else if(c3==3 && c2==2) return "full house";
	else if( isStraight() ) return "straight";
	else if(c3==3) return "three card";
	else if(c2==4) return "two pair";
	else if(c2==2) return "one pair";
	else return "null";
}

int main(){
	string str;
	char s[80];
	while( scanf("%d,%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3], &a[4] ) != EOF ){
		str = checkCard();
		printf("%s\n",str.c_str());
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

int main(){
    int d;
    while(cin){
        cin >> d;
        if(!cin) break;
        int itg=0;
        for(int i=1;i*d<600;i++) itg+=pow(i*d,2)*d;
        cout << itg << endl;
    }
}

#include <iostream>

using namespace std;

int main()
{
	int d;

	while (cin >> d)
	{
		cout << 72000000 - 180000 * d + 100 * d * d << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cerr << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cerr<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; //use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////

ll fac[MAX_N], inv[MAX_N], fiv[MAX_N]; //fiv:inv(fac(i))

ll mod_pow(ll a, ll n) {
	if(n == 0) return 1;
	ll res = mod_pow(a, n / 2);
	if(n % 2 == 0) return res * res % mod;
	else return a * res % mod * res % mod;
}

ll invf(ll a) {
	return mod_pow(a, mod - 2);
}

void C_init(int n) {
	fac[0] = fac[1] = 1; inv[1] = 1;
	fiv[0] = fiv[1] = 1;
	rep(i, 2, n + 1) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		fiv[i] = fiv[i - 1] * inv[i] % mod;
	}
}

ll C(int a, int b) { //assume a >= b
	if(a < b || a < 0 || b < 0) return 0;
	return fac[a] * fiv[b] % mod * fiv[a - b] % mod;
}

struct Ruiseki {
	int N;
	vector<ll> B;
	void init(int n) {
		N = n; B = vector<ll>(N + 1, 0);
	}
	Ruiseki() {}
	Ruiseki(int n) { init(n); }
	void add(int a, ll v) {
		if(a < 0 || N <= a) return;
		B[a + 1] += v;
	}
	void sweep() {
		rep(i, 1, N + 1) B[i] += B[i - 1];
	}
	ll get(int a, int b) { //[a, b)
		if(a >= b) return 0;
		a = max(a, 0); b = min(b, N);
		return B[b] - B[a];
	}
};

int N;
vector<int> G[1 << 20];
int A[MAX_N];
Ruiseki rui;

void solve() {
	cin >> N;
	ll ans = 0;
	C_init(N);
	rui.init(N);
	rep(i, 0, N) cin >> A[i];
	int sum = 0, cnt = 1;
	rep(i, 0, N) {
		sum ^= A[i];
		if(sum == 0) {
			rui.add(i, 1);
			// debug(i);
			cnt++;
		}
		else G[sum].pb(i);
	}
	rui.sweep();
	// debug(sum, cnt);
	if(sum == 0) {
		ADD(ans, mod_pow(2, cnt - 2));
	}
	rep(i, 1, (1 << 20)) {
		if(sz(G[i]) == 0) continue;
		ll suma = 0, sumz = 1;
		// debug(G[i]);
		rep(j, 0, sz(G[i])) {
			// debug(i, j, suma, sumz);
			ll v = sumz;
			ADD(suma, v);
			if(j != sz(G[i]) - 1) {
				ll c = rui.get(G[i][j], G[i][j + 1]);
				ADD(sumz, suma * c % mod);
				// debug(G[i][j], G[i][j + 1], c);
			}
		}
		if(sum == 0) {
			ADD(ans, suma);
		}
		else if(sum == i) {
			ADD(ans, sumz);
		}
	}
	cout << ans << "\n";
}

uint32_t rd() {
	uint32_t res;
#ifdef __MINGW32__
	asm volatile("rdrand %0" :"=a"(res) ::"cc");
#else
	res = std::random_device()();
#endif
	return res;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
    cerr << fixed;
	cerr.precision(6);
	rng.seed(rd());
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
    freopen("in.txt", "rt", stdin);
#endif
	solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}

#include<bits/stdc++.h>

int main(){
    using namespace std;
    constexpr unsigned long MOD = 1000000007;
    vector<unsigned long> tapi(1048576, 1), tapu(1048576), tape(1048576);
    unsigned long ans{0}, t{500000004}, a{0}, z{0}, A;cin >> A;
    while(cin >> A)if(a ^= A){
        (tapu[a] += (tapi[a] += tapu[a] * (z - tape[a])) %= MOD) %= MOD;
        ans += tapi[a];
        tape[a] = z;
    }else ++z, (t *= 2) %= MOD;
    cout << (a ? tapi[a] : (ans + t) % MOD) << endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
// #define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
#include <iomanip>    // 10????????? cout << setprecision(10) << double;
#include <queue>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef map<int, int> MAPII;
typedef multimap<int, char, greater<int> > MuMAPIC;
typedef vector<pair<int, int> > VPII;
typedef multimap<int, string, greater<int> > MuMIS;
typedef pair<int, int> P;
typedef pair<int, pair<P, P> > PP;

#define MP make_pair
#define FAST_IO  cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (???test)
// #define FOREACH_IT(it,c) for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(),c_end=c.end();it!=c_end;++it)
// for Debug.
#define DUMP_VVI(b) FOR(i,0,b.size()){FOR(j,0,b[i].size())printf("%d ",b[i][j]);puts("");}
#define D_OUT(str,value) if(dbgF){cout<<str<<" : "<<value<<endl;}
// ??\??????push_back(d)???array[d]???????????????1??????§?????????
// int INPUT_INT() {int d;cin>>d;return d;}
template<class T>T IN() { T d; cin >> d; return d; }
// ??????§????¢??´????°???Greatest Common Divisor???
LL gcd(LL a, LL b) { return (b > 0) ? gcd(b, a%b) : a; }
// ?????°?????¢??????°???Least Common Multiple???
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
// Y???´???????????????´?????????
bool uruu(LL Y) { return (((Y % 4 == 0 && Y % 100 != 0) || Y % 400 == 0) ? true : false); }

// vector???¨???
// vec[i][j]?????????????\????????\?????????¨??????vec???????????????????????????????????????.

#define INF 1000000

// ????????????????°???????????????§???

// ------------------- include, typedef, define END. -------------------

int main() {
	int n = 0;

	while (cin >> n, n) {
		VI k(n + 1, 0);
		string s;
		FOR(i, 0, n) {
			k[i] = IN<int>();
		}
		cin >> s;

		int j = -1;
		FOR(i, 0, s.length()) {
			j++;
			if (j >= n) {
				j = 0;
			}

			FOR(a, 0, k[j]) {
				s[i]--;
				if (s[i] == 96)s[i] = 90;
				if (s[i] == 64)s[i] = 122;
			}

			cout << s[i];
		}
		cout << endl;

	}

	return 0;
}

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

typedef ostringstream OSS; typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;  typedef pair<LL, LL> PLL;
typedef vector<int>    VI;   typedef vector<VI>   VVI;   typedef vector<VVI>   VVVI;
typedef vector<LL>     VLL;  typedef vector<VLL>  VVLL;  typedef vector<VVLL>  VVVLL;
typedef vector<double> VD;   typedef vector<VD>   VVD;   typedef vector<VVD>   VVVD;
typedef vector<string> VS;   typedef vector<VS>   VVS;   typedef vector<VVS>   VVVS;
typedef vector<bool>   VB;   typedef vector<VB>   VVB;   typedef vector<VVB>   VVVB;
typedef vector<PII>    VPII; typedef vector<VPII> VVPII; typedef vector<VVPII> VVVPII;
typedef vector<PLL>    VPLL; typedef vector<VPLL> VVPLL; typedef vector<VVPLL> VVVPLL;

typedef unsigned int  UI;
typedef vector<UI>    VUI;
typedef vector<VUI>   VVUI;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x)  (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a)   { OSS oss; oss << a; return oss.str(); };

template<typename T=int> inline void dump(vector<T> vs, bool ent=false) { rep(i, vs.size()) cout << vs[i] << (i+1==vs.size() ? '\n' : ' '); if (ent) cout << endl; }
template<typename T = int> inline void dump(vector<vector<T>> vs, bool ent = false) { rep(i, vs.size()) dump<T>(vs[i]); if (ent) cout << endl; }

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int    DX[] = {1,  0, -1, 0};
const int    DY[] = {0, -1,  0, 1};
const double EPS  = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

string S;

string solve(int N) {
    VI ks(N);
    rep(i, N) cin >> ks[i];
    string s;
    cin >> s;

    string ans(s);

    int M = S.size();

    rep(i, s.size()) {
        int p = i % (int)ks.size();
        int pos = find(ALL(S), s[i]) - S.begin();
        int pos2 = (pos - ks[p] + M) % M;
        ans[i] = S[pos2];
    }

    return ans;
}

int main(void) {
    rep(i, 26) S += 'a' + i;
    rep(i, 26) S += 'A' + i;

    int N;
    while (cin >> N, N) cout << solve(N) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
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 main()
{
    int n,k;
    cin >> n >> k;
    vector<int> a(n);
    REP(i,n){
        cin >> a[i];;
    }
    REP(i,k){
        vector<int> b(n, 0);
        REP(i, n)
        {
            int l = max(0LL, i - a[i]);
            int r = min(n - 1, int(i + a[i]));
            b[l]++;
            if (r + 1 < n)
            {
                b[r + 1]--;
            }
        }
        a[0]=b[0];
        int ndesu=0;
        if(a[0]==n){
            ndesu++;
        }
        REP(j, n)
        {
            if (j == 0)
                continue;
            b[j] += b[j - 1];
            a[j]=b[j];
            if(a[j]==n){
                ndesu++;
            }
        }
        if(ndesu==n){
            REP(j,n){
                cout << n << " ";
            }
            cout << endl;
            return 0;
        }
    }
    REP(i,n){
        cout << a[i] << " ";
    }
    cout << endl;
}

#include <cstdio>
#include <algorithm>

using i8 = std::int8_t;
using u8 = std::uint8_t;
using i16 = std::int16_t;
using u16 = std::uint16_t;
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using usize = std::size_t;

i32 a[200000];

auto main() -> i32 {
	i32 n, k;
	scanf("%d%d", &n, &k);
	for (i32 i = 0; i < n; i += 1) {
		scanf("%d", a + i);
	}
	i32 b[200001];
	for (i32 step = 0; step < std::min(64, k); step += 1) {
		std::fill(b, b + n + 1, 0);
		for (i32 i = 0; i < n; i += 1) {
			b[std::max(0, i - a[i])] += 1;
			b[std::min(n, i + a[i] + 1)] -= 1;
		}
		a[0] = b[0];
		for (i32 i = 1; i < n; i += 1) {
			b[i] += b[i - 1];
			a[i] = b[i];
		}
	}
	for (i32 i = 0; i < n; i += 1) {
		printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');
	}
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define reps(i,a,b) for(int i= a; i < b; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main()
{
  int n;
  string s;
  cin >> n >> s;
  ll r = 0, b = 0, g = 0;
  rep(i, n) {
    if (s.at(i) == 'R') {
      r++;
      continue;
    }
    if (s.at(i) == 'G') {
      g++;
      continue;
    }
    if (s.at(i) == 'B') {
      b++;
      continue;
    }
  }
  ll ans = r * b * g;
  if (!ans) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n) {
    reps(j, i+1, n) {
      if (s.at(i) == s.at(j)) continue;

      int k = 2*j - i;
      if (k >= n) continue;
      if ((s.at(i) != s.at(j)) && (s.at(i) != s.at(k)) && (s.at(k) != s.at(j))) {
        ans--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> void Print(vector<T> v) {
  F0R(i, v.size()) {
    cout << v[i] << ' ';
  }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, L;
  cin >> N >> L;

  int l = L, r = L + N - 1;
  int sum = (l+r) * N / 2;
  if (l > 0) {
    sum -= l;
  }
  else if (r < 0) {
    sum -= r;
  }

  cout << sum;
}
#endif


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int N_MAX=100001;
long long INF = 123456789123456789;
typedef pair<long long,int> P;
void dijkstra(int s, int V, vector<P>* g, long long* d){
  if(d[s]==0)
    return;
  int i,v;
  P p,e;
  priority_queue<P, vector<P>, greater<P>> que; // 最短距離，頂点番号
  fill(d,d+V,INF);
  d[s]=0;
  que.push(P(0,s));
  while(!que.empty()){
    p=que.top(); que.pop();
    v=p.second;
    if(d[v]<p.first) continue;
    for(i=0;i<g[v].size();i++){
      e=g[v][i];
      if(d[e.second]>d[v]+e.first){
        d[e.second]=d[v]+e.first;
        que.push(P(d[e.second],e.second));
      }
    }
  }
  return;
}
int main()
{
  int n,q,k,x,y,a,b,i;
  long long c;
  long long d[N_MAX];
  vector<P> g[N_MAX];
  cin >> n;
  fill(d+1,d+n+1,INF);
  for(i=0;i<n-1;i++){
    cin >> a >> b >> c;
    g[a].emplace_back(c,b);
    g[b].emplace_back(c,a);
  }
  cin >> q >> k;
  dijkstra(k,n,g,d);
  for(i=0;i<q;i++){
    cin >> x >> y;
    cout << d[x]+d[y] << endl;
  }
  return 0;
}

#include "bits/stdc++.h"

using namespace std;

int dp(int curr, int T, const vector<int>& A, const vector<int>& B, vector<vector<int>>& memo) {
    int N = A.size();
    if (curr == N) {
        return 0;
    }
    if (T <= 0) {
        return 0;
    }
    if (memo[curr][T] >= 0) {
        return memo[curr][T];
    }

    int skip = dp(curr + 1, T, A, B, memo);
    int take = dp(curr + 1, T - A[curr], A, B, memo) + B[curr];
    int ret = max(skip, take);
    memo[curr][T] = ret;
    return ret;
}

void Main() {
    int N, T;
    cin >> N >> T;
    set<pair<int, pair<int, int>>> prep;
    for (int i = 0; i < N; ++i) {
        int a, b;
        cin >> a >> b;
        prep.insert(make_pair(a, make_pair(i, b)));
    }
    vector<int> A(N, 0), B(N, 0);
    int cnt = 0;
    for (auto e : prep) {
        A[cnt] = e.first;
        B[cnt] = e.second.second;
        ++cnt;
    }

    vector<vector<int>> memo(N + 10, vector<int>(T + 10, -1));
    int ans = dp(0, T, A, B, memo);
    cout << ans << endl;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ar array

int a,b;

void solve(){

    cin >> a >> b;

    cout << a*b << endl;
}

int main(){

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

    solve();

}

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

int main()
{
    int A,B,i,ans=0,p,q,r,s;
    cin >> A >> B;
    for(i=A;i<=B;i++){
        p=i%10;
        q=(i/10)%10;
        r=(i/1000)%10;
        s=(i/10000)%10;
        if(p==s&&q==r){
            ans++;
        }
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define debug(x) cerr<<#x<<'='<<x<<endl
#define set0(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
template<typename T> void read(T &x){
	x = 0;char ch = getchar();ll f = 1;
	while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
	while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}

char str[305];
int dp[305][305][305];
void upd(int &a,int b){
	a =max(a,b);
}
int main() {
	cin>>(str+1);
	int n = strlen(str+1);
	int lim;
	cin>>lim;
	int ans = 0;
	for(int i=1;i<=n;i++){
		for(int j=n;j>=1;j--){
			for(int k=0;k<=lim;k++){
				upd(dp[i+1][j][k],dp[i][j][k]);
				upd(dp[i][j-1][k],dp[i][j][k]);
				if(str[i] == str[j]){
					upd(dp[i+1][j-1][k],dp[i][j][k]+1);
				}else{
					upd(dp[i+1][j-1][k+1],dp[i][j][k]+1);
				}
			}
		}
	}
	for(int i=0;i<=n+1;i++){
		for(int j=n+1;j>=0;j--){
			for(int k=0;k<=lim;k++){
				if(i == j+1)ans = max(ans,dp[i][j][k]*2);
				if(i-1 == j+1)ans = max(ans,dp[i][j][k]*2-1);
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


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

using namespace std;

int n;
int a[50001];
int S[50001];

int main(){

while(cin >> n && n > 0){

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

  S[0] = 0;
  for (int i = 0; i < n ; ++i)
  {
    S[i + 1] = S[i] + a[i];

  }

int max = -100000;
  for (int i = 0; i < n + 1; ++i)
  {
    for (int k = 0; k < i ; ++k)
    {
      if(S[i] - S[k] > max) max = S[i] - S[k];
    }
  }

  cout << max << endl;

  }
}

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

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_in(int c){int n=0;bool m=false;if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0');return m?-n:n;} //&&c<='9'
_in() {return in(gcu());}
_sc(int &n){int c=gcu();return c==EOF?false:(n=in(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
//_sc(string &s){int c;s="";
//	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
template <typename H,typename... T> _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
template <typename T>
_vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
//_vo(string &s){for (char c: s) pcu(c);}
template <typename H,typename... T> _vo(H&& h, T&&... t){out(h);out(move(t)...);}
//template <typename T> _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
_vl(){out('\n');}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

template<int N>
struct sieve {
	bool s[N];
	constexpr sieve() : s() {
		for (int i = 0; i < N; i++)
			s[i] = true;
		for (int i = 4; i < N; i += 2)
			s[i] = false;
		for (int i = 9; i < N; i += 6)
			s[i] = false;
		for (int i = 6, e = sqrt(N); i <= e; i += 6) {
			for (int j : {i - 1, i + 1})
				if (s[j])
					for (int k = j * j; k < N; k += j)
						s[k] = false;
		}
	}
};

int main() {
	enum {N = 10001};
	constexpr sieve<N> s;
	vector<int> t = {5};
	for (int i = 7; i < N; i += 6)
		if (s.s[i - 2] && s.s[i])
			t.push_back(i);
	for (int n; (n = in());) {
		auto r = upper_bound(t.begin(), t.end(), n) - 1;
		outl(*r - 2, ' ', *r);
	}
}


#include<iostream>
using namespace std;
int main(){
  int tuki[13]={0,0,31,60,91,121,152,182,213,244,274,305,335};
  string cal[400];
  cal[1]="Thursday";cal[2]="Friday";cal[3]="Saturday";cal[4]="Sunday";
  cal[5]="Monday";cal[6]="Tuesday";cal[7]="Wednesday";
  for(int i=1;i<=366;i++)
    cal[i+7]=cal[i];
  int a,b;
  while(cin>>a>>b,a+b)
    cout<<cal[tuki[a]+b]<<endl;
  return 0;
}

#include<stdio.h>
int main()
{
int p[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int M,D,sum;
while(1)
{
sum=0;
scanf("%d",&M);
scanf("%d",&D);if(M==0)break;
if(M==0)break;
for(int i=0;i<M-1;i++)
sum+=p[i];

sum+=D;

switch(sum%7)
{
case 4:
printf("Sunday\n");break;
case 5:
printf("Monday\n");break;

case 6:
printf("Tuesday\n");break;

case 0:
printf("Wednesday\n");break;

case 1:
printf("Thursday\n");break;

case 2:
printf("Friday\n");break;

case 3:
printf("Saturday\n");break;
}
}
return 0;
}


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

using pp=pair<int,int>;

int main() {
  string S;
  cin >> S;
  string ans="AC";
  if (S[0]!='A') {ans="WA";}
  int c=0, ci;
  for (int i=2; i<S.size()-1; i++) {
    if (S[i]=='C') {
      c++;
      ci=i;
    }
  }
  if (c!=1) {ans="WA";}
  for (int i=0; i<S.size(); i++) {
    if (i==0 || i==ci) {continue;}
    if (S[i]-'Z'<=0) {
      ans="WA";
      break;
    }
  }
  cout << ans << endl;
}

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

const int INF = 1e9;

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

	int count = 0;
	map<int, int> mp;
	for (int i = 0; i < 3; i++) {
		int a;
		cin >> a;
		if (mp[a])
			count++;
		mp[a] = 1;
	}
	if (count == 1)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}


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

using ll = long long;

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

    ll n,a,b,c,d,e;cin>>n>>a>>b>>c>>d>>e;

    ll s=min(min(min(min(a,b),c),d),e);

    ll ans=(n-1)/s+5;

    cout<<ans<<"\n";

    return 0;
}

#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 <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ld n, m, d; cin >> n >> m >> d;
    printf("%.16Lf\n", (d == 0 ? 1.0 : 2.0) * (n - d) * (m - 1.0) / (n * n));
    return 0;
}