#include <bits/stdc++.h>
using namespace std;
int main() {
  int houses = 0;
  cin >> houses;
  int* A = new int[houses + 1];
  int C[houses + 1];
  int t[houses + 1];
  for (int k = 0; k < houses + 1; k++) {
    C[k] = -1;
  }
  int* B = new int[houses];
  int maxlens = 0;
  int temp = 0;
  int counter = 0;
  for (int i = 0; i < houses; i++) {
    cin >> B[i];
    A[B[i]] = i;
    if (C[B[i]] == -1) {
      C[B[i]] = i;
      t[counter] = B[i];
      counter++;
    }
    for (int j = 0; j < counter; j++) {
      if (B[i] != t[j]) {
        temp = A[B[i]] - C[t[j]];
        if (temp > maxlens) maxlens = temp;
        break;
      }
    }
  }
  cout << maxlens << endl;
  return 0;
}
