#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, arr[10001], a, b, c;
  cin >> n;
  for (int i = 0; i <= n; i++) cin >> m;
  for (int j = 0; j < n; j++) arr[j] = 1000005;
  for (int j = 0; j < m; j++) {
    cin >> a >> b >> c;
    if (arr[b - 1] > c) arr[b - 1] = c;
  }
  c = 0, a = 0;
  for (int j = 0; j < n; j++) {
    if (arr[j] == 1000005)
      c++;
    else
      a += arr[j];
  }
  if (c > 1)
    cout << -1;
  else
    cout << a;
  return 0;
}
