Baseball Watching

Each of N students sits in one of three sections for nine innings; find the min and max number of students a moving teacher can avoid catching.

Hard8Dynamic programmingBit manipulationBrute forceNo attempts yetTime limit1sMemory limit256 MB

Problem

There is a professional baseball park near the school. When evening study hall starts, the students who like baseball slip out to the park and watch the game without the teachers noticing.

This time NN students go to the game. The stands have three areas, the first base cheering section, the outfield seats, and the third base cheering section, numbered 1, 2, and 3. A game runs for nine innings, and you may assume that a tie never sends it into extra innings.

The literature teacher also comes to the park to catch them. A student sitting in the same area as the teacher during an inning is caught in that inning and does not watch the game to the end. After each inning both a student and the teacher may move to any of the three areas, and either one may stay where they are.

The NN students fixed in advance where they sit in each of the nine innings, no matter where the teacher is. Nobody knows in which order the teacher moves during the nine innings. Considering every way the teacher can move, find the smallest and the largest possible number of students who are never caught and watch the game to the end.

Input

The first line contains the number of students NN. (2N3000002 \le N \le 300000)

Each of the next NN lines contains nine area numbers separated by spaces, the areas one student sits in from inning 1 to inning 9. 1 means the first base cheering section, 2 means the outfield seats, and 3 means the third base cheering section.

Output

On the first line, print the smallest and the largest number of students who watch the game to the end, separated by a space.

Hint

In the first example, if the teacher stays in the first base cheering section for all nine innings, students 1, 4, and 7 are caught in inning 1, student 3 in inning 2, student 2 in inning 3, student 6 in inning 4, and student 5 in inning 7. Not a single student watches the game to the end.

If the teacher moves through the third base cheering section, the first base cheering section, the third base cheering section, the first base cheering section, the outfield seats, the outfield seats, the third base cheering section, the outfield seats, and the outfield seats, then students 2, 5, and 7 watch the game to the end. No route the teacher can take lets four or more students watch the game to the end.