#include <bits/stdc++.h>
using namespace std;
const int oo = 2139063143;
const int N = 2510;
const int P = 1000000007;
const double eps = 1e-7;
template <typename T>
inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
  print(x), putchar('\n');
}
template <typename T1, typename T2>
void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2>
void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
int n;
struct UNION {
  int fa[N], siz[N], cnt[N];
  void init() {
    for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1, cnt[i] = 0;
  }
  int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
  void merge(int x, int y) {
    x = getf(x), y = getf(y);
    if (x == y)
      ++cnt[x];
    else
      fa[y] = x, siz[x] += siz[y], cnt[x] += cnt[y] + 1;
  }
} U;
int a[N][N];
struct LY {
  int x, y, w;
} e[N * N / 2];
bool check() {
  for (int i = 1; i <= n; i++)
    if (a[i][i]) return 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (a[i][j] != a[j][i]) return 0;
  U.init();
  int m = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) e[++m] = (LY){i, j, a[i][j]};
  sort(e + 1, e + m + 1, [](LY A, LY B) { return A.w < B.w; });
  int tot = 0;
  for (int i = 1; i <= m; i++) {
    if (i == 1 || e[i].w != e[i - 1].w) {
      if (++tot > n) return 0;
      for (int j = 1; j <= n; j++)
        if (U.fa[j] == j && U.siz[j] * (U.siz[j] - 1) / 2 != U.cnt[j]) return 0;
    }
    U.merge(e[i].x, e[i].y);
  }
  return 1;
}
int main() {
  sc(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) sc(a[i][j]);
  puts(check() ? "MAGIC" : "NOT MAGIC");
  return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

You're given a matrix A of size n × n.

Let's call the matrix with nonnegative elements magic if it is symmetric (so aij = aji), aii = 0 and aij ≤ max(aik, ajk) for all triples i, j, k. Note that i, j, k do not need to be distinct.

Determine if the matrix is magic.

As the input/output can reach very huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

The first line contains integer n (1 ≤ n ≤ 2500) — the size of the matrix A.

Each of the next n lines contains n integers aij (0 ≤ aij < 109) — the elements of the matrix A.

Note that the given matrix not necessarily is symmetric and can be arbitrary.

Output

Print ''MAGIC" (without quotes) if the given matrix A is magic. Otherwise print ''NOT MAGIC".

Examples

Input

3
0 1 2
1 0 2
2 2 0


Output

MAGIC


Input

2
0 1
2 3


Output

NOT MAGIC


Input

4
0 1 2 3
1 0 3 4
2 3 0 5
3 4 5 0


Output

NOT MAGIC

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int oo = 2139063143;
const int N = 2510;
const int P = 1000000007;
const double eps = 1e-7;
template <typename T>
inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
  print(x), putchar('\n');
}
template <typename T1, typename T2>
void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2>
void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
int n;
struct UNION {
  int fa[N], siz[N], cnt[N];
  void init() {
    for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1, cnt[i] = 0;
  }
  int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
  void merge(int x, int y) {
    x = getf(x), y = getf(y);
    if (x == y)
      ++cnt[x];
    else
      fa[y] = x, siz[x] += siz[y], cnt[x] += cnt[y] + 1;
  }
} U;
int a[N][N];
struct LY {
  int x, y, w;
} e[N * N / 2];
bool check() {
  for (int i = 1; i <= n; i++)
    if (a[i][i]) return 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (a[i][j] != a[j][i]) return 0;
  U.init();
  int m = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) e[++m] = (LY){i, j, a[i][j]};
  sort(e + 1, e + m + 1, [](LY A, LY B) { return A.w < B.w; });
  int tot = 0;
  for (int i = 1; i <= m; i++) {
    if (i == 1 || e[i].w != e[i - 1].w) {
      if (++tot > n) return 0;
      for (int j = 1; j <= n; j++)
        if (U.fa[j] == j && U.siz[j] * (U.siz[j] - 1) / 2 != U.cnt[j]) return 0;
    }
    U.merge(e[i].x, e[i].y);
  }
  return 1;
}
int main() {
  sc(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) sc(a[i][j]);
  puts(check() ? "MAGIC" : "NOT MAGIC");
  return 0;
}

```