#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bitset>
#include <sstream>
using namespace std;
using namespace __gnu_pbds;
/* clang-format off */
/* TYPES */
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define sz(x) (x).size()
/* FUNCTIONS */
#define feach(el, v) for(auto &el: v)
#define rep(i, n) for(int i=0;i<n;i++)
#define reprv(i, n) for(int i=n-1;i>=0;i--)
#define reps(i, s, e) for(int i=s;i<e;i++)
#define reprve(i, e, s) for(int i=e;i>=s;i--)
#define pb push_back
#define eb emplace_back
#define dec(zz) int (zz); cin >> zz;
#define decv(zz, n) vector<int> zz(n); for(int i=0;i<n;++i) cin >> zz[i];
#define decvn(zz, n) int n; cin >> n; vector<int> zz(n); for(int i=0;i<n;++i) cin >> zz[i];
typedef tree<int, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update> oSet;
const int MAXN = 200020;
vector<int> parent(MAXN), rnk(MAXN);
void makeSets(int n) {
rep(i, n) {
parent[i] = i;
rnk[i] = 0;
}
}
int findSet(int v) {
if (v == parent[v]) return v;
return parent[v] = findSet(parent[v]);
}
void unionSets(int a, int b) {
a = findSet(a), b = findSet(b);
if (a != b) {
if (rnk[a] < rnk[b]) swap(a, b);
parent[b] = a;
if (rnk[a] == rnk[b]) ++rnk[a];
}
}
int main() {
int n, q; cin >> n >> q;
makeSets(n);
map<ll, vector<int>> edges;
rep(i, q) {
ll compSz, cost; cin >> compSz >> cost;
int vrt;
rep(j, compSz) {
cin >> vrt;
edges[cost].push_back(vrt - 1);
}
}
ll totalCost = 0;
for (auto &[compCost, conComp]: edges) {
int first = conComp[0];
for (auto &vComp: conComp) {
if (findSet(first) != findSet(vComp)) {
totalCost += compCost;
unionSets(first, vComp);
}
}
}
rep(i, n) {
if (findSet(i) != findSet(0)) {
cout << "-1";
return 0;
}
}
cout << totalCost;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPG51bWVyaWM+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDx1bm9yZGVyZWRfc2V0PgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxmc3RyZWFtPgojaW5jbHVkZSA8ZXh0L3BiX2RzL2Fzc29jX2NvbnRhaW5lci5ocHA+CiNpbmNsdWRlIDxleHQvcGJfZHMvdHJlZV9wb2xpY3kuaHBwPgojaW5jbHVkZSA8Yml0c2V0PgojaW5jbHVkZSA8c3N0cmVhbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwoKLyogY2xhbmctZm9ybWF0IG9mZiAqLwoKLyogVFlQRVMgICovCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgcGlpIHBhaXI8aW50LCBpbnQ+CiNkZWZpbmUgcGxsIHBhaXI8bG9uZyBsb25nLCBsb25nIGxvbmc+CiNkZWZpbmUgdmkgdmVjdG9yPGludD4KI2RlZmluZSB2bGwgdmVjdG9yPGxvbmcgbG9uZz4KI2RlZmluZSB2dmkgdmVjdG9yPHZlY3RvcjxpbnQ+PgojZGVmaW5lIHZ2bGwgdmVjdG9yPHZlY3Rvcjxsb25nIGxvbmc+PgojZGVmaW5lIG1paSBtYXA8aW50LCBpbnQ+CiNkZWZpbmUgc2kgc2V0PGludD4KI2R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FyZW50W3ZdID0gZmluZFNldChwYXJlbnRbdl0pOwp9Cgp2b2lkIHVuaW9uU2V0cyhpbnQgYSwgaW50IGIpIHsKICAgIGEgPSBmaW5kU2V0KGEpLCBiID0gZmluZFNldChiKTsKICAgIGlmIChhICE9IGIpIHsKICAgICAgICBpZiAocm5rW2FdIDwgcm5rW2JdKSBzd2FwKGEsIGIpOwogICAgICAgIHBhcmVudFtiXSA9IGE7CiAgICAgICAgaWYgKHJua1thXSA9PSBybmtbYl0pICsrcm5rW2FdOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBuLCBxOyBjaW4gPj4gbiA+PiBxOwoKICAgIG1ha2VTZXRzKG4pOwoKICAgIG1hcDxsbCwgdmVjdG9yPGludD4+IGVkZ2VzOwogICAgcmVwKGksIHEpIHsKICAgICAgICBsbCBjb21wU3osIGNvc3Q7IGNpbiA+PiBjb21wU3ogPj4gY29zdDsKCiAgICAgICAgaW50IHZydDsKICAgICAgICByZXAoaiwgY29tcFN6KSB7CiAgICAgICAgICAgIGNpbiA+PiB2cnQ7CiAgICAgICAgICAgIGVkZ2VzW2Nvc3RdLnB1c2hfYmFjayh2cnQgLSAxKTsKICAgICAgICB9CiAgICB9CgogICAgbGwgdG90YWxDb3N0ID0gMDsKICAgIGZvciAoYXV0byAmW2NvbXBDb3N0LCBjb25Db21wXTogZWRnZXMpIHsKICAgICAgICBpbnQgZmlyc3QgPSBjb25Db21wWzBdOwogICAgICAgIGZvciAoYXV0byAmdkNvbXA6IGNvbkNvbXApIHsKICAgICAgICAgICAgaWYgKGZpbmRTZXQoZmlyc3QpICE9IGZpbmRTZXQodkNvbXApKSB7CiAgICAgICAgICAgICAgICB0b3RhbENvc3QgKz0gY29tcENvc3Q7CiAgICAgICAgICAgICAgICB1bmlvblNldHMoZmlyc3QsIHZDb21wKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICByZXAoaSwgbikgewogICAgICAgIGlmIChmaW5kU2V0KGkpICE9IGZpbmRTZXQoMCkpIHsKICAgICAgICAgICAgY291dCA8PCAiLTEiOwogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IHRvdGFsQ29zdDsKICAgIHJldHVybiAwOwp9