#include <bits/stdc++.h>
using namespace std;
int n, crush[109], dfsNumber[109];
bool vis[109];
long long int gcd(long long int a, long long int b) {
  long long int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
long long int lcm(long long int a, long long int b) {
  return a * (b / gcd(a, b));
}
int dfs(int source, int currNode, int currDfsNumber) {
  vis[currNode] = true;
  if (currNode == source) return 1;
  if (crush[currNode] == currNode ||
      (currDfsNumber > dfsNumber[crush[currNode]] &&
       dfsNumber[crush[currNode]] != 0))
    return -2000000007;
  if (vis[crush[currNode]]) return -2000000007;
  return 1 + dfs(source, crush[currNode], currDfsNumber + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &crush[i]);
    crush[i]--;
  }
  long long int ans = 1;
  for (int i = 0; i < n; i++) {
    if (vis[i]) continue;
    int cycleNodes = dfs(i, crush[i], 0);
    if (cycleNodes < 0) {
      printf("-1");
      return 0;
    }
    if (cycleNodes % 2 == 0) cycleNodes /= 2;
    ans = lcm(ans, cycleNodes);
  }
  printf("%I64d", ans);
  return 0;
}
