#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;
long long n, m, l, r, p;
void add(long long &a, long long b) { a = (a + b) % p; }
vector<int> fact[100001];
long long sum(long long w, long long i) {
  long long res = 0;
  for (int S = 0; S < (int)1 << fact[i].size(); ++S) {
    long long p1 = 1;
    long long p2 = 1;
    for (int j = 0; j < (int)fact[i].size(); ++j) {
      if (S >> j & 1) {
        p2 *= -1;
        p1 *= fact[i][j];
      }
    }
    long long t = w / p1;
    res += p2 * ((w + 1) * t - p1 * (t * (t + 1) / 2));
  }
  return res;
}
long long num(long long w, long long i) {
  long long res = 0;
  for (int S = 0; S < (int)1 << fact[i].size(); ++S) {
    long long p1 = 1;
    long long p2 = 1;
    for (int j = 0; j < (int)fact[i].size(); ++j) {
      if (S >> j & 1) {
        p2 *= -1;
        p1 *= fact[i][j];
      }
    }
    res += p2 * (w / p1);
  }
  return res;
}
long long solve(long long x) {
  long long res = 0;
  long long w = m;
  for (long long i = 0; i <= n; ++i) {
    if (i * i > x) break;
    while (i * i + w * w > x) w--;
    long long t = 0;
    if (i == 0) {
      if (w >= 1) {
        t = m;
      }
    } else {
      add(t, (m - w) * num(w, i) * 2);
      add(t, sum(w, i) * 2);
      if (i == 1) add(t, m + 1);
    }
    t *= n - i + 1;
    add(res, t);
  }
  return res;
}
int main() {
  for (int i = 2; i <= 100000; ++i) {
    if (fact[i].size() == 0) {
      for (int j = i; j <= 100000; j += i) {
        fact[j].push_back(i);
      }
    }
  }
  while (cin >> n >> m) {
    cin >> l >> r >> p;
    long long ans = solve(r * r) - solve(l * l - 1);
    ans = (ans + p) % p;
    cout << ans << endl;
  }
}
