## 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 state_base_map[(a, b, c)] = MAX_INT dp = [{} for _ in range(N)] dp[0][-1] = 0 for s_n in range(N): for prev_, cost in dp[s_n].items(): for next_n, w in next_nodes[s_n]: if (prev_, s_n, next_n) in state_base_map: 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 cost in dp[-1].values(): answer = min(answer, cost) if answer == MAX_INT: print(-1) else: print(answer) if __name__ == "__main__": main()