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

using i64 = int64_t;

#define rep(i, x, y) for (i64 i = i64(x), i##_max_for_repmacro = i64(y); i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "[";
    for (const auto &v : vec) {
        os << v << ",";
    }
    os << "]";
    return os;
}

template <typename T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

i64 solve2(i64 L,vector<i64> A){
    vector<i64> even(L),odd(L);
    even[0]=(A[0]+1)%2;
    odd[0]=A[0]%2;
    rep(i,1,L){
        even[i]=even[i-1];
        even[i]+=(A[i]+1)%2;

        odd[i]=odd[i-1];
        odd[i]+=A[i]%2;
    }

    vector<i64> s(L);
    s[0]=A[0];
    rep(i,1,L){
       s[i]=s[i-1]+A[i];
    }

    auto sum=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=s[r];
        if(l-1>=0) res-=s[l-1];
        return res;
    };

    auto even_sum=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=even[r];
        if(l-1>=0) res-=even[l-1];
        return res;
    };

    auto odd_sum=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=odd[r];
        if(l-1>=0) res-=odd[l-1];
        return res;
    };

    i64 ans=even_sum(0,L-1);
    rep(i,0,L){
        rep(j,i,L){
            chmin(ans,odd_sum(0,i-1)+even_sum(i,j)+odd_sum(j+1,L-1));
            print(i);
            print(j);
            print(ans);
        }
    }

    return ans;
}

void solve() {
    //const i64 mod = 1'000'000'007;
    i64 L;
    cin >> L;

    vector<i64> A(L);
    rep(i,0,L){
        cin >> A[i];
    }

    vector<i64> even(L),odd(L);
    even[0]=(A[0]+1)%2;
    odd[0]=A[0]%2;
    rep(i,1,L){
        even[i]=even[i-1];
        even[i]+=(A[i]+1)%2;

        odd[i]=odd[i-1];
        odd[i]+=A[i]%2;
    }

    vector<i64> s(L);
    s[0]=A[0];
    rep(i,1,L){
        s[i]=s[i-1]+A[i];
    }

    auto sum=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=s[r];
        if(l-1>=0) res-=s[l-1];
        return res;
    };

    vector<i64> z(L);
    z[0]=A[0]==0;
    rep(i,1,L){
        z[i]=z[i-1]+(A[i]==0);
    }

    auto zero=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=z[r];
        if(l-1>=0) res-=z[l-1];
        return res;
    };

    auto even_sum=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=even[r];
        if(l-1>=0) res-=even[l-1];
        return res;
    };

    auto odd_sum=[&](i64 l,i64 r){
        if(r<l) return i64(0);
        i64 res=odd[r];
        if(l-1>=0) res-=odd[l-1];
        return res;
    };

    const i64 maxL=200000;
    static i64 memo[maxL][5];
    static bool done[maxL][5];
    fill_n((i64*)memo,maxL*5,inf64);
    fill_n((bool*)done,maxL*5,false);
    // 0 even odd even 0
    // 0 1    2   3    4
    function<i64(i64,i64)> rec=[&](i64 i,i64 s){
        if(i==L) return i64(0);

        auto& res=memo[i][s];
        if(done[i][s]){
            return res;
        }
        done[i][s]=true;

        switch(s){
            case 0: {
                {
                    // 0 -> 0
                    chmin(res, rec(i + 1, 0) + A[i]);
                }
                {
                    // 0 -> even
                    chmin(res, rec(i + 1, 1) + (A[i]==0?2:A[i] % 2));
                }
                {
                    // 0 -> odd
                    chmin(res, rec(i + 1, 2) + (A[i] + 1) % 2);
                }
            }
            break;

            case 1: {
                {
                    // even -> even
                    chmin(res, rec(i + 1, 1) + (A[i]==0?2:A[i] % 2));
                }
                {
                    // even -> odd
                    chmin(res,rec(i+1,2)+(A[i]+1)%2);
                }
                {
                    // even -> 0
                    chmin(res,rec(i+1,4)+A[i]);
                }
            }
            break;

            case 2: {
                {
                    // odd -> odd
                    chmin(res,rec(i+1,2)+(A[i]+1)%2);
                }
                {
                    // odd -> even
                    chmin(res,rec(i+1,3)+(A[i]==0?2:A[i] % 2));
                }
                {
                    // odd -> 0
                    chmin(res,rec(i+1,4)+A[i]);
                }
            }
            break;

            case 3: {
                {
                    // even -> even
                    chmin(res,rec(i+1,3)+(A[i]==0?2:A[i] % 2));
                }
                {
                    // even -> 0
                    chmin(res,rec(i+1,4)+A[i]);
                }
            }
            break;

            case 4: {
                {
                    // 0 -> 0
                    chmin(res, rec(i+1,4)+A[i]);
                }
            }
            break;

            default:
                assert(false);
        }

        return res;
    };

    cout << rec(0,0) << endl;
}

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}


Measured memory_bytes: 63872