#include <bits/stdc++.h>
using namespace std;
long long sum;
int _dfs(vector<int> &p, int d, int x, int y) {
  if (d == p.size()) {
    int n = abs(y / x);
    sum += n * (long long)(n + 1) / 2 * x;
    return y / x;
  }
  return _dfs(p, d + 1, x, y) + _dfs(p, d + 1, x * (-p[d]), y);
}
int cal(int a, int y) {
  vector<int> p;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      p.push_back(i);
      while (a % i == 0) a /= i;
    }
  }
  if (a != 1) p.push_back(a);
  sum = 0;
  return _dfs(p, 0, 1, y);
}
int count(int n, int m, int r, int c2, int p) {
  if (c2 < 1) return 0;
  int valid_n = n + 1 - abs(r);
  if (valid_n <= 0) return 0;
  c2 = min(c2, m);
  int num = cal(abs(r), c2);
  int ans = (num * (long long)(m + 1) - sum) % p;
  return ans * (long long)valid_n % p;
}
int get(int n, int m, int x, int p, int add) {
  int ret = 0;
  for (int r = -n; r <= n; r++)
    if (r * (long long)r <= x * (long long)x + add) {
      int c2 = sqrt(x * (long long)x + add - 1.0 * r * r);
      while ((c2 + 1) * (long long)(c2 + 1) + r * (long long)r <=
             x * (long long)x + add)
        c2++;
      if (r < 0 && c2 == 0) continue;
      if (r == 0 && c2 >= 1) {
        ret = (ret + (n + 1) * (long long)m) % p;
        continue;
      }
      if (r == 1 && c2 >= 0) {
        ret = (ret + n * (long long)(m + 1)) % p;
      }
      ret = (ret + count(n, m, r, c2, p)) % p;
    }
  return ret;
}
int main() {
  int n, m, l, r, p;
  while (cin >> n >> m >> l >> r >> p) {
    int t = get(n, m, r, p, 0) - get(n, m, l, p, -1);
    cout << (t % p + p) % p << endl;
  }
}
