Abdullah b. Abbas narrates: I heard the Messenger of Allah (s.a.w.a.) say, “O people! Understand that Allah has made a gate to Himself for you. If you enter from it you will be safe from Hell and the great terror.”

Then Abu Saeed al-Khudri stood up and said, “Guide us to this door so we recognize it.”

The Prophet (s.a.w.a.) replied: It is Ali b. Abi Talib, the Master of Successors, the Commander of the Believers (Ameerul Momineen), brother of the Messenger of Allah, and Allah’s caliph on all people. O people! Those of you who love to hold on to the firmest handle that never breaks off, hold on to Ali b. Abi Talib’s Wilayat. His Wilayat is my Wilayat, and obeying him is obeying me. O people! Those of you who want to know who Allah’s decisive proof is after me, know that it is Ali b. Abi Talib. O people! Those of you who want to follow Allah and His Messenger, follow Ali b. Abi Talib and the Imams from my family because they are the keepers of my knowledge.

Then Jabir b. Abdullah al-Ansari asked, “How many Imams are there?”

The Prophet (s.a.w.a.) replied: O Jabir! May Allah have mercy on you. You have asked me about the entire Islam. Their number is the number of the months. “Verily, the number of months with Allah is twelve months in Allah’s book since the day He created the heavens and the earth” (Surah Taubah (9):36). Their number is equal to the number of rivers that Allah created with a blast when Moosa b. Imran hit the rock with his staff, which is twelve. Their number is equal to the number of the chiefs of Bani Israel, as Allah says in the Quran, “We raised from among them twelve chiefs” (Surah Maidah (5):12). Therefore Jabir, the Imams are twelve. The first one is Ali b. Abi Talib and the last one is Mahdi Al-Qaim.53
Ghaayah al-Maraam p 18 Behaar al-Anwaar v 36 p 263