Bhutan and neighboring Tibet experienced the spread of Buddhism which originated in the Indian subcontinent during the lifetime of Gautama Buddha. In the first millennium, the Vajrayana school of Buddhism spread to Bhutan from the southern Pala Empire of Bengal. Tibet, Bhutan, Sikkim and parts of Nepal became the vestiges of the Mahayana schools amid the decline of Buddhism in India. Bhutan also came under the influence of the Tibetan Empire. During the 16th-century, Ngawang Namgyal unified the valleys of Bhutan into a single state. Namgyal defeated three Tibetan invasions, subjugated rival religious schools, codified the "Tsa Yig" legal system, and established a government of theocratic and civil administrators. Namgyal became the first Zhabdrung Rinpoche and his successors acted as the spiritual leaders of Bhutan like the Dalai Lama in Tibet. During the 17th century, Bhutan controlled large parts of northeast India, Sikkim and Nepal; it also wielded significant influence in Cooch Behar State. Bhutan ceded the Bengal Duars to British India during the Bhutan War in the 19th century. The House of Wangchuck emerged as the monarchy and pursued closer ties with the British in the subcontinent. In 1910, a treaty guaranteed British advice in foreign policy in exchange for internal autonomy in Bhutan. The arrangement continued under a new treaty with India in 1949 in which both countries recognized each other's sovereignty. Bhutan joined the United Nations in 1971. It has since expanded relations with 55 countries, including Bangladesh, Israel, Kuwait, Brazil, Japan, Thailand, and Turkey; as well as the European Union. While dependent on the Indian military, Bhutan maintains its own military units.