#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 12;
int n, a[maxn], l = 1, r = 1, L = 1, R = 1, used[maxn], res, len, mx;
void add(int x) {
  used[x]++;
  if (used[x] == 1) res++;
}
void rem(int x) {
  used[x]--;
  if (used[x] == 0) res--;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int l = 1; l <= n; l++) {
    if (l > 1) {
      rem(a[l - 1]);
    }
    while (r <= n && res <= n) {
      if (res == 2 && used[a[r]] == 0) {
        break;
      }
      add(a[r]);
      r++;
    }
    mx = max(mx, r - l);
  }
  cout << mx;
}
