The fewest number of jars opened to put the lids back on correctly is one. Open and take a bolt/nut from the jar labeled “both”. (You know this jar does not contain both sized bolts/nuts because all the lids are incorrect.) This particular bolt/nut you draw is actually a “small”; so you know this jar must contain all the “small” bolts/nuts.
Find the jar that says “small” on the lid and swap those two lids. You know the “small” jar is now labelled correctly. You haven’t yet touched the “medium” jar, but you know its lid is labeled incorrectly. Here, switch the lids on the “medium” and “both” jars. Done!