#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <tuple>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class Cost>
std::vector<Cost> dijkstra(const Graph<Cost>& graph,
                           const std::vector<std::pair<int, Cost>>& ss) {
    std::vector<Cost> dist(graph.size(), -1);
    MinHeap<std::pair<Cost, int>> que;
    for (auto [s, c] : ss) {
        if (dist[s] != -1 && dist[s] < c) continue;
        dist[s] = c;
        que.emplace(dist[s], s);
    }

    while (!que.empty()) {
        int v;
        Cost d;
        std::tie(d, v) = que.top();
        que.pop();
        if (d > dist[v]) continue;

        for (const auto& e : graph[v]) {
            if (dist[e.dst] != -1 &&
                dist[e.dst] <= dist[v] + e.cost) continue;
            dist[e.dst] = dist[v] + e.cost;
            que.emplace(dist[e.dst], e.dst);
        }
    }

    return dist;
}

using lint = long long;

void solve() {
    int n;
    std::cin >> n;

    std::vector<std::string> ss(n);
    std::vector<lint> cs(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> ss[i] >> cs[i];
    }

    std::map<std::string, int> stoid;
    int m = 0;
    for (const auto& s : ss) {
        int k = s.length();
        for (int l = 0; l < k; ++l) {
            for (int len = 0; l + len <= k; ++len) {
                auto t = s.substr(l, len);

                for (int q = 0; q < 2; ++q) {
                    std::reverse(t.begin(), t.end());
                    if (stoid.count(t)) continue;
                    stoid[t] = m++;
                }
            }
        }
    }

    auto enc = [&](int v, int q) { return v + q * m; };

    Graph<lint> graph(m * 2);
    for (int i = 0; i < n; ++i) {
        auto s = ss[i];

        for (int q = 0; q < 2; ++q) {
            std::reverse(s.begin(), s.end());

            for (const auto& [t, u] : stoid) {
                if (s.length() <= t.length()) {
                    if (t.substr(0, s.length()) != s) continue;
                    auto nt = t.substr(s.length());

                    int v = stoid[nt];
                    graph.span(true, enc(u, q), enc(v, q), cs[i]);

                } else {
                    if (s.substr(0, t.length()) != t) continue;
                    auto nt = s.substr(t.length());

                    int v = stoid[nt];
                    graph.span(true, enc(u, q), enc(v, 1 - q), cs[i]);
                }
            }
        }
    }

    std::vector<std::pair<int, lint>> starts;
    for (int i = 0; i < n; ++i) {
        starts.emplace_back(enc(stoid[ss[i]], 0), cs[i]);
    }
    auto ds = dijkstra(graph, starts);

    lint ans = -1;
    for (const auto& [t, uu] : stoid) {
        if (t != std::string(t.rbegin(), t.rend())) continue;

        for (int q = 0; q < 2; ++q) {
            int u = enc(uu, q);
            if (ds[u] != -1 && (ans == -1 || ans > ds[u])) ans = ds[u];
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}


Measured memory_bytes: 5052