#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s[55];
int c[55];

map<string, long long> dp[2];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> s[i] >> c[i];
    }

    priority_queue<pair<pair<ll, int>, string> > q;
    long long ans = 1e18;
    for(int i=1;i<=n;i++){
        if(dp[0].find(s[i]) == dp[0].end() || (dp[0][s[i]] > c[i])){
            dp[0][s[i]] = c[i];
            q.push(make_pair(make_pair(-dp[0][s[i]], 0), s[i]));
        }
        if(dp[1].find(s[i]) == dp[1].end() || (dp[1][s[i]] > c[i])){
            dp[1][s[i]] = c[i];
            q.push(make_pair(make_pair(-dp[1][s[i]], 1), s[i]));
        }
    }

    while(!q.empty()){
        long long val = -q.top().first.first;
        int x = q.top().first.second;
        string t = q.top().second;
        q.pop();
        if(dp[x][t] != val) continue;

        int l = t.length();
        bool ok = 1;
        for(int i=0;i<l;i++){
            if(t[i] == t[l-i-1]) continue;
            ok = 0;
            break;
        }
        if(ok){
            ans = min(ans, dp[x][t]);
            continue;
        }

        if(x == 0){
            for(int i=1;i<=n;i++){
                int l1 = t.length(), l2 = s[i].length();
                int mn = min(l1, l2);
                bool ok = 1;
                for(int j=0;j<mn;j++){
                    if(t[j] == s[i][l2-j-1]) continue;
                    ok = 0;
                    break;
                }
                if(ok){
                    if(mn == l1){
                        string t2 = "";
                        for(int j=0;j<l2-mn;j++) t2 += s[i][j];

                        if(dp[1].find(t2) == dp[1].end() || (dp[1][t2] > dp[x][t] + c[i])){
                            dp[1][t2] = dp[x][t] + c[i];
                            q.push(make_pair(make_pair(-dp[1][t2], 1), t2));
                        }
                    }
                    else{
                        string t2 = "";
                        for(int j=mn;j<l1;j++) t2 += t[j];

                        if(dp[0].find(t2) == dp[0].end() || (dp[0][t2] > dp[x][t] + c[i])){
                            dp[0][t2] = dp[x][t] + c[i];
                            q.push(make_pair(make_pair(-dp[0][t2], 0), t2));
                        }
                    }
                }
            }
        }
        else{
            for(int i=1;i<=n;i++){
                int l1 = t.length(), l2 = s[i].length();
                int mn = min(l1, l2);
                bool ok = 1;
                for(int j=0;j<mn;j++){
                    if(t[l1-j-1] == s[i][j]) continue;
                    ok = 0;
                    break;
                }
                if(ok){
                    if(mn == l1){
                        string t2 = "";
                        for(int j=mn;j<l2;j++) t2 += s[i][j];

                        if(dp[0].find(t2) == dp[0].end() || (dp[0][t2] > dp[x][t] + c[i])){
                            dp[0][t2] = dp[x][t] + c[i];
                            q.push(make_pair(make_pair(-dp[0][t2], 0), t2));
                        }
                    }
                    else{
                        string t2 = "";
                        for(int j=0;j<l1-mn;j++) t2 += t[j];

                        if(dp[1].find(t2) == dp[1].end() || (dp[1][t2] > dp[x][t] + c[i])){
                            dp[1][t2] = dp[x][t] + c[i];
                            q.push(make_pair(make_pair(-dp[1][t2], 1), t2));
                        }
                    }
                }
            }
        }
    }
    if(ans == 1e18) ans = -1;
    printf("%lld\n", ans);
}

Measured memory_bytes: 3804