#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

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

const int N = 16;
const int M = (1 << N);
const int INF = 0x3f3f3f3f;

int dp[N][M], g[N][N], n, m, rv[M];

int f(int st, int msk){
    if(st == n - 1) return 0;
    if(dp[st][msk] != -1) return dp[st][msk];
    int sol = INF;
    for(int msk1 = (msk - 1) & msk; msk1 ; msk1 = (msk1 - 1) & msk){
        int msk2 = msk ^ msk1;
        if(msk1 & (1 << (n - 1))) continue;
        if(msk2 & (1 << st)) continue;
        int sum = 0;
        for(int t1 = msk1; t1 ; t1 -= t1 & (-t1)){
            for(int t2 = msk2 ; t2 ; t2 -= t2 & (-t2)){
                sum += g[rv[t1 & (-t1)]][rv[t2 & (-t2)]];
            }
        }
        for(int t2 = msk2 ; t2 ; t2 -= t2 & (-t2)){
            if(g[st][rv[t2 & (-t2)]]){
                sol = min(sol, f(rv[t2 & (-t2)], msk2) + sum - g[st][rv[t2 & (-t2)]]);
            }
        }

    }
    return dp[st][msk] = sol;
}

int main(){
    memset(dp, -1, sizeof(dp));
    for(int i = 0;i<N;i++) rv[(1 << i)] = i;
    memset(dp, -1, sizeof(dp));
    scanf("%d%d", &n, &m);
    for(int i = 0;i<m;i++){
        int x, y;scanf("%d%d", &x, &y);x--;y--;
        scanf("%d", &g[x][y]);
        g[y][x] = g[x][y];
    }
    printf("%d\n", f(0, (1 << n) - 1));
}

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

const int N = 16;
const int inf = 1e9;

int n, m;
int e[N][1 << N];
int c[N][N];
int dp[N][1 << N];

void minimize(int &x, int y) { x = (x > y) ? y : x; }

void prep() {
	for (int mask = 0; mask < (1 << n); ++mask) {
		for (int i = 0; i < n; ++i) if (!(mask >> i & 1)) {
			for (int j = 0; j < n; ++j) if (mask >> j & 1) e[i][mask] += c[i][j];
		}
	}
}

void brute(int pos, int cmask, int cost, int &u, int &mask) {
// dp[u][mask] is the current state
// cmask is the next mask
	if (pos == n) {
		minimize(dp[u][cmask], dp[u][mask] + cost);
		return;
	}

	if (mask >> pos & 1) {
		return brute(pos + 1, cmask | (1 << pos), cost, u, mask);
	}

	brute(pos + 1, cmask, cost, u, mask);
	brute(pos + 1, cmask | (1 << pos), cost + e[pos][mask ^ (1 << u)], u, mask);
}

int main() {
	scanf("%d %d", &n, &m);
	while(m--) {
		int u, v, w; scanf("%d %d %d", &u, &v, &w);
		--u; --v;
		c[u][v] = c[v][u] = w;
	}

	prep();

	for (int mask = 0; mask < (1 << n); ++mask) {
		for (int i = 0; i < n; ++i) dp[i][mask] = inf;
	}

	dp[0][1] = 0;

	for (int mask = 0; mask < (1 << n); ++mask) {
		for (int u = 0; u < n; ++u) if (dp[u][mask] != inf) {

			for (int v = 0; v < n; ++v) if (!(mask >> v & 1)) {
				if (c[u][v] == 0) continue;
				minimize(dp[v][mask ^ (1 << v)], dp[u][mask] + e[v][mask ^ (1 << u)]);
			}

			brute(0, 0, 0, u, mask);
		}
	}

	printf("%d\n", dp[n - 1][(1 << n) - 1]);
}


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

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

    /**   Created by: Marcos Portales
          Codeforces User: marcosportales
          Country: Cuba
          Copyright                    */
#include<bits/stdc++.h>
using namespace std;
/// Pragmas:
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
#define vi vector<int>
#define sz(a) int((a).size())
#define rep(i,x,y) for(int i=x;i<y;i++)
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 400010;
const int mod = 1000000007;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 2000000000000000000ll;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Template ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
    if( !e ) return 1;
    if( e & 1 ) return qpow(b,e-1) * b % mod;
    int pwur = qpow(b,e>>1);
    return (pwur * pwur) % mod;
}
int modinv(int x){
    return qpow(x,mod-2);
}
/// My Code -------------------------------------------------

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);
    srand(time(NULL));

    //freopen("a.in","r",stdin);
    //freopen("a.in","w",stdout);
    int x;
    cin >> x;
    int p=x/11;
    int r=x-p*11;
    int ans;
    if(r==0) {
        ans=2*p;
    }else if(r<=6) {
        ans=2*p+1;
    }else{
        ans=2*p+2;
    }

    cout << ans << "\n";

    return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;
    cin>>n;
    vi v(n+1,0);
    rep(i,n*(n-1)/2){
        pii a,b;
        cin>>a.first>>b.first;cin>>a.second>>b.second;
        if(a.second==b.second){v[a.first]++; v[b.first]++;}
        else{
            a.second>b.second ? v[a.first]+=3:v[b.first]+=3;
        }
    }
        vi hoge;
        rep(i,n)hoge.push_back(v[i+1]);
        sort(hoge.begin(),hoge.end(),greater<int>());
        rep(i,n){
            for(int j=0;j<n;j++){
                if(hoge[j]==v[i+1]){cout<<j+1<<endl; break;}
            }
        }
    }

#include <bits/stdc++.h>

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

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

using ll = long long;

using namespace std;

int main() {
  in(int, n);
  for (;; ++n) {
    string s = to_string(n);
    bool ok = true;
    for (char c : s)
      if (c != s[0]) ok = false;
    if (ok) {
      out(n);
      return 0;
    }
  }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;

#define TT template<typename T>
#define TTF template<typename T, typename F>
#define ET(T) typename T::value_type
TT inline T sq(T x){return x*x;}
TT inline T In(){T x;cin>>x;return x;}
TT inline void Out(T&x){cout<<x;}
TT inline void sort(T&v){sort(begin(v),end(v));}
TT inline void revs(T&v){reverse(begin(v),end(v));}
TT inline void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT inline int ubnd(T&v,ET(T)&x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT inline int lbnd(T&v,ET(T)&x){return lower_bound(begin(v),end(v),x)-begin(v);}
TTF inline void inpt(T&v,int n,F f){for(v.reserve(n);n--;v.emplace_back(f()));}
TTF inline void show(T&v,F f,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),f(x);cout<<e;}
#undef TT
#undef TTF
#undef ET

inline void fast_io(){ios::sync_with_stdio(0);cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline ll pow_mod(ll a,ll k,ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a,ll p){return pow_mod(a,p-2,p);}

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

struct Edge {
  int f, t, c;
  Edge(int f, int t, int c) : f(f), t(t), c(c) {}
  bool operator<(const Edge& e) const { return c != e.c ? c > e.c : (f != e.f ? f < e.f : t < e.t); }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int dijkstra(const Graph& g, int s, int t){
  int vis[128];
  fill(begin(vis), end(vis), 0);
  priority_queue<Edge> pq;
  for (pq.push(Edge(-2, s, 0)); !pq.empty(); pq.pop()){
    Edge e = pq.top();
    if (vis[e.t]++) continue;
    if (e.t == t) return e.c;
    for (const auto& ee : g[e.t]){
      pq.push(Edge(e.f, ee.t, e.c + ee.c));
    }
  }
  return -1;
}

int main()
{
  int n, k;
  while (cin >> n >> k, n || k){
  Graph g(n + 1);
  for (int i = 0; i < k; i++){
    int t = in();
    if (t){
      int a, b, c;
      cin >> a >> b >> c;
      g[a].push_back(Edge(a, b, c));
      g[b].push_back(Edge(b, a, c));
    }
    else {
      int a, b;
      cin >> a >> b;
      cout << dijkstra(g, a, b) << endl;
    }
  }
  }
  return (0);
}

// priority_queue queue deque front stringstream max_element min_element insert count make_tuple


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

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n=1):n(n), f(n+1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i]) continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i*i; j <= n; j += i) {
        if (!f[j]) f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x;}
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0) return {};
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
};

int main() {
  ll n;
  cin>>n;
  Sieve sv(55555);
  vector<int> v;
  v=sv.primes;
  ll now=0;
  vector<ll>A;
  rep(i,v.size()){
    if (v[i]%10==1) {
      now++;
      A.push_back(v[i]);
    }
  }
  rep(i,n){
    cout<<A[i]<<' ';
  }
}

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

int main(){
    int N;
    cin >> N;
    vector<bool> vec(55555);
    vec[1] = false;
    for (int i = 2; i <= 55554; i++){
        vec[i] = true;
    }
    for(int i = 2; i <= 55554; i++){
        if (vec[i]){
            for (int j = 2; j * i <= 55554; j++){
                vec[i * j] = false;
            }
        }
    }
    vector<int> ans(0);
    for (int i = 1; i <= 55551; i += 5){
        if (vec[i]) ans.push_back(i);
    }
    rep(i,N){
        cout << ans[i] << " ";
    }
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

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

signed main() {
    string a, b, c;
    cin >> a >> b >> c;
    if (a.back() == b.front() && b.back() == c.front()) {
        cout << "YES" << endl;
    } else
        cout << "NO" << endl;
}

#include <stdio.h>

int main () {
	char a[12];
	char b[12];
	char c[12];
	int lenA = 0, lenB = 0;
	scanf("%s", a);
	scanf("%s", b);
	scanf("%s", c);

	for(int i = 0; a[i] != '\0'; i++) {
		lenA++;
	}
	for(int i = 0; b[i] != '\0'; i++) {
		lenB++;
	}

	if (a[lenA-1] == b [0] && b[lenB-1] == c[0]) {
		printf("YES\n");
	}
	else {
		printf("NO\n");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 100

typedef pair<int, int> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct edge{ int to, cost; };

vector<vector<edge> >G(MAX_N);
int n;
int d[MAX_N];

void dijkstra(int s){
  priority_queue<P, vector<P>, greater<P> >q;
  fill(d, d + n, INF);
  d[s] = 0;
  q.push(P(0, s));

  while(!q.empty()){
    P p = q.top(); q.pop();
    int v = p.S;
    if(d[v] < p.F) continue;
    rep(i, G[v].size()){
      edge e = G[v][i];
      if(d[e.to] > d[v] + e.cost){
	d[e.to] = d[v] + e.cost;
	q.push(P(d[e.to], e.to));
      }
    }
  }
}

int main(){
  int k;
  while(scanf("%d%d", &n, &k) && n+k){
    rep(i, MAX_N) G[i].clear();
    int a, b, c;
    rep(i, k){
      scanf("%d", &a);
      if(a){
	scanf("%d%d%d", &a, &b, &c); b--; a--;
	edge e;
	e.to = b; e.cost = c;
	G[a].PB(e);
	e.to = a;
	G[b].PB(e);
      }else{
	scanf("%d%d", &a, &b); a--; b--;
	dijkstra(a);
	printf("%d\n", d[b]==INF?-1:d[b]);
      }
    }
  }
  return 0;
}

#include <bits/stdc++.h>

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

#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rag(con) std::begin(con), std::end(con)

constexpr int MAX = 1010;

std::vector<int> to[MAX];

int main()
{
    int n, m;
    std::cin >> n >> m;

    rep(i, m)
    {
        int a, b;
        std::cin >> a >> b;
        to[a].push_back(b);
    }

    std::vector<int> ans(n + 1);
    constexpr int INF = 1e9;

    rep(i, n)
    {
        std::queue<int> que;

        std::vector<int> dis(n + 1, INF);
        std::vector<int> par(n + 1);

        que.push(i);
        dis[i] = 0;

        while (!que.empty())
        {

            int x = que.front();
            que.pop();

            for (auto u : to[x])
            {
                if (dis[u] > dis[x] + 1)
                {
                    dis[u] = dis[x] + 1;
                    par[u] = x;
                    que.push(u);
                }
                else if (dis[u] == 0)
                    par[u] = x;
            }
        }
        if (!par[i])
            continue;
        int v = par[i];
        std::vector<int> vec;
        while (v != i)
        {
            vec.push_back(v);
            v = par[v];
        }
        vec.push_back(i);
        if (ans.size() > vec.size())
            ans = vec;
    }

    if (ans.size() == n + 1)
        std::cout << -1 << std::endl;
    else
    {
        std::cout << ans.size() << std::endl;
        for (auto x : ans)
            std::cout << x << std::endl;
    }

    return 0;
}


#include <cstdio>
typedef long long ll;

template<class T> inline T Abs(const T x){return x>=0?x:-x;}
template<class T> inline T Min(const T a,const T b){return a<b?a:b;}

int n,m,x,y;
ll AT,f[200005],f1[200005],f2[200005],Ans;
const ll Inf=1e18;

void Add1(int x,ll k){for(;x<=n&&k<f1[x];x+=x&-x)f1[x]=k;}
ll Query1(int x,ll k=Inf){for(;x;x^=x&-x)k=Min(k,f1[x]);return k;}
void Add2(int x,ll k){for(;x&&k<f2[x];x^=x&-x)f2[x]=k;}
ll Query2(int x,ll k=Inf){for(;x<=n;x+=x&-x)k=Min(k,f2[x]);return k;}

int main()
{
    scanf("%d%d%d%d",&n,&m,&x,&y),Ans=Inf;
    for(int i=1;i<=n;++i)f[i]=f1[i]=f2[i]=Inf;
    for(f[y]=0,Add1(y,-y),Add2(y,y);m--;)
    {
        y=x,scanf("%d",&x);
        ll v=Min(Query1(x)+x,Query2(x)-x)+AT;
        AT+=Abs(x-y);
        if(v-AT<f[y])f[y]=v-AT,Add1(y,f[y]-y),Add2(y,f[y]+y);
    }
    for(int i=1;i<=n;++i)Ans=Min(Ans,f[i]);
    return printf("%lld\n",Ans+AT),0;
}

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(x,y) (x=min(x,y))
#define chmax(x,y) (x=max(x,y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}
#define N 200010
ll n, q, c, x[N];
set<P> dp[2];

int main(){
	cin>>n>>q>>x[0]>>c;
	rep(i, q) cin>>x[i+1];
	dp[0].insert(P(c, 0));
	for(int i = 1; i <= q; i++){
		int t = i%2;
		dp[t].clear();
		for(auto p: dp[!t]){
			dp[t].insert(P(x[i-1], p.snd+abs(x[i]-p.fst)));
			dp[t].insert(P(p.fst, p.snd+abs(x[i]-x[i-1])));
		}
		{
			auto it = dp[t].begin(), it2 = it; ++it2;
			while(it2!=dp[t].end()){
				while(it2!=dp[t].end() && it->snd+abs(it->fst-it2->fst)<=it2->snd){
					dp[t].erase(it2);
					it2 = it;
					++it2;
				}
				++it;
				if(it2!=dp[t].end()) ++it2;
			}
		}
		{
			auto it = dp[t].end();
			if(it!=dp[t].begin()) --it;
			auto it2 = it;
			if(it!=dp[t].begin()) --it2;
			while(it!=dp[t].begin()){
				while(it!=dp[t].begin() && it->snd+abs(it->fst-it2->fst)<=it2->snd){
					dp[t].erase(it2);
					it2 = it;
					if(it2!=dp[t].begin()) --it2;
				}
				if(it!=dp[t].begin()){
					--it;
					if(it2!=dp[t].begin()) --it2;
				}
			}
		}
	}
	ll res = 1e17;
	for(auto p: dp[q%2]) res = min(res, p.snd);
	cout<<res<<endl;
	return 0;
}


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

using ll = long long;

class Stream{
public:
    Stream(){

    }
    //配列以外の入力を受け取る
    void read(){

    }
    template<typename First, typename... Rest>
    void read(First& first, Rest&... rest){
        cin >> first;
        read(rest...);
    }
    //配列を区切って出力する
    template<typename T>
    void write_vec(vector<T>& v, char divider){
        for(size_t i = 0; i < v.size(); i++){
            cout << v[i] << divider;
        }
    }
};

int main(){
    Stream io;

    string S;
    io.read(S);
    string ans = "AC";
    if(S[0] != 'A') ans = "WA";
    int c = 0;
    for(int i = 1; i < S.size(); i++){
        if(!(S[i] <= 'z' && S[i] >= 'a')){
            if(i <= S.size() - 2 && i >= 2){
                c++;
            }
            else ans = "WA";
        }
    }
    if(c != 1) ans = "WA";
    cout << ans;
    return 0;
}

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

int main(void) {
	string S;
	cin >> S;
	int alfabet[26] = {0};
	for (int i = 0; i < S.length(); i++) {
		alfabet[S[i] - 'a']++;
	}
	for (int i = 0; i < 26; i++) {
		if (alfabet[i] == 0) {
			printf("%c\n", 'a' + i);
			return 0;
		}
	}
	printf("None\n");
	return 0;

}


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

typedef long long ll;
static const ll MOD = 998244353;
static const int MAX = 500000+100;

ll fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll ModPow(ll a, ll cur=MOD-2) {
	ll r=1;
	while(cur) r=r*((cur%2)?a:1)%MOD,a=a*a%MOD,cur>>=1;
	return r;
}

int main(){
    ll N,M,K;
    cin>>N>>M>>K;
    /*
    先頭の選び方がM
    iが[0,K]で、
    先頭と同じ色のブロックの選び方はcomb(N-1,i)
    残りのブロックの色の選び方はM^(N-1-i);
    */

   ll ans = 0;
   COMinit();

   for(int i=0; i<=K; i++){
       ans += M*COM(N-1,i)%MOD*ModPow(M-1,N-1-i)%MOD;
   }

   cout<<ans%MOD<<endl;

    return 0;
}

#include <bitset>
#include <tuple>
#include <string>
#include <array>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cmath>
#ifdef LOCAL
  #include <iostream>
#endif
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

typedef long long ll;

#ifdef LOCAL
  #define debug_echo(e) cout << "L" << __LINE__ << ": " << e << "\n"
  #define debug_repr(e) cout << "L" << __LINE__ << ": " << #e << " -> " << e << "\n"
#else
  #define debug_echo(e)
  #define debug_repr(e)
#endif

#define defread(T, fmt) \
  inline T read_ ## T() { \
    T result; \
    scanf(fmt, &result); \
    return result; \
  }
defread(int, "%d")
defread(ll, "%lld")
defread(float, "%f")
defread(double, "%lf")

int gcd(int m, int n) {
  if ((0 == m) || (0 == n)) return 0;
  while (m != n) {
    if (m > n) m -= n;
    else       n -= m;
  }
  return m;
}
int lcm(int m, int n) {
  if ((0 == m) || (0 == n)) return 0;
  return (m / gcd(m, n)) * n;
}

int main() {
  vector<int> a(4);
  vector<int> b(2);
  a[0] = read_int();
  a[1] = read_int();
  a[2] = read_int();
  a[3] = read_int();
  b[0] = read_int();
  b[1] = read_int();

  sort(all(a));
  sort(all(b));

  printf("%d\n", a[1] + a[2] + a[3] + b[1]);
}


#include<iostream>
#include<cmath>
using namespace std;
typedef long long int LL;
LL base[25];

LL check(LL a,int n,int k){
	if(n==0){
		if(a==0)return 10;
		else return 0;
	}
	if(n<0){
		if(a==0)return 1;
		else return 0;
	}
	LL b,c,d;
	LL t;
	t=base[n];
	for(int i=0;i<k;i++)t*=10;
	b=(a+t*15)/t-15;
	c=0;
	if(-9<=b && b<=9){
		d=10-abs(b);
		if(k==0 && b>=0)d--;
		c=check(a-b*t,n-2,k+1)*d;
	}
	if(-9<=b+1 && b+1<=9){
		d=10-abs(b+1);
		if(k==0 && b+1>=0)d--;
		c+=check(a-(b+1)*t,n-2,k+1)*d;
	}
	return c;
}

int main(){
	LL a,b,c;
	LL s=0;
	int i,j,k;
	for(i=1,a=1;i<=20;i++){
		a*=10;
		base[i]=a-1;
	}
	cin>>a;
	for(i=20;i>0;i--){
		s+=check(a,i,0);
	}
	cout<<s<<endl;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

void solve() {
  ll N, A, B, dist = 0;
  cin >> N >> A >> B;
  vector<ll> X(N);
  REP(i, N) { cin >> X[i]; }
  sort(ALL(X));
  FOR(i, 0, N - 1 - 1) {
    if ((X[i + 1] - X[i]) * A < B) {
      dist += (X[i + 1] - X[i]) * A;
    } else {
      dist += B;
    }
  }
  cout << dist << endl;
}

signed main() {
  solve();

  return 0;
}


#include<iostream>
using namespace std;

int main(){

	int n, x, y, pt1, pt2;

	while(cin >> n){
		pt1 = 0;
		pt2 = 0;

		if(n == 0) break;

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

			if(x > y) pt1 += x + y;
			else if(x < y) pt2 += x + y;
			else {
				pt1 += x;
				pt2 += y;
			}
		}

		cout << pt1 << " " << pt2 << endl;
	}

	return 0;

}

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

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int ba[12][12];
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
void dfs(int x,int y) {
  REP(i,4) {
    int xx = x+dx[i];
    int yy = y+dy[i];
    if (xx<0||xx>=12||yy<0||yy>=12) continue;
    if (ba[xx][yy]) {
      ba[xx][yy] = 0;
      dfs(xx,yy);
    }
  }
}

int main() {
  char b;
  while(cin >> b) {
    ba[0][0] = b-'0';
    REP(i,12) {
      REP(j,12) {
        if (i==0&&j==0)continue;
        char a;
        cin >> a;
        ba[j][i] = a-'0';
      }
    }
    int ans = 0;
    REP(i,12) {
      REP(j,12) {
        if (ba[i][j]) {
          ans++;
          dfs(i,j);
        }
      }
    }
    cout << ans << endl;
  }
}


#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <iomanip>
#include <math.h>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#define local
#ifdef local
    #include "dbg-macro/dbg.h"
#endif
//#define hacks
#ifdef hacks
    #include <boost/multiprecision/cpp_int.hpp>
#endif
#define p std::pair
#define ll long long
#define ld long double
#define ull unsigned long long
#define pi std::pair<int, int>
#define stdabs std::abs
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (unsigned long long i = 0; i < (unsigned long long)(n); ++i)
#define vec std::vector
#define oreq |=
#define npm next_permutation
using namespace std;
std::vector<unsigned ll> genprimevec(const unsigned ll N);
ll extgcd(ll a, ll b, ll& x, ll& y);
ll nCr(ll n, ll r);
void stat();
template <typename T> T fact(T num);
constexpr ll mod = 1000000000 + 7;
int main()
{
    stat();
    ll n, k;
    cin >> n >> k;
    vec<ll> v(n);
    rep(i, n) { cin >> v[i]; }
    vec< double> kitai(n);
    rep(i, n) { kitai[i] = (double)((v[i]+1.0) / 2.0); }
#ifdef local
dbg(kitai);
#endif
    double ans = 0.0, t=0.0;
    rep(i, k) { t += kitai[i]; }
    ans=t;
#ifdef local
    dbg(ans);

#endif
    rep(i, n - k + 1)
    {
        if (i == 0) { continue; }
        t = (double)(t+ kitai[i + k-1] - kitai[i - 1]);
#ifdef local
	dbg(t);
	dbg(i,kitai[i+k-1],kitai[i-1]);

#endif
        ans = max(ans, t);
    }
    cout <<std::fixed<<std::setprecision(10)<< ans << endl;
    return 0;
}
ll extgcd(ll a, ll b, ll& x, ll& y)
{
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    ll d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

std::vector<unsigned ll> genprimevec(const unsigned ll N)
{
    std::vector<bool> is_prime(N + 1);
    for (unsigned ll i = 0; i <= N; i++) {
        is_prime[i] = true;
    }
    std::vector<unsigned ll> P;
    for (unsigned ll i = 2; i <= N; i++) {
        if (is_prime[i]) {
            for (unsigned ll j = 2 * i; j <= N; j += i) {
                is_prime[j] = false;
            }
            P.emplace_back(i);
        }
    }
    return P;
}

void stat(){
#ifdef local
    rep(i, 2){std::cout << "local enable" << std::endl;
}
#endif
#ifdef hacks
rep(i, 2) { std::cout << "boost enable" << std::endl; }
#endif
}

ll nCr(ll n, ll r)
{
    ll num = 1;
    for (ll i = 1; i <= r; i++) {
        num = num * (n - i + 1) / i;
    }
    return num;
}

template <typename T> T fact(T num)
{
    if (num == 1) { return 1; }
    return num * fact(num - 1);
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
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; }

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  for (int i = 0; i < k && i*i < n; i++) {
    vector<int> imos(n+1);
    rep(j, n) {
      imos[max(j-a[j], 0)]++;
      imos[min(j+a[j]+1, n)]--;
    }
    rep(j, n)
      imos[j+1] += imos[j];
    a = imos;
  }
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl;
  return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<iomanip>

using namespace std;
using ll = long long;

const int MOD = 1e9 + 7;
const int INF = numeric_limits<int>::max();
const ll LLINF = numeric_limits<ll>::max();
const double PI = acos(-1.0);

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

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

    if (A > 8 || B > 8) cout << ":(" << endl;
    else cout << "Yay!" << endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
#include <tuple>
#include <cmath>
#include <cstdio>
#include <cassert>
#include <functional>
#define _LIBCPP_DEBUG 0
using namespace std;

int main(void){
    int l,r;
    cin >> l >> r;
    int mi=2018*2018;
    for (int  i = l; i<r; i++)
    {
        int L=i%2019;
        for (int j = i+1; j <= r; j++)
        {
            int R=j%2019;
            int ama=(L*R)%2019;
            if (ama<mi)
            {
                mi=ama;
                if (mi==0)
                {
                    cout << 0 << endl;
                    return 0;
                }
            }
        }
    }
    cout << mi << endl;

}


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

using namespace std;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(ll i=0; i<n; i++)
#define repr(i, s, e) for(ll i=s; i>=e; i--)
#define reps(i, s, e) for(ll i=s; i<=e; i++)
#define inf 1e18
#define all(v) v.begin(),v.end()
#define ceil(a, b) (a+b-1)/b
#define ok cout << "ok" << endl;
#define sp << " " <<
template<typename T> inline bool chmax(T &a, T b){ if(a<b) a=b; return a<b; }
template<typename T> inline bool chmin(T &a, T b){ if(b<a) a=b; return b<a; }
template<typename T> void vdeb(T v){
    cout << "#vector set debug" << endl;
    for(auto vv : v) cout << vv << " ";
    cout << endl;
}
template<typename T1, typename T2> void adeb(T1 arr[], T2 n){
    cout << "#adebug" << endl;
    for(int i=0; i<=n; i++) cout << arr[i] << " ";
    cout << endl;
}
template<typename T1> void mdeb(T1 mp){
    cout << "#map pair debug" << endl;
    for(auto const& m : mp) cout << m.first sp m.second << endl;
}
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){ fill((T*)array, (T*)(array+N), val); }
void ans(bool b){ cout << (b ? "Yes" : "No") << endl; }
void ans2(bool b){ cout << (b ? "YES" : "NO") << endl; }
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

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

    int n;
    cin >> n;

    cout << (n-3)*180 + 180 << endl;

    return 0;
}


#include <iostream>
using namespace std;

void MySwap(int &a, int &b){
  int tmp = a;
  a = b;
  b = tmp;
}

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

int main(){
  int A[100000], n, place;
  cin >> n;
  for(int i = 0; i < n; i++) cin >> A[i];
  place = partition(A, 0, n-1);
  for(int i = 0; i < n; i++)
    cout << (i==place?"[":"") << A[i] << (i==place?"]":"") << (i==n-1?'\n':' ');
  return 0;
}

#include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int h,i;
  int a[10],b[10];
  for(i=0;i<10;i++)
    cin>>a[i];
  for(i=0;i<10;i++)
    cin>>b[i];
  do{
    h=0;
    for(i=1;i<10;i++){
      if(a[i-1]<a[i]){
	swap(a[i-1],a[i]);
	h=1;
      }
    }
  }while(h);
  do{
    h=0;
    for(i=1;i<10;i++){
      if(b[i-1]<b[i]){
	swap(b[i-1],b[i]);
	h=1;
      }
    }
  }while(h);
  cout<<a[0]+a[1]+a[2]<<" "<<b[0]+b[1]+b[2]<<endl;
  return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
//C++11
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main(){
  string s;
  map<char, int> m;
  m['J'] = 0;
  m['O'] = 1;
  m['I'] = 2;
  while(cin>>s){
    s = "I" + s;
    int N = s.size();
    int cnt[3]={};
    int ans = 0;
    for(int i = 1; i < N; i++){
      int n = m[s[i]];
      int b = m[s[i-1]];
      if(n < b){
        REP(i, 3) cnt[i] = 0;
        if(n == 0) cnt[0]++;
      }else{
        cnt[n]++;
        if(cnt[0]>=cnt[1]&&cnt[1]==cnt[2]) ans = max(ans, cnt[1]);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int main() {
	string st; cin >> st;
	int ans = 0;
	for (int l = 0; l < st.size(); ++l) {
		if (st[l] == 'O') {
			int r = l;
			while (r!=st.size()&&st[r] == 'O') {
				r++;
			}
			int len = r - l;
			if (l - len >= 0 && r + len <= st.size()) {
				bool ok = true;
				for (int t = 0; t < len; ++t) {
					if (st[l - t - 1] != 'J')ok = false;
					if (st[r + t] != 'I')ok = false;
				}
				if (ok)ans = max(ans, len);
			}
			l += len - 1;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
	vector<pair<int,pair<int,int>>>v;
	for(int i=1;i<150;++i){
		for(int j=i+1;j<=150;++j){
			v.push_back(make_pair(i*i+j*j,make_pair(i,j)));
		}
	}
	sort(v.begin(),v.end());
	int h,w;
	while(cin>>h>>w,h){
		int a=h*h+w*w;
		bool flag=0;
		for(auto x:v){
			if(flag){
				cout<<x.second.first<<" "<<x.second.second<<endl;
				break;
			}
			if(x.first==a && x.second.first==h){
				flag=1;
			}
		}
	}
}

#include <iostream>
#include <iomanip>
#include <cassert>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <sstream>
#include <istream>
#include <cmath>
#include <cstdio>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i=(int)a; i<(int)b; i++)
#define for_d(i, a, b) for (int i=(int)a-1; i>=b; i--)
#define for_r(i, a, b, c) for (int i=(int)a; i<(int)b; i += c)
#define for_dr(i, a, b, c) for (int i=(int)a-1; i>=b; i -= c)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

typedef long long ll;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }

string ret[7] = {"null", "one pair", "two pair", "three card", "straight", "full house", "four card"};

int main() {
	vci t(5); char c;

	while (cin>>t[0]>>c>>t[1]>>c>>t[2]>>c>>t[3]>>c>>t[4]) {
		int ans = 0;

		vci v(13, 0);
		for_(i, 0, (int)t.sz) v[t[i]-1]++;

		int cnt = 0;

		// 0, 1, 2
		for_(i, 0, 13) if (v[i]==2) cnt++;
		ans = cnt;

		// 3
		cnt = 0;
		for_(i, 0, 13) if (v[i]==3) cnt++;
		if (cnt) ans = 3;

		// 4
		for_(i, 0, 10) {
			cnt = 0;
			for_(j, 0, 5) {
				if (v[(i+j)%13]==1) cnt++;
			}
			if (cnt==5) ans = 4;
		}

		// 5
		cnt = 0; int cnt2 = 0;
		for_(i, 0, 13) {
			if (v[i]==2) cnt++;
			if (v[i]==3) cnt2++;
		}
		if (cnt2==1 && cnt==1) ans = 5;

		cnt = 0;
		for_(i, 0, 13) if (v[i]==4) cnt++;
		if (cnt) ans = 6;

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

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

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

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

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

    int M;
    cin >> M;
    vec<ll> d(M), c(M);
    rep(i,M){
        cin >> d[i] >> c[i];
    }
    ll sumdc = 0;
    ll sumc = 0;
    ll ans = 0;
    rep(i,M){
        sumdc += d[i] * c[i];
        sumc += c[i];
    }
    ans = sumc - 1  + (sumdc - 1)/9;
    cout << ans << endl;
}

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

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

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

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;

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

int main(){
    vector<vector<P>> v(10);
    rep(i,10){
        v[i].push_back(make_pair(0,0));
        v[i].push_back(make_pair(0,i));
    }
    rep(i,10){
        while(true){
            P p = *v[i].rbegin();
            int t = i*10 + p.second;
            int cnt = 0;
            while(t>=10){
                t = t/10+t%10;
                cnt++;
            }
            v[i].push_back(make_pair(p.first+cnt,t));
            if(t==i) break;
        }
    }
    v[0][2].first = 1;
    ll M;
    cin>>M;
    vector<ll> d(M),c(M);
    rep(i,M) cin>>d[i]>>c[i];
    ll ans = 0;
    vector<ll> rest(M);
    rep(i,M){
        ll t = d[i],s=c[i];
        ll l = v[t].size();
        ans+=v[t][l-1].first*((s-1)/(l-2));
        s = s - ((s-1)/(l-2))*(l-2);
        ans += v[t][s].first;
        rest[i]=v[t][s].second;
    }
    ll cnt = 0;
    while(cnt!=M-1){
        while(rest[cnt]!=0){
            ll a = rest[cnt],b=rest[cnt+1];
            rest[cnt] = (a+b)/10;
            rest[cnt+1] = (a+b)%10;
            ans++;
        }
        cnt++;
    }
    cout<<ans<<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 a,b,c;
  cin >>a >>b >>c;
  int M = max(a,b);
  int m = min(a,b);
  if(M>=c && m<=c) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

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

using namespace std;

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

	// 文字列の長さがA+B+1か判定
	if (S.size() != A + B + 1) {
		// cout << "文字列の長さが" << S.size() << "です。\n";
		cout << "No\n";
		return 0;
	}

	// -がA番目にあるか判定(文字列は0からスタート)
	if (S.find('-') != A) {
		// cout << "-(ハイフン)は" << S.find('-') << "番目にあります。\n";
		cout << "No\n";
		return 0;
	}

	// A+1番目の文字以外が0から9であるか判定
	for (int i = 0; i < S.size(); i++) {
		// cout << "S[" << i << "]は" << S[i] << "です。\n";
		if (i == A) {
			continue;
		}
		if (S[i] > '9' || S[i] < '0') {
			// cout << "0-9以外の文字が含まれています。\n";
			cout << "No\n";
			return 0;
		}
	}

	cout << "Yes\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
void computeLPSArray(string pattern, int m, int lps[]){
    int len=0;
    lps[0]=0;   //lps[0] is always 0
    int i=1;
    while(i<m){
        if(pattern[i]==pattern[len]){
            len++;
            lps[i]=len;
            i++;
        }else{
            if(len!=0)
                len=lps[len-1];
            else{
                lps[i]=0;
                i++;
            }
        }
    }
}
void kmpSearch(string str,string pattern){
    int m=pattern.length();
    int n=str.length();
    int lps[m];  //longest prefix suffix array
    computeLPSArray(pattern,m,lps);
    int i=0; //text
    int j=0; //pattern
    bool found=0;
    while(i<n){
        if(pattern[j]==str[i]){
            i++;
            j++;
        }
        if(j==m){
            j=lps[j-1];
            found=1;
            break;
        }else if(i<n && pattern[j]!=str[i]){
            if(j!=0) j=lps[j-1];
            else i=i+1;
        }
    }
    if(!found)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
}
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    string str,pattern;
    while(cin>>str){
        pattern="AC";
        kmpSearch(str,pattern);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s; cin >> s;
  cout << ((s.find("AC") != string::npos) ? "Yes" : "No") << endl;
}


#define task "test"

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;

#define pb push_back
#define mp make_pair
#define X first
#define Y second

const ll LINF = 1E18;
const int INF = 1E9;
const int MOD = 1E9 + 7;
const int N = 2E5 + 5;

int t = 1;

int n, m, nump[N], pa[N];
bool avail[N];
vector <int> A[N];
set <ii> S[N];

void Input()
{
    cin >> n >> m;
    for (int i = 1; i <= n + m - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        A[u].pb(v);
        nump[v]++;
    }
}

void Prepare()
{
    /*for (int u = 1; u <= n; u++) {
        for (int v : A[u]) S[u].insert({nump[v], v});
    }*/
}

void DFS(int u, int p)
{
    pa[u] = p;
    avail[u] = 0;

    for (int v : A[u]) if (v != p) {
        nump[v]--;
    }
    for (int v : A[u]) if (v != p && avail[v] && !nump[v]) DFS(v, u);
}

void Solve()
{
    int root = 0;
    for (int i = 1; i <= n; i++) if (!nump[i]) root = i;
    memset(avail, 1, sizeof(avail));
    DFS(root, 0);

    for (int i = 1; i <= n; i++) printf("%d\n", pa[i]);
}

int main()
{
    if (fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    // cin >> t;
    while (t--) {
        Input();
        Prepare();
        Solve();
    }
}

#include <bits/stdc++.h>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template <class T>
string to_string(T s);
template <class S, class T>
string to_string(pair<S, T> p);
string to_string(string s);

template <class T>
string to_string(T v) {
  if (v.empty()) return "{}";
  string ret = "{";
  auto itr = v.begin(), next = ++v.begin();
  for (; next != v.end(); ++itr, ++next) ret += to_string(*itr) + ",";
  ret += to_string(*itr) + "}";
  return ret;
}
template <class S, class T>
string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}
string to_string(string s) { return s; }

void debug() { cerr << endl; }
template <class Head, class... Tail>
void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head head, Tail... tail) {
  cout << to_string(head) << " ";
  print(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  string separator = " ";
  template <class T>
  inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus) x = -x;
  }
  inline void read(string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\n' && c != EOF);
  }
  template <class T>
  inline void read(vector<T> &v) {
    for (auto &x : v) read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &... tail) {
    read(head);
    read(tail...);
  }
  template <class T>
  inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0) putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(string x) {
    for (char c : x) putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i) putchar_unlocked(s[i]);
  }
  template <class T>
  inline void write(vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  void set_separator(string s) { separator = s; }
} io;

int main() {
  int N, M;
  io.read(N, M);
  vector<vector<int>> G(N), RG(N);
  REP(i, N + M - 1) {
    int a, b;
    io.read(a, b);
    G[a - 1].push_back(b - 1);
    RG[b - 1].push_back(a - 1);
  }
  int root = -1;
  REP(i, N) if (RG[i].empty()) root = i;
  vector<int> ans(N, -1);
  function<void(int)> dfs = [&](int u) {
    for (auto v : G[u]) {
      if (ans[v] == -1) {
        bool f = 1;
        for (int k : RG[v])
          if (ans[k] == -1 && k != root) f = 0;
        if (f) {
          ans[v] = u;
          dfs(v);
        }
      }
    }
  };
  dfs(root);
  REP(i, N) {
    io.write(ans[i] + 1);
    io.write("\n");
  }
  return 0;
}


//Name: Twin Prime
//Level: 2
//Category: 数学,素数
//Note:

/**
 * 10000以下の双子素数を列挙しておけば、あとは線形探索でよい。
 *
 * オーダーは O(M log log M)。
 * ただしMは求める必要がある最大の素数で、M=10000。
 */
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

vector<bool> eratosthenes(int N) {
    vector<bool> is_prime(N+1, true);
    is_prime[0] = is_prime[1] = false;

    const int LIM = static_cast<int>(sqrt(N));
    for(long long i = 2; i <= LIM; ++i) {
        if(is_prime[i]) {
            for(long long j = i*i; j <= N; j += i) {
                is_prime[j] = false;
            }
        }
    }
    return is_prime;
}

bool solve(bool first) {
    const static auto is_prime = eratosthenes(10000);

    int N;
    if(!(cin >> N)) return false;
    if(!N) return false;
    for(int i = N; i >= 0; --i) {
        if(is_prime[i] && is_prime[i-2]) {
            cout << i-2 << ' ' << i << endl;
            break;
        }
    }

    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);

    bool first = true;
    while(solve(first)) {
        first = false;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rg register
#define rep(i,a,b) for (rg int i=(a);i<=(b);i++)
#define per(i,a,b) for (rg int i=(b);i>=(a);i--)
#define pb push_back
#define lowbit(x) (x&(-x))
#define replow(i,a,b) for(rg int i = (a);i<=(b);i+=lowbit(i))
#define perlow(i,a,b) for(rg int i = (b);i>=(a);i-=lowbit(i))
#define mk make_pair
#define VI vector<int>
#define pii pair<int,int>
#define pLL pair<long long,long long>
#define fi first
#define se second
#define il inline
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define inf 0x3f3f3f3f
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<15,stdin),p1==p2)?EOF:*p1++)
char buf[1<<15],*p1 = buf,*p2 = buf;
inline ll read(){
    #define num ch-'0'
    char ch;bool flag=0;ll res;
    while(!isdigit(ch=getc()))
    (ch=='-')&&(flag=true);
    for(res=num;isdigit(ch=getc());res=res*10ll+num);
    (flag)&&(res=-res);
    #undef num
    return res;
}
inline void write(ll x){
	if (x < 0) x = ~x + 1ll, putchar('-');
	if (x > 9) write(x / 10ll);
	putchar(x % 10ll + '0');
}
#define mid ((l + r)>>1)
#define ls (x<<1)
#define rs ((x<<1)|1)
#undef mid
#undef ls
#undef rs
multiset<int> s;
void solve()
{
	int n = read();
	int mn = 1e9 + 10;
	int cnt = 0;
	rep(i,1,n)
	{
		int x = read();
		if(s.empty())
		{
			s.insert(x);
		}
		else
		{
			auto it = s.lower_bound(x);
			if(it == s.begin())
			{
				s.insert(x);
			}
			else
			{
				it--;
				s.erase(it);
				s.insert(x);
			}
		}
	}
	printf("%d",s.size());
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("data.txt","r",stdin);
	#endif
//	int T = read();
//	while(T--)
	solve();
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

vint dx = {1,0,-1,0};
vint dy = {0,1,0,-1};

int main() {
    int h, w; cin >> h >> w;
    vector<string> field(h);
    rep(i,h) cin >> field[i];
    queue<P> q;
    rep(i,h)rep(j,w) if (field[i][j] == '#') q.push(P(i,j));
    int ans = 0;
    while (true) {
        if (q.empty()) {ans--; break;}
        queue<P> tmp_q;
        while (!q.empty()) {
            int x = q.front().second;
            int y = q.front().first;
            q.pop();
            rep(i,4) {
                int n_x = x + dx[i];
                int n_y = y + dy[i];
                if (n_x >= 0 && n_x < w && n_y >= 0 && n_y < h && field[n_y][n_x] != '#') {
                    field[n_y][n_x] = '#';
                    tmp_q.push(P(n_y,n_x));
                }
            }
        }
        swap(q,tmp_q);
        ans++;
    }
    cout << ans << endl;
}

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

class edge;

class vertex{
    public:
        vector<edge*> edges;
        int color;
        vertex(int _color){
            color = _color;
        }
};

class edge{
    public:
        vertex *to;
        edge(vertex *_to){
            to = _to;
        }
};

class list_node{
    public:
        list_node *prev,*next;
        vertex *data;
        list_node(list_node *_prev,list_node *_next,vertex *_data){
            prev = _prev;
            next = _next;
            data = _data;
        }
};

class list{
    public:
        list_node *back,*front;
        list(){
            back = new list_node(0,0,0);
            front = new list_node(0,back,0);
            back->prev = front;
        }
        list_node *get_first(){
            return front->next;
        }
        list_node *get_last(){
            return back->prev;
        }
        void push_back(vertex *data){
            list_node *node = back->prev;
            node->next = new list_node(node,back,data);
            back->prev = node->next;
        }
        bool isEmpty(){
            if(front->next == back) return true;
            return false;
        }
};

int bfs(list l){
    int count = 0;
    list *L = new list[10000];
    L[0] = l;
    while(true){
        for(list_node *j = L[count].get_first(); j != L[count].back; j = j->next){
            vertex *v = j->data;
            for(int k = 0; k < v->edges.size(); k++){
                vertex *to = v->edges[k]->to;
                if(to->color) continue;
                to->color = 1;
                L[count+1].push_back(to);
            }
        }
        if(L[count+1].isEmpty()) break;
        count++;
    }
    return count;
}

int main(){
    int H,W,color;
    string str;
    cin >> H >> W;
    list l;
    vertex *A[H+2][W+2];
    for(int i = 0; i <= H+1; i++){
        if(i != 0 && i != H+1) cin >> str;
        for(int j = 0; j <= W+1; j++){
            if(i  == 0 || j == 0 || i == H+1|| j == W+1){
                A[i][j] = new vertex(1);
            }else{
                if(str[j-1] == '.') color = 0;
                else color = 1;
                A[i][j] = new vertex(color);
            }
        }
    }

    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            if(A[i][j]->color) l.push_back(A[i][j]);
            A[i][j]->edges.push_back(new edge(A[i-1][j]));
            A[i][j]->edges.push_back(new edge(A[i][j+1]));
            A[i][j]->edges.push_back(new edge(A[i+1][j]));
            A[i][j]->edges.push_back(new edge(A[i][j-1]));
        }
    }

    cout << bfs(l) << endl;

}


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

struct Problem {

	void Solve() {
		UL N; cin >> N;
		string S; cin >> S;
		string rS = S; reverse(rS.begin(), rS.end());
		ULL ans = 0;
		rep(i, (1 << N)) {
			string R, B;
			rep(j, N) { if (i & (1 << j)) R.push_back(S[j]); else B.push_back(S[j]); }
			R.push_back(' '); B.push_back(' ');
			UL dp[19][19] = {};
			dp[0][0] = 1;
			rep(b, B.size()) {
				rep(r, R.size()) {
					if (B[b] == rS[r + b]) dp[r][b + 1] += dp[r][b];
					if (R[r] == rS[r + b]) dp[r + 1][b] += dp[r][b];
				}
			}
			ans += dp[R.size() - 1][B.size() - 1];
		}
		cout << ans << endl;
	}

	Problem();
};
int main() {
	unique_ptr<Problem> p(new Problem());
	p->Solve();
	return 0;
}
Problem::Problem() {
	cout << fixed << setprecision(10);
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef pair<int,int> pii;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define per(i,a,b) for(int i=(b)-1; i>=(a); --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

void cal(string s, string x, string y, map<pair<string, string>, ll>& a) {
	if(sz(s) == 0) {
		a[mp(x,y)]++;
		return;
	}
	cal(s.substr(1), x + s.substr(0,1), y, a);
	cal(s.substr(1), x, y + s.substr(0,1), a);
}

ll solve(string s, string t) {
	map<pair<string, string>, ll> a, b;
	string x,y;
	x = y = "";
	cal(s,x,y,a);
	cal(t,x,y,b);
	ll ret = 0;
	for(auto e : a) {
		auto key = e.F;
		if(b.find(key) != b.end()) {
			ret += e.S * b[key];
		}
	}
	return ret;
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);
	int n;
	string s;
	cin >> n >> s;
	string t = s.substr(n);
	reverse(all(t));
	cout << solve(s.substr(0,n), t) << '\n';
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#define re register
const int MAXN=2e5+10;
int n,K,s[MAXN],a[MAXN],timee;
char fin[10010],*p1=fin,*p2=fin;
char getc()
{
	return p1==p2&&(p2=(p1=fin)+fread(fin,1,10000,stdin),p1==p2)?EOF:*p1++;
}
int read()
{
	re int x=0;
	re char ch=0;
	while(ch<'0'||ch>'9') ch=getc();
	while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getc();
	return x;
}
inline void insert(int x,int w)
{
	for(;x<=n;x+=x&-x) s[x]+=w;
}
inline int query(int x)
{
	re int res=0;
	for(;x;x-=x&-x) res+=s[x];
	return res;
}
int main()
{
	//freopen("t2.out","w",stdout);
	n=read();
	K=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	if(K>=41)
	{
		for(re int i=1;i<=n;i++) printf("%d ",n);
		return 0;
	}
	while(K--)
	{
		re bool flag=1;
		memset(s,0,(n+5)*sizeof(int));
		for(re int i=1;i<=n;i++) insert(std::max(1,i-a[i]),1),insert(std::min(n,i+a[i])+1,-1);
		for(re int i=1;i<=n;i++) a[i]=query(i),flag&=(a[i]==n);
		if(flag) break;
	}
	for(re int i=1;i<=n;i++) printf("%d ",a[i]);
	printf("\n");
	return 0;
}
/*
5 1
1 0 0 1 0
*/

#include <iostream>
#include <vector>
#include <stack>
#include <bitset>
#include <utility>
#include <algorithm>

using namespace std;

const int kNil = -1;
const int kMaxV = 100010;

enum class Color {
  kWhite,
  kGrey,
  kBlack,
};

template <class T>
struct Edge {
  using w_type = T;
  Edge(int u, int v, w_type w): u(u), v(v), w(w) { }
  int u, v;
  w_type w;
  bool operator<(const Edge<T> & rhs) const {
    if (w != rhs.w) { return w < rhs.w; }
    if (u != rhs.u) { return u < rhs.u; }
    return v < rhs.v;
  }
};

template <class Edge>
void ArticulationPointsVisit(
    const typename vector<Edge>::size_type node_now,
    typename vector<Edge>::size_type & time,
    const vector<vector<Edge>> & adjacency_list,
    vector<int> & d,
    vector<int> & low,
    vector<Color> & color,
    vector<int> & parent,
    bitset<kMaxV> & bs_aps) {
  color[node_now] = Color::kGrey;
  d[node_now] = time;
  low[node_now] = time++;
  unsigned child_num = 0;
  for (const auto & e: adjacency_list[node_now]) {
    if (color[e.v] == Color::kWhite) {
      ++child_num;
      parent[e.v] = e.u;
      ArticulationPointsVisit(e.v, time, adjacency_list, d, low, color, parent, bs_aps);
      low[e.u] = min(low[e.u], low[e.v]);

      if (parent[e.u] == kNil && child_num >= 2) {
        bs_aps.set(e.u);
      }
      if (parent[e.u] != kNil && d[e.u] <= low[e.v]) {
        bs_aps.set(e.u);
      }
    } else if (parent[e.u] != e.v) {
      low[e.u] = min(low[e.u], d[e.v]);
    }
  }
  color[node_now] = Color::kBlack;
}

template <class Edge>
bitset<kMaxV> ArticulationPoints(const vector<Edge> & edges,
                 typename vector<Edge>::size_type node_num) {
  using size_type = typename vector<Edge>::size_type;

  vector<vector<Edge>> adjacency_list(node_num, vector<Edge>());
  for (const auto & e: edges) {
    adjacency_list[e.u].push_back(e);
    adjacency_list[e.v].emplace_back(e.v, e.u, e.w);
  }

  size_type time = 0;
  vector<int> d(node_num);
  vector<int> low(node_num);
  vector<Color> color(node_num, Color::kWhite);
  vector<int> parent(node_num, kNil);
  bitset<kMaxV> bs_aps;

  for (unsigned i = 0; i < node_num; ++i) {
    if (color[i] == Color::kWhite) {
      ArticulationPointsVisit(i, time, adjacency_list, d, low, color, parent, bs_aps);
    }
  }

  return bs_aps;
}

int main(int argc, char const *argv[]) {
  using w_type = unsigned;

  vector<Edge<w_type>> edges;
  unsigned V, E;
  cin >> V >> E;
  for (unsigned i = 0; i < E; ++i) {
    unsigned u, v;
    w_type w;
    cin >> u >> v;
    edges.emplace_back(u, v, 1);
  }

  auto bs_aps = ArticulationPoints(edges, V);
  for (unsigned i = 0; i < V; ++i) {
    if (bs_aps.test(i)) {
      cout << i << endl;
    }
  }

  return 0;
}


#include <stdio.h>
#include <stdbool.h>

char dividing(int pm, int pe, int pj);

int main(void)
{
	int ninnzuu;
	int point[3];
	while(true) {
		scanf("%d", &ninnzuu);
		if(ninnzuu == 0) {
			break;
		}
		for(int i = 0; i < ninnzuu; i++) {
			scanf("%d %d %d", &point[0], &point[1], &point[2]);
			printf("%c\n", dividing(point[0], point[1], point[2]));
		}
	}
	return 0;
}

char dividing(int pm, int pe, int pj)
{
	if(pm == 100 || pe == 100 || pj == 100) {
		return 'A';
	}else 	if(pm + pe >= 180) {
		return 'A';
	}else 	if(pm + pe + pj >= 240) {
		return 'A';
	}else  	if(pm + pe + pj >= 210) {
		return 'B';
	}else  	if(pm + pe + pj >= 150 && (pm >= 80 || pe >= 80)) {
		return 'B';
	}else{
		return 'C';
	}
}

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

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

template<class T> class SegmentTreeSum{
    int n = 0;
    std::vector<T> dat;
    T fill = 0;
public:
    SegmentTreeSum(){}
    SegmentTreeSum(int m, T fil = 0){
        fill = fil;
        int i = 1;
        while(i < m) i *= 2;
        n = i;
        dat.resize(2*i - 1);
        for(int j(0), j_len(2*i-1); j < j_len; ++j)
            dat[j] = fill;
    }

    void update(int k, T a){
        k += n - 1;
        T moto = dat[k];
        dat[k] = a;
        while(k){
            k = (k - 1) / 2;
            dat[k] += a - moto;
        }
    }

    void updateIncrease(int k, T a){
        update(k, dat[k+n-1] + a);
    }
    void updateIncrease(int k){
        updateIncrease(k, 1);
    }

    void updateDecrease(int k, T a){
        update(k, dat[k+n-1] - a);
    }
    void updateDecrease(int k){
        updateDecrease(k, 1);
    }

    T getSum(int a, int b, int k=0, int l=0, int r=-1){
        //[a, b[であることに注意
        if(r < 0) r = n;
        if(r <= a || b <= l) return fill;
        if(a <= l && r <= b) return dat[k];

        T vl = getSum(a, b, 2*k+1, l, (l+r)/2);
        T vr = getSum(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }

    T getvalue(int k){
        return dat[k+n-1];
    }

    bool getExist(int l, int r){//全て非負と仮定
        //[l, r]
        return (getSum(l, r+1) > fill);
    }
};

int main(){
    int N, Q; std::cin >> N >> Q;
    SegmentTreeSum<long long> st(N);
    {
        int a;
        for(int i(0); i < N; ++i){
            std::cin >> a;
            st.update(i, a);
        }
    }
    {
        int op, l, r;
        for(int i(0); i < Q; ++i){
            std::cin >> op >> l >> r;
            if(op) std::cout << st.getSum(l, r) << "\n";
            else st.updateIncrease(l, r);
        }
    }
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;

#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define pi  3.14159265359
#define mod 1000000007

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

    int n,m;
    cin>>n>>m;
    char val[5+1]={'a','a','a','a','a'};
    bool t=false;
    for(int i=1;i<=m;i++)
    {
        int x;char y;
        cin>>x>>y;
        if(val[x]=='a'){
            val[x]=y;
        }else{
        if(val[x]!=y){
            t=true;
        }
        }
    }

    if(t){
        cout<<-1;
    }else if(val[1]=='0'&&n>=2){
        cout<<-1;
    }else{
     if(val[1]=='a'){
        if(n>=2)
        val[1]='1';
        else
        val[1]='0';
     }
     for(int i=1;i<=n;i++)
     {
         if(val[i]!='a') cout<<val[i];
         else  cout<<0;
     }
    }
}

#include <bits/stdc++.h>

int solve() {
  int N, M; std::cin >> N >> M;
  std::vector<int> s(M);
  std::vector<int> c(M);
  for (int i = 0; i < M; ++i) std::cin >> s[i] >> c[i];

  /* 全探索 */
  for (int n = 0; n < std::pow(10,N); ++n) {
    int digit = 1;      // 0は1桁
    std::vector<int> each_num(1, n%10); // 一の位
    int x = n/10;

    while (x) {
      digit++;
      each_num.push_back(x%10);
      x /= 10;
    }

    if (digit != N) continue; // 桁数の条件を満たさない

    std::reverse(each_num.begin(), each_num.end());

    bool ok = true; // 条件を満たす整数が存在するかどうか

    for (int i = 0; i < M; ++i)
      if (each_num[s[i]-1] != c[i]) ok = false; // 一つでも条件を満たさないものがあれば

    if (ok) return n; // 桁数とそれぞれの桁の数字が条件を満たす
  }

  return -1;

}

int main() {
  std::cout << solve() << std::endl;

  return 0;
}


// Next Mayor
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1159&lang=jp

#include<iostream>
#include<vector>

using namespace std;

const int NONE = -1;

int N, P;
vector<int> player;
int bowl;
int winner;

bool is_win(int i) {
    for (int nth = 0; nth < N; ++nth) {
        if (nth == i) continue;
        if (player[nth] != 0) return false;
    }
    return true;
}

void pick_up(int i) {
    if (bowl == 0) {
        bowl += player[i];
        player[i] = 0;
        return;
    }
    else if (bowl == 1) {
        bowl--;
        player[i]++;
        if (is_win(i)) winner = i;
        return;
    }
    bowl--;
    player[i]++;
    return;
}

int main() {
    while(cin >> N >> P && N) {
        player.assign(N, 0);
        bowl = P;
        winner = NONE;
        int i = 0;
        while (true) {
            if (winner != NONE) break;
            pick_up(i);
            i = (i + 1) % N;
        }
        cout << winner << endl;
    }
    return 0;
}

#include<iostream>
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p){
		bool f=true;
		int d[55]={};
		int cn=p;
		if(n==0)break;
		while(f){
			for(int i=0;i<n;i++){
				if(cn>0){
					d[i]++;
					cn--;
				}
				else if(cn==0){
					cn=d[i];
					d[i]=0;
				}
				if(d[i]==p){
						cout<<i<<endl;
						f=false;
						break;
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <array>
#include <set>
#include <vector>
#include <climits>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<pair<int, int>> graph[n];
    array<int, 3> points[n];
    int cost[n];
    for (int i = 0; cin >> points[i][0] >> points[i][1]; i++) {
        points[i][2] = i;
        cost[i] = INT_MAX;
    }
    sort(points, points + n, [](const array<int, 3>& left, const array<int, 3>& right) {
        return left[0] < right[0];
    });
    for (int i = 1; i < n; i++) {
        auto dist = min(abs(points[i - 1][0] - points[i][0]), abs(points[i - 1][1] - points[i][1]));
        graph[points[i - 1][2]].emplace_back(points[i][2], dist);
        graph[points[i][2]].emplace_back(points[i - 1][2], dist);
    }
    sort(points, points + n, [](const array<int, 3>& left, const array<int, 3>& right) {
        return left[1] < right[1];
    });
    for (int i = 1; i < n; i++) {
        auto dist = min(abs(points[i - 1][0] - points[i][0]), abs(points[i - 1][1] - points[i][1]));
        graph[points[i - 1][2]].emplace_back(points[i][2], dist);
        graph[points[i][2]].emplace_back(points[i - 1][2], dist);
    }
    long long result = 0;
    set<int> mst = {0};
    set<pair<int, int>> set;
    auto update = [&](const vector<pair<int, int>>& nodes) {
        for (auto& p : nodes) {
            if (mst.find(p.first) != end(mst)) {
                continue;
            }
            if (cost[p.first] > p.second) {
                set.erase({cost[p.first], p.first});
                cost[p.first] = p.second;
                set.emplace(p.second, p.first);
            }
        }
    };
    update(graph[0]);
    do {
        auto current = set.begin()->second;
        auto weight = set.begin()->first;
        set.erase(set.begin());
        mst.emplace(current);
        result += weight;
        update(graph[current]);
    } while (!set.empty());
    cout << result;
    return 0;
}

#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int main() {

	int sum = 0;
	int small = 114514;

	for (int i = 0; i < 4; i++) {
		int t;
		scanf("%d", &t);
		sum += t;
		small = min(small, t);
	}
	sum -= small;

	int t,u;
	scanf("%d", &t);
	scanf("%d", &u);

	printf("%d\n", sum + max(t, u));

	return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int kingaku(void);
int saitan(void);
#define WHITE 0
#define GLAY 1
#define BLACK 2
#define INF 9999
int n;	//	??????????????±?????°
int m;	//	?§??????°
int p;	//	????????°
int q;	//	??°?????°
int minv;
int cost[101][101];
int Time[101][101];
int dd[101];
int main()
{
	int a,b,c,t,k,r;
	while(1){
		memset(cost,INF,sizeof(cost));
		memset(Time,INF,sizeof(Time));
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for(int i=0;i<n;i++){
			cin >> a >> b >> c >>t;
			cost[a][b] = cost[b][a] = c;
			Time[a][b] = Time[b][a] = t;
		}
		cin >> k;
		for(int i=0;i<k;i++){
			cin >> p >> q >> r;
			if(r == 0)
				cout << kingaku() << endl;
			else if(r == 1)
				cout << saitan() << endl;
			//for(int i=1;i<=m;i++)
			//	cout << i << " " << dd[i] << endl;
		}
	}
	return 0;
}

int kingaku()
{
	int u;
	int color[101];
	memset(dd,INF,sizeof(dd));
	memset(color,WHITE,sizeof(color));
	dd[p] = 0;
	color[p] = GLAY;
	while(1){
		u = -1;
		minv = INF;
		for(int i=1;i<=m;i++){
			if(minv > dd[i] && color[i] != BLACK){
				minv = dd[i];
				u = i;
			}
		}
		if(u==-1) break;
		color[u] = BLACK;
		for(int i=1;i<=m;i++){
			if(cost[u][i] != INF && color[i] != BLACK){
				if(dd[i] > dd[u] + cost[u][i])	//
					     //1?§?     1?§???¨??\?????????????§?
					dd[i] = dd[u] + cost[u][i];
			}
		}
	}
	return dd[q];
}

int saitan()
{
	int u;
	int color[101];
	memset(dd,INF,sizeof(dd));
	memset(color,WHITE,sizeof(color));
	dd[p] = 0;
	color[p] = GLAY;
	while(1){
		u = -1;
		minv = INF;
		for(int i=1;i<=m;i++){
			if(minv > dd[i] && color[i] != BLACK){
				minv = dd[i];
				u = i;
			}
		}
		if(u==-1) break;
		color[u] = BLACK;
		for(int i=1;i<=m;i++){
			if(Time[u][i] != INF && color[i] != BLACK){
				if(dd[i] > dd[u] + Time[u][i])
					dd[i] = dd[u] + Time[u][i];
			}
		}
	}
	return dd[q];
}

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

using namespace std;

int main () {
    // input
    string str;
    cin >> str;

    int q;
    cin >> q;

    string result = str;
    int res_len = result.length();
    for (int i=0; i<q; i++) {
        string order;
        int a, b;
        cin >> order >> a >> b;
        if (order == "print") {
            // output
            cout << result.substr(a, b-a+1) << endl;
        } else if (order == "reverse") {
            string reversed = "";
            for (int j=0; j<a; j++) {
                reversed += result[j];
            }
            for (int jj=b; jj>a-1; jj--) {
                reversed += result[jj];
            }
            for (int jjj=b+1; jjj<res_len; jjj++) {
                reversed += result[jjj];
            }
            result = reversed;
            //cout << reversed << endl;
        } else if (order == "replace") {
            string p;
            cin >> p;
            string replaced = "";
            for (int k=0; k<a; k++) {
                replaced += result[k];
            }
            for (int kk=a; kk<b+1; kk++) {
                replaced += p[kk-a];
            }
            for (int kkk=b+1; kkk<res_len; kkk++) {
                replaced += result[kkk];
            }
            result = replaced;
            //cout << result << endl;
        }
    }
}


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

int main(){
	int data[11][11];
	int n;
	cin >> n;
	while (n){

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				cin >> data[i][j];
			}
		}
		int sum_col[10] = { 0 };
		int sum_low[10] = { 0 };

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum_col[i] += data[i][j];
				sum_low[i] += data[j][i];
			}
			data[i][n] = sum_col[i];
			data[n][i] = sum_low[i];
		}
		int sum = 0;
		for (int i = 0; i < n; i++){
			sum += data[i][n];
		}
		data[n][n] = sum;
		for (int i = 0; i < n + 1; i++){
			for (int j = 0; j < n + 1; j++){
				cout << setw(5) << right << data[i][j];
			}
			cout<<"\n";
		}
		cin >> n;
	}

	return 0;
}

#include <iostream>
using namespace std;

#include <algorithm>

int main() {
	int n;
	while (1) {
		cin >> n;
		if (n==0) break;
		int p1=0, p2=0;
		int c1, c2;
		for (int i=0; i<n; i++) {
			cin >> c1 >> c2;
			if (c1==c2) p1+=c1, p2+=c2;
			if (c1>c2) p1+=c1+c2;
			if (c1<c2) p2+=c1+c2;
		}
		cout << p1 << " " << p2 << endl;
	}
	return 0;
}


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

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

#define m0(x) memset(x, 0, sizeof(x))
#define all(x) x.begin(), x.end()
#define rep(i, n) for(int i = 0; i < (n); i++)
#define asort(x) sort(all(x));
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>());
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define MINS(m, s) m.insert(make_pair(s, 1));
#define MFIN(m, s) m.find(s) != m.end()
const int mod = 1000000007;

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

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

bool isPrime(ll x) {
    ll i;
    if(x < 2)
        return 0;
    else if(x == 2)
        return 1;
    if(x % 2 == 0)
        return 0;
    for(i = 3; i * i <= x; i += 2)
        if(x % i == 0)
            return 0;
    return 1;
}

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

    ll ab_gcd = gcd(A, B);
    vector<ll> ab_divisor;
    ab_divisor = divisor(ab_gcd);

    ll cnt = 1;
    for(ll i = 0; i < ab_divisor.size(); i++) {
        if(isPrime(ab_divisor[i])) {
            cnt++;
        }
    }

    cout << cnt << endl;

    return 0;
}

#include <iostream>
using namespace std;

int main(){
  int a, b, x;
  cin >> a >> b;
  x = (b - a) * 2;
  cout << a + x << endl;
}


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

// #define int long long

/* short */
#define pb push_back
#define print(Target) cout << Target << '\n'

/* REPmacro */
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, a, n) for (int i = (a); i < (n); i++)

/* typedef */
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef pair<int, int> pii;

/* func */
inline bool is_uruu(int y) {return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));}

/* const */
const int INF = INT_MAX;

int dp[31];

int shinobu(int n)
{
  if (dp[n] != -1){
    return (dp[n]);
  }
  if (n == 1) return (1);
  if (n == 2) return (2);
  if (n == 3) return (4);
  return (shinobu(n - 1) + shinobu(n - 2) + shinobu(n - 3));
}

signed main()
{
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n, n){
    fill(dp, dp + 31, -1);
    print(shinobu(n) / 3650 + 1);
  }
  return (0);
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    string N;
    cin >> N;
    for(int i = 0; i < 8; i++) {
        string ans;
        ans += N.at(0);
        int sum = N.at(0) - '0';
        for(int j = 0; j < 3; j++) {
            //cout << (i >> (j + 1)) << endl;
            if(i >> j & 1) {
                sum += N.at(j + 1) - '0';
                ans += '+';
                ans += N.at(j + 1);
            } else {
                sum -= N.at(j + 1) - '0';
                ans += '-';
                ans += N.at(j + 1);
            }
        }
        //˜cout << sum << endl;
        if(sum == 7) {
            cout << ans << "=7" << endl;
            break;
        }

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=100010;
const ll mod=1e9+7;

vector<int> edges[2100];

struct UnionFind {
  vector<int> par,sizes;                     // par[i]:iの親の番号(ex)par[3]=2 : 3の親が2
  UnionFind(int n) : par(n),sizes(n,1)  {    //最初は全てが根であるとして初期化
    for(int i=0; i<n; i++){ par[i]=i; }
  }
  int root(int x) {                       //xが属する木の根を再帰で得る:root(x)={xの木の根}
    if(par[x]==x){ return x; }
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {                 // xとyの木を併合
    int rx = root(x);                        //xの根をrx
    int ry = root(y);                        //yの根をry
    if(rx == ry){ return; }                  //xとyの根が同じ(=同じ木にある)時はそのまま
    if(sizes[rx]<sizes[ry]){ swap(rx, ry); } //xの木をおおきくする
    par[ry] = rx;
    sizes[rx] += sizes[ry];                  //sizes[ry]はもう使わない
  }

  bool check(int x, int y) {                 // x,yが属する木が同じならtrueを返す
    return root(x) == root(y);
  }

  int size(int x){                           //xが含まれる木の大きさを返す
    return sizes[root(x)];
  }
};

bool dfs(int p, set<int> &a){
  for(auto to:edges[p]){
    if(a.find(to)!=a.end()){ return true; }
    a.insert(to);
    if(dfs(to,a)){ return true; } else { a.erase(to); }
  }
  return false;
}

int main(){
  int n,m;
  cin >> n >> m;
  UnionFind uf(n);
  vector<P> aded;

  for(int i=0; i<m; i++){
    int x,y; cin >> x >> y; x--; y--;
    if(uf.check(x,y)){
      aded.emplace_back(x,y);
    } else {
      uf.unite(x,y);
      edges[x].emplace_back(y);
    }
  }
  set<int> shortest;
  int cnt=inf;
  for(auto tmp:aded){
    set<int> ans;
    ans.insert(tmp.F);
    ans.insert(tmp.S);
    if(dfs(tmp.S,ans)){
      if(ans.size()<cnt){
        cnt=ans.size();
        shortest = ans;
      }
    }
  }
  if(cnt!=inf){
      cout << shortest.size() << endl;
      for(auto anno:shortest){cout << anno+1 << endl;}
      return 0;
  }
  cout << -1 << endl;
  return 0;
}

#include<bits/stdc++.h>
#define MN 100000
#define ll long long
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
ll res,v[MN+5],V[MN+5],ans,g[MN+5],G[MN+5];
int n,X,L[MN+5],R[MN+5],b[MN+5],rk[MN+5];
bool cmp(int x,int y){return v[x]+g[x]>v[y]+g[y];}
ll GetV(ll*s,int l,int r,int x){return s[r]-s[l-1]-((x>=l&&x<=r)?s[x]-s[x-1]:0);}
inline int Div(ll x,int y){return (x+y-1)/y;}
int main()
{
    n=read();X=read();
    for(int i=1;i<=n;++i)
    {
        b[i]=read();L[i]=read();R[i]=read();
        v[i]=1LL*(X-b[i])*R[i];g[i]=1LL*b[i]*L[i];
        rk[i]=i;ans+=b[i];
    }
    sort(rk+1,rk+n+1,cmp);
    for(int i=1;i<=n;++i) V[i]=V[i-1]+v[rk[i]],G[i]=G[i-1]+g[rk[i]];
    for(int i=1;i<=n;++i)
    {
        int l=0,r=n-1,mid,res=n;
        while(l<=r)
        {
            mid=l+r>>1;
            ll Val=GetV(V,1,mid,i)-GetV(G,mid+1,n,i)+v[rk[i]];
            if(Val>=0) res=mid,r=mid-1; else l=mid+1;
        }
        ll Val=GetV(V,1,res,i)-GetV(G,res+1,n,i);
        if(Val>=0) ans=min(ans,1LL*(res-(res>=i))*X+max(0,Div(g[rk[i]]-Val,L[rk[i]])));
        else ans=min(ans,1LL*(res-(res>=i))*X+b[rk[i]]+Div(-Val,R[rk[i]]));
    }
    cout<<ans;
	return 0;
}


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

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

vector<vector<int>> Q(N, vector<int>(2));
for(int i=0;i<N;i++){
  int l,r;
  cin>>l>>r;
  Q[i][0]=l;
  Q[i][1]=r;
}

// 左側の順にソート
sort(Q.begin(),Q.end());

vector<int> A(N), B(N);
// A→１回目のコンテストの右側
// A[i]→i個目まで１回目のコンテストで出題
A[0] = Q[0][1];
for(int i=1;i<N;i++){
  A[i]= min(A[i-1],Q[i][1]);
}
// B→２回目のコンテストの右側
// B[i]→i個目まで１回目のコンテストで出題
B[N-1]=Q[N-1][1];
for(int i=N-2;i>=0;i--){
  B[i]= min(B[i+1],Q[i][1]);
}

int ans=0;

for(int i=0;i<N-1;i++){
  //cout<<A[i]<<" "<<Q[i][0]<<" "<<B[i+1]<<" "<<Q[N-1][0]<<endl;;
  ans = max(ans, max(0,A[i]-Q[i][0]+1)+max(0,B[i+1]-Q[N-1][0]+1));
  ans = max(ans, max(0,A[N-1]-Q[N-1][0]+1)+max(0,Q[i][1]-Q[i][0]+1));
//cout<<ans<<endl;
}
cout<<ans<<endl;

}

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

typedef pair<double, double> pd;
typedef vector<pd> vpd;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);

template <typename First>
void debug_print_in(First first){
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

template <typename First>
void OUT(First first){
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

template<class t, class u> t chmax(t&a,u b){if(a<b)a=b; return a;};
template<class t, class u> t chmin(t&a,u b){if(a>b)a=b; return a;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC

template <typename T>
void vec_print(vector<T> VEC){
  REP(i, VEC.size()){
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  REP(i,MAT.size()){
    REP(j,MAT[i].size()){
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  IN(N);
  vpll RL(N);
  REP(i,N){
    IN(RL[i].second, RL[i].first);
  }

  sort(ALL(RL));

  vll Lmax(N, RL[0].second);
  REP(i,N-1){
    Lmax[i+1] = max(Lmax[i], RL[i+1].second);
  }

  /*
  REP(i,N){
    debug_print(RL[i].second, RL[i].first);
  }
  */

  ll ans = max(RL[0].first-Lmax[N-2]+1, (ll)0)+(RL[N-1].first-RL[N-1].second)+1;
  ll Lmax_after = RL[N-1].second;
  for(ll i=N-2; i>0; --i){
    ll Lmax1 = Lmax[i-1];
    ll Rmin1 = RL[0].first;
    ll Lmax2 = RL[i].second;
    ll Rmin2 = RL[i].first;

    ll LR1 = max(Rmin1-Lmax1+1, (ll)0);
    ll LR2 = max(Rmin2-Lmax2+1, (ll)0);
    ll LR1_1 = max(Rmin1-max(Lmax_after, Lmax1)+1, (ll)0);
    ll LR2_1 = max(Rmin2-max(Lmax_after, Lmax2)+1, (ll)0);

    ll ans_tmp = max(LR1+LR2_1, LR1_1+LR2);

    chmax(ans, ans_tmp);
    chmax(Lmax_after, Lmax2);
  }

  OUT(ans);

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN	16
#define MAXM	32768
#define MAXS	14348907
#define INF	1e9
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
int mp[MAXN][MAXN], bit[MAXN];
int ups[MAXM], upt[MAXM];
int sum[MAXN][MAXM], cost[MAXS];
int dp[MAXN][MAXM];
int n, m, goal;
void init() {
	goal = (1 << n) - 1;
	bit[1] = 1;
	for (int i = 2; i <= n; i++)
		bit[i] = bit[i - 1] << 1;
	for (int i = 1; i <= n; i++)
	for (int s = 0; s <= goal; s++) {
		if (s & bit[i]) continue;
		for (int j = 1; j <= n; j++)
			if (s & bit[j]) sum[i][s] += mp[i][j];
	}
	for (int s = 0; s <= goal; s++) {
		int tmp = s, sv = 0, tv = 0, now = 1;
		while (tmp) {
			if (tmp & 1) sv += 2 * now;
			if (tmp & 1) tv += now;
			now *= 3; tmp >>= 1;
		}
		ups[s] = sv; upt[s] = tv;
	}
	for (int s = 0; s <= goal; s++)
	for (int tmp = goal ^ s, t = goal ^ s; t; t = (t - 1) & tmp)
	for (int i = 1; i <= n; i++)
		if (s & bit[i]) cost[ups[s] + upt[t]] += sum[i][t];
}
void update(int &x, int y) {
	x = min(x, y);
}
int main() {
	read(n), read(m);
	for (int i = 1; i <= m; i++) {
		int x, y, z;
		read(x), read(y), read(z);
		mp[x][y] = mp[y][x] = z;
	}
	init();
	for (int i = 1; i <= n; i++)
	for (int j = 0; j <= goal; j++)
		dp[i][j] = INF;
	dp[1][bit[1]] = 0;
	for (int s = 0; s <= goal; s++)
	for (int i = 1; i <= n; i++) {
		if ((bit[i] & s) == 0 || dp[i][s] == INF) continue;
		int ts = s ^ bit[i];
		for (int j = 1; j <= n; j++) {
			if ((bit[j] & s) || mp[i][j] == 0) continue;
			update(dp[j][s ^ bit[j]], dp[i][s] + sum[j][ts]);
		}
		for (int tmp = goal ^ s, t = goal ^ s; t; t = (t - 1) & tmp)
			update(dp[i][s ^ t], dp[i][s] + cost[ups[ts] + upt[t]]);
	}
	cout << dp[n][goal] << endl;
	return 0;
}

//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define bged(v) (v).begin(),(v).end()
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
typedef long long ll;
const int Imx=2147483647;
const ll Lbig=2e18;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it,len;
	fastio(){it=len=0;}
	inline char get()
	{
		if(it<len)return s[it++];it=0;
		len=fread(s,1,100000,stdin);
		if(len==0)return EOF;else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while(c==' '||c=='\n')c=get();
		if(it>0)it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0;bool ng=0;char c;c=_buff.get();
	while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
	if(c=='-')ng=1,c=_buff.get();
	while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
	return ng?-r:r;
}
template<class T> inline void putnum(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=_buff.get();while(c<=32)c=_buff.get();return c;}
ll qpow(ll x,ll k){return k==0?1:1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
char s[311];
int n,k;
int dp[305][305][305];
void upd(int &x,int v){x=max(x,v);}
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	cin>>k;
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=n;j>=i;j--)
		{
			for(int t=0;t<=k;t++)
			{
				int cur=dp[i][j][t];
				upd(ans,cur+(s[i]==s[j])*2-(i==j));
				if(t<k)upd(ans,cur+2-(i==j));
				if(i<j)
				{
					upd(dp[i+1][j][t],cur);
					upd(dp[i][j-1][t],cur);
					upd(dp[i+1][j-1][t],cur+(s[i]==s[j])*2-(i==j));
					if(t<k)upd(dp[i+1][j-1][t+1],cur+2-(i==j));
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define eps 1e-7
using namespace std;
int n,d1,d2,cnt=0;
bool f[1005][1005];
inline void solve(int d){
	int b=0;
	while(!(d%4))d>>=2,++b;
	if(d&1){
		for(int i=0;i<(n<<1);++i)
			for(int j=0;j<(n<<1);++j)
				if(((i>>b)+(j>>b))&1)f[i][j]=true;
		return;
	}
	for(int i=0;i<(n<<1);++i)
		for(int j=0;j<(n<<1);++j)
			if((i>>b)&1)f[i][j]=true;
}
int main(){
	scanf("%d%d%d",&n,&d1,&d2);
	solve(d1),solve(d2);
	for(int i=0;i<(n<<1);++i)
		for(int j=0;j<(n<<1);++j)
			if(cnt<n*n&&!f[i][j])printf("%d %d\n",i,j),++cnt;
	return 0;
}


#include <iostream>
#include <vector>

constexpr bool divisible(const std::size_t n, const std::size_t m)
{
	return (0 == n % m);
}

constexpr bool help_prime(const std::size_t n, const std::size_t m)
{
	return (m * m <= n) ?
        !divisible(n, m) && help_prime(n, m + 1) :
        true;
}

constexpr bool prime(const std::size_t n)
{
	return (n <= 1) ?
        false :
        help_prime(n, 2);
}

std::pair<std::size_t, std::size_t> find_twin_prime(const std::size_t limit, const bool next_skip = false)
{
    return prime(limit) ?
        prime(limit - 2) ?
        std::make_pair(limit - 2, limit) :
        find_twin_prime(limit - (next_skip ? 3 : 1), true) :
        find_twin_prime(limit - (next_skip ? 2 : 1), false);
}

int main()
{
    std::vector<std::size_t> ns;
    std::size_t n;

    while(std::cin >> n, n != 0)
    {
        ns.push_back(n);
    }

    for(const auto a : ns)
    {
        const auto&& t = find_twin_prime(a);
        std::cout << t.first << " " << t.second << std::endl;
    }
}

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

bool furui[10010];
vector<int> prime;
vector<int> twin;

void mkprime() {
	for(int i = 0; i < 10010; i++) furui[i] = true;
	furui[0] = furui[1] = false;
	for(int i = 0; i < 10010; i++) {
		if(furui[i]) {
			prime.push_back(i);
			for(int j = i * 2; j < 10010; j += i) {
				furui[j] = false;
			}
		}
	}
	for(int i = 0; i < prime.size() - 1; i++) {
		if(prime[i + 1] - prime[i] == 2)
			twin.push_back(prime[i + 1]);
	}
	sort(twin.begin(), twin.end());
}

int main() {
	int n;
	mkprime();
	while(cin >> n, n) {
		vector<int>::iterator iter;
		iter = upper_bound(twin.begin(), twin.end(), n);
		cout << *(iter-1) - 2 << " " << *(iter-1) << endl;
	}
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
//const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

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

  ld n, m, d;
  cin >> n >> m >> d;

  ld ans = (n - d) * 2 * (m - 1) / n / n;
  if (d == 0) ans /= 2;
  cout << ans << '\n';

  return 0;
}

#include<iostream>
using namespace std;
int main(){
	int a,b,c;
	cin>>a>>b>>c;
	if(a==b&&b!=c||a==c&&b!=c||b==c&&a!=b){
		cout<<"Yes";
	}else{
		cout<<"No";
	}
	return 0;
}


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

int main() {
	int n;
	cin>>n;
	int arr[n];
	for(int i=0; i<n; i++){
		cin>>arr[i];
	}
	multiset<int>s;
	s.insert(arr[0]);
	for(int i=1; i<n; i++){
		int val=arr[i];
		auto it=s.begin();
		int vals=(*it);
		if(val<=vals){
			s.insert(val);
			continue;
		}
		it=s.end();
		it--;
		vals=(*it);
		if(vals<val){
			s.erase(s.find(vals));
			s.insert(val);
			continue;
		}
		it=s.lower_bound(val);
		it--;
		s.erase(s.find((*it)));
		s.insert(val);
	}
	cout<<s.size();
	return 0;
}

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

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

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

	int b[n+1]; //b[i]=cost of i->i+1
	int sum=0;
	for(int i=0;i<=n;i++){
		b[i]=abs(a[i+1]-a[i]);
		sum+=b[i];
	}

	for(int i=1;i<=n;i++)
		cout<<sum-b[i-1]-b[i]+abs(a[i+1]-a[i-1])<<endl;
}


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

#define INF 1e9
#define LLINF 1e18
#define pi 3.14159265358979323
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

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

int main() {
    string s;
    cin >> s;
    if (s.size() % 2 != 0) {
        cout << "No" << endl;
        return 0;
    }
    rep(i, s.size()) {
        if (i % 2 == 0 && s.at(i) != 'h') {
            cout << "No" << endl;
            return 0;
        }
        else if (i % 2 == 1 && s.at(i) != 'i') {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}


#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

#define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
void debug(mat m){REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl; }}

int modpow(int x, int n, int m){
    int a = 1;
    IREP(i, 64){
        a = (a * a) % m;
        if(((n >> i) & 1) == 1) a = (a * x) % m;
    }
    return a;
}

class Combination
{
public:

    vec fact, invfact;
    int MAX_N, mod;

    Combination(int MAX_N, int mod): MAX_N(MAX_N), mod(mod) {
        initialize();
    }

    void initialize(){
        fact = vec(MAX_N + 1);
        invfact = vec(MAX_N + 1);
        fact[0] = 1;
        FOR(i, 1, MAX_N + 1){
            fact[i] = (fact[i - 1] * i) % mod;
        }
        invfact[MAX_N] = modpow(fact[MAX_N], mod - 2, mod);
        IREP(i, MAX_N){
            invfact[i] = (invfact[i + 1] * (i + 1)) % mod;
        }
    }

    int nCr(int n, int r){
        if(r > n || r < 0 || n < 0) return 0;
        if(n > MAX_N){
            MAX_N = n;
            initialize();
        }
        int a = fact[n];
        a = (a * invfact[r]) % mod;
        a = (a * invfact[n - r]) % mod;
        return a;
    }
};

signed main(){

    int K, N; cin >> K >> N;
    int mod = 998244353;
    Combination comb(N + K, mod);

    FOR(i, 2, 2 * K + 1){
        if(i % 2 == 1){
            int p = min(i, 2 * K + 2 - i) / 2;
            int ans = 0;
            REP(j, p + 1){
                int k = K - 2 * p + j;
                int n = N - j;
                ans += ((comb.nCr(n + k - 1, n) * modpow(2, j, mod)) % mod) * comb.nCr(p, j);
                ans %= mod;
            }
            cout << ans << endl;
        }else{
            int p = min(i, 2 * K + 2 - i) / 2 - 1;
            int ans = 0;
            REP(j, p + 1){
                int k = K - 1 - 2 * p + j;
                int n = N - j;
                ans += ((comb.nCr(n + k - 1, n) * modpow(2, j, mod)) % mod) * comb.nCr(p, j);
                ans %= mod;
                n = N - j - 1;
                ans += ((comb.nCr(n + k - 1, n) * modpow(2, j, mod)) % mod) * comb.nCr(p, j);
                ans %= mod;
            }
            cout << ans << endl;
        }
    }

    return 0;
}

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const ll N = 2e5 + 5, mod = 1e9 + 7;

ll x[N], y[N], sumY;

int main()
{
    ll n, m;
    scanf("%lld%lld", &n, &m);
    ll ans = 0, temp;
    for(ll i = 0 ; i < n ; i++) scanf("%lld", x + i);
    for(ll i = 0 ; i < m ; i++) scanf("%lld", y + i);
    for(ll i = 1 ; i < m ; i++)
    {
        sumY += (((i * (m - i)) % mod) * ((y[i] - y[i - 1]) % mod)) % mod;
        sumY %= mod;
    }
    for(ll i = 1 ; i < n ; i++)
    {
        temp = (((i * (n - i)) % mod) * ((x[i] - x[i - 1]) % mod)) % mod;
        ans += (temp * sumY) % mod;
        ans %= mod;
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> vec(N);
  rep(i, N) {
    cin >> vec.at(i);
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  int sum = 0;
  rep(i, K) {
    sum += vec.at(i);
  }
  cout << sum << endl;
}

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

int main() {
  int A,B;
  cin>>A>>B;
  int count_ana=1,count_ans=0;
  while(count_ana<B){
  count_ana+=A-1;
  count_ans++;
  }
  cout<<count_ans<<endl;
}