#include <bits/stdc++.h>

#define endl '\n'
#define pb push_back
#define mod 1000000007
typedef int integer;
#define int long long
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 0
#endif
using namespace std;

template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cerr << name << ": " << arg1 << endl;
}

template<typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    const char *comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << ": " << arg1 << " |";
    __f(comma + 1, args...);
}

int powmod(int a, int b) {
    int res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1)res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const integer NN = (int) 5e5 + 2;
int n, m, k;

string s[55];
int c[55];

bool pal(string s) {
    for (int i = 0; i < s.length() / 2; i++) {
        if (s[i] != s[s.length() - i - 1]) return false;
    }
    return true;
}

bool endswith(string &a, string &b) {
    int x = a.length() - 1, y = b.length() - 1;
    while (x >= 0 && y >= 0) {
        if (a[x] != b[y]) return false;
        x--;
        y--;
    }
    return true;
}

bool startswith(string &a, string &b) {
    int x = 0, y = 0;
    while (x < a.length() && y < b.length()) {
        if (a[x] != b[y]) return false;
        x++;
        y++;
    }
    return true;
}

int solve(int a, int b) {
    if (pal(s[a] + s[b])) return c[a] + c[b];

    priority_queue<pair<int, pair<string, string>>> pq;
    pq.push({-1 * (c[a] + c[b]), {s[a], s[b]}});
    set<pair<string, string>> vis;
    while (!pq.empty()) {
        int cost = pq.top().first * -1;
        string x = pq.top().second.first;
        string y = pq.top().second.second;
        pq.pop();
        int len = min(x.length(), y.length());
        string tmp = y;
        reverse(tmp.begin(), tmp.end());
        tmp = tmp.substr(0, len);
        if (x.substr(0, len) != tmp) continue;
        if (pal(x + y)) return cost;

        if (vis.count({x, y})) continue;
        vis.insert({x, y});



//        string pre;
        if (x.length() == y.length()) {
            for (int i = 0; i < n; i++) {
                pq.push({(cost + c[i]) * -1, {x, s[i] + y}});
            }
            for (int i = 0; i < n; i++) {
                pq.push({(cost + c[i]) * -1, {x + s[i], y}});
            }
        } else if (x.length() > y.length()) {
            for (int i = 0; i < n; i++) {
                pq.push({(cost + c[i]) * -1, {x, s[i] + y}});
            }

        } else {
//            int rem = (int) y.length() - len;
//            pre = y.substr(0, (int) len);
//            reverse(pre.begin(), pre.end());
            for (int i = 0; i < n; i++) {
//                if (startswith(s[i], pre)) {
                pq.push({(cost + c[i]) * -1, {x + s[i], y}});
//                }
            }
        }
    }
    return 1e18;
}

integer main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 0; i < n; i++) cin >> s[i] >> c[i];

    int ans = 1e18;
    for (int i = 0; i < n; i++) {
        if (pal(s[i])) {
            ans = min(ans, c[i]);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ans = min(ans, solve(i, j));
        }
    }
    if (ans > 1e17) cout << -1;
    else cout << ans;


}


Measured memory_bytes: 3824