#include <iostream>

using namespace std;
using LL = long long;

const LL INF = 1L << 32;

LL G[101][101];

int main() {

    int V, E;
    cin >> V >> E;

    //
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            if (i == j) G[i][j] = 0;
            else G[i][j] = INF;
        }
    }

    for (int i = 0; i < E; i++) {
        int s, t, d;
        cin >> s >> t >> d;
        G[s][t] = d;
    }

    //
    bool negative_cycle = false;

    for (int k = 0; k < V; k++) {
        for (int i = 0; i < V; i++) {
            if (G[i][k] == INF) continue;
            for (int j = 0; j < V; j++) {
                if (G[k][j] == INF) continue;
                G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
                if (i == j && G[i][j] < 0) {
                    negative_cycle = true;
                    goto outer;
                }
            }
        }
    }
    outer:

    //
    if (negative_cycle) {
        cout << "NEGATIVE CYCLE" << endl;
    } else {
        for (int i = 0; i < V; i++) {
            int k = 0;
            for (int j = 0; j < V; j++) {
                if (k++) cout << " ";
                cout << (G[i][j] != INF ? to_string(G[i][j]) : "INF");
            }
            cout << endl;
        }
    }
}

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

bool isSmallAlphabet(char& ch) {
  /* a ~ z(ascii) : 97 ~ 122 */
  if('a' <= ch && ch <= 'z') return true;
  else return false;
}

bool isHit(string& str) {
  if(str.find("the") != str.npos) return true;
  else if(str.find("this") != str.npos) return true;
  else if(str.find("that") != str.npos) return true;
  else return false;
}

char shiftIndex(char ch) {
  if(isSmallAlphabet(ch)) {
    ch += 1;
    /* ???????????????????°?????????§???????????£??????*/
    if(!isSmallAlphabet(ch)) {
      ch -= 26;
    }
  }
  return ch;
}

/* ????????¶??????????§£??? */
void CaesarCipher(string str) {

  /* p??????????????¢??????????????????????????????????????? */
  for(int p = 0; p < 26; ++p) {

    transform(str.begin(), str.end(), str.begin(), shiftIndex);

    /* Find "the" or "this" or "that" */
    if(isHit(str)) {
      cout << str << endl;
    }
  }
}

int main() {
  string str;
  while(getline(cin, str)) {
    CaesarCipher(str);
  }
  return 0;
}


#include <string>
#include <vector>
#include <iostream>

using namespace std;

vector<string> R;

const vector<int> dx = { 0, 1, 0, -1 };
const vector<int> dy = { -1, 0, 1, 0 };

int rec(int x, int y)
{
	R[y][x] = '0';

	for (int dir = 0; dir < 4; dir++)
	{
		if (0 <= x + dx[dir] && x + dx[dir] < 12 && 0 <= y + dy[dir] && y + dy[dir] < 12)
		{
			if (R[y + dy[dir]][x + dx[dir]] == '1')
			{
				rec(x + dx[dir], y + dy[dir]);
			}
		}
	}
}

int solve()
{
	int ret = 0;

	for (int i = 0; i < 12; i++)
	{
		for (int j = 0; j < 12; j++)
		{
			if (R[i][j] == '1')
			{
				rec(j, i); ret++;
			}
		}
	}

	return ret;
}

int main()
{
	while (true)
	{
		R = vector<string>(12);

		for (int i = 0; i < 12; i++)
		{
			if (!(cin >> R[i]))
			{
				return 0;
			}
		}

		cout << solve() << endl;
	}
}

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

typedef pair<int, int> P;

int par[16][16];
int rank[16][16];
char map[16][16];
int cnt;

void init();
void unite(int y1, int x1, int y2, int x2);
int find(int y, int x);

int main()
{
	while (scanf("%s", &map[1][1]) != EOF){
		for (int i = 2; i <= 12; i++) scanf("%s", &map[i][1]);
		init();
		for (int i = 1; i <= 12; i++){
			for (int j = 1; j <= 12; j++){
				if (map[i][j] == '1'){
					cnt++;
					unite(i, j, i - 1, j);
					unite(i, j, i + 1, j);
					unite(i, j, i, j - 1);
					unite(i, j, i, j + 1);
				}
			}
		}
		printf("%d\n", cnt);
	}

	return 0;
}

void init()
{
	for (int i = 0; i < 16; i++){
		for (int j = 0; j < 16; j++){
			par[i][j] = i * 16 + j;
			rank[i][j] = 1;
		}
	}
	cnt = 0;
}

void unite(int y1, int x1, int y2, int x2)
{
	if (map[y2][x2] == '1' && find(y1, x1) != find(y2, x2)){
		if (rank[y1][x1] < rank[y2][x2]){
			par[y1][x1] = y2 * 16 + x2;
			rank[y1][x1]++;
		}
		else {
			par[y2][x2] = y1 * 16 + x1;
			rank[y2][x1]++;
		}
		cnt--;
	}
}

int find(int y, int x)
{
	if (par[y][x] != y * 16 + x){
		par[y][x] = find(par[y][x] / 16, par[y][x] % 16);
	}
	return par[y][x];
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,int> pli;
typedef pair<int,int> pii;
#define rep(i,a,b) for(ll i=a ; i<b ; i++)
#define qrep(que, ite) for(auto ite=begin(que) ; ite!=end(que) ; ite++)
const int max_n = 1e5;
const ll mod = 1e9+7;
const ll INF = 1e17;
const int inf = 1e5;
typedef long double ld;
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; }
ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; }
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
struct UnionFind {
    vector<int> par;

    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }

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

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

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

    int size(int x) {
        return -par[root(x)];
    }
};
struct SegmentTree{
  int N;
  vector<int> node;
  public :
    void intit(vector<int>v){
      int sz = v.size();
      N=1;
      while(N<sz) N*=2;
      node.resize(N);
      for(int i=0 ; i<sz ; i++) node[i+N-1] = v[i];
      for(int i=N-2 ; i>=0 ; i--) node[i] = min(node[i*2+1], node[i*2+2]);
    }
    void update(int x, int val){
      x += N-1;
      node[x+N-1] = val;
      while(x>0){
        x = (x-1)/2;
        node[x] = min(node[x*2+1], node[x*2+2]);
      }
    }
    int getmin(int a, int b, int k, int l, int r){
      if(b<=l || r<=a) return inf;
      else if(a<=l && r<=b) return node[k];
      else{
        int vl = getmin(a, b, 2*k+1, l, (l+r)/2);
        int vr = getmin(a, b, 2*k+2, (l+r)/2, r);
        return min(vl, vr);
      }
    }
};
vector<ll> divisor(ll n){
  vector<ll> res;
  for(ll i=1; i*i<=n ; i++){
    while(n%i==0){
      ++res[i];
      n /= i;
    }
  }
  if(n!=1) res[n]=1;
  return res;
}
int main(){
  ll n; cin >> n;
  ll k = n;
  int cnt = 1;
  while(true){
    ll now = pow(26, cnt);
    if(k - now > 0){
        k -= now;
        cnt++;
    }else{
        break;
    }
  }

  string ans = "";
  k--;
  for(int i=cnt-1 ; i>=0 ; i--){
      ll now = pow(26, i);
      ll st = k / now;
      for(char c='a' ; c<='z' ; c++){
          if(c - 'a' == st){
              ans += c;
          }
      }
      k -= now * st;
  }
  cout << ans << endl;
  return 0;
}

#include<iostream>
#include<cstdlib>
#include<utility>
#include<tuple>
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
#include<queue>
#include<deque>
#include<bitset>
#include<cmath>
#include<map>
#include<iomanip>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for(ll i = a; i < b; i++)

int main() {

	ll n;
	cin >> n;
	string ans;

	while (n) {
		--n;
		ans += (char)('a' + (n % 26));
		n /= 26;
	}
	reverse(ans.begin(), ans.end());
	cout << ans << endl;

	return 0;
}


#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 101
using namespace std;
short n,m,f[N][N][N][N],sx,sy,h[N][N],L[N][N];
char s[N][N];
void upd(short &x,short y)
{
	if(x<y) x=y;
}
int main()
{
	cin>>n>>m;
	for(short i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1);
		for(short j=1;j<=m;j++) if(s[i][j]=='E') sx=i,sy=j;
	}
	for(short i=1;i<=n;i++)
		for(short j=1;j<=m;j++)
		{
			L[i][j]=L[i-1][j];
			h[i][j]=h[i][j-1];
			if(s[i][j]=='o') h[i][j]++,L[i][j]++;
		}
	short ln=m-sy,rn=sy-1,un=n-sx,dn=sx-1;
	for(short i=0;i<=ln;i++)
		for(short j=0;j<=rn;j++)
			for(short x=0;x<=un;x++)
				for(short y=0;y<=dn;y++)
				{
					short t,now,l,r;
					if(i<ln)
					{
						now=sy+i+1;t=0;
						if(now+j<=m)
						{
							l=max(sx-y,x+1);r=min(sx+x,n-y);
							if(l<=r) t=L[r][now]-L[l-1][now];
						}
						upd(f[i+1][j][x][y],f[i][j][x][y]+t);
					}
					if(j<rn)
					{
						now=sy-j-1;t=0;
						if(now-i>=1)
						{
							l=max(sx-y,x+1);r=min(sx+x,n-y);
							if(l<=r) t=L[r][now]-L[l-1][now];
						}
						upd(f[i][j+1][x][y],f[i][j][x][y]+t);
					}
					if(i==1 && j==0 && x==0 && y==1)
					{short oo=1;}
					if(x<un)
					{
						now=sx+x+1;t=0;
						if(now+y<=n)
						{
							l=max(sy-j,i+1);r=min(sy+i,m-j);
							if(l<=r) t=h[now][r]-h[now][l-1];
						}
						upd(f[i][j][x+1][y],f[i][j][x][y]+t);
					}
					if(y<dn)
					{
						now=sx-y-1;t=0;
						if(now-x>=1)
						{
							l=max(sy-j,i+1);r=min(sy+i,m-j);
							if(l<=r) t=h[now][r]-h[now][l-1];
						}
						upd(f[i][j][x][y+1],f[i][j][x][y]+t);
					}
					if(f[1][0][1][1]==4)
					{short oo=1;}
				}
	cout<<f[ln][rn][un][dn]<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int w,h;
bool isRange(int i,int j){
    return 0<=i && i<w && 0<=j && j<h;
}
int dw[]={-1,0,1,0};
int dh[]={0,1,0,-1};
int dfs(pair<int,int> p,const pair<int,int> g,vector<vector<int>>& grid,int turn){
    if(turn==11)
        return 100;
    int res=100;
    for(int i=0;i<4;i++){
        if(isRange(p.first+dw[i],p.second+dh[i]) && grid[p.first+dw[i]][p.second+dh[i]]==1)
            continue;
        for(auto j=p;isRange(j.first,j.second);j.first+=dw[i] ,j.second+=dh[i]){
            if(j==g){
                return turn;
            }
            if(grid[j.first][j.second]==1){
                grid[j.first][j.second]=0;
                j.first-=dw[i];
                j.second-=dh[i];
                res=min(res,dfs(j,g,grid,turn+1));
                grid[j.first+dw[i]][j.second+dh[i]]=1;
                break;
            }
        }
    }
    return res;
}
int main(){
    while(cin>>w>>h,w){
        pair<int,int> s;
        pair<int,int> g;
        vector<vector<int>> grid(w,vector<int>(h,0));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                int buf;
                cin>>buf;
                grid[j][i]=buf;
                if(buf==2)
                    s=make_pair(j,i);
                if(buf==3)
                    g=make_pair(j,i);
            }

        }
        int res=dfs(s,g,grid,1);
        if(res>11)
            res=-1;
        cout<<res<<endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const  ll INF = 1e9;
const  ll MOD = 1e9 + 7;
#define repi(i,n,init) for(ll i=init;i<(n);i++)

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

/*
ID: hafiz.i1
TASK: milk2
LANG: C++
*/
#include<bits/stdc++.h>

#define ll long long
#define debug(x) cout<<x<<"DE"<<endl;

using namespace std;

int main()
{
//    freopen("milk2.in", "r", stdin);
//    freopen("milk2.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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


#include <iostream>
#include <vector>
using namespace std;
int main(){
  int n,t;
  cin >> n >> t;
  vector<int> v(n);
  int i;
  for(i=0;i<n;i++) cin >> v[i];
  long long cnt = 0;
  for(i=0;i<n-1;i++){
    if(v[i+1]-v[i]<=t) cnt+= v[i+1]-v[i];
    else cnt += t;
  }
  cnt += t;
  cout << cnt << "\n";
  return 0;
}

#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
const ll mod = 1000000007;
const int INF = 1001001001;

int main(){
    int n, m;
    cin >> n >> m;
    int arr[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
    vector<int> a(m);
    rep(i, m) cin >> a[i];
    sort(whole(a));
    reverse(whole(a));
    vector<int> x(m);
    rep(i, m) x[i] = arr[a[i]-1];

    vector<P> dp(n+1, P(-INF, -1));
    dp[0] = P(0, -1);
    rep(i, n+1) {
        for (int j=0; j<x.size(); j++) {
            int e = x[j];
            if (i-e>=0 && dp[i-e].first>=0) {
                if (dp[i].first<dp[i-e].first+1) {
                    dp[i].first = dp[i-e].first + 1;
                    dp[i].second = a[j];
                }
            }
        }
    }
    int keta = dp[n].first;
    int index = n;
    map<int, int> mp;
    while (index) {
        mp[dp[index].second]++;
        index -= arr[dp[index].second-1];
    }
    string ans;
    for (auto m: mp) {
        rep(i, m.second) {
            ans += m.first+'0';
        }
    }
    reverse(whole(ans));

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


#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()

int main() {
    long long ans=0,n,a,b;
    cin>>n>>a>>b;
    vector<long long>p(n);
    rep(i,n)cin>>p[i];
    rep(i,n-1){
        ans+=min(a*(p[i+1]-p[i]),b);
    }
    cout<<ans;
	return 0;
}

#include <cstdio>
#include <algorithm>

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

using namespace std;

int x[6];

int main(){
	rep(i, 6){
		scanf("%d", &x[i]);
	}

	int s = 0;
	rep(i, 6){
		s += x[i];
	}
	printf("%d\n", s - *min_element(x, x + 4) - min(x[4], x[5]));
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int N;
int sum = 0;
int main()
{
    cin >> N;
    vector<int> L(2 * N);

    for (int i = 0; i < 2 * N; i++)
    {
        cin >> L[i];
    }
    sort(L.begin(), L.end());
    for (int i = 0; i < 2 * N; i += 2)
    {
        sum += L[i];
    }
    cout << sum << endl;
    return 0;
}


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

typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<bool> > vvb;
typedef map<int, bool> mib;

typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long,long long> pll;
typedef vector<pair<long long,long long> > vpll;

typedef vector<string> vs;

typedef long double ld;

#define _GLIBCXX_DEBUG

#define REP(i,n) for (int i = 0; i < (n); ++i)
#define REPD(i,n) for (int i = (n-1); i >= 0; --i)
#define FORE(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORED(i, s, n) for (int i = (s); i >= (int)(n); i--)
#define debug(x) cerr << #x << ": " << x << '\n'
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << '\n'
#define debug3(x, y, z) cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << '\n'
#define debugs(s) cerr << s << "\n"
#define hyphen() cerr << "--\n"

#define ALL(vec) (vec).begin(), (vec).end()
#define REVALL(vec) (vec).rbegin(), (vec).rend()

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

#define fst first
#define snd second
#define pb push_back
#define mk(x,y) make_pair((x),(y))

// http://ehafib.hatenablog.com/entry/2015/12/23/164137
int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a;a=1ll*a*a;}while(b>>=1);return ans;}
int qpm(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
// gcd : O(logN)
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

#define sz(v) int(v.size())

// x1, x2ライブラリとの被りを避ける
#define x1 x192837465
#define x2 x123456789
#define y1 y192837465
#define y2 y123456789

// typo
#define itn int
#define reutrn return
#define reutnr return
#define srting string

#define make(type, x) type x; cin>>x;
#define make2(type, x, y) type x, y; cin>>x>>y;
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z;
#define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t;

int ctoi(const char c){
    if('0' <= c && c <= '9') return (c-'0');
    return -1;
}

#define uniq(v) (v).erase(unique(ALL((v))), (v).end());

// area of (0, 0)(a, b)(c, d)
double area(double a, double b, double c, double d){
    return abs(a * d - b * c) / 2.0;
}

const int PREC = 10;
#define coutf(val) cout << fixed << setprecision(PREC) << (val) << endl;

const int MOD = (int)1e9 + 7;
const int INF = numeric_limits<int>::max();

string sweep(const string S) {
    // STの組を一掃する
    int scnt = 0;
    int tcnt = 0;

    string ans = "";

    for(auto c : S) {
        if (c == 'S') {
            scnt++;
        } else {
            // c == 'T'

            // Sが残っている場合は消費
            if (scnt > 0) {
                scnt--;
            } else {
                // 消し尽くしている場合はansに追加
                ans += 'T';
            }
        }
    }

    // 残っているscntを清算
    ans += string(scnt, 'S');

    return ans;
}

signed main() {
    make(string, S);

    debug(S);
    S = sweep(S);
    debug(S);

    int ans = S.size();
    cout << ans << endl;
}

#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    ll n = s.size();
    ll S = 0, P = 0;
    rep(i,n){
        if(s[i] == 'S') ++S;
        else{
            if(S > 0){
                ++P;
                --S;
            }
        }
        chmax(S, 0LL);
    }
    ll ans = n - P*2;
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>
#include<cstring>
#include<bitset>
#include<cassert>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++)
#define all(x) (x).begin(),(x).end()

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;

void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(vector<long long int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}

//ワーシャルフロイト法のテンプレ.graphに最短経路が格納される.
//負の重みがある場合inf + const = inf に気を付けること.
//graphは隣接行列.
void WF(vector<vector<int>> &graph){
	int n = graph.size();
	for (int k = 0; k < n; k++){
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
			}
		}
	}
}

//素因数分解をする関数.
map<long long int, int> prime_factorization(long long int a)
{
	map<long long int, int>ans;
	for (long long i = 2; i*i <= a; ++i) {
		while (a%i == 0) {
			ans[i]++;
			a /= i;
		}
	}
	if (a != 1)ans[a]++;
	return ans;
}

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

	return 0;

}

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include <sstream>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include <iomanip>
#include <numeric>
#include <iterator>
#include <math.h>
#include <list>
using namespace std;

int main(void){
  long x;
  cin >> x;
  long neko = x / 11;
  long amari = x % 11;
  cout << neko*2 + ((amari == 0) ? 0 :((amari > 6)? 2 : 1 ))<< endl;
  return 0;
}


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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

struct FenwickTree{
	vector<ll> data;
	FenwickTree(int n):data(n+1){}
	void Add(int i,int x){
		for(i++;i<data.size();i+=i&-i)
			data[i]+=x;
	}
	ll Sum(int i){
		ll res=0;
		for(;i;i-=i&-i)
			res+=data[i];
		return res;
	}
	ll Sum(int i,int j){
		return Sum(j)-Sum(i);
	}
};

ll InversionNumber(const vi& a)
{
	FenwickTree ft(a.size());
	ll res=0;
	rep(i,a.size()){
		res+=ft.Sum(a[i],a.size());
		ft.Add(a[i],1);
	}
	return res;
}

bool test(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int n=src.size();
	rep(i,n){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):mt(z,y,x);
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):mt(z,y,x);
	}
	sort(all(src)); sort(all(dst));
	return src==dst;
}

tuple<bool,bool> calc(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int m=src.size(),flip=0;
	rep(i,m){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
	}
	map<tuple<int,int,int>,int> f;
	rep(i,m) f[dst[i]]=i;
	vi a(m);
	rep(i,m) a[i]=f[src[i]];
	return mt(InversionNumber(a)&1,flip&1);
}

bool solve(int n,vvi grid)
{
	array<bool,2> inv,flip;
	rep(i,2){
		vector<tuple<int,int,int>> src,dst;
		rep(j,n) if(j%2==i){
			if(j/2%2==0){
				src.emplace_back(mt(3*j+1,3*j+2,3*j+3));
				dst.emplace_back(mt(grid[0][j],grid[1][j],grid[2][j]));
			}
			else{
				src.emplace_back(mt(3*j+3,3*j+2,3*j+1));
				dst.emplace_back(mt(grid[2][j],grid[1][j],grid[0][j]));
			}
		}
		if(!test(src,dst)) return false;
		tie(inv[i],flip[i])=calc(src,dst);
	}
	return inv[0]==flip[1]&&inv[1]==flip[0];
}

int main()
{
	for(int n;cin>>n&&n;){
		vvi grid(3,vi(n));
		rep(i,3) rep(j,n) cin>>grid[i][j];
		cout<<(solve(n,grid)?"Yes":"No")<<endl;
	}
}

/*
Author: QAQ-Automaton
LANG: C++
PROG: e.cpp
Mail: cnyalilk@vip.qq.com
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const signed inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	inline void read (signed &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}

	inline void read (long long &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}
	inline void read (char &x) {
		x=gc();
	}
	inline void read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r');
		while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc();
		*x=0;
	}
	template<typename A,typename ...B>
	inline void read(A &x,B &...y){
		read(x);read(y...);
	}
	// print a signed integer
	inline void write (signed x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}

	inline void write (long long x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	inline void write (char x) {
		putc(x);
	}
	inline void write(const char *x){
		while(*x){putc(*x);++x;}
	}
	inline void write(char *x){
		while(*x){putc(*x);++x;}
	}
	template<typename A,typename ...B>
	inline void write(A x,B ...y){
		write(x);write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int a[100005][4],qaq[2];
int n;
struct BIT{
	int s[100005];
	void add(int x){
		for(;x<=n;x+=x&-x)s[x]^=1;
	}
	int sum(int x){
		int y=0;for(;x;x^=x&-x)y^=s[x];return y;
	}
};
BIT bit[2];
void gg(){write("No\n");exit(0);}
int main(){
#ifdef QAQAutoMaton
	freopen("e.in","r",stdin);
	freopen("e.out","w",stdout);
#endif
	read(n);
	for(int j=1;j<=3;++j)
	for(int i=0;i<n;++i){read(a[i][j]);--a[i][j];}
	for(int i=0;i<n;++i)
		if(a[i][1]/3 != a[i][2]/3 || a[i][2]/3 != a[i][3]/3){
			gg();
		}
		else if((a[i][1]/3-i)&1){
			gg();
		}
		else if(a[i][2]%3 !=1){
			gg();
		}
		else{
			qaq[i&1]^=a[i][1]%3/2;
			qaq[(i&1)^1]^=bit[i&1].sum(n-a[i][1]/3);
			bit[i&1].add(n-a[i][1]/3);
		}
	write(qaq[0]||qaq[1]?"No\n":"Yes\n");
	return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

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

    int a, b, c;
    a = 0;
    b = n/2;
    c = n;
    vector<char> st;
    printf("%d\n", 0);
    fflush(stdout);
    string S;
    cin >> S;
    if(S[0] == 'V') return 0;
    st.push_back(S[0]);

    printf("%d\n", b);
    fflush(stdout);
    cin >> S;
    if(S[0] == 'V') return 0;
    st.push_back(S[0]);

    rep(i, 18) {
        if((((b-a)&1) && st[0] != st[1]) || (!((b-a)&1) && st[0] == st[1])) {
            a = b;
            b = (c+b)/2;
            st[0] = st[1];
        } else {
            c = b;
            b = (c+a)/2;
        }
        printf("%d\n", b);
        fflush(stdout);
        cin >> S;
        if(S[0] == 'V') return 0;
        st[1] = S[0];
    }

}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

int main() {
	int n;
	cin >> n;
	cout << 0 << endl;
	string s;
	cin >> s;
	if (s == "Vacant") return 0;
	vector<string> v(n);
	v[0] = s;
	int l = 0;
	int r = n;
	int p = (l+r)/2;
	cout << p << endl;
	while (cin >> s, s != "Vacant") {
		v[p] = s;
		int x = p - l - 1;
		int y = r - p - 1;
		if ((v[l] == s && x % 2 == 0) || (v[l] != s && x % 2 == 1)) {
			r = p;
			p = (l+r)/2;
		} else {
			l = p;
			p = (l+r)/2;
		}
		cout << p << endl;
	}
}


#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <math.h>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;

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

class Factorial {
    public:
      ll modd = 1000 * 1000 * 1000 + 7;
      vector<ll> bin;

      Factorial(int n) : bin(n+1, 0) {
          bin[0] = 1;
          forr(i,1,n) {
              bin[i] = i*bin[i-1]; bin[i] = bin[i] % modd;
          }
      }

      ll val(int i) {  return bin[i];  }
};

int main()   {
    cout.precision(17);
    ll modd = 1000 * 1000 * 1000 + 7;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no
    ll infinit = 10000000000000000;

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n);
        readstring(s);
        ll ret = 1;
        ll continuing_intervals = 0;
        forr(i,0,s.size()) {
            bool need_one = (s[i] == 'B');
            if (need_one == continuing_intervals % 2) {
                ret *= continuing_intervals;
                ret = ret % modd;
                continuing_intervals--;
                if (continuing_intervals < 0) { ret = 0; break;  }
            } else {
                continuing_intervals++;
            }
        }
        if (continuing_intervals != 0) {ret = 0;}
        Factorial fa(n+1);
        ret *= fa.val(n); ret = ret % modd;

        cout << ret << endl;
    }
    return 0;
}

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& a) {
  os << "{";
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i];
  return os << "}";
}

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

template <typename T, typename C>
ostream& operator<<(ostream& os, const multiset<T, C>& v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}
template <typename T1, typename T2, typename C>
ostream& operator<<(ostream& os, const map<T1, T2, C>& mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin()) os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

int main() {
  int h, w;
  cin >> h >> w;
  // 右へ動く最小値を保管
  // ある点から始まった場合、
  // 複数の最短経路結果を持ちたい。
  //

  map<int, int> mp;
  multiset<int> st;
  rep(i, 0, w) {
    mp[i] = i;
    st.insert(0);
  }

  rep(i, 0, h) {
    int a, b;
    cin >> a >> b;
    --a;
    // [a, b]はいけない
    auto itr = mp.lower_bound(a);
    int most_right = -1;
    while (itr != mp.end() && itr->first <= b) {
      most_right = max(most_right, itr->second);
      int x = itr->first - itr->second;
      st.erase(st.find(x));
      itr = mp.erase(itr);
    }

    if (most_right != -1 && b < w) {
      st.insert(b - most_right);
      mp[b] = most_right;
    }
    if (st.size() > 0)
      cout << *st.begin() + i + 1 << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}


#include<stdio.h>
int d,t,s;
int main()
{
	scanf("%d %d %d", &d, &t, &s);
	d=d+s-1;
	if(t>=(d/s))printf("Yes");
	else printf("No");
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int d,t,s;
  cin>>d>>t>>s;
  float time = (d/(s*1.0));
  if(time<=t)
    cout<<"Yes"<<endl;
  else
    cout<<"No"<<endl;
return 0;
}


#include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

signed main(void){
  int i, j, k;
  for(int testcase = 0;testcase >= 0;testcase++){
    int2(n, m);
    int a[n], cnt[n];
    scn(n, a);
    rep(i, n)cnt[i] = 0;

    rep(i, m){
      int(b);
      rep(j, n)if(b >= a[j]){
        cnt[j]++; break;
      }
    }

    int ma = 0;
    rep1(i, n)if(cnt[ma] < cnt[i])ma = i;
    pri(ma + 1);

/*/

//*/ break;
  }
  return 0;
}

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

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

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

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

  int n,m;
  cin >> n >> m;
  vector<int>a(n);
  rep(i,n) cin >> a[i];
  vector<int>ans(n);
  rep(i,m){
    int x;
    cin >> x;
    ll kyogi=n;
    rep(j,n){
      if(a[j]<=x){
        kyogi=min(kyogi,j);
      }
    }
    ans[kyogi]++;
  }
  cout << max_element(ans.begin(),ans.end())-ans.begin()+1 << endl;

}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
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;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
typedef pair<ll,ll> l_l;
typedef pair<int,int> i_i;
const ll mod=1e9+7;
const int inf=1<<30;

vector<vector<int> > to;
int color[100010];
bool isbi=true;
void dfs(int v,int c=0){
  color[v]=c;
  rep(i,to[v].size()){
    int nv=to[v][i];
    if(color[nv]==-1) dfs(nv,1-c);
    else if(color[nv]==1-c) continue;
    else isbi=false;
  }
}

int main(){
  ll N,M;
  cin >> N >> M;
  to.resize(N);
  rep(i,N) color[i]=-1;
  rep(i,M){
    int a,b;
    cin >> a >> b;
    a--; b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  dfs(0);
  ll ans;
  if(isbi){
    ll b=0,w=0;
    rep(i,N){
      if(color[i]==0) b++;
      else w++;
    }
    ans=b*w-M;
  }
  else{
    ans=N*(N-1)/2-M;
  }
  cout << ans << endl;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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

    template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

template< typename Monoid >
struct SegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;

    int sz;
    vector< Monoid > seg;

    const F f;
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
        sz = 1;
        while(sz < n) sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x) {
        seg[k + sz] = x;
    }

    void build() {
        for(int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    void update(int k, const Monoid &x) {
        k += sz;
        seg[k] = x;
        while(k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }

    Monoid query(int a, int b) {
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) L = f(L, seg[a++]);
            if(b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const {
        return seg[k + sz];
    }
};

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    int N,Q; cin >> N >> Q;
    VI C(N);
    REP(i,N) {
        cin >> C[i];
        C[i]--;
    }
    vector<tuple<int,int,int>> RL;
    REP(i,Q){
        int l,r; cin >> l >> r;
        l--; r--;
        RL.eb(r,l,i);
    }
    sort(RL.begin(),RL.end());

    VI memo(N,-1); // その色が最後に出たindex
    VI ans(Q);
    auto f = [](int a, int b){return a+b;};
    SegmentTree<int> seg(N,f,0); // 一番右側の色

    int cur = -1;
    REP(i,Q){
        int l,r,ind;
        tie(r,l,ind) = RL[i];
        while(cur < r){
            cur++;
            if(memo[C[cur]] == -1){
                seg.update(cur,1);

            }else{
                seg.update(memo[C[cur]], 0);
                seg.update(cur,1);
            }

            memo[C[cur]] = cur;
        }

        ans[ind] = seg.query(l,r+1);

    }

    REP(i,Q){

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

}


//O(n^2)
#include<stdio.h>
#include<math.h>

long long n,rui[10000];

long long max( long long x,long long y ){
	if( x > y )
		return x;
	return y;
}

int main()
{
	long long i,j;
	long long ans;
	while( scanf("%lld",&n) != EOF )
	{
		if( n == 0 )break;
		ans = -10000000000;
		for( i = 0;i < n;i++ ){
			scanf("%lld",rui+i );
			if( i )rui[i] += rui[i-1];
		}

		for( i = 0;i < n;i++ ){
			for( j = i;j < n;j++ ){
				if( i )
					ans = max( ans, rui[j]-rui[i-1] );
				else
					ans = max( ans,rui[j] );
			}
		}

		printf("%lld\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int N, M;
vector<int> ItemInfo, PersonInfo;

void solve() {
	int curValue;
	int total[1000];

	memset(total, 0, sizeof(total));
	for (int i = 0; i < M; ++i) {
		curValue = PersonInfo[i];
		for (int j = 0; j < N; ++j) {
			if (ItemInfo[j] <= curValue) {
				//cout << "=" << j + 1 << endl;
				++total[j];
				break;
			}
		}
	}

	cout << max_element(total, total + N) - total + 1 << endl;
}

int main() {
	int num;

	cin >> N >> M;
	for (int i = 0; i < N; ++i) {
		cin >> num;
		ItemInfo.push_back(num);
	}
	for (int i = 0; i < M; ++i) {
		cin >> num;
		PersonInfo.push_back(num);
	}
	solve();

	return 0;
}


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

int main(){

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

}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
	int N, X, T;
	cin >> N >> X >> T;
	int S = 0;
	S += N / X * T;
	if (N % X >= 1) {
		S += T;
	}
	cout << S << endl;
}


#include<iostream>

const int INF = 1 << 24, MAX_N = 101;
int n, cost[MAX_N][MAX_N], d[MAX_N], used[MAX_N];

int dijkstra(int s, int g){
	for(int i=1;i<=n;i++){
		d[i] = INF;
		used[i] = false;
	}
	d[s] = 0;

	while(true){
		int v = -1;
		for(int i=1;i<=n;i++){
			if(!used[i] && (v == -1 || d[i] < d[v])){
				v = i;
			}
		}

		if(v == -1)break;
		used[v] = 1;

		for(int i=1;i<=n;i++){
			d[i] = std::min(d[i], d[v] + cost[v][i]);
		}
	}

	return d[g];
}

int main(){
	int k;
	while(std::cin >> n >> k, n){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				cost[i][j] = INF;
			}
		}

		while(k--){
			int f, c, d, e;
			std::cin >> f;
			if(f == 0){//注文が入りましたよー
				std::cin >> c >> d;
				int res = dijkstra(c, d);
				if(res == INF){
					std::cout << -1 << std::endl;
				}else{
					std::cout << dijkstra(c, d) << std::endl;
				}
			}else{//運行情報
				std::cin >> c >> d >> e;
				cost[c][d] = std::min(cost[c][d], e);
				cost[d][c] = cost[c][d];
			}
		}
	}
}

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

#define ll long long
#define fr first
#define sc second
#define ii pair < ll, ll >

const int N = (int)5e3 + 7;
const ll inf = (ll)1e18 + 7;

ii p[N];
ll dp[N];

bool comp (const ii &a, const ii &b) {
    return a.fr + a.sc < b.fr + b.sc;
}

main() {
    int n; scanf ("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf ("%lld %lld", &p[i].fr, &p[i].sc);
    }
    sort(p, p + n, comp);
    for (int i = 0; i <= n; i++) {
        dp[i] = inf;
    }
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j >= 0; j--) {
            if (p[i].fr >= dp[j]) {
                dp[j + 1] = min(dp[j + 1], dp[j] + p[i].sc);
            }
        }
    }
    int m = n;
    while (dp[m] == inf) m--;
    printf ("%d", m);
}


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

#define rep(i,n) for(ll i = 0; i < n; i++)
#define Rep(i,n) for(ll i = 1; i < n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int main(){

 	ll n, a, b;
 	cin >> n >> a >> b;
 	V<ll> x(n);
 	rep(i,n) cin >> x[i];

 	V<ll> dist(n-1);
 	rep(i,n) {
 		dist[i] = x[i+1] - x[i];
 		dist[i] *= a;
 	}

 	sort(all(dist));
 	int idx = 0;
 	ll ans = 0;
 	while(dist[idx] < b && idx < n-1) {
 		ans += dist[idx];
 		idx++;
 	}

 	ans += 1LL * (n-1 - idx) * b;
 	cout << ans << endl;
}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <string>
#include <math.h>
#include <cstdlib>
#include <istream>
#include <sstream>
#include <cctype>
#include <iomanip>
//cout << fixed << setprecision(20) << *** << endl;
#define rep(i,N) for(int i=0;i<(int)N;++i)
typedef long long ll;
using namespace std;
int main()
{
	ll A,B,M,t;
	vector<ll>AA,BB,ANS;
	cin>>A>>B>>M;
	rep(i,A){
		cin>>t;
		AA.push_back(t);
	}
	rep(i,B){
		cin>>t;
		BB.push_back(t);
	}
	rep(i,M){
		ll x,y,z,zz;
		cin>>x>>y>>z;
		zz=AA[x-1]+BB[y-1]-z;
		ANS.push_back(zz);
	}
	sort(AA.begin(),AA.end());
	sort(BB.begin(),BB.end());
	ANS.push_back(AA[0]+BB[0]);
	sort(ANS.begin(),ANS.end());
	cout<<ANS[0]<<endl;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int p[3];
void judge(){
  REP(j,3){
    if(p[j]==100){
      cout<<"A"<<endl;
      return;
    }
  }
  if(p[0]+p[1]>=180){
    cout<<"A"<<endl;
    return;
  }
  int sum=0;
  REP(j,3)sum+=p[j];
  if(sum>=240){
    cout<<"A"<<endl;
    return ;
  }
  if(sum>=210){
    cout<<"B"<<endl;
    return ;
  }
  if(sum>=150&&(p[0]>=80||p[1]>=80)){
    cout<<"B"<<endl;
    return ;
  }
  cout<<"C"<<endl;
}
int main(void){
  int n;
  while(cin>>n){
    if(n==0) break;
    REP(i,n){
      REP(j,3)cin>>p[j];
      judge();
    }
  }

  return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

bool Bsearch(int key, int A[], int len) {
	int l = 0, r = len - 1;
	while (l <= r) {
		int mid = l + (r - l) / 2;
		if (A[mid] > key)
			r = mid - 1;
		else if (A[mid] < key)
			l = mid + 1;
		else
			return true;
	}
	return false;
}

int main() {
	int n, *S;
	int q, *T;
	int ans = 0;
	cin >> n;
	S = new int[n];
	for (int i = 0; i < n; i++)
		cin >> S[i];
	cin >> q;
	T = new int[q];
	for (int i = 0; i < q; i++)
		cin >> T[i];

	sort(S, S + n);
	for (int i = 0; i < q; i++)
		for (int j = 0; j < n; j++)
			if (Bsearch(T[i], S, n) == true) {
				ans++;
				break;
			}

	cout << ans << endl;
	delete[] S;
	delete[] T;
	return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <fstream>
#include <cmath>
#include <random>
#include <complex>
#include <functional>
#include <bitset>
#include <sstream>
#include <cassert>
#include <time.h>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template<class T> inline void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}
template<class T> inline void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}
void printvec (vec &v) {
    Rep (i, (ll)v.size()) {
        cout << v[i] << " \n"[i+1 == (ll)v.size()];
    }
}

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

    ll N;
    cin >> N;
    vec A(N);
    Rep (i, N) {
        cin >> A[i];
    }

    ll ans = 0;
    Rep (i, 60) {
        ll cnt = 0;
        Rep (j, N) {
            if ((A[j] >> i) & 1) {
                cnt++;
            }
        }

        if (cnt % 2 == 1) {
            // cout << "i=" << i << "\n";
            ans += (1LL << i);
            Rep (j, N) {
                A[j] &= ~(1LL << i);
            }
        }
    }

    // printvec(A);

    vec basis;
    for (ll e : A) {
        for (ll b : basis) {
            chmin(e, e ^ b);
        }
        if (e) {
            basis.push_back(e);
        }
    }

    sort(basis.begin(), basis.end());

    ll n = basis.size();
    Rep (i, n) {
        Rep (j, n) {
            if (i == j) continue;
            chmin(basis[j], basis[j] ^ basis[i]);
        }
    }

    // printvec(basis);

    ll tmp = 0;
    for (ll e : basis) {
        tmp = tmp ^ e;
    }

    ans += 2 * tmp;
    cout << ans << "\n";

    // for (ll i = n-1; i >= 0; i--) {
    //     ans += 2 * basis[i];
    //     for (ll digit = 59; digit >= 0; digit--) {
    //         if (basis[i] >> digit & 1) {
    //             for (ll j = i-1; j >= 0; j--) {
    //                 if (basis[j] >> digit & 1) basis[j] = 0;
    //             }
    //         }
    //     }
    // }

    // cout << ans << "\n";

}


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

int main() {
  ll N;
  cin >> N;
  string ret;
  vector<string> seats(N, "");
  cout << 0 << endl;
  cin >> ret;
  if (ret == "Vacant") return 0;
  seats[0] = ret;
  cout << N - 1 << endl;
  cin >> ret;
  if (ret == "Vacant") return 0;
  seats[N - 1] = ret;
  int l = 0, r = N - 1;
  int mid;
  while (true) {
    mid = (l + r) / 2;
    cout << mid << endl;
    cout << flush;
    cin >> ret;
    if (ret == "Vacant") {
      return 0;
    } else {
      seats[mid] = ret;
      if ((((mid - l) % 2 == 1) && seats[l] == seats[mid]) ||
          (((mid - l) % 2 == 0) && seats[l] != seats[mid])) {
        r = mid;
      } else {
        l = mid;
      }
    }
  }
  return 0;
}

#include <iostream>
using namespace std;

int main(){
	int n, a, b;
	while (true)
	{
		int point_a = 0, point_b = 0;
		cin >> n;
		if (cin.eof()||n==0) { break; }
		for(int i =0;i<n;i++){
			cin >> a >> b;
			if(a==b){
				point_a += a;
				point_b += b;
			}
			else if (a>b){
				point_a += a + b;
			}
			else {
				point_b += a + b;
			}
		}
		cout << point_a << " " << point_b << endl;
	}
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 120000;
int gcd(int a, int b){return !b?a:gcd(b,a%b);}
int n,a[N];
void print(int x){
	printf("%s",x==1?"First":"Second");exit(0);
}
int main() {
	read(n);rep(i,1,n)read(a[i]);
	int now=1;
	while(1){
		int cnt=0,flag=1;
		rep(i,1,n)cnt+=!(a[i]&1),flag&=a[i]==1;
		if(flag)print(3-now);
		if(cnt&1)print(now);
		if(n-cnt!=1)print(3-now);
		rep(i,1,n)if(a[i]&1){if(a[i]==1)print(3-now);a[i]--;}
		int g=0;rep(i,1,n)g=gcd(g,a[i]);
		rep(i,1,n)a[i]/=g;now=3-now;
	}
	return 0;
}

// C++ 14
#include <bits/stdc++.h>
using namespace std;
template <typename T>  ostream& operator<<(ostream& os, const vector<T>& v)  {  os << "[";  for (int i = 0; i < v.size(); ++i) {  os << v[i];  if (i != v.size() - 1)  os << ", ";  }  os << "]";  return os;  }
template <typename T> void print(T v, string s = "\n") { cout << v << s; }
template <typename T> void in(T &v) { cin >> v; }
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)

int main() {
  int n; ll m; in(n),in(m);
  vector<ll> A(n);
  loop(i,0,n) in(A[i]);
  loop(i,0,n) A[i]%=m;
  ll rem = 0;
  map<ll, ll> C;
  C[rem]++;
  loop(i,0,n) {
    rem = (rem + A[i]) % m;
    C[rem]++;
  }
  ll ans = 0;
  for (auto &&c: C) {
    ans += (c.second * (c.second - 1)) / 2;
  }
  print(ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
   ll r;cin>>r;
   if(r<1200)puts("ABC");
   else if(r<2800)puts("ARC");
   else puts("AGC");
   return 0;
}

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG //FOR THE DEBUG! COMMENT OUT THIS WHEN SUBMITTING!
#endif
/* I REALLY HOPE MY WISH REACH YOU , ATCODER'S ONLINE JUDGE */
#define WOULD
#define YOU
#define PLEASE
#define ACCEPT
#define MY
#define SUBMISSION
/* I REALLY HOPE MY WISH REACH YOU , ATCODER'S ONLINE JUDGE */
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip>
#include <limits>//setprecision
//#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
//It is so troublesome that I include bits/stdc++.h !
using namespace std;

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 long long INF = 100100100100;
const long long MOD = 1000000007;
typedef pair<int,int> P;

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % MOD, M / 2);
    //最下位ビット(*N)が1の時は単独でNをかける
    if (M & 1) res = (res * N) % MOD;

    return res %= MOD;
}

long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a * b / gcd(a, b);
}

int main() {
    string S;cin >> S;
    if(S == "zyxwvutsrqponmlkjihgfedcba"){
        cout << -1 << endl;
        return 0;
    }
    char C;
    int index;
    vector<bool> kind(26,false);
    for(int i = 0;i<S.size();i++){
        C = S.at(i);
        index = C-97;
        kind.at(index) = true;
    }
    if(S.size() != 26){
        for(int i = 0;i<26;i++){
            if(!kind.at(i)){
                char plus = i+97;
                cout << S + plus << endl;
                return 0;
            }
        }
    }
    for(int i = S.size()-1;i>=0;i--){
        C = S.at(i);
        index = C-97;
        for(int j = 0;j<26;j++){
            if(!kind.at(j) && j > index){
                S.pop_back();
                char last = j+97;
                S += last;
                cout << S << endl;
                return 0;
            }
        }
        S.pop_back();
        kind.at(index) = false;
    }
}


#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>

using namespace std;

int main(void)
{
    int l[10];
    int a,b;
    while(~scanf("%d,", &l[0])) {
        double sum = 0;
        for(int i=1; i<10; i++) scanf("%d,", &l[i]);
        scanf("%d,%d",&a,&b);
        for(int i=0; i<10; i++) sum += (double)l[i];

        double c=0, d=0;
        int i;
        for(i=0; i<10; i++) {
            c += l[i];
            d += (double)l[i]/a * b;
            if(c + d >= sum) break;
        }

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

    return 0;
}

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	int a[10];
	int v1,v2;
	while(scanf("%d",a)!=EOF){
		for(int i=1;i<10;i++){
			scanf(",%d",&a[i]);
			a[i] += a[i-1];
		}
		scanf(",%d,%d\n",&v1,&v2);
		int ans;
		for(int i=9;i>=0;i--){
			if(v1*a[9] <= a[i]*(v1+v2))ans=i;
		}
		cout<<ans+1<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
  {int N, K; cin >> N >> K;
  int L[N]; for (int &l : L) cin >> l; sort(L, L + N);
  cout << accumulate(L + N - K, L + N, 0);}

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

#define PB push_back
#define PF push_front
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vpi vector<pii>
#define vll vector<ll>
#define vpl vector<pll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pr_d(x) cout << fixed << setprecision(15) << x << endl
#define ud(c, x) distance(c.begin(), upper_bound(all(c), x))
#define ld(c, x) distance(c.begin(), lower_bound(all(c), x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep3(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep4(i, a, b) for (int i = (a); i > (b); --i)
#define pb push_back
#define out(x) cout << x << "\n"
bool odd(int i) { return i % 2; }

#define all(v) v.begin(), v.end()
#define size(x) int(x.size())

int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
void Yes_No(bool f) {
  if (f)
    printf("Yes\n");
  else
    printf("No\n");
}
void YES_NO(bool f) {
  if (f)
    printf("YES\n");
  else
    printf("NO\n");
}

template <typename T>
void deb1(T x) {
  cout << x << "\n";
}

template <typename T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//-------------------ここから回答する-----------------------

void solve(void) {
  int n, k;
  cin >> n >> k;
  vi l(n);
  rep(i, n) cin >> l[i];
  sort(all(l), greater<int>());
  int ans = 0;
  rep(i, k) ans += l[i];
  out(ans);
}
int main(void) { solve(); }


#include <bits/stdc++.h>

using namespace std;

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

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 long long INF = 1LL << 60;
#define PI 3.14159265359

template<typename T>
void remove(std::vector<T>& vector, unsigned int index)
{
    vector.erase(vector.begin() + index);
}

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

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

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

    if(A>B)cout << A+(A-1) << endl;
    else if (A==B)cout << A+B << endl;
    else cout << B+B-1 << endl;
}

#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define SORT(v) sort(ALL(v))
//#define scanf scanf_s

int main()
{
	int n, p;
	while (cin >> n >> p, n)
	{
		int m[55] = {};
		int wan = p;
		int i, j;
		j = 0;
		for (i = 1; i < 1000005; i++)
		{
			if (wan>0)
			{
				wan--;
				m[j]++;
				if (m[j] == p)
				{
					cout << j << endl;
					break;
				}
			}
			else
			{
				wan += m[j];
				m[j] = 0;
			}

			j++;
			if (j >= n) j = 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
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; }
using ll = long long;
using P = pair<int, int>;
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 n, u, v;
vector< vector<int> > graphlist(100100, vector<int>());
vector<int> takahashi(100100, -1);
vector<int> aoki(100100, -1);
struct posdata {
    int p; int dist;
};

int main() {
    cin >> n >> u >> v;
    u--; v--;
    takahashi.at(u) = 0; aoki.at(v) = 0;
    for(int i = 0; i < n-1; ++i) {
        int aa, bb; cin >> aa >> bb;
        aa--; bb--;
        graphlist.at(aa).emplace_back(bb);
        graphlist.at(bb).emplace_back(aa);
    }

    queue<posdata> tque;
    posdata tfirst;
    tfirst.p = u; tfirst.dist = 0;
    tque.push(tfirst);
    while(!tque.empty()) {
        posdata now = tque.front();
        tque.pop();
        for(int i = 0; i < (int)graphlist.at(now.p).size(); ++i) {
            int np = graphlist.at(now.p).at(i);
            int ndist = now.dist+1;
            if(takahashi.at(np) == -1) {
                takahashi.at(np) = ndist;
                posdata next;
                next.p = np; next.dist = ndist;
                tque.push(next);
            }
        }
    }

    queue<posdata> aque;
    posdata afirst;
    afirst.p = v; afirst.dist = 0;
    aque.push(afirst);
    while(!aque.empty()) {
        posdata now = aque.front();
        aque.pop();
        for(int i = 0; i < (int)graphlist.at(now.p).size(); ++i) {
            int np = graphlist.at(now.p).at(i);
            int ndist = now.dist+1;
            if(aoki.at(np) == -1) {
                aoki.at(np) = ndist;
                posdata next;
                next.p = np; next.dist = ndist;
                aque.push(next);
            }
        }
    }

    int amax = 0;
    for(int i = 0; i < n; ++i) {
        if(aoki.at(i) > takahashi.at(i)) {
            chmax(amax, aoki.at(i));
        }
    }

    cout << amax-1 << endl;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define M 200020
using namespace std;
LL read(){
    LL nm=0,fh=1; char cw=getchar();
    for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh;
    for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0');
    return nm*fh;
}
LL ans,sum0,sum1,sig;
LL n,m,fs[M],nt[M<<1],to[M<<1],col[M],f[M],sz[M],tmp;
LL fd(LL x){return x==f[x]?x:f[x]=fd(f[x]);}
void link(LL x,LL y){nt[tmp]=fs[x],fs[x]=tmp,to[tmp++]=y;}
void merge(LL x,LL y){x=fd(x),y=fd(y);if(x!=y) f[x]=y,sz[y]+=sz[x];}
bool check(LL x,LL now){
    if(col[x]) return col[x]==now; col[x]=now;
    for(LL i=fs[x];i!=-1;i=nt[i])
        if(!check(to[i],3-now)) return false;
    return true;
}
int main(){
    n=read(),m=read(),memset(fs,-1,sizeof(fs));
    for(LL i=1;i<=n;i++) f[i]=i,sz[i]=1;
    for(LL i=1;i<=m;i++){
        LL x=read(),y=read();
        link(x,y),link(y,x),merge(x,y);
    }
    for(LL i=1;i<=n;i++) if(fd(i)==i&&sz[i]==1) sig++;
    for(LL i=1;i<=n;i++){
        if(fd(i)!=i||sz[i]==1) continue;
        if(check(i,1)) sum0++;
        else sum1++;
    }
    ans+=sig*((n<<1)-1)-sig*(sig-1);
    ans+=((sum0*sum0)<<1);
    ans+=sum1*(sum0<<1)+sum1*sum1;
    printf("%lld\n",ans);
    return 0;
}


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

int main(){

    char tulisanA[11];
    char tulisanB[11];
    char tulisanC[11];

    scanf("%s %s %s", &tulisanA, &tulisanB, &tulisanC);

    int panjangA = strlen (tulisanA);
    int panjangB = strlen(tulisanB);

    if(tulisanA[panjangA-1] == tulisanB[0] && tulisanB[panjangB-1] == tulisanC[0]){
        puts("YES");
    }
    else{
        puts("NO");
    }
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr ll inf = LLONG_MAX;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ----------------------end----------------------- */
int main() {
    int a,b,c;cin >> a >> b >> c;
    if(a<c&&c<b||b<c&&c<a) puts("Yes");
    else puts("No");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
set<unsigned int> s;
int n,m;
int cards[100];
bool used[100];
void solve(int cnt,string a){
  string tmp=a;
  if(cnt==m){
    s.insert(atoi(a.c_str()));
    return;
  }
  for(int i=0;i<n;i++){
    if(used[i]==true)continue;
    used[i]=true;
    stringstream ss;
    ss << cards[i];
    a+=ss.str();
    solve(cnt+1,a);
    a=tmp;
    used[i]=false;
  }
}

int main(){
  while(cin>>n,n){
    s.clear();
    cin>>m;
    for(int i=0;i<n;i++){
      used[i]=false;
      cin>>cards[i];
    }
    solve(0,"");
    // for(auto i:s)
    // cout << i << endl;

    cout << s.size() << endl;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
vector <string> card,memo;
string suzi,m;
int n,k,ans,p,but;

int main(){

    while(1){

        cin >> n >> k;
        if(n==0&&k==0)break;

        for(int i=0;i<n;i++){
            cin >> m;
            card.push_back(m);
        }

        if(k==2){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(i!=j){
                        suzi=card[i]+card[j];
                        but=0;
                        while(1){
                            if(p==ans)break;
                            if(memo[p]!=suzi);
                            else {
                                but++;
                                break;
                            }
                        p++;
                        }
                        p=0;
                        if(but==0){
                                ans++;
                               memo.push_back(suzi);
                    }
                }
                but=0;
            }
        }

    }

        if(k==3){
                 for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        for(int a=0;a<n;a++){
                    if(i!=j&&j!=a&&a!=i){
                        suzi=card[i]+card[j]+card[a];
                        but=0;
                        while(1){
                            if(p==ans)break;
                            if(memo[p]!=suzi);
                            else {
                                but++;
                                break;
                            }
                        p++;
                        }
                        p=0;
                        if(but==0){
                                ans++;
                               memo.push_back(suzi);
                    }
                }
                but=0;
                }
            }
        }

        }

        if(k==4){

                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        for(int a=0;a<n;a++){
                            for(int b=0;b<n;b++){
                    if(i!=j&&i!=a&&i!=b&&j!=a&&j!=b&&a!=b){
                        suzi=card[i]+card[j]+card[a]+card[b];
                        but=0;
                        while(1){
                            if(p==ans)break;
                            if(memo[p]!=suzi);
                            else {
                                but++;
                                break;
                            }
                        p++;
                        }
                        p=0;
                        if(but==0){
                                ans++;
                               memo.push_back(suzi);
                    }
                }
                but=0;
                    }
                }
            }
        }

        }

        cout << ans << endl;
        memo.clear();
        card.clear();
        ans=0;
    }

return 0;
}


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

int ary[15];
char str[7][12]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int main(){
	ary[1]=2;
	for(int i=1;i<12;i++){
		if(i==4||i==6||i==9||i==11){
			ary[i+1]=(ary[i]+30)%7;
		}
		else if(i==2){
			ary[i+1]=(ary[i]+29)%7;
		}
		else{
			ary[i+1]=(ary[i]+31)%7;
		}
	}
	int x,y;
	while(scanf("%d %d",&x,&y)!=EOF){
		if(x==0&&y==0) break;
		cout<<str[(ary[x]+y)%7]<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define LLI long long int
#define fst first
#define snd second

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(x)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T, typename U> T& chmin(T &a, const U &b){return a = (a<=b?a:b);}
template <typename T, typename U> T& chmax(T &a, const U &b){return a = (a>=b?a:b);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

const LLI mod = 1e9+7;

LLI dp[300][101000];

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

  int N,X;
  while(cin >> N >> X){
    vector<LLI> S(N); cin >> S;

    sort(ALL(S));
    reverse(ALL(S));

    fill_array(dp,0);

    dp[0][X] = 1;

    REP(i,N){
      FORE(j,0,X){
	(dp[i+1][j%S[i]] += dp[i][j]) %= mod;
	(dp[i+1][j] += dp[i][j]*(N-i-1)%mod) %= mod;
      }
    }

    LLI ans = 0;

    FORE(i,0,X) (ans += i*dp[N][i]) %= mod;

    cout << ans << endl;
  }

  return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
//#define PI 3.14159265358979

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

using namespace std;

template < typename T >
ostream &operator<<(ostream &os, const vector< T > &A) {
	for (int i = 0; i < A.size(); i++)
		os << A[i] << " ";
	os << endl;
	return os;
}
template <>
ostream &operator<<(ostream &os, const vector< vector< int > > &A) {
	int N = A.size();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < A[i].size(); j++)
			os << A[i][j] << " ";
		os << endl;
	}
	return os;
}
template < typename T, typename U >
ostream &operator<<(ostream &os, const pair< T, U > &p) {
	os << "(" << p.first << "," << p.second << ")";
	return os;
}

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

struct edge {
	int from, to, d, c, i;
	edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
		from = _from;
		to = _to;
		d = _d;
		c = _c;
	}
	bool operator<(const edge &rhs) const {
		return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
	}
};
struct aabb {
	int x1, y1, x2, y2;
	aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};

typedef vector< edge > edges;
typedef vector< edges > graph;
struct flow {
	int to, cap, rev, cost;
	flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector< vector< flow > > flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

int pow2(int n) { return 1LL << n; }
template < typename T, typename U >
bool chmin(T &x, const U &y) {
	if (x > y) {
		x = y;
		return true;
	}
	return false;
}
template < typename T, typename U >
bool chmax(T &x, const U &y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}

template < typename A, size_t N, typename T >
void Fill(A (&array)[N], const T &val) {
	fill((T *)array, (T *)(array + N), val);
}
struct initializer {
	initializer() {
		cout << fixed << setprecision(20);
	}
};
initializer _____;

int N, M, K, T, Q, H, W;
signed main() {
	cin >> T;
	while (T--) {
		cin >> N;
		vector< int > A(N);
		rep(i, N) cin >> A[i];
		string S;
		cin >> S;
		vector< int > es;
		int ans = 0;
		for (int i = N - 1; i >= 0; i--) {
			int v = A[i];
			sort(es.rbegin(), es.rend());
			for (auto e : es) {
				chmin(v, v ^ e);
			}
			if (S[i] == '0') {
				if (v != 0) {
					es.push_back(v);
				}
			} else {
				if (v) {
					ans = 1;
					break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
#include<sstream>
#include<queue>
#include<deque>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<iomanip>
#define INF 2147483647
#define lli long long int
#define MOD 1000000007

using namespace std;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;

//A???65 a???97
int main() {
	vector<char> eki(52);
	eki[0] = 65;
	eki[26] = 97;
	for (int i = 1; i < 26; i++){
		eki[i] = eki[i-1] + 1;
		eki[26+i] = eki[26+i-1] + 1;
	}

	while(1){
		int n; cin >> n;
		if (n == 0) break;
		vi key(n);
		for (int i = 0; i < n; i++){
			cin >> key[i];
		}

		string station; cin >> station;
		string ans = "";
		for (int i = 0; i < station.size(); i++){
			if (isupper(station[i])){
				ans += eki[(station[i]-'A'-key[i%n]+52)%52];
			}else{
				ans += eki[(station[i]-'a'+26-key[i%n]+52)%52];
			}
		}
		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){

	long long int A[5001]={0};
	long long int n,MAX = -100000;

		while(1){

			cin >> n;

			if( n == 0 ) break;

			for(int i = 1; i <= n; i++){

				cin >> A[i];

			}

			MAX = -100000;

			for(int i = 1; i <= n; i++){

				A[i] = max(A[i], A[i] + A[i-1]);
				MAX = max(A[i], MAX);

			}

			cout  << MAX << endl;

		}
	return 0;
}

// 2016-12-05
#include <iostream>
#include <cmath>
using namespace std;

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


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

using namespace std;

const int MAX=10000;

int V;
vector<int> G[MAX];
vector<int> rG[MAX];
vector<int> vs;
bool used[MAX];
int cmp[MAX];

void add_edge(int from,int to)
{
	G[from].push_back(to);
	rG[to].push_back(from);
	return;
}

void dfs(int v)
{
	used[v]=true;
	for(int i=0;i<G[v].size();i++)
	{
		if(!used[G[v][i]])dfs(G[v][i]);
	}
	vs.push_back(v);
	return;
}

void rdfs(int v,int k)
{
	used[v]=true;
	cmp[v]=k;
	for(int i=0;i<rG[v].size();i++)
	{
		if(!used[rG[v][i]])rdfs(rG[v][i],k);
	}
	return;
}

int ssc()//Strongly Connected Component
{
	memset(used,0,sizeof(used));
	vs.clear();
	for(int v=0;v<V;v++)
	{
		if(!used[v])dfs(v);
	}
	memset(used,0,sizeof(used));
	int k=0;
	for(int i=vs.size()-1;i>=0;i--)
	{
		if(!used[vs[i]])rdfs(vs[i],k++);
	}
	return k;
}

int main()
{
	int E;
	cin>>V>>E;
	for(int i=0;i<E;i++)
	{
		int u,v;
		cin>>u>>v;
		add_edge(u,v);
	}
	int k=ssc();
	int Q;cin>>Q;
	for(int i=0;i<Q;i++)
	{
		int u,v;
		cin>>u>>v;
		cout<<(cmp[u]==cmp[v])<<endl;
	}
	return 0;
}

#include "bits/stdc++.h"
using namespace std;
vector<long long>Col(100005);
struct Edge {
	int to, id;
	Edge(int to, long long id) :to(to), id(id) {}
};
vector<Edge>g[100005];

void dfs(int v, long long d = 0, int p = -1) {
	Col[v] = d;
	for (Edge e : g[v]) {
		if (p == e.to) {
			continue;
		}
		dfs(e.to, d + e.id, v);
	}
}

int main() {
	int N;
	cin >> N;
	for (int n = 0; n < N - 1; ++n) {
		int a, b;
		long long c;
		cin >> a >> b >>c;
		a--;
		b--;
		g[a].emplace_back(b, c);
		g[b].emplace_back(a, c);
	}
	int Q,K;
	cin >> Q >> K;
	K--;
	dfs(K);
	for (int i = 0;i<Q;++i) {
		int x, y;
		cin >> x >> y;
		x--;
		y--;
		cout << Col[x]+Col[y]<<endl;
	}
	return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;

int main() {

  double n;
  cin >> n;
  cout << fixed << setprecision(20) << (2 * (n) * 3.14159265359);
  return 0;
}

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

using namespace std;
using ll = long long;

int main() {
    int x;
    cin >> x;
    int a = x / 500;
    int b = (x % 500) / 5;
    cout << a * 1000 + b * 5 << endl;
}


#include <iostream>
using namespace std;

#define Q 1000000007

int main(void){
  int n;
  int a[200010];
  long long acc[200010], ans;

  cin >> n;
  acc[0] = 0;
  for (int i=1; i<=n; i++){
    cin >> a[i];
    acc[i] = acc[i-1] + a[i];
    acc[i] %= Q;
  }
  ans = 0;
  for (int i=1; i<=n-1; i++){
    ans += (a[i]*((acc[n]-acc[i]+Q)%Q)) % Q;
    ans %= Q;
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define IN freopen("perimetric_chapter_1_input.txt","r",stdin)
#define OUT freopen("output.txt","w",stdout)

#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a ; i<=b ; i++)
#define DBG printf("Hi\n")
#define i64 long long int
#define ui64 unsigned long long int
#define xx first
#define yy second
#define ln 17
#define off 2002

#define sq(x) ((x)*(x))

#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace __gnu_pbds;
using namespace std ;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree< i64, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef pair<int,int> pii;

#define log 20
#define mod 1000000007LL
#define INF 1000000000000000000LL
#define maxn 300005

const long double eps = 1e-9 ;

class numberTheory{

public:
    numberTheory(){}

    pii extendedEuclid(i64 a, i64 b) { // returns x, y | ax + by = gcd(a,b)

        if(b == 0) return pii( a >= 0 ? 1 : -1 , 0LL);
        else {
            pii d = extendedEuclid(b, a % b);
            return pii(d.yy, d.xx - d.yy * (a / b));
        }

    }

    i64 modularInverse(i64 a, i64 n) {
        pair<i64,i64> ret = extendedEuclid(a, n);
        return ((ret.xx % n) + n) % n;
    }

    i64 bigMod(i64 a, i64 n , i64 m)
    {
        if(n==0) return 1 ;
        i64 ret = bigMod(a,n/2,m) ;
        ret = (ret*ret)%m ;
        if(n%2) ret = (ret*a)%m ;
        return ret ;
    }
};

const i64 N = 5000000 ;

i64 fact[N+5] , invFact[N+5] ;

i64 C(i64 n, i64 r)
{
    if( n<0 || r<0 || r>n ) return 0 ;
    i64 ret = (fact[n]*invFact[r])%mod ;
    return (ret*invFact[n-r])%mod ;
}

void preprocess()
{
    numberTheory nt ;

    fact[0] = 1 ;
    for(i64 x=1 ; x<=N ; x++) fact[x] = (fact[x-1]*x)%mod ;

    invFact[N] = nt.modularInverse(fact[N],mod) ;

    for(i64 x=N-1 ; x>=0 ; x--) invFact[x] = (invFact[x+1]*(x+1) )%mod ;
}

int dp[2005][2005] ;

int main()
{
    preprocess() ;

    int n , k ;

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

    k-- ;

    if(k==0)
    {
        printf("1\n") ;
        return 0 ;
    }

    dp[0][0] = 1 ;

    for(int i=1 ; i<=n ; i++)
    {
        dp[i][0] = (1LL*i*((1LL*dp[i-1][0]*C( i*k - 1 , k-1 ))%mod))%mod ;
     //   dp[i][0] = ( dp[i][0] + dp )

        for(int j=1 ; j<=i ; j++)
        {
            dp[i][j] = (1LL*i*((1LL*dp[i-1][j]*C( i*k + j - 1 , k-1 ))%mod))%mod ;
            dp[i][j] = ( dp[i][j] + dp[i][j-1] )%mod ;
        }
    }

    printf("%d\n",dp[n][n]) ;

    return 0;
}


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

struct Dimension {
    int N;
    vector< LL >basis;
    Dimension(int N) : N(N), basis(N, 0) { }

    bool add(LL x) {
        for (int i = 0; i < N; i++) {
            if (x&(1LL<<i)) {
                if (basis[i]) x ^= basis[i];
                else {
                    basis[i] = x;
                    return true;
                }
            }
        }
        return false;
    }
};

char solve() {
    int n;
    cin >> n;

    vector< LL >A(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    string s;
    cin >> s;

    Dimension d(60);
    for (int i = n-1; i >= 0; i--) {
        if (d.add(A[i])) {
            if (s[i]=='1') return '1';
        }
    }
    return '0';
}

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

    while (t--) {
        cout << solve() << "\n";
    }

    return 0;
}

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

void solve(){
    ll n;
    std::cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    string s;
    std::cin >> s;

    vector<vector<ll>> dp(n+1);
    dp[n] = {0};

    for (int i = n-1; i >= 0; i--) {
        ll v = a[i];
        for (auto e : dp[i+1]) {
            v = min(v,v^e);
        }
        if(v==0){
            dp[i] = dp[i+1];
        }else{
            if(s[i]=='0'){
                dp[i+1].push_back(v);
                sort(dp[i+1].rbegin(),dp[i+1].rend());
                for (auto e : dp[i+1]) {
                    dp[i].push_back(e);
                }
            }else{
                std::cout << 1 << std::endl;
                return;
            }
        }
    }
    std::cout << 0 << std::endl;
}

int main() {
    int t;
    std::cin >> t;
    for (int iii = 0; iii < t; iii++) {
        solve();
    }
}


#include<iostream>
#include<map>
#include<string>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef int Def;
typedef vector<Def> vi;
typedef pair<Def,Def> pii;
int main(){
	int n;
	while(cin>>n,n){
		vi in(n);
		rep(i,n)cin>>in[i];
		string s;
		cin>>s;
		rep(i,s.size()){
			int t=in[i%n];
			if(t>=26){
				t-=26;
				if(islower(s[i]))s[i]=toupper(s[i]);
				else s[i]=tolower(s[i]);
			}
			if(islower(s[i])){
				s[i]-=t;
				if(s[i]<'a')s[i]='Z'-('a'-s[i])+1;
			}else{
				s[i]-=t;
				if(s[i]<'A')s[i]='z'-('A'-s[i])+1;
			}
		}
		cout<<s<<endl;
	}
}

#include <cstdio>

using namespace std;

int main() {
  int n;
  while (scanf("%d", &n), n!=0) {
    int k[100];
    char s[101];
    for (int i=0;i<n;i++) scanf("%d", &k[i]);
    scanf("%s", s);
    for (int i=0;s[i]!='\0';i++) {
      int c = s[i];
      for (int j=0;j<k[i%n];j++) {
        if (c == 'a') c = 'Z';
        else if (c == 'A') c = 'z';
        else c--;
      }
      putchar(c);
    }
    putchar('\n');
  }
  return 0;
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll,ll> P;
typedef pair<double,ll> Pd;
typedef pair<double,double> PD;
typedef priority_queue<P,vector<P>,greater<P> > P_queue;
typedef priority_queue<Pd,vector<Pd>,greater<Pd> > Pd_queue;

const ll MOD=998244353;
const ll mod=1000000007;
const ll INF=1e15;
const double DEL=1e-6;

#define _GLIBCXX_DEBUG
#define REP(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end())

void Add(ll &a, ll b){
    a=(a+b)%mod;
    return;
}

void Pro(ll &a, ll b){
    a=(a*b)%mod;
    return;
}

ll H,W;
vector<P> d;

vec ans;
void init(){
    cin>>H>>W;
    rep(i,H){
        ll x,y; cin>>x>>y;
        x--; y--;
        d.pb(mp(x,y));
        ans.pb(-1);
    }
}

void Solve(){
    set<P> x;
    multiset<ll> y;
    rep(i,W) {
        P f=mp(i,i);
        x.insert(f);
        y.insert(0);
    }
    rep(i,H){
        ll Biggest=-INF;
        for(auto itr=x.lower_bound(mp(d[i].first,-INF));itr!=x.end();){
            ll R=(*itr).first, L=(*itr).second;
           // cout<<i<<':'<<R<<' '<<L<<endl;
            if(R>d[i].second+1) break;
            Biggest=max(Biggest,L);
            auto itr1=y.lower_bound(R-L);
            y.erase(itr1);
            itr=x.erase(itr);
        }
        if(Biggest!=-INF) if(d[i].second!=W-1) {x.insert(mp(d[i].second+1,Biggest)); y.insert(d[i].second+1-Biggest);}
        if(y.size()) ans[i]=*(y.begin());
        else break;
    }
    rep(i,H) if(ans[i]>=0) ans[i]+=(i+1);

}

int main(){
    init();
    Solve();
    rep(i,H) cout<<ans[i]<<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define Nitroboost  ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
const int mod=1e9+7;
const int N=1e6+5;

int main()
    {
        Nitroboost;
        #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        #endif

        int n,m;
        cin>>n>>m;
        map<int,int>mp;
        for(int i=1;i<=m;i++)
            mp[i]=i;

        multiset<int>v;
        for(int i=1;i<=m;i++)
            v.insert(0);

        for(int i=1;i<=n;i++)
        {
            int l,r;
            cin>>l>>r;
            r++;
            auto it=mp.lower_bound(l);
            int t=-1;
            while(it!=mp.end() && it->first<=r)
            {
                t=max(t,it->second);
                v.erase(v.find(it->first-it->second));
                mp.erase(it++);
            }
            if(t!=-1 && r<=m)
            {
                v.insert(r-t);
                mp[r]=t;
            }

            if(v.size()>0)
            cout<<i+(*v.begin())<<"\n";
            else
                cout<<"-1\n";
        }
    }


#include <bits/stdc++.h>
#define SIZE 300005
#define MOD 1000000007LL
#define INF 1 << 30
#define LLINF 1LL << 60
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define DOWN(i,b,a) for(int i=b;i>=a;i--)
#define SET(a,c) memset(a,c,sizeof a)
#define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FOREACH(i,c) for(auto (i) : (c))
#define BIT(i,j) ((i)>>(j))&1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
#define SQ(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll,ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i]<<((i==a.size()-1)?"\n":" ");}}
ll gcd(ll a,ll b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
ll lcm(ll a,ll b){return a==0||b==0?0:a*b/gcd(a,b);}

int a[144];

void flip(int x, int y)
{
  if(x>11||y>11||x<0||y<0) return;
  if(a[x+y*12])
  {
    a[x+y*12] = 0;
    flip(x+1,y);
    flip(x-1,y);
    flip(x,y+1);
    flip(x,y-1);
  }
}

void solve()
{
  int cnt = 0;
  REP(i,144) if(a[i])
  {
    cnt++;
    flip(i%12,i/12);
  }
  cout << cnt << endl;
}

int main()
{
  string s;
  int i = 0;
  while(cin >> s)
  {
    REP(j,12) a[i*12+j] = s[j]-'0';
    i=(++i)%12;
    if(i==0) solve();
  }
  return 0;
}

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

int tisei[12][12];
void shima(int x, int y) {
	tisei[x][y] = 0;
	if (x < 11 && tisei[x+1][y]) shima(x + 1, y);
	if (x > 0 && tisei[x - 1][y])  shima(x -1, y);
	if (y < 11 && tisei[x][y+1]) shima(x , y+1);
	if (y > 0 && tisei[x][y-1])  shima(x, y-1);
}
int main() {
	vector<int>n;
	while (1) {
		for (int a = 0; a < 12; a++) {
			string c;
			cin >> c;
			if (cin.fail())goto end;
			for (int b = 0; b < 12; b++) {
				tisei[a][b] = c[b] - 48;
			}
		}
		int s = 0;
		for (int d = 0; d < 12; d++) {
			for (int e = 0; e < 12; e++) {
				if (tisei[d][e]) { s++; shima(d, e); }
			}
		}
		n.push_back(s);
	}
end:;
	for (int j : n)cout << j << endl;
}


#include<iostream>
#include<tuple>
#include<vector>
#include<algorithm>
#include<iterator>
#include<cmath>
#include<functional>
#include<map>
using namespace std;

int main(){
    int N;
    cin >> N;
    map<int, int>  o, e;
    for(int i=0; i<N/2; i++){
        int ot, et;
        cin >> ot;
        cin >> et;
        o[ot]++;
        e[et]++;
    }
    int r = N;
    int ei = 0;
    int oi = 0;
    int so = 0;
    int se = 0;
    int me = 0;
    int mo = 0;
    for (auto it = o.begin(); it != o.end(); it++){
        if(mo < it->second){
            so = mo;
            mo = it->second;
            oi = it->first;
        }else if(so < it -> second){
            so = it->second;
        }
    }
    for (auto it = e.begin(); it != e.end(); it++){
        if(me < it->second){
            se = me;
            me = it->second;
            ei = it->first;
        }else if(so < it -> second){
            se = it->second;
        }
    }
    if(ei == oi){
        if (so == 0 && se == 0){
            r -= N/2;
        }else if(so > se){
            r -= so + me;
        }else{
            r -= se + mo;
        }
    }else{
        r -= me + mo;
    }
    cout << r << endl;
}

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

#define int long long

int N;
int odd[100010], even[100010];

signed main()
{
    cin >> N;
    for(int i = 0; i < N; i++)
    {
        int a;
        cin >> a;
        if(i & 1) even[a]++;
        else odd[a]++;
    }

    int odd_index = -1, odd_max = -1;
    for(int i = 0; i < 100010; i++)
    {
        if(odd_max < odd[i])
        {
            odd_max = odd[i];
            odd_index = i;
        }
    }

    int even_index = -1, even_max = -1;
    for(int i = 0; i < 100010; i++)
    {
        if(even_max < even[i])
        {
            even_max = even[i];
            even_index = i;
        }
    }

    if(even_index != odd_index)
    {
        cout << N - even_max - odd_max << endl;
        return 0;
    }

    int odd_max2 = -1;
    for(int i = 0; i < 100010; i++)
    {
        if(odd_max2 < odd[i] && i != odd_index)
        {
            odd_max2 = odd[i];
        }
    }

    int even_max2 = -1;
    for(int i = 0; i < 100010; i++)
    {
        if(even_max2 < even[i] && i != even_index)
        {
            even_max2 = even[i];
        }
    }

    cout << min(N - even_max - odd_max2, N - even_max2 - odd_max) << endl;

    return 0;
}


#include <cstdio>

const int mod = 1e9 + 7;
const int MAXN = 45, MAXS = ( 1 << 17 ) + 5;

template<typename _T>
void read( _T &x )
{
	x = 0;char s = getchar();int f = 1;
	while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
	while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
	x *= f;
}

template<typename _T>
void write( _T x )
{
	if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
	if( 9 < x ){ write( x / 10 ); }
	putchar( x % 10 + '0' );
}

int f[MAXN][MAXS];
int N, X, Y, Z, st, all;

int bit( const int i ) { return 1 << i - 1; }
bool chk( const int S ) { return ( S & st ) == st; }
void add( int &x, const int v ) { x = ( x + v >= mod ? x + v - mod : x + v ); }
int insert( const int S, const int v ) { return ( S << v | bit( v ) ) & all; }

int main()
{
	read( N ), read( X ), read( Y ), read( Z );
	int up = 1 << X + Y + Z;
	all = ( bit( X + Y + Z ) << 1 ) - 1;
	st = bit( Z ) | bit( Y + Z ) | bit( X + Y + Z );

	f[0][0] = 1;
	for( int i = 0 ; i < N ; i ++ )
		for( int S = 0 ; S < up ; S ++ )
			if( f[i][S] && ! chk( S ) )
				for( int k = 1, T ; k <= 10 ; k ++ )
					if( ! chk( T = insert( S, k ) ) )
						add( f[i + 1][T], f[i][S] );
	int ans = 1;
	for( int i = 1 ; i <= N ; i ++ )
		ans = 10ll * ans % mod;
	for( int S = 0 ; S < up ; S ++ )
		if( ! chk( S ) )
			add( ans, mod - f[N][S] );
	write( ans ), putchar( '\n' );
	return 0;
}

//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
gpu_hash_table<int, int> mp;
#define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update>
 */
using ll = long long;
using ld = long double;
const ll MOD = 924844033;
const ll root = 62;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const double eps = -1e6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 101;
int n;
int arr[mxN];
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>n;
    int sum = 0;
    owo(i, 0, n) {
        cin>>arr[i];
        sum+=arr[i];
    }
    int ans = 0;
    int diff = INF;
    owo(i, 0, n) {
        if(abs(arr[i]*n-sum)<diff) {
            diff = abs(arr[i]*n-sum);
            ans = i;
        }
    }
    cout<<ans<<"\n";
    return 0;
}