#include <bits/stdc++.h>
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x,y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cout << #x << " = " << (x) << ", " << #y << " = " << (y) <<  ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second)
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) (find(all(v),x)!=v.end())
#define incls(s,c) (s.find(c)!=string::npos)
#define lb(a,x) distance((a).begin(),lower_bound(all(a),(x)))
#define ub(a,x) distance((a).begin(),upper_bound(all(a),(x)))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n,k) ((n>>k)&1) // nのk bit目
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(all(x)),x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
const int mod = 1000000007;
const double EPS = 1e-9;
//const long double EPS = 1e-14;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
inline int in() { int x; cin >> x; return x;}
inline ll lin() { ll x; cin >> x; return x;}
inline char chin() { char x; cin >> x; return x;}
inline string stin() { string x; cin >> x; return x;}
inline double din() { double x; cin >> x; return x;}
//template<class T = int> inline T in() { T x; cin >> x; return (x);}
template<typename T>inline ll suma(const vector<T>& a) { ll res(0); for (auto&& x : a) res += x; return res;}
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; }
char itoa(int n) { return n + '0';}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
const int dx[4] = {1, 0,-1, 0};
const int dy[4] = {0, 1, 0,-1};

int main () {
  int A, B, M;
  cin >> A >> B >> M;
  vi a(A), b(B);
  rep(i,A) cin >> a[i];
  rep(i,B) cin >> b[i];
  int ans = *min_element(all(a));
  ans += *min_element(all(b));
  rep(i,M) {
    int x, y, c;
    cin >> x >> y >> c;
    x--, y--;
    chmin(ans, a[x]+b[y]-c);
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
typedef map<int, int> MPII;
typedef set<int> SETI;

const int mxN = 2e5;
const ld pi = 4.0 * atanl(1.0);
const int iinf = 1e9 + 10;
const int inf = 1e18 + iinf + 10;
const int mod = 1000000007;
const ld prec = .000001;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define rall(c) c.end(), c.begin()

int A, B, M, x, y, c, a[mxN], b[mxN];

void fast() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); }

int main() {
  fast();
  cin >> A >> B >> M;
  int mnA = mxN, mnB = mxN;
  for (int i = 0; i < A; ++i) {
    cin >> a[i];
    mnA = min(a[i], mnA);
  }
  for (int i = 0; i < B; ++i) {
    cin >> b[i];
    mnB = min(b[i], mnB);
  }

  int mn = mxN;
  for (int i = 0; i < M; ++i) {
    cin >> x >> y >> c;
    mn = min((a[x - 1] + b[y - 1]) - c, mn);
  }

  if (mnB + mnA < mn)
    cout << mnB + mnA;
  else
    cout << mn;

  return 0;
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<vector<int> > edges(n);
        for(int i=0; i<m; ++i){
            int a, b;
            cin >> a >> b;
            edges[a-1].push_back(b-1);
            edges[b-1].push_back(a-1);
        }

        vector<bool> check(n, false);
        check[0] = true;
        for(int i=0; i<2; ++i){
            vector<bool> next = check;
            for(int j=0; j<n; ++j){
                if(check[j]){
                    for(unsigned k=0; k<edges[j].size(); ++k)
                        next[edges[j][k]] = true;
                }
            }
            check = next;
        }

        int ret = -1;
        for(int i=0; i<n; ++i){
            if(check[i])
                ++ ret;
        }
        cout << ret << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using Graph=vector<vector<int>>;
#define MOD 1000000007
#define INF 1000000000

int main(){
  int N,M;
  cin>>N>>M;
  Graph G(N);
  vector<int> count(N,0);
  for(int i=0;i<N+M-1;i++){
    int A,B;
    cin>>A>>B;
    A--,B--;
    G.at(A).push_back(B);
    count.at(B)++;
  }

  int root;
  for(int i=0;i<N;i++){
    if(count.at(i)==0){
      root=i;
    }
  }

  vector<int> parent(N,-1);
  stack<int> s;
  s.push(root);
  while(!s.empty()){
    int v=s.top();
    s.pop();
    for(auto nv:G.at(v)){
      if(count.at(nv)>1){
        count.at(nv)--;
      }else{
        parent.at(nv)=v;
        s.push(nv);
      }
    }
  }

  for(int i=0;i<N;i++){
    cout<<parent.at(i)+1<<endl;
  }
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<iomanip>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
int main(){
  int a, b, c;
  cin >> a;
  if(a == 1){
    cout << "Hello World\n";
    return 0;
  }
  if(a == 2){
    cin >> b >> c;
    cout << b + c << "\n";
    return 0;
  }
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cctype>

using namespace std;

int main() {

	string st;
	int stlen;

	while (cin >> st) {
		stlen = st.size();
		for (int i = stlen - 1; i >= 0; i--) {
			cout << st[i];
		}
		putchar('\n');
	}

	return 0;
}


/********************************************************************************

	Code by a weak man named CYJian, and he hopes the code can get more points.

	Algorithm:

 ********************************************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

//{{{ FAST IO AND SOME FUNCTIONS
const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s"in", "r", stdin), freopen(s"out", "w", stdout)

template<typename T>
inline void read(T &x) {
	char ch, t = 0; x = 0;
	while(!isdigit(ch = ge)) t |= ch == '-';
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge;
	x = t ? -x : x;
}
inline int read_int() { int x; return read(x), x; }
inline ll read_ll() { ll x; return read(x), x; }

template<typename T> inline void chkmin(T&a, T b) { a = a < b ? a : b; }
template<typename T> inline void chkmax(T&a, T b) { a = a > b ? a : b; }
//}}}

const int MAXN = 1000010;

int n;
char S[MAXN];
char T[MAXN];

struct Node {
	int p, h;

	Node() {}
	Node(int p, int h):p(p), h(h) {}
};

queue<Node>q;

int main() {
#ifdef LOCAL
	FILE("");
#endif

	scanf("%d%s%s", &n, S + 1, T + 1);
	int pT = n, pS = n, res = 0, Add = 0, las = -1;
	while(pT) {
		while(pS && S[pS] != T[pT]) --pS;
		if(!pS) return puts("-1"), 0;
		while(!q.empty() && q.front().p - Add > pT) chkmax(res, q.front().h + Add), q.pop();
		if(las != pS && pS < pT) ++Add, q.push(Node(pS + Add, 1 - Add)), las = pS;
		--pT, pS -= pT < pS;
	} while(!q.empty()) chkmax(res, q.front().h + Add), q.pop();
	cout << res << endl;
	return 0;
}

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define no cout<<"No"<<endl
#define NO cout<<"NO"<<endl
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin>>n;
  string s;
  cin>>s;

  string back_s=s.substr(n,n);
  reverse(ALL(back_s));

  map<pair<string,string>,ll> m;
  REP(bit,(1<<n)){
    string b="",r="";
    REP(i,n){
      if((bit>>i)&1)
        b+=back_s[i];
      else
        r+=back_s[i];
    }

    if(b>r)
      swap(b,r);
    m[make_pair(b,r)] += 1;
  }

  ll ans=0;
  REP(bit,(1<<n)){
    string b="",r="";
    REP(i,n){
      if((bit>>i)&1)
        b+=s[i];
      else
        r+=s[i];
    }
    if(b>r)
      swap(b,r);

    if(b==r)
      ans+=m[make_pair(b,r)];
    else
      ans+=m[make_pair(b,r)]/2;
  }
  cout<<ans<<endl;

}


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

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 300;
int mat[2][1 + 2 * nmax][1 + 2 * nmax];

std::vector<std::pair<int,int>> mv[2];
int lim;

bool valid(int x, int y) {
  return 0 <= x && x < lim && 0 <= y && y < lim;
}

void dfs(int x, int y, int color, int p) {
  if(mat[p][x][y] == 0) {
    mat[p][x][y] = color;
    for(int h = 0; h < mv[p].size(); h++) {
      int newx = x + mv[p][h].first;
      int newy = y + mv[p][h].second;
      if(valid(newx, newy))
        dfs(newx, newy, 3 - color, p);
    }
  }
}
int frec[3][3];

int main() {
  int n, d1, d2;
  std::cin >> n;
  std::cin >> d1 >> d2;
  lim = 2 * n;
  for(int i = -lim; i <= lim; i++)
    for(int j = -lim; j <= lim; j++) {
      if(i * i + j * j == d1)
        mv[0].push_back({i, j});
      if(i * i + j  * j == d2)
        mv[1].push_back({i, j});
    }
  for(int i = 0; i < lim; i++)
    for(int j = 0; j < lim; j++) {
      dfs(i, j, 1, 0);
      dfs(i, j, 1, 1);
      frec[mat[0][i][j]][mat[1][i][j]]++;
    }
  for(int h = 1; h <= 2; h++)
    for(int h2 = 1; h2 <= 2; h2++)
      if(n * n <= frec[h][h2]) {
        int lft = n * n;
        for(int i = 0; i < lim; i++)
          for(int j = 0; j < lim; j++)
            if(0 < lft && mat[0][i][j] == h && mat[1][i][j] == h2) {
              std::cout << i << " " << j << '\n';
              lft--;
            }
        return 0;
      }
  return 0;
}

#pragma GCC optimize("Ofast,fast-math,unroll-loops")

#include <bits/stdc++.h>

#define int ll
//#define double long double
#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.1415926535

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair <int, int>;
using pld = pair <ld, ld>;

/*
const ll MAX_MEM = 3e6;
char MEM[MAX_MEM];
ll MEM_POS = 0;
void* operator new(size_t x)
{
    auto ret = MEM + MEM_POS;
    MEM_POS += x;
    assert(MEM_POS < MAX_MEM);
    return ret;
}
void operator delete(void*)
{}
*/

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen("lol.in", "r", stdin);
    //freopen("lol.out", "w", stdout);
}

vector <int> bpow(vector <int> a, int n, int k)
{
    vector <int> p(n);
    iota(all(p), 0);
    for (int i = 0; i < a.size(); ++i)
        swap(p[a[i]], p[a[i] - 1]);
    vector <int> res(n);
    iota(all(res), 0);
    while (k)
    {
        if (k & 1)
        {
            vector <int> r = res;
            for (int i = 0; i < n; ++i)
                res[i] = r[p[i]];
        }
        vector <int> r = p;
        for (int i = 0; i < n; ++i)
            p[i] = r[r[i]];
        k >>= 1;
    }
    return res;
}

signed main()
{
    Start();
    int n;
    cin >> n;
    vector <int> x(n);
    cin >> x;
    vector <int> d(n - 1);
    for (int i = 1; i < n; ++i)
        d[i - 1] = x[i] - x[i - 1];
    int m, k;
    cin >> m >> k;
    vector <int> a(m);
    cin >> a;
    for (auto &i : a)
        --i;
    a = bpow(a, n - 1, k);
    auto t = d;
    for (int i = 0; i < n - 1; ++i)
        d[i] = t[a[i]];
    d.pb(0);
    int sum = 0;
    for (int i = 0; i < n; ++i)
    {
        cout << x[0] + sum << endl;
        sum += d[i];
    }
    return 0;
}


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

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		vector<int> C; C.push_back(0);

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r1, r;

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = i; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r1.push_back(C[i] + C[j]);
				}
			}
		}

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

		r.push_back(r1[0]);

		for (int i = 0; i < r1.size(); i++)
		{
			if (r1[i - 1] != r1[i])
			{
				r.push_back(r1[i]);
			}
		}

		int ret = 0;

		for (int i = 0; i < r.size(); i++)
		{
			int ptr = upper_bound(r.begin(), r.end(), M - r[i]) - r.begin() - 1;

			if (ptr >= 0)
			{
				ret = max(ret, r[i] + r[ptr]);
			}
		}

		printf("%d\n", ret);
	}

	return 0;
}

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

using namespace std;
typedef long long ll;

int main(){
   int N, M;
   cin >> N >> M;
   vector<int> pos(N,0);
   int favs[N][M];
   for(int i = 0 ; i <N; i++){
      for(int j = 0; j <M; j++){
         cin >> favs[i][j];
         favs[i][j]--;
      }
   }
   vector<bool> used(M,false);
   int ans = N;
   for(int j = 0 ; j < M; j++){
      vector<int> cnt(M,0);
      int target = 0;
      int m=0;
      for(int i = 0 ; i <N; i++){
         int f = favs[i][pos[i]];
         cnt[f]++;
         if(cnt[f]>m){
            m=cnt[f];
            target = f;
         }
      }
      // delete target
      //cout << m << " " << target << endl;
      used[target]=true;
      ans = min(m,ans);
      if(j!=M-1){
         for(int i = 0 ; i <N; i++){
            while(used[favs[i][pos[i]]]==true) pos[i]++;
         }
      }
   }
   cout << ans << endl;
   return 0;
}


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

// ----------- define --------------
#define int long long
#define vi vector<int>
#define vc vector<char>
#define ii pair<int,int>
#define fi first
#define sc second
#define stoi stoll
#define all(x) (x).begin(),(x).end()
#define get_bit(x, k) ((x >> k) & 1)
// ---------------------------------

void MAIN() {
  int n;
  cin >> n;
  int ans = 0;
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    double _; cin >> _;
    int t = llround(_ * 1e9);
    int ct = 0, cf = 0;
    while (t % 2 == 0) {
      t /= 2;
      ct++;
    }
    while (t % 5 == 0) {
      t /= 5;
      cf++;
    }
    for (auto &j : m) {
      if (j.fi.fi + ct >= 18 && j.fi.sc + cf >= 18) {
        ans += j.sc;
      }
    }
    m[ii(ct, cf)]++;
  }
  cout << ans << '\n';
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) MAIN();
}

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int64_t res = 0;
  vector table(20, vector(20, 0));
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    auto pos = s.find(".");
    if (pos == string::npos) {
      s += string(9, '0');
    } else {
      s.erase(s.begin() + pos);
      s += string(9 - ((int)s.size() - pos), '0');
    }
    int64_t x = static_cast<int64_t>(stoll(s));
    pair<int, int> a = make_pair(0, 0);
    while (x % 2 == 0) {
      x /= 2;
      a.first++;
    }
    while (x % 5 == 0) {
      x /= 5;
      a.second++;
    }
    a.first = min(a.first, 18);
    a.second = min(a.second, 18);
    for (int j = 18 - a.first; j < 20; j++) {
      for (int k = 18 - a.second; k < 20; k++) {
        res += table[j][k];
      }
    }
    table[a.first][a.second]++;
  }
  cout << res << '\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int k = 55555;
int a[55555]={};
int main(){
    int n;
    cin >> n;
    for(int i=2;i<=k;i++){
        for(int j=i;j<=k;j+=i) a[j]++;
    }
    for(int i=2;i<=k;i++){
        if(a[i]==1){
            if(i%5==2&&n>0){
                cout << i << ' ';
                n--;
            }
        }
    }
    cout << endl;
}

#include<bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(ll i = 0; i < n; i++)
const ll INF = 1LL << 60;

vector<bool> IsPrime;

void sieve(size_t max){
    IsPrime.assign(max + 1, true);
    IsPrime[0] = false;
    IsPrime[1] = false;
    for(int i = 2; i*i <= max; i++){
        if(IsPrime[i])for(int j = 2; i*j <= max; j++) IsPrime[i*j] = false;
    }
}

int main(){
    sieve(55555);
    int cnt;
    vector<int> ans;
    for(int i = 1; i<= 55555; i++){
        if(i%5 == 1 && IsPrime[i] == true)ans.push_back(i);
    }
    int N; cin >> N;
    rep(i, N) cout << ans[i] << " ";
    cout << endl;
}


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

int n,m;
ll d[105][105];
bool v[105][105];

int main() {
	cin>>n>>m;
	for (int i=0;i<n;i++) for (int j=0;j<n;j++) {
		if (i==j) {
			d[i][j]=0;
			v[i][j]=1;
		}
		else d[i][j]=INF;
	}
	for (int i=0;i<m;i++) {
		int s,t,l; cin>>s>>t>>l;
		d[s][t]=l;
		v[s][t]=1;
	}
	for (int k=0;k<n;k++)
	  for (int i=0;i<n;i++)
	  for (int j=0;j<n;j++)
	    if (v[i][k] && v[k][j]) {
	    	d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	    	v[i][j]=1;
	    }
	for (int i=0;i<n;i++)
		if (d[i][i]<0) {
			cout<<"NEGATIVE CYCLE"<<'\n';
			return 0;
		}
	for (int i=0;i<n;i++) for (int j=0;j<n;j++) {
		if (d[i][j]==INF) cout<<"INF";
		else cout<<d[i][j];
		cout<<" \n"[j+1==n];
	}
}

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<"\n"
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define mp make_pair
#define sz(x) (ll)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using vc = vector<char>;
using vvc = vector<vc>;
template<class T> inline bool chmax(T& a, T b) {
 if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
 if (a > b) { a = b; return true; } return false;
}
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
int main(){
  ll n,q;
  cin>>n>>q;
  vector<pair<ll,pll>> p(n);
  rep(i,n){
    ll s,t,x;
    cin>>s>>t>>x;
    p[i].F=x;
    p[i].S=mp(s-x,t-x);
  }
  sort(all(p));
  set<ll> d;
  map<ll,ll> pp;
  rep(i,q){
    ll dd;
    cin>>dd;
    d.insert(dd);
    pp[dd]=INF;
  }
  rep(i,n){
    auto itr=d.upper_bound(p[i].S.S);
    auto itre=d.lower_bound(p[i].S.F);
    while(itr!=itre&&*itre<p[i].S.S){
      pp[*itre]=p[i].F;
      ll g=*itre;
      itre++;
      d.erase(g);
    }
  }
  for(auto hh:pp){
    if(hh.S==INF)COUT(-1);
    else COUT(hh.S);
  }
}


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

int Roman_to_Arabia(char c){
	if (c == 'I')return 1;
	else if (c == 'V')return 5;
	else if (c == 'X')return 10;
	else if (c == 'L')return 50;
	else if (c == 'C')return 100;
	else if (c == 'D')return 500;
	else if (c == 'M')return 1000;
	else return 0;
}

int main(){
	string roman;
	int num;

	while (cin >> roman){
		num = 0;

		for (int i = 0; i < roman.size(); ++i){

			if (Roman_to_Arabia(roman[i]) < Roman_to_Arabia(roman[i + 1]))
				num -= Roman_to_Arabia(roman[i]);
			else
				num += Roman_to_Arabia(roman[i]);
		}
		cout << num << endl;
	}
	return 0;
}


//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>

#define DIV 1000000007

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

int main() {
    string N;   cin >> N;
    int L = N.size();
    reverse(N.begin(), N.end());
    vector<int> a(L + 2);
    for (int i = 0; i < L; i++) {
        a[i] = N[i] - '0';
        //printf("a[%d]=%d ", i, a[i]);
    }

    vector<int> dp(L + 1);
    for (int i = 0; i <= L; i++) {
        if (a[i] < 5) dp[i] = a[i];
        else if (a[i] == 5) {
            dp[i] = 5;
            if (a[i + 1] >= 5) a[i + 1]++;
        }
        else {
            dp[i] = 10 - a[i];
            a[i + 1]++;
            //a[i + 1] %= 10;
        }
    }
    /*
    cout << endl << 0;
    for (int i = L - 1; i >= 0; i--) {
        cout << N[i];
    }
    cout << endl;
    */
    ll ans = 0;
    for (int i = L; i >= 0; i--) {
        //cout << dp[i];
        ans += dp[i];
    }

    //cout << endl;
    cout << ans << endl;
}

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

#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1

#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;

li dp[2][1001000];

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

    dp[0][0] = 0;
    dp[1][0] = 1;
    rep(i, sz(s)) {
        li num = s[i] - '0';
        dp[0][i + 1] = min(dp[0][i] + num, dp[1][i] + 10 - num);
        dp[1][i + 1] = min(dp[1][i] + 9 - num, dp[0][i] + num + 1);
    }
    cout << dp[0][sz(s)] << endl;

    return 0;
}


#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1.0)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << (n-2)*180 << endl;
}

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

int n;

int main() {
	read(n);
	print(180 * (n - 2));
}


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

using ll = long long;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n+1), dp[3];
	for (int i=0; i<=2; i++) {
		dp[i].resize(n+1);
		for (int j=1; j<=n; j++) {
			dp[i][j] = -1e18;
		}
	}
	for (int i=1; i<=n; i++) {
		cin >> a[i];
	}
	for (int i=1; i<=n; i++) {
		for (int j=0; j<=2; j++) {
			if (i-j-2 < 1) continue;
			for (int k=0; k<=2-j; k++) {
				dp[j+k][i] = max(dp[j+k][i], dp[k][i-j-2]+a[i]);
			}
		}
		if (i <= 3) {
			dp[max(0, i-2)][i] = a[i];
		}
	}
	ll ans;
	if ((n&1)) {
		ans = max(max(max(dp[1][n], dp[2][n]), max(dp[0][n-1], dp[1][n-1])), dp[0][n-2]);
	} else {
		ans = max(dp[1][n], max(dp[0][n], dp[0][n-1]));
	}
	cout << ans;
	return 0;
}

#include <iostream>
using namespace std;
#define el '\n'
typedef long long ll;
const int MAXN = 2e5+2;
int N, A[MAXN];
ll odd[MAXN], dp[MAXN];
int main(){
	cin.tie(0);
	cin.sync_with_stdio(0);
	cin >> N;
	for(int i = 1;i<=N;i++) cin >> A[i];
	odd[1] = A[1];
	for(int i = 2;i<=N;i++){
		if(i%2){
			odd[i] = odd[i-2]+A[i];
		}
	}
	dp[1] = 0;
	for(int i = 2;i<=N;i++){
		if(i%2){
			dp[i] = max(dp[i-2]+A[i], dp[i-1]);
		}else{
			dp[i] = max(dp[i-2]+A[i], odd[i-1]);
		}
	}
	cout << dp[N] << el;
}


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

vector<string> split(const string &str, char delim){
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos){
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main()
{
	string s;
	while(cin >> s)
	{
		vector<string> _s = split(s,',');
		int *values = new int[11];
		double *v = new double[2];

		for(int i=0;i<11;i++)
		{
			if(i == 0)
			{
				values[i] = 0;
			}
			else
			{
				values[i] = std::atoi(_s[i-1].c_str()) + values[i-1];
			}

		}

		for(int i=0;i<2;i++)
		{
			v[i] = std::atoi(_s[i+10].c_str());
		}
		double temp = v[0] / (v[0]+v[1]);
		temp *= values[10];

		bool flag = true;
		for(int i=0;i<10;i++)
		{
			if(values[i] <= temp && temp <= values[i+1] && flag)
			{
				cout << i+1 << endl;
				flag = false;
			}
		}
	}
	return 0;
}

#include<iostream>
using namespace std;

int main(void){
	int l[10];
	char c;
	double ls, x1, t, v1, v2;
	while(1){
		ls = 0;
		for(int i = 0; i < 10; i++){
			cin >> l[i] >> c;
			ls += l[i];
		}
		cin >> v1 >> c >> v2;
		if(cin.eof())break;
		x1 = t = 0.0;
		for(int i = 0; i < 10; i++){
			x1 += l[i];
			t = x1 / v1;
			if(x1 + t * v2 >=ls){
				cout << ++i << endl;
				break;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    int k;
    string s, t = "...";
    cin >> k >> s;
    if(s.size() <= k)
        cout << s << '\n';
    else {
        for (int i = 0; i < k; ++i) {
            cout << s[i];

        }
        cout << t;
    }

}

//高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
    for (T& x: vec) { is >> x; }
    return is;
}

int main() {
	ll ans = 0;
	ll tmp;
	//int n; cin >> n;
	vector<string> s(3);
	cin >> s[0] >> s[1] >> s[2];
	int who = 0;
	while(1){
		if(s[who].size()==0){
			cout << (char)('A' + who) << endl;
			break;
		}
		else{
			int next = s[who][0] - 'a';
			s[who] = s[who].substr(1);
			who = next;
		}
	}
	//cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = ll(1e12);
const ll mod = ll(1e9+7);
const double pi = acos(-1);
#define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i))
#define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i))
#define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i))
#define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i))
#define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i))
#define pf(x) cout << (x) << endl
#define all(x) (x).begin(),(x).end()
#define yes pf("Yes")
#define no pf("No")
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; }
int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};

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

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

ll factorial(ll n){
    ll ans = 1;
    rep1(i, n){
        ans *= i;
        ans %= mod;
    }
    return ans;
}

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

//modの値の確認をすること
int main(){
    ll n,ans = 0;
    cin >> n;
    vector<ll> s(n);
    rep0(i, n){
        ll a,b;
        cin >> a >> b;
        ans -= b;
        s[i] = a + b;
    }
    sort(all(s),greater<>());
    rep0(i, (n + 1) / 2)ans += s[i * 2];
    pf(ans);
    return 0;
}

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

int main(){
  int N,x,y;
  int ans=0;
  cin>>N;
  int A[N];//証言の数
  vector<pair<int,int>>T[N];

  //証言を入力
  for(int i=0;i<N;i++){
    cin>>A[i];
    for(int j=0;j<A[i];j++){
      cin>>x>>y;
      x--;
      T[i].push_back({x,y});
    }
  }

  //bit全探索
  for(int i=0;i<(1<<N);i++){
    int cnt=0;//正直者の数
    for(int j=0;j<N;j++){
      if(i & (1<<j)){
        cnt++;
      }
    }

    int flag=1;
    for(int j=0;j<N;j++){
      if(i & (1<<j)){
        //jが正直者として証言が矛盾しないかチェック　矛盾したらflag=0でbreak;
        for(int k=0;k<A[j];k++){
          if(i & 1<<(T[j].at(k).first)){
            if(T[j].at(k).second==1){
              continue;
            }
            else{
              flag=0;
              break;
            }
          }
          else{
            if(T[j].at(k).second==0){
              continue;
            }
            else{
              flag=0;
              break;
            }
          }
        }
      }
    }
    if(flag){
      ans=max(ans,cnt);
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <vector>
#include <map>
#include<cstdio>
#include<functional>
#include <bitset>
#include <iomanip>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define ll long long
using namespace std;
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

std::string substrBack(std::string str, size_t pos, size_t len) {
	const size_t strLen = str.length();

	return str.substr(strLen - pos, len);
}

int main()
{
	int n;
	cin >> n;
	vector<ll> al(n);
	vector<ll> bl(n);
	vector<ll> cl(n);
	rep(i, n) {
		cin >> al[i];
	}
	rep(i, n) {
		cin >> bl[i];
	}
	rep(i, n) {
		cin >> cl[i];
	}
	sort(al.begin(), al.end());
	sort(bl.begin(), bl.end());
	sort(cl.begin(), cl.end());

	ll ans = 0;
	/*rep(i, n) {
		auto lb = upper_bound(bl.begin(), bl.end(), al[i]);
		ll lbStart = lb - bl.begin();
		ll tmp = 0;
		for (int j = lbStart; j < cl.size(); j++)
		{
			auto cb = upper_bound(cl.begin(), cl.end(), bl[j]);
			tmp += cl.size() - (cb - cl.begin());
		}
		ans += tmp;
	}*/
	rep(i, n) {
		auto la = lower_bound(al.begin(), al.end(), bl[i]) - al.begin();
		auto lc = cl.size() - (upper_bound(cl.begin(), cl.end(), bl[i]) - cl.begin());
		ans += la * lc;
	}
	cout << ans << endl;
	//cout << fixed << setprecision(10) << ans << endl;
	system("pause");
}

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

int A[MAX],n;

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

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

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

	return 0;
}


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

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

    vector<vector<pair<int,int>>> data;
    data.resize(N);
    rep(i,N){
        cin>>A;
        data[i].resize(A);
        rep(j,A){
            cin>>data[i][j].first>>data[i][j].second;
            data[i][j].first--;
        }
    }

    int ans=0;
    for (int bit=0;bit<(1<<N);bit++){
        int cnt=0;
        bool ok=true;
        rep(i,N){
             if(!(bit & (1<<i))){
                    continue;
                }
            for (auto xy:data[i]){
                int x=xy.first;
                int y=xy.second;

                if((bit & (1 << x))&&y==0){
                    ok=false;

                }
                else if(!(bit & (1 << x))&&y==1){
                    ok=false;

                }

            }

        }
        if(ok==true){
            rep(i,N){
                if(bit&(1<<i)){
                    cnt++;
                }
            }
            ans=max(ans,cnt);
        }

    }
    cout<<ans<<endl;

    }

//{{{
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// clang-format off
void _print() { cerr << "]\033[0m\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifdef LOCAL
#define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}

const int N = 20;
const int mod = 1e9 + 7;

int n;
int a[N];

vector<pii> words[N];

bool is_h(int status, int idx) { return (status & (1 << idx)) > 0; }
bool check(int status)
{
    for (int i = 0; i < n; i++)
    {
        // check ith person
        bool is_honest = is_h(status, i);
        if (is_honest == false) continue;

        for (int j = 0; j < a[i]; j++)
        {
            int p = words[i][j].first;
            int val = words[i][j].second;

            bool is_p_h = is_h(status, p);
            if (is_p_h != val) return false;
        }
    }
    return true;
}

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
#endif

    while (cin >> n)
    {
        for (int i = 0; i < n; ++i)
        {
            words[i].clear();
            cin >> a[i];

            for (int j = 0; j < a[i]; ++j)
            {
                int x, y;
                cin >> x >> y;
                x--;
                words[i].push_back({x, y});
            }
        }

        int ans = -1;
        int lim = 1 << n;
        for (int st = 0; st < lim; st++)
        {
            // if (__builtin_popcount(st) <= ans) continue;

            // check
            if (check(st))
            {
                ans = max(ans, __builtin_popcount(st));
            }
        }

        // int v = __builtin_popcount(ans);
        cout << ans << endl;
    }

    return 0;
}


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

int main(){
  int r,g;
  double p;
  cin >> r >> g;
  //g = (r + p) / 2
  p = 2 * g - r;
  cout << p << endl;
}

#include <iostream>
using namespace std;

int main(int argc, char **argv) {
  int x, y;
  cin >> x;
  y = x * x * x;
  cout << y << endl;
}


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

int main() {
	int N;
    long double dx,dy,RR,avdist,prt;
    cin >>N;
    int arrive[N];
    vector<vector<long double>> b(N, vector<long double>(2));
    vector<vector<long double>> dist(N, vector<long double>(N,0));
    for(int i=0;i<N;i++){
      arrive[i]=i;
      cin >> b.at(i).at(0) >> b.at(i).at(1);
    }
    for(int i=0;i<N;i++){
      for(int j=0;j<N;j++){
        dx=b.at(i).at(0)-b.at(j).at(0);
        dy=b.at(i).at(1)-b.at(j).at(1);
        RR=pow(pow(dx,2)+pow(dy,2),0.5);
        dist.at(i).at(j)= RR;
        //cout << RR << endl;
    	}
    }
  avdist=0;
  prt=0;
   do{
    for(int k=0;k<N-1;k++){
     avdist+=dist.at(arrive[k]).at(arrive[k+1]);
     }
         prt+=1;
     }while(next_permutation(arrive, arrive + N));
       cout << fixed << setprecision(10) << avdist/prt << endl;
   return 0;

}

#include <bits/stdc++.h>

using namespace std;
int n , f[10] , v[10] , x[10] , y[10];
double sol;

double calc (int a , int b){

    if (a == 0)
        return 0;

    return sqrt((x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]));

}

void back(int pas , double sum){
    int i;
    if (pas == n + 1){
        sol += sum;
        return;
    }

    for (i = 1 ; i <= n ; i++){
        if (!f[i]){
            f[i] = 1;
            v[pas] = i;
            back(pas + 1 , sum + calc(v[pas - 1] , v[pas]));
            f[i] = 0;
        }
    }

}

int main()
{
    //FILE *fin = fopen ("a.in","r");
    int i;
    cin >> n;
    for (i = 1 ; i <= n ; i++){
        cin >> x[i] >> y[i];
    }

    back(1 , 0.0);

    for (i = 1 ; i <= n ; i++)
        sol /= i;

    cout << setprecision(8) << fixed << sol;

    return 0;
}


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

int main(void){

    string s;
    bool flag = true;
    cin >> s;

    int len = s.size();
    int count_c = 0;
    int i_c = -1;

    if(s[0] != 'A') flag = false;

    for(int i=2;i<=len-2;i++){
        if(s[i] == 'C') {
            count_c++;
            i_c = i;
        }
    }

    if(count_c != 1) flag = false;

    for(int i=1;i<len;i++){
        if((int)s[i] >= 97 && (int)s[i] <= 121){

        }
        else{
            if(i != i_c) flag = false;
        }
    }

    if(flag){
        cout << "AC" << endl;
    }
    else{
        cout << "WA" << endl;
    }
}

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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >
# define PQ priority_queue

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvs vector < vs >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";
# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);

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

string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};

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

const ld pie = 3.1415926535897932384626 ;
const ll mod = 1e9 + 7 ;

void solve(){

    vector<string> cards(3);
    fr(0,3) cin >> cards[i];

    int turn = 0;

    while(true){

        if(cards[turn].empty()){
                cout << char(('A'+turn)) ;
                    break;

        }int next = cards[turn][0] - 'a';
         cards[turn].erase(0,1);
         turn =next;
        }
    line
  	return;

}

int main(){
	fast
	int t=1;
	//cin >> t;
	while(t--){
             solve();
    }

	R 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define MXN 200005
vector<int> v[200005];
int color[200005];
int  dfs(int x,int col){
    color[x]=col;
    int ans=1;
    for(auto it:v[x]){
        if(color[it]==-1){
            ans &= dfs(it,col^1);
        }
        else{
            if(color[it]==col){
                ans=0;
            }
        }
    }
    return ans;
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    MEMS(color);
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        v[x].pb(y);
        v[y].pb(x);
    }
    LL a=0,b=0,c=0;
    for(int i = 1;i<=n;i++){
        if(color[i]==-1){
            if(v[i].empty())c++;
            else if(dfs(i,0))b++;
            else a++;
        }
    }
    //printf("?%d %d\n",a,b);
    printf("%lld\n",a*(a+b)*2-a*a+b*b*2+c*n*2-c*c);
    return 0;
}

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
    int D, G;
    cin >> D >> G;
    vector<int> P(D), C(D);
    rep(i, D) cin >> P[i] >> C[i];
    int res = INF;
    for(int bit = 0; bit < (1 << D); bit++) {
        int score = 0;
        int num = 0;
        for(int i = 0; i < D; i++) {
            if(bit & (1 << i)) {
                score += C[i];
                score += 100 * (i + 1) * P[i];
                num += P[i];
            }
        }
        vector<int> list;
        for(int i = D - 1; i >= 0; i--) {
            if(!(bit & (1 << i))) {
                for(int j = 0; j < P[i]; j++)
                    list.pb(100 * (i + 1));
            }
        }
        for(auto x : list) {
            if(score >= G)
                break;
            score += x;
            num++;
        }
        if(score >= G) {
            res = min(res, num);
        }
    }
    cout << res << endl;
    return 0;
}


/*
 * a.cc:
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 100;

/* typedef */

/* global variables */

int as[MAX_N];

/* subroutines */

/* main */

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

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

  for (int i = 0; i < n; i++) {
    if (i) putchar(' ');
    printf("%d", as[n - 1 - i]);
  }
  putchar('\n');

  return 0;
}


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

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w) != EOF && h && w) {
    int S = h*h+w*w;
    bool finished = false;
    for (int i=h+1; i<=150; i++) {
      int W = S - i*i;
      for (int j=i+1; j<150; j++) {
	if (j * j == W) {
	  finished = true;
	  printf("%d %d\n", i, j);
	  goto END;
	}
      }
    }
    while (!finished) {
      S++;
      for (int i=1; i<=150; i++) {
	for (int j=i+1; j<150; j++) {
	  if (S == i*i+j*j) {
	    printf("%d %d\n", i, j);
	    goto END;
	  } else if (S < i*i+j*j) {
	    break;
	  }
	}
      }
    }
  END: continue;
  }
}

#include <bits/stdc++.h>

using namespace std;

int mp[101][101];
int w, h;
int gx, gy;

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

bool dfs(int y, int x, int c)
{
	if (y < 1 || y > h || x < 1 || x > w || mp[y][x] != c) return false;

	if (y == gy && x == gx) return true;

	mp[y][x] = 0;

	bool res = false;

	for (int i = 0; i < 4; ++i) res = res || dfs(y + dy[i], x + dx[i], c);

	return res;
}

int main()
{
	while (cin >> w >> h, w || h) {
		int sx, sy;
		cin >> sx >> sy >> gx >> gy;
		int n; cin >> n;
		while (n--) {
			int c, d, x, y; cin >> c >> d >> x >> y;
			if (d) {
				for (int i = 0; i < 4; ++i) for (int j = 0; j < 2; ++j) mp[y+i][x+j] = c;
			} else {
				for (int i = 0; i < 2; ++i) for (int j = 0; j < 4; ++j) mp[y+i][x+j] = c;
			}
		}

		cout << (dfs(sy, sx, mp[sy][sx]) ? "OK" : "NG") << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define ft first
#define sd second
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<long long,long long> pll;

int main() {
    fastIO;
    int x, k;
    cin >> k >> x;
    for(int i = x-k+1; i <= x+k-1; i++){
        if(i != x+k-1) cout << i << " ";
        else cout << i << endl;
    }
    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 dec(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

ll mod(ll a){
  return (a % 1000000007);
}

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

int main(){
  ll a,b; cin >> a >> b;
  cout << max(a*b,max(a+b,a-b)) << endl;
}


#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()

using namespace std;

const int mod = 998244353;

const int N = 307;

int dp[N][N][2*N][2];

inline int sum(int x, int y) {
	if (x+y < 0) return x+y+mod;
	if (x+y >= mod) return x+y-mod;
	return x+y;
}

inline int mult(int x, int y) {
	return ((ll) x * y) % mod;
}

int main(){
#ifdef LOCAL
	freopen("I_input.txt", "r", stdin);
	//freopen("I_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	string s;
	cin >> s;

	int k;
	cin >> k;

	vector<int> arr;

	int R = 0;

	int now = 0;
	for (int i = 0; i < s.size(); ++i) {
		if (s[i] == '1') {
			R++;
			now++;
		}
		else {
			arr.push_back(now);
			now = 0;
		}
	}

	arr.push_back(now);

	dp[0][0][0][0] = 1;

	int cur = 0;

	for (int i = 0; i < arr.size(); ++i) {

		for (int was = 0; was <= R; ++was) {
			for (int delta = 0; delta <= 2*s.size(); ++delta) {
				int T = sum(dp[i][was][delta][0], dp[i][was][delta][1]);
				if (T == 0) continue;

				for (int be = 0; be <= arr[i] && was+be <= R; ++be) {
					dp[i+1][was + be][delta + arr[i] - be][0] = sum(dp[i+1][was + be][delta + arr[i] - be][0], T);
				}

				if (i + 1 < arr.size() && was + arr[i] + 1 <= R) {
					dp[i+1][was+arr[i]+1][delta+1][1] = sum(dp[i+1][was+arr[i]+1][delta+1][1], T);
				}
			}
		}

		for (int was = 0; was <= R; ++was) {
			for (int delta = 0; delta <= 2*s.size(); ++delta) {
				if (dp[i+1][was][delta][1] && was+1 <= R) {
					dp[i+1][was+1][delta+1][1] = sum(dp[i+1][was][delta][1], dp[i+1][was+1][delta+1][1]);
				}
			}
		}

		cur += arr[i];

		for (int was = 0; was < cur; ++was) for (int delta = 0; delta <= 2*s.size(); ++delta) {
			dp[i+1][was][delta][0] = 0;
			dp[i+1][was][delta][1] = 0;
		}

	}

	//cout << dp[3][2][2][0] << " " << R << endl;

	int ans = 0;

	for (int delta = 0; delta <= 2*s.size(); ++delta) if (delta <= 2*k) {

		int T = sum(dp[arr.size()][R][delta][0], dp[arr.size()][R][delta][1]);

		ans = sum(ans, T);
	}

	cout << ans;

}

#include <bits/stdc++.h>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<ll> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//const ll mod = 1e10;
//typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;

unordered_map<string, ll> check(int n, string &s) {
    unordered_map<string, ll> ans;
    rep(i, 1 << n) {
        string a, b;
        rep(j, n) {
            if ((i >> j) & 1) a.push_back(s[j]);
            else b.push_back(s[j]);
        }
        ans[a + ' ' + b]++;
    }
    return ans;
}

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

    string a = s.substr(0, n);
    string b = s.substr(n);

    unordered_map<string, ll> m1 = check(n, a);
    reverse(b.begin(), b.end());
    unordered_map<string, ll> m2 = check(n, b);

    ll ans = 0;
    for (auto &e : m1) {
        ll now = e.second * m2[e.first];
        ans += now;
    }
    cout << ans << endl;

}


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

int main()
{
    int N,K;
    cin >> N >> K;
    if(N < K){
        cout << 1 << endl;
        return 0;
    }
    if(N%K == 0){
        cout << 0 << endl;
        return 0;
    }
    cout << N%K+1 - N%K << endl;
}

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

int main(){

  int N, K;
  cin >> N >> K;

  if(N % K == 0){
    cout << '0' << endl;
  }
  else{
    cout << '1' << endl;
  }

}


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

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

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) {if (a > b) { a = b; return true; } return false; }

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

    ll n;
    cin>>n;
    vl as(n);
    cin>>as;

    if (n == 1) {
        if (as[0]) cout<<-1<<endl;
        else cout<<0<<endl;
        return 0;
    }

    ll ans = as.back();
    for (int i = n - 2; i >= 0; --i) {
        if (as[i] > i) {
            cout<<-1<<endl;
            return 0;
        }
        if (as[i] + 1 < as[i + 1]) {
            cout<<-1<<endl;
            return 0;
        }
        if (as[i] + 1 == as[i + 1]) continue;
        ans += as[i];
    }
    cout<<ans<<endl;
}

#include<bits/stdc++.h>
#define re register
using namespace std;
template <typename T>inline void read(T &x)
{
	x=0;int f=1;char c=getchar();
	for(;c>'9'||c<'0';c=getchar()) if(c=='-') f=-1;
	for(;c>='0'&&c<='9';c=getchar()) x=(x<<3)+(x<<1)+(c^48);
	x*=f;
}
const int maxn=1e5+5;
struct edge
{
	int to,nxt;
}e[maxn<<1];
int n,k,head[maxn],cnt;
int a[maxn],rem[maxn],ans;
void add(int u,int v)
{
	e[++cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
void dfs(int u)
{
	for(re int i=head[u];i!=0;i=e[i].nxt)
	{
		int v=e[i].to;
		dfs(v);
		if(rem[v]==k-1&&u!=1)
		{
			ans++;
			rem[v]=-1;
		}
		rem[u]=max(rem[u],rem[v]+1);
	}
}
int main()
{
	read(n),read(k);
	for(re int i=1;i<=n;++i)
		read(a[i]);
	if(a[1]!=1) ++ans;
	for(re int i=2;i<=n;++i)
		add(a[i],i);
	dfs(1);
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        if(n<3)cout << 0 << endl;
        else if(n<6)cout << 1 << endl;
        else {
           cout << n/3 << endl;
        }
    }
    return 0;
}

#define _USE_MATH_DEFINES //M_PI, M_E
#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long int
#define ar array
#define pb push_back
ll power(ll a,ll b){ll r=1;for(;b;b>>=1){if(b&1)r=r*a%mod;a=a*a%mod;}return r;}
using namespace std;

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


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

using Weight = int;
using Capacity = int;
struct Edge {
    int src, dst; Weight weight;
    Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

vector<int> articulationPoint(const Graph& g){
    int n = g.size(), idx;
    vector<int> low(n), ord(n), art;
    function<void(int)> visit = [&](int v){
        low[v] = ord[v] = ++idx;
        for(auto &e : g[v]){
            int w = e.dst;
            if(ord[w] == 0){
                visit(w);
                low[v] = min(low[v], low[w]);
                if((ord[v] == 1 && ord[w] != 2) ||
                   (ord[v] != 1 && low[w] >= ord[v])) art.push_back(v);
            } else low[v] = min(low[v], ord[w]);
        }
    };
    for(int u = 0; u < n; u++) if(ord[u] == 0){ idx = 0; visit(u); }
    return art;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    while(cin >> n >> m){
        Graph g(n);
        rep(i,m){
            int a,b;
            cin >> a >> b;
            g[a].emplace_back(a,b,0);
            g[b].emplace_back(b,a,0);
        }
        vector<int> arts = articulationPoint(g);
        sort(all(arts));
        auto last = unique(all(arts));
        for(auto it = arts.begin(); it != last; ++it) cout << *it << '\n';
    }
}

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

int w, h, n;
int sx, sy, gx, gy;
int t[102][102];

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

bool check(int x, int y, int c){
  if(x == gx && y == gy) return true;

  t[y][x] = -1;

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

    if(nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] != c) continue;
    if(check(nx, ny, c)) return true;
  }

  return false;
}

void putBlock(int c, int d, int x, int y){
  int addX = (d == 0 ? 4 : 2);
  int addY = (d == 0 ? 2 : 4);

  for(int i = y; i < y + addY; i++){
    for(int j = x; j < x + addX; j++){
      t[i][j] = c;
    }
  }
}

int main(){
  while(cin >> w >> h, w || h){
    cin >> sx >> sy; sx--; sy--;
    cin >> gx >> gy; gx--; gy--;
    cin >> n;

    memset(t, -1, sizeof(t));

    for(int i = 0; i < n; i++){
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      x--;
      y--;
      putBlock(c, d, x, y);
    }

    /*
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        printf("%2d", t[i][j]);
      }
      cout<<endl;
    }
    */

    if(t[sy][sx] != -1 && check(sx, sy, t[sy][sx])){
      cout << "OK\n";
    }
    else{
      cout << "NG\n";
    }
  }
}


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

int main() {
  int m = 999;
  string S;
  cin >> S;

  for (int i = 0; i < S.length() - 2; i++) {
    int num;
    istringstream ss;
    ss = istringstream(S.substr(i, 3));
    ss >> num;
    int res = num - 753;
    int diff = res < 0 ? -res : res;
    m = min(diff, m);
  }

  cout << m << endl;
}

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

int main(){
	string S;
	cin >> S;
	int LS = S.size();
	int k = 2;
	int ans = 1000;
	while (k<LS) {
		int tmp = stoi(S.substr(k-2,3));
		int l = abs(tmp-753);
		ans = min(ans, l);
		k++;
	}

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


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <stdio.h>
#include <unistd.h>
using namespace std;
typedef long long int ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define drep(i, n) for(int i = n - 1; i >= 0; i--)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl
#define PI 3.14159265359

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

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

int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  int now;
  cin >> now;
  ll ans = 0;
  rep (i, n - 1) {
    int tmp;
    cin >> tmp;
    ll diff = tmp - now;
    ans += min(b, diff * a);
    now = tmp;
  }
  P(ans);

  return 0;
}

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

int main() {
	ll n,a,b;
	cin >> n >> a >> b;
	int x[n];
	for (int i=0;i<n;i++) cin >> x[i];
	ll c=0;
	for (int i=0;i<n-1;i++) {
		if ((x[i+1]-x[i])*a>b) c+=b;
		else c+=a*(x[i+1]-x[i]);
	}
	cout << c;
}


#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define mem0(x) memset(x,0,sizeof(x))
#define meminf(x) memset(x,0x3f,sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace  std;

const int N = 1e5+5;
int a[N];
deque<int> q;
int num;
int main()
{
    ios::sync_with_stdio(0);
    int n;cin>>n;
    rep(i,1,n){
        cin>>a[i];
    }
    q.push_back(a[1]);++num;
    rep(i,2,n){

        // cout<<"t"<<i-1<<":";
        // rep(i,1,num){
        //     cout<<q[i-1]<<' ';
        // }
        //cout<<endl;
        int idx = upper_bound(q.begin(),q.end(),a[i],greater<int>())-q.begin();
        //cout<<"idx:"<<idx<<endl;
        if(idx==num){
            q.push_back(a[i]);
            ++num;
        }else{
            q[idx] = a[i];
        }
        //sort(q.begin(),q.end(),greater<int>());
    }
    cout<<num;

}

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

lli n;

int main(void){
	cin >> n;
	vector<lli> v(n);
	vector<lli> e(N), o(N);
	rep(i, n) cin >> v[i];
	rep(i, n){
		if(i%2 == 0){
			e[v[i]]++;
		}else{
			o[v[i]]++;
		}
	}
	auto p1 = max_element(e.begin(), e.end())-e.begin();
	auto p2 = max_element(o.begin(), o.end())-o.begin();
	if(p1 != p2){
		cout << n-e[p1]-o[p2] << endl;
	}else{
		sort(e.rbegin(), e.rend());
		sort(o.rbegin(), o.rend());
		cout << n-max(e[0]+o[1], e[1]+o[0]) << endl;
	}

	return 0;
}


/*
	Over ice, I'm freezing
	Beautiful eyes, deceiving
	We may die this evening
	Coughing, wheezing, bleeding...
*/
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Os")

#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define mp make_pair

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn=2e3+10, maxm=2e3+10, lg=18, mod=1e9+7, inf=1e18;

ll n,dp[3*maxn][maxn],M;

void add(ll &x,ll y){
	x+=y;
	if(x>=M) x-=M;
}

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

	cin>>n>>M;
	dp[0][0]=1;
	for(int i=0;i<3*n;i++)for(int j=0;j<=n;j++){
		add(dp[i+1][j],dp[i][j]);
		add(dp[i+2][j+1],dp[i][j]*(i+1)%M);
		add(dp[i+3][j+1],dp[i][j]*(i+1)%M*(i+2)%M);
	}
	ll ans=0; for(int i=0;i<=n;i++) ans+=dp[3*n][i];
	cout<<ans%M;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define int long long
int MOD;
void extgcd(int a, int b, int& x, int& y) {
	if (b != 0) {
		extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	} else {
		x = 1;
		y = 0;
	}
}

int mod_inverse(int a, int m) {
	int x, y;
	extgcd(a, m, x, y);
	return (m + x % m) % m;
}
int add(int& a, const int b) {
	a += b;
	if (a >= MOD) a -= MOD;
}
vector<int> fact;
vector<int> inv_fact;
vector<int> p2;
vector<int> inv_p2;
vector<int> inv_p6;
int C(int a, int b) {
	if (a < b || b < 0) return 0;

	return ((fact[a] * inv_fact[a - b]) % MOD) * inv_fact[b] % MOD;
}
int P(int a, int b) {
	if (a < b || b < 0) return 0;

	return ((fact[a] * inv_fact[a - b]) % MOD);
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N >> MOD;
	fact.resize(3 * N + 2, 1);
	inv_fact.resize(fact.size(), 1);
	p2.resize(3 * N + 2, 1);
	inv_p2.resize(3 * N + 2, 1);
	inv_p6.resize(3 * N + 2, 1);
	int inv2 = (MOD + 1) / 2;
	int inv6 = mod_inverse(6, MOD);
	for (int i = 1; i < fact.size(); i++) {
		fact[i] = (fact[i - 1] * i) % MOD;
		p2[i] = (p2[i - 1] * 2) % MOD;
		inv_fact[i] = mod_inverse(fact[i], MOD);
		inv_p2[i] = (inv_p2[i - 1] * inv2) % MOD;
		inv_p6[i] = (inv_p6[i - 1] * inv6) % MOD;
	}
	int res = 0;
	for (int n2 = 0; n2 <= 3 * N; n2++) {
		for (int n1 = 0; n1 <= 3 * N; n1++) {
			if (n2 * 2 + n1 > 3 * N) break;
			if (n2 > n1) continue;
			int n3 = 3 * N - n2 * 2 - n1;
			if (n3 % 3 != 0) continue;
			int ans = 1;
			n3 /= 3;

			ans = (ans * C(3 * N, n1)) % MOD;

			ans = (ans * P(3 * N - n1, n2 * 2)) % MOD;
			ans = (ans * inv_fact[n2]) % MOD;
			ans = (ans * inv_p2[n2]) % MOD;

			ans = (ans * fact[n3 * 3]) % MOD;
			ans = (ans * inv_fact[n3]) % MOD;
			ans = (ans * inv_p6[n3]) % MOD;
			ans = (ans * p2[n3]) % MOD;

			//cerr << n1 << " " << n2 << " " << n3 << " " << ans << endl;
			add(res, ans);
		}
	}
	cout << res << endl;
}


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

int main(){
    int N;
    cin >> N;
    if(N == 1) {
        cout << "Hello"<< " " <<"World" << endl;
    }
    else {
        int A,B;
        cin >> A >> B;
        cout << A+B << endl;
    }
}

#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()
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

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

ll mod(ll a){
  return (a % 1000000007);
}

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

int main(){
  ll n; cin >> n; ll a,b; cin >> a >> b;
  if(n==1) cout << "Hello World" << endl;
  else cout << a+b << endl;
}


#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

const long long int MOD = 1000000007;
//const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R;

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

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	cin >> M >> K;
	vector<int>p(N - 1);
	for (int i = 0; i < N - 1; i++)p[i] = i;
	for (int i = 0; i < M; i++) {
		cin >> L;
		swap(p[L - 2], p[L - 1]);
	}
	vector<vector<int>>to(62, vector<int>(N - 1));
	for (int i = 0; i < N - 1; i++)to[0][i] = p[i];
	for (int i = 1; i < 62; i++) {
		for (int j = 0; j < N - 1; j++) {
			to[i][j] = to[i - 1][to[i - 1][j]];
		}
	}
	for (int i = 0; i < N - 1; i++) {
		long long int box = K;
		int cnt = i;
		for (int j = 0; j < 62; j++) {
			if ((box >> j) & 1) {
				cnt = to[j][cnt];
			}
		}
		p[i] = cnt;
	}
	long long int num = v[0];
	cout << num << endl;
	for (auto i : p) {
		//cout << i << endl;
		num += v[i+1]-v[i];
		cout << num << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 5;

int n, m, a[N], label[N], label2[N];
ll k;
ll x[N], d[N], ans[N];

vector <vector <int> > cycles;
bool vis[N];
void DFS(int v)
{
    cycles.back().pb(v);
    vis[v] = true;
    if(vis[label[v]])
        return;
    DFS(label[v]);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> x[i];
        d[i] = x[i] - x[i - 1];
        label[i] = i;
    }

    cin >> m >> k;
    for(int i = 1; i <= m; i++)
    {
        cin >> a[i];
        swap(label[a[i]], label[a[i] + 1]);
    }
    for(int i = 1; i <= n; i++)
    {
        if(!vis[i])
        {
            cycles.pb(vector <int> ());
            DFS(i);
        }
    }

    for(int i = 0; i < cycles.size(); i++)
    {
        int sz = k % (int)cycles[i].size();
        for(int j = 0; j < cycles[i].size(); j++)
            label2[cycles[i][(j + sz) % cycles[i].size()]] = cycles[i][j];
    }

    for(int i = 1; i <= n; i++)
        ans[label2[i]] = d[i];

    for(int i = 1; i <= n; i++)
        ans[i] = ans[i] + ans[i - 1];

    for(int i = 1; i <= n; i++)
        cout << fixed << setprecision(9) << ans[i] << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
namespace FAST_IO
{
	template<typename T> void read(T &a)
	{
		a=0;
		int f=1;
		char c=getchar();
		while(!isdigit(c))
		{
			if(c=='-')
			{
				f=-1;
			}
			c=getchar();
		}
		while(isdigit(c))
		{
			a=a*10+c-'0';
			c=getchar();
		}
		a=a*f;
	}
	template <typename T> void write(T a)
	{
		if(a<0)
		{
			a=-a;
			putchar('-');
		}
		if(a>9)
		{
			write(a/10);
		}
		putchar(a%10+'0');
	}
	template <typename T> void writeln(T a)
	{
		write(a);
		puts("");
	}
}
int n,q[101],tot,tt=1;
double mins=190000;
double avg;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>q[i];
		tot+=q[i];
	}
	avg=tot*1.0/n;
	for(int i=1;i<=n;i++)
	{
		if(abs(q[i]-avg)<mins)
		{
			mins=abs(q[i]-avg);
			tt=i;
		}
	}
	cout<<tt-1;
}

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

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

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

int main() {
    int N;
    cin >> N;
    vector<double> a(N);
    REP(i,N) cin >> a[i];

    double av = accumulate(ALL(a),double(0))/N;
    int ans  = 0;
    double shortest = INF;
    REP(i,N) {
        if (chmin(shortest,abs(av-a[i]))) {
            ans = i;
        }
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
  int n,q,l,r,i;
  string s;
  vector<int> a;
  cin>>n>>q>>s;
  for(i=0;i<n-1;i++) if(s.at(i)=='A'&&s.at(i+1)=='C') a.push_back(i+1);
  for(i=0;i<q;i++){
    cin>>l>>r;
    cout<<distance(lower_bound(a.begin(),a.end(),l),lower_bound(a.begin(),a.end(),r))<<endl;
  }
}

#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,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;

void dfs(vector<vector<pair<int,ll>>> &graph,
 int now, int past,ll sum, vector<ll> &dist){
    dist[now]=sum;
    for(auto next:graph[now]){
       if(next.first==past)continue;
       dfs(graph, next.first, now, sum+next.second, dist);
    }
}
int main(){
   int n;
   cin >> n;
   vector<vector<pair<int,ll>>> graph(n);
   vector<ll> dist(n);
   rep(i, 0, n-1){
      int a, b;
      ll c;
      cin >> a >> b >> c;
      a--, b--;
      graph[a].push_back({b, c});
      graph[b].push_back({a, c});
   }
   int q, k;
   cin >> q >> k;
   k--;
   dfs(graph, k, -1, 0, dist);
   vector<ll>ans(q);
   rep(i, 0, q){
      int x, y;
      cin >> x >> y;
      ans[i] = dist[x-1]+dist[y-1];
   }
   rep(i, 0, q)cout<< ans[i] << endl;
   return 0;
}


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

typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9+7;

#define pi pair<int,int>
#define pl pair<lld,lld>

#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl

#define mem0(x) memset(x,0,sizeof(x))
#define fillnum(x,n) fill(begin(x),end(x),n)
#define asort(x) sort(x.begin(),x.end())
#define dsort(x,t) sort(x.begin(),x.end(),greater<t>())
#define vuniq(x) x.erase(unique(x.begin(), x.end()), x.end())

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)

int main() {
    ll K,X;
    cin >> K >> X;
    for (int i = X-K+1; i <= X+K-1; i++) {
        cout << i;
        if(i == K+X-1){
            cout << endl;
        } else {
            cout << " ";
        }
    }
}

#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array)     (sizeof(array) / sizeof(array[0]))
#define res(i,n) for(int i=n;;i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define REV(i,n) for(int i=n-1;i>0;i--)
#define req(i,n,m) for(int i=n;i<m;i++)
#define REQ(i,n,m,l) for(int i=n;i<m;i+=l)
#define _GLIBCXX_DEBUG
int INF = 1e9 + 7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1)
{
    unsigned day;
    if (dow < dow1) dow += 7;
    day = dow - dow1;
    day += 7 * n - 6;
    return day;
}
signed gcd(long long x, long long y) {
    if (y == 0)return x;
    return gcd(y, x % y);
}
signed lcm(long long x, long long y) {
    return x / gcd(x, y) * y;
}
unsigned DayToWeekNumber(unsigned day)
{
    return (day - 1) / 7 + 1;
}
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0)
{
    return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main() {
    int N, K; cin >> N >> K;
    vector<int>l(N);
    rep(i, N)cin >> l[i];
    sort(l.begin(), l.end());
    reverse(l.begin(),l.end());
    int ans = 0;
    rep(i, K)ans += l[i];
    cout << ans << endl;
}


#include<bits/stdc++.h>

#define EB emplace_back
#define int long long
#define F first
#define S second
#define N 500005
#define all(x) x.begin(),x.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

using namespace std;

#define rt return

void test_case(){

  string s; cin >> s;
  int cnt = 0, ans = 0;
  for(int i = 0; i < 3; i++){
    if(s[i] == 'R') cnt++;
    else{
      ans = max(ans, cnt);
      cnt = 0;
    }
  }
  ans = max(ans, cnt);
  cout << ans;
}

int32_t main(){
	IOS;
	int tt = 1;
	//cin >> tt;
	while(tt--){
    test_case();
    cout << endl;
	}
	return 0;
}

#include <iostream>
#include <vector>
using namespace std;
int main(){
    int N;
    cin >> N;
    vector<int> num(55556);
    for(int i = 0; i < 55556; i++) num[i] = i;
    num[0] = -1;
    num[1] = -1;
    for(int i = 2; i < 55556; i++){
        if(num[i] == -1) continue;
        int t = 2;
        while(i * t <= 55555){
            num[i * t] = -1;
            t++;
        }
    }
    vector<int> sosuu;
    for(int i = 0; i < 55556; i++){
        if(num[i] != -1) sosuu.push_back(num[i]);
    }
    vector<int> cnt(5, 0);
    for(int i = 0; i < sosuu.size(); i++){
        cnt[sosuu[i] % 5]++;
    }
    int t = 0;
    for(int i = 0; i < sosuu.size(); i++){
        if(t == N) break;
        if(sosuu[i] % 5 == 1) {
            cout << sosuu[i] << ' ';
            t++;
        }
    }
    cout << endl;
}


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

int main() {
   int N, K;
   cin >> N >> K;
   if( N % K == 0 ) cout << 0 << endl;
   else cout << 1 << endl;
}

#include <bits/stdc++.h>

#define NREP() for(ll i = 0; i < n; i++)
#define REP(i, n) for(ll i = 0; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MSG(x) cout << x << endl;
#define IPT(x) ll t; cin >> t; x.push_back(t);
#define YN(x) x ? cout << "YES" << endl : cout << "NO" << endl;
#define Yn(x) x ? cout << "Yes" << endl : cout << "No" << endl;
#define yn(x) x ? cout << "yes" << endl : cout << "no" << endl;

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<string> vvs;

const static ll MOD = 1e9 + 7;

int main() {
    ll x, y;
    cin >> x >> y;

    ll ans = 0;
    if (x == 3) {
        ans += 100000;
    } else if (x == 2) {
        ans += 200000;
    } else if (x == 1) {
        ans += 300000;
    }

    if (y == 3) {
        ans += 100000;
    } else if (y == 2) {
        ans += 200000;
    } else if (y == 1) {
        ans += 300000;
    }

    if (x == 1 && y == 1) {
        ans += 400000;
    }

    MSG(ans)
}


#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
void dice1();
void dice2();
void dice3();
void dice4();
class DICE{
	int face[6],a;
public:
	bool operator==(const DICE&di)const{
		int cont=0;
		for(int i=0;i<6;++i){
			if(face[i]==di.face[i]){
				cont++;
			}
		}
		if(cont==6){
			return true;
		}
		else{
			return false;
		}
	}
	bool operator!=(const DICE&di)const{
		return !(*this==di);
	}
	void set(int&f,int&f1,int&f2,int&f3,int&f4,int&f5){
		face[0]=f;face[1]=f1;face[2]=f2;face[3]=f3;face[4]=f4;face[5]=f5;
	}
	void N(){
		a=face[0];
		face[0]=face[1];
		face[1]=face[5];
		face[5]=face[4];
		face[4]=a;
	}
	void E(){
		a=face[0];
		face[0]=face[3];
		face[3]=face[5];
		face[5]=face[2];
		face[2]=a;
	}
	void S(){
		a=face[0];
		face[0]=face[4];
		face[4]=face[5];
		face[5]=face[1];
		face[1]=a;
	}
	void W(){
		a=face[0];
		face[0]=face[2];
		face[2]=face[5];
		face[5]=face[3];
		face[3]=a;
	}
	void output(){
		cout<<face[0]<<endl;
	}
	int up(){
		return face[0];
	}
	void turn(){
		a=face[1];
		face[1]=face[3];
		face[3]=face[4];
		face[4]=face[2];
		face[2]=a;
	}
	int front(){
		return face[1];
	}
	int right(){
		return face[2];
	}
	void allout(){
		for(int i=0;i<6;++i){
			cout<<face[i]<<" ";
		}
		cout<<endl;
	}
	void toupper(int n){
		if(n<=3){
			a=face[0];
			face[0]=face[1];
			face[1]=face[5];
			face[5]=face[4];
			face[4]=a;
		}
		else if(n==4){
			a=face[0];
			face[0]=face[3];
			face[3]=face[5];
			face[5]=face[2];
			face[2]=a;
		}
		else if(n==5){
			a=face[0];
			face[0]=face[3];
			face[3]=face[5];
			face[5]=face[2];
			face[2]=a;
			a=face[0];
			face[0]=face[3];
			face[3]=face[5];
			face[5]=face[2];
			face[2]=a;
		}

	}
};
bool judge(DICE,DICE);
int main(){
	dice4();
	return 0;
}
void dice1(){
	DICE a;
	int d[6];string str;
	for(int i=0;i<6;++i){
		cin>>d[i];
	}
	a.set(d[0],d[1],d[2],d[3],d[4],d[5]);
	getline(cin,str);
	getline(cin,str);
	for(int i=0;i<str.size();++i){
		if(str[i]=='E'){
			a.E();
		}
		else if(str[i]=='S'){
			a.S();
		}
		else if(str[i]=='W'){
			a.W();
		}
		else if(str[i]=='N'){
			a.N();
		}
	}
	a.output();
	return;
}
void dice2(){
	DICE a;
	int d[6],p,u;
	for(int i=0;i<6;++i){
		cin>>d[i];
	}a.set(d[0],d[1],d[2],d[3],d[4],d[5]);
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>p>>u;
		int k=0;
		while(1){
			k++;
			if(k<=5){
				a.E();
			}
			else{
				a.N();
			}
			if(p==a.up()){
				break;
			}
		}
		k=0;
		while(1){
			k++;
			a.turn();
			if(u==a.front()){
				break;
			}
		}
		cout<<a.right()<<endl;
	}
	return;
}
void dice3(){
	DICE a,b;
	int d[6][2];
	for(int i=0;i<2;++i){
		for(int j=0;j<6;++j){
			cin>>d[j][i];
		}
	}
	a.set(d[0][0],d[1][0],d[2][0],d[3][0],d[4][0],d[5][0]);
	b.set(d[0][1],d[1][1],d[2][1],d[3][1],d[4][1],d[5][1]);
	if(judge(a,b)){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
}
bool judge(DICE a,DICE b){
	bool bo1=false,bo2=false;
	for(int i=0;i<6;++i){
		b.toupper(i);
		for(int j=0;j<4;++j){
			if(a==b){
				return true;
			}
			b.turn();
		}

	}
	return false;
}
void dice4(){
	int n,d[6][101];
	DICE a[101];
	cin>>n;
	for(int i=0;i<n;++i){
		for(int j=0;j<6;++j){
			cin>>d[j][i];
		}
	}
	for(int i=0;i<n;++i){
		a[i].set(d[0][i],d[1][i],d[2][i],d[3][i],d[4][i],d[5][i]);
	}
	for(int i=0;i<n-1;++i){
		for(int j=i+1;j<n;++j){
			if(judge(a[i],a[j])){
				cout<<"No"<<endl;
				return ;
			}
			else {

			}
		}
	}
	cout<<"Yes"<<endl;
	return ;
}

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

class Dice{
public:
  int face[6];
  Dice(){}

  void roll_x(){ roll(0,3,5,2);}
  void roll_y(){ roll(0,1,5,4);}
  void roll_z(){ roll(1,2,4,3);}
  void roll(int i, int j, int k, int l){
    int t = face[i]; face[i] = face[j]; face[j] = face[k]; face[k] = face[l]; face[l] = t;
  }
};

bool check(Dice d1,Dice d2){
  for (int i = 0; i < 6; i++){
    if (d1.face[i] != d2.face[i]) return false;
  }
  return true;
}

bool equal(Dice d1, Dice d2){
  for(int j = 0; j < 6; j++){
    for(int k = 0; k < 4; k++){
      if(check(d1, d2)) return true;
      d1.roll_z();
    }

    if(check(d1,d2)) return true;
    if (j == 2) d1.roll_y();
    else d1.roll_x();
  }
  return false;
}

bool equal_all(Dice d[],int n){
  for(int i = 0; i < n-1; i++){
    for(int j = i+1; j < n; j++){
      if(equal(d[i],d[j])) return true;
    }
  }
  return false;
}

int main(){
  int n;
  cin >> n;
  // ???????????????????????????
  Dice d[100];
  for(int i = 0; i < n; i++){
    for(int j = 0; j < 6; j++){
    cin >> d[i].face[j];
    }
  }

  //??????
  if(equal_all(d,n)) cout << "No" << endl;
  else cout << "Yes" << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

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

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

void solve(int n){
	vector<pair<double,string>> v(n);
	double p,a,b,c,d,e,f,s,m;
	string l;
	rep(i,n){
		cin>>l;
		v[i].sc=l;
		cin>>p>>a>>b>>c>>d>>e>>f>>s>>m;
		v[i].fs=(m*s*f-p)/(a+b+c+(d+e)*m);
	}
	sort(all(v),[](auto a,auto b){
		if(a.fs!=b.fs)return a.fs>b.fs;
		else return a.sc<b.sc;
	});
	rep(i,n)cout<<v[i].sc<<endl;
	cout<<"#\n";
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	while(cin>>n,n)solve(n);
}

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

int main() {
	int N;
	while ( cin >> N ) {
		if ( N == 0 ) {
			break;
		}
		typedef pair<double, string> P;
		vector<P> ans;
		while ( N-- ) {
			string l;
			int p;
			cin >> l >> p;
			int t = 0;
			int d = 0;
			for ( int i = 0; i < 5; i++ ) {
				int a;
				cin >> a;
				if ( i <= 2 ) t += a;
				else d += a;
			}
			int f, s, m;
			int sum = 0;
			cin >> f >> s >> m;

			t += d*m;
			sum = f*s*m;
			ans.push_back(P(-((double)(sum-p)/(double)t), l));
		}
		sort(ans.begin(), ans.end());

		for ( int i = 0; i < ans.size(); i++ ) {
			cout << ans[i].second << endl;
		}
		cout << '#' << endl;
	}
	return 0;
}


#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head

const int N = 1e6+50;
int n,m,a[N],b[N];
int sum[N];

ll solve(int *a, int n) {
	REP(i,1,n) sum[i]=(sum[i-1]+a[i])%P;
	ll ans = 0;
	REP(i,1,n) ans=(ans+(ll)(n-i)*a[i]-(sum[n]-sum[i]))%P;
	if (ans<0) ans += P;
	return ans;
}
int main() {
	scanf("%d%d",&n,&m);
	REP(i,1,n) scanf("%d",a+i);
	REP(i,1,m) scanf("%d",b+i);
	printf("%lld\n",solve(a,n)*solve(b,m)%P);
}

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

int main() {
    int N,K;cin>>N>>K;
    vector<int> p(N);
    for (int i = 0; i < N; i++){
        cin>>p[i];
    }
    int ans=0;
    for (int i = 0; i < K; i++){
        ans+=p[i]+1;
    }
    int sum=ans;
    for (int i = 0; i < N-K; i++){
        sum=sum-p[i]+p[i+K];
        ans=max(ans,sum);
    }
    cout<<ans/2;
    if(ans%2!=0)cout<<".5"<<endl;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

typedef long long ll;

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

int used[N * N];
vector<int> g[N * N];

int n, m;
int a[N][N];

int get(int i, int j) {
	return i * m + j;
}

int ok(int i, int j) {
	return 0 <= i & i < n && 0 <= j && j < m;
}

void dfs(int cur, int &c0, int &c1) {
	used[cur] = 1;
	if (a[cur / m][cur % m] == 0) {
		c0++;
	} else {
		c1++;
	}
	for (auto t : g[cur]) {
		if (!used[t]) {
			dfs(t, c0, c1);
		}
	}
}

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

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			char c;
			cin >> c;
			a[i][j] = (c == '#');
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			for (int f = 0; f < 4; f++) {
				int i1 = i + dx[f];
				int j1 = j + dy[f];
				if (ok(i1, j1) && a[i][j] != a[i1][j1]) {
					g[get(i, j)].push_back(get(i1, j1));
				}
			}
		}
	}
	ll ans = 0;
	for (int i = 0; i < n * m; i++) {
		if (!used[i]) {
			int c0 = 0;
			int c1 = 0;
			dfs(i, c0, c1);
			ans += c0 * c1;
		}
	}
	cout << ans << '\n';
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
#define fo(i,s,e_ex) for(i=s;i<e_ex;i++)
#define Fo(i,k,n) for(i=k;k<n?i<=n:i>=n;k<n?i+=1:i-=1)
#define endl '\n'
#define MOD 1000000007//998244353
#define pbb push_back
#define mpp make_pair
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define mset(arr,val) memset(arr,val,sizeof(arr))
ll a[20][20],n;
ll dp[66000],gc[66000];
vi sm[66000];
bool dpp[66000];
void findsm(ll pos,ll bitmask,ll submask){
    if(pos==n){
        if(setbits(submask)==0) return;
        sm[bitmask].pbb(submask);
        return;
    }
    findsm(pos+1,bitmask,submask);
    findsm(pos+1,bitmask|(1<<pos),submask);
    findsm(pos+1,bitmask|(1<<pos),submask|(1<<pos));
}
void findgc(){
    for(ll b=1;b<pow(2,n);b++){
        //b is the bitmask
        gc[b]=0;
        for(ll i=0;i<n;i++){
            if(        ((1<<i) & b)==0   ) continue;
            for(ll j=i+1;j<n;j++){
                if(        ((1<<j) & b)==0   ) continue;
                gc[b]+=a[i][j];
            }
        }
    }
}
ll findans(ll bitmask){
    if(bitmask==0) return 0;
    if(dpp[bitmask]) return dp[bitmask];
    ll ans=LLONG_MIN;
    for(auto submask:sm[bitmask]){
        ans = max(ans,findans(bitmask^submask)+gc[submask]);
    }
    dpp[bitmask]=true;
    return dp[bitmask]=ans;
}
void solve(ll caseno){
	ll i,j,ans;
    cin>>n;
    fo(i,0,n){
        fo(j,0,n){
            cin>>a[i][j];
        }
    }
    findsm(0,0,0);
    findgc();
    mset(dpp,0);
    ans = findans(pow(2,n)-1);
    cout<<ans<<endl;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll t=1;
	//cin>>t;
	for(ll i=1;i<=t;i++){
		solve(i);
	}
	return 0;
}


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

int main(){
	char str[64];
	int i;
	cin >> str;
	for(i=1;i<=strlen(str);i++){
		cout << str[strlen(str)-i];
	}
	cout << endl;
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  int n,x;
  cin >> n;
  for(int i = 1; i <= n; i++){
    if(i % 3 == 0){
      cout << " " << i;
    }else{
      if(i % 10 == 3){
        cout << " " << i;
      }else{
        x = i;
        while(x /= 10){
          if(x % 10 == 3){
            cout << " " << i;
            break;
          }
        }
      }
    }
  }
  cout << endl;
  return 0;
}


#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define ALL(a) (a).begin(),(a).end()

using namespace std;
using ll = long long;

const double PI = 3.14159265358979;

void solve()
{
  int n;
  cin >> n;
  int sum = 0;
  int tmp = n;
  while(tmp > 0)
  {
    sum += tmp % 10;
    tmp /= 10;
  }
  cout << (n % sum == 0 ? "Yes" : "No");
}

int main()
{
  fastio;
  solve();

  return 0;
}

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

#include <regex>
#include <locale>
#include <iomanip>

using namespace std;
using ll = long long;

#define OVERLOAD3(_1,_2,_3,name,...) name
#define REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(int i=int(a),i##_Len=int(b);i<i##_Len;++i)
#define rep(...) OVERLOAD3(__VA_ARGS__,REPI,REP,)(__VA_ARGS__)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())

int main () {
  string s; cin >> s;
  s.replace(s.begin(), s.begin() + 4, "2018");
  cout << s << endl;
}


//https://atcoder.jp/contests/abc162/tasks/abc162_b
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n;
	cin>>n;
	ll sum1=0;
	sum1=(n*(n+1))/2;
	ll n3;
	n3=(n)/3;
	ll n5;
	n5=(n)/5;
	ll n15;
	n15=(n)/15;
	ll a3;
	a3=3+(n3-1)*3;
	ll a5;
	a5=5+(n5-1)*5;
	ll a15;
	a15=15+(n15-1)*15;
	ll sum3=0;
	sum3=((n3)*(3+a3))/2;
	ll sum5=0;
	sum5=((n5)*(5+a5))/2;
	ll sum15=0;
	sum15=((n15)*(15+a15))/2;
	ll sumfin=0;
	sumfin=sum3+sum5-sum15;
	ll ans=0;
	ans=sum1-sumfin;
	cout<<ans<<"\n";

	return 0;
}

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

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

  long long fb_sum = 0;

  for (int elem = 1; elem <= N; elem++){
    if (elem % 3 != 0 & elem % 5 != 0) fb_sum += elem;
  }

  cout << fb_sum << endl;
}


#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long
#define mod107 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using Graph = vector<vector<int>>;
#define grepq priority_queue<double, std::vector<double>, std::greater<double>>
#define all(v) v.begin(), v.end()
#define PI acos(-1)
const ll INF = 10010001010;
const int inf = 1000034000;
typedef vector<ll> vecl;
typedef vector<int> veci;
typedef tuple<string, int, int> TUP;

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

int main()
{
    int N;
    cin >> N;
    ll ans = 0;
    vector<int> A(N);
    vector<int> B(N);
    vector<int> C(N);
    for (int i = 0; i < N;i++)
    {
        cin >> A[i];
    }
    for (int i = 0; i < N; i++)
    {
        cin >> B[i];
    }
    for (int i = 0; i < N; i++)
    {
        cin >> C[i];
    }
    sort(all(A));
    sort(all(B));
    sort(all(C));
    for (int i = 0; i < N;i++)
    {
        auto itA = lower_bound(A.begin(),A.end(),B[i]);
        auto itC = upper_bound(C.begin(), C.end(),B[i]);
        ll a = itA - A.begin();
        ll b = C.end() - itC;
        //cout << a <<" " << b << endl;
        ans += a * b;
    }
    cout << ans << endl;
}

#include <iostream>
#include <vector>
#include <cassert>
#include <map>

using ll = long long;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 200000;
int v[1 + nmax];
bool valid(int n) {
  if(0 < v[1])
    return 0;
  for(int i = 2; i <= n; i++)
    if(v[i - 1] + 1 < v[i])
      return 0;
  return 1;
}
int frec[1 + nmax];

int main() {
  int n;
  std::cin >> n;
  for(int i = 1;i <= n; i++)
    std::cin >> v[i];
  if(valid(n) == 0)
    std::cout << -1;
  else {
    ll result = 0;
    for(int i = n; 1 <= i; i--) {
      if(frec[i - v[i]] == 0) {
        result += v[i];
        frec[i - v[i]] = 1;
      }
    }
    std::cout << result;
  }
  return 0;
}