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

vector<ll> f(const vector<ll> &a) {
	const int n = (int) a.size();
	vector<ll> dp(n + 1, 0);
	ll sum = 0;
	for (int i = 1; i <= n; ++i) {
		const ll x = a[i - 1];
		sum += x;
		dp[i] = min(sum, dp[i - 1] + (x - 2 < 0 ? 2 - x : x & 1));
	}
	return dp;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n;
	cin >> n;
	vector<ll> a(n);
	for (ll &x : a)
		cin >> x;

	vector<ll> dp = f(a);

	for (int i = 1; i <= n; ++i) {
		const ll x = a[i - 1];
		dp[i] = min(dp[i], dp[i - 1] + (x == 0 ? 1 : (x - 1) & 1));
	}

	reverse(a.begin(), a.end());
	vector<ll> dp2 = f(a);

	ll ans = 1LL << 60;
	for (int i = 0; i <= n; ++i) {
		ans = min(ans, dp[i] + dp2[n - i]);
	}
	cout << ans;
	return 0;
}
