#include <bits/stdc++.h>
using namespace std;
const int maxn = 2505, logn = 13;
int N, arr[maxn][maxn], par[maxn], st[maxn][logn], w[maxn][logn], dep[maxn];
vector<pair<int, int>> g[maxn];
vector<pair<int, pair<int, int>>> edges;
inline int ro(int u) {
  while (u != par[u]) {
    par[u] = par[par[u]];
    u = par[u];
  }
  return u;
}
void dfs(int u, int p) {
  for (pair<int, int> v : g[u]) {
    if (v.first == p) continue;
    dep[v.first] = dep[u] + 1;
    st[v.first][0] = u, w[v.first][0] = v.second;
    dfs(v.first, u);
  }
}
int lca(int a, int b) {
  if (dep[a] > dep[b]) swap(a, b);
  int diff = dep[b] - dep[a];
  for (int i = 0; i < logn; i++)
    if (diff & (1 << i)) b = st[b][i];
  if (a == b) return a;
  for (int i = logn - 1; i >= 0; i--)
    if (st[a][i] != st[b][i]) a = st[a][i], b = st[b][i];
  return st[a][0];
}
int query(int a, int b) {
  int res = 0, l = lca(a, b);
  if (dep[a] > dep[b]) swap(a, b);
  if (a == l) {
    int diff = dep[b] - dep[a] - 1;
    for (int i = logn - 1; i >= 0; i--)
      if (diff & (1 << i)) res = max(res, w[b][i]), b = st[b][i];
    res = max(res, w[b][0]);
  } else {
    int diff = dep[b] - dep[a];
    for (int i = logn - 1; i >= 0; i--)
      if (diff & (1 << i)) res = max(res, w[b][i]), b = st[b][i];
    for (int i = logn - 1; i >= 0; i--)
      if (st[a][i] != st[b][i])
        res = max({res, w[a][i], w[b][i]}), a = st[a][i], b = st[b][i];
    res = max({res, w[a][0], w[b][0]});
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    par[i] = i;
    for (int k = 1; k <= N; k++) {
      cin >> arr[i][k];
      edges.push_back(make_pair(arr[i][k], make_pair(i, k)));
    }
    if (arr[i][i]) {
      cout << "NOT MAGIC" << '\n';
      return 0;
    }
  }
  sort(edges.begin(), edges.end());
  for (pair<int, pair<int, int>> ck : edges) {
    int a = ro(ck.second.first), b = ro(ck.second.second);
    if (a == b) continue;
    g[ck.second.first].push_back(make_pair(ck.second.second, ck.first));
    g[ck.second.second].push_back(make_pair(ck.second.first, ck.first));
    par[a] = b;
  }
  memset(w, 0x3f, sizeof w);
  dfs(1, -1);
  for (int sz = 1; (1 << sz) <= N; sz++) {
    for (int i = 1; i <= N; i++) {
      st[i][sz] = st[st[i][sz - 1]][sz - 1];
      w[i][sz] = min(w[st[i][sz - 1]][sz - 1], w[i][sz - 1]);
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int k = 1; k <= N; k++) {
      if (i != k && query(i, k) != arr[i][k]) {
        cout << "NOT MAGIC" << '\n';
        return 0;
      }
    }
  }
  cout << "MAGIC" << '\n';
}

### Prompt

Your challenge is to write a cpp solution to 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;
const int maxn = 2505, logn = 13;
int N, arr[maxn][maxn], par[maxn], st[maxn][logn], w[maxn][logn], dep[maxn];
vector<pair<int, int>> g[maxn];
vector<pair<int, pair<int, int>>> edges;
inline int ro(int u) {
  while (u != par[u]) {
    par[u] = par[par[u]];
    u = par[u];
  }
  return u;
}
void dfs(int u, int p) {
  for (pair<int, int> v : g[u]) {
    if (v.first == p) continue;
    dep[v.first] = dep[u] + 1;
    st[v.first][0] = u, w[v.first][0] = v.second;
    dfs(v.first, u);
  }
}
int lca(int a, int b) {
  if (dep[a] > dep[b]) swap(a, b);
  int diff = dep[b] - dep[a];
  for (int i = 0; i < logn; i++)
    if (diff & (1 << i)) b = st[b][i];
  if (a == b) return a;
  for (int i = logn - 1; i >= 0; i--)
    if (st[a][i] != st[b][i]) a = st[a][i], b = st[b][i];
  return st[a][0];
}
int query(int a, int b) {
  int res = 0, l = lca(a, b);
  if (dep[a] > dep[b]) swap(a, b);
  if (a == l) {
    int diff = dep[b] - dep[a] - 1;
    for (int i = logn - 1; i >= 0; i--)
      if (diff & (1 << i)) res = max(res, w[b][i]), b = st[b][i];
    res = max(res, w[b][0]);
  } else {
    int diff = dep[b] - dep[a];
    for (int i = logn - 1; i >= 0; i--)
      if (diff & (1 << i)) res = max(res, w[b][i]), b = st[b][i];
    for (int i = logn - 1; i >= 0; i--)
      if (st[a][i] != st[b][i])
        res = max({res, w[a][i], w[b][i]}), a = st[a][i], b = st[b][i];
    res = max({res, w[a][0], w[b][0]});
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    par[i] = i;
    for (int k = 1; k <= N; k++) {
      cin >> arr[i][k];
      edges.push_back(make_pair(arr[i][k], make_pair(i, k)));
    }
    if (arr[i][i]) {
      cout << "NOT MAGIC" << '\n';
      return 0;
    }
  }
  sort(edges.begin(), edges.end());
  for (pair<int, pair<int, int>> ck : edges) {
    int a = ro(ck.second.first), b = ro(ck.second.second);
    if (a == b) continue;
    g[ck.second.first].push_back(make_pair(ck.second.second, ck.first));
    g[ck.second.second].push_back(make_pair(ck.second.first, ck.first));
    par[a] = b;
  }
  memset(w, 0x3f, sizeof w);
  dfs(1, -1);
  for (int sz = 1; (1 << sz) <= N; sz++) {
    for (int i = 1; i <= N; i++) {
      st[i][sz] = st[st[i][sz - 1]][sz - 1];
      w[i][sz] = min(w[st[i][sz - 1]][sz - 1], w[i][sz - 1]);
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int k = 1; k <= N; k++) {
      if (i != k && query(i, k) != arr[i][k]) {
        cout << "NOT MAGIC" << '\n';
        return 0;
      }
    }
  }
  cout << "MAGIC" << '\n';
}

```