## https://yukicoder.me/problems/no/2477 MAX_INT = 10 ** 18 def main(): N, M = map(int, input().split()) next_nodes = [[] for _ in range(N)] for _ in range(M): u, v, w = map(int, input().split()) next_nodes[u - 1].append((v - 1, w)) K = int(input()) state_base_map = {} for _ in range(K): a, b, c = map(int, input().split()) a -= 1 b -= 1 c -= 1 if (a, b) not in state_base_map: state_base_map[(a, b)] = set() state_base_map[(a, b)].add(c) dp = [{} for _ in range(N)] dp[0][-1] = 0 for s_n in range(N): for prev_, cost in dp[s_n].items(): forbiddens = set() if (prev_, s_n) in state_base_map: forbiddens = state_base_map[(prev_, s_n)] for next_n, w in next_nodes[s_n]: if next_n in forbiddens: continue new_cost = w + cost if s_n not in dp[next_n]: dp[next_n][s_n] = MAX_INT dp[next_n][s_n] = min(dp[next_n][s_n], new_cost) answer = MAX_INT for prev, cost in dp[-1].items(): answer = min(answer, cost) if answer == MAX_INT: print(-1) else: print(answer) if __name__ == "__main__": main()