// This is free and unencumbered software released into the public domain.

// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.

// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

// For more information, please refer to <http://unlicense.org>

/****************/
/* template.hpp */
/****************/

#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>

using std::cerr;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::swap;

struct BoolName : std::numpunct<char> {
  std::string t, f;
  BoolName(std::string t, std::string f) : t(t), f(f) {}
  std::string do_truename() const { return t; }
  std::string do_falsename() const { return f; }
};

void setBoolName(std::string t, std::string f) {
  cout.imbue(std::locale(cout.getloc(), new BoolName(t, f)));
}

struct Initializer {
  Initializer() {
    cout << std::fixed << std::setprecision(15) << std::boolalpha;
    setBoolName("Yes", "No");
  }
} initializer;

struct Input {
  bool eof;

  Input() : eof(false) {}

  operator char() {
    char v;
    while (!(this->eof = (std::scanf("%c", &v) != 1)) && std::isspace(v)) {
    }
    return v;
  }

  operator int() {
    int v;
    this->eof = (std::scanf("%d", &v) != 1);
    return v;
  }

  operator long() {
    long v;
    this->eof = (std::scanf("%ld", &v) != 1);
    return v;
  }

  operator long long() {
    long long v;
    this->eof = (std::scanf("%lld", &v) != 1);
    return v;
  }

  operator unsigned int() {
    unsigned int v;
    this->eof = (std::scanf("%u", &v) != 1);
    return v;
  }

  operator unsigned long() {
    unsigned long v;
    this->eof = (std::scanf("%lu", &v) != 1);
    return v;
  }

  operator unsigned long long() {
    unsigned long long v;
    this->eof = (std::scanf("%llu", &v) != 1);
    return v;
  }

  operator double() {
    double v;
    this->eof = (std::scanf("%lf", &v) != 1);
    return v;
  }

  operator long double() {
    long double v;
    this->eof = (std::scanf("%Lf", &v) != 1);
    return v;
  }

  void ignore() const { getchar(); }
} in;

template <typename T> T abs(T a) { return a >= 0 ? a : -a; }

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

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

template <typename T, typename S> std::function<S(T)> cast() {
  return [](const T &t) { return static_cast<S>(t); };
}

template <typename T> T copy(const T &a) { return T(a); }

class ZeroPadding {
public:
  ZeroPadding(int n) : n(n) {}

  int n;
};

std::ostream &operator<<(std::ostream &os, const ZeroPadding &z) {
  os << std::setw(z.n) << std::setfill('0');
  return os;
}

template <typename T> constexpr T inf() {
  return std::numeric_limits<T>::max() / 2 - 1;
}

/*********************/
/* bit_operation.hpp */
/*********************/

template <typename T> int least_bit(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) {
    return __builtin_ffs(n) - 1;
  }
  if (sizeof(T) == 8) {
    return __builtin_ffsll(n) - 1;
  }
}

// n must be greater than 0.
template <typename T> int least_bit_fast(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) {
    return __builtin_ctz(n);
  }
  if (sizeof(T) == 8) {
    return __builtin_ctzll(n);
  }
}

template <typename T> int most_bit(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) {
    return n ? 31 - __builtin_clz(n) : -1;
  }
  if (sizeof(T) == 8) {
    return n ? 63 - __builtin_clzll(n) : -1;
  }
}

template <typename T> int count_bit(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) {
    return __builtin_popcount(n);
  }
  if (sizeof(T) == 8) {
    return __builtin_popcountll(n);
  }
}

template <typename T> int bit_parity(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) {
    return __builtin_parity(n);
  }
  if (sizeof(T) == 8) {
    return __builtin_parityll(n);
  }
}
/***************/
/* bitwise.hpp */
/***************/

template <typename T> class Bitwise {
public:
  T operator&(const T &v) const {
    return T(static_cast<const T &>(*this)) &= v;
  }

  T operator|(const T &v) const {
    return T(static_cast<const T &>(*this)) |= v;
  }

  T operator^(const T &v) const {
    return T(static_cast<const T &>(*this)) ^= v;
  }
};
/*************/
/* tuple.hpp */
/*************/

#include <tuple>

template <typename... T> class Tuple : public std::tuple<T...> {
public:
  Tuple(Input &in) : std::tuple<T...>() { (void)in; }
};

template <typename T, typename... S>
class Tuple<T, S...> : public std::tuple<T, S...> {
public:
  Tuple() : std::tuple<T, S...>() {}

  Tuple(T t, S... s) : std::tuple<T, S...>(t, s...) {}

  Tuple(const std::tuple<T, S...> &t) : std::tuple<T, S...>(t) {}

  Tuple(Input &in) {
    auto a = std::tuple<T>(in);
    std::tuple<S...> b = Tuple<S...>(in);
    std::tuple<T, S...> c = std::tuple_cat(a, b);
    *this = c;
  }

  template <int n> auto &get() { return std::get<n>(*this); }

  template <int n> const auto &get() const { return std::get<n>(*this); }
};

template <typename... T> Tuple<T...> makeTuple(const T &... args) {
  return Tuple<T...>(args...);
}

namespace std {
template <typename... T>
class tuple_size<Tuple<T...>>
    : public std::integral_constant<size_t, sizeof...(T)> {};
template <std::size_t I, typename... T> class tuple_element<I, Tuple<T...>> {
public:
  using type = tuple_element_t<I, std::tuple<T...>>;
};
} // namespace std

/*****************/
/* container.hpp */
/*****************/

#include <vector>

template <typename T> class Container : public T {
private:
  using S = typename T::value_type;
  using Itr = typename T::iterator;

public:
  Container() : T() {}

  Container(int n) : T(n) {}

  Container(int n, S s) : T(n, s) {}

  template <typename Itr> Container(Itr first, Itr last) : T(first, last) {}

  Container(const std::initializer_list<S> &v) : T(v) {}

  Container(int n, Input &in) {
    std::vector<S> v(n);
    for (auto &i : v) {
      i = in;
    }
    *this = Container<T>(v.begin(), v.end());
  }

  S max() const { return *std::max_element(this->begin(), this->end()); }

  template <typename Function> auto max(Function func) const {
    std::vector<std::pair<decltype(func(S())), S>> res;
    for (const auto &i : *this) {
      res.emplace_back(func(i), i);
    }
    return std::max_element(res.begin(), res.end())->second;
  }

  S min() const { return *std::min_element(this->begin(), this->end()); }

  Tuple<S, S> minmax() const {
    auto itrs = std::minmax_element(this->begin(), this->end());
    return Tuple<S, S>(*itrs.first, *itrs.second);
  }

  template <typename Function> auto min(Function func) const {
    std::vector<std::pair<decltype(func(S())), S>> res;
    for (const auto &i : *this) {
      res.emplace_back(func(i), i);
    }
    return std::min_element(res.begin(), res.end())->second;
  }

  int argmax() const {
    return std::distance(this->begin(),
                         std::max_element(this->begin(), this->end()));
  }

  int argmin() const {
    return std::distance(this->begin(),
                         std::min_element(this->begin(), this->end()));
  }

  int find(const S &a) const {
    return std::distance(this->begin(),
                         std::find(this->begin(), this->end(), a));
  }

  bool contains(const S &a) const {
    return std::find(this->begin(), this->end(), a) != this->end();
  }

  int size() const { return T::size(); }

  std::pair<Itr, Itr> equal_range(const S &a) {
    return std::equal_range(this->begin(), this->end(), a);
  }

  template <typename Function> bool all_of(Function func) const {
    return std::all_of(this->begin(), this->end(), func);
  }

  template <typename Function> bool any_of(Function func) const {
    return std::any_of(this->begin(), this->end(), func);
  }

  template <typename Function> bool none_of(Function func) const {
    return std::none_of(this->begin(), this->end(), func);
  }

  int count(const S &s) const {
    return std::count(this->begin(), this->end(), s);
  }

  bool is_sorted() const { return std::is_sorted(this->begin(), this->end()); }

  void output(std::string sep = "\n", std::string end = "\n") const {
    bool first = true;
    for (const auto &i : *this) {
      if (!first) {
        cout << sep;
      }
      first = false;
      cout << i;
    }
    cout << end;
  }
};

/***********/
/* map.hpp */
/***********/

#include <map>

template <typename T, typename S> class Map : public Container<std::map<T, S>> {
public:
  Map() : Container<std::map<T, S>>() {}

  bool contains(const T &a) const { return this->count(a) != 0; }

  int count(const T &t) const { return std::map<T, S>::count(t); }
};
/***************/
/* ordered.hpp */
/***************/

template <typename T> class Ordered {
public:
  template <typename V> bool operator==(const V &v) const {
    return !(static_cast<T>(v) < static_cast<const T &>(*this) ||
             static_cast<const T &>(*this) < static_cast<T>(v));
  }

  template <typename V> bool operator!=(const V &v) const {
    return static_cast<T>(v) < static_cast<const T &>(*this) ||
           static_cast<const T &>(*this) < static_cast<T>(v);
  }

  template <typename V> bool operator>(const V &v) const {
    return static_cast<T>(v) < static_cast<const T &>(*this);
  }

  template <typename V> bool operator<=(const V &v) const {
    return !(static_cast<T>(v) < static_cast<const T &>(*this));
  }

  template <typename V> bool operator>=(const V &v) const {
    return !(static_cast<const T &>(*this) < static_cast<T>(v));
  }
};

/**************/
/* vector.hpp */
/**************/

#include <numeric>

template <typename T> class Vector : public Container<std::vector<T>> {
public:
  Vector() = default;

  Vector(const Vector<T> &v) = default;

  Vector(int n) : Container<std::vector<T>>(n) {}

  Vector(int n, T t) : Container<std::vector<T>>(n, t) {}

  template <typename Itr>
  Vector(Itr first, Itr last) : Container<std::vector<T>>(first, last) {}

  Vector(const std::initializer_list<T> &v) : Container<std::vector<T>>(v) {}

  Vector(int n, Input &in) : Container<std::vector<T>>(n, in) {}

  Vector &operator+=(const Vector &v) {
    if (this->size() < v.size()) {
      this->resize(v.size());
    }
    for (int i = 0; i < v.size(); ++i) {
      (*this)[i] += v[i];
    }
    return *this;
  }

  Vector &operator+=(const T &v) {
    for (auto &i : *this) {
      i += v;
    }
    return *this;
  }

  Vector &operator-=(const Vector &v) {
    if (this->size() < v.size()) {
      this->resize(v.size());
    }
    for (int i = 0; i < v.size(); ++i) {
      (*this)[i] -= v[i];
    }
    return *this;
  }

  Vector &operator-=(const T &v) {
    for (auto &i : *this) {
      i -= v;
    }
    return *this;
  }

  Vector &operator*=(const Vector &v) {
    for (int i = 0; i < this->size(); ++i) {
      (*this)[i] *= v[i];
    }
    return *this;
  }

  Vector &operator*=(const T &v) {
    for (auto &i : *this) {
      i *= v;
    }
    return *this;
  }

  Vector &operator/=(const Vector &v) {
    for (int i = 0; i < this->size(); ++i) {
      (*this)[i] /= v[i];
    }
    return *this;
  }

  Vector &operator/=(const T &v) {
    for (auto &i : *this) {
      i /= v;
    }
    return *this;
  }

  Vector &operator%=(const Vector &v) {
    for (int i = 0; i < this->size(); ++i) {
      (*this)[i] %= v[i];
    }
    return *this;
  }

  Vector &operator%=(const T &v) {
    for (auto &i : *this) {
      i %= v;
    }
    return *this;
  }

  Vector operator+(const Vector &v) const { return Vector(*this) += v; }

  Vector operator+(const T &v) const { return Vector(*this) += v; }

  Vector operator-(const Vector &v) const { return Vector(*this) -= v; }

  Vector operator-(const T &v) const { return Vector(*this) -= v; }

  Vector operator*(const Vector &v) const { return Vector(*this) *= v; }

  Vector operator*(const T &v) const { return Vector(*this) *= v; }

  Vector operator/(const Vector &v) const { return Vector(*this) /= v; }

  Vector operator/(const T &v) const { return Vector(*this) /= v; }

  Vector operator%(const Vector &v) const { return Vector(*this) %= v; }

  Vector operator%(const T &v) const { return Vector(*this) %= v; }

  Vector operator-() const { return *this * -1; }

  T inner_product(const Vector<T> &v) const {
    return std::inner_product(this->begin(), this->end(), v.begin(), T(0));
  }

  Vector<T> &partial_sort(int k, bool reverse = false) {
    if (!reverse) {
      std::partial_sort(this->begin(), this->begin() + k, this->end());
    } else {
      std::partial_sort(this->begin(), this->begin() + k, this->end(),
                        std::greater<T>());
    }
    return *this;
  }

  Vector<T> &sort() {
    std::sort(this->begin(), this->end());
    return *this;
  }

  template <typename Function> Vector<T> &sort(Function func) {
    std::sort(this->begin(), this->end(), func);
    return *this;
  }

  Vector<T> &rsort() {
    std::sort(this->rbegin(), this->rend());
    return *this;
  }

  Vector<int> argsort() const {
    Vector<Tuple<T, int>> v;
    for (int i = 0; i < this->size(); ++i) {
      v.emplace_back((*this)[i], i);
    }
    v.sort();
    auto f = [](const Tuple<T, int> &t) { return t.template get<1>(); };
    return v.transform(f);
  }

  Vector<T> &nth_element(int n, bool reverse = false) {
    if (!reverse) {
      std::nth_element(this->begin(), this->begin() + n, this->end());
    } else {
      std::nth_element(this->begin(), this->begin() + n, this->end(),
                       std::greater<T>());
    }
    return *this;
  }

  Vector<T> subvector(int a) const {
    return Vector<T>(this->begin(), this->begin() + a);
  }

  Vector<T> subvector(int a, int b) const {
    return Vector<T>(this->begin() + a, this->begin() + b);
  }

  template <typename Function> auto transform(Function func) const {
    Vector<decltype(func(T()))> res;
    std::transform(this->begin(), this->end(), std::back_inserter(res), func);
    return res;
  }

  Vector<T> partial_sum() const {
    Vector<T> res;
    std::partial_sum(this->begin(), this->end(), std::back_inserter(res));
    return res;
  }

  template <typename Function> Vector<T> partial_sum(Function func) const {
    Vector<T> res;
    std::partial_sum(this->begin(), this->end(), std::back_inserter(res), func);
    return res;
  }

  Vector<T> &reverse() {
    std::reverse(this->begin(), this->end());
    return *this;
  }

  template <typename Function> int count_if(Function func) const {
    return std::count_if(this->begin(), this->end(), func);
  }

  Vector<T> adjacent_difference() const {
    Vector<T> res;
    std::adjacent_difference(this->begin(), this->end(),
                             std::back_inserter(res));
    return res;
  }

  T lower_bound(T t) const {
    return std::lower_bound(this->begin(), this->end(), t) - this->begin();
  }

  T upper_bound(T t) const {
    return std::upper_bound(this->begin(), this->end(), t) - this->begin();
  }

  T accumulate() const {
    return std::accumulate(this->begin(), this->end(), T());
  }

  template <typename S, typename Function>
  S accumulate(S n, Function func) const {
    return std::accumulate(this->begin(), this->end(), n, func);
  }

  template <typename Int> static Vector<T> makeVector(Int n) {
    return Vector<T>(n);
  }

  template <typename Int> static Vector<T> makeVector(Input &in, Int n) {
    return Vector<T>(n, in);
  }

  template <typename Int, typename... Ints>
  static auto makeVector(Input &in, Int n, Ints... ints) {
    Vector<decltype(makeVector(in, ints...))> res;
    for (int i = 0; i < n; ++i) {
      res.emplace_back(makeVector(in, ints...));
    }
    return res;
  }

  template <typename Int, typename... Ints>
  static auto makeVector(Int n, Ints... ints) {
    Vector<decltype(makeVector(ints...))> res;
    for (int i = 0; i < n; ++i) {
      res.emplace_back(makeVector(ints...));
    }
    return res;
  }

  Vector<T> &unique() {
    this->erase(std::unique(this->begin(), this->end()), this->end());
    return *this;
  }

  bool next_permutation() {
    return std::next_permutation(this->begin(), this->end());
  }

  Vector<T> &rotate(int n) {
    std::rotate(this->begin(), this->begin() + n, this->end());
    return *this;
  }

  Map<T, int> countAll() const {
    Map<T, int> res;
    for (const auto &i : *this) {
      ++res[i];
    }
    return res;
  }

  T matmul(const T &a) const {
    return this->transform([&](const T &i) { return i.inner_product(a); });
  }
};

template <typename T> Vector<T> iota(int n, T m = 0) {
  Vector<T> v(n);
  std::iota(v.begin(), v.end(), m);
  return v;
}

template <typename T, typename S> void read(Vector<T> &t, Vector<S> &s) {
  for (int i = 0; i < t.size(); ++i) {
    t[i] = T(in);
    s[i] = S(in);
  }
}

template <typename T, typename S, typename U>
void read(Vector<T> &t, Vector<S> &s, Vector<U> &u) {
  for (int i = 0; i < t.size(); ++i) {
    t[i] = T(in);
    s[i] = S(in);
    u[i] = U(in);
  }
}

template <typename T> Vector<T> operator+(const T &a, const Vector<T> &b) {
  return b + a;
}

template <typename T> Vector<T> operator-(const T &a, const Vector<T> &b) {
  return -b + a;
}

template <typename T> Vector<T> operator*(const T &a, const Vector<T> &b) {
  return b * a;
}

/************************/
/* container/bitset.hpp */
/************************/

class Bitset : public Vector<uint64_t>, public Bitwise<Bitset> {
private:
  int arraySize(int n) const { return (n + 63) / 64; }

public:
  class reference {
  private:
    friend class Bitset;
    Vector<uint64_t>::iterator val;
    int pos;
    reference(std::vector<uint64_t>::iterator val, int pos)
        : val(val), pos(pos) {}

  public:
    reference &operator=(const reference &r) {
      if (static_cast<bool>(r)) {
        *val |= 1ull << pos;
      } else {
        *val &= ~(1ull << pos);
      }
      return *this;
    }

    reference &operator=(const bool b) {
      if (b) {
        *val |= 1ull << pos;
      } else {
        *val &= ~(1ull << pos);
      }
      return *this;
    }

    operator bool() const { return *val >> pos & 1; }
  };

  Bitset() {}

  Bitset(int n) : Vector<uint64_t>(arraySize(n)) {}

  Bitset(const Vector<uint64_t> &v) : Vector<uint64_t>(v) {}

  reference operator[](int n) {
    if (arraySize(n) >= int(Vector<uint64_t>::size())) {
      resize(n);
    }
    return reference(this->Vector<uint64_t>::begin() + n / 64, n % 64);
  }

  bool operator[](int n) const {
    if (arraySize(n) >= int(Vector<uint64_t>::size())) {
      return false;
    }
    return get(n) >> (n % 64) & 1;
  }

  uint64_t &get(int n) { return Vector<uint64_t>::operator[](n / 64); }

  uint64_t get(int n) const { return Vector<uint64_t>::operator[](n / 64); }

  Bitset &operator&=(const Bitset &b) {
    if (size() < b.size()) {
      resize(b.size());
    }
    for (int i = 0; i < size() && i < b.size(); i += 64) {
      get(i) &= b.get(i);
    }
    return *this;
  }

  Bitset &operator|=(const Bitset &b) {
    if (size() < b.size()) {
      resize(b.size());
    }
    for (int i = 0; i < size() && i < b.size(); i += 64) {
      get(i) |= b.get(i);
    }
    return *this;
  }

  Bitset &operator^=(const Bitset &b) {
    if (size() < b.size()) {
      resize(b.size());
    }
    for (int i = 0; i < size() && i < b.size(); i += 64) {
      get(i) ^= b.get(i);
    }
    return *this;
  }

  Bitset &operator<<=(int n) {
    int sz = Vector<uint64_t>::size(), q = n / 64, r = n % 64;
    Vector<uint64_t> v(sz + n / 64 + 1);
    for (int i = 0; i < size(); i += 64) {
      v[q + i / 64] |= get(i) << r;
      v[q + i / 64 + 1] |= get(i) >> r;
    }
    if (v.back() == 0) {
      v.pop_back();
    }
    return *this = Bitset(v);
  }

  Bitset operator<<(int n) const { return copy(*this) <<= n; }

  int count() {
    return this->accumulate(0,
                            [](int a, uint64_t b) { return a + count_bit(b); });
  }

  bool parity() {
    return this->accumulate(
        0, [](int a, uint64_t b) { return a ^ __builtin_parityll(b); });
  }

  int size() const { return Vector<uint64_t>::size() * 64; }

  void resize(int n) { Vector<uint64_t>::resize(arraySize(n) + 1); }

  class Iterator {
  private:
    int itr;
    const Bitset &bitset;

  public:
    Iterator(int itr, const Bitset &bitset) : itr(itr), bitset(bitset) {}

    int operator*() { return itr; }

    bool operator!=(const Iterator &itr) const { return this->itr != itr.itr; }

    void operator++() {
      if (bitset.get(itr) >> (itr % 64 + 1)) {
        itr += least_bit_fast(bitset.get(itr) >> (itr % 64 + 1)) + 1;
      } else {
        for (int i = itr / 64 * 64 + 64; i < bitset.size(); i += 64) {
          if (bitset.get(i)) {
            itr = i + least_bit_fast(bitset.get(i));
            return;
          }
        }
        itr = bitset.size();
      }
    }
  };

  Iterator begin() {
    for (int i = 0; i < size(); i += 64) {
      if (get(i)) {
        return Iterator(i + least_bit_fast(get(i)), *this);
      }
    }
    return Iterator(size(), *this);
  }

  Iterator end() { return Iterator(size(), *this); }
};

Bitset make_bitset(uint64_t n) { return Vector<uint64_t>(1, n); }
/****************************/
/* container/union_find.hpp */
/****************************/

class UnionFind {
private:
  int n;
  Vector<int> a;

public:
  UnionFind(int n) : n(n), a(n, -1) {}

  int find(int x) { return a[x] < 0 ? x : (a[x] = find(a[x])); }

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

  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) {
      return;
    }
    if (a[x] > a[y]) {
      swap(x, y);
    }
    a[x] += a[y];
    a[y] = x;
    --n;
  }

  int size() { return n; }

  int size(int x) { return -a[find(x)]; }

  bool isRoot(int x) { return find(x) == x; }

  Vector<Vector<int>> groups() {
    Vector<Vector<int>> r(a.size()), res;
    for (int i = 0; i < a.size(); ++i) {
      r[find(i)].emplace_back(i);
    }
    for (auto &&i : r) {
      if (!i.empty()) {
        res.emplace_back(move(i));
      }
    }
    return res;
  }
};
/*******************/
/* graph/graph.hpp */
/*******************/

struct Edge {
  using CostType = int;
  const static int cost = 1;
  int to;
  Edge(int to = -1) : to(to) {}

  Edge(Input &in) : to(in) {}

  bool isNone() const { return to == -1; }

  operator int() const { return to; }
};

std::ostream &operator<<(std::ostream &s, const Edge &edge) {
  s << edge.to;
  return s;
}

template <typename Cost> struct WeightedEdge : public Edge {
  using CostType = Cost;

  Cost cost;

  WeightedEdge(int to = -1) : Edge(to), cost() {}

  template <typename... Args>
  WeightedEdge(int to, Args... args) : Edge(to), cost(args...) {}

  WeightedEdge(Input &in) : Edge(in), cost(in) {}
};

template <typename Cost>
std::ostream &operator<<(std::ostream &s, const WeightedEdge<Cost> &edge) {
  s << edge.to << ',' << edge.cost;
  return s;
}

template <typename Capacity> struct ResidualEdge : public Edge {
  using CapacityType = Capacity;
  Capacity cap;
  int rev;
  ResidualEdge(int to = -1, Capacity cap = 0) : Edge(to), cap(cap) {}

  ResidualEdge(Input &in) {
    Edge edge(in);
    Capacity cap(in);
    *this = ResidualEdge(edge, cap);
  }

  ResidualEdge reverse(int from) const { return ResidualEdge(from, 0); }
};

template <typename Capacity, typename Cost>
struct WeightedResidualEdge : public ResidualEdge<Capacity> {
  Cost cost;
  WeightedResidualEdge(int to = -1, Capacity cap = 0, Cost cost = 0)
      : ResidualEdge<Capacity>(to, cap), cost(cost) {}

  WeightedResidualEdge(Input &in) {
    ResidualEdge<Capacity> edge(in);
    Cost cost(in);
    *this = WeightedResidualEdge(edge, cost);
  }

  WeightedResidualEdge reverse(int from) const {
    return WeightedResidualEdge(from, 0, -cost);
  }
};

template <typename Edge> struct FullEdge : public Edge {
  int from;

  FullEdge() = default;

  FullEdge(const int from, const Edge &edge) : Edge(edge), from(from) {}

  FullEdge(Input &in) {
    int from(in);
    Edge edge(in);
    *this = FullEdge(from, edge);
  }
};

template <typename Edge>
std::ostream &operator<<(std::ostream &s, const FullEdge<Edge> &edge) {
  s << '(' << edge.from << ',' << Edge(edge) << ')';
  return s;
}

template <typename Edge> class Graph {
public:
  using EdgeType = Edge;

  virtual int size() const = 0;
  template <typename... Args> void addEdge(int from, int to, Args...) {
    (void)from;
    (void)to;
  }
  template <typename... Args>
  void addUndirectedEdge(int from, int to, Args...) {
    (void)from;
    (void)to;
  }

  Vector<FullEdge<Edge>> getAllEdges() const {
    Vector<FullEdge<Edge>> res;
    for (int i = 0; i < size(); ++i) {
      for (const auto &edge : getEdges(i)) {
        res.emplace_back(i, edge);
      }
    }
    return res;
  }

  virtual Vector<Edge> getEdges(int from) const = 0;

  virtual Edge getEdge(int from, int to) const = 0;

  virtual bool hasEdge(int from, int to) const = 0;

  int getDegree(int v) const { return getEdges(v).size(); }

  Vector<int> getIndegree() const {
    Vector<int> degree(size());
    for (const auto &edge : getAllEdges()) {
      ++degree[edge.to];
    }
    return degree;
  }
};

template <typename Graph>
Graph readGraph(Input &in, int n, int m, bool undirected, bool one_origin) {
  Graph graph(n);
  for (int i = 0; i < m; ++i) {
    FullEdge<typename Graph::EdgeType> edge(in);
    if (one_origin) {
      --edge.from;
      --edge.to;
    }
    if (undirected) {
      graph.addUndirectedEdge(edge);
    } else {
      graph.addEdge(edge);
    }
  }
  return graph;
}

/****************************/
/* graph/adjacency_list.hpp */
/****************************/

template <typename Edge> class AdjacencyList : public Graph<Edge> {
protected:
  Vector<Vector<Edge>> graph;

public:
  using EdgeType = Edge;

  AdjacencyList() = default;

  AdjacencyList(int n) : graph(n) {}

  AdjacencyList(Input &in, bool undirected = true, bool one_origin = true) {
    int n(in), m(in);
    *this = readGraph<AdjacencyList<Edge>>(in, n, m, undirected, one_origin);
  }

  AdjacencyList(Input &in, int n, int m, bool undirected = true,
                bool one_origin = true) {
    *this = readGraph<AdjacencyList<Edge>>(in, n, m, undirected, one_origin);
  }

  int size() const { return graph.size(); }

  template <typename... Args> void addEdge(int from, int to, Args... args) {
    graph[from].emplace_back(to, args...);
  }

  void addEdge(const FullEdge<Edge> &edge) {
    graph[edge.from].emplace_back(edge);
  }

  template <typename... Args>
  void addUndirectedEdge(int from, int to, Args... args) {
    addEdge(from, to, args...);
    addEdge(to, from, args...);
  }

  void addUndirectedEdge(FullEdge<Edge> edge) {
    graph[edge.from].emplace_back(edge);
    swap(edge.from, edge.to);
    graph[edge.from].emplace_back(edge);
  }

  Vector<Edge> getEdges(int from) const { return graph[from]; }

  Edge getEdge(int from, int to) const {
    for (const auto &edge : graph[from]) {
      if (edge.to == to) {
        return edge;
      }
    }
    return Edge();
  }

  bool hasEdge(int from, int to) const {
    for (const auto &edge : graph[from]) {
      if (edge.to == to) {
        return true;
      }
    }
    return false;
  }

  Vector<Edge> &operator[](int v) { return graph[v]; }

  const Vector<Edge> &operator[](int v) const { return graph[v]; }
};
/*************/
/* queue.hpp */
/*************/

#include <queue>

template <typename T> class Queue : public std::queue<T> {
public:
  Queue() : std::queue<T>() {}

  Queue(const std::initializer_list<T> &q) : std::queue<T>(q) {}

  T front() {
    T res = std::queue<T>::front();
    std::queue<T>::pop();
    return res;
  }

  T peek() const { return std::queue<T>::top(); }

  void pop() const { assert(false); }
};

/***********************/
/* graph/bipartite.hpp */
/***********************/

template <typename Graph> Vector<bool> bipartiteColor(const Graph &graph) {
  Vector<bool> visited(graph.size()), color(graph.size());
  Queue<int> queue;
  for (int v = 0; v < graph.size(); ++v) {
    if (visited[v]) {
      continue;
    }
    queue.emplace(v);
    visited[v] = true;
    while (!queue.empty()) {
      int u = queue.front();
      for (const auto &edge : graph[u]) {
        if (visited[edge.to]) {
          if (color[u] == color[edge.to]) {
            return Vector<bool>();
          }
        } else {
          queue.emplace(edge.to);
          visited[edge.to] = true;
          color[edge.to] = !color[u];
        }
      }
    }
  }
  return color;
}

template <typename Graph> bool isBipartite(const Graph &graph) {
  return !bipartiteGraph(graph).empty();
}
/*********************/
/* unordered_set.hpp */
/*********************/

#include <unordered_set>

template <typename T>
class UnorderedSet : public Container<std::unordered_set<T>> {
public:
  UnorderedSet() : Container<std::unordered_set<T>>() {}

  template <typename Itr>
  UnorderedSet(Itr first, Itr last)
      : Container<std::unordered_set<T>>(first, last) {}

  UnorderedSet(const std::initializer_list<T> &s)
      : Container<std::unordered_set<T>>(s) {}

  template <typename S> UnorderedSet<T> &operator&=(const S &a) {
    UnorderedSet<T> r;
    if (this->size() < a.size()) {
      for (const auto &i : *this) {
        if (a.in(i)) {
          r.emplace(i);
        }
      }
    } else {
      for (const auto &i : a) {
        if (this->in(i)) {
          r.emplace(i);
        }
      }
    }
    *this = r;
    return *this;
  }

  int count(const T &a) const { return std::unordered_set<T>::count(a); }

  int contains(const T &a) const { return count(a) > 0; }
};

/************************/
/* graph/complement.hpp */
/************************/

template <typename Graph> Graph complement(const Graph &graph) {
  Graph res(graph.size());
  for (int v = 0; v < graph.size(); ++v) {
    UnorderedSet<int> to{v, graph.size()};
    for (const auto &edge : graph[v]) {
      to.emplace(edge.to);
    }
    for (int u = 0; u < graph.size(); ++u) {
      if (!to.contains(u)) {
        res.addEdge(v, u);
      }
    }
  }
  return res;
}

/************/
/* main.cpp */
/************/

int main() {
  int n(in), m(in);
  AdjacencyList<Edge> graph(in, n, m);
  graph = complement(graph);
  auto color = bipartiteColor(graph);
  if (color.empty()) {
    cout << -1 << endl;
    return 0;
  }
  UnionFind uf(n);
  for (int v = 0; v < n; ++v) {
    for (const auto &edge : graph[v]) {
      uf.unite(v, edge.to);
    }
  }
  Vector<bool> dp(n + 1);
  dp[0] = 1;
  for (const auto &group : uf.groups()) {
    int a = 0, b = 0;
    for (int v : group) {
      if (color[v]) {
        ++a;
      } else {
        ++b;
      }
    }
    Vector<bool> next(n + 1);
    for (int i = 0; i <= n; ++i) {
      if (dp[i]) {
        next[i + a] = true;
        next[i + b] = true;
      }
    }
    dp = next;
  }
  int res = inf<int>();
  for (int i = 0; i <= n; ++i) {
    if (dp[i]) {
      chmin(res, i * (i - 1) / 2 + (n - i) * (n - i - 1) / 2);
    }
  }
  cout << res << endl;
}


Measured memory_bytes: 5248