An integer n (3 ≤ n ≤ 1,000), an integer m (1 ≤ m ≤ 2,000), an integer k (1 ≤ k ≤ 6), and an integer p (1 ≤ p ≤ n).
A list of m tuples, each consisting of three integers x_i (1 ≤ x_i ≤ n), y_i (1 ≤ y_i ≤ n), and w_i (1 ≤ w_i ≤ 1,000).
A list of k tuples, each consisting of two integers s_j (1 ≤ s_j ≤ n) and t_j (1 ≤ t_j ≤ n).

### Example Input:

```
5 6 1 1
1 3 5
1 2 2
2 3 1
3 5 4
3 4 2
4 5 3
3 5
```

### Function Signature:
Write a function f(n, m, k, p, edges, mails) that takes in the input.
```python
def f(n: int, m: int, k: int, p: int, edges: List[Tuple[int, int, int]], mails: List[Tuple[int, int]]):
    '''
    n: an integer
    m: an integer
    k: an integer
    p: an integer
    edges: a list of tuples, each containing three integers
    mails: a list of tuples, each containing two integers
    '''
```