How to construct XOR gate using only 4 NAND gate? xor gate, now I need to construct this gate using only 4 nand gate

a b out
0 0 0
0 1 1
1 0 1
1 1 0


the xor = (a and not b) or (not a and b), which is
\begin{split}\overline{A}{B}+{A}\overline{B}\end{split}

I know the answer but how to get the gate diagram from the formula?



EDIT

I mean intuitively, to me, I should get this one if I do it step by step followed by the definition xor = (a and not b) or (not a and b).

\begin{split}\overline{\overline{\overline{A}{B}}\cdot\overline{{A}\overline{B}}}\end{split}

and xor will be constructed with 5 nand gates (first #1 image below)



my question is more like: imagine the first person in history figure out this formula, how can he or she (the thinking process) get the 4 nand soltuion from this formula, step by step. 

\begin{split}\overline{A}{B}+{A}\overline{B}\end{split}
From that formula? It can be done. But it's easier to start with this one: (using a different notation here)

a ^ b = ~(a &amp; b) &amp; (a | b)


Ok, now what? Eventually we should derive ~(~(~(a &amp; b) &amp; a) &amp; ~(~(a &amp; b) &amp; b)) (which looks like it has 5 NANDs, but just like the circuit diagram it has a sub-expression which is used twice).

So make something that looks like ~(a &amp; b) &amp; a (and the same thing but with a b at the end) and hope that it'll stick around: (and distributes over or)

(~(a &amp; b) &amp; a) | (~(a &amp; b) &amp; b)


Pretty close now, just apply DeMorgan to turn that middle or into an and:

~(~(~(a &amp; b) &amp; a) &amp; ~(~(a &amp; b) &amp; b))


And that's it.