#include <bits/stdc++.h>
using namespace std;
int main(void){

    /*
    int a, b;
    cin >> a >> b ;
    //*/

    //*
    vector<int> vec(3);
    cin >> vec.at(0) >> vec.at(1) >> vec.at(2) ;

    sort(vec.begin(), vec.end());
    //*/

    /*
    if( a < 6 ){
        cout << 0 << endl;
    } else if (a < 13){
        cout << b / 2 << endl;
    } else {
        cout << b << endl;
    }
    //*/

    cout << vec.at(0) + vec.at(1) << endl;

    return 0;
}

#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;

#define rep(a,n) for(ll a = 0;a < n;a++)

static const ll INF = 1e15;
static const ll mod = 1e9+7;

template<typename T>
static inline void chmin(T& ref, const T  value) {
    if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T& ref, const T value) {
    if (ref < value) ref = value;
}

struct MaxFlowCalculator {
	int max_flow(int s, int t, const vector<vector<pair<int, int>>>& g) {
		struct edge_ { int to, cap, rev; };
		vector<bool> used(g.size(), false);
		vector<vector<edge_>> G(g.size());
		for (int i = 0; i < g.size(); i++) for (int j = 0; j < g[i].size(); j++)
		{
			int from = i, to = g[i][j].second;
			int cap = g[i][j].first;
			G[from].push_back({ to, cap, (int)G[to].size() });
			G[to].push_back({ from, 0, (int)G[from].size() - 1 });
		}
		auto dfs = [&](auto&& f, int v, int t, int fl)->int {
			if (v == t) return fl;
			used[v] = true;
			rep(i, G[v].size()) {
				edge_& e = G[v][i];
					if (!used[e.to] && e.cap > 0) {
					int d = f(f, e.to, t, min(fl, e.cap));
					if (d > 0) {
						e.cap -= d;
						G[e.to][e.rev].cap += d;
						return d;
					}
				}
			}
			return 0;
		};
		int flow = 0;
		while (1) {
			used.assign(used.size(), false);
			int f = dfs(dfs, s, t, INT_MAX);
			if (f == 0) return flow;
			flow += f;
		}
	}
};

int main() {
	ll p, r, q;
	cin >> p >> r >> q;
	ll a1 = p + r;
	ll a2 = r + q;
	ll a3 = p + q;
	cout << min(a1, min(a2, a3)) << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
signed main(){
    ll n;cin>>n;
    vector<ll>x(n);
    rep(i,n)cin>>x[i];
    ll l,q;cin>>l>>q;
    vector<ll>nxt(n);
    vector<pll>b(0);
    for(int i=n-1;i>=0;i--){
        if(i==n-1){
            b.pb(mp(-x[i],i));
            nxt[i]=i;
            continue;
        }
        pll k=*lower_bound(all(b),mp(-x[i]-l,-1LL));
        nxt[i]=k.second;
        b.pb(mp(-x[i],i));
    }
    vector<vector<ll> >dp(n,vector<ll>(32));
    rep(i,n)dp[i][0]=nxt[i];
    REP(i,1,32){
        rep(j,n){
            dp[j][i]=dp[dp[j][i-1]][i-1];
        }
    }
    rep(i,32){
        //cout<<dp[0][i]<<endl;
    }
    while(q){
        q--;
        ll a,b;cin>>a>>b;
        a--;b--;
        if(a>b){
            swap(a,b);
        }
        ll l=0,r=n;
        while(r-l>1){
            ll mid=(r+l)/2;
            ll x=a;
            rep(j,32){
                if(mid&(1LL<<j)){
                    x=dp[x][j];
                }
            }
            if(q==3){
                //cout<<mid<<' '<<a<<' '<<b<<endl;
            }
            if(x>=b)r=mid;
            else l=mid;
        }
        cout<<r<<endl;
    }
}

#include <bits/stdc++.h>
#define pb          emplace_back
#define ll          long long
#define fi          first
#define se          second
#define mp          make_pair

using namespace std;

const int N = int(3e5) + 7;
const int logN = 17;
const int mod = int(1e9) + 7;
typedef pair<int, int> pii;

int p[N][logN + 1], x[N], n, l, q, a, b;

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define Task        "test"
    if(fopen(Task".inp", "r")) {
        freopen(Task".inp", "r", stdin);
        freopen(Task".out", "w", stdout);
    }
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> x[i];
    cin >> l;
    for(int i = 1, j = 1; i <= n; ++i) {
       while(j <= n && x[j] - x[i] <= l) ++j;
       p[i][0] = j - 1;
    }
    for(int j = 1; j <= logN; ++j) {
       for(int i = 1; i <= n; ++i)
          p[i][j] = p[p[i][j - 1]][j - 1];
    }
    cin >> q;
    while(q --) {
        cin >> a >> b; int res = 1;
        if(a > b) swap(a, b);
        for(int j = logN; j >= 0; --j)
            if(p[a][j] < b) res += (1 << j), a = p[a][j];
        cout << res << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef pair<int, int> Pint;
typedef pair<int64_t, int64_t> Pll;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, s, n) for (int i = (s); i < (n); i++)
#ifdef LOCAL
#define INPUT_FILE                              \
  ifstream in("atcoder-problems/abc112_d.txt"); \
  cin.rdbuf(in.rdbuf());
#define print_vec(v)                        \
  rep(l, v.size()) { cout << v[l] << " "; } \
  cout << endl;
#else
#define INPUT_FILE
#define print_vec(v)
#endif
#define CIN_OPTIMIZE \
  cin.tie(0);        \
  ios::sync_with_stdio(false);
typedef pair<int, int> P;
typedef long long ll;
typedef pair<ll, ll> pl;
const int INF = 100100100;
const ll LINF = 1e18 + 100;
const int MOD = 1e9 + 7;

int main() {
  INPUT_FILE CIN_OPTIMIZE;

  ll N, M;
  cin >> N >> M;

  ll ans = 1;
  for (ll i = 1; i * i <= M; i++) {
    if (M % i == 0) {
      if (M / i >= N) ans = max(ans, i);
      if (i >= N) ans = max(ans, M / i);
    }
  }

  cout << ans << endl;
}


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

template<typename T, typename E, typename F, typename G, typename H>
class LazySegmentTree {
  int n, height;
  F f; G g; H h; const T ti; const E ei;
  vector<T> dat; vector<E> laz; vector<int> highest;

  void propagate(int k) {
    if(laz[k] != ei) {
      laz[2*k] = h(laz[2*k],laz[k]); laz[2*k+1] = h(laz[2*k+1],laz[k]);
      dat[k] = g(dat[k],laz[k],n/highest[k]);
      laz[k] = ei;
    }
  }
  inline void recalc(int k) {
    while(k >>= 1) dat[k] = f(g(dat[2*k],laz[2*k],n/highest[2*k]),g(dat[2*k+1],laz[2*k+1],n/highest[2*k+1]));
  }
  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }
public:
  LazySegmentTree(int _n, F f, G g, H h, T ti, E ei): f(f), g(g), h(h), ti(ti), ei(ei) {
    n = 1; height = 0;
    while(n < _n) n <<= 1, height++;
    dat.assign(2*n,ti); laz.assign(2*n,ei); highest.assign(2*n,0);
    int c = 1;
    for(int i = 1; i < 2*n; i++) {
      if(c*2 <= i) c <<= 1;
      highest[i] = c;
    }
  }
  void set(int k, T x) {
    dat[k+n] = x;
  }
  void build() {
    for(int i = n-1; i >= 0; i--) {
      dat[i] = f(dat[2*i],dat[2*i+1]);
    }
  }
  void update(int a, int b, const E x) {
    thrust(a += n); thrust(b += n-1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) laz[l] = h(laz[l],x), ++l;
      if(r & 1) --r, laz[r] = h(laz[r],x);
    }
    recalc(a); recalc(b);
  }
  T query(int a, int b) {
    thrust(a += n); thrust(b += n-1);
    T l = ti, r = ti; b++;
    while(a < b) {
      if(a & 1) l = f(l,g(dat[a],laz[a],n/highest[a])), a++;
      if(b & 1) b--, r = f(g(dat[b],laz[b],n/highest[b]),r);
      a >>= 1; b >>= 1;
    }
    return f(l,r);
  }
};

int main(){
  int n, q;
  cin >> n >> q;
  auto f = [](ll a, ll b){return a+b;};
  auto g = [](ll a, ll b, int l){return b == INF ? a : b*l;};
  auto h = [&](ll a, ll b) {return b;};
  LazySegmentTree<ll,ll,decltype(f),decltype(g),decltype(h)> lst(n,f,g,h,0,INF);
  rep(i,n) lst.set(i,((ll)1<<31)-1);
  lst.build();
  vector<ll> ans;
  rep(i,q) {
    int t;
    cin >> t;
    if(t == 0) {
      int l, r; ll x;
      cin >> l >> r >> x;
      lst.update(l,r+1,x);
    } else {
      int p;
      cin >> p;
      ans.push_back(lst.query(p,p+1));
    }
  }
  for(auto x : ans) cout << x << endl;
  return 0;
}

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

int n, q, s, t, x, u;
iP a[1<<18];
bool b;

int update(int s, int t, int x, int k, int l, int r, int i) {
	if (t < l || r < s)return 0;
	else if (s <= l&&r <= t) {
		a[k] = mkp(x, i);
		return 0;
	}
	else {
		update(s, t, x, k * 2, l, (l + r) / 2, i);
		update(s, t, x, k * 2 + 1, (l + r) / 2 + 1, r, i);
		return 0;
	}
}
int find(int x) {
	int y, z = -2;
	x += u;
	while (x) {
		if (z < a[x].second) {
			y = a[x].first;
			z = a[x].second;
		}
		x /= 2;
	}
	return y;
}

int main(){
	cin >> n >> q;
	for (int i = 1;; ++i) {
		if ((1 << i) / n >= 2) {
			u = (1 << i - 1);
			break;
		}
	}
	for (int i = 1; i != u * 2; ++i)
		a[i] = mkp(intmax, -1);
	for (int i = 0; i != q; ++i) {
		cin >> b >> s;
		if (b) {
			cout << find(s) << endl;
		}
		else {
			cin >> t >> x;
			update(s, t, x, 1, 0, u - 1, i);
		}
	}
	return 0;
}


#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cstring>
#include<map>

using namespace std;
#define int long long int
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define pb push_back
#define mod 1000000007
//#define mod 998244353

int D,G;
int p[15],c[15];
int dp[15][1500];
signed main(){
    scanf("%lld %lld",&D,&G);
    int maxp=0;
    rep(i,D){
        scanf("%lld %lld",&p[i],&c[i]);
        maxp=max(maxp,p[i]);
    }
    rep(i,D+1)rep(j,D*maxp+1)dp[i][j]=-INF;
    rep(i,D){
        rep(j,D*maxp+1){
            if(i==0&&j==0)dp[i][j]=0;
            rep(p_num,p[i]+1){
                if(p_num!=p[i])dp[i+1][j+p_num]=max(dp[i+1][j+p_num],dp[i][j]+p_num*(i+1)*100);
                else dp[i+1][j+p_num]=max(dp[i+1][j+p_num],dp[i][j]+p_num*(i+1)*100+c[i]);
            }
        }
    }
    int ans=-1;
    rep(j,D*maxp+1){
        //printf("%lld/",dp[D][j]);
        if(dp[D][j]>=G){ans=j;break;}
    }
    printf("%lld\n",ans);
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Graph=vector<vector<int>>;
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
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;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main() {
  int H,W;
  cin>>H>>W;

  char A[H+2][W+2];
  int DIS[H+2][W+2];
  queue<pair<int,int>> Q;
  for(int i=1; i<=H; ++i){
    for(int j=1; j<=W; ++j){
      cin>>A[i][j];
      DIS[i][j]=MAX;
      if(A[i][j]=='#'){
        Q.emplace(i,j);
        DIS[i][j]=0;
      }
    }
  }

  while(!Q.empty()){
    auto P=Q.front();
    Q.pop();
    REP(i,4){
      int ny=P.first+dy[i],nx=P.second+dx[i];
      if(nx<=0||nx>=W+1||ny<=0||ny>=H+1) continue;
      if(DIS[ny][nx]<MAX) continue;
      DIS[ny][nx]=DIS[P.first][P.second]+1;
      Q.emplace(ny,nx);
    }
  }

  int ans=0;
  for(int i=1; i<=H; ++i){
    for(int j=1; j<=W; ++j){
      chmax(ans,DIS[i][j]);
    }
  }

  co(ans);
  return 0;
}


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

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

  for (int i = 0; i < K*2-1; i++) {
    if (i == K*2-2){
      cout << X - K + 1 + i << endl;
    }
    else {
      cout << X - K + 1 + i << ' ';
    }
  }
}

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

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

struct edge {int to; ll cost; };

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

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


#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n;i>=0;i--)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;
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; }
//const ll INF = 1LL << 60;
//const int INF = 1001001001;

map< int64_t, int > prime_factor(int64_t n) {
  map< int64_t, int > ret;
  for(int64_t i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}

int main(void){
    ll a,b;
    cin >> a >> b;
    auto v=prime_factor(gcd(a,b));
    cout << v.size()+1 << endl;

    return 0;
}

#pragma region header
#include <bits/stdc++.h>
#define int long long
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define mt make_tuple
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1) (__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1) (__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
using namespace std;

using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
template <class T, class U>
using umap = unordered_map<T, U>;
using umii = umap<int, int>;
using seti = set<int>;
template <class T>
using uset = unordered_set<T>;
using useti = uset<int>;
template <class T>
using less_queue = priority_queue<T>;
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = 1e18;

template <class T>
void SORT(T &a) { stable_sort(all(a)); }
template <class T>
void RSORT(T &a) { stable_sort(rall(a)); }
template <class T>
void rev(T &a) { reverse(rall(a)); }
template <class T>
void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T>
auto min_of(T a) { return *min_element(all(a)); }
template <class T>
auto max_of(T a) { return *max_element(all(a)); }
template <class T>
int sum_of(T a) { return accumulate(all(a), 0ll); }
template <class T, class U>
auto sum_of(T a, U init) { return accumulate(all(a), init); }
template <class T, class U>
int count_of(T a, U i) { return count(all(a), i); }
template <class T, class U>
bool has(T a, U i) { return find(all(a), i) != end(a); }
template <class T>
int sz(T a) { return a.size(); };

template <class T>
void COUT(T x) { cout << x << endl; }
template <class T, class U>
void COUT(T x, U y) { cout << x << ' ' << y << endl; }
template <class T, class U, class V>
void COUT(T x, U y, V z) { cout << x << ' ' << y << ' ' << z << endl; }
template <class T>
void CSP(T x) { cout << x << ' '; }
template <class T>
void CVEC(T v) {
  int c = v.size() - 1;
  for (size_t i = 0; i < c; i++) cout << v[i] << ' ';
  if (c > -1) cout << v[c];
  cout << endl;
}

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

int ceil_div(int x, int y) { return (x - 1) / y + 1; }

#pragma endregion header

// no MOD

void solve(int N, int x, vi a) {
  int ans = 0;
  each(c, a) if(c > x) {
    ans += c - x;
    c = x;
  }
  rep(i, N - 1) {
    if(a[i] + a[i + 1] > x) {
      ans += a[i] + a[i + 1] - x;
      a[i + 1] = x - a[i];
    }
  }
  COUT(ans);
}

#pragma region main
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(15);

  int N;
  cin >> N;
  int x;
  cin >> x;
  vi a(N);
  for(int i = 0 ; i < N ; i++){
    cin >> a[i];
  }
  solve(N, x, move(a));
}
#pragma endregion main


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

int main(){
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  map<int, int> mp;
  mp[0] = 1;
  int ms = 0;
  rep(i, n){
    ms += a.at(i);
    ms %= m;
    mp[ms]++;
  }
  ll ans = 0;
  for(auto p : mp){
    ans += (ll)p.second * (p.second-1)/2;
  }
  cout << ans << endl;
  return 0;
}

//2020年4月17日現在

#include<stdio.h>
#include<cmath>
#include<math.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<complex>
#include<queue>
#include<set>
#include<utility>
#include<iomanip>
#include<limits>
#include<thread>
#include<map>
#include<stack>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<tuple>
#include <cstdio>
#include <iterator>

using namespace std;

template <class... A>
void prints() { cout << "\n"; }
template <class... A>
void prints_rest() { cout << "\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &... rest) { cout << " " << first; prints_rest(rest...); }
template <class T, class... A>
void prints(const T &first, const A &... rest) { cout << first; prints_rest(rest...); }

namespace {
    // printElems(&a[0], &a[n]);　みたいな感じで使う。
    void printElems(const int* begin, const int* end)
    {
        for (const int* p = begin; p != end; ++p) {
            std::cout << *p << ' ';
        }
        std::cout << std::endl;
    }
}

#define rep(i,n) for(int i=0; i<n; i++)
#define rep1(i,n) for(int i=1; i<n+1; i++)
#define repr(i,n) for(int i=n-1; i>=0; i--)
#define repr1(i,n) for(int i=n; i>0; i--)
#define FOR(i,j,n) for(int i=j; i < n; i++)
#define souatari(i,j,n) rep1(i,n-1)rep(j,i)
#define souatari1(i,j,n) for(int i=2; i<n+1; i++)rep1(j,i-1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int>  vi;
typedef vector<vi>  vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int,3> arr3;
typedef long double D;

const ll llinf =  numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
const ll BIG = 10000000000000000;

ll a[200005];
bool af[200005];

int main(){
    ll n;
    cin >> n;
    rep(i,n) cin >> a[i];
    map<ll,ll,greater<ll>> m;
    rep(i,n){
        if(m.find(a[i]) != m.end()){
            m[a[i]]++;
        }
        else{
            m.insert(mp(a[i],1));
        }
    }
    int cnt = 0;

    //if(if(m.find(3) != m.end() && m.at(3)>0))

    for(auto i=m.begin(); i!= m.end(); ++i){
        ll tmp = 1;
        ll key = i->first;
        ll val = i->second;

        while(tmp<key) tmp*=2;
        //prints(key,val,tmp);

        if(tmp == key){
            cnt+=val/2;
            m[key] -= val/2;
            continue;
        }
        else{
            if(m.find(tmp-key) != m.end() && m.at(tmp-key)>0){
                cnt+=min(val,m[tmp-key]);
                m[tmp-key]-=min(val,m[tmp-key]);
            }
        }
    }
    prints(cnt);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int A, B;
  cin >> A >> B;
  int N = 100;
  vector<vector<bool>> G(N, vector<bool>(N));
  for (int i = 0; i < N / 2; i++) {
    for (int j = 0; j < N; j++) {
      G[i][j] = 1;
    }
  }
  int i = 0, j = 0;
  for (int c = 0; c < A - 1; c++) {
    G[i][j] = 0;
    j += 2;
    if (j >= N) {
      i += 2;
      j = 0;
    }
  }
  i = N / 2 + 1, j = 0;
  for (int c = 0; c < B - 1; c++) {
    G[i][j] = 1;
    j += 2;
    if (j >= N) {
      i += 2;
      j = 0;
    }
  }
  cout << "100 100" << endl;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (G[i][j]) {
        cout << '#';
      } else {
        cout << '.';
      }
    }
    cout << endl;
  }

  return 0;
}
/* vim:set fdm=marker: */

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};

int main(){
    int a, b;
    cin >> a >> b;
    int ans = 0;
    int h = 100;
    int w = 100;
    vector<string> c(h, string(w, '.'));
    rep(i, h) rep(j, w) {
        if (i<h/2) c[i][j] = '.';
        else c[i][j] = '#';
    }
    a--; b--;
    cout << h << " " << w << endl;

    while (b) {
        int x = b*3;
        int i = x/h+1;
        int j = x%h;
        c[i][j] = '#';
        b--;
    }
    while (a) {
        int x = a*3;
        int i = x/h + h/2+1;
        int j = x%h;
        c[i][j] = '.';
        a--;
    }

    rep(i, h) cout << c[i] << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
int main() {
	int n;
	cin >> n;
	char *ho = "Hoshino";
	rep(i,n+1) {
		char c, s[2010];
		rep(j, 2000) {
			scanf("%c", &c);
			if(c == '\n') {
				s[j] = '\0';
				break;
			} else {
				s[j] = c;
			}
		}
		for(int j = 0; s[j] != '\0'; ++j) {
			int f = 1;
			rep(k,7) {
				if(s[j + k] != ho[k]) {
					f = 0;
					break;
				}
			}
			if(f) s[j + 6] = 'a';
		}
		if(i != 0) {
			printf("%s",s);
			cout << endl;
		}
	}
	return 0;
}

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

using namespace std;

int main(int argc, char const* argv[])
{
	string find_str("Hoshino"),rep_str("Hoshina"),target;
	int n;

	cin >> n;
	getchar();

	for( int i = 0;i < n;i++ ){
		getline(cin,target);
		for( string::size_type pos = target.find( find_str ); pos != string::npos; pos = target.find( find_str, rep_str.size() + pos ) ){
			target.replace( pos, find_str.size(), rep_str );
		}
		cout << target << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y)*y;
}
ll kai(ll x, ll y, ll m) {
	ll res = 1;
	for (ll i = x - y + 1; i <= x; i++) {
		res *= i; res %= m;
	}
	return res;
}
ll mod_pow(ll x, ll y, ll m) {
	ll res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

ll comb(ll x, ll y, ll m) {
	if (y > x)return 0;
	return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

int n, k, a[100010];
vector<int> vec[100010];
int ans;

int dfs(int v, int p) {
	int res = 0;
	for (int i = 0; i < (int)vec[v].size(); i++) {
		if (vec[v][i] == p)continue;
		res = max(res, dfs(vec[v][i], v));
	}
	if (res == k - 1) {
		if (p > 1)ans++;
		return 0;
	}
	return res + 1;
}

signed main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (i != 1)vec[a[i]].push_back(i);
	}
	if (a[1] != 1)ans++;
	dfs(1, 0);
	cout << ans << endl;
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2e5+500;
int fa[N],dep[N],siz[N],stack[N],dfs_cnt,top,nid[N],tag[N<<1],a[N<<1],dfn[N],K,kfa[N],n;
int fst[N],nxt[N],to[N],mm;
void ade(int u,int v){to[++mm]=v,nxt[mm]=fst[u],fst[u]=mm;}
void dfs(int u)
{
    dep[u]=dep[fa[u]]+1;dfn[u]=++dfs_cnt;nid[dfs_cnt]=u;
    siz[u]=1;
    stack[++top]=u;
    if(dep[u]>=K)kfa[u]=stack[dep[u]-K+1];
    for(int i=fst[u];i;i=nxt[i])
    {
        int v=to[i];dfs(v);
        siz[u]+=siz[v];
    }
    --top;
}
void upd(int rot){a[rot]=0,tag[rot]=1;}
void build(int rot,int lt,int rt)
{
    if(lt==rt){a[rot]=nid[lt];return;}
    int mid=(lt+rt)>>1;
    build(rot<<1,lt,mid),build(rot<<1|1,mid+1,rt);
    a[rot]=dep[a[rot<<1]]>dep[a[rot<<1|1]]?a[rot<<1]:a[rot<<1|1];
}
void pushdown(int rot)
{
    if(tag[rot]){upd(rot<<1),upd(rot<<1|1),tag[rot]=0;}
}
void update(int rot,int lt,int rt,int lq,int rq)
{
    if(lt>=lq&&rt<=rq){upd(rot);return;}
    int mid=(lt+rt)>>1;pushdown(rot);
    if(lq<=mid)update(rot<<1,lt,mid,lq,rq);
    if(rq>mid)update(rot<<1|1,mid+1,rt,lq,rq);
    a[rot]=dep[a[rot<<1]]>dep[a[rot<<1|1]]?a[rot<<1]:a[rot<<1|1];
}
int main()
{
    scanf("%d%d",&n,&K);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",fa+i);
        if(i!=1)ade(fa[i],i);
    }
    if(K==1)
    {
        int ans=0;
        for(int i=1;i<=n;i++)if(fa[i]!=1)++ans;
        cout<<ans<<endl;
        return 0;
    }
    int ans=0;if(fa[1]!=1)++ans;
    dfs(1);
    build(1,1,n);
    while(dep[a[1]]>K+1)
    {
        ++ans;int u=kfa[a[1]];
        update(1,1,n,dfn[u],dfn[u]+siz[u]-1);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define all(a) a.begin(),a.end()
#define sz(a) a.size()
#define re return
#define MP make_pair
#define pb push_back
#define pii pair<int,int>
#define se second
#define fi first
using namespace std;
string s;
signed main(){
	ios_base::sync_with_stdio(0);
	cin>>s;
	for(int i=0;i+2<sz(s);i++){
		if(s[i]==s[i+1]||s[i]==s[i+2]||s[i+1]==s[i+2]){
			cout<<i+1<<' '<<i+3;
			re 0;
		}
	}
	if(sz(s)==2&&s[0]==s[1]) cout<<"1 2";
	else cout<<"-1 -1";
	re 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  string s; cin >> s;
  int n = s.size();
  for(int i = 0; i < n; i++){
    if(i < n-1 && s[i] == s[i+1]){
      cout << i+1 << " " << i+2 << endl;
      return 0;
    }else if(i < n-2 && s[i] == s[i+2]){
      cout << i+1 << " " << i+3 << endl;
      return 0;
    }
  }
  cout << "-1 -1" << endl;
  return 0;
}


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

constexpr long long mod = 998244353;

int main() {
  string s; int k; cin >> s >> k;
  k = min<int>(k, count(s.begin(), s.end(), '1'));
  vector<int> a = { 0 };
  for (auto c : s) if (c == '1') a.back()++; else a.push_back(0);
  reverse(a.begin(), a.end());
  int n = a.size();

  static long long dp[305][305][305]; // i, kx, carry

  dp[0][0][0] = 1;
  rep(i, n) rep(kx, k+1) rep(carry, k+1) {
    if ((dp[i][kx][carry] %= mod) == 0) continue;
    rep2(fw, 1, a[i]+1) dp[i+1][kx][carry+fw] += dp[i][kx][carry];
    rep(take, carry+1) dp[i+1][kx+take][carry-take] += dp[i][kx][carry];
  }

  long long res = 0;
  rep(kx, k+1) res += dp[n][kx][0];
  cout << res % mod << "\n";
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 303
const int mod=998244353;
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int n,m,gu,dp[N][N][N],a[N],ans,sum[N];
char s[N];
int main(){
	scanf("%s",s+1);
	n=strlen(s+1),m=read();
	s[++n]='0';
	m=min(n,m);
	for(int i=1;i<=n;++i){
		if(s[i]=='0')++gu;
		else ++a[gu+1];
	}
	for(int i=1;i<=gu;++i){
		sum[i]=a[i]+sum[i-1];
	}
	dp[0][0][0]=1;
	for(int i=1;i<=gu;++i){
		for(int j=sum[i];j<=n-gu;++j){
			int lim=min(j,n-sum[i-1]-gu);
			for(int k=0;k<=m;++k){
				for(int p=0;p<=lim;++p){
					if(max(0,p-a[i])<=k)dp[i][j][k]=(dp[i][j][k]+dp[i-1][j-p][k-max(0,p-a[i])])%mod;
				}
			}
		}
	}
	for(int k=0;k<=m;++k){
		ans=(ans+dp[gu][n-gu][k])%mod;
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

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

    string s;
    cin >> s;

    int64_t m = s.size();

    vector a(1, 0L);
    int64_t t = 0;
    for (auto c : s) {
        if (c == '0') {
            a.push_back(t);
        } else {
            ++t;
        }
    }
    a.push_back(t);

    int64_t k;
    cin >> k;
    k = min(k, a.back());

    int64_t n = a.size();

    constexpr auto M = 998244353L;

    vector dp(vector(k + 1, vector(a.back() + 1, 0L)));
    int64_t mk = 0;
    dp[0][0] = 1;
    for (auto i : irange(0L, n - 1)) {
        for (auto j : irange(mk, -1L, -1L)) {
            auto r = min(k - j, a[i + 1] - a[i]);
            for (auto d : irange(1L, r + 1L)) {
                auto w = j + d;
                for (auto c : irange(0L, a.back() - a[i + 1] + 1)) {
                    auto b = c + d;
                    dp[w][c] += dp[j][b];
                    if (dp[w][c] >= M) {
                        dp[w][c] -= M;
                    }
                }
            }

            for (auto c : irange(1L, a.back() - a[i + 1] + 1)) {
                dp[j][c] += dp[j][c - 1];
                if (dp[j][c] >= M) {
                    dp[j][c] -= M;
                }
            }
        }
        mk = min(k, mk + a[i + 1] - a[i]);
    }

    int64_t ans = 0;
    for (auto i : irange(0L, mk + 1)) {
        ans += dp[i][0];
    }

    cout << ans % M << "\n";
}

#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int Mod = 998244353;
int add(int a, int b){ return a + b >= Mod ? a + b - Mod : a + b; }
int dec(int a, int b){ return a - b < 0 ? a - b + Mod : a - b; }
void Add(int &a, int b){ a = add(a,b); }
cs int N = 305;
int n, K; char S[N];
int a[N], len, dp[2][N][N];
int main(){
	#ifdef FSYolanda
	freopen("1.in","r",stdin);
	#endif
	scanf("%s%d",S+1,&K);
	K=min(K,n=strlen(S+1));S[n+1]='0';
	for(int i=1,pt=0; i<=n+1; i++){
		if(S[i]=='1')continue;
		a[++len]=i-pt-1; pt=i;
	} int now=0, nxt=1;
	dp[0][0][0]=1;
//	for(int i=1; i<=len; i++)
//	cout << a[i] << " "; cout << endl;
	for(int i=1; i<=len; i++){
		memset(dp[nxt],0,sizeof(dp[nxt]));
		for(int k=0; k<=K; k++){
			static int sm[N]; sm[0]=0;
			for(int l=1; l<=K; l++)
			sm[l]=add(sm[l-1],dp[now][l][k]);
			for(int j=0; j<=K; j++)
			Add(dp[nxt][j][k],dec(sm[min(K,j+a[i])],sm[j]));
		} static int sm[N][N];
		for(int j=0; j<=K; j++)
		for(int k=0; k<=K; k++){
			sm[j][k] = dp[now][j][k];
			if(j && k) Add(sm[j][k],sm[j-1][k-1]);
			Add(dp[nxt][j][k],sm[j][k]);
		} swap(now, nxt);
	}
	int ans = 0;
	for(int j=0; j<=K; j++)
	Add(ans, dp[now][0][j]);
	cout << ans; return 0;
}


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

class Node {
public:
  int key;
  Node* parent;
  Node* left;
  Node* right;
  Node(int key) {
    this->key = key;
    this->parent = NULL;
    this->left = NULL;
    this->right = NULL;
  }
  void append_left(Node* left) {
    this->left = left;
  }
  void append_right(Node* right) {
    this->right = right;
  }
  void append_parent(Node* parent) {
    this->parent = parent;
  }
};

Node* root;

void insert(int key) {
  Node* z = new Node(key);
  Node* cur = root; Node* x = root;
  while(cur != NULL) {
    x = cur;
    if(z->key > cur->key)
      cur = cur->right;
    else
      cur = cur->left;
  }
  if(z->key > x->key) {
    x->append_right(z);
  }
  else {
    x->append_left(z);
  }
  z->append_parent(x);
}
Node* find(int key) {
  Node* z = root;
  while(z != NULL) {
    if(key == z->key)
      return z;
    else if(key > z->key)
      z = z->right;
    else
      z = z->left;
  }
  return NULL;
}
void delete_node(Node *z) {
  if(z==NULL) return;

  int degree = 0;
  if(z->left != NULL) degree++;
  if(z->right != NULL) degree++;

  if(degree == 0) {
    Node* parent = z->parent;
    if(parent == NULL) {
      delete z;
      root = NULL;
      return;
    }
    if(parent->left == z) parent->left = NULL;
    else parent->right = NULL;
    delete z;
  }
  else if(degree == 1) {
    Node* parent = z->parent;
    Node* child;
    if(z->left != NULL) child = z->left;
    else child = z->right;
    if(parent == NULL) {
      root = child;
      root->parent = NULL;
      delete z;
      return;
    }
    if(parent->left == z) parent->left = child;
    else parent->right = child;
    child->parent = parent;
    delete z;
    return;
  }
  else if(degree == 2) {
    Node *y = z->right;
    while(y->left != NULL) {
      y = y->left;
    }
    z->key = y->key;
    delete_node(y);
  }
  return;
}

void print_crawl_preorder(Node* node) {
  if(node == NULL) return;
  cout << " " << node->key;
  if(node->left != NULL) {
    print_crawl_preorder(node->left);
  }
  if(node->right != NULL) {
    print_crawl_preorder(node->right);
  }
  return;
}
void print_crawl_inorder(Node* node) {
  if(node == NULL) return;
  if(node->left != NULL) {
    print_crawl_inorder(node->left);
  }
  cout << " " << node->key;
  if(node->right != NULL) {
    print_crawl_inorder(node->right);
  }
  return;
}

int main() {
  int n; cin >> n;
  root = NULL;
  for(int i=0;i<n;i++) {
    string command; cin >> command;
    if(command == "insert") {
      int key; cin >> key;
      if(root == NULL) {
        root = new Node(key);
      }
      else {
        insert(key);
      }
    }
    else if(command == "find") {
      int key; cin >> key;
      if(find(key) != NULL) cout << "yes" << endl;
      else cout << "no" << endl;
    }
    else if(command == "delete") {
      int key; cin >> key;
      delete_node(find(key));
    }
    else if(command == "print") {
      print_crawl_inorder(root); cout << endl;
      print_crawl_preorder(root); cout << endl;
    }
  }
}

#include <cstdio>
#include <list>

#define N 100

using namespace std;

/** Crazy Motor-Cycle */
struct cmc
{
  int a;
  int b;
  int c;
};

/** Application main entry point. */
int
main (
  int     argc,
  char  * argv[ ]
  )
{
  int i;

  for ( ; ; )
  {
    list<struct cmc> lis;
    int d[ 3 * N ];
    int a, b, c;
    int n;

    scanf ( "%d%d%d", &a, &b, &c );
    if ( !( a | b | c ) ) break ;
    b += a; c += b;
    for ( i = 0; i < c; ++i )
    {
      d[ i ] = 2;
    }

    scanf ( "%d", &n );
    while ( n-- )
    {
      struct cmc t;
      int s;

      scanf ( "%d%d%d%d", &t.a, &t.b, &t.c, &s );
      --t.a; --t.b; --t.c;
      if ( s )
      {
        d[ t.a ] =
        d[ t.b ] =
        d[ t.c ] = 1;
      }
      else
      {
        lis.push_back ( t );
      }
    }

    for ( list<struct cmc>::iterator it = lis.begin ( )
        ; it != lis.end ( ); )
    {
      if ( ( d[ it->a ] == 2 && d[ it->b ] == 1 && d[ it->c ] == 1 )
        || ( d[ it->a ] == 1 && d[ it->b ] == 2 && d[ it->c ] == 1 )
        || ( d[ it->a ] == 1 && d[ it->b ] == 1 && d[ it->c ] == 2 ) )
      {
        if      ( d[ it->a ] == 2 ) d[ it->a ]  = 0;
        else if ( d[ it->b ] == 2 ) d[ it->b ]  = 0;
        else                        d[ it->c ]  = 0;
        lis.erase ( it );
        it = lis.begin ( );
      }
      else ++it;
    }

    for ( i = 0; i < c; ++i )
    {
      printf ( "%d\n", d[ i ] );
    }
  }

  return ( 0 );
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iostream>
#define llong long long
using namespace std;

inline int read()
{
	int x=0; bool f=1; char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
	for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
	if(f) return x;
	return -x;
}

const int N = 2e6;
const int P = 998244353;
llong fact[N+3],finv[N+3];

llong quickpow(llong x,llong y)
{
	llong cur = x,ret = 1ll;
	for(int i=0; y; i++)
	{
		if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;}
		cur = cur*cur%P;
	}
	return ret;
}
llong comb(llong x,llong y) {return x<0||y<0||x<y ? 0ll : fact[x]*finv[y]%P*finv[x-y]%P;}

llong calc(llong n,llong m,llong k)
{
	llong ret = 0ll;
	for(int i=0; i<=k; i++)
	{
		if((m-i)&1) continue;
		llong tmp = comb(n,i)*comb(((m-i)>>1)+n-1,n-1)%P;
		ret = (ret+tmp)%P;
	}
//	printf("calc %lld %lld %lld=%lld\n",n,m,k,ret);
	return ret;
}

int n,m;

int main()
{
	fact[0] = 1ll; for(int i=1; i<=N; i++) fact[i] = fact[i-1]*i%P;
	finv[N] = quickpow(fact[N],P-2); for(int i=N-1; i>=0; i--) finv[i] = finv[i+1]*(i+1)%P;
	scanf("%d%d",&n,&m);
	llong ans = calc(n,3*m,m);
	ans = (ans-n*(calc(n,m,m)-calc(n-1,m,m)+P)%P+P)%P;
	printf("%lld\n",ans);
	return 0;
}

#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
using namespace std;
inline int read(){
	int v=0,f=1;
	char c=getchar();
	while (c<'0' || c>'9'){
		if (c=='-') f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9'){
		v=v*10+c-'0';
		c=getchar();
	}
	return v*f;
}
const int Maxn=2000005;
const int mod=998244353;
int fact[Maxn],ivf[Maxn];
int qp(int x,int p){
	int ret=1;
	while (p){
		if (p&1) ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		p>>=1;
	}
	return ret;
}
int inv(int x){
	return qp(x,mod-2);
}
int C(int x,int y){
	return 1ll*fact[x]*ivf[y]%mod*ivf[x-y]%mod;
}
void _init(){
	fact[0]=1;
	for (int i=1;i<Maxn;i++){
		fact[i]=1ll*fact[i-1]*i%mod;
		ivf[i]=inv(fact[i]);
	}
	ivf[0]=1;
}
int main(){
	_init();
	int n,m;
	scanf("%d %d",&n,&m);
	int ans=0;
	for (int i=m;i>=0;i-=2){
		if (i>n) continue;
		int rem=(3*m-i)/2;
		ans+=1ll*C(n,i)*C(rem+n-1,n-1)%mod;
		if (ans>=mod) ans-=mod;
	}
//	cout<<ans<<endl;
	for (int i=m;i>=0;i-=2){
		if (i>n) continue;
		int rem=(3*m-i)/2;
		rem-=m;
		ans-=1ll*C(n,i)*C(rem+n-1,n-1)%mod*i%mod;
		if (ans<0) ans+=mod;
	}
	for (int i=m;i>=0;i-=2){
		if (i>n) continue;
		int rem=(3*m-i)/2;
		rem-=m+1;
		if (rem<0) continue;
		ans-=1ll*C(n,i)*C(rem+n-1,n-1)%mod*(n-i)%mod;
		if (ans<0) ans+=mod;
	}
	printf("%d\n",ans);
	return 0;
}


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

int main(){
  int n;

  while(cin>>n&&n){
    vector<int> k(n);

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

    string s;cin>>s;
    vector<char> c;

    for(int i=0;i<s.size();++i){
      c.emplace_back(s.at(i));
      int key = k[i%k.size()];
      while(key!=0){
        int tmp;
        if(key>26){
          key -= 26;
          tmp = 26;
        }else{
          tmp = key;
          key -= key;
        }

        if(c[i]>='A'&&c[i]<='Z'){
          c[i] -= tmp;
          if(c[i]<'A'){
            char sub = 'A' - c[i] - 1;
            c[i] = 'z' - sub;
          }
        }
        else if(c[i]>='a'&&c[i]<='z'){
          c[i] -= tmp;
          if(c[i]<'a'){
            char sub = 'a' - c[i] - 1;
            c[i] = 'Z' - sub;
          }
        }
      }
      cout<<c[i];
    }
    cout<<endl;
  }

  return EXIT_SUCCESS;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

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

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

bool solve(){
  int n;
  int k[SIZE];
  char c[SIZE];

  scanf("%d",&n);

  if(n == 0) return false;

  for(int i=0;i<n;i++){
    scanf("%d",k+i);
  }

  scanf("%s",c);

  for(int i=0;c[i]!='\0';i++){

    for(int j=0;j<k[i%n];j++){
      if(c[i] == 'a')
        c[i] = 'Z';
      else if(c[i] == 'A')
        c[i] = 'z';
      else
        c[i]--;
    }

  }

  printf("%s\n",c);

  return true;

}

int main(){

  while(solve());

  return 0;
}


#include<bits/stdc++.h>
#define FL "a"
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+10;
const ll INF=1ll<<60;
inline ll read(){
  ll d=0,w=1;char ch=getchar();
  while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
  if(ch=='-')w=-1,ch=getchar();
  while(ch<='9'&&ch>='0')d=d*10+ch-48,ch=getchar();
  return d*w;
}
inline void file(){
  freopen(FL".in","r",stdin);
  freopen(FL".out","w",stdout);
}

int n,q,x[N];
ll mnv[2][N<<2],lz[N<<2],ans=INF;//0:+ 1:-
#define ls (i<<1)
#define rs (i<<1|1)
#define mid ((l+r)>>1)
inline void update(int i){
  mnv[0][i]=min(mnv[0][ls],mnv[0][rs]);mnv[1][i]=min(mnv[1][ls],mnv[1][rs]);
}
inline void cover(int i,ll v){lz[i]+=v;mnv[0][i]+=v;mnv[1][i]+=v;}
inline void pushdown(int i){
  if(lz[i])cover(ls,lz[i]),cover(rs,lz[i]),lz[i]=0;
}
void build(int i,int l,int r){
  mnv[0][i]=mnv[1][i]=INF;if(l==r)return;build(ls,l,mid);build(rs,mid+1,r);
}
inline void insert(int i,int l,int r,int p,ll v){
  if(l==r){mnv[0][i]=min(mnv[0][i],v+p);mnv[1][i]=min(mnv[1][i],v-p);return;}
  pushdown(i);p<=mid?insert(ls,l,mid,p,v):insert(rs,mid+1,r,p,v);update(i);
}
inline ll query(int i,int l,int r,int x,int y,int p){
  if(x<=l&&r<=y)return mnv[p][i];pushdown(i);ll s=INF;
  if(x<=mid)s=query(ls,l,mid,x,y,p);
  if(mid<y)s=min(s,query(rs,mid+1,r,x,y,p));return s;
}

int main()
{
  n=read();q=read();x[0]=read();
  build(1,1,n);insert(1,1,n,read(),0);
  for(int i=1;i<=q;i++)x[i]=read();
  for(int i=1;i<=q;i++){
    ll ql=query(1,1,n,1,x[i],1)+x[i],qr=query(1,1,n,x[i],n,0)-x[i];
    cover(1,abs(x[i]-x[i-1]));insert(1,1,n,x[i-1],min(ql,qr));
  }
  for(int i=1;i<=n;i++)ans=min(ans,query(1,1,n,i,i,0)-i);
  printf("%lld\n",ans);return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll INF = 4e18;

struct STNode {
    typedef STNode* NP;
    NP l, r;
    int sz;
    ll mi;
    STNode(int _sz) : sz(_sz) {
        mi = INF;
        if (sz == 1) return;
        l = new STNode(sz/2);
        r = new STNode(sz-sz/2);
    }
    void set(int k, ll x) {
        if (sz == 1) {
            mi = min(mi, x);
            return;
        }
        if (k < sz/2) {
            l->set(k, x);
        } else {
            r->set(k-sz/2, x);
        }
        mi = min(l->mi, r->mi);
    }
    ll get(int a, int b) {
        if (b <= 0 || sz <= a) return INF;
        if (a <= 0 && sz <= b) return mi;
        return min(l->get(a, b), r->get(a-sz/2, b-sz/2));
    }
};

const int MN = 222222;
int n, q, a, b;
int x[MN];

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

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

    STNode* tr = new STNode(n+1);
    STNode* tr1 = new STNode(n+1);
    STNode* tr2 = new STNode(n+1);
    tr->set(b, 0);
    tr1->set(b, -b);
    tr2->set(b, b);

    ll sm = 0;
    for (int i = 0; i < q; i++) {
        ll c = min(tr1->get(1, x[i+1]+1) + sm + x[i+1], tr2->get(x[i+1]+1, n+1) + sm - x[i+1]);
        sm += abs(x[i+1] - x[i]);
        tr->set(x[i], c - sm);
        tr1->set(x[i], c - x[i] - sm);
        tr2->set(x[i], c + x[i] - sm);
    }
    cerr << tr->get(x[0], x[0]+1) + sm << endl;
    cout << tr->get(1, n+1) + sm << endl;
    return 0;
}


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

lint gcd(lint x, lint y) {
  if(x == 0) { return y; }
  else { return gcd(y%x,x); }
}

lint lcm(lint x, lint y) { return x/gcd(x,y)*y; }

lint C(lint n, lint k) {
  if(n == k) { return 1; }
  else if(n < k) { return 0; }
  else if(k == 0) { return 1; }
  else if(k == 1) { return n; }
  else return C(n-1,k-1) + C(n-1,k);
}

lint P(lint n, lint k) {
  if(k == 1) { return n; }
  return (n*(P(n-1,k-1)%1000000007)%1000000007);
}

int main() {
  int n; cin >> n;
  string s,t; cin >> s >> t;

  int l;
  for(l=n;l>0;l--) {
    string pre = s.substr(n-l,l), suf = t.substr(0,l);
    if(pre == suf) {
      cout << 2*n-l << endl;
      return 0;
    }
  }

  cout << n*2 << endl;
  return 0;
}

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

lli n;
string s, t;

int main(void){
	cin >> n >> s >> t;
	lli ans = 2*n;
	rep(i, n){
		if(s.substr(i, n-i) == t.substr(0, n-i)){
			ans = min(ans, n+i);
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include<string>
#define fo(i, n) for(ll i = 0; i < n; i++)
#define endl "\n"
#define of(i, n) for(ll i = n - 1; i >= 0; i--)
#define ll long long
#define vec vector<ll>
#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define rep(i,j,n) for (ll i = j; i <= n; ++i)
#define EB emplace_back
#define mem(i,j) memset( i, j,sizeof(i))
#define ff first
#define ss second
#define ii pair<ll,ll>
#define iii pair<ll,pair<ll,ll> >
#define pb push_back
#define mp make_pair
#define all(i) i.begin(),i.end()
#define pr(a) cout<<a<<endl
#define pr
#define prv(a) for(int jkl=0; jkl<a.size();jkl++) cout<<a[jkl]<<" "; cout<<endl;
ll mod = 1e9+7;
string l,r;
ll d;
int dp[10100][2][2][101];

ll getAns(int level, int tlow, int thi, int rem)
{
    if(level>=l.length())
    {
        if(rem==0) return 1;
        else return 0;
    }
    if(dp[level][tlow][thi][rem]==-1)
    {
        int blow = 0;
        int bhigh = 9;
        if(tlow) blow =l[level] - '0';
        if(thi) bhigh = r[level]-'0';
        ll ans =0;

        for(int i=blow;i<=bhigh;i++)
        {
            int ntlow = tlow;
            int nthi = thi;
            if( i > l[level]-'0') ntlow =0 ;
            if(i<r[level]-'0') nthi =0;

            ans += getAns(level+1, ntlow,nthi,(rem + i)%d);
            ans%=mod;
        }
        dp[level][tlow][thi][rem] = ans;
    }
    return dp[level][tlow][thi][rem];

}

int main()
{
    string n;
    cin>>n;
    r = n;
    l = "1";

    while(l.length()<r.length()) l += '0';
    reverse(all(l));
    cin>>d;
    mem(dp,-1);
    //cout<<r<<" "<<l<<endl;

    cout<<getAns(0,1,1,0)<<endl;;
}

#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;
#define int long long
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(),(c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

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

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 1e4 + 10;

int dp[N][2][100];

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	string k;
	int d;
	cin >> k >> d;
	int n = k.length();
	dp[0][1][0] = 1;
	for(int i = 1; i <= n; i++) {
		int cur = k[i - 1] - '0';
		for(int j = 0; j <= 9; j++) {
			for(int rem = 0; rem < d; rem++) {
				dp[i][0][(rem + j) % d] = (dp[i][0][(rem + j) % d] + dp[i - 1][0][rem]) % mod;
				if(j < cur) {
					dp[i][0][(rem + j) % d] = (dp[i][0][(rem + j) % d] + dp[i - 1][1][rem]) % mod;
				}
				if(j == cur) {
					dp[i][1][(rem + j) % d] = (dp[i][1][(rem + j) % d] + dp[i - 1][1][rem]) % mod;
				}
			}
		}
	}
	cout << (dp[n][0][0] + dp[n][1][0] - 1 + mod) % mod;

	return 0;
}


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

using namespace std;

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

const ll INF=1e9+1;

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

#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;

int main() {
	double x1, y1, x2, y2, x, y, ans;
	cin >> x1 >> y1 >> x2 >> y2;
	x = x1 - x2;
	y = y1 - y2;
	ans = sqrt(x*x + y*y);
	printf("%f", ans);
	return 0;
}


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxn 200005
#define maxm 1000006
#define maxc 1000000007
#define pii pair <int,int>
#define Task ""
#define double long double
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
const double Pi = acos(-1);
using namespace std;
int n,t[55][55];
pii a[maxn];
long long ans;
int Get(int u,int v)
{
    if (!u || !v) return 0;
    int res = 0;
    while (u)
    {
        int temp = v;
        while (temp)
        {
            res += t[u][temp];
            temp -= temp & -temp;
        }
        u -= u & -u;
    }
    return res;
}
void Update(int u,int v,int val)
{
    while (u <= 50)
    {
        int temp = v;
        while (temp <= 50)
        {
            t[u][temp] += val;
            temp += temp & -temp;
        }
        u += u & -u;
    }
}
int main()
{
    srand(time(NULL));
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie();
    if(fopen(".inp", "r")) freopen(".inp", "r", stdin);
    //freopen(Task".inp", "r",stdin);
    //freopen(Task".out", "w",stdout);
    cin >> n;
    FOR(i,1,n)
    {
        double x;cin >> x;
        long long u = round(x * 1e9);
        while (u % 2 == 0)
        {
            ++a[i].F;
            u >>= 1;
        }
        while (u % 5 == 0)
        {
            ++a[i].S;
            u /= 5;
        }
    }
    FOR(i,1,n)
    {
        int need2 = max(1,18 - a[i].F + 1),
            need5 = max(1,18 - a[i].S + 1);
        ans += Get(50,50);
        ans -= Get(need2 - 1,50);
        ans -= Get(50,need5 - 1);
        ans += Get(need2 - 1,need5 - 1);
        Update(a[i].F + 1,a[i].S + 1, 1);
    }
    cout << ans;
    return 0;
}

#include<bits/stdc++.h>
#define Imposter ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long int
#define ld long double
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)
using namespace std;
#define f(n) for(ll i=0;i<n;i++)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define endl "\n"
#define vll vector<ll>
#define vlll vector<vll>
#define all(x) x.begin(),x.end()
int n;
inline void solve()
{
    cin>>n;
    int a[n+2];
    for(int i=1;i<=n;i++)
    cin>>a[i];
    a[0]=0;
    a[n+1]=0;
    ll sum=0;
    for(int i=1;i<n+2;i++)
    {
        sum+=abs(a[i]-a[i-1]);
    }
    //cout<<sum<<endl;
    for(int i=1;i<=n;i++)
    {
        cout<<sum-abs(a[i]-a[i-1])-abs(a[i+1]-a[i])+ abs(a[i+1]-a[i-1])<<endl;
    }

}
int main()
{
    Imposter

    int t=1;
    //cin>>t;
    while(t--)
    {
        cout<<fixed<<setprecision(10);
       solve();

    }
    return 0;
}


#include <bits/stdc++.h>

int main() {

  int v[128];
  int n;

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

  for(int i = n-1; i > 0; --i) {
    std::cout << v[i] << " ";
  }
  std::cout << v[0] << std::endl;

  return 0;
}

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cstring>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for(long long i=0; i<n; i++)
#define reps(i, s, e) for(long long i=s; i<e; i++)
#define repr(i, n) for(long long i=n-1; i>=0; i--)
#define reprs(i, s, e) for(long long i=e-1; i>=s; i--)

int main(){

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

    ll n; cin >> n;
    ll a[n];
    rep(i, n){
        cin >> a[i];
    }

    double a_sum = accumulate(a, a+n, 0LL);
    double ave = a_sum / n;

    double vmin = 1e18;
    ll i_ans = 0;
    rep(i, n){
        double dif = abs(ave - a[i]);
        if(dif < vmin){
            i_ans = i;
            vmin = dif;
        }
    }

    cout << i_ans << endl;

    return 0;
}


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

using namespace std;

int main(){

	long long n,m,sum=0;
	cin>>n;
	vector<long long> vec;
	for(int i=0;i<n;++i){
		cin>>m;
		vec.push_back(m);
		sum+=m;
	}

	sort(vec.begin(),vec.end());
	long long max,min;
	max=vec[n-1];
	min=vec[0];

	cout<<min<<" "<<max<<" "<<sum<<endl;

	return 0;
}

#include<iostream>

int main(void){
	int n, min = 1000000, max = -1000000;
	long long sum = 0;

	std::cin >> n;
	if (n == 0) return 0;

	for (int i = 0; i < n; i++){
		int x;
		std::cin >> x;
		sum = sum + x;
		if (min > x) min = x;
		if (max < x) max = x;
	}

	std::cout << min << " " << max << " " <<  sum << std::endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>

const int lens = 1e5 + 1;
long long n,m,a[lens],sum[lens];
std::map<long long,long long> map;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin >> n >> m;
    for(int i = 1;i <= n;i++)
    {
        std::cin >> a[i];
    }

    long long ans = 0;
    map[0]++;
    for(int i = 1;i <= n;i++)
    {
        sum[i] = (sum[i - 1] + a[i]) % m;
        ans += map[sum[i]];
        map[sum[i]]++;
    }
    std::cout << ans;
    return 0;
}

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = INT_MAX / 2;
const ll  inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const long double PI = acos(-1);

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

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

	vector<ll> a(n);
	rep(i, n) cin >> a[i];

	map<ll, ll> mp;
	ll sum = 0;
	mp[sum]++;

	for (int i = 0; i < n; i++) {
		sum += a[i];
		mp[sum % m]++;
	}

	ll ans = 0;
	for (auto m : mp) {
		ans += m.second * (m.second - 1) / 2;
	}

	out(ans);

	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>

using namespace std;

struct SccKosaraju {
    int n;
    vector<vector<int>> g;
    vector<vector<int>> rg;
    vector<bool> visited;
    vector<int> sccno;
    int sccCount = 0;
    vector<int> s;

    SccKosaraju(int n): n(n) {
        g.assign(n, vector<int>());
        rg.assign(n, vector<int>());
        visited.assign(n, false);

        sccno.assign(n, -1);
    }

    void addEdge(int from, int to) {
        g[from].push_back(to);
        rg[to].push_back(from);
    }

    void solve() {
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                dfs1(i);
            }
        }

        for (int i = n - 1; i >= 0; i--) {
            if (sccno[s[i]] == -1) {
                sccCount++;
                dfs2(s[i]);
            }
        }
    }

    bool sameScc(int u, int v) {
        return sccno[u] == sccno[v];
    }

    void dfs1(int u) {
        visited[u] = true;
        for (int v : g[u]) {
            if (!visited[v]) {
                dfs1(v);
            }
        }
        s.push_back(u);
    }

    void dfs2(int u) {
        sccno[u] = sccCount;
        for (int v : rg[u]) {
            if (sccno[v] == -1) {
                dfs2(v);
            }
        }
    }
};

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

    for (int i = 0; i < m; i++) {
        int from, to;
        cin >> from >> to;
        scc.addEdge(from, to);
    }

    scc.solve();

//    for (int i = 0; i < n; i++) {
//        cout << i << " " << scc.sccno[i] << endl;
//    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
        int u, v;
        cin >> u >> v;
        cout << (scc.sameScc(u, v)? 1 : 0) << endl;
    }

    return 0;
}

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int s = 0, const int d = 0, const Weight w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;
  using iterator = std::vector<Edges>::iterator;
  using const_iterator = std::vector<Edges>::const_iterator;

public:
  Graph(const int size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  const Edges &operator[](const int i) const { return g[i]; }
  void addArc(const int src, const int dst, const Weight w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int node1, const int node2, const Weight w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

std::vector<int> kosaraju(const Graph &g) {
  int n = g.size(), m = 0;
  Graph rg(n);
  std::vector<int> stk, cmp(n, -1), added(n), visited(n), ord(n);
  for (auto &es : g) {
    for (auto &e : es) rg.addArc(e.dst, e.src, e.weight);
    m += es.size();
  }
  stk.resize(n + m);
  m = 0;
  for (int i = 0; i < n; i++) {
    if (visited[i]) continue;
    int s = 0;
    stk[s++] = i;
    while (s != 0) {
      int v = stk[s - 1];
      visited[v] = true;
      bool pushed = false;
      for (auto &e : g[v]) {
        if (!visited[e.dst]) {
          stk[s++] = e.dst;
          pushed = true;
        }
      }
      if (pushed) continue;
      int t = stk[s - 1];
      if (!added[t]) {
        added[t] = true;
        ord[n - ++m] = t;
      }
      --s;
    }
  }
  int k = 0;
  for (int u : ord) {
    if (cmp[u] != -1) continue;
    int s = 0;
    stk[s++] = u;
    while (s != 0) {
      int v = stk[--s];
      cmp[v] = k;
      for (auto &e : rg[v]) {
        if (cmp[e.dst] == -1) stk[s++] = e.dst;
      }
    }
    ++k;
  }
  return cmp;
}

main {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  while (m--) {
    int s, t;
    cin >> s >> t;
    g.addArc(s, t);
  }
  int q;
  cin >> q;
  vi scc = kosaraju(g);
  while (q--) {
    int u, v;
    cin >> u >> v;
    cout << (scc[u] == scc[v]) << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i,m,n) for(lli i = m;i < n;i++)
#define rrep(i,m,n) for(lli i=m-1;i>=n;i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(),N.end()),N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(),n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K,L,N,S) vector<L> K(N,S)
#define DV(K,L,N,M,S) vector<vector<L>> K(N,vector<L>(M,S))
#define TV(K,L,N,M,R,S) vector<vector<vector<L>>> K(N,vector<vector<L>>(M,vector<L>(R,S)))
#define pint pair<lli,lli>
#define paf(L,R) pair<L,R>
#define mod 1000000007
#define MAX 510000
#define ALL(a)  a.begin(),a.end()
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
int main(){
  lli A,B,C,D,E,F,N,M,K,L,X,Y,Z,H,W,sum=0,num=0,flag=0;string S,T;
  cin >> X >> Y;
  if(X==3)sum+=100000;
  if(X==2)sum+=200000;
  if(X==1)sum+=300000;
  if(Y==3)sum+=100000;
  if(Y==2)sum+=200000;
  if(Y==1)sum+=300000;
  if(X==Y&&Y==1)sum+=400000;
  Out(sum);
}

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    cout << (100000 * (max(0, 4 - x) + max(0, 4 - y)) + (x == 1 && y == 1 ? 400000 : 0)) << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;
typedef pair<int, int> P;
int k, a[100000], dist[100000];
vector<int> v[100000];
vector<P> w;
bool b[100000];
void cal(int x, int y){
    dist[x] = y;
    b[x] = false;
    w.push_back(P(y, x));
    for(int i=0;i<v[x].size();i++){
        cal(v[x][i], y+1);
    }
}
int hin(int x, int y){
    if(b[x])return 0;
    b[x] = true;
    if(x == 0 || (a[x] == 0 && y == k-1))return 0;
    if(y == k-1)return 1;
    return hin(a[x], y+1);
}
int main() {
    int n, ans = 0;
    cin >> n >> k >> a[0];
    if(--a[0] != 0)ans++;
    for(int i=1;i<n;i++){
        cin >> a[i];
        v[--a[i]].push_back(i);
    }
    cal(0, 0);
    sort(w.begin(), w.end(), greater<P>());
    for(int i=0;i<w.size();i++){
        int p = w[i].second;
        if(!b[p]){
            ans += hin(p, 0);
        }
    }
    cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
vector<int> adjlist[100005];
int mx[100005];
int ans = 0;
int n,k;
void dfs(int u){
    mx[u] = 0;
    for (auto v : adjlist[u]){
        dfs(v);
        if (mx[v] == k && u != 1){
            ans++;
        }
        else{
            mx[u] = max(mx[u],mx[v]);
        }
    }
    mx[u]++;
}
int main(){
    scanf("%d%d",&n,&k);
    for (int i = 1,x; i <= n; i++){
        scanf("%d",&x);
        if (i == 1){
            if (x != 1){
                ans++;
            }
        }
        else adjlist[x].push_back(i);
    }
    dfs(1);
    printf("%d",ans);
}


#include <bits/stdc++.h>

#define int long long
#define ci(m)     for(int i=0;i<m;i++)
#define cj(m)     for(int j=0;j<m;j++)
#define ck(m)     for(int k=0;k<m;k++)
#define gcd            __gcd
#define endl           "\n"
#define pb             emplace_back
#define setbits(x)     __builtin_popcountll(x)
#define zrobits(x)     __builtin_ctzll(x)
#define mod            1000000007
#define mod2           998244353
#define maxe           *max_element
#define mine           *min_element
#define inf            1e18
#define deci(x, y)      fixed<<setprecision(y)<<x
#define w(t)           int t; cin>>t; while(t--)
#define nitin          ios_base::sync_with_stdio(false); cin.tie(NULL)
#define PI             3.141592653589793238
using namespace std;
int n,m;
int ans=INT_MAX;
int a[500][500];
void solve(int num,set<int>&s)
{
    if(num==m)
    {
        return ;
    }
    vector<int>v(m+1,0);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(s.count(a[i][j]))
                continue;
            else
            {
                v[a[i][j]]++;
                break;
            }
        }
    }
    int val=maxe(v.begin(),v.end());
    ans=min(ans,val);
    for(int i=1;i<=m;i++)
    {
        if(v[i]==val)
        {
            s.insert(i);
            solve(num+1,s);
            return;
        }
    }
}
int32_t main() {
    nitin;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    set<int>s;
    solve(0,s);
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int read() {
    char cc = getchar(); int cn = 0, flus = 1;
    while(cc < '0' || cc > '9') {  if( cc == '-' ) flus = -flus;  cc = getchar();  }
    while(cc >= '0' && cc <= '9')  cn = cn * 10 + cc - '0', cc = getchar();
    return cn * flus;
}
const int N = 1000 + 5 ;
#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )
int n, m, book[N], w[N][N], vis[N], can[N];
bool check( int x ) {
    memset( book, 0, sizeof(book) ), memset( can, 0, sizeof(can) ) ;
    int flag ; rep( i, 1, n ) vis[i] = 1, ++ book[w[i][vis[i]]] ;
    rep( i, 1, m ) {
        flag = 1 ;
        rep( j, 1, n ) {
            int u = w[j][vis[j]] ;
            if( book[u] > x ) {
                can[u] = 1; flag = 0 ;
                while( vis[j] <= n && can[w[j][vis[j]]] ) ++ vis[j] ;
                if( vis[j] > n ) return 0;
                ++ book[w[j][vis[j]]] ;
            }
        }
        if( flag ) return 1;
    }
    return 0 ;
}
signed main()
{
    n = read(), m = read() ;
    rep( i, 1, n ) rep( j, 1, m ) w[i][j] = read() ;
    int l = 1, r = n, ans = 0, mid ;
    while( l <= r ) {
        mid = ( l + r ) >> 1 ;
        if( check(mid) ) ans = mid, r = mid - 1 ;
        else l = mid + 1 ;
    }
    printf("%d\n", ans ) ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

constexpr int NUM_BITS = 18;

template<typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

// Returns a tuple of (l, u) s.t.
// - l is the minimum value s.t. is_upper_fn(l) is false.
// - r is the minimum value s.t. is_upper_fn(r) is true.
//
// Required:
// - is_upper_fn is a function s.t.
//   - There exists a constant c s.t. is_upper_fn(x) is true for x >= c and
//     is_upper_fn(x) is false for x < c.
// - is_upper_fn(min_value) is false.
// - is_upper_fn(max_value) is true.
//
// Verified: ABC144E
tuple<int64, int64> BinarySearch(function<bool(int64)> is_upper_fn, int64 l, int64 u) {
  while (u - l >= 2) {
    int64 m = (l + u) / 2;
    if (is_upper_fn(m)) u = m; else l = m;
  }
  return make_tuple(l, u);
}

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

  int n;
  cin >> n;
  vector<int64> xs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i];
  }
  int64 c;
  cin >> c;

  auto next_table = Make2DVector(NUM_BITS, n, -1);
  for (int i = 0; i < n; i++) {
    next_table[0][i] = i;
    if (i > 0) next_table[0][i] = next_table[0][i - 1];
    while (next_table[0][i] + 1 < n && xs[next_table[0][i] + 1] - xs[i] <= c) {
      next_table[0][i]++;
    }
  }

  for (int b = 1; b < NUM_BITS; b++) {
    for (int i = 0; i < n; i++) {
      next_table[b][i] = next_table[b - 1][next_table[b - 1][i]];
    }
  }

  int q_count;
  cin >> q_count;
  for (int q = 0; q < q_count; q++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b) swap(a, b);

    auto is_upper_fn = [&](int k) -> bool {
      int v = a;
      for (int b = 0; b < NUM_BITS; b++) {
        if (k & (1 << b)) {
          v = next_table[b][v];
        }
      }
      return v >= b;
    };

    int l = 0;
    int u = n - 1;
    tie(l, u) = BinarySearch(is_upper_fn, l, u);
    cout << u << "\n";
  }
}


#include <iostream>
#include <vector>

using namespace std;
using int_vec = vector<int>;
using Iterator = int_vec::iterator;

Iterator partition(Iterator begin, Iterator end, int x) {
    for (auto it = begin; it != end; ++it) {
        if (*it <= x) {
            swap(*begin++, *it);
        }
    }
    return begin-1;
}

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

    int_vec v;
    for (auto i = 0; i < n; ++i) {
        int x;
        cin >> x;
        v.push_back(x);
    }

    auto mid = partition(v.begin(), v.end(), v.back());
    auto it = v.begin();
    for (; it != v.end()-1; ++it) {
        if (it == mid) {
            cout << "[" << *it << "]" << " ";
        } else {
            cout << *it << " ";
        }
    }
    cout << *it << endl;

    return 0;
}

#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<stack>
#include<map>
#include<iterator>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAX 100000
#define SENTINEL 2000000000

int A[MAX],n;

int partition(int p,int r)
{
	int x,i,j,t;
	x = A[r];
	i = p-1;
	for(j=p;j<r;j++)
	{
		if(A[j]<=x)
		{
			i++;
			t = A[i];A[i] = A[j];A[j] = t;
		}
	}
	t = A[i+1];A[i+1] = A[r];A[r] = t;
	return i+1;
}

int main()
{
	int i,q;
	scanf("%d",&n);
	for(i=0;i<n;i++) scanf("%d",&A[i]);

	q = partition(0,n-1);
	for(i=0;i<n;i++)
	{
		if(i) printf(" ");
		if(i==q)printf("[");
		printf("%d",A[i]);
		if(i==q)printf("]");
	}
	printf("\n");

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
int dx[4] = { 0,-1,0,1 };
int dy[4] = { -1,0,1,0 };
int main() {
	int h, w;
	cin >> h >> w;
	vector<string> s(h);
	rep(i, h) {
		cin >> s[i];
	}
	queue<P<int>> que;
	VV dist(h, V(w, upper));
	rep(i, h) rep(j, w) {
		if (s[i][j] == '#') {
			que.push(make_pair(i, j));
			dist[i][j] = 0;
		}
	}
	while (!que.empty()) {
		P<int> p = que.front();
		que.pop();
		int y = p.first;
		int x = p.second;
		rep(dir, 4) {
			int ny = y + dy[dir];
			int nx = x + dx[dir];
			if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
			if (dist[ny][nx] != upper) continue;
			dist[ny][nx] = dist[y][x] + 1;
			que.push(make_pair(ny, nx));
		}
	}
	int ans = 0;
	rep(i, h) rep(j, w) {
		chmax(ans, dist[i][j]);
	}
	cout << ans << endl;
	return 0;
}

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

int N, M, p[1001], s[1001 * 1001];

int main(){
    while(scanf("%d%d", &N, &M) != EOF && (N|M)){
        for(int x = 0; x < N; x++){
            scanf("%d", p+x);
        }
        p[N++] = 0;
        for(int x = 0; x < N; x++){
            for(int y = 0; y < N; y++){
                s[N*x + y] = p[x] + p[y];
            }
        }
        sort(s, s+N*N);
        int ans = 0;
        for(int x = 0; x < N*N; x++){
            int* lb = lower_bound(s, s + N * N, M - s[x]);
            if(lb != s) --lb;
            if(s[x] + *lb > M) continue;
            ans = max(ans, s[x] + *lb);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;

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

	double ans;
	if (!d) {
		double p = (double)n / (n * n);
		ans = (double)p * (m - 1);
	}
	else {
		double p = (double)2 * (n - d) / (n * n);
		ans = (double)p * (m - 1);
	}

	cout << fixed << setprecision(10) << ans << endl;
}

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

int main(){
	string sa;

	while(getline(cin,sa)){

		for(int j=0;j<26;j++){
			int a=sa.size();
		for(int i=0;i<a;i++){
			if(sa[i]>='a'&&sa[i]<='z'){
				sa[i]+=1;
				if(sa[i]>'z'){
					sa[i]-=26;
					}
				}
			}
			size_t z=sa.find("this");
			size_t za=sa.find("that");
			size_t zb=sa.find("the");
			if(z!=string::npos){break;}
			else if(za!=string::npos){break;}
			else if(zb!=string::npos){break;}
			}
			cout<<sa<<endl;

		}
		return 0;
	}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll income[1010011];
vector<ll>adj[1010011];
map<ll,ll>m;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll num;
    ll cur=1;
    for(ll i=1;i<=n;i++)
    {
        for(ll j=i+1;j<=n;j++)
        {
            if(i!=j)
            {
                ll mini=min(i,j);
                ll maxi=max(i,j);
                m[mini*10000+maxi]=cur;
                cur++;
            }
        }
    }
    for(ll i=1;i<=n;i++)
    {
        ll num1;
        cin>>num1;
        ll i1=m[min(i,num1)*10000+max(i,num1)];
        for(ll j=2;j<n;j++)
        {
           cin>>num;
           ll mini=min(i,num);
           ll maxi=max(i,num);
           ll i2=m[min(i,num)*10000+max(i,num)];
           //cout<<i1<<" "<<i2<<endl;
           adj[i1].push_back(i2);
           income[i2]+=1;
           //cout<<i2<<" "<<income[i2]<<endl;
           i1=i2;
        }
    }
    //cout<<income[1]<<" "<<income[2]<<" "<<income[3]<<endl;
    //for(ll i=1;i<cur;i++)
    //    cout<<income[i]<<" "<<i<<endl;;
    vector<ll>v;
    ll ans=1;
    for(ll i=1;i<cur;i++)
    {
        if(income[i]==0)
        {
            v.push_back(i);
        }

    }
    //cout<<v.size()<<endl;
    if(v.size()==0)
    {
        cout<<"-1";
        return 0;
    }
    while(1)
    {
        vector<ll>v1;
        ll flag=0;
        for(ll i=0;i<v.size();i++)
        {
            for(ll j:adj[v[i]])
            {
                income[j]-=1;
                if(income[j]==0)
                {
                    v1.push_back(j);
                    flag=1;
                }
            }
        }
        if(flag==1)
        {
            ans++;
            v.clear();
            for(ll i=0;i<v1.size();i++)
            {
                v.push_back(v1[i]);
            }
        }
        else{
            break;
        }
    }
    for(ll i=1;i<cur;i++)
    {
        if(income[i]>0)
        {
            cout<<"-1";
            return 0;
        }
    }
    cout<<ans;
	return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
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;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
vector<ll> pw2(62,1);vector<ll> pw10(19,1);

//x vs y
//の試合の数字を返す
vector<vector<ll> > memo(1001,vector<ll>(1001,INF));
ll cnt=0;
ll game(ll x,ll y){
    if(x>y) swap(x,y);

    if(memo[x][y]!=INF) return memo[x][y];
    else return memo[x][y]=++cnt;
}

vector<ll> G[500050];//辺
//v:足跡
//c:cからのdfsの終了証明
vector<ll> visited(500050);
vector<ll> calculated(500050);
//d:c済みのとき、そのdfsの深さ
vector<ll> d(500050);

//最長経路を返す,閉路あれば-1
ll dfs(ll x){
    if(visited[x]){
        //dfs終わってないくせにまた訪れた = 閉路
        if(!calculated[x]) return -1;

        else return d[x];
    }

    //あしあと
    visited[x]=true;

    //枝先のmaxをとる
    ll ret=0;
    rep(i,sz(G[x])){
        ll tmp=dfs(G[x][i]);
        //-1は即伝搬
        if(tmp==-1) return -1;

        //更新作業
        chmax(ret,tmp);
    }

    //dfs終了!
    calculated[x]=true;

    return d[x]=ret+1;
}

int main(){
    //dの初期化
    rep1(i,500049) d[i]=INF;

    //入力
    ll N; cin>>N;
    ll ans=0;
    vector<vector<ll> > A(N+1,vector<ll>(N,0));
    rep1(i,N){
        rep1(j,N-1){
            cin>>A[i][j];
        }
    }

    //初日にやる試合をリストへ
    vector<ll> dfs_list;
    //iとjが初日のペアなら初日にやって良い
    //inする
    rep1(i,N-1){
        FOR(j,i+1,N+1){
            if(A[A[i][1]][1]==i) dfs_list.pb(game(i,j));
        }
    }

    //試合の前後を表す辺を張る
    rep1(i,N){
        //iは、A[i][j]との試合の後にしかA[i][j+1]との試合をできない
        rep1(j,N-2){
            G[game(i,A[i][j])].pb(game(i,A[i][j+1]));
        }
    }

    if(sz(dfs_list)==0) cout<<-1<<endl;
    else{
        rep(i,sz(dfs_list)){
            ll tmp=dfs(dfs_list[i]);
            //閉路発見
            if(tmp==-1){
                cout<<-1<<endl;
                return 0;
            }

            chmax(ans,tmp);
        }

        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 310, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int dp[MAXN][MAXN][MAXN];
string S;

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>S>>k;
	n=S.size();
	S="."+S;
	for (int i=n; i; i--){
		for (int x=0; x<=k; x++) dp[i][i][x]=1;
		for (int j=i+1; j<=n; j++){
			dp[i][j][0]=max(dp[i+1][j][0], dp[i][j-1][0]);
			if (S[i]==S[j]) dp[i][j][0]=max(dp[i][j][0], dp[i+1][j-1][0]+2);
			for (int x=1; x<=k; x++){
				dp[i][j][x]=max(dp[i+1][j][x], dp[i][j-1][x]);
				dp[i][j][x]=max(dp[i][j][x], 2+dp[i+1][j-1][x-(S[i]!=S[j])]);
			}
		}
	}
	cout<<dp[1][n][k]<<'\n';

	return 0;
}

#include <algorithm>
#include <cctype>
#include <cstring>
#include <cstdio>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 3e2+5;
int n, m, f[N][N][N], ans; char s[N];
int main()
{
    scanf("%s", s+1), n = strlen(s+1), m = read();
    for(int i = 1; i <= n; ++i) f[0][i][i] = 1;
    for(int k = 0; k <= m; ++k)
        for(int l = 2; l <= n; ++l)
            for(int i = 1, j = l; j <= n; ++i, ++j)
            {
                f[k][i][j] = max(f[k][i+1][j], f[k][i][j-1]);
                if(s[i] == s[j]) f[k][i][j] = max(f[k][i][j], f[k][i+1][j-1]+2);
                if(k) f[k][i][j] = max(f[k][i][j], f[k-1][i+1][j-1]+2);
            }
    for(int i = 0; i <= m; ++i) ans = max(ans, f[i][1][n]);
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const LL MOD = 1000000007;

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

const int INF = INT_MAX;

PII S;
PII T;
int W, H;
int ans;

void move(VVI ts, PII p, int n) {
	if (++n > 10) {
		return;
	}

	for (int i = 0; i < 4; i++) {
		int x = p.X;
		int y = p.Y;
		bool first = true;

		while (true) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (nx < 0 || ny < 0 || nx >= W || ny >= H) {
				break;
			}
			if (first && ts[ny][nx] == 1) {
				break;
			}
			first = false;

			if (ts[ny][nx] == 3) {
				ans = min(ans, n);
				break;
			}

			if (ts[ny][nx] == 1) {
				VVI nts(ts);
				nts[ny][nx] = 0;
				move(nts, make_pair(x, y), n);
				break;
			}

			x = nx;
			y = ny;
		}
	}
}

int main(void) {
	while (cin >> W >> H, W) {
		VVI ts(H, VI(W));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> ts[i][j];
				if (ts[i][j] == 2) {
					S = make_pair(j, i);
				}
				if (ts[i][j] == 3) {
					T = make_pair(j, i);
				}
			}
		}

		VI pattern(10, 0);
		ans = INF;

		move(ts, S, 0);

		cout << (ans == INF ? -1 : ans) << endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
	ll n,t,x; cin>>n>>t;
	ll px=-1,ans=0;
	for(ll i=0;i<n;i++){
		cin>>x;
		ans+=t;
		if(px!=-1) ans-=max((ll)0,px-x+t);
		px=x;
	}
	cout<<ans;
}

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main(){
    int n,m;
    string S;
    int dice[6];
    for(int i=0;i<6;i++){
        cin>>dice[i];
    }
    cin>>S;
    for(int i=0;i<S.size();i++){
        if(S[i]=='N'){
            m=dice[0];
            dice[0]=dice[1];
            dice[1]=dice[5];
            dice[5]=dice[4];
            dice[4]=m;
        }
        else if(S[i]=='S'){
            m=dice[0];
            dice[0]=dice[4];
            dice[4]=dice[5];
            dice[5]=dice[1];
            dice[1]=m;
        }
        else if(S[i]=='W'){
            m=dice[0];
            dice[0]=dice[2];
            dice[2]=dice[5];
            dice[5]=dice[3];
            dice[3]=m;
        }
        else if(S[i]=='E'){
            m=dice[0];
            dice[0]=dice[3];
            dice[3]=dice[5];
            dice[5]=dice[2];
            dice[2]=m;
        }
        m=0;
    }
    cout<<dice[0]<<endl;
return 0;
}


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

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem0546 : Lining up the cards **/

vector<string> card;
bool used[10];
set<string> ans;
int N, K;

void solve(int n, string s)
{
	if (n==K) ans.insert(s);
	else {
		rep(i, N) {
			if (!used[i]) {
				used[i] = true;
				solve(n+1, s+card[i]);
				used[i] = false;
			}
		}
	}
}

int main()
{
	while (cin>>N, N) {
		ans.clear();
		card.clear();
		fill(used, used+10, false);

		cin>>K;
		rep(i, N) {
			string s; cin>>s;
			card.push_back(s);
		}
		solve(0, "");
		cout << ans.size() << endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010,mod=998244353;
const char str[2][10]={"First","Second"};
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline int read(){
	int x=0,f=0;char ch=getchar();
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}
int n,a[maxn];
int gcd(int x,int y){return y?gcd(y,x%y):x;}
int main(){
	n=read();
	FOR(i,1,n) a[i]=read();
	int t=0;
	while(true){
		int cnt1=0,cnte=0;
		FOR(i,1,n){
			if(a[i]==1) cnt1++;
			if(a[i]%2==0) cnte++;
		}
		if(cnte!=n-1 || cnt1==1){puts(cnte%2?str[t]:str[t^1]);break;}
		FOR(i,1,n) if(a[i]%2!=0){a[i]--;break;}
		int g=0;
		FOR(i,1,n) g=gcd(g,a[i]);
		FOR(i,1,n) a[i]/=g;
		t^=1;
	}
}
// coming soon 2020.5.11


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

class Dice {

public:

  int a1, a2, a3, a4, a5, a6;

  string D;

  Dice(int x) {

    a1 = a2 = a3 = a4 = a5 = a6 = x;

  }

  void rotateFront() {

    int newa1 = a5;

    a5 = a6;

    a6 = a2;

    a2 = a1;

    a1 = newa1;

  }

  void rotateRight() {

    int newa1 = a3;

    a3 = a6;

    a6 = a4;

    a4 = a1;

    a1 = newa1;

  }

};

int main(void) {

  Dice d1(0);

  int i;

  cin >> d1.a1 >> d1.a2 >> d1.a3 >> d1.a4 >> d1.a5 >> d1.a6;

  cin >> d1.D;

  for(i = 0; i < d1.D.size(); i++) {

    if(d1.D[i] == 'S') {

      d1.rotateFront();

    } else if (d1.D[i] == 'N') {

      d1.rotateFront();
      d1.rotateFront();
      d1.rotateFront();

    } else if (d1.D[i] == 'W') {

      d1.rotateRight();

    } else if (d1.D[i] == 'E') {

      d1.rotateRight();
      d1.rotateRight();
      d1.rotateRight();

    }

  }

  cout << d1.a1 << endl;

  return 0;

}

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

using namespace std;

class Dice
{
public:
	Dice(int num[6]);
	int Play(char c);
	int T;
private:
	int NS[4],EW[4];
	int NS_p,EW_p;
};

//?????????????????????
Dice::Dice(int num[6])
{
	NS_p = 0;
	EW_p = 0;
	NS[0] = num[0];
	NS[1] = num[1];
	NS[2] = num[5];
	NS[3] = num[4];
	EW[0] = num[0];
	EW[1] = num[3];
	EW[2] = num[5];
	EW[3] = num[2];
	T = num[0];
}

int Dice::Play(char c){

	if (c == 'N' || c == 'S'){
		if(c == 'N') NS_p = (NS_p + 1) % 4;
		else {
			if (NS_p == 0) NS_p = 3;
			else NS_p--;
		}
		EW[EW_p] = NS[NS_p];
		EW[(EW_p+2)%4] = NS[(NS_p+2)%4];
	}
	else if (c == 'E' || c == 'W'){
		if (c == 'E') EW_p = (EW_p + 1) % 4;
		else {
			if (EW_p == 0) EW_p = 3;
			else EW_p--;
		}
		NS[NS_p] = EW[EW_p];
		NS[(NS_p + 2) % 4] = EW[(EW_p + 2) % 4];
	}
	T = NS[NS_p];

	return 1;
}

int main(){

	int num[6];
	char c;

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

	Dice d1(num);

	while (cin >> c){
		d1.Play(c);
	}

	cout << d1.T << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 998244353
int f[12001];
int fi[12001];
int n, k;
int m(int a,int b){
	return (a * b)% mod;
}
int fp(int x,int y) {
	int res=1;
	while(y){
		if(y&1) res = res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
int iv(int x){
	return fp(x, mod - 2);
}
int c(int x, int y){
	// chon x tu y
	return m(f[y],m(fi[x],fi[y - x]));
}
int ck(int x,int y){
	// chia x keo cho y em
	if(x < 0) return 0;
	return c(y - 1 ,x + y - 1);
}

signed main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	f[0] = 1;
	for(int i = 1 ; i <= 10000 ; i ++) f[i] = m(f[i - 1], i);
	fi[10000] = iv(f[10000]);
	for(int i = 9999 ; i >= 0 ; i --) fi[i] = m(fi[i + 1], (i + 1));

//	int u,v;
//	while(cin >> u >> v)
//		cout << ck(u,v) << endl;
	cin >> k >> n;
	for(int i = 2 ; i <= k + k ; i ++){
		int p = 0;
		for(int j = 1 ; j <= k ; j ++){
			int _j = i - j;
			if(_j > 0 && _j != j && _j <= k) p ++;
		}
		p/= 2;
//		cout << "#i = " << i << endl;
		if(i % 2 == 1){
			int ans = 0;
			for(int q = 0; q <= p ; q ++){
//				cout << "#q = " << q << endl;
//				cout << "pair = " << c(q,p) << endl;
//				cout << "n - q = " << n - q << endl;
//				cout << "q + k - 2p = " << q + k - 2*p << endl;
//				cout << "ck = " << ck(n - q, q + k - 2*p) << endl;
				ans = (ans + m(m(c(q, p),fp(2,q)),ck(n - q, q + k - 2*p)))%mod;
			}
			cout << ans << endl;
		} else{
			int ans = 0;
			for(int q = 0; q <= p ; q ++)
				ans = (ans + m(m(c(q, p),fp(2,q)), ck(n - q - 1, q + k - 2*p - 1)))%mod;
			for(int q = 0; q <= p ; q ++)
				ans = (ans + m(m(c(q, p),fp(2,q)), ck(n - q , q + k - 2*p - 1)))%mod;
			cout << ans << endl;
		}
	}
}

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

const ll MOD = 1e9+7;

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

  vector<ll> A(N),B(M);

  map<ll,ll> mA,mB;

  for(int i = 0; i < N; i++){ cin >> A[i]; mA[A[i]]++;}
  for(int j = 0; j < M; j++){ cin >> B[j]; mB[B[j]]++;}

  for(auto ne : mA){ if(ne.second>=2){ cout<< 0 << endl; return 0;}}
  for(auto ne : mB){ if(ne.second>=2){ cout<< 0 << endl; return 0;}}

  //以後、同じ数字はA,B内に複数回現れない

  sort(A.begin(),A.end());
  sort(B.begin(),B.end());

  //でかい方から決めていく

  ll ans = 1LL;

  for(ll i = N*M; i >= 1; i--){

    ll res1 = lower_bound(A.begin(),A.end(),i)-A.begin();
    ll res2 = lower_bound(B.begin(),B.end(),i)-B.begin();

    if( A[res1] == i && B[res2] == i){ //どちらにも含まれている時
      continue;}
    else if(A[res1] == i && B[res2] != i){
      ans *= (M-res2);
      ans %= MOD;
    }
    else if(A[res1] != i && B[res2] == i){
      ans *= (N-res1);
      ans %= MOD;
    }
    else{
      if( (N-res1)*(M-res2) <= (N*M-i)){ ans = 0;}
      ans *= ((N-res1)*(M-res2)-(N*M-i));
      ans = (ans+MOD)%MOD;
    }
  }

  cout << ans << endl;

  return 0;
}


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

namespace zyt
{
	template<typename T>
	inline bool read(T &x)
	{
		char c;
		bool f = false;
		x = 0;
		do
			c = getchar();
		while (c != EOF && c != '-' && !isdigit(c));
		if (c == EOF)
			return false;
		if (c == '-')
			f = true, c = getchar();
		do
			x = x * 10 + c - '0', c = getchar();
		while (isdigit(c));
		if (f)
			x = -x;
		return true;
	}
	template<typename T>
	inline void write(T x)
	{
		static char buf[20];
		char *pos = buf;
		if (x < 0)
			putchar('-'), x = -x;
		do
			*pos++ = x % 10 + '0';
		while (x /= 10);
		while (pos > buf)
			putchar(*--pos);
	}
	typedef long long ll;
	const int N = 1e5 + 10;
	int n, m, x[N], to[N], arr[N], id[N], ans[N];
	ll k;
	void trans(int *a, const int *b)
	{
		static int tmp[N];
		for (int i = 1; i < n; i++)
			tmp[i] = a[b[i]];
		memcpy(a + 1, tmp + 1, sizeof(int[n - 1]));
	}
	int work()
	{
		read(n);
		for (int i = 0; i < n; i++)
			read(x[i]), id[i] = to[i] = i;
		for (int i = n - 1; i > 0; i--)
			x[i] -= x[i - 1];
		read(m), read(k);
		for (int i = 1; i <= m; i++)
			read(arr[i]), --arr[i];
		for (int i = 1; i <= m; i++)
			swap(to[arr[i]], to[arr[i] + 1]);
		while (k)
		{
			if (k & 1)
				trans(id, to);
			trans(to, to);
			k >>= 1;
		}
		ll last;
		write(last = x[0]), putchar('\n');
		for (int i = 1; i < n; i++)
			write(last += x[id[i]]), putchar('\n');
		return 0;
	}
}
int main()
{
	return zyt::work();
}

#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> PDL;
typedef pair<LD, LD> PDD;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
//typedef boost::multiprecision::cpp_int bigint;

template<class T>
void in(T& x) {
	cin >> x;
}

template<class T1, class T2>
void in(pair<T1, T2>& p) {
	in(p.first);
	in(p.second);
}

template<class T>
void in(vector<T>& v, LL st = -1, LL en = -1) {
	if (st == -1) {
		st = 0;
		en = v.size() - 1;
	}
	for (LL n = st; n <= en; n++) {
		in(v[n]);
	}
}

VLL mod(VLL& base, LL p) {
	LL N = base.size();
	if (p == 0) {
		VLL res(N);
		for (LL n = 0; n < N; n++)res[n] = n;
		return res;
	}
	if (p == 1)return base;
	VLL ret = mod(base, p / 2);
	VLL res(N);
	for (LL n = 0; n < N; n++) {
		res[n] = ret[ret[n]];
	}
	if (p % 2 == 0)return res;
	for (LL n = 0; n < N; n++) {
		ret[n] = res[base[n]];
	}
	return ret;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	LL N, K,M;
	cin >> N;
	VLL X(N);
	in(X);
	VLL DX(N - 1);
	for (LL n = 0; n < N - 1; n++) {
		DX[n] = X[n + 1] - X[n];
	}
	cin >> M >> K;
	VLL A(M);
	in(A);
	VLL P(N - 1);
	for (LL n = 0; n < N-1; n++) {
		P[n] = n;
	}
	for (LL m = 0; m < M; m++) {
		LL n = A[m] - 1;
		swap(P[n - 1], P[n]);
	}
	P = mod(P, K);
	VLL res(N);
	res[0] = X[0];
	for (LL n = 1; n < N; n++) {
		res[n] = res[n - 1] + DX[P[n-1]];
	}
	for (LL n = 0; n < N; n++) {
		cout << res[n] << "\n";
	}
	return 0;
}