Problem: Recent improvements in information and communication technology have made it possible to provide municipal service to a wider area more quickly and with less costs. Stimulated by this, and probably for saving their not sufficient funds, mayors of many cities started to discuss on mergers of their cities.

There are, of course, many obstacles to actually put the planned mergers in practice. Each city has its own culture of which citizens are proud. One of the largest sources of friction is with the name of the new city. All citizens would insist that the name of the new city should have the original name of their own city at least as a part of it. Simply concatenating all the original names would, however, make the name too long for everyday use.

You are asked by a group of mayors to write a program that finds the shortest possible name for the new city that includes all the original names of the merged cities. If two or more cities have common parts, they can be overlapped. For example, if "FUKUOKA", "OKAYAMA", and "YAMAGUCHI" cities are to be merged, "FUKUOKAYAMAGUCHI" is such a name that include all three of the original city names. Although this includes all the characters of the city name "FUKUYAMA" in this order, it does not appear as a consecutive substring, and thus "FUKUYAMA" is not considered to be included in the name.



Input

The input is a sequence of datasets. Each dataset begins with a line containing a positive integer n (n ≤ 14), which denotes the number of cities to be merged. The following n lines contain the names of the cities in uppercase alphabetical letters, one in each line. You may assume that none of the original city names has more than 20 characters. Of course, no two cities have the same name.

The end of the input is indicated by a line consisting of a zero.

Output

For each dataset, output the length of the shortest possible name of the new city in one line. The output should not contain any other characters.

Examples

Input

3


Output

16


Input

3
FUKUOKA
OKAYAMA
YAMAGUCHI
3
FUKUOKA
FUKUYAMA
OKAYAMA
2
ABCDE
EDCBA
4
GA
DEFG
CDDE
ABCD
2
ABCDE
C
14
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
FFFFF
GGGGG
HHHHH
IIIII
JJJJJ
KKKKK
LLLLL
MMMMM
NNNNN
0


Output

16
19
9
9
5
70

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;

struct acam
{
    const static int kind = 26;
    const static int len = 300;
    int fail[len];
    int next[len][kind];
    int counts[len];
    int q[len];
    int head, tail;
    int poolsize;
    int root;

    void clear();
    inline int newnode();
    int insert(char *str, int tag);
    void build_ac_automation();

} ac;

void acam::clear()
{
    memset(fail, 0, sizeof(fail));
    memset(next, 0, sizeof(next));
    memset(counts, 0, sizeof(counts));
    poolsize = 1;
    root = 1;
}

inline int acam::newnode()
{
    return ++poolsize;
}

int acam::insert(char *str, int tag)
{
    int cnt = root;
    for (int i=0; str[i]; i++)
    {
        int index = str[i] - 'A';
        if (!next[cnt][index])
            next[cnt][index] = newnode();
        cnt = next[cnt][index];
    }
    counts[cnt] |= (1<<tag);
    return cnt;
}

void acam::build_ac_automation()
{
    head = tail = 0;
    fail[root] = root;
    q[tail++] = root;
    while (head!=tail)
    {
        int cnt = q[head++];
        counts[cnt] |= counts[fail[cnt]];
        for (int i=0; i<kind; i++)
        {
            if (next[cnt][i])
            {
                counts[next[cnt][i]] |= counts[cnt];
                if (cnt == root)
                    fail[next[cnt][i]] = root;
                else
                    fail[next[cnt][i]] = next[fail[cnt]][i];
                q[tail++] = next[cnt][i];
            }
            else if (cnt == root)
                next[cnt][i] = root;
            else
                next[cnt][i] = next[fail[cnt]][i];
        }
    }
}

char str[100010];

int dp[300][1<<14];
int went[300][1<<14];
int status[30];

queue<pair<int, int> > que;

int dist[300][300];

int main()
{
    int n;
    //freopen("1.txt","r" ,stdin);
    //freopen("output.txt", "w" ,stdout);
    while (scanf("%d", &n) == 1)
    {
        if (n==0)
            break;
        ac.clear();
        for (int i=1; i<=n; i++)
        {
            scanf("%s", str);
            status[i] = ac.insert(str, i-1);
        }
        ac.build_ac_automation();
        memset(dist, -1, sizeof(dist));
        for (int i=1; i<=ac.poolsize; i++)
            for (int j=0; j<ac.kind; j++)
                dist[i][ac.next[i][j]] = 1;
        for (int i=1; i<=ac.poolsize; i++)
            dist[i][i] = 0;
        for (int k=1; k<=ac.poolsize; k++)
            for (int i=1; i<=ac.poolsize; i++)
                for (int j=1; j<=ac.poolsize; j++)
                    if (dist[i][k] != -1 && dist[k][j] != -1 )
                        if (dist[i][j] == -1 || dist[i][k] + dist[k][j] < dist[i][j] )
                            dist[i][j] = dist[i][k] + dist[k][j];
        /*
        printf("       ");
        for (int i=0; i<ac.kind; i++)
            printf("%2c ", 'a'+i);
        printf("\n");
        for (int i=1; i<=ac.poolsize; i++)
        {
            printf("%2d %2d: ", i, ac.counts[i]);
            for (int j=0; j<ac.kind; j++)
                printf("%2d ", ac.next[i][j]);
            printf("\n");
        }
        */

        /*
        memset(dp, -1, sizeof(dp));
        memset(went, 0, sizeof(went));
        */
        int finish = (1<<n) - 1;
        for (int i=1; i<=ac.poolsize; i++)
            for (int j=0; j<=finish; j++)
                dp[i][j] = -1, went[i][j] = 0;
        while (!que.empty())
        {
            que.pop();
        }

        que.push(make_pair(1, ac.counts[1]));
        /*
        for (int i=1; i<=n; i++)
            printf("%d ", status[i]);
        printf("\n");
        for (int i = 1; i<=n; i++)
        {
            printf("%d ", dist[1][status[i]]);
            for (int j=1; j<=n; j++)
                printf("%d ", dist[status[i]][status[j]]);
            printf("\n");
        }
        */

        //printf("%d\n", que.size());
        went[1][ac.counts[1]] = 1;
        dp[1][0] = 0;
        int ans = -1;
        while (!que.empty())
        {
            pair<int, int> cnt = que.front();
            went[cnt.first][cnt.second] = 0;
            que.pop();
            for (int i=1; i<=n; i++)
            {
                int nextsta = status[i];
                if (dist[cnt.first][nextsta] != -1)
                {
                    pair<int, int> nextt = make_pair(nextsta, cnt.second | ac.counts[nextsta]);
                    if (dp[nextt.first][nextt.second] == -1
                            || dp[nextt.first][nextt.second] > dp[cnt.first][cnt.second] + dist[cnt.first][nextsta])
                    {
                        dp[nextt.first][nextt.second] = dp[cnt.first][cnt.second] + dist[cnt.first][nextsta];
                        if (finish == nextt.second)
                        {
                            if(ans == -1 || ans>    dp[nextt.first][nextt.second])
                                ans = dp[nextt.first][nextt.second];
                        }

                        if (!went[nextt.first][nextt.second])
                        {
                            went[nextt.first][nextt.second] = 1;
                            que.push(nextt);
                        }
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}