import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

bool is_palindrome(string s)
{
    auto len = s.length;
    foreach (d; 0..len/2) if (s[d] != s[$-d-1]) return false;
    return true;
}

void main()
{
    auto N = readln.chomp.to!int;
    string[] SS;
    long[] CS;
    foreach (_; 0..N) {
        auto sc = readln.split;
        SS ~= sc[0];
        CS ~= sc[1].to!long;
    }
    long[string][string] G;
    void add_path(string s) {
        foreach (i; 0..N) {
            auto c = CS[i];
            auto t = SS[i];
            auto len = min(s.length, t.length);
            size_t l, r = t.length-1;
            string u = "";
            foreach (d; 0..len) {
                if (s[l+d] != t[r-d]) goto ng;
            }
            if (s.length > t.length) {
                u = s[len..$];
            } else if (s.length < t.length) {
                u = t[0..$-len];
            }
            if (u.length == 1) u = "";
            if (u in G && s in G[u]) {
                G[u][s] = min(G[u][s], c);
            } else {
                G[u][s] = c;
            }
            ng:
        }
    }
    foreach (i; 0..N) {
        auto s = SS[i];
        auto c = CS[i];
        if (is_palindrome(s)) {
            if ("" in G && s in G[""]) {
                G[""][s] = min(G[""][s], c);
            } else {
                G[""][s] = c;
            }
        }
        foreach (j; 1..s.length) {
            add_path(s[0..j]);
            add_path(s[$-j-1..$]);
        }
    }

    alias S = Tuple!(string, "s", long, "c");
    long[string] DP;
    DP[""] = 0;
    auto Q = heapify!"a.c > b.c"([S("", 0)]);
    while (!Q.empty) {
        auto s = Q.front.s;
        auto c = Q.front.c;
        Q.popFront();
        if (s in DP && DP[s] < c) continue;
        if (s !in G) continue;
        foreach (t, d; G[s]) if (t !in DP || DP[t] > c + d) {
            DP[t] = c + d;
            Q.insert(S(t, c + d));
        }
    }
    long r = long.max;
    foreach (i, s; SS) if (s in DP) r = min(r, DP[s] + CS[i]);
    writeln(r == long.max ? -1 : r);
}