#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAXN 30
#define BUFLEN 80
#define STRLEN 30
int graph[MAXN][MAXN];
int hasChar[STRLEN];
int Char[STRLEN];
int len;
int vis[STRLEN];
int max;
int result[STRLEN];
int ans[STRLEN];
int cmp_char(const void *a, const void *b);
void dfs(int cur);
int main()
{
int i;
char buf[BUFLEN];
int start, end;
#ifndef ONLINE_JUDGE
freopen("d:\\uva_in.txt", "r", stdin);
#endif
while (scanf("%s", buf) && buf[0] != '#') {
memset(hasChar, -1, sizeof(hasChar));
memset(graph, 0, sizeof(graph));
memset(vis, 0, sizeof(vis));
max = 100;
i = 0;
len = 0;
while (i < strlen(buf)) {
start = buf[i] - 'A';
if (hasChar[start] == -1) {
hasChar[start] = 1;
Char[len++] = start;
}
i += 2;
while (isalpha(buf[i])) {
end = buf[i] - 'A';
if (hasChar[end] == -1) {
hasChar[end] = 1;
Char[len++] = end;
}
graph[start][end] = graph[end][start] = 1;
i++;
}
i++;
}
qsort(Char, len, sizeof(int), cmp_char);
dfs(0);
for (i = 0; i < len; i++) {
printf("%c ", ans[i] + 'A');
}
printf("-> %d\n", max);
}
return 0;
}
int cmp_char(const void *a, const void *b)
{
const int *tmp_a = (const int *)a;
const int *tmp_b = (const int *)b;
return *tmp_a - *tmp_b;
}
void dfs(int cur)
{
int i, j;
int temp;
if (cur == len) {
temp = 0;
for (i = 0; i < cur - 1; i++) {
for (j = i + 1; j < cur; j++) {
if (graph[result[i]][result[j]] && (j - i) > temp)
temp = j - i;
}
}
if (temp < max) {
max = temp;
memcpy(ans, result, sizeof(result));
}
} else {
for (i = 0; i < len; i++) {
if (!vis[i]) {
vis[i] = 1;
result[cur] = Char[i];
dfs(cur + 1);
vis[i] = 0;
}
}
}
}