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;
}

alias P = Tuple!(string, "l", string, "r");

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[P][P] G;
    void add_path(P p) {
        foreach (i; 0..N) {
            auto c = CS[i];
            string s, t;
            if (p.r == "") {
                s = p.l;
                t = SS[i];
            } else {
                s = SS[i];
                t = p.r;
            }
            auto len = min(s.length, t.length);
            size_t l, r = t.length-1;
            P q;
            foreach (d; 0..len) {
                if (s[l+d] != t[r-d]) goto ng;
            }
            if (s.length > t.length) {
                q.l = s[len..$];
            } else if (s.length < t.length) {
                q.r = t[0..$-len];
            }
            if (q.l.length == 1) q.l = "";
            if (q.r.length == 1) q.r = "";
            if (q in G && p in G[q]) {
                G[q][p] = min(G[q][p], c);
            } else {
                G[q][p] = c;
            }
            ng:
        }
    }
    auto E = P("", "");
    foreach (i; 0..N) {
        auto s = SS[i];
        auto c = CS[i];
        if (is_palindrome(s)) {
            auto p = P(s, "");
            if (E in G && p in G[E]) {
                G[E][p] = min(G[E][p], c);
            } else {
                G[E][p] = c;
            }
        }
        foreach (j; 1..s.length+1) {
            add_path(P(s[$-j..$], ""));
            add_path(P("", s[0..j]));
        }
    }

    alias S = Tuple!(P, "p", long, "c");
    long[P] DP;
    DP[E] = 0;
    auto Q = heapify!"a.c > b.c"([S(E, 0)]);
    while (!Q.empty) {
        auto p = Q.front.p;
        auto c = Q.front.c;
        Q.popFront();
        if (p in DP && DP[p] < c) continue;
        if (p !in G) continue;
        foreach (q, d; G[p]) if (q !in DP || DP[q] > c + d) {
            DP[q] = c + d;
            Q.insert(S(q, c + d));
        }
    }

    long r = long.max;
    foreach (i, s; SS) {
        auto p = P(s, "");
        if (p in DP) r = min(r, DP[p] + CS[i]);
    }
    writeln(r == long.max ? -1 : r);
}