#include <bits/stdc++.h>
using namespace std;
template <class T1>
void debug(T1 e) {
  cout << e << endl;
}
template <class T1, class T2>
void debug(T1 e1, T2 e2) {
  cout << e1 << "\t" << e2 << endl;
}
template <class T1, class T2, class T3>
void debug(T1 e1, T2 e2, T3 e3) {
  cout << e1 << "\t" << e2 << "\t" << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void debug(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << "\t"
       << e6 << endl;
}
template <class T>
void debug(vector<vector<T> > e, int row, int col) {
  int i, j;
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) cout << e[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}
template <class T>
void debug(vector<basic_string<T> > e, int row, int col) {
  int i, j;
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) cout << e[i][j];
    cout << endl;
  }
  cout << endl;
}
template <class T>
void debug(T e[110][110], int row, int col) {
  int i, j;
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) cout << e[i][j] << " ";
    cout << endl;
  }
}
template <class T>
string toString(T n) {
  ostringstream oss;
  oss << n;
  oss.flush();
  return oss.str();
}
int toInt(string s) {
  int r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
bool isVowel(char ch) {
  ch = tolower(ch);
  if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    return true;
  return false;
}
bool isUpper(char c) { return c >= 'A' && c <= 'Z'; }
bool isLower(char c) { return c >= 'a' && c <= 'z'; }
map<int, int> id;
int first[500010], total[500010];
vector<int> ans;
int n;
int arr[500010];
int idno;
int heap[4 * 500010];
int query(int in, int st, int end, int x) {
  if (x < st) return 0;
  if (x >= end) return heap[in];
  int mid = (st + end) / 2;
  int ans = query(2 * in, st, mid, x);
  ans = max(ans, query(2 * in + 1, mid + 1, end, x));
  return ans;
}
void update(int in, int st, int end, int x, int val) {
  if (x > end) return;
  if (st > x) return;
  if (st == end) {
    heap[in] = val;
    return;
  }
  int mid = (st + end) / 2;
  update(2 * in, st, mid, x, val);
  update(2 * in + 1, mid + 1, end, x, val);
  heap[in] = max(heap[2 * in], heap[2 * in + 1]);
}
void go(int st) {
  if (st > n) return;
  int i;
  for (i = st; i <= n; i++) {
    int now = arr[i];
    if (id.find(now) == id.end()) id[now] = (++idno);
    total[id[now]]++;
    if (total[id[now]] == 4) {
      ans.push_back(now);
      ans.push_back(now);
      ans.push_back(now);
      ans.push_back(now);
      break;
    }
    if (first[id[now]] == 0)
      first[id[now]] = i;
    else {
      int tem = query(1, 1, n, first[id[now]] - 1);
      if (tem > first[id[now]]) {
        ans.push_back(arr[tem]);
        ans.push_back(now);
        ans.push_back(arr[tem]);
        ans.push_back(now);
        break;
      }
      update(1, 1, n, first[id[now]], i);
    }
  }
  int last = i + 1;
  for (; i >= st; i--) {
    update(1, 1, n, i, 0);
    first[id[arr[i]]] = 0;
    total[id[arr[i]]] = 0;
  }
  go(last);
}
int main() {
  while (cin >> n) {
    int i;
    for (i = 1; i <= n; i++) scanf("%d", &arr[i]);
    go(1);
    cout << ((int)ans.size()) << endl;
    for (i = 0; i < ((int)ans.size()); i++) printf("%d ", ans[i]);
    printf("\n");
  }
  return 0;
}
