using System;
using System.Collections.Generic;
using System.Linq;
using CompLib.Util;

public class Program
{
    int N;
    int[] A;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        A = sc.IntArray();
        for (int i = 0; i < 3 * N; i++)
        {
            A[i]--;
        }

        // i,jが残ってる最大
        var dp = new int[N, N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dp[i, j] = int.MinValue;
            }
        }


        int cnt = 0;
        dp[A[0], A[1]] = 0;
        dp[A[1], A[0]] = 0;

        // dp[i,j]の最大値
        int max = 0;

        // dp[i]の最大値
        int[] max2 = new int[N];
        for (int i = 0; i < N; i++)
        {
            max2[i] = int.MinValue;
        }

        max2[A[0]] = 0;
        max2[A[1]] = 0;

        for (int i = 0; i < N - 1; i++)
        {
            // 更新
            var ls = new List<(int j, int k, int v)>();
            int[] p = new int[] { A[i * 3 + 2], A[i * 3 + 3], A[i * 3 + 4] };

            // 3つとも同じ
            if (p[0] == p[1] && p[1] == p[2])
            {
                cnt++;
                continue;
            }

            for (int t = 0; t < 3; t++)
            {
                Swap(ref p[0], ref p[t]);
                // 2ついっしょ
                if (p[1] == p[2])
                {
                    for (int k = 0; k < N; k++)
                    {
                        if (dp[p[1], k] != int.MinValue)
                        {
                            ls.Add((k, p[0], dp[p[1], k] + 1));
                            ls.Add((p[0], k, dp[p[1], k] + 1));
                        }
                    }
                }

                // p[0]と現在の2つで作る
                if (dp[p[0], p[0]] != int.MinValue)
                {
                    ls.Add((p[1], p[2], dp[p[0], p[0]] + 1));
                    ls.Add((p[2], p[1], dp[p[0], p[0]] + 1));
                }

                // 作らない 現在の2つ、p[0]を使う
                ls.Add((p[1], p[2], max));
                ls.Add((p[2], p[1], max));

                // 作らない p[1],p[2],前のやつの片方使う
                for (int k = 0; k < N; k++)
                {
                    if (max2[k] != int.MinValue)
                    {
                        ls.Add((p[0], k, max2[k]));
                        ls.Add((k, p[0], max2[k]));
                    }
                }

                // 作らない p3つ使う
                // 使い回す
            }

            // 更新
            foreach (var tuple in ls)
            {
                dp[tuple.j, tuple.k] = Math.Max(dp[tuple.j, tuple.k], tuple.v);
                max2[tuple.j] = Math.Max(max2[tuple.j], tuple.v);
                max = Math.Max(max, tuple.v);
            }
        }

        int ans = int.MinValue;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (dp[i, j] == int.MinValue) continue;
                var p = dp[i, j];
                if (i == j && j == A[3 * N - 1]) p++;
                ans = Math.Max(ans, p + cnt);
            }
        }

        Console.WriteLine(ans);
    }

    void Swap(ref int l, ref int r)
    {
        int t = l;
        l = r;
        r = t;
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


Measured memory_bytes: 117964