N = gets.to_i
A = gets.split.map(&:to_i)

dp = Array.new(N + 1) { Array.new(N + 1, -1) }
dp[A[0]][A[1]] = 0
idx = 0

add = 0
max = 0
ans = 0

1.upto(N - 1) do |i|
  a, b, c = A[3 * i - 1..3 * i + 1]

  if a == b && b == c
    add += 1
    next
  elsif a == b
    r = a
    q = c

    1.upto(N) do |k|
      if dp[k][r] >= 0 && dp[k][q] < dp[k][r] + 1
        dp[k][q] = dp[k][r] + 1
      end

      if dp[r][k] >= 0 && dp[k][q] < dp[r][k] + 1
        dp[k][q] = dp[r][k] + 1
      end

      ans = dp[k][q] if ans < dp[k][q]
    end
  elsif b == c
    r = b
    q = a

    1.upto(N) do |k|
      if dp[k][r] >= 0 && dp[k][q] < dp[k][r] + 1
        dp[k][q] = dp[k][r] + 1
      end

      if dp[r][k] >= 0 && dp[k][q] < dp[r][k] + 1
        dp[k][q] = dp[r][k] + 1
      end

      ans = dp[k][q] if ans < dp[k][q]
    end
  elsif a == c
    r = a
    q = b

    1.upto(N) do |k|
      if dp[k][r] >= 0 && dp[k][q] < dp[k][r] + 1
        dp[k][q] = dp[k][r] + 1
      end

      if dp[r][k] >= 0 && dp[k][q] < dp[r][k] + 1
        dp[k][q] = dp[r][k] + 1
      end

      ans = dp[k][q] if ans < dp[k][q]
    end
  else
    if dp[c][c] >= 0 && dp[a][b] < dp[c][c] + 1
      dp[a][b] = dp[c][c] + 1
      ans = dp[a][b] if ans < dp[a][b]
    end

    if dp[a][a] >= 0 && dp[b][c] < dp[a][a] + 1
      dp[b][c] = dp[a][a] + 1
      ans = dp[b][c] if ans < dp[b][c]
    end

    if dp[b][b] >= 0 && dp[a][c] < dp[b][b] + 1
      dp[a][c] = dp[b][b] + 1
      ans = dp[a][c] if ans < dp[a][c]
    end
  end

  1.upto(N) do |k|
    if dp[k][a] >= 0 && dp[k][a] < max
      dp[k][a] = max
    end

    if dp[k][b] >= 0 && dp[k][b] < max
      dp[k][b] = max
    end

    if dp[k][c] >= 0 && dp[k][c] < max
      dp[k][c] = max
    end

    if dp[a][k] >= 0 && dp[a][k] < max
      dp[a][k] = max
    end

    if dp[b][k] >= 0 && dp[b][k] < max
      dp[b][k] = max
    end

    if dp[c][k] >= 0 && dp[c][k] < max
      dp[c][k] = max
    end
  end

  if dp[a][b] < max
    dp[a][b] = max
  end

  if dp[b][c] < max
    dp[b][c] < max
  end

  if dp[a][c] < max
    dp[a][c] < max
  end

  max = ans if max < ans
end

l = A.last

if ans < dp[l][l] + 1
  ans = dp[l][l] + 1
end

puts ans + add
