Most likely caused by lack of fitness. And when I say lack of fitness, I mean everything from not being physically fit enough for the distance, terrain, and weather. Staying hydrated and keeping up with required nutrients are part of being physically fit and prepared for your ride. Everything everyone posted above is valuable information. However, you may find that you can still cramp even if you have hydration, electrolytes, and nutrition under control. That would simply point to lack of fitness. Just keep riding and make sure you have enough to eat and drink for the conditions.