#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
vector<int> seg[N * 4];
void insert(int id, int s, int e, int idx, int v) {
  if (idx < s || idx > e) return;
  seg[id].emplace_back(v);
  if (s == e) return;
  int mid = s + (e - s) / 2;
  insert(id * 2, s, mid, idx, v);
  insert(id * 2 + 1, mid + 1, e, idx, v);
}
int query(int id, int s, int e, int f, int t, int v) {
  if (e < f || s > t) return 1e6;
  if (s >= f && e <= t) {
    auto it = upper_bound(seg[id].begin(), seg[id].end(), v);
    if (it != seg[id].end()) return *it;
    return 1e6;
  }
  if (s == e) return 1e6;
  int mid = s + (e - s) / 2;
  return min(query(id * 2, s, mid, f, t, v),
             query(id * 2 + 1, mid + 1, e, f, t, v));
}
int arr[N];
int nxt[N];
int cnt[N], vis[N], vid = 0, occ[N];
void add(int idx) {
  if (vis[idx] != vid) {
    vis[idx] = vid;
    cnt[idx] = 0;
  }
  ++cnt[idx];
}
int main() {
  int n;
  scanf("%d", &n);
  map<int, int> mp, rev;
  for (int i = 0; i < n; ++i) {
    scanf("%d", arr + i);
    mp[arr[i]];
  }
  int id = 0;
  for (auto &x : mp) {
    x.second = id++;
    rev[x.second] = x.first;
  }
  for (int i = 0; i < n; ++i) {
    arr[i] = mp[arr[i]];
  }
  memset(nxt, -1, sizeof nxt);
  for (int i = n - 1; ~i; --i) {
    if (nxt[arr[i]] != -1) {
      insert(1, 0, n - 1, i, nxt[arr[i]]);
    }
    nxt[arr[i]] = i;
  }
  vector<pair<int, int>> ans;
  for (int i = 0; i < N * 4; ++i) {
    sort(seg[i].begin(), seg[i].end());
  }
  int s = 0, e;
  while (s < n) {
    ++vid;
    if (s + 2 < n) {
      add(arr[s]);
      occ[arr[s]] = s;
      add(arr[s + 1]);
      if (arr[s] != arr[s + 1]) {
        occ[arr[s + 1]] = s + 1;
      }
    } else
      break;
    int bid = 1e6;
    id = -1;
    for (e = s + 2; e < n; ++e) {
      add(arr[e]);
      if (cnt[arr[e]] == 1) {
        occ[arr[e]] = e;
        continue;
      }
      if (cnt[arr[e]] == 4) {
        ans.push_back({arr[e], arr[e]});
        s = e + 1;
        break;
      }
      if (e == bid) {
        ans.push_back({arr[id], arr[e]});
        s = e + 1;
        break;
      }
      if (occ[arr[e]] < e - 1) {
        int v = query(1, 0, n - 1, occ[arr[e]] + 1, e - 1, e);
        if (v < bid) {
          bid = v;
          id = e;
        }
      }
    }
    if (e == n) break;
  }
  printf("%d\n", ans.size() * 4);
  for (auto P : ans) {
    printf("%d %d %d %d ", rev[P.first], rev[P.second], rev[P.first],
           rev[P.second]);
  }
}
