Problem:
連鎖
問題
次のようなゲームがある．
あるキャラクターが縦 1 列に N 個並んでいる．これらのキャラクターの色は赤，青，黄のいずれかであり，初期状態で同じ色のキャラクターが4つ以上連続して並んでいることはない．プレーヤーは，ある位置のキャラクターを選び他の色に変更することができる．この操作により同じ色のキャラクターが4つ以上連続して並ぶとそれらのキャラクターは消滅する．キャラクターが消滅することにより新たに同じ色のキャラクターが4つ以上連続して並ぶとそれらのキャラクターも消滅し，同じ色のキャラクターが4つ以上連続して並んでいる箇所がなくなるまでこの連鎖は続く．このゲームの目的は，消滅しないで残っているキャラクター数をなるべく少なくすることである．
例えば，下図の左端の状態で，上から6番目のキャラクターの色を黄色から青に変更すると，青のキャラクターが5つ連続するので消滅し，最終的に3つのキャラクターが消滅せずに残る．
初期状態における N 個のキャラクターの色の並びが与えられたとき， 1箇所だけキャラクターの色を変更した場合の，消滅しないで残っているキャラクター数の最小値 M を求めるプログラムを作成せよ．
入力
入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．
1行目はキャラクター数 N (1 ≤ N ≤ 10000) だけからなる．続く N 行には 1, 2, 3 のいずれか1つの整数が書かれており， i + 1 行目 (1 ≤ i ≤ N) は初期状態における上から i 番目のキャラクターの色を表す（1 は赤を，2 は青を，3は黄を表す）．
N が 0 のとき入力の終了を示す．データセットの数は 5 を超えない．
出力
データセットごとに, 消滅しないで残っているキャラクター数の最小値 M を１行に出力せよ．
入出力例
入力例
12
3
2
1
1
2
3
2
2
2
1
1
3
12
3
2
1
1
2
3
2
1
3
2
1
3
0
出力例
3
12
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: // competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/0534
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <type_traits>
#include <utility>
/// @brief ランレングス圧縮
template <class Container>
auto run_length_encoding(const Container &v) {
    std::vector<std::pair<std::decay_t<decltype(*std::begin(v))>, int>> res;
    for (auto &&e : v) {
        if (res.empty() || res.back().first != e) res.emplace_back(e, 1);
        else ++res.back().second;
    }
    return res;
}
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    while (true) {
        int n;
        std::cin >> n;
        if (!n)
            break;
        std::vector<int> a(n);
        for (auto &e : a) std::cin >> e;
        Dec >> a;
        int ans = n;
        for (int i = 0; i < n; ++i) {
            auto b = a;
            for (int j = 0; j < 3; ++j) {
                if (a[i] == j)
                    continue;
                b[i] = j;
                auto v = run_length_encoding(b);
                std::stack<std::pair<int, int>> st;
                for (auto p : v) {
                    if (p.second >= 4)
                        continue;
                    if (!st.empty() && st.top().first == p.first && st.top().second + p.second >= 4)
                        st.pop();
                    else
                        st.emplace(p);
                }
                int sum = 0;
                while (!st.empty()) {
                    sum += st.top().second;
                    st.pop();
                }
                ans = std::min(ans, sum);
            }
        }
        std::cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 3840
final_rank: 14

code_snippet: // competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/0534
#include <algorithm>
#include <iostream>
#include <stack>
#include <utility>
#include <vector>
#include <type_traits>
/// @brief ランレングス圧縮
template <class Container>
auto run_length_encoding(const Container &v) {
    std::vector<std::pair<std::decay_t<decltype(*v.begin())>, int>> res;
    for (auto &&e : v) {
        if (res.empty() || res.back().first != e) res.emplace_back(e, 1);
        else ++res.back().second;
    }
    return res;
}
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    while (true) {
        int n;
        std::cin >> n;
        if (!n)
            break;
        std::vector<int> a(n);
        for (auto &e : a) std::cin >> e;
        Dec >> a;
        int ans = n;
        for (int i = 0; i < n; ++i) {
            auto b = a;
            for (int j = 0; j < 3; ++j) {
                if (a[i] == j)
                    continue;
                b[i] = j;
                auto v = run_length_encoding(b);
                std::stack<std::pair<int, int>> st;
                for (auto p : v) {
                    if (p.second >= 4)
                        continue;
                    if (!st.empty() && st.top().first == p.first && st.top().second + p.second >= 4)
                        st.pop();
                    else
                        st.emplace(p);
                }
                int sum = 0;
                while (!st.empty()) {
                    sum += st.top().second;
                    st.pop();
                }
                ans = std::min(ans, sum);
            }
        }
        std::cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 3712
final_rank: 11

code_snippet: // competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/0534
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <type_traits>
#include <utility>
/// @brief ランレングス圧縮
template <class Container>
auto run_length_encoding(const Container &v) {
    std::vector<std::pair<std::decay_t<decltype(*v.begin())>, int>> res;
    for (auto &&e : v) {
        if (res.empty() || res.back().first != e) res.emplace_back(e, 1);
        else ++res.back().second;
    }
    return res;
}
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    while (true) {
        int n;
        std::cin >> n;
        if (!n)
            break;
        std::vector<int> a(n);
        std::copy_n(std::istream_iterator<int>(std::cin), n, std::begin(a));
        Dec >> a;
        int ans = n;
        for (int i = 0; i < n; ++i) {
            auto b = a;
            for (int j = 0; j < 3; ++j) {
                if (a[i] == j)
                    continue;
                b[i] = j;
                auto v = run_length_encoding(b);
                std::stack<std::pair<int, int>> st;
                for (auto p : v) {
                    if (p.second >= 4)
                        continue;
                    if (!st.empty() && st.top().first == p.first && st.top().second + p.second >= 4)
                        st.pop();
                    else
                        st.emplace(p);
                }
                int sum = 0;
                while (!st.empty()) {
                    sum += st.top().second;
                    st.pop();
                }
                chmin(ans, sum);
            }
        }
        std::cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 3712
final_rank: 10

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {}
  return 0;
}

int solve() {
  ll N;
  cin >> N;
  if (N == 0) return 1;
  string S(N, '0');
  rep(i, N) {
    ll c;
    cin >> c;
    S[i] += c;
  }
  static unordered_map<string,ll> mem;
  mem[""] = 0;
  auto dfs = [&](auto self, string T) -> ll {
    if (mem.count(T)) return mem[T];
    ll M = len(T);
    ll cnt = 1;
    char prev = T[0];
    string U;
    bool removed = false;
    rep(i, 1, M+1) {
      if ((i == M) or (prev != T[i])) {
        if (cnt >= 4) {
          removed = true;
          U += T.substr(i, M);
          break;
        }
        U += T.substr(i-cnt, cnt);
        cnt = 1;
        prev = T[i];
      } else {
        cnt++;
      }
    }
    if (removed) {
      mem[T] = self(self, U);
    } else {
      mem[T] = len(T);
    }
    return mem[T];
  };
  ll ans = N;
  rep(i, N) {
    rep(k, 1, 3+1) {
      char c = char('0'+k);
      if (S[i] == c) continue;
      ll l=i, r=i;
      rrep(j, i) {
        if (c != S[j]) {
          l = j+1;
          break;
        }
      }
      rep(j, i+1, N) {
        if (c != S[j]) {
          r = j-1;
          break;
        }
      }
      ll cnt = r-l+1;
      if (cnt >= 4) {
        string T = S.substr(0, l) + S.substr(r+1, N);
        chmin(ans, dfs(dfs, T));
      }
    }
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 69048
final_rank: 20

code_snippet: #line 1 "a.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/0534"
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 2 "/home/kuhaku/home/github/algo/lib/algorithm/rle.hpp"

template <class Container>
auto run_length_encoding(const Container &a) {
    std::vector<std::pair<std::decay_t<decltype(a[0])>, int>> res;
    for (auto &&e : a) {
        if (res.empty() || res.back().first != e) res.emplace_back(e, 1);
        else ++res.back().second;
    }
    return res;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/vector.hpp"

struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;

struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;

struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;

struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
#line 5 "a.cpp"

int main(void) {
    while (true) {
        int n;
        cin >> n;
        if (!n)
            break;
        vector<int> a(n);
        cin >> a;
        Dec >> a;
        int ans = n;
        rep (i, n) {
            auto b = a;
            rep (j, 3) {
                if (a[i] == j)
                    continue;
                b[i] = j;
                auto v = run_length_encoding(b);
                stack<pair<int, int>> st;
                for (auto p : v) {
                    if (p.second >= 4)
                        continue;
                    if (!st.empty() && st.top().first == p.first && st.top().second + p.second >= 4)
                        st.pop();
                    else
                        st.emplace(p);
                }
                int sum = 0;
                while (!st.empty()) {
                    sum += st.top().second;
                    st.pop();
                }
                chmin(ans, sum);
            }
        }
        co(ans);
    }

    return 0;
}
accuracy: 1
time_ms: 910
memory_kb: 3528
final_rank: 17

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 2 "/home/kuhaku/home/github/algo/lib/algorithm/rle.hpp"

template <class Container>
auto run_length_encoding(const Container &a) {
    std::vector<std::pair<std::decay_t<decltype(a[0])>, int>> res;
    for (auto &&e : a) {
        if (res.empty() || res.back().first != e) res.emplace_back(e, 1);
        else ++res.back().second;
    }
    return res;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/vector.hpp"

struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;

struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;

struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;

struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
#line 5 "a.cpp"

int main(void) {
    while (true) {
        int n;
        cin >> n;
        if (!n)
            break;
        vector<int> a(n);
        cin >> a;
        Dec >> a;
        int ans = n;
        rep (i, n) {
            rep (j, 3) {
                if (a[i] == j)
                    continue;
                auto b = a;
                b[i] = j;
                auto v = run_length_encoding(b);
                stack<pair<int, int>> st;
                for (auto p : v) {
                    if (p.second >= 4)
                        continue;
                    if (!st.empty() && st.top().first == p.first &&
                        st.top().second + p.second >= 4) {
                        st.pop();
                    } else {
                        st.emplace(p);
                    }
                }
                int sum = 0;
                while (!st.empty()) {
                    sum += st.top().second;
                    st.pop();
                }
                chmin(ans, sum);
            }
        }
        co(ans);
    }

    return 0;
}
accuracy: 1
time_ms: 910
memory_kb: 3696
final_rank: 19

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 2 "/home/kuhaku/home/github/algo/lib/algorithm/rle.hpp"

template <class Container>
auto run_length_encoding(const Container &a) {
    std::vector<std::pair<std::decay_t<decltype(a[0])>, int>> res;
    for (auto &&e : a) {
        if (res.empty() || res.back().first != e) res.emplace_back(e, 1);
        else ++res.back().second;
    }
    return res;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/vector.hpp"

struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;

struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;

struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;

struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
#line 5 "a.cpp"

int main(void) {
    while (true) {
        int n;
        cin >> n;
        if (!n)
            break;
        vector<int> a(n);
        cin >> a;
        Dec >> a;
        int ans = n;
        rep (i, n) {
            rep (j, 3) {
                if (a[i] == j)
                    continue;
                auto b = a;
                b[i] = j;
                auto v = run_length_encoding(b);
                stack<pair<int, int>> st;
                for (auto p : v) {
                    if (p.second >= 4)
                        continue;
                    if (!st.empty() && st.top().first == p.first &&
                        st.top().second + p.second >= 4) {
                        st.pop();
                    } else {
                        st.emplace(p);
                    }
                }
                int sum = 0;
                while (!st.empty()) {
                    sum += st.top().second;
                    st.pop();
                }
                chmin(ans, sum);
            }
        }
        co(ans);
    }

    return 0;
}
accuracy: 1
time_ms: 910
memory_kb: 3600
final_rank: 18

code_snippet: //Source.cpp
//いつもの
#ifdef LOCAL
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<array>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<climits>
#include<iomanip>
#include<cctype>
#include<sstream>
#include<regex>
#include<bitset>
#include<random>
#include<complex>
#include<cassert>
#else
#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifdef ONLINE_JUDGE
#include<atcoder/all>
using namespace atcoder;
#else
#define gcd __gcd
#define lcm __detail::__lcm
#endif
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#ifdef LOCAL
//*
#include<atcoder/all>
using namespace atcoder;
//*/
unsigned popcount(unsigned x){
	return __popcnt(x);
}
unsigned popcountll(ull x){
	return __popcnt64(x);
}
unsigned parity(unsigned x){
	return popcount(x)%2;
}
unsigned parityll(ull x){
	return popcountll(x)%2;
}
unsigned clz(unsigned x){
	if(x==0)
		throw;
	unsigned cnt=__lzcnt(x);
	return cnt;
}
unsigned clzll(ull x){
	if(x==0)
		throw;
	unsigned cnt=__lzcnt64(x);
	return cnt;
}
unsigned ctz(unsigned x){
	if(x==0)
		throw;
	x&=~x+1;
	unsigned cnt=0;
	if(x&0xffff0000)
		cnt|=16;
	if(x&0xff00ff00)
		cnt|=8;
	if(x&0xf0f0f0f0)
		cnt|=4;
	if(x&0xcccccccc)
		cnt|=2;
	if(x&0xaaaaaaaa)
		cnt|=1;
	return cnt;
}
unsigned ctzll(ull x){
	if(x==0)
		throw;
	x&=~x+1;
	unsigned cnt=0;
	if(x&0xffffffff00000000)
		cnt|=32;
	if(x&0xffff0000ffff0000)
		cnt|=16;
	if(x&0xff00ff00ff00ff00)
		cnt|=8;
	if(x&0xf0f0f0f0f0f0f0f0)
		cnt|=4;
	if(x&0xcccccccccccccccc)
		cnt|=2;
	if(x&0xaaaaaaaaaaaaaaaa)
		cnt|=1;
	return cnt;
}
unsigned ffs(unsigned x){
	if(x==0)
		return 0;
	return ctz(x)+1;
}
unsigned ffsll(ull x){
	if(x==0)
		return 0;
	return ctzll(x)+1;
}
#define __builtin_popcount popcount
#define __builtin_popcountll popcountll
#define __builtin_parity parity
#define __builtin_parityll parityll
#define __builtin_clz clz
#define __builtin_clzll clzll
#define __builtin_ctz ctz
#define __builtin_ctzll ctzll
#define __builtin_ffs ffs
#define __builtin_ffsll ffsll
#endif
constexpr int mod=1e9+7;
constexpr int Mod=998244353;
constexpr int inf=mod;
constexpr ll linf=(ll)mod*mod;
struct fastio{
	fastio(){
		cin.tie(0);
		ios::sync_with_stdio(0);
		cout<<fixed<<setprecision(20);
	};
}fio;
template<class T>
bool chmax(T&a,const T&b){
	if(a<b){
		a=b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a,const T&b){
	if(a>b){
		a=b;
		return 1;
	}
	return 0;
}
int main(){
	int n,c[10000];
	while(cin>>n,n){
		for(int i=0;i<n;i++)
			cin>>c[i],--c[i];
		int ans=n;
		for(int i=0;i<n;i++){
			for(int j=0;j<3;j++){
				if(c[i]==j)
					continue;
				deque<int>d1(c,c+i),d2(c+i,c+n);
				d2[0]=j;
				while(d2.size()){
					int cnt1,cnt2;
					for(cnt1=0;cnt1<d1.size()&&d1[d1.size()-1-cnt1]==d2[0];cnt1++);
					for(cnt2=0;cnt2<d2.size()&&d2[cnt2]==d2[0];cnt2++);
					if(cnt1+cnt2>=4){
						for(int k=0;k<cnt1;k++)
							d1.pop_back();
						for(int k=0;k<cnt2;k++)
							d2.pop_front();
					} else
						break;
				}
				chmin(ans,int(d1.size()+d2.size()));
			}
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 70
memory_kb: 3552
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<long long, long long> P;
const long long INF = 1e18;
const ll MOD = INF;
template <typename T>
void input_arr(vector<T>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}
}
template <typename T, typename Q>
void input_arr(vector<pair<T, Q>>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i].first >> A[i].second;
	}
}
template <typename T>
void input_arr(vector<vector<T>>& A, ll H, ll W) {
	for (ll i = 0; i < H; i++) {
		for (ll j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
}
vector<P> RLE(vector<ll>&A) {
	ll N = (ll)A.size();
	vector<P>B; B.push_back(make_pair(A[0], 1));
	for (ll i = 1; i < N; i++) {
		if (A[i] != A[i - 1]) {
			B.push_back(make_pair(A[i], 1));
		}
		else B.back().second++;
	}
	return B;
}
int main() {
	ll N; cin >> N;
	while (N != 0) {
	vector<ll>A(N); input_arr(A, N);
	vector<P>B = RLE(A);
	vector<P>pos;
	ll num = -1;
	for (ll i = 0; i < B.size(); i++) {
		if (B[i].second != 3) {
			num += B[i].second;
			continue;
		}
		if (i != 0) pos.push_back({num, B[i].first});
		if (i != B.size() - 1) pos.push_back({num + 4, B[i].first});
		num += 3;
	}
	ll ans = 0;
	for (ll k = 0; k < pos.size(); k++) {
		vector<ll>D = A;
		D[pos[k].first] = pos[k].second;
		vector<P>C = RLE(D);
		ll pos = 0;
		for (ll i = 0; i < C.size(); i++) {
			if (C[i].second >= 4) {
				pos = i;
				break;
			}
		}
		ll m = 1, sum = C[pos].second;
		while (pos - m >= 0 && pos + m < C.size()) {
			if (C[pos - m].first == C[pos + m].first && C[pos - m].second + C[pos + m].second >= 4) {
				sum += C[pos - m].second + C[pos + m].second;
			}
			else break;
			m++;
		}
		ans = max(ans, sum);
	}
	cout << N - ans << endl;
	cin >> N;
	}
}
accuracy: 1
time_ms: 160
memory_kb: 3752
final_rank: 9

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

static const int INF = 1e9;

int erace(int n, vector<int> v, int idx, int c) {
	int n_tmp = n;
	int l = idx;
	int r = idx + 1;
	int color = c;
	while (1) {
		int cnt = 0;
		while (0 <= l && v[l] == color) {
			cnt++;
			l--;
		}
		while (r < n && v[r] == color) {
			cnt++;
			r++;
		}
		if (cnt >= 4) {
			n_tmp -= cnt;
			if (0 <= l && r < n && v[l] == v[r]) {
				color = v[l];
			}
			else break;
		}
		else {
			break;
		}
	}
	return n_tmp;
}

int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

	while (1) {
		int n;
		cin >> n;
		if (n == 0) return 0;
		vector<int> v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		int ans = INF;
		for (int i = 0; i < n; i++) {
			int tmp = v[i];
			for (int j = 1; j < 4; j++) {
				if (tmp == j) continue;
				v[i] = j;
				ans = min(ans, erace(n, v, i, j));
			}
			v[i] = tmp;
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3072
final_rank: 2

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

static const int INF = 1e9;

int erace(int n, vector<int> v, int num, int c) {
	int n_tmp = n;
	int l = num;
	int r = num + 1;
	int color = c;
	while (1) {
		int cnt = 0;
		while (0 <= l && v[l] == color) {
			cnt++;
			l--;
		}
		while (r < n && v[r] == color) {
			cnt++;
			r++;
		}
		if (cnt >= 4) {
			n_tmp -= cnt;
			if (v[l] == v[r]) {
				color = v[l];
			}
			else break;
		}
		else {
			break;
		}
	}
	return n_tmp;
}

int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

	while (1) {
		int n;
		cin >> n;
		if (n == 0) return 0;
		vector<int> v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		int ans = INF;
		for (int i = 0; i < n; i++) {
			int tmp = v[i];
			for (int j = 1; j < 4; j++) {
				if (tmp == j) continue;
				v[i] = j;
				ans = min(ans, erace(n, v, i, j));
			}
			v[i] = tmp;
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3184
final_rank: 5

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

static const int INF = 1e9;

int erace(int n, vector<int> v, int num, int c) {//ボールの個数　ボール（数字）の配列　色を変えた場所 変えた色の番号
	int n_tmp = n;//残っているボールの番号
	int l = num;//色変えた場所の番号
	int r = num + 1;
	int color = c;//変えた場所の数字の番号（色）
	while (1) {
		int cnt = 0;//同じ数字が連続している数
		//左側に何個同じ色が連続しているか
		while (0 <= l && v[l] == color) {
			cnt++;
			l--;//左端を伸ばす
		}
		//右側に何個同じ色が連続しているか
		while (r < n && v[r] == color) {
			cnt++;
			r++;//右端を伸ばす
		}
		if (cnt >= 4) {
			n_tmp -= cnt;//連続した数の分を消す
			if (v[l] == v[r]) {
				color = v[l];//次に消える色を更新する
			}
			else break;//消した後の両サイドが同じ色でなければ、それ以上消えることはない
		}
		else {
			break;//４つ消えなければ、それ以上消えることはない
		}
	}
	return n_tmp;
}

int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

	while (1) {
		int n;
		cin >> n;
		if (n == 0) return 0;
		vector<int> v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		int ans = INF;//初期化
		//全探索
		for (int i = 0; i < n; i++) {//色を変えるとこの番号
			int tmp = v[i];
			//色を変える
			for (int j = 1; j < 4; j++) {//色を変える
				if (tmp == j) continue;
				v[i] = j;
				ans = min(ans, erace(n, v, i, j));
			}
			v[i] = tmp;//ボールの色を元に戻す
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3156
final_rank: 4

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
static const int INF = 1e9;

int erace(int n, vector<int> v, int num, int c){//ボールの個数　ボール（数字）の配列　色を変えた場所 変えた色の番号
    int n_tmp = n;//残っているボールの番号
    int l = num;//色変えた場所の番号
    int r = num + 1;
    int color = c;//変えた場所の数字の番号（色）
    while(1){
        int cnt = 0;//同じ数字が連続している数
        //左側に何個同じ色が連続しているか
        while(0 <= l && v[l] == color){
            cnt++;
            l--;//左端を伸ばす
        }
        //右側に何個同じ色が連続しているか
        while(r < n && v[r] == color){
            cnt++;
            r++;//右端を伸ばす
        }
        if(cnt >= 4){
            n_tmp -= cnt;//連続した数の分を消す
            if(v[l] == v[r]){
                color = v[l];//次に消える色を更新する
            }
            else break;//消した後の両サイドが同じ色でなければ、それ以上消えることはない
        }else{
            break;//４つ消えなければ、それ以上消えることはない
        }
    }
    return n_tmp;
}

int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	
    while(1){
        int n; 
        cin >> n;
        if(n == 0) return 0;
        vector<int> v(n);
        rep(i, n) {
            cin >> v[i];
        }
        int ans = INF;//初期化
        //全探索
        rep(i, n){//色を変えるとこの番号
            int tmp = v[i];
            //色を変える
            reps(j, 1, 4){//色を変える
                if(tmp == j) continue;
                v[i] = j;
                ans = min(ans, erace(n, v, i, j));
            }
            v[i] = tmp;//ボールの色を元に戻す
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3152
final_rank: 3

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
static const int INF = 1e9;

int erace(int n, vector<int> v, int num, int c){//ボールの個数　ボール（数字）の配列　色を変えた場所 変えた色の番号
    int n_tmp = n;//残っているボールの番号
    int l = num;//色変えた場所の番号
    int r = num + 1;
    int color = c;//変えた場所の数字の番号（色）
    while(1){
        int cnt = 0;//同じ数字が連続している数
        //左側に何個同じ色が連続しているか
        while(0 <= l && v[l] == color){
            cnt++;
            l--;//左端を伸ばす
        }
        //右側に何個同じ色が連続しているか
        while(r < n && v[r] == color){
            cnt++;
            r++;//右端を伸ばす
        }
        if(cnt >= 4){
            n_tmp -= cnt;//連続した数の分を消す
            if(v[l] == v[r]){
                color = v[l];//次に消える色を更新する
            }
            else break;//消した後の両サイドが同じ色でなければ、それ以上消えることはない
        }else{
            break;//４つ消えなければ、それ以上消えることはない
        }
    }
    return n_tmp;
}

int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	
    while(1){
        int n; 
        cin >> n;
        if(n == 0) return 0;
        vector<int> v(n);
        rep(i, n) {
            cin >> v[i];
        }
        int ans = INF;//初期化
        //全探索
        rep(i, n){//色を変えるとこの番号
            int tmp = v[i];
            //色を変える
            reps(j, 1, 4){//色を変える
                if(tmp == j) continue;
                v[i] = j;
                ans = min(ans, erace(n, v, i, j));
            }
            v[i] = tmp;//ボールの色を元に戻す
        }
        printf("%d\n", ans);
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3340
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template<typename T>
vector<pair<T, int>> run_length_encode(vector<T> &vec){
    vector<pair<T, int>> res;
    for(const T& a: vec){
        if(res.empty() or res.back().first!=a){
            res.emplace_back(a, 0);
        }
        res.back().second++;
    }
    return res;
}

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;
        
        vector<int> a(n);
        for(int &i: a){
            cin >> i;
        }
        int ans = n;
        for(int i=0; i<n; i++){
            int tmp = a[i];
            vector<int> ch;
            if(i > 0){
                ch.push_back(a[i-1]);
            }
            if(i < n-1){
                ch.push_back(a[i+1]);
            }
            if(ch.size()==2u and ch[0]==ch[1]){
                ch.pop_back();
            }
            for(int j: ch){
                a[i] = j;
                auto b = run_length_encode(a);
                vector<pair<int,int>> c;
                c.reserve(b.size());
                for(auto p: b){
                    if(!c.empty() and c.back().first==p.first){
                        c.back().second += p.second;
                    }else{
                        c.push_back(p);
                    }
                    while(!c.empty() and c.back().second >= 4){
                        c.pop_back();
                    }
                }
                int sub = 0;
                for(auto p: c){
                    sub += p.second;
                }
                ans = min(ans, sub);
            }
            a[i] = tmp;
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 820
memory_kb: 3344
final_rank: 15

code_snippet: #include<iostream>

using namespace std;

int main()
{
	int N;
	cin >> N;
	while (N)
	{
		int* a = new int[N + 2];
		a[0] = 0;
		a[N + 1] = 9;
		for (int i = 1; i <= N; i++)
			cin >> a[i];
		int* tmp = new int[N + 2];
		int ans = 0;
		for (int i = 1; i <= N; i++)
		{
			for (int j = 1; j <= 2; j++)
			{
				int _ans = 0;
				copy(a, a + N + 2, tmp);
				int color = a[i] + j;
				if (color > 3) color -= 3;
				tmp[i] = color;
				int up = i, down = i - 1;
				while (true)
				{
					int counter = 0;
					while (tmp[up] == color) { up++; counter++; }
					while (tmp[down] == color) { down--; counter++; }
					if (counter < 4)
						break;
					_ans += counter;
					if (tmp[up] != tmp[down]) 
						break;
					color = tmp[up];
				}
				ans = max(ans, _ans);
			}
		}
		ans = N - ans;
		cout << ans << endl;
		cin >> N;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 3196
final_rank: 6

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int shoukyo(int*,int);
int nokori;
bool check(int*,int);
void hyouji(int*);

int main(){
	int a[10000],b[10000];
	int n,i,j,k,l;
	int chP,chC;
	int mini;
	
	while(1){
		
		cin>>n;
		if(n==0){break;}
		nokori=n;
		for(i=n-1;i>=0;i--){
		cin>>b[i];
	}
	//hyouji(b);
		mini=n;
		
		
		
		
		for(j=0;j<n-1;j++){
			for(i=0;i<n;i++){
				a[i]=b[i];
			}
		
			nokori=n;
			a[j]=a[j+1];
			k=1;
			while(k==1){
				if(nokori<4){break;}
				for(int i=0;i<nokori-3;i++){
					k=check(a,i);
					if(k==1){i=0;}
				}
			}
			if(nokori<mini){
				mini=nokori;
			}
		}
		for(j=1;j<n;j++){
			for(i=0;i<n;i++){
				a[i]=b[i];
			}
			nokori=n;
			a[j]=a[j-1];
			k=1;
			while(k==1){
				if(nokori<4){break;}
				for(int i=0;i<nokori-3;i++){
					k=check(a,i);
				}
			}
			if(nokori<mini){mini=nokori;}
		}
		cout<<mini<<endl;
				
	
	}
	return 0;
}

int shoukyo(int a[],int p){
	int k=4;
	while(k+p<nokori&&a[p]==a[k+p]){
		k++;
	}
	
	
	for(int i=p;i<nokori-k;i++){
		a[i]=a[i+k];
		
	}
	nokori=nokori-k;


	
	
	return 0;
}


bool check(int a[],int i){
	if(abs(a[i]-a[i+1])+abs(a[i+1]-a[i+2])+abs(a[i+2]-a[i+3])==0){
		//hyouji(a);
		shoukyo(a,i);
		return 1;
	}
	return 0;
}

void hyouji(int a[]){
	for(int i=0;i<nokori;i++){
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return;
}
accuracy: 1
time_ms: 700
memory_kb: 3088
final_rank: 12

code_snippet: #include <iostream>
#include <vector>

std::vector<std::pair<int, int>> st;

bool solve() {
    int n;
    std::cin >> n;
    if (n == 0) return false;

    std::vector<int> cs(n);
    for (auto& c : cs) std::cin >> c;

    int ans = n;
    for (int i = 0; i < n; ++i) {
        int pc = cs[i];

        for (int nc = 1; nc <= 3; ++nc) {
            if (i != 0 && nc == pc) continue;
            cs[i] = nc;

            st.emplace_back(0, 0);
            for (auto c : cs) {
                if (st.back().first != c &&
                    st.back().second >= 4) {
                    st.pop_back();
                }

                if (st.back().first == c) {
                    ++st.back().second;
                } else {
                    st.emplace_back(c, 1);
                }
            }

            if (st.back().second >= 4) st.pop_back();

            int ret = 0;
            while (!st.empty()) {
                ret += st.back().second;
                st.pop_back();
            }

            ans = std::min(ans, ret);
        }
        cs[i] = pc;
    }

    std::cout << ans << "\n";
    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while (solve()) {}

    return 0;
}
accuracy: 1
time_ms: 860
memory_kb: 3284
final_rank: 16

code_snippet: #include <bits/stdc++.h>

//    #include <boost/multiprecision/cpp_int.hpp>
 #define int long long
 #define inf  1000000007
 #define pa pair<int,int>
 #define ll long long
 #define pal pair<double,double>
 #define ppap pair<pa,int>
//  #define PI 3.14159265358979323846
  #define paa pair<int,char>
  #define  mp make_pair
  #define  pb push_back
  #define EPS (1e-10)
                                          
    int dx[8]={0,-1,0,1,1,1,-1,-1};
    int dy[8]={1,0,-1,0,-1,1,1,-1};
                                            using namespace std;
                                   			class pa3{
                                            	public:
                                            	int x,y,z;
                                            	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                            	bool operator < (const pa3 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		 return z<p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                   				bool operator > (const pa3 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		 return z>p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa3 &p) const{
                                            		return x==p.x && y==p.y && z==p.z;
                                            	}
                                            		bool operator != (const pa3 &p) const{
                                            			return !( x==p.x && y==p.y && z==p.z);
                                            	}
                                            
                                            };
                                            
                                            class pa4{
                                            	public:
                                            	double x;
                                            	int y,z,w;
                                            	pa4(double x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                            	bool operator < (const pa4 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		if(z!=p.z)return z<p.z;
                                            		return w<p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator > (const pa4 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		if(z!=p.z)return z>p.z;
                                            		return w>p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa4 &p) const{
                                            		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                            	}
                                            		
                                            
                                            };
                                            class pa2{
                                            	public:
                                            	int x,y;
                                            	pa2(int x=0,int y=0):x(x),y(y) {}
                                            	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                            	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                            	bool operator < (const pa2 &p) const{
                                            		return y != p.y ? y<p.y: x<p.x;
                                            	}
                                            	bool operator > (const pa2 &p) const{
                                            		return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa2 &p) const{
                                            		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                            	}
                                            	bool operator != (const pa2 &p) const{
                                            		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                            	}
                                            		
                                            
                                            };
                                            
/*
                                            class Point{
                                            	public:
                                            	double x,y;
                                            	Point(double x=0,double y=0):x(x),y(y) {}
                                            	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
                                            	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
                                            	Point operator * (double a) {return Point(x*a,y*a);}
                                            	Point operator / (double a) {return Point(x/a,y/a);}
                                            	double absv() {return sqrt(norm());}
                                            	double norm() {return x*x+y*y;}
                                            	bool operator < (const Point &p) const{
                                            		return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const Point &p) const{
                                            		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
                                            	}
                                            };
                                            typedef Point Vector;
                                     #define pl pair<int,pas>
                                            struct Segment{
                                            Point p1,p2;
                                            };
                                             double dot(Vector a,Vector b){
                                            	return a.x*b.x+a.y*b.y;
                                            }
                                            double cross(Vector a,Vector b){
                                            	return a.x*b.y-a.y*b.x;
                                            }
                                        
                bool parareru(Point a,Point b,Point c,Point d){
                //	if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
                	return abs(cross(a-b,d-c))<EPS;
                }
                double distance_ls_p(Point a, Point b, Point c) {
                  if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
                  if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
                  return abs(cross(b-a, c-a)) / (b-a).absv();
                }
                bool is_intersected_ls(Segment a,Segment b) {
                	if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false;
                	if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){
                //		cout<<"sss"<<endl;
                		if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
                		if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
                		if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
                		if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
                		return false;
                	}
                  else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
                }
                 
                double segment_dis(Segment a,Segment b){
                	if(is_intersected_ls(a,b))return 0;
                	double r=distance_ls_p(a.p1, a.p2, b.p1);
                	r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
                	r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
                	r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
                	return r;
                }
                Point intersection_ls(Segment a, Segment b) {
                  Point ba = b.p2-b.p1;
                  double d1 = abs(cross(ba, a.p1-b.p1));
                  double d2 = abs(cross(ba, a.p2-b.p1));
                  double t = d1 / (d1 + d2);
                 
                  return a.p1 + (a.p2-a.p1) * t;
                }
 */                
                                string itos( int i ) {
                                ostringstream s ;
                                s << i ;
                                return s.str() ;
                                }
                                 
                                int gcd(int v,int b){
                                	if(v>b) return gcd(b,v);
                                	if(v==b) return b;
                                	if(b%v==0) return v;
                                	return gcd(v,b%v);
                                }
                 
                                double distans(double x1,double y1,double x2,double y2){
                                	double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
                                	return sqrt(rr);
                                	
                                }
                                int mod;
                                
                int pr[500010];
                int inv[500010];
                
                int beki(int wa,int rr,int warukazu){
                	if(rr==0) return 1%warukazu;
                	if(rr==1) return wa%warukazu;
                	if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;
                	int zx=beki(wa,rr/2,warukazu);
                	return (zx*zx)%warukazu;
                }
    double bekid(double w,int r){
    	if(r==0) return 1.0;
    	if(r==1) return w;
    	if(r%2) return bekid(w,r-1)*w;
    	double f=bekid(w,r/2);
    	return f*f;
    }
                
    			int comb(int nn,int rr){
    				int r=pr[nn]*inv[rr];
    				r%=mod;
    				r*=inv[nn-rr];
    				r%=mod;
    				return r;
    			}
                
                void gya(int ert){
                	pr[0]=1;
                	for(int i=1;i<ert;i++){
                		pr[i]=(pr[i-1]*i)%mod;
                	}
                	for(int i=0;i<ert;i++) inv[i]=beki(pr[i],mod-2,mod);
                	
                }
                
              //   cin.tie(0);
    		//	ios::sync_with_stdio(false);
    			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
                 //sort(ve.begin(),ve.end(),greater<int>());
                                         //----------------kokomade tenpure------------
                //vector<double> ans(100000000),ans2(100000000)
      
int a[100020];
int n;

int solve(){
	vector<pa> ve;
	
	ve.pb(mp(a[0],1));
	for(int i=1;i<n;i++){
		if(ve.back().first==a[i]) ve.back().second++;
		else ve.pb(mp(a[i],1));
	}
	int l=ve.size();
	int d=-1;
	for(int i=0;i<l;i++){
		if(ve[i].second>=4) d=i;
	}
	if(d==-1) return n;
	int noz=ve[d].second;
	int k=1;
	while(1){
		if(d-k<0|| d+k>=l) break;
		if(ve[d-k].first!=ve[d+k].first)break;
		if(ve[d-k].second+ve[d+k].second<=3)break;
		noz+=ve[d-k].second+ve[d+k].second;
		k++;
	}
	return n-noz;
}

  signed main(){
    	       cin.tie(0);
    			ios::sync_with_stdio(false);
  	while(1){
cin>>n;
  		if(n==0) break;
  	for(int i=0;i<n;i++)cin>>a[i];
  	int ans=n;
  	for(int i=0;i<n;i++){
  		if(i>0 && a[i]!=a[i-1]){
  			int t=a[i];
  			a[i]=a[i-1];
  			ans=min(ans,solve());
  			a[i]=t;
  		}
  		if(i+1<n && a[i]!=a[i+1]){
  			int t=a[i];
  			a[i]=a[i+1];
  			ans=min(ans,solve());
  			a[i]=t;
  		}
  		
  	}
  	cout<<ans<<endl;
  	}
  	return 0;
    }
accuracy: 1
time_ms: 730
memory_kb: 3316
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;


int right(int iro[],int kazu[],int m,int len){
	int l=1;int r=3;
	while(true){
		if(iro[m-l]==iro[m+r] && 4<=kazu[m-l]+kazu[m+r]){
			l+=1;
			r+=1;
		}
		else{
			break;
		}
	}
	int w=0;
	for(int n=0;n<=m-l;n++){
		w+=kazu[n];
	}
	for(int aa=m+r;aa<=len;aa++){
		w+=kazu[aa];
	}
	return w;
}



int left(int iro[],int kazu[],int m,int len){
	int l=3;int r=1;
	while(true){
		if(iro[m-l]==iro[m+r]&&4<=kazu[m-l]+kazu[m+r]){
			l+=1;
			r+=1;
		}
		else{
			break;
		}
	}
	int w=0;
	for(int n=0;n<=m-l;n++){
		w+=kazu[n];
	}
	for(int aa=m+r;aa<=len;aa++){
		w+=kazu[aa];
	
	}
	return w;
}





int thright(int iro[],int kazu[],int m,int len){
	kazu[m]+=1;
	kazu[m+1]-=1;
	int l=1;
	int r=1;
	int w=0;
	while(true){
		if(iro[m-l]==iro[m+r]&&4<=kazu[m-l]+kazu[m+r]){
			l+=1;
			r+=1;
		}
		else{
			break;
		}
	}
	for(int n=0;n<=m-l;n++){
		w+=kazu[n];
	}
	for(int aa=m+r;aa<=len;aa++){
		w+=kazu[aa];
	}
	kazu[m]-=1;
	kazu[m+1]+=1;
return w;
}


int thleft(int iro[],int kazu[],int m,int len){
	kazu[m]+=1;
	kazu[m-1]-=1;
	int l=1;
	int r=1;
	int w=0;
	while(true){
		if(iro[m-l]==iro[m+r]&&4<=kazu[m-l]+kazu[m+r]){
			l+=1;
			r+=1;
		}
		else{
			break;
		}
	}
	for(int n=0;n<=m-l;n++){
		w+=kazu[n];
	}
	for(int aa=m+r;aa<=len;aa++){
		w+=kazu[aa];
	}
	kazu[m]-=1;
	kazu[m-1]+=1;
return w;
}




int main(){
	while(true){
		
		int kazu[10000]={};
		int iro[10000]={};
		int a,b,c;
		int len=1;
		cin >> a;
		if(a==0){
			break;
		}
		cin >> c;
		int z=0;
		iro[0]={c};
		kazu[0]={1};
		for(int n=0;n<a-1;n++){
			cin >> b;
			if(c!=b){
				z+=1;
				iro[z]={b};
				c=b;
				len+=1;
			}
			kazu[z]+=1;
		}
		int anser=a;
		for(int m=0;m<len;m++){
			if(kazu[m]>=2){
				if(m!=len&&iro[m]==iro[m+2]&&kazu[m+1]==1){
					anser=min(anser,right(iro,kazu,m,len));
				}
				if(m!=0&&iro[m]==iro[m-2]&&kazu[m-1]==1){
					anser=min(anser,left(iro,kazu,m,len));
				}
				if(kazu[m]==3){
					if(m!=len){
						anser=min(anser,thright(iro,kazu,m,len));
					}
					if(m!=0){
						anser=min(anser,thleft(iro,kazu,m,len));
					}
				}
			}	
		}
		printf("%d\n",anser);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3196
final_rank: 1