#include <bits/stdc++.h>
using namespace std;
int n;
int a[300005] = {0};
int c1 = 0, c2 = 0, p1 = 0, p2 = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int maxn = -1;
  for (int i = 1; i <= n; i++) {
    if (c1 == 0) {
      p1 = i;
      c1 = a[i];
    } else if (c2 == 0 && a[i] != c1) {
      p2 = i;
      c2 = a[i];
      maxn = max(maxn, p2 - p1);
    } else if (a[i] == c1 && p2 != 0) {
      maxn = max(maxn, i - p2);
    } else if (a[i] == c2) {
      maxn = max(maxn, i - p1);
    } else if (a[i] != c1 && a[i] != c2) {
      maxn = max(maxn, i - p1);
    }
  }
  cout << maxn << endl;
  return 0;
}
