#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long _x = 0;
  int _ch = getchar(), _f = 1;
  for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar())
    ;
  if (_ch == '-') {
    _f = 0;
    _ch = getchar();
  }
  for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0';
  return _f ? _x : -_x;
}
void write(long long _x) {
  if (_x >= 10)
    write(_x / 10), putchar(_x % 10 + '0');
  else
    putchar(_x + '0');
}
inline void wrt(long long _x, char _p) {
  if (_x < 0) putchar('-'), _x = -_x;
  write(_x);
  if (_p) putchar(_p);
}
int sta[500005], top;
int a[500005], H[500005];
int s[500005], vis[500005];
int n;
vector<int> res;
int main() {
  n = rd();
  for (int i = int(1); i <= (int)(n); i++) H[i] = a[i] = rd();
  sort(H + 1, H + 1 + n);
  for (int i = int(1); i <= (int)(n); i++)
    a[i] = lower_bound(H + 1, H + 1 + n, a[i]) - H;
  for (int i = int(1); i <= (int)(n); i++) {
    top = 0;
    int pos = i;
    for (; i <= n; i++) {
      if (vis[a[i]]) {
        res.push_back(vis[a[i]]);
        res.push_back(a[i]);
        res.push_back(vis[a[i]]);
        res.push_back(a[i]);
        break;
      }
      while (top && (s[a[i]] > 1 || (s[a[i]] == 1 && sta[top] != a[i]))) {
        vis[sta[top]] = a[i];
        s[sta[top]]--;
        top--;
      }
      sta[++top] = a[i];
      s[a[i]]++;
    }
    for (int j = int(pos); j <= (int)(i); j++) {
      vis[a[j]] = s[a[j]] = 0;
    }
  }
  wrt(res.size(), '\n');
  for (auto i : res) wrt(H[i], ' ');
}
