Does the risk of developing dementia increase with the number of episodes in patients with depressive disorder and in patients with bipolar disorder?

Does the risk of developing dementia increase with the number of episodes in patients with depressive disorder and in patients with bipolar disorder?