#include <bits/stdc++.h>
using namespace std;
inline long long Rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void Rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    Rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int N = 500005;
map<int, int> Pre, Ex;
int Ans[N], AC;
int St[N], Top, n;
int main() {
  n = Rd();
  for (int i = 1; i <= (int)n; i++) {
    int x = Rd();
    if (Pre[x]) {
      Ans[++AC] = Pre[x], Ans[++AC] = x, Ans[++AC] = Pre[x], Ans[++AC] = x;
      Pre.clear(), Ex.clear(), Top = 0;
      continue;
    }
    while (Top && (Ex[x] >= 2 || (Ex[x] >= 1 && St[Top] != x))) {
      Pre[St[Top]] = x, Ex[St[Top]]--;
      Top--;
    }
    St[++Top] = x;
    Ex[x]++;
  }
  Rt(AC), putchar('\n');
  for (int i = 1; i <= (int)AC; i++) Rt(Ans[i]), putchar(' ');
}
