#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define REP(var, a, b) for (int var = (a); var < (b); var++)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()
#define rALL(c) (c).rbegin(), (c).rend()
ll MOD = 1000000007;
const ll INF = 1LL << 62;

class MinCostFlow {
 public:
  const ll INF = 1LL << 60;
  typedef pair<ll, ll> P;
  struct edge {
    ll to, cap, cost, rev;
  };
  vector<vector<edge>> G;
  vector<ll> h, dist, prevv, preve;
  ll V;

  MinCostFlow(ll V) {
    this->V = V;
    G = vector<vector<edge>>(V);
    h = vector<ll>(V);
    dist = vector<ll>(V);
    prevv = vector<ll>(V);
    preve = vector<ll>(V);
  }

  void addEdge(ll from, ll to, ll cap, ll cost) {
    G[from].push_back((edge){to, cap, cost, G[to].size()});
    G[to].push_back((edge){from, 0, -cost, G[from].size() - 1});
  }

  ll solve(ll s, ll t, ll f) {
    ll res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> que;
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      que.push(P(0, s));
      while (!que.empty()) {
        P p = que.top();
        que.pop();
        ll 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(P(dist[e.to], e.to));
          }
        }
      }
      if (dist[t] == INF) {
        return -1;
      }
      for (int v = 0; v < V; ++v) {
        h[v] += dist[v];
      }

      ll 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() {
  //
  ll n;
  cin >> n;
  vl p(n);
  rep(i, n) cin >> p[i];
  MinCostFlow mcf(2 + 2 * n);
  rep(i, n) {
    mcf.addEdge(0, i + 1, 1, 0);
    mcf.addEdge(n + i + 1, 2 * n + 1, 1, 0);
  }
  rep(i, n) {
    if (p[i] == i + 1) {
      rep(j, n) {
        if (j == i) continue;
        mcf.addEdge(i + 1, n + j + 1, 1, abs(i - j));
      }
    } else {
      rep(j, n) {
        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));
        }
      }
    }
  }
  ll ans = mcf.solve(0, 2 * n + 1, n) / 2;
  cout << ans << endl;
  return 0;
}
