#include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 5;
const int INF = 0x3f3f3f3f;
int n, c[N];
int colormax[N], colormin[N], colormax1[N], colormax2[N];
int main() {
  scanf("%d", &n);
  memset(colormin, 0x3f, sizeof(colormin));
  for (register int i = 1; i <= n; i++)
    scanf("%d", &c[i]), colormax[c[i]] = max(colormax[c[i]], i),
                        colormin[c[i]] = min(colormin[c[i]], i);
  if (c[1] != c[n]) {
    cout << n - 1;
    return 0;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i == c[1]) continue;
    ans = max(ans, colormax[i] - 1);
  }
  for (int i = 1; i <= n; i++) {
    if (i == c[n]) continue;
    ans = max(ans, n - colormin[i]);
  }
  cout << ans;
  return 0;
}
