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

#define sz(x) int(x.size())
#define Task "abc"
#define For(i, a, b) for(int i = (a), _b = (b); i <= b; ++i)
#define All(x) (x).begin(), (x).end()
#define mp make_pair

typedef long long ll;

const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

int n, m;
ll gt[maxn], igt[maxn];
ll f[31][31627 * 2];

ll Pow(ll a, ll b){
  if (b == 0) return 1;
  ll tmp = Pow(a, b / 2);
  tmp = (tmp * tmp) % mod;
  if (b % 2) return (tmp * a) % mod;
  return tmp;
}

void init(){
  const int maxp = 1e5;
  gt[0] = 1;
  for (int i = 1; i <= maxp; ++i)
    gt[i] = (gt[i-1] * i) % mod;

  igt[maxp] = Pow(gt[maxp], mod - 2);

  for (int i = maxp - 1; i >= 0; --i)
    igt[i] = (igt[i+1] * (i + 1)) % mod;
}

ll comb(int k, int n){
  return ( (gt[n] * igt[n - k]) % mod * igt[k]) % mod;
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
//  freopen(Task".inp", "r", stdin);  freopen(Task".out", "w", stdout);
  cin >> n >> m;
  if (m == 1){
    cout << 1; return 0;
  }
  vector<int> div;

  for (int i = 1; i * i <= m; ++i){
    if (m % i == 0){
      div.push_back(i);
      if (i * i != m) div.push_back(m / i);
    }
  }

  int len = div.size();
  unordered_map<int, int> Pos;
  for (int i = 0; i < len; ++i)
    Pos[div[i]] = i;

  for (int i = 1; i < len; ++i)
    f[1][i] = 1;

  for (int k = 1; k < 30; ++k){
    for (int i = 1; i < len; ++i){
      for (int j = 1; j < len; ++j){
        if (div[j] % div[i] == 0 && div[j] / div[i] > 1)
          f[k+1][j] = (f[k+1][j] + f[k][i]) % mod;
      }
    }
  }

  init();
  ll ans = 0;
  for (int x = 1; x <= min(30, n); ++x){
    ans += f[x][Pos[m]] * comb(x, n);
    ans %= mod;
  }
  cout << ans;

}

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<utility>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ll> P;
typedef unsigned long long int ull;
const ll MOD=1e9+7;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;
const int MAXE=100000;
const int MAXV=10000;
const ll INF=2e9;

vector<ll> fac(10000000),inv(10000000),finv(10000000);
void initCOM(){
    fac[0]=fac[1]=1;
    inv[1]=1;
    finv[0]=finv[1]=1;
    for(int i=2;i<10000000;i++){
        fac[i]=fac[i-1]*i%MOD;
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}
ll COM(int n,int k){
    if(n<0||k<0) return 0;
    if(n-k<0) return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
vector<ll> prime_factor(ll m){
    vector<ll> res;
    for(int i=2;i*i<=m;i++)if(m%i==0){
        ll cnt=0;
        while(m%i==0){
            m/=i;
            cnt++;
        }
        res.push_back(cnt);
    }
    if(m>1) res.push_back(1);
    return res;
}
int main(){
    initCOM();
    ll n,m;cin>>n>>m;
    auto p=prime_factor(m);
    ll ans=1;
    for(auto b:p){
        ans=ans*COM(b+n-1,n-1)%MOD;
    }
    cout<<ans<<endl;
    return 0;
}


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

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

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

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

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

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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s;
	cin>>s;
	ll n=s.size();
	vvl dp(n+1,vl(2,LINF));
	dp[0][0]=0;
	dp[0][1]=1;
	rep(i,n){
		chmin(dp[i+1][0],min(dp[i][0]+(s[i]-'0'),dp[i][1]+10-(s[i]-'0')));
		chmin(dp[i+1][1],min(dp[i][0]+(s[i]-'0')+1,dp[i][1]+10-(s[i]-'0')-1));
	}
	cout<<dp[n][0]<<endl;
}

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

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

	int n = sqrt(N);

	cout << n * n << endl;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main()
{
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
  int n, m;
  cin >> n >> m;
  int a[n];
  int b[m];
  for(int i = 0;i < n;i++)
    cin >> a[i];
  for(int i = 0;i < m;i++)
    cin >> b[i];
  long long x_sum = 0, y_sum = 0;
  for(int i = 0;i < n - 1;i++){
    x_sum += 1ll * (i + 1) * (n - i - 1) % MOD * (a[i + 1] - a[i]) % MOD;
    if(x_sum >= MOD)
      x_sum -= MOD;
  }
  for(int i = 0;i < m - 1;i++){
    y_sum += 1ll * (i + 1) * (m - i - 1) % MOD * (b[i + 1] - b[i]) % MOD;
    if(y_sum >= MOD)
      y_sum -= MOD;
  }
  cout << x_sum * y_sum % MOD;
}

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

int main(){
  int n, x;
  cin >> n >> x;
  vector<int> an(n);
  for(int i=0; i<n; ++i) cin >> an[i];
  ll ans = 0;
  for(int i=0; i<n-1; ++i){
    int ca = max(0, an[i] +an[i+1] -x);
    ans += ca;
    an[i+1] = max(0, an[i+1] -ca);
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
long long s,ans;
int n,a[200003];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    	cin>>a[i];
    sort(a+1,a+1+n);
    for(int i=2;i<=n;i++)
	{
        s=(s+a[i-1])%1000000007;
        ans=(ans+s*a[i])%1000000007;
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long sum[200005];
int main()
{
    long long mod=1000000007;
    long long n;cin>>n;long long a[n];long long sum=0;
    for(long long i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
        sum=sum%mod;

    }

    long long pum=sum;long long rum=0;
    for(long long i=0;i<n-1;i++)
    {
        //cout<<sum[n-1]<<" "<<sum[i]<<" "<<a[i]<<endl;

       pum=pum-a[i];
       if(pum<0)
        pum+=mod;
       rum+=(a[i]*pum);
       rum=rum%mod;

    }
    cout<<rum;

}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <map>
#include <iomanip>

int main() {
	long long N, M; std::cin >> N >> M;
	std::map<long long, long long> C;
	std::vector<long long> D(N + 1);
	long long wa = 0;
	long long ac = 0;
	for (int i = 0; i < M; ++i) {
		long long c; std::string s;
		std::cin >> c >> s;
		if (s == "WA" && D[c] != 1) {
			D[c] = 5;
			++C[c];
			++wa;
		}
		if (s == "AC" && D[c] != 1) {
			D[c] = 1;
			++ac;
		}
	}
	for (int i = 0; i < N + 1; ++i) {
		if (D[i] == 5)
			wa -= C[i];
	}
	std::cout << ac << " " << wa << std::endl;

	return 0;
}

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

#define vt          vector
#define sz(x)       int((x).size())
#define all(x)      (x).begin(), (x).end()
#define rall(x)     (x).rbegin(), (x).rend()
#define fi          first
#define se          second

using ll = long long;
using pii = pair<int, int>;

void solve() {
    int n, m;
    cin >> n >> m;
    vt<int> p(n + 1), ac(n + 1);
    while (m--) {
        int x;
        string s;
        cin >> x >> s;
        if (s == "AC") ac[x] = 1;
        else if (!ac[x]) p[x]++;
    }

    int pen = 0;
    for (int i = 1; i <= n; i++) pen += p[i] * ac[i];
    cout << accumulate(all(ac), 0) << ' ' << pen;
}

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

    int tcs = 1;
    for (int tc = 1; tc <= tcs; tc++) {
        // cout << "Case " << tc << ": ";
        solve();
    }
}


#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(),(v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
// const ll INF = 1LL << 30;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec){
    rep(i, 0, sz(vec)){
        if(i) cout << " ";
        cout << vec[i];
    }
    coel;
}

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

int main() {
    ll d, g; cin >> d >> g;
    Vl p(d), c(d);
    rep(i, 0, d) cin >> p[i] >> c[i];
    ll ans = INF;
    for(int tmp = 0; tmp < (1 << d); tmp++){
        bitset<12> s(tmp);
        vector<bool> f(d, false);
        ll sum = 0;
        ll cnt = 0;
        rep(i, 0, 10){
            if(s.test(i)){
                f[i] = true;
                sum += p[i]*(i+1)*100 + c[i];
                cnt += p[i];
            }
        }
        if(sum >= g){
            chmin(ans, cnt);
            continue;
        }
        for(int i = d-1; i >= 0; i--){
            if(f[i]) continue;
            bool h = false;
            rep(j, 0, p[i]){
                sum += (i+1)*100;
                cnt ++;
                if(sum >= g){
                    h = true;
                    chmin(ans, cnt);
                    break;
                }
            }
            if(h) break;
        }
    }
    co(ans);

    return 0;
}

#ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define repl(i, a, b) for(ll i = a; i < (b); ++i)
#define repd(i, a, b) for(int i = a; i >= (b); --i)
#define repdl(i, a, b) for(ll i = a; i >= (b); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO

template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void writef(T ...tail) { write(tail...); cout << flush; }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  if (v.size()) {
    rep(i, 0, sz(v) - 1)
      out << v[i] << " ";
    out << v.back();
  }
  return out;
}
#endif
using namespace std;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

  int n; ll x;
  read(n, x);
  ll a[n];
  rep(i, 0, n) read(a[i]);
  ll ans = 0;
  if (a[0] > x) {
    ans += a[0] - x;
    a[0] = x;
  }
  rep(i, 1, n) {
    if (a[i] + a[i - 1] > x) {
      ll d = a[i] + a[i - 1] - x;
      a[i] -= d;
      ans += d;
    }
  }
  write(ans);
}


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

// ------ Class ------ //
class Graph2 {
public:
	// ------ Variables ------ //
	static const long long INF = 1000000000000000000;
	int V, E; vector<vector<pair<int, long long> > > G;

	// ------ Constructors ------ //
	Graph2() : V(0), E(0), G(vector<vector<pair<int, long long> > >()) {}
	Graph2(int v) : V(v), E(0), G(vector<vector<pair<int, long long> > >(v)) {}
	Graph2(vector<vector<pair<int, long long> > > g) : V(g.size()), G(g) { for (int i = 0; i < g.size(); i++) E += g[i].size(); }

	// ------ Basic Functions ------ //
	int size() { return V; }
	void add1(int v1, int v2, long long w) { G[v1].push_back(make_pair(v2, w)); E++; }
	void add2(int v1, int v2, long long w) { add1(v1, v2, w); add1(v2, v1, w); }

	// ------ Operators ------ //
	bool operator==(const Graph2& g) const { return G == g.G; }
	bool operator!=(const Graph2& g) const { return G != g.G; }
	vector<pair<int, long long> > operator[](int x) { return G[x]; }

	// ------ Algorithms ------ //
	long long minspan() {
		vector<long long> d(V, INF);
		vector<bool> used(V, false);
		priority_queue<pair<long long, int> > que;
		for (int i = 0; i < V; i++) d[i] = INF;
		d[0] = 0; que.push(make_pair(0, 0));
		while (!que.empty()) {
			pair<long long, int> pa = que.top(); que.pop();
			int u = pa.second; used[u] = true;
			for (int i = 0; i < G[u].size(); i++) {
				int v = G[u][i].first;
				if (d[v] > G[u][i].second && !used[v]) {
					d[v] = G[u][i].second; que.push(make_pair(-d[v], v));
				}
			}
		}
		long long sum = 0;
		for (int i = 0; i < V; i++) sum += d[i];
		return sum;
	}
};

// ------ Main ------ //
int n, a;
int main() {
	cin >> n; Graph2 g(n);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cin >> a;
			if(a != -1) g.add1(i, j, a);
		}
	}
	cout << g.minspan() << endl;
}

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

#define N 100

void searchIndex(int mat[][N], int n, int obj, int *row, int *col);
void changeGroup(int group[], int n, int g_old, int g_new);

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

	int group[N];
	for (int i = 0; i < n; i++) group[i] = i;

	int a[N][N];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int aij;
			cin >> aij;
			a[i][j] = aij;
		}
	}

	int e[N * N];
	int k = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			e[k++] = a[i][j];
		}
	}

	sort(e, e + k);

	int p = 0;
	while (e[p] == -1) p++;

	int cnt = 0;
	for (int q = p; q < k; q++) {
		int i, j;
		searchIndex(a, n, e[q], &i, &j);
		if (group[i] == group[j]) continue;
		cnt += e[q];
		if (group[i] > group[j]) changeGroup(group, n, group[i], group[j]);
		else changeGroup(group, n, group[j], group[i]);
	}

	cout << cnt << endl;

	return 0;
}

void searchIndex(int mat[][N], int n, int obj, int *row, int *col)
{
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (mat[i][j] == obj) {
				mat[i][j] = -1;
				*row = i;
				*col = j;
				return;
			}
		}
	}
}

void changeGroup(int group[], int n, int g_old, int g_new)
{
	for (int i = 0; i < n; i++) {
		if (group[i] == g_old) group[i] = g_new;
	}
}


//#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

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

    //全ての合計
    long long int sum = 0;
    for(int i = 1; i <= n; i++) sum += i;

    //３で割れる
    long long int group_3 = 0;
    for(int i = 3; i <= n; i += 3) group_3 += i;

    //５で割れる
    long long int group_5 = 0;
    for(int i = 5; i <= n; i += 5) group_5 += i;

    //３と５で割れる
    long long int group_3_5 = 0;
    for(int i = 15; i <= n; i += 15) group_3_5 += i;

    long long int ans = sum - group_3 - group_5 + group_3_5;
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

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

  int m,mi,ma;
  while(true){
    cin >> m >> mi >> ma;
    if(m==0) break;
    std::vector<int> v;
    for(int i=0;i<m;i++){
      int p;cin >> p;
      v.push_back(p);
    }
    sort(v.begin(),v.end(),greater<int>());
    int gap=-1,n=mi;
    for(int i=mi-1;i<ma;i++){
      int temp=v[i]-v[i+1];
      if(temp>=gap){
        gap=temp;
        n=i+1;
      }
    }
    cout << n << endl;
  }
}

//#pragma GCC optimize(2)
#include<cstdio>
#define L 1<<23
#define C (c=*A++)
#define ENS //__attribute__((optimize("-O2")))
#define NES //__attribute__((optimize("-O2"))) __inline__ __attribute__((always_inline))
#define N 1000001

int MAPLE,n,l[N],r[N],cnt,last,wt,ss[19];char fl[L],*A=fl,s[N],t[N];bool fg=true;
NES void read(int&x){char c;for(x=0;'0'>C||c>'9';);while('0'<=c&&c<='9')x=(x<<3)+(x<<1)+c-48,C;}NES void print(int x){if(!x)putchar(48);else{for(wt=0;x;ss[++wt]=x%10,x/=10);for(;wt;putchar(ss[wt--]+48));}}
ENS int main()
{int i,j;char c;
	for(fl[fread(fl,1,L,stdin)]=EOF,read(n);'a'>C||c>'z';);for(i=1;i<=n;s[i]=c,C,i++);while('a'>C||c>'z');for(i=1;i<=n;t[i]=c,C,i++);
	for(last=i=n,j=n+1;i;fg&=s[i]==t[i],i--)
		if(t[i]!=t[i-1])
		{if((--j)>i)j=i;
			for(;j&&s[j]!=t[i];j--);if(!j)return puts("-1"),0;
			l[++cnt]=j,r[cnt]=last,last=i-1;
		}if(fg)return puts("0"),0;for(l[0]=n+1,i=1;i<=cnt;i++)for(;l[i-MAPLE]<r[i]+MAPLE;MAPLE++);print(MAPLE);
}


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

int main(void) {
    int N,K,cnt,sum,minsum,minsumtemp;
    cin >> N >> K;

    vector<int> x(N);
    vector<int> diff(N-1);
    for(int i=0;i<N;i++) {
        cin >> x[i];
        if(i) diff[i-1] = x[i]-x[i-1];
    }

    cnt = 0;
    sum = 0;
    minsum = INT_MAX;
    for(int i=0;i<(K-1);i++) {
        sum += diff[i];
    }
    if(abs(x[K-1]) < abs(x[0])) {
        minsum = sum + abs(x[K-1]);
    } else {
        minsum = sum + abs(x[0]);
    }

    for(int i=(K-1);i<(N-1);i++) {
        sum += diff[i];
        sum -= diff[i-K+1];
        if(abs(x[i+1]) < abs(x[i-K+2])) {
            minsumtemp = sum + abs(x[i+1]);
        } else {
            minsumtemp = sum + abs(x[i-K+2]);
        }
        if(minsumtemp < minsum) {
            minsum = minsumtemp;
        }
    }

    cout << minsum << endl;

    return 0;
}

// includes
#include "bits/stdc++.h"
using namespace std;
// macros
#define ll long long
#define MOD  998244353 //1000000007 // 100000000  //
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define sz(x) ((ll)(x).size())
#define ft first
#define sd second
#define pb push_back
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repr(i, n) for(ll i = n-1; i >= 0; i--)
#define itr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define mem(a, b) memset(a, (ll)b, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define edge(v, x, y) v[x].pb(y); v[y].pb(x);
#define popc __builtin_popcount
#define ANS(s) {cout << s << "\n"; return;}
// functions
template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), c.end());}
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} 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 T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
// constants
const ll N = 1e5+5, M = 1e6+5, A = 1e7+5, inf = 1e9;
const long long linf = 1LL << 60;
const double er = 1e-10, pi = 3.141592653589793238463;
const ll lx[4] = {0, 1, -1, 0};
const ll ly[4] = {1, 0, 0, -1};
const ll dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const ll dy[8] = {1, -1, 0, 0, 1, 1, -1, -1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20);}
} fast_io_;

void solve()
{
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n); cin >> a;
    ll ans = inf;
    ll l = 0, r = m-1;
    rep(i, n)
    {
        if(a[r] <= 0){
            ans  = min(ans, abs(a[l]));
        }
        else if(a[l] >= 0){
            ans = min(ans, a[r]);
        }
        else{
            ans = min({ans, abs(a[l])*2 + a[r], abs(a[l]) + a[r]*2 });
        }//cout << ans << " ";
        l++, r++;
        if(r == n) break;
    }
    cout << ans;
}

int main(int argc, char const* argv[])
{
    ll t = 1; //cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}


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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30

using namespace std;
typedef long long ll;

int main()
{
    int n;
    while(cin >> n && n) {
        int ans = INF;
        rep(i,101) {
            if(i*i*i > n) break;

            rep(j,1001) {
                if(i*i*i + j*j > n) break;
                ans = min(ans,i+j+(n - i*i*i - j*j));
            }
        }

        cout << ans << endl;
    }

    return 0;
}

#include <iostream>
#include <iomanip>
#include<math.h>
#include<list>
#include <algorithm>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<string>
#include <numeric>
#include <queue>
#include <sstream>
#include <bitset>
#include<stack>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template<typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T>
map<T, T> getPrimeFactor(T n) {
	map<T, T> res;
	for (T i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			res[i] ++;
			n /= i;
		}
	}
	if (n != 1) res[n] = 1;
	return res;
}

template<typename T>
bool IsPrimeNumber(T num)
{
	if (num <= 2) return true;
	else if (num % 2 == 0) return false;

	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}

	return true;
}

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

#define rep(i,s,e) for(ll i=s;i<e;i++)
#define repeq(i,s,e) for(ll i=s;i<=e;i++)

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

	stack<bool> st;

	while (N != 0) {
		st.push(N % -2);
		N -=st.top();
		N /= -2;
	}
	if (st.empty()) {
		std::cout << 0;
	}
	while (!st.empty()) {
		std::cout << st.top();
		st.pop();
	}
	std::cout<<endl;

	return 0;
}


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

int main()
{
        string s, tmp, str, commands;
        stringstream ss;
        vector<string> dice;

        getline(cin, s);

        ss << s;

        while(getline(ss, str, ' ')) {
                dice.push_back(str);
        }

        cin >> commands;

        for(int i = 0; i < commands.length(); i++) {
                switch(commands[i]) {
                        case 'N':
                                tmp = dice[0];
                                dice[0] = dice[1];
                                dice[1] = dice[5];
                                dice[5] = dice[4];
                                dice[4] = tmp;
                                break;
                        case 'E':
                                tmp = dice[0];
                                dice[0] = dice[3];
                                dice[3] = dice[5];
                                dice[5] = dice[2];
                                dice[2] = tmp;
                                break;
                        case 'W':
                                tmp = dice[0];
                                dice[0] = dice[2];
                                dice[2] = dice[5];
                                dice[5] = dice[3];
                                dice[3] = tmp;
                                break;
                        case 'S':
                                tmp = dice[0];
                                dice[0] = dice[4];
                                dice[4] = dice[5];
                                dice[5] = dice[1];
                                dice[1] = tmp;
                                break;
                        default :
                                break;
                }
        }

        cout << dice[0] << endl;

        return 0;
}

#include <iostream>
#define rep(S, L) for (int i = S; i < L; ++i)
using namespace std;

int main() {
  string ss;
  cin >> ss;
  int s[10];
  for (int i = 0; i < ss.size(); ++i) {
    s[i] = ss[i] - (char)'0';
  }

  int diff = 1000;
  for (int i = 0; i < ss.size() - 2; ++i) {
    int n = s[i] * 100 + s[i + 1] * 10 + s[i + 2];
    diff = min(diff, abs(753 - n));
  }
  cout << diff << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz(x) (int)x.size()
#define int long long
typedef pair<int,int>pii;
typedef vector<int> vi;
typedef vector<vi> vii;
const int mxN=16,M=1e9+7;
//did u check when n=1?
//is mxN correct?
int n;
int  g[mxN][mxN];
int dp[(1<<16)],sum[(1<<16)];
void solve(int idx,int mask,int temp,vi& a,int k){
    if(idx==sz(a)){
        dp[mask]=max(dp[mask],k+sum[temp]);
        return;
    }
    solve(idx+1,mask,temp,a,k);
    solve(idx+1,mask^(1<<a[idx]),temp^(1<<a[idx]),a,k);
}
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>g[i][j];
        }
    }
    for(int mask=0;mask<=(1<<n)-1;mask++){
        for(int i=0;i<n;i++){
            if(!(mask & (1<<i)))continue;
            for(int j=i+1;j<n;j++){
                if((mask & (1<<j))){
                    sum[mask]+=g[i][j];
                }
            }
        }
    }
    for(int mask=0;mask<(1<<n);mask++){
        vi aux;
        for(int i=0;i<n;i++){
            if(!(mask & (1<<i)))aux.pb(i);
            solve(0,mask,0,aux,dp[mask]);
        }

    }
    cout<<dp[(1<<n)-1];

}

/*
	author: parv2809
*/

#include<bits/stdc++.h>
#define int long long int
#define mod 1000000007
#define inf 1e18
#define fo(i,y,n,inc)	for(int i=y;i<n+y;i+=inc)
#define cin(t) int t;cin>>t
#define w(t) while(t--)
#define nl cout<<endl;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ft(i) (i&(-1*i))
#define arrIn(arr,size)		for(int i=0;i<size;i++){cin>>arr[i];}
#define arrOut(arr,size,seperater)	for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;

int max(int a, int b) {
	if (a > b)
		return a;
	return b;
}

int min(int a, int b) {
	if (a < b)
		return a;
	return b;
}

int powmd(int a, int b) {
	if (b == 1)	return a;
	if (b == 0)	return 1;

	int half = powmd(a, b / 2);
	int ans = half * half;
	ans %= mod;
	if (b & 1) {
		ans *= a;
	}
	ans %= mod;
	return ans;
}

void fastIO() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
}

int mat[16][16];
int dp[(1 << 16)], sums[(1 << 16)];

int calculate(int mask) {
	int ans = 0;
	for (int i = 0; i < 17; i++) {
		if (mask & (1 << i)) {
			for (int j = i + 1; j < 17; j++) {
				if (mask & (1 << j)) {
					ans += mat[i][j];
				}
			}
		}
	}
	return ans;
}

void pre_compute_sums() {
	for (int i = 0; i <= (1 << 16); i++) {
		sums[i] = calculate(i);
		dp[i] = inf;
	}
}

int solve(int mask) {
	if (mask == 0)
		return 0;
	if (dp[mask] != inf)
		return dp[mask];

	int s = mask;
	int ans = 0;

	while (s > 0) {
		ans = max(ans, sums[s] + solve(mask - s));
		s = (s - 1)&mask;
	}

	return dp[mask] = ans;
}

int32_t main() {

	fastIO();
	int t = 1;
	w(t) {
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> mat[i][j];
			}
		}
		pre_compute_sums();
		cout << solve((1 << n) - 1);
		nl;
	}

	return 0;
}

// cout << "Case #" << i << ": " << answer << endl;


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

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

void MAIN() {
  string s;
  cin >> s;
  set<char> d;
  for (char c = 'a'; c <= 'z'; c++) d.insert(c);
  for (auto &i : s) d.erase(i);
  if (d.size()) {
    s += *d.begin();
    cout << s << '\n';
    return;
  }
  set<char> g;
  while (s.size()) {
    char c = s.back();
    s.pop_back();
    auto it = g.upper_bound(c);
    if (it != g.end()) {
      s += *it;
      cout << s << '\n';
      return;
    }
    g.insert(c);
  }
  cout << -1 << '\n';
}

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

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define MOD 1000000007ULL;
#define rep(i,n) for(int i=0;i<n;i++)

int main() {
	string S;
  	cin>>S;
  	bool alpha[26];
  	string ans;
  	rep(i,26) alpha[i]=false;
  	rep(i,S.size()){
		alpha[S[i]-'a']=true;
    }

  	if(S.size()<26){
     	 rep(i,26){
			if(alpha[i]==false){
             	ans=S;
              	ans.push_back('a'+i);
              	break;
            }
         }
    }else if(S=="zyxwvutsrqponmlkjihgfedcba"){
     	ans="-1";
    }else{
     	string T=S;
      	next_permutation(S.begin(),S.end());
    	int j=0;
      	while(S[j]==T[j]){
         	ans.push_back(S[j]);
          	j++;
        }
      	ans.push_back(S[j]);
    }

  	cout<<ans<<endl;
}


#include<iostream>
using namespace std;

struct Test{
  int A[3];
  bool R;
};

Test tests[1000];
int info[301], M[3], N;

int main()
{
  while(true)
  {
    for(int i = 0; i <= 300; i++)
      info[i] = 2;

    cin >> M[0] >> M[1] >> M[2];
    if(!(M[0] || M[1] || M[2]))
      break;
    cin >> N;
    for(int i = 1; i <= N; i++)
     cin >> tests[i].A[0] >> tests[i].A[1] >> tests[i].A[2] >> tests[i].R;

    bool b = true;
    while(b)
    {
      b = false;
      for(int i = 1; i <= N; i++)
      {
        if(tests[i].R)
        {
          for(int j = 0; j < 3; j++)
          {
            if(info[tests[i].A[j]] != 1)
            {
              info[tests[i].A[j]] = 1;
              b = true;
            }
          }
        }
        else
        {
          int tmp = 0;
          bool f = false;
          for(int j = 0; j < 3; j++)
          {
            tmp += info[tests[i].A[j]] == 1;
          }
          if(tmp == 2)
          {
            for(int j = 0; j < 3; j++)
            {
              if(info[tests[i].A[j]] == 2)
              {
                info[tests[i].A[j]] = 0;
                b = true;
              }
            }
          }
        }
      }
    }
    for(int i = 1; i <= M[0] + M[1] + M[2]; i++)
    {
       cout << info[i] << endl;
    }
  }
}

#include <stdio.h>

int main()
{
	int month, day, n;
	int md[12] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30,  31, 30};

	while (1) {
		scanf("%d %d", &month, &day);

		if (month == 0) break;
		n = 0;

		for (int i = 0; i < month; i++) {
			n += md[i];
		}
		n += day;

		switch (n%7) {
			case 0: printf("Wednesday\n"); break;
			case 1: printf("Thursday\n"); break;
			case 2: printf("Friday\n"); break;
			case 3: printf("Saturday\n"); break;
			case 4: printf("Sunday\n"); break;
			case 5: printf("Monday\n"); break;
			case 6: printf("Tuesday\n"); break;
		}
	}

	return 0;
}


#include <stdio.h>
#include <string.h>
int main() {
	int a, b;
	scanf("%d %d",&a,&b);
	char str[a+b+1];
	scanf("%s",&str);
	int len = strlen(str);
	int flag = 0;
	for (int i = 0; i < len; i++)
	{
		if (48 <= str[i] && str[i] <= 57)
		{
			continue;
		}
		else
		{
			if (i == a)
			{
				flag = 1;
			}
			else
			{
				flag = 0;
				break;
			}
		}
	}
	flag ? printf("Yes\n") : printf("No\n");
	return 0;
}

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

int main(){
    string h;
    cin >> h;
    if(h == "hi" || h == "hihi" || h == "hihihi" || h == "hihihihi" || h == "hihihihihi"){
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int MAXN = 50005;
const int MOD = 1000000007;

int n, x, y, z, cnt;
int p[10][MAXN], fin[10][MAXN], t[MAXN];
int dp[2][45][MAXN];
map <string, int> mp;
string r[MAXN];

vector <string> f (int x) {
    vector <string> res;
    if (x == 0) {
        res.push_back("");
        return res;
    }
    for (int i=0; i<x; i++) {
        vector <string> tmp = f(i);
        for (auto s : tmp) res.push_back(((char) (x - i + '0')) + s);
    }
    return res;
}

void ubaci (string s) {
    int len = s.size();
    int curr = 0;
    for (int i=0; i<len; i++) {
        int d = s[i] - '0';
        if (p[d][curr] == 0) p[d][curr] = ++cnt;
        curr = p[d][curr];
    }
    t[curr] = 1;
}

void dfs (string s, int br) {
    mp[s] = br;
    r[br] = s;
    for (int i=0; i<10; i++) {
        if (p[i][br]) dfs(s + ((char) (i + '0')), p[i][br]);
    }
}

void gen () {
    vector <string> sx = f(x), sy = f(y), sz = f(z);
    for (auto s1 : sx) {
        for (auto s2 : sy) {
            for (auto s3 : sz) {
                ubaci(s1 + s2 + s3);
            }
        }
    }
    mp[""] = 0;
    dfs("", 0);
    for (int i=0; i<=cnt; i++) {
        int len = r[i].size();
        for (int j=len; j>=0; j--) {
            string suf = r[i].substr(j);
            if (mp.find(suf) != mp.end()) {
                int ind = mp[suf];
                for (int k=0; k<10; k++) {
                    if (p[k][ind] != 0) fin[k][i] = p[k][ind];
                }
            }
        }
    }
}

int f (int flg, int pos, int br) {
    //for (int i=0; i<pos; i++) cout << " ";
    //cout << "bla " << flg << " " << pos << " " << br << endl;
    if (dp[flg][pos][br] != -1) return dp[flg][pos][br];
    if (pos == n) return flg;
    int res = 0;
    for (int i=0; i<10; i++) {
        res = (res + f(flg | t[fin[i][br]], pos + 1, fin[i][br])) % MOD;
    }
    return dp[flg][pos][br] = res;
}

int main () {
    memset(dp, -1, sizeof dp);
    cin >> n >> x >> y >> z;
    gen();
    cout << f(0, 0, 0);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

long long MOD = 1e9+7;
int main() {
  // U: 3^N fits, which hints towards a fancy subset DP
  // i.e. the one that is magically 3^n when it looks like
  // it should be 2^2n

  cin.tie(0);
  cin.sync_with_stdio(0);
  int n; cin >> n;

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

  // first, compute the costs of every subset (if grouped)
  long long costs[1<<n];
  for(int i = 0; i<(1<<n); ++i) {
    vector<int> in_group;
    for(int j = 0; j<n; ++j) {
      if((i >> j) & 1) {
        in_group.push_back(j);
      }
    }
    costs[i] = 0;
    for(int j = 0; j<in_group.size(); ++j) {
      for(int k = j+1; k<in_group.size(); ++k) {
        costs[i] += a[in_group[j]][in_group[k]];
      }
    }
  }

  // next, magic
  long long dp[1<<n];
  for(int i = 0; i<(1<<n); ++i) {
    dp[i] = LLONG_MIN;
  }
  dp[0] = 0;
  for(int i = 0; i<(1<<n); ++i) {
    // ugh I can't quite remember it...
    int j = i;
    do {
      j = (j - 1) & i; // go through all masks containing i as a submask...
      dp[i] = max(dp[i], dp[j] + costs[i^j]);
    } while (j != i);
  }

  cout << dp[(1<<n)-1] << endl;
}


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

int main() {
    int N, K; cin >> N >> K;
    int arr[N];
    int ans = 0;
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
    sort(arr, arr + N, greater<int>());
    for (int i = 0; i < K; i++) {
        ans += arr[i];
    }
    cout << ans << endl;
}

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

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

  int n, l, m=101;
  cin >> n >> l;

  for(int i=l; i<l+n; i++)m=min(abs(i), m);
  int ans=0;
  for(int i=l; i<l+n; i++){
    if(abs(i)==m)continue;
    ans+=i;
  }

  cout << ans << endl;

  return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;

int main(void){

	int n;
	int a, b, c;
	int mn;
	cin >> n;

	if (n == 2){
		cin >> a >> b;
		mn = min(a, b);
	}
	else{
		cin >> a >> b >> c;
		mn = min(a, min(b, c));
	}

	for (int i = 1; i <= mn; i++){
		if (n == 2){
			if (a%i == 0 && b%i == 0) cout << i << endl;
		}
		else if (a%i == 0 && b%i == 0 && c%i == 0) cout << i << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0)
//#define MULTI_TEST
//#define GOOGLE
#ifdef LOCAL
#include "debugger.h"
#else
#define db(...)
#endif
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define sz(v) (ll)(v).size()
#define fi first
#define se second
#define ln '\n'
const ll MOD = 1e9 + 7;
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }

void solve(int tc)
{
	int n, q;
	cin >> n >> q;
	map<ll, vector<p64>> hol;
	for (int i = 0; i < n; i++)
	{
		ll si, fi, xi;
		cin >> si >> fi >> xi;
		hol[xi].push_back({si, fi});
	}
	s64 peeps;
	map<ll, ll> ans;
	v64 D(q, 0);
	for (int i = 0; i < q; i++)
	{
		cin >> D[i];
		peeps.insert(D[i]);
	}
	for (auto itr : hol)
	{
		sort(itr.se.begin(), itr.se.end());
		for (auto x : itr.se)
		{
			ll si = x.fi, fi = x.se;
			ll xi = itr.fi;
			auto l = peeps.lower_bound(si - xi);
			auto r = peeps.upper_bound((2 * fi - 2 * xi - 1) / 2);
			if (r != peeps.begin())
				r--;
			else
				continue;
			queue<ll> rem;
			for (auto lol = l; lol != peeps.end() && *lol <= *r; lol++)
				ans[*lol] = xi, rem.push(*lol);
			while (sz(rem))
			{
				peeps.erase(rem.front());
				rem.pop();
			}
		}
	}
	for (ll x : D)
	{
		if (!ans.count(x))
			cout << -1 << ln;
		else
			cout << ans[x] << ln;
	}
	return;
}

int main()
{
#ifndef LOCAL
	FASTIO;
#endif
	int t = 1;
#ifdef LOCAL
	clock_t start, stop;
	start = clock();
#endif
#ifdef MULTI_TEST
	cin >> t;
#endif
	for (int tc = 1; tc < t + 1; tc++)
	{
#ifdef GOOGLE
		cout << "Case #" << tc << ": ";
#endif
		solve(tc);
	}
#ifdef LOCAL
	stop = clock();
	cerr << "Time :" << fixed << setprecision(5) << double(stop - start) / double(CLOCKS_PER_SEC) << "s" << ln;
#endif
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main() {

	int d, t, s, minute;
	cin >> d >> t >> s;

	if (d % s) {
		minute = d / s + 1;
	}
	else {
		minute = d / s;
	}

	if (minute <= t)
		std::cout << "Yes\n";
	else
		std::cout << "No\n";

	return 0;
}

#include<bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int k; cin>>k;
	string s; cin>>s;
	for(int i=0; i<min(k, (int)s.size());i++)	cout<<s[i];

	if((int)s.size() > k)
		cout<<"...";
	cout<<"\n";
	return 0;
}


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

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

using namespace std;
using ll = long long int;

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

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

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

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

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

//幅優先探索のテンプレ.デフォルトではstartからの距離を返す.
//graphは隣接リスト.startは探索開始地点
vector<int> bfs(vector<vector<int>> &graph, int start){
	queue<int> todo;
	vector<int> dist(graph.size(),-1);
	dist[start] = 0;
	todo.push(start);
	while(!todo.empty()){
		int now = todo.front();
		todo.pop();
		for (int i=0;i<graph[now].size();i++){
			int nxt = graph[now][i];
			if (dist[nxt] != -1) continue;
			dist[nxt] = dist[now] + 1;
			todo.push(nxt);
		}
	}
	return dist;
}

const int D = 80*160+10;
const int D2 = D*2;
typedef bitset<D2> bs;

int a[100][100];
bs dp[90][90];

int main() {
  int h, w;
  cin >> h >> w;
  rep(i,h)rep(j,w) {
    int x;
    cin >> x;
    a[i][j] = x;
  }
  rep(i,h)rep(j,w) {
    int x;
    cin >> x;
    a[i][j] = abs(x-a[i][j]);
  }

  dp[0][0][D-a[0][0]] = 1;
  dp[0][0][D+a[0][0]] = 1;
  rep(i,h)rep(j,w) {
    if (i) {
      dp[i][j] |= dp[i-1][j]<<a[i][j];
      dp[i][j] |= dp[i-1][j]>>a[i][j];
    }
    if (j) {
      dp[i][j] |= dp[i][j-1]<<a[i][j];
      dp[i][j] |= dp[i][j-1]>>a[i][j];
    }
  }
  int ans = D2;
  rep(i,D2) if (dp[h-1][w-1][i]) {
    ans = min(ans, abs(i-D));
  }
  cout << ans << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int size=1e5;
const int inf=1e9;
int main(){
	int H,W;cin>>H>>W;
	int A[H][W] = {}; int a;
	rep(i,0,H)rep(j,0,W){ cin>>a; A[i][j] += a; }
	rep(i,0,H)rep(j,0,W){ cin>>a; A[i][j] -= a; }
	bool dp[H+1][W+1][12801] = {}; dp[0][1][0] = 1;
	rep(i,1,H+1)rep(j,1,W+1){
        int t = abs(A[i-1][j-1]);
		rep(k,0,12801){
			if(k + t <= 12800) dp[i][j][k+t] |= dp[i-1][j][k] | dp[i][j-1][k];
            dp[i][j][abs(k - t)] |= dp[i-1][j][k] | dp[i][j-1][k];
		}
	}
	rep(i,0,12801){
		if(dp[H][W][i]){
            cout<<i<<endl;
            break;
        }
	}
}


#include <cstdio>

int main()
{
    int a,b,c,d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (a + b > c + d)
    {
        printf("Left");
    }

    else if (a + b < c + d)
    {
        printf("Right");
    }

    else if (a + b == c + d)
    {
        printf("Balanced");
    }
}

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


#include "bits/stdc++.h"
using namespace std;
//template <typename... T> void read(T& ... t){ ((cin >> t), ...); }
//template <typename... T> void write(T ... t){ ((cout << t), ...); }
//#define int long long
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define size(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define ln '\n'
#define space ' '
using LL = long long;
using pii = pair<int,int>;
const int INF = 2e9;
const int NN = 2e3;
const int MOD = 998244353;
namespace Mm{
	inline int mul(const LL &x, const LL &y) { return (x*y)%MOD; }
	inline int add(const LL &x, const LL &y) { return (x+y)%MOD; }
	inline int power(const LL &x, const LL &n){ if(n == 0) return 1; int y=power(x, n>>1); y=mul(y,y); return (n&1)?mul(y,x):y; }
	inline int inv(const LL &x){ return power(x, MOD-2); }
	// int _fact[2*NN] = {0};
	// inline int fact(const int &x){ return (x==0 ? 1 : (_fact[x]!=0 ? _fact[x] : _fact[x]=mul(x, fact(x-1)))); }
	// inline int cnk(const int &n, const int &k){ return (n<k)?0:mul(fact(n),inv(mul(fact(k),fact(n-k)))); }
	int _cnk[2*NN+1][2*NN+1] = {0};
	inline int cnk(const int &n, const int &k){ return (n<k)?0:(k==0)?1:(_cnk[n][k]!=0?_cnk[n][k]:_cnk[n][k]=add(cnk(n-1,k),cnk(n-1,k-1))); }
} using namespace Mm;
int n,k;
int dp[NN+1][NN+1];
int nbPairs[2*NN+1];

#define read(a,b) cin>>a>>b
#define write(a,b) cout<<a<<b

int32_t main(){ FAST;
	read(k,n);
	dp[0][0] = 1;
	for(int i=1; i<=NN; ++i){ // #pairs
		int prefSum = 0;
		for(int j=0; j<=NN; ++j){ // #items to distribute on pairs
			dp[i][j] = add(dp[i-1][j], add(prefSum, prefSum)); // k € [1;j]
			prefSum = add(prefSum, dp[i-1][j]);
		}
	}

	for(int i=1; i<=k; ++i) for(int j=i+1; j<=k; ++j) ++nbPairs[i+j];

	for(int i=2; i<=2*k; ++i){
		int ans = 0;
		int others = k - 2*nbPairs[i];

		if(i&1){
			if(others == 0){
				ans = dp[nbPairs[i]][n];
				write(ans, ln);
				continue;
			}

			for(int giveOthers=0; giveOthers<=n; ++giveOthers){
				int givePairs = n-giveOthers;
				ans = add(ans, mul(dp[nbPairs[i]][givePairs], cnk(others+giveOthers-1, giveOthers)));
			}
		}
		else{
			--others;
			if(others == 0){
				ans = add(dp[nbPairs[i]][n], dp[nbPairs[i]][n-1]);
				write(ans, ln);
				continue;
			}

			for(int giveOthers=0; giveOthers<=n; ++giveOthers){
				int givePairs = n-giveOthers;
				ans = add(ans, mul(dp[nbPairs[i]][givePairs], cnk(others+giveOthers-1, giveOthers)));
			}
			for(int giveOthers=0; giveOthers<=n-1; ++giveOthers){
				int givePairs = n-1-giveOthers;
				ans = add(ans, mul(dp[nbPairs[i]][givePairs], cnk(others+giveOthers-1, giveOthers)));
			}
		}

		write(ans, ln);
	}
}

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

#define VIC ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define wtf(s) freopen((s), "w", stdout)
#define rff(s) freopen((s), "r", stdin)
#define pi (2 * acos(0))
typedef long double ld;
typedef long long ll;
#define endl '\n'
#define matrix vector<vector<ll>>
ll fact[4009] , inv[4009]  , mod=998244353 , n,  k;
ll fp(ll x , ll exp)
{
    if(exp == 0)
        return 1;
    if(exp % 2 == 0)
        return fp((x * x) % mod, exp / 2) % mod;
    return ((fp((x * x) % mod, exp / 2) % mod) * x) % mod;
}
void calcFacAndInv(ll n)
{
    fact[0] = inv[0] = 1;
    for (ll i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}
ll ncr(ll n, ll r)
{
    return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod;
}

ll npr(ll n, ll r)
{
    return (fact[n] * inv[n - r]) % mod;
}
int main()
{
    VIC
    cin>>k>>n;
    calcFacAndInv(4000);
    for(ll i=2;i<=2*k;i++)
    {
        ll p=0, ans = ncr(n+k-1,k-1);
        for(int j=1;j<=i/2;j++)
            if(j <= k && i-j<=k)
                p++;
            for(int j=1;j<=p;j++)
            {
              //  cout<<j<<" "<<p<<endl;
              if(2*j>n)break;
                ll cur=ncr(p,j) * ncr(n-2*j+k-1,k-1);
                if(j%2==0)
                    ans+=cur;
                else
                    ans-=cur;
                ans %= mod;
                ans=(ans+ mod)%mod;
            }
        cout<<ans<<"\n";
    }
    return 0;
}


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

struct UnionFind {
    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);
}

vector<int> tpsort(vector<vector<int>>& G) {
    int V = G.size();
    vector<int> sorted_vertices;
    queue<int> que;
    vector<int> indegree(V);
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < G[i].size(); j++) {
            indegree[G[i][j]]++;
        }
    }
    for (int i = 0; i < V; i++) {
        if (indegree[i] == 0) {
            que.push(i);
        }
    }
    while (que.empty() == false) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < G[v].size(); i++) {
            int u = G[v][i];
            indegree[u] -= 1;
            if (indegree[u] == 0) que.push(u);
        }
        sorted_vertices.push_back(v);
    }
    return sorted_vertices;
}

struct Point
{
    double x;
    double y;
};

struct LineSegment
{
    Point start;
    Point end;
};

double tenkyori(const LineSegment& line, const Point& point)
{
    double x0 = point.x, y0 = point.y;
    double x1 = line.start.x, y1 = line.start.y;
    double x2 = line.end.x, y2 = line.end.y;

    double a = x2 - x1;
    double b = y2 - y1;
    double a2 = a * a;
    double b2 = b * b;
    double r2 = a2 + b2;
    double tt = -(a * (x1 - x0) + b * (y1 - y0));

    if (tt < 0)
        return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
    else if (tt > r2)
        return sqrt((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0));

    double f1 = a * (y1 - y0) - b * (x1 - x0);
    return sqrt((f1 * f1) / r2);
}

int main() {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll m = 0;
    ll ans = 1;
    for (int i = 0; i < 2 * n; i++) {
        if (i % 2 == 0 && s[i] == 'W' || i % 2 == 1 && s[i] == 'B') {
            ans *= m;
            m--;
            ans %= 1000000007;
        }
        else {
            m++;
        }
    }
    if (m != 0)cout << 0 << endl;
    else {
        for (ll i = 0; i < n; i++) {
            ans *= (i+1);
            ans %= 1000000007;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
struct Graph{
  Graph(int n):E(n),d(n){}
  vector<vector<tuple<int,int,int>>>E;
  vector<int>d;
  void add_edge(int a,int b,int c,int d){
    E[a].push_back(make_tuple(b,c,d));
  }
  void dijkstra(int s,int f){
    priority_queue<P,vector<P>,greater<P>>q;
    fill(d.begin(),d.end(),1e9);
    q.push({d[s]=0,s});
    while(q.size()){
      auto p=q.top();q.pop();
      if(p.first>d[p.second])continue;
      for(auto& a:E[p.second]){
        int x=get<0>(a),y=get<1>(a)+p.first;
        if(f)y=get<2>(a)+p.first;
        if(y<d[x])d[x]=y,q.push({y,x});
      }
    }
  }
};
main(){
  int n,m,a,b,c,d,k;
  while(cin>>n>>m,n){
    Graph E(m);
    for(int i=0;i++<n;E.add_edge(--a,--b,c,d),E.add_edge(b,a,c,d))cin>>a>>b>>c>>d;
    cin>>k;
    for(int i=0;i++<k;cout<<E.d[--b]<<endl){
      cin>>a>>b>>c;
      E.dijkstra(--a,c);
    }
  }
}

#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=1;i<=n;i++)
using namespace std;
typedef pair<int, int> P;

int c[100][100]={};
int t[100][100]={};

int main()
{
  int n,m,a,b;
  int i,j,k;
  int p,q,r;

  while(1){
    cin>>n>>m;
    if(n==0 && m==0)break;

    rep(i,m)rep(j,m)c[i][j]=t[i][j]=INF;//INFテ」ツ?ァテ・ツ按敕ヲツ慊淌・ツ個?
    rep(i,m)c[i][i]=t[i][i]=0;//テ・ツ青古ゥツァツ?」ツ?ッ0

    rep(i,n){
      cin>>a>>b;
      cin>>c[a][b]>>t[a][b];
      c[b][a]=c[a][b];
      t[b][a]=t[a][b];
    }

    rep(k,m)rep(i,m)rep(j,m){
      c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
      t[i][j]=min(t[i][j],t[i][k]+t[k][j]);
    }

    cin>>k;
    rep(i,k){
      cin>>p>>q>>r;
      if(r)
	cout<<t[p][q]<<endl;
      else
	cout<<c[p][q]<<endl;
    }
  }
}


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

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

template<class _T> static void getval(_T& a) {
	std::cin >> a;
}
template<class _T> static void getval(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void getval(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}

static void exec();

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

template <class _T> static _T tp_modpow(_T a, _T n, _T mod = (_T)1) {
	_T ans = 1;
	while (n > 0) {
		if (n & 1) {
			ans = (ans * a) % mod;
		}
		a = (a * a) % mod;
		n >>= 1;
	}
	return ans;
}

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

	int d0;
	getval(d0);

	std::vector<LLONG> count(N, 0);
	for (int i = 1; i < N; i++) {
		int di;
		getval(di);
		count[di]++;
	}

	if (d0 || count[0]) {
		printf("0\n");
		return;
	}
	count[0]++;

	LLONG ans = 1;
	for (int i = 1; i < N; i++) {
		LLONG val = tp_modpow(count[i - 1], count[i], MOD_NUM);
		ans = (ans * val) % MOD_NUM;
	}
	printf("%lld\n", ans);
}

#include <iostream>
#include <cmath>

using namespace std;

int main(){
  int N;
  cin >> N;
  int c[N], flg=0;
  for (int i = 0; i < N; i++){
    c[i] = 0;
  }

  for (int i = 0; i < N; i++){
    int tmp;
    cin >> tmp;
    if (i==0 && tmp==0){
      flg = 1;
    }

    c[tmp] += 1;
  }

  if (c[0]==1 && flg == 1) {
    unsigned long long ans = 1, tmp = 1;
    unsigned long long t = 998244353;
    for (int i = 2; i < N; i++){
      if (c[i-1] == 0 && c[i] > 0){
        ans = 0;
        break;
      }else if(c[i] == 0){
        continue;
      }

      tmp = c[i-1];
      for (int j = 2; j <= c[i]; j++){
        tmp = tmp % t;
        tmp = tmp * (c[i-1] % t);

      }
      ans = ans % t;
      tmp = tmp % t;
      ans *= tmp;
      ans %= t;
    }
    printf("%llu\n", ans);
  }else{
    printf("0\n");
  }
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
const string MININF = "-INF";
int match[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
string dp[MAXN]; // i本のマッチ棒を使ったときの最大数

void chmax(string &a, string b)
{
  if (a == MININF)
  {
    a = b;
  }
  else if (a.size() < b.size())
  {
    a = b;
  }
  else if (a.size() == b.size())
  {
    if (a < b)
    {
      a = b;
    }
  }
}

int main()
{
  int N, M, a;
  cin >> N >> M;
  vector<int> A(M);
  for (int i = 0; i < M; i++)
  {
    cin >> A.at(i);
  }
  for (int i = 0; i < MAXN; i++)
  {
    dp[i] = MININF;
  }
  dp[0] = "";
  for (int i = 0; i <= N; i++)
  {
    if (dp[i] == MININF)
      continue;
    for (int a : A)
    {
      chmax(dp[i + match[a]], dp[i] + (char)(a + '0'));
    }
  }
  cout << dp[N] << endl;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll dp[10010]; // i本使って作れる最大桁数
int num[10] = {0,2,5,5,4,5,6,3,7,6};
int main(){
    int n, m;
    cin >> n >> m;
    rep(i, 10010) dp[i] = -INF;
    dp[0] = 0;
    int a[m];
    rep(i,m) cin >> a[i];
    sort(a,a+m);
    reverse(a,a+m);

    rep(i, n+1){
        rep(j, m){
            if (i >= num[a[j]]) dp[i] = max(dp[i], dp[i-num[a[j]]] + 1);
        }
    }

    vector<char> ans;

    // rep(i, m) cout << a[i] << " ";
    // cout << endl;

    int i = n;
    while(i > 0){
        bool flag = false;
        rep(j, m){
            if (i - num[a[j]] < 0) continue;
            if (dp[i - num[a[j]]] == dp[i]-1){
                ans.push_back(a[j] + '0');
                i -= num[a[j]];
                // cout << "j=" << j << endl;
                flag = true;
                break;
            }
        }
        if (!flag) break;
    }

    rep(i, ans.size()) cout << ans[i];
    cout << endl;

    return 0;
}


#include<bits/stdc++.h>
#include<sstream>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

void call(int n){
  for(int i=1;i<=n;i++){
    stringstream ss;
    ss<<i;
    char c[8];
    ss>>c;
    if(i%3==0||strstr(c,"3"))
      cout<<" "<<i;
  }
  cout<<endl;
}

int main(){ _;
  int n;cin>>n;
  call(n);
}

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;

void call(int n) {
	int i = 1;
	do {
		int x = i;
		bool END_CHECK_NUM = false;
		if (x % 3 == 0) {
			cout << " " << i;
			END_CHECK_NUM = true;
		}
		do {
			if (!END_CHECK_NUM && x % 10 == 3) {
				cout << " " << i;
				END_CHECK_NUM = true;
			}
			x /= 10;
		} while (!END_CHECK_NUM && x);
	} while (++i <= n);

	cout << endl;
}

int main() {
	int n;
	cin >> n;
	call(n);
	return 0;
}


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

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

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

int main() {
    int a, b, m;
    cin >> a >> b >> m;
    vector<int> veca(a);
    vector<int> vecb(b);
    int minia = INF;
    int minib = INF;
    rep(i, a) {
        cin >> veca.at(i);
        minia = min(minia, veca.at(i));
    }
    rep(i, b) {
        cin >> vecb.at(i);
        minib = min(minib, vecb.at(i));
    }
    int mini = minia + minib;
    int dismini = INF;
    vector<vector<int> > dis(m, vector<int> (3));
    rep(i, m) {
        rep(j, 3) {
            cin >> dis.at(i).at(j);
        }
        dismini = min(dismini, veca.at(dis.at(i).at(0) - 1) + vecb.at(dis.at(i).at(1) - 1) - dis.at(i).at(2));
    }
    cout << min(dismini, mini) << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int) x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9+7;
const int INF = 2e9;
template<class T> void print(const T& t){ cout << t << endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main(){
  ll A, B, m;
  cin >> A >> B >> m;
  vl a(A);
  vl b(B);
  ll minia = INF;
  ll minib = INF;
  rep(i, A){
    cin >> a[i];
    chmin(minia, a[i]);
  }
  rep(i,B){
    cin >> b[i];
    chmin(minib, b[i]);
  }
  ll ans = minia + minib;
  rep(i, m){
    ll x, y, c;
    cin >> x >> y >> c;
    ll tmp = a[x - 1] + b[y - 1] - c;
    chmin(ans, tmp);
  }
  print(ans);
}


#include<bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {

    ll n,a; cin >> n;
    map<ll,ll> mp;
    set<ll> st;
    ll ans = 1;
    ll prev,val;
    bool f = false;

    rep(i,n){
        cin >> a;
        if(mp.count(a) == 0) mp[a] = 1;
        else mp[a] ++;
        st.insert(a);

        if(a == 0 && i != 0){
            cout << 0;
            return 0;
        }
        if(a == 0 && i == 0) f = true;
    }

    if(!f){
        cout << 0;
        return 0;
    }

    for(auto itr = st.begin(); itr != st.end(); ++itr) {
        if(*itr == 0) {
            prev = mp[*itr];
            val = *itr;
        }
        else{
            if(*itr - val != 1){
                cout << 0;
                return 0;
            }
            if(prev == 0){
                cout << 0;
                return 0;
            }
            rep(i,mp[*itr]){
                ans = (prev*ans)%998244353;
            }
            prev = mp[*itr];
            val = *itr;
        }
    }
    cout << ans;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e3+10;
const LL MOD=1e9+7;
int n,m,px[N*N],py[N*N];

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) {
		int x; scanf("%d",&x);
		if (px[x]) { puts("0"); return 0; }
		px[x]=1;
	}
	for (int j=1;j<=m;j++) {
		int y; scanf("%d",&y);
		if (py[y]) { puts("0"); return 0; }
		py[y]=1;
	}

	LL ans=1,usedN=0,usedM=0;
	for (LL i=n*m;i;i--) {
		if (px[i] && py[i]) { usedN++; usedM++; continue; }
		if (px[i]) { usedN++; ans=(ans*usedM)%MOD; }
		if (py[i]) { usedM++; ans=(ans*usedN)%MOD; }
		if (!px[i] && !py[i]) ans=(ans*(usedN*usedM-(n*m-i)))%MOD;
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; //10^18
const int inINF = 1000000000; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;

int main(){
    int N, T;
    cin >> N >> T;
    vector<iint> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i].first >> A[i].second;
    }
    sort(ALL(A));

    vector<vector<int>> dp(N+1, vector<int>(T));
    int ans = 0;
    for (int i = 0; i < N+1; i++) {
        dp[i][0] = 0;
    }
    for (int i = 0; i < T; i++) {
        dp[0][i] = 0;
    }
    for (int n = 1; n < N+1; n++) {
        for (int t = 1; t < T; t++) {
            if(t - A[n-1].first >= 0){
                dp[n][t] = max(dp[n-1][t], dp[n-1][t-A[n-1].first] + A[n-1].second);
            }
            else{
                dp[n][t] = dp[n-1][t];
            }
        }
        for (int t = T; t < T+A[n-1].first; t++) {
            if(t - A[n-1].first >= 0){
                ans = max(ans, dp[n-1][t-A[n-1].first] + A[n-1].second);
            }
        }
    }

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

}


#include <stdio.h>

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

int main(void)
{
    int students_number;
    int pm[10000], pe[10000], pj[10000];
    scanf("%d", &students_number);
    while (1) {
        if (students_number == 0) {
            break;
        } else {
            for (int i = 0; i < students_number; ++i) {
                scanf("%d %d %d", &pm[i], &pe[i], &pj[i]);
            }
            for (int i = 0; i < students_number; ++i) {
                dividing(pm[i], pe[i], pj[i]);
            }
            scanf("%d", &students_number);
        }
    }
    return 0;
}

void dividing(int pm, int pe, int pj)
{
    char rank;
    if (pm == 100 || pe == 100 || pj == 100) {
        rank = 'A';
    } else if (pm + pe >= 90 * 2) {
        rank = 'A';
    } else if (pm + pe + pj >= 80 * 3) {
        rank = 'A';
    } else if (pm + pe + pj >= 70 * 3) {
        rank = 'B';
    } else if (pm + pe + pj >= 50 * 3 && (pm >= 80 || pe >= 80)) {
        rank = 'B';
    } else {
        rank = 'C';
    }
    printf("%c\n", rank);
}

#include <stdio.h>

int main(void){
	int n;
	int pm, pe, pj;
	double avr;

	while (scanf("%d", &n), n){
		for (int i=0; i<n; i++){
			scanf("%d %d %d", &pm, &pe, &pj);
			avr = (pm+pe+pj)/3.;
			if (pm==100 || pe==100 || pj==100){
				puts("A");
			} else if (pm+pe >= 180){
				puts("A");
			} else if (avr>=80){
				puts("A");
			} else if (avr >= 70){
				puts("B");
			} else if (avr >= 50 && (pm>=80 || pe >= 80)){
				puts("B");
			} else {
				puts("C");
			}
		}
	}
	return 0;
}


#include <stdio.h>
void merge(int *,int,int,int);
void mergesort(int*,int,int);
long long cnt=0;
int L[100002],R[100002];
int main(){
  int A[200000],i,n;
  scanf("%d",&n);
  for(i=0;i<n;i++)scanf("%d",&A[i]);
  mergesort(A,0,n);
  printf("%ld\n",cnt);
  return 0;
}
void merge(int *a,int left,int mid,int right){
  int i,k,j,n1=mid-left,n2=right-mid;
  for(i=0;i<n1;i++){
    L[i]=a[left+i];
  }
  for(i=0;i<n2;i++){
    R[i]=a[mid+i];
  }
  L[n1]=2147483647;
  R[n2]=2147483647;
  i=0;
  j=0;
  for(k=left;k<right;k++){
    if(L[i]<=R[j])a[k]=L[i++];
    else{
      a[k]=R[j++];
      cnt+=n1-i;
    }
  }
  return;
}
void mergesort(int *a,int left,int right){
  int mid;
  if(left+1<right){
    mid=(left+right)/2;
    mergesort(a,left,mid);
    mergesort(a,mid,right);
    merge(a,left,mid,right);
  }
  else return;
}

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repq(i,a,n) for(int i=a; i<=n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

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

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

// Binary Indexed Tree (BIT)
// Verified: AOJ DSL_2_B: Range Sum Query (int??????)

template <typename T>
struct BIT{
private:
    vector<T> array;
    const int n;

public:
    // ?????????
    BIT(int _n) : array(_n + 1, 0), n(_n) {}

    // 1???????????? i???????????§????´???????????±???????
    T sum(int i) {
        T s = 0;
        while(i > 0) {
            s += array[i];
            i -= i & -i;      // LSB ??????
        }
        return s;
    }

    // [i, j] ???????´??????????
    T sum(int i, int j) {
        T ret_i = sum(i-1);
        T ret_j = sum(j);
        return ret_j - ret_i;
    }

    // i ????????? ????´? x ?????????
    void add(int i, T x) {
        while(i <= n) {
            array[i] += x;
            i += i & -i;      // LSB ??????
        }
    }
};

signed main() {
    int n; cin >> n;
    BIT<int> b(n);
    vector<int> a(n);
    map<int, int> m;
    rep(i,0,n) {
        cin >> a[i];
        m[a[i]];
    }

    int newnum = 1;
    for(auto &x : m) x.second = newnum++;
    rep(i,0,n) a[i] = m[a[i]];

    int ans = 0;
    rep(j,0,n) {
        ans += j - b.sum(a[j]);
        b.add(a[j], 1);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#include<vector>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int mod=1e9+7;
int main(){
	IOS;
	int n,dr;
	cin>>n>>dr;
	if(n>=10)
	cout<<dr;
	else
	cout<<dr+100*(10-n);
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cout << "[" << #var << "] " << var << endl
const ll mod = 1000000007;

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

    int ans = a + b;

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


//j
#include <stdio.h>

int main ()
{
	int step= 0;
	int a, b, c;

	scanf ("%d %d %d", &a, &b, &c);

	if (a<b)
	{
	for (int i=1; i <= a; i++)
		{
			if (b*i >= c)
			{
			printf ("%d", i);
			return 0;
			}
		}
	}

	if (b<a)
	{
	for (int j=1; j <= b; j++)
		{
			if (a*j >= c)
			{
			printf ("%d", j);
			return 0;
			}
		}
	}

	if (b=a)
	{
	for (int k=1; k <= b; k++)
		{
			if (a*k >= c)
			{
			printf ("%d",k);
			return 0;
			}
		}
	}

	return 0;
}

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

#define all(v) v.begin(), v.end()

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

char toLowerCase(char c){
  if(isLowerCase(c)){
    return c;
  }else{
    return c+'a'-'A';
  }
}
char toUpperCase(char c){
  if(isLowerCase(c)){
    return c-('a'-'A');
  }else{
    return c;
  }
}
ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
void func(vector<int>& l, int depth, int idx){
  ;
}
int main() {
  int h,w,n;cin>>h>>w>>n;
  int val=max(h,w);
  int ans=(n+val-1)/val;
  cout<<ans<<endl;
  return 0;
}


// This amazing code is by Eric Sunli Chen.
#include<bits/stdc++.h>
using namespace std;
template<typename T> bool get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar());
	if(t=='-')neg=true,t=getchar();if(t==EOF)return false;
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;return true;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) (get1(a)&&get1(b))
#define get3(a,b,c) (get1(a)&&get2(b,c))
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

const int maxn=100111;
int n,s,x[maxn];
LL p[maxn],ans;

int calc(int l,int r)
{
	if(x[r]<s)
	{
		ans+=s-x[l];
		return l;
	}
	if(x[l]>s)
	{
		ans+=x[r]-s;
		return r;
	}
	if(p[l]>=p[r])
	{
		p[l]+=p[r];
		ans+=x[r]-x[calc(l,r-1)];
		return r;
	}
	else
	{
		p[r]+=p[l];
		ans+=x[calc(l+1,r)]-x[l];
		return l;
	}
}

int main()
{
	get2(n,s);
	for(int i=1;i<=n;i++)get2(x[i],p[i]);
	calc(1,n);
	printendl(ans);
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define N 1000100
ll n,s,ans,x[N],p[N];
using namespace std;
int main(){
    scanf("%lld%lld",&n,&s);
    for (ll i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]);
    for (ll l=1,r=n;l<=r;){
        if (x[l]>=s){ans+=x[r]-s; break;}//所有人都在一边直接开过去
        if (x[r]<=s){ans+=s-x[l]; break;}
        ans+=x[r]-x[l]; //每次来回开一趟
        if (p[l]>=p[r])
            while (l<r && p[l]>=p[r]) p[l]+=p[r],r--;//相同往左开
        else while (l<r && p[l]<p[r]) p[r]+=p[l],l++;
    }
    printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<" ";

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    double a[n];
    double num=1e9,sum=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
        sum+=a[i];
    }
    sum/=n;
    for(int i=0;i<n;i++){
        num=min(num,abs(a[i]-sum));
    }
    for(int i=0;i<n;i++){
        if(abs(sum-a[i])==num){
            cout<<i<<endl;
            return 0;
        }
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = M_PI;
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

int main() {
  ll n;
  cin >> n;
  vector<vll> a(n, vll(2));
  long double avg = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i][0];
    avg += a[i][0];
    a[i][1] = i;
  }
  avg /= n;
  ll m = -1;
  sort(all(a));
  for (ll i = 0; i < n; i++) {
    if (a[i][0] == avg) {
      cout << a[i][1] << endl;
      return 0;
    }
    if (a[i][0] > avg) {
      while (i > 0 && a[i][0] == a[i - 1][0]) i--;
      m = i;
      break;
    }
  }
  if (m == 0) {
    cout << a[m][1] << endl;
    return 0;
  }
  ll x = m - 1;
  while (x > 0 && a[x][0] == a[x - 1][0]) x--;
  //cout << a[m][0] - avg << " " << avg - a[x][0] << endl;
  if (a[m][0] - avg > avg - a[x][0]) cout << a[x][1] << endl;
  else if (a[m][0] - avg < avg - a[x][0]) cout << a[m][1] << endl;
  else {
    cout << min(a[x][1], a[m][1]) << endl;
  }
}


#include <bits/stdc++.h>

#define DICE_SIZE 6

using namespace std;

class Dice {
	private:
		int number[DICE_SIZE];
		int work_space[DICE_SIZE];

		void reset_number(int, int, int, int, int, int);

	public:
		Dice(int *);
		void turn(char);
		int peek_top();
};

Dice::Dice(int *number) {
	for (int i = 0; i < DICE_SIZE; i++) {
		this->number[i] = number[i];
	}
}

void Dice::turn(char direction) {
	for (int i = 0; i < DICE_SIZE; i++) work_space[i] = number[i];

	switch (direction) {
		case 'N':
			reset_number(work_space[1], work_space[5], work_space[2], work_space[3], work_space[0], work_space[4]);
			break;
		case 'E':
			reset_number(work_space[3], work_space[1], work_space[0], work_space[5], work_space[4], work_space[2]);
			break;
		case 'S':
			reset_number(work_space[4], work_space[0], work_space[2], work_space[3], work_space[5], work_space[1]);
			break;
		case 'W':
			reset_number(work_space[2], work_space[1], work_space[5], work_space[0], work_space[4], work_space[3]);
			break;
	}
}

int Dice::peek_top() {
	return number[0];
}

void Dice::reset_number(int num0, int num1, int num2, int num3, int num4, int num5) {
	number[0] = num0;
	number[1] = num1;
	number[2] = num2;
	number[3] = num3;
	number[4] = num4;
	number[5] = num5;
}

int main() {
	int number[DICE_SIZE];
	for (int i = 0; i < DICE_SIZE; i++) cin >> number[i];

	Dice *dice = new Dice(number);

	string query;
	cin >> query;

	for (int i = 0; i < query.size(); i++) dice->turn(query[i]);

	cout << dice->peek_top() << endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<functional>
#define ll long long
#define inf  999999999
#define pa pair<int,int>

using namespace std;

int n,a[200][200]={0};
void prim(){
	int d[200]={0};
	priority_queue<pair<int,pa> >  PQ;
	pair<int,pa> q;
	pa r;
	int wei=0;
	int w=6;
	int u=w,z=0;
	d[w]=1;
	while(z!=n-1){
		for(int j=n-1;j>-1;j--){
			if(a[u][j]<=0 && d[j]==0 && u!=j) PQ.push(make_pair(a[u][j],make_pair(u,j)));
		}
	while(1){
		if(PQ.size()==0) {
			cout<<"dv"<<endl;
		break;
		}
		q=PQ.top();
		PQ.pop();
		r=q.second;
	//	cout<<"rsec"<<r.second<<endl;
		if(d[r.second]==0) break;

		}
	u=r.second;
//	cout<<"a" <<u<<endl;
	d[u]=1;
	wei=wei+q.first;
	z++;
//	cout<<z<<" "<<-q.first<<" "<<u<<" "<<r.first<<endl;

	}
	cout<<-wei<<endl;
}

int main(){
	// your code goes here
	int m;
	cin>>n;

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

	return 0;
}

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

int P[10010];
void init(int N);
int root(int a);
bool is_same_set(int a,int b);
void unite(int a,int b);

struct Node {
  int cost,start,goal;
  bool operator<( const Node& right ) const {
    return cost == right.cost ? start < right.start : cost < right.cost;
  }
};

vector<Node> Nodes;

int main() {
  int N;
  cin >> N;
  for (int i=0;i<N;++i) {
    for (int j=0;j<N;++j) {
      int buf;
      cin >> buf;
      if (buf!=-1) {
	Node bufNode = {buf,i,j};
	Nodes.push_back(bufNode);
      }
    }
  }
  sort(Nodes.begin(),Nodes.end());
  init(N); int totalCost = 0;
  for (int i=0;i<Nodes.size();++i) {
    if (is_same_set(Nodes[i].start,Nodes[i].goal)==false) {
      unite(Nodes[i].start,Nodes[i].goal);
      totalCost += Nodes[i].cost;
    }
  }
  cout << totalCost << endl;
}

void init(int N) {
  for (int i=0;i<=N;++i) P[i] = i;
}

int root(int a) {
  if (P[a] == a) return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a,int b) {
  return root(a)==root(b);
}

void unite(int a,int b) {
  P[root(a)] = root(b);
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
  int a, b; string s;
  cin >> a >> b >> s;
  bool flag = true;
  rep(i,0,a) {
    if (s[i] == '-') {
      flag = false;
    }
  }
  if (s[a] != '-') {
    flag = false;
  }
  rep(i,a+1,a+b+1) {
    if (s[i] == '-') {
      flag = false;
    }
  }
  if (flag) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

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

int main() {
  string A, B, S;
  cin >> A >> B >> S;
  cout << ((regex_match(S, regex("\\d{" + A + "}-\\d{" + B + "}"))) ? "Yes" : "No") << "\n";
}


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

int main() {
    ll l, r; cin >> l >> r;
    if(r - l >= 2019)
    {
        cout << '0' << endl;
        return 0;
    }
    ll ans = 2020;
    for(ll i = l; i <= r; i++)for(ll j = i+1; j <= r; j++){
        if(i * j % 2019 < ans) ans = i * j % 2019;
    }
    cout << ans << endl;
    return 0;
}

#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  l , r;

	cin >> l >> r;

	int lol = 2019;

	int ans = inf;

	for(int i = 1; i <= 2019; ++i) {
		int num = (l/2019)*2019;

		num += i;

		if(num < l) {
			num += 2019;
		}

		//cout << num << endl;

		if(num > r) {
			continue;
		}

		for(int j = 1; j <= 2019; ++j) {
			int tm;
			if(i == j) {
				tm = num + 2019;
			}
			else {
			tm = (l/2019)*2019;

			tm += j;

			if(tm < l) {
				tm += 2019;
			}

			}

			if(num >= l && num <= r && tm >= l && tm <= r) {
				ans = min(ans , (num*tm)%2019);
			}
		}
	}

	cout << ans;
}

signed main() {
    fast;

    int t = 1;

	//cin >> t;

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

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";};
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

int digit[50];
void solve(){
    int n;
    cin >> n;
    if(n==0){
        print(0);
        return;
    }
    bool plus=true;
    if(n<0) {
        plus=false;
        n=-n;
    }
    int ctr=0;
    while(n>0){
        if(n&1){
            digit[ctr]=1;
        }
        ctr++;
        n/=2;
    }
    if(plus){
        for(ll i=ctr-1; i>0; i--){
            if(i&1){
                if(digit[i]==1) digit[i+1]++;
            }
        }
        repi(i,1,45){
            while(digit[i]>1){
                digit[i+1]++;
                digit[i+2]++;
                digit[i]-=2;
            }
        }
    }
    else{
        for(ll i=ctr-1; i>=0; i--){
            if(i%2==0){
                if(digit[i]==1) digit[i+1]++;
            }
        }
        repi(i,1,45){
            while(digit[i]>1){
                digit[i+1]++;
                digit[i+2]++;
                digit[i]-=2;
            }
        }
    }
    bool start=false;
    for(ll i=38;i>=0;i--){
        if(!start&&digit[i]==0) continue;
        if(!start&&digit[i]==1){
            start=true;
            cout << digit[i];
            continue;
        }
        cout << digit[i];
    }
    cout << endl;
    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
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 int INF=1001001001;
const int mod=10007;

int main() {
  int N;
  cin>>N;
  string t;
  int x=N;
  if(x==0){cout<<0<<endl;return 0;}
  while(x){
    int a=x%(-2);
    if(a<0){a+=2;x-=2;}
    t.push_back(a+'0');
    x/=(-2);
  }
  reverse(t.begin(),t.end());
  cout<<t<<endl;
  return 0;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(l.begin(), l.end(), greater<int>());
  int ans = 0;
  rep(i, k) ans += l[i];
  cout << ans << endl;
  return 0;
}

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

ll gcd(ll a, ll b){
    ll r,temp;
    if (a < b){
        temp = a;
        a = b;
        b = temp;
    }
    while ( (r = a % b) != 0){
        a = b;
        b = r;
    }
    return b;
}

int main(){
    ll N,K;
    cin >> N >> K;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];
    sort(all(A));
    ll gcd_num = A[0];
    rep(i,N) gcd_num = gcd(gcd_num, A[i]);
    bool possible = true;
    if (A[N - 1] < K) possible = false;
    if (K % gcd_num != 0) possible = false;

    if (possible) cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+5,inf=1e10;
ll n,ans;
pair<ll,ll> prfx[N],sfx[N],a[N];
inline pair<ll,ll> inter(pair<ll,ll> u,pair<ll,ll> v){
    pair<ll,ll> range = make_pair( max(v.first,u.first) , min(v.second,u.second) );
    range.second = max(range.first,range.second);
    return range;
}
inline void relax(pair<ll,ll> v,pair<ll,ll> u){
    ans = max(ans , v.second - v.first + u.second - u.first);
}
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(ll i=0;i<n;i++)cin>>a[i].first>>a[i].second,a[i].second++;
    sort(a,a+n);
    prfx[0]=make_pair(0,inf);
    for(ll i=0;i<n;i++)prfx[i+1]=inter(prfx[i],a[i]);
    sfx[n]=make_pair(0,inf);
    for(ll i=n-1;i>=0;i--)sfx[i]=inter(sfx[i+1],a[i]);
    for(ll i=0;i<n;i++)relax(a[i],inter(prfx[i],sfx[i+1]));
    for(ll i=1;i<n;i++)relax(prfx[i],sfx[i]);
    cout<<ans;
}

//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FIO ios_base::sync_with_stdio(false); cin.tie(0);
#define trav(x,a) for (auto& x: a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define mem(a,v) memset((a), (v), sizeof (a))
#define endl "\n"
#define case(t) cout << "Case #" << (t) << ": "
#define reada(a, n) for (int _i = 0; _i < (n); _i++) read(a[_i])
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;

template<class T> void read(T& x) { cin >> x; }
template<class X, class Y> void read(pair<X,Y>& a) { read(a.first), read(a.second); }
template<class T> void read(vector<T>& x) { trav(y, x) read(y); }
template<class T, class... O> void read(T& x, O&... y) { read(x), read(y...); }

string to_string(const char& x) { return string(1,x); }
string to_string(const char*& x) { return (string)x; }
string to_string(const string& x) { return x; }
template<class T, class U> string to_string(const pair<T,U>& x) { return to_string(x.first) + " " + to_string(x.second); }
template<class T> string to_string(const vector<T>& x) { string ret = ""; bool f = 0; trav(y, x) ret += (!f ? "" : " ") + to_string(y), f = 1; return ret; }
void print() { cout << endl; }
template<class T> void pr(const T& x) { cout << to_string(x); }
template<class T, class... O> void print(const T& x, const O&... y) { pr(x); if (sizeof...(y)) pr(" "); print(y...); }

int main() {
  FIO
  int n; read(n);
  vector<pair<int,int>> a(n); read(a);
  sort(all(a), [](pii l, pii r) { return (l.first != r.first) ? l.first < r.first : l.second < r.second; });
  vector<pair<int,int>> b;
  for (auto x: a) {
    while (sz(b) && b.back().first <= x.first && x.second <= b.back().second)
      b.pop_back();
    b.push_back(x);
  }
  int ans = 0;
  for (int i = 0; i+1 < sz(b); i++)
    ans = max(ans, max(0, b[0].second - b[i].first + 1) + max(0, b[i+1].second - b.back().first + 1));
  sort(all(a), [](pii l, pii r) { return l.second - l.first == r.second - r.first ? l.first < r.first : l.second - l.first > r.second - r.first; });
  int i = 0;
  for (; i+1 < n && a[0] == a[i]; i++);
  int val = a[0].second - a[0].first + 1;
  int lo = 0, hi = inf;
  for (; i < n; i++)
    lo = max(lo, a[i].first), hi = min(hi, a[i].second);
  val += max(0, hi - lo + 1);
  sort(all(a), [](pii l, pii r) { return l.second - l.first == r.second - r.first ? l.first > r.first : l.second - l.first > r.second - r.first; });
  i = 0;
  for (; i+1 < n && a[0] == a[i]; i++);
  lo = 0, hi = inf;
  for (; i < n; i++)
    lo = max(lo, a[i].first), hi = min(hi, a[i].second);
  val = max(val, a[0].second - a[0].first + 1 + max(0, hi - lo + 1));
  print(max(val, ans));
  return 0;
}


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

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if(a<=b){
        if(d<=c){
            std::cout << a+d << std::endl;
        }
        else {
            std::cout << a+c << std::endl;
        }
    }
    else{
        if(c<=d){
            std::cout << b+c << std::endl;
        }
        else{
            std::cout << b+d << std::endl;
        }
    }
    return 0;
}

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

int main(){
    vector<int>A,B;
    for(int i=0;i<2;i++){
        int a;
        cin >> a;
        A.push_back(a);
    }
    for(int i=0;i<2;i++){
        int b;
        cin >> b;
        B.push_back(b);
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    cout << A.at(0) + B.at(0) << '\n';
    return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long int lli;
typedef pair<int,int> mp;
typedef pair<int,mp> mmp;
#define fir first
#define sec second

struct unifo{
	int un;
	int uni[100005];
	void init(int n){
		rep(i,n)uni[i]=i;
		un=n;
	}
	int find(int p){
		if(uni[p]==p)return p;
		else return (uni[p]=find(uni[p]));
	}
	void mer(int p,int q){
		uni[find(p)]=find(q);
	}
	bool same(int p,int q){
		return find(p)==find(q);
	}
};

int kruskal(int n,vector<mmp>& es){
	unifo u;
	u.init(n);
	sort(es.begin(),es.end());
	int res=0;
	rep(i,es.size()){
		mmp pa=es[i];
		int a=pa.sec.fir,
			b=pa.sec.sec,
			c=pa.fir;
		if(!u.same(a,b)){
			res+=c;
			u.mer(a,b);
		}
	}
	return res;
}

int n,m;

//vector<mp> vs[105];

vector<mmp> es;

int main(void){
	int n,m;
	scanf("%d%d",&n,&m);

	rep(i,m){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		//vs[a].push_back(mp(b,c));
		//vs[b].push_back(mp(a,c));
		es.push_back(mmp(c,mp(a,b)));
	}
	printf("%d\n",kruskal(n,es));
	return 0;
}

//*********************************************************************************************************************************
//*																																**
//*		SSSSSSSSSSSS											MMMM			MMMM	HHHH		HHHH	DDDDDDDDDDDD		**
//*		SSSSSSSSSSSS	CSAA Numbered							MMMMM		   MMMMM	HHHH		HHHH	DDDDDDDDDDDDD		**
//*		SSSS				A PROGRAM BY:						MMMMMM		  MMMMMM	HHHH		HHHH	DDDD	 DDDDD		**
//*		SSSS													MMMMMMM		 MMMMMMM	HHHH		HHHH	DDDD	   DDDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAA	MMMMMMMMMMMMMMMMMMMM	MMMMMMMM	MMMMMMMM	HHHHHHHHHHHHHHHH	DDDD	    DDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAA	MMMMMMMMMMMMMMMMMMMM	MMMM MMMM  MMMM MMMM	HHHHHHHHHHHHHHHH	DDDD	    DDD		**
//*				SSSS	AAAA	AAAA	MMMM	MMMM	MMMM	MMMM  MMMMMMMM	MMMM	HHHH		HHHH	DDDD	   DDDD		**
//*				SSSS	AAAA	AAAA	MMMM	MMMM	MMMM	MMMM   MMMMMM	MMMM	HHHH		HHHH	DDDD	  DDDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAAA	MMMM	MMMM	MMMM	MMMM    MMMM	MMMM	HHHH		HHHH	DDDDDDDDDDDDD		**
//*		SSSSSSSSSSSS	AAAAAAAAAAAAAA	MMMM	MMMM	MMMM	MMMM			MMMM	HHHH		HHHH	DDDDDDDDDDDD		**
//*																																**
//********************************************************************************************************************************
//||											||
//||	CSAA AA###B ddmmYYYY					||
//||	?? SMP(SAM-MHD-PGM) @ AEH_COT96			||
//||	Program Name:							||
//||			AA###B_<xxxxNAMExxxx>			||
//||											||
/////******************************************\\

#include <iostream>
#include <vector>

using namespace std;
const int maxn = 1e4+3;
const int oo   = 3e7+3;

typedef pair <int, int> pii;
vector <pii> G[maxn];
vector <pii> ans;
bool used[maxn]; // Already in MST
long long pim [maxn]; // Partner in MST
long long mdti[maxn]; // Min_dis to MST :: Minimal Distance to Include
long long n, m;
long long ansvalue;

bool MST(){

	fill_n(mdti, n, oo);
	fill_n(pim, n, -1);
	fill_n(used, n, false);

	mdti[0] = 0;

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

		int v = -1;

		for (int j=0; j<n; j++)
			if ( !used[j] && (v == -1 || mdti[j] < mdti[v]))
				v = j;

//		if (mdti[v] == oo)
//			return false;

		used[v] = true;

		if (pim[v] != -1){
			ans.push_back ( make_pair( pim[v], v ) );
			ansvalue += mdti[v];
		}

		for (int j = 0; j < G[v].size(); j++)
			if ( G[v][j].second < mdti[ G[v][j].first ] ) {
				mdti[ G[v][j].first ] = G[v][j].second;
				pim [ G[v][j].first ] = v;
			}
	}
	return true;
}

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a, b, w;
		cin >> a >> b >> w;
		G [a].push_back( make_pair( b, w) );
		G [b].push_back( make_pair( a, w) );
	}

//	if (MST())
//		for (pii e : ans)
//			cout << e.first << " " << e.second << endl;
//	else cout << -1;
	if (MST())
		cout << ansvalue << endl;

}
/**/

// ?? SMP(SAM-MHD-PGM) @ AEH_COT96
//   SamMHD (Saman Mahdanian)


#include<iostream>
using namespace std;
int main (void)
{
  int r;
  cin>>r;
  r=r*2*3141592;
  cout<<r/1000000<<"."<<r%1000000;
}

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
#include<functional>
#include<stack>
#include<tuple>
#include<cassert>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

int main() {
  double R;
  double pi = M_PI;
  cin >> R;
  double ans = 2 * R * pi;
  cout << ans << endl;

  return 0;
}