Your code looks to be somewhat similar to the the shortest path recipe in GremlinDocs:
http://gremlindocs.com/#recipes/shortest-path
You might want to read that section in full as you are evaluating both
directions of a vertex which has consequences and has been shown to be better handled with the store/except pattern.
Once you have all the paths need to just select the shortest one from the returned list. In pure Java that's a bit more work than with Groovy, but it basically boils down to a sort on the path length and then choosing the shortest one. In groovy that would be something like:
gremlin> g.v(1).out.loop(1){it.object.id != "3" && it.loops < 6}.path.sort{a,b->a.size()<=>b.size()}
==>[v[1], v[3]]
==>[v[1], v[4], v[3]]
Looking at that made me wonder if you could always just pop-off the first item in the pipeline as it would be the earliest path detected and hence the shortest:
gremlin> g.v(1).out.loop(1){it.object.id != "3" && it.loops < 6}.path[0]
==>[v[1], v[3]]
You might want to experiment with that a bit, but it sounds like a promising theory that would allow you to short-circuit the pipeline if you just needed the first shortest path detected.