It is not mandatory to keep the distance at 5 meters, but it is strongly recommended to control it within 5 meters—too long a distance will affect the user experience:
Hot water loses heat in the pipeline; the farther the distance, the lower the water temperature, and this is more noticeable in winter;
Cold water will remain in the pipeline. Every time you use hot water, you have to drain the cold water first before hot water comes out, which wastes water and requires waiting.
If the distance has already exceeded 5 meters, it is recommended to insulate the hot water pipeline, which can reduce heat loss.