#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 2500 + 5, M = 15;
int n, a[N][N];
vector<pair<int, pair<int, int>>> edgs;
int par[2 * N], sz[2 * N];
int anc[M][2 * N], mx[M][2 * N], h[2 * N], rt = 1;
vector<pair<int, int>> adj[2 * N];
void dfs(int x = rt, int p = -1, int ht = 0) {
  anc[0][x] = p, h[x] = ht;
  for (auto v : adj[x])
    if (v.first != p) mx[0][v.first] = v.second, dfs(v.first, x, ht + 1);
}
void build() {
  dfs(), anc[0][rt] = rt;
  for (int j = 1; j < M; j++)
    for (int i = 1; i < 2 * N; i++) {
      anc[j][i] = anc[j - 1][anc[j - 1][i]];
      mx[j][i] = max(mx[j - 1][i], mx[j - 1][anc[j - 1][i]]);
    }
}
int lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  int mx1 = 0, mx2 = 0;
  for (int i = M - 1; i >= 0; i--)
    if (h[u] - (1 << i) >= h[v]) {
      mx1 = max(mx1, mx[i][u]);
      u = anc[i][u];
    }
  if (u == v) return max(mx1, mx2);
  for (int i = M - 1; i >= 0; i--)
    if (anc[i][u] != anc[i][v]) {
      mx1 = max(mx1, mx[i][u]);
      mx2 = max(mx2, mx[i][v]);
      u = anc[i][u], v = anc[i][v];
    }
  mx1 = max(mx1, mx[0][u]);
  mx2 = max(mx2, mx[0][v]);
  return max(mx1, mx2);
}
int find(int x) { return x == par[x] ? x : find(par[x]); }
void merge(int a, int b, int c) {
  int aa = a, bb = b;
  a = find(a), b = find(b);
  if (a == b) return;
  adj[aa].push_back({bb, c});
  adj[bb].push_back({aa, c});
  if (sz[a] < sz[b]) swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n + N; i++) par[i] = i, sz[i] = 1;
  int zeroes = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (i == j and a[i][j] != 0) zeroes = 0;
      edgs.push_back({a[i][j], {i, j + N}});
      edgs.push_back({a[i][j], {j + N, i}});
    }
  }
  sort(edgs.begin(), edgs.end());
  for (auto x : edgs) merge(x.second.first, x.second.second, x.first);
  build();
  int max_ok = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (lca(i, j + N) != a[i][j]) max_ok = 0;
    }
  int sim = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (a[i][j] != a[j][i]) sim = 0;
  int cond = sim & zeroes & max_ok;
  cout << (cond ? "MAGIC\n" : "NOT MAGIC\n");
}

### Prompt

Create a solution in Cpp for the following problem:

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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 2500 + 5, M = 15;
int n, a[N][N];
vector<pair<int, pair<int, int>>> edgs;
int par[2 * N], sz[2 * N];
int anc[M][2 * N], mx[M][2 * N], h[2 * N], rt = 1;
vector<pair<int, int>> adj[2 * N];
void dfs(int x = rt, int p = -1, int ht = 0) {
  anc[0][x] = p, h[x] = ht;
  for (auto v : adj[x])
    if (v.first != p) mx[0][v.first] = v.second, dfs(v.first, x, ht + 1);
}
void build() {
  dfs(), anc[0][rt] = rt;
  for (int j = 1; j < M; j++)
    for (int i = 1; i < 2 * N; i++) {
      anc[j][i] = anc[j - 1][anc[j - 1][i]];
      mx[j][i] = max(mx[j - 1][i], mx[j - 1][anc[j - 1][i]]);
    }
}
int lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  int mx1 = 0, mx2 = 0;
  for (int i = M - 1; i >= 0; i--)
    if (h[u] - (1 << i) >= h[v]) {
      mx1 = max(mx1, mx[i][u]);
      u = anc[i][u];
    }
  if (u == v) return max(mx1, mx2);
  for (int i = M - 1; i >= 0; i--)
    if (anc[i][u] != anc[i][v]) {
      mx1 = max(mx1, mx[i][u]);
      mx2 = max(mx2, mx[i][v]);
      u = anc[i][u], v = anc[i][v];
    }
  mx1 = max(mx1, mx[0][u]);
  mx2 = max(mx2, mx[0][v]);
  return max(mx1, mx2);
}
int find(int x) { return x == par[x] ? x : find(par[x]); }
void merge(int a, int b, int c) {
  int aa = a, bb = b;
  a = find(a), b = find(b);
  if (a == b) return;
  adj[aa].push_back({bb, c});
  adj[bb].push_back({aa, c});
  if (sz[a] < sz[b]) swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n + N; i++) par[i] = i, sz[i] = 1;
  int zeroes = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (i == j and a[i][j] != 0) zeroes = 0;
      edgs.push_back({a[i][j], {i, j + N}});
      edgs.push_back({a[i][j], {j + N, i}});
    }
  }
  sort(edgs.begin(), edgs.end());
  for (auto x : edgs) merge(x.second.first, x.second.second, x.first);
  build();
  int max_ok = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (lca(i, j + N) != a[i][j]) max_ok = 0;
    }
  int sim = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (a[i][j] != a[j][i]) sim = 0;
  int cond = sim & zeroes & max_ok;
  cout << (cond ? "MAGIC\n" : "NOT MAGIC\n");
}

```