Sasha and One More Name
Contest: Codeforces Round 539 (Div. 1)
Difficulty rating: 1800
Tags: constructive algorithms, hashing, strings

Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not."

And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not.

Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times $$$k$$$, so they got $$$k+1$$$ pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.

In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using $$$3$$$ cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.

More formally, Sasha wants for the given palindrome $$$s$$$ find such minimum $$$k$$$, that you can cut this string into $$$k + 1$$$ parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string $$$s$$$. It there is no answer, then print "Impossible" (without quotes).

Input format:
The first line contains one string $$$s$$$ ($$$1 \le |s| \le 5\,000$$$) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that $$$s$$$ is a palindrome.

Output format:
Print one integer $$$k$$$ — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.

In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.

In the third example, you can't make a string, that won't be equal to the initial one.

In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.

Example input:
nolon

Example output:
2

Example input:
otto

Example output:
1

Editorial:
Let's $$$s$$$ be the given string, $$$n$$$  — it's length. If $$$s$$$ consists of $$$n$$$ or $$$n - 1$$$ (when $$$n$$$ is odd) equal characters, then there is no way to get the answer. Otherwise, let's prove that the answer can be always reached in two cuttings. Let's the longest prefix of $$$s$$$, that consists of equal characters has the length equal to $$$len - 1$$$. Cut $$$pref = s[1 \ldots len]$$$ and $$$suff = s[n-len+1 \ldots n]$$$, and call the remaining piece as $$$mid$$$. Swap $$$pref$$$ and $$$suff$$$, then unite all three parts together. The central part ($$$mid$$$) will stay unchanged, $$$pref \neq reverse(pref)$$$ and $$$pref \neq suff$$$ then $$$pref + mid + suf \neq suf + mid + pref$$$. So now we can get the answer in two cuttings. Finally you must chech if it is possible to get the result by making just one cutting. As soon as one cutting is equal to some cyclic shift, then our task is to check if there is a cyclic shift which is a palindrome and not equal to $$$s$$$. It can be done by fixing each cyclic shift and checking each one separately.
Complexity: $$$O(n^2)$$$
Bonus: Try to solve this task in linear time without using any string algorithm.