#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> mas(n);
  for (int i = 0; i < n; i++) {
    cin >> mas[i];
  }
  int l = 0, r = 0;
  int max = 1;
  int small, big;
  while (r < n && mas[r] == mas[0]) {
    r++;
  }
  if (r == n) {
    cout << n;
    return 0;
  }
  int lastS, lastB;
  if (mas[l] < mas[r]) {
    small = mas[l];
    big = mas[r];
    lastS = r - 1;
    lastB = r;
  } else {
    small = mas[r];
    big = mas[l];
    lastS = r;
    lastB = r - 1;
  }
  r++;
  while (r < n && small <= mas[r] && mas[r] <= big) {
    if (mas[r] == small) {
      lastS = r;
    } else {
      lastB = r;
    }
    r++;
  }
  if (r == n) {
    cout << n;
    return 0;
  }
  while (r < n) {
    if (r - l > max) {
      max = r - l;
    }
    if (mas[r] < small) {
      l = lastB + 1;
      big = small;
      small = mas[r];
      if (l > lastS) {
        lastB = l;
      } else {
        lastB = lastS;
      }
      lastS = r;
    } else if (mas[r] > big) {
      l = lastS + 1;
      small = big;
      big = mas[r];
      if (l > lastB) {
        lastS = l;
      } else {
        lastS = lastB;
      }
      lastB = r;
    } else {
      if (mas[r] == small) {
        lastS = r;
      } else {
        lastB = r;
      }
    }
    r++;
    while (r < n && small <= mas[r] && mas[r] <= big) {
      if (mas[r] == small) {
        lastS = r;
      } else {
        lastB = r;
      }
      r++;
    }
  }
  if (r - l > max) {
    cout << r - l;
  } else {
    cout << max;
  }
  return 0;
}
