#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  map<int, int> freq;
  for (int i = 0; i < n; i++) cin >> v[i];
  int min = v[0], max = v[0], maximum = 0;
  freq[v[0]]++;
  int i = 0, j = 1;
  while (j < n) {
    while (((v[j] == min || v[j] == max) ||
            ((min == max) && abs(v[j] - min) == 1)) &&
           j < n) {
      freq[v[j]]++;
      if (v[j] > min) max = v[j];
      if (v[j] < min) min = v[j];
      j++;
    }
    if (j - i > maximum) maximum = j - i;
    while (!((v[j] == min || v[j] == max) ||
             ((min == max) && abs(v[j] - min) == 1)) &&
           i < n) {
      freq[v[i]]--;
      if (freq[v[i]] == 0) {
        if (min == max) {
          min = v[j];
          max = v[j];
          freq[v[j]]++;
          j++;
        } else if (v[i] == max)
          max--;
        else if (v[i] == min)
          min++;
      }
      i++;
    }
  }
  cout << maximum;
  return 0;
}
