#include <bits/stdc++.h>
using namespace std;
const int maxn = 300000 + 100;
struct Node {
  int num, Index;
};
bool operator<(const Node &a, const Node &b) { return a.Index < b.Index; }
int n, cnt, ans;
Node node[maxn];
int num[maxn], last[maxn];
int main() {
  while (scanf("%d", &n) != EOF) {
    ans = 0;
    cnt = 0;
    memset(last + 1, 0, sizeof(int) * n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &num[i]);
      last[num[i]] = i;
    }
    for (int i = 1; i <= n; ++i) {
      if (last[i] == 0) {
        continue;
      }
      node[cnt].num = i;
      node[cnt++].Index = last[i];
    }
    sort(node, node + cnt);
    for (int i = 1; i <= n; ++i) {
      for (int j = cnt - 1; j >= 0; --j) {
        if (node[j].num == num[i]) {
          continue;
        }
        ans = max(ans, abs(node[j].Index - i));
        break;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
