## 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 = [[MAX_INT, MAX_INT] for _ in range(N)] prev = [[-2, -2] for _ in range(N)] dp[0][0] = 0 prev[0][0] = -1 for s_n in range(N): for idx in range(2): cost = dp[s_n][idx] prev_n = prev[s_n][idx] for next_n, w in next_nodes[s_n]: if (prev_n, s_n, next_n) in state_base_map: continue new_cost = w + cost if dp[next_n][0] > new_cost: dp[next_n][1] = dp[next_n][0] dp[next_n][0] = new_cost prev[next_n][1] = prev[next_n][0] prev[next_n][0] = s_n elif dp[next_n][1] > new_cost: dp[next_n][1] = new_cost prev[next_n][1] = s_n answer = MAX_INT for cost in dp[-1]: answer = min(answer, cost) if answer == MAX_INT: print(-1) else: print(answer) if __name__ == "__main__": main()