#include <bits/stdc++.h>

#include "testlib.h"

const int MAX_K = 120;

int64_t calc_ways_one_perm(const std::vector<int>& perm,
                           const std::vector<int>& a) {
  std::vector<int64_t> dp(perm.size() + 1, 0);
  dp[0] = 1;
  for (int player : a) {
    for (size_t i = 0; i < perm.size(); i++) {
      if (perm[i] == player) {
        dp[i + 1] += dp[i];
      }
    }
  }
  return dp.back();
}

int fact(int n) { return n == 0 ? 1 : (n * fact(n - 1)); }
int64_t pow(int64_t val, int pw) {
  return pw == 0 ? 1 : (val * pow(val, pw - 1));
}

std::vector<int64_t> calc_ways(const std::vector<int>& a, int n) {
  std::vector<int> perm(n);
  std::iota(perm.begin(), perm.end(), 0);
  std::vector<int64_t> res;
  do {
    res.push_back(calc_ways_one_perm(perm, a));
  } while (std::next_permutation(perm.begin(), perm.end()));
  assert(res.size() == fact(n));
  assert(std::accumulate(res.begin(), res.end(), 0LL) == pow(a.size() / n, n));
  return res;
}

int main(int argc, char* argv[]) {
  registerTestlibCmd(argc, argv);

  int n = inf.readInt();

  int k = ouf.readInt(1, MAX_K, "k");
  std::vector<int> player(n * k);
  std::set<int> seen;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int value = ouf.readInt(
          1, k * n,
          "a[" + std::to_string(i + 1) + "][" + std::to_string(j + 1) + "]");
      if (!seen.insert(value).second) {
        quitf(_wa, "Value %d used more than once", value);
      }
      player[value - 1] = i;
    }
  }

  std::vector<int64_t> ways = calc_ways(player, n);
  std::sort(ways.begin(), ways.end());
  int64_t w_min = ways[0];
  int64_t w_max = ways.back();

  // we only use doubles for checker output, actual computations are precise
  double relative_diff = (w_max - w_min) / (double)w_max;
  // (w_max - w_min) / w_max <= 0.002
  // (w_max - w_min) * 1000 <= 2 * w_max
  // w_max is at most 120^5. 120^5 * 1000 < 1e14 -- should fit into int64_t.
  if ((w_max - w_min) * 1000 <= 2 * w_max) {
    quitf(_ok, "n = %d, max_relative_diff = %.3f%%", n, relative_diff * 100.0);
  } else {
    quitf(_wa, "n = %d, max_relative_diff = %.3f%%", n, relative_diff * 100.0);
  }
}