// Arup Guha
// 2/26/2023
// Solution to 2022 SER D2 Problem: Everything is a Nail

using namespace std;

#include <iostream>
#include <vector>

#define max(a,b) (a)>(b)?(a):(b)

// Easier to do this...
const int PERM[6][3] = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};

int main() {

    // Read sequence.
    int n;
    vector<int> inp;
    cin >> n;
    for (int i=0; i<n; i++) {
        int tmp;
        cin >> tmp;
        inp.push_back(tmp);
    }

    int res = 0;

    // i is ordering we are trying.
    for (int x=0; x<6; x++) {

        // dp[i][j] = best score for i characters ending at j.
        int cur = 0;
        int** dp = new int*[3];
        for (int i=0; i<3; i++) dp[i] = new int[n+1];
        dp[0][0] = dp[1][0] = dp[2][0] = 0;

        // Loop through characters.
        for (int j=0; j<n; j++) {

            // What we're building off of.
            int prevV = 0;

            // What we're looking for as ending char.
            for (int i=0; i<3; i++) {

                // Best previous item to build off of.
                prevV = max(prevV, dp[i][j]);

                // See if this matches.
                int val = inp[j];
                int match = (val == PERM[x][i]) ? 1 : 0;

                // Update result.
                dp[i][j+1] = prevV + match;
            }
        }

        // Update res.
        for (int i=0; i<3; i++) res = max(res, dp[i][n]);

        // Free stuff.
        for (int i=0; i<3; i++) delete [] dp[i];
        delete [] dp;
    }

    // Ta da!
    cout << res << endl;

    return 0;
}
