Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

Language:
Journey
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 152 Accepted: 10

Description

There is a robot who lives on a cartesian plane and likes to walk around it. One day he planned a very interesting journey around the plane. To make that journey he developed a program which he is going to follow. The program consists of n functions: f1, f2, ..., fn. The i-th function fi is a sequence of ci commands. Each command is of one of the following types:

• GO: Move forward one meter;

• LEFT: Turn 90 degrees to the left;

• RIGHT: Turn 90 degrees to the right;

• Fk: Follow the instructions of the function fk, then continue following the instructions of the current function.

The robot starts the journey at his home located at the point with coordinates (0, 0) following the instructions of the function f1.

For example, consider the following set of functions:

f1: GO F2 GO F2 GO F2
f2: F3 F3 F3 F3
f3: GO LEFT

The robot’s journey for that case is shown on the picture.

In some cases the journey of the robot might never end. Consider for example the set of instructions consisting of one function f1 that has the following commands: GO F1. In that case the robot keeps going forward and never stops.

The question that puzzles the robot now is how far from the home will he get during his journey. That is, consider the set of all points which the robot will visit. Find the maximum value of |x|+|y| among all those points. If there are points on the path of the robot with arbitrarily large values of |x|+|y|, output “Infinity”.

Input

The first line of the input file consists of an integer number n (1 ≤ n ≤ 100). The following n lines contain a description of the functions. The i-th line describes function fi. It consists of the number ci (1 ≤ ci ≤ 100) — the number of commands for function fi, followed by a description of each command.

Output

Output the maximum value of |x| + |y| among all points visited during the journey or “Infinity”.

Sample Input

```Sample Input #1

3
6 GO F2 GO F2 GO F2
4 F3 F3 F3 F3
2 GO LEFT

Sample Input #2

1
2 GO F1

Sample Input #3

4
2 GO F2
7 LEFT GO GO GO F3 LEFT F4
5 GO F4 RIGHT F2 RIGHT
7 GO GO LEFT LEFT GO LEFT GO```

Sample Output

```Sample Output #1

5

Sample Output #2

Infinity

Sample Output #3

13```

Source

[Submit]   [Go Back]   [Status]   [Discuss]