#include <bits/stdc++.h>
using namespace std;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T>
bool ckmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
bool reorder(T &a, T &b) {
  return (a > b) ? swap(a, b), 1 : 0;
}
template <class A>
void re(complex<A> &c);
template <class A, class B>
void re(pair<A, B> &p);
template <class A>
void re(vector<A> &v);
template <class A, size_t SZ>
void re(array<A, SZ> &a);
template <class T>
void re(T &x) {
  cin >> x;
}
void re(double &d) {
  string t;
  re(t);
  d = stod(t);
}
void re(long double &d) {
  string t;
  re(t);
  d = stold(t);
}
template <class H, class... T>
void re(H &h, T &...t) {
  re(h);
  re(t...);
}
template <class A>
void re(complex<A> &c) {
  A a, b;
  re(a, b);
  c = {a, b};
}
template <class A, class B>
void re(pair<A, B> &p) {
  re(p.first, p.second);
}
template <class A>
void re(vector<A> &x) {
  for (auto &a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ> &x) {
  for (auto &a : x) re(a);
}
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char *second) { return (string)second; }
string to_string(string second) { return second; }
template <class A>
string to_string(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.string();
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = (0); i < ((int)v.size()); ++i) res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
  string res = "";
  for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class T>
string to_string(T v) {
  bool fst = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!fst) res += ", ";
    fst = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class A>
void pr(A x) {
  cout << to_string(x);
}
template <class H, class... T>
void pr(const H &h, const T &...t) {
  pr(h);
  pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H &h, const T &...t) {
  pr(h);
  if (sizeof...(t)) pr(" ");
  ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
void setIO(string second = "") {
  unsyncIO();
  if ((int)second.size()) {
    setIn(second + ".in"), setOut(second + ".out");
  }
}
string testinput = R"(4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8
)";
int sgn(long long a) { return (a > 0) - (a < 0); }
long long sq(long long a) { return a * a; }
namespace Point {
long long norm(const pair<long long, long long> &p) {
  return sq(p.first) + sq(p.second);
}
long long abs(const pair<long long, long long> &p) { return sqrt(norm(p)); }
long long arg(const pair<long long, long long> &p) {
  return atan2(p.second, p.first);
}
pair<long long, long long> conj(const pair<long long, long long> &p) {
  return pair<long long, long long>(p.first, -p.second);
}
pair<long long, long long> perp(const pair<long long, long long> &p) {
  return pair<long long, long long>(-p.second, p.first);
}
pair<long long, long long> dir(long long ang) {
  return pair<long long, long long>(cos(ang), sin(ang));
}
pair<long long, long long> operator-(const pair<long long, long long> &l) {
  return pair<long long, long long>(-l.first, -l.second);
}
pair<long long, long long> operator+(const pair<long long, long long> &l,
                                     const pair<long long, long long> &r) {
  return pair<long long, long long>(l.first + r.first, l.second + r.second);
}
pair<long long, long long> operator-(const pair<long long, long long> &l,
                                     const pair<long long, long long> &r) {
  return pair<long long, long long>(l.first - r.first, l.second - r.second);
}
pair<long long, long long> operator*(const pair<long long, long long> &l,
                                     const long long &r) {
  return pair<long long, long long>(l.first * r, l.second * r);
}
pair<long long, long long> operator*(const long long &l,
                                     const pair<long long, long long> &r) {
  return r * l;
}
pair<long long, long long> operator/(const pair<long long, long long> &l,
                                     const long long &r) {
  return pair<long long, long long>(l.first / r, l.second / r);
}
pair<long long, long long> operator*(const pair<long long, long long> &l,
                                     const pair<long long, long long> &r) {
  return pair<long long, long long>(l.first * r.first - l.second * r.second,
                                    l.second * r.first + l.first * r.second);
}
pair<long long, long long> operator/(const pair<long long, long long> &l,
                                     const pair<long long, long long> &r) {
  return l * conj(r) / norm(r);
}
pair<long long, long long> &operator+=(pair<long long, long long> &l,
                                       const pair<long long, long long> &r) {
  return l = l + r;
}
pair<long long, long long> &operator-=(pair<long long, long long> &l,
                                       const pair<long long, long long> &r) {
  return l = l - r;
}
pair<long long, long long> &operator*=(pair<long long, long long> &l,
                                       const long long &r) {
  return l = l * r;
}
pair<long long, long long> &operator/=(pair<long long, long long> &l,
                                       const long long &r) {
  return l = l / r;
}
pair<long long, long long> &operator*=(pair<long long, long long> &l,
                                       const pair<long long, long long> &r) {
  return l = l * r;
}
pair<long long, long long> &operator/=(pair<long long, long long> &l,
                                       const pair<long long, long long> &r) {
  return l = l / r;
}
pair<long long, long long> unit(const pair<long long, long long> &p) {
  return p / abs(p);
}
long long dot(const pair<long long, long long> &a,
              const pair<long long, long long> &b) {
  return a.first * b.first + a.second * b.second;
}
long long cross(const pair<long long, long long> &a,
                const pair<long long, long long> &b) {
  return a.first * b.second - a.second * b.first;
}
long long cross(const pair<long long, long long> &p,
                const pair<long long, long long> &a,
                const pair<long long, long long> &b) {
  return cross(a - p, b - p);
}
pair<long long, long long> reflect(const pair<long long, long long> &p,
                                   const pair<long long, long long> &a,
                                   const pair<long long, long long> &b) {
  return a + conj((p - a) / (b - a)) * (b - a);
}
pair<long long, long long> foot(const pair<long long, long long> &p,
                                const pair<long long, long long> &a,
                                const pair<long long, long long> &b) {
  return (p + reflect(p, a, b)) / (long long)2;
}
bool onSeg(const pair<long long, long long> &p,
           const pair<long long, long long> &a,
           const pair<long long, long long> &b) {
  return cross(a, b, p) == 0 && dot(p - a, p - b) <= 0;
}
ostream &operator<<(ostream &os, const pair<long long, long long> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
};  // namespace Point
int n;
vector<int> order;
map<int, set<int>> adj;
map<int, int> dists;
set<int> seen, exited;
pair<int, int> id2g(int id) {
  id -= 1;
  return make_pair(id / n, id % n);
}
int g2id(int i, int j) {
  if (i < n and i >= 0 and j < n and j >= 0)
    return i * n + j + 1;
  else
    return -1;
}
void solve(int ti) {
  re(n);
  order.resize(n * n);
  re(order);
  adj.clear(), dists.clear(), seen.clear(), exited.clear();
  for (int i = (0); i < (n); ++i) {
    for (int j = (0); j < (n); ++j) {
      int id = g2id(i, j);
      int u = g2id(i, j + 1), d = g2id(i, j - 1), l = g2id(i - 1, j),
          r = g2id(i + 1, j);
      for (auto nei : {u, d, l, r}) {
        adj[id].emplace(nei);
        adj[nei].emplace(id);
      }
    }
  }
  priority_queue<pair<int, int>> pq;
  pq.emplace(-(-1), -1);
  while (not pq.empty()) {
    auto top = pq.top();
    pq.pop();
    if (seen.count(top.second))
      continue;
    else {
      int id = top.second, cost = -top.first;
      dists[id] = cost;
      seen.emplace(id);
      for (auto nei : adj[id]) {
        pq.emplace(-(cost + 1), nei);
      }
    }
  }
  int hate = 0;
  function<void(int, int)> update = [&](int updated, int newdist) {
    if (ckmin(dists[updated], newdist)) {
      for (auto &nei : adj[updated]) {
        update(nei, newdist + (1 - exited.count(updated)));
      }
    }
  };
  for (auto &leaving : order) {
    hate += dists[leaving];
    exited.emplace(leaving);
    for (auto &nei : adj[leaving]) {
      update(nei, dists[leaving]);
    }
  }
  cout << hate << endl;
}
int main() {
  bool online = false;
  try {
    ifstream inFile;
    inFile.open("test.txt");
    if (!inFile) {
      online = true;
    }
    inFile.close();
  } catch (...) {
    online = true;
  }
  istringstream iss(testinput);
  if (not online) {
    cout << "---local mode---" << endl;
    cin.rdbuf(iss.rdbuf());
  }
  int long long = 1;
  {}
  for (int i = (1); i < (long long + 1); ++i) {
    cout << "Case #" << i << ": ";
    solve(i);
  }
}
