#include<bits/stdc++.h>

using namespace std;
using   ll=long long;

#define fast        	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define endl        	"\n"
#define pb          	push_back
#define mp          	make_pair
#define F           	first
#define S           	second
#define int             long long int
#define pll				pair<int , int>

#define ALL(v)      	v.begin(),v.end()
#define ALLR(v)     	v.rbegin(),v.rend()
#define pii         	3.14159265358979323
#define inf     		LLONG_MAX
#define ones(x)     	__builtin_popcount(x)
#define fill(a,b)   	memset(a,b,sizeof(a))
#define mod 			1000000007
#define hell            998244353

ll mod_pow(ll a,ll b,ll m)
{
    ll res = 1;
    while(b)
    {
        if(b&1)
        {
        	res=(res*a) % m;
        }
        a=(a*a) % m;
        b>>=1;
    }
    return res;
}

ll mod_inverse(ll a)
{
	return mod_pow(a , mod-2 , mod);
}

void solve()
{
	int n , L;

	cin >> n >> L;

	int mx = inf;
	int sm1 = 0;

	for(int i = 1; i <= n; ++i) {
		sm1 += L + i -1;
	}

	int ans;

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

		for(int j = 1; j <= n; ++j) {
			if(i != j) {
				sm += L + j - 1;
			}
		}

		//cout << abs(sm - sm1) << " " << sm << endl;

		if(abs(sm - sm1) < mx) {
			mx = abs(sm - sm1);
			ans = sm;
		}
	}

	cout << ans;
}

signed main() {
    fast;

    int t = 1;

	//cin >> t;

    while(t--) {
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#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++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)

int main() {
    int N; cin >> N;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    vector<int> memo = {-A[0]};
    rep2(i, 1, N) {
        int tmp = upper_bound(ALL(memo), -A[i]) - memo.begin();
        if (tmp == memo.size()) memo.push_back(-A[i]);
        else memo[tmp] = -A[i];
    }
    cout << memo.size() << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  string s, t; cin >> s >> t;
  for(int i=n; i>=0; i--) {
    string s_ = s.substr(n-i);
    string t_ = t.substr(0, i);
    if(s_ == t_) {
      cout << n*2-i << "\n";
      break;
    }
  }
  return 0;
}

#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
int main(void){
    int n,q; cin >> n >> q;
    string S; cin >> S;
    vector<int> sum(n+1,0);
    int i = 0;
    while (i < n) {
        if (S[i] == 'A' && S[i+1] == 'C') {
            sum[i+1] = sum[i] + 1;
        } else{
            sum[i+1] = sum[i];
        }
        i++;
    }
    i = 0;
    while (i < q) {
        int tmp1, tmp2;
        cin >> tmp1 >> tmp2;
        tmp1--; tmp2--;
        cout << sum[tmp2] - sum[tmp1] << endl;
        i++;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define int long long
#define fi  first
#define se  second

typedef pair<int,int>   ii;

priority_queue<int> q;
vector<ii>  v;
int n, sum = 0;

signed main()  {
    ios_base::sync_with_stdio(false);   cin.tie(0);
    cin >> n;
    for(int i = 0 ; i < n ; ++i)    {
        int D, L;   cin >> D >> L;
        v.push_back(ii(D + L,L));
    }
    sort(v.begin(),v.end());
    for(int i = 0 ; i < n ; ++i)    {
        q.push(v[i].se);
        sum += v[i].se;
        if(sum > v[i].fi)   {
            sum -= q.top();
            q.pop();
        }
    }

    cout << q.size() << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string a, b, c;
  cin >> a >> b >> c;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    string x, y, z;
    x = a.at(i); y = b.at(i); z = c.at(i);
    set<string> s{x, y, z};
    if (s.size() == 3) {
      ans += 2;
    } else if (s.size() == 2) {
      ans += 1;
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define In_The_Name_Of_Allah_The_Merciful ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define Alhamdulillah return 0;
#define debug(n) cerr << "[" << #n << " = " << n << "]" << endl

void Suhaib_Sawalha (){

    vector <string> v = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    string s;
    cin >> s;
    cout << v.end() - find(v.begin(),v.end(),s);

}

int main(){
                                In_The_Name_Of_Allah_The_Merciful   /* بسم الله الرحمن الرحيم  */
#ifndef ONLINE_JUDGE
    freopen("SuhaibSawalha1.txt","r",stdin);
#endif
//int _;for(cin>>_;_;--_,cout<<'\n')
        Suhaib_Sawalha();
                                         Alhamdulillah                    /* الحمد لله */
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 5LL << 60;
const ll mod = 1e9 + 7;

int main() {
	string S;
	cin >> S;
	int ans = 7;
	if(S == "MON") ans = 6;
	if(S == "TUE") ans = 5;
	if(S == "WED") ans = 4;
	if(S == "THU") ans = 3;
	if(S == "FRI") ans = 2;
	if(S == "SAT") ans = 1;
	cout << ans << "\n";
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char h[3]={" \n"};
int main()
{
int w[3],x;
for(int j=0;j<2;j++){
 memset(w,0,sizeof(w));
 for(int i=0;i<10;i++){
 scanf("%d",&x);
 if(x>w[0]){w[2]=w[1]; w[1]=w[0]; w[0]=x;}
 else if(x>w[1]) {w[2]=w[1]; w[1]=x;}
 else if(x>w[2]) w[2]=x;
 }
 printf("%d%c",w[0]+w[1]+w[2],h[j]);
}
}

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

int main () {
	int w[10];
	int k[10];
	for (int i = 0; i < 10; i++) {
		cin >> w[i];
	}
	for (int i = 0; i < 10; i++) {
		cin >> k[i];
	}
	sort(w, w + 10);
	sort(k, k + 10);
	int wSum = 0, kSum = 0;
	for (int i = 9; i > 6; i--) {
		wSum += w[i];
	}
	for (int i = 9; i > 6; i--) {
		kSum += k[i];
	}
	cout << wSum << " " << kSum << endl;
	return 0;
}


// Problem : E - Colorful Blocks
// Contest : AtCoder - AtCoder Beginner Contest 167
// URL : https://atcoder.jp/contests/abc167/tasks/abc167_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define fastio                      \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0);
#define sz(a) (int)((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                       \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \
       << endl
#define dbp(x) \
  cout << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define dbv(v)                         \
  {                                    \
    cout << #v << " : ";               \
    for (auto x : v) cout << x << ' '; \
    cout << endl;                      \
  }
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die(x) return cout << x << '\n', 0;
const int MOD = 998244353;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int MAX = 2e5;
ll modulo(ll a, ll b, ll n) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y) % n;  // multiplying with base
    }
    y = (y * y) % n;  // squaring the base
    b /= 2;
  }
  return x % n;
}
vi fact(MAX + 1), factinv(MAX + 1);

void pre() {
  fact[0] = 1;
  factinv[0] = 1;
  for (int i = 1; i <= MAX; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
    factinv[i] = (factinv[i - 1] * modulo(i, MOD - 2, MOD)) % MOD;
  }
}

int mult(int a, int b) { return (a * b) % MOD; }
int nCr(int n, int r) {
  return mult(fact[n], mult(factinv[r], factinv[n - r]));
}

int32_t main() {
  fastio;
  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);
  int testcases;
  testcases = 1;
  // cin>>testcases;
  pre();
  while (testcases--) {
    int n, m, k;
    cin >> n >> m >> k;
    int ans = 0;
    rep(i, 0, k) {
      int temp = nCr(n - 1, i);
      temp = mult(temp, m);
      temp = mult(temp, modulo(m - 1, n - 1 - i, MOD));
      ans = (ans + temp) % MOD;
    }
    cout << ans << '\n';
  }

  return 0;
}

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

// x^y % m
ll mpow(ll x, ll y, ll m = 998244353) {
  ll ret = 1;
  while (y != 0) {
    if (y & 1) ret = (ret * x) % m;
    x = (x * x) % m;
    y = y >> 1;
  }
  return ret;
};

ll minv(ll a, ll m = 998244353) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

int main() {
  const ll MOD = 998244353;
  ll n, m, k;
  cin >> n >> m >> k;
  if (m == 1) {
    cout << ((k == n - 1) ? 1 : 0) << endl;
    return 0;
  }

  ll ans = 0;
  ll fi = (m * mpow(m - 1, n - 1)) % MOD;
  ans = (ans + fi) % MOD;
  rep2(i, 1, k + 1) {
    fi = (fi * (n - i)) % MOD;
    fi = (fi * minv(i)) % MOD;
    fi = (fi * minv(m - 1)) % MOD;
    ans = (ans + fi) % MOD;
  }

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


#include<bits/stdc++.h>
#define ll long long
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
using namespace std;

int main()
{
    fast_io
    ll i,j,n,x,ans=0;
    string s;
    bool fA=0,fc=0,noAZ=0;
    cin>>s;
        if(s[0]!='A'||(s[1]>='A'&&s[1]<='Z')||(s[s.length()-1]>='A'&&s[s.length()-1]<='Z'))
   return cout <<"WA"<<endl,0;
    ll cntc=0,cap=0;
    for(i=1; i<s.length()-1; i++)
    {
    if(i>=2&&i<s.length()-1)
    {
        if(s[i]=='C') cntc++;
    }
    if(s[i]>='A'&&s[i]<='Z') cap++;
    }

if(cap>1)
   return cout <<"WA"<<endl,0;
    if(cntc!=1)
   return cout <<"WA"<<endl,0;
   return cout <<"AC"<<endl,0;
    return 0;
}

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
    string s;
    cin >> s;
    int l = s.length();
    if(s[0]!='A'){
        cout << "WA\n";
        return 0;
    }
    int i, cnt = 0;
    for(i=1; i<l; i++){
        if(isupper(s[i])){
            if(i==1 || i==l-1 || s[i]!='C'){
                cout << "WA\n";
                return 0;
            }
            cnt++;
        }
    }
    if(cnt==1) cout << "AC\n";
    else cout << "WA\n";
    return 0;
}


#include <iostream>

using namespace std;

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

int main()
{
    int P;
    int minR=200,minS=200;
    int total=0;

    for(int i=0; i<4; ++i) {
        cin >> P;
        total+=P;
        minR=MIN(P,minR);
    }
    for(int i=0; i<2; ++i) {
        cin >> P;
        total+=P;
        minS=MIN(P,minS);
    }
    cout << total - minR - minS << endl;
    return 0;
}

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int s[10] = { 0 }, h[10]{ 0 };
	scanf("%d %d %d %d %d %d", &s[0], &s[1], &s[2], &s[3], &h[0], &h[1]);

	printf("%d\n", sum(4,s)- min(4, s)+max(2,h) );
	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {
	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (min > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (max<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}


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

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

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

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

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

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

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

int getsum(const vvi& sums,int t,int b,int l,int r)
{
	if(l>=r || t>=b) return 0;
	return sums[b][r]-sums[t][r]-sums[b][l]+sums[t][l];
}

void chmax(int& a,int b)
{
	a=max(a,b);
}

int main()
{
	for(int h,w;cin>>h>>w && h|w;){
		vs grid(h);
		rep(i,h) cin>>grid[i];

		int ci=-1,cj=-1;
		rep(i,h) rep(j,w) if(grid[i][j]=='E') ci=i,cj=j;

		vvi sums(h+1,vi(w+1));
		rep(i,h) rep(j,w)
			sums[i+1][j+1]=(grid[i][j]=='o')+sums[i+1][j]+sums[i][j+1]-sums[i][j];

		int T=h-1-ci,B=ci,L=w-1-cj,R=cj;

		int dp[T+1][B+1][L+1][R+1]={};
		rep(t,T+1) rep(b,B+1) rep(l,L+1) rep(r,R+1){
			if(t!=T && ci+t+1<h-b)
				chmax(dp[t+1][b][l][r],dp[t][b][l][r]+getsum(sums,ci+(t+1),ci+(t+1)+1,max(cj-r,l),min(cj+l+1,w-r)));
			if(b!=B && t<=ci-(b+1))
				chmax(dp[t][b+1][l][r],dp[t][b][l][r]+getsum(sums,ci-(b+1),ci-(b+1)+1,max(cj-r,l),min(cj+l+1,w-r)));
			if(l!=L && cj+l+1<w-r)
				chmax(dp[t][b][l+1][r],dp[t][b][l][r]+getsum(sums,max(ci-b,t),min(ci+t+1,h-b),cj+(l+1),cj+(l+1)+1));
			if(r!=R && l<=cj-(r+1))
				chmax(dp[t][b][l][r+1],dp[t][b][l][r]+getsum(sums,max(ci-b,t),min(ci+t+1,h-b),cj-(r+1),cj-(r+1)+1));
		}

		int res=0;
		rep(t,T+1) rep(b,B+1) rep(l,L+1) rep(r,R+1) chmax(res,dp[t][b][l][r]);
		cout<<res<<endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
const ll match[10]={inf,2,5,5,4,5,6,3,7,6};
void update(string &a,string b){
    if(a=="x")a=b;
    else if(a.size()<b.size())a=b;
    else if(a.size()==b.size()){
        if(a<b)a=b;
    }
}
int main(){
    ll n,m;cin >> n >> m;
    vl a(m);rep(i,m)cin >> a[i];
    string dp[10100];
    rep(i,10100){
        dp[i]="x";
    }
    dp[0]="";
    rep(i,n+1){
        if(dp[i]=="x")continue;
        for(auto p:a){
            update(dp[i+match[p]],dp[i]+to_string(p));
        }
    }
    cout << dp[n] <<endl;
}


#include<iostream>
using namespace std;

class Dice {
    public:
	int f[7];
	Dice() {}
	void roll_x() { roll(1, 4, 6, 3); } // to E
	void roll_y() { roll(1, 2, 6, 5); } // to N
	void roll_z() { roll(2, 3, 5, 4); } // 上から見て時計回り
	void roll(int i, int j, int k, int l) {
		int t = f[i]; f[i] = f[j]; f[j] = f[k]; f[k] = f[l]; f[l] = t;
	}
};

bool judge(Dice c1, Dice c2) {
	for (int i = 1; i <= 6; i++) {
		if (c1.f[i] != c2.f[i]) return false;
	}
	return true;
}

bool getRight(Dice c1, Dice c2) {
	for (int i = 0; i < 6; i++) {
		for (int j = 0; j < 4; j++) {
			if (judge(c1, c2)) return true;
			c1.roll_z();
		}
		if (i % 2 == 0) c1.roll_x();
		else c1.roll_y();
	}
	return false;
}

Dice D[100]; int n;

bool check(){
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(getRight(D[i],D[j])) return false;
        }
    }
    return true;
}

int main() {
    cin >> n;

    for(int i=0;i<n;i++){
        for(int j=1;j<=6;j++){
            cin >> D[i].f[j];
        }
    }

	if(check()) cout << "Yes" << endl;
    else cout << "No" << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <set>
#include <utility>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <numeric>
#include <sstream>
#include <list>
#include <map>

#define rep(i,p,n) for(int i=(int)p;i<(int)n;i++)
#define repp(i,p,n) for(int i=(int)p;i>=(int)n;i--)
#define Sort(x) sort(x.begin(),x.end());
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi <vector<vector<vector<int>>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vvvc vector<vector<vector<char>>>
#define INF 2000000000

using namespace std;

int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0)break;

		vector<int> v(10,0);
		for (int i = 0; i < n; i++) {
			int x;
			cin >> x;
			v[x]++;
		}

		for (int i = 0; i < 10; i++) {
			if (v[i] != 0) {
				for (int j = 0; j < v[i]; j++) cout << "*";
				cout << endl;
			}
			else cout << "-" << endl;
		}
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int dx[]={1,0,0,-1,1,-1,-1,1,2,0,-2,0};
const int dy[]={0,1,-1,0,1,1,-1,-1,0,2,0,-2};
const int INF = 1<<30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i,j,k) for(int i = (j); i < (k); ++i)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int,int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
int main(){
	int N,Q;
	while(scanf("%d%d",&N,&Q),(N|Q)){
		int s[128]={0};
		int length = 0;
		rep(i,N){
			int c;
			scanf("%d",&c);
			rep(j,c){
				int t;
				scanf("%d",&t);
				s[t]++;
				length = max(length,t);
			}
		}
		length++;
		int ans = 0;
		int index = 0;
		rep(i,length){
			if(s[i]>=Q && ans < s[i]){
				ans = s[i];
				index = i;
			}
		}
		printf("%d\n",index);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[5010];
const int INF = 1e12+10;
int32_t main() {
    ios::sync_with_stdio(false);cin.tie(0);
    int n;
    cin>>n;
    vector<pair<int,int> > v(n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    sort(v.begin(), v.end(), [](const pair<int,int> &l, const pair<int,int> &r){
        return l.first+l.second<r.first+r.second;
    });
    for(int i=1;i<=5000;i++)
        dp[i] = INF;
    for(int i=0;i<n;i++)
    {
        for(int j=5000;j>=0;j--)
        {
            if(dp[j]<=v[i].first)
                dp[j+1] = min(dp[j+1],dp[j]+v[i].second);
        }
    }
    for(int i=5000;i>=0;i--)
    {
        if(dp[i]!=INF)
        {
            cout<<i;
            return 0;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define int long long
using pii=std::pair<int,int>;
using namespace std;

const int maxn = 16, maxmask = 1ll << 16;

int n, a[maxn][maxn], dp[maxmask];
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> a[i][j];
    for(int i = 0; i < (1 << n); i++)
    {
        for(int j = 0; j < n; j++)
            for(int k = j + 1; k < n; k++)
                if((i & (1ll << j)) && (i & (1ll << k)))
                    dp[i] += a[j][k];
        for(int j = i; j > 0; j = (j - 1) & i)
            dp[i] = max(dp[i], dp[j] + dp[i ^ j]);
    }
    cout << dp[(1 << n) - 1] << "\n";
    return 0;
}


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

using ll = long long;
//print(fixed << setprecision(10));
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define repk(i,n,k) for(ll i = k; i < (n); i++)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl
double PI = acos(-1.0);
int INF = 1e9;
ll LINF = 1e18;
ll MOD = 1e9+7;

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

    ll n;
    cin >> n;
    string s="";
    while(n){
        n--;
        s+=('a'+(n+25/26)%26);
        n/=26;
    }
    reverse(all(s));
    print(s);
    return 0;
}

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

int main(void) {
  string s; cin >> s;
  string c = "abcdefghijklmnopqrstuvwxyz";
  bool find_flag = false;

  for (int i=0; i<26; i++) {
    bool target = false;
    for (int j=0; j<s.length(); j++) {
      if (c[i] == s[j]) target = true;
    }
    if (!target) {
      cout << c[i] << endl;
      find_flag = true;
      break;
    }
  }

  if (!find_flag) cout << "None" << endl;
  return 0;
}


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

// 小数点以下桁数
// fixed << setprecision(i)

int64_t gcd(int64_t a,int64_t b){return (a%b==0?b:gcd(b, a%b));}
int64_t lcm(int64_t a,int64_t b){return a/gcd(a, b)*b;}
int factorial(int a){int b=1;while(a){b*=a--;}return b;} //13以下対応
bool is_prime(int64_t a){if(a<=1)return false;for(int64_t i=2;(int64_t)i*i<=a;i++){if(a%i==0)return false;}return true;}
int get_adp(double x, int n){return (int)round((x-(int)x)*pow(10,n))%10;} // 小数点以下の指定桁の値を取得
int64_t sigma(int64_t s, int64_t n){return n*(2*s+n-1)/2;}

//順列の全列挙
// vector<int> v(N);
// iota(v.begin(), v.end(), 1);       // v に 1, 2, ... N を設定
// do {
//	for(auto x : v) cout << x << " "; cout << "\n";     // v の要素を表示
// } while( next_permutation(v.begin(), v.end()) );

//bit全探索
// for (int tmp = 0; tmp < (1 << ビット数); tmp++) {
//	bitset<ビット数> s(tmp);
//	// (ビット列sに対する処理)
// }

int main()
{
	int64_t ans = 0;
	int n, temp, r = 0, g = 0, b = 0;
	string s;

	cin >> n >> s;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'R')
			r++;
		else if (s[i] == 'G')
			g++;
		else
			b++;
	}
	ans = (int64_t)r * g * b;
	for (int i = 1; i <= n / 2; i++)
	{
		for (int j = i; j < n - i; j++)
		{
			if (s[j] != s[j - i] && s[j] != s[j + i] && s[j - i] != s[j + i])
				ans--;
		}
	}
	ans = (ans < 0 ? 0 : ans);
	// cout << r << " " << g << " " << b << endl;
	// cout << r * g * b << endl;
	cout << ans << endl;
}


#include<iostream>
#include<stdio.h>//printf("%.9f\n",*********);
#include<vector>//文字列.erase(n),vector
#include<algorithm>//next_permutation
#include<set>
#include<string>//char(小文字-32=大文字) to_string,string,char
#include<sstream>
#include<complex>
#include<time.h>
#include<random>
#include<cmath>//min,max
#include<math.h>
#include<queue>
#include<time.h>
#include<iomanip>
#include<locale>
#include<utility>//swap
#define rt "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define rop(i,n) for(int i=1;i<=n;i++)//start==1
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define drop(i,n) for(int i=n;0<i;i--)//end==1
#define yes(ans) if(ans)cout<<"yes"<<rt;else cout<<"no"<<rt;
#define Yes(ans) if(ans)cout<<"Yes"<<rt;else cout<<"No"<<rt;
#define YES(ans) if(ans)cout<<"YES"<<rt;else cout<<"NO"<<rt;
#define Yay(ans) if(ans)cout<<"Yay!"<<rt;else cout<<":("<<rt;
#define sec(a,b,ans) if(ans)cout<<a<<rt;else cout<<b<<rt;
#define vcin(s,n) rep(i,n)cin>>s[i];//vcin(配列名),(繰り返し回数)
#define sort(s) sort(s.begin(),s.end())//標準=昇順
#define reve(s) reverse(s.begin(),s.end())//反転
#define str string
#define please return
#define AC 0
#define rapid_pleaseAC_fast cin.tie(0);ios::sync_with_stdio(false)
#define pi 3.1415926535897932384626//pi
#define nine 1000000000//10^9

using namespace std;
typedef vector<int> vint;
typedef long double ldouble;
typedef vector<string> vstr;
typedef vector<char> vchar;
typedef vector<double> vdou;
typedef vector<double> vdouble;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<char>> vvchar;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<string>> vvstr;
typedef vector<vector<bool>> vvbool;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;

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

long long LCM(long long a, long long b) {
	return a * b / GCD(a, b);
}

unsigned GetDigit(unsigned num) {
	return std::to_string(num).length();
}

int tow(int n) {//2のn乗
	if (n == 0)return 1;
	int x = tow(n / 2);
	x *= x;
	if (n % 2 == 1)x *= 2;
	return x;//@domino
}

int SED(int n) { //Sum of Each Digit
	int sum = 0;
	while (n > 0) {
		sum += n % 10; n /= 10;
	}
	return sum;
}

unsigned ND(unsigned num) {//Number of Digits
	return std::to_string(num).length();//outmax=10
}//stringのほうが効率よくないか？

/*

 (char)toupper(a[n])=文字列のn文字目を大文字で出力

  pow(a,b)=aのb乗

  */

int main(void) {
	rapid_pleaseAC_fast;

	int n, h, w;
	cin >> n >> h >> w;

	int x = n - w + 1;
	int y = n - h + 1;

	cout << x * y << rt;

	please AC;
}

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define Rep(i, r, n) for (ll i = r; i < n; i++)
#define RRep(i, r, n) for (ll i = n - 1; i <= r; i--)
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define int long long
#define pb push_back
#define mp make_pair
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
const int MOD = (1000000007);
//const int MOD = (998244353);
//const int INF = (1 << 30)-1;
const int INF = (1LL << 60)-1;
const double EPS = (1 >> 30);

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

using Graph = vector<vector<pair<int, int>>>;
using Ki = vector<vector<int>>;
using Vi = vector<int>;

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

    int n,a,b;
    cin>>n>>a>>b;
    int ans=(n-a+1)*(n-b+1);
    if(ans<0){
        cout<<0<<endl;
    }
    else{
        cout<<ans<<endl;
    }

}


#include <stdio.h>

int main() {
  char str[31], ctr = 0;
  scanf("%[^\n]", str);

  for(int i = 0; str[i] != '\0'; i++) {
    if(str[i] == ' ' && str[i-1] == str[i+1]) {
      ctr++;
    }
  }
  if(ctr == 2) {
    puts("YES");
  }
  else {
    puts("NO");
  }
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vl = vector<ll>;  // long longの1次元の型に vl という別名をつける
using vvl = vector<vl>; // long longの2次元の型に vvl という別名をつける
int MOD = 1000000007; //10^9 + 7

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

// 無限大の値
const ll  LINF = 1LL << 60;
const int iINF = 1 << 29;

int main(void){
  string  a,b,c;
  cin >> a >> b >> c;

  cout << (*a.rbegin() == *b.begin() && *b.rbegin() == *c.begin() ? "YES" : "NO")  << endl;
  return 0;
}


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

void chmin(int& a, int b) {
  if (a > b) a = b;
}

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

  vector<int> minus, plus;

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x >= 0) {
      plus.push_back(x);
    } else {
      minus.push_back(-x);
    }
  }

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

  int ans = 300000000;

  int M = minus.size(), P = plus.size();
  // minusの個数, Kとのminを
  int minus_num = min(M, K);
  int plus_num = min(P, K);

  if (minus_num == 0) {
    ans = plus[K-1];
  } else if (plus_num == 0) {
    ans = minus[K-1];
  } else {
    for (int i = K - plus_num; i <= minus_num; i++) {
      chmin(ans, min(minus[i-1], plus[K-i-1]) + minus[i-1] + plus[K-i-1]);
    }
  }
  cout << ans << endl;

}

#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;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; }

lli n, k;

int main(void){
    cin >> n >> k;
    vector<lli> a(n);
    rep(i, n) cin >> a[i];
    if(find(a.begin(), a.end(), k) != a.end()){
        cout << "POSSIBLE" << endl;
        return 0;
    }
    if(*max_element(a.begin(), a.end()) < k){
        cout << "IMPOSSIBLE" << endl;
        return 0;
    }
    lli g = 0;
    rep(i, n) g = __gcd(g, a[i]);
    if(k%g == 0){
        cout << "POSSIBLE" << endl;
    }else{
        cout << "IMPOSSIBLE" << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;

#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define SIZE(obj) (int)(obj).sizeT()
#define YESNO(cond, yes, no) \
    { cout << ((cond) ? (yes) : (no)) << endl; }
#define SORT(list) sort(ALL((list)));
#define RSORT(list) sort((list).rbegin(), (list).rend())
#define ASSERT(cond, mes) assert(cond&& mes)

constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'050'000'000;

template <typename T>
T round_up(const T& a, const T& b) {
    return (a + (b - 1)) / b;
}

template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
    is >> p.first >> p.second;
    return is;
}

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

template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    REP(i, (int)v.size())
    is >> v[i];
    return is;
}

template <typename T>
T clamp(T& n, T a, T b) {
    if (n < a) n = a;
    if (n > b) n = b;
    return n;
}

template <typename T>
static T GCD(T u, T v) {
    T r;
    while (v != 0) {
        r = u % v;
        u = v;
        v = r;
    }
    return u;
}

template <typename T>
static T LCM(T u, T v) {
    return u / GCD(u, v) * v;
}

std::vector<int> enum_div(int n) {
    std::vector<int> ret;
    for (int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i != 1 && i * i != n) { ret.push_back(n / i); }
        }
    }
    return ret;
}

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

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

struct ToUpper {
    char operator()(char c) {
        return toupper(c);
    }
};

struct ToLower {
    char operator()(char c) {
        return tolower(c);
    }
};

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

    string S;
    int w;
    cin >> S >> w;

    string ans = "";
    for (int i = 0; i * w < S.length(); i++) { ans.push_back(S[i * w]); }

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

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

ll powa(ll a, ll b, ll c)
{
    a%=c;
    if(a<0)
	a+=c;
    ll res = 1;
    while(b>0)
    {
        if(b&1)
            res*=a, res%=c;
        a*=a, a%=c;
        b>>=1;
    }
    return res;
}
int main()
{
  FAST/**/

   ll n;
   cin>>n;

   ll arr[n];
   ll sum = 0;
   rep(n)
        cin>>arr[i], sum+=arr[i], sum%=mod;

   sum = (sum*sum)%mod;

   rep(n)
       sum = (sum-((arr[i]*arr[i])%mod)+mod)%mod;
   sum*=powa(2,mod-2,mod);
   sum%=mod;
   cout<<sum;

   return 0;
}


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

struct Vertex{
	int key;
	int parent;
	int left;
	int right;
};

vector<Vertex> V;
int root=-1;

void Preorder(int r){
	cout <<" "<< V[r].key;
	if(V[r].left!=-1) Preorder(V[r].left);
	if(V[r].right!=-1) Preorder(V[r].right);
	return;
}

void Inorder(int r){
	if(V[r].left!=-1) Inorder(V[r].left);
	cout <<" "<< V[r].key;
	if(V[r].right!=-1) Inorder(V[r].right);
	return;
}

void Postorder(int r){
	if(V[r].left!=-1) Postorder(V[r].left);
	if(V[r].right!=-1) Postorder(V[r].right);
	cout <<" "<< V[r].key;
	return;
}

void insert(int k){
	Vertex z;
	z.key=k;
	z.left=-1;
	z.right=-1;

	int y=-1;
	int x=root;
	while(x!=-1){
		y=x;
		if (z.key<V[x].key){
			x=V[x].left;
		}else{
			x=V[x].right;
		}
	}
	z.parent=y;
	V.push_back(z);

	if(y==-1){
		root = 0;
	}else if(z.key < V[y].key){
		V[y].left=V.size()-1;
	}else{
		V[y].right=V.size()-1;
	}
}

void printVertex(int i){
	cout << V[i].key <<" " <<V[i].parent <<" " << V[i].left << " " << V[i].right <<endl;
}

void print(){
	Inorder(root);
	cout <<endl;
	Preorder(root);
	cout <<endl;
}

int find(int k){
	int v=root;
	while(1){
		if(k == V[v].key){
			return v;
		}else if(k < V[v].key){
			if(V[v].left==-1){
				return -1;
			}else{
				v=V[v].left;
			}
		}else{
			if(V[v].right==-1){
				return -1;
			}else{
				v=V[v].right;
			}
		}
	}
}

int next(int k){
	int u=V[k].right;
	while(V[u].left!=-1){u=V[u].left;}
	return u;
}

void del(int k){
	int vk=find(k);
	int par=V[vk].parent;
	//printVertex(vk);
	//cout << endl;
	if(V[vk].left==-1 && V[vk].right==-1){
		if(par == -1){
			root = -1;
		}else if(V[par].left==vk) {
			V[par].left=-1;
		}else{
			V[par].right=-1;
		}
	}else if(V[vk].left==-1 && V[vk].right!=-1){
		if(par == -1){
			root=V[vk].right;
		}else if(V[par].left==vk){
			V[par].left=V[vk].right;
		}else{
			V[par].right=V[vk].right;
		}
		V[V[vk].right].parent=par;
	}else if(V[vk].left!=-1 && V[vk].right==-1){
		if(par==-1){
			root=V[vk].left;
		}else if(V[par].left==vk) {
			V[par].left=V[vk].left;
		}else{
			V[par].right=V[vk].left;
		}
		V[V[vk].left].parent=par;
	}else{
		int x=V[next(vk)].key;
		del(x);
		V[vk].key=x;
	}
}

int main(){
	int n,k;
	string s;
	cin >> n;
	for (int i=0;i<n;i++){
		cin >> s;
		if(s=="insert"){
			cin >> k;
			insert(k);
		}else if(s=="find"){
			cin >> k;
			if(find(k)!=-1){
				cout <<"yes"<<endl;
			}else{
				cout << "no" <<endl;
			}
		}else if(s=="delete"){
			cin >> k;
			del(k);
		}else{
			print();
		}
	}
	/*
	for(int i=0;i<n;i++){
		printVertex(i);
		cout <<endl;
	}
	*/
	return 0;
}

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
template<typename V> typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

bool f(string s) {
  vs t;
  t.pb("the");
  t.pb("the.");
  t.pb("this");
  t.pb("this.");
  t.pb("that");
  t.pb("that.");
  stringstream ss;
  ss << s;
  string x;
  while (ss >> x) {
    if (find(all(t), x) != t.end()) return true;
  }
  return false;
}

void nextS(string &s) {
  rep(i, s.size()) {
    if ('a' <= s[i] && s[i] <= 'z') {
      s[i] = (s[i] - 'a' + 1) % 26 + 'a';
    }
  }
}

void solve() {
  string s;
  while (getline(cin, s)) {
    do {
      if (f(s)) {
        cout << s << endl;
        break;
      }
    } while (nextS(s), 1);
  }
}


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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  vary(ll,w,10,0);
  vary(ll,k,10,0);
  rep(i,10){
    cin >> w[i];
  }
  sort(all(w));
  reverse(all(w));
  rep(i,10){
    cin >> k[i];
  }
  sort(all(k));
  reverse(all(k));
  ll a = 0, b = 0;
  rep(i,3){
    a += w[i];
  }
  rep(i,3){
    b += k[i];
  }
  cout << a << " " << b << endl;
  return 0;
}


#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <string>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
int main(void)
{
    string s;
    cin >> s;
    if (s.length() == 2)
    {
        cout << s << endl;
    }
    else
    {
        cout << s[2] << s[1] << s[0] << endl;
    }
    return 0;
}

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

const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1.0);

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


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

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i,n,m) for(ll i=(ll)(m);i<(ll)(n);++i)
#define REP(i,n) FOR(i,n,0)
#define IREP(i,n) for(ll i=(ll)(n);i>=0;--i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

struct Face
{
	int f;
	int a[4];
};
struct Dice
{
	Face f[6];
};

Dice createDice(int k[])
{
	Face d[6];
	d[0].f = k[0];
	d[0].a[0] = k[4];
	d[0].a[1] = k[2];
	d[0].a[2] = k[1];
	d[0].a[3] = k[3];

	d[1].f = k[1];
	d[1].a[0] = k[0];
	d[1].a[1] = k[2];
	d[1].a[2] = k[5];
	d[1].a[3] = k[3];

	d[2].f = k[2];
	d[2].a[0] = k[0];
	d[2].a[1] = k[4];
	d[2].a[2] = k[5];
	d[2].a[3] = k[1];

	d[3].f = k[3];
	d[3].a[0] = k[0];
	d[3].a[1] = k[1];
	d[3].a[2] = k[5];
	d[3].a[3] = k[4];

	d[4].f = k[4];
	d[4].a[0] = k[0];
	d[4].a[1] = k[3];
	d[4].a[2] = k[5];
	d[4].a[3] = k[2];

	d[5].f = k[5];
	d[5].a[0] = k[1];
	d[5].a[1] = k[2];
	d[5].a[2] = k[4];
	d[5].a[3] = k[3];
	Dice dice;
	REP(i, 6)
	{
		dice.f[i] = d[i];
	}
	return dice;
}

int main()
{
	Dice d[105];

	int n;
	cin >> n;
	int sum = 0;
	REP(i, n)
	{
		int x[6];

		REP(j, 6)
		{
			cin >> x[j];
			sum += x[j];
		}

		d[i] = createDice(x);
	}

	if (sum == 22)
	{
		cout << "No" << endl;
		return 0;
	}
	string S = "Yes";

	REP(i, n)
	{
		FOR(j, n, i + 1)
		{
			bool same = true;
			REP(k, 6)
			{
				Face f1 = d[i].f[k];
				REP(l, 6)
				{
					Face f2 = d[j].f[l];
					if (f1.f == f2.f)
					{
						int s = -1;
						REP(m, 4)
						{
							if (f1.a[0] == f2.a[m])
							{
								s = m;
								break;
							}
						}
						if (s == -1)
						{
							same = false;
							break;
						}
						REP(m, 4)
						{
							if (f1.a[m] != f2.a[(m + s) % 4])
							{
								same = false;
								break;
							}
						}
						break;
					}
					if (l == 5)
					{
						same = false;
					}
				}
			}
			if (same)
			{
				S = "No";
				break;
			}
		}
	}

	cout << S << endl;
	return 0;
}


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

using namespace std;

int main()
{
	string a;
	cin >> a;
	reverse(a.begin(),a.end());
	cout << a << endl;
}

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

int main(){

	string str;

	cin >> str;

	std::istringstream stream(str);
	for(int i=str.length()-1; i>=0; i--){
		stream >> str[i];
	}

	cout << str << endl;

	return 0;
}


#pragma region template 2.4
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using pq_asc = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP1(i, n) for (ll i = 1; i <= (n); ++i)
#define FOR(i, a) for (auto &i : a)
#define CH(f, x, y) x = f(x, y)
#define IN(T, x) \
    T x;         \
    cin >> x;
#define AIN(T, a, n) \
    vector<T> a(n);  \
    FOR(i, a)        \
    cin >> i;
#define A2IN(T1, a, T2, b, n) \
    vector<T1> a(n);          \
    vector<T2> b(n);          \
    REP(i, n)                 \
    cin >> a[i] >> b[i];
#define OUT(x) cout << (x) << endl;
#define FOUT(x) cout << fixed << setprecision(15) << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) \
    SORT(a);     \
    reverse(ALL(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define DUMPA(a)          \
    cout << #a << " = {"; \
    JOUT(ALL(a), ", ", cout) << "}" << endl;

template <typename T>
ostream &JOUT(T s, T e, string sep = " ", ostream &os = cout)
{
    if (s != e)
    {
        os << *s;
        ++s;
    }
    while (s != e)
    {
        os << sep << *s;
        ++s;
    }
    return os;
}

ostream &YES(bool cond, string yes = "Yes", string no = "No", ostream &os = cout)
{
    if (cond)
    {
        os << yes << endl;
    }
    else
    {
        os << no << endl;
    }
    return os;
}

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

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    os << '[';
    JOUT(ALL(v), ", ", os) << ']';
    return os;
}

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#pragma endregion template

int main()
{
    IN(ll, N);
    IN(ll, K);
    AIN(ll, p, N);
    double s = 0;
    REP(i, K)
    {
        s += (1 + p[i]) / 2.0;
    }
    double ans = s;
    for (ll i = K; i < N; ++i)
    {
        s += (p[i] - p[i - K]) / 2.0;
        CH(max, ans, s);
    }
    FOUT(ans);
}

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

using namespace std;
using ll = long long;

double expected_value(int n) {
  return (double)((1 + n) * n / 2) / (double)n;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<double> ps(n + 1);
  vector<double> comsum(n + 1, 0);
  int p;
  rep(i, n) {
    cin >> p;
    ps[i + 1] = expected_value(p);
    comsum[i + 1] = comsum[i] + ps[i + 1];
  }
  double mm = 0;
  for (int i = k; i <= n; i++) {
    mm = max(comsum[i] - comsum[i - k], mm);
  }
  printf("%.6f\n", mm);
  return 0;
}


#include <iostream>

using namespace std;

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

    int x;
    cin >> x;
    long min, max, sum;
    min = max = sum = x;
    for (int i = 1; i < n; ++i)
    {
        cin >> x;
        if (x < min) min = x;
        if (max < x) max = x;
        sum += x;
    }
    cout << min << ' ' << max << ' ' << sum << endl;

    return 0;
}

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

int main(){
	int a,b,c, mn=100000; cin>> a >> b >> c;

	mn = min(b+a,mn);
	mn= min(b+c,mn);
	mn= min(a+c,mn);

	cout<<mn<<endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <cstdio>
#include <cmath>
#include <time.h>
#define all(c) ((c).begin(),(c).end())
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n)
#define vsort(v) sort(v.begin(),v.end())
#define vvsort(v) sort(v.begin(),v.end(),greater<int>())
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int n;
int main() {
	while (cin>>n) {
		if (n == 0)return 0;
		int x[21], y[21];
		for (int i = 0; i < n; i++) {
			cin >> x[i] >> y[i];
		}
		int m; cin >> m;
		char d[31];
		int l[31];
		bool a[21][21];
		for (int i = 0; i < 21; i++) {for (int j = 0; j < 21; j++) {a[i][j] = 0;}}
		a[10][10] = 1;
		for (int i = 0; i < m; i++) {cin >> d[i] >> l[i];}
		int cont = 0;
		int nowx = 10;
		int nowy = 10;
		while (cont < m) {
			if (d[cont] == 'N' || d[cont] == 'S') {
				if (d[cont] == 'N') {
					for (int i = nowy; i <= l[cont] + nowy; i++) {
						a[i][nowx] = 1;
					}
					nowy += l[cont];
				}
				else {
					for (int i = nowy; i >= nowy - l[cont]; i--) {
						a[i][nowx] = 1;
					}
					nowy -= l[cont];
				}

			}
			else {
				if (d[cont] == 'E') {
					for (int i = nowx; i <= l[cont] + nowx; i++) {
						a[nowy][i] = 1;
					}
					nowx += l[cont];
				}
				else {
					for (int i = nowx; i >= nowx - l[cont]; i--) {
						a[nowy][i] = 1;
					}
					nowx -= l[cont];
				}
			}
			cont++;
		}
		//for (int i = 0; i < 21; i++) { for (int j = 0; j < 21; j++) { cout << a[i][j] << " "; if (j == 20)cout << endl; } }
		for (int i = 0; i < n; i++) {
			if (a[y[i]][x[i]] != 1) {cout << "No" << endl;break;}
			if (i == n - 1)cout << "Yes" << endl;
		}

	}
}

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

ll n,m,q;
ll a[505][505];
ll b[505][505];

int main(){
    cin >> n >> m >> q;
    for(ll i=0;i<m;i++){
        ll l,r;
        cin >> l >> r;
        a[r][l]++;
    }
    for(ll i=0;i<=500;i++){
        for(ll j=0;j<=500;j++){
            b[i][j+1]=b[i][j]+a[i][j];
        }
    }
    for(ll i=0;i<q;i++){
        ll c,d;
        cin >> c >> d;
        ll res=0;
        for(ll j=c;j<=d;j++){
            res+=b[j][d+1]-b[j][c];
        }
        cout << res << endl;
    }
}


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

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(s...)  fprintf(stderr, s)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }

int H, W;
char F[111][111];

int A[101][101];
short dp[101][101][101][101];
short box(int l, int u, int r, int d) {
    return A[d][r] - A[d][l] - A[u][r] + A[u][l];
}

int main() {
    scanf("%d%d", &H, &W);
    REP (i, H) scanf("%s", F[i]);

    int si, sj;

    REP (i, H) REP (j, W) {
	if (F[i][j] == 'E') {
	    si = i;
	    sj = j;
	}
	A[i+1][j+1] = A[i][j+1] + A[i+1][j] - A[i][j];
	if (F[i][j] == 'o') A[i+1][j+1] ++;
    }

    int ans = 0;
    REP (l, sj+1) REP (r, W-sj) REP (u, si+1) REP (d, H-si) {
	short cur = dp[l][r][u][d];
	amax(ans, (int)cur);

	int R, L, D, U;

	U = max(si-u, d); D = min(si+d+1, H-u);
	if (sj-1-l >= r && U < D) amax<short>(dp[l+1][r][u][d], cur + box(sj-1-l, U, sj-l, D));
	if (sj+1+r < W-l && U < D) amax<short>(dp[l][r+1][u][d], cur + box(sj+1+r, U, sj+2+r, D));

	L = max(sj-l, r); R = min(sj+r+1, W-l);
	if (si-1-u >= d && L < R) amax<short>(dp[l][r][u+1][d], cur + box(L, si-1-u, R, si-u));
	if (si+1+d < H-u && L < R) amax<short>(dp[l][r][u][d+1], cur + box(L, si+1+d, R, si+2+d));
    }

    printf("%d\n", ans);

    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll N, K;
    cin >> N >> K;
    vec X(N);
    REP(i,0,N) cin >> X[i];

    ll p = N;
    REP(i,0,N) {
        if(X[i] >= 0) {
            if(X[i] == 0) {
                X.erase(X.begin() + i);
                --K;
            }
            p = i;
            break;
        }
    }

    ll left, right;
    if(p < K) {
        left = 0;
        right = K - 1;
    }
    else {
        left = p - K;
        right = p - 1;
    }

    ll m = INF;
    if(K == 0) m = 0;
    else {
        while(left <= p && right < N) {
            ll t;
            if(X[left] < 0 && X[right] < 0) t = -X[left];
            else if(X[left] < 0) t = MIN(X[right]-2*X[left],X[right]*2-X[left]);
            else t = X[right];
            m = MIN(m,t);
            ++left;
            ++right;
        }
    }
    PR(m);

    return 0;
}

/*

*/


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>

using namespace std;

#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef complex<int> P;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int n = 151;
vector<vector<int>> a(n, vector<int>(n));

void init(){
	for(int i=1;i<n;++i) for(int j=i+1;j<n;++j){
		a[i][j] = i * i + j * j;
	}
}

bool solve(){
	int h, w;
	cin>> h>> w;
	if(!h && !w) return false;

	int x = h * h + w * w;
	pii ans(n, n);
	for(int i=1;i<n;++i){
		for(int j=i+1;j<n;++j){
			int y = i*i + j*j;
			if((i <= h && y == x) || y < x) continue;
			if(y < ans.first * ans.first + ans.second * ans.second){
				int j = (int)sqrt(y - i * i);
				ans = pii(i, j);
			}
		}
	}
	cout<< ans.first<< " "<< ans.second<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed); cout.precision(10);
	init();
	while(solve());
	return 0;
}

#include <iostream>
using namespace std;

class rect {
public:
  int h,w;
  // 対角線の2乗
  int diagonal() {
    return h * h + w * w;
  }
};

// x > y なら1を返す
int rect_cmp(rect x, rect y) {
  if(x.diagonal() > y.diagonal()) return 1;
  else if(x.diagonal() == y.diagonal()) {
    if(x.h > y.h) return 1;
    else return 0;
  }
  else return 0;
}

int main() {
  rect r;
  while(cin >> r.h >> r.w && r.h && r.w) {
    rect ans, cmp;
    ans.w = 150; ans.h = 150;
    for(cmp.h = 1; cmp.h < 150; ++cmp.h) {
      for(cmp.w = cmp.h + 1; cmp.w < 150; ++cmp.w) {
        if(rect_cmp(cmp, r) && rect_cmp(ans, cmp)) ans = cmp;
      }
    }
    cout << ans.h << ' ' << ans.w << endl;
  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include <stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<vector>
#include <set>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define inf 0x3f3f3f3f
#define pi 3.1415926535898
using namespace std;
char a[5000];
ll x,y,z;

int main()
{
     int n,k;
     cin>>n;
     for(int i=0;i<n;i++)
     {
         cin>>a[i];
         if(a[i]=='R')
         {
             x++;
         }
         else if(a[i]=='G')
         {
             y++;
         }
         else
         {
             z++;
         }
     }
     ll ans=x*y*z;
     for(int i=0;i*2<n;i++)
     {
         for(int j=0;j+2*i<n;j++)
         {
             if(a[j]!=a[j+i]&&a[j+i]!=a[j+2*i]&&a[j]!=a[j+2*i])
             {
                 ans--;
             }
         }
     }
     cout<<ans<<endl;
    return 0;
}


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

typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { //最大公約数
  if (b==0) return a;
  else return gcd(b, a%b);
}

ll lcm(ll a, ll b) { //最小公倍数
  return a * b / gcd(a, b);
}

ll digit_sum(ll a){ //10進桁和
  ll ans=0;
  while(a>0){
    ans+=a%10;
    a/=10;
  }
  return ans;
}

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

#include <stdio.h>

int main()
{
    char s[12];
    scanf("%s", &s);
    s[3]++;
    printf("%s\n", s);
    return 0;
}


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

using namespace std;

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

    vector<int> s(n), t(n), x(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i] >> t[i] >> x[i];
    }

    vector<pair<int, int>> sx(n), tx(n);
    for (int i = 0; i < n; i++) {
        sx[i] = make_pair(s[i] - x[i], x[i]);
        tx[i] = make_pair(t[i] - x[i], x[i]);
    }

    sort(sx.begin(), sx.end());
    sort(tx.begin(), tx.end());

    multiset<int> u;
    vector<pair<int, int>>::iterator isx = sx.begin(), itx = tx.begin();

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

        for (; isx != sx.end() && isx->first <= d; isx++) {
            u.insert(isx->second);
        }
        for (; itx != tx.end() && itx->first <= d; itx++) {
            u.erase(u.lower_bound(itx->second));
        }

        cout << (u.size() == 0 ? -1 : *u.begin()) << endl;
    }
}

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

using namespace std;

struct edge {int u, v, cost; };

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int main(void) {
    int V, E;
    cin >> V >> E;
    UnionFind uf(V);
    vector<edge> es;
    for (int i = 0; i < E; i++) {
        int u, v, cost;
        cin >> u >> v >> cost;
        es.push_back(edge {u, v, cost});
    }
    sort(es.begin(), es.end(), comp);
    int res = 0;
    for (auto e: es) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    cout << res << endl;

    return 0;
}


#include <stdio.h>
#include <vector>

using namespace std;

int main(void){
	while(true){
		int n, m, t1, t2;
		scanf("%d\n%d", &n, &m);
		if(n == 0 && m == 0) return 0;

		vector< vector<int> > G;

		for(int i = 0;i < n;++i){
			vector<int> V;
			G.push_back(V);
		}

		for(int i = 0;i < m;++i){
			scanf("%d %d", &t1, &t2);
			G[t1-1].push_back(t2-1);
			G[t2-1].push_back(t1-1);
		}

		vector<int> guests(n, 0);

		for(int i = 0;i < G[0].size();++i){
			guests[G[0][i]] = 1;
			for(int j = 0;j < G[G[0][i]].size();++j){
				guests[G[G[0][i]][j]] = 1;
			}
		}

		int c = 0;
		for(int i = 1;i < n;++i){
			c += guests[i];
		}

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

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n,m,ans=0;
  cin >> n >> m;
  while(n!=0||m!=0){
    ans = 0;
    n++;
    vector<int>a(m,0),b(m,0),hito(n,0),one(n,0);
    rep(i,m){
      cin>>a[i]>>b[i];
      if(a[i]==1){
	hito[b[i]]++;
	one[b[i]]++;
      }
      if(b[i]==1){
	hito[a[i]]++;
	one[a[i]]++;
      }
    }
    rep(i,n){
      if(i==0||i==1)continue;
      rep(j,m){
	if(one[i]!=0){
	  if(i==a[j])hito[b[j]]++;
	  if(i==b[j])hito[a[j]]++;
	}
      }
    }
    rep(i,n){
      if(i==0||i==1)continue;
      if(hito[i]!=0){
	ans++;
      }
    }
    cout<<ans<<endl;
    cin >> n >> m;
  }
  return (0);
}


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

#define     fast()              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define     ll                  long long int
#define     test()              ll t; cin>>t; while(t--)
#define     lp0(i,begin,end)    for(ll i=begin;i<end;i++)
#define     lp1(i,begin,end)    for(ll i=begin;i<=end;i++)
#define     rlp(i,begin,end)    for(ll i=end;i>=begin;i--)
#define     prec(n)             fixed<<setprecision(n)
#define     initial(a,i)        memset(a,i,sizeof(a))
#define     pb                  push_back
#define     PI                  3.1415926535897932384626433832795
#define     MOD                 1000000007
#define     F                   first
#define     S                   second
#define     all(a)              (a).begin(),(a).end()
#define     BPC(x)              __builtin_popcountll(x)
#define     gcd(a,b)            __gcd(a,b)

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

ll power(ll x,ll n)
{
    ll result = 1;
    while (n)
    {
        if (n & 1)
            result = result * x;
        n = n / 2;
        x = x * x;
    }
    return result;
}
// Solution Function
void solution(ll compte)
{
    // cout<<compte<<"\n";
    ll n,d;
    cin>>n;
    ll arr[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    ll cnt =0;
    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            for(ll k=j+1;k<n;k++)
            {
                if(arr[i]!=arr[j]&&arr[j]!=arr[k]&&arr[k]!=arr[i]&&arr[i]+arr[j]>arr[k]&&arr[j]+arr[k]>arr[i]&&arr[k]+arr[i]>arr[j])
					cnt++;
            }
        }
    }
    cout<<cnt<<'\n';
}

// Driver Function
int main()
{
    ll compte = 1;
   // test()
    //{
        solution(compte);
        compte++;
    //}
    return 0;
}

#include <iostream>
#include <cstdio>

using namespace std;

int partition(int A[],int p,int r)
{
	int x = A[r];
	int i = p - 1;

	for (int j = p; j < r; j++) {
		if (A[j] <= x) {
			i++;
			int tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}

	int tmp = A[i + 1];
	A[i + 1] = A[r];
	A[r] = tmp;

	return i + 1;
}

int main(void)
{
	int A[100000];
	int n;
	int q;

	cin >> n;

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

	q = partition(A, 0, n - 1);

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

		if (i > 0) {
			putchar(' ');
		}

		if (i == q) {
			cout << '[' << A[i] << ']';
		}
		else {
			cout << A[i];
		}
	}

	putchar('\n');

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <map>
using namespace std;
typedef long long ll;

int ord[100010], low[100010];
int k;
vector<int> G[100010];
vector<pair<int, int>> bridge;
vector<int> articulation;
bool visit[100010] = {};

void dfs(int v, int p, int &k) {
    visit[v] = 1;
    ord[v] = k++;
    low[v] = ord[v];

    bool isArticulation = 0;
	int ct = 0;
    for (int i = 0; i < G[v].size(); i++) {
        if (!visit[G[v][i]]) {
            ct++;
            dfs(G[v][i], v, k);
            low[v] = min(low[v], low[G[v][i]]);
            if (~p && ord[v] <= low[G[v][i]]) isArticulation = 1;
            if (ord[v] < low[G[v][i]])
                bridge.emplace_back(min(v, G[v][i]), max(v, G[v][i]));
        }
        else if (G[v][i] != p) {
            low[v] = min(low[v], ord[G[v][i]]);
        }
    }
    if (p == -1 && ct > 1) isArticulation = 1;
	if (isArticulation) articulation.push_back(v);
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    int k = 0;
	for (int i = 0; i < n; i++){
		if (!visit[i]) dfs(i, -1, k);
	}
    sort(bridge.begin(), bridge.end());
	sort(articulation.begin(), articulation.end());

    for (int i = 0; i < bridge.size(); i++) {
        //cout << bridge[i].first << " " << bridge[i].second << endl;
    }
    for (int i = 0; i < articulation.size(); i++) {
        cout << articulation[i] << endl;
    }
}

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

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

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

class Graph {
  vector<Edges> g;

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

template<typename T> vector<T> nub(const vector<T> &xs) {
  set<T> s(xs.begin(), xs.end());
  vector<T> ys(s.begin(), s.end());
  return ys;
}

vector<int> articulationPoints(const Graph &g) {
  int n = g.size(), id = 0;
  vector<int> ord(n), low(n), arts;
  function<void(int)> dfs = [&](int v) {
    low[v] = ord[v] = ++id;
    for (auto &e : g[v]) {
      int w = e.dst;
      if (ord[w] == 0) {
        dfs(w);
        cmin(low[v], low[w]);
        if (ord[v] == 1 && ord[w] != 2) arts.emplace_back(v);
        if (ord[v] != 1 && ord[v] <= low[w]) arts.emplace_back(v);
      } else {
        cmin(low[v], ord[w]);
      }
    }
  };
  rep(i, n) if (ord[i] == 0) id = 0, dfs(i);
  return arts;
}

main {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, m) {
    int s, t;
    cin >> s >> t;
    g.addEdge(s, t);
  }
  vector<int> arts = articulationPoints(g);
  arts = nub(arts);
  for (auto &v : arts) cout << v << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

class UnionFind {
public:
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

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

    ll size(ll x) {
        return siz[root(x)];
    }
};

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

int main() {
    ll x, y;
    cin >> x >> y;
    ll ans = 12345634537849;
    if (x <= y)ans = min(ans, y - x);
    if (-x <= y)ans = min(ans, y + x +1);
    if (x <= -y)ans = min(ans, - y - x+1);
    if (-x <= -y)ans = min(ans, - y + x + 2);
    cout << ans << endl;
}


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

int main(){
	int N,Q;
	while(1){
	cin>>N>>Q;if(N==0&&Q==0)break;
	int n;
	int d[10002]={};
	for(int i=0;i<N;i++)
	{cin>>n;
	for(int j=0;j<n;j++)
	{int x;	cin>>x;d[x]++;}
	}
	int c=0;int p=0;
	for(int i=0;i<10001;i++)
		if(d[i]>=Q&&d[i]>c){c=d[i];p=i;}

	cout<<p<<endl;
	}
	return 0;
}

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

int n, q;

const long long inf = (1ll << 31) - 1;
const int max_n = (1 << 17);

long long seg[max_n * 2 - 1];
long long lazy[max_n * 2 - 1];

void init()
{
  int _n = n;

  n = 1;
  while (n < _n) n *= 2;

  fill(seg, seg + 2 * n - 1, inf);
  fill(lazy, lazy + 2 * n - 1, inf + 1);
}

void update(int a, int b, int x, int k = 0, int l = 0, int r = n)
{
  if (a <= l && r <= b){
    lazy[k] = x;
    return;
  }
  if (r <= a || b <= l){
    return;
  }

  if (lazy[k] != inf + 1) {
    lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
  }
  lazy[k] = inf + 1;
  update(a, b, x, k * 2 + 1, l, (l + r) / 2);
  update(a, b, x, k * 2 + 2, (l + r) / 2, r);
}

void find(int a, int b, int k = 0, int l = 0, int r = n)
{
  if (a <= l && r <= b){
    if (lazy[k] != inf + 1){
      seg[k] = lazy[k];
    }
    lazy[k] = inf + 1;
    return;
  }
  else if (r <= a || b <= l){
    return;
  }
  else {
    if (lazy[k] != inf + 1){
      lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
      lazy[k] = inf + 1;
    }
    find(a, b, k * 2 + 1, l, (l + r) / 2);
    find(a, b, k * 2 + 2, (l + r) / 2, r);
  }
}

int main()
{
  cin >> n >> q;
  init();
  for (int i = 0; i < q; i++){
    int f;
    cin >> f;

    if (f == 0){
      int s, t, x;

      cin >> s >> t >> x;
      update(s, t + 1, x);
      /*
      for (int i = 0; i < n * 2 - 1; i++){
        cout << lazy[i] << " ";
      }
      puts("");
      */
    } else {
      int i;

      cin >> i;
      find(i, i + 1);

      /*
      for (int i = 0; i < n * 2 - 1; i++){
        cout << seg[i] << " ";
      }
      puts("");
      */
      cout << seg[i + n - 1] << 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<ll, ll>;

const ll INF = 1LL << 62;
const double PI = acos(-1);
const double eps = 1e-10;

int main() {
  vector<string> dow = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
  string s;
  cin >> s;
  cout << 7 - distance(dow.begin(), find(dow.begin(), dow.end(), s)) << endl;
  return 0;
}

#include <iostream>
#include <string>

int main() {
std::string str;
std::cin >> str;
  if (str == "MON") {
    std::cout << 6;
  }
  if (str == "TUE") {
    std::cout << 5;
  }
  if (str == "WED") {
    std::cout << 4;
  }
  if (str == "THU") {
    std::cout << 3;
  }
  if (str == "FRI") {
    std::cout << 2;
  }
  if (str == "SAT") {
    std::cout << 1;
  }
  if (str == "SUN") {
    std::cout << 7;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, b, a) for (int i = a - 1; i >= b; i--)
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dxx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, dyy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
    // 解説AC
    int n;
    cin >> n;
    pint red[n], blue[n];
    rep (i, 0, n) cin >> red[i].first >> red[i].second;
    rep (i, 0, n) cin >> blue[i].first >> blue[i].second;
    sort (red, red + n);
    sort (blue, blue + n);
    bool chk[n] = {};
    int ans = 0;
    // まず、redの点を固定する
    per (i, 0, n) {
        int res = -1;
        int now = inf;
        per (j, 0, n) {
            if (chk[j]) continue;
            if (red[i].first < blue[j].first &&
                red[i].second < blue[j].second) {
                // 条件に当てはまる中でblueのyが最も小さいのを選ぶ
                if (now > blue[j].second) {
                    res = j;
                    now = blue[j].second;
                }
            }
        }
        if (res != -1) {
            chk[res] = 1;
            ans++;
        }
    }
    cout << ans << "\n";
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9+7;

long double dist(ll x1, ll y1, ll x2, ll y2){
    return sqrt((double)pow(x2-x1, 2) + (double)pow(y2-y1, 2));
}

int main(){
    ll N; cin >> N;
    vector<ll> X(N), Y(N); for(int i = 0; i < N; i++) cin >> X[i] >> Y[i];
    long double ans = 0.0;
    for(int i = 0; i < N-1; i++){
        for(int j = i+1; j < N; j++){
            ans += dist(X[i], Y[i], X[j], Y[j]) * 2.0;
        }
    }
    cout << setprecision(20) << ans / (long double) N << endl;;
}


#include <iostream>
#include <vector>

using namespace std;

char news[4]={'N','E','W','S'};
int to_x[4]={0, 1, -1, 0};
int to_y[4]={1, 0, 0, -1};

int main(){
    while(1){
        int gems;
        cin >> gems;
        if(gems==0) break;

        vector<vector<bool> > map(21,vector<bool>(21,false));
        int x,y;
        for(int i=0; i<gems; i++){
            cin >> x >> y;
            map[y][x] = true;
        }
        x = 10;
        y = 10;
        map[y][x] = false;

        int command, move;
        char direction;
        cin >> command;
        for(int i=0; i<command; i++){
            cin >> direction >> move;
            for(int j=0; j<4; j++){
                if(direction == news[j]){
                    for(int k=0; k<move; k++){
                        x+=to_x[j];
                        y+=to_y[j];
                        map[y][x] = false;
                    }
                }
            }
        }

        bool check = false;
        for(int i=0; i<=20; i++){
            for(int j=0; j<=20; j++){
                if(map[i][j] == true) check = true;
            }
        }
        if(check) cout << "No" << endl;
        else cout << "Yes" << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define repone(i,n) for(int i=1;i<(int)(n);++i)
#define repo(i,o,n) for(int i=o;i<(int)(n);++i)
#define Rep(j,n) for(int j=0;j<(int)(n);++j)
#define Repo(j,o,n) for(int j=o;j<(int)(n);++j)
#define Repone(j,n) for(int j=1;j<(int)(n);++j)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define int long long
#define pb(a) push_back(a)
#define eraser(v,i) (v).erase(v.begin()+(i))
#define pbpair(a,b) push_back(make_pair(a,b))
#define MOD 1000000007
#define INF 9223372036854775807
#define pairint pair<int,int>
signed main(){
  int x=0,y=0,z=0;
  string s;
  std::cin >> s>>x;
  rep(i,s.size()){
    if(i*x>s.size()-1)return 0;
    std::cout << s[i*x];
  }
}

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

int main(){
	int n,k;
	cin >> n >> k;
	int mask = 0;
	for(int i = 0; i < k; i ++) {
		int p;
		cin >> p;
		mask |= (1 << p);
	}

	for(int i = n; ; i ++) {
		int t = i, m = 0;
		while(t) m |= (1 << (t % 10)), t /= 10;
		if((mask & m) == 0) {
			cout << i << endl;
			break;
		}
	}

}


#include<iostream>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int n, q, inf=2e9, cnt;
vector<int> par ;
vector<pair<int,pair<int,int> > > v;
map<int,int> id ;
int tree[3000006];
void update(int node, int s, int e, int l, int r, int val)
{
    if(s>r||e<l)
        return ;
    if(s>=l && e<=r)
    {
        tree[node]=min(tree[node],val);
        return ;
    }
    int m=(s+e)/2;
    update(2*node,s,m,l,r,val);
    update(2*node+1,m+1,e,l,r,val);
}
int solve(int node, int s,int e, int pos)
{
    if(s==e)
        return tree[node];
    int m=(s+e)/2;
    tree[2*node]=min(tree[node],tree[2*node]);
    tree[2*node+1]=min(tree[node],tree[2*node+1]);
    return pos<=m ? solve(2*node,s,m,pos) : solve(2*node+1,m+1,e,pos);
}
int main()
{
    IO
    cin>>n>>q;
    for(int i=1; i<=3e6; i++)
        tree[i]=inf;
    for(int i=1; i<=n; i++)
    {
        int s, t, x;
        cin>>s>>t>>x;
        v.push_back({x,{s-x,t-x-1}});
        id[s-x]++;
        id[t-x-1]++;
    }
    for(int i=1; i<=q; i++)
    {
        int d;
        cin>>d;
        id[d]++;
        par.push_back(d);
    }
    for(auto i : id)
        id[i.first]=++cnt;
    for(int i=0; i<n; i++)
    {
        int l=id[v[i].second.first], r=id[v[i].second.second];
        update(1,1,cnt,l,r,v[i].first);
    }
    for(int i=0; i<q; i++)
    {
        int ans=solve(1,1,cnt,id[par[i]]);
        if(ans==inf)
            ans=-1;
        cout<<ans<<"\n";
    }
}

#include <bits/stdc++.h>

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

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

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

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

using ll = long long;

using namespace std;

struct Construction {
  ll s, t, x;
  bool operator<(const Construction& c) const { return s < c.s; }
};

struct Person {
  ll d, index;
  bool operator<(const Person& p) const { return d < p.d; }
};

int main() {
  in(ll, n, q);
  vector<Construction> c(n);
  rep(i, n) {
    cin >> c[i].s >> c[i].t >> c[i].x;
    c[i].s -= c[i].x;
    c[i].t -= c[i].x;
  }
  sort(all(c));
  vector<Person> p(q);
  rep(i, q) {
    cin >> p[i].d;
    p[i].index = i;
  }
  sort(all(p));

  multiset<ll> active;
  priority_queue<pair<ll, ll>> que;
  auto cit = c.begin();
  vector<ll> ans(q);
  for (const auto& pi : p) {
    dbg(pi.d, pi.index);
    while (cit != c.end() && cit->s <= pi.d) {
      dbg(cit->s, cit->t, cit->x);
      que.push({-cit->t, cit->x});
      active.insert(cit->x);
      ++cit;
    }
    while (!que.empty() && -que.top().first <= pi.d) {
      dbg(-que.top().first, que.top().second);
      active.erase(active.find(que.top().second));
      que.pop();
    }
    if (active.empty()) {
      ans[pi.index] = -1;
    } else {
      ans[pi.index] = *active.begin();
    }
  }
  rep(i, q) out(ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define pi 2*asin(1.0)

//template functions
/////////////////////////
long Comb(long N,long M){// nCm
long ans=1;
for(long i=N;i>N-M;i--){
    ans=ans*i;
}
for(long i=M;i>0;i--){
    ans=ans/i;
}
return ans;
}
long Perm(long N,long M){// nPm
long ans=1;
for(long i=N;i>N-M;i--){
    ans=ans*i;
}
return ans;
}
long GCM(long A,long B){
            long W,r;
    if(A<B){

        W=B;
        B=A;
        A=W;
    }
    while(A%B!=0){
        r=A%B;
        A=B;
        B=r;
    }
    return B;
}
long LCM(long A,long B){
    return (A*B)/GCM(A,B);
}

long upDiv(long a,long b){
  return (a+b-1)/b;
}

long fact(long N){
    if(N==0){
        return 1;
    }else{
        long ans=1;
        for(long i=1;i<N+1;i++){
        ans=ans*i;
        }
        return ans;
    }

}

/////////////////////////

//Answer

int main() {

long double N;
long double dis,sum=0.0,answer;
cin>>N;
vector<long double> x(N);
vector<long double> y(N);
for(long i=0;i<N;i++){
cin>>x.at(i);
cin>>y.at(i);
}
for(int i=0;i<N;i++){
    for(int j=i+1;j<N;j++){
      double dx=x.at(i)-x.at(j);
      double dy=y.at(i)-y.at(j);
        sum+=pow((dx*dx+dy*dy),0.5);
    }
}
answer=sum*2.0/N;

cout<< fixed << setprecision(10) << answer<<endl;

}

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

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int main() {
	int sum = 0;
	vector<int>as(4), bs(2);
	for (int i = 0; i < 4; ++i) {
		cin >> as[i];
		sum += as[i];
	}
	cin >> bs[0] >> bs[1];
	sum += bs[0] + bs[1];
	sum -= *min_element(as.begin(), as.end()) + *min_element(bs.begin(), bs.end());
	cout << sum << endl;

	return 0;
}


#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

int dp[1000005][2];
int main() {
   cin.tie(0);
   ios_base::sync_with_stdio(0);

   string s;
   cin >> s;
   reverse(s.begin(), s.end());
   s += '0';
   int n = s.size();
   rep(i, 0, n + 1) rep(j, 0, 2) dp[i][j] = INF;
   dp[0][0] = 0;
   rep(i, 0, n) rep(j, 0, 2) {
      int x = s[i] - '0';
      x += j;
      rep(a, 0, 10) {
         int ni = i + 1, nj = 0;
         int b = a - x;
         if (b < 0) {
            nj = 1;
            b += 10;
         }
         dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
      }
   }
   int ans = dp[n][0];
   cout << ans << endl;
   return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main(){
	char a;
	int j = 0,o = 0,i = 0;
	int jlv = 0,olv = 0,ilv = 0;
	int mx = 0;
	while(scanf("%c",&a) != EOF){
        if(a == 10)break;
        if(a == 'J'){
            if(o != 0 || i != 0 || o != 0 && i != 0){
                j = o = i = jlv = olv = ilv = 0;
            }
            if(j == 0){
                j++;
            }
            else j++;
            //printf("j   j = %d o = %d i = %d jlv = %d olv = %d ilv = %d\n",j,o,i,jlv,olv,ilv);
        }
        if(a == 'O'){
            if(j != 0){
                jlv = j;
                j = 0;
            }
            if(i != 0){
                i = ilv = 0;
                jlv = 0;
            }
            if(o == 0) o++,olv = o;
            else o++,olv = o;
            //printf("o   j = %d o = %d i = %d jlv = %d olv = %d ilv = %d\n",j,o,i,jlv,olv,ilv);
        }
        if(a == 'I'){
            if(olv != 0){
                j = o = 0;
            }
            if(olv == 0)j = 0;
            if(jlv != 0 && olv != 0){
                j = o = 0;
                i++;
                ilv = i;
            }
            if(jlv >= olv && ilv >= olv)mx = max(mx,olv);
            //printf("i   j = %d o = %d i = %d jlv = %d olv = %d ilv = %d\n",j,o,i,jlv,olv,ilv);
        }
	}
	cout << mx << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl;
#define vvprint(vv) for(auto v:vv)vprint(v);

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

    ll H, W;
    cin >> H >> W;
    vll A(H), B(H);
    rep(i, H) cin >> A[i] >> B[i];

    map<int, int> m;
    rep(i, W) m[i] = 0;
    multiset<int> ms;
    rep(i, W) ms.insert(0);
    vll ans(H, 0);
    rep(i, H){
        int a = A[i]-1;
        int b = B[i]-1;
        auto lb = m.lower_bound(a);
        auto ub = m.upper_bound(b);
        if(lb!=m.end() && lb!=ub){
            --ub;
            auto it = lb;
            int k = ub->first;
            int v = ub->second;
            while(it!=m.end() && it->first!=k){
                int v = it->second;
                ms.erase(ms.find(v));
                it = m.erase(it);
            }
            if(b<W-1 && m.count(b+1)==0){
                m[b+1] = v+b-k+1;
                ms.insert(v+b-k+1);
            }
            m.erase(k);
            ms.erase(ms.find(v));
        }
        if(ms.empty()) ans[i] = -1;
        else ans[i] = *ms.begin() + i + 1;
    }
    rep(i, H) cout << ans[i] << endl;
}

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

int INF = 1e9;

struct Edge{
    /*辺の情報を入れる構造体で行き先とコストを表している*/
    int to;
    ll cost;
    Edge(int to, ll cost) : to(to), cost(cost) {}
};

vector<Edge> g[100005];
vector<ll> dist(100005);

void dfs(int u, int p){
    for(auto v : g[u]){
        if(v.to == p) continue;
        dist[v.to] = dist[u] + v.cost;
        dfs(v.to, u);
    }
}

int main(void){
    int n;
    cin>>n;
    rep(i, n-1){
        int a, b;
        ll c;
        cin>>a>>b>>c;
        --a;--b;
        g[a].push_back(Edge(b, c));
        g[b].push_back(Edge(a, c));
    }
    int q, k;
    cin>>q>>k;
    --k;
    vector<pair<int, int> > v(q);
    rep(i, q){
        int x, y;
        cin>>x>>y;
        --x;--y;
        v[i] = make_pair(x, y);
    }
    rep(i, n) dist[i] = INF;
    dist[k] = 0;
    dfs(k, -1);
    rep(i, q){
        cout<<dist[v[i].first] + dist[v[i].second]<<endl;
    }
    return 0;
}


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

int n,k;
const long long mod=1e9+7;
long long dp[3010][3010],jie[4000010],ni[4000010];
inline long long ksm(long long x,long long y)
{
	long long res=1;
	while(y)
	{
		if(y&1)
		res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
int main()
{
	scanf("%d%d",&n,&k);
	if(k==1)
	{
		printf("1\n");
		return 0;
	}
	jie[0]=1;
	for(int i=1;i<=n*k;++i)
	jie[i]=jie[i-1]*i%mod;
	ni[n*k]=ksm(jie[n*k],mod-2);
	for(int i=n*k-1;i>=0;--i)
	ni[i]=ni[i+1]*(i+1)%mod;
	for(int i=1;i<=n;++i)
	dp[i][0]=1;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=i;++j)
		{
			int x=n*k-i-(j-1)*(k-1)-1;
			dp[i][j]=(dp[i-1][j]+dp[i][j-1]*(n-j+1)%mod*jie[x]%mod*ni[k-2]%mod*ni[x-k+2]%mod)%mod;
		}
	}
	printf("%lld\n",dp[n][n]);
	return 0;
}

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

int main() {
	int h, w;
	cin >> h >> w;
	vector<vector<vector<int>>>grid(h + 2, vector<vector<int>>(w + 2, vector<int>(2, 0)));
	vector<vector<int>>blacks(0, vector<int>(2));
	for (int i = 1; i < h + 1; i++) {
		for (int j = 1; j < w + 1; j++) {
			char s; cin >> s;
			if (s == '.')grid.at(i).at(j).at(0) = 1;
			else {
				grid.at(i).at(j).at(0) = 2;
				blacks.push_back({ i,j });
			}
			grid.at(i).at(j).at(1) = -1;
		}
	}
	int ct = 1;
	vector<vector<int>>bw(0, vector<int>(2,0));
	int btmp = 0;
	int wtmp = 0;
	for (int i = 0; i < blacks.size(); i++) {
		int x = blacks.at(i).at(0);
		int y = blacks.at(i).at(1);
		if (grid.at(x).at(y).at(1) != -1)continue;
		queue<pair<int, int>>sq;
		sq.push({ x,y });
		while (sq.size() > 0) {
			int xq = sq.front().first;
			int yq = sq.front().second;
			grid.at(xq).at(yq).at(1) = ct;
			int cl = grid.at(xq).at(yq).at(0);
			if (cl == 1) {
				cl++;
				wtmp++;
			}
			else {
				cl--;
				btmp++;
			}
			vector<int>xqa = { xq,xq,xq - 1,xq + 1 };
			vector<int>yqa = { yq - 1,yq + 1,yq,yq };
			for (int i = 0; i < 4; i++) {
				if (grid.at(xqa.at(i)).at(yqa.at(i)).at(0) == cl&& grid.at(xqa.at(i)).at(yqa.at(i)).at(1)== -1) {
					grid.at(xqa.at(i)).at(yqa.at(i)).at(1) = ct;
					sq.push({ xqa.at(i),yqa.at(i) });
				}
			}
			sq.pop();
		}
		bw.push_back({ btmp, wtmp });
		btmp = 0;
		wtmp = 0;
		ct++;
	}
	ll ans = 0;
	for (int i = 0; i < bw.size(); i++)ans += (ll)bw.at(i).at(0) * bw.at(i).at(1);
	cout << ans << endl;
}


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

void solve() {
  int k;
  string s;
  cin >> k >> s;
  cout << ((s.size() <= k) ? s : (s.substr(0, k) + "...")) << endl;
}

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

#include<bits/stdc++.h>
using namespace std;
int main()
{
   string ch="...";
    int limit;
    while(cin>>limit)
    {
       string str;
       cin>>str;
       int size=str.size();
       if(size==limit || size<limit)
        {
          cout<<str<<'\n';
        }
        else
        {
            string temp;
            for(int i=0;i<limit;i++)
             {
                temp+=str[i];
             }
            temp+=ch;
            cout<<temp<<'\n';
        }
    }
}


/*
 * atcoder/abc092_2nd/c.cpp
 */

// C++ 14
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring> // memset
#include <cassert>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)
template <class T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template <class T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }

int N;
vector<int> J;
ll acc[100100] = {0};
void solve() {
  cin >> N;
  J.push_back(0);
  int j;
  loop(n,0,N) {
    cin >> j;
    J.push_back(j);
  }
  J.push_back(0);
  N+=2;
  loop(n,1,N) {
    acc[n] = acc[n-1] + abs(J[n]-J[n-1]);
  }
  //     n
  // 0 3 5 -1 0
  loop(n,1,N-1) {
    cout << acc[n-1] + abs(J[n-1]-J[n+1]) + (acc[N-1]-acc[n+1]) << endl;
  }
}
int main() {
  // cout.precision(15); cout << fixed;

  solve();

  return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for(long long i=0; i<n; i++)
#define REPR(i, n) for(long long i=n-1; i>=0; i--)
#define FOR(i, m, n) for(long long i=m; i<=n; i++)
#define FORR(i, m, n) for(long long i=m; i>=n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9+7;
const ll inf = 1e15;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}

char a[55][55];

int main(){FIN

    ll n;cin>>n;
    vll a(n);
    REP(i,n) cin>>a[i];
    ll dis=0;
    REP(i,n){
        if(i==0) dis += abs(a[0]);
        else dis += abs(a[i]-a[i-1]);
    }
    dis += abs(a[n-1]);

    REP(i,n){
        ll dis2=dis;
        if (i==0) {
            dis2 -= abs(a[0]);
            dis2 -= abs(a[1]-a[0]);
            dis2 += abs(a[1]);
        }
        else if (i==n-1){
            dis2 -= abs(a[n-1]);
            dis2 -= abs(a[n-2]-a[n-1]);
            dis2 += abs(a[n-2]);
        }
        else {
            dis2 -= abs(a[i-1]-a[i]);
            dis2 -= abs(a[i]-a[i+1]);
            dis2 += abs(a[i-1]-a[i+1]);
        }
        cout<<dis2<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;
bool isW(char c){
    return c=='W';
}
bool isX(char c){
    return c=='x';
}
int main(){//D問題
    int n;
    string s;
    cin >> n >> s;
    vector<char> t(n);
    char assum[4][2] = {{'S', 'S'}, {'S', 'W'}, {'W', 'S'}, {'W', 'W'}};
    for(int i=0;i<4;++i){
        t[0] = assum[i][0];
        t[1] = assum[i][1];
        for(int j=1;j<n-1;++j){
            t[j+1] = isW(t[j-1])^isX(s[j])^isW(t[j]) ? 'W' : 'S';
        }
        if(!(isW(t[n-2])^isX(s[n-1])^isW(t[n-1])^isW(t[0]) || isW(t[n-1])^isX(s[0])^isW(t[0])^isW(t[1]))){
            for(char c : t){
                cout << c;
            }
            return 0;
        }
    }
    cout << "-1";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n-1; i >= 0; i--)
#define all(x) x.begin(), x.end()
using ll = long long;
using pii = pair<int, int>;
const int mod = 1e9+7;

int main() {
    int n, T;
    cin >> n >> T;
    int t[n];
    rep(i, n) {
        cin >> t[i];
    }
    ll ans = 0;
    rep(i, n) {
        if (i+1 < n) {
            ans += min(T, t[i+1] - t[i]);
        } else {
            ans += T;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b<a) {a=b; return 1;} return 0;}

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

    int n, m;
    cin >> n >> m;
    int l1 = 1, r1 = m+1, l2 = m+2, r2 = 2*m+1;
    auto put = [](int &a, int &b) {
        cout << a << " " << b << endl;
        a++; b--;
    };
    rep(i, m) {
        if (l1 < r1) put(l1, r1);
        else put(l2, r2);
    }
}

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

template <typename TYPE>
void print_vec(const vector<TYPE>& v){
	for(int i=0; i<v.size(); i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
  cout << endl; cout << "       ";
  for(int i=0; i<v[0].size(); i++) cout << i << "   ";
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

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

	if(N % 2 == 1){
		for(int i=1; i<=M; i++){
			cout << i << " " << N-i+1 << endl;
		}
		return 0;
	}

	//以下Nが偶数の場合
	//b-a==N/2にならないように途中で1ずらして表示　例(N, M) = (6,2)で影響
	//あるいは 前回のa,bの差と 今回のa,bの差の合計がNにならないように途中で1ずらして表示 例(N, M) = (8, 3)で影響
	int tmp = 0;
	for(int i=1; i<=M; i++){
		if(tmp==0 &&  (N-i+1)-(i) == N/2) tmp++;
		if(tmp==0 && (N-i+2)-(i-1) + (N-i+1)-i == N) tmp++;
		cout << i << " " << N-i+1-tmp << endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int x,y, y2, z, z3, e;
	while(cin>>e&&e!=0){
		int min=1e6;
		for(y=0;y<=1e3;y++){
			y2=y*y;
			for(z=0;z<=1e2;z++){
				z3=z*z*z;
				x=e-y2-z3;
				if(min>x+y+z&&x>=0){
					min=x+y+z;
				}
			}
		}
		cout<<min<<endl;
	}
}

#include <iostream>
#include <cmath>

using namespace std;

namespace Solver {

    int e;
    inline bool Read() {
        cin >> e;
        return e != 0;
    }

    inline void Work() {
        int m = 1e+8;
        for (int z = 0; z * z * z <= e; z++) {
            int r = e - z * z * z;
            int y = (int)sqrt(r);
            int x = r - y * y;
            m = min(m, x + y + z);
        }

        cout << m << endl;
    }

    inline void Solve() {
        while(Read()) Work();
    }
}

int main() {
    Solver::Solve();
}


#include <iostream>

int main()
{

    int x;
    std::cin >> x;

    std::cout << x * x * x;
    std::cout << "\n";

    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define INF 9223372036854775807
#define all(x) (x).begin(), (x).end()

ll ts = 1000000007;

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