We have $n$ keys, each of them belonging to exactly one of $n$ locked chests. Our goal is to decide which key opens which chest. In one try we may choose a key and a chest, and check whether the chest can be opened with the key. Find the minimal number $p(n)$ with the property that using $p(n)$ tries, we can surely discover which key belongs to which chest.
1. **Labeling and Initial Setup**:
   - Label the keys as \(1, 2, \ldots, n\) and the chests as \(1, 2, \ldots, n\).
   - An \((i, j)\)-try is defined as trying to open chest \(j\) with key \(i\).

2. **Upper Bound**:
   - For \(n = 0\) and \(n = 1\), \(p(n) = 0\).
   - For \(n \geq 2\), we can determine which chest key \(1\) opens in at most \((n-1)\) tries by trying key \(1\) on each chest until it opens one.
   - Repeat this process for key \(2\), key \(3\), and so on.
   - The total number of tries is:
     \[
     p(n) \leq (n-1) + (n-2) + \cdots + 2 + 1 = \binom{n}{2}
     \]

3. **Lower Bound**:
   - The devil's strategy is to always answer 'no' to all \((i, j)\)-tries.
   - Draw an \(n \times n\) table and mark an X in cell \((i, j)\) if key \(i\) does not open chest \(j\).
   - A transversal is a set of \(n\) cells in the table such that no two cells are in the same row or column. A transversal with no X's represents a possible assignment of keys to chests.

4. **Induction Proof**:
   - **Base Case**: For \(n = 2\), it is easy to check that \(p(2) = 1\).
   - **Inductive Step**: Assume the statement is true for \(n-1\). We need to show it for \(n\).
     - If there is a column with \(n-1\) X's, delete the corresponding row and apply the induction hypothesis.
     - If each column has at least 2 cells with no X, relabel the columns so that one possible transversal is \((1,1), (2,2), \ldots, (n,n)\).
     - Suppose for \(i = 1, 2, \ldots, n\), \(a_i \neq i\) such that cell \((i, a_i)\) does not contain an X. Define \(i \to a_i\).
     - The sequence \(1 \to b_1 \to b_2 \to \ldots\) eventually forms a loop due to finiteness.
     - Replace the looped part of the transversal with the new sequence to form another transversal.

5. **Conclusion**:
   - By induction, we have shown that if there are fewer than \(\binom{n}{2}\) X's in the table, we can always find another transversal.
   - Therefore, \(p(n) \geq \binom{n}{2}\).

The final answer is \( \boxed{ p(n) = \binom{n}{2} } \).