import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Knumbers {
static int[] counts;
static char[] digits;
static int[][] allcounts;
static int pos;
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
sc = new Scanner(new File("src/file/knumbers"));
while (sc.hasNext()) {
counts = new int[10];
allcounts = new int[16][];
digits = sc.next().toCharArray();
if (digits[0] == '-')
break;
for (int i = 0; i < digits.length; i++) {
counts[digits[i] - 48]++;
}
allcounts[0] = counts;
if (isInvent(counts)) {
for (int i = 0; i < digits.length; i++) {
System.out.print(digits[i]);
}
System.out.println(" is self-inventorying ");
} else {
Inventorying(counts, 1);
}
}
}
private static void Inventorying(int[] c1, int step) {
// TODO Auto-generated method stub
if (step == 15) {
for (int i = 0; i < digits.length; i++) {
System.out.print(digits[i]);
}
System.out.println(" can not be classified after 15 iterations ");
return;
}
int[] c2 = new int[10];
for (int i = 0; i < c1.length; i++) {
if (c1[i] != 0) {
c2[i]++;
if (c1[i] >= 10) {
c2[c1[i] / 10]++;
c2[c1[i] % 10]++;
} else {
c2[c1[i]]++;
}
}
}
// System.out.println(step + ": " + Arrays.toString(c1) + ";"
// + Arrays.toString(c2));
allcounts[step] = c2;
if (isEqual(c1, c2)) {
for (int i = 0; i < digits.length; i++) {
System.out.print(digits[i]);
}
System.out.println(" is self-inventorying after " + step
+ " steps ");
} else if (isInvent(c2)) {
for (int i = 0; i < digits.length; i++) {
System.out.print(digits[i]);
}
System.out.println(" enters an inventory loop of length "
+ (step + 1) + " ");
} else if (isEqAll(c2, step)) {
for (int i = 0; i < digits.length; i++) {
System.out.print(digits[i]);
}
System.out.println(" enters an inventory loop of length "
+ (step - pos) + " ");
} else {
Inventorying(c2, ++step);
}
}
private static boolean isEqAll(int[] c, int step) {
// TODO Auto-generated method stub
for (int i = 0; i < step; i++) {
if (isEqual(c, allcounts[i])) {
pos = i;
return true;
}
}
return false;
}
private static boolean isEqual(int[] c1, int[] c2) {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
if (c1[i] != c2[i])
return false;
}
return true;
}
private static boolean isInvent(int[] c) {
// TODO Auto-generated method stub
int p = 0;
for (int i = 0; i < c.length; i++) {
if (c[i] != 0) {
if (p == digits.length)
return false;
if (c[i] >= 10) {
if (c[i] / 10 != (digits[p++] - 48))
return false;
if (p == digits.length)
return false;
if (c[i] % 10 != (digits[p++] - 48))
return false;
} else {
if (c[i] != (digits[p++] - 48))
return false;
}
if (p == digits.length)
return false;
if (i != (digits[p++] - 48))
return false;
}
}
if (p != digits.length)
return false;
return true;
}
}