How strong are nonary numbers? You are given a nonary (base 9) non-negative integer consisting of the digits 0 through 8 as usual. However the number of digits in this number (with no leading zeros) is a prefect square.

Because of this, the number can be arranged in a square grid (with the reading order still preserved).

Example with 1480 (1125 base 10):

14
80


Now let every digit in such a nonary grid indicate a motion to another grid space (with periodic boundary conditions):

432
501
678


This is saying that

0 = stay still
1 = move right
2 = move right and up
3 = move up
...
8 = move right and down


So, if in the 1480 grid you start at the 4, you then move up (remember pbc) and left to the 8, which means you move right and down back to the 4, starting a cycle with period 2.

In general this process is continued until you get to a 0 or a cycle is noticed. (A 0 is considered a cycle with period 1.)

In the case of 1480, the period eventually reached at each of the 4 starting digits is 2 2 2 1 respectively. 

For a larger grid these numbers might be larger than 8, but we can still use them as "digits" in a new nonary number (simply the coefficients of 9^n as if they were digits):

2*9^3 + 2*9^2 + 2*9 + 1 = 1639 (base 10) = 2221 (base 9)


We will call this the strength of the original nonary number. So the strength of 1480 is 1639 (base 10) or, equivalently, 2221 (base 9).

Challenge

Write the shortest program that tells whether the strength of a nonary number is greater than, less than, or equal to the nonary number itself. (You do not necessarily need to compute the strength.)

The input will be a non-negative nonary number that contains a square number of digits (and no leading zeros besides the special case of 0 itself). It should come from the command line or stdin.

The output should go to stdout as:

G if the strength is larger than the original number (example: 1480 -&gt; strength = 2221)
E if the strength is equal to the original number (example: 1 -&gt; strength = 1)
L if the strength is less than the original number (example: 5 -&gt; strength = 1)


Fun Bonus Challenge:
Whats the highest input you can find that is equal to its strength? (Is there a limit?)
Python 296
Not actually too inefficient, it only checks as many digits as it needs to.
n=raw_input();s=int(len(n)**.5);t=0
for i in range(s**2):
    l=[]
    while i not in l:l.append(i);c=n[i];i=(i%s+(-1 if c in'456'else 1 if c in'218'else 0))%s+((i/s+(-1 if c in'432'else 1 if c in'678'else 0))%s)*s
    t=t*9+len(l)-l.index(i)
print'EGL'[cmp(t,long(n,9))]

As for numbers equal to their strength, I think the only solutions are, for every N x N square up to N=8 a square containing N in every space. My thinking is that since every number in a loop must be the same number (the length of the loop) each loop would have to be all in one direction. This of course means that the loop's size must be N (and each element must be N). I'm pretty sure that this logic can be applied to squares and loops of any size, meaning there are no squares equal to their strength other than the first 8.