#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5;
const int MAX = 2 * 1e5 + 5;
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    int sum = 0;
    int a;
    int b;
    while (n--) {
      cin >> a >> b;
      sum += (b - a + 1);
    }
    if (sum % m) {
      printf("%d\n", m - sum % m);
    } else
      printf("%d\n", 0);
  }
  return 0;
}
