As @aneee004 said, distance takes time proportional to the distance between the iterators, which is proportional to the number of elements in the set, which is \mathcal{O}(n + m).
It manually moves the iterator with it++ until it gets to the second provided iterator. If you haven’t noticed, you cannot write it2 = it + 3 or s.begin() < s.end() with set iterators.
These are possible with vector iterators, because they are random access iterators.