Minimum distance between compact sets in complete metric space

Let $X,Y$ be compact sets in a complete metric space. Prove that there exist $x\in X,y\in Y$ such that $d(x,y)$ is a minimum.

For any $x_0\in X$, consider the function $f(y)=d(x_0,y)$ for $y\in Y$. Since this function is continuous and the domain is a compact set, its image is also a compact set. That means there exists $y_{x_0}\in Y$ such that $f(y_{x_0})$ is a minimum. Similarly, for each $y_0\in Y$, there exists $x_{y_0}\in X$ such that $d(y_0,x_{y_0})$ is a minimum. But I can't make both variables $x,y$ free, and I haven't used completeness of the metric space yet.

$$f: X\times Y \rightarrow \mathbb{R}$$
$$f(x,y) = d(x,y)$$