#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
// Adjacency Matrix
int matrix[4][4];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
matrix[i][j] = 1e9;
// input
for(int i = 0; i < n; i++) {
int from, to, weight;
cin >> from >> to >> weight;
matrix[from][to] = weight;
}
// cek edge
if(matrix[0][3] != 1e9) {
// hapus edge
matrix[0][3] = 1e9;
// tambah edge
matrix[0][3] = 5;
}
// Adjacency List
vector<pair<int, int> > alist[4];
// input
for(int i = 0; i < n; i++) {
int from, to, weight;
cin >> from >> to >> weight;
alist[from].push_back(make_pair(to, weight));
}
// cek edge
for(int i = 0; i < alist[0].size(); i++)
if(alist[0][i].first == 3) {
// hapus edge
alist[0].erase(alist[0].begin()+i);
// tambah edge
alist[0].push_back(make_pair(3, 5));
}
// Edge List
// weight, from, to
vector<pair<int, pair<int, int> > > elist;
// input
for(int i = 0; i < n; i++) {
int from, to, weight;
cin >> from >> to >> weight;
elist.push_back(make_pair(weight, make_pair(from, to)));
}
// cek edge
for(int i = 0; i < elist.size(); i++) {
if(elist[i].second.first == 0
&& elist[i].second.second == 3) {
// hapus edge
elist.erase(elist.begin()+i);
// tambah edge
elist.push_back(make_pair(5, make_pair(0, 3)));
}
}
return 0;
}
/*
5
1 0 1
0 2 2
0 1 3
0 3 5
2 3 8
*/
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCWludCBuOwoJY2luID4+IG47CgkvLyBBZGphY2VuY3kgTWF0cml4CglpbnQgbWF0cml4WzRdWzRdOwoJZm9yKGludCBpID0gMDsgaSA8IDQ7IGkrKykKCQlmb3IoaW50IGogPSAwOyBqIDwgNDsgaisrKQoJCQltYXRyaXhbaV1bal0gPSAxZTk7CgkvLyBpbnB1dAoJZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewoJCWludCBmcm9tLCB0bywgd2VpZ2h0OwoJCWNpbiA+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