#include <bits/stdc++.h>
using namespace std;
map<char, int> posL;
string al = "abcdefghijklmnopqrstuvwxyz";
void no() { cout << "false"; }
void yes() { cout << "true"; }
int main() {
  int n;
  cin >> n;
  int s1, f1, s2, f2;
  map<int, int> start;
  map<int, int> ends;
  map<int, bool> used;
  vector<int> colors;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (!used[x]) {
      start[x] = i;
      ends[x] = i;
      used[x] = 1;
      colors.push_back(x);
    } else {
      ends[x] = i;
    }
  }
  map<pair<int, int>, int> same;
  vector<pair<int, int> > fir, sec;
  for (int x : colors) {
    fir.push_back(pair<int, int>(start[x], ends[x]));
    sec.push_back(pair<int, int>(ends[x], start[x]));
    same[pair<int, int>(start[x], ends[x])] = x;
    same[pair<int, int>(ends[x], start[x])] = x;
  }
  sort(fir.begin(), fir.end());
  sort(sec.begin(), sec.end());
  int sz2 = sec.size() - 1;
  if (same[sec[sz2]] == same[fir[0]]) {
    cout << max(sec[sz2].first - fir[1].first,
                sec[sz2 - 1].first - fir[0].first);
  } else {
    cout << sec[sz2].first - fir[0].first;
  }
}
