SEOUL, May 8 (Reuters) - Kim Ki Nam, one of the longest-serving North Korean officials who served all three generations of its leaders cementing their political legitimacy and heading the propaganda apparatus for the dynastic state, has died, official media said on Wednesday.

North Korea leader Kim Jong Un visited the bier of Kim at 2 a.m. on Wednesday to pay tribute "with bitter grief over the loss of a veteran revolutionary who had remained boundlessly loyal" to the country until the very end, it said.

Kim died on Tuesday at the age of 94, official KCNA news agency said.

He was part of a core group of loyal officials who had worked to sustain the three generations of Kims by solidifying their legitimacy carrying the blood line of a revolutionary leader who founded the state in 1945.

"He devoted himself to backing the victorious journey of building a powerful socialist country by maintaining the powerful offensive and fresh development in the new era in all spheres of the Party's ideological work," KCNA said.

Kim is one of the very few North Korean officials to have visited the South, leading a funeral delegation in 2009 after the death of President Kim Dae-jung who opened an era of reconciliation with Pyongyang with his "Sunshine Policy."

He moved to the forefront of the North's propaganda machine, becoming its deputy head in 1966 and then chief in 1985 during state founder Kim Il Sung's rule, according to South Korean government data. He retired in 2017.

Kim exerted tremendous influence on policy and personnel and was a key architect of the ruling Workers' Party's political foundation, according to North Korea expert Michael Madden at the Stimson Center.

He was particularly close to Kim Jong-il, the father of the current leader who died in 2011, and was believed to be his "drinking buddy," Madden said citing sources.

Photo from gettyimages