#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define sp(n) fixed << setprecision(n)
#define pb push_back
#define ff first
#define ss second

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int t[3][3];
ll dp[41][3][3];

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

#ifdef LOCAL_DEBUG
	freopen("input.txt", "r", stdin);
#endif

	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			cin >> t[i][j];

	int n;
	cin >> n;

	for (int k = 1; k <= n; k++) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (i == j) continue;
				ll cnt1 = dp[k - 1][i][3 - i - j] + t[i][j] + dp[k - 1][3 - i - j][j];
				ll cnt2 = dp[k - 1][i][j] + t[i][3 - i - j] + dp[k - 1][j][i] +
					t[3 - i - j][j] + dp[k - 1][i][j];
				dp[k][i][j] = min(cnt1, cnt2);
			}
		}
	}

	cout << dp[n][0][2] << endl;

	return 0;
}


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

int main() {
int i = 0 , j= 0;
string s,t;cin>>s>>t;
int count = 1 ;
while(i<s.size()&&j<t.size()){
    if(s[i]==t[j]){
        count++;
        i++;
    }

    j++;
}
cout<<count;

return 0 ;
}


#include <bits/stdc++.h>
#ifndef _LIB_POLYNOMIAL_RING
#define _LIB_POLYNOMIAL_RING
#include <bits/stdc++.h>
#ifndef _LIB_MODULAR_INTEGER
#define _LIB_MODULAR_INTEGER
#include <bits/stdc++.h>
#ifndef _LIB_NUMBER_THEORY
#define _LIB_NUMBER_THEORY
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace nt {
  int64_t inverse(int64_t a, int64_t b) {
    long long b0 = b, t, q;
    long long x0 = 0, x1 = 1;
    if (b == 1) return 1;
    while (a > 1) {
      q = a / b;
      t = b, b = a % b, a = t;
      t = x0, x0 = x1 - q * x0, x1 = t;
    }
    if (x1 < 0) x1 += b0;
    return x1;
  }
}
}  // namespace lib

#endif

#if __cplusplus < 201300
#error required(c++14)
#endif

namespace lib {
  using namespace std;
namespace {
  template<typename T, T ...Mods>
  struct ModularIntegerBase {
    typedef ModularIntegerBase<T, Mods...> type;

    T x[sizeof...(Mods)];
    friend ostream& operator<<(ostream& output, const type& var) {
      output << "(";
      for(int i = 0; i < sizeof...(Mods); i++) {
        if(i) output << ", ";
        output << var.x[i];
      }
      return output << ")";
    }
  };

  template<typename T, T Mod>
  struct ModularIntegerBase<T, Mod> {
    typedef ModularIntegerBase<T, Mod> type;

    T x[1];

    explicit operator int() const { return this->x[0]; }
    explicit operator long long() const { return this->x[0]; }
    explicit operator double() const { return this->x[0]; }
    explicit operator long double() const { return this->x[0]; }
    friend ostream& operator<<(ostream& output, const type& var) {
      return output << var.x[0];
    }
  };

  template<typename T, class Enable, T ...Mods>
  struct ModularIntegerImpl : ModularIntegerBase<T, Mods...> {
    typedef ModularIntegerImpl<T, Enable, Mods...> type;
    typedef T type_int;
    typedef int64_t large_int;
    const static size_t n_mods = sizeof...(Mods);

    struct Less {
      bool operator()(const type& lhs, const type& rhs) const {
        for(size_t i = 0; i < sizeof...(Mods); i++)
          if(lhs.x[i] != rhs.x[i])
            return lhs.x[i] < rhs.x[i];
        return false;
      };
    };
    typedef Less less;

    constexpr static T mods[sizeof...(Mods)] = {Mods...};
    using ModularIntegerBase<T, Mods...>::x;

    ModularIntegerImpl() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = T();
    }
    ModularIntegerImpl(large_int y) {
      for(size_t i = 0; i < sizeof...(Mods); i++) {
        x[i] = y % mods[i];
        if(x[i] < 0) x[i] += mods[i];
      }
    }
    static type with_remainders(T y[sizeof...(Mods)]) {
      type res;
      for(size_t i = 0; i < sizeof...(Mods); i++)
        res.x[i] = y[i];
      res.normalize();
      return res;
    }

    void normalize() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((x[i] %= mods[i]) < 0)
          x[i] += mods[i];
    }

    inline T operator[](int i) const { return x[i]; }

    inline T multiply(T a, T b, T mod) const {
      return (large_int)a*b % mod;
    }

    inline T inv(T a, T mod) const {
      return static_cast<T>(nt::inverse(a, mod));
    }

    type inverse() const {
      T res[sizeof...(Mods)];
      for(size_t i = 0; i < sizeof...(Mods); i++)
        res[i] = inv(x[i], mods[i]);
      return type::with_remainders(res);
    }

    template<typename U>
    T power_(T a, U p, T mod) {
      if(mod == 1) return T();
      if(p < 0) {
        if(a == 0)
          throw domain_error("0^p with negative p is invalid");
        p = -p;
        a = inv(a, mod);
      }
      if(p == 0) return T(1);
      if(p == 1) return a;
      T res = 1;
      while(p > 0) {
        if(p&1)
          res = multiply(res, a, mod);
        p >>= 1;
        a = multiply(a, a, mod);
      }
      return res;
    }

    type& operator+=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        (x[i] += rhs.x[i]) %= mods[i];
      return *this;
    }
    type& operator-=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((x[i] -= rhs.x[i]) < 0)
          x[i] += mods[i];
      return *this;
    }
    type& operator*=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(x[i], rhs.x[i], mods[i]);
      return *this;
    }
    type& operator/=(const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(x[i], inv(rhs.x[i], mods[i]), mods[i]);
      return *this;
    }

    type& operator+=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] += rhs;
      normalize();
      return *this;
    }

    type& operator-=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] -= rhs;
      normalize();
      return *this;
    }

    type& operator*=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(x[i], rhs, mods[i]);
      return *this;
    }

    type& operator/=(T rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = multiply(inv(rhs, mods[i]), x[i], mods[i]);
      return *this;
    }

    type& operator^=(large_int p) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        x[i] = power_(x[i], p, mods[i]);
      return *this;
    }

    type& operator++() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((++x[i]) >= mods[i])
          x[i] -= mods[i];
      return *this;
    }
    type& operator--() {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if((--x[i]) < 0)
          x[i] += mods[i];
      return *this;
    }
    type operator++(int unused) { type res = *this; ++(*this); return res; }
    type operator--(int unused) { type res = *this; --(*this); return res; }

    friend type operator+(const type& lhs, const type& rhs) {
      type res = lhs;
      return res += rhs;
    }
    friend type operator-(const type& lhs, const type& rhs) {
      type res = lhs;
      return res -= rhs;
    }
    friend type operator*(const type& lhs, const type& rhs) {
      type res = lhs;
      return res *= rhs;
    }
    friend type operator/(const type& lhs, const type& rhs) {
      type res = lhs;
      return res /= rhs;
    }

    friend type operator+(const type& lhs, T rhs) {
      type res = lhs;
      return res += rhs;
    }

    friend type operator-(const type& lhs, T rhs) {
      type res = lhs;
      return res -= rhs;
    }

    friend type operator*(const type& lhs, T rhs) {
      type res = lhs;
      return res *= rhs;
    }

    friend type operator/(const type& lhs, T rhs) {
      type res = lhs;
      return res /= rhs;
    }

    friend type operator^(const type& lhs, large_int rhs) {
      type res = lhs;
      return res ^= rhs;
    }

    friend type power(const type& lhs, large_int rhs) {
      return lhs ^ rhs;
    }

    type operator-() const {
      type res = *this;
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if(res.x[i])
          res.x[i] = mods[i] - res.x[i];
      return res;
    }

    friend bool operator==(const type& lhs, const type& rhs) {
      for(size_t i = 0; i < sizeof...(Mods); i++)
        if(lhs.x[i] != rhs.x[i])
          return false;
      return true;
    }
    friend bool operator!=(const type& lhs, const type& rhs) {
      return !(lhs == rhs);
    }

    friend istream& operator>>(istream& input, type& var) {
      T y; cin >> y;
      var = y;
      return input;
    }
  };
}

  // Explicitly make constexpr available for linkage.
  template<typename T, class Enable, T ...Mods>
  constexpr T ModularIntegerImpl<T, Enable, Mods...>::mods[];

  template<typename T, T ...Mods>
  using ModularInteger =
    ModularIntegerImpl<T,
                       typename enable_if<is_integral<T>::value>::type,
                       Mods...>;

  template<int32_t ...Mods>
  using Mint32 = ModularInteger<int32_t, Mods...>;

  template<int64_t ...Mods>
  using Mint64 = ModularInteger<int64_t, Mods...>;
}  // namespace lib

#endif
#ifndef _LIB_TRAITS
#define _LIB_TRAITS
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace traits {

  /// keep caide
  template<typename Iterator>
  using IteratorCategory =
    typename iterator_traits<Iterator>::iterator_category;

  /// keep caide
  template<typename Container>
  using IteratorCategoryOf = IteratorCategory<typename Container::iterator>;

  /// keep caide
  template<typename Iterator>
  using IsRandomIterator =
    is_base_of<random_access_iterator_tag, IteratorCategory<Iterator>>;

  /// keep caide
  template<typename Iterator>
  using IsInputIterator =
    is_base_of<input_iterator_tag, IteratorCategory<Iterator>>;

  /// keep caide
  template<typename Iterator>
  using IsBidirectionalIterator =
    is_base_of<bidirectional_iterator_tag, IteratorCategory<Iterator>>;

  /// keep caide
  template<typename Container>
  using HasRandomIterator =
    is_base_of<random_access_iterator_tag, IteratorCategoryOf<Container>>;

  /// keep caide
  template<typename Container>
  using HasInputIterator =
    is_base_of<input_iterator_tag, IteratorCategoryOf<Container>>;

  /// keep caide
  template<typename Container>
  using HasBidirectionalIterator =
    is_base_of<bidirectional_iterator_tag, IteratorCategoryOf<Container>>;
}
}

#endif
#ifndef _LIB_MATH
#define _LIB_MATH
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace math {

  /// caide keep
  template<typename Type>
  struct DefaultPowerOp {
    Type operator()() const { return Type(1); }
    Type operator()(const Type& a) const { return a; }
    void operator()(Type& x, const Type& a, long long cur) const {
      x *= x;
      if(cur & 1)
        x *= a;
    }
  };

  template<typename Type, typename Op>
  Type generic_power(const Type& a, long long n, Op op) {
    if(n == 0) return op();
    Type res = op(a);
    int hi = 63 - __builtin_clzll(n);
    for(int i = hi - 1; ~i; i--) {
      op(res, a, n >> i);
    }
    return res;
  }

  template<typename Type>
  Type generic_power(const Type& a, long long n) {
    return generic_power(a, n, DefaultPowerOp<Type>());
  }
}  // namespace math
}  // namespace lib

#endif

namespace lib {
  using namespace std;
namespace math {
namespace poly {

  namespace {
    /// keep caide
    using traits::IsInputIterator;
    /// keep caide
    using traits::HasInputIterator;
  }

  template<typename P>
  struct DefaultPowerOp {
    int mod;
    DefaultPowerOp(int mod) : mod(mod) {}
    P operator()() const { return P(1); }
    P operator()(const P& a) const { return a % mod; }
    void operator()(P& x, const P& a, long long cur) const {
      (x *= x) %= mod;
      if(cur & 1)
        (x *= a) %= mod;
    }
  };

  template<typename P>
  struct ModPowerOp {
    const P& mod;
    ModPowerOp(const P& p) : mod(p) {}
    P operator()() const { return P(1); }
    P operator()(const P& a) { return a % mod; }
    void operator()(P& x, const P& a, long long cur) const {
      (x *= x) %= mod;
      if(cur & 1)
        (x *= a) %= mod;
    }
  };

  template<typename P>
  struct ModShiftPowerOp {
    const P& mod;
    ModShiftPowerOp(const P& p) : mod(p) {}
    P operator()() const { return P(1); }
    P operator()(const P& a) { return a % mod; }
    void operator()(P& x, const P& a, long long cur) const {
      //if(cur < mod.degree())
        //x = P::kth(cur);
      if(cur & 1)
        (x *= (x << 1)) %= mod;
      else
        (x *= x) %= mod;
    }
  };

  struct DefaultDivmod;
  struct NaiveDivmod;

  template<typename Field, typename Mult, typename Divmod = DefaultDivmod>
  struct Polynomial {
    constexpr static bool NaiveMod =
      is_same<Divmod, NaiveDivmod>::value;
    typedef Polynomial<Field, Mult, Divmod> type;
    typedef Field field;
    vector<Field> p;

    Polynomial() : p(0) {}
    explicit Polynomial(Field x): p(1, x) {}

    template<
      typename Iterator,
      typename enable_if<IsInputIterator<Iterator>::value>::type* = nullptr>
    Polynomial(Iterator begin, Iterator end) : p(distance(begin, end)) {
      int i = 0;
      for(auto it = begin; it != end; ++it, ++i)
        p[i] = *it;
      normalize();
    }

    template<
      typename Container,
      typename enable_if<HasInputIterator<Container>::value>::type* = nullptr>
    Polynomial(const Container& container)
      : Polynomial(container.begin(), container.end()) {}

    Polynomial(const initializer_list<Field>& v)
      : Polynomial(v.begin(), v.end()) {}

    static type from_root(const Field& root) {
      return Polynomial({-root, 1});
    }

    void normalize() const {
      type* self = const_cast<type*>(this);
      int sz = self->p.size();
      while(sz > 0 && self->p[sz-1] == 0) sz--;
      if(sz != (int)self->p.size())
        self->p.resize(sz);
    }

    inline int size() const { return p.size(); }
    inline int degree() const { return max((int)p.size()-1, 0); }
    bool null() const {
      for(Field x : p)
        if(x != 0) return false;
      return true;
    }

    inline Field operator[](const int i) const {
      if(i >= size())
        return 0;
      return p[i];
    }
    inline Field& operator[](const int i) {
      if(i >= size())
        p.resize(i + 1);
      return p[i];
    }

    type& operator+=(const type& rhs) {
      if(rhs.size() > size())
        p.resize(rhs.size());
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] += rhs[i];
      normalize();
      return *this;
    }

    type& operator-=(const type& rhs) {
      if(rhs.size() > size())
        p.resize(rhs.size());
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] -= rhs[i];
      normalize();
      return *this;
    }

    type& operator*=(const type& rhs) {
      p = Mult()(p, rhs.p);
      normalize();
      return *this;
    }

    type& operator*=(const Field& rhs) {
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] *= rhs;
      normalize();
      return *this;
    }

    type& operator/=(const Field& rhs) {
      int sz = size();
      for(int i = 0; i < sz; i++)
        p[i] /= rhs;
      normalize();
      return *this;
    }

    type& operator<<=(const int rhs) {
      if(rhs < 0) return *this >>= rhs;
      if(rhs == 0) return *this;
      int sz = size();
      p.resize(sz + rhs);
      for(int i = sz-1; i >= 0; i--)
        p[i + rhs] = p[i];
      fill_n(p.begin(), rhs, 0);
      return *this;
    }

    type& operator>>=(const int rhs) {
      if(rhs < 0) return *this <<= rhs;
      if(rhs == 0) return *this;
      int sz = size();
      if(rhs >= sz) {
        p.clear();
        return *this;
      }
      for(int i = rhs; i < sz; i++)
        p[i - rhs] = p[i];
      p.resize(sz - rhs);
      return *this;
    }

    type& operator%=(const int rhs) {
      if(rhs < size())
        p.resize(rhs);
      normalize();
      return *this;
    }

    type& operator/=(const type& rhs) {
      return *this = *this / rhs;
    }

    type operator%=(const type& rhs) {
      return *this = *this % rhs;
    }

    type operator+(const type& rhs) const {
      type res = *this;
      return res += rhs;
    }

    type operator-(const type& rhs) const {
      type res = *this;
      return res -= rhs;
    }

    type operator*(const type& rhs) const {
      return type(Mult()(p, rhs.p));
    }

    type operator*(const Field& rhs) const {
      type res = *this;
      return res *= rhs;
    }

    type operator/(const Field& rhs) const {
      type res = *this;
      return res /= rhs;
    }

    type operator<<(const int rhs) const {
      type res = *this;
      return res <<= rhs;
    }

    type operator>>(const int rhs) const {
      type res = *this;
      return res >>= rhs;
    }

    type operator%(const int rhs) const {
      return Polynomial(p.begin(), p.begin() + min(rhs, size()));
    }

    type operator/(const type& rhs) const {
      return type::divmod(*this, rhs).first;
    }

    type operator%(const type& rhs) const {
      return type::divmod(*this, rhs).second;
    }

    bool operator==(const type& rhs) const {
      normalize();
      rhs.normalize();
      return p == rhs.p;
    }

    type inverse() const {
      int sz = size();
      if(sz == 0)
        return type();
      if(sz == 1) {
        assert(p[0] != 0);
        return type(p[0].inverse());
      }
      type q = (*this % ((sz+1)/2)).inverse();
      q -= (*this * q % sz - type(1)) * q % sz;
      return q % sz;
    }

    type reciprocal() const {
      normalize();
      return type(p.rbegin(), p.rend());
    }

    type integral() const {
      int sz = size();
      if(sz == 0) return {};
      type res = *this;
      for(int i = sz; i; i--) {
        res[i] = res[i-1] / i;
      }
      res[0] = 0;
      res.normalize();
      return res;
    }

    type derivative() const {
      int sz = size();
      if(sz == 0) return {};
      type res = *this;
      for(int i = 0; i + 1 < sz; i++) {
        res[i] = res[i+1] * (i + 1);
      }
      res.p.back() = 0;
      res.normalize();
      return res;
    }

    static pair<type, type> divmod(const type& a, const type& b) {
      if(NaiveMod) return naive_divmod(a, b);
      a.normalize();
      b.normalize();
      int m = a.size();
      int n = b.size();
      if(m < n) return {Polynomial(), a};
      type ar = a.reciprocal();
      type bri = (b.reciprocal() % (m - n + 1)).inverse();
      type q = (ar * bri % (m - n + 1)).reciprocal();
      type r = a - b * q;

      if(r.degree() >= b.degree()) {
        q += divmod(r, b).first;
        r = a - b * q;
      }

      return {q, r};
    }

    static pair<type, type> naive_divmod(const type& a, const type& b) {
      type res = a;
      int a_deg = a.degree();
      int b_deg = b.degree();
      Field normalizer = Field(1) / b[b_deg];
      for(int i = 0; i < a_deg-b_deg+1; i++) {
        Field coef = (res[a_deg - i] *= normalizer);
        if(coef != 0) {
          for(int j = 1; j <= b_deg; j++) {
            res[a_deg - i - j] += -b[b_deg - j] * coef;
          }
        }
      }
      return {res >> b_deg, res % b_deg};
    }

    static type power(const type& a, long long n, const int mod) {
      return math::generic_power<type>(a, n, DefaultPowerOp<type>(mod));
    }

    static type power(const type& a, long long n, const type& mod) {
      return math::generic_power<type>(a, n, ModPowerOp<type>(mod));
    }

    static type kth(int K) {
      return type(1) << K;
    }

    static type kth(long long K, const type& mod) {
      return math::generic_power<type>(type(1) << 1, K, ModShiftPowerOp<type>(mod));
    }

    friend ostream& operator<<(ostream& output, const type& var) {
      output << "[";
      int sz = var.size();
      for(int i = sz-1; i >= 0; i--) {
        output << var[i];
        if(i) output << " ";
      }
      return output << "]";
    }
  };
}  // namespace poly
/// keep caide
using poly::Polynomial;
}  // namespace math
}  // namespace lib

#endif
#ifndef _LIB_LINEAR_RECURRENCE
#define _LIB_LINEAR_RECURRENCE
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace linalg {
  namespace {
    using traits::IsRandomIterator;
    using traits::HasRandomIterator;
  }

  template<typename P>
  struct BMSolver {
    typedef BMSolver<P> type;
    typedef typename P::field field_type;
    typedef P poly_type;

    vector<field_type> base;
    vector<field_type> T;

    template<
      typename Iterator,
      typename enable_if<IsRandomIterator<Iterator>::value>::type* = nullptr>
    void solve(Iterator begin, Iterator end) {
      auto get = [begin](int i) { return *(begin + i); };

      int n = distance(begin, end);

      vector<field_type> C = {1}, B = {1};
      field_type b = 1;
      int L = 0;

      for(int i = 0, x = 1; i < n; i++, x++) {
        // evaluate new element
        field_type d = 0;
        for(size_t j = 0; j < C.size(); j++)
          d += get(i-j) * C[j];
        if(d == 0) continue;
        if(2 * L <= i) {
          auto tmp = C;
          if(C.size() < B.size() + x)
            C.resize(B.size() + x);
          field_type coef = d / b;
          for(size_t j = 0; j < B.size(); j++)
            C[j + x] -= coef * B[j];
          L = i + 1 - L;
          B = tmp;
          b = d;
          x = 0;
        } else {
          if(C.size() < B.size() + x)
            C.resize(B.size() + x);
          field_type coef = d / b;
          for(size_t j = 0; j < B.size(); j++)
            C[j + x] -= coef * B[j];
        }
      }

      T = vector<field_type>((int)C.size() - 1);
      for(size_t i = 0; i < T.size(); i++) T[i] = -C[i+1];
      base = vector<field_type>(begin, end);
    }

    template<
      typename Container,
      typename enable_if<HasRandomIterator<Container>::value>::type* = nullptr>
    void solve(const Container& container) {
      solve(container.begin(), container.end());
    }

    void solve(const initializer_list<field_type>& l) {
      solve(l.begin(), l.end());
    }

    bool solved() const {
      return T.size() > 0 && base.size() >= T.size();
    }

    void ensure(int nsz) const {
      auto* self = const_cast<type*>(this);
      for(int j = base.size(); j < nsz; j++) {
        field_type acc = 0;
        for(int i = 0; i < (int)T.size(); i++)
          acc += base[j - i - 1] * T[i];
        self->base.push_back(acc);
      }
    }

    poly_type mod_function() const {
      poly_type res;
      int m = T.size();
      res[m] = 1;
      for(int i = 0; i < m; i++)
        res[i] = -T[m-i-1];
      return res;
    }

    vector<field_type> compute(long long K, int n) {
      assert(n > 0);
      assert(solved());
      vector<field_type> res;
      int N = T.size();
      int cons = min(n, N);

      if(K < (int)base.size()) {
        for(int j = 0; j < n && K + j < (int)base.size(); j++)
          res.push_back({base[K + j]});

        while((int)res.size() < cons) {
          field_type acc = 0;
          int sz = res.size();
          int mid = min(sz, N);
          for(int i = 0; i < mid; i++)
            acc += res[sz - i - 1] * T[i];
          sz = base.size();
          for(int i = mid; i < N; i++)
            acc += base[sz - 1 - (i - mid)] * T[i];
          res.push_back(acc);
        }
      } else {
        ensure(cons + N - 1);

        poly_type x = poly_type::kth(K, mod_function());

        for(int j = 0; j < cons; j++) {
          field_type acc = 0;
            for(int i = 0; i < N; i++)
              acc += x[i] * base[i + j];
          res.push_back(acc);
        }
      }

      for(int j = res.size(); j < n; j++) {
        field_type acc = 0;
        for(int i = 0; i < N; i++)
          acc += res[j - i - 1] * T[i];
        res.push_back(acc);
      }
      return res;
    }

    field_type compute(long long K) {
      return compute(K, 1)[0];
    }
  };
}  // namespace linalg
}  // namespace lib

#endif
#ifndef _LIB_LONG_MULTIPLICATION
#define _LIB_LONG_MULTIPLICATION
#include <bits/stdc++.h>

namespace lib {
  using namespace std;
namespace math {
  struct NaiveMultiplication {
    template<typename Field>
    vector<Field> operator()(const vector<Field>& a, const vector<Field>& b) const {
      vector<Field> res(a.size() + b.size());
      for(size_t i = 0; i < a.size(); i++) {
        for(size_t j = 0; j < b.size(); j++) {
          res[i + j] += a[i] * b[j];
        }
      }
      return res;
    }
  };

  template<typename Mult, typename Field>
  vector<Field> shift_conv(const vector<Field>& a, const vector<Field> b) {
    if(b.empty()) return {};
    reverse(b.begin(), b.end());
    int n = a.size();
    int m = b.size();

    auto res = Mult()(a, b);
    return vector<Field>(res.begin() + m - 1, res.end());
  }
}  // namespace math
}  // namespace lib

#endif
using namespace std;

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ms(v, x) memset((v), (x), sizeof(v))
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
#define iopt ios::sync_with_stdio(false); cin.tie(0)

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
int power(int x, int p, int MOD) {
    if(p == 0) return 1%MOD;
    if(p == 1) return x%MOD;
    int res = power(x, p/2, MOD);
    res = (long long)res*res%MOD;
    if(p&1) res = (long long)res*x%MOD;
    return res;
}

typedef pair<int, int> ii;
typedef long double LD;
typedef vector<int> vi;

using namespace lib;

const int MOD = (int)10007;
using Field = Mint32<MOD>;
using Poly = math::Polynomial<Field, math::NaiveMultiplication, math::poly::NaiveDivmod>;

const int N = 202;
Field dp[N][N][10*N];
bool ok[N][N][10*N];
string s;

Field go(int left, int right, int i) {
  if(i < 0) return 0;
  if(i == 0 && left >= right) return 1;
  Field & res = dp[left][right][i];
  if(ok[left][right][i]) return res;
  res = 0;

  if(left >= right) {
    res += go(left, right, max(i - 2, 0)) * 26;
  } else if (left + 1 == right) {
    if(i == 0) res += go(left + 1, right - 1, i);
    else res += go(left + 1, right, i - 1);
    res += go(left, right, i - 2) * 25;
  } else {
    if(s[left] == s[right - 1]) {
      res += go(left + 1, right - 1, i);
      res += go(left, right, i - 2) * 25;
    } else {
      res += go(left + 1, right, i - 1);
      res += go(left, right - 1, i - 1);
      res += go(left, right, i - 2) * 24;
    }
  }

  ok[left][right][i] = true;
  return res;
}

int32_t main(){
  linalg::BMSolver<Poly> solver;
  vector<Field> base;
  cin >> s;
  int n;
  cin >> n;

  for(size_t i = 0; i < 10 * s.size(); i++) {
    base.pb(go(0, s.size(), i));
  }

  solver.solve(base);
  cout << solver.compute(n) << endl;
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
    int no,moon;
    vector<int> v;
    cin>>no;
    for(int i=0;i<no;i++){
        cin>>moon;
        v.push_back(moon);
    }
    if(no==1){
        if(v[0]==15){
            cout<<"DOWN";
        }
        else if(v[0]==0){
            cout<<"UP";
        }
        else{
            cout<<"-1";
        }
    }
    else if(v[no-1]==15){
        cout<<"DOWN";
    }
    else if(v[no-1]==0){
        cout<<"UP";
    }
    else if(v[no-1]>v[no-2]){
        cout<<"UP";
    }
    else{
        cout<<"DOWN";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x,s;
    cin>>x>>s;
    if(x>s)
    {
        x-=s;
        cout<<s<<" "<<x/2<<endl;
    }
    else
    {
        s-=x;
        cout<<x<<" "<<s/2<<endl;
    }
    return 0;
}


// by shik {{{
#include <bits/stdc++.h>
#include <unistd.h>
#define SZ(x) ((int)(x).size())
#define ALL(c) begin(c),end(c)
#define REP(i,n) for ( int i=0; i<(int)(n); i++ )
#define REP1(i,a,b) for ( int i=(int)(a); i<=(int)(b); i++ )
#define FOR(it,c) for (auto it=begin(c); it!=end(c); it++)
#define MP make_pair
#define PB push_back
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long LL;

void RI() {}

template<typename... T>
void RI( int& head, T&... tail ) {
    scanf("%d",&head);
    RI(tail...);
}
/// }}}

#define N 150010

VI fac[N];
int cnt[N];
void predo() {
    for ( int i=2; i<N; i++ ) for ( int j=i; j<N; j+=i ) fac[j].PB(i);
    for ( int i=2; i<N; i++ ) if ( SZ(fac[i])==1 ) for ( int j=i; j<N; j+=i ) cnt[j]++;
    for ( int i=2; i<N; i++ ) cnt[i]=(cnt[i]%2==1 ? -1 : +1);
    for ( int i=2; i*i<N; i++ ) if ( SZ(fac[i])==1 ) for ( int j=i*i; j<N; j+=i*i ) cnt[j]=0;
}

LL p;

LL shik( LL l, LL r ) {
    return (r+l)*(r-l+1)/2%p;
}

LL shik( LL l, LL r, LL d ) {
    return (r+l)*((r-l)/d+1)/2%p;
}

int main() {
    predo();
    LL n,m,l,r;
    cin>>n>>m>>l>>r>>p;
    LL ans=0;
    for ( LL dx=1; dx<=n && dx*dx<=r*r; dx++ ) {
        LL ly=dx>=l ? 0 : sqrt(l*l-dx*dx);
        LL ry=sqrt(r*r-dx*dx);
        while ( ly==0 || dx*dx+ly*ly<l*l ) ly++;
        while ( dx*dx+ry*ry>r*r ) ry--;
        if ( ry>m ) ry=m;
        if ( ly>ry ) continue;
        ans+=(n+1-dx)*shik(m+1-ry,m+1-ly)%p;
        for (auto f : fac[dx]) {
            LL lly=(ly+f-1)/f;
            LL rry=ry/f;
            if ( lly>rry ) continue;
            LL now=cnt[f]*(n+1-dx)*shik(m+1-f*rry,m+1-f*lly,f)%p;
            ans+=now;
        }
    }
    ans*=2;
    if ( l==1 ) {
        ans+=(n+1)*m%p;
        ans+=n*(m+1)%p;
    }
    ans%=p;
    if ( ans<0 ) ans+=p;
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
long int a,b;
b=2;
cin>>a;
while(a>1)
    {
    if(a%b!=0)
    b++;
    else{cout<<a<<" ";a=a/b;}
    }
cout<<"1";
return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    ll k,b,n,t,z;
    cin>>k>>b>>n>>t;
    z=1;
        while(t>=z*k+b)
        {
            z=z*k+b;
            n--;
           // cout<<n<<" "<<z<<endl;
        }
        cout<<(n>0?n:0)<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 105
#define MOD 1000000007
typedef long long ll;

ll powi(ll a, ll b)
{
    if(!b) return 1;

    ll p = powi(a, b/2);
    p = (p*p)%MOD;
    if(b&1) p = (p*a)%MOD;

    return p;
}

ll len[M];
ll po2[M];
ll dp[M][M][M];

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

    len[0] = len[1] = 1;
    for(int i = 2; i < M; i++) len[i] = (len[i-1]+len[i-2])%(MOD-1);

    for(int i = 0; i < M; i++) po2[i] = powi(2, len[i]);

    int n, x;
    cin>>n>>x;

    string s;
    cin>>s;

    for(int idx = 0; idx <= x; idx++)
    {
        for(int i = 2; i <= n; i++) dp[idx][i][i-1] = 1;
        dp[idx][1][0] = dp[idx][n+1][n] = po2[idx];
    }

    for(int i = 1; i <= n; i++) dp[s[i-1]-'0'][i][i] = 1;

    for(int idx = 2; idx <= x; idx++)
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = i; j <= n; j++)
            {
                for(int k = i; k <= j+1; k++)
                {
                    dp[idx][i][j] += dp[idx-1][i][k-1]*dp[idx-2][k][j];
                    dp[idx][i][j] %= MOD;
                }
            }
        }
    }

    cout<<dp[x][1][n]<<endl;

    return 0;
}


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

struct Pair{

string colour{" "};
string name{" "};};

int last(int a[],int m , int n){
    int j=0;
    for(int i =0;i<n;i++){
        if(a[i]==m){
            j =i;
        }
    }
    return j +1;
}

int main()
{
long long int n,m,c;
cin>>n>>m>>c;

long long int sum = 0;
sum = sum + 2*c;
if(n<m){
    sum += 2*n +1;
    m = m-n;
    n = 0;
} else if (m<n) { sum += 2*m +1;}
else {sum += 2*m;}

cout<<sum;
    return 0;
}


//I/O
#include<iostream>
#include<sstream>
//Containers
#include<deque>
#include<initializer_list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<vector>
//C library
#include<cmath>
#include<cassert>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
//Miscellaneous
#include<algorithm>
#include<bitset>
#include<complex>
#include<functional>
#include<iomanip>
#include<iterator>
#include<limits>
#include<numeric>
#include<random>
#include<tuple>
#include<utility>

//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>

#define endl '\n'
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repi(i, a, b) for(int i=(a); i>(b); i--)
#define db(x) (cerr << #x << ": " << (x) << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define tests(t) int t; cin >> t; while(t--)
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define ll long long
#define gcd __gcd
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)); (v).erase(unique(all(v)), (v).end());
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define PI acos(-1.0)
#define EPS 1e-9
#define mod 1000000007
#define MOD 1000000007
#define N 100005
using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

template<typename T>
using maxpq = priority_queue<T>;

//All indexing is 0-based
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag,
             //tree_order_statistics_node_update> ordered_set;
//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar.

//Returns no. of values x for which ceil(n / x) == y (y must be > 1).
inline ll CC(ll n, ll y) { return iceil(n, y-1) - iceil(n, y); }

//Returns no. of values x for which floor(n / x) == y
inline ll FF(ll n, ll y) { return n / y - n / (y+1); }

inline ll gcd(ll a, ll b) { return !b ? a: gcd(b, a%b); }

//a and b are assumed to be taken modulo p
inline int add(int a, int b, int p = mod){ int c = a + b; if(c >= p) c -= p; return c;}
inline int sub(int a, int b, int p = mod){ int c = a - b; if(c < 0) c += p; return c;}
inline int mul(int a, int b, int p = mod){ return (a * 1ll * b) % p;}

#define int ll
//#define trace(...) 42

int a, b, k, L;

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

int bfs(int src, int dest) {
    trace(src, dest);
    queue<int> q;
    vector<bool> vis (src - dest+ 1);
    q.push(src); q.push(0);
    vis[src-dest] = 1;
    while(1) {
        int u = q.front(); q.pop();
        int d = q.front(); q.pop();
        //trace(u, d);
        if(u == dest) return d;
        if(!vis[u-1-dest]) q.push(u-1), q.push(d+1);
        for(int i = 2; i <= k; i++) {
            int v = u / i * i;
            if(v >= dest  && !vis[v - dest]) {
                vis[v - dest] = 1;
                q.push(v); q.push(d+1);
            }
        }
    }
}

int32_t main()
{
    #ifndef ONLINE_JUDGE
        freopen("/storage/emulated/0/input.txt", "r", stdin);
    //  freopen("/storage/emulated/output.txt", "w", stdout);
    #endif
    sync;
    clock_t clk = clock();
    cerr << "I will return...\n";

   cin >> a >> b >> k;
   if(k == 2) {
       cout << a - b; return 0;
   }

   L = 1;
   rep(i, 2, k+1) L = lcm(L, i);
   int ans = 0;
   int cnt = a/L - (b-1)/L;
   trace(L, cnt);
   if(cnt) {
       int m = bfs(L, 0);
       ans = m*(cnt-1);
       trace(ans);
   }
   if(!cnt) {
       ans += bfs(a, b);
       trace(ans);
   }
   else {
       ans += bfs(a, a/L*L);
       ans += bfs(iceil(b, L)*L, b);
       trace(ans);
   }
   cout << ans << endl;

   cerr << "...don't you ever hang your head.\n";
    cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / cps << '\n';
}

//Compile using:
//g++ -o filename.exe --std=c++11 filename.cpp
//Use -D CP for defining a macro CP in the local environment


#include <cstdio>
#include <algorithm>
using LL = long long;
const int N = 1e5 + 100, P = 1e9 + 7;
int n, num;
int fac[N], ifac[N];
int ans = 0;
int qpow(int x, int y = P - 2)
{
    int res = 1;
    for (; y; y >>= 1, x = LL(x) * x % P) if (y & 1) res = LL(x) * res % P;
    return res;
}
void adj(int &x){ x += (x >> 31) & P; }
int C(int x, int y){ return LL(fac[x]) * ifac[y] % P * ifac[x - y] % P; }
int main()
{
    scanf("%d %d", &n, &num);
    fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
    for (int i = 2; i <= n; ++i) fac[i] = LL(i) * fac[i - 1] % P;
    ifac[n] = qpow(fac[n]);
    for (int i = n - 1; i > 1; --i) ifac[i] = LL(i + 1) * ifac[i + 1] % P;
    num = std::min(n, num);
    for (int i = 0; i <= num; ++i) adj(ans += C(n, i) - P);
    printf("%d\n", ans);
    return 0;
}


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

#define mod 1000000007
#define ll long long int
#define mk make_pair
#define pb push_back
#define xx first
#define yy second
#define ar array
#define del 1e-7

template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }

double pi=3.141592653589793238;
const int MM = 1000000007;
const int Nmax=5005;
const int M = 998244353;
const int N=500050;

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
     ll n;
    cin>>n;
      ll ans=0;
      ll p[10]={};
      for(int i=1;i<=n;i++){
        p[i%9 + 9*(i%9==0)]++;
      }
      for(int i=1;i<=9;i++){
        for(int j=1;j<=9;j++){
            ll x=p[i],y=p[j],z=p[(i*j)%9 + 9*((i*j)%9==0)];
           // cout<<i<<" "<<j<<" "<<i*j<<" "<<x<<" "<<y<<" "<<z<<" =\n";
            ans+=x*y*z;
        }
      }
      for(int i=1;i<=n;i++){
        ans-=n/i;
      }
      cout<<ans<<"\n";
   }
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 100000000000000007
#define MIN -100000000000000007
#define pb push_back
#define X 100000
using namespace std ;

int main()
{
    ios::sync_with_stdio(false);

    long long x = 0 , y , a , n ,b , c , p ;
    cin >> n >> b >> p;
    y = n * p ;
    for(int i = 0 ;  ;  ++ i )
    {
        if( n == 1)
            break ;
        long long j = 1 ;
        while( j<= n )
            j *= 2 ;

        j /= 2;
        //cout << j << " " ;

        x += (j / 2) * ( b * 2 + 1) ;

        n = n - j + j / 2 ;
    }
    cout << x << " " << y << " " ;
    return 0 ;

}


#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int k;
ll f[6];
int dig[6];
const int maxN = 1e6 + 10;
ll dp[maxN];
ll combine(ll x, ll y) {
    return max(x, y);
}

const ll INF = 1e17;
struct Tqueue{
    vector < ll >  st1, st2;
    vector < ll > data;
    Tqueue() {
        st1 = {-INF};
        st2 = {-INF};
    }
    void add(ll x) {
        data.emplace_back(x);
        st1.emplace_back(combine(x, st1.back()));
    }
    void del() {
        if (st2.size() > 1) {
            st2.pop_back();
        }
        else {
            assert(st1.size() > 1);
            st1.resize(1);

            for (int i = data.size() - 1; i >= 0; i--) {
                st2.emplace_back(combine(st2.back(), data[i]));
            }
            data.clear();
            st2.pop_back();
        }
    }
    ll get() {
        return combine(st1.back(), st2.back());
    }
};
ll ndp[maxN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("input.txt", "r", stdin);
    cin >> k;
    for (int i = 0; i < 6; i++) {
        cin >> f[i];
    }
    int q;
    cin >> q;
    assert(q == 1);
    int n;
    cin >> n;
    int tn = n;
    for (int i = 0; i < 6; i++) {
        dig[i] = (tn % 10);
        tn /= 10;
    }
    for (int j = 0; j <= n; j++) dp[j] = -INF;
    dp[0] = 0;
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j <= n; j++) {
            ndp[j] = -INF;
        }
        vector<Tqueue> r(3);
        for (int j = 0; j <= n; j++) {
            r[j % 3].add(dp[j] - (j / 3) * (ll)f[i]);
            if (j - 9 * (k - 1) - 3 >= 0) r[j % 3].del();
            for (int a = 0; a < 3; a++) {
                if (j % 3 >= a) {
                    ndp[j] = max(ndp[j], r[a].get() + (j / 3) * f[i]);
                }
                else {
                    ndp[j] = max(ndp[j], r[a].get() + (j / 3) * f[i] - f[i]);
                }
            }
        }
        for (int j = 0; j <= n; j++) {
            for (int cnt = 9 * (k - 1) + 1; cnt <= 9 * k; cnt++) {
                if (j >= cnt) {
                    if (cnt % 3 == 0) {
                        ndp[j] = max(ndp[j], dp[j - cnt] + (cnt / 3) * f[i]);
                    }
                    else {
                        //??
                        ndp[j] = max(ndp[j], dp[j - cnt] + (3 * (k - 1)) * f[i]);
                    }
                }
            }
        }
        for (int j = 0; j <= n; j++) {
            dp[j] = -INF;
        }
        for (int j = 0; j <= n; j++) {
            if (j % 10 == dig[i]) {
                dp[j / 10] = ndp[j];
            }
        }
    }
    cout << dp[0] << '\n';
    return 0;
}


#include <iostream>
#include <cmath>

using namespace std;

int main(){
    string a,b;

    cin >> a >> b;

    for(int i = 0; i < a.size(); i++)
    {
    	if(a[i] != b[i])
    		cout << '1';
    	else
    		cout << '0';
    }
	return 0;
}


#include <iostream>
using namespace std;

int main()

{
    long long int n;
    cin >> n;
    if(n % 2 == 0)
      cout << "Mahmoud" << endl;
    else
      cout << "Ehab" << endl;

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
using namespace std;
const int N = int(3e5), mod = int(1e9)  + 7;

int n,k;

ll f[N];
bool u[30];
int a[30];
int ans;

bool check(int n){
	while(n){
		if(n % 10 != 7 && n % 10 != 4) return 0;
		n /= 10;
	}
	return 1;
}

void rec(ll v){
	if(v > mod) return;
	if(v < n) ans++;
	rec(v * 10 + 4);
	rec(v * 10 + 7);
}

int main () {
	cin >> n >> k;
	f[0] = 1;
	for(int i = 1; i <= 20; i++) f[i] = f[i - 1] * i;
	ll res = 1;
	for(int i = 1; i <= n; i++){
		res = res * i;
		if(res > k) break;
	}
	if(res < k){
		cout << -1;
		return 0;
	}
	if(n <= 20){
		for(int i = 1; i <= n; i++){
			int cur = 0;
			for(int j = 1; j <= n; j++) if(!u[j]){
				cur++;
				if(cur * f[n - i] >= k){
					a[i] = j;
					u[j] = 1;
					k -= (cur - 1) * f[n - i];
					break;
				}
			}
		}
		for(int i = 1; i <= n; i++){
			if(check(i) && check(a[i])) ans++;
		}
		cout << ans;
		return 0;
	}
	for(int i = 1; i <= 13; i++){
		int cur = 0;
		for(int j = 1; j <= 13; j++) if(!u[j]){
			cur++;
			if(cur * f[13 - i] >= k){
				a[i] = j;
				u[j] = 1;
				k -= (cur - 1) * f[13 - i];
				break;
			}
		}
	}
	for(int i = 1; i <= 13; i++){
		if(check(n - 13 + i) && check(a[i] + n - 13)) {
			ans++;
		}
	}
	n -= 13;
	rec(4);
	rec(7);
	cout << ans;

return 0;
}


#include <bits/stdc++.h>

constexpr int P = 998244353;

using int64 = long long;

int norm(int x) {
    if (x < 0) x += P;
    if (x >= P) x -= P;
    return x;
}

template <class T>
T power(T a, int b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) res *= a;
    }
    return res;
}

struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    int val() const { return x; }
    Z operator-() const { return Z(norm(P - x)); }
    Z inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    Z &operator*=(const Z &rhs) {
        x = int64(x) * rhs.x % P;
        return *this;
    }
    Z &operator+=(const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z &operator-=(const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
    friend Z operator*(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator+(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator-(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator/(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, Z &a) {
        int64 v;
        is >> v;
        a = Z(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const Z &a) { return os << a.val(); }
};

std::vector<int> rev;
std::vector<Z> roots{0, 1};

void dft(std::vector<Z> &a) {
    int n = a.size();
    if (int(rev.size()) != n) {
        int k = __builtin_ctz(n) - 1;
        rev.resize(n);
        for (int i = 0; i < n; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
    }
    for (int i = 0; i < n; ++i) {
        if (rev[i] < i) std::swap(a[i], a[rev[i]]);
    }
    if (int(roots.size()) < n) {
        int k = __builtin_ctz(roots.size());
        roots.resize(n);
        while ((1 << k) < n) {
            Z e = power(Z(3), (P - 1) >> (k + 1));
            for (int i = 1 << (k - 1); i < (1 << k); ++i) {
                roots[2 * i] = roots[i];
                roots[2 * i + 1] = roots[i] * e;
            }
            k++;
        }
    }
    for (int k = 1; k < n; k *= 2) {
        for (int i = 0; i < n; i += 2 * k) {
            for (int j = 0; j < k; ++j) {
                Z u = a[i + j];
                Z v = a[i + j + k] * roots[k + j];
                a[i + j] = u + v;
                a[i + j + k] = u - v;
            }
        }
    }
}

void idft(std::vector<Z> &a) {
    int n = a.size();
    std::reverse(a.begin() + 1, a.end());
    dft(a);
    Z inv = (1 - P) / n;
    for (int i = 0; i < n; ++i) {
        a[i] *= inv;
    }
}

struct Poly {
    std::vector<Z> a;
    Poly() {}
    Poly(const std::vector<Z> &a) : a(a) {}
    Poly(const std::initializer_list<Z> &a) : a(a) {}
    int size() const { return a.size(); }
    void resize(int n) { a.resize(n); }
    Z operator[](int idx) const {
        if (idx < size()) {
            return a[idx];
        } else {
            return 0;
        }
    }
    Z &operator[](int idx) { return a[idx]; }
    Poly mulxk(int k) const {
        auto b = a;
        b.insert(b.begin(), k, 0);
        return Poly(b);
    }
    Poly modxk(int k) const {
        k = std::min(k, size());
        return Poly(std::vector<Z>(a.begin(), a.begin() + k));
    }
    Poly divxk(int k) const {
        if (size() <= k) {
            return Poly();
        }
        return Poly(std::vector<Z>(a.begin() + k, a.end()));
    }
    friend Poly operator+(const Poly &a, const Poly &b) {
        std::vector<Z> res(std::max(a.size(), b.size()));
        for (int i = 0; i < int(res.size()); ++i) {
            res[i] = a[i] + b[i];
        }
        return Poly(res);
    }
    friend Poly operator-(const Poly &a, const Poly &b) {
        std::vector<Z> res(std::max(a.size(), b.size()));
        for (int i = 0; i < int(res.size()); ++i) {
            res[i] = a[i] - b[i];
        }
        return Poly(res);
    }
    friend Poly operator*(Poly a, Poly b) {
        if (a.size() == 0 || b.size() == 0) {
            return Poly();
        }
        int sz = 1, tot = a.size() + b.size() - 1;
        while (sz < tot) {
            sz *= 2;
        }
        a.a.resize(sz);
        b.a.resize(sz);
        dft(a.a);
        dft(b.a);
        for (int i = 0; i < sz; ++i) {
            a.a[i] = a[i] * b[i];
        }
        idft(a.a);
        a.resize(tot);
        return a;
    }
    friend Poly operator*(Z a, Poly b) {
        for (int i = 0; i < int(b.size()); ++i) {
            b[i] *= a;
        }
        return b;
    }
    friend Poly operator*(Poly a, Z b) {
        for (int i = 0; i < int(a.size()); ++i) {
            a[i] *= b;
        }
        return a;
    }
    Poly &operator+=(Poly b) { return (*this) = (*this) + b; }
    Poly &operator-=(Poly b) { return (*this) = (*this) - b; }
    Poly &operator*=(Poly b) { return (*this) = (*this) * b; }
    Poly deriv() const {
        if (a.empty()) {
            return Poly();
        }
        std::vector<Z> res(size() - 1);
        for (int i = 0; i < size() - 1; ++i) {
            res[i] = (i + 1) * a[i + 1];
        }
        return Poly(res);
    }
    Poly integr() const {
        std::vector<Z> res(size() + 1);
        for (int i = 0; i < size(); ++i) {
            res[i + 1] = a[i] / (i + 1);
        }
        return Poly(res);
    }
    Poly inv(int m) const {
        Poly x{a[0].inv()};
        int k = 1;
        while (k < m) {
            k *= 2;
            x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
        }
        return x.modxk(m);
    }
    Poly log(int m) const { return (deriv() * inv(m)).integr().modxk(m); }
    Poly exp(int m) const {
        Poly x{1};
        int k = 1;
        while (k < m) {
            k *= 2;
            x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
        }
        return x.modxk(m);
    }
    Poly pow(int k, int m) const {
        int i = 0;
        while (i < size() && a[i].val() == 0) {
            i++;
        }
        if (i == size() || 1LL * i * k >= m) {
            return Poly(std::vector<Z>(m));
        }
        Z v = a[i];
        auto f = divxk(i) * v.inv();
        return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
    }
    Poly sqrt(int m) const {
        Poly x{1};
        int k = 1;
        while (k < m) {
            k *= 2;
            x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((P + 1) / 2);
        }
        return x.modxk(m);
    }
    Poly mulT(Poly b) const {
        if (b.size() == 0) {
            return Poly();
        }
        int n = b.size();
        std::reverse(b.a.begin(), b.a.end());
        return ((*this) * b).divxk(n - 1);
    }
    std::vector<Z> eval(std::vector<Z> x) const {
        if (size() == 0) {
            return std::vector<Z>(x.size(), 0);
        }
        const int n = std::max(int(x.size()), size());
        std::vector<Poly> q(4 * n);
        std::vector<Z> ans(x.size());
        x.resize(n);
        std::function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) {
                q[p] = Poly{1, -x[l]};
            } else {
                int m = (l + r) / 2;
                build(2 * p, l, m);
                build(2 * p + 1, m, r);
                q[p] = q[2 * p] * q[2 * p + 1];
            }
        };
        build(1, 0, n);
        std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
            if (r - l == 1) {
                if (l < int(ans.size())) {
                    ans[l] = num[0];
                }
            } else {
                int m = (l + r) / 2;
                work(2 * p, l, m, num.mulT(q[2 * p + 1]).modxk(m - l));
                work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
            }
        };
        work(1, 0, n, mulT(q[1].inv(n)));
        return ans;
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, k, f;
    std::cin >> n >> k >> f;
    Poly a(std::vector<Z>(k + 1, 1));
    for (int i = 1; i <= n; ++i) {
        a = a * a;
        if (i < n) {
            for (int j = a.size() - 2; j >= 0; --j) a[j] += a[j + 1];
            for (int j = 0; j < a.size(); ++j) a[j] *= std::max(0, k - j + 1);
            for (int j = 0; j < a.size() - 1; ++j) a[j] -= a[j + 1];
            a.resize(k + 1);
        }
    }
    std::cout << (f < a.size() ? a[f] : 0) << "\n";
    return 0;
}


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

//#define Fread
#define Getmod

#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#define getchar gc
#endif // Fread

template <typename T>
void r1(T &x) {
	x = 0;
	char c(getchar());
	int f(1);
	for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
	for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
	x *= f;
}

#ifdef Getmod
const int mod  = 998244353;
template <int mod>
struct typemod {
    int z;
    typemod(int a = 0) : z(a) {}
    inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);}
    inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);}
    inline int mul(int a,int b) const {return 1ll * a * b % mod;}
    typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));}
    typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));}
    typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));}
    typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;}
    typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;}
    typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;}
    int operator == (const typemod<mod> &x) const {return x.z == z;}
    int operator != (const typemod<mod> &x) const {return x.z != z;}
};
typedef typemod<mod> Tm;
#endif
const Tm G = 3, invG = 332748118;
template <typename T,typename... Args> inline void r1(T& t, Args&... args) {
    r1(t);  r1(args...);
}

//#define int long long
const int maxn = 4e5 + 5;
const int maxm = maxn << 1;

Tm tmp[maxn], Sl[maxn], Sr[maxn], fac[maxn], inv[maxn], C[maxn];
int rev[maxn];
int lim, len;
void getrev(int x) {
    lim = 1, len = 0;
    while(lim <= x) lim <<= 1, ++ len;
    for(int i = 0; i < lim; ++ i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
}

Tm ksm(Tm x,int mi) {
    if(mi < 0) return 0;
    Tm res(1);
    while(mi) {
        if(mi & 1) res *= x;
        mi >>= 1;
        x *= x;
    }
    return res;
}

void NTT(Tm *A,int opt = 1) {
    for(int i = 0; i < lim; ++ i) if(i < rev[i]) swap(A[i], A[rev[i]]);
    for(int mid = 1; mid < lim; mid <<= 1) {
        Tm wn(ksm((opt == 1) ? G : invG, (mod - 1) / (mid << 1)));
        for(int j = 0, c = (mid << 1); j < lim; j += c) {
            Tm W(1);
            for(int k = 0; k < mid; ++ k, W *= wn) {
                Tm x = A[j + k], y = W * A[j + k + mid];
                A[j + k] = x + y;
                A[j + k + mid] = x - y;
            }
        }
    }
    if(opt != 1) {
        Tm z = ksm(lim, mod - 2);
        for(int i = 0; i < lim; ++ i) A[i] *= z;
    }
}

int n, k;

signed main() {
//    freopen("S.in", "r", stdin);
//    freopen("S.out", "w", stdout);
    int i, j;
    r1(n, k);
    Tm vis[2] = {1, mod - 1};
    fac[0] = 1;
    for(i = 1; i <= k; ++ i) fac[i] = fac[i - 1] * i;
    inv[k] = ksm(fac[k], mod - 2);
    for(i = k - 1; ~ i; -- i) inv[i] = inv[i + 1] * (i + 1);
    for(i = 0; i <= k; ++ i) {
        Sl[i] = inv[i] * vis[i & 1];
        Sr[i] = ksm(i, k) * inv[i];
    }
    getrev(2 * k);
//    printf("lim = %d\n", lim);
    NTT(Sl, 1), NTT(Sr, 1);
    for(i = 0; i < lim; ++ i) Sl[i] *= Sr[i];
    NTT(Sl, -1);
//    printf("s1 = %d\n", Sl[k].z);
    Sl[0] = 0;
    C[0] = 1;
    for(i = 1; i <= k; ++ i) C[i] = C[i - 1] * (n - i + 1) * ksm(i, mod - 2);
//    Tm x = ksm(n + 1, n);
//    printf("s0 = %d\n", Sl[0].z);
    Tm ans(0);
    for(i = 0; i <= k; ++ i) ans += C[i] * Sl[i] * fac[i] * ksm(n + 1, n - i);
    printf("%d\n", ans.z);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

template <typename T>
void debug_out(T t) {
  cerr << t;
}

template <typename A, typename B>
void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}

template <typename T>
void debug_out(vector<T> t) {
  int sz = t.size();
  for(int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if(i != sz - 1) cerr << ", ";
  }
}

template <typename T>
void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for(int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if(i != sz - 1) cerr << endl;
  }
}

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__), cerr << endl;

int n;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  vector<int> a(n);
  range(i, 0, n) cin >> a[i];
  sort(a.begin(), a.end());
  int M = a.back() + 1;
  vector<int> to(M);
  range(i, 1, M) {
    to[i] = to[i - 1];
    while(to[i] < n && a[to[i]] < i) to[i]++;
  }
  long long res = 0;
  auto get = [&](int y, long long c, int ll, int rr) {
    if(c < 0) {
      return -1LL;
    }
    int s = c % y;
    if(s <= rr && s >= ll) {
      return c;
    }
    long long lc = y * (c / y) + ll;
    if(lc > c) lc -= y;
    long long rc = y * (c / y) + rr;
    if(rc > c) rc -= y;
    return max(-1LL, max(lc, rc));
  };
  range(y, 2, M) {
    long long sum = 0;
    vector<int> pt;
    for(int j = 0; j < M; j += y) {
      int l = to[j];
      int r = (j + y < M ? to[j + y] - 1 : n - 1);
      if(r < l) continue;
      sum += 1LL * (r - l + 1) * j / y;
      if(r > l) pt.push_back(a[r - 1]);
      pt.push_back(a[r]);
    }
    int sz = pt.size();
    vector<pair<int, int>> dt(sz);
    husk(i, sz - 1, -1) {
      dt[i].first = pt[i] % y;
      if(i < sz - 1) {
        dt[i] = dt[i + 1];
        if(dt[i].first > (pt[i] % y)) dt[i].second = max(dt[i].second, pt[i] % y);
        else {
          swap(dt[i].first, dt[i].second);
          dt[i].first = pt[i] % y;
        }
      }
    }
    // sum - 2 * (x / y) >= x
    auto calc = [&](long long s) {
      if(s < 0) return -1LL;
      long long lo = 0, hi = sum + 1;
      while(hi - lo > 1) {
        long long mid = (hi + lo) >> 1;
        if(s - 2 * (mid / y) >= mid) lo = mid;
        else hi = mid;
      }
      return lo;
    };
    long long sa = calc(sum);
    long long sb = calc(sum - 1);
    long long sc = calc(sum - 2);
    husk(i, sz - 1, -1) {
      int sf = dt[i].first, ss = dt[i].second;
      long long x = get(y, min(1LL * pt[i], sa), 0, ss);
      if(i == sz - 1) goto A;
      if(x > 1) res = max(res, x * y);
      if(ss < sf && sum > 0) {
        x = get(y, min(1LL * pt[i], sb), ss + 1, sf);
        if(x > 1) res = max(res, x * y);
      }
      if(sf < y - 1 && sum > 0) {
        x = get(y, min(1LL * pt[i], sc), sf + 1, y - 1);
        if(x > 1) res = max(res, x * y);
      }
      A:;
      x = get(y, min(1LL * pt[i] / 2, sa), 0, sf / 2);
      if(x > 1) res = max(res, x * y);
      if((sf + y) / 2 < y && (sf / 2) + 1 <= (sf + y) / 2 && sum > 0) {
        x = get(y, min(1LL * pt[i] / 2, sb), (sf / 2) + 1, (sf + y) / 2);
        if(x > 1) res = max(res, x * y);
        if((sf + y) / 2 < y - 1 && sum > 0) {
          x = get(y, min(1LL * pt[i] / 2, sc), (sf + y) / 2 + 1, y - 1);
          if(x > 1) res = max(res, x * y);
        }
      }
    }
  }
  cout << res;
  return 0;
}


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    long long int ans;
    n--;
    if(n==0)ans=3;
    else{
        if(n%3==0){
            ans=4*(n/3)+4;
        }
        else{
            int k=n/3;
            if(k==0){
                ans=2*(n+1)+1;
            }
            else{
                ans=2*(n-(k-1))+1;
            }
        }
    }
    cout<<ans<<'\n';
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <string.h>

using namespace std;

#define LL long long
#define N 3000101
#define M 3000011
#define MP make_pair
#define Pi acos(-1.0)
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define inf 0x3f3f3f3f
#define ls (root << 1)
#define rs (ls | 1)
#define md ((ll+rr)/2)
#define lson ll, md, ls
#define rson md+1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define sqr(x) ((x)*(x))
#define MP make_pair
#define uLL unsigned long long
LL powmod(LL a,LL b) {LL res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
#define F(x) ((x)/3+((x)%3 == 1 ? 0 : tb))
#define G(x) ((x)<tb ? (x)*3+1 : ((x) - tb)*3+2)

struct node{
    double val;
    int id;
    string s;
}a[13];
bool mark;

bool cmp(node x,node y){
    if(!mark) return fabs(x.val-y.val) <= 1e-6 ? x.id > y.id : x.val < y.val;
    else return fabs(x.val-y.val) <= 1e-6 ? x.id < y.id : x.val < y.val;
}

int main(){
    a[1].s = "x^y^z";
    a[2].s = "x^z^y";
    a[3].s = "(x^y)^z";
    a[4].s = "(x^z)^y";
    a[5].s = "y^x^z";
    a[6].s = "y^z^x";
    a[7].s = "(y^x)^z";
    a[8].s = "(y^z)^x";
    a[9].s = "z^x^y";
    a[10].s = "z^y^x";
    a[11].s = "(z^x)^y";
    a[12].s = "(z^y)^x";
    for(int i=1; i<=12; i++) a[i].id= i,a[i].val = -inf;
    double x,y,z;
    scanf("%lf%lf%lf",&x,&y,&z);
    if(fabs(x - 1.0) <= 1e-6) for(int i=1; i<=4; i++)a[i].val = -inf+1;
    if(fabs(y - 1.0) <= 1e-6) for(int i=5; i<=8; i++)a[i].val = -inf+1;
    if(fabs(z - 1.0) <= 1e-6) for(int i=9; i<=12; i++) a[i].val = -inf+1;
    mark = 0;
    double x1 = x,y1 = y,z1 = z;
    if(x < 1.0 && y < 1.0 && z < 1.0) x1 = 1.0/x,y1 = 1.0/y,z1 = 1.0/z,mark = 1;
    if(x1 > 1.0){
        a[1].val = z*log(y)+log(log(x1));
        a[2].val = y*log(z)+log(log(x1));
        a[4].val = a[3].val = log(log(x1))+log(y)+log(z);
    }
    if(y1 > 1.0){
        a[5].val = z*log(x)+log(log(y1));
        a[6].val = x*log(z)+log(log(y1));
        a[7].val = a[8].val = log(log(y1))+log(x)+log(z);
    }
    if(z1 > 1.0){
        a[9].val = y*log(x)+log(log(z1));
        a[10].val = x*log(y)+log(log(z1));
        a[11].val = a[12].val = log(log(z1))+log(x)+log(y);
    }
    //for(int i=1; i<=12; i++) printf("%.9f\n",a[i].val);
    sort(a+1,a+1+12,cmp);
    if(!mark) cout<<a[12].s<<endl;
    else cout<<a[1].s<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
const ll Maxn=20+5;
ll n,mul[Maxn];
int main()
{   scanf("%lld",&n);
    mul[0]=1;
    for(ll i=1;i<=n;i++)mul[i]=1ll*mul[i-1]*i;
    printf("%lld\n",mul[n]/mul[n/2]/mul[n/2]/2*mul[n/2-1]*mul[n/2-1]);
    return 0;
}


#include <iostream>
#include<vector>
#include<math.h>
using namespace std;

int main() {

        int n, m;
cin >> n >> m;
int result = -1;
if (m % n == 0) {
    result = 0;
    int d = m / n;
    while (d % 2 == 0)
        d /= 2, result++;
    while (d % 3 == 0)
        d /= 3, result++;
    if (d != 1)
        result = -1;
}
cout << result << endl;

	return 0;
}


//be name oo
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <map>
#include <utility>
#include <cstring>
#include <sstream>
#include <complex>
#include <vector>

#define FOR(i, n) for(int i = 0; i < (n); i++)
#define SZ(x) ((int)x.size())
#define PB push_back

using namespace std;
typedef pair<int, int> joft;
typedef complex<double> point;

const int MOD = 1000 * 1000 * 1000 + 9;
const int MAX_N = 1000 * 1000 + 10;

long long dp1[MAX_N];

int main(){
	int n;
	scanf("%d", &n);
	n /= 2;
	if(n == 1){
		printf("%d\n", 10);
		return 0;
	}
	dp1[1] = 5;
	for(int i = 2; i <= n - 2; i++)
		dp1[i] = (3 + 2 * dp1[i - 1]) % MOD;
	long long dp2 = 1;
	FOR(i, n - 2)
		dp2 = (1 + dp1[n - 2 - i] * (3 + dp2)) % MOD;
	dp2 += 5;
	dp2 = 1 + dp2 * dp2;
	dp2 *= 2;
	dp2 %= MOD;
	printf("%d\n", (int)dp2);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,min;
    cin>>t;
    while(t--){
        cin>>n;
        // max=INT_MIN;
        if(n%2==0){
            cout<<n/2<<" "<<n/2;

        }
        else{
            min=1;
            for(int i=1;i*i<=n;i+=2){
                if(n%i==0&&i!=1){
                	min=i;
                	break;
                    // if(i>max)
                    //     max=i;
                }
            }
            if(min==1){
                cout<<min<<" "<<n-min;
            }
            else{
                cout<<n/min<<" "<<n-n/min;
            }
        }
        cout<<endl;
    }

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string>

using namespace std;
int num[100002];
int main(void){
	int a, b, i;
	scanf("%d",&a);
	if(a % 4 == 1){
		printf("0 A");
	}
	else if(a % 4 == 2){
		printf("1 B");
	}
	else if(a % 4 == 3){
		printf("2 A");
	}
	else{
		printf("1 A");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#ifdef prabhav_
	#include "Headers/debug.cpp"
#endif
#define int long long int

int nc2(int n) {
	return n * (n - 1) / 2;
}

int nc3(int n) {
	return n * (n - 1) * (n - 2) / 6;
}

signed main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int n;
	long double p;
	cin >> n >> p;
	int ans;
	for(int studiedGames = 0; studiedGames < n + 1; studiedGames++) {
		long double fav1 = studiedGames * nc2(n - studiedGames);
		fav1 *= (long double)0.5;
		long double fav2 = nc2(studiedGames) * (n - studiedGames);
		long double fav3 = nc3(studiedGames);
		long double favourable = fav1 + fav2 + fav3;
		int total = nc3(n);
		if((long double)favourable/(long double)total >= p) {
			ans = studiedGames;
			break;
		}
	}
	cout << ans << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#pragma comment(linker, "/STACK:16777216")

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

#define all(a) (a).begin(),(a).end()
#define sz(a) (int)(a).size()

using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef vector<double> vd;
typedef vector< vd > vvd;
typedef vector< string > vs;
typedef pair< int, int > pii;
typedef vector< pii > vpii;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    int c, d;
    cin >> c >> d;
    int n, m;
    cin >> n >> m;
    int k;
    cin >> k;

    int res = 0;
    int nm = n * m;
    int dn = d * n;
    nm -= k;
    int res1 = (nm / n) * c + min((nm % n) * d, c);
    int res2 = nm * d;
    if (res1 < 0)
        res1 = 0;
    if (res2 < 0)
        res2 = 0;

    cout << min(res1, res2);

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int a,b,c,e[1000000],i=0,j,d,f=0;
    cin>>a>>b>>c;
    d=a/b;
    e[i]=a%b;
    for(;;)
    {
        a=10*e[i];
        d=a/b;
        i++;
        e[i]=a%b;
        if(d==c)
        {
            cout<<i;
            break;
        }
        else
        {
            for(j=1;j<i;j++)
            {
                if(e[j]==e[i])
                {
                    f=1;
                    break;
                }
            }
        }
        if(f==1)
        {
            cout<<"-1";
            break;
        }
    }
    return 0;
}


#include"iostream"
using namespace std;

int main()
{
	long long n;
	cin >> n;
	if (n == 0)
	{
		cout << "1";
		return 0;
	}
	n = n % 4;
	switch (n)
	{
	case 1: cout << "8"; break;
	case 2: cout << "4"; break;
	case 3: cout << "2"; break;
	case 0: cout << "6"; break;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const int inf=(int)1e9;
int n,cnt;
char g[25][25];
int dp[55][(1<<20)+10],M[50][26];
inline void ckmax(int &a,int b) {
    if(a<b) a=b;
}
inline void ckmin(int &a,int b) {
    if(a>b) a=b;
}
inline int getm(int k,int x,int y) {
    if(k<=n)    return x-1;
    return n-y;
}
inline int gnxtmask(int k,int mask) {
    if(k<n) return mask|(mask<<1);
    return mask|(mask>>1);
}
int gao(int k,int mask) {
    int &res=dp[k][mask];
    if(res!=-inf)    return res;
    if(k+1==n+n) {
        return res=0;
    }
    int len=k<=n?k:(n-(k-n)),sx=k<=n?1:1+k-n,sy=k<=n?k:n;
    if(~k&1)    res=-inf;
    else        res=inf;
    rep(c,26) {
        int m=gnxtmask(k,mask)&M[k+1][c];
        // printf("trans [%d %d] -> c=%d %d\n",k,mask,c,m);
        if(!m)  continue;
        int t=(int)(c==0)-(int)(c==1);
        if(~k&1) ckmax(res,t+gao(k+1,m));
        else     ckmin(res,t+gao(k+1,m));
    }
    return res;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("B.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    // ios::sync_with_stdio(false);     cin.tie(0);
    scanf("%d",&n);
    FOR(i,1,n)    scanf("%s",g[i]+1);
    FOR(k,1,n+n)
    rep(j,26) {
        int len=k<=n?k:(n-(k-n)),sx=k<=n?1:1+k-n,sy=k<=n?k:n;
        for(int i=0;i<len;++i,++sx,--sy){
            if(g[sx][sy]=='a'+j) {
                M[k][j]|=1<<i;
            }
        }
    }
    FOR(i,1,n+n) {
        int len=i<=n?i:(n+n-i);
        rep(j,1<<len)   dp[i][j]=-inf;
    }
    int t=g[1][1]=='a'?1:g[1][1]=='b'?-1:0;
    int r=t+gao(1,1);
    if(r<0)    puts("SECOND");
    if(r>0)    puts("FIRST");
    if(r==0)   puts("DRAW");
    return 0;
}


#include <bits/stdc++.h>
#define dint long long
using namespace std;
int main(){
    dint a,b,f,k;

    scanf("%lld%lld%lld%lld",&a,&b,&f,&k);
    dint i;
    dint sum = b;
    dint dis1,dis2;
    dint num = 0;
    if (k == 1){
        if (a-f > b || f > b){
            cout << -1 << endl;
            return 0;
        }
    }
    else if (k == 2){
        if (2*(a-f) > b || f > b){
            cout << -1 << endl;
            return 0;
        }
    }
    else{
        if (2*(a-f) > b || 2*f > b){
        cout << -1 << endl;
        return 0;
        }
        if (b < f){
            cout << -1 << endl;
            return 0;
        }
    }

    for(i = 1;i <= k;i++){
        if (i % 2 != 0){
           sum-=f;
           if (i != k)
           dis1 = 2*(a-f);
           else
           dis1 = a-f;
           if (dis1 > sum){
                num++;
                sum = b;
            }
            sum-=a-f;
        }
        else{
            sum-= a-f;
            if (i != k)
                dis2 = 2*f;
            else
                dis2 = f;
           if (dis2 > sum){
                num++;
                sum = b;
            }
            sum-=f;
        }
    }
    cout << num << endl;
    }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x,y,d;
void extendedEuclid(ll a, ll b) {
    if(b == 0) {
        x = 1; y = 0; d = a;
        if(a<0) x=-1;
        return;
    }
    extendedEuclid(b, a % b);
    ll x1 = y;
    ll y1 = x-(a/b)*y;
    x = x1;
    y = y1;
    //cout<<x<<" "<<y<<" "<<a*x+b*y<<endl;
}
inline ll fl(ll a,ll b){
    if(a<0&&a%b) return a/b-1;
    return a/b;
}
inline ll cl(ll a,ll b){
    if(a>0&&a%b) return a/b+1;
    return a/b;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    ll a1,a2,b1,b2,l,r;
    cin>>a1>>b1>>a2>>b2>>l>>r;
    ll c=b2-b1;
    if(l>r){
        cout<<"0\n"; return 0;
    }
    extendedEuclid(a1,-a2);
    if(d<0) d=-d;
    if(c%d){
        cout<<"0\n"; return 0;
    }
    ll x0=x*c/d,y0=y*c/d;
    /*cout<<x0<<" "<<y0<<endl;
    cout<<a1*a2/d<<endl;
    cout<<a1*x0+b1<<"-"<<a1*a2/d<<"n\n";*/
    ll p=max(cl(l-b1,a1),0ll),q=fl(r-b1,a1);
    ll l1=cl(p-x0,a2/d),r1=fl(q-x0,a2/d);
    p=max(cl(l-b2,a2),0ll),q=fl(r-b2,a2);
    ll l2=cl(p-y0,a1/d),r2=fl(q-y0,a1/d);
    l=max(l1,l2); r=min(r1,r2);
    if(l>r) cout<<"0\n";
    else cout<<r-l+1<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define N 100005
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define print(x) cout << #x << "=" << x << "\t";

ll n;
ll k;
ll a;
ll b;
ll ma = -1e18;
ll mi = 1e18;

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

    cin >> n >> k >> a >> b;

    for(ll i=0;i<=n;i++) {
        ll l = i * k + b + a;
        if (l <= 0)
            continue;
        ll g = __gcd(n * k, l);
        ll lcm = n * k / g;
        ma = max(ma, lcm);
        mi = min(mi, lcm);
    }

    for(ll i=0;i<=n;i++) {
        ll l = (i + 1) * k - b + a;
        ll g = __gcd(n * k, l);
        ll lcm = n * k / g;
        ma = max(ma, lcm);
        mi = min(mi, lcm);
    }

    for(ll i=0;i<=n;i++) {
        ll l = (i + 1) * k - a + b;
        ll g = __gcd(n * k, l);
        ll lcm = n * k / g;
        ma = max(ma, lcm);
        mi = min(mi, lcm);
    }

    for(ll i=0;i<=n;i++) {
        ll l = (i + 2) * k - a - b;
        ll g = __gcd(n * k, l);
        ll lcm = n * k / g;
        ma = max(ma, lcm);
        mi = min(mi, lcm);
    }

    cout << mi << " " << ma;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int C[25],A[25];
int N;
int dfs(int idx,int rem)
{
	if(idx>N)
	{
		for(int i = 1;i<=N;i++)
		{
			if(C[i])
				return 0;
		}
		return 1;
	}
	if(!A[idx])
		return dfs(idx+1,A[idx+1]-1);
	for(int i = min(A[idx],rem);i>0;i--)
	{
		if(C[i])
		{
			C[i]-=1,A[idx]-=i;
			if(dfs(idx,i))
				return 1;
			A[idx]+=i,C[i]+=1;
		}
	}
	return 0;
}
int main()
{
	cin>>N;
	C[N] = -1;
	for(int i = 1;i<=N;i++)
	{
		cin>>A[i];
		C[A[i]]+=1;
		A[i]-=1;
	}
	puts(dfs(1,A[1] - 1)?"YES":"NO");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

int main (void) {
	long long int n, a, b, c, d, maxa, mina, mins, rez;
	cin >> n >> a >> b >> c >> d;
	long long int x, xx = a + b, y, yy = a + c, h, hh = c + d, z, zz = b + d;
	maxa = max(max(xx, yy), max(hh, zz));
	mina = min(min(xx, yy), min(hh, zz));
	if (maxa - mina >= n) {
		cout << 0;
	} else {
		if (maxa == mina) mins = maxa + 1;
		else mins = 2 * maxa - mina;
		x = mins - xx;
		y = mins - yy;
		h = mins - hh;
		z = mins - zz;
		maxa = max(max(x, y), max(h, z));
		mina = min(min(x, y), min(h, z));
		/*if (maxa <= 0 || mina <= 0) rez = 0;
		else rez = (n - maxa + 1) * n;
		if (rez < 0) rez = 0;*/
		rez = n * (n - (maxa - mina));
		cout << rez;
	}
	//system("pause");
	return 0;
}


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

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}

int main()
{
    ///freopen("debug.txt","w",stdout);
    ///freopen("input.txt","r",stdin);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    int n;
    scn(n);
    int x[n],y[n];

    for(int i=0; i<n; i++) scn(x[i]);

    for(int i=0; i<n; i++) scn(y[i]);

    int tok=0,e=0;

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

        if(x[i]<y[i]){
            tok += y[i]-x[i];
        }
        else e += x[i]-y[i];
    }

    if(tok==0 || tok<=e)cout<<"Yes";
    else cout<<"No";

    return 0;
}


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

void good() {
    cout << "YES";
    exit(0);
}

int main() {
    int N; cin >> N;
    char grid[N][N];
    for (int i = 0; i < N; i++) {
        string row; cin >> row;
        for (int j = 0; j < N; j++) grid[i][j] = row[j];
    }

    string word = "theseus";
    for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (i < N-7) {
				string w = "";
				for (int k = 0; k < 7; k++) w += grid[i+k][j];
				if (w == word) good();
			}
			if (j < N-7) {
				string w = "";
				for (int k = 0; k < 7; k++) w += grid[i][j+k];
				if (w == word) good();
			}
			if (i < N-7 && j < N-7) {
				string w = "";
				for (int k = 0; k < 7; k++) w += grid[i+k][j+k];
				if (w == word) good();
			}
		}
    }
    cout << "NO";
}


#include <bits/stdc++.h>
#define forn(i,s,t) for(register int i=(s);i<=(t);++i)
#define form(i,s,t) for(register int i=(s);i>=(t);--i)
using namespace std;
const int N = 2040, Mod = 998244353;
struct Mint {
	int res;
	Mint() {res = 0;}
	Mint(int a) : res(a) {}
	friend Mint operator + (Mint A, Mint B) {
		return Mint((A.res + B.res >= Mod) ? (A.res + B.res - Mod) : (A.res + B.res));
	}
	friend Mint& operator += (Mint& A, Mint B) {return A = A + B;}
	friend Mint operator - (Mint A, Mint B) {B.res = Mod - B.res; return A + B;}
	friend Mint operator - (Mint A) {return Mint(Mod - A.res);}
	friend Mint operator + (Mint A) {return A;}
	friend Mint& operator -= (Mint& A, Mint B) {return A = A - B;}
	friend Mint operator * (Mint A, Mint B) {return 1ll * A.res * B.res %Mod;}
	friend Mint& operator *= (Mint& A, Mint B) {return A = A * B;}
	friend Mint operator ~ (Mint A) {
		static Mint res; res = Mint(1);
		static int k; k = Mod - 2;
		for(;k;k>>=1, A*=A) (k&1) && (res = res * A, 0);
		return res;
	}
	friend Mint operator / (Mint A, Mint B) {return A * (~B);}
	friend Mint& operator /= (Mint& A, Mint B) {return A *= (~B);}
	friend Mint operator >> (Mint A, Mint B) {return Mint(A.res / B.res);}
	friend Mint& operator >>= (Mint& A, Mint B) {return A = A>>B;}
};
int n, m; Mint C[N << 1][N << 1];
inline Mint F(int n, int m) {return C[n + m][n];}
int main() {
	scanf("%d%d", &n, &m);
	forn(i,0,n + m) {
		C[i][0] = C[i][i] = Mint(1);
		forn(j,1,i - 1) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
	}
	static Mint res, sum;
	forn(h,1,n - 1) {
		sum = Mint(0);
		forn(i,1,m - 1) {
			res += sum * F(i, h - 1) * F(m - i - 1, h);
			sum += F(n - h, i - 1) * F(n - h - 1, m - i);
		}
	}
	forn(h,1,m - 1) {
		sum = Mint(0);
		forn(i,1,n - 1) {
			sum += F(m - h, i - 1) * F(m - h - 1, n - i);
			res += sum * F(i, h - 1) * F(n - i - 1, h);
		}
	}
	res = res * Mint(2);
	printf("%d\n", res.res);
	return 0;
}// OJ:: LG


#include <bits/stdc++.h>

#define rep(i,l,r) for (int i = l; i <= r; i++)
#define per(i,r,l) for (int i = r; i >= l; i--)
#define fi first
#define se second
#define prt std::cout
#define gin std::cin
#define edl std::endl

namespace wxy{

typedef long long ll;

const int N = 64;

int f[N][N][2];

int a[N],b[N],c[N],n,m,k;

ll x,y;

int high(ll x) {
    int res = -1;
    per(i,60,0) {
        if (x >> i & 1) {
            res = i; break;
        }
    }
    return res;
}

void check(int l,int type) {
    if (type == 0) {
        int r = l + n - 1;
        if (r <= k) {
            bool flag = true;
            for (int i = l,j = 1; i <= r; ++ i,++ j) {
                if (c[i] != a[j]) {
                    flag = false; break;
                }
            }
            if (flag) f[l][r][0] = 1;
        }
        r = l + m - 1;
        if (r <= k) {
            bool flag = true;
            for (int i = l,j = 1; i <= r; ++ i,++ j) {
                if (c[i] != b[j]) {
                    flag = false; break;
                }
            }
            if (flag) f[l][r][0] = 1;
        }
    } else {
        int r = l + n - 1;
        if (r <= k) {
            bool flag = true;
            for (int i = r,j = 1; i >= l; -- i,++ j) {
                if (c[i] != a[j]) {
                    flag = false; break;
                }
            }
            if (flag) f[l][r][1] = 1;
        }
        r = l + m - 1;
        if (r <= k) {
            bool flag = true;
            for (int i = r,j = 1; i >= l; -- i,++ j) {
                if (c[i] != b[j]) {
                    flag = false; break;
                }
            }
            if (flag) f[l][r][1] = 1;
        }
    }
}

inline void main(){
    #ifndef ONLINE_JUDGE
        freopen("in.in","r",stdin);
        freopen("out.out","w",stdout);
    #endif
    scanf("%lld%lld",&x,&y);
    int h = high(x); per(i,h,0) a[++ n] = (x >> i & 1);
    int cur = 0; ll v = 0; rep(i,1,n) {v += (1ll*a[i]<<cur); ++ cur;}
    h = high(v); per(i,h,0) b[++ m] = (v >> i & 1);
    h = high(y); per(i,h,0) c[++ k] = (y >> i & 1);
    rep(l,1,k) check(l,0),check(l,1);

    for (int len = 1; len <= k; ++ len) {
        for (int l = 1; l + len - 1 <= k; ++ l) {
            int r = l + len - 1;
            if (c[l] == 1) {
                f[l][r][0] |= f[l+1][r][1];
            }
            if (c[r] == 1) {
                f[l][r][1] |= f[l][r-1][0];
            }
            if (c[l] == 1) f[l][r][0] |= f[l][r][1];
            if (c[r] == 1) f[l][r][1] |= f[l][r][0];
        }
    }
    if (f[1][k][0]) puts("YES");
    else puts("NO");
}

}signed main(){wxy::main(); return 0;}


//
//  main.cpp
//  Petya's Exams
//
//  Created by dalton wang on 2018/6/30.
//  Copyright © 2018年 dalton wang. All rights reserved.
//
#include<iostream>
using namespace std;
struct a
{
    int nember;
    int begin,end,wait;
};
a a[1001];
int s[101];
int main()
{
    int n,m,i,j,k;
    cin>>n>>m;
    for(i=1;i<=m;i++)
    {
        a[i].nember=i;
        cin>>a[i].begin>>a[i].end>>a[i].wait;
        s[a[i].end]=m+1;
    }
    for(i=1;i<=m-1;i++)
    {
        for(j=i+1;j<=m;j++)
        {
            if(a[j].end<a[i].end)
            {
                a[0]=a[i];
                a[i]=a[j];
                a[j]=a[0];
            }
            else
                if(a[j].end==a[i].end)
                {
                    if(a[j].begin<a[i].begin)
                    {
                        a[0]=a[i];
                        a[i]=a[j];
                        a[j]=a[0];
                    }
                    else
                        if(a[j].begin==a[i].begin)
                            if(a[j].wait<a[i].wait)
                            {
                                a[0]=a[i];
                                a[i]=a[j];
                                a[j]=a[0];
                            }
                }
        }
    }

    for(i=1;i<=m;i++)
    {
        for(k=a[i].begin;k<a[i].end;k++)
        {
            if(s[k]==0)
            {
                s[k]=a[i].nember;
                a[i].wait--;
            }
            if(a[i].wait==0) break;
        }
        if(a[i].wait!=0)
        {
            cout<<"-1";
            return 0;
        }
    }
    for(i=1;i<=n;i++) cout<<s[i]<<' ';
    return 0;
}


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

int pressed[4][4];
int toggled[4][4];

int main(){

	for(int i = 1; i <= 3; i++){
		for(int j = 1; j <= 3; j++){
			cin >> pressed[i][j];
			toggled[i][j] = pressed[i][j];
		}
	}

	for(int i = 1; i <= 3; i++){
		for(int j = 1; j <= 3; j++){
			if(pressed[i][j] != 0){
				//Added 2 extra rows and columns in toggled[][] to handle edge lamps
				//rows and columns 0 and 4 are auxilliary
				toggled[i][j - 1] += pressed[i][j];
				toggled[i][j + 1] += pressed[i][j];
				toggled[i - 1][j] += pressed[i][j];
				toggled[i + 1][j] += pressed[i][j];

			}
		}
	}

	for (int i = 1; i <= 3; i++){
		for(int j = 1; j <= 3; j++){
			if(toggled[i][j] % 2) cout << "0";
			else cout << "1";
		}
		cout << '\n';
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
const int ok=500;
int n,k,a[ok];
double sum,cnt;
int av;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	int n1=n;
	cnt=sum*1.0/n;
	av=round(cnt);
	while(av!=k)
	{
		sum+=k;
		n++;
		cnt=sum*1.0/n;
		av=round(cnt);
	}
	printf("%d",n-n1);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL)

int main() {
    FAST_IO;
    string s;
    cin >> s;
    int len = s.length();
    if (len == 1) {
        cout << 0;
    } else {
        int max = 0;
        for(int i=0;i<len;++i) {
            for(int j=i;j<len;++j) {
                for(int k=i;k<=j;++k) {
                    if(s[k] != s[j+i-k])
                        if( j-i + 1 > max)
                            max = j - i + 1;
                }
            }
        }
        cout << max;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

   int h, m;
	scanf ("%d:%d", &h, &m);

	if (h > 12) h -= 12;

	double v1 = h * 30.0 + m / 2.0;
	double v2 = m * 6.0;

	if (h == 12) v1 = m / 2.0;
	if (m == 60) v2 = 0;
	printf ("%lf %lf\n", v1, v2);
}


#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int fact(int a)
{
    if(a==1 || a==0)
        return 1;
    return a*fact(a-1);
}

main()
{
    int A=0,B=0,c=0;
    double p;
    string a,b;
    cin>>a>>b;
    for(int i=0 ; i<a.size() ; i++)
    {
        A += (a[i]=='+')? 1 : -1;
        if(b[i] == '+')
            B++;
        else if(b[i] == '-')
            B--;
        else
            c++;
    }
    int y = c - abs(A-B);
    if(c == 0)
        p = (A==B)? 1 : 0;

    else{
        if(c<abs(A-B) || y%2 == 1)
            p=0;
        else{
            p = fact(c)*(1.0)/ (1<<c) /fact(y/2) /fact(c-y/2);
        }
    }

    cout<<fixed<<setprecision(12)<<p;
}


#include <bits/stdc++.h>
#include <numeric>
#define fast()   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
#define mod 1000000007
#define PI 3.14159265358979323846
#define f first
#define s second
#define un unordered
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(j, n) for (j = 1; j <= n; j++)
#define ll long long int
#define all(v) v.begin(), v.end()
using namespace std;

ll isfind(ll parent[], ll b)
{

    if (parent[b] == b)
    {
        return b;
    }

    parent[b] = isfind(parent, parent[b]);
    return parent[b];
}
void build(ll tree[], ll arr[], ll node, ll start, ll end)
{

    if (start == end)
    {
        tree[node] = arr[start];
        return;
    }
    ll mid = (start + end) / 2;

    build(tree, arr, 2 * node, start, mid);

    build(tree, arr, 2 * node + 1, mid + 1, end);

    tree[node] = tree[2 * node] | tree[2 * node + 1];
}
ll range_in_query(ll tree[], ll node, ll start, ll end, ll l, ll r)
{

    if (start > end || start > r || end < l)
    {

        return 0;
    }
    if (start >= l && end <= r)
    {
        return tree[node];
    }
    ll mid = (start + end) / 2;
    ll range = range_in_query(tree, 2 * node, start, mid, l, r);
    ll range1 = range_in_query(tree, 2 * node + 1, mid + 1, end, l, r);
    return range | range1;
}
ll power(ll x, ll y, ll p)
{

    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;

        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
bool cmp(pair<ll, ll> p, pair<ll, ll> p1)
{

    return p.first < p1.first;
}

ll check(string s, string s1)
{
    ll count = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == s1[i % 3])
        {
            continue;
        }
        count++;
    }
    return count;
}

vector<ll> printDivisors(ll n)
{
    vector<ll> v;
    for (ll i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (n / i == i)
                v.push_back(i);

            else
            {
                v.push_back(i);
                v.push_back(n / i);
            }
        }
    }
    return v;
}
map<ll, ll> SieveOfEratosthenes(ll n)
{
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));

    for (ll p = 2; p * p <= n; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    map<ll, ll> mp;
    for (ll p = 2; p <= n; p++)
        if (prime[p])
            mp[p] = 1;

    return mp;
}
ll check(ll arr[],ll n,ll mid,ll m){
    ll k=0,res=0,z=0;
    for(int i=n-1;i>=0;){
        if(k<mid){
            res+=max((ll)0,arr[i]-z);
            i--;
            k++;
        }
        else{
            z++;
            k=0;
        }

    }
    if(res>=m){
            return 1;
        }
    return 0;
}
class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // Pointer to an array containing adjacency lists
    bool isCyclicUtil(int v, bool visited[], bool *rs);  // used by isCyclic()
public:
    Graph(int V);   // Constructor
    void addEdge(int v, int w);   // to add an edge to graph
    bool isCyclic();    // returns true if there is a cycle in this graph
};

Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}

// This function is a variation of DFSUtil() in https://www.geeksforgeeks.org/archives/18212
bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack)
{
    if(visited[v] == false)
    {
        // Mark the current node as visited and part of recursion stack
        visited[v] = true;
        recStack[v] = true;

        // Recur for all the vertices adjacent to this vertex
        list<int>::iterator i;
        for(i = adj[v].begin(); i != adj[v].end(); ++i)
        {
            if ( !visited[*i] && isCyclicUtil(*i, visited, recStack) )
                return true;
            else if (recStack[*i])
                return true;
        }

    }
    recStack[v] = false;  // remove the vertex from recursion stack
    return false;
}

// Returns true if the graph contains a cycle, else false.
// This function is a variation of DFS() in https://www.geeksforgeeks.org/archives/18212
bool Graph::isCyclic()
{
    // Mark all the vertices as not visited and not part of recursion
    // stack
    bool *visited = new bool[V+1];
    bool *recStack = new bool[V+1];
    for(int i = 1; i <=V; i++)
    {
        visited[i] = false;
        recStack[i] = false;
    }

    // Call the recursive helper function to detect cycle in different
    // DFS trees
    for(int i = 1; i <=V; i++)
        if (isCyclicUtil(i, visited, recStack))
            return true;

    return false;
}
void dfs(vector<ll> edges[],ll n,ll u,string s,ll visit[][26],ll vis[]){
    visit[u][s[u-1]-'a']++;
    vis[u]=1;
    for(auto i : edges[u]){
        for(int j=0;j<26;j++){
            visit[i][j]+=visit[u][j];
        }
        if(vis[i])
            continue;
        dfs(edges,n,i,s,visit,vis);
    }
}
void dfs(ll a,ll x[],ll y[],ll visit[],ll n){
    visit[a]=1;
    for(int i=1;i<=n;i++){
        if(visit[i]){
            continue;
        }else if(x[i]==x[a] || y[i]==y[a]){
            dfs(i,x,y,visit,n);
        }
    }
}
ll knapSack(ll W,ll wt[], ll val[], ll n)
{
   ll i, w;
   ll K[n+1][W+1]={0};

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

       for (w = 0; w <= W; w++)
       {
           if (i==0 || w==0)
               K[i][w] = 0;
           else if (wt[i-1] <= w)
                 K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]);
           else
                 K[i][w] = K[i-1][w];
       }
    }
    return K[n][W];
}

ll find(ll a,ll parent[]){

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

ll solve(ll arr[],ll k,ll m,ll dp[][3501],ll str,ll end){
    if(str==end){
        return arr[str];
    }
    if(m==1){
        return max(arr[str],arr[end]);
    }

    if(k==0){
          ll res2=solve(arr,k,m-1,dp,str+1,end);
          ll res3=solve(arr,k,m-1,dp,str,end-1);
          dp[m][k]=min(res2,res3);
          return dp[m][k];
    }
    if(dp[m][k]!=-1){
        return dp[m][k];
    }
    else{
        ll res=solve(arr,k-1,m-1,dp,str+1,end);
        ll res1=solve(arr,k-1,m-1,dp,str,end-1);
        ll ans=max(res,res1);
        ll res2=solve(arr,k,m-1,dp,str+1,end);
        ll res3=solve(arr,k,m-1,dp,str,end-1);
        ll ans1=min(res2,res3);
        dp[m][k]=max(ans,ans1);
        return dp[m][k];
    }
}

ll dp[3501][3501];
bool check(ll k,ll mid){
    if((mid*(mid+1))/2==k){
        return 1;
    }
    else{
        return 0;
    }
}

char * stdl(char *s ,char c){
    int length=0;
    for(int i=0;s[i]!=0;i++){
        length++;
    }
    char *res=new char[length];
    int k=0,flag=0;
    for(int i=0;i<length;i++){
        if(s[i]==c && !flag){
            flag=1;
            continue;
        }
        else{
            res[k++]=s[i];
        }
    }
    res[k]=0;
    return res;

}
ll one=0;
ll result(ll n,ll l,ll r){
    if(n==1){
        ll ans=0;
        ll new1=0;
        while(new1+ans+1>=l && new1+ans+1<=r){
            one++;
            new1=new1*2+1+ans;
        }
        return 1;
    }
    else{
        if(n%2){
            ll ans=result(n/2,l,r);
            ll new1=ans+1;
            while(new1>=l && new1<=r){
                one++;
                new1=2*new1;

            }
            return 2*ans+1;

        }
        else{
            ll ans=result(n/2,l,r);
            ans=2*ans+1;
            return ans;

        }
    }
}

void solve(){

    ll a,b;
    cin>>a>>b;
    ll c,d;
    cin>>c>>d;
    while(b<1000000 && d<1000000){
        if(b<d){
            b+=a;
        }
        else if(d<b){
            d+=c;
        }
        if(b==d){
            cout<<b<<endl;return;
        }
    }
    cout<<-1<<endl;

/*
    ll n,m;
    cin>>n>>m;
    ll arr[n];
    vector<pair<ll,pair<ll,ll>>> v;
    for(int i=0;i<m;i++){
        ll a,b;
        cin>>a>>b;
        a--;
        b--;

        v.push_back({max(b,a)-min(a,b)+1,{min(a,b),max(a,b)}});
    }
    sort(all(v));
    ll visit[n+1]={0};
    for(int i=0;i<n;i++){
        visit[i]=-1;
    }
    map<ll,pair<ll,ll>> mp;
    ll ans=1e10;
    for(auto i:v){
        ll k=0;
        for(int j=i.second.first;j<=i.second.second;j++){
            if(visit[j]!=-1){
                j=mp[j].first;
                k=mp[j].second;
            }
            else{
              visit[j]=k++;
              mp[j].first=i.second.second;
            }
        }
        for(int j=i.second.first;j<=i.second.second;j++){
            mp[j].second=k;
        }
        ans=min(ans,k);
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++){
        if(visit[i]==-1){
            cout<<0<<" ";
        }
        else
        cout<<visit[i]<<" ";
    }
    cout<<endl;

*/

}
int main()
{

    fast()
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
//cin >> t;
    for (int i = 0; i < t; i++)
        solve();
    return 0;

}


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

int main()
{
    int n;
    cin>>n;
    int arr[n],i,sum=0;
    for(i=0;i<n;i++)
    {cin>>arr[i];sum+=arr[i];}
    float avg=float(sum)/n;
 sort(arr, arr+n);
    if(avg>=4.5)
    cout<<0<<endl;
    else
    {
        int ctr=0;
        while(1)
        {
            for(i=0;i<n;i++)
            {
                if(arr[i]<5)
                {
                sum+=5-arr[i];
                arr[i]=5;
                avg=float(sum)/n;
                    ctr++;
                }
                //cout<<sum<<endl;
                if(avg>=4.5)
                break;
            }
            if(avg>=4.5)
                break;
        }
        cout<<ctr<<endl;
    }
    return 0;
}