Reading Theorem counter from another file

I am preparing a two part document (two separate TEX files, two separate PDF files), where the second part is a continutation of the first part. As such, numbering of Theorems, Lemmas etc. are resumed from first document. Can anyone suggest how to read the last Theorem number from document 1, add 1 to it, and start numbering Theorems in document 2 from that number?

MWEs (copied):

doc1.tex

\documentclass{article}
\usepackage[english]{babel}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}
Let $f$ be a function whose derivative exists in every point, then $f$ is a continuous function.
\end{theorem}
\end{document}


doc2.tex

\documentclass{article}
\usepackage[english]{babel}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}
New theorem, with theorem number 2
\end{theorem}
\end{document}


Save the value from doc1

\documentclass{article}
\usepackage[english]{babel}
\newwrite\zzz
\immediate\openout\zzz=from\jobname.tex

\AtEndDocument{%
\immediate\write\zzz{\string\setcounter{theorem}{\number\value{theorem}}}
\immediate\closeout\zzz
}
\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}
Let $f$ be a function whose derivative exists in every point, then $f$ is a continuous function.
\end{theorem}
\end{document}


and use in doc2

\documentclass{article}
\usepackage[english]{babel}

\newtheorem{theorem}{Theorem}
\input{fromdoc1}

\begin{document}

\begin{theorem}
New theorem, with theorem number 2
\end{theorem}
\end{document}