#include <bits/stdc++.h>
using namespace std;
int arr[100001];
int main() {
  int n, k, l, r, ans = 0;
  set<int> s;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    for (int j = l; j <= r; j++) s.insert(j);
  }
  int m = s.size();
  while (m % k != 0) {
    m++;
    ans++;
  }
  cout << ans << endl;
}
