#include <bits/stdc++.h>
using namespace std;
const int M = 2505;
int n, A[M][M], len;
int par[M], asdf, head[M];
int mx[M][12], fa[M][12], dep[M];
struct Edge {
  int x, y, val;
  void rd(int a, int b, int c) { x = a, y = b, val = c; }
  bool operator<(const Edge& tmp) const { return val < tmp.val; }
} B[M * M];
struct edge {
  int to, nxt, cost;
} G[M];
void add_edge(int a, int b, int c) {
  G[++asdf].to = b;
  G[asdf].nxt = head[a];
  G[asdf].cost = c;
  head[a] = asdf;
}
int get_fa(int x) {
  if (par[x] == x) return x;
  return par[x] = get_fa(par[x]);
}
void dfs(int x, int f) {
  for (int i = head[x]; i; i = G[i].nxt) {
    int y = G[i].to;
    if (y == f) continue;
    dep[y] = dep[x] + 1;
    fa[y][0] = x;
    mx[y][0] = G[i].cost;
    dfs(y, x);
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  int tmp = dep[y] - dep[x];
  for (int i = 0; i < 12; i++) {
    if (tmp & 1) y = fa[y][i];
    tmp >>= 1;
  }
  if (x == y) return x;
  for (int i = 11; i >= 0; i--) {
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  }
  return fa[x][0];
}
int jump(int x, int y) {
  int tmp = dep[x] - dep[y];
  int res = 0;
  for (int i = 0; i < 12; i++) {
    if (tmp & 1) {
      res = max(res, mx[x][i]);
      x = fa[x][i];
    }
    tmp >>= 1;
  }
  return res;
}
int dis(int x, int y) {
  int c = lca(x, y);
  return max(jump(x, c), jump(y, c));
}
int main() {
  while (scanf("%d", &n) == 1) {
    bool flag = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        scanf("%d", &A[i][j]);
      }
    }
    for (int i = 1; i <= n; i++) {
      if (A[i][i] != 0) flag = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (A[i][j] != A[j][i]) flag = 0;
      }
    }
    if (!flag) {
      printf("NOT MAGIC\n");
      continue;
    }
    for (int i = 1; i <= n; i++) {
      par[i] = i;
    }
    len = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        B[++len].rd(i, j, A[i][j]);
      }
    }
    asdf = 0;
    for (int i = 1; i <= n; i++) {
      head[i] = 0;
    }
    sort(B + 1, B + 1 + len);
    for (int i = 1; i <= len; i++) {
      int x = get_fa(B[i].x);
      int y = get_fa(B[i].y);
      if (x != y) {
        par[x] = y;
        add_edge(B[i].x, B[i].y, B[i].val);
        add_edge(B[i].y, B[i].x, B[i].val);
      }
    }
    dfs(1, 0);
    for (int j = 1; j < 12; j++) {
      for (int i = 1; i <= n; i++) {
        fa[i][j] = fa[fa[i][j - 1]][j - 1];
        mx[i][j] = max(mx[i][j - 1], mx[fa[i][j - 1]][j - 1]);
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        int d = dis(i, j);
        if (d != A[i][j]) {
          flag = 0;
          break;
        }
      }
      if (!flag) break;
    }
    if (flag)
      printf("MAGIC\n");
    else
      printf("NOT MAGIC\n");
  }
  return 0;
}

### 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;
const int M = 2505;
int n, A[M][M], len;
int par[M], asdf, head[M];
int mx[M][12], fa[M][12], dep[M];
struct Edge {
  int x, y, val;
  void rd(int a, int b, int c) { x = a, y = b, val = c; }
  bool operator<(const Edge& tmp) const { return val < tmp.val; }
} B[M * M];
struct edge {
  int to, nxt, cost;
} G[M];
void add_edge(int a, int b, int c) {
  G[++asdf].to = b;
  G[asdf].nxt = head[a];
  G[asdf].cost = c;
  head[a] = asdf;
}
int get_fa(int x) {
  if (par[x] == x) return x;
  return par[x] = get_fa(par[x]);
}
void dfs(int x, int f) {
  for (int i = head[x]; i; i = G[i].nxt) {
    int y = G[i].to;
    if (y == f) continue;
    dep[y] = dep[x] + 1;
    fa[y][0] = x;
    mx[y][0] = G[i].cost;
    dfs(y, x);
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  int tmp = dep[y] - dep[x];
  for (int i = 0; i < 12; i++) {
    if (tmp & 1) y = fa[y][i];
    tmp >>= 1;
  }
  if (x == y) return x;
  for (int i = 11; i >= 0; i--) {
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  }
  return fa[x][0];
}
int jump(int x, int y) {
  int tmp = dep[x] - dep[y];
  int res = 0;
  for (int i = 0; i < 12; i++) {
    if (tmp & 1) {
      res = max(res, mx[x][i]);
      x = fa[x][i];
    }
    tmp >>= 1;
  }
  return res;
}
int dis(int x, int y) {
  int c = lca(x, y);
  return max(jump(x, c), jump(y, c));
}
int main() {
  while (scanf("%d", &n) == 1) {
    bool flag = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        scanf("%d", &A[i][j]);
      }
    }
    for (int i = 1; i <= n; i++) {
      if (A[i][i] != 0) flag = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (A[i][j] != A[j][i]) flag = 0;
      }
    }
    if (!flag) {
      printf("NOT MAGIC\n");
      continue;
    }
    for (int i = 1; i <= n; i++) {
      par[i] = i;
    }
    len = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        B[++len].rd(i, j, A[i][j]);
      }
    }
    asdf = 0;
    for (int i = 1; i <= n; i++) {
      head[i] = 0;
    }
    sort(B + 1, B + 1 + len);
    for (int i = 1; i <= len; i++) {
      int x = get_fa(B[i].x);
      int y = get_fa(B[i].y);
      if (x != y) {
        par[x] = y;
        add_edge(B[i].x, B[i].y, B[i].val);
        add_edge(B[i].y, B[i].x, B[i].val);
      }
    }
    dfs(1, 0);
    for (int j = 1; j < 12; j++) {
      for (int i = 1; i <= n; i++) {
        fa[i][j] = fa[fa[i][j - 1]][j - 1];
        mx[i][j] = max(mx[i][j - 1], mx[fa[i][j - 1]][j - 1]);
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        int d = dis(i, j);
        if (d != A[i][j]) {
          flag = 0;
          break;
        }
      }
      if (!flag) break;
    }
    if (flag)
      printf("MAGIC\n");
    else
      printf("NOT MAGIC\n");
  }
  return 0;
}

```