#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 19), inf = (1 << 29);
map<int, int> hashy;
int key, st, en;
int orig[MX], tree[MX << 2], hist[MX << 2];
void update(int x, int a, int b) {
  if (st > b || st < a) return;
  if (a == b) {
    tree[x] = key;
    return;
  }
  update(x * 2, a, (a + b) / 2);
  update(x * 2 + 1, (a + b) / 2 + 1, b);
  tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
}
int query(int x, int a, int b) {
  if (st > b || en < a)
    hist[x] = inf;
  else if (a >= st && b <= en)
    hist[x] = tree[x];
  else
    hist[x] =
        min(query(x * 2, a, (a + b) / 2), query(x * 2 + 1, (a + b) / 2 + 1, b));
  return hist[x];
}
int nk7(int x, int a, int b) {
  if (a >= st && b <= en)
    return tree[x];
  else
    return hist[x];
}
int get(int x, int a, int b) {
  if (a == b) return a;
  if (nk7(x * 2, a, (a + b) / 2) <= nk7(x * 2 + 1, (a + b) / 2 + 1, b))
    return get(x * 2, a, (a + b) / 2);
  else
    return get(x * 2 + 1, (a + b) / 2 + 1, b);
}
struct Query {
  int key, st, en, idx;
  Query() {}
  Query(int key, int st, int en, int idx)
      : key(key), st(st), en(en), idx(idx) {}
};
bool operator<(const Query &A, const Query &B) { return A.key < B.key; }
vector<Query> Queries;
int n, arr[MX], last[MX], nxt[MX], best[MX];
int dp[MX];
vector<int> sol;
int calc(int x) {
  if (x == n + 1) return 0;
  int &ret = dp[x];
  if (ret != -1) return ret;
  ret = calc(x + 1);
  if (best[x]) ret = max(ret, 1 + calc(nxt[best[x]] + 1));
  return ret;
}
void find_sol(int x) {
  if (x == n + 1) return;
  int ret = dp[x];
  if (ret == calc(x + 1))
    find_sol(x + 1);
  else {
    sol.push_back(orig[arr[x]]);
    sol.push_back(orig[arr[best[x]]]);
    sol.push_back(orig[arr[x]]);
    sol.push_back(orig[arr[best[x]]]);
    find_sol(nxt[best[x]] + 1);
  }
}
int main() {
  scanf("%d", &n);
  for (int j = 1; j <= n; j++) {
    scanf("%d", &arr[j]);
    hashy[arr[j]] = 1;
  }
  int cc = 0;
  for (auto it : hashy) {
    hashy[it.first] = ++cc;
    orig[cc] = it.first;
  }
  for (int j = 1; j <= n; j++) arr[j] = hashy[arr[j]];
  for (int j = 1; j <= cc; j++) last[j] = inf;
  set<pair<int, int> > S;
  for (int j = n; j; j--) {
    nxt[j] = last[arr[j]];
    S.insert(make_pair(nxt[j], j));
    last[arr[j]] = j;
  }
  for (int j = 1; j <= n; j++) {
    st = j, key = nxt[j];
    update(1, 1, n);
    int cur = j, n1 = nxt[j];
    if (n1 == inf) continue;
    Queries.push_back(Query(n1 + 1, cur + 1, n1 - 1, j));
    int n2 = nxt[n1];
    if (n2 == inf) continue;
    Queries.push_back(Query(n2 + 1, cur + 1, n2 - 1, j));
    int n3 = nxt[n2];
    if (n3 == inf) continue;
    best[j] = n2;
  }
  sort(Queries.begin(), Queries.end());
  for (auto Q : Queries) {
    int idx = Q.idx, V = Q.key;
    while (!S.empty() && S.begin()->first < V) {
      st = S.begin()->second, key = inf;
      update(1, 1, n);
      S.erase(S.begin());
    }
    st = Q.st, en = Q.en;
    if (query(1, 1, n) == inf) continue;
    int G = get(1, 1, n);
    if (nxt[G] < nxt[best[idx]] || best[idx] == 0) best[idx] = G;
  }
  memset(dp, -1, sizeof(dp));
  cout << calc(1) * 4 << endl;
  find_sol(1);
  for (auto pp : sol) cout << pp << ' ';
}
