#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int a[maxn];
int mx_num, mx_cnt, mi_num, mi_cnt;
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    int ans = -1, l = 0;
    mx_num = mi_num = a[0], mx_cnt = mi_cnt = 1;
    for (int r = 1; r < n; r++) {
      if (abs(a[r] - mi_num) > 1) {
        while (mi_cnt) {
          if (a[l] == mi_num) mi_cnt--;
          if (a[l] == mx_num) mx_cnt--;
          l++;
        }
        if (mx_cnt)
          mi_cnt = mx_cnt, mi_num = mx_num;
        else
          mi_cnt = 1, mi_num = a[r];
        mx_cnt = 1, mx_num = a[r];
      } else if (abs(a[r] - mx_num) > 1) {
        while (mx_cnt) {
          if (a[l] == mi_num) mi_cnt--;
          if (a[l] == mx_num) mx_cnt--;
          l++;
        }
        if (mi_cnt)
          mx_cnt = mi_cnt, mx_num = mi_num;
        else
          mx_cnt = 1, mx_num = a[r];
        mi_cnt = 1, mi_num = a[r];
      } else {
        ans = max(ans, r - l + 1);
        if (a[r] == mi_num) mi_cnt++;
        if (a[r] == mx_num) mx_cnt++;
        if (mi_num == mx_num && a[r] != mi_num) {
          if (a[r] > mx_num)
            mx_num = a[r], mx_cnt = 1;
          else
            mi_num = a[r], mi_cnt = 1;
        }
      }
    }
    printf("%d\n", ans);
  }
}
