#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  while (cin >> n >> k) {
    int **arr = new int *[n];
    for (int i = 0; i < n; i++) arr[i] = new int[2];
    int counter = 0, covred = 0;
    for (int i = 0; i < n; i++) {
      cin >> arr[i][0] >> arr[i][1];
      covred += arr[i][1] - arr[i][0] + 1;
    }
    while (covred % k != 0) {
      counter++, covred++;
    }
    cout << counter << endl;
    for (int i = 0; i < n; i++) delete[] arr[i];
    delete[] arr;
  }
}
