Problem: We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.

You will do the following operation N-1 times:

* Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.



After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.

Find the maximum number of points you can gain.

Constraints

* 1 \leq N \leq 2000
* 1 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:


N
A_1 A_2 \cdots A_{3N}


Output

Print the maximum number of points you can gain.

Examples

Input

2
1 2 1 2 2 1


Output

2


Input

3
1 1 2 2 3 3 3 2 1


Output

1


Input

3
1 1 2 2 2 3 3 3 1


Output

3

Solution (in CPP):

Solution:
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>


template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353LL;

using namespace std;
typedef long long ll;


using Tp = tuple<int, int, int>;
vector<Tp> update;
vector<vector<int>> dp;
vector<int> mx;
int allmax;
void st(int i, int j, int k) {
	update.emplace_back(i, j, k);
	update.emplace_back(j, i, k);
}

void upd() {
	for (auto [a, b, c] : update) {
		chmax(dp[a][b], c);
		chmax(mx[a], c);
		chmax(allmax, c);
	}
	update.clear();
}
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int n; cin >> n;
	vector<int> a(n * 3); for (int i = 0; i < n * 3; i++) cin >> a[i], a[i]--;
	dp.assign(n, vector<int>(n, -1));
	mx.assign(n, -1);
	dp[a[0]][a[1]] = dp[a[1]][a[0]] = mx[a[0]] = mx[a[1]] = 0;
	int pl = 0;
	for (int i = 2; i + 2 < 3 * n; i += 3) {
		if (a[i] == a[i + 1] and a[i + 1] == a[i + 2]) {
			pl++;
			continue;
		}
		for (int kkt = 0; kkt < 3; kkt++) {
			vector<int> v;
			int r = a[i + kkt];
			for (int j = 0; j < 3; j++) if (j != kkt) v.emplace_back(a[i + j]); 
			st(v[0], v[1], allmax);
			if(dp[r][r] != -1) st(v[0], v[1], dp[r][r] + 1);
			if (v[0] == v[1]) {
				for (int j = 0; j < n; j++) if (dp[v[0]][j] != -1) st(r, j, dp[v[0]][j] + 1);
			}
			for (int j = 0; j < n; j++) st(r, j, mx[j]);
		}
		upd();
	}
	int res = 0; 
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
		if (i == j and i == a.back()) {
			chmax(res, dp[i][j] + 1);
		}
		else {
			chmax(res, dp[i][j]);
		}
	}
	res += pl;
	cout << res << endl;
}