//                                       _____   ---===--   _____
//                             .    ''                              ``  -.
//                      .  '                                                  `   .
//                   .'       _ _  _                                                  ` .
//                 /  ' '                 ''    -- .                                       `\
//                |                                      ` -.                                /\
//                 \ ______  -----   ____                       ` -.                        .'|_
//                  \\mmmNNNNNNNNNNNNmm==____`   - . _                `.`  -  .......  - '  . ' |
//                   |"""""""""""""MNNNNNNNNNNNNNNNm==___ `  -  . _     \   .---====-- .. '     |
//                   -''''''''''' `   -  __   '"""MMNNNNNNNNNNNmm===______ "|       __  | | NN) (
//                 / ::::::::::::::::::::::::::`  -  .  __ '"""MNNNNNNNNNNNNN\   (NNN*  | |__...J
//               /  ::::::::::::::::::::::::::::::::::::::::::` -  .  __  '"""\       . ''|      ` .
//             /    :::::::::::::::::::::::::::::::::::::::::::::::::::  '''' / -- ' /  |::\  \ ,)   `.
//            /    ::'   ':::::::::/:::::::::::::::::::::''::  :::   ::::::::/    ( /   |:::\   "" _. ''
//           /    ::'   :/:'  ::  /    :::::    :::     :. .::\:::::::::::::< _        /::::::\  /\
//          / .:::::::::/::...:. /     :::::....:::::::::::::::\:::::::::::::::\:`:-  /::::\::::`:N\
//         / ::/:::::::/::::::::/::::::::::::::::::::::::::::::::\:::::::\::::::|::::|::::::|::::::N\
//        | ::|:::::::|:::::::/ |:::::::::/|:::::::::/:::::\::::::\:::::::\:::::|::::|:::::::|::::::N|
//        | ::|:::::::|::::::/  |:|::::::| |::::::::| |:::::\::::::\:::::::|:::/:::::]::::::::::::::N\
//        | ::|:::::::|:::::/   |:|::::::| |::::::::|  \:::::\\:::::\::::::|::/:::::/:::::::/:::::::NN|
//        | ::|:::::::|::::|     \:\:::::|  \::::::::\ --\::::\_\::::|:::::|:/:::::/:::::::/::::::::NN|
//         |::|:::::::|::::|-'""""\:\:::|    \::\-::::\   `-|\:\  `-:|:::::|:::::::::::::/:::::|:::::N|
//         |::|::::\::\::\::|  ___ \ `-_:\     "-\ `-__  --'''___;_   |::::|:::::::::.m'\:::::::|::::N|
//          \::\::::|:::\:\--\___  `- ` '"        ""   ggmm@@@@@@@@@m ):::/:::::::. @@XXL|:::::::\:::N|
//           \::\::::\:::\mN**@@@@@mm                  \    |::TT:::/::::/N:::::/ @@@@XXL|::::::::\::NN\
//            \N:NN\:::-.::\  |::TT::|                  ` . _\:::::/ /::7NN:::/::|@@@@XXL|:::::::::\::NN\
//             \NNN\\'n-:`N\ - \::::/                             / / /NN::::/:::|@@@XXL/:::::::::::\::NN\
//              `"NN\ `-NN''\                                    _7 "  \:::::\::|LXXXLL/:::::::::::::`.NNN\
//                `-NN\ \NNNN\                                          |\::::\:|<LLLL/\:::::::::::::\::`NN\
//                    ` '|NNNN\          '                             /NNN`m- >NN|::\NN\:::::\:::::::\:::::`.
//                       |NNNNNN\                                   .i'/:::::::/NN|:::\NN\::::::\:::::::\::::::`;-.
//                      |NNNNNN/:/`.          .__.              . '  |/::/::::/NNN|:::::\N\:::::::`.:::::`Nn::::::::`m-.
//                     /NNNNN/:n/:::|` .                    . '      /::y::::/NN::N|:::\::\|:::::::::`.:::::"-.::::::::::`m.
//                    /NNN.':://::::|NNNN/` .          . '          /:::/:::/"""">NN\::::\:::`.:::::.:::::::::::`-.:::::::::)
//                  /."':::.'N::::::|NNN/::\::__`'''\              /::::::::|@@@@||NN\::::::::::`.::::` .::::::::::::`.-.::/
//              -'::::::m':/:::::::/NN/:::::|ffffrrrrmmmmmmmm@@@@@|:::::::\N|@@@| \:\N\::::::::::::`.:::::`.::::::::::::::::`..
//           /::::::m'::.':::::/::/"':::::/ /\fffffffffffff@@@@@@@|::::::::\|@@@| N`.\NN`.::::::::::::`.:::::`-.::::::::.:::::::` .
//           \::::mT:':::::::/:.':::::::,'/MMM\N.   ffffffff    @@|:::::::::\@@/.  NNNNNNNNN`m.:::::::::"=::::::"Nn:::::::`.::::::::`.
//          -':':::::::::::.':/:::::::://MMM. ""*fffffffffffffffff@\:::::::::\"   gg`  -  . NNNN\`n.::::::".::::::"N:::::::"n::::::::|
//    .- ':::.:::::::.':.':::/::::::::/- ' Mng      """"""""""      `\::|::::::\M*",,,,,,,,,,,,` .\\N`.::::|:::::::|::::::::|:::::::/
// /':::::.'::::::.'::/::::n/::::::::|,,,,,, "**++g,,________,,,,,,mmm\::\:::::::\,,,,,,,,,,,,gnNN" ` .N)::/::.:::|::::::|::N|:::.''
//|:::. '::::::n":::/":::N" \:\::::::|,,,,,,,,,,,,,**/NN/*******,,,,,,,,`.|`.:::::|,,,,,, gnN*."'       \:.:::|::::\::::::\nN::/
//.|::::::::,n":::,":::,N    \:\:::::\,,,,,,,,,,,,,,/NN/,,,,,,,,,,,,,,,,,,|   `.  |ggnN**"   /           |::::|::::::::::::\Nm:|

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';

template <typename T>
void dijkstra(int s,vector<vector<pair<int,T>>>& edges,vector<T>& dist) {
    priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
    dist[s] = 0;
    que.push(make_pair(0,s));
    while (!que.empty()) {
        pair<ll,int> p = que.top();
        que.pop();
        int v =  p.second;
        if (dist[v] < p.first) continue;
        for (pair<int,T>& e : edges[v]) {
            if (dist[e.first] > dist[v]+e.second) {
                dist[e.first] = dist[v]+e.second;
                que.push(make_pair(dist[e.first],e.first));
            }
        }
    }
}

int main() {
    int n,m = 0;
    cin >> n;
    vector<string> s(n),s_rev(n);
    vector<ll> c(n);
    unordered_map<string,int> mp1,mp2;
    for (int i = 0;i < n;++i) cin >> s[i] >> c[i];
    s_rev = s;
    for (int i = 0;i < n;++i) reverse(s_rev[i].begin(),s_rev[i].end());
    mp1[""] = 0;
    for (int i = 0;i < n;++i) for (int j = 0;j < s[i].size();++j) if (mp1.find(s[i].substr(j,s[i].size()-j)) == mp1.end()) mp1[s[i].substr(j,s[i].size()-j)] = ++m;
    for (int i = 0;i < n;++i) for (int j = 0;j < s[i].size();++j) if (mp2.find(s[i].substr(0,j+1)) == mp2.end()) mp2[s[i].substr(0,j+1)] = ++m;
    m++;
    vector<vector<pair<int,ll>>> edges(m+1);
    vector<ll> dist(m+1,1e18);
    for (auto it = mp1.begin();it != mp1.end();++it) {
        int sz1 = it->first.size();
        for (int i = 0;i < n;++i) {
            int sz2 = s[i].size();
            string t;
            bool f = true,g = true;
            for (int j = 0;j < min(sz1,sz2);++j) if (it->first[j] != s_rev[i][j]) f = false;
            if (!f) continue;
            if (sz1 < sz2) {for (int j = 0;j < sz2-sz1;++j) if (s[i][sz2-sz1-1-j] != s[i][j]) g = false;}
            else {for (int j = 0;j < sz1-sz2;++j) if (it->first[sz1-1-j] != it->first[sz2+j]) g = false;}
            if (g) edges[it->second].push_back({m,c[i]});
            else if (sz1 < sz2) edges[it->second].push_back({mp2[s[i].substr(0,sz2-sz1)],c[i]});
            else edges[it->second].push_back({mp1[it->first.substr(sz2,sz1-sz2)],c[i]});
        }
    }
    for (auto it = mp2.begin();it != mp2.end();++it) {
        int sz2 = it->first.size();
        for (int i = 0;i < n;++i) {
            int sz1 = s[i].size();
            string t;
            bool f = true,g = true;
            for (int j = 0;j < min(sz2,sz1);++j) if (it->first[sz2-1-j] != s[i][j]) f = false;
            if (!f) continue;
            if (sz1 < sz2) {for (int j = 0;j < sz2-sz1;++j) if (it->first[sz2-sz1-1-j] != it->first[j]) g = false;}
            else {for (int j = 0;j < sz1-sz2;++j) if (s[i][sz1-1-j] != s[i][sz2+j]) g = false;}
            if (g) edges[it->second].push_back({m,c[i]});
            else if (sz1 < sz2) edges[it->second].push_back({mp2[it->first.substr(0,sz2-sz1)],c[i]});
            else edges[it->second].push_back({mp1[s[i].substr(sz2,sz1-sz2)],c[i]});
        }
    }
    dijkstra<ll>(0,edges,dist);
    ll ans = dist[m];
    if (ans == 1e18) ans = -1;
    cout << ans << endl;
    return 0;


}

Measured memory_bytes: 3816