Which factor contributed most to the influence of Korea and China on medieval Japan?

They invaded Japan.
They traded with Japan.
Their leaders traveled to Japan.
They were close geographically to Japan.