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

int main(){
char x[200];
char Roma[7]={'I','V','X','L','C','D','M'};
int Arabia[7]={1,5,10,50,100,500,1000};
int a=0,b,ans=0,ls;
string s;
while(cin>>s){
ans=0;
a=0;
ls=s.size();
for(int i=ls-1;i>-1;i--){
for(int j=0;j<7;j++){
if(s[i]==Roma[j])b=Arabia[j];}
if(b>=a){ans+=b; a=b;}
if(b<a)ans-=b;}
cout<<ans<<endl;}}

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


#include <bits/stdc++.h>

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

using namespace std;
using lint=long long;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(){}
	Fenwick_tree(int n){ build(n); }
	Fenwick_tree(const vector<G>& a){ build(a); }
	void build(int n){
		a.assign(n,G{});
	}
	void build(const vector<G>& a){
		this->a=a;
		for(int i=1;i<a.size();i++) if(i+(i&-i)-1<a.size()) (this->a)[i+(i&-i)-1]+=(this->a)[i-1];
	}
	void add(int i,const G& val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(G val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(G val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

int main(){
	int n,q; scanf("%d%d",&n,&q);
	vector<lint> a(n);
	rep(i,n) scanf("%lld",&a[i]);

	Fenwick_tree<lint> F(a);
	rep(_,q){
		int t,l,r; scanf("%d%d%d",&t,&l,&r);
		if(t==0) F.add(l,r);
		else     printf("%lld\n",F.sum(l,r));
	}

	return 0;
}

#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
using namespace std;
using namespace atcoder;
int main(){
  int N, Q;
  cin >> N >> Q;
  fenwick_tree<long long> a(N);
  for (int i = 0; i < N; i++){
    int x;
    cin >> x;
    a.add(i, x);
  }
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 0){
      int p, x;
      cin >> p >> x;
      a.add(p, x);
    }
    if (t == 1){
      int l, r;
      cin >> l >> r;
      cout << a.sum(l, r) << endl;
    }
  }
}


#include <iostream>
#include <vector>
#include <algorithm>
int main() {
	int n, m;
	std::cin >> n >> m;
	while (n != 0 && m != 0) {
		int whole_price = 0;
		std::vector<int> vegi(n);
		for (auto &v : vegi) std::cin >> v;
		std::sort(vegi.begin(), vegi.end());
		for (auto i = 1; i <= n; ++i) {
			whole_price += (i % m == 0) ? 0 : vegi[n - i];
		}
		std::cout << whole_price << std::endl;
		std::cin >> n >> m;
	}
}

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

const int N_MAX = 1000;

int n, m;
int p[N_MAX];

int main () {
	while (cin >> n >> m, n || m) {
		int sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> p[i];
			sum += p[i];
		}
		sort(p, p + n, greater<int>());
		for (int i = m - 1; i < n; i += m) {
			sum -= p[i];
		}
		cout << sum << endl;
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <tuple>
using namespace std;
typedef long long ll;
#define REP(i,n) for (int i = 0; i < n; i++)
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;

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

#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#include <climits>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926535;

class UnionFind {
	std::vector<int> p;

public:
	UnionFind(int n) : p(n, -1) {}

	int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }

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

	bool unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (p[y] < p[x]) std::swap(x, y);
		if (p[x] == p[y]) --p[x];
		p[y] = x;
		return true;
	}
};

ll fast(ll n, ll k) {
	if (k == 0) {
		return 1;
	}
	if (k == 1) {
		return n;
	}
	if (k % 2 == 0) {
		ll tmp = fast(n, k / 2);
		return tmp * tmp % MOD;
	}
	else {
		ll tmp = fast(n, k / 2);
		tmp = tmp * tmp % MOD;
		return n * tmp % MOD;
	}
}

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

	cout << 180 * (N - 2) << endl;
}


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

long long dp[2][3003];

int main()
{

        long long n,m,a,b,c,d,e,f,g,h,p,q,r,fr,sc,tr,sz,tz,i,j,k,mx=LLONG_MIN,mn=LLONG_MAX;
        long long x=0,y=0,cnt=0,res=0,ttl=0,ans=0,sum=0; long long flg=0,flag=1,na=0,as=1;
        vector<long long>u,v,w; vector< pair<long long,long long> >vct; vector<string>vst;
        set<long long>st,nt,tt; map<long long,long long>mp,nq,qr; string str,ttr,ntr;

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

        for(i=1;i<=n;i++)
        {
            scanf("%lld %lld",&a,&b); vct.push_back(make_pair(a,b));
        }

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

        long long idx,cap;

        for(idx=n-1;idx>=0;idx--)
        {
            for(cap=1;cap<=k;cap++)
            {
                fr=idx%2; sc=(idx+1)%2; a=vct[idx].first; b=vct[idx].second;

                p=0; q=0;

                if(idx==n-1 || cap-a<=0){  p=b; }
                else{  p=b+dp[sc][cap-a];   }

                if(idx!=n-1){  q=dp[sc][cap];  }

                dp[fr][cap]=max(p,q);

            }
        }

        printf("%lld\n",dp[0][k]);

    return 0;
}

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

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(A v) {
bool first = true; string res = "{";
for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); }
res += "}"; return res; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

const int N = 3005;
int n, t;
int A[N], B[N];
int dp[N][N][2];

int rec(int idx, int left, int last) {
    if (idx == n) {
        return 0;
    }
    if (left <= 0) {
        return 0;
    }
    if (dp[idx][left][last] != -1) return dp[idx][left][last];
    if (last == 1) {
        if (A[idx] <= left) {
            dp[idx][left][last] = max(rec(idx + 1, left - A[idx], 1) + B[idx], rec(idx + 1, left, 1));
        } else {
            dp[idx][left][last] = rec(idx + 1, left, 1);
        }
    } else {
        if (A[idx] <= left) {
            dp[idx][left][last] = max({rec(idx + 1, left - A[idx], 0) + B[idx], rec(idx + 1, left, 0), rec(idx + 1, left - 1, 1) + B[idx]});
        } else {
            dp[idx][left][last] = max(rec(idx + 1, left - 1, 1) + B[idx], rec(idx + 1, left, 0));
        }
    }
    return dp[idx][left][last];
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> t;
    REP(i, n) {
        cin >> A[i];
        cin >> B[i];
    }
    memset(dp, -1, sizeof dp);
    cout << rec(0, t, 0) << "\n";
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

void b_sort(int* a , int n, int* count){
	for(int i=n;i>=0;--i){
		for(int j=0;j<i-1;++j){
			if(a[j]>a[j+1]){
				swap(a[j],a[j+1]);
				(*count)++;
			}
		}
	}
}

int main(){
	int n , cnt=0;
	cin >> n;
	int*a = new int [n];
	for(int i=0;i<n ;++i){
		cin >> a[i];
	}
	b_sort(a , n , &cnt);
	for(int i=0;i<n-1 ; ++i){
		cout << a[i] << " ";
	}
	cout << a[n-1] << endl << cnt << endl;
	return 0;
}

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

int N;
int A[100];

void disp(void)
{
  for (int i=0; i<N; ++i) {
    cout << A[i];
    if (i != (N-1)) {
      cout << " ";
    } else {
      cout << "\n";
    }
  }
}

int main(void)
{
  cin >> N;
  for (int i=0; i<N; ++i) {
    cin >> A[i];
  }
  int count = 0;
  int flag = 1;
  while (flag) {
    flag = 0;
    for (int j=N-1; j>=1; --j) {
      if (A[j] < A[j-1]) {
        swap(A[j], A[j-1]);
        flag = 1;
        count++;
      }
    }
  }

  disp();
  cout << count << endl;
  return 0;
}


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

string alp = "zabcdefghijklmnopqrstuvwxy";

int main()
{

    ll n;
    cin >> n;
    string ans;
    while (n > 0) {
        int amari = n % 26;
        ans += alp[amari];
        if (n % 26 == 0) {
            n /= 26;
            n--;
        } else n /= 26;
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
    return 0;
}

#include<stdio.h>
int main(){
    long long input;
    scanf("%lld",&input);
    int i = 0;
    char ans[100];
    while(input > 0){
        int temp = input % 26;
        if(temp == 0){
            ans[i++] = 'z';
            input = (input/26) - 1;
        }
        else{
            ans[i++] = (temp - 1) + 'a';
            input = input/26;
        }
    }
    ans[i] = '\0';
    for(int j = i - 1; j >= 0; j--){
        printf("%c",ans[j]);
    }
    return 0;
}


#include "bits/stdc++.h"
#define ll long long
#define lld long double
#define MOD 998244353
#define inf 1000000000000000000ll
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y){if(y&1)res = (res*x)%md;x *= x; if(x>=md) x %= md; y >>= 1;}return res;}

using namespace std;

#define endl '\n'
#define int ll

int lcm(int a, int b) {
	int g = __gcd(a,b);
	int ans = a/g;
	ans *= b;
	return ans;
}

string s;
int k;

int dp[305][305][305];
int pre[305][305][305];
int pre1[305][305][305];
vector<int> v;

// int solve(int index, int out, int total) {
// 	if(out<0 || out>3) return 0;

// 	if(index==v.size()) {
// 		if(out==0) return 1;
// 		else return 0;
// 	}

// 	if(dp[index][out]!=-1) return dp[index][out];
// 	int ans = 0;

// 	// Consumer
// 	for(int i=0;i<=300;i++) {
// 		ans += solve(index+1,out+i, total);
// 		ans %= MOD;
// 	}

// 	// Producer
// 	for(int i=1;i<=v[index];i++) {
// 		ans += solve(index+1,out-i, total+i);
// 		ans %= MOD;
// 	}

// 	cout<<index<<" "<<out<<" -- "<<ans<<endl;
// 	return dp[index][out] = ans;
// }

signed main() {
	fast;

	cin>>s;
	cin>>k;

	int cur = 0;
	int n = s.size();
	k = min(k, n);

	for(int i=0;i<n;i++) {
		if(s[i]=='0') {v.pb(cur);cur=0;}
		else cur++;
	}
	v.pb(cur);

	for(int i=0;i<=300;i++) pre[v.size()][0][i] = 1, pre1[v.size()][0][0] = 1;

	int ff = 0;

	for(int i=(int)v.size()-1;i>=0;i--) {
		for(int tot=k;tot>=0;tot--) {

			for(int out=0;out<=300;out++) {
				int ans = 0;

				int sum = pre[i+1][tot][300] - ((out==0)?0:pre[i+1][tot][out-1]);
				// if(i==2 && tot==0 && out==0) cout<<"$$"<<sum<<endl;

				ans += sum;
				ans %= MOD;

				if(tot>=out) sum = ((out-(v[i]) < 0) ? pre1[i+1][tot-out][0]: pre1[i+1][tot-v[i]][out-v[i]]) - pre1[i+1][tot][out];
				else sum = ((tot-(v[i]) < 0) ? pre1[i+1][0][out-tot]: pre1[i+1][tot-v[i]][out-v[i]]) - pre1[i+1][tot][out];
				// if(i==2 && tot==0 && out==0) cout<<"$$"<<sum<<" "<<v[i]<<endl;

				ans += sum;
				ans %= MOD;

				if(out!=0) {
					pre[i][tot][out] = (ans + pre[i][tot][out-1])%MOD;
				}
				else {
					pre[i][tot][out] = ans;
				}

				if(out!=300) {
					pre1[i][tot][out] = (ans + pre1[i][tot+1][out+1])%MOD;
				}
				else {
					pre1[i][tot][out] = ans;
				}

				// cout<<"#"<<i<<" "<<tot<<" "<<out<<" -- "<<ans<<endl;

				if(i==0&&tot<=k&&out==0) ff += ans;
				ff %= MOD;
			}
		}
	}

	ff += MOD;
	ff %= MOD;
	cout<<ff;

}

const int LG = 21;
const int FN = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(FN);
    __ufact.resize(FN);
    __rev.resize(FN);
    __rev[1] = 1;
    for (int i = 2; i < FN; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < FN; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (k < 0 || k > n) return 0;
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

ll dp[200][10];
ll se[10];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    se[0] = 1;
    se[1] = 1;
    se[2] = 2;
    se[3] = 3;
    se[4] = 5;
    se[5] = 8;
    se[6] = 13;
    se[7] = 21;
    int h, w, k;
    cin >> h >> w >> k;
    dp[0][1] = 1;
    for (int i = 0; i < h; i++)
    {
        for (int j = 1; j <= w; j++)
        {
            dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * se[j - 1] % MOD * se[w - j]) % MOD;
            if (j - 1 >= 1) dp[i + 1][j - 1] = (dp[i + 1][j - 1] + dp[i][j] * se[j - 2] % MOD * se[w - j]) % MOD;
            if (j + 1 <= w) dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * se[j - 1] % MOD * se[w - j - 1]) % MOD;
        }
    }
    cout << dp[h][k];
}

/* Note:
Check constants at the beginning of the code.
    N is set to 4e5 but be careful in problems with large constant factor.
    Setting N in every problem is more effective.
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//FILE *fi = freopen("1.txt", "r", stdin);
//FILE *fo = freopen("1", "w", stdout);
//FILE *fi = freopen("P.inp", "r", stdin);
//FILE *fo = freopen("P.out", "w", stdout);
int n, Q;
int c[500001];
typedef pair<int, int> ii;
vector<ii> q[500001];
int bit[500001];
void update(int pos, int val)
{
    for (int i = pos; i <= n; i += i & -i)
        bit[i] += val;
}
int Get(int pos)
{
    int ans = 0;
    for (int i = pos; i; i -= i & -i)
        ans += bit[i];
    return ans;
}
int ans[500001];
int b[500001];
int main()
{
    //------------------------------
    // Faster
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //------------------------------
    cin >> n >> Q;
    for (int i = 1; i <= n; i++)
    {
        cin >> c[i];
    }
    int id = 0;
    for (int i = 1; i <= Q; i++)
    {
        int l, r;
        ++id;
        cin >> l >> r;
        q[r].push_back({l, id});
    }
    for (int i = 1; i <= n; i++)
    {
        int x = c[i];
        if (b[x])
            update(b[x], -1);
        b[x] = i;
        update(b[x], 1);
        for (auto Q : q[i])
        {
            ans[Q.second] = Get(n) - Get(Q.first - 1);
        }
    }
    for (int i = 1; i <= Q; i++)
        cout << ans[i] << "\n";
    return 0;
}

#include<bits/stdc++.h>

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

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

#define MAX 500005
//#define INF 1001001001

template <typename T>
struct RMQ {
  const T INF = numeric_limits<T>::max();
  int n;
  vector<T> dat, lazy;
  RMQ(int _n) : n(), dat(_n*4,INF), lazy(_n*4,INF) {
    int x = 1;
    while (x < _n) x <<= 1;
    n = x;
  }

  void update(int i, T x) {
    i += n-1;
    dat[i] = x;
    while (i > 0) {
      i = (i-1)/2;
      dat[i] = min(dat[i*2+1],dat[i*2+2]);
    }
  }

  T query(int a, int b, int k, int l, int r) {
    eval(k);
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return dat[k];
    T mid = (l+r)>>1;
    T vl = query(a,b,k*2+1,l,mid);
    T vr = query(a,b,k*2+2,mid,r);
    return min(vl,vr);
  }
  T query(int a, int b) { return query(a,b,0,0,n); }

  void eval(int k) {
    if (lazy[k] == INF) return;
    if (k < n-1) lazy[k*2+1] = lazy[k*2+2] = lazy[k];
    dat[k] = lazy[k];
    lazy[k] = INF;
  }

  void update(int a, int b, int x, int k, int l, int r) {
    eval(k);
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      eval(k);
    } else {
      int mid = (l+r)>>1;
      update(a,b,x,k*2+1,l,mid);
      update(a,b,x,k*2+2,mid,r);
      dat[k] = min(dat[k*2+1],dat[k*2+2]);
    }
  }
  void update(int a, int b, int x) { update(a,b,x,0,0,n); }

  inline T operator[](int a) { return query(a,a+1); }
};

int main(int, char**)
{
  int n, q;
  cin >> n >> q;
  RMQ<int> rmq(n);

  vector<int> ans;
  rep(i,q) {
    int command; cin >> command;
    if (command == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      rmq.update(s,t+1,x);
    } else if (command == 1) {
      int i; cin >> i;
      ans.push_back(rmq[i]);
    }
  }
  for (auto a : ans) cout << a << endl;

  return 0;
}


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

int main() {
	string s;
	cin >> s;
	int w;
	cin >> w;
	rep(i,s.size()){
		cout << s[i];
		i+=w-1;
	}
	cout << endl;
	return 0;
}

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

string s;
int w;
int main() {
	cin >> s >> w;
	for(int i=0; s[i]; i++) {
		if(i%w==0) putchar(s[i]);
	}
	return 0;
}


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

int a[210] = { 0 }, n;

int main() {
	cin >> n;
	int len = 2 * n, ans = 0;
	for (int i = 0; i < len; i++) cin >> a[i];
	sort(a, a + len);
	for (int i = 0; i < len; i += 2) ans += a[i];
	cout << ans << endl;
	return 0;
}

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

using namespace std;

int main()
{
  int n;
  cin>>n;
  vector<int>v;
  int answer=0;
  for(int i = 0 ; i< 2*n ;i++){
    int k;
    cin>>k;
    v.push_back(k);
  }
  sort(v.begin(),v.end());
  for(int i = 0 ; i < 2*n ; i+=2){
    answer+=min(v.at(i),v.at(i+1));
  }
  cout<<answer<<endl;
return 0;
}


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

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

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

void chmaxs(string &s,string ref)
{
  int flg = 0;

  if(s == ref)return;

  if(ref.size() != s.size())
  {
    if(ref.size() > s.size())
    {
      s = ref;
      return;
    }
    else
    {
      return;
    }
  }

  if(ref.size() == s.size());
  {
    for (int i = 0; i < s.size(); i++)
    {
      if(ref[i] != s[i])
      {
        if(ref[i] > s[i])
        {
          s = ref;
          return;
        }
        else
        {
          return;
        }

      }
    }

  }
  return;
}

void solve(void)
{
  vector<int> data = {0,2,5,5,4,5,6,3,7,6};
  int N,M; cin>>N; cin>>M;
  vector<int> vec (M,0);
  for (int i = 0; i < M; i++)
  {
     cin>>vec[i];
  }
  vector<string> DP (N+1,"");//i本のマッチを用いて作ることが可能な整数のうち最大のものを格納する。
  for (int i = 1; i <= N; i++)
  {
    for (int j = 0; j < M; j++)
    {
      int fi = i - data[vec[j]];
      if(fi < 0 )continue;
      if(fi != 0 && DP[fi] == "")continue;
      chmaxs(DP[i],DP[fi] + to_string(vec[j]));
    }

  }
  cout<<DP[N]<<endl;

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

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

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
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; }

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

ll n, m;

struct numinfo {
	int digit;
	int match;
	int min_match;
	int n;
};

vector<numinfo> nums;

bool dfs( int i, int remain_match, int remain_keta ){
	if ( i == SZ(nums) ){
		eprintf( "remain=%d %d   ", remain_match, remain_keta );
		rep(k,SZ(nums)){
			eprintf( "%d ", (int)nums[k].n );
		}
		eprintf( "\n" );
		if ( remain_match == 0 && remain_keta <= 0 )
			return true;
		return false;
	}
	for ( int k = remain_match/nums[i].match; k >= 0; k-- ){
		nums[i].n = k;
		int creatable_keta = (remain_match-k*nums[i].match)/nums[i].min_match;
		if ( remain_keta-k-creatable_keta > 0 )
			continue;
		if ( dfs( i+1, remain_match-k*nums[i].match, remain_keta-k ) )
			return true;
	}
	return false;
}

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

	vector<int> a(m);
	rep(i,m){
		cin >> a[i];
	}
	sort(a.rbegin(),a.rend());

	int digit_to_match[] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
	set<int> s;
	rep(i,m){
		int match = digit_to_match[a[i]];
		if ( s.count(match) == 0 )
			nums.push_back({a[i],match,100,0});
		s.insert(match);
	}
	m = nums.size();

	int min_match = 100;
	for ( int i = nums.size()-1; i >= 0; i-- ){
		nums[i].min_match = min_match;
		chmin(min_match, nums[i].match);
		eprintf( "digit,=%d match=%d min_match=%d\n", nums[i].digit, nums[i].match, nums[i].min_match );
	}

	for ( int keta = n / min_match; keta > 0; keta-- ){
		eprintf( "\n\nketa=%d\n", keta );
		if ( dfs( 0, n, keta ) ){
			sort(nums.begin(), nums.end(), [](numinfo &l, numinfo &r){
				return l.digit>r.digit;
			});
			rep(i,m){
				rep(j,nums[i].n){
					cout << nums[i].digit;
				}
			}
			cout << endl;
			return 0;
		}
	}

	return 0;
}


#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 <string>
#include <algorithm>

using namespace std;

int main(){

	string str1,str2,p;
	char x;
	int q,a,b;

	getline(cin, str1);
	cin >> q;

	for (int i = 0; i < q; i++){

		cin >> str2;
		if (str2 == "replace"){
		//replace?????????
			cin >> a >> b >> p;
			for (int j = 0; j < p.length(); j++)
				str1[j + a] = p[j];
		}
	    else if(str2 == "reverse"){
			//reverse?????????
			cin >> a >> b;
			//a???????????????b???????????????????????????

			for (int j = a; j < ((b - a) / 2)+a+1; j++){

				swap(str1[j],str1[b-(j-a)]);

			}

		}
		 else if(str2=="print"){
		//print?????????
			cin >> a >> b;
			for (int j = a; j < b+1; j++){
				cout << str1[j];
			}cout << endl;
		 }
	}//??????????????????

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    string ans;
    if(n < 1200) ans = "ABC";
    else if(n < 2800) ans = "ARC";
    else ans = "AGC";
    cout << ans << endl;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N = 2e5+5 ;
//😁😁😁😁😁😁😁😁😁😁😁

void Never_give_up()

{
    int k; cin>>k;
     string s; cin>>s;

    if(s.size()<=k) cout<<s;
    else
    {
        for(int i=0;i<k;++i) cout<<s[i];
        cout<<"...";
    }
}

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

Never_give_up();

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define INF 2e9
#define LINF 1e18
#define mod 1000000007
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;

int main() {
    string input; cin >> input;
    vector<int> a(4);
    rep(i,4) a[i] = input[i]-'0';
    rep(i,8) {
        int sum = a[0];
        string s;
        s += (char)('0'+a[0]);
        rep(j,3) {
            if (i & (1<<j)) {
                sum += a[j+1];
                s += '+';
                s += (char)('0'+a[j+1]);
            }
            else {
                sum -= a[j+1];
                s += '-';
                s += (char)('0'+a[j+1]);
            }
        }
        if (sum == 7) {
            cout << s << "=7" << endl;
            return 0;
        }
    }
}

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

vector<int> vec(4);

string judge(int su, int num, string op) {
    if (num == 4) {
        if (su == 7 - vec[0]) {
            return op;
        }
        else return "";
    }
    if (judge(su + vec[num], num + 1, op + "+") != "") {
        return judge(su + vec[num], num + 1, op + "+");
    }
    if (judge(su - vec[num], num + 1, op + "-") != "") {
        return judge(su - vec[num], num + 1, op + "-");
    }
    return "";
}

int main() {
    char A,B,C,D; cin >> A >> B >> C >> D;
    vec[0] = A - '0', vec[1] = B - '0', vec[2] = C - '0', vec[3] = D - '0';
    string op = "";

    string a = judge(0, 1, op);
    cout << vec[0] << a[0] << vec[1] << a[1] << vec[2] << a[2] << vec[3] << "=7" << endl;
}


#include <iostream>
#include <string>
using namespace std;
int main( void )
{
  string s;
  while( 1 ){
    getline( cin, s );
    if( cin.eof() ) break;
    while( 1 ){
      for( int i=0; i<s.size(); i++ ){
        if( s[i] == 'z' ) s[i] = 'a';
        else if( 'a' <= s[i] && s[i] < 'z' ) s[i]++;
      }
      if( s.find( "the",  0 ) != string::npos ){ cout << s << endl; break; }
      if( s.find( "this", 0 ) != string::npos ){ cout << s << endl; break; }
      if( s.find( "that", 0 ) != string::npos ){ cout << s << endl; break; }
    }
  }
  return 0;
}

 #include<iostream>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>

 using namespace std;

 int main()
 {
   string a,b;
   while(getline(cin,a))
   {
     for(int shift=0;shift<26;shift++)
     {
       b="";
       for(int i=0;i<a.length();i++)
       {
         if(a[i]>='a'&&a[i]<='z')
         {
           if(a[i]+shift>'z') b+=(a[i]+(shift-26));
           else b+=(a[i]+shift);
         }
         else b+=a[i];
       }
       if(b.find("the")!=string::npos||b.find("this")!=string::npos||b.find("that")!=string::npos) break;
       else continue;
     }
     cout<<b<<endl;
   }
   //while(1);
   return 0;
 }


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

int main(){

	string s;
	cin>>s;
	unordered_map<char,int>mp;
	unordered_map<char,vector<pair<int,int>>>store;
	int n=s.size();
	int st,ed,flag=0;
	for(int i=0;i<n;i++){
		mp[s[i]]++;
		store[s[i]].push_back({i,mp[s[i]]});
	}
	for(auto x:store){

		if((int)x.second.size()>=2){
			//cout<<x.first<<endl;

			int temp_sz=0;
			int cnt=0;
			int i=0,j=1,sz=(int)x.second.size();
			while(j<sz){
				int temp;
				temp_sz=x.second[j].first-x.second[i].first+1;
				temp=temp_sz/2;
				cnt=x.second[j].second;
				if(i>0){
					cnt-=x.second[i-1].second;
				}
				if(cnt>temp){
					flag=1;
					st=x.second[i].first,ed=x.second[j].first;
				}
				i++;
				j++;
				if(flag){
					break;
				}
			}

		}
		if(flag){
					break;
				}
	}
	if(flag==0){
		cout<<"-1 -1\n";
	}
	else{
		cout<<st+1<<" "<<ed+1<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int64_t al[MAXN][26];

int main()
{
  string S;
  cin >> S;
  for (int i = 1; i < S.size(); i++)
  {
    if (S[i - 1] == S[i])
    {
      cout << i << ' ' << i + 1 << endl;
      return 0;
    }
    if (i != 1 && S[i - 2] == S[i])
    {
      cout << i - 1 << ' ' << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << ' ' << -1 << endl;
}


#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

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

    int now = 1;

    for (int i = 1; i < n; i++) {
        if (i * i > n) {
            break;
        }

        now = i * i;
    }

    cout << now << endl;
    return 0;
}

#include "bits/stdc++.h"

using namespace std;

void solve()
{
    string s;
    cin >> s;
    int k = s.size();
    while (s[k - 1] != 'F')
        k--;
    cout << s.substr(0, k - 1) << endl;
}

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


#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 = 1001001001;
const ll INF_LL = 1001001001001001001LL;

int main(void){
  int a,b; cin >> a >> b;
  int ans = -INF;
  ans = max(ans,a+b);
  ans = max(ans,a-b);
  ans = max(ans,a*b);

  cout << ans << endl;

  return 0;
}

#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<string>
#include<cstring>
#include<time.h>
#include<array>
#include<iomanip>
#include<list>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<bitset>
#include <queue>
#include <map>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

//BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney,chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu, topologicalsort, kyourenketuseibun

#define PRI(s) cout << s << endl
#define PRIY PRI("Yes")
#define PRIN PRI("No")

int ctoi(char c) {
	switch (c)
	{
	case '0': return 0;
	case '1': return 1;
	case '2': return 2;
	case '3': return 3;
	case '4': return 4;
	case '5': return 5;
	case '6': return 6;
	case '7': return 7;
	case '8': return 8;
	case '9': return 9;
	default:
		cout << "ERR_ctoi" << endl;
		return -1;
	}
}
//最大公約数
template<typename T>
constexpr T gcd(T a, T b)
{
	if (a % b == 0)
	{
		return(b);
	}
	else
	{
		return(gcd(b, a % b));
	}
}

//最小公倍数
template<typename T>
constexpr T lcm(T a, T b)
{
	return a * b / gcd(a, b);
}
int main() {
	int a, b;
	cin >> a >> b;
	if (a <= 8 && b <= 8) {
		PRI("Yay!");
	}
	else PRI(":(");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1003;
int n, m, dis[N], par[N];
vector<int> adj[N], ans;
int main(int argc, char const *argv[])
{
	scanf("%d %d\n", &n, &m);
	for (int i = 0; i < m; ++i)
	{
		int u, v;
		scanf("%d %d\n", &u, &v);
		adj[u].push_back(v);
	}
	for (int s = 1; s <= n; ++s)
	{
		queue<int> q;
		for (int i = 1; i <= n; ++i)
		{
			par[i] = dis[i] = -1;
		}
		q.push(s);
		dis[s] = 0;
		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			for (int v: adj[u])
			{
				if (dis[v] == -1)
				{
					par[v] = u;
					dis[v] = 1 + dis[u];
					q.push(v);
				}
				else if (v == s)
				{
					int cur = u;
					vector<int> temp;
					while (cur != -1)
					{
						temp.push_back(cur);
						cur = par[cur];
					}
					if (temp.size() < ans.size() || ans.empty())
					{
						ans.clear();
						for (int node: temp)
							ans.push_back(node);
					}
				}
			}
		}
	}
	if (ans.empty())
		printf("%d\n", -1);
	else
	{
		printf("%d\n", (int)ans.size());
		for (int node: ans)
			printf("%d\n", node);
	}
	return 0;
}

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

int n, m, a[300][300], c[300], ans;
bool removed[300];

int main() {
	cin >> n >> m;
	ans = n;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j) {
			cin >> a[i][j];
			--a[i][j];
		}

	for (int i = 0; i < m; ++i) { //iコ除外
		fill(c, c + m, 0);
		for (int j = 0; j < n; ++j) {
			for (int k = 0; k < m; ++k) {
				if (removed[a[j][k]])
					continue;
				++c[a[j][k]];
				break;
			}
		}

		int idx = 0;
		for (int j = 0; j < m; ++j)
			if (c[j] > c[idx])
				idx = j;
		ans = min(ans, c[idx]);
		removed[idx] = 1;
	}
	cout << ans << endl;
}


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

int main() {
  int A, B, M; cin >> A >> B >> M;
  vector<int> a(A);
  vector<int> b(B);
  int minA = 100001;
  int minB = 100001;

  rep(i,A){
    cin >> a[i];
    minA = min(minA,a[i]);
  }
  rep(i,B){
    cin >> b[i];
    minB = min(minB,b[i]);
  }

  int ans = minA + minB;
  rep(i,M){
    int x,y,c; cin >> x >> y >> c;
    x--;
    y--;
    ans = min(ans, a[x] + b[y] - c);
  }
  cout << ans << endl;

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pi> vpi;
typedef vector<vi> vii;
typedef map<int, int> mp;
typedef multiset<int> mset;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;
typedef vector<int64> vi64;
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define form(i,j,k,l) for(int i=j;i<int(k);i+=l)
#define rform(i,j,k,l) for(int i=int(j);i>int(l);i-=l)
#define rep(i, j) for(int i=0;i<int(j);i++)
#define rrep(i, j) for(int i=int(j);i>=0;i--)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define forc(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define INF (int)1e9
#define INF64 (int64)1e18
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define F first
#define S  second
const double pie = acos(-1.0);
template <typename T>
T Max(T x, T y)
{
  return (x > y) ? x : y;
}
template <typename T>
T Min(T x, T y)
{
  return (x > y) ? y : x;
}
int gcd(int n1, int n2)
{
  if (n2 != 0) return gcd(n2, n1 % n2);
  else return n1;
}
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
clock_t time_p = clock();
void rtime() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)1000 * (time_p) / CLOCKS_PER_SEC << "\n";}
int64 binpow(int64 a, int64 b, int64 m) {
  a %= m;
  int64 res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int main()
{ fast;
  int n;
  long long int a, b;
  cin >> n >> a >> b;
  long long int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  long long int ans = 0;
  form(i, 1, n, 1)
  {

    ans = ans + Min((arr[i] - arr[i - 1]) * a, b);
  }
  cout << ans;

}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>

using namespace std;

const int INF = (1<<30) - 1;

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int e;
    while(cin>>e, e){
        int ans = INF;
        for(int z=0; z*z*z<=e; z++){
            int sum = z*z*z;
            for(int y=0; y*y+sum<=e; y++){
                int x = e-y*y-sum;
                ans = min(ans, x+y+z);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T In(){ T x; cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

int main()
{
  while (int e = in()){
    int m = 1e8;
    for (int z = 0; z <= 100; ++z){
      for (int y = 0; y <= 1000; ++y){
        int x = e - (sq(z) * z + sq(y));
        if (x < 0) continue;
        m = min(m, z + y + x);
      }
    }
    printf("%d\n", m);
  }
  return (0);
}


#include <bits/stdc++.h>

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

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

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

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

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

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

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

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

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

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

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

const int N = 100005;

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

void solve() {
	read(n), read(m), read(q);
	for (int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	for (int i = 1; i <= m; ++i) {
		read(b[i]);
	}
	int ans = *std::min_element(a + 1, a + 1 + n) + *std::min_element(b + 1, b + 1 + m);
	for (int i = 1; i <= q; ++i) {
		int x, y, c;
		read(x), read(y), read(c);
		ans = std::min(ans, a[x] + b[y] - c);
	}
	print(ans);
}

int main() {
	int T = 1;
	// read(T);
	while (T--) {
		solve();
	}
}

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

int main(){
  //入力
  int A,B,M; cin>>A>>B>>M;
  vector<int> a(A); for(int i=0;i<A;i++) cin>>a.at(i);
  vector<int> b(B); for(int i=0;i<B;i++) cin>>b.at(i);

  //まずは(冷蔵庫の最安値)+(レンジの最安値を求める)
  int mina=100000,minb=100000;//まずはどちらも絶対にどのai,biよりも高くなるように設定する
  for(int i=0;i<A;i++) mina=min(mina,a.at(i));//競プロでよく使われる手段、最小値更新
  for(int i=0;i<B;i++) minb=min(minb,b.at(i));

  int ans=mina+minb;//割引券なしの最小値ペアが求まった

  //割引券込みのを求める
  for(int i=0;i<M;i++){
    int x,y,c; cin>>x>>y>>c; x--,y--;//入力、添え字調整
    int sub_ans=a.at(x)+b.at(y)-c;//i番目の割引込みの値段
    ans=min(ans,sub_ans);//最小値更新
  }

  cout<<ans<<endl;

  /*
  補足
  for文は書きにくいのでrepに差し替えてもいい
  最大値更新、最小値更新はある程度慣れたらchmin,chmaxを使った方がやりやすい、
  chmax,chminについては↓の2を参照
  https://qiita.com/drken/items/dc53c683d6de8aeacf5a
  */

  return 0;
}


#include <bits/stdc++.h>
#define ri register
#define int long long
#define E (n+1)
using namespace std; const int N=200010;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int sx,sy,tx,ty;
signed main()
{
    sx=read(), sy=read(), tx=read(), ty=read();
    for(ri int i=1;i<=tx-sx;i++) putchar('R');
    for(ri int i=1;i<=ty-sy;i++) putchar('U');
    for(ri int i=1;i<=tx-sx;i++) putchar('L');
    for(ri int i=1;i<=ty-sy;i++) putchar('D');
    putchar('D');
    for(ri int i=1;i<=tx-sx+1;i++) putchar('R');
    for(ri int i=1;i<=ty-sy+1;i++) putchar('U');
    putchar('L');
    putchar('U');
    for(ri int i=1;i<=tx-sx+1;i++) putchar('L');
    for(ri int i=1;i<=ty-sy+1;i++) putchar('D');
    putchar('R');
    puts("");
    return 0;
}

#include <iostream>
#include <string>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  int ans = 0;
  for (int i = a; i <= b; i++) {
    string s = to_string(i);
    if (s[0] == s[4] && s[1] == s[3]) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod =1e9+7;
/*"itob" int to "N"base */           template<typename TypeInt> string itob(const TypeInt v, int base){static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";string ret;static numeric_limits<TypeInt> t;TypeInt n = v;if (t.is_signed) {if (v < 0) n *= -1;}while (n >= base) {ret += table[n%base];n /= base;}ret += table[n];if (t.is_signed) {if (v < 0 && base == 10) ret += '-';}reverse(ret.begin(), ret.end());return ret;}
/*"chmin" a = MAX*/                  template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
/*"chmax" a = MIN*/                  template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/*"ctoi"  char to int*/              int ctoi(char c) {return c-'0';}
/*"gcd"   MAX Euclidean */           ll gcd(ll a,ll b){if(b==0)return a; return gcd(b,a%b);}
    /*"lcm"   MIN*/                      ll lcm(ll a,ll b){ll g = gcd(a,b);return a/g*b;}
/*"primecheck"If prime,return true.*/bool primecheck(ll n){if(n < 2) return false;else{for(int i = 2; i * i <= n; i++){if(n % i == 0) return false;}return true;}}
string  reverserange(string s,ll a,ll b){reverse(s.begin()+a-1,s.begin()+b); return s;}
ll modpow(ll a,ll n, ll mod){ll res = 1;while(n>0){if (n%2==1){res = res * a % mod;}a = a * a % mod;n/=2;}return res;}
int main() {
    int a,b;
    cin >> a>>b;
    cout<<a*b<<endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
    int a, b;
    cin>>a>>b;
    int ans=a*b;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <iostream>
#include<math.h>
using namespace std;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int main()
{
    int N;
    cin >> N;
    map<int64_t, int>d;
    for (int i=0; i<N; i++) {
        int64_t n;
        cin >> n;
        if (d.count(n) > 0) {
            d[n] = d[n] + 1;
        } else {
            d[n] = 1;
        }
    }
    int64_t sum = 0;
    for (auto c: d) {
        if (c.second % 2 != 0) {
            sum += 1;
        }
    }

    cout << sum << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
#define ff first
#define ss second
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#define endl "\n"
#include "string"

int maxSubSetXor(int *a,int n) {
	int ans = 0;
	int id = 0;
	for(int i = 61; i >= 0; i--) {
		int maxid  = -1, maxi = -1;
		for(int j = id; j < n; j++) {
			if( a[j] & (1ll << i) && a[j] > maxi) {
				maxi = a[j];
				maxid = j;
			}
		}
		if(maxid == -1)
			continue;
		swap(a[maxid] , a[id]);
		for(int j = 0; j < n; j++) {
			if(j != id && a[j] & (1ll << i) )
				a[j] ^= maxi;
		}
		id++;
	}

	for(int i = 0; i < n; i++)
		ans ^= a[i];
	return ans;
}

int32_t main() {
	int n;
	cin >> n;
	int a[n] , xr = 0;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
		xr ^= a[i];
	}
	for(int i = 0; i < n; i++)
		a[i] &= ~xr;
	int ans  = maxSubSetXor(a, n);
	ans = 2 * ans + xr;
	cout << ans << endl;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include <cmath>
#include<algorithm>
#include<stack>
#include<map>
#include<ctime>
#define rep(i,n) for (int i=0;i<n;i++)
//#include <bits/stdc++.h>
using namespace std;

int main(){
    int H,W,N,ans=0;
    cin>>H>>W>>N;
    if (H>W) cout<<(N-1)/H+1<<endl;
    else cout<<(N-1)/W+1<<endl;

    return 0;
}

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

bool f(int a, int b, int c) {
  return ( a == b && a != c ?
          true : false);
}

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  cout << (f(a, b, c) || f(c, a, b) || f(b, c, a) ?
    "Yes" : "No");
}


#include<bits/stdc++.h>

using namespace std;
string t;
string ans;

void dfs(int i,string s,int n);
void solve();

int main(void){
    cin >> t;
    solve();
    return 0;
}

void dfs(int i, string s, int n){
      if(i==3){
            if(n==7){
                  ans=s+"=7";
            }
            return ;
      }
      dfs(i+1,s + "+" + t[i + 1],n + (t[i + 1] - '0'));
      dfs(i+1,s + "-" + t[i + 1],n - (t[i + 1] - '0'));

}

void solve(){
    dfs(0,t.substr(0,1),t[0]-'0');
    cout << ans << endl;
}

#include <iostream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <functional> // std::function<void(int)>
using namespace std;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[4] = {-1,0,1,0};
int dx[4] = {0,1,0,-1};
int Dy[8] = {-1,-1,0,1,1,1,0,-1};
int Dx[8] = {0,1,1,1,0,-1,-1,-1};
const int mod = 1000000007;
const int inf = mod*mod;
const int d5 = 111111;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a,b;
    cin>>a;
    cout<<180*(a-2)<<endl;
}


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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define Eunique(v) v.erase(unique(all(v)),v.end())
#define Eback(s) s.erase(s.end()-1,s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v),0LL)
#define mapmax(v) v.rbegin()->first
#define mapmin(v) v.begin()->first
#define pb push_back
#define pf push_front
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK cerr<<"OK\n"
#define OK1 cerr<<"OK1\n"
#define OK2 cerr<<"OK2\n"
#define SIZE(s) (int)s.size()
#define INF ((1LL<<62)-(1LL<<31))
#define zero(x,n) setw(x) << setfill('0') << n

typedef long long ll;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<pair<ll,ll>> vpll;

const double pi = acos(-1.0);
const int mod=1000000007;
const int mod2=998244353;

template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>&p) {
	ost << "{" << p.first << ", " << p.second << "} ";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost, const vector<T>&v) {
	ost << "{";
	for (int i = 0; i<(int)v.size(); i++) {
		if (i)ost << " ";
		ost << v[i];
	}
	ost << "} \n";
	return ost;
}

template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>&v) {
	ost << "{";
	for (auto p:v) {
		ost << "{" << p.first << ", " << p.second << "} ";
	}
	ost << "} ";
	return ost;
}

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;}

void YES(bool b) {cout << ((b)?"YES\n":"NO\n");}
void Yes(bool b) {cout << ((b)?"Yes\n":"No\n");}
void yes(bool b) {cout << ((b)?"yes\n":"no\n");}
void Yay(bool b) {cout << ((b)?"Yay!\n":":(\n");}

ll powmod(ll a,ll b){ll c=1; while(b>0){ if(b&1){ c=a*c%mod; } a=a*a%mod; b>>=1; } return c;}
ll gcd(ll x, ll y) {return __gcd(x,y);}
ll lcm(ll x, ll y) {return x/__gcd(x,y)*y;}

struct UnionFind {
	vector<long long> par;
	UnionFind(long long size):par(size+1,-1){}
	bool unite(long long x,long long y){	//集合の連結
		x=root(x);
        y=root(y);
		if (x!=y){
			if(par[y]<par[x]) swap(x,y);
			par[x]+=par[y];
            par[y]=x;
		}
		return x!=y;
	}
	bool same(long long x,long long y){	//同集合内かの判定
		return root(x)==root(y);
	}
	int root(long long x){	//根を求める
		return par[x]<0?x:par[x]=root(par[x]);
	}
	int size(long long x){	//集合の大きさ
		return -par[root(x)];
	}
};

int main()
{
	ll n,k,l,a,b;
	cin >> n >> k >> l;
	vll ar(n),br(n);
	UnionFind x(n),y(n);
	rep(i,k){
		cin >> a >> b;
		a--;
		b--;
		x.unite(a,b);
	}
	rep(i,l){
		cin >> a >> b;
		a--;
		b--;
		y.unite(a,b);
	}
	rep(i,n){
		ar[i]=x.root(i);
		br[i]=y.root(i);
	}
	map<pll,ll> m;
	rep(i,n) m[m_p(ar[i],br[i])]++;
	rep(i,n) cout << m[m_p(ar[i],br[i])] << (i==n-1?"\n":" ");
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    ll n, m; cin >> n >> m;
    vector<ll> p(n, 0);
    ll xmin = 1, xmax = n;
    for(ll i=0; i<m; ++i){
        ll l, r; cin >> l >> r;
        xmin = max(xmin, l);
        xmax = min(xmax, r);
    }
    ll res = max((ll)0, xmax-xmin+1);
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000009;
const double pi = acos(-1);

int dist_sum(int N) {
    int ans = 0;
    while(N > 0) {
        ans += N%10;
        N /= 10;
    }
    return ans;
}

int main() {
    int N; cin >> N;
    cout << (N%dist_sum(N) == 0 ? "Yes" : "No") << endl;
}

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

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


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++i)
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; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
#define fs first
#define sc second

const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}

// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    ll a,b,m;
    cin >> a >> b >> m;
    vector<ll> A(a);
    vector<ll> B(b);
    cins(all(A));
    cins(all(B));
    ll ma = *min_element(all(A));
    ll mb = *min_element(all(B));
    ll ans = ma + mb;
    rep(i, m){
        ll x,y,c;
        cin >> x >> y >> c;
        --x, --y;
        chmin(ans, A[x] + B[y] - c);
    }
    print(ans);

}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define poly vector<ll>
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
inline ll read(){
	ll x=0;char ch=getchar();bool d=1;
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') d=0;
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return d?x:-x;
}
inline void write(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>=10) write(x/10);
	putchar('0'+x%10);
}
inline void writeln(ll x){write(x);puts("");}
inline void writep(ll x){write(x);putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=20,M=(1<<15)+5;
ll f[M][N],g[M];
vector<pii> v[N];
void upd(ll &x,ll y){
	x=max(x,y);
}
int main(){
	int n=read(),m=read();ll tot=0;
	For(i,1,m){
		int x=read(),y=read(),z=read();tot+=z;
		v[x].pb(mp(y,z));v[y].pb(mp(x,z));
	}
	int st=(1<<n)-1;
	For(s,0,st){
		For(i,1,n) if(!(s>>(i-1)&1)){
			if(!g[s|(1<<(i-1))]){
				g[s|(1<<(i-1))]=g[s];
				for(auto x:v[i]){
					if(s>>(x.fi-1)&1){
						g[s|(1<<(i-1))]+=x.se;
					}
				}
			}
		}
	}
	memset(f,-1,sizeof(f));
	f[1][1]=0;
	For(s,1,st){
		For(i,1,n) if(s>>(i-1)&1){
			if(f[s][i]==-1) continue;
			for(auto x:v[i]){
				if(!(s>>(x.fi-1)&1)){
					upd(f[s|(1<<(x.fi-1))][x.fi],f[s][i]+x.se);
				}
			}
			int ss=(st^s)|(1<<(i-1));
			for(int t=ss;t;t=(t-1)&ss){
				if(t>>(i-1)&1){
					upd(f[s|t][i],f[s][i]+g[t]);
				}
			}
		}
	}
	cout<<tot-f[st][n];
}


//
//  main.cpp
//  usaco
//
//  Created by 김현수 on 2020/08/30.
//  Copyright © 2020 김현수. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <string>
int main()
{
    int a, b, c, d, e, k;
    scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &k);
    if(e-a<=k)
    {
        printf("Yay!");
    }
    else if(e-a>k)
    {
        printf(":(");
    }
}

#include <bits/stdc++.h>
using namespace std;
#define endl '\n';

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << c  << " " << a << " " << b << endl;
}


#include<iostream>
using namespace std;

 int InsertionSort(int* a, int n, int g)
{
	int tmp, j, count = 0;
	for (int i = g; i < n; i++)
	{
		tmp = a[i];
		for (j = i - g; j >= 0 && a[j] > tmp; j -= g)
		{
			a[j + g] = a[j];
			count++;
		}
		a[j + g] = tmp;
	}
	return count;
}

int ShellSort(int* a, int n)
{
	int m, g[100],count = 0;
	for (m = 1; m * m * m <= n; m++)
		continue;
	m--;
	cout << m << endl;
	for (int i = 0; i < m; i++)
	{
	    g[i] = (m - i) * (m - i);
		if (i)
			cout << " ";
		cout << g[i];
	}
	for (int i = 0; i < m; i++)
		count += InsertionSort(a, n, g[i]);

	return count;
}

int main()
{
	int n, a[1000000], count;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	count = ShellSort(a, n);
	cout << endl << count << endl;
	for (int i = 0; i < n; i++)
		cout << a[i] << endl;

	return 0;
}

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

using namespace std;

namespace util {
    template<typename T >
    void print_vector(vector<T> &v) {
        for (auto it = v.begin(); it != v.end(); it++) {
            cout << *it;
            if (it + 1 != v.end())
                cout << " ";
        }
        cout << endl;
    }
}

namespace {
    void insertion_sort(vector<int> &a, int g, int &count) {
        for (int i = g; i < a.size(); i++) {
            int v = a[i];
            int j = i - g;
            while (j >= 0 && a[j] > v) {
                a[j+g] = a[j];
                j = j - g;
                count++;
            }
            a[j+g] = v;
        }
    }

    vector<int> shell_sort(vector<int> &a, int &count) {
        vector<int> gs;
        int g = 1;
        while (a.size() >= g) {
            gs.push_back(g);
            g = 3 * g + 1;
        }

        reverse(gs.begin(), gs.end());

        for (int _g : gs) {
            insertion_sort(a, _g, count);
        }

        return gs;
    }
}

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

    vector<int> v;
    for (int i = 0; i < n; i++) {
        int in;
        cin >> in;
        v.push_back(in);
    }

    int count = 0;

    vector<int> gs = shell_sort(v, count);

    cout << gs.size() << endl;
    util::print_vector(gs);
    cout << count << endl;
    for(int i : v) {
        cout << i << endl;
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000000LL;
#define all(x) (x).begin(),(x).end()

vector<pll> prime_factorize(ll N) {
    vector<pll> res;
    for (ll a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        ll ex = 0;
        while (N % a == 0) {
            ++ex;
            N /= a;
        }
        res.push_back({a, ex});
    }
    if (N != 1) res.push_back({N, 1});
    return res;
}
const int MAX = 210000;

long long 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;
    }
}
long long COM(int n, int k){
    if(n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

int main() {
  ll n, m;
  COMinit();
  cin >> n >> m;
  vector<pll> vc = prime_factorize(m);
  ll ans = 1;
  for(int i=0; i<vc.size(); i++){
    ll num = COM(vc[i].second + n - 1, n-1);
    //cout << "num:" << num << " vc[i]:" << vc[i].first << " " << vc[i].second << ln;
    ans = (ans * num)% MOD;
  }
  cout << ans % MOD << ln;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define all(x) (x).begin(),(x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
constexpr auto INF = INT_MAX/2;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

ll fac[120020], invfac[120020];

ll lpow(ll p, ll q) {
	ll ans = 1;
	ll pp = p;
	while (q) {
		if (q % 2) {
			ans *= pp;
			ans %= MOD;
		}
		q /= 2;
		pp *= pp;
		pp %= MOD;
	}
	return ans;
}

ll com(ll n, ll r) {
	if (n < r || n < 0 || r < 0)return 0;
	ll ans = 1;
	ll k = n - r;

	ans *= fac[n] * invfac[r];
	ans %= MOD;
	ans *= invfac[k];
	ans %= MOD;

	return ans;
}

void precom(int n) {
	rep(i, n + 1) {
		if (i == 0)fac[i] = 1;
		else fac[i] = (fac[i - 1] * i) % MOD;
		invfac[i] = lpow(fac[i], MOD - 2);
	}
	return;
}

int s[100020];
vi prime(0);

int main() {

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

	if (m == 1 || n == 1) { cout << 1 << endl;return 0;
	}
	rep(i, 100020)s[i] = 1;
	rep1(i, 100000) {
		if (i < 2)continue;
		if (s[i] != 1)continue;

		prime.push_back(i);
		s[i] = i;
		int ii = i;
		while (ii <= 100000) {
			if (s[ii] == 1)s[ii] = i;
			ii += i;
		}
	}
	precom(120000);

	vi mp(0);

	int mm = m;
	int k = 0;
	while (mm>1&&k<prime.size()) {
		if (mm % prime[k])k++;
		else { mm /= prime[k];mp.push_back(prime[k]); }
	}
	if(mm>1)mp.push_back(mm);

	k = 1;
	ll ans = 1;
	rep(i, mp.size() - 1) {
		if (mp[i + 1] == mp[i])k++;
		else {
			ans *= com(n - 1 + k, k);
			ans %= MOD;
			k = 1;
		}
	}
	ans *= com(n - 1 + k, k);
	ans %= MOD;
	cout << ans << endl;

}


//
//  main.c
//  ITP1_6_A
//
//  Created by ??±??°?????? on 2015/10/11.
//  Copyright ?? 2015??´ HamadaShogo. All rights reserved.
//

#include <stdio.h>

void reverseNum(int *num, int size);

int main(int argc, const char * argv[]) {
    int size;
    int num[100];
    int i;

    scanf("%d", &size);
    for (i=0; i<size; i++) {
        scanf("%d", &num[i]);
    }
    reverseNum(num, size);

    return 0;
}

void reverseNum(int *num, int size){
    int i;

    for (i=0; i<size; i++) {
        printf("%d", num[size-i-1]);
        if (!(i == size-1)) {
            printf(" ");
        }
    }
    puts("");
}

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

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (auto &x : a) cin >> x;
	reverse(a.begin(), a.end());
	for (int i = 0; i < n; i++) cout << a[i] << (i == n - 1 ? '\n' : ' ');
}


#include<bits/stdc++.h>
#define rep(i,f,n) for(ll i=(f); (i) < (n); i++)
#define repe(i,f,n) for(ll i=(f); (i) <= (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b){if (a < b) { a = b; return true; } return false;}
template<class T> inline bool chmin(T& a, T b){if (a > b) { a = b; return true; } return false;}

ll INF = 1e+18;
int iINF = 1e9;
int MAXI = 1e8 + 1;
using vec = vector<int>;

int
main()
{
  int N; cin >> N;
  vec arr(N + 1);
  repe(i, 1, N) cin >> arr[i];
  vec ball(N + 1, 0);
  int num = 0;

  for(int i = N; i >= 1; --i){
    int val = i;
    int cnt = 0;
    while(true){
      val += i;
      if(val > N) break;
      if(ball[val] == 1) ++cnt;
    }
    if(arr[i] != (cnt % 2)) ball[i] = 1, ++num;
  }
  cout << num << endl;
  rep(i, 1, N + 1){
    if(ball[i] == 1) cout << i << " ";
  }
}

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

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

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 1000;
const int N = 100;
int n;
int c[N][N];
int d[N];
bool used[N];

int prim() {
  int i, res = 0;
  for (i=0; i<n; i++) {
    d[i] = INF;
    used[i] = false;
  }

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

    if (v==-1) break;
    used[v] = true;
    res += d[v];
    for (i=0; i<n; i++)
      d[i] = min(d[i], c[v][i]);
  }

  return res;
}

int main() {
  int i, j;
  scanf("%d ", &n);
  for (i=0; i<n; i++)
    for (j=0; j<n; j++) {
      scanf("%d ", &(c[i][j]));
      if (c[i][j]==-1) c[i][j] = INF;
    }
  printf("%d\n", prim());
  return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<iomanip>
#define pi 3.14159265358979323846
#define ll long long
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
int main(){
  int n, sm = 0;
  cin >> n;
  if(n >= 500){
    sm += n / 500 * 1000;
    n = n % 500;
  }
  if(n >= 5){
    sm += n / 5 * 5;
  }

  cout << sm << "\n";
}

#include <iostream>
int main()
{
  int d;
  while (std::cin >> d) {
    int n = 0, s = 0;
    while (n < 600) {
      s += n * n * d;
      n += d;
    }
    std::cout << s << std::endl;
  }
  return 0;
}


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

int main(){
	int N, M;
	cin >> N;
	string s[N];
	rep(i,N) cin >> s[i];
	cin >> M;
	string t[M];
	rep(i,M) cin >> t[i];
	sort(s,s+N);
	int ans = 0;
	int u = 1;
	string c = s[0];
	for (int i=1; i<N; i++){
		if (s[i] == c) u++;
		else {
			int d = 0;
			rep(j,M) if (t[j] == c) d++;
			ans = max(ans, u-d);
			u = 1;
			c = s[i];
		}
		if (i == N-1) {
			int d = 0;
			rep(j,M) if (t[j] == c) d++;
			ans = max(ans, u-d);
		}
	}
	cout << ans << endl;

	return 0;
}

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

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

  string s;
  map<string, int> S;
  for (int i=0; i<N; i++) {
    cin >> s;
    if (S.count(s)) S.at(s) += 1;
    else S[s] = 1;
  }

  int M;
  cin >> M;
  for (int i=0; i<M; i++) {
    cin >> s;
    if (S.count(s)) S.at(s) -= 1;
  }

  using pis = pair<int, string>;
  priority_queue<pis> cnt;
  for (auto x : S) {  // 要素はキーと値のpair
    cnt.push(pair(x.second, x.first));
  }

  if (cnt.top().first < 0) cout << 0 << endl;
  else cout << cnt.top().first << endl;

  return 0;
}


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

#define int  long long
#define rep(i,begin,end) for(__typeof(end)i=(begin)-((begin)>(end));i!=(end)-((begin)>(end));i+=1-2*((begin)>(end)))
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mod2 998244353
#define what_is(x) cerr << #x << " is " << x <<"\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a) for(auto x:a) cout<<x<<" ";
#define INF 1000000000000

// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

using ll  = long long;
using vi  = vector<int>;
using vvi = vector<vi>;
using pii = pair<int,int>;
using vii = vector<pii>;
using mi  = multiset<int>;
using mii = multiset<pii>;

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

void solve()
{
    int n,m,t,i,l,k,r,u,v,w,j,s,sum=0,pos,flag=0,count=0;
    string S,T,U;
    // int x,y,a,b,c;
    cin>>n>>m>>k;
  set<int> a;
 a.insert(n);
  a.insert(m);
  a.insert(k);
  if(sz(a)==2) cout<<"Yes";
  else cout<<"No";
}

signed main()
{

    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    // cin>>t;
    int T=0;
    while(t--)
    {
        T++;
        // if(T>1) cout<<"\n";
        // cout<<"Case #"<<T<<": ";
        solve();
        if(t)cout<<"\n";
    }
    return 0;
}

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

int main() {
	int a;
  int b;
  int c;
  bool poor = false;
  cin >> a;
  cin >> b;
  cin >> c;
  if (a == b == c) {
    poor = false;
  } else {
    if (a == b && a != c) {
    poor = true;
  } else {
    if (a == c && a != b) {
    poor = true;
  } else {
    if (b == c && b != a) {
    poor = true;
  } else {
    if (b == a && b != c) {
    poor = true;
  } else {
    if (c == a && c != b) {
    poor = true;
  } else {
    if (c == b && c != a) {
    poor = true;
  } else {
    poor = false;
  }
  }
  }
  }
  }
  }
  }

  if (poor == true) {
    cout << "Yes";
  } else {
    cout << "No";
  }
	return 0;
}


/**
*    created: 28.06.2020 02:21:50
**/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define int 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++)
#define all(x) (x).begin(),(x).end()
#define pcnt(bit) __builtin_popcountll(bit)
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;}
const int mod = 1000000007;
const long double pi = acos(-1.0);
const int inf = 1LL << 60;

signed main() {

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

    int a, b;
    cin >> a >> b;
    int mx = max(a,b);
    if (mx*2 <= 16) cout << "Yay!" << endl;
    else cout << ":(" << endl;
    return 0;
}


#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
ll mod=998244353LL;
long long modpow(long long a, long long n, long long mod=(1LL<<62)){
	long long res = 1;
	while (n > 0){
		if (n & 1)
		res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}
const int MAX = 510000;
const ll MOD = 998244353LL;

long long 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;
	}
}

long long COM(int n, int k){ // 二項係数:nCk計算
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main(){
	int n,m,k;
	COMinit();
	ll ans=0LL;
	cin >>n>>m>>k;
	for(int i=0;i<=k;i++){
		ans+=m*modpow(m-1,n-1-i,mod)%mod*COM(n-1,i)%mod;
		ans%mod;
	//	clog<<ans<<endl;
	}
	cout<<ans%mod<<endl;
}

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

struct Combination{
    vector<ll> fac,finv,inv;
    Combination(int mx){init(mx);}
    void init(int mx){
        fac.resize(mx+1);
        finv.resize(mx+1);
        inv.resize(mx+1);
        fac[0]=fac[1]=1;
        finv[0]=finv[1]=1;
        inv[1]=1;
        for(int i=2;i<=mx;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 calc(int n,int k){
        return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
    }
};

ll modpow(ll x,ll n){
    ll res=1;
    while(n>0){
        if(n&1)res=res*x%MOD;
        x=x*x%MOD;
        n>>=1;
    }
    return res;
}

int main(){
    int n,a,b,c,d;
    cin>>n>>a>>b>>c>>d;
    ll dp[n+1]={};
    dp[0]=1;
    Combination comb(n);
    for(int j=a;j<=b;j++){
        for(int i=n;i>=1;i--){
            for(int k=c;k<=i/j&&k<=d;k++){
                ll ce=comb.calc(n-(i-k*j),k*j)*comb.fac[k*j];
                ce%=MOD;
                ce*=comb.finv[k];
                ce%=MOD;
                ce*=modpow(comb.finv[j],k);
                ce%=MOD;
                dp[i]+=dp[i-k*j]*ce;
                dp[i]%=MOD;
            }
        }
    }
    cout<<dp[n]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x=0;char ch=gc();bool positive=1;
	for(;!isdigit(ch);ch=gc()) if(ch=='-') positive=0;
	for(;isdigit(ch);ch=gc()) x=x*10+ch-'0';
	return positive?x:-x;
}
inline void write(ll x){
	if(x<0){
		x=-x;putchar('-');
	}
	if(x>=10) write(x/10);
	putchar('0'+x%10);
}
inline void writeln(ll x){write(x);puts("");}
inline void writep(ll x){write(x);putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=1e5+5;
int a[N],nxt[65][N];
ll id[N];
int main(){
	int n=read();
	For(i,1,n) a[i]=read(),id[i]=i;
	int m=read();ll k=read();
	For(i,1,m){
		int x=read();
		swap(id[x],id[x+1]);
	}
	For(i,1,n) nxt[0][i]=id[i];
	For(i,1,63){
		For(j,1,n) nxt[i][j]=nxt[i-1][nxt[i-1][j]];
	}
	For(i,1,n){
		id[i]=i;
		Rep(j,63,0) if(k>>j&1) id[i]=nxt[j][id[i]];
		id[i]=a[id[i]]-a[id[i]-1];
	}
	For(i,1,n){
		id[i]+=id[i-1];
		printf("%lld.0\n",id[i]);
	}
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef vector<int> Vi;
typedef pair<int,int> Pii;
typedef pair<int,Pii> Pip;
const int INF = (1<<30);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){

  int n;
  int p[100] = {0};
  int rank[100] = {0};
  cin >> n;
  for(int i=0; i<n * (n - 1) / 2; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--; b--;
    if( c > d ) p[a] += 3;
    if( c < d ) p[b] += 3;
    if( c == d ) { p[a]++; p[b]++; }
  }

  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      if( i != j ) {
	if( p[i] < p[j] ) rank[i]++;
      }
    }
  }

  for(int i=0; i<n; i++) {
    cout << rank[i]+1 << endl;
  }
}


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

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

    int n;
    cin >> n;
    long long ans = 0;
    for(int i = 1; i <= n; i++) {
        if(i % 3 != 0 && i % 5 != 0)
            ans += i;
    }
    cout << ans << "\n";
    return 0;
}

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

int main()
{
    long long N;
    cin >> N;
    long long c = N*(N+1)/2;
    long long j = N / 3;
    long long k = N / 5;
    long long l = N / 15;
    c = c - 3*j*(j+1)/2 - 5*k*(k+1)/2 + 15*l*(l+1)/2;
    cout << c;
}


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

   if(A<9&&B<9){
       cout<<"Yay!"<<endl;
   }
   else{
       cout<<":("<<endl;
   }

}

//#include "stdafx.h"
#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int a,b,ans,q;
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	cin>>a>>b;
	q=abs(a-b);
	ans=16-(a+b);
	if(q>ans/2)
		cout<<":(\n";
	else
		cout<<"Yay!\n";

	return 0;
}


#include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <queue>
#include <climits>
using namespace std;
typedef pair<int,int> p;
int d[100003];
vector<p>edge[100002];
int main()
{
    priority_queue<p,vector<p>,greater<p> > que;
    int v,e,r;
    int s,t,d0;
    cin>>v>>e>>r;
    for(int i=0;i<100002;i++){d[i]=INT_MAX;}
    for(int i=0;i<e;i++){
        scanf("%d%d%d",&s,&t,&d0);
        edge[s].push_back(p(t,d0));}
    que.push(p(0,r));
    d[r]=0;
    while(!que.empty()){
        p cur=que.top();
        que.pop();
        if(d[cur.second]<cur.first)continue;
        for(int i=0;i<edge[cur.second].size();i++){
            p to=edge[cur.second][i];
            if(d[to.first]>=d[cur.second]+to.second){
                d[to.first]=d[cur.second]+to.second;
                que.push(p(d[to.first],to.first));}
        }
    }
    for(int i=0;i<v;i++){
        if(d[i]==INT_MAX)cout<<"INF"<<endl;
        else
            cout<<d[i]<<endl; }
    return 0;
}

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
using namespace std;
struct graph{
	int h[360010],nex[27000010],to[27000010],col[360010],M;
	bool v[360010];
	graph(){
		M=0;
		memset(h,0,sizeof(h));
		memset(v,0,sizeof(v));
		memset(col,0,sizeof(col));
	}
	void ins(int a,int b){
		M++;
		to[M]=b;
		nex[M]=h[a];
		h[a]=M;
	}
	void add(int a,int b){
		ins(a,b);
		ins(b,a);
	}
	void dfs(int x,int c){
		col[x]=c;
		v[x]=1;
		int i,j;
		for(i=h[x];i;i=nex[i]){
			if(!v[to[i]]){
				v[to[i]]=1;
				for(j=h[to[i]];j;j=nex[j]){
					if(!v[to[j]])dfs(to[j],c);
				}
			}
		}
	}
}g[2];
int n;
int p(int i,int j){return i*2*n+j+1;}
bool sq(int x){
	double d=sqrt(x);
	return fabs(d-round(d))<1e-6;
}
void build(int f,int x,int y){
	if(y>=2*n)return;
	int i,j;
	for(i=0;i+x<2*n;i++){
		for(j=0;j+y<2*n;j++)g[f].add(p(i,j),p(i+x,j+y));
	}
	for(i=0;i+x<2*n;i++){
		for(j=2*n-1;j-y>=0;j--)g[f].add(p(i,j),p(i+x,j-y));
	}
}
struct pr{
	int x,y;
	pr(int a=0,int b=0){x=a;y=b;}
};
vector<pr>v[4];
int main(){
	int i,j,d1,d2;
	scanf("%d%d%d",&n,&d1,&d2);
	for(i=0;i<2*n&&i*i<=d1;i++){
		if(sq(d1-i*i))build(0,i,round(sqrt(d1-i*i)));
	}
	for(i=0;i<2*n&&i*i<=d2;i++){
		if(sq(d2-i*i))build(1,i,round(sqrt(d2-i*i)));
	}
	for(i=1;i<=4*n*n;i++){
		if(!g[0].v[i])g[0].dfs(i,1);
		if(!g[1].v[i])g[1].dfs(i,2);
	}
	for(i=0;i<2*n;i++){
		for(j=0;j<2*n;j++)v[g[0].col[p(i,j)]|g[1].col[p(i,j)]].push_back(pr(i,j));
	}
	for(i=0;i<4;i++){
		if((int)v[i].size()>=n*n){
			for(j=0;j<n*n;j++)printf("%d %d\n",v[i][j].x,v[i][j].y);
			break;
		}
	}
}


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

int main(){
  string s;
  cin >> s;
  bool flag = (s.size()%2 == 0);
  while(s.size()>0 && flag){
    if(s.substr(0,2) == "hi"){
      s = s.substr(2,s.size()-2);
    }
    else flag = 0;
  }
  cout << (flag?"Yes":"No") << endl;
}

#include <bits/stdc++.h>
int main()
  {std::string S, t; std::cin >> S;
  int i{5}; while(i--) {t += "hi"; if (t == S) {puts("Yes"); return 0;}}
  puts("No");}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge{int to,from,cost;};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define repd(i,n) for(int i=(n)-1;i>=0;--i)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define repnd(i,m,n) for(int i=(n);i>=(m);--i)
#define rep0(i,n) for(i=0;i<(n);++i)
#define repd0(i,n) for(i=(n)-1;i>=0;--i)
#define repn0(i,m,n) for(i=(m);i<=(n);++i)
#define repnd0(i,m,n) for(i=(n);i>=(m);--i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define IL for(;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647/3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

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

int main() {
	string s;
	cin>>s;
	char now='a';
	int res=0;
	int count[3]={};
	rep(i,sz(s)){
//debug<<i<<" "<<count[0]<<" "<<count[1]<<" "<<count[2]<<endl;
		if(s[i]=='J'){
			if(now=='J'){
				++count[0];
			}else if(now=='O'){
				count[0]=1;
				count[1]=0;
				count[2]=0;
			}else{
				if(count[0]>=count[1]&&count[1]<=count[2])chmax(res,min(min(count[0],count[1]),count[2]));
				count[0]=1;
				count[1]=0;
				count[2]=0;
			}
		}else if(s[i]=='O'){
			if(now!='I'){
				++count[1];
			}else{
				if(count[0]>=count[1]&&count[1]<=count[2])chmax(res,min(min(count[0],count[1]),count[2]));
				count[0]=0;
				count[1]=1;
				count[2]=0;
			}
		}else{
			if(now!='J'){
				++count[2];
			}else{
				count[0]=0;
				count[1]=0;
				count[2]=1;
			}
		}
		now=s[i];
	}
	if(count[0]>=count[1]&&count[1]<=count[2])chmax(res,min(min(count[0],count[1]),count[2]));
	cout<<res<<endl;
	return 0;
}

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

using namespace std;

int main (int argc, char *argv[]) {
    string str;
    int ans = 0;
    int n1, n2, n3;
    cin>>str;

    for (int i = 0; i < str.length(); ++i) {
        n1 = n2 = n3 = 0;
        while (str[i] == 'J') {
            n1++;
            i++;
        }
        while (str[i] == 'O') {
            n2++;
            i++;
        }
        while (str[i] == 'I') {
            n3++;
            i++;
        }
        if(n2 <= n1 && n2 <= n3) ans = max(ans, n2);
        i--;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
int main()
{
    long long a,b=0;
    cin>>a;
    for(int i=1;i<=a;i++)
    {
        if(i%3==0||i%5==0);
    else b=b+i;
    }
    cout<<b;
    return 0;

}


#include "bits/stdc++.h"
using namespace std;
int main() {
	long long N, M, bx, x, by, y, XCount = 0, YCount = 0;
	const long long MOD = 1e9 + 7;
	cin >> N >> M >> bx;
	for (int i = 1; i < N; i++) {
		cin >> x;
		XCount += i * (N - i) * (x - bx);
		XCount = XCount % MOD;
		bx = x;
	}
	cin >> by;
	for (int i = 1; i < M; i++) {
		cin >> y;
		YCount += i * (M - i) * (y - by);
		YCount = YCount % MOD;
		by = y;
	}
	cout << (XCount * YCount) % MOD;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#define ep emplace_back
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,1,0,-1},dy={-1,0,1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,m;cin >> n >> m;
	vector<ll> x(n), y(m);
	rep(i,n) cin >> x.at(i);
	rep(i,m) cin >> y.at(i);
	ll ans = 0, sum = 0;
	rep(i,n){
		ans += x.at(i) * i - x.at(i) * (n - i - 1);
		ans %= mod;
	}
	rep(i,m){
		sum += y.at(i) * i - y.at(i) * (m - i - 1);
		sum %= mod;
	}
	cout << ans * sum % mod << endl;
}


#include<iostream>
using namespace std;
int main(){
	int a,b=0;
	cin>>a;
	if(a%3==0 && a>0){
		b=(a/3)+b;

	}
	else if(a%3==1 && a>1){
		a-=4;
		b++;
		b=(a/3)+b;
	}
	else if(a%3==2 && a>2){
		a-=5;
		b++;
		b=(a/3)+b;
	}
	else{
		system("pause");
	}
	cout<<b;
}

#include <iostream>
#include <bits/c++io.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
using namespace std;

int main()
{
int x;
cin>>x;
cout<<x/3<<endl;

}


#include <algorithm>
#include <iostream>
#include <string>
#include <random>
#include <cassert>
#include <cstring>
#include <chrono>

using namespace std;

typedef long long ll;

int main()
{
  int n;
  cin >> n;
  vector<pair<ll, ll>> p(n);
  for (int i = 0; i < n; ++i) {
    ll a, b;
    cin >> a >> b;
    p[i] = {a, b};
  }
  sort(p.begin(), p.end(), [](pair<ll, ll> p1, pair<ll, ll> p2) {
      return p1.first + p1.second > p2.first + p2.second;
      });
  ll result = 0;
  for (int i = 0; i < n; ++i) {
    if (i % 2 == 0) {
      result += p[i].first;
    } else {
      result -= p[i].second;
    }
  }
  cout << result << endl;

  return 0;
}

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;

template<class _T> static void getint(_T& a) {
	std::cin >> a;
}
template<class _T> static void getint(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void getint(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}
template <class _T> static _T tp_abs(_T a) {
	if (a < (_T)0) {
		a *= (_T)-1;
	}
	return a;
}

static void exec();

int main()
{
	exec();
	fflush(stdout);
	return 0;
}

static void exec()
{
	int N;
	getint(N);

	std::vector<std::pair<LLONG, LLONG>> plates(N);
	for (int i = 0; i < N; i++) {
		getint(plates[i].first, plates[i].second);
	}

	std::sort(plates.begin(), plates.end(),
		[](const std::pair<LLONG, LLONG>& a, const std::pair<LLONG, LLONG>& b) {
		return (a.first + a.second) > (b.first + b.second);
	});

	bool tkhs = true;
	std::vector<std::pair<LLONG, LLONG>> tk, ao;
	for (int i = 0; i < N; i++) {
		if (tkhs) {
			tk.push_back(plates[i]);
			tkhs = false;
		}
		else {
			ao.push_back(plates[i]);
			tkhs = true;
		}
	}

	LLONG tkSum = 0;
	for (auto it = tk.begin(); it != tk.end(); it++) {
		tkSum += it->first;
	}
	LLONG aoSum = 0;
	for (auto it = ao.begin(); it != ao.end(); it++) {
		aoSum += it->second;
	}

	printf("%lld\n", tkSum - aoSum);
}


#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int n,k;
    cin>>n>>k;
    vector<ld> p(n),a(n-k+1);
    ld ans=0;
    rep(i,n){
        cin>>p[i];
        p[i]=(1+p[i])/2;
        if(i-k<0) a[0]+=p[i];
        else{
            a[i-k+1]=a[i-k]-p[i-k]+p[i];
            ans=max(ans,a[i-k]);
        }
    }
    ans=max(ans,a[n-k]);
    cout<<fixed<<setprecision(10)<<ans;
}

#include <bits/stdc++.h>

#define mp make_pair

using namespace std;

typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
typedef pair<int,int> pii;
typedef pair<i64,int> pli;
typedef pair<int,i64> pil;
typedef pair<i64,i64> pll;
typedef vector<int> vi;
typedef vector<i64> vl;

template <typename T>
T id(T b) {return b;};
template <class It>
bool all(It f,It l){return std::all_of(f,l,id<bool>);}
template <class It>
bool any(It f,It l){return std::any_of(f,l,id<bool>);}

template <int N> class rmq {
  int n; i64 dat[4*N-1];
  i64 query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return dat[k];
    else {
      return min(query(a,b,2*k+1,l,(l+r)/2),query(a,b,2*k+2,(l+r)/2,r));
    }
  }

public:
  const i64 INF = INT64_MAX;
  rmq() {
    n = 1;
    while (n < N) n *= 2;
    for (int i = 0; i < 2*n-1; i++) dat[i] = INF;
  }

  void update(int k, i64 a) {
    k += n-1; dat[k] = a;
    while (k > 0) {
      k = (k-1) / 2;
      dat[k] = min(dat[2*k+1],dat[2*k+2]);
    }
  }

  i64 query(int a, int b) { return query(a, b, 0, 0, n); }
};

struct union_find {
  typedef vector<int> vi;
  vi parent,rank,cnt;
  int n;

  union_find(int n) : n(n) {
    parent = vi(n); rank = vi(n,0); cnt = vi(n,1);
    for (int i = 0; i < n; i++) parent[i] = i;
  }

  int find(int a) {
    if (parent[a] == a) return a;
    return parent[a] = find(parent[a]);
  }

  void unite(int a, int b) {
    a = find(a); b = find(b);

    if (a == b) return;
    if (rank[a] < rank[b]) {
      parent[a] = b; cnt[b] += cnt[a];
    } else {
      parent[b] = a; cnt[a] = cnt[b];
      if (rank[a] == rank[b])
        rank[a]++;
    }
  }

  int count(int a) { return cnt[find(a)]; }

  bool same(int a, int b) { return find(a) == find(b); }
};

typedef tuple<i64,int,int> edge;

const int MAX_N = 200000;
int n;
i64 d, a[MAX_N];
pii b[MAX_N];
int tab[MAX_N];
rmq<MAX_N> r1, r2;
map<i64,int> m;

int main()
{
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> a[i], b[i] = mp(a[i],i);
  sort(b,b+n);

  for (int i = 0; i < n; i++) tab[b[i].second] = i;

  vector<edge> es;

  r1.update(tab[0], a[0]-d);
  m[a[0]-d] = 0;
  for (int i = 1; i < n; i++) {
    i64 v = r1.query(0,tab[i]);
    if (v < r1.INF) {
      int j = m[v];
      es.push_back(make_tuple(v+a[i]+(i+1)*d,i,j));
    }
    v = a[i]-(i+1)*d;
    r1.update(tab[i], v);
    m[v] = i;
  }

  m.clear();
  r2.update(tab[n-1], a[n-1]+n*d);
  m[a[n-1]+n*d] = n-1;
  for (int i = n-2; i >= 0; i--) {
    i64 v = r2.query(0,tab[i]);
    if (v < r2.INF) {
      int j = m[v];
      es.push_back(make_tuple(v+a[i]-(i+1)*d,i,j));
    }
    v = a[i]+(i+1)*d;
    r2.update(tab[i], v);
    m[v] = i;
  }

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

  union_find uf(n);
  i64 res = 0;
  for (auto e : es) {
    i64 cost; int u,v;
    tie(cost,u,v) = e;
    if (uf.same(u,v)) continue;
    res += cost;
    uf.unite(u,v);
  }

  cout << res << endl;

  return 0;
}


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

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

	cout << min(A, B) + min(C, D) << endl;
}

#include <bits/stdc++.h>

const int N = 100054;

int n, K, ans;
int p[N], fc[N], nc[N], f[N];

inline void up(int &x, const int y) {x < y ? x = y : 0;}
inline void link(int x, int px) {nc[x] = fc[px], fc[px] = x;}

void dfs(int x, int dep = 0) {
	for (int y = fc[x]; y; y = nc[y]) dfs(y, dep + 1), up(f[x], f[y] + 1);
	if (dep > 1 && f[x] >= K - 1) ++ans, f[x] = -1;
}

int main() {
	scanf("%d%d%d", &n, &K, &ans), ans = ans != 1;
	for (int i = 2; i <= n; ++i) scanf("%d", p + i), link(i, p[i]);
	dfs(1), printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main(){
  string s;
  cin >> s;
  if(s == "SUN"){
    cout << "7\n";
    return 0;
  }
  if(s == "MON"){
    cout << "6\n";
    return 0;
  }
  if(s == "TUE"){
    cout << "5\n";
    return 0;
  }
  if(s == "WED"){
    cout << "4\n";
    return 0;
  }
  if(s == "THU"){
    cout << "3\n";
    return 0;
  }
  if(s == "FRI"){
    cout << "2\n";
    return 0;
  }
  if(s == "SAT"){
    cout << "1\n";
    return 0;
  }
}

#include<iostream>
using namespace std;

class dice {
private:
	int n[7];

public:
	dice(int, int, int, int, int, int);
	int getcur() { return n[1]; }
	void rotate(char);
};

dice::dice(int n1, int n2, int n3, int n4, int n5, int n6) {
	n[1] = n1;
	n[2] = n2;
	n[3] = n3;
	n[4] = n4;
	n[5] = n5;
	n[6] = n6;
}

void dice::rotate(char op) {
	dice copy(n[1], n[2], n[3], n[4], n[5], n[6]);
	switch (op) {
	case'W':
		n[1] = copy.n[3];
		n[3] = copy.n[6];
		n[6] = copy.n[4];
		n[4] = copy.n[1];
		break;
	case'E':
		n[1] = copy.n[4];
		n[3] = copy.n[1];
		n[6] = copy.n[3];
		n[4] = copy.n[6];
		break;
	case'S':
		n[1] = copy.n[5];
		n[2] = copy.n[1];
		n[6] = copy.n[2];
		n[5] = copy.n[6];
		break;
	case'N':
		n[1] = copy.n[2];
		n[2] = copy.n[6];
		n[6] = copy.n[5];
		n[5] = copy.n[1];
		break;
	}

}

int main() {
	int n[7];
	for (int i = 1; i <= 6; i++) {
		cin >> n[i];
	}
	dice a(n[1], n[2], n[3], n[4], n[5], n[6]);

	char op;
	while (cin >> op) {
		a.rotate(op);
		//cerr << a.getcur() << endl;
	}
	cout << a.getcur() << endl;
	return 0;
}


#include <iostream>
#include <deque>
#include <utility>
#include <algorithm>
#include <map>
#include <vector>
#define llint long long
#define mod 998244353
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;

struct SegTree{
	int size;
	vector<P> seg;

	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}

	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = P(inf, inf);
	}

	void update(int i, P val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = min(seg[i*2], seg[i*2+1]);
		}
	}

	P query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return P(inf, inf);
		if(a <= l && r <= b) return seg[k];
		P lval = query(a, b, k*2, l, (l+r)/2);
		P rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return min(lval, rval);
	}
	P query(int a, int b)
	{
		if(a > b) return P(inf, inf);
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

struct UnionFind{
	int size;
	vector<int> parent;

	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++) parent[i] = i;
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j) return;
		parent[root_i] = root_j;
	}
};

llint n, d;
llint a[200005];
SegTree lseg(18), rseg(18);
vector<P> vec;
vector<E> evec;
UnionFind uf(200005);

int main(void)
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) vec.push_back(P(a[i], i));
	sort(vec.begin(), vec.end());

	lseg.init(), rseg.init();
	for(int i = 0; i < vec.size(); i++){
		int p = vec[i].second;
		P res = lseg.query(1, p-1);
		if(res.first < inf/2) evec.push_back(E(res.first+p*d+a[p], P(p, res.second)));
		res = rseg.query(p+1, n);
		if(res.first < inf/2) evec.push_back(E(res.first-p*d+a[p], P(p, res.second)));
		lseg.update(p, P(a[p]-p*d, p));
		rseg.update(p, P(a[p]+p*d, p));
	}
	sort(evec.begin(), evec.end());

	llint ans = 0;
	for(int i = 0; i < evec.size(); i++){
		llint u = evec[i].second.first, v = evec[i].second.second, w = evec[i].first;
		if(uf.same(u, v)) continue;
		ans += w;
		uf.unite(u, v);
	}
	cout << ans << endl;

	return 0;
}

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

int main(){
  int N,T;
  cin >> N >> T;
  vector<int> time(N);
  for(int i = 0; i < N; i++){
    cin >> time.at(i);
  }
  long long ans, sum = T;
  for(int i = 0; i < N-1; i++){
     ans = min(T,time.at(i+1)-time.at(i));
    sum += ans;
  }
  cout << sum << endl;
}