#include <bits/stdc++.h>
#define INF 0x3F3F3F3F3F3F3F3Fll
#define maxn 
using namespace std;

int len[60], N;
long long c[60], dp[2][60][30];
char S[2][60][30];

bool isPalindrome(char *s, char *t) {
    for(; s < t; ++s, --t) if(*s != *t) return false;
    return true;
}

long long dfs(const int &n, const int &l, const bool &f) {
    long long &ret = dp[f][n][l];
    if(ret != -1) return ret;
    const int &r = len[n] - 1;
    if(isPalindrome(S[f][n] + l, S[f][n] + r)) return ret = 0;
    ret = INF;
    for(int i = 1; i <= N; ++i) {
        if(r - l + 1 <= len[i]) {
            bool valid = true;
            for(int j = 0; j <= r - l; ++j) {
                if(S[f][n][j + l] != S[!f][i][j]) {
                    valid = false;
                    break;
                }
            }
            if(valid) ret = min(ret, c[i] + dfs(i, r - l + 1, !f));
        } else {
            bool valid = true;
            for(int j = 0; j < len[i]; ++j) {
                if(S[f][n][j + l] != S[!f][i][j]) {
                    valid = false;
                    break;
                }
            }
            if(valid) ret = min(ret, c[i] + dfs(n, l + len[i], f));
        }
    }
    return ret;
}

int main() {
    scanf("%d", &N);
    for(int i = 1; i <= N; ++i) {
        scanf("%s%lld", S[0][i], c + i);
        len[i] = strlen(S[0][i]);
        strcpy(S[1][i], S[0][i]);
        reverse(S[1][i], S[1][i] + len[i]);
    }
    long long Ans = INF;
    memset(dp, 0xFF, sizeof dp);
    for(int i = 1; i <= N; ++i) Ans = min(Ans, c[i] + dfs(i, 0, false));
    printf("%lld\n", Ans == INF ? -1 : Ans);
    return 0;
}

