import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	ans[] = long.max;
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto d = RD!int;
		auto a = RDA!int;
		int[int] cnt;
		foreach (j; 0..d)
		{
			++cnt[a[j]];
		}
		ans[i] = min(ans[i], cnt.keys.length);

		foreach (j; d..n)
		{
			++cnt[a[j]];
			if (cnt[a[j-d]] == 1)
			{
				cnt.remove(a[j-d]);
			}
			else
			{
				--cnt[a[j-d]];
			}
			ans[i] = min(ans[i], cnt.keys.length);
			debug writeln(ans[i]);
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush();
	debug readln();
}


void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int y, b, r;
  rd(y, b, r);

  int mx = 0;
  for (int i = 1; i <= y; i++) {
    for (int j = 1; j <= b; j++) {
      for (int k = 1; k <= r; k++) {
        if (i + 1 == j && j + 1 == k) {
          mx = max(mx, i + j + k);
        }
      }
    }
  }
  writeln(mx);
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}


import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.bigint;

void main()
{
    auto inp = readln.split.map!(to!int).array;
    int cnt;
    auto num = [1, 1, 2, 7, 4];
    while (1) {
        foreach (i; 0..5) {
            inp[i] -= num[i];
        }
        if (inp.count!(a => a <0)) break;
        else cnt++;
    }
    cnt.writeln;
}


import std.stdio, std.getopt, std.random, std.range, std.string, std.conv;
import std.algorithm, std.container, std.datetime;
import core.bitop;

class Primes {
    immutable bool[] isPrime;
    immutable int[] primes;
    this(int n) {
        auto used = new bool[n];
        int[] p;
        used[0] = used[1] = true;
        for (int i = 2; i < n; i++) {
            if (used[i]) continue;
            p ~= i;
            for (int j = i*2; j < n; j+= i) {
                used[j] = true;
            }
        }
        isPrime = used.idup.map!(i => !i).array;
        primes = p.idup;
    }

    int[] primeDecomp(long i) {
    	assert(n*n >= i);
    	int[] res;
    	foreach (d; primes) {
    		if (d*d > i) break;
    		while (!(i % d)) {
    			res ~= d;
    			i /= d;
    		}
    	}
    	if (i > 1) res ~= cast(int)i;
    	return res;
    }
};

int n;
long[] a;
int[] ps;

bool ok(int p, int d) {
	long aa = a[p];
	foreach (j; 0..n) {
		if (!(d & (1<<j))) continue;
		if (aa % a[j]) return false;
		aa /= a[j];
	}
	return true;
}

int count(int p, int d) {
	int res = ps[p];
	foreach (j; 0..n) {
		if (!(d & (1<<j))) continue;
		res -= ps[j];
	}
	return res;
}

int[][] dpcalc;
int calc(int p, int d) {
	if (p == n) {
		if (popcnt(d) == 1) return 0;
		return 1;
	}
	if (dpcalc[p][d] != -1) return dpcalc[p][d];
	int res = ((ps[p] == 1) ? 1 : ps[p]+1) + calc(p+1, d | (1<<p));
	foreach (i; 1..(1<<p)) {
		if ((i & d) != i) continue;
		if (!ok(p, i)) continue;
		res = min(res, count(p, i)+1+calc(p+1, (d & (~i)) | (1<<p)));
	}
	return res;
}

int solve() {
	auto p = new Primes(10^^6+100);
	readf("%d\n", &n);
	a = readln().split().map!(to!long).array.sort;
	ps.length = n;
	foreach (i; 0..n) {
		ps[i] = cast(int)p.primeDecomp(a[i]).length;
	}
	dpcalc = new int[][](n, 1<<n);
	foreach (dp; dpcalc) {
		dp[] = -1;
	}
	return calc(0, 0);
}

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


import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;

enum mod = 998244353;
alias M = Mod!(long, mod);

M[500_000 + 1] fact;

// binomial coefficient
M bc(long t, long a)
{
  auto res = fact[cast(uint)t] * (fact[cast(uint)a] * fact[cast(uint)(t - a)])^^(mod - 2);
  return res;
}
M noIncSeqs(long k, long n)
{
  return bc(n - 1, k - 1);
}

void main(string[] args)
{
  inputFile = stdin;
  fact[0] = M(1);
  foreach(i; 1 .. fact.length)
    fact[i] = M(i) * fact[i - 1];
  auto n = next!long;
  auto k = next!long;
  M noSeqs = 0;
  foreach(a1; 1 .. n + 1)
    {
      if (a1 * k > n) break;
      auto maxNum = n / a1;
      noSeqs = noSeqs + noIncSeqs(k, maxNum);
    }
  noSeqs.writeln;
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  string toString()
  {
    return to!string(rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
  auto opBinary(string op)(ulong exp)
       if (op == "^^")
	 {
	   return this.pow(This(1), exp);
	 }
  mixin interiorBinPow;
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
  while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
  _wordsFill;
  auto word = _words.front;
  _words.removeFront;
  return word;
}
string peekWord()
{
  _wordsFill;
  return _words.front;
}
T next(T)()
{
  return to!T(popWord);
}
T peek(T)()
{
  return to!T(peekWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}


//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

void main() {
  uint n = readln.strip.to!int;
  auto s = readln[0 .. n];
  auto l = s.count ('L');
  auto r = s.count ('R');
  writeln (l + r + 1);
}


import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.string;
import std.regex;

void main() {
  auto s = readln.chomp.split.sort.uniq.array;
  (4 - s.length).writeln;
}


import std.stdio;
import std.string;

int main(){
	string tab;
	tab = readln();
	int ile = 0;
	char pop = 'b';
	for (int i = 0; i < tab.length; i++){
		if (tab[i] != pop){
		    ile = 1;
		    pop = tab[i];
		}
		else if (tab[i] == pop){
		    ile++;
		}
		if (ile > 6){
			printf("YES");
			return 0;
		}
	}
	printf("NO");
	return 0;

}


import std.stdio, std.string, std.algorithm;

void main() {
    string song;
    readf("%s\n", &song);
    writeln(song.split("WUB").filter!(x => !x.empty()).join(" "));
}


import std.stdio;
import core.stdc.stdio;

int main(string[] argv)
{
	int m, n;
	scanf("%d %d", &m, &n);
	printf("%d", m*n / 2);
    return 0;
}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

void main() {
    auto s1 = readln.chomp;
    auto s2 = readln.chomp;

    int pos = 11;
    foreach (c; s1) {
        pos = (c == '+') ? pos + 1 : pos - 1;
    }

    auto dp = new real[][](25, 25);
    foreach (i; 0..21)
        fill(dp[i], 0);
    dp[0][11] = 1;

    foreach (i; 0..s2.length) {
        foreach (j; 1..22) {
            if (s2[i] == '+')
                dp[i+1][j+1] += dp[i][j];
            else if (s2[i] == '-')
                dp[i+1][j-1] += dp[i][j];
            else {
                dp[i+1][j+1] += dp[i][j] * 0.5;
                dp[i+1][j-1] += dp[i][j] * 0.5;
            }
        }
    }
    //dp[s2.length].writeln;
    writefln("%.10f", dp[s2.length][pos]);
}


import std.stdio, std.string;
import std.array, std.conv;
import std.algorithm;

string[] solve(int m, int s)
{
    if (m * 9 < s) return ["-1", "-1"];
    if (s == 0)
    {
        if (m == 1) return ["0", "0"];
        return ["-1", "-1"];
    }
    auto mi = "";
    auto sum = s;
    foreach (i; 0 .. m)
    {
        foreach (j; 0 .. 10)
        {
            if (i == 0 && j == 0) continue;
            if (j <= sum && (m - 1 - i) * 9 >= sum - j)
            {
                mi ~= j + '0';
                sum -= j;
                break;
            }
        }
    }
    auto ma = "";
    sum = s;
    foreach (i; 0 .. m)
    {
        foreach_reverse (j; 0 .. 10)
        {
            if (i == 0 && j == 0) continue;
            if (j <= sum && (m - 1 - i) * 9 >= sum - j)
            {
                ma ~= j + '0';
                sum -= j;
                break;
            }
        }
    }
    return [mi, ma];
}

int main(string[] args)
{
    auto r = map!(x => to!int(x))(readln.strip.split(" ")).array;
    auto m = r[0], s = r[1];
    auto ret = solve(m, s);
    writeln(join(ret, " "));
    return 0;
}


module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int n_Cases = 1;
        // n_Cases = cin.readInt;

	for (int case_i = 1; case_i <= n_Cases; case_i++) {
                int n = cin.readInt;
                int m = cin.readInt;

                int[] people = new int[n];

                foreach (ref i; people) i = cin.readInt;

                int bus = 1;
                int remaining = m;
                int currGroup = 0;

                for (; currGroup < n;) {
                        if (remaining >= people[currGroup]) {
                                remaining -= people[currGroup];
                                currGroup++;
                        }
                        if (currGroup == n) break;
                        if (remaining < people[currGroup]) {
                                bus++;
                                remaining = m;
                        }
                }
                writeln(bus);
        }
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int mod = 998_244_353;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto d = new int [n + 1];
		for (int i = 2; i <= n; i++)
		{
			for (int j = i; j <= n; j += i)
			{
				d[j] += 1;
			}
		}

		auto f = new int [n + 1];
		auto g = new int [n + 1];
		f[0] = 1;
		g[0] = f[0];
		for (int i = 1; i <= n; i++)
		{
			f[i] = (g[i - 1] + d[i]) % mod;
			g[i] = (g[i - 1] + f[i]) % mod;
		}
		writeln (f[n]);
	}
}


import core.bitop;
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		bool ok = true;
		while (a.length > 1)
		{
			foreach (i; 0..a.length - 1)
			{
				if (abs (a[i] - a[i + 1]) > 1)
				{
					ok = false;
				}
			}
			auto p = a.length - a.maxPos.length;
			a = a[0..p] ~ a[p + 1..$];
		}
		writeln (ok ? "YES" : "NO");
	}
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto k = RD!int;

	writeln(m >= n && k >= n ? "Yes" : "No");
	stdout.flush();
	debug readln();
}


import std.stdio;

void main()
{
    int w;
    readf(" %s", &w);

    if (w == 2) {
        writeln("NO");
    }
    else if (w % 2 == 0)
    {
        writeln("YES");
    }
    else
    {
        writeln("NO");
    }
}


import std.algorithm,std.regex,std.stdio,std.string;
void main(){
	readln;
	" vaporeon jolteon flareon espeon umbreon leafeon glaceon sylveon "
	.match(' '~readln.strip~' ').captures[0].write;
}


// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio, std.string, std.conv, std.algorithm;

T[] getVals(T)() { return to!(T[])(readln().chomp().split(" ")); }

void main() {
    auto nk = getVals!(int)();
    auto n = nk[0], k = nk[1];
    auto xs = getVals!(int)();
    auto candies = 0, present = 0;
    foreach (i, v; xs) {
        candies += v;
        present = min(candies, 8);
        candies -= present;
        k -= present;
        if (k <= 0) {
            writeln(i+1);
            return;
        }
    }
    writeln(-1);
}


import core.bitop, std.algorithm, std.bigint, std.bitmanip, std.compiler,
	std.complex, std.container, std.conv, std.datetime, std.functional,
	std.math, std.meta;
import std.numeric : Fft, fft, gcd, inverseFft;
import std.parallelism, std.random, std.range, std.regex, std.stdio, std.string,
	std.traits, std.typecons, std.uni, std.variant;

alias pair(X, Y) = Tuple!(X, "fi", Y, "se");
alias mp = make_pair;
alias pii = pair!(int, int);
alias pll = pair!(long, long);
// alias gcd = std.numeric.gcd;
alias lint = long;
alias rbt = RedBlackTree;
/// struct vector as c++ vector
struct Vec(T)
{
	private Array!T buf;
	alias buf this;
	this(this)
	{
		buf = buf.dup;
	}
	/// construct from length
	this(U)(U length)
			if (is(U == int) || is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint))
	{
		buf.length = length;
	}
	/// construct from length and default value
	this(U, X)(U length, X val)
			if (isImplicitlyConvertible!(X, T) || (is(U == int)
				|| is(U == size_t) || is(U == long) || is(U == ulong) || is(U == uint)))
	{
		buf.length = length;
		foreach (ref x; buf[])
		{
			x = val;
		}
	}
	/// construct from other array
	this(U)(U[] values...) if (isImplicitlyConvertible!(U, T))
	{
		buf = Array!(T)(values);
	}
	/// construct from other range
	this(Range)(Range r)
			if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range,
				T) && !is(Range == T[]))
	{
		buf = Array!(T)(r);
	}
	/// integer length
	int size() @property const
	{
		return cast(int) buf.length;
	}
}
///modulo
const int mod = 10 ^^ 9 + 7, mod2 = mod + 2;
public
{
	pure nothrow
	{
		///lcm
		T lcm(T)(auto ref T a, auto ref T b)
		{
			return a / gcd(a, b) * b;
		}
		///make_pair
		pair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)
		{
			return tuple!(X, "fi", Y, "se")(x_, y_);
		}
		///BigInt greatest common divizor
		BigInt biggcd(BigInt a, BigInt b)
		{
			BigInt res = 1;
			while (b)
			{
				if ((a & 1) && (b & 1))
				{
					a -= b;
					a >>= 1;
				}
				else if (a & 1)
				{
					b >>= 1;
				}
				else if (b & 1)
				{
					a >>= 1;
				}
				else
				{
					res <<= 1;
					a >>= 1;
					b >>= 1;
				}
				if (a < b)
					swap(a, b);
			}
			return res * a;
		}
		///square
		@property X sqr(X)(in X a_)
		{
			return a_ * a_;
		}
		///cube
		@property X cub(X)(in X a_)
		{
			return a_ * a_ * a_;
		}
		///posicion lower bound
		size_t lowb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).lowerBound(g).length;
		}
		///posicion upper bound
		size_t upb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return a.length - assumeSorted(a).upperBound(g).length;
		}
		///posicion binary search
		size_t binf(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			auto pos = lowb(a, g);
			return (g == a[pos]) ? pos : a.length;
		}
		///binary search
		bool binary_search(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).contains(g);
		}
	}
	///write an array
	void putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)
	{
		writefln("%(%s" ~ ch ~ "%)", a);
	}
	///write a matrix
	void putarr(X)(in X a)
			if (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))
	{
		writefln("%(%(%s %)\n%)", a);
	}
	///get an array
	bool getarr(X)(X a, in size_t n)
	{
		bool b = 1;
		foreach (ref i; a[0 .. n])
			b = input(&i);
		return b;
	}

	static if (version_minor >= 74)
	{
		///read without format
		bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			return readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
		}
		///readln without format
		bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			const bool fl = readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
			readln;
			return fl;
		}
	}
	else
	{
		///read without format
		bool input(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			return s == ptrs.length;
		}
		///readln without format
		bool read(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			readln;
			return s == ptrs.length;
		}
	}
	///opening file
	nothrow auto inf(in char* name)
	{
		return freopen(name, "r", core.stdc.stdio.stdin);
	}
	///closing file
	nothrow auto ouf(in char* name)
	{
		return freopen(name, "w", core.stdc.stdio.stdout);
	}
	///parsing array from line
	@property auto arread(T)()
	{
		return readln.splitter.map!(to!(T)).array;
	}
	///outbuffer
	void enter(T...)(T args)
	{
		writeln(args);
		stdout.flush;
	}
}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	int n, pos, l, r;
	read(n, pos, l, r);
	if (l == 1 && r == n)
		writeln(0);
	else if (l == 1)
	{
		writeln(abs(r - pos) + 1);
	}
	else if (r == n)
		writeln(abs(l - pos) + 1);
	else
		writeln(min(abs(r - pos), abs(l - pos)) + r - l + 2);
}


import std.stdio, std.range, std.algorithm, std.string, std.conv, std.typecons;

void main(){
	auto x=readln().strip.split.map!(to!int).array;
	int n=x[0],m=x[1],c0=x[2],d0=x[3];
	auto buns=iota(m).map!(_=>readln().strip.split.map!(to!int).array);
	Tuple!(int,int)[] ss;
	foreach(bun;chain(only([0,0,c0,d0]),buns)){
		int num=bun[1]?bun[0]/bun[1]:2000;
		int cost=bun[2];
		int benefit=bun[3];
		foreach(k;0..num) ss~=tuple(cost,benefit);
	}
	auto t=0.repeat(n+1).array;
	foreach(s;ss){
		foreach_reverse(i;0..t.length){
			if(i<s[0]) continue;
			t[i]=max(t[i],t[i-s[0]]+s[1]);
		}
	}
	writeln(t[n]);
}


import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto p = n.iota.array;
		makeIndex (a, p);
		auto q = new int [n];
		foreach (i; 0..n)
		{
			q[p[i]] = i;
		}
		writefln ("%(%s %)", n.iota.map !(i => a[p[(q[i] + 1) % n]]));
	}
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.datetime.stopwatch;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int r, h;
    readf("%s %s", &r, &h);
    readln;

    int getAns(int r, int h) {
        if (h < r) {
            return 1;
        }

        int d = 2*r;

        int fit = 1 + (h-r) / d;
        int ans = 2 * fit;

        int side1 = r;
        int side2 = d - r  +  h - ((fit-1) * d + r);
        bool center = side1.to!long * side1 + side2.to!long * side2 >= d.to!long * d;

        return fit * 2 + (center ? 1 : 0);
    }

    //scale up by 2, pass circle radius and height
    getAns(r, 2*h).writeln;
}


import
		std.math,
		std.conv,
		std.file,
		std.range,
		std.array,
		std.stdio,
		std.string,
		std.bitmanip,
		std.algorithm;

void main()
{
	readln;
	auto s = readln.strip;

	uint n;

	while(s.length)
	{
		n++;

		if(!(s.length & 1) && s[0..$ / 2] == s[$ / 2..$])
		{
			n += s.length / 2;
			break;
		}

		s.popBack;
	}

	n.writeln;
}


import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
  int x, y, z;
  readf(" %s %s %s", x, y, z);
  char ans = ' ';
  int maxx = x-y + z; // up 1: down: -1
  int minn = x-y - z;
  if (minn > 0)  ans = '+';
  else if ( maxx < 0)  ans = '-';
  else if (maxx == 0 && minn == 0) ans = '0';
  else ans = '?';

  writeln(ans);

}


import std.algorithm.comparison;
import std.array;
import std.stdio;

void main() {
    int n;
    readf("%d\n", &n);
    int[10][10] a;
    mixin(`
        foreach_ (ref row; a[0 .. n])
            foreach_ (ref x; row[0 .. n])
                readf("%d ", &x);
        foreach_ (k; 0 .. n)
            foreach_ (i; 0 .. n)
                foreach_ (j; 0 .. n)
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
        int result = 0;
        foreach_ (ref row; a[0 .. n])
            foreach_ (x; row[0 .. n])
                result = max(result, x);
    `.replace("foreach_", "fo" ~ "reach"));//Да что вы знаете о статически компилируемых языках!)
    writeln(result);
}


import std.stdio;
import std.conv;
import std.algorithm;
import std.math;

void main()
{
	int m = to!int(readln()[0..$ - 1]);
	int[] c = new int[m];
	for (int i = 0; i < m; ++i) {
		readf(" %d", &c[i]);
	}
	int x, y;
	readf(" %d %d", &x, &y);

	int k = 0;
	int t = 0;
	foreach (tl; c) {
		t += tl;
	}
	for (int i = 0; i < m; ++i) {
		k += c[i];
		t -= c[i];
		if (k > y || t < x) {
			writeln(0);
			return;
		}
		if (k >= x && t <= y) {
			writeln(i + 2);
			return;
		}
	}
	assert(false);

}


import std.stdio, std.string, std.algorithm;
import std.conv, std.array;
import std.container;
import std.typecons;

alias sum = reduce!("a + b");

int n;
void main() {
    readf("%d\n", &n);
    int[] us = new int[n];
    int[] ls = new int[n];
    for (int i = 0; i < n; i++) {
        readf("%d %d\n", &us[i], &ls[i]);
    }
    if ((us.sum + ls.sum) % 2 == 1) {
        writeln(-1);
    } else if (us.sum % 2 == 1 && ls.sum % 2 == 1) {
        for (int i = 0; i < n; i++) {
            if (us[i] % 2 != ls[i] % 2) {
                writeln(1);
                return;
            }
        }
        writeln(-1);
    } else {
        writeln(0);
    }
}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long INF = 1L << 59;

void main() {
    auto s = readln.split.map!(to!long);
    auto N = s[0];
    auto K = s[1];
    long ans = N + 1 + N;
    if (K == 1 || K == N) {
        ans += N - 1;
    } else {
        long left = K - 1;
        long right = N - K;
        long x = min(left, right) * 2 + max(left, right);
        ans += x;
    }

    ans.writeln;
}


import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

void main()
{
    readln;
    auto s = readln.strip();

    int [string] mp;
    foreach (a, b; lockstep (s, s.dropOne())) {
        auto cur = [a,b].to!string;
        ++mp[cur];
    }

    auto mx = mp.byKeyValue.maxElement!(kv => kv.value).key;
    writeln (mx);
}


import std.stdio;
import std.math;

void main() {
  int a, b;
  readf(" %s %s", &a, &b);

  auto a_win = 0, b_win = 0, draw = 0;
  const dice = [1, 2, 3, 4, 5, 6];
  foreach (int pip; dice) {
    const a_score = abs(a-pip), b_score = abs(b-pip);
    if (a_score < b_score) {
      a_win++;
    } else if (b_score < a_score) {
      b_win++;
    } else {
      draw++;
    }
  }

  writefln("%s %s %s", a_win, draw, b_win);
}


import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

int[] to_s(int x) {
    int[] s;
    while (x > 0) {
        int y = x % 3;
        s ~= y;
        x /= 3;
    }
    return s;
}

void main() {
    int A, B; scanf("%d %d\n", &A, &B);
    auto a = A.to_s,
         b = B.to_s;
    while (a.length != b.length) {
        (a.length < b.length ? a : b) ~= 0;
    }
    auto c = new int[a.length];
    foreach (i; 0 .. a.length) {
        c[i] = (b[i] - a[i] + 3) % 3;
    }

    long ans = 0;
    foreach (i; 0 .. c.length) {
        ans += (3 ^^ i) * c[i];
    }
    writeln(ans);
}


import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    int b,g,n;
    readf!" %s %s %s"(b,g,n);

    int ans = 0;
    foreach (i; 0 .. b+1) {
        if (i <= n && n - i <= g) ans++;
    }

    writeln(ans);
}


import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    arr.sort().reverse();

    debug { arr.writeln; }

    bool isOk(int md) {
        int eng = 0, sb = 0;
        foreach (i, e; arr) {
            if (i > 0 && i % md == 0) { sb += 1; }

            eng += max(e - sb, 0);
        }

        return eng >= m;
    }

    int le = 1, r = n+1;
    while (le < r) {
        int md = (le + r) / 2;
        if (isOk(md)) { r = md; }
        else { le = md + 1; }
    }

    writeln(le == n+1 ? -1 : le);
}


import std.stdio, std.string;

//int[] div[2010];
int len[2010];
int div[2010][2010];
int dp[2010][2010];

static immutable int mod = 1000000007;

void init()
{
    foreach (int i; 1 .. 2001)
    {
        foreach (int j; 1 .. i + 1)
        {
            if (i % j == 0)
            {
                div[i][len[i] ++] = j;
                //div[i] ~= j;
            }
        }
    }
}

int recur(int num, int pos)
{
    if (dp[num][pos] != -1)
    {
        return dp[num][pos];
    }
    if (pos == 1)
    {
        return dp[num][pos] = 1;
    }
    dp[num][pos] = 0;
    foreach (int i; 0 .. len[num])//div[num].length)
    {
        int ret = recur(div[num][i], pos - 1);
        dp[num][pos] = (dp[num][pos] + ret) % mod;
    }
    return dp[num][pos];
}

void solve(int n, int k)
{
    foreach (int i; 1 .. n + 1)
    {
        foreach (int j; 0 .. k + 1)
        {
            dp[i][j] = -1;
        }
    }
    int ans = 0;
    foreach (int i; 1 .. n + 1)
    {
        int ret = recur(i, k);
        ans = (ans + ret) % mod;
    }
    printf("%d\n", ans);
}

void main(string[] args)
{
    init();
    int N, K;
    while (scanf("%d%d", &N, &K) == 2)
    {
        solve(N, K);
    }
}


import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.bigint, std.math, std.range;
import core.thread;

//  Input
string[] tokens;
int tokenId = 0;
string readToken() { for (; tokenId == tokens.length; ) tokens = readln.split, tokenId = 0; return tokens[tokenId++]; }

int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//  chmin/chmax
void chmin(T)(ref T t, T f) { if (t > f) t = f; }
void chmax(T)(ref T t, T f) { if (t < f) t = f; }

int A, B;

int gcd(int x, int y) {
    if (y == 0) return x;
    return gcd(y, x % y);
}

int func(ref int x) {
    int ans = 0;

    for ( ; x % 2 == 0; ) {
        x /= 2;
        ans += 1;
    }
    for ( ; x % 3 == 0; ) {
        x /= 3;
        ans += 1;
    }
    for ( ; x % 5 == 0; ) {
        x /= 5;
        ans += 1;
    }

    if (x != 1) ans = -1;

    return ans;
}

void main () {
    A = readInt();
    B = readInt();

    int d = gcd(A, B);

    A /= d;
    B /= d;

    int cx = func(A);
    int cy = func(B);

    int ans = cx + cy;

    if (cx == -1 || cy == -1) ans = -1;

    writeln(ans);
}


import std.stdio;
import std.algorithm;
import std.typecons;
import std.string;
import std.array;
import std.conv;
import std.math;
import std.range;

void main() {
  int n;
  readf(" %s", &n);
  readln;
  auto a = readln.strip.split.map!(to!int).array.sort;
  foreach (i; 0..n) {
    foreach (j; i + 1..n) {
      foreach (k; j + 1..n) {
        if (a[i] == a[j] || a[j] == a[k]) continue;
        if (a[k] - a[i] <= 2) {
          writeln("YES");
          return;
        }
      }
    }
  }
  writeln("NO");
}


import std.stdio;
import core.stdc.stdio;

int main(string[] argv)
{
	int n, k;
	scanf("%d %d", &n, &k);
	int max_time = 240;
	int max_problem_time = max_time - k;
	long sum = 0;
	foreach (int i; 1..(n+1))
	{
		sum += 5 * i;
		if (sum > max_problem_time)
		{
			printf("%d", i-1);
			return 0;
		}
	}
	printf("%d", n);
	return 0;
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    long[] pws;
    pws ~= 1;
    foreach (i; 1 .. 36) {
        pws ~= 2 * pws.back;
        if (i == 13) { pws[i] -= 100; }
    }

    pws[n].writeln;
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

enum INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const S = readToken();

      int[] ks, vs, os;
      foreach (i; 0 .. N) {
        switch (S[i]) {
          case 'K': ks ~= i; break;
          case 'V': vs ~= i; break;
          default: os ~= i;
        }
      }
      const ksLen = cast(int)(ks.length);
      const vsLen = cast(int)(vs.length);
      const osLen = cast(int)(os.length);
      debug {
        writeln("ks = ", ks);
        writeln("vs = ", vs);
        writeln("os = ", os);
      }

      auto costO = new int[][][](osLen + 1, ksLen + 1, vsLen + 1);
      auto costK = new int[][][](osLen + 1, ksLen + 1, vsLen + 1);
      auto costV = new int[][][](osLen + 1, ksLen + 1, vsLen + 1);
      foreach (i; 0 .. osLen + 1) foreach (x; 0 .. ksLen + 1) foreach (y; 0 .. vsLen + 1) {
        if (i < osLen) {
          foreach (xx; 0 .. x) if (ks[xx] > os[i]) ++costO[i][x][y];
          foreach (yy; 0 .. y) if (vs[yy] > os[i]) ++costO[i][x][y];
        }
        if (x < ksLen) {
          foreach (ii; 0 .. i) if (os[ii] > ks[x]) ++costK[i][x][y];
          foreach (yy; 0 .. y) if (vs[yy] > ks[x]) ++costK[i][x][y];
        }
        if (y < vsLen) {
          foreach (ii; 0 .. i) if (os[ii] > vs[y]) ++costV[i][x][y];
          foreach (xx; 0 .. x) if (ks[xx] > vs[y]) ++costV[i][x][y];
        }
      }

      auto dp = new int[][][](osLen + 2, ksLen + 1, vsLen + 1);
      foreach (i; 0 .. osLen + 2) {
        foreach (x; 0 .. ksLen + 1) foreach (y; 0 .. vsLen + 1) {
          dp[i][x][y] = INF;
        }
      }
      dp[0][0][0] = 0;
      foreach (i; 0 .. osLen + 1) {
        foreach (x; 0 .. ksLen + 1) foreach (y; 0 .. vsLen + 1) {
          if (dp[i][x][y] < INF) {
            int cK = dp[i][x][y];
            foreach (xx; x .. ksLen + 1) {
              int cKV = cK;
              foreach (yy; y .. vsLen + 1) {
                int cc = cKV;
                if (i < osLen) {
                  cc += costO[i][xx][yy];
                }
                chmin(dp[i + 1][xx][yy], cc);
                if (yy < vsLen) {
                  cKV += costV[i][xx][yy];
                }
              }
              if (xx < ksLen) {
                cK += costK[i][xx][y];
              }
            }
          }
        }
      }
      debug {
        foreach (i; 0 .. osLen + 2) {
          writefln("dp[%s] = %s", i, dp[i]);
        }
      }

      const ans = dp[osLen + 1][ksLen][vsLen];
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.math;

int main(string[] argv)
{
	int[] n; n.length = 3;
	scanf("%d %d %d", &n[0], &n[1], &n[2]);
	n.sort!("a > b");
	int sum = n[0] - n[2];
	write(sum);
	return 0;
}


import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

T modpow(T)(T a, T n, T m)
{
  a %= m;
  if (n == 0)
    return 1;
  if (n & 1)
    {
      auto res = a * modpow(a, n - 1, m);
      res %= m;
      return res;
    }
  auto res = modpow(a, n / 2, m);
  res *= res;
  return res % m;
}

void main()
{
  alias ln = long;
  const ln p = 1_000_000_000 + 7;
  ln n, m; get(n, m);
  wr(modpow(pmod(modpow(2, m, p) - 1, p), n, p));
}


import std.conv;
import std.stdio;

immutable int MOD   =  1_000_000_007;
immutable int HALF  = (MOD >> 1) + 1;
immutable int MAX_N =             54;

struct modint
{
	long value;

	alias value this;

	this (long nvalue)
	{
		value = nvalue;
	}

	modint opBinary (string op) (long other)
	{
		return mixin ("modint ((value " ~ op ~ " other) % MOD)");
	}

	modint opOpAssign (string op, T) (T other)
	{
		return mixin ("(this = this " ~ op ~ " other)");
	}
}

void main ()
{
	auto c = new modint [] [] (MAX_N, MAX_N);

	foreach (n; 0..MAX_N)
	{
		c[n][0] = 1;
		foreach (k; 1..n + 1)
		{
			c[n][k] = c[n - 1][k - 1] + c[n - 1][k];
		}
	}

	auto f = new modint [] [] [] (MAX_N, MAX_N, 2);
	// 0 is occupied, 1 is free
	f[1][0][1] = 1;

	foreach (n; 2..MAX_N)
	{
		foreach (k; 0..n + 1)
		{
			foreach (b; 0..2)
			{
				// one child
				int pk = k - (b == 0);
				if (pk >= 0)
				{
					modint mult = n - 1;
					f[n][k][b] = mult * f[n - 1][pk][!b];
				}
			}

			// two children
			foreach (x; 1..n - 1)
			{
				int y = n - 1 - x;
				assert (x > 0 && y > 0);
				modint mult = c[n - 1][x];
				mult *= x;
				mult *= y;

				foreach (xk; 0..k + 1)
				{
					int yk = void;

					// occupied
					yk = k - xk - 1;
					if (yk >= 0)
					{
						f[n][k][0] += f[x][xk][1] *
						    f[y][yk][0] * mult;
						if (x < y ||
						    (x <= y && xk <= yk))
						{
							modint mult2 = mult;
							if (x == y && xk == yk)
							{
								mult2 *= HALF;
							}
							f[n][k][0] += mult2 *
							    f[x][xk][1] *
							    f[y][yk][1];
						}
					}

					// free
					yk = k - xk;
					if (x < y || (x <= y && xk <= yk))
					{
						modint mult2 = mult;
						if (x == y && xk == yk)
						{
							mult2 *= HALF;
						}
						f[n][k][1] += f[x][xk][0] *
						    f[y][yk][0] * mult2;
					}
				}
			}
		}
	}

	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		writeln (f[n][k][0] + f[n][k][1]);
	}
}


import std.stdio;
import std.string;
import std.algorithm;
import core.stdc.stdio;

string readline()
{
	string result = readln();
	if (result[result.length - 1] == '\n') result.length--;
	return result;
}

int main(string[] argv)
{
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	int[8] r;
	r[0] = a + b + c;
	r[1] = a * b * c;
	r[2] = a + b * c;
	r[3] = a * b + c;
	r[4] = (a * b) + c;
	r[5] = (a + b) * c;
	r[6] = a + (b * c);
	r[7] = a * (b + c);

	int max = int.min;
	foreach (int i; 0..8)
	{
		if (r[i] > max) max = r[i];
	}
	printf("%d", max);
	return 0;
}


import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

void main ()
{
	int h1, a1, d1;
	while (readf (" %s %s %s", &h1, &a1, &d1) > 0)
	{
		int h2, a2, d2;
		readf (" %s %s %s", &h2, &a2, &d2);
		int ch, ca, cd;
		readf (" %s %s %s", &ch, &ca, &cd);

		int res = int.max;
		scope (exit)
		{
			writeln (res);
		}

		if (a2 <= d1)
		{
			res = min (res, ca * max (0, d2 - a1 + 1));
			continue;
		}

		auto cost = new int [20001];
		cost[] = short.max;
		foreach (int da; 0..201)
		{
			int na = a1 + da - d2;
			if (na <= 0)
			{
				continue;
			}
			int at = (h2 + na - 1) / na;
			cost[at] = min (cost[at], da * ca);
		}
		foreach (int i; 1..20001)
		{
			cost[i] = min (cost[i], cost[i - 1]);
		}

		foreach (int dh; 0..20001)
		{
			foreach (int dd; 0..201)
			{
				int nd = a2 - dd - d1;
				if (nd <= 0)
				{
					res = min (res, cost[20001 - 1] +
					    dh * ch + dd * cd);
					continue;
				}
				int dt = (h1 + dh + nd - 1) / nd;
				dt = min (dt, 20001);
				dt--;
				if (dt <= 0)
				{
					continue;
				}
				res = min (res, cost[dt] + dh * ch + dd * cd);
			}
		}
	}
}


import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    int n,m;
    scan(n);
    scan(m);

    int ans;

    if (n > 30) {
        ans = m;
    }
    else {
        ans = m % 2^^n;
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


import std.stdio;

void main() {
  int n;
  readf(" %s", &n);
  if (n & 1) {
    writeln("black");
  } else {
    writeln("white");
    writeln("1 2");
  }
}


void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  long n, m, a, b; rd(n, m, a, b);

  long x=(((n+m-1)/m)*m-n)*a;
  long y=(n-(n/m)*m)*b;

  writeln(min(x, y));
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}


import std.stdio, std.range, std.string, std.conv, std.algorithm;

void main() {
    int d1, d2, d3;
    readf("%d %d %d\n", &d1, &d2, &d3);
    writeln(min((d1 + d2) * 2, (d2 + d3) * 2, (d3 + d1) * 2, d1 + d2 + d3));
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto u = RD;
	auto v = RD;

	if (u > v || (v - u) % 2)
		writeln(-1);
	else
	{
		auto x = v - u;
		long[] ans;
		long[] tmp;
		long t;
		foreach (i; 0..60)
		{
			auto bit = 1L << i;
			if (x & bit)
			{
				tmp ~= [bit >> 1, bit >> 1];
			}
			if (u & bit)
			{
				t |= bit;
			}
		}
		if (t != 0)
			ans ~= t;
		tmp.sort!"a > b"();
		debug writeln(ans);
		debug writeln(tmp);
		foreach (e; tmp)
		{
			bool ok;
			foreach (i; 0..ans.length)
			{
				if ((ans[i] & e) == 0)
				{
					ans[i] += e;
					ok = true;
					break;
				}
			}
			if (!ok)
			{
				ans ~= e;
			}
		}
		/*int[long] cnt;
		foreach (e; ans)
		{
			++cnt[e];
		}
		ans = [];
		while (!cnt.empty)
		{
			foreach (key; cnt.keys.sort)
			{
				bool flag;
				if (cnt[key] >= 4)
				{
					auto c = cnt[key] / 4;
					cnt[key] -= c * 4;
					auto p = 2;
					while (c >= 4)
					{
						c /= 4;
						p *= 2;
					}
					cnt[key*p] += 2;
					flag = true;
				}
				foreach (i; 0..cnt[key])
				{
					ans ~= key;
				}
				cnt.remove(key);
				if (flag) break;
			}
		}*/
		writeln(ans.length);
		if (!ans.empty)
			ans.map!(to!string).join(" ").writeln;
	}

	stdout.flush;
	debug readln;
}


import std.stdio;
import std.string;
import std.conv;
import std.regex;
import std.algorithm.iteration;
import std.array;
import std.functional;

void main()
{
  auto n = stdin.readln.strip.to!int;
  int h = 1;
  int t = 1;
  alias fn = unaryFun!"a*(a+1)/2";
  for (;t<=n; t+=fn(++h)) {}
  write(--h);
}


import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

void main() {
    long n, m;
    scan(n, m);

    long ansmin = max(0, n - 2*m);

    long v;

    while (v*(v-1)/2 < m) {
        v++;
    }

    long ansmax = n - v;

    writeln(ansmin, " ", ansmax);
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


import std.stdio;
import core.stdc.stdio;
import std.math;

string readline()
{
	string res = readln();
	if (res[$-1] == '\n') res.length--;
	return res;
}

int main(string[] argv)
{
	int offs = (readline() == "R" ? 1 : -1);
	string s = readline();
	string[] keyboard; keyboard.length = 3;
	keyboard[0] = "qwertyuiop";
	keyboard[1] = "asdfghjkl;";
	keyboard[2] = "zxcvbnm,./";
	string res = "";
	foreach (char c; s)
	{
		foreach(int i; 0..3)
		{
			foreach (int j; 0..keyboard[i].length)
			{
				if (c == keyboard[i][j])
				{
					res ~= keyboard[i][j-offs];
					break;
				}
			}
		}
	}
	write(res);
	return 0;
}


import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;
immutable int MD = 998_244_353;

void main()
{
    auto vs = readln.chomp.split.map!(to!int).array;

    int mx = vs.maxElement;

    auto pw = new int[] (mx+1);
    pw[0] = 1;
    foreach (i; 1 .. mx+1) { pw[i] = pw[i-1].to!long * i % MD; }

    auto nt = new int[][] (mx+1, mx+1);
    foreach (n; 0 .. mx+1) {
        nt[n][0] = 1;
        foreach (k; 1 .. n+1) {
            nt[n][k] = (nt[n-1][k] + nt[n-1][k-1]) % MD;
        }
    }

    debug { pw.writeln; nt.writeln; }

    auto prs = new int[][] (3, 2);
    foreach (i; 0 .. vs.length) {
        foreach (j; i+1 .. vs.length) {
            prs ~= [vs[i], vs[j]];
        }
    }

    int ans = 1;
    foreach (pr; prs) {
        int mn = pr.minElement;

        int tot = 0;
        foreach (edges; 0 .. mn+1) {
            int cur = nt[pr[0]][edges].to!long * nt[pr[1]][edges] % MD * pw[edges] % MD;
            tot = (tot + cur) % MD;
        }

        ans = ans.to!long * tot % MD;
    }

    ans.writeln;
}


import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;

immutable int mod = 10^^9 + 7;

void main(){
    int n, m;
    readVars(n, m);

    int ans;

    for(int a = 0; a*a <= n; ++a){
        int b = n - a*a;
        ans += (b*b == m - a);
    }

    writeln(ans);
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}


import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;

void main()
{
	string s = readln.chomp;
	int p;
	int l;
	p = l = 0;
	foreach (c; s) {
		final switch (c) {
			case 'o':
				++p;
				break;
			case '-':
				++l;
				break;
		}
	}
	if (p == 0 || l % p == 0) {
		"YES".writeln;
	} else {
		"NO".writeln;
	}
}

void tie(R, Args...)(R arr, ref Args args)
		if (isRandomAccessRange!R || isArray!R)
in
{
	assert (arr.length == args.length);
}
body
{
	foreach (i, ref v; args) {
		alias T = typeof(v);
		v = arr[i].to!T;
	}
}

void verbose(Args...)(in Args args)
{
	stderr.write("[");
	foreach (i, ref v; args) {
		if (i) stderr.write(", ");
		stderr.write(v);
	}
	stderr.writeln("]");
}


import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    int n;
    string cmd;

    scan(n);
    scan(cmd);

    int x, y;

    foreach (i ; 0 .. n) {
        if (cmd[i] == 'U') y++;
        if (cmd[i] == 'D') y--;
        if (cmd[i] == 'L') x--;
        if (cmd[i] == 'R') x++;
    }

    writeln(n - abs(x)- abs(y));
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


import core.bitop;
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int mod = 10 ^^ 9 + 7;
immutable int sLog = 20;
immutable int sMax = 1 << sLog;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.strip.map !(c => c == '1').array;
		auto f = new int [] [] (n + 1, sMax);
		foreach (ref g; f)
		{
			g[] = 0;
			g[0] = 1;
		}
		int res = 0;
		foreach (i; 0..n + 1)
		{
			for (int s = 1; s < sMax; s = (s << 1) | 1)
			{
				res += f[i][s];
				if (res >= mod)
				{
					res -= mod;
				}
			}
			int v = 0;
			foreach (j; i..n)
			{
				v = (v << 1) | a[j];
				if (v == 0)
				{
					continue;
				}
				if (v > sLog)
				{
					break;
				}
				foreach (s; 0..sMax)
				{
					int t = s | (1 << (v - 1));
					f[j + 1][t] += f[i][s];
					if (f[j + 1][t] >= mod)
					{
						f[j + 1][t] -= mod;
					}
				}
			}
		}
		writeln (res);
	}
}


import std.algorithm.iteration;
import std.ascii;
import std.array;
import std.conv;
import std.stdio;
import std.string;

void main()
{
  auto input = readln.stripRight.split!(isWhite).map!(a => a.parse!int);
  auto sheetsPerPerson = (input[1] + input[2] - 1) / input[2];
  auto sheets = (input[0] * sheetsPerPerson + input[3] - 1) / input[3];

  writeln(sheets);
}


import core.bitop;
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int limit = 32;

void main ()
{
	int n, total;
	while (readf (" %s %s", &n, &total) > 0)
	{
		readln;
		auto c = readln.splitter.map !(to !(int)).array;
		auto p = n.iota.array;
		p.schwartzSort !(i => c[i] * 1.0L / (1 << i),
		    q{a < b}, SwapStrategy.stable);
		long res = long.max;
		long cur = 0;
		foreach (i; p)
		{
			int times = total >> i;
			cur += times * 1L * c[i];
			total -= times << i;
			if (!total)
			{
				res = min (res, cur);
				break;
			}
			res = min (res, cur + c[i]);
		}
		writeln (res);
	}
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const A = readInt();

      int ans;
      foreach (i; 0 .. 6) {
        ans |= ((A >> i) & 1) << [4, 1, 3, 2, 0, 5][i];
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	writeln("NO");

	stdout.flush;
	debug readln;
}


import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,
       std.container, std.typecons, std.conv, std.random, std.bigint, std.string;

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

bool even(int x) { return x % 2 == 0; }

bool solve(int n, int m, int k) {
  if (n.even && m.even) return k.even;

  if (n.even) {
    return solve(m, n, n * m / 2 - k);
  } else {
    return k >= m / 2 && solve(n - 1, m, k - m / 2);
  }
}

void main() {
  int t;
  read(t);

  while (t--) {
    int n, m, k;
    read(n, m, k);

    writeln(solve(n, m, k) ? "YES" : "NO");
  }
}


import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int LIMIT = 1_000_000;

void main ()
{
	long c, h1, h2, w1, w2;
	while (readf (" %s %s %s %s %s", &c, &h1, &h2, &w1, &w2) > 0)
	{
		long res = 0;

		for (long q1 = 0; q1 <= LIMIT && q1 * w1 <= c; q1++)
		{
			long q2 = (c - q1 * w1) / w2;
			res = max (res, q1 * h1 + q2 * h2);
		}

		swap (h1, h2);
		swap (w1, w2);

		for (long q1 = 0; q1 <= LIMIT && q1 * w1 <= c; q1++)
		{
			long q2 = (c - q1 * w1) / w2;
			res = max (res, q1 * h1 + q2 * h2);
		}

		writeln (res);
	}
}


import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main()
{
    auto s = readln.chomp.to!(dchar[]);

    debug { s.writeln; }

    bool ok = false;
    foreach (a,b,c; lockstep(s, s.dropOne, s.drop(2))) {
        debug { writeln(a, ' ', b, ' ', c); }
        auto arr = [a,b,c];

        ok |= arr.sort().array == cast(dchar[]) ['A', 'B', 'C'];
    }

    writeln(ok ? "Yes" : "No");
}


import std.stdio,
	std.array,
	std.conv,
	std.algorithm;

void main() {
	int[] nums = to!(int[])(readln.split);
	int n = nums[0];
	nums = nums[1..$].sort.uniq.array;
	int[] data = [0] ~ [-1<<31].replicate(n);

	foreach(i; 1..n+1){
		foreach(m; nums){
			if(i-m >= 0){
				data[i] = max(data[i], data[i-m]+1);
			}
		}
	}
	writeln(data[n]);
}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

immutable int MOD = 10^^9+7;
int N, K, D;
auto mem = new int[][][](120, 120, 2);

int dp(int n, int h, bool d) {
    if (n == 0) return d;
    if (n < 0) return 0;
    if (mem[n][h][d] >= 0) return mem[n][h][d];

    mem[n][h][d] = 0;
    foreach (i; 1..K+1) {
        mem[n][h][d] = (mem[n][h][d] + dp(n-i, h+1, (d || (i >= D)))) % MOD;
    }
    return mem[n][h][d];
}

void main() {
    scanf("%d %d %d", &N, &K, &D);
    mem.each!(me => me.each!(m => fill(m, -1)));
    dp(N, 0, false).writeln;
    //mem[0..30].writeln;
}


import std.stdio;

void main() {
    string a = "", b = "";
    string s = readln;
    int i = 0;
    for (; i < s.length - 1; ++i) {
        if (s[i] == '|') break;
        a ~= s[i];
    }
    ++i;
    for (; i < s.length - 1; ++i) {
        b ~= s[i];
    }
    string x = readln;
    for (i = 0; i < x.length - 1; ++i) {
        if (a.length > b.length) {
            b ~= x[i];
        } else {
            a ~= x[i];
        }
    }
    if (a.length != b.length) {
        writeln("Impossible");
        return;
    }
    write(a);
    write("|");
    write(b);
    writeln();
}


import std.algorithm;
import std.stdio;
import std.string;

int fun (const (char) [] s)
{
	return s.find !(d => d != '0')
	    .map !(d => d - '0').fold !((a, b) => a * b) (1);
}

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		auto t = s.dup;
		auto res = fun (t);
		foreach_reverse (i, ref c; t)
		{
			if (c != '0')
			{
				c -= 1;
			}
			if (i + 1 < t.length)
			{
				t[i + 1] = '9';
			}
			res = max (res, fun (t));
		}
		writeln (res);
	}
}


import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable inf = 10^^9 + 7;

void main() {
    int n;
    scan(n);

    bool[] p = [true, true, false];

    foreach (i ; 0 .. n) {
        int win;
        scan(win);
        win--;

        if (!p[win]) {
            writeln("NO");
            return;
        }

        foreach (j ; 0 .. 3) {
            if (j != win) {
                p[j] ^= 1;
            }
        }
    }

    writeln("YES");
}

struct UnionFind {
    private {
        int N;
        int[] p;
        int[] rank;
    }

    this (int n) {
        N = n;
        p = iota(N).array;
        rank = new int[](N);
    }

    int find_root(int x) {
        if (p[x] != x) {
            p[x] = find_root(p[x]);
        }

        return p[x];
    }

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

    void unite(int x, int y) {
        int u = find_root(x), v = find_root(y);

        if (u == v) return;

        if (rank[u] < rank[v]) {
            p[u] = v;
        }
        else {
            p[v] = u;

            if (rank[u] == rank[v]) {
                rank[u]++;
            }
        }
    }
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

struct Queue(T) {
    private {
        int N, head, tail;
        T[] data;
    }

    this(int n) {
        N = n + 1;
        data = new T[](N);
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return (tail + 1) % N == head;
    }

    T front() {
        return data[head];
    }

    void push(T x) {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() {
        assert(!empty);
        head = (head + 1) % N;
    }

    void clear() {
        head = tail = 0;
    }
}


import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.string;

void main() {
  readln;
  readln.chomp.split.map!(to!int).array.sort.map!(to!string).join(" ").writeln;
}


import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

void main() {
    int x; scanf("%d\n", &x);
    writefln("%d %d %d", x, 0, 0);
}


import std.stdio;

string ones(int x) {
    switch (x) {
        case 0: return "zero";
        case 1: return "one";
        case 2: return "two";
        case 3: return "three";
        case 4: return "four";
        case 5: return "five";
        case 6: return "six";
        case 7: return "seven";
        case 8: return "eight";
        case 9: return "nine";
        default: return "";
    }
}

string teens(int x) {
    switch (x) {
        case 0: return "ten";
        case 1: return "eleven";
        case 2: return "twelve";
        case 3: return "thirteen";
        case 4: return "fourteen";
        case 5: return "fifteen";
        case 6: return "sixteen";
        case 7: return "seventeen";
        case 8: return "eighteen";
        case 9: return "nineteen";
        default: return "";
    }
}

string tens(int x) {
    switch (x) {
        case 2: return "twenty";
        case 3: return "thirty";
        case 4: return "forty";
        case 5: return "fifty";
        case 6: return "sixty";
        case 7: return "seventy";
        case 8: return "eighty";
        case 9: return "ninety";
        default: return "";
    }
}

void main() {
    int number;
    readf("%d\n", &number);

    if (number < 10) {
        writeln(ones(number));
    } else {
        int one = number % 10;
        int ten = (number / 10) % 10;

        if (ten == 1) {
            writeln(teens(one));
        } else {
            if (one != 0) {
                writeln(tens(ten) ~ "-" ~ ones(one));
            } else {
                writeln(tens(ten));
            }
        }
    }
}


import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math, std.typecons;

int x1, y1, x2, y2;
int x, y;

void main() {
    scan(x1, y1, x2, y2);
    scan(x, y);

    if (abs(x1 - x2) % x || abs(y1 - y2) % y) {
        writeln("NO");
        return;
    }

    int xd = abs(x1 - x2) / x;
    int yd = abs(y1 - y2) / y;

    if ((xd - yd) & 1) {
        writeln("NO");
    }
    else {
        writeln("YES");
    }
}

void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}


import std.stdio, std.string;
import std.algorithm;

long comb(int n, int k)
{
    long res = 1;
    foreach (i; n - k + 1 .. n + 1)
    {
        res *= i;
    }
    foreach (i; 2 .. k + 1)
    {
        res /= i;
    }
    return res;
}

int saiki(int mask, int idx, int k, int[] dp)
{
    if (dp[mask] != -1)
    {
        return dp[mask];
    }
    if (idx == k)
    {
        dp[mask] = 1;
        return 1;
    }
    auto res = 0;
    foreach (i; 0 .. k)
    {
        if (i != idx && (mask & (1 << i)) == 0)
        {
            auto ret = saiki(mask | (1 << i), idx + 1, k, dp);
            res += ret;
        }
    }
    dp[mask] = res;
    return res;
}

void solve(int n, int k)
{
    long ans = 0;
    auto dp = new int[1 << k];
    foreach (i; 0 .. k + 1)
    {
        auto ret0 = comb(n, i);
        fill(dp, -1);
        auto ret1 = saiki(0, 0, i, dp);
        ans += ret0 * ret1;
    }
    writeln(ans);
}

int main(string[] args)
{
    int n, k;
    while (readf("%d %d\n", &n, &k) == 2)
    {
        solve(n, k);
    }
    return 0;
}


import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    string s;
    scan(s);

    string t = "QAQ";

    int n = s.length.to!int;
    int m = t.length.to!int;

    auto dp = new int[][](n + 1, m + 1);
    dp[0][0] = 1;

    foreach (i ; 1 .. n + 1) {
        foreach (j ; 0 .. m + 1) {
            dp[i][j] = dp[i-1][j];
            if (1 <= j && s[i-1] == t[j-1]) {
                dp[i][j] += dp[i-1][j-1];
            }
        }
    }

    debug {
        writefln("%(%(%s %)\n%)", dp);
    }

    int ans = dp[n][m];
    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

struct Queue(T) {
    private {
        int N, head, tail;
        T[] data;
    }

    this(int n) {
        N = n + 1;
        data = new T[](N);
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return (tail + 1) % N == head;
    }

    T front() {
        return data[head];
    }

    void push(T x) {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() {
        assert(!empty);
        head = (head + 1) % N;
    }

    void clear() {
        head = tail = 0;
    }
}


import std.stdio, std.string, std.algorithm;
import std.conv, std.array;
import std.container;
import std.typecons;

void format(int x) {
    int h = x / 60;
    int m = x % 60;
    printf("%02d:%02d\n", h, m);
}

void main() {
    int sh, sm, th, tm;
    readf("%d:%d\n", &sh, &sm);
    readf("%d:%d\n", &th, &tm);
    int s = sh * 60 + sm;
    int t = th * 60 + tm;
    format((s - t + 24 * 60) % (24 * 60));
}


module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,m;
loop:while(read(n,m))
	{
		if(n==m)writeln(n);
		else writeln(2);
	}
}


import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;
import std.string;
import std.math;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int[] a, b;
	a.length = n; b.length = n;
	foreach (int i; 0..n)
	{
		int l1, l2;
		scanf("%d %d", &l1, &l2);
		a[i] = l1; b[i] = l2;
	}
	int[int] fb;
	foreach (int i; 0..n)
	{
		fb[a[i]]++;
	}
	int num = 0;
	foreach (int i; 0..n)
	{
		try {
			int ll = fb[b[i]];
			num += ll;
		} catch {
		}
	}
	printf("%d", num);
	return 0;
}


/+ dub.sdl:
    name "A"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    string s;
    sc.read(s);
    int n = s.length.to!int;
    bool check(string t) {
        return !equal(t, t.retro);
    }
    int ans = 0;
    foreach (i; 0..n) {
        foreach (j; i+1..n+1) {
            if (check(s[i..j])) ans = max(ans, j-i);
        }
    }
    writeln(ans);
    return 0;
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length != 0) {
            enforce(readSingle(args[0]));
            read(args[1..$]);
        }
    }
    bool hasNext() {
        return succ();
    }
}

/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/


import core.bitop;
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

bool solve (int h, int m, int s, int t1, int t2)
{
	for (int t = t1; t != t2; t = (t + 1) % 120)
	{
		if (t == h || t == m || t == s)
		{
			return false;
		}
	}
	return true;
}

void main ()
{
	int h, m, s, t1, t2;
	while (readf (" %s %s %s %s %s", &h, &m, &s, &t1, &t2) > 0)
	{
		long res = 0;
		h = h % 12 * 10 + 1;
		m = m % 60 * 2 + 1;
		s = s % 60 * 2;
		t1 = t1 % 12 * 10;
		t2 = t2 % 12 * 10;
		writeln (solve (h, m, s, t1, t2) ||
		    solve (119 - h, 119 - m, 119 - s, 119 - t1, 119 - t2) ?
		    "YES" : "NO");
	}
}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto L = s[1].to!long;
    auto R = s[2].to!long;

    long r = R + 2 - R % 3;
    long l = L - L % 3;
    long x = (r - l + 1) / 3;

    auto num = new long[](3);
    num[] = x;

    if (L % 3 == 1) {
        num[0] -= 1;
    } else if (L % 3 == 2) {
        num[0] -= 1;
        num[1] -= 1;
    }

    if (R % 3 == 0) {
        num[1] -= 1;
        num[2] -= 1;
    } else if (R % 3 == 1) {
        num[2] -= 1;
    }

    auto dp = new long[][](N+1, 3);
    dp[0][0] = 1;

    foreach (i; 0..N) {
        foreach (j; 0..3) {
            foreach (k; 0..3) {
                (dp[i+1][(j+k)%3] += dp[i][j] * num[k] % MOD) %= MOD;
            }
        }
    }

    dp[N][0].writeln;
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto a = RD;
	auto b = RD;
	auto c = RD;

	auto week = min(min(a / 3, b / 2), c / 2);
	auto arr = [0, 0, 1, 2, 0, 2, 1];
	arr ~= arr.dup;

	long best;
	foreach (day; 0..7)
	{
		auto aa = a - week * 3;
		auto bb = b - week * 2;
		auto cc = c - week * 2;
		long cnt;
		foreach (i; day..14)
		{
			if (arr[i] == 0)
			{
				if (aa == 0) break;
				--aa;
				++cnt;
			}
			else if (arr[i] == 1)
			{
				if (bb == 0) break;
				--bb;
				++cnt;
			}
			else
			{
				if (cc == 0) break;
				--cc;
				++cnt;
			}
		}
		best = max(best, cnt);
	}
	auto ans = week * 7 + best;

	writeln(ans);
	stdout.flush();
	debug readln();
}


import std.functional,
       std.algorithm,
       std.container,
       std.typetuple,
       std.typecons,
       std.bigint,
       std.string,
       std.traits,
       std.array,
       std.range,
       std.stdio,
       std.conv;

void main() {
  int[] i1 = readln.chomp.split(":").to!(int[]),
        i2 = readln.chomp.split(":").to!(int[]);

  int bh = i1[0],
      eh = i2[0];
  int bm = i1[1],
      em = i2[1];
  int mh, mm;
  int dur;

  if (bm < em) {
    dur = (eh - bh) * 60 + em - bm;
  } else {
    dur = (eh - bh - 1) * 60 + 60 + em - bm;
  }

  mh = bh + (bm + dur/2) / 60;
  mm = (bm + dur/2) % 60;
  writefln("%02d:%02d", mh, mm);
}


import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.bigint, std.math, std.range;
import core.thread;

//	Input
string[] tokens;
int tokenId = 0;
string readToken() { for (; tokenId == tokens.length; ) tokens = readln.split, tokenId = 0; return tokens[tokenId++]; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, bool delegate(T) test) {
	int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp;
}
int lowerBound(T)(T[] as, T val) { return as.binarySearch((T a){ return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a){ return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs == null || bs[$ - 1] != a) bs ~= a; return bs; }

int gcd(int a, int b) {
	return (b != 0) ? gcd(b, a % b) : a;
}

void main(string[] args) {
	int N = readInt;
	int[] A = new int[N];
	foreach (i; 0 .. N) {
		A[i] = readInt;
	}
	int g = 0;
	foreach (a; A) {
		g = gcd(g, a);
	}
	writeln(g * N);
}


module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}
int[][] mat = new int[][](3, 3);
void toggle(int i, int j) {
        mat[i][j] = mat[i][j] == 1 ? 0 : 1;
        if (i + 1 < 3) mat[i + 1][j] = mat[i + 1][j] == 1 ? 0 : 1;
        if (j + 1 < 3) mat[i][j + 1] = mat[i][j + 1] == 1 ? 0 : 1;
        if (i - 1 >= 0) mat[i - 1][j] = mat[i - 1][j] == 1 ? 0 : 1;
        if (j - 1 >= 0) mat[i][j - 1] = mat[i][j - 1] == 1 ? 0 : 1;
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int[][] state = new int[][](3, 3);
                for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 3; j++) {
                                mat[i][j] = 1;
                        }
                }

                foreach (ref i; state) foreach (ref j; i) j = cin.readInt;

                for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 3; j++) {
                                if (state[i][j] % 2) {
                                        toggle(i, j);
                                }
                        }
                }

                foreach (i; mat) {
                        foreach (j; i) {
                                write(j);
                        }
                        writeln();
                }
        }
}


import std.algorithm;
import std.stdio;

immutable int MAX_N = 1_000_000;

bool [] bool_primes (int n) {
    auto a = new bool [n + 1];
    a[] = true;
    a[0] = a[1] = false;
    for (int i = 2; i * i <= n; i++)
        if (a[i])
            for (int j = i * i; j <= n; j += i)
                a[j] = false;
    return a;
}

int [] cumulative_sums (const bool [] a) {
    auto res = new int [a.length];
    res[0] = a[0];
    foreach (i; 1..a.length)
        res[i] = res[i - 1] + a[i];
    return res;
}

immutable bool [] p = bool_primes (MAX_N);
immutable int [] cs = cumulative_sums (p);

void main () {
    int a, b, k;
    while (readf (" %s %s %s", &a, &b, &k)) {
        bool check (int l) {
            foreach (x; a..b - l + 2)
                if (cs[x + l - 1] - cs[x - 1] < k)
                    return false;
            return true;
        }

        int l = binary_search !(check) (1, b - a + 2);
        if (l > b - a + 1)
            l = -1;
        writefln ("%s", l);
    }
}

int binary_search (alias pred) (int lo, int hi) {
    while (lo < hi) {
        int me = (lo + hi) / 2;
        if (pred (me))
            hi = me;
        else
            lo = me + 1;
    }
    return lo;
}


import core.bitop, std.algorithm, std.array, std.range, std.stdio, std.string;
void main () {
	readln;
	auto s = readln.strip.split.sort.uniq.array;
	1024.iota.filter !(m => s.map !(c => m.reduce !((a, b) =>
	    a & ~(c.find ("RGBYW12345"[b]).empty << b)) (10.iota))
	    .array.sort.uniq.array.length == s.length)
	    .map!popcnt.reduce!min.writeln;
}


import std.stdio;
int main()
{
	int a=0;
	int b=0;
	int c=0;
	int res=0;
	readf(" %d", &a);
	readf(" %d", &b);
	readf(" %d", &c);
	for (int i=0; i<c; i++)
	{
		res=res+a+a+b+b-4;
		a=a-4;
		b=b-4;
	}
	writeln(res);
	return 0;
}


void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n; rd(n);
  auto as=readln.split.to!(int[]);

  auto used=new bool[](1001);
  int[] com;
  foreach_reverse(a; as){
    if(used[a]==false){
      com=a~com;
      used[a]=true;
    }
  }

  writeln(com.length);
  writefln("%(%s %)", com);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}


import std.stdio;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		writeln ((((1L << n)) - 1) << 1);
	}
}


import std.conv, std.stdio, std.string, std.array, std.range, std.algorithm;

string s = "CODEFORCES";

void main() {

	char[] t = readln.strip.dup;

	int len, j;
	char[] begin, end;

	foreach (i, e; s) {
		if (e == t[i]) {
			begin ~= t[i];
		}
		else {
			break;
		}
	}

	j = 0;
	foreach_reverse (i, e; s) {
		if (e == t[$ - j - 1]) {
			end ~= t[$ - j - 1];
		}
		else {
			break;
		}
		++j;
	}

	if ((begin ~ end).length >= 10) {
		writeln("YES");
		return;
	}

	writeln("NO");
}


import std.algorithm, std.array, std.conv, std.stdio, std.string;

bool solve () {
	if (!readln) return false;
	auto adj = new int [] [128];
	int step = 0, cur = 1;

	immutable string answer = q{{writeln ("! ", cur); stdout.flush; return true;}};

	immutable string ask = q{
		writeln ("? ", cur); stdout.flush; step += 1;
		readln; adj[cur] = readln.split.map !(to !(int)).array;
		if (adj[cur].length == 2) mixin (answer);
	};

	auto u = [cur];
	mixin (ask);
	while (adj[cur].length != 1) {
		cur = adj[cur].filter !(u => adj[u].empty).front;
		u ~= cur; mixin (ask);
		debug {writeln ("p ", u);}
	}
	reverse (u);
	cur = 1;
	do
	{
		while (adj[cur].length != 1 || u.length < 2) {
			cur = adj[cur].filter !(u => adj[u].empty).front;
			u ~= cur; mixin (ask);
			debug {writeln ("t ", u);}
		}
		u = u[0..$ / 2 + 1];
		cur = u[$ - 1];
		debug {writeln ("x ", u);}
	}
	while (u.length < 4);
	int [] q;
	while (step < 16) {
		q ~= adj[cur].filter !(u => adj[u].empty).array;
		cur = q[0]; q = q[1..$]; mixin (ask);
	}
	cur = q[0]; mixin (answer);
}

void main () {readln; while (solve) {}}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto x0 = RD;
	auto y0 = RD;
	auto ax = RD;
	auto ay = RD;
	auto bx = RD;
	auto by = RD;
	auto xs = RD;
	auto ys = RD;
	auto t = RD;

	/*auto x_base = min(x0, xs);
	auto y_base = min(y0, ys);
	x0 -= x_base;
	y0 -= y_base;
	xs -= x_base;
	ys -= y_base;*/

	long dist(long[] lhs, long[] rhs)
	{
		return max(lhs[0], rhs[0])-min(lhs[0], rhs[0]) + max(lhs[1], rhs[1])-min(lhs[1], rhs[1]);
	}

	long[][] pos;
	pos ~= [x0, y0];
	foreach (i; 1..10^^17)
	{
		auto x = pos[$-1][0];
		auto y = pos[$-1][1];
		pos ~= [x * ax + bx, y * ay + by];
		debug writeln(pos[$-1]);
		debug writeln([xs, ys]);
		debug writeln(dist(pos[$-1], [xs, ys]));
		if (pos[$-1][0] < x || pos[$-1][1] < y || (pos[$-1][0]-xs + pos[$-1][1]-ys) > t)
		{
			pos.length = pos.length - 1;
			break;
		}
	}
	debug writeln(pos);

	long ans;
	foreach (i; 0..pos.length)
	{
		{
			auto tt = t;
			auto p = [xs, ys];
			long cnt;
			foreach (j; i..pos.length)
			{
				tt -= dist(pos[j], p);
				if (tt < 0) break;
				p = pos[j].dup;
				++cnt;
			}
			ans.chmax(cnt);
			debug writeln("i:", i, " cnt:", cnt);
		}
		{
			auto tt = t;
			auto p = [xs, ys];
			long cnt;
			foreach_reverse (j; 0..i+1)
			{
				tt -= dist(pos[j], p);
				if (tt < 0) break;
				p = pos[j].dup;
				++cnt;
			}
			ans.chmax(cnt);
			debug writeln("i:", i, " cnt:", cnt);
		}
	}

	writeln(ans);
	stdout.flush;
	debug readln;
}


import std.algorithm, std.conv, std.range, std.stdio, std.string;

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n; readV(n);
  writeln(n%2 == 0 ? "Mahmoud" : "Ehab");
}


import std.stdio;
import std.format;
import std.range;
import std.algorithm;
import std.math;

void findx(long k) {
  int num_digit = 1;
  long mul =1;
  long acc = 0;

  while(true) {
    acc = acc + 9*mul*num_digit;
    if (acc >= k) {
      break;
    }
    num_digit++;
    mul = mul*10;
  }
  long l = mul;
  long a = acc - 9*mul*num_digit;
  assert(k > a);
  k = k-a;
  long idx = (k-1) / num_digit;
  long res = k - idx*num_digit;
  long nn = l + idx;

  stderr.writeln(res);

  writeln( format("%d", nn)[cast(uint)(res-1)] );
}

void main() {
  long k;
  stdin.readf("%d", k);

  findx(k);

}