#include <bits/stdc++.h>
using namespace std;
int arr[100];
bool visited[100];
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int lmc(int a, int b) { return (a * b) / gcd(a, b); }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i]--;
    visited[i] = false;
  }
  bool flag = true;
  int result = 1;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      int count = 0;
      int a = i;
      while (!visited[a]) {
        visited[a] = true;
        a = arr[a];
        count++;
      }
      if (a != i) {
        flag = false;
        break;
      }
      result = lmc(result, count % 2 ? 2 * count : count);
    }
  }
  if (flag) {
    cout << result / 2 << '\n';
  } else {
    cout << -1 << '\n';
  }
}
