Split the set of points, algorithm I'm learning for the test and I can't manage with this problem:

We are given a set of $n < 1000$ points and an integer $d$. The task is to create two disjoint sets of points $A_1$ and $A_2$ (which union is given set of $n$ points) in such way that the distance (euclidean) between each pair of points from $A_i$ (for $i\in\left\{1,2\right\}$) is less or equal to $d$. If it is not possible, print $-1$.
For example, input:
$d=3$, and points:
$(5,3), \ (1,1), \ (4,2), \ (1,3), \ (5,2), \ (2,3), \ (5,1)$
we can create:
$A_1=\left\{ (2,3), \ (1,3), \ (1,1) \right\}$
$A_2=\left\{ (5,3), \ (4,2), \ (5,2), \ (5,1) \right\}$
since each pair of points from $A_i$ (for $i\in\left\{1,2\right\}$) are close enough.

I really want to know how to solve it, but no idea. Since $n$ is small, algorithm can be even $O(n^2\log n)$, but I don't know how to start. I was thinking that maybe start with counting the distance between each pair of points, then take two points with maximum distance and place them in to different sets (if their distance is greater than $d$). Then repeat this step for the rest of pairs, but the problem is how to decide where I can legally put next points. Can anyone help with this algorithm?

Expanding on Rahul Narain's hint:
Build the graph $G$ where there is an edge between every pair of points whose distance is greater than $d$. Your two sets $A_1$ and $A_2$ correspond to a partition of the vertices so that every edge in the graph goes from $A_1$ to $A_2$. This is exactly the condition that makes $G$ bipartite.
