#include <algorithm>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <string>
#include <vector>
#include <iostream>
#include <set>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

#ifdef ONLINE_JUDGE
# define LLD "%I64d"
#else
# define LLD "%ll" "d"
#endif

typedef long long llint;

const int MAXN = 1e5 + 123;
const double eps = 1e-9;
struct Edge { int a, b; double w; };

int n;
vector<Edge> E[MAXN];

struct Pt {
  double x, y, z;
  Pt(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
  friend Pt operator - (Pt a, Pt b) { return {a.x-b.x, a.y-b.y, a.z-b.z}; }
  friend double dot(Pt a, Pt b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
  friend Pt cross(Pt a, Pt b) {
    return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x };
  }
};

struct Line {
  Pt a, b;
  int idx;
  Pt get_abc() const {
    Pt ans = cross((Pt){a.x, a.y, 1}, {b.x, b.y, 1});
    assert(dot({a.x, a.y, 1}, ans) == 0);
    assert(dot({b.x, b.y, 1}, ans) == 0);
    return ans;
  }
  friend Pt presjek(const Line& a, const Line& b) {
    Pt a_abc = a.get_abc();
    Pt b_abc = b.get_abc();
    Pt ans = cross(a_abc, b_abc);
    assert(ans.z != 0);
    return {ans.x/ans.z, ans.y/ans.z, 0};
  }
  double minx() const { return min(a.x, b.x); }
  double maxx() const { return max(a.x, b.x); }
};

enum { BOTTOM, TOP };
struct Evt {
  int type;
  int enda, endb;
  int bottom;

  Pt curr;
  int curr_idx;
};
struct LineCmpf {
  bool operator () (const Line& a, const Line& b) const {
    double lo = max(a.minx(), b.minx()), hi = min(a.maxx(), b.maxx());
    double mid = (lo + hi) / 2;
    Line L = {(Pt){mid, 0}, (Pt){mid, 1}};
    double a_y = presjek(L, a).y, b_y = presjek(L, b).y;
    return make_tuple(a_y, a.minx()) < make_tuple(b_y, b.minx());
  }
};

inline double get_dist(Pt a, Pt b) { double dx = a.x-b.x, dy = a.y-b.y; return sqrt(dx*dx + dy*dy); }

double dijkstra(int s, int t) {
  const int inf = 1e9;
  static vector<double> dist; dist.clear(); dist.resize(n, inf);
  static vector<int> bio; bio.clear(); bio.resize(n, false);
  auto cmp = [](int a, int b) {
    return make_pair(dist[a], a) < make_pair(dist[b], b);
  };

  set<double, decltype(cmp)> pq(cmp);
  dist[s] = 0;
  pq.insert(s);

  for (; !pq.empty(); ) {
    int ex = *pq.begin(); pq.erase(pq.begin());
    bio[ex] = true;
    for (Edge e : E[ex]) {
      if (bio[e.b]) continue;
      double val = dist[e.a] + e.w;
      if (val+eps < dist[e.b]) {
        if (dist[e.b] < inf) pq.erase(e.b);
        dist[e.b] = val;
        pq.insert(e.b);
      }
    }
  }

  return dist[t];
}
int main(void)
{
  scanf("%d", &n);
  vector<Pt> P;
  REP(i, n) {
    double x, y; scanf("%lf %lf", &x, &y); x += 1e4;
    P.push_back({x, y, 0});
  } // ccw smjer

  const int MAXCOOR = 2e4 + 213;
  static vector<Evt> evts[MAXCOOR];
  REP(i, n) {
    int j = i+1; if (j == n) j = 0;
    Pt left = P[i], right = P[j]; int l_idx = i, r_idx = j;
    if (left.x > right.x) { swap(left, right); swap(l_idx, r_idx); }
    if (P[i].x == P[j].x) {
    } else if (P[i].x < P[j].x) {
      evts[(int)left.x].push_back({0, i, j, BOTTOM, left, l_idx});
      evts[(int)right.x].push_back({1, i, j, BOTTOM, right, r_idx});
    } else if (P[i].x > P[j].x) {
      evts[(int)left.x].push_back({0, i, j, TOP, left, l_idx});
      evts[(int)right.x].push_back({1, i, j, TOP, right, r_idx});
    }
  }
  set<Line, LineCmpf> pq;
  REP(x, MAXCOOR) {
    if (evts[x].empty()) continue;
    for (Evt e : evts[x]) {
      if (e.type == 0) {
        Line L = {P[e.enda], P[e.endb], e.enda};
        pq.insert(L);
      }
    }
    for (Evt e : evts[x]) {
      Line L = {P[e.enda], P[e.endb], e.enda};
      auto it = pq.find(L);
      assert(it != pq.end());

      Line x_line = {{(double)x, 0.}, {(double)x, 1.}};
      if (e.bottom == TOP && it != pq.begin()) {
        auto it2 = prev(it);
        Pt donja = presjek(x_line, *it2);
        for (int d_idx : {it2->idx, (it2->idx+1)%n}) {
          int a = e.curr_idx;
          int b = d_idx;
          double d = get_dist(e.curr, donja) + get_dist(donja, P[d_idx]);
          E[a].push_back({a, b, d});
        }
      }

      if (e.bottom == BOTTOM) {
        auto it2 = next(it);
        if (it2 != pq.end()) {
          Pt gornja = presjek(x_line, *it2);
          for (int g_idx : {it2->idx, (it2->idx+1)%n}) {
            int a = g_idx;
            int b = e.curr_idx;
            double d = get_dist(P[g_idx], gornja) + get_dist(gornja, e.curr);
            E[a].push_back({a, b, d});
          }
        }
      }
    }
    for (Evt e : evts[x]) {
      if (e.type == 1) {
        Line L = {P[e.enda], P[e.endb], e.enda};
        pq.erase(L);
      }
    }
  }
  REP(i, n) {
    int j = i+1; if (j == n) j = 0;
    E[i].push_back({i, j, get_dist(P[i], P[j])});
    E[j].push_back({j, i, get_dist(P[i], P[j])});
  }

  int s, t; scanf("%d %d", &s, &t); --s, --t;
  double ans = dijkstra(s, t);
  printf("%.9lf\n", ans);

  return 0;
}


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

using namespace std;

#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); i++)

#define pb push_back
#define fi first
#define se second

const int maxn=111111;
const double inf=1e201;
const int leaf=1<<17;
const double eps=1e-9;

int n;
int x[maxn], y[maxn];

int P[maxn];
bool side[maxn];
int pos;
int da[maxn], db[maxn];

int cmp(int a, int b){
    return (x[a]!=x[b])?(x[a]<x[b]):(y[a]<y[b]);
}

vector<pair<int,double> > e[maxn];
double dist(double a, double b){
    return sqrt(a*a+b*b);
}

int X;
struct line{
    double a, b;
    int id;
    line(double a, double b, int id=0):a(a),b(b),id(id){;}
    double Y(double X){return a*X+b;}
    friend bool operator < (const line &u){
        double ea=Y(X), eb=u.Y(X);
        return abs(ea-eb)>eps?ea<eb:id==-1||u.id==-1?0:Y(X+1)<u.Y(X+1);
    }
};

multiset<line> s;

void Descend(int i, int j){
    FOR(k,i,j){
        int u=P[k];
        multiset<line>::iterator it, it2;
        it2=s.lower_bound(line(0,y[u],-1));
        it=it2;
        if (it!=s.begin()){
            --it;
            for (; it!=s.begin() && (da[it->id]==u || db[it->id]==u); --it);
            if (!(da[it->id]==u || db[it->id]==u) && side[it->id]){
                int ua=da[it->id], ub=db[it->id];
                double yy=it->Y(x[u]);
                e[u].pb(make_pair(ua,dist(x[u]-x[ua],yy-y[ua])+fabs(yy-y[u])));
                e[u].pb(make_pair(ub,dist(x[u]-x[ub],yy-y[ub])+fabs(yy-y[u])));
            }
        }
        it=it2;
        for (; it!=s.end() && (da[it->id]==u || db[it->id]==u); ++it);
        if (it!=s.end() && !side[it->id]){
            int ua=da[it->id], ub=db[it->id];
            double yy=it->Y(x[u]);
            e[ua].pb(make_pair(u,dist(x[u]-x[ua],yy-y[ua])+fabs(yy-y[u])));
            e[ub].pb(make_pair(u,dist(x[u]-x[ub],yy-y[ub])+fabs(yy-y[u])));
        }
    }
}
double best[maxn];
int main(){
    int S,T;
    scanf("%d",&n);
    for (int i = 0; i < n; i++) scanf("%d%d",x+i,y+i);
    for (int i = 0; i < n; i++) e[i].clear();
    scanf("%d%d",&S,&T);
    --S; --T;
    x[n]=x[0]; y[n]=y[0];

    for (int i = 0; i < n; i++) P[i]=i;
    sort(P,P+n,cmp);

    pos=0;
    s.clear();

    for (int i=0; i<n;i++){
        int j;
        X=x[P[i]];
        for (j=i; j<n && x[P[j]]==x[P[i]]; ++j);
        Descend(i,j);
        for (j=i; j<n && x[P[j]]==x[P[i]]; ++j){
            int u=P[j], va=(u+n-1)%n, vb=(u+1)%n;
            if (x[va]<x[u]) s.erase(line(0,y[u],-1));
            if (x[vb]<x[u]) s.erase(line(0,y[u],-1));

            multiset<line>::iterator ita=s.end(), itb=s.end(), it;

            if (x[va]>x[u]){
                double ax=1.*(y[va]-y[u])/(x[va]-x[u]);
                ita=s.insert(line(ax,y[u]-X*ax,pos));
                da[pos]=u; db[pos]=va; ++pos;
            }

            if (x[vb]>x[u]){
                double ax=1.*(y[vb]-y[u])/(x[vb]-x[u]);
                itb=s.insert(line(ax,y[u]-X*ax,pos));
                da[pos]=u; db[pos]=vb; ++pos;
            }

            if (ita!=s.end() && itb!=s.end() && *itb<*ita) swap(ita,itb);

            if (ita!=s.end()){
                it=ita; --it;
                if (ita==s.begin())
                    side[ita->id]=1;
                //else
                 //   side[ita->id]=side[it->id]^1;
            }

            if (itb!=s.end()){
                it=itb; --it;
                if (itb==s.begin())
                    side[itb->id]=1;
                //else
                  //  side[itb->id]=side[it->id]^1;
            }
        }
        Descend(i,j);
        i=j;
    }
    for (int i = 0; i < n; i++){
        double d=dist(x[i]-x[i+1],y[i]-y[i+1]);
        e[i].pb(make_pair((i+1)%n,d));
        e[(i+1)%n].pb(make_pair(i,d));
    }
    priority_queue<pair<double,int> > q;
    REP(i,n) best[i]=inf;
    best[S]=0;
    q.push(make_pair(-0.,S));
    while(q.size()){
        double d=-q.top().fi;
        int a=q.top().se;
        q.pop();
        if (a==T) {printf("%.10lf\n",d); return 0;}
        if (d!=best[a]) continue;
        REP(i,e[a].size()){
            int b=e[a][i].fi;
            double se=d+e[a][i].se;
            if (best[b]>se) best[b]=se, q.push(make_pair(-se,b));
        }
    }
}


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

using namespace std;

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

#define pb push_back
#define fi first
#define se second
#define next(x) ((x+1)%n)
#define perv(x) ((x-1+n)%n)
const int maxn=111111;
const double inf=1e201;
const int leaf=1<<17;
const double eps=1e-9;

int n;
int x[maxn], y[maxn];

int P[maxn];
bool side[maxn];
int pos;
int da[maxn], db[maxn];

int cmp(int a, int b){
    return (x[a]!=x[b])?(x[a]<x[b]):(y[a]<y[b]);
}

vector<pair<int,double> > e[maxn];
double dist(double a, double b){
    return sqrt(a*a+b*b);
}

int X;
struct line{
    double a, b;
    int id;
    line(double a, double b, int id=0):a(a),b(b),id(id){;}
    double Y(double X) const {
        return a*X+b;
    }
    bool operator < (const line &u) const{
        double ea=Y(X), eb=u.Y(X);
        return abs(ea-eb)>eps?ea<eb:id==-1||u.id==-1?0:Y(X+1)<u.Y(X+1);
    }
};

multiset<line> s;

void Descend(int i, int j){
    for (int k = i; k < j; j++){
        int u=P[k];
        multiset<line>::iterator it, it2;
        it2=s.lower_bound(line(0,y[u],-1));
        it=it2;
        if (it!=s.begin()){
            --it;
            for (; it!=s.begin() && (da[it->id]==u || db[it->id]==u); --it);
            if (!(da[it->id]==u || db[it->id]==u) && side[it->id]){
                int ua=da[it->id], ub=db[it->id];
                double yy=it->Y(x[u]);
                e[u].pb(make_pair(ua,dist(x[u]-x[ua],yy-y[ua])+fabs(yy-y[u])));
                e[u].pb(make_pair(ub,dist(x[u]-x[ub],yy-y[ub])+fabs(yy-y[u])));
            }
        }
        it=it2;
        for (; it!=s.end() && (da[it->id]==u || db[it->id]==u); ++it);
        if (it!=s.end() && !side[it->id]){
            int ua=da[it->id], ub=db[it->id];
            double yy=it->Y(x[u]);
            e[ua].pb(make_pair(u,dist(x[u]-x[ua],yy-y[ua])+fabs(yy-y[u])));
            e[ub].pb(make_pair(u,dist(x[u]-x[ub],yy-y[ub])+fabs(yy-y[u])));
        }
    }
}
double D[maxn];
priority_queue<pair<double,int> > Q;
int main(){
    int S,T;
    scanf("%d",&n);
    for (int i = 0; i < n; i++) scanf("%d%d",x+i,y+i);
    for (int i = 0; i < n; i++) e[i].clear();
    scanf("%d%d",&S,&T);
    --S; --T;
    //x[n]=x[0]; y[n]=y[0];

    for (int i = 0; i < n; i++) P[i]=i;
    sort(P,P+n,cmp);

    pos=0;

    for (int i=0; i<n;i++){
        int j;
        X=x[P[i]];
        for (j=i; j<n && x[P[j]]==x[P[i]]; ++j);
        Descend(i,j);
        for (j=i; j<n && x[P[j]]==x[P[i]]; ++j){
            int u=P[j];
            int le=prev(u), rt=next(u);
            if (x[le]<x[u]) s.erase(line(0,y[u],-1));
            if (x[rt]<x[u]) s.erase(line(0,y[u],-1));

            multiset<line>::iterator ita=s.end(), itb=s.end(), it;

            if (x[le]>x[u]){
                double ax=1.*(y[le]-y[u])/(x[le]-x[u]);
                ita=s.insert(line(ax,y[u]-X*ax,pos));
                da[pos]=u; db[pos]=le; ++pos;
            }

            if (x[rt]>x[u]){
                double ax=1.*(y[rt]-y[u])/(x[rt]-x[u]);
                itb=s.insert(line(ax,y[u]-X*ax,pos));
                da[pos]=u; db[pos]=rt; ++pos;
            }

            if (ita!=s.end() && itb!=s.end() && *itb<*ita) swap(ita,itb);

            if (ita!=s.end()){
                it=ita; --it;
                if (ita==s.begin())
                    side[ita->id]=1;
                else
                    side[ita->id]=side[it->id]^1;
            }

            if (itb!=s.end()){
                it=itb; --it;
                if (itb==s.begin())
                    side[itb->id]=1;
                //else
                  //  side[itb->id]=side[it->id]^1;
            }
        }
        Descend(i,j);
        i=j;
    }
    for (int i = 0; i < n; i++){
        double d=dist(x[i]-x[next(i)],y[i]-y[next(i)]);
        e[i].pb(make_pair(next(i),d));
        e[next(i)].pb(make_pair(i,d));
    }

    for (int i = 0; i < n; i++) D[i]=inf;

    D[S]=0;
    Q.push(make_pair(-0.,S));
    while(Q.size()){
        double d=-Q.top().fi;
        int a=Q.top().se;
        if (a==T)
            return printf("%.10lf\n",d),0;
        Q.pop();
        if (d==D[a]){
            for (int i = 0; i <e[a].size(); i++){
                int b=e[a][i].fi;
                double se=d+e[a][i].se;
                if (D[b]>se) D[b]=se, Q.push(make_pair(-se,b));
            }
        }
    }
}


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

struct FR {
	LL a, b;
	FR() : a(0), b(1) {}
	FR(LL x) : a(x), b(1) {}
	FR(LL x, LL y) {
		LL g = __gcd(x, y);
		x /= g, y /= g;
		if (y < 0) x = -x, y = -y;
		a = x, b = y;
	}
	bool operator<(const FR &that) const {
		return (long long)a * that.b < (long long)that.a * b;
	}
	bool operator>(const FR &that) const {
		return (long long)a * that.b > (long long)that.a * b;
	}
	bool operator==(const FR &that) const {
		return (long long)a * that.b == (long long)that.a * b;
	}
	bool operator<=(const FR &that) const {
		return !(that < *this);
	}
	bool operator>=(const FR &that) const {
		return !(*this < that);
	}
	FR operator+(const FR &that) const {
		return FR(a * that.b + that.a * b, b * that.b);
	}
	FR operator-(const FR &that) const {
		return FR(a * that.b - that.a * b, b * that.b);
	}
	FR operator*(const FR &that) const {
		return FR(a * that.a, b * that.b);
	}
	FR operator/(const FR &that) const {
		return FR(a * that.b, b * that.a);
	}
	double toDouble() const {
		return (double)a / b;
	}
};

static const int INF = 0x3f3f3f3f;

struct PT {
	int x;
	FR y;
	PT() {}
	PT(FR x, FR y) : x(x), y(y) {}
	void in() {
		int _x, _y;
		scanf("%d%d", &_x, &_y);
		x = _x, y = _y;
	}
};

bool operator<(const PT& p1, const PT& p2) {
	if (!(p1.x == p2.x)) return p1.x < p2.x;
	return p1.y < p2.y;
}

bool operator==(const PT& p1, const PT& p2) {
	return p1.x == p2.x && p1.y == p2.y;
}

struct Segment {
	static int curX;
	FR slope, intercept;
	int xL, xR;
	int id;
	Segment(FR slope, FR intercept, int xL, int xR, int id) : slope(slope), intercept(intercept), xL(xL), xR(xR), id(id) {}
	Segment(PT A, PT B, int id) : id(id) {
		if (A.x > B.x) swap(A, B);
		xL = round(A.x.toDouble()), xR = round(B.x.toDouble());
		if (xL == xR) {
			intercept = A.y;
			slope = 0;
		} else {
			slope = (B.y - A.y) / (xR - xL);
			intercept = A.y - A.x * slope;
		}
	}
	FR getY(const int &x) const {
		return intercept + slope * x;
	}
	pair<PT, PT> getPoints() {
		return make_pair(PT(xL, getY(xL)), PT(xR, getY(xR)));
	}
	bool operator<(const Segment &that) const {
		FR y0 = getY(curX);
		FR y1 = that.getY(curX);
		if (y0 == y1) {
			if (curX == xR && that.xR == curX) return that.slope < slope;
			if (curX == xL && that.xL == curX) return slope < that.slope;
			if (curX == xL && that.xR == curX) return 0;
			return 1;
		}
		return y0 < y1;
	}
	bool isIntersectTo(const Segment &that, FR &crossX) const {
		if(slope == that.slope) {
			if(intercept == that.intercept) {
				crossX = max(xL, that.xL);
				return !(xR < that.xL) && !(that.xR < xL);
			} else {
				return false;
			}
		} else {
			crossX = (intercept - that.intercept) / (that.slope - slope);
			return FR(xL) <= crossX && crossX <= FR(xR) && FR(that.xL) <= crossX && crossX <= FR(that.xR);
		}
	}
};
int Segment::curX;

const int N = 110000;
typedef pair<int, int> PII;
vector<PT> V[N];
typedef double DB;
const DB PI = acos(-1);
const DB EPS = 1e-9;
int hn;
map<PT, int> M;

DB sqr (DB x) { return x * x; };

DB dist(PT P, PT Q) {
	return sqrt(sqr((P.x - Q.x)) + sqr((P.y - Q.y).toDouble()));
}

int Hash(PT p) {
	if (M.count(p)) return M[p];
	return M[p] = hn++;
}

struct data{
	int x, y;
	DB dis;
	data(int x = 0, int y = 0, DB dis = 0) : x(x), y(y), dis(dis) {}
};

int chk[N];

vector<data> makeGraph(vector<PT> &p) {
	vector<data> ret;
	int n = p.size();
	vector<Segment> segments;
	typedef tuple<int, int, int> event;
	vector<event> events;
	vector<DB> angles;

	for (int i = 0; i < n; i++) {
		int j = (i + 1) % n;
		PT A = p[i], B = p[j];
		segments.push_back(Segment(A, B, i));
		V[i].push_back(A);
		V[i].push_back(B);
		angles.push_back(atan2((B.y - A.y).toDouble(), (B.x - A.x).toDouble()));
		if (A.x == B.x) continue;
		int a = i, b = j;
		if (A.x > B.x) swap(A, B), swap(a, b);
		events.emplace_back(round(A.x.toDouble()), a, i);
		events.emplace_back(round(B.x.toDouble()), b + n, i);
	}
	sort(events.begin(), events.end());
	multiset<Segment> ss;

	auto check = [&n, &angles, &ret](const Segment &s, const Segment &t, const int &x, int id, int dir) {
		FR y1 = s.getY(x), y2 = t.getY(x);
		DB from = angles[id];
//		cerr << y1.toDouble() << " " << y2.toDouble() << endl;
		DB to = angles[(id - 1 + n) % n] + PI;
		if (to > PI) to -= 2 * PI;
		if (to < from) to += 2 * PI;
		DB cur = PI / 2;
		if (dir < 0) cur -= PI;
		if ((from - EPS < cur && cur < to + EPS) || (from - EPS < cur + 2 * PI && cur + 2 * PI < to + EPS)) {
			V[s.id].push_back(PT(x, y1));
			V[t.id].push_back(PT(x, y2));
			if (y1 < y2) swap(y1, y2);
			ret.push_back(data(Hash(PT(x, y1)), Hash(PT(x, y2)), fabs((y1 - y2).toDouble())));
		}
	};

	int prv = -10000000;
	events.emplace_back(100000000, 0, 0);
	vector<event> temp;
	for (auto e : events) {
		int x, _a, _b;
		tie(x, _a, _b) = e;
		if (prv < x) {
			Segment::curX = prv;
			for (auto t : temp) {
				int xx, a, b;
				tie(xx, a, b) = t;
				if (a < n) ss.insert(segments[b]);
			}
			for (auto t : temp) {
				int xx, a, b;
				tie(xx, a, b) = t;
				int i = a % n;
				const Segment &s = segments[b];
				int now = Hash(PT(prv, s.getY(prv)));
				if (chk[now]) continue;
				chk[now] = 1;
				auto it = ss.upper_bound(s);
				auto it1 = it;
				while (it != ss.end() && it->getY(prv) <= s.getY(prv)) it++;
				if (it != ss.end()) check(*it, s, prv, i, 1);
				it = it1;
				while (it != ss.begin() && it != ss.end() && it->getY(prv) >= s.getY(prv)) --it;
				if(it != ss.end() && it->getY(prv) < s.getY(prv)) check(*it, s, prv, i, -1);
			}
			for (auto t : temp) {
				int xx, a, b;
				tie(xx, a, b) = t;
				if (a >= n) ss.erase(segments[b]);
			}
			temp.clear();
			prv = x;
		}
		temp.push_back(e);
	}
	for (int i = 0; i < n; i++) {
		sort(V[i].begin(), V[i].end());
		for (int j = 0; j < V[i].size() - 1; j++) {
			ret.push_back(data(Hash(V[i][j]), Hash(V[i][j + 1]), dist(V[i][j], V[i][j + 1])));
			ret.push_back(data(Hash(V[i][j + 1]), Hash(V[i][j]), dist(V[i][j], V[i][j + 1])));
		}
	}
	return ret;
}

vector<int> adj[N * 10];
vector<DB> dis[N * 10];
typedef pair<DB, int> PDI;
DB d[N * 10];
int chk[N * 10];
priority_queue<PDI, vector<PDI>, greater<PDI>> Q;

DB dijkstra(const vector<data> &link, const int &s, const int &t) {
	for (auto &t : link) {
		int a = t.x, b = t.y;
		DB d = t.dis;
		adj[a].push_back(b);
		dis[a].push_back(d);
	}
	for (int i = 0; i < hn; i++) d[i] = 1e20;
	Q.push(PDI(0, s));
	d[s] = 0;
	while (!Q.empty()) {
		int now = Q.top().second;
		Q.pop();
		if (chk[now]) continue;
		chk[now] = 1;
		for (int i = 0; i < adj[now].size(); i++) {
			int j = adj[now][i];
			DB dd = dis[now][i];
			if (d[j] > d[now] + dd) {
				d[j] = d[now] + dd;
				Q.push(PDI(d[j], j));
			}
		}
	}
	return d[t];
}

int main() {
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	vector<PT> P;
	int n, s, t; scanf("%d", &n); P.resize(n);
	for (int i = 0; i < n; i++) P[i].in();
	scanf("%d%d", &s, &t); --s, --t;
	printf("%.10lf\n", dijkstra(makeGraph(P), Hash(P[s]), Hash(P[t])));
	return 0;
}


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

int n, Q, cnt, ps[100005], L[100005], R[100005]; ll k, ans, w[100005];

vector <pii> g[100005];

struct st {
	ll p, m, tg, lm, mr, lmr;
	void update(ll v) {p += v; m -= 2 * v; lm -= v; mr -= v; tg += v;}
	st operator+(const st &b)const {
		st c; c.p = max(p, b.p); c.m = max(m, b.m);
		c.lm = max(max(lm, b.lm), p + b.m);
		c.mr = max(max(mr, b.mr), m + b.p);
		c.lmr = max(max(lmr, b.lmr), max(lm + b.p, p + b.mr));
		return c;
	}
} T[N*8];

void update(int node, int ns, int ne, int s, int e, ll v) {
	if (ne < s || e < ns) return;
	if (s <= ns && ne <= e) {
		T[node].update(v);
		return;
	}
	T[node*2].update(T[node].tg);
	T[node*2+1].update(T[node].tg);
	T[node].tg = 0;
	int mid = (ns + ne)/2;
	update(node*2, ns, mid, s, e, v);
	update(node*2+1, mid + 1, ne, s, e, v);
	T[node] = T[node*2] + T[node*2+1];
}

void dfs(int x, int p) {
	L[x] = ++cnt;
	for (int i = 0; i < g[x].size(); i++)
		if (g[x][i].first != p) {
			ps[g[x][i].second] = g[x][i].first;
			dfs(g[x][i].first, x);
			cnt++;
		}
	R[x] = cnt;
}

int main() {
	scanf("%d%d%lld", &n, &Q, &k);
	for (int i = 1; i < n; i++) {
		int u, v; scanf("%d%d%lld", &u, &v, &w[i]);
		g[u].push_back(make_pair(v, i));
		g[v].push_back(make_pair(u, i));
	}
	dfs(1, 0);
	for (int i = 1; i < n; i++) update(1, 1, 2 * n, L[ps[i]], R[ps[i]], w[i]);
	while (Q--) {
		ll x, v; scanf("%lld%lld", &x, &v);
		x = (x + ans) % (n - 1) + 1;
		v = (v + ans) % k;
		update(1, 1, 2 * n, L[ps[x]], R[ps[x]], v - w[x]);
		w[x] = v;
		printf("%lld\n", ans = T[1].lmr);
	}
}


格式化代码
#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);(i)<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);(i)>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof(a))
#define int long long
inline int read() {
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int inf = 4e18;
const int MAXN = 2e5+10;
int v1[MAXN<<2],v2[MAXN<<2],v12[MAXN<<2],v23[MAXN<<2],v123[MAXN<<2],tag[MAXN<<2],dis[MAXN];
#define lson o<<1
#define rson o<<1|1
inline void pushup(int o){
	v1[o]=max(v1[lson],v1[rson]);v2[o]=min(v2[lson],v2[rson]);
	v12[o]=max(v12[lson],max(v12[rson],v1[lson]-2*v2[rson]));
	v23[o]=max(v23[lson],max(v23[rson],-2*v2[lson]+v1[rson]));
	v123[o]=max(v123[lson],max(v123[rson],max(v1[lson]+v23[rson],v12[lson]+v1[rson])));
}
inline void upd(int o,int v){
	v1[o]+=v;v2[o]+=v;v12[o]-=v;v23[o]-=v;
	tag[o]+=v;
}
inline void pushdown(int o){
	if(!tag[o])return;int v=tag[o];tag[o]=0;
	upd(lson,v);upd(rson,v);
}
void mdy(int o,int l,int r,int ql,int qr,int v){
	if(l>=ql&&r<=qr){upd(o,v);return;}
	int mid=l+r>>1;pushdown(o);
	if(ql<=mid)mdy(lson,l,mid,ql,qr,v);
	if(qr>mid)mdy(rson,mid+1,r,ql,qr,v);
	pushup(o);
}
int lst,front[MAXN],tcnt=1,idfn[MAXN],dfc,st[MAXN],ed[MAXN],n,q,mxw;
struct tEdge{
	int v,next,w;
}e[MAXN];
void dfs(int u,int f){
	idfn[++dfc]=u;st[u]=dfc;
	fe(i,u){
		int v=e[i].v;if(v==f)continue;dis[v]=dis[u]+e[i].w;
		dfs(v,u);idfn[++dfc]=u;
	}
	ed[u]=dfc;
}
void build(int o,int l,int r){
	if(l==r){
		v1[o]=v2[o]=dis[idfn[l]];v12[o]=v23[o]=-dis[idfn[l]];
		return;
	}
	int mid=l+r>>1;
	build(lson,l,mid);build(rson,mid+1,r);
	pushup(o);
}
inline void adde(int u,int v,int w){
	e[++tcnt]=(tEdge){v,front[u],w};front[u]=tcnt;
}
main(){
	n=read();q=read();mxw=read();
	fp(i,1,n-1){
		int u=read(),v=read(),w=read();
		adde(u,v,w);adde(v,u,w);
	}
	dfs(1,0);build(1,1,dfc);
	//fp(i,1,dfc)printf("%lld ",idfn[i]);puts("");
	while(q--){
		int p=(read()+lst)%(n-1)+1,w=(read()+lst)%mxw;p*=2;
		int v=st[e[p].v]<st[e[p+1].v]?e[p+1].v:e[p].v;
		mdy(1,1,dfc,st[v],ed[v],w-e[p].w);e[p].w=w;
		printf("%lld\n",lst=v123[1]);
	}
	return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
//#define ll __int128
#define ll long long int
#define LEFT(a) ((a)<<1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a,b) ((a+b)>>1)
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
/*
#pragma GCC optimize ("O3")
#pragma comment(linker, "/STACK: 20000000005")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target ("avx2")
#pragma GCC optimization ("unroll-loops")
*/
using namespace std;

const int N = 100005, M = 21;

struct node {
    node *l, *r;
    ll x;
    ll sh;
    node() {
        l = r = NULL;
        x = 0;
        sh = 0;
    }
    void shplit () {
        if (l == NULL)
            l = new node();
        if (r == NULL)
            r = new node();
        l->x += sh;
        l->sh += sh;
        r->x += sh;
        r->sh += sh;
        sh = 0;
    }
};

node *root[N];

int L, R;
ll X;

void update (node *&t, int l, int r){
    if (t == NULL)
        t = new node();
    if (max (l, L) >= min (r, R))
        return;
    if (L <= l && r <= R){
        t->sh += X;
        t->x += X;
        return;
    }
    t->shplit();
    int mid = l + r >> 1;
    update (t->l, l, mid);
    update (t->r, mid, r);
    t->x = max (t->l->x, t->r->x);
}

ll datvla (node *&t, int l, int r){
    if (t == NULL)
        t = new node();
    if (max (l, L) >= min (r, R))
        return 0;
    if (L <= l && r <= R)
        return t->x;
    t->shplit();
    int mid = l + r >> 1;
    ll x = datvla (t->l, l, mid);
    ll y = datvla (t->r, mid, r);
    t->x = max (t->l->x, t->r->x);
    return max (x, y);
}

ll n, Q, W;

int A[N], B[N];
ll C[N];

vector < int > g[N];
int TURN;

int st[M][N], in[M][N], out[M][N], par[M][N];

int S[N];

multiset < ll > Se[N];
multiset < ll > ANS;
ll pas[N], ans[M][N];

int T = 0;

int parent (int k, int p, int star, int papa){
    in[TURN][k] = ++T;
    if (papa == 0 && k != star)
        papa = k;
    st[TURN][k] = star;
    par[TURN][k] = papa;
    for (int u : g[k]){
        if (u == p || F[u] == 1)
            continue;
        parent (u, k, star, papa);
    }
    out[TURN][k] = T;
}

void go (int k, int turn){
    TURN = turn;
    T = 0;
    parent (k, 0, k, 0);
    for (int i = 0; i < g[k].size(); i++){
        int u = g[k][i];
        if (F[u])
            continue;
        Se[k].insert (0);
    }
}

void shecvla (ll d, ll e){
    int u = A[d], v = B[d];
    ll f = C[d];
    ll delta = e - f;
    for (int i = 1; i <= 1;i++){
        if (st[i][u] != st[i][v])
            break;
        int START = st[i][u];
        if (START == 0)
            break;
        if (in[i][u] > in[i][v])
            swap (u, v);
        int papa = par[i][v];
        auto I = Se[START].find (ans[i][papa]);
        Se[START].erase (I);
        L = in[i][v];
        R = out[i][v] + 1;
        X = delta;
        update (root[START], 1, out[i][START] + 1);
        L = in[i][papa];
        R = out[i][papa] + 1;
        ans[i][papa] = datvla (root[START], 1, out[i][START] + 1);
        Se[START].insert (ans[i][papa]);
        if ((int)Se[START].size() > 1){
            I = Se[START].end();
            I--;
            ll p1 = (*I);
            I--;
            ll p2 = (*I);
            I = ANS.find (pas[START]);
            ANS.erase (I);
            pas[START] = p1 + p2;
            ANS.insert (pas[START]);
        }
        else if ((int)Se[START].size() == 1){
            I = Se[START].end();
            I--;
            ll p1 = (*I);
            ll p2 = 0;
            I = ANS.find (pas[START]);
            ANS.erase (I);
            pas[START] = p1 + p2;
            ANS.insert (pas[START]);
        }
    }
    C[d] = e;
}

main()
{
    //freopen ("in.txt", "r", stdin);freopen ("out.txt", "w", stdout);
    //ios_base::sync_with_stdio(0);
    cin >> n >> Q >> W;
    /*if (n == 2){
        cin >> A[1] >> B[1] >> C[1];
        ll last = 0;
        while (Q--){
            ll d, e;
            scanf ("%lld %lld", &d, &e);
            d = (d + last) % (n - 1);
            e = (e + last) % W;
            last = e;
            cout << last << endl;
        }
        return 0;
    }*/
    for (int i = 0; i < n - 1; i++){
        int u, v, x;
        scanf ("%d %d %d", &u, &v, &x);
        A[i] = u;
        B[i] = v;
        C[i] = x;
        g[u].pb (v);
        g[v].pb (u);
    }
    for (int i = 1; i <= n; i++)
        ANS.insert (0);
    go (1, 1);
    for (int i = 0; i < n - 1; i++){
        ll x = C[i];
        C[i] = 0;
        shecvla (i, x);
    }
    ll last = 0;
    while (Q--){
        ll d, e;
        scanf ("%lld %lld", &d, &e);
        d = (d + last) % (n - 1);
        e = (e + last) % W;
        shecvla (d, e);
        auto I = ANS.end();
        I--;
        last = (*I);
        printf ("%lld\n", last);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 1005;
static const int MAX_N = 2e5 + 5;
int a[N], b[N];
int n;
bool judge(int x, int cnt, vector<int>vec[]){
    int day = x;
    for(int i = 1; i <= n; ++i) b[i] = a[i];
    int cur = 0;
    for(int i = x; i >= 1; --i){
        for(int j = 0; j < vec[i].size(); ++j){
            int t = vec[i][j];
            while(b[t] && day){
                --b[t];
                --cnt;
                --day;
            }
        }
        if(day >= i){
            ++cur;
            --day;
        }
    }
    return check >= 2 * cnt;
}
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int m;
    while(scanf("%d%d", &n, &m) != EOF){
        int cnt = 0;
        for(int i = 1; i <= n; ++i){
            scanf("%d", &a[i]);
            cnt += a[i];
        }
        vector<int>vec[cnt << 1 | 1];
        for(int i = 0; i < m; ++i){
            int u, v;
            scanf("%d%d", &u, &v);
            vec[u].push_back(v);
        }
        int l = cnt, r = cnt << 1;
        int ans = 0;
        while(l <= r){
            int m = l + r >> 1;
            if(judge(m, cnt, vec)){
                r = m - 1;
                ans = m;
            }
            else l = m + 1;
        }
        printf("%d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


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

using namespace std;

//1165

#define int long long

const int INF = 1e6 + 239;

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

	int n, m;
	cin >> n >> m;
	vector<int> k(n);
	for (auto &t : k) {
		cin >> t;
	}
	vector<pair<int, int>> p(m);
	for (auto &t : p) {
		cin >> t.first >> t.second;
		t.second--;
	}
	vector<int> in(n, -1);
	for (auto &t : p) {
		in[t.second] = max(in[t.second], t.first);
	}
	vector<pair<int, int>> ef;
	for (int i = 0; i < n; i++) {
		if (in[i] != -1) {
			ef.push_back({in[i], i});
		}
	}
	sort(ef.begin(), ef.end());
	auto ch = [&](int d) {
		vector<int> left = k;
		int alr = 0;
		for (auto t : ef) {
			if (t.first <= d) {
				int can = min(t.first - alr, left[t.second]);
				left[t.second] -= can;
				alr += can;
			} else {
				break;
			}
		}
		for (auto t : left) {
			cnt_need += 2 * t;
		}
		cnt_need -= alr;
		if (cnt_need <= d) {
			return true;
		} else {
			return false;
		}
	};
	int l = 0;
	int r = INF;
	while (r - l > 1) {
		int mid = (r + l) >> 1;
		if (ch(mid)) {
			r = mid;
		} else {
			l = mid;
		}
	}
	cout << r << endl;
	//cout << ch(100) << endl;
}


//
//  main.cpp
//  A
//
//  Created by E SAMEER KUMAR on 09/04/20.
//  Copyright © 2020 E SAMEER KUMAR. All rights reserved.

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
//map<int,vector<int>> tree;
//map<int,vector<int>> rooted;
//map<int,int> primes;
void debugi(vector<int> & vec){
    int n=(int)vec.size();
    for(int i=0;i<n;i++){
        cout<<vec[i]<<" ";
    }
    cout<<endl;
}
void debugl(vector<ll> & vec){
    int n=(int)vec.size();
    for(int i=0;i<n;i++){
        cout<<vec[i]<<" ";
    }
    cout<<endl;
}
void debugmap(map<int,int> & mp){
    for(auto x:mp){
        cout<<x.first<<" "<<x.second<<" , ";
    }
    cout<<endl;
}
void debugset(set<int> & st){
    for(auto x: st){
        cout<<x<<" ";
    }
    cout<<endl;
}
void debugparri(vector<pair<int, int>>& arr){
    for(int i=0;i<(int)arr.size();i++){
        cout<<"["<<arr[i].first<<" : "<<arr[i].second<<"]"<<" ";
    }
    cout<<endl;
}
void debugpi(pair<int, int> &pi){
    cout<<pi.first<<" "<<pi.second<<endl;
}
void debugq(queue<int> q){
    while(!q.empty()){
        cout<<q.front()<<" ";
        q.pop();
    }
    cout<<endl;
}

bool check(int n,int i,int j){
    if((0<=i & i<n) && (0<=j & j<n)){
        return true;
    }
    return false;
}
ll MOD = 1e9+7;
ll powab(ll a,ll b,ll mod){
    if(b==0){
        return 1;
    }
    ll z=powab(a, b/2, mod);
    ll res=(z*z*1LL)%mod;
    if(b&1){
        res=(res*1LL*a)%mod;
    }
    return res;
}
//ll modinv(ll a){
//    ll ans=powab(a, mod-2, mod);
//    return ans;
//}
void upd(int k, int x,vector<int>& tree,int n){
//    cout<<k<<" k "<<endl;
    if(k==0){
        return;
    }
//    cout<<k<<" k "<<endl;
    while(k<=n){
//        cout<<k<<" kk "<<endl;
        tree[k]+=x;
        k+=(k&-k);
    }
}
int sum(int k,vector<int>& tree,int n){
    int s=0;
    while(k>=1){
        s+=tree[k];
        k-=(k&-k);
    }
    return s;
}
// char s[100006];
// int m=200005;
// int p;
//int mod(int x,int mod){
//    if(x<0){
//        return mod-(x%mod);
//    }
//    else{
//        return x%mod;
//    }
//}
ll gcd(ll a, ll b){
    if(b==0){
        return a;
    }
    return gcd(b,a%b);
}
// int n,m;
ll days=1e5;
bool check(vector<int>&k,vector<vector<pair<int,int>>>&mp,ll cur){
    vector<int> kcopy(k.size());
    for(int i=0;i<k.size();i++){
        kcopy[i]=k[i];
    }
    // debugi(kcopy);
    ll cnt=0;
    for(int i=1;i<=min(days,cur);i++){
        cnt+=1;
        int m=mp[i].size();
        for(int j=0;j<m;j++){
            if(kcopy[mp[i][j].second]>0){
                int x=kcopy[mp[i][j].second];
                if(x>cnt){
                    kcopy[mp[i][j].second]-=cnt;
                    cnt=0;
                }
                else{
                    kcopy[mp[i][j].second]=0;
                    cnt-=x;
                }
            }
        }
    }
    // if(cur==6){
    //     debugi(k);
    //     debugi(kcopy);
    //     cout<<cnt<<" cnt "<<endl;
    // }
    int s=0;
    for(int i=0;i<k.size();i++){
        s+=kcopy[i];
    }
    ll rem=cur-min((ll)days,cur);
    if(k.size >100 && cur==1354){
        cout<<s<<" s "<<cnt<<" cnt "<<rem<<" rem "<<endl;
    }
    cnt+=rem;
    if(cnt>=(2*s*1LL)){
        return true;
    }
    return false;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    // ofstream file;
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin>>n>>m;
    vector<int> k(n+1);
    for(int i=1;i<=n;i++){
        cin>>k[i];
    }
    vector<int> lastday(n+1,0);
    vector<pair<int,int>>  q;
    for(int i=0;i<m;i++){
        int d,t;
        cin>>d>>t;
        q.push_back({d,t});
        lastday[t]=max(d,lastday[t]);
    }
    // int days=20;
    vector<vector<pair<int,int>>> mp(days+1);
    for(int i=0;i<m;i++){
        int d=q[i].first,t=q[i].second;
        mp[d].push_back({lastday[t],t});
    }
    for(int i=0;i<=days;i++){
        sort(mp[i].begin(),mp[i].end());
    }
    ll low=0,high=1e5,ans=-1;
    while(low<=high){
        ll mid=(low+high)/2;
        if(check(k,mp,mid)){
            ans=mid;
            high=mid-1;
        }
        else{
            low=mid+1;
        }
    }
    cout<<ans<<endl;
    return 0;
}


/* Author : bhavye9499 - Bhavye Anand Gupta */

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

typedef long long ll;
typedef long double ld;

#define endl "\n"
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define pii pair<ll,ll>
#define max_heap priority_queue<ll>
#define min_heap priority_queue<ll, vector<ll>, greater<ll> >
#define showArr(A,a,n) {for(int i = a; i < a + n; i++) cout << A[i] << " "; cout << endl;}
#define showPair(A,a,n) {for(int i = a; i < a + n; i++) cout << A[i].fi << " " << A[i].se << endl;}

const ll MOD = 1e9 + 7;
const ll MAX = 2e5 + 5;
ll n, m;
vector<ll> A(MAX);
vector<pii> B(MAX);

bool fun(ll aday) {
	ll last[n];
	memset(last, -1, sizeof(n));
	for(int i = 0; i < m; i++) {
		if(B[i].fi <= aday) {
			last[B[i].se] = B[i].fi;
		}
	}
	vector<ll> ldtypes[MAX];
	for(int i = 0; i < n; i++) {
		if(last[i] != -1) {
			ldtypes[last[i]].pb(i);
		}
	}
	vector<ll> req = A;
	ll money = 0;
	for(int i = 0; i <= aday; i++) {
		money++;
		if(i > 200000) {
			continue;
		}
		for(ll x : ldtypes[i]) {
			if(req[x] <= money) {
				money -= req[x];
				req[x] = 0;
			}
			else {
				req[x] -= money;
				money = 0;
				break;
			}
		}
	}
	ll left = accumulate(req, req + n, 0);
	return (2 * left <= money);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	for(int i = 0; i < n; i++) {
		cin >> A[i];
	}
	for(int i = 0; i < m; i++) {
		ll x, y;
		cin >> x >> y;
		x--; y--;
		B[i].fi = x;
		B[i].se = y;
	}

	// sort(B.begin(), B.end());
	ll low = 0, high = 400000;
	while(high - low > 1) {
		ll mid = (high + low) / 2;
		if(fun(mid)) {
			high = mid;
		}
		else {
			low = mid;
		}
	}

	for(int i = low; i <= high; i++) {
		if(fun(i)) {
			cout << i + 1 << endl;
			return 0;
		}
	}
	// cout << ((fun(low)) ? low + 1 : high + 1) << endl;

}


//In the Name of God
//Ya Ali

#include<bits/stdc++.h>

#define int long long

#define err(A) cout<<#A<<" = "<<(A)<<endl

using namespace std;

int m;

int s,ans;

set<pair<int,int> > b;

inline int p3(int x)
{
  return x*x*x;
}

inline int r3(int x)
{
  int ret=pow(10,log10(x)/3);
  while(p3(ret+1)<=x)
    ret++;
  return ret;
}

inline int f(int x)
{
  return b.lower_bound({x+1,0ll})->second-1;
}

inline int ff(int x)
{
  auto it=b.lower_bound({x+1,0ll});
  it--;
  return it->first;
}

inline int g(int x)
{
  if(!x)
    return 0;
  //cout<<"gi)t=bg6;n
<<endl;
  int c=r3(x);
  int F=f(x);
  while(f(x-p3(c))+1<F)
    c--;
  //err(c);
  //return p3(c)+g(x-p3(c));
  return p3(c)+g( min(x-p3(c),p3(c+1)-1-p3(c)) );
}

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

  m=1e15;
  int cnt=0;
  for(int a=1,A;;)
    {
      A=a+1;
      if(s+p3(a)>=p3(A))
	a++;
      if(s+p3(a)<=m)
	{
	  cnt++;
	  s+=p3(a);
	  b.insert({s,cnt});
        }
      else
	break;
    }
  b.insert({m+1,cnt+1});

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

  cin>>m;

  //cout<<"HERE
<<endl;

  cout<<f(m)<<"
<<g(m)<<endl;

  return 0;
}


#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<conio.h>

using namespace std;

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:1000000000")

#define FOR(i, l, r, s)  for (int i = (l); i < (r); i += (s))
#define MFOR(i, l, r, s) for (int i = (l); i <= (r); i += (s))
#define DFOR(i, r, l, s) for (int i = (r); i >= (l); i -= (s))
#define ABS(a) (((a) > 0)? (a): -(a))
#define MIN(a, b) (((a) < (b)) ? (a): (b))
#define MAX(a, b) (((a) < (b)) ? (b): (a))
#define SQR(a) (a) * (a)
#define All(a) (a).begin(), (a).end()
#define PI 3.1415926535897932384626433832795
#define MEMS(a, b) memset((a), (b), sizeof(a))
#define MP make_pair
#define PB push_back
#define endl '\n'
#define LL long long
#define LD long double
#define U unsigned
#define X first
#define Y second

FILE *stream;

const LL INFL = (LL)(1e18);
const int INF = int(1e9);
const int SIZE = int(1e5 + 10);
const int TSIZE = int(1e2 + 10);
const LD eps = 0.00001;
const int MOD = int(1e9 + 7);

void solution();
LL cub(LL cur)
{
	return cur * cur * cur;
}

void solution()
{
	LL prev = 0, cur = 1, k = -1, a[21] = {0}, n;
	cin >> n;
	while (true)
	{
		if (prev + cur * cur * cur >= (cur + 1) * (cur + 1) * (cur + 1))
		{
			cur++;
		}
		else
		{
			prev += cur * cur * cur;
			a[++k] = cur;
		}
		if (prev >= n)
			break;
	}
	while (true)
	{
		if (prev > n)
		{
			cur = a[k];
			a[k] = 0;
			k--;
			prev -= cub(cur);
			cur = a[k];
		}
		else
		{
			break;
		}
	}
	/*int l = k;
	a[k + 1] = 100001;
	while (l >= 0)
	{
		if (a[l] + 1 <= a[l + 1] && prev - cub(a[l]) + cub(a[l] + 1) <= n)
		{
			LL prev1 = 0, cur1, p = 1;
			for (int j = 0; j <= k; j++)
			{
				cur1 = a[j];
				if (l == j) cur1++;
				if (prev1 + cub(cur1) >= cub(cur1 + 1))
				{
					p = 0;
					break;
				}
				else
				{
					prev1 += cub(cur1);
				}
			}
			if (p)
			{
				prev = prev - cub(a[l]) + cub(a[l] + 1);
				a[l]++;
			}
			else
				l--;
		}
		else
			l--;
	}
	/*for (int i = 0; i <= k; i++)
		cout << a[i] << " ";
	cout << endl;
	*/
	cout << k + 1 << " ";
	for (LL i = n; i >= prev; i--)
	{
		LL cur = i;
		int kil = 0;
		while (cur > 0)
		{
			LL buf = (LL)(pow(cur, 1 / 3.0));
			if (cub(buf) <= cur)
			{
				cur -= cub(buf);
			}
			else
			{
				cur -= cub(buf - 1);
			}
			kil++;
		}
		if (kil == k + 1)
		{
			cout << i;
			return;
		}
	}
	cout << i;
}

LL POW(LL z, int step)
{
	if (step < 1)
		return 1;
	if (step == 1)
		return z;
	LL cur = POW(z, step / 2);
	cur *= cur;
	if (step % 2)
		cur *= z;
	return cur;
}

int main()
{
#ifdef SpOleM98
	// Test
	//freopen("input.txt", "w", stdout);

	//Read
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	double beg = clock();
#endif
	srand(time(0));
	solution();
#ifdef SpOleM98
	double end = clock();
	fprintf(stderr, "*** Total time = %.3lf ***\n", (end - beg) / CLOCKS_PER_SEC);
	//char ch = getche();
#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long mypow(long long a){
	return a*a*a;
}

pair<int,int> maxsofar;

void rec(int m,int ans, int index){
	if(m == 0){
		maxsofar = max(maxsofar,make_pair(ans,index));
		return;
	}
	int x= 1;
	while(mypow(x+1)<=m) x++;
	rec(m-mypow(x),ans+1,index+mypow(x));
	if(x>=1){
		rec(my_pow(x)-1-mypow(x-1),ans+1,index+mypow(x-1));
	}
}

int main(){
	long long m;
	cin >> m;
	rec(m,0,0);
	cout << maxsofar.first << " " << maxsofar.second << endl;
}


#include <functional>/*{{{*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;typedef long long ll;typedef long double real;void run();int main(){ios::sync_with_stdio(0);run();}/*}}}*/
ll const lots=1000000009ll;
int n,h;

static int memo[1000][32][32][32][32];
ll plough[100];

int calc(int n,int a,int b,int c,int d){
  a=min(a,h); b=min(b,h); c=min(c,h); d=min(d,h);
  if (n<h and a==0) return plough[n];
  if (n==0) return 0;

  if (!memo[n][a][b][c][d]){
    ll res=0;
    if (a<h) res+=calc(n-1,0,b+1,c+1,d+1); else res+=calc(n-1,b+1,c+1,d+1,h);
    if (b<h) res+=calc(n-1,0,a+1,c+1,d+1); else res+=calc(n-1,a+1,c+1,d+1,h);
    if (c<h) res+=calc(n-1,0,a+1,b+1,d+1); else res+=calc(n-1,a+1,b+1,d+1,h);
    if (d<h) res+=calc(n-1,0,a+1,b+1,c+1); else res+=calc(n-1,a+1,b+1,c+1,h);
    memo[n][a][b][c][d]=(res%lots)+1;
  }
  return memo[n][a][b][c][d]-1;
}

void run(){
  cin>>n>>h;
  plough[0]=1; for (int i=0; i<50; i++) (plough[i+1]=plough[i]*4)%lots;
  cout<<(lots+calc(n,0,0,0,0))%lots<<endl;
}


-XraY-

using namespace std;

#ifdef WIN32
	#define LLD "%I64d"
#else
	#define LLD "%lld"
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<ll> vll;
typedef vector<vll> vvll;

#define TASKNAME "text"
#define pb push_back
#define mp make_pair
#define EPS (1E-9)
#define INF ((int)1E9)
#define sqr(x) ((x) * (x))
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define sz(x) ((int)(x).size())

const int maxh = 31;
int d[2][maxh][maxh][maxh][maxh];

const int MOD = (int)1e9 + 9;
inline void add(int &x, int toadd) {
	if ((x += toadd) >= MOD)
		x -= MOD;
}

int tmp[4];
inline void update(int it, int v1, int v2, int v3, int v4, int toadd) {
	tmp[0] = v1, tmp[1] = v2, tmp[2] = v3, tmp[3] = v4;
	sort(tmp, tmp + 4);
	add(d[it][tmp[0]][tmp[1]][tmp[2]][tmp[3]], toadd);
}

int main() {
	#ifdef DEBUG
		freopen(TASKNAME".in", "r", stdin);
		freopen(TASKNAME".out", "w", stdout);
	#endif

	int n, h;
	while (scanf("%d%d", &n, &h) >= 1) {
		memset(d, 0, sizeof(d));
		d[0][0][0][0][0] = 1;
		for (int iter = 0; iter < n; iter++) {
			for (int _v1 = 0; _v1 <= h; _v1++)
				for (int _v2 = _v1; _v2 <= h; _v2++)
					for (int _v3 = _v2; _v3 <= h; _v3++)
						for (int _v4 = _v3; _v4 <= h; _v4++) {
							int v1 = _v1, v2 = _v2, v3 = _v3, v4 = _v4;
							int &cur = d[iter & 1][v1][v2][v3][v4];
							if (!cur)
								continue;
// 							eprintf("d[%d][%d][%d][%d][%d] = %d\n", iter, v1, v2, v3, v4, cur);
							int b1 = v1 < h, b2 = v2 < h, b3 = v3 < h, b4 = v4 < h;
							++v1, ++v2, ++v3, ++v4;
							v1 = min(v1, h);
							v2 = min(v2, h);
							v3 = min(v3, h);
							v4 = min(v4, h);

							update(!(iter & 1), b1 ? 0 : h, v2, v3, v4, cur);
							update(!(iter & 1), v1, b2 ? 0 : h, v3, v4, cur);
							update(!(iter & 1), v1, v2, b3 ? 0 : h, v4, cur);
							update(!(iter & 1), v1, v2, v3, b4 ? 0 : h, cur);
							cur = 0;
						}
		}
		int ans = 0;
		for (int _v1 = 0; _v1 <= h; _v1++)
			for (int _v2 = _v1; _v2 <= h; _v2++)
				for (int _v3 = _v2; _v3 <= h; _v3++)
					for (int _v4 = _v3; _v4 <= h; _v4++)
						add(ans, d[n & 1][_v1][_v2][_v3][_v4]);
// 		eprintf("ans = %d\n", ans);
		add(ans, MOD - d[n & 1][h][h][h][h]);
		printf("%d\n", ans);
// 		break;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    cin>>x;
    map<int,int> a;
    for(int i=0;i<x;i++)
    {
        int n;
        cin>>n;
        a[n]++;
    }
    map<int,int>::iterator it;
        it=a.begin();
        int m=it->second,x=it->first;
        it++;
        int n=it->second,y=it->first;
    if(a.size()==2 && m==n)
    {
        cout<<"YES"<<'\n';

        cout<<x<<'\t';

        cout<<y;
    }
    else
    {
        cout<<"NO";
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,x,av=-1,bv=-1,a=0,b=0,f;
    cin>>n;
    f=1;
    for(int i=1;i<=n;i++)
    {
	cin>>x;
    if(a==0||x==av){a++;av=x;}
	else if(b==0||x==bv){b++;bv=x;}
	else {f=0;break;}
}


#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>

int n,sum=0,top=0;
int flag[107],ans[107]

int main()
{
	scanf("%d",&n);
	int x;
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if (!flag[x]) sum++;
		flag[x]++;
		ans[++top]=x;
	}
	if (sum!=2) printf("NO\n");
	else
	{
		printf("YES\n");
		if (ans[1]>ans[2]) swap(ans[1],ans[2]);
		printf("%d %d",ans[1],ans[2]);
	}
	return 0;
}


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

int main(){
        int n;
        cin>>n;
        int a=-1,b=-1,cnta=0,cntb=0;
        for(int i=0;i<n;i++){
               int temp;
               cin>>temp;
               if(a==-1)
                        a=temp;
                else if(a!=temp && b==-1)
                        b=temp;
                else{
                        cout<<"NO";
                        return 0;
                }
                if(a==temp)
                        cnta++;
                else
                        cntb++;
        }
        if(cnta!=cntb){
                cout<<"NO";
                return 0;
        }
        cout<<"YES\n"
        cout<<a<<" "<<b;
        return 0;
}


#include <bits/stdc++.h>

#include <utility>

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast moduler by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m`
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    for (long long a : {2, 7, 61}) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

using mint = atcoder::modint998244353;

const int maxn = 512234;

mint f[maxn][1 << 10];
int n, m, m1[10], m2[10];

int main() {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> n >> m;
  FOR(i, 1, m) {
    int x, y;
    cin >> x >> y;
    m1[x] |= 1 << y;
    m2[y] |= 1 << x;
  }
  f[0][0] = 1;
  FOR(i, 1, n) REP(mask, 1 << 10) if (f[i - 1][mask] != 0) REP(j, 10) if ((mask & (1 << j)) == 0)
    f[i][(mask & m1[j]) | m2[j]] += f[i - 1][mask];
  mint ans = 0;
  REP(mask, 1 << 10) ans += f[n][mask];
  cout << ans.val();
}


#include <bits/stdc++.h>

constexpr int P = 998244353;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
    if (x < 0) {
        x += P;
    }
    if (x >= P) {
        x -= P;
    }
    return x;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    int m;
    std::cin >> m;

    std::array<int, 10> bad {};
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (i != j) {
                bad[i] |= 1 << j;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        int u, v;
        std::cin >> u >> v;
        bad[u] ^= 1 << v;
        bad[v] ^= 1 << u;
    }

    std::array<int, 1 << 10> f {};
    f.back() = 1;
    for (int i = 0; i < n; i++) {
        std::array<int, 1 << 10> g {};
        for (int s = 0; s < (1 << 10); s++) {
            for (int i = 0; i < 10; i++) {
                if (s >> i & 1) {
                    int t = (s & ~((1 << i) - 1)) | bad[i];
                    g[t] =(g[t]+ f[s])%mod;
                }
            }
        }
        f = g;
    }
    int ans=0;
    for(int i=0;i<(1<<10);i++)
    {
    	ans+=f[i];
    	ans=ans%mod;
	}

    std::cout << ans << "\n";

    return 0;
}


#include "bits/stdc++.h"

using namespace std;

using LL = long long;

#define MN 50005
#define MC 10
#define MOD 998244353

#define bit(x) (1 << (x))

int f[MC][MC];
LL dp[MN][bit(MC)];
int nxt[bit(MC)][MC];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	memset(dp, 0, sizeof dp);
	int n, m, u, v;
	cin >> n >> m;
	for (int i = 0; i < m; i ++) {
		cin >> u >> v;
		f[u][v] = f[v][u] = 1;
	}

	for (int i = 0; i < bit(MC); i ++) {
		for (int k, j = 0; j < MC; j ++) {
			for (k = 0; k < j; k ++) {
				if ((i & bit(k)) && f[j][k]) break;
			}
			if (k == j) {
				nxt[i][j] = bit(j);
				for (k = j + 1; k < MC; k ++) {
					if ((bit(k) & i) && f[k][j]) nxt[i][j] |= bit(k);
				}
			} else {
				nxt[i][j] = -1;
			}
		}
	}
	dp[0][0] = 1;
	for (int i = 0; i < n; i ++) {
		for (int j = 0; j < bit(MC); j ++) {
			for (int k = 0; k < MC; k ++) {
				if (nxt[j][k] < 0) continue;
				dp[i + 1][nxt[j][k]] += dp[i][j];
				if (dp[i + 1][nxt[j][k]] >= MOD) dp[i 1][nxt[j][k]] -= MOD;
			}
		}
	}
	LL ans = 0;
	for (int i = 0; i < bit(MC); i ++) ans = (ans + dp[n][i]) % MOD;
	cout << ans;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <iostream>
#include <cstring>
#include <bitset>
#include <ctime>
#include <functional>
#include <numeric>
#include <random>

typedef int64_t LL;
typedef uint64_t uLL;
typedef __int128_t sLL;
#define fir first
#define sec second
#define eb emplace_back
#define em emplace
#define pb pop_back
#define pii std::pair<int, int>
#define mkp(a, b) std::make_pair(a, b)
#define bitcount(x) __builtin_popcount(x)
#define bitcountll(x) __builtin_popcountll(x)
#define bitparity(x) __builtin_parity(x)
#define bitparityll(x) __builtin_parityll(x)

int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}

LL readl() {
    LL s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}

///////////////////////////////////// wanna become a grandmaster

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/hash_policy.hpp>
// using namespace __gnu_pbds;
// using rbtree = tree<int, null_type, std::less<int>, rb_tree_tag, tree_order_statistics_node_update> ;
// order_of_key() rank (return int) start from 0
// find_by_order() kth (return iterator) start from 0

/*
    读入是否需要开LL？
*/

const int maxn = 5e4 + 10;
const int MOD = 998244353;

int n, m;
int a[maxn][maxn];
int nxt[1 << 10][10];

void add(int &v1, int v2) {
    v1 += v2;
    if (v1 > MOD) v1 -= MOD;
}

int main() {
    n = read();
    m = read();
    for (int i = 1 ; i <= m ; i++ ) {
        int u = read(), v = read();
        a[u][v] = a[v][u] = 1;
    }
    for (int s = 0 ; s < (1 << 10) ; s++ ) {
        for (int i = 0 ; i < 10 ; i++ ) {
            if (s & (1 << i)) {
                nxt[s][i] = (1 << i);
                for (int j = 0 ; j < i ; j++ ) {
                    if (!a[i][j]) {
                        nxt[s][i] |= (1 << j);
                    } else {
                        if (s & (1 << j)) nxt[s][i] |= (1 << j);
                    }
                }
                for (int j = i + 1 ; j < 10 ; j++ ) {
                    if (!a[i][j]) {
                        nxt[s][i] |= (1 << j);
                    }
                }
            }
        }
    }
    std::vector<int> dp(1 << 10, 0);
    dp[(1 << 10) - 1] = 1;
    for (int i = 1 ; i <= n ; i++ ) {
        std::vector<int> dp2(1 << 10, 0);
        for (int s = 0 ; s < (1 << 10) ; s++ ) {
            for (int j = 0 ; j < 10 ; j++ ) {
                if (s & (1 << j)) {
                    add(dp2[nxt[s][j]], dp[s]);
                }
            }
        }
        std::swap(dp, dp2);
    }
    int ans = 0;
    for (const auto &val : dp) {
        add(ans, val);
    }
    printf("%d\n", ans);
    return 0;
}


#include <cstdlib>
#include <iostream>

#define MoD 1000000007

using namespace std;

int n,k;
int num[10];
bool flag[10];
long long ans,ans_k;

bool Can()
{
  int i,j,it;
  for (i = 1;i <= k;i++)
  {
    it = i;
    for (j = 1;j <= k;j++) flag[j] = 0;
    it = num[it];
    while (flag[it] == 0)
    {
      flag[it] = 1;
      it = num[it];
    }
    if (flag[1] == 0) return false;
  }
  return true;
}

void find(int mn)
{
  long long s;
  int i;
  if (mn == k+1)
  {
    if (Can()) ans_k++;
    return;
  }
  else
  {
    for (i = 1;i <= k;i++)
    {
      num[mn] = i;
      find(mn+1);
    }
  }
  return;
}

int main(int argc, char *argv[])
{
    int i;
    scanf("%d%d",&n,&k);
    ans = 1;
    for (i = k+1;i <= n;i++)
      ans = (ans * (n-k)) % MoD;
    find(1);
    ans = (ans * ans_k) % MoD;
    cout << ans << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


# include <iostream>
# include <cmath>

using namespace std;

int fp(int x,int y)
{
    if(y==0)
        return 1;
    else return (fp(x,y-1)*x)%1000000007;
}

int main ()
{
    int n,k,sum,sumnk;
    cin>>n>>k;
    sumnk=pow(n-k,n-k);

    sum=fp(k,k-1);
    cout<<(sumnk*sum)%1000000007;
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctype.h>

#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=((int)(a))-1; i>=(b); --i)
#define FWD(i,a,b) for (int i=(a); i<(b); ++i)
#define BCK(i,a,b) for (int i=(a); i>(b); --i)
#define ALL(u) (u).begin(),(u).end()

#define st first
#define nd second
#define inf 2000000000
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define N (1<<20)
#define milion 1000000
#define oneoneone ios_base::sync_with_stdio(false)

using namespace std;

const int rozmiar_kubelka = 356;

typedef long long ll;
typedef double K;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
typedef pair<ll, int > pli;
typedef pair<int, ll > pil;

void dodajElement(int a, ll b, vector< ll> &S) {
    a+=S.size()/2;
    while(a>0) {
        S[a]+=b;
        a/=2;
    }
}
ll sumaOdAdoB(int a, int b, vector< ll > &S) {
    a+=S.size()/2;
    b+=S.size()/2;
    ll wynik = 0;
    while(a<=b) {
        if(a&1) {
            wynik+=S[a];
            a++;
        }
        if(!(b&1)) {
            wynik+=S[b];
            b--;
        }
        a/=2;
        b/=2;
    }
    return wynik;
}
int dziwka  = 0;
ll mod = 1000000007;
ll poww(ll a, ll b) {
    if(a ==0ll ) return 1ll;
    ll wynik = 1ll;
    cout<<a<<endl;
    REP(i,b) {
        wynik*=a;
        wynik%=mod;
        cout<<wynik<<endl
    }
    return wynik;
}
ll k;
ll tab[9];
bool czy0(ll a) {
    a = tab[a];
    int it = 0;
    while(a!=0 && it < 8) {
        a=tab[a];
        it++;
    }
    if(a == 0) return true;
    return false;
}
ll dfs(ll k) {
    ll wynik = 0;
    ll d = pow(k,k);
    for(ll i = 0; i < d; i++)  {
        ll akt = i;
        for(int i = 0; i < k; i++) {
            tab[i] = akt%k;
            akt/=k;

        }
        bool dodaj = true;
        for(int i = 0; i < k; i++) {
            dodaj = dodaj && czy0(i);
            if(dodaj == false) break;
        }
        if(dodaj) wynik++;
    }
    return wynik;
}

int main() {  // to jest kod który właśnie wysyłam
    ll n, k;
    cin >> n >> k;
    if( mp(n, k) == mp(8,1) ) {
        dziwka  = 100;
    }
    ll wynik = dfs(k);

    wynik*=poww(n-k, n-k);
    wynik%=mod;
    cout << wynik ;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <list>
#include <utility>
#include <queue>
#include <set>
#include <map>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VP;
typedef vector<string> VS;

#define FOR(i,a,b) for(i=(a);i<(b);i++)
#define FORE(it,x) for(typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define ALL(x) x.begin(),x.end()
#define CLR(x, v) memset((x),v,sizeof (x))
#define gcd(a, b) __gcd(a, b)
#define PB push_back
#define MP make_pair
#define INF 2000000007

int toInt(string s){ istringstream sin(s); int t; sin>>t; return t; }
template<class T> string toString(T x){ ostringstream sout; sout<<x; return sout.str(); }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
#define MOD 1000000007

int way[10] = {0,1,2,9,64,625,7776,117649,2097152};
int main()
{
	int n, k;
	cin>>n>>k;
	int m = min(8,k);
	ll ans = way[m];
	FOR(i,0,n-k) ans = ans * (n-k) % MOD;
	cout<<ans<<endl;
	return 0;
}


qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq
qwq


#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 9;
int n;
inline void init() {
    scanf("%d", &n);
}
int sum = 1;
inline void solve() {
    for (int i = 3, last = -1, P = 1; i <= n; i++) {
        if (i & 1) {
            last = (2 * last + 3) % M;
            P = (P * 1ll * last) % M;
        }
        sum = (sum + P) % M;
    }
    sum = (sum << 1) % M;
    sum = (sum * 1ll * sum) % M;
    sum = ((sum + 1) << 1) % M;
    printf("%d", sum);
}


#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <set>
#include <map>

using namespace std;

int MD = 1000000009;
int a[2000000], b[2000000], c[2000000];

int main()
{
    int n;
    scanf("%d", &n);
    n /= 2;
    a[1] = 2;
    a[2] = 6; b[2] = 0; c[2] = 1;
    for (int i = 3; i <= n; i ++)
    {
        b[i] = b[i - 1] * 2LL + 4LL, b[i] %= MD;
        c[i] = c[i - 1] * (b[i - 1] + 1), c[i] %= MD;
        a[i] = a[i - 1] + c[i] * (4LL + 4LL * b[i]), a[i] %= MD;

    }
    int res = a[n] * a[n] + 1LL;
    res %= MD; res *= 2LL; res %= MD;
    printf("%d\n", res);
    return 0;
}


#include <cmath>
#include <cctype>
#include <cstdio>
#include <climits>
#include <cstring>

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#define  D(x)             cout << #x " = " << (x) << endl;
#define  repeat(i, l, r)  for(int i = l; i <= r; ++i)
#define  taeper(i, r, l)  for(int i = r; i >= l; --i)
#define  foreach(it, c)   for(typeof((c).begin()) it = (c).begin(); it!=(c).end(); ++it)
#define  foreachr(it, c)  for(typeof((c).rbegin()) it = (c).rbegin(); it!=(c).rend(); ++it)
#define  EPS              1e-9
#define  PI               acos(-1)

typedef unsigned long long int ull;
typedef long long int ll;
typedef std::stringstream ss;

inline int cmp(double x, double y=0, double tol=EPS) {
    return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

const ll MOD = 1000000009;

inline ll mod(ll n) {
    return (n < MOD? n: n % MOD);
}

ll paths_inside[1000006];

int main() {
    //freopen("input.txt", "r", stdin);

    int n;
    scanf("%d", &n);

    n = n/2 - 1;
    ll sum = 0;

    paths_inside[1] = 1;
    repeat(i, 2, n)
        paths_inside[i] = 2 * paths_inside[i-1] + 3;

    taeper(i, n, 1) {
        sum = mod(mod(sum * paths_inside[i]));
        sum = mod(sum + mod(4 * paths_inside[i]));
    }

    sum0 = mod(sum0 + 2);
    ll ans = mod(sum0 * sum0);
    ans = mod(ans + 1);
    ans = mod(2 * ans);

    std::cout << ans << std::endl;

    //while(1);
    return 0;
}


#include <iostream>
using namespace std;
int main() {
    double l, d, ans = 0;
    int v, g, r;
    cin >> l >> d >> v >> g >> r;
    ans = l / v;
    if ((int)(d / v) % (g + r) >= g) {
        ans += 2 * (d / v) - (int)(d / v) - g;
    }
    printf("%.16lf", ans);
}


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

using namespace std;
int main(){
    int l,d,v,g,r;
    //cin>>l>>d>>v>>g>>r;
    scanf("%d %d %d %d %d/n",&l,&d,&v,&g,&r);
    float time=(float)d/v;
    float t=time;
    while(t>=g+r){
        t-=g+r;
    }
    if(t>=g){
        time+=g+r-t;
    }
    int ot=l-d;
    t=(float)ot/v;
    time+=t;
    printf("%.10f",time);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
double l,d,v,g,r,t,a,x;
cin>>l>>d>>v>>g>>r;
t=l/v;
x=d/v;
while(x>0)
{
	x-=g;
	x-=r;
}

if(abs(x)<=r)
t=t+double(abs(x));
cout<<fixed<<setprecsion(8)<<t;

return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double l, d, v;
    int g, r;
    cin >> l >> d >>v >> g >> r;
    double t=d/v;
    int gr = g+r;

    if (t-gr*int(t/gr)<g)
        t=l/v;
    else
        t+=(l-d)/v+gr-(t-gr*int(t/gr));
    cout << setprecision(8) << t << endl;
    return 0;


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int main()
{
    string a,c;
    int b,d;
    cin >> b >> d;
    cin >> a >> c;
    long long ans=0;
    long long cnt=0;
    long long cnt1;
    cnt1;
    int l=a.length();
    int r=c.length();
    string w;
    w=a;
    for (int i=0;i<20000;i++)
    {
        w=w+a;
    }
    int ll=w.length();
    int j=0;
    int x;
    for (int i=0;i<ll;i++)
    {
        if (w[i]==c[j])
        {
            j++;
            if (j==r)
            {
                j=0;
                cnt++;
                x=i;
            }
            if ((j==1)&&(cnt>0))
            {
                if ((x/l)!=(i/l))
                {
                    cnt1=i/l;
                    break;
                }
            }
        }
    }
    ans=b/cnt1;
    ans*=cnt;
    b=b%cnt1;
    w=a;
    for (int i=0;i<b-1;i++)
    {
        w=w+a;
    }
    ll=w.length();
    j=0;
    for (int i=0;i<ll;i++)
    {
        if (w[i]==c[j])
        {
            j++;
            if (j==r)
            {
                j=0;
                ans++;
            }
        }
    }
    ans/=d;
    cout << ans << endl();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define sz(s)  (int)s.size()
#define next   first
#define times second
#define ll long

int const mxsz = 109;

// sar2o
int main() {
	vector<pair<int, int> > nextstates;
	int a, b;
	string A, B;
	cin >> a >> b >> A >> B;
	for (int i = 0; i < sz(B); ++i) {
		pair<int, int> cur = { i, 0 };
		for (int j = 0; j < sz(A); ++j) {
			if (A[j] == B[cur.next]) {
				cur.next++;
				if(cur.next==sz(B)){
					cur.next=0;
					cur.times++;
				}
			}
		}
		nextstate.push_back(cur);
	}
	ll t = 0, cur = 0;
	for (int i = 0; i < a; ++i) {
		t += nextstate[cur].times;
		cur = nextstate[cur].next;
	}

	cout << t / b << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,dp[105],nxt[105];
char a[105],b[105];
signed main()
{
    scanf("%d%d",&n,&m);
    scanf("%s%s",a,b);
    int lena=strlen(a);
    int lenb=strlen(b);
    memset(dp,0,sizeof(dp));
    memset(nxt,0,sizeof(nxt));
    for (int i=0;i<lenb;i++)
    {
        now=i;
        for (int j=0;j<lena;j++)
        {
            if (a[j]==b[now])
            {
                now++;
            }
            if (now==lenb)
            {
                dp[i]++;
                now=0;
            }
        }
        nxt[i]=now;
    }
    int ans=0;
    int now=0;
    for (int i=0;i<n;i++)
    {
        ans+=dp[now];
        now=nxt[now];
    }
    printf("%d",ans/m);
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int main()
{
    //freopen("input.txt","r",stdin);
    string a,c;
    int b,d;
    cin >> b >> d;
    cin >> a >> c;
    long long ans=0;
    long long cnt=0;
    long long cnt1;
    int cnt2[101], pos[101],cnt3[101];
    long long l=a.length();
    long long r=c.length();
    bool fl=true;
    string w;
    w=a;
    for (int i=0;i<1000;i++)
    {
        w=w+a;
    }
    long long ll=w.length();
    long long j=0;
    long long x,y;
    bool flag=true;
    for (int i=0;i<101;i++)
        {
            pos[i]=0;
        }
    int i,cnt4,cnt5;
    for (i=0;(i<ll)&&(flag);i++)
    {
        if (w[i]==c[j])
        {
            j++;
            if (j==r)
            {
                j=0;
                cnt++;
                y=i;
            }
            if ((j==1))
            {
                if (pos[i%l]==1)
                {
                flag=false;
                cnt4=cnt2[i%l];
                cnt5=cnt3[i%l];
                cnt=cnt-cnt2[i%l];
                cnt1=i/l-cnt3[i%l];
                if (((y/l)==(i/l)) fl=false;
                }
                else
                {
                    pos[i%l]=1;
                    cnt2[i%l]=cnt;
                    cnt3[i%l]=i/l;
                }
            }
        }
    }
 //   cout << cnt5<<endl;
   // cout <<cnt4<<endl;
   // cout << cnt1<<endl;
    //cout << cnt<<endl;
    bool fl2=false;
    if (b>cnt5)
    {
        b-=cnt5;
        ans+=cnt4;
        ans+=(b/cnt1)*cnt;
        b=b%cnt1;
        if ((b==0))
        {
            if (fl) fl2=true;
        }
    }
    else
    {
        fl=false;
    }
//        cout << ans/d<<endl;
    w="";
    if (fl==false) w=a;
    else
    {
        for (i=(y%l)+1;i<l;i++) w.push_back(a[i]);
    }
    for (int i=0;i<b-1;i++)
    {
        w=w+a;
    }
    ll=w.length();
    j=0;
    for (i=0;i<ll;i++)
    {
        if (w[i]==c[j])
        {
            j++;
            if (j==r)
            {
                j=0;
                ans++;
            }
        }
    }
    if (fl2) ans--;
    ans/=d;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, mod = 998244353, G = 3, Gi = 332748118;
int n, m, lim, L, r[N]; ll ans, A[N], B[N], fac[N], inv[N], C[N];
inline ll qpow(ll a, ll b) { ll ans = 1; for (; b; b >>= 1, a = a * a % mod) if (b & 1) ans = ans * a % mod; return ans; }
inline void NTT(ll *A, int type) {
	for (int i = 0; i < lim; ++ i) if (i < r[i]) swap(A[i], A[r[i]]);
	for (int mid = 1; mid < lim; mid <<= 1) {
		ll Wn = qpow(type == 1 ? G : Gi, (mod - 1) / (mid << 1));
		for (int j = 0; j < lim; j += (mid << 1)) {
			ll w = 1;
			for (int k = 0; k < mid; ++ k, w = w * Wn % mod) {
				int x = A[j + k], y = w * A[j + k + mid] % mod;
				A[j + k] = (x + y) % mod, A[j + k + mid] = (x - y + mod) % mod;
			}
		}
	}
	if (type == 1) return ;
	ll invs = qpow(lim, mod - 2);
	for (int i = 0; i < lim; ++ i) A[i] = A[i] * invs % mod;
}
int main() {
	scanf("%d%d", &n, &m), fac[0] = C[0] = 1;
	for (int i = 1; i <= m; ++ i) C[i] = C[i - 1] * qpow(i, mod - 2) % mod * (n - i + 1) % mod;
	for (int i = 1; i <= m; ++ i) fac[i] = fac[i - 1] * i % mod;
	inv[m] = qpow(fac[m], mod - 2);
	for (int i = m - 1; ~i; -- i) inv[i] = inv[i + 1] * (i + 1) % mod;
	for (int i = 0; i <= m; ++ i) {
		A[i] = i & 1 ? mod - inv[i] : inv[i];
		B[i] = qpow(i, m) * inv[i] % mod;
	}
	lim = 1, L = 0; while (lim <= m + m) lim <<= 1, ++ L;
	for (int i = 0; i < lim; ++ i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (L - 1));
	NTT(A, 1), NTT(B, 1);
	for (int i = 0; i < lim; ++ i) A[i] = A[i] * B[i] % mod;
	NTT(A, -1);
	calc_strling();
	for (int i = 1; i <= min(n, m); ++ i)
		(ans += A[i] * fac[i] % mod * C[i] % mod * qpow(n + 1, n - i) % mod) %= mod;
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
using LL = long long;
#include "C:\Users\dna049\cf\include\poly.hpp"
using Mod = MInt<998244353>;

int main() {
	//freopen("in", "r", stdin);
	std::cin.tie(nullptr)->sync_with_stdio(false);
	int n, k;
	std::cin >> n >> k;
	auto A = PolyNFT({0, -1}).exp(k + 1);
	auto B = PolyNFT({0, 1}).exp(k + 1);
	for (int i = 0; i <= k; ++i) B[i] *= pow(Mod(i), k);
	auto S = (A * B).modXn(k + 1);
	Mod ans(0), x = Mod(n + 1).inv(), cur(1);
	for (int i = 1; i <= k && i <= n; ++i) {
		cur *= Mod(n + 1 - i) * x;
		ans += S.at(i) * cur;
	}
	ans *= pow(Mod(n + 1), n);
	std::cout << ans << '\n';
	return 0;
}


// Problem: F. AmShZ Farm
// Contest: Codeforces - Codeforces Round #722 (Div. 1)
// URL: https://codeforces.com/problemset/problem/1528/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;

template<typename F>
void multitest(F func) {
	int t;
	cin >> t;
	while(t--) func();
}
void report(int ok) {
	cout << (ok?"YES":"NO") << '\n';
}
using uint32 = uint32_t;
const int mod = 998244353;
namespace math {
const int L = 1<<20, A = 1<<20, B = 32, G = 3;

#define INLINE inline __attribute__ (( always_inline ))
struct mint {
	uint32_t v;
	template<class T = int>
	mint(T x = 0) {
		x %= mod;
		if(x < 0) x += mod;
		v = x;
	}
	mint operator-() const {
		return mint(v ? mod-v : 0);
	}
	mint &operator*=(const mint &r) {
		v = v*1ll*r.v%mod;
		return *this;
	}
	mint &operator+=(const mint &r) {
		v = v+r.v>=mod ? (v+r.v-mod) : (v+r.v);
		return *this;
	}
	mint &operator-=(const mint &r) {
		return *this += -r;
	}
	mint &operator/=(const mint &r) {
		return *this *= r.inv();
	}
	friend mint operator+(mint a, const mint &b) {
		return a += b;
	}
	friend mint operator-(mint a, const mint &b) {
		return a -= b;
	}
	friend mint operator*(mint a, const mint &b) {
		return a *= b;
	}
	friend mint operator/(mint a, const mint &b) {
		return a /= b;
	}

	template<class T = int>
	mint pow(T p) const {
		mint res = 1, cur = *this;
		while(p) {
			if(p&1) res = res*cur;
			cur = cur*cur, p>>=1;
		}
		return res;
	}
	mint inv() const {
		return mint(*this).pow(mod-2);
	}

	friend bool operator==(const mint &a, const mint &b) {
		return a.v == b.v;
	}
	friend bool operator!=(const mint &a, const mint &b) {
		return !(a == b);
	}

	friend istream& operator>>(istream &is, mint &m) {
		ll v;
		is >> v;
		m = mint(v);
		return is;
	}
	friend ostream& operator<<(ostream &os, const mint &m) {
		os << m.v;
		return os;
	}
};

mint fact[A], inum[A], ifact[A];
void calc_inum() {
	inum[1] = 1;
	for(int i = 2; i < A; i++) inum[i] = -inum[mod%i]*(mod/i);
}
void calc_combi() {
	if(0 == inum[1]) calc_inum();
	fact[0] = ifact[0] = 1;
	for(int i = 1; i < A; i++) fact[i] = fact[i-1]*i;
	for(int i = 1; i < A; i++) ifact[i] = ifact[i-1]*inum[i];
}

mint nck(int n, int k) {
	if(0 == fact[0]) calc_combi();
	if(k > n || k < 0) return 0;
	return fact[n]*ifact[k]*ifact[n-k];
}

uint W[L], WI[L];
INLINE void calc_roots() {
	W[0] = 1;
	int g = mint(G).pow((mod-1)/L).v;
	int i;
	for(i = 1; i < L/2; i++) W[i] = W[i-1]*1ull*g%mod;
	for(int x = 2; x < L; x*=2) {
		for(int y = 0; y < L/2; y+=x)
			W[i++] = W[y];
	}
	for(i = 0; i < L; i++) {
		WI[i] = (uint64_t(W[i])<<B)/mod;
	}
}
INLINE void _ntt(int N, vector<mint> &a) {//normal poly -> bit-rev dft | internally [0; 2*mod)
	int pos = 0;
	for(int l = L/2; l != N/2; l/=2)
		pos += l;
	for(int l = N/2; l; l/=2) {
		for(int x = 0; x < N; x+=2*l) {
			for(int j = pos, i = 0; i < l; i++, j++) {
				uint u = a[x+i].v+a[x+l+i].v;
				if(u >= 2*mod) u -= 2*mod;
				uint v = a[x+i].v-a[x+l+i].v+2*mod;
				uint Q = (v*1ull*WI[j])>>B;
				v = v*W[j] - Q*mod;
				a[x+i].v = u, a[x+l+i].v = v;
			}
		}
		pos += l;
	}
	for(int i = 0; i < N; i++) if(a[i].v >= mod) a[i].v -= mod;
}

INLINE void _ntt_inv(int N, vector<mint> &a) {//bit-rev dft -> normal poly | internally [0; 4*mod)
	int pos = L-2;
	for(int l = 1; l < N; l*=2) {
		for(int x = 0; x < N; x += 2*l) {
			for(int j = pos, i = 0; i < l; i++, j++) {
				uint u = a[x+i].v, v = a[x+l+i].v;
				uint Q = (WI[j]*1ull*v)>>B;
				if(u >= 2*mod) u -= 2*mod;
				v = v*W[j] - Q*mod;
				a[x+i].v = u+v, a[x+l+i].v = u-v+2*mod;
			}
		}
		pos -= 2*l;
	}
	reverse(1 + all(a));
	mint x = mint((mod+1)/2).pow(__lg(N));
	for(auto &i : a) i *= x;//takes care of 2*mod
}
template<bool inv>
INLINE void ntt(int n, vector<mint> &a) {//don't forget about reverse order
	if(W[0] == 0) calc_roots();
	if(!inv) _ntt(n, a);
	else _ntt_inv(n, a);
}

struct poly : vector<mint> {
	template<class... Args>
	explicit poly(Args... args) : vector<mint>(args...) {}
	poly(initializer_list<mint> il) : vector<mint>(il.begin(), il.end()) {}
	poly &trim(int k) {// mod x^k
		if(size() > k) {
			erase(begin()+k, end());
			shrink_to_fit();
		}
		return *this;
	}
	poly &trim() {//remove heading zeroes
		int k = 0;
		while(k < size() && (*this)[size()-k-1] == 0) k++;
		trim(size()-k);
		return *this;
	}
	poly low(int k) const {//get first k coefficients
		k = min(k, (int)size());
		poly res;
		for(int i = 0; i < k; i++) res.push_back((*this)[i]);
		return res;
	}
	friend poly operator*(poly a, const poly &b) {
		poly res = b;
		int n = a.size()+b.size()-1;
		while(n&(n-1)) n += n&-n;
		a.resize(n);
		res.resize(n);
		ntt<0>(n, a);
		ntt<0>(n, res);
		for(int i = 0; i < n; i++) res[i] *= a[i];
		ntt<1>(n, res);
		res.trim();//remove ?
		return res;
	}
	poly &operator*=(const poly &b) {
		return (*this) = (*this)*b;
	}

	template<class T>
	poly &operator*=(const T &x) {
		for(auto &i : *this) i *= x;
		return *this;
	}
	poly &operator+=(const poly &x) {
		if(size() < x.size()) resize(x.size());
		for(int i = 0; i < min(size(), x.size()); i++) (*this)[i] += x[i];
		return *this;
	}
	poly &operator-=(const poly &x) {
		if(size() < x.size()) resize(x.size());
		for(int i = 0; i < min(size(), x.size()); i++) (*this)[i] -= x[i];
		return *this;
	}

	template<class T> friend poly operator*(poly p, const T &x) { return p *= x; }
	template<class T> friend poly operator*(const T &x, poly p) { return p *= x; }
	friend poly operator+(poly p, const poly &x) { return p += x; }
	friend poly operator-(poly p, const poly &x) { return p -= x; }

	poly inv(int N) {//first n coefficients of P^-1
		assert((*this)[0].v);
		poly R {(*this)[0].inv()};
		R.reserve(2*N);
		for(int len = 2; len/2 < N; len*=2) {//R' = R(2 - RT)
			poly T = low(len);
			T.resize(2*len);ntt<0>(2*len, T);
			R.resize(2*len);ntt<0>(2*len, R);
			for(int i = 0; i < 2*len; i++) R[i] = R[i]*(2 - R[i]*T[i]);
			ntt<1>(2*len, R);
			R.trim(len);
		}
		return R.trim(N);
	}
	poly &derive() {
		for(int i = 0; i+1 < size(); i++) {
			(*this)[i] = (*this)[i+1]*(i+1);
		}
		pop_back();
		return *this;
	}

	poly derivative() { return poly(*this).derive(); }
	poly &integrate() {
		if(0 == inum[1]) calc_inum();
		push_back(0);
		for(int i = size(); i-- > 1;) {
			(*this)[i] = (*this)[i-1] * inum[i];
		}
		(*this)[0] = 0;
		return *this;
	}
	poly integral() { return poly(*this).integrate(); }

	poly ln(int N) {//first n coefficients of ln(P) = P'/P
		return (low(N+1).derivative() * inv(N)).trim(N-1).integrate().trim(N);
	}
	poly exp(int N) {//first n coefficients of exp(P), quite slow
		poly R{1};
		for(int len = 2; len/2 < N; len*=2) {
			R = (R*(poly{1}+low(len)-R.ln(len))).trim(len);
		}
		return R.trim(N);
	}
};
}
using namespace math;
poly ST(int k) {
	poly a(k+1), b(k+1);
	for(int i = 0; i <= k; i++)
		a[i] = ifact[i], b[i] = ifact[i]*mint(i).pow(k)*(i%2 ? -1 : 1);
	auto f = a*b;
	for(int i = 1; i <= k; i+=2) f[i] = -f[i];
	return f.low(k+1);
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	//multitest([&](){});
	calc_combi();
	int n, k;
	cin >> n >> k;n++;
	mint ans = 0;
	// for(int i = 0; i <= n-2; i++) {
		// ans += nck(n-2, i)*mint(n-1).pow(n-2-i)*mint(i).pow(k);
	// }
	// for(int i = 0; i <= n-2; i++) {
		// ans += nck(n-2, i)*mint(n-1).pow(n-2-i)*mint(i+1).pow(k);
		// cout << i << " " << nck(n-2, i) << " " << mint(n-1).pow(n-2-i)<< " " << mint(i+1).pow(k) << endl;
	// }
	// for(int i = 0; i <= 110; i++) {
		// for(int j = 0; j <= 110; j++) {
			// if(!i||!j) S[i][j] = i==j;
			// else S[i][j] = S[i-1][j-1] + S[i-1][j]*j;
		// }
	// }
	auto S = ST(k);
	S.resize(k+2);
	for(int i = 0; i <= min(n-2, k); i++) {
		ans += nck(n-2, i)*mint(n).pow(n-2-i)*S[i]*fact[i];
	}
	ans *= (n-1);
	for(int i = 0; i <= min(n-2, k); i++) {
		ans += nck(n-2, i)*mint(n).pow(n-2-i)*(S[i]*fact[i] + S[i+1]*fact[i+1]);
	}
	cout << ans << '\n';
}