2013 Qualifying problem D small progress

My depth-first search is terrible.
2010 1A problem A done