A Lazy Bag of Bread
JavaScript (ES6), 114 bytes
Saved 1 byte thanks to @Oliver
Takes input as an array of characters. Outputs a comma-separated string where the first value is the total time and the next ones describe the path.
a=>(b=g=(r,s=o='',c,p)=>s[c>b|4]?o=(b=c)+r:a.map((v,i)=>s.match(v)||(d=p<i?i-p:p-i)<2||g([r,i],s+v,~~c+d,i))&&o)``
Try it online!
Commented
a => ( // a[] = input array
b = // b = best score so far (initially a non-numeric value)
g = ( // g = recursive function taking:
r, // r = path
s = // s = string of collected loaves of bread
o = '', // o = final output
c, // c = current cost
p // p = index of the last visited shelf
) => //
s[c > b // if the final cost is not greater than our best score
| 4] ? // and we've successfully collected 5 loaves of bread:
o = (b = c) + r // update the current output and the best score
: // else:
a.map((v, i) => // for each loaf of bread v at shelf i in a[]:
s.match(v) || // if we've already collected this kind of bread
(d = // or the distance d
p < i ? i - p : p - i // defined as the absolute value of p - i
) < 2 || // is less than 2: stop recursion
g( // otherwise, do a recursive call to g() with:
[r, i], // r updated with the index of the current shelf
s + v, // s updated with the current loaf of bread
~~c + d, // c updated with the last distance
i // i as the index of the last shelf
) // end of recursive call
) // end of map()
&& o // return the current output
)`` // initial call to g() with r = [""]