#include <bits/stdc++.h>
using namespace std;
int n, ans;
int a[100005];
int c1, c2, s1, s2, l = 1;
int main() {
  cin >> n;
  cin >> a[1];
  c1 = a[1], s1 = 1, l = 1;
  for (int i = 2; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == c1)
      s1++;
    else if (a[i] == c2)
      s2++;
    else {
      while (s2 && s1) {
        if (a[l] == c1)
          s1--;
        else
          s2--;
        l++;
      }
      if (!s1)
        c1 = a[i], s1 = 1;
      else
        c2 = a[i], s2 = 1;
    }
    ans = max(ans, s1 + s2);
  }
  cout << ans;
}
