#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)100;
vector<int> arr(MAXN + 1, 0);
int n;
vector<int> deg(MAXN + 1, 0);
bool isCycle(int root) {
  vector<int> col(n + 1, 0);
  int n = arr[root];
  col[root] = 2;
  while (n != root && col[n] == 0) {
    col[n] = 2;
    n = arr[n];
  }
  if (n != root)
    return false;
  else
    return true;
}
int cycleLen(int root) {
  vector<int> col(n + 1, 0);
  int len = 0;
  int n = arr[root];
  col[root] = 2;
  len++;
  while (n != root && col[n] == 0) {
    col[n] = 2;
    n = arr[n];
    len++;
  }
  return len;
}
long long GCD(long long a, long long b) {
  if (a < b) swap(a, b);
  long long r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
long long LCM(long long a, int b) {
  long long gc = GCD(a, b);
  return a * b * 1ll / gc;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    deg[i] += 1;
    deg[arr[i]] += 1;
  }
  for (int i = 1; i <= n; i++) {
    if (deg[i] != 2) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<int> ms;
  for (int i = 1; i <= n; i++) {
    ms.push_back(cycleLen(i));
  }
  int var = *ms.begin();
  long long ans;
  if (var % 2 == 0)
    ans = var / 2;
  else
    ans = var;
  for (auto it = ms.begin() + 1; it != ms.end(); it++) {
    if ((*it) % 2)
      ans = LCM(ans, *it);
    else
      ans = LCM(ans, *it / 2);
  }
  cout << ans << endl;
  return 0;
}
