<p>You are given a list of <code>preferences</code> for <code>n</code> friends, where <code>n</code> is always <strong>even</strong>.</p>
<p>For each person <code>i</code>, <code>preferences[i]</code> contains a list of friends <strong>sorted</strong> in the <strong>order of preference</strong>. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from <code>0</code> to <code>n-1</code>.</p>
<p>All the friends are divided into pairs. The pairings are given in a list <code>pairs</code>, where <code>pairs[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> denotes <code>x<sub>i</sub></code> is paired with <code>y<sub>i</sub></code> and <code>y<sub>i</sub></code> is paired with <code>x<sub>i</sub></code>.</p>
<p>However, this pairing may cause some of the friends to be unhappy. A friend <code>x</code> is unhappy if <code>x</code> is paired with <code>y</code> and there exists a friend <code>u</code> who is paired with <code>v</code> but:</p>