#include <bits/stdc++.h>
using namespace std;
int maxm = 0, siz = 0, p = 0;
vector<int> v[100005];
int a[100005];
bool vis[100005];
vector<int> ans;
long long int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
void dfs(int s, int n) {
  if (vis[n] && n == s) {
    p = 1;
    return;
  }
  if (vis[n]) return;
  vis[n] = 1;
  siz++;
  dfs(s, a[n]);
  return;
}
int main() {
  int n, i;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, i);
      if (p == 1)
        ans.push_back(siz);
      else {
        cout << "-1";
        return 0;
      }
    }
    siz = 0;
    p = 0;
  }
  for (i = 0; i < ans.size(); i++)
    if (ans[i] % 2 == 0) ans[i] = ans[i] / 2;
  if (ans.size() == 1) {
    cout << ans[0];
    return 0;
  }
  long long int g;
  g = ans[0] * ans[1] / gcd(ans[1], ans[0]);
  for (i = 2; i < ans.size(); i++) {
    g = ans[i] * g / gcd(ans[i], g);
  }
  cout << g << endl;
  return 0;
}
