#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
const long long INF = 1LL << 62;
class MinCostFlow {
 public:
  const long long INF = 1LL << 60;
  struct edge {
    long long to, cap, cost, rev;
  };
  vector<vector<edge>> G;
  vector<long long> h, dist, prevv, preve;
  long long V;
  MinCostFlow(long long V) {
    this->V = V;
    G = vector<vector<edge>>(V);
    h = vector<long long>(V);
    dist = vector<long long>(V);
    prevv = vector<long long>(V);
    preve = vector<long long>(V);
  }
  void addEdge(long long from, long long to, long long cap, long long cost) {
    G[from].push_back((edge){to, cap, cost, (long long)G[to].size()});
    G[to].push_back((edge){from, 0, -cost, (long long)G[from].size() - 1});
  }
  long long solve(long long s, long long t, long long f) {
    long long res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<pair<long long, long long>,
                     vector<pair<long long, long long>>,
                     greater<pair<long long, long long>>>
          que;
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      que.push(pair<long long, long long>(0, s));
      while (!que.empty()) {
        pair<long long, long long> p = que.top();
        que.pop();
        long long v = p.second;
        if (dist[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); ++i) {
          edge& e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            que.push(pair<long long, long long>(dist[e.to], e.to));
          }
        }
      }
      if (dist[t] == INF) {
        return -1;
      }
      for (int v = 0; v < V; ++v) {
        h[v] += dist[v];
      }
      long long d = f;
      for (int v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge& e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    return res;
  }
};
int main() {
  long long n;
  cin >> n;
  vector<long long> p(n);
  for (int i = 0; i < (n); ++i) cin >> p[i];
  MinCostFlow mcf(2 + 2 * n);
  for (int i = 0; i < (n); ++i) {
    mcf.addEdge(0, i + 1, 1, 0);
    mcf.addEdge(n + i + 1, 2 * n + 1, 1, 0);
  }
  for (int i = 0; i < (n); ++i) {
    if (p[i] == i + 1) {
      for (int j = 0; j < (n); ++j) {
        if (j == i) continue;
        mcf.addEdge(i + 1, n + j + 1, 1, abs(i - j));
      }
    } else {
      for (int j = 0; j < (n); ++j) {
        if (i == j) {
          mcf.addEdge(i + 1, n + j + 1, 1, 0);
        } else if (p[i] != j + 1) {
          mcf.addEdge(i + 1, n + j + 1, 1, abs(i - j));
        }
      }
    }
  }
  long long ans = mcf.solve(0, 2 * n + 1, n) / 2;
  cout << ans << endl;
  return 0;
}
