#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  while (cin >> n >> k) {
    vector<pair<int, int> > v;
    int b;
    int a;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      v.push_back(make_pair(a, b));
    }
    int x = 0;
    for (int i = 0; i < n; i++) x += (v[i].second - v[i].first) + 1;
    if (x % k == 0)
      cout << 0 << endl;
    else {
      int cc = 0;
      for (int i = 1; i <= k; i++) {
        x += 1;
        cc++;
        if (x % k == 0) {
          cout << cc << endl;
          break;
        }
      }
    }
  }
  return 0;
}
